import { c as brainObservations } from './memory-schema-Bi7cChwo.js';
import { e as getBrainDb } from './sqlite-Dsbe0wec.js';
import { e as eq } from './logger-ChCwlV5k.js';

//#region ../core/dist/store/cross-db-cleanup.js
/**
* Cross-database cleanup hooks for brain.db → tasks.db soft FK enforcement.
*
* SQLite does not support foreign key constraints across database connections.
* This module provides application-layer guards that maintain referential
* integrity between brain.db and tasks.db after destructive operations.
*
* Implements the recommendations from T030 audit (XFKB-001 through XFKB-005).
*
* @task T033
* @epic T029
*/
/**
* Clean up brain.db references after a session is deleted from tasks.db.
*
* Handles:
* - XFKB-004: Nullify brain_observations.source_session_id where it matches
*
* @remarks
* Best-effort: failures do not propagate. brain.db may not be initialised.
*
* @param sessionId - The ID of the session being deleted from tasks.db
* @param cwd - Optional working directory
*
* @example
* ```ts
* await cleanupBrainRefsOnSessionDelete('ses_20260321_abc');
* ```
*/
async function cleanupBrainRefsOnSessionDelete(sessionId, cwd) {
	let brainDb = null;
	try {
		brainDb = await getBrainDb(cwd);
	} catch {
		return;
	}
	try {
		await brainDb.update(brainObservations).set({ sourceSessionId: null }).where(eq(brainObservations.sourceSessionId, sessionId));
	} catch {}
}
/**
* Verify a task ID exists in tasks.db before writing a cross-DB reference to brain.db.
* Returns true if the task exists, false otherwise.
*
* Provides write-guard for XFKB-001/002/003 on brain.db insert.
*
* @remarks
* Used as a write-guard before inserting cross-DB references into brain.db.
*
* @param taskId - Task ID to verify
* @param tasksDb - The tasks.db drizzle instance
* @returns True if the task exists in tasks.db
*
* @example
* ```ts
* if (await taskExistsInTasksDb('T042', db)) { /* safe to reference *\/ }
* ```
*/
async function taskExistsInTasksDb(taskId, tasksDb) {
	const { tasks } = await import('./tasks-schema2-DJGD-IKf.js');
	const { eq: eqOp } = await import('./index-DIWrtHjY.js');
	return (await tasksDb.select({ id: tasks.id }).from(tasks).where(eqOp(tasks.id, taskId)).all()).length > 0;
}
async function sessionExistsInTasksDb(sessionId, tasksDb) {
	const { sessions } = await import('./tasks-schema2-DJGD-IKf.js');
	const { eq: eqOp } = await import('./index-DIWrtHjY.js');
	return (await tasksDb.select({ id: sessions.id }).from(sessions).where(eqOp(sessions.id, sessionId)).all()).length > 0;
}

export { cleanupBrainRefsOnSessionDelete as c, sessionExistsInTasksDb as s, taskExistsInTasksDb as t };
//# sourceMappingURL=cross-db-cleanup-C_FLyd-6.js.map
