import { b as getNexusDb, a as getBrainDb, g as getTasksDb } from './connections2-D22b0xbS.js';
import './dual-scope-db-ATrwq0LH.js';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './dist4-CiQ5cizO.js';
import './adapters-D2gEs_5L.js';
import './registry2-BSyCTA3q.js';
import './data-accessor-CVbz8X_7.js';
import './tasks-CnwUAUXf.js';
import './conduit-Ca_OFWR_.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import 'node:fs/promises';
import 'jsonc-parser';
import '@cleocode/cant';
import 'node:util';
import 'node:buffer';
import 'child_process';
import './branch-lock-qP1EkZ7p.js';
import './src2-DK8DKufK.js';
import 'llmtxt/identity';
import './sessions-j73dkbCI.js';
import './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import './session-manifest-ZHrj54LR.js';
import './storage-B9UA3BHR.js';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './data-safety-central-vmfu_Y4J.js';
import './sqlite-data-accessor-C59wZ0zp.js';
import './cross-db-cleanup-C_FLyd-6.js';
import './query-ops-BDMaTdYw.js';
import 'node:net';
import './src3-CMSGYxDI.js';
import './local-model-fit2-J1xCIhC_.js';
import './credential-pool-B04qO8at.js';
import './catalog-model-resolver2-Cexk2TN0.js';
import './role-resolver-DF9BQ_U6.js';
import './identity2-DeAB5RXE.js';
import './role-executor-C4-3bmgC.js';
import 'node:stream';
import 'crypto';
import 'os';
import 'node:http2';
import 'fs/promises';
import './models-catalog-DG3aPok8.js';
import './catalog-resolver2-c0qfF34B.js';
import './providers-CmIcBfg-.js';
import './brain-maintenance-bSR6GwAG.js';
import './graph-auto-populate2-DYsUNGSL.js';
import './temporal-supersession-BZ46j30w.js';
import './typed-query-l1D_QElt.js';
import './llm-backend-resolver-BPbvttS4.js';
import '@ai-sdk/provider-utils';
import './observer-reflector2-D8nxuglX.js';
import './learnings2-2m2p7lzT.js';
import './memory-accessor2-BzlgmY-Z.js';
import './quality-scoring-CCPuRQmJ.js';
import './patterns2-DYKfsbwN.js';
import './graph-memory-bridge2-C7Y32Mk4.js';
import './sigil2-DftKm7Um.js';
import './services-Dlf3Gefq.js';
import './audit4-S87oN-BC.js';
import 'process';
import './cleo-home-BCqJ5rg1.js';

