/** * Per-adapter resume strategies — the daemon's view of "how do I * continue a previous session for THIS adapter?". * * The driver already has an `AgentCliContinuation` declaration in * the adapter manifest (AIP-45). That covers strategy NAMES * (`native-resume`, `pinned-session`, `transcript`, `none`) but is * silent on the mechanics that live OUTSIDE the runtime — like * "where on disk does this adapter store its conversations?" or * "what argv do I pass to resume into PTY mode?". * * This table is that practical bridge. Each entry declares three * optional hooks: * * outputHint regex matched against each output line; group 1 is * the resume id. Stored as `desc.resumeMetadata[storeAs]`. * fsProbe async function that probes a per-adapter directory * for a stored session id, scoped to a workspace cwd. * Used when `outputHint` never matched. * spawnArgs given a captured id, returns the argv to spawn a * PTY that resumes the session. agentproto then * bypasses ACP/agent-cli and runs the provider * directly — the most reliable resume path because * it leans on the provider's native UX. * * Adapters that don't declare anything here fall back to: * 1. ACP-level resume via `resumeSessionId` on POST /sessions/agent * 2. Fresh spawn (same shape, no continuity) * * Eventually each adapter should ship its own strategy on * `AgentCliHandle.resumeStrategy` (driver-side). This central table * is the bootstrap: it lets a new adapter become resume-capable * without bumping its npm package; once that lands, entries * graduate into the adapter packages. */ /** Where on disk the resume id ends up on the session descriptor. */ type ResumeMetadataKey = "claudeResumeId" | "hermesResumeId" | "codexResumeId" | "openClawResumeId" | "openCodeResumeId" | "mastracodeInprocessResumeId" | "piResumeId"; interface ResumeStrategy { /** When set, run on every output line for this adapter. Group 1 * captures the resume id. The match is saved on the descriptor * as `resumeMetadata[storeAs]`. */ outputHint?: RegExp; /** Where the matched id is recorded on the descriptor. */ storeAs: ResumeMetadataKey; /** Probe the adapter's on-disk session store for the resume id of a * session in the given workspace. Returns the id or null when nothing * eligible. * * When `expectedId` is set the session's own conversation id is known * (agent-cli descriptors carry it as `adapterSessionId`; for * claude-code it IS the on-disk `.jsonl` uuid — see acp-host.ts) and * the probe MUST bind to exactly that transcript: it confirms * `` exists and returns it, else null. It never falls * through to a mtime guess in that case — resuming *some other* * session that merely shares the cwd is a silent cross-session * correctness bug, strictly worse than not resuming at all. * * Only when `expectedId` is absent (e.g. a raw PTY claude session that * never had an ACP session id) does the probe fall back to * most-recently-modified, filtered to files at-or-after `prevStartedAt` * (avoids resuming an unrelated prior conversation). * * `configDir` is the session's isolated provider config dir * (`SessionDescriptor.adapterConfigDir`): a daemon-spawned claude-code * session (#824) persists its transcripts under that dir, not the * provider's global store — the probe must look there. Omit for a * native PTY / pre-#824 session (global store applies). * * Skip when the adapter doesn't persist sessions externally. */ fsProbe?(cwd: string, prevStartedAt: string, expectedId?: string, configDir?: string): Promise; /** Return the argv to spawn a PTY that resumes into the given id. * When omitted, the daemon falls back to ACP-level resume via * the agent-cli protocol instead of the provider's native CLI. */ spawnArgs?(id: string): string[]; /** Env var the resumed PTY needs pointed at the session's isolated * provider config dir to find ITS OWN conversation store — see * `ConversationStore.configDirEnvVar`'s doc (conversation-store.ts) for * why this exists and what silently omitting it breaks. Undefined for * a provider with no config-dir-isolated store. */ configDirEnvVar?: string; /** Absolute transcript path for a conversation id — see * `ConversationStore.transcriptPath`. Present only when the provider's * resume command also accepts a path in place of a bare id. */ transcriptPath?(cwd: string, conversationId: string, configDir?: string): string; } declare const RESUME_STRATEGIES: Record; /** * Helper: which adapters declare any resume capability? Used by * `agentproto sessions restart` to print a helpful "this adapter * doesn't support resume; falling back to fresh spawn" hint when * applicable. */ declare function hasResumeStrategy(adapterSlug: string | undefined): boolean; interface RestartCandidate { adapterSlug?: string; resumeMetadata?: Record; pty?: boolean; adapterSessionId?: string; /** Manifest-declared `capabilities.resumable` for `adapterSlug` (see * `SessionDescriptor.resumable`'s doc) — the honesty gate for the whole * decision tree below. `false` means an `adapterSessionId` here is a * dead end: the adapter cannot rehydrate a prior conversation from it, no * matter how it's presented. Omitted/`true` preserves today's behaviour * (assumed resumable) for every adapter/row that doesn't set this. */ resumable?: boolean; /** * Manifest-declared `capabilities.nativeTerminalResume` for `adapterSlug`. * The `pty-native` strategy requires this flag for ACP-origin sessions. * Legacy PTY rows without the flag can use a newly-added store strategy * when they already carry an exact native resume id; explicit false blocks * that fallback. ACP resumability (`resumable`) does not imply a TUI resume. */ nativeTerminalResume?: boolean; } interface FsProbeCandidate extends RestartCandidate { cwd?: string; startedAt: string; /** The session's isolated provider config dir (`SessionDescriptor. * adapterConfigDir`) — where a daemon-spawned claude-code session's * transcripts actually live since #824. A full `SessionDescriptor` * satisfies this structurally; absent on pre-#824/PTY rows, where the * provider's global store is the right place to probe. */ adapterConfigDir?: string; } /** * Where a restart should land. Computed once so callers never diverge: * * 1. pty-native — the adapter has a captured resume id AND declares * `spawnArgs` (e.g. claude-code): respawn a PTY running the * provider's own resume command. Most reliable CONTINUITY mechanism * when it applies — but only actually reachable by default when the * prior session was ITSELF a real PTY (`prev.pty === true`), or the * caller explicitly opts in (`preferNativeTerminal`) — see the * `mayPreferNative` doc below for why. * 2. pty-plain — the previous session was a real PTY with no * native strategy match: re-run the same argv, no continuity. * 3. agent — an agent-cli session with no native strategy (or an * ACP-origin session where pty-native doesn't apply — see above): * resume at the ACP level via the adapter's own session id (may * still 404 if the adapter never persisted a turn — callers * should retry without `resumeSessionId` on a "not found" error). * 4. unsupported — a generic command session; no history to resume * and no shape to clone reliably. */ type RestartStrategy = { kind: "pty-native"; argv: string[]; } | { kind: "pty-plain"; } | { kind: "agent"; resumeSessionId?: string; resumeFallback?: boolean; } | { kind: "unsupported"; reason: string; }; interface DecideRestartStrategyOptions { /** * Explicit opt-in to provider-native terminal resume for a session whose * ORIGIN was agent-cli/ACP (not itself a PTY) — a human who wants the raw * provider TUI instead of ACP-level resume, understanding the tradeoffs * (see `mayPreferNative`'s doc below). Default false/omitted: an * ACP-origin session always resumes via ACP. */ preferNativeTerminal?: boolean; } declare function decideRestartStrategy(prev: RestartCandidate, opts?: DecideRestartStrategyOptions): RestartStrategy; /** * Matches an adapter's rejection of a `resumeSessionId` it can't honour — * the trigger for the "retry as a fresh spawn" fallback in * `session-restart-core.ts` and the CLI's `sessions restart`. Beyond a * literal "not found" (an unknown/never-persisted id), this also covers the * ACP wrapper-level rejection an adapter with `loadSession: false` throws * when a generic `resumeSessionId` reaches it despite `capabilities. * resumable: true` at the manifest level (e.g. claude-sdk — resumable via * its own SDK session store, but no ACP `session/load` surface): the * `@agentclientprotocol/sdk` server-side dispatcher throws a JSON-RPC * "Method not found" for an unimplemented `session/load`, and an agent may * instead reject with its own "does not support" / "not supported" wording. * Shared by every fallback call site so they can't drift out of sync (see * this module's own header comment on why that matters). */ declare const RESUME_ID_REJECTED_RE: RegExp; /** * Matches the OTHER way a resume attempt dies: the freshly-spawned adapter * took its transport down while the resume RPC was still in flight, so the * ACP SDK rejected the pending `session/load` with its connection-close * error instead of a protocol-level answer. * * This is what a `session_restart` of a transport-dead session looked like * in the field (2026-09-25, `sess_950d1251`): the restart spawned a new * wrapper, the wrapper died mid-`loadSession`, and the operator got a bare * "ACP connection closed" — no fallback, because the message says nothing * about the id being rejected, which is all {@link RESUME_ID_REJECTED_RE} * looks for. * * Treated as a fallback trigger for the same reason a rejected id is: the * stored conversation could not be rehydrated, and a fresh spawn (plus the * daemon-transcript digest) is strictly better than handing the operator an * error and no session. It costs exactly one retry — if the adapter is * broken in general rather than on this id, the retry fails too and the real * error propagates. */ declare const RESUME_CONNECTION_LOST_RE: RegExp; /** * Generic filesystem-fallback: for each known adapter strategy that * defines an `fsProbe`, run it against this session's cwd. If a * resume id is found, attach it to the descriptor's resumeMetadata. * * Lets `restart` recover continuity even when our own output sniffer * missed the resume hint (session killed too quickly, output buffered * past the kill, …). Eligible only when the file is at-or-after the * session's `startedAt` (avoid resuming an unrelated prior session * in the same cwd — see ResumeStrategy.fsProbe comment). * * Fix: when NO `adapterSessionId` was ever captured (session killed * before the ACP handshake), run a recency-window probe. When an * `adapterSessionId` exists but points to a missing JSONL, the probe * tries exact-bind and returns nothing — that's correct, we NEVER * silently resume a sibling conversation (safety: prevents cross- * session contamination). The adapter-level resume will fail with * "not found", triggering the fresh-spawn fallback + digest injection. */ declare function augmentWithFsResume(prev: T): Promise; /** What `probeNativeTranscript` found for a candidate's own transcript. */ interface NativeTranscriptProbe { /** Directory that was (or would be) probed for the transcript. */ dir: string; /** Absolute path of the transcript file the id maps to. */ path: string; /** Whether that exact file exists on disk right now. */ exists: boolean; } /** * Locate the candidate's own on-disk transcript for a provider whose native * resume command accepts an absolute path (`ResumeStrategy.transcriptPath`). * Returns `undefined` when the adapter declares no `transcriptPath`, or the * candidate has no cwd / no conversation id to map — callers treat that as * "nothing to upgrade, and no directory worth naming in diagnostics". * * The id lookup mirrors `decideRestartStrategy`'s (`resumeMetadata[storeAs]`), * falling back to `adapterSessionId` (for claude-code the ACP session id IS * the on-disk `.jsonl` uuid — acp-host.ts pins them equal) so a probe can * still name the transcript it looked for when the sniffer never fired. * `exists: false` is the honest "transcript not found" answer — never fall * through to a sibling file (cross-session contamination, see fsProbe's doc). */ declare function probeNativeTranscript(prev: FsProbeCandidate): Promise; /** * Describe which resume path a restart used, for CLI banners / MCP * responses. Returns a short phrase like "resumed via claude --resume", * or "resumed via ACP", or "" when no resume was attempted. * * Honesty gate (resume-honesty fix): "resumed via ACP" is a claim of actual * conversation continuity, so it's only returned when the adapter's * declared capability backs that claim (`resumable !== false`). An adapter * that declared `resumable: false` but still carries an `adapterSessionId` * gets an honest degraded label instead of the lie — never silently "". * * Origin gate (mirrors `decideRestartStrategy`'s own — MUST stay in sync or * this label lies about what actually happened): the native-resume phrasing * is only reported when `decideRestartStrategy` itself would have picked * pty-native for this `prev` — i.e. `prev.pty === true` or the caller passed * the SAME `preferNativeTerminal` it decided the strategy with. Without this * gate, a captured `resumeMetadata` alone used to make this function claim * "resumed via claude --resume" even when the actual decision (now) * downgrades an ACP-origin session to ACP-level resume instead. */ declare function describeResumePath(prev: RestartCandidate, opts?: DecideRestartStrategyOptions): string; /** * Shell-style argv tokenizer for descriptors that don't carry `argv` * separately (legacy persisted rows predating that field). Same rules * as the spawn-dialog tokenizer in the web app: whitespace splits, * single + double quotes group. */ declare function tokenizeCommand(s: string): string[]; export { type DecideRestartStrategyOptions, type FsProbeCandidate, type NativeTranscriptProbe, RESUME_CONNECTION_LOST_RE, RESUME_ID_REJECTED_RE, RESUME_STRATEGIES, type RestartCandidate, type RestartStrategy, type ResumeMetadataKey, type ResumeStrategy, augmentWithFsResume, decideRestartStrategy, describeResumePath, hasResumeStrategy, probeNativeTranscript, tokenizeCommand };