import type { Database as DatabaseType } from "better-sqlite3"; import { type Repositories } from "./repositories/index.js"; import { CompactionService, ProjectScanService, GitService, EventTriggerService, UpdateService, AgentRulesService, InstanceRegistryService, CrossInstanceService, SensitiveDataService, WorkflowAdvisorService, PMDiagnosticsTracker } from "./services/index.js"; export interface Services { compaction: CompactionService; scan: ProjectScanService; git: GitService; events: EventTriggerService; update: UpdateService; agentRules: AgentRulesService; registry: InstanceRegistryService; crossInstance: CrossInstanceService; sensitiveData: SensitiveDataService; advisor: WorkflowAdvisorService; diagnostics: PMDiagnosticsTracker; } export declare function initDatabase(projectRoot: string, ideKey?: string): DatabaseType; export declare function getDb(): DatabaseType; export declare function getRepos(): Repositories; export declare function getServices(): Services; export declare function getProjectRoot(): string; export declare function getDbPath(): string; /** * Re-initialize the database at a new project root. * * Used when the agent provides the correct project_root at session start * and the server originally resolved to the wrong location (e.g. the * global fallback because the IDE spawned from $HOME). * * If the old DB at the incorrect location contains data, this function * copies it to the new location so previous sessions/decisions/etc. survive. * * @returns {{ migrated: boolean; oldPath: string; newPath: string; message: string }} */ export declare function reinitDatabase(newProjectRoot: string, ideKey?: string): { migrated: boolean; oldPath: string; newPath: string; message: string; }; /** * Create a backup copy of the database file. * Uses SQLite's backup API for a safe, consistent copy. */ export declare function backupDatabase(destPath?: string): string; /** * Replace the live database with the contents of a backup file. * * FR-D1 T1. The previous implementation (dispatcher-admin `case "restore"`) * copied the backup over memory.db while this process still held the * connection open, and never removed the old -wal/-shm. SQLite then * checkpointed the STALE WAL back over the freshly restored file โ€” so the * restore silently did not happen, `integrity_check` still returned "ok", and * the user was told to restart, which is the very act that undid their * rollback. Proof and quoted output: docs/foundations/01-durability.md P1. * * sqlite.org/howtocorrupt.html ยง1.4 lists "overwriting a database file with * another without also deleting any hot journal associated with the original * database" among the actions likely to lead to corruption. This function is * the fix for that specific bullet. * * Order matters and every step is load-bearing: * 1. validate the candidate before touching anything (it may not be a * database at all โ€” the old code checked existence only) * 2. safety-backup, and ABORT if it fails. The live path used to swallow * that failure; only the dead code in tools/backup.ts got it right * 3. close the connection, so nothing can write behind us * 4. delete main + -wal + -shm TOGETHER โ€” this is the actual bug fix * 5. copy, then reopen through initDatabase so migrations run against a * possibly-older backup and repos/services are rebuilt * * @throws if the candidate is unreadable, fails integrity_check, carries no * schema_meta version, or if the safety backup cannot be written. */ export declare function restoreDatabase(inputPath: string): { restored_from: string; safety_backup: string; schema_version: string; }; export declare function queryAll(sql: string, params?: unknown[]): Record[]; export declare function queryOne(sql: string, params?: unknown[]): Record | null; export declare function execute(sql: string, params?: unknown[]): { lastId: number; }; export declare function executeMany(statements: Array<{ sql: string; params: unknown[]; }>): void; export declare function now(): string; export declare function getCurrentSessionId(): number | null; export declare function getLastCompletedSession(): { id: number; ended_at: string; summary: string | null; agent_name: string; } | null; export declare function getDbSizeKb(): number; export declare function forceFlush(): void; /** * F10: Log a tool invocation for session replay diagnostics. * Silent no-op if the tool_call_log table doesn't exist (older schemas). */ export declare function logToolCall(toolName: string, outcome?: "success" | "error", notes?: string): void; //# sourceMappingURL=database.d.ts.map