/** * SessionDB — Persistent per-project SQLite database for session events. * * Stores raw events captured by hooks during a Claude Code session, * session metadata, and resume snapshots. Extends SQLiteBase from * the shared package. */ import { SQLiteBase } from "../db-base.js"; import type { SessionEvent } from "../types.js"; /** * Returns the worktree suffix to append to session identifiers. * Returns empty string when running in the main working tree. * * Set CONTEXT_MODE_SESSION_SUFFIX to an explicit value to override * (useful in CI environments or when git is unavailable). * Set to empty string to disable isolation entirely. */ export declare function getWorktreeSuffix(): string; /** A stored event row from the session_events table. */ export interface StoredEvent { id: number; session_id: string; type: string; category: string; priority: number; data: string; source_hook: string; created_at: string; data_hash: string; } /** Session metadata row from the session_meta table. */ export interface SessionMeta { session_id: string; project_dir: string; started_at: string; last_event_at: string | null; event_count: number; compact_count: number; } /** Resume snapshot row from the session_resume table. */ export interface ResumeRow { snapshot: string; event_count: number; consumed: number; } export declare class SessionDB extends SQLiteBase { /** * Cached prepared statements. Stored in a Map to avoid the JS private-field * inheritance issue where `#field` declarations in a subclass are not * accessible during base-class constructor calls. * * `declare` ensures TypeScript does NOT emit a field initializer at runtime. * Without `declare`, even `stmts!: Map<...>` emits `this.stmts = undefined` * after super() returns, wiping what prepareStatements() stored. The Map * is created inside prepareStatements() instead. */ private stmts; constructor(opts?: { dbPath?: string; }); /** Shorthand to retrieve a cached statement. */ private stmt; protected initSchema(): void; protected prepareStatements(): void; /** * Insert a session event with deduplication and FIFO eviction. * * Deduplication: skips if the same type + data_hash appears in the * last DEDUP_WINDOW events for this session. * * Eviction: if session exceeds MAX_EVENTS_PER_SESSION, evicts the * lowest-priority (then oldest) event. */ insertEvent(sessionId: string, event: SessionEvent, sourceHook?: string): void; /** * Retrieve events for a session with optional filtering. */ getEvents(sessionId: string, opts?: { type?: string; minPriority?: number; limit?: number; }): StoredEvent[]; /** * Get the total event count for a session. */ getEventCount(sessionId: string): number; /** * Ensure a session metadata entry exists. Idempotent (INSERT OR IGNORE). */ ensureSession(sessionId: string, projectDir: string): void; /** * Get session statistics/metadata. */ getSessionStats(sessionId: string): SessionMeta | null; /** * Increment the compact_count for a session (tracks snapshot rebuilds). */ incrementCompactCount(sessionId: string): void; /** * Upsert a resume snapshot for a session. Resets consumed flag on update. */ upsertResume(sessionId: string, snapshot: string, eventCount?: number): void; /** * Retrieve the resume snapshot for a session. */ getResume(sessionId: string): ResumeRow | null; /** * Mark the resume snapshot as consumed (already injected into conversation). */ markResumeConsumed(sessionId: string): void; /** * Delete all data for a session (events, meta, resume). */ deleteSession(sessionId: string): void; /** * Remove sessions older than maxAgeDays. Returns the count of deleted sessions. */ cleanupOldSessions(maxAgeDays?: number): number; }