//#region src/routes/+page.server.ts
/**
* Home page ("Mission Control") server load.
*
* Supplies everything the Wave 1E root dashboard renders without needing
* client-side fetches on first paint:
*
*   - Substrate stats (nexus, brain, tasks) — driven from their DBs.
*   - A 24-hour activity histogram bucketed by hour (brain observations +
*     tasks completions). Feeds the `Sparkline` on the dashboard.
*   - A 20-row "recent activity" cross-feed (decisions, observations,
*     task completions) ordered by timestamp.
*   - Active session count — populates the `LIVE` indicator.
*
* All queries are wrapped in `try / catch` so a corrupt or missing DB
* degrades gracefully to null arrays instead of 500ing the page.
*
* @task T990
* @wave 1E
*/
var TWENTY_FOUR_HOURS_MS = 1440 * 60 * 1e3;
function formatCount(n) {
	if (n >= 1e3) return `${(n / 1e3).toFixed(1)}k`;
	return String(n);
}
/**
* Build 24 hourly buckets from `now - 23h` → `now`.
*/
function buildBuckets(now) {
	const buckets = [];
	const startHour = now - 1380 * 60 * 1e3;
	const hourStart = (t) => /* @__PURE__ */ new Date(t - t % (3600 * 1e3));
	for (let i = 0; i < 24; i++) {
		const ts = hourStart(startHour + i * 60 * 60 * 1e3);
		buckets.push({
			hourStart: ts.toISOString(),
			total: 0,
			observations: 0,
			completions: 0
		});
	}
	return buckets;
}
function bucketIndex(iso, now) {
	try {
		const delta = now - new Date(iso).getTime();
		if (delta < 0 || delta > TWENTY_FOUR_HOURS_MS) return -1;
		const hoursAgo = Math.floor(delta / (3600 * 1e3));
		return Math.max(0, 23 - hoursAgo);
	} catch {
		return -1;
	}
}
var load = ({ locals }) => {
	const now = Date.now();
	const cutoff = new Date(now - TWENTY_FOUR_HOURS_MS).toISOString();
	let nexusStats = null;
	let brainStats = null;
	let tasksStats = null;
	let observationCount = null;
	let decisionCount = null;
	let activeSessions = 0;
	const activity24h = buildBuckets(now);
	const recentActivity = [];
	try {
		const nexus = getNexusDb();
		if (nexus) {
			const nodeRow = nexus.prepare("SELECT COUNT(*) AS cnt FROM nexus_nodes").get();
			const relRow = nexus.prepare("SELECT COUNT(*) AS cnt FROM nexus_relations").get();
			nexusStats = [{
				value: formatCount(nodeRow.cnt),
				label: "Symbols"
			}, {
				value: formatCount(relRow.cnt),
				label: "Relations"
			}];
		}
	} catch {}
	try {
		const brain = getBrainDb(locals.projectCtx);
		if (brain) {
			const nodeRow = brain.prepare("SELECT COUNT(*) AS cnt FROM brain_page_nodes").get();
			const obsRow = brain.prepare("SELECT COUNT(*) AS cnt FROM brain_observations").get();
			brainStats = [{
				value: formatCount(nodeRow.cnt),
				label: "Nodes"
			}, {
				value: formatCount(obsRow.cnt),
				label: "Observations"
			}];
			observationCount = obsRow.cnt;
			try {
				decisionCount = brain.prepare("SELECT COUNT(*) AS cnt FROM brain_decisions").get().cnt;
			} catch {}
			try {
				const recent = brain.prepare("SELECT id, title, created_at FROM brain_observations WHERE created_at >= ? ORDER BY created_at DESC LIMIT 500").all(cutoff);
				for (const row of recent) {
					const idx = bucketIndex(row.created_at, now);
					if (idx >= 0) {
						const bucket = activity24h[idx];
						if (bucket) {
							bucket.observations++;
							bucket.total++;
						}
					}
				}
				for (const row of recent.slice(0, 6)) recentActivity.push({
					kind: "observation",
					id: row.id,
					title: row.title ?? "(observation)",
					detail: null,
					timestamp: row.created_at
				});
			} catch {}
			try {
				const decisions = brain.prepare("SELECT id, title, created_at FROM brain_decisions ORDER BY created_at DESC LIMIT 6").all();
				for (const row of decisions) recentActivity.push({
					kind: "decision",
					id: row.id,
					title: row.title,
					detail: null,
					timestamp: row.created_at
				});
			} catch {}
		}
	} catch {}
	try {
		const tasks = getTasksDb(locals.projectCtx);
		if (tasks) {
			const taskRow = tasks.prepare("SELECT COUNT(*) AS cnt FROM tasks").get();
			const epicRow = tasks.prepare("SELECT COUNT(*) AS cnt FROM tasks WHERE type = 'epic'").get();
			tasksStats = [{
				value: formatCount(taskRow.cnt),
				label: "Tasks"
			}, {
				value: formatCount(epicRow.cnt),
				label: "Epics"
			}];
			try {
				const recent = tasks.prepare("SELECT id, title, completed_at FROM tasks WHERE completed_at IS NOT NULL AND completed_at >= ? ORDER BY completed_at DESC LIMIT 500").all(cutoff);
				for (const row of recent) {
					const idx = bucketIndex(row.completed_at, now);
					if (idx >= 0) {
						const bucket = activity24h[idx];
						if (bucket) {
							bucket.completions++;
							bucket.total++;
						}
					}
				}
				for (const row of recent.slice(0, 6)) recentActivity.push({
					kind: "task-done",
					id: row.id,
					title: row.title,
					detail: null,
					timestamp: row.completed_at
				});
			} catch {}
			try {
				const created = tasks.prepare("SELECT id, title, created_at FROM tasks WHERE created_at >= ? ORDER BY created_at DESC LIMIT 6").all(cutoff);
				for (const row of created) recentActivity.push({
					kind: "task-created",
					id: row.id,
					title: row.title,
					detail: null,
					timestamp: row.created_at
				});
			} catch {}
			try {
				activeSessions = tasks.prepare("SELECT COUNT(*) AS cnt FROM sessions WHERE status = 'active'").get().cnt;
			} catch {
				activeSessions = 0;
			}
		}
	} catch {}
	recentActivity.sort((a, b) => a.timestamp < b.timestamp ? 1 : -1);
	return {
		nexusStats,
		brainStats,
		tasksStats,
		activity24h,
		recentActivity: recentActivity.slice(0, 20),
		activeSessions,
		observationCount,
		decisionCount,
		projectName: locals.projectCtx.name,
		projectPath: locals.projectCtx.projectPath
	};
};

var _page_server_ts = /*#__PURE__*/Object.freeze({
	__proto__: null,
	load: load
});

const index = 2;
let component_cache;
const component = async () => component_cache ??= (await import('./_page.svelte-Clbkk1gm.js')).default;
const server_id = "src/routes/+page.server.ts";
const imports = ["_app/immutable/nodes/2.y5jKpHiR.js","_app/immutable/chunks/DyGQ_VXC.js","_app/immutable/chunks/DrKWWDpL.js","_app/immutable/chunks/CQCxd1p9.js","_app/immutable/chunks/wI_OOyoh.js","_app/immutable/chunks/B9psmq5-.js","_app/immutable/chunks/JPJd4fEI.js","_app/immutable/chunks/CEqOYim7.js","_app/immutable/chunks/CqF5Lomf.js"];
const stylesheets = ["_app/immutable/assets/HeroHeader.ByXNeezk.css","_app/immutable/assets/Sparkline.DVOptay1.css","_app/immutable/assets/StatBlock.DX6OXz7z.css","_app/immutable/assets/Tabs.CU9_rsWl.css","_app/immutable/assets/ui.CH-9N9-0.css","_app/immutable/assets/2.Cnug3B4a.css"];
const fonts = [];

export { component, fonts, imports, index, _page_server_ts as server, server_id, stylesheets };
//# sourceMappingURL=2-Bn-bcReF.js.map
