/** * Runtime seam for headless one-shot runs (T003 of the one-shot CLI bridge plan). * * Owns everything about a RUNTIME that the shared runner machinery must not * know: how to build argv for a one-shot invocation (fresh thread vs continued) * and how to read that runtime's NDJSON stdout back into fold turns. The * generic lifecycle — spawn, per-write-target concurrency slot, durable event * log, timeout + kill grace — stays in claude-runner.ts and run-event-log.ts; * an adapter here is pure policy over argv and wire format, so adding a runtime * never touches the runner. * * The registry is keyed by `SessionRuntimeType`. A runtime type without a * registered adapter (`getRunDriver` → undefined) is one the web server cannot * drive one-shot yet — callers decide whether that is a refusal or a fallback. * * Normalizers are total functions over raw stdout text: they never throw, they * tolerate unparsable lines by counting them (`skippedLines`, the same * wire-format-drift signal the claude reader reports), and every field access * on a decoded event is defensive because the wire schema is the runtime's to * change. The turns they produce are shaped exactly like what `foldRunEventLog` * appends through `appendSessionTurn` — assistant text plus one turn per tool * call — so a run's transcript fold needs no runtime-specific code downstream. */ import type { SessionRuntimeType } from "../utils/storage-utils.js"; /** * One turn a run folds into the transcript sidecar — the assistant variant of * `SessionTurnInput` (run-transcript.ts), minus the `run` tag the fold adds. * A tool call rides as its own turn with empty text, exactly as * `foldAssistantEvent` emits them. */ export interface RunFoldTurn { type: "assistant"; text: string; tool?: { name: string; }; } /** Input for building one headless invocation's argv. */ export interface OneShotRunInput { /** The staged user message; may be multiple words. Never mutated. */ message: string; /** Thread title for a FRESH thread; ignored when continuing. */ title?: string; /** * Runtime session id to continue. Absent (or blank) means a fresh thread — * except a blank-but-present id, which throws: silently minting a new thread * would fork conversation state on what is clearly a caller bug. */ runtimeSessionId?: string; /** * Directory for the runtime's session storage when continuing (pi: * `--session-dir`), so a runtime that stores sessions under the caller's cwd * keeps the same store across cwd changes. Adapters that have no such flag * ignore it, and every adapter must work without it — the caller wiring that * supplies it lands separately. */ sessionDir?: string; } /** What one run's raw stdout folded down to. */ export interface RunOutputFold { /** Turns in stream order, ready for `appendSessionTurn` (minus the run tag). */ turns: RunFoldTurn[]; /** Concatenated text-part content — the run's reply text. */ replyText: string; /** * The runtime's own session id, taken from the first stdout line that * carried one. This is what lets a first turn lazily mint the session's * `runtimeSessionId`: the caller persists it after the run settles. */ runtimeSessionId?: string; /** Message of the last `error` event the stream carried, when any. */ error?: string; /** Lines skipped: unparsable JSON, non-object JSON, or an unknown event type. */ skippedLines: number; } /** What one runtime contributes to the shared runner machinery. */ export interface RunDriverAdapter { /** The session runtime type this adapter drives. */ readonly runtimeType: SessionRuntimeType; /** Binary spawned for this runtime (PATH-resolved at spawn time). */ readonly binary: string; /** Argv after the binary, for a fresh vs continued one-shot invocation. */ buildArgv(input: OneShotRunInput): string[]; /** Normalizes raw NDJSON stdout into fold turns + session id + reply/error. */ foldOutput(raw: string): RunOutputFold; } /** Registers (or replaces) the adapter for a runtime type. Idempotent. */ export declare function registerRunDriver(adapter: RunDriverAdapter): void; /** * The adapter for a runtime type, or undefined when that runtime has no * one-shot driver yet (a future runtime type is simply not drivable). */ export declare function getRunDriver(runtimeType: SessionRuntimeType): RunDriverAdapter | undefined; /** * Every runtime type with a registered one-shot adapter, in registration order. * The runners route enumerates the drivable surface from this rather than a * maintained list, so a driver added later shows up automatically. */ export declare function getRunDriverRuntimeTypes(): SessionRuntimeType[]; /** * Argv for `opencode run --format json`. * * Fresh thread: ["run", "--format", "json", "--title", , <message>] * Continuation: ["run", "--format", "json", "-s", <runtimeSessionId>, <message>] * * The message travels as ONE argv element even though it may contain many * words — the CLI accepts multiple positionals, but a single element cannot be * re-tokenized by a shell and keeps the staged message byte-identical. A fresh * thread without a title falls back to opencode's own truncated-prompt title. */ export declare function buildOpencodeRunArgv(input: OneShotRunInput): string[]; /** * Normalizes raw `opencode run --format json` stdout into fold turns. * * Every line is expected to be one JSON object `{type, timestamp, sessionID, * part?|error?}` with type ∈ step_start | text | step_finish | tool_use | * reasoning | error — but the schema is opencode's to change, so nothing here * is fatal: unparsable lines are skipped and counted, unknown types are skipped * and counted, and `sessionID` is harvested from ANY decoded object line (the * contract puts it on every emitted line; taking it from an unexpected shape * still beats losing it). Consecutive `text` events coalesce into one turn — * they are chunks of one reply segment — and each named `tool_use` becomes its * own turn, mirroring how the claude fold reads content blocks. `reasoning` * events are known but folded by nothing: thinking is not the reply. */ export declare function foldOpencodeOutput(raw: string): RunOutputFold; /** * Argv for `pi -p --mode json`. * * Fresh thread: ["-p", "--mode", "json", <message>] * Continuation: ["-p", "--mode", "json", "--session-id", <id>, <message>] * plus `--session-dir`, <dir> when the caller supplies sessionDir, so pi's * session store stays stable across cwd changes. * * Allow-all policy: NO tool-permission flags — the old ask/change intent * machinery is being deleted project-wide and runs are full-tool, so the path * has no permission vocabulary to emit. The message travels as ONE argv * element exactly like opencode's, so a multi-word staged message stays * byte-identical and can never be re-tokenized by a shell. */ export declare function buildPiRunArgv(input: OneShotRunInput): string[]; /** * Normalizes raw `pi -p --mode json` stdout into fold turns. * * Every line is expected to be one JSON object from the event stream * (`session` header, `message_update` deltas, `tool_execution_start`/`_end`, * agent/turn bookkeeping) — but the schema is pi's to change, so nothing here * is fatal: unparsable lines and unknown event types are skipped and counted, * the session id is harvested from the `session` header line only, and * consecutive `text_delta`s coalesce into ONE assistant turn (they are chunks * of one reply segment). Each named `tool_execution_start` becomes its own * tool turn after flushing pending text, mirroring the opencode tool fold; * `thinking_delta` and the tool result carries are folded by nothing. */ export declare function foldPiOutput(raw: string): RunOutputFold; /** * Argv for `claude -p --output-format json`. * * Fresh thread: ["-p", "--output-format", "json", "--dangerously-skip-permissions", <message>] * Continuation: ["-p", "--output-format", "json", "--dangerously-skip-permissions", "--resume", <id>, <message>] * * `--dangerously-skip-permissions` is REQUIRED so a headless `-p` run never * blocks on an approval prompt — the approved allow-all design, with the * diff-review safety gate living in the web UI rather than the driver. Runs * through this driver reach the child verbatim (streamJsonArgv: false at the * call site), so the fold below reads exactly this output contract. */ export declare function buildClaudeCodeRunArgv(input: OneShotRunInput): string[]; /** * Normalizes raw `claude -p --output-format json` stdout into fold turns. * * Line shapes: `system` (init carries the `session_id`), `user` (echoed turn), * `assistant` (message with content blocks), `result` (terminal envelope with * `result` text and `is_error`). The legacy pre-stream envelope — a single * `{is_error, result}` object with no `type` — is recognized by shape alone, * exactly as the built-in runner reader does. Tolerant by construction: every * field access is defensive, unparsable/unknown lines are skipped and counted, * and the session id is harvested from any decoded line that carries one. * Text blocks of one assistant message flush as ONE turn with tool_use blocks * as their own turns in block order (the run-event-log's claude fold shape); * a terminal `result` string is the reply-text fallback only when no assistant * content was folded, and a failing `result` (is_error) surfaces as `error` * instead of reply text — mirroring the runner's reply precedence. */ export declare function foldClaudeCodeOutput(raw: string): RunOutputFold; /** * Argv for `codex exec --json`. * * Fresh thread: ["exec", "--json", "--sandbox", "workspace-write", <message>] * Continuation: ["exec", "resume", <id>, "--json", "--sandbox", "workspace-write", <message>] * * `--sandbox workspace-write` is the full-write sandbox this allow-all design * runs under; `--json` is the NDJSON event stream the fold reads. The resume * id is the `thread_id` a settled run harvested from its `thread.started` * line. Runs through this driver reach the child verbatim * (streamJsonArgv: false at the call site). */ export declare function buildCodexRunArgv(input: OneShotRunInput): string[]; /** * Normalizes raw `codex exec --json` stdout into fold turns. * * EXTRA conservative on purpose: the codex wire has drifted between builds * (the observed 0.150.1 emits `item.*` events; the documented shape wraps * agent text in `agent_message` payloads), so nothing here may throw and * anything unrecognized is skipped and counted. Text folds from agent-message * items (consecutive ones coalesce into one turn), tool turns come from * `command_execution` start items (one per invocation, named by the item * type), the thread id is harvested from any line that carries one, and * ambiguous shapes fold text only — the panels tick tools by name and tolerate * missing detail. */ export declare function foldCodexOutput(raw: string): RunOutputFold; //# sourceMappingURL=run-driver.d.ts.map