/** * 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 task is deleted from tasks.db. * * Handles: * - XFKB-001/002: Nullify brain_decisions.context_epic_id / context_task_id * - XFKB-003: Delete brain_memory_links rows where task_id matches * - XFKB-005: Delete brain_page_nodes with id='task:' and cascade brain_page_edges * * This is a best-effort cleanup — brain.db is a cognitive store and minor * staleness is preferable to failing task deletions due to brain.db errors. * * @remarks * Best-effort: failures in brain.db cleanup do not propagate to the caller. * A background reconciliation pass can clean up any residual stale refs. * * @param taskId - The ID of the task being deleted from tasks.db * @param cwd - Optional working directory * * @example * ```ts * await cleanupBrainRefsOnTaskDelete('T042'); * ``` */ export declare function cleanupBrainRefsOnTaskDelete(taskId: string, cwd?: string): Promise; /** * 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'); * ``` */ export declare function cleanupBrainRefsOnSessionDelete(sessionId: string, cwd?: string): Promise; /** * 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 *\/ } * ``` */ export declare function taskExistsInTasksDb(taskId: string, tasksDb: Awaited>): Promise; /** * Verify a session ID exists in tasks.db before writing a cross-DB reference to brain.db. * Returns true if the session exists, false otherwise. * * Provides write-guard for XFKB-004 on brain.db insert. * * @remarks * Used as a write-guard before inserting cross-DB references into brain.db. * * @param sessionId - Session ID to verify * @param tasksDb - The tasks.db drizzle instance * @returns True if the session exists in tasks.db * * @example * ```ts * if (await sessionExistsInTasksDb('ses_abc', db)) { /* safe to reference *\/ } * ``` */ /** * Reconcile orphaned cross-DB references in brain.db. * * Scans brain.db for references to tasks/sessions that no longer exist in * tasks.db and cleans them up: * - brain_decisions with stale context_task_id or context_epic_id → nullify * - brain_observations with stale source_session_id → nullify * - brain_memory_links with stale task_id → delete row * * This is the background reconciliation pass mentioned in the module doc. * Safe to run at any frequency — idempotent. * * @param cwd - Optional working directory * @returns Counts of orphaned references cleaned up */ export declare function reconcileOrphanedRefs(cwd?: string): Promise<{ decisionsFixed: number; observationsFixed: number; linksRemoved: number; }>; /** * Verify a session ID through the caller's shared tasks database handle. * * @param sessionId - Session ID to verify. * @param tasksDb - Shared tasks database handle. * @returns True when the session exists. */ export declare function sessionExistsInTasksDb(sessionId: string, tasksDb: Awaited>): Promise; /** * Verify a session ID through fresh read-only connections and the caller's live handle. * * This is the bounded fallback for callers whose shared project handle either * closed during the primary query or returned a stale empty result. It does not * evict the dual-scope cache, so other domains retaining the shared handle stay * live while the probe runs. The live handle is checked last because it can own * a committed, unlinked SQLite file that is no longer reachable by path. * * @param sessionId - Session ID to verify. * @param tasksDb - Shared tasks handle whose native path anchors the probe when available. * @param cwd - Project root used to resolve the project-scope cleo.db. * @returns True when the session is present in any live project session table. * @throws Error when no database candidate can be read, distinguishing unavailable validation from absence. * @task T12034 */ export declare function sessionExistsInTasksDbFresh(sessionId: string, tasksDb: Awaited> | null, cwd?: string): Promise; /** * Verify a task ID through fresh read-only connections and the caller's live handle. * * Checks both the active legacy tasks table and its consolidated prefixed * counterpart so callers remain correct during the E3-to-E6 transition. * * @param taskId - Task ID to verify. * @param tasksDb - Shared tasks handle used only when no project root is available. * @param cwd - Project root used to resolve the authoritative project database. * @returns True when the task exists in either supported project table. * @throws Error when no database candidate can be read, distinguishing unavailable validation from absence. * @task T12034 */ export declare function taskExistsInTasksDbFresh(taskId: string, tasksDb: Awaited> | null, cwd?: string): Promise; /** * Verify an agent exists in the global Agent Registry before creating cross-DB references. * Returns true if the agent_id exists in the global cleo.db agent_registry_agents table. * * Provides write-guard for agent_instances and agent_error_log in tasks.db * that reference agents whose identity lives in the global Agent Registry. * * @param agentId - Agent slug (e.g. 'cleo-db-lead') to verify * @param cwd - Optional working directory * @returns True if the agent exists in the global Agent Registry * * @task T238 */ export declare function agentExistsInAgentRegistryDb(agentId: string, cwd?: string): Promise; //# sourceMappingURL=cross-db-cleanup.d.ts.map