/** * Store-layer accessor for the Pi `SessionStorage` tree-persistence tables * (`pi_session_entries` + `pi_session_leaf`) — T11761 · S3 · T11899. * * This module is the ONLY place that touches the native `cleo.db` handle for Pi * session persistence. It lives INSIDE the store chokepoint (`packages/core/src/ * store/**`), the Gate-3 allowlist, so it may extract the native `DatabaseSync` * that {@link openDualScopeDb} already holds (via drizzle's `$client`) and run * prepared statements over it — exactly the established `conduit-sqlite.ts` * pattern. It NEVER calls `new DatabaseSync(` itself. * * The durable adapter (`packages/core/src/llm/pi/pi-session-storage.ts`) sits * OUTSIDE the chokepoint, so it must NOT open a raw handle; it calls these * accessors and wraps every WRITE in `withWriterLease('project', 'bulk', …)` so * the daemon remains the sole arbitrated writer (ZERO authority for Pi). * * Physical model (see the `t11899-pi-session-tree` migration): * - `pi_session_entries(session_id, entry_id, parent_id, type, payload_json, * seq, ts)` — one row per `SessionTreeEntry`; PRIMARY KEY `(session_id, * entry_id)`. `seq` is a per-session monotonic insertion ordinal that * preserves append order (entry ids are random 8-char tokens, so insertion * order is NOT recoverable from the id alone). * - `pi_session_leaf(session_id, leaf_id, created_at, updated_at)` — one row per * session recording the active tree leaf (and the session's `createdAt` * metadata anchor). * * @module * @task T11899 * @task T11761 * @epic T10403 */ /** Physical name of the Pi session-entry tree table. */ export declare const PI_SESSION_ENTRIES_TABLE: "pi_session_entries"; /** Physical name of the Pi session leaf-pointer / metadata table. */ export declare const PI_SESSION_LEAF_TABLE: "pi_session_leaf"; /** * A persisted Pi session-tree entry row, decoded from `pi_session_entries`. * * `payloadJson` is the JSON-serialized residue of the `SessionTreeEntry` minus * the columnized fields (`id` / `parentId` / `type` / `timestamp`) — the adapter * re-hydrates the full Pi entry by merging the columns back over it. */ export interface PiSessionEntryRow { /** Owning session id (daemon-stamped; never minted by Pi). */ readonly sessionId: string; /** The Pi entry id (`SessionTreeEntry.id`). */ readonly entryId: string; /** Parent entry id, or `null` at a tree root. */ readonly parentId: string | null; /** The `SessionTreeEntry.type` discriminator (`message` / `leaf` / `label` / …). */ readonly type: string; /** JSON residue of the entry's type-specific fields. */ readonly payloadJson: string; /** Per-session monotonic insertion ordinal (preserves append order). */ readonly seq: number; /** The Pi entry timestamp (`SessionTreeEntry.timestamp`, ISO-8601). */ readonly ts: string; } /** The leaf-pointer + metadata row for one session. */ export interface PiSessionLeafRow { /** Owning session id. */ readonly sessionId: string; /** The active tree leaf entry id, or `null` (no leaf yet / reset to root). */ readonly leafId: string | null; /** Session creation timestamp (ISO-8601) — the metadata `createdAt` anchor. */ readonly createdAt: string; /** Last leaf-update timestamp (ISO-8601). */ readonly updatedAt: string; } /** Narrow shape of the native handle methods this accessor uses. */ type NativeRunResult = { changes: number | bigint; lastInsertRowid: number | bigint; }; interface NativeStatement { run(...params: ReadonlyArray): NativeRunResult; get(...params: ReadonlyArray): Record | undefined; all(...params: ReadonlyArray): Array>; } interface NativeHandle { prepare(sql: string): NativeStatement; } /** * Extract the native `DatabaseSync` handle for the PROJECT-scope `cleo.db`. * * Routes through {@link openDualScopeDb} (the dual-scope chokepoint) — which * applies the pragma SSoT, runs the consolidated migrations (creating the * `pi_session_*` tables), and manages the singleton cache — then extracts the * native handle drizzle holds on `$client`. NEVER opens a raw connection (Gate * 3): it reuses the handle the chokepoint already owns. * * @param cwd - Working directory for project resolution (defaults to `cwd`). * @returns The live native handle. * @throws When the chokepoint returns a handle without `$client`. */ export declare function getPiSessionNativeDb(cwd?: string): Promise; /** * Return the leaf/metadata row for a session, or `null` when the session has no * row yet (never written to). * * @param native - The native project `cleo.db` handle. * @param sessionId - The session id. * @returns The leaf row or `null`. */ export declare function readPiSessionLeaf(native: NativeHandle, sessionId: string): PiSessionLeafRow | null; /** * Return all entry rows for a session in stable append order (`seq ASC`). * * @param native - The native project `cleo.db` handle. * @param sessionId - The session id. * @returns The ordered entry rows (empty when the session has none). */ export declare function readPiSessionEntries(native: NativeHandle, sessionId: string): PiSessionEntryRow[]; /** * Return a single entry row by id, or `null` when absent. * * @param native - The native project `cleo.db` handle. * @param sessionId - The owning session id. * @param entryId - The entry id. * @returns The entry row or `null`. */ export declare function readPiSessionEntry(native: NativeHandle, sessionId: string, entryId: string): PiSessionEntryRow | null; /** * Insert (or no-op on conflict) one session-tree entry. * * The caller MUST already hold the PROJECT/`bulk` writer lease — this accessor * performs the raw write and does NOT acquire the lease itself (separation of * concerns: the lease boundary lives in `pi-session-storage.ts`, outside the * chokepoint). The `seq` is computed as `MAX(seq)+1` for the session under the * same statement batch, so it is monotonic per session. * * @param native - The native project `cleo.db` handle (leased section). * @param row - The entry to persist (without `seq` — assigned here). */ export declare function insertPiSessionEntry(native: NativeHandle, row: Omit): void; /** * Upsert the leaf pointer + metadata row for a session. * * On first write the `created_at` anchor is set to `createdAt`; subsequent writes * preserve the existing `created_at` (via `ON CONFLICT`) and only advance * `leaf_id` + `updated_at`. The caller MUST hold the writer lease. * * @param native - The native project `cleo.db` handle (leased section). * @param sessionId - The session id. * @param leafId - The new active leaf id, or `null`. * @param createdAt - The session creation anchor (used only on first insert). * @param updatedAt - The update timestamp. */ export declare function upsertPiSessionLeaf(native: NativeHandle, sessionId: string, leafId: string | null, createdAt: string, updatedAt: string): void; export {}; //# sourceMappingURL=pi-session-store.d.ts.map