export interface HarnessSessionRecord { agentId: string; taskId: string; mode: string; harness: string; /** Harness-native session id (or name for goose). */ id: string; /** True when the id was pre-assigned on the first run (deterministic). */ preassigned?: boolean; model?: string | null; /** Working directory the session was created in (resume requires it). */ cwd: string; createdAt: string; lastUsedAt: string; attempts: number; lastError?: string; /** * Approximate size of the conversation so far, in CHARACTERS (2026-12). * * Every turn adds the prompt it sent plus the output it produced, so this * grows with the conversation instead of measuring one message. Tokens are * estimated as `chars / 4` (`estimateSessionTokens`) - a deliberate, * provider-agnostic approximation: it is the only number every harness can * report without parsing four different usage formats, and being slightly * conservative is the safe direction (we rotate a little early rather than * overflow the model's window mid-answer). */ contextChars?: number; /** Turns this session has served (chat/doc/board), for the UI/debug logs. */ turns?: number; /** * Consecutive failed runs on this session (2026-12). * * Conversational modes (chat/doc/board) KEEP their session after a failure - * a transient provider error must not throw away the conversation - but a * session that fails twice in a row is treated as poisoned and replaced. */ consecutiveFailures?: number; /** How many times this conversation has been rotated by the context budget. */ rotations?: number; /** Why the last rotation happened (shown in logs/notices). */ lastRotatedReason?: string; } /** * Default context budget for a conversational session (2026-12). * * A project can override it (`Project.chatMaxContextTokens`); this is what * "not stated" means. 400k is large enough that a normal project conversation * never rotates, and small enough that it happens well before the biggest * windows fill. */ export declare const DEFAULT_CHAT_CONTEXT_TOKENS = 400000; /** Modes that are ONE CONVERSATION and must not lose it to a transient error. */ export declare const CONVERSATIONAL_MODES: ReadonlySet; /** * Rough token estimate for a conversation from its accumulated character count. * * ~4 characters per token is the standard English approximation, and it is the * only one that works for every harness (some print usage, most do not). */ export declare function estimateSessionTokens(contextChars: number | undefined): number; /** * Has this conversation outgrown its budget? * * `limit` is `Project.chatMaxContextTokens` (null/undefined = the 400k * default). A record with no `contextChars` (written before this field existed) * is treated as EMPTY, not as over budget - the first run under the new code * adopts it and starts counting from its own prompt. */ export declare function sessionContextExceeded(record: Pick | null, limitTokens?: number | null): boolean; /** * Load a record when valid for this harness + cwd; otherwise null (fresh). * * Each mode resolves ONLY its own record. The old build→plan fallback was * removed (2026-09): resuming a plan session in build mode replays a * conversation that opens with "PLAN MODE … must NOT execute", causing the * model to report plan/read-only mode even though the task is workMode=build. * Build tasks that genuinely want planning context already have their own * `.build.json` record after the first build run. */ export declare function loadHarnessSession(agentId: string, taskId: string, mode: string, harness: string, cwd: string): HarnessSessionRecord | null; /** Persist a record (best-effort). */ export declare function saveHarnessSession(rec: HarnessSessionRecord): void; /** Drop a single record (e.g. after a resume failure). */ export declare function clearHarnessSession(agentId: string, taskId: string, mode: string): void; /** Read a record regardless of harness/cwd (used for accounting + rotation). */ export declare function peekHarnessSession(agentId: string, taskId: string, mode: string): HarnessSessionRecord | null; /** * Add one turn's worth of characters to a session's context estimate. * * Called after a successful turn. Best-effort: a missing record (cleared mid * run) is a no-op, and the conversation simply starts counting again next time. */ export declare function addHarnessSessionContext(agentId: string, taskId: string, mode: string, addedChars: number): HarnessSessionRecord | null; /** * Record a failed turn on a conversational session. * * Returns the new consecutive-failure count, so the caller can decide whether * the session is worth keeping (1) or poisoned (>= 2). */ export declare function noteHarnessSessionFailure(agentId: string, taskId: string, mode: string, error?: string): number; /** * Take back the recorded failure of a turn that DID deliver its answer (2026-11). * * A conversational turn can exit non-zero AFTER the model posted its reply * through the MCP tool (OpenCode V2 client/server). The bridge only learns that * later, from the channel, so the caller clears the strike it did not deserve - * otherwise one delivered turn plus one transient error would reach the 2-strike * conversation reset and silently forget the thread. */ export declare function clearHarnessSessionFailure(agentId: string, taskId: string, mode: string): void; /** True when a record exists (used to decide whether resume is even possible). */ export declare function hasHarnessSession(agentId: string, taskId: string, mode: string): boolean; /** * Would the next run of this conversation RESUME, or start fresh? (2026-12) * * Read-only: it applies exactly the gates `spawnHarness` applies (record * present, same harness, same cwd) plus the context budget, so a caller can * choose a bigger seed window when the conversation is about to be new * (`chat-reply`). It must never rotate anything itself. */ export declare function conversationWouldResume(agentId: string, taskId: string, mode: string, harness: string, cwd: string, limitTokens?: number | null): boolean; /** * Live harness-session record (2026-10). * * The resume store above is written only when a run FINISHES. A forced stop * kills the CLI before that, so the daemon had no way to learn the * harness-native session id of the run it just killed - and on OpenCode V2 the * shared service keeps executing that session after the CLI dies. * * This record is written the moment the harness session id is captured, keyed * by the BRIDGE session id (the `--session` value the daemon knows). On a * forced stop the daemon reads it, interrupts the harness session, and only * then removes the run scope. A clean exit removes it in `cleanMcp`. */ export interface LiveHarnessSessionRecord { bridgeSessionId: string; harness: string; /** Harness-native session id (opencode `ses_...`, claude/codex id, ...). */ id: string; /** Working directory the harness actually ran in (the run scope when one was created). */ cwd: string; /** `/.teamshare-runs/` when a run scope was used. */ scopeDir: string | null; agentId: string | null; taskId: string | null; mode: string | null; createdAt: string; } /** Persist the live harness id (best-effort; never throws). */ export declare function saveLiveHarnessSession(rec: LiveHarnessSessionRecord): void; /** Load the live harness id for a bridge session, or null. */ export declare function loadLiveHarnessSession(bridgeSessionId: string): LiveHarnessSessionRecord | null; /** Drop the live harness id (clean exit, or after a successful stop). */ export declare function clearLiveHarnessSession(bridgeSessionId: string): void;