/** * @file Shared types for the locked-down AI agent surface. Socket runs * Claude / Codex / OpenCode / Gemini through CLI subprocesses, never via SDK * calls. These types model the cross- agent contract: a permission mode, a * tool allowlist, a tool denylist, a prompt, a working directory. Per-agent * flag translation lives in `spawn.ts` — callers pass the uniform * `SpawnAiAgentOptions` and the helper produces the right CLI args for the * chosen agent. */ /** * The set of AI agents the helper can drive. Each must be installed as a CLI on * PATH; discovery happens via `discoverAiAgents()`. */ export type AiAgentName = 'claude' | 'codex' | 'gemini' | 'opencode'; /** * Permission mode passed to the agent's CLI. Socket's rule (CLAUDE.md * "Programmatic Claude calls") requires every headless call to set one of these * explicitly — never the agent's default. * * - `acceptEdits` — Edit/Write proceed without prompting. Required for fix-mode * skills; `dontAsk` would DENY rather than auto-accept. * - `dontAsk` — read-only / no-mutation use. AskUserQuestion calls are * suppressed. * - `plan` — research / proposal mode. The agent can read but cannot write or run * tools that mutate state. */ export type PermissionMode = 'acceptEdits' | 'dontAsk' | 'plan'; /** * Reasoning-effort level for the agent session. Two agents consume it, each via * its own flag — `buildArgs` translates per-agent: * * - Claude: `--effort `, accepts low/medium/high/xhigh/max. * - Codex: `-c model_reasoning_effort=`, accepts minimal/low/medium/high/ * xhigh (no `max` — `buildArgs` clamps `max` → `xhigh`, codex's ceiling). * * Gemini / opencode have no reasoning-effort flag, so they ignore it. Pair a * cheap model with low effort for mechanical work and reserve high/max for * tasks that genuinely need deeper reasoning. */ export type AiEffort = 'high' | 'low' | 'max' | 'medium' | 'xhigh'; /** * Result of an agent spawn. Consumers should inspect `exitCode` before using * `stdout` — non-zero usually means the agent's CLI itself rejected, not just * that the prompt failed. */ export interface AgentSpawnResult { readonly attempts: number; readonly durationMs: number; readonly exitCode: number; /** * True when the spawn failed because the API stayed overloaded (HTTP 529 / * "Overloaded") through every retry. Lets a caller distinguish "back off and * retry later" from "the work genuinely failed" — both surface as a non-zero * `exitCode`, so this is the only way to tell them apart. */ readonly overloaded: boolean; readonly stderr: string; readonly stdout: string; /** * True when the selected MODEL could not serve the request — it is offline * (e.g. "Claude Fable 5 is currently unavailable") or gated/absent for this * account ("issue with the selected model … may not exist or you may not have * access"). Distinct from `overloaded`: the model is not coming back within a * retry window, so the right response is to FALL OVER to the next agent in * the tier chain rather than retry the same one. Both surface as a non-zero * `exitCode`; this flag is how a caller knows to route elsewhere. */ readonly unavailable: boolean; } /** * Inputs to a single agent spawn. * * Required: `prompt`, `cwd`, `tools`, `disallow`, `permissionMode`. * * Pre-built profiles in `profiles.ts` cover the common shapes (the `AI_PROFILE` * capability ladder) — callers spread a tier and override per-call (model, * timeout, addDirs). * * Why the lockdown fields are required rather than defaulted to a permissive * shape: the CLAUDE.md rule says "all four lockdown flags MUST be set on every * spawn." Making them required at the type level is the type-system version of * that rule. */ export interface SpawnAiAgentOptions { /** * Optional explicit agent. Defaults to claude when discovered. */ readonly agent?: AiAgentName | undefined; /** * Allow-list extras (Bash glob patterns, MCP tools, etc.). */ readonly allow?: readonly string[] | undefined; /** * Extra dirs the agent can read (e.g. parent of cwd for monorepo). */ readonly addDirs?: readonly string[] | undefined; /** * Tool denylist — required to enforce the lockdown. */ readonly disallow: readonly string[]; /** * Reasoning-effort level (claude `--effort`); agent default if absent. * claude-specific — other agents ignore it. */ readonly effort?: AiEffort | undefined; /** * Extra environment variables for the spawned agent process, merged over the * inherited env. Gated off by default and inherit-only when absent. Used by * egress-containment callers to inject `HTTP_PROXY` / `NODE_OPTIONS`; no * effect when omitted. */ readonly env?: Readonly> | undefined; /** * Override the agent's flag list (rare; for one-off advanced cases). */ readonly extraArgs?: readonly string[] | undefined; /** * Model name override; agent default if absent. */ readonly model?: string | undefined; /** * Permission mode — see PermissionMode docstring. */ readonly permissionMode: PermissionMode; /** * The prompt sent on stdin. */ readonly prompt: string; /** * Repository / working directory for the agent. */ readonly cwd: string; /** * Per-call timeout (ms). Caller should set for predictable bounds. */ readonly timeoutMs?: number | undefined; /** * Tool allowlist. Required to enforce the lockdown. */ readonly tools: readonly string[]; } /** * The cwd-aware "is this binary on PATH?" snapshot. Each entry is the absolute * path resolved by `which`; missing agents are absent from the map (not * present-with-undefined). */ export type DiscoveredAgents = Readonly>>; /** * Cleanup policy for `spawnAiAgentsInWorktrees`. Default 'always'. * * - `always` — remove the worktree after the per-item function returns, * regardless of success/failure or whether changes were committed. Matches * the "leave no trace" expectation. * - `never` — never auto-remove. The caller or a human inspector cleans up. * Useful for debugging. * - `on-empty` — remove only if the worktree had no changes (committed or * staged). Preserves evidence on failure. */ export type WorktreeCleanup = 'always' | 'never' | 'on-empty';