import type { AttemptToolCatalog, ModelContextSnapshot } from "@opengeni/contracts"; import type { Database } from "./database.js"; /** * Session-scoped content-addressed storage for large, highly repetitive JSON. * * Long sessions record a tool catalog and a model-request snapshot for every * attempt. Consecutive attempts repeat nearly all of that content, so storing * each copy inline makes those two tables the largest in the database. Writers * now externalize the repetitive parts into `session_content_blobs`, keyed by * the SHA-256 of their canonical JSON, and keep only digests in the owning row. * Readers hydrate both the encoded and the legacy inline form, so the stored * representation is an implementation detail: hydrated values are exactly the * values that were written. * * Blobs are owned by one session (never shared across sessions or workspaces), * so their lifecycle is the session's: they cascade with it and move with it * into a session archive. A blob that no stored row names any more (a * snapshot's chunks after the next request replaced it) is deleted by the * collector in `session-content-garbage.ts`; writers and snapshot readers hold * the session's content lock shared so that never races them. */ export declare const SESSION_CONTENT_REFS_VERSION: 1; export type AttemptToolCatalogContentRefs = { v: typeof SESSION_CONTENT_REFS_VERSION; entries: string[]; }; export type ModelContextSnapshotContentRefs = { v: typeof SESSION_CONTENT_REFS_VERSION; instructions: string; layers: string; tools: string; skills: string; /** Content-defined chunks of `providerRequest.body`; null when inline or absent. */ body: string[] | null; }; export type SessionContentScope = { accountId: string; workspaceId: string; sessionId: string; }; export declare class SessionContentBlobMissingError extends Error { readonly digest: string; readonly code = "session_content_blob_missing"; constructor(digest: string); } /** Sort object keys recursively; arrays keep their order. Matches jsonb equality. */ export declare function canonicalJsonForContentDigest(value: unknown): string; export declare function digestContentValue(value: unknown): string; /** Split text into content-defined chunks; never splits a surrogate pair. */ export declare function chunkTextByContent(text: string): string[]; type EncodedValue = { stored: Stored; refs: Refs; blobs: Map; }; /** Externalize every catalog entry; the stored catalog keeps all identity fields. */ export declare function encodeAttemptToolCatalogContent(catalog: AttemptToolCatalog): EncodedValue; /** Externalize the repeated instruction/tool/skill prefix and the raw request body. */ export declare function encodeModelContextSnapshotContent(snapshot: ModelContextSnapshot, cache?: ModelContextSnapshotContentCache): EncodedValue; type BlobReader = Pick; type SnapshotContentField = "instructions" | "layers" | "tools" | "skills"; type BodyChunks = { readonly text: string; readonly chunks: readonly string[]; readonly digests: readonly string[]; /** `Buffer.byteLength(JSON.stringify(chunk))` per chunk. */ readonly jsonBytes: readonly number[]; }; /** * Per-attempt memo for consecutive model-context snapshots of one run. Each * request repeats the previous request's instructions, layers, tools and * skills, and its body starts with the previous body. The memo recognizes a * repeated value by comparing it with a private copy (structure and every * primitive) and the shared prefix of a body chunk by chunk, so only new * content is serialized and hashed. Every digest, chunk and byte count is * exactly what the uncached functions compute. */ export declare class ModelContextSnapshotContentCache { private readonly fields; private body; /** `Buffer.byteLength(JSON.stringify(value))` of a repeated field, when known. */ fieldJsonBytes(name: SnapshotContentField, value: unknown): number | undefined; fieldDigest(name: SnapshotContentField, value: unknown): string; /** `chunkTextByContent(text)` and each chunk's digest, reusing the shared prefix. */ bodyChunks(text: string): BodyChunks; /** `Buffer.byteLength(JSON.stringify(text))`; chunks never split a surrogate pair. */ bodyJsonBytes(text: string): number; private match; } /** * `Buffer.byteLength(JSON.stringify(snapshot), "utf8")` for a parsed (plain) * snapshot, reusing the memo for repeated fields and the request body. */ export declare function modelContextSnapshotJsonUtf8Bytes(snapshot: ModelContextSnapshot, cache?: ModelContextSnapshotContentCache): number; type BlobWriter = Pick; /** * Hold the session's content lock shared until the transaction ends. The * unreferenced-blob collector takes it exclusively (and only when free), so a * transaction holding it can rely on a blob it found staying until it commits * the row that refers to it, and on reading the blobs of a snapshot row it * read. Call it inside a transaction, before the first read it protects. */ export declare function lockSessionContentShared(tx: Pick, sessionId: string): Promise; /** * Insert the values this session does not already hold. Idempotent and * race-safe; must run in the transaction that writes the row referring to them. */ export declare function writeSessionContentBlobs(tx: BlobWriter, scope: SessionContentScope, blobs: ReadonlyMap): Promise; /** Load exact referenced values; a missing blob is corruption, never an empty value. */ export declare function readSessionContentBlobs(tx: BlobReader, scope: Pick, digests: readonly string[]): Promise>; /** Return the exact catalog that was written, from either stored form. Unverified. */ export declare function hydrateStoredAttemptToolCatalog(tx: BlobReader, row: { catalog: unknown; contentRefs: unknown; }): Promise; /** Return the exact snapshot that was written, from either stored form. Unverified. */ export declare function hydrateStoredModelContextSnapshot(tx: BlobReader, scope: Pick, row: { snapshot: unknown; contentRefs: unknown; }): Promise; /** The digests a stored snapshot's content refs name, or null when it has none. */ export declare function modelContextSnapshotRefDigests(contentRefs: unknown): string[] | null; /** * Rebuild a stored snapshot from blob values already read (the archive bundle * keeps blobs as their own rows). Unverified, like the database form. */ export declare function hydrateModelContextSnapshotFromValues(snapshot: unknown, contentRefs: unknown, values: ReadonlyMap): unknown; export {};