import { d as getDbStatus, b as getNexusDb, a as getBrainDb, g as getTasksDb, c as getConduitDb, e as getAgentRegistryDb } from './connections2-D22b0xbS.js';
import { json } from '@sveltejs/kit';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import './dual-scope-db-ATrwq0LH.js';
import './paths-2M_UxD_H.js';
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 '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/api/health/+server.ts
/**
* Health check endpoint for the CLEO Studio server.
*
* GET /api/health
*   {
*     ok, service, version,          // identity
*     checkedAt, uptime,             // time
*     databases: {
*       nexus: { available, rowCount, schemaVersion, path },
*       brain: { … },
*       tasks: { … },
*       conduit: { … },
*       agent-registry: { … },
*     }
*   }
*
* Version is derived at runtime from `packages/studio/package.json`
* (T990 audit fix — was hardcoded as `2026.4.47`). Row counts + schema
* versions are queried on every request; all SQLite calls are guarded
* with `try/catch` so a corrupted DB cannot crash the endpoint.
*
* @task T990
* @wave 1E
*/
/**
* Resolve and cache the Studio package version. Reads
* `packages/studio/package.json` once per process via an eagerly-run
* IIFE so we never repeat file I/O on every `/api/health` hit.
*
* Falls back to `'unknown'` if the file cannot be parsed (e.g. during
* a broken build). The fallback is explicit rather than silent so an
* operator can diagnose a wedged deploy.
*/
var pkgVersion = (() => {
	try {
		const pkgPath = path.resolve(fileURLToPath(new URL("../../../../package.json", import.meta.url)));
		const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
		return typeof pkg.version === "string" ? pkg.version : "unknown";
	} catch {
		return "unknown";
	}
})();
/** Cache the process start time so uptime is relative to boot, not request. */
var bootedAt = Date.now();
/** Query `SELECT COUNT(*)` for the given table, swallowing schema errors. */
function safeCount(db, table) {
	try {
		return db.prepare(`SELECT COUNT(*) AS cnt FROM ${table}`).get()?.cnt ?? null;
	} catch {
		return null;
	}
}
/**
* Attempt to read `PRAGMA user_version` — standard SQLite convention
* for schema versioning. Returns a string for JSON consistency
* (SQLite returns int, but some tools prefer `"7"` over `7`).
*/
function safeSchemaVersion(db) {
	try {
		const value = db.prepare("PRAGMA user_version").get()?.user_version;
		return typeof value === "number" ? String(value) : null;
	} catch {
		return null;
	}
}
var GET = ({ locals }) => {
	const dbStatus = getDbStatus(locals.projectCtx);
	const nexusReport = {
		available: dbStatus.nexus,
		rowCount: null,
		schemaVersion: null,
		path: dbStatus.nexusPath
	};
	const brainReport = {
		available: dbStatus.brain,
		rowCount: null,
		schemaVersion: null,
		path: dbStatus.brainPath
	};
	const tasksReport = {
		available: dbStatus.tasks,
		rowCount: null,
		schemaVersion: null,
		path: dbStatus.tasksPath
	};
	const conduitReport = {
		available: dbStatus.conduit,
		rowCount: null,
		schemaVersion: null,
		path: dbStatus.conduitPath
	};
	const signaldockReport = {
		available: dbStatus.signaldock,
		rowCount: null,
		schemaVersion: null,
		path: dbStatus.signaldockPath
	};
	if (dbStatus.nexus) {
		const db = getNexusDb();
		if (db) {
			nexusReport.rowCount = safeCount(db, "nexus_nodes");
			nexusReport.schemaVersion = safeSchemaVersion(db);
		}
	}
	if (dbStatus.brain) {
		const db = getBrainDb(locals.projectCtx);
		if (db) {
			brainReport.rowCount = safeCount(db, "brain_observations");
			brainReport.schemaVersion = safeSchemaVersion(db);
		}
	}
	if (dbStatus.tasks) {
		const db = getTasksDb(locals.projectCtx);
		if (db) {
			tasksReport.rowCount = safeCount(db, "tasks");
			tasksReport.schemaVersion = safeSchemaVersion(db);
		}
	}
	if (dbStatus.conduit) {
		const db = getConduitDb(locals.projectCtx);
		if (db) conduitReport.schemaVersion = safeSchemaVersion(db);
	}
	if (dbStatus.signaldock) {
		const db = getAgentRegistryDb();
		if (db) signaldockReport.schemaVersion = safeSchemaVersion(db);
	}
	return json({
		ok: true,
		service: "cleo-studio",
		version: pkgVersion,
		checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
		uptime: Math.round((Date.now() - bootedAt) / 1e3),
		databases: {
			nexus: nexusReport,
			brain: brainReport,
			tasks: tasksReport,
			conduit: conduitReport,
			"agent-registry": signaldockReport
		}
	});
};

export { GET };
//# sourceMappingURL=_server.ts-B20nBpN5.js.map
