import { type Database } from "./database.js"; /** * Profiled upper envelope for one session's complete active, model-facing * transcript. Bytes alone do not bound the decoded object graph, so rows, JSON * nodes, and object properties are independently limited. Provider-token * compaction alone does not keep history below these boundaries: inline * screenshots cost far more stored bytes than provider tokens. Automatic * compaction therefore also runs once the stored footprint reaches * {@link ACTIVE_SESSION_HISTORY_COMPACTION_RATIO} of any limit. The limits stay * the final materialization guard: a corrupt/imported/pathological transcript * fails deterministically before a Postgres driver decodes it instead of * using the pod OOM killer as admission, and an over-limit history is read * only in the bounded recovery form below. */ export declare const ACTIVE_SESSION_HISTORY_MAX_JSON_BYTES: number; export declare const ACTIVE_SESSION_HISTORY_MAX_ROWS = 8192; export declare const ACTIVE_SESSION_HISTORY_MAX_JSON_NODES = 131072; export declare const ACTIVE_SESSION_HISTORY_MAX_JSON_PROPERTIES = 65536; /** * Share of each materialization limit at which automatic compaction runs. * * At 60% of 15 MiB (9 MiB) the guard still has 6 MiB of headroom: roughly 30 * further 200 KB screenshots, which covers the one model step of tool results * appended between two compaction checks, while the checkpoint request reads * and serializes the same history below the guard. A single tool result can * carry more (up to 8 MiB of inline payload), so no ratio can rule out a jump * past the guard; the bounded recovery read handles that case. */ export declare const ACTIVE_SESSION_HISTORY_COMPACTION_RATIO = 0.6; export type ActiveSessionHistoryLimitKind = "json_bytes" | "rows" | "json_nodes" | "json_properties"; /** * Stored size of active history in the units the guard measures. `jsonBytes` * is the UTF-8 size of the history as one JSON array. */ export type ActiveSessionHistoryFootprint = { jsonBytes: number; rows: number; jsonNodes: number; jsonProperties: number; }; export declare const ACTIVE_SESSION_HISTORY_LIMITS: Readonly; export declare class ActiveSessionHistoryLimitExceededError extends Error { readonly actualBytes: number; readonly maximumBytes: number; readonly limitKind: ActiveSessionHistoryLimitKind; readonly actual: number; readonly maximum: number; readonly code = "active_history_too_large"; constructor(actualBytes: number, maximumBytes: number, limitKind?: ActiveSessionHistoryLimitKind, actual?: number, maximum?: number); } export declare function addActiveSessionHistoryFootprint(base: ActiveSessionHistoryFootprint, delta: ActiveSessionHistoryFootprint): ActiveSessionHistoryFootprint; /** The stored dimension that calls for compaction, preferring one already over its limit. */ export type ActiveSessionHistoryPressure = { limitKind: ActiveSessionHistoryLimitKind; actual: number; threshold: number; maximum: number; exceeded: boolean; }; export declare function activeSessionHistoryCompactionPressure(footprint: ActiveSessionHistoryFootprint, limits?: Readonly, ratio?: number): ActiveSessionHistoryPressure | null; export type ActiveSessionHistoryRow = { id: string; position: number; /** The turn that wrote this row; null for rows a compaction rewrote without one. */ turnId: string | null; item: Record; providerArtifactInvalidatedAt: Date | null; }; /** How an over-limit history was bounded for compaction. */ export type ActiveSessionHistoryRecovery = { limitKind: ActiveSessionHistoryLimitKind; actual: number; maximum: number; /** Oldest active rows left out of the bounded view entirely. */ omittedRows: number; /** Rows whose inline base64 data-URL payloads were replaced by an omission note. */ elidedRows: number; }; export type ActiveSessionHistoryRead = { items: ActiveSessionHistoryRow[]; /** Measured stored footprint of the complete active history. */ footprint: ActiveSessionHistoryFootprint; /** Set only when the history exceeded a limit and was read in bounded recovery form. */ recovery: ActiveSessionHistoryRecovery | null; }; export type ActiveSessionHistoryReadOptions = { pageSize?: number; limits?: Partial; /** * `throw` (default) rejects an over-limit history. `bounded_recovery` instead * returns a view that fits {@link ACTIVE_SESSION_HISTORY_COMPACTION_RATIO} of * every limit, for compaction only: inline data-URL payloads are left out of * the oldest items first (tool results before messages), then the oldest * rows are left out. Payloads are removed in PostgreSQL, so they are never * decoded by the worker. It still throws when not even the newest row fits. */ overLimit?: "throw" | "bounded_recovery"; /** * `active` (default) reads the live model history. `stored_transcript` * reads the session's own conversation since its latest context clear: * rows a compaction superseded are included, and every row a compaction * wrote (its summary or Codex checkpoint and the copies it retained) is * left out. Used to rebuild a portable context when a Codex remote * compaction chat switches to another provider's model. */ rows?: "active" | "stored_transcript"; }; /** * Model-facing note that replaces an inline image or file left out of a * bounded recovery view. Content-free and identical for every payload. */ export declare const ACTIVE_HISTORY_INLINE_PAYLOAD_OMISSION = "[Opengeni left out an inline image or file from this older item to keep context compaction within its memory limit]"; /** Size of one stored row as the guard measures it. */ export type ActiveSessionHistoryRowMeasure = { position: number; jsonBytes: number; /** `jsonBytes` after inline data-URL payloads are replaced. */ elidedJsonBytes: number; jsonNodes: number; jsonProperties: number; message: boolean; }; export type ActiveSessionHistoryRecoveryPlan = { startPosition: number; omittedRows: number; /** Non-message rows at or before this position have payloads elided. */ elideOtherThrough: number | null; /** Message rows at or before this position have payloads elided. */ elideMessagesThrough: number | null; elidedRows: number; }; /** * Keep as many of the newest rows as fit `budget` when all of their inline * payloads are elided, then elide only as much as the byte budget needs: * tool results and other non-message items oldest-first, then messages * oldest-first. When older rows must be left out, the view starts at a * message where one is available, so no kept tool result loses its call. * Null when not even the newest row fits. */ export declare function planActiveSessionHistoryRecovery(rows: readonly ActiveSessionHistoryRowMeasure[], budget: Readonly): ActiveSessionHistoryRecoveryPlan | null; /** * Turn PostgreSQL's sentinel back into protocol-valid content: an image or * file part becomes a text part carrying the omission note, a computer * screenshot keeps its shape with the existing omission card image, and any * other occurrence (for example inside a text output) becomes the note. */ export declare function restoreElidedInlinePayloads(value: unknown): unknown; /** * Memory-bounded active-history read for model input and compaction. PostgreSQL * first measures the complete logical window without returning any JSONB item, * then keyset-pages the admitted rows on the same repeatable-read snapshot. */ export declare function readActiveSessionHistory(db: Database, workspaceId: string, sessionId: string, options?: ActiveSessionHistoryReadOptions): Promise; /** The footprint a bounded recovery view must fit. */ export declare function activeSessionHistoryRecoveryBudget(limits?: Readonly): ActiveSessionHistoryFootprint; /** Complete active history, or {@link ActiveSessionHistoryLimitExceededError}. */ export declare function getActiveSessionHistoryItemsPaged(db: Database, workspaceId: string, sessionId: string, pageSize?: number, maximumJsonBytes?: number, maximumRows?: number, maximumJsonNodes?: number, maximumJsonProperties?: number): Promise;