Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

105 KB
Raw Blame
#!/usr/bin/env node
// Fake of an ACP CLI's stdio surface (JSON-RPC 2.0: initialize, authenticate, session/new,
// session/prompt and scripted session/update streams) for driver tests. Env knobs:
//   FAKE_ACP_LOAD_NULL  session/load returns null, so the driver falls through to session/new
//   FAKE_ACP_REJECT_LIVE_LOAD_FILE  while it exists, load/resume of a session live in this process errors
//     (FAKE_ACP_REJECT_LIVE_LOAD_DSH=1: with dsh 0.2's -32602 "session is already active" instead of -32000)
//   FAKE_ACP_CACHED_LIVE_LOAD  a live session/load is acknowledged but keeps the old MCP credentials, like Qwen
//   FAKE_ACP_COPY_LIVE_LOAD=1  a session/load of a session still open adds a second copy, and each open copy streams
//     FAKE_ACP_TEXT_REPLY again, like mistral-vibe 3.1.2; session/close ends every copy
//   FAKE_ACP_TEXT_REPLY  answer every session/prompt with FAKE_ACP_UPDATES_FILE's updates, then only this text
//   FAKE_STREAM_SCRIPT, FAKE_STREAM_CADENCE  a scripted reply chosen by a marker in the prompt (stream-script.ts)
//   FAKE_ACP_MODE  happy (default) | image | empty-reply | reasoning-only | exit-early | fail-after-text | crash-in-prompt
//     | abort-in-prompt (a sign-in link, then a TCMalloc CHECK line on stderr, then exit 134 as bubblewrap reports SIGABRT) | hang
//     | hang-initialize | unkeyed-tool | finished-tools (tool_call frames that arrive already completed or failed, one updated again)
//     | no-auth | auth-required | auth-required-api-key (session/new fails until authenticate carries
//       _meta.api_key === FAKE_ACP_EXPECTED_API_KEY; the request is dumped to FAKE_ACP_DUMP.authenticate.json)
//     | permission | question
//     | ask-question (a mid-prompt vendor question: FAKE_ACP_ASK_METHOD, default cursor/ask_question, params
//       FAKE_ACP_ASK_PARAMS; the client's reply is written to FAKE_ACP_ASK_ANSWER. FAKE_ACP_ASK_TIMING=with-result
//       settles the turn with the card open; after-result asks between turns)
//     | interleave (message, tool, message, tool, message) | no-session-config (set_mode/set_model answer -32601)
//     | set-model-internal-error (set_model answers -32603)
//     | ask-peer | delegate-peer | chief-delegate | create-peer (coordinate_bots through the injected agents
//       MCP; FAKE_ACP_REDELEGATE_FILE, FAKE_ACP_TASKID_FILE; a hand-off result wake gets a fixed line)
//     | echo-gated (echo the whole prompt; hold until FAKE_ACP_GATE_FILE exists) | safe-agent-reads
//     | stall-after-text | stall-after-tool (then silent forever) | slow-tool (silent for FAKE_ACP_TOOL_MS, or until
//       FAKE_ACP_TOOL_GATE exists when set; FAKE_ACP_TOOL_OUTPUT is the text its running call shows meanwhile)
//     | quiet-then-answer (silent for FAKE_ACP_QUIET_MS; with QWEN_HOME, append FAKE_ACP_QWEN_LOG lines to
//       Qwen's debug log every FAKE_ACP_LOG_EVERY_MS; with XDG_DATA_HOME, append FAKE_ACP_OPENCODE_LOG lines once to
//       OpenCode's shared log, {session} replaced by the session id) | mcp-tools (list each mcpServers entry's tools with
//       grok 1.0.25's initialize, or FAKE_ACP_MCP_HANDSHAKE; FAKE_ACP_MCP_SCHEMA_BYTES=1 adds each schema's length)
//     | lend-question (list_shared_computers around a question)
//     | list-peers (list_bots through the agents MCP; keeps the first agents server it is offered, like Hermes)
//     | background-wake (OpenCode 2.3.1+: the first prompt's shell call moves to the background and the prompt ends, or with
//       STOP_ME in it waits for session/cancel; FAKE_ACP_WAKE_MS (300) later that shell's finish wakes a turn no prompt
//       asked for, one step every FAKE_ACP_STEP_MS (50), each appended to FAKE_ACP_WOKEN_FILE, until session/close or
//       session/delete interrupts it; only session/delete ends the job. session/fork answers <id>-fork; a closed or
//       deleted session refuses session/prompt until loaded. Advertises close (unless FAKE_ACP_NO_CLOSE=1), delete and, unless
//       FAKE_ACP_NO_FORK=1, fork; FAKE_ACP_CLOSE_ENDS_JOBS=1: session/close ends the job too, as grok 1.0.89's does;
//       FAKE_ACP_LATE_UPDATE=1: a tool_call_update of the finished prompt arrives 50 ms after its answer)
//   FAKE_ACP_GROK_NATIVE_MCP  _x.ai/mcp/list reports these comma-separated native servers after the session's accepted ones
//   FAKE_ACP_MCP_TRANSPORTS  remote MCP transports advertised in initialize, e.g. "http,sse"
//   FAKE_ACP_CAPABILITIES_META  JSON initialize advertises as agentCapabilities._meta
//   FAKE_ACP_MCP_HOLD=1  every session connects its MCP servers at once and keeps the stdio ones running, as Grok does
//   FAKE_ACP_PERMISSION_OPTIONS  JSON options override in permission mode
//   FAKE_ACP_PERMISSION_PRIOR_UPDATES  JSON array of tool_call/tool_call_update updates sent before the process's first
//     permission request only, as Mistral Vibe does before a request that names the call by toolCallId alone
//   FAKE_ACP_DUMP  path for {argv, env}; with FAKE_ACP_MODES or FAKE_ACP_MODELS also <dump>.prompts.jsonl
//     and <dump>.initialize.json for the initialize params
//   FAKE_ACP_LAUNCH_COUNT_FILE  process counter incremented at startup
//   FAKE_ACP_VERSION  what `--version` prints (default "fake-acp 1.0.0"); a CLI the driver checks the shape of needs its own
//   FAKE_ACP_PROMPT_STDERR  a line written to stderr when a prompt arrives; FAKE_ACP_PROMPT_STDERR_THEN=hang leaves it open
//   FAKE_ACP_RPC_DUMP  this process's method list, rewritten after every request
//   FAKE_ACP_RPC_APPEND_FILE  one {"pid","method","cwd"} line appended per request
//   FAKE_ACP_RPC_FAILURE_FILE  JSON-RPC error for session/prompt; once read the process stays poisoned
//   FAKE_ACP_RPC_FAILURE_METHOD  initialize, authenticate, session/new or session/prompt (default)
//   FAKE_ACP_RPC_FAILURE_GATE  hold the error until this file exists
//   FAKE_ACP_RPC_FAILURE_ONCE  fail once, then answer as usual (the failure file is removed)
//   FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT  emit text and a tool result before failing
//   FAKE_ACP_RPC_FAILURE_AFTER_USAGE  emit the scripted usage updates and FAKE_ACP_UPDATES_FILE's before failing
//   FAKE_ACP_LOAD_ERROR  JSON-RPC error returned by session/load
//   FAKE_ACP_LOAD_REPLAY  session/load first replays this many history updates of the session (REPLAYED-USER-<n>,
//     REPLAYED-AGENT-<n>), marked _meta.isReplay with FAKE_ACP_LOAD_REPLAY_MARKED=1; FAKE_ACP_LOAD_UNANSWERED=1 then leaves
//     the load unanswered, as grok 1.0.x can
//   FAKE_ACP_GROK_EFFORT=1  sessions report grok 1.0.41's `reasoning_effort` select (starting on --reasoning-effort, else
//     high) and session/set_config_option changes it; =refuse answers that set with -32602
//   FAKE_ACP_GROK_LOGIN  what `models` says of the login, as grok 1.0.25 prints it: signed-in | signed-out | refreshes (signed
//     out once, then signed in, as a token refreshed by the first call) | hang | signed-out-hang (says signed out, then hangs
//     as a slow token refresh would); FAKE_ACP_GROK_LOGIN_COUNT counts the calls
//   FAKE_ACP_MODELS  model ids: opencode-shaped configOptions and set_config_option (-32602 for others);
//     a new session starts on AGY_ACP_DEFAULT_MODEL when set, taken unchecked as agy_acp_server 1.1.1 does
//   FAKE_ACP_MODELS_FILE  the same list, read once at startup, as `opencode acp` never re-reads a login
//   FAKE_ACP_MODELS_REFRESH_FROM  `models --refresh` copies this file over FAKE_ACP_MODELS_FILE, as OpenCode 1.19.4
//     refetches its models.dev cache only then; OPENCODE_CONFIG_CONTENT's provider models join any model list
//   FAKE_ACP_MODELS_LIST=v2  OpenCode 2: `models` answers empty with exit 0, `models --verbose` and `models --refresh` exit 1;
//     with FAKE_ACP_MODELS_REFRESH_FROM a running process refreshes its own list after FAKE_ACP_SELF_REFRESH_MS (default 300)
//   FAKE_ACP_MODELS_LOCK  path: as OpenCode 1.19.4, a `models` or `acp` process without OPENCODE_DISABLE_MODELS_FETCH=1
//     starts a catalog fetch under this lock and, ended early, leaves it; `models --refresh` fails while it is held
//   FAKE_ACP_MODELS_LIST=auto-only  Cursor CLI 2026.10.22: `models` lists only `auto`
//   FAKE_ACP_VERBOSE_FAIL_ONCE  marker path: the first `models --verbose` fails, like a new OpenCode home's first run
//   FAKE_ACP_MODEL_STICKS  set_config_option succeeds but keeps the old model
//   FAKE_ACP_OMIT_MODEL_VALUE  when the live model equals this id, the model option has no currentValue
//     (Copilot CLI 1.0.105 does this for auto)
//   FAKE_ACP_EXIT_ON_MODEL  the process exits when set_config_option selects this model
//   FAKE_ACP_HANG_ON_MODEL  set_config_option never answers a switch to this model, as a stuck account can; other requests
//     are still answered
//   FAKE_ACP_VARIANTS  JSON map model -> {id?, currentValue?, options} of ACP select values
//   FAKE_ACP_CONFIG_UPDATES  JSON [{after, sessionId?, configOptions, replay?}] emitted after the named response
//   FAKE_ACP_USAGE_ROOT  usage at the prompt result's root, as opencode 1.18.18 sends it
//   FAKE_ACP_USAGE_UPDATES_FILE  JSON numbers re-read per prompt, each sent as usage_update {used, size: 200000}
//   FAKE_ACP_UPDATES_FILE  JSON array of session/update params ({update, _meta?}) re-read per prompt, sent as-is
//   FAKE_ACP_AUTOPILOT=1  set_config_option takes Kiro 3.1.0's `autopilot` ("on" or "off")
//   FAKE_ACP_MODE_ACK_FILE  while it exists a mode switch answers "empty" (no configOptions) or "error"
//   FAKE_ACP_HANG_ON  a prompt whose text holds this hangs as in mode hang; others answer
//   FAKE_ACP_CURSOR_FAST=1  a `fast` select ("true") once initialize declares _meta.parameterizedModelPicker, as Cursor CLI 2026.10.19; =stuck acknowledges set_config_option but keeps "true"
//   FAKE_ACP_SESSION_CLOSE=1  initialize advertises sessionCapabilities.close (OpenCode 1.19); session/close answers {}
//     after FAKE_ACP_SESSION_CLOSE_MS; =error advertises it and answers every session/close with an error
//   FAKE_ACP_SESSION_ID  the first session's id (default fake-acp-session); later ones add -2, -3, …
//   FAKE_ACP_SESSION_DELETE=1  initialize advertises sessionCapabilities.delete (OpenCode 2.2.6); session/delete always answers {}
//   FAKE_ACP_IGNORE_CANCEL=1  session/cancel is ignored: a hanging prompt keeps running
//   FAKE_ACP_TOOL_LIMIT  session/prompt lists the session's MCP tools and refuses, as OpenCode Zen does, past this many;
//     under it the reply is "tools: <server>=<count>,…"
//   FAKE_ACP_CANCEL_STOP_REASON  the stopReason a cancelled hanging prompt answers (default cancelled; OpenCode answers end_turn)
//   FAKE_ACP_HANG_ONCE_FILE  with FAKE_ACP_HANG_ON, only the first matching prompt hangs; it writes this file
//   FAKE_ACP_HOLD_FILE  while this file exists, a session/prompt waits before it is answered
//   FAKE_ACP_GATEWAY_PID_FILE  the first `acp` process starts a stand-in for Hermes' shared gateway the way Hermes does
//     (a session of its own, HERMES_GATEWAY_DETACHED=1, `gateway run`) and writes its pid here; later ones leave it be
//   FAKE_ACP_AGY_STEPS_FILE  JSON [[stepType, text], …] re-read per prompt and appended to the session's log under
//     GEMINI_HOME (antigravity-acp/conversations/<session>.db, a folder made at startup), as agy_acp_server 1.4.5
//     writes its trajectory
//   FAKE_ACP_MODE=hermes-steer  hermes-agent's ACP `/steer`: sessions advertise `steer`; a prompt says "working. ", waits for
//     FAKE_ACP_GATE_FILE, then says "done" and every steer it took; `/steer <text>` during it answers "⏩ Steer queued for
//     the active turn: …" (FAKE_ACP_STEER_FAIL=1: "⚠️ Steer failed: …"; =rpc: a JSON-RPC error), and with no prompt running it runs <text>
//     as a prompt that says "ran: <text>". FAKE_ACP_STEER_HOLD_FILE holds a `/steer` while it exists;
//     FAKE_ACP_HERMES_COMMANDS lists the commands sessions advertise (default help,steer,queue).
//   FAKE_ACP_MODE=opencode-steer  OpenCode 2.3's `_session/steering`: initialize reports _meta.steering.supported; a prompt says
//     "working. ", waits for FAKE_ACP_GATE_FILE, then says "done" and every steer it took. `_session/steering` answers
//     {outcome:"steered"}, or {outcome:"promptRequired"} with no prompt running (FAKE_ACP_STEER_FAIL=rpc: a JSON-RPC error).
//   FAKE_ACP_MODE=devin-steer  Devin's second `session/prompt`: initialize reports agentCapabilities._meta["cognition.ai/midTurnPrompts"];
//     a prompt during a running one joins it, and both end together with the same usage and _meta["cognition.ai/userMessageId"].
//   FAKE_ACP_MODE=grok-steer  Grok's second `session/prompt` with `[ui] follow_up_behavior = "steer"` in GROK_HOME/config.toml: a prompt
//     during a running one is injected into it and answers {stopReason:"cancelled", _meta:{completionKind:"removedFromQueue"}}
//     at once; the running prompt then ends with end_turn and every steer it took.
//   FAKE_ACP_STEERING=off  the two steer modes above report no steering capability.
//   FAKE_ACP_MODE=grok-complete  grok 1.0.41's `_x.ai/session/prompt_complete`: a prompt says "answered", then sends the
//     notification {sessionId, promptId: the prompt's _meta.promptId, stopReason: FAKE_ACP_COMPLETE_STOP (end_turn),
//     agentResult: FAKE_ACP_COMPLETE_RESULT (null)}, then answers per FAKE_ACP_COMPLETE_REPLY: "after" (default) at once
//     with that stopReason and usage, "never", "late" after FAKE_ACP_COMPLETE_REPLY_MS (3500) with "late words" and usage,
//     "late-bare" the same reply with no words, or "rate-limit-error" (JSON-RPC -32003). FAKE_ACP_COMPLETE_NAMES (own) lists the prompts the notifications name:
//     own, none (no promptId), task (a background task's task-completed-… id), other-session.
//   FAKE_ACP_START_FAILURE  this line on stderr at startup, then exit 1 before reading anything, as grok 1.0.x does when it
//     cannot start its threads
import { AsyncLocalStorage } from "node:async_hooks";
import { spawn } from "node:child_process";
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync, writeSync } from "node:fs";

const mode = process.env.FAKE_ACP_MODE ?? "happy";
let apiKeyAuthenticated = false;

if (process.env.FAKE_ACP_START_FAILURE) {
  writeSync(2, `${process.env.FAKE_ACP_START_FAILURE}\n`);
  process.exit(1);
}

// The scripted replies every fake plays (FAKE_STREAM_SCRIPT, FAKE_STREAM_CADENCE); inlined, since tests copy this fake
// out of the repo.
type StreamStep = { say?: string; think?: string; tool?: string; fail?: string };
type StreamScript = { steps: StreamStep[]; chars: number; ms: number };
function streamScript(prompt: string): StreamScript | null {
  if (!process.env.FAKE_STREAM_SCRIPT) return null;
  let scripts: Record<string, StreamStep[]>;
  try { scripts = JSON.parse(readFileSync(process.env.FAKE_STREAM_SCRIPT, "utf8")); } catch { return null; }
  // A prompt can carry earlier requests as context: the newest marker in it is the request.
  const marker = Object.keys(scripts).filter((key) => prompt.includes(key)).sort((a, b) => prompt.lastIndexOf(b) - prompt.lastIndexOf(a) || b.length - a.length)[0];
  if (!marker) return null;
  const [chars = 4, ms = 40] = (process.env.FAKE_STREAM_CADENCE ?? "4:40").split(":").map(Number);
  return { steps: scripts[marker]!, chars: Math.max(1, chars), ms: Math.max(0, ms) };
}
function playStreamScript(script: StreamScript, play: { chunk(step: StreamStep, piece: string): void; end(step: StreamStep): void; done(failure?: string): void; cancelled(): boolean }): void {
  const queue = script.steps.slice();
  const next = (): void => {
    if (play.cancelled()) return;
    const step = queue.shift();
    if (!step) return play.done();
    if (step.fail !== undefined) return play.done(step.fail);
    if (step.tool !== undefined) {
      play.end(step);
      setTimeout(next, script.ms);
      return;
    }
    const text = step.say ?? step.think ?? "";
    let at = 0;
    const send = (): void => {
      if (play.cancelled()) return;
      if (at < text.length) {
        play.chunk(step, text.slice(at, at += script.chars));
        setTimeout(send, script.ms);
        return;
      }
      play.end(step);
      setTimeout(next, script.ms);
    };
    send();
  };
  next();
}

// Follow the spawning server down; on Windows ppid does not change after parent exit.
{
  const spawner = process.ppid;
  const orphanWatch = setInterval(() => {
    if (process.ppid !== spawner) process.exit(0);
    try { process.kill(spawner, 0); } catch (error) {
      if ((error as NodeJS.ErrnoException).code === "ESRCH") process.exit(0);
    }
  }, 500);
  orphanWatch.unref();
}
const ONE_PIXEL_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
const modelsText = process.env.FAKE_ACP_MODELS_FILE && existsSync(process.env.FAKE_ACP_MODELS_FILE)
  ? readFileSync(process.env.FAKE_ACP_MODELS_FILE, "utf8")
  : process.env.FAKE_ACP_MODELS ?? "";
// One id per line when the list holds a newline (DeepSeek's wire values are JSON pairs with commas); else comma-separated.
const listedModels = (text: string) => {
  const ids = text.split(text.includes("\n") ? "\n" : ",").map((id) => id.trim()).filter(Boolean);
  try {
    const declared = JSON.parse(process.env.OPENCODE_CONFIG_CONTENT || "{}")?.provider ?? {};
    for (const [provider, entry] of Object.entries<any>(declared)) {
      for (const id of Object.keys(entry?.models ?? {})) if (ids.length && !ids.includes(`${provider}/${id}`)) ids.push(`${provider}/${id}`);
    }
  } catch {}
  return ids;
};
const models = listedModels(modelsText);
if (process.env.FAKE_ACP_MODELS_LIST === "v2" && process.env.FAKE_ACP_MODELS_REFRESH_FROM && process.argv[2] === "acp") {
  setTimeout(() => {
    const upstream = readFileSync(process.env.FAKE_ACP_MODELS_REFRESH_FROM!, "utf8");
    if (process.env.FAKE_ACP_MODELS_FILE) writeFileSync(process.env.FAKE_ACP_MODELS_FILE, upstream);
    models.splice(0, models.length, ...listedModels(upstream));
  }, Number(process.env.FAKE_ACP_SELF_REFRESH_MS ?? 300)).unref();
}
const defaultModelEnv = process.env.AGY_ACP_DEFAULT_MODEL;
if (models.length && defaultModelEnv && !models.includes(defaultModelEnv)) models.push(defaultModelEnv);
let currentModel: string | null = models.length && defaultModelEnv ? defaultModelEnv : models[0] ?? null;
const variantConfigs: Record<string, { id?: string; currentValue?: string; options: any[] }> =
  JSON.parse(process.env.FAKE_ACP_VARIANTS ?? "{}");
let currentVariant = variantConfigs[currentModel ?? ""]?.currentValue;
const variantValues = (entries: any[]): string[] => entries.flatMap((entry) => (
  typeof entry?.value === "string" ? [entry.value] : Array.isArray(entry?.options) ? variantValues(entry.options) : []
));
const modes = (process.env.FAKE_ACP_MODES ?? "").split(",").filter(Boolean);
let currentMode: string | null = modes[0] ?? null;
let currentFast: string | null = null;
const grokEffort = process.env.FAKE_ACP_GROK_EFFORT;
let currentEffort = process.argv.includes("--reasoning-effort") ? process.argv[process.argv.indexOf("--reasoning-effort") + 1]! : "high";
const taskIdFile = process.env.FAKE_ACP_TASKID_FILE ?? "";
const peerIdOf = (list: string): string => /id: ([\w-]+)/.exec(list)?.[1] ?? "";
function handoffCall(message: string, options: { consult?: boolean; reason?: string; doneWhen?: string } = {}): { name: string; args: (list: string) => object } {
  const reason = options.reason ? { reason: options.reason } : {};
  const doneWhen = options.doneWhen ? { done_when: options.doneWhen } : {};
  return { name: "coordinate_bots", args: (list) => ({ bot_ids: [peerIdOf(list)], message, ...(options.consult ? { consult: true } : {}), ...reason, ...doneWhen }) };
}
const handoffIdOf = (reply: string): string => /"requestId":"([\w-]+)"/.exec(reply)?.[1] ?? "";
const statusCall = (id: string): { name: string; args: () => object } => ({ name: "assignment_status", args: () => ({ id }) });
const logFile = process.env.FAKE_ACP_LOG_FILE ?? "";
const rejectLiveLoadFile = process.env.FAKE_ACP_REJECT_LIVE_LOAD_FILE ?? "";
const liveLoadError = (sessionId: string) => process.env.FAKE_ACP_REJECT_LIVE_LOAD_DSH === "1"
  ? { code: -32602, message: `Invalid params: session is already active: ${sessionId}` }
  : { code: -32000, message: "session already active in this agent" };
function fakeLog(line: string): void {
  if (!logFile) return;
  try {
    appendFileSync(logFile, `${new Date().toISOString()} ${line}\n`);
  } catch {}
}
let chiefDelegatedTaskId = "";
function rememberDelegatedTaskId(id: string): void {
  chiefDelegatedTaskId = id;
  if (taskIdFile) {
    try {
      writeFileSync(taskIdFile, id);
    } catch {}
  }
}
function savedDelegatedTaskId(): string {
  if (chiefDelegatedTaskId) return chiefDelegatedTaskId;
  if (taskIdFile && existsSync(taskIdFile)) {
    try {
      chiefDelegatedTaskId = readFileSync(taskIdFile, "utf8").trim();
    } catch {}
  }
  return chiefDelegatedTaskId;
}
const configOptions = () => {
  const variant = variantConfigs[currentModel ?? ""];
  const options = [
    ...(models.length ? [
        {
          id: "model",
          name: "Model",
          category: "model",
          type: "select",
          ...(process.env.FAKE_ACP_OMIT_MODEL_VALUE === currentModel ? {} : { currentValue: currentModel }),
          options: models.map((value) => ({ value, name: value })),
        },
      ] : []),
    ...(variant ? [{
      id: variant.id ?? "effort",
      name: "Effort",
      category: "thought_level",
      type: "select",
      currentValue: currentVariant,
      options: variant.options,
    }] : []),
    ...(modes.length ? [{
      id: "mode",
      name: "Mode",
      category: "mode",
      type: "select",
      currentValue: currentMode,
      options: modes.map((value) => ({ value, name: value })),
    }] : []),
    ...(grokEffort ? [{
      id: "reasoning_effort",
      name: "Reasoning Effort",
      category: "thought_level",
      type: "select",
      currentValue: currentEffort,
      options: ["high", "medium", "low"].map((value) => ({ value, name: value })),
    }] : []),
    ...(currentFast ? [{
      id: "fast",
      name: "Fast",
      type: "select",
      currentValue: currentFast,
      options: [{ value: "true", name: "On" }, { value: "false", name: "Off" }],
    }] : []),
  ];
  return options.length ? options : null;
};
// FAKE_ACP_SESSION_MODELS "id|Name,id|Name": cursor-shaped `models.availableModels` whose ids
// (`default[]`) differ from argv slugs (`auto`); commas inside `[...]` do not split an id.
const acpModels = (process.env.FAKE_ACP_SESSION_MODELS ?? "")
  .split(/,(?![^[]*\])/)
  .filter(Boolean)
  .map((entry) => {
    const [modelId, name] = entry.split("|");
    return name ? { modelId, name } : { modelId };
  });
const sessionModels = () =>
  acpModels.length ? { currentModelId: acpModels[0].modelId, availableModels: acpModels } : null;
const sessionModesList = (process.env.FAKE_ACP_SESSION_MODES ?? "").split(",").filter(Boolean);
let currentSessionMode: string | null = sessionModesList[0] ?? null;
const sessionModesPayload = () =>
  sessionModesList.length
    ? { currentModeId: currentSessionMode ?? sessionModesList[0], availableModes: sessionModesList.map((id) => ({ id, name: id })) }
    : null;

const argv = process.argv.slice(2);
const dumpEnv = Object.fromEntries(
  [
    "PATH",
    "HOME",
    "USERPROFILE",
    "NO_PROXY",
    "no_proxy",
    "PI_CODING_AGENT_SESSION_DIR",
    "TEMP",
    "TMP",
    "SystemRoot",
    "FAKE_ACP_MODE",
    "FAKE_ACP_RPC_DUMP",
    "FAKE_ACP_IMAGE_CAPABILITY",
    "FAKE_ACP_DUMP_PROMPT",
    "TEST_POLICY",
    "OPENCODE_API_KEY",
    "OPENCODE_PERMISSION",
    "OPENCODE_DISABLE_AUTOUPDATE",
    "OPENCODE_DISABLE_CLAUDE_CODE_PROMPT",
    "OPENAI_API_KEY",
    "OPENROUTER_API_KEY",
    "ANTHROPIC_API_KEY",
    "XAI_API_KEY",
    "GROK_AGENT",
    "BOX_TOKEN",
    "OMB_TTS_KEY",
    "OMB_FISH_AUDIO_API_KEY",
    "OMB_CLOUD_READY_TOKEN",
    "OMB_CLOUD_BOOTSTRAP",
    "OMB_LICENSE_KEY",
    "OMB_INSTALLATION_CREDENTIAL",
    "FACTORY_API_KEY",
    "UNSLOTH_STUDIO_AUTH_TOKEN",
    "CURSOR_API_KEY",
    "CLINE_API_KEY",
    "CLINE_PROVIDER",
    "CLINE_MODEL",
    "CLINE_MCP_SETTINGS_PATH",
    "CURSOR_AUTH_TOKEN",
    "KIMI_MODEL_NAME",
    "KIMI_MODEL_API_KEY",
    "KIMI_MODEL_BASE_URL",
    "KIMI_MODEL_PROVIDER_TYPE",
    "KIMI_MODEL_DISPLAY_NAME",
    "TEST_TURN_MODEL",
    "MY_AGENT_TOKEN",
    "VENICE_API_KEY",
    "GEMINI_HOME",
    "AGY_ACP_FORCE_FILE_STORAGE",
    "ANTIGRAVITY_HARNESS_PATH",
    "AGY_ACP_DEFAULT_MODEL",
    "OPENCODE_DISABLE_PROJECT_CONFIG",
    "GEMINI_API_KEY",
    "MISTRAL_API_KEY",
    "OMB_ANTHROPIC_API_KEY",
    "COPILOT_ALLOW_ALL",
    "COPILOT_GITHUB_TOKEN",
    "GH_TOKEN",
    "GITHUB_TOKEN",
    "COMMAND_CODE_API_KEY",
    "KIRO_API_KEY",
    "WINDSURF_API_KEY",
  ].flatMap((key) => (process.env[key] === undefined ? [] : [[key, process.env[key]]] as const)),
);
const dumpState: Record<string, unknown> = { argv, env: dumpEnv, pid: process.pid };
const writeDumpAtomic = (path: string, contents: string): void => {
  const tmp = `${path}.${process.pid}.tmp`;
  const delays = [5, 10, 20, 40, 80];
  try {
    writeFileSync(tmp, contents);
    for (let attempt = 0; ; attempt += 1) {
      try {
        renameSync(tmp, path);
        return;
      } catch (error) {
        const code = (error as NodeJS.ErrnoException).code ?? "";
        if (!["EPERM", "EACCES", "EBUSY"].includes(code) || attempt >= delays.length) throw error;
        Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delays[attempt]);
      }
    }
  } finally {
    try { unlinkSync(tmp); } catch {}
  }
};
if (process.env.FAKE_ACP_DUMP) {
  writeDumpAtomic(process.env.FAKE_ACP_DUMP, JSON.stringify(dumpState, null, 2));
}
if (process.env.FAKE_ACP_LAUNCH_COUNT_FILE) {
  let launches = 0;
  try {
    launches = Number.parseInt(readFileSync(process.env.FAKE_ACP_LAUNCH_COUNT_FILE, "utf8").trim(), 10) || 0;
  } catch {}
  try {
    writeFileSync(process.env.FAKE_ACP_LAUNCH_COUNT_FILE, String(launches + 1));
  } catch {}
}
if (argv.includes("--version")) {
  console.log(process.env.FAKE_ACP_VERSION ?? "fake-acp 1.0.0");
  process.exit(0);
}
// Cursor's driver probes `agent status` / `agent models` on the binary it spawns for ACP.
if (argv[0] === "status" || argv[0] === "whoami") {
  const authenticated = process.env.FAKE_ACP_AUTH !== "0";
  console.log(JSON.stringify({ isAuthenticated: authenticated }));
  process.exit(0);
}
const gatewayPidFile = process.env.FAKE_ACP_GATEWAY_PID_FILE;
if (gatewayPidFile && argv[0] === "acp" && !existsSync(gatewayPidFile)) {
  const gateway = spawn(process.execPath, ["-e", "setInterval(() => {}, 1 << 30)", "gateway", "run", "--quiet"], {
    detached: true,
    stdio: "ignore",
    env: { ...process.env, HERMES_GATEWAY_DETACHED: "1" },
  });
  gateway.unref();
  writeFileSync(gatewayPidFile, String(gateway.pid));
}
const grokLogin = process.env.FAKE_ACP_GROK_LOGIN;
if (argv[0] === "models" && grokLogin) {
  const countFile = process.env.FAKE_ACP_GROK_LOGIN_COUNT;
  const calls = countFile && existsSync(countFile) ? Number(readFileSync(countFile, "utf8")) || 0 : 0;
  if (countFile) writeFileSync(countFile, String(calls + 1));
  if (grokLogin === "hang" || grokLogin === "signed-out-hang") {
    if (grokLogin === "signed-out-hang") console.log("You are not authenticated.");
    setInterval(() => {}, 1_000);
    await new Promise(() => {});
  }
  const signedIn = grokLogin === "signed-in" || (grokLogin === "refreshes" && calls > 0);
  console.log([signedIn ? "You are logged in with grok.com." : "You are not authenticated.",
    "Default model: grok-4.5", "Available models:", "  - grok-4.5 (default)"].join("\n"));
  process.exit(0);
}
const catalogLock = process.env.FAKE_ACP_MODELS_LOCK;
if (catalogLock && (argv[0] === "models" || argv[0] === "acp") && !argv.includes("--refresh") && process.env.OPENCODE_DISABLE_MODELS_FETCH !== "1") {
  writeFileSync(catalogLock, String(process.pid));
}
if (argv[0] === "models" && argv.includes("--refresh")) {
  if (catalogLock && existsSync(catalogLock)) {
    console.error("models.dev cache is locked by another process");
    process.exit(1);
  }
  if (process.env.FAKE_ACP_MODELS_LIST === "v2") {
    console.error("Unrecognized flag: --refresh in command opencode models");
    process.exit(1);
  }
  if (process.env.FAKE_ACP_MODELS_REFRESH_FROM && process.env.FAKE_ACP_MODELS_FILE) {
    writeFileSync(process.env.FAKE_ACP_MODELS_FILE, readFileSync(process.env.FAKE_ACP_MODELS_REFRESH_FROM));
  }
  process.exit(0);
}
if (argv[0] === "models" || argv.includes("--list-models")) {
  const failOnce = process.env.FAKE_ACP_VERBOSE_FAIL_ONCE;
  if (failOnce && argv.includes("--verbose") && !existsSync(failOnce)) {
    writeFileSync(failOnce, "failed once\n");
    console.error("Failed to run the query 'CREATE TABLE `workspace` (…)'");
    process.exit(1);
  }
  if (process.env.FAKE_ACP_MODELS_LIST === "v2") {
    if (argv.includes("--verbose")) {
      console.error("Unrecognized flag: --verbose");
      process.exit(1);
    }
    process.exit(0);
  }
  if (process.env.FAKE_ACP_MODELS_LIST === "auto-only") {
    console.log(["Available models", "", "auto - Auto (default)"].join("\n"));
    process.exit(0);
  }
  if (models.length) {
    const verbose = argv.includes("--verbose");
    console.log(
      models.flatMap((slug) => verbose
        ? [
            slug,
            JSON.stringify({
              id: slug.slice(slug.indexOf("/") + 1),
              providerID: slug.slice(0, slug.indexOf("/")),
              name: slug,
              status: "active",
              limit: { context: 200_000 },
            }, null, 2),
          ]
        : [slug]).join("\n"),
    );
    process.exit(0);
  }
  console.log(
    [
      "Available models",
      "",
      "auto - Auto (default)",
      "composer-2.5 - Composer 2.5 (current)",
      "gpt-5.3-codex - Codex 5.3",
      "cursor-live - Cursor Live",
    ].join("\n"),
  );
  process.exit(0);
}

// ACP names the session in every update and request: the one whose request this answers, or the live one.
const answering = new AsyncLocalStorage<string>();
const out = (obj: any) => {
  if ((obj?.method === "session/update" || obj?.method === "session/request_permission") && obj.params && obj.params.sessionId === undefined) {
    obj = { ...obj, params: { sessionId: answering.getStore() ?? liveSession ?? "fake-acp-session", ...obj.params } };
  }
  process.stdout.write(JSON.stringify(obj) + "\n");
};
const result = (id: unknown, res: unknown) => out({ jsonrpc: "2.0", id, result: res });
const configUpdates = JSON.parse(process.env.FAKE_ACP_CONFIG_UPDATES ?? "[]") as Array<{
  after: string; sessionId?: string; configOptions: unknown[]; replay?: boolean;
}>;
const emitConfigUpdates = (after: string, sessionId: string) => {
  for (const update of configUpdates.filter((entry) => entry.after === after)) {
    out({
      jsonrpc: "2.0", method: "session/update", params: {
        sessionId: update.sessionId ?? sessionId,
        ...(update.replay ? { _meta: { isReplay: true } } : {}),
        update: { sessionUpdate: "config_option_update", configOptions: update.configOptions },
      },
    });
  }
};
// Response and updates go in one chunk to exercise wire ordering.
const resultAndConfigUpdates = (id: unknown, res: unknown, after: string, sessionId: string) => {
  const updates = configUpdates.filter((entry) => entry.after === after).map((update) => ({
    jsonrpc: "2.0", method: "session/update", params: {
      sessionId: update.sessionId ?? sessionId,
      ...(update.replay ? { _meta: { isReplay: true } } : {}),
      update: { sessionUpdate: "config_option_update", configOptions: update.configOptions },
    },
  }));
  process.stdout.write([{ jsonrpc: "2.0", id, result: res }, ...updates].map((message) => JSON.stringify(message)).join("\n") + "\n");
};
const rpcMethods: string[] = [];
const recordMethod = (method: string, sessionId?: unknown, meta?: unknown) => {
  rpcMethods.push(method);
  if (process.env.FAKE_ACP_RPC_DUMP) writeFileSync(process.env.FAKE_ACP_RPC_DUMP, JSON.stringify(rpcMethods));
  if (process.env.FAKE_ACP_RPC_APPEND_FILE) {
    try {
      appendFileSync(process.env.FAKE_ACP_RPC_APPEND_FILE, JSON.stringify({ pid: process.pid, method, cwd: process.cwd(), sessionId, ...(meta === undefined ? {} : { meta }) }) + "\n");
    } catch {}
  }
};

const configCalls: Array<{ method: string; params: unknown }> = [];

let pendingPermissionId: number | null = null;
let permissionPriorSent = false;
let onPermissionAnswered: ((allowed: boolean) => void) | null = null;
let lastPermissionCancelled = false;
let pendingAskQuestionId: number | null = null;
let onAskQuestionAnswered: (() => void) | null = null;

let hangingPromptId: unknown = null;
let streamingPromptId: unknown = null;
let backgroundWakePlayed = false;
const backgroundJobs = new Map<string, { job: ReturnType<typeof setTimeout> | null; step: ReturnType<typeof setInterval> | null }>();
const detachedSessions = new Set<string>();
const endWokenStep = (sessionId: unknown) => {
  const job = backgroundJobs.get(String(sessionId));
  if (job?.step) clearInterval(job.step);
  if (job) job.step = null;
};
let hangKeepAlive: ReturnType<typeof setInterval> | null = null;

type McpEntry = { command: string; args?: string[]; env?: Array<{ name: string; value: string }> };
let agentsMcp: McpEntry | null = null;
type AnyMcpEntry = { name: string; command?: string; args?: string[]; env?: Array<{ name: string; value: string }>; url?: string; headers?: Array<{ name: string; value: string }> };
let sessionMcp: AnyMcpEntry[] = [];
const additionalMcp = argv.indexOf("--additional-mcp-config");
const launchMcp: AnyMcpEntry[] = additionalMcp < 0 ? [] : Object.entries(
  JSON.parse(readFileSync(argv[additionalMcp + 1].slice(1), "utf8")).mcpServers,
).map(([name, value]) => {
  const server = value as { command: string; args: string[]; env: Record<string, string> };
  return { name, command: server.command, args: server.args,
    env: Object.entries(server.env).map(([name, value]) => ({ name, value })) };
});
const acceptedMcp = (servers: AnyMcpEntry[]) => [
  ...launchMcp,
  // Copilot CLI 1.0.105 rejects client-supplied stdio servers, but accepts launcher-owned MCP configuration.
  ...servers.filter((server) => process.env.FAKE_ACP_REJECT_STDIO_MCP !== "1" || !server.command),
];
function offeredAgents(servers: AnyMcpEntry[]): McpEntry | null {
  const server = acceptedMcp(servers).find((server) => server.name === "agents");
  return server?.command ? { command: server.command, args: server.args, env: server.env } : null;
}
/** grok 1.0.25's initialize (`grok mcp doctor`) */
const GROK_MCP_HANDSHAKE = { protocolVersion: "2025-11-25", capabilities: { extensions: { "io.modelcontextprotocol/ui": { mimeTypes: ["text/html;profile=mcp-app"] } } }, clientInfo: { name: "grok-shell", version: "1.0.25" } };

/** An initialized client of a Streamable HTTP MCP server: one POST per frame, the answer read as JSON or as one event. */
async function httpMcp(entry: AnyMcpEntry, handshake: unknown): Promise<(frame: object) => Promise<any>> {
  const headers: Record<string, string> = { "content-type": "application/json", accept: "application/json, text/event-stream" };
  for (const { name, value } of entry.headers ?? []) headers[name] = value;
  const post = async (frame: object) => {
    const response = await fetch(entry.url!, { method: "POST", headers, body: JSON.stringify({ jsonrpc: "2.0", ...frame }), signal: AbortSignal.timeout(20_000) });
    const session = response.headers.get("mcp-session-id");
    if (session) headers["mcp-session-id"] = session;
    const text = await response.text();
    const body = text.split("\n").find((line) => line.startsWith("data:"))?.slice(5) ?? text;
    const parsed = body.trim() ? JSON.parse(body) : {};
    if (!response.ok || parsed.error) throw new Error(`HTTP ${response.status} ${parsed.error?.message ?? ""}`.trim());
    return parsed.result;
  };
  await post({ id: 1, method: "initialize", params: handshake });
  await post({ method: "notifications/initialized" });
  return post;
}
/** FAKE_ACP_MCP_HOLD=1: like Grok, each session's stdio MCP servers stay running once connected, until this process ends. */
const heldMcp = new Set<ReturnType<typeof spawn>>();
process.once("exit", () => { for (const child of heldMcp) child.kill(); });
function holdSessionMcp(): void {
  if (process.env.FAKE_ACP_MCP_HOLD !== "1") return;
  for (const entry of sessionMcp) void listMcpTools(entry, GROK_MCP_HANDSHAKE, true).catch(() => {});
}

async function listMcpTools(entry: AnyMcpEntry, handshake: unknown, hold = false): Promise<string[]> {
  const names = (result: any) => (result?.tools ?? []).map((tool: { name: string; inputSchema?: unknown }) =>
    process.env.FAKE_ACP_MCP_SCHEMA_BYTES === "1" ? `${tool.name}=${JSON.stringify(tool.inputSchema ?? {}).length}` : tool.name);
  if (typeof entry.url === "string") {
    const post = await httpMcp(entry, handshake);
    return names(await post({ id: 2, method: "tools/list", params: {} }));
  }
  return new Promise((resolve, reject) => {
    const env = { ...process.env };
    for (const { name, value } of entry.env ?? []) env[name] = value;
    const child = spawn(entry.command!, entry.args ?? [], { env, stdio: ["pipe", "pipe", "ignore"] });
    if (hold) heldMcp.add(child);
    const finish = (error: Error | null, tools?: string[]) => { clearTimeout(timer); if (!hold || error) child.kill(); if (error) reject(error); else resolve(tools!); };
    const timer = setTimeout(() => finish(new Error("mcp timeout")), 20_000);
    child.on("error", (error) => finish(error));
    const write = (frame: object) => child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", ...frame })}\n`);
    let pending = "";
    child.stdout.on("data", (chunk) => {
      pending += chunk;
      for (let nl = pending.indexOf("\n"); nl !== -1; nl = pending.indexOf("\n")) {
        const line = pending.slice(0, nl);
        pending = pending.slice(nl + 1);
        let frame: any;
        try { frame = JSON.parse(line); } catch { continue; }
        if (frame.error) return finish(new Error(String(frame.error.message)));
        if (frame.id === 1) { write({ method: "notifications/initialized" }); write({ id: 2, method: "tools/list", params: {} }); }
        if (frame.id === 2) return finish(null, names(frame.result));
      }
    });
    write({ id: 1, method: "initialize", params: handshake });
  });
}
let liveSession: string | null = null;
let idleModeUpdateSent = false;
let modeUpdateAfterSetModeSent = false;
let modeUpdateOnSetModelSent = false;
const openCopies = new Map<string, number>();
let sessionsOpened = 0;
let rpcFailure: unknown = null;
function emitSessionReports(): void {
  const updatesFile = process.env.FAKE_ACP_UPDATES_FILE;
  if (updatesFile && existsSync(updatesFile)) {
    try {
      for (const params of JSON.parse(readFileSync(updatesFile, "utf8")) as unknown[]) {
        if (params && typeof params === "object") out({ jsonrpc: "2.0", method: "session/update", params });
      }
    } catch {}
  }
  const usageFile = process.env.FAKE_ACP_USAGE_UPDATES_FILE;
  if (usageFile && existsSync(usageFile)) {
    try {
      for (const used of JSON.parse(readFileSync(usageFile, "utf8")) as unknown[]) {
        if (typeof used === "number") {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "usage_update", used, size: 200000 } } });
        }
      }
    } catch {}
  }
}
function failRpc(msg: { method: string; id: unknown }): boolean {
  if (msg.method !== (process.env.FAKE_ACP_RPC_FAILURE_METHOD ?? "session/prompt")) return false;
  const failureFile = process.env.FAKE_ACP_RPC_FAILURE_FILE;
  if (failureFile && existsSync(failureFile)) rpcFailure = JSON.parse(readFileSync(failureFile, "utf8"));
  if (!rpcFailure) return false;
  if (msg.method === "session/prompt" && process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE === "1") emitSessionReports();
  if (msg.method === "session/prompt" && process.env.FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT === "1") playTurn();
  const fail = () => {
    recordMethod(`${msg.method}.error`);
    out({ jsonrpc: "2.0", id: msg.id, error: rpcFailure });
    if (process.env.FAKE_ACP_RPC_FAILURE_ONCE === "1") {
      rpcFailure = null;
      if (failureFile) try { unlinkSync(failureFile); } catch {}
    }
  };
  const gate = process.env.FAKE_ACP_RPC_FAILURE_GATE;
  if (gate && !existsSync(gate)) {
    const timer = setInterval(() => {
      if (!existsSync(gate)) return;
      clearInterval(timer);
      fail();
    }, 20);
  } else fail();
  return true;
}

const antigravityConversations = process.env.FAKE_ACP_AGY_STEPS_FILE && process.env.GEMINI_HOME
  ? `${process.env.GEMINI_HOME}/antigravity-acp/conversations` : null;
if (antigravityConversations) mkdirSync(antigravityConversations, { recursive: true });

function appendAntigravitySteps(sessionId: unknown): void {
  const file = process.env.FAKE_ACP_AGY_STEPS_FILE, directory = antigravityConversations;
  if (!file || !directory || typeof sessionId !== "string" || !existsSync(file)) return;
  const { DatabaseSync } = process.getBuiltinModule("node:sqlite");
  const database = new DatabaseSync(`${directory}/${sessionId}.db`);
  try {
    database.exec("CREATE TABLE IF NOT EXISTS steps (idx INTEGER PRIMARY KEY, step_type INTEGER, step_payload BLOB)");
    const insert = database.prepare("INSERT INTO steps (step_type, step_payload) VALUES (?, ?)");
    for (const [type, text] of JSON.parse(readFileSync(file, "utf8")) as Array<[number, string]>) insert.run(type, Buffer.from(text));
  } finally {
    database.close();
  }
}

async function httpMcpCall(entry: AnyMcpEntry, name: string, args: Record<string, unknown>): Promise<string> {
  const post = await httpMcp(entry, { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "fake-acp", version: "1" } });
  const result = await post({ id: 2, method: "tools/call", params: { name, arguments: args } });
  if (result?.isError) throw new Error(`Fixture MCP request failed: ${JSON.stringify(result)}`);
  return String(result?.content?.[0]?.text ?? "");
}

function driveMcp(entry: McpEntry, calls: Array<{ name: string | (() => string); args: (prev: string) => object }>, strict = false): Promise<string> {
  return new Promise((resolve, reject) => {
    const env = { ...process.env };
    for (const { name, value } of entry.env ?? []) env[name] = value;
    const child = spawn(entry.command, entry.args ?? [], { env, stdio: ["pipe", "pipe", "inherit"] });
    child.on("error", reject);
    const timer = setTimeout(() => (child.kill(), reject(new Error("mcp timeout"))), 60_000);
    let step = -1; // -1 = initialize in flight
    let last = "";
    const write = (obj: unknown) => child.stdin.write(JSON.stringify(obj) + "\n");
    const next = () => {
      step += 1;
      if (step >= calls.length) {
        clearTimeout(timer);
        child.kill();
        return resolve(last);
      }
      const call = calls[step];
      const args = call.args(last);
      write({ jsonrpc: "2.0", id: step + 2, method: "tools/call", params: { name: typeof call.name === "function" ? call.name() : call.name, arguments: args } });
    };
    let buf = "";
    child.stdout.on("data", (c) => {
      buf += c;
      let nl;
      while ((nl = buf.indexOf("\n")) !== -1) {
        const line = buf.slice(0, nl);
        buf = buf.slice(nl + 1);
        if (!line.trim()) continue;
        let msg: any;
        try {
          msg = JSON.parse(line);
        } catch {
          continue;
        }
        if (msg.id === undefined) continue;
        if (msg.error || (strict && msg.result?.isError)) {
          clearTimeout(timer);
          child.kill();
          reject(new Error(`Fixture MCP request failed: ${JSON.stringify(msg.error ?? msg.result)}`));
          return;
        }
        if (step === -1) {
          write({ jsonrpc: "2.0", method: "notifications/initialized" });
          next();
          continue;
        }
        last = String(msg.result?.content?.[0]?.text ?? "");
        next();
      }
    });
    write({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2024-11-05" } });
  });
}

function playTurn() {
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "hello from fake acp" } } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call", toolCallId: "tc-1", title: "run", rawInput: { path: "/fixture/readme.md", password: "acp-input-secret" } } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call_update", toolCallId: "tc-1", status: "completed", rawOutput: { text: "fixture file content", api_key: "acp-output-secret" } } } });
}

function playInterleaveTurn() {
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "before one" } } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call", toolCallId: "tc-1", title: "run" } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call_update", toolCallId: "tc-1", status: "completed" } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "before two" } } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call", toolCallId: "tc-2", title: "run" } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call_update", toolCallId: "tc-2", status: "completed" } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "after" } } } });
}

function playReasoningTurn() {
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_thought_chunk", content: { text: "considering the request at length" } } } });
  out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_thought_chunk", content: { text: " without ever producing an answer" } } } });
}

let buf = "";
process.stdin.on("data", (c) => {
  buf += c;
  let nl;
  while ((nl = buf.indexOf("\n")) !== -1) {
    const line = buf.slice(0, nl);
    buf = buf.slice(nl + 1);
    if (!line.trim()) continue;
    let msg: any;
    try {
      msg = JSON.parse(line);
    } catch {
      continue;
    }
    const sessionId = msg.params?.sessionId;
    if (typeof sessionId === "string") answering.run(sessionId, () => handle(msg));
    else handle(msg);
  }
});

function heldByHoldFile(msg: any): boolean {
  const hold = process.env.FAKE_ACP_HOLD_FILE;
  if (!hold || !existsSync(hold)) return false;
  const poll = setInterval(() => {
    if (existsSync(hold)) return;
    clearInterval(poll);
    handle(msg);
  }, 50);
  return true;
}

function advertiseHermesCommands(sessionId: string) {
  out({ jsonrpc: "2.0", method: "session/update", params: { sessionId, update: { sessionUpdate: "available_commands_update",
    availableCommands: (process.env.FAKE_ACP_HERMES_COMMANDS ?? "help,steer,queue").split(",").map((name) => ({ name, description: name })) } } });
}

let gatedTurn: { run: string; steers: string[]; pending: unknown[] } | null = null;
let gatedRuns = 0;
/** A prompt that says "working. " and waits for FAKE_ACP_GATE_FILE. devin-steer: a prompt during it joins the run and is
 * answered with the first one, with the same usage and run id, as Devin does. opencode-steer: only `_session/steering` joins it. */
function grokCompletePrompt(msg: any) {
  const sessionId = msg.params?.sessionId, own = msg.params?._meta?.promptId, stopReason = process.env.FAKE_ACP_COMPLETE_STOP ?? "end_turn";
  const say = (text: string) => out({ jsonrpc: "2.0", method: "session/update", params: { sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text } } } });
  say("answered");
  const named: Record<string, { sessionId: unknown; promptId?: unknown }> = {
    own: { sessionId, promptId: own }, none: { sessionId }, task: { sessionId, promptId: "task-completed-00000000-0000-4000-8000-000000000002" },
    "other-session": { sessionId: "another-session", promptId: own },
  };
  for (const name of (process.env.FAKE_ACP_COMPLETE_NAMES ?? "own").split(",")) {
    out({ jsonrpc: "2.0", method: "_x.ai/session/prompt_complete", params: { ...named[name], stopReason, agentResult: process.env.FAKE_ACP_COMPLETE_RESULT ?? null } });
  }
  const usage = { sessionId, requestId: own, promptId: own, inputTokens: 20, outputTokens: 7 };
  switch (process.env.FAKE_ACP_COMPLETE_REPLY ?? "after") {
    case "after": result(msg.id, { stopReason, _meta: usage }); break;
    case "never": hangKeepAlive = setInterval(() => {}, 1_000); break;
    case "late": case "late-bare": setTimeout(() => {
      if (process.env.FAKE_ACP_COMPLETE_REPLY === "late") say(" late words");
      result(msg.id, { stopReason, _meta: usage });
    }, Number(process.env.FAKE_ACP_COMPLETE_REPLY_MS ?? 3_500)); break;
    case "rate-limit-error": out({ jsonrpc: "2.0", id: msg.id, error: { code: -32003, message: "Rate limited", data: { message: process.env.FAKE_ACP_COMPLETE_RESULT ?? "Rate limited" } } }); break;
  }
}

function gatedPrompt(msg: any) {
  const text = (Array.isArray(msg.params?.prompt) ? msg.params.prompt : []).map((block: any) => block?.text ?? "").join("");
  if (gatedTurn && mode === "grok-steer") {
    recordMethod("session/prompt.steer");
    gatedTurn.steers.push(text);
    result(msg.id, { stopReason: "cancelled", _meta: { completionKind: "removedFromQueue" } });
    return;
  }
  if (gatedTurn && mode === "devin-steer") {
    recordMethod("session/prompt.steer");
    gatedTurn.steers.push(text);
    gatedTurn.pending.push(msg.id);
    return;
  }
  const turn = { run: `run-${++gatedRuns}`, steers: [] as string[], pending: [msg.id] as unknown[] };
  gatedTurn = turn;
  const say = (line: string) => out({ jsonrpc: "2.0", method: "session/update",
    params: { sessionId: msg.params.sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: line } } } });
  say("working. ");
  const finish = () => {
    gatedTurn = null;
    say(`done${turn.steers.length ? `; steered: ${turn.steers.join(" | ")}` : ""}`);
    for (const id of turn.pending) {
      result(id, { stopReason: "end_turn", usage: { inputTokens: 10, outputTokens: 5 }, _meta: { "cognition.ai/userMessageId": turn.run } });
    }
  };
  const gate = process.env.FAKE_ACP_GATE_FILE;
  if (!gate || existsSync(gate)) return finish();
  const poll = setInterval(() => {
    if (!existsSync(gate)) return;
    clearInterval(poll);
    finish();
  }, 50);
}

/** OpenCode's `_session/steering`: promptRequired when no turn runs; FAKE_ACP_STEER_FAIL=rpc answers a JSON-RPC error. */
function sessionSteering(msg: any) {
  if (process.env.FAKE_ACP_STEER_FAIL === "rpc") {
    out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "unsupported_control" } });
    return;
  }
  if (!gatedTurn) return result(msg.id, { outcome: "promptRequired" });
  const prompt = Array.isArray(msg.params?.prompt) ? msg.params.prompt : [];
  gatedTurn.steers.push(prompt.map((block: any) => block?.text ?? "").join(""));
  result(msg.id, { outcome: "steered" });
}

let hermesTurn: { steers: string[] } | null = null;
/** hermes-agent's prompt, `/steer` included, as acp_adapter/server.py and commands.py answer them. */
function hermesPrompt(msg: any) {
  const text = (Array.isArray(msg.params?.prompt) ? msg.params.prompt : []).map((block: any) => block?.text ?? "").join("").trim();
  const say = (line: string) => out({ jsonrpc: "2.0", method: "session/update",
    params: { sessionId: msg.params.sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: line } } } });
  const steer = /^\/steer\s+([\s\S]+)$/.exec(text)?.[1]?.trim();
  const held = process.env.FAKE_ACP_STEER_HOLD_FILE;
  if (steer !== undefined && held && existsSync(held)) {
    const poll = setInterval(() => {
      if (existsSync(held)) return;
      clearInterval(poll);
      hermesPrompt(msg);
    }, 50);
    return;
  }
  if (steer !== undefined && hermesTurn) {
    recordMethod("session/prompt.steer");
    if (process.env.FAKE_ACP_STEER_FAIL === "rpc") {
      out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "unsupported_control" } });
      return;
    }
    if (process.env.FAKE_ACP_STEER_FAIL === "1") say("⚠️ Steer failed: the agent is compressing its context");
    else {
      hermesTurn.steers.push(steer);
      say(`⏩ Steer queued for the active turn: ${steer.slice(0, 80)}${steer.length > 80 ? "..." : ""}\n`);
    }
    result(msg.id, { stopReason: "end_turn" });
    return;
  }
  if (steer !== undefined) {
    say(`ran: ${steer}`);
    result(msg.id, { stopReason: "end_turn", _meta: { inputTokens: 3, outputTokens: 2 } });
    return;
  }
  const turn = { steers: [] as string[] };
  hermesTurn = turn;
  say("working. ");
  const finish = () => {
    hermesTurn = null;
    say(`done${turn.steers.length ? `; steered: ${turn.steers.join(" | ")}` : ""}`);
    result(msg.id, { stopReason: "end_turn", _meta: { inputTokens: 10, outputTokens: 5 } });
  };
  const gate = process.env.FAKE_ACP_GATE_FILE;
  if (!gate || existsSync(gate)) return finish();
  const poll = setInterval(() => {
    if (!existsSync(gate)) return;
    clearInterval(poll);
    finish();
  }, 50);
}

/** Applies one ACP message while preserving the fake CLI's launcher-owned MCP behavior. */
function handle(msg: any) {
  if (msg.id !== undefined && (msg.result !== undefined || msg.error !== undefined) && msg.id === pendingAskQuestionId) {
    pendingAskQuestionId = null;
    if (process.env.FAKE_ACP_ASK_ANSWER) {
      // Written aside and renamed in, so a test that sees the file never reads it half-written.
      const partial = `${process.env.FAKE_ACP_ASK_ANSWER}.partial`;
      writeFileSync(partial, JSON.stringify(msg.error !== undefined ? { error: msg.error } : { result: msg.result }));
      renameSync(partial, process.env.FAKE_ACP_ASK_ANSWER);
    }
    onAskQuestionAnswered?.();
    return;
  }
  if (msg.id !== undefined && (msg.result !== undefined || msg.error !== undefined) && msg.id === pendingPermissionId) {
    pendingPermissionId = null;
    const chosen = msg.result?.outcome?.optionId;
    lastPermissionCancelled = msg.result?.outcome?.outcome === "cancelled";
    // FAKE_ACP_PERMISSION_ANSWER: the option the client picked.
    if (process.env.FAKE_ACP_PERMISSION_ANSWER) writeFileSync(process.env.FAKE_ACP_PERMISSION_ANSWER, String(chosen ?? "cancelled"));
    onPermissionAnswered?.(typeof chosen === "string" && chosen.startsWith("allow"));
    return;
  }
  if (!msg.method) return;
  if (msg.method === "session/prompt" && heldByHoldFile(msg)) return;
  recordMethod(msg.method, msg.params?.sessionId, msg.params?._meta);

  switch (msg.method) {
    case "initialize": {
      if (process.env.FAKE_ACP_DUMP) writeFileSync(`${process.env.FAKE_ACP_DUMP}.initialize.json`, JSON.stringify(msg.params));
      if (failRpc(msg)) break;
      if (mode === "hang-initialize") {
        setInterval(() => {}, 1_000);
        return;
      }
      if (mode === "exit-early") {
        process.stderr.write("fake-acp: simulated crash before result\n");
        process.exit(3);
      }
      const authMethods = mode === "no-auth" ? [] : [{ id: process.env.FAKE_ACP_AUTH_METHOD ?? "cached_token" }];
      const agentName = process.env.FAKE_ACP_AGENT_NAME;
      const capabilityMeta = process.env.FAKE_ACP_CAPABILITIES_META;
      const acceptsImages = process.env.FAKE_ACP_IMAGE_CAPABILITY === "1";
      const mcpTransports = (process.env.FAKE_ACP_MCP_TRANSPORTS ?? "").split(",").map((entry) => entry.trim()).filter(Boolean);
      const closesSessions = ["1", "error"].includes(process.env.FAKE_ACP_SESSION_CLOSE ?? "");
      const deletesSessions = process.env.FAKE_ACP_SESSION_DELETE === "1" || mode === "background-wake";
      if (["1", "stuck"].includes(process.env.FAKE_ACP_CURSOR_FAST ?? "") && msg.params?.clientCapabilities?._meta?.parameterizedModelPicker === true) {
        currentFast = "true";
      }
      result(msg.id, {
        protocolVersion: 1,
        authMethods,
        agentInfo: agentName
          ? { name: agentName, version: process.env.FAKE_ACP_AGENT_VERSION ?? "test" }
          : undefined,
        agentCapabilities: agentName || acceptsImages || mcpTransports.length || closesSessions || deletesSessions || mode === "devin-steer" || capabilityMeta
          ? {
              ...(mode === "devin-steer" && process.env.FAKE_ACP_STEERING !== "off" ? { _meta: { "cognition.ai/midTurnPrompts": true } } : {}),
              ...(capabilityMeta ? { _meta: JSON.parse(capabilityMeta) } : {}),
              ...(agentName ? { loadSession: true, sessionCapabilities: { resume: true }, auth: { logout: true } } : {}),
              ...(closesSessions || deletesSessions
                ? { sessionCapabilities: { ...(closesSessions ? { close: {} } : {}), ...(deletesSessions ? { delete: {} } : {}),
                  ...(mode === "background-wake" ? { ...(process.env.FAKE_ACP_NO_CLOSE === "1" ? {} : { close: {} }),
                    ...(process.env.FAKE_ACP_NO_FORK === "1" ? {} : { fork: {} }) } : {}) } }
                : {}),
              ...(acceptsImages ? { promptCapabilities: { image: true } } : {}),
              ...(mcpTransports.length ? { mcpCapabilities: { http: mcpTransports.includes("http"), sse: mcpTransports.includes("sse") } } : {}),
            }
          : undefined,
        _meta: {
          modelState: { currentModelId: "fake-acp-model" },
          ...(mode === "opencode-steer" && process.env.FAKE_ACP_STEERING !== "off" ? { steering: { supported: true } } : {}),
          ...(process.env.FAKE_ACP_GROK_VERSION ? { grokShell: true, agentVersion: process.env.FAKE_ACP_GROK_VERSION } : {}),
        },
      });
      break;
    }
    case "authenticate":
      if (process.env.FAKE_ACP_DUMP) writeFileSync(`${process.env.FAKE_ACP_DUMP}.authenticate.json`, JSON.stringify(msg.params));
      if (failRpc(msg)) break;
      if (mode === "authenticate-failed") {
        out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: "Login expired" } });
        break;
      }
      if (mode === "auth-required-api-key") {
        if (msg.params?._meta?.api_key !== (process.env.FAKE_ACP_EXPECTED_API_KEY ?? "")) {
          out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: "Invalid API key" } });
          break;
        }
        apiKeyAuthenticated = true;
      }
      result(msg.id, {});
      break;
    case "_x.ai/mcp/list":
      // Like Grok: the servers this session accepted, then any native ones from its own config.
      result(msg.id, { result: { servers: [...sessionMcp.map((entry) => entry.name), ...(process.env.FAKE_ACP_GROK_NATIVE_MCP ?? "").split(",").filter(Boolean)]
        .map((name) => ({ name, session: { enabled: true, status: "ready" } })), sessionMcpResolved: true } });
      break;
    case "session/new": {
      if (failRpc(msg)) break;
      if (mode === "auth-required" || (mode === "auth-required-api-key" && !apiKeyAuthenticated)) {
        out({
          jsonrpc: "2.0",
          id: msg.id,
          error: { code: -32000, message: "Authentication required", data: { providerId: "opencode-go" } },
        });
        break;
      }
      const servers: AnyMcpEntry[] = Array.isArray(msg.params?.mcpServers) ? msg.params.mcpServers : [];
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.session.json`, JSON.stringify(msg.params));
        dumpState.mcpServers = servers;
        writeDumpAtomic(process.env.FAKE_ACP_DUMP, JSON.stringify(dumpState, null, 2));
      }
      const offered = offeredAgents(servers);
      agentsMcp = mode === "list-peers" ? agentsMcp ?? offered : offered;
      sessionMcp = acceptedMcp(servers);
      holdSessionMcp();
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.mcp.json`, JSON.stringify(servers, null, 2));
      }
      const opts = configOptions();
      const mdls = sessionModels();
      const sm = sessionModesPayload();
      sessionsOpened += 1;
      const firstSession = process.env.FAKE_ACP_SESSION_ID ?? "fake-acp-session";
      liveSession = sessionsOpened === 1 ? firstSession : `${firstSession}-${sessionsOpened}`;
      openCopies.set(liveSession, 1);
      resultAndConfigUpdates(msg.id, {
        sessionId: liveSession,
        ...(opts ? { configOptions: opts } : {}),
        ...(mdls ? { models: mdls } : {}),
        ...(sm ? { modes: sm } : {}),
      }, "session/new", liveSession);
      if (mode === "hermes-steer") advertiseHermesCommands(liveSession);
      break;
    }
    case "session/close":
      // A failed close leaves the session, its woken steps and its jobs exactly as they were.
      if (process.env.FAKE_ACP_SESSION_CLOSE === "error") {
        out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "close failed" } });
        break;
      }
      endWokenStep(msg.params?.sessionId);
      openCopies.delete(String(msg.params?.sessionId));
      if (process.env.FAKE_ACP_CLOSE_ENDS_JOBS === "1") {
        const job = backgroundJobs.get(String(msg.params?.sessionId))?.job;
        if (job) clearTimeout(job);
        backgroundJobs.delete(String(msg.params?.sessionId));
      }
      if (mode === "background-wake") detachedSessions.add(String(msg.params?.sessionId));
      if (process.env.FAKE_ACP_SESSION_CLOSE_MS) setTimeout(() => result(msg.id, {}), Number(process.env.FAKE_ACP_SESSION_CLOSE_MS));
      else result(msg.id, {});
      break;
    case "session/delete": {
      endWokenStep(msg.params?.sessionId);
      const job = backgroundJobs.get(String(msg.params?.sessionId))?.job;
      if (job) clearTimeout(job);
      backgroundJobs.delete(String(msg.params?.sessionId));
      if (mode === "background-wake") detachedSessions.add(String(msg.params?.sessionId));
      result(msg.id, {});
      break;
    }
    case "session/fork":
      result(msg.id, { sessionId: `${msg.params?.sessionId}-fork` });
      break;
    case "session/load": {
      if (process.env.FAKE_ACP_LOAD_ERROR) {
        out({ jsonrpc: "2.0", id: msg.id, error: JSON.parse(process.env.FAKE_ACP_LOAD_ERROR) });
        break;
      }
      if (process.env.FAKE_ACP_LOAD_NULL) {
        result(msg.id, null);
        break;
      }
      if (rejectLiveLoadFile && existsSync(rejectLiveLoadFile) && liveSession === msg.params?.sessionId) {
        out({
          jsonrpc: "2.0",
          id: msg.id,
          error: liveLoadError(msg.params.sessionId),
        });
        break;
      }
      const cachedLiveLoad = process.env.FAKE_ACP_CACHED_LIVE_LOAD === "1" && liveSession === msg.params?.sessionId;
      if ((mode === "safe-agent-reads" || mode === "chief-delegate" || mode === "create-peer") && !cachedLiveLoad) {
        agentsMcp = offeredAgents(msg.params?.mcpServers ?? []);
      }
      if (mode === "list-peers") {
        agentsMcp ??= offeredAgents(msg.params?.mcpServers ?? []);
      }
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.mcp.json`, JSON.stringify(msg.params?.mcpServers ?? []));
      }
      if (!cachedLiveLoad) { sessionMcp = acceptedMcp(msg.params?.mcpServers ?? []); holdSessionMcp(); }
      const opts = configOptions();
      const mdls = sessionModels();
      liveSession = typeof msg.params?.sessionId === "string" ? msg.params.sessionId : liveSession;
      detachedSessions.delete(String(msg.params?.sessionId));
      const copies = openCopies.get(String(msg.params?.sessionId)) ?? 0;
      openCopies.set(String(msg.params?.sessionId), process.env.FAKE_ACP_COPY_LIVE_LOAD === "1" ? copies + 1 : 1);
      for (let turn = 1; turn <= Number(process.env.FAKE_ACP_LOAD_REPLAY ?? 0); turn++) {
        for (const [sessionUpdate, text] of [["user_message_chunk", `REPLAYED-USER-${turn}`], ["agent_message_chunk", `REPLAYED-AGENT-${turn}`]]) {
          out({ jsonrpc: "2.0", method: "session/update", params: {
            sessionId: msg.params.sessionId, ...(process.env.FAKE_ACP_LOAD_REPLAY_MARKED === "1" ? { _meta: { isReplay: true } } : {}),
            update: { sessionUpdate, content: { type: "text", text } },
          } });
        }
      }
      if (process.env.FAKE_ACP_LOAD_UNANSWERED === "1") break;
      const sm = sessionModesPayload();
      resultAndConfigUpdates(msg.id, { ...(opts ? { configOptions: opts } : {}), ...(mdls ? { models: mdls } : {}), ...(sm ? { modes: sm } : {}) }, "session/load", msg.params.sessionId);
      if (mode === "hermes-steer") advertiseHermesCommands(msg.params.sessionId);
      break;
    }
    case "session/resume": {
      if (rejectLiveLoadFile && existsSync(rejectLiveLoadFile) && liveSession === msg.params?.sessionId) {
        out({
          jsonrpc: "2.0",
          id: msg.id,
          error: liveLoadError(msg.params.sessionId),
        });
        break;
      }
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.mcp.json`, JSON.stringify(msg.params?.mcpServers ?? []));
      }
      const opts = configOptions();
      const mdls = sessionModels();
      const sm = sessionModesPayload();
      liveSession = typeof msg.params?.sessionId === "string" ? msg.params.sessionId : liveSession;
      result(msg.id, { ...(opts ? { configOptions: opts } : {}), ...(mdls ? { models: mdls } : {}), ...(sm ? { modes: sm } : {}) });
      break;
    }
    // droid sets model/autonomy here, not via argv. Recorded beside FAKE_ACP_DUMP; last writer wins.
    case "session/set_mode":
    case "session/set_model": {
      if (mode === "no-session-config") {
        return out({ jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: "method not found" } });
      }
      if (msg.method === "session/set_model" && (mode === "set-model-invalid-params" ||
          (mode === "set-model-invalid-after-first" && configCalls.some((call) => call.method === "session/set_model")))) {
        // Cursor's answer when handed an argv slug like `auto`.
        return out({ jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: "Invalid params" } });
      }
      if (msg.method === "session/set_model" && mode === "set-model-internal-error") {
        return out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "Internal error: model switch failed" } });
      }
      const settingId = msg.method === "session/set_mode" ? "modeId" : "modelId";
      if (typeof msg.params?.sessionId !== "string" || typeof msg.params?.[settingId] !== "string") {
        out({
          jsonrpc: "2.0",
          id: msg.id,
          error: { code: -32602, message: `Invalid params: sessionId and ${settingId} must be strings` },
        });
        break;
      }
      if (msg.method === "session/set_mode" && sessionModesList.length) {
        if (!sessionModesList.includes(msg.params.modeId)) {
          out({ jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: `Invalid params: unknown modeId "${msg.params.modeId}"` } });
          break;
        }
        currentSessionMode = msg.params.modeId;
      }
      configCalls.push({ method: msg.method, params: msg.params });
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
      }
      if (msg.method === "session/set_mode" && !modeUpdateAfterSetModeSent && process.env.FAKE_ACP_MODE_UPDATE_AFTER_SET_MODE) {
        const [trigger, override] = process.env.FAKE_ACP_MODE_UPDATE_AFTER_SET_MODE.split(":");
        if (trigger && override && msg.params.modeId === trigger) {
          modeUpdateAfterSetModeSent = true;
          currentSessionMode = override;
          const resp = { jsonrpc: "2.0", id: msg.id, result: {} };
          const notif = { jsonrpc: "2.0", method: "session/update", params: { sessionId: msg.params.sessionId, update: { sessionUpdate: "current_mode_update", currentModeId: override } } };
          process.stdout.write(`${JSON.stringify(resp)}\n${JSON.stringify(notif)}\n`);
          break;
        }
      }
      if (msg.method === "session/set_model" && !modeUpdateOnSetModelSent && process.env.FAKE_ACP_MODE_UPDATE_ON_SET_MODEL) {
        const override = process.env.FAKE_ACP_MODE_UPDATE_ON_SET_MODEL.trim();
        if (override) {
          modeUpdateOnSetModelSent = true;
          currentSessionMode = override;
          out({ jsonrpc: "2.0", method: "session/update", params: { sessionId: msg.params.sessionId, update: { sessionUpdate: "current_mode_update", currentModeId: override } } });
        }
      }
      result(msg.id, {});
      break;
    }
    case "session/set_config_option": {
      const { configId, value } = msg.params ?? {};
      const variant = variantConfigs[currentModel ?? ""];
      if (variant && configId === (variant.id ?? "effort") && variantValues(variant.options).includes(value)) {
        if (!process.env.FAKE_ACP_VARIANT_STICKS) currentVariant = value;
        configCalls.push({ method: msg.method, params: msg.params });
        if (process.env.FAKE_ACP_DUMP) {
          writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
        }
        resultAndConfigUpdates(msg.id, process.env.FAKE_ACP_EMPTY_VARIANT_ACK ? {} : { configOptions: configOptions() }, "effort", msg.params.sessionId);
        break;
      }
      if (configId === "reasoning_effort" && grokEffort && ["high", "medium", "low"].includes(value)) {
        if (grokEffort === "refuse") {
          out({ jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: "Invalid params: reasoning_effort" } });
          break;
        }
        currentEffort = value;
        configCalls.push({ method: msg.method, params: msg.params });
        if (process.env.FAKE_ACP_DUMP) {
          writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
        }
        result(msg.id, { configOptions: configOptions() });
        break;
      }
      if (configId === "fast" && currentFast && (value === "true" || value === "false")) {
        if (process.env.FAKE_ACP_CURSOR_FAST !== "stuck") currentFast = value;
        configCalls.push({ method: msg.method, params: msg.params });
        if (process.env.FAKE_ACP_DUMP) {
          writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
        }
        result(msg.id, { configOptions: configOptions() });
        break;
      }
      if (configId === "mode" && modes.includes(value)) {
        const ackFile = process.env.FAKE_ACP_MODE_ACK_FILE;
        const ack = ackFile && existsSync(ackFile) ? readFileSync(ackFile, "utf8").trim() : "";
        if (ack === "error") {
          out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: "Internal error: could not change mode" } });
          break;
        }
        if (ack === "empty") {
          result(msg.id, {});
          break;
        }
        currentMode = value;
        configCalls.push({ method: msg.method, params: msg.params });
        if (process.env.FAKE_ACP_DUMP) {
          writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
        }
        result(msg.id, process.env.FAKE_ACP_EMPTY_MODE_ACK ? {} : { configOptions: configOptions() });
        break;
      }
      if (configId === "autopilot" && process.env.FAKE_ACP_AUTOPILOT === "1" && (value === "on" || value === "off")) {
        configCalls.push({ method: msg.method, params: msg.params });
        if (process.env.FAKE_ACP_DUMP) {
          writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
        }
        result(msg.id, { configOptions: configOptions() });
        break;
      }
      if (configId === "model" && value === process.env.FAKE_ACP_EXIT_ON_MODEL) process.exit(4);
      if (configId === "model" && value === process.env.FAKE_ACP_HANG_ON_MODEL) break;
      if (configId !== "model" || !models.includes(value)) {
        out({
          jsonrpc: "2.0",
          id: msg.id,
          error: { code: -32602, message: `Invalid params: model not found: ${value}`, data: { modelId: value } },
        });
        break;
      }
      if (!process.env.FAKE_ACP_MODEL_STICKS) {
        currentModel = value;
        currentVariant = variantConfigs[value]?.currentValue;
      }
      configCalls.push({ method: msg.method, params: msg.params });
      if (process.env.FAKE_ACP_DUMP) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.config.json`, JSON.stringify(configCalls, null, 2));
      }
      resultAndConfigUpdates(msg.id, { configOptions: configOptions() }, "model", msg.params.sessionId);
      break;
    }
    case "session/prompt": {
      if (process.env.FAKE_ACP_DUMP && (modes.length || models.length)) {
        appendFileSync(`${process.env.FAKE_ACP_DUMP}.prompts.jsonl`, JSON.stringify({
          pid: process.pid, sessionId: msg.params?.sessionId, mode: currentMode, model: currentModel,
        }) + "\n");
      }
      emitConfigUpdates("session/prompt", msg.params.sessionId);
      if (process.env.FAKE_ACP_DUMP && process.env.FAKE_ACP_VARIANTS) {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.selection.json`, JSON.stringify({
          sessionId: msg.params.sessionId, model: currentModel, variant: currentVariant,
        }));
      }
      if (process.env.FAKE_ACP_DUMP && process.env.FAKE_ACP_DUMP_PROMPT === "1") {
        writeFileSync(`${process.env.FAKE_ACP_DUMP}.prompt.json`, JSON.stringify(msg.params?.prompt ?? null, null, 2));
      }
      appendAntigravitySteps(msg.params?.sessionId);
      if (process.env.FAKE_ACP_PROMPT_STDERR) {
        process.stderr.write(`${process.env.FAKE_ACP_PROMPT_STDERR}\n`);
        if (process.env.FAKE_ACP_PROMPT_STDERR_THEN === "hang") return;
      }
      if (failRpc(msg)) return;
      if (mode === "hermes-steer") {
        hermesPrompt(msg);
        return;
      }
      if (mode === "opencode-steer" || mode === "devin-steer" || mode === "grok-steer") {
        gatedPrompt(msg);
        return;
      }
      if (mode === "grok-complete") {
        grokCompletePrompt(msg);
        return;
      }
      if (process.env.FAKE_ACP_TOOL_LIMIT) {
        const limit = Number(process.env.FAKE_ACP_TOOL_LIMIT);
        const handshake = process.env.FAKE_ACP_MCP_HANDSHAKE ? JSON.parse(process.env.FAKE_ACP_MCP_HANDSHAKE) : GROK_MCP_HANDSHAKE;
        void Promise.all(sessionMcp.map((entry) => listMcpTools(entry, handshake).then((tools) => `${entry.name}=${tools.length}`))).then((counts) => {
          const total = counts.reduce((sum, entry) => sum + Number(entry.split("=")[1]), 0);
          if (total > limit) {
            recordMethod("session/prompt.tool-limit");
            out({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: `Internal error: Invalid value for 'tools': maximum number of items is ${limit}.`,
              data: { service: "session", errorName: "APIError" } } });
            return;
          }
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `tools: ${counts.join(",")}` } } } });
          result(msg.id, { stopReason: "end_turn", _meta: { inputTokens: 10, outputTokens: 5 } });
        });
        return;
      }
      if (process.env.FAKE_ACP_TEXT_REPLY !== undefined) {
        emitSessionReports();
        for (let copy = openCopies.get(String(msg.params?.sessionId)) ?? 1; copy > 0; copy -= 1) {
          out({ jsonrpc: "2.0", method: "session/update", params: { sessionId: msg.params?.sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: process.env.FAKE_ACP_TEXT_REPLY } } } });
        }
        result(msg.id, { stopReason: "end_turn", _meta: { inputTokens: 12, outputTokens: 4 } });
        return;
      }
      const fullPrompt = (Array.isArray(msg.params?.prompt) ? msg.params.prompt : []).map((block: any) => block?.text ?? "").join("");
      const hangOn = process.env.FAKE_ACP_HANG_ON, hangOnce = process.env.FAKE_ACP_HANG_ONCE_FILE;
      const held = Boolean(hangOn) && fullPrompt.includes(hangOn!) && !(hangOnce && existsSync(hangOnce));
      if (held && hangOnce) writeFileSync(hangOnce, String(Date.now()));
      if (mode === "hang" || held) {
        hangingPromptId = msg.id;
        hangKeepAlive = setInterval(() => {}, 1_000);
        return;
      }
      if (mode === "stall-after-text") {
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "half an answer, then silence" } } } });
        hangingPromptId = msg.id;
        hangKeepAlive = setInterval(() => {}, 1_000);
        return;
      }
      if (mode === "abort-in-prompt") {
        process.stderr.write("Open https://accounts.google.com/o/oauth2/v2/auth?state=private-state&code=4/0AVGzR1private-code-value\n");
        process.stderr.write("2 third_party/tcmalloc/internal/sysinfo.h:76] CHECK in NumCPUs: maybe_cpus.has_value() (false) \n");
        process.exit(134);
      }
      if (mode === "crash-in-prompt") {
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "working" } } } });
        setTimeout(() => process.exit(3), 200);
        return;
      }
      if (mode === "fail-after-text") {
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "half a report, then a crash" } } } });
        recordMethod("session/prompt.error");
        out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: "fake acp: turn failed after streaming" } });
        return;
      }
      const complete = () => {
        emitSessionReports();
        recordMethod("session/prompt.result");
        result(
          msg.id,
          process.env.FAKE_ACP_USAGE_ROOT
            ? { stopReason: "end_turn", usage: { inputTokens: 10, outputTokens: 5 }, _meta: {} }
            : { stopReason: "end_turn", _meta: { inputTokens: 10, outputTokens: 5 } },
        );
        const idleMode = process.env.FAKE_ACP_IDLE_MODE_UPDATE;
        if (idleMode && !idleModeUpdateSent) {
          idleModeUpdateSent = true;
          const sid = msg.params?.sessionId ?? liveSession;
          setTimeout(() => {
            currentSessionMode = idleMode;
            out({ jsonrpc: "2.0", method: "session/update", params: { sessionId: sid, update: { sessionUpdate: "current_mode_update", currentModeId: idleMode } } });
          }, 10);
        }
      };
      const script = streamScript((Array.isArray(msg.params?.prompt) ? msg.params.prompt : []).map((block: any) => block?.text ?? "").join(""));
      if (script) {
        streamingPromptId = msg.id;
        const update = (u: Record<string, unknown>) => out({ jsonrpc: "2.0", method: "session/update", params: { update: u } });
        let tools = 0;
        playStreamScript(script, {
          cancelled: () => streamingPromptId !== msg.id,
          chunk: (step, piece) => update({ sessionUpdate: step.think !== undefined ? "agent_thought_chunk" : "agent_message_chunk", content: { text: piece } }),
          end: (step) => {
            if (step.tool === undefined) return;
            const id = `script-tool-${++tools}`;
            update({ sessionUpdate: "tool_call", toolCallId: id, title: step.tool, rawInput: { command: step.tool } });
            update({ sessionUpdate: "tool_call_update", toolCallId: id, status: "completed" });
          },
          done: (failure) => {
            streamingPromptId = null;
            if (failure) out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: failure } });
            else complete();
          },
        });
        return;
      }
      if (mode === "quiet-then-answer") {
        const lines: string[] = process.env.FAKE_ACP_QWEN_LOG ? JSON.parse(process.env.FAKE_ACP_QWEN_LOG) : [];
        let logTimer: ReturnType<typeof setInterval> | null = null;
        if (lines.length && process.env.QWEN_HOME && liveSession) {
          const dir = `${process.env.QWEN_HOME}/debug`;
          mkdirSync(dir, { recursive: true });
          let i = 0;
          logTimer = setInterval(() => {
            appendFileSync(`${dir}/${liveSession}.txt`, `${new Date().toISOString()} ${lines[Math.min(i++, lines.length - 1)]}\n`);
          }, Number(process.env.FAKE_ACP_LOG_EVERY_MS ?? 100));
        }
        const openCodeLines: string[] = process.env.FAKE_ACP_OPENCODE_LOG ? JSON.parse(process.env.FAKE_ACP_OPENCODE_LOG) : [];
        if (openCodeLines.length && process.env.XDG_DATA_HOME && liveSession) {
          const dir = `${process.env.XDG_DATA_HOME}/opencode/log`;
          mkdirSync(dir, { recursive: true });
          const session = liveSession;
          setTimeout(() => {
            appendFileSync(`${dir}/opencode.log`, openCodeLines.map((line) => `${line.replaceAll("{session}", session)}\n`).join(""));
          }, Number(process.env.FAKE_ACP_LOG_EVERY_MS ?? 100));
        }
        setTimeout(() => {
          if (logTimer) clearInterval(logTimer);
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "done" } } } });
          complete();
        }, Number(process.env.FAKE_ACP_QUIET_MS ?? 600));
        return;
      }
      if (mode === "slow-tool" || mode === "stall-after-tool") {
        const tool = (update: Record<string, unknown>) =>
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { toolCallId: "tc-slow", ...update } } });
        // ACP's default status is pending: this tool_call carries none.
        tool({ sessionUpdate: "tool_call", title: "sleep", kind: "execute", rawInput: { command: "sleep 45 && echo done" } });
        const printed = process.env.FAKE_ACP_TOOL_OUTPUT;
        tool({ sessionUpdate: "tool_call_update", status: "in_progress", ...(printed ? { content: [{ type: "content", content: { type: "text", text: printed } }] } : {}) });
        const finish = () => tool({ sessionUpdate: "tool_call_update", status: "completed", rawOutput: { output: "done" } });
        if (mode === "stall-after-tool") {
          finish();
          hangingPromptId = msg.id;
          hangKeepAlive = setInterval(() => {}, 1_000);
          return;
        }
        const answer = () => {
          finish();
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "done" } } } });
          complete();
        };
        const gate = process.env.FAKE_ACP_TOOL_GATE;
        if (gate) {
          const held = setInterval(() => {
            if (!existsSync(gate)) return;
            clearInterval(held);
            answer();
          }, 20);
          return;
        }
        setTimeout(answer, Number(process.env.FAKE_ACP_TOOL_MS ?? 600));
        return;
      }
      if (mode === "unkeyed-tool") {
        // Violates ACP by omitting toolCallId.
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call", title: "run", rawInput: { command: "echo done" } } } });
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call_update", status: "completed", rawOutput: { output: "done" } } } });
        out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "done" } } } });
        complete();
        return;
      }
      if (mode === "background-wake") {
        // OpenCode 2.3.1+: a shell command still running at its timeout moves to the background, the prompt ends, and the
        // command's finish later wakes a turn OpenCode streams as session/updates no session/prompt asked for.
        const sessionId = String(msg.params?.sessionId);
        if (detachedSessions.has(sessionId)) {
          out({ jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: "Invalid params", data: { sessionId } } });
          return;
        }
        const update = (u: Record<string, unknown>) => out({ jsonrpc: "2.0", method: "session/update", params: { sessionId, update: u } });
        if (backgroundWakePlayed) {
          update({ sessionUpdate: "agent_message_chunk", content: { text: "second answer" } });
          complete();
          return;
        }
        backgroundWakePlayed = true;
        update({ sessionUpdate: "tool_call", toolCallId: "tc-build", status: "in_progress", kind: "execute", title: "make", rawInput: { command: "make" } });
        update({ sessionUpdate: "tool_call_update", toolCallId: "tc-build", status: "completed", rawOutput: { output: "Command moved to the background (shell ID: sh_1)." } });
        update({ sessionUpdate: "agent_message_chunk", content: { text: "build started" } });
        const entry: { job: ReturnType<typeof setTimeout> | null; step: ReturnType<typeof setInterval> | null } = { job: null, step: null };
        backgroundJobs.set(sessionId, entry);
        entry.job = setTimeout(() => {
          entry.job = null;
          let step = 0;
          entry.step = setInterval(() => {
            step += 1;
            update({ sessionUpdate: "session_info_update", _meta: { "opencode/retry": null } });
            update({ sessionUpdate: "agent_message_chunk", content: { text: `WOKEN step ${step}` } });
            update({ sessionUpdate: "tool_call", toolCallId: `tc-woken-${step}`, status: "completed", kind: "execute", title: "make test", rawOutput: { output: "WOKEN ok" } });
            if (process.env.FAKE_ACP_WOKEN_FILE) appendFileSync(process.env.FAKE_ACP_WOKEN_FILE, `${sessionId} ${step}\n`);
          }, Number(process.env.FAKE_ACP_STEP_MS ?? 50));
        }, Number(process.env.FAKE_ACP_WAKE_MS ?? 300));
        if (JSON.stringify(msg.params?.prompt ?? []).includes("STOP_ME")) {
          hangingPromptId = msg.id;
          return;
        }
        complete();
        if (process.env.FAKE_ACP_LATE_UPDATE === "1") {
          setTimeout(() => update({ sessionUpdate: "tool_call_update", toolCallId: "tc-build", status: "completed" }), 50);
        }
        return;
      }
      if (mode === "finished-tools") {
        const update = (u: Record<string, unknown>) => out({ jsonrpc: "2.0", method: "session/update", params: { update: u } });
        // An unkeyed call left running, so an unkeyed finished one must not take its place in the queue.
        update({ sessionUpdate: "tool_call", title: "build", rawInput: { command: "make" } });
        update({ sessionUpdate: "tool_call", toolCallId: "tc-done", status: "completed", title: "read", rawOutput: { output: "ok" } });
        update({ sessionUpdate: "tool_call", toolCallId: "tc-denied", status: "failed", title: "rm -rf build",
          content: [{ type: "content", content: { type: "text", text: "Permission denied" } }] });
        update({ sessionUpdate: "tool_call", status: "completed", title: "echo", rawOutput: { output: "unkeyed" } });
        update({ sessionUpdate: "tool_call", status: "failed", title: "screenshot",
          content: [{ type: "image", data: ONE_PIXEL_PNG, mimeType: "image/png" }] });
        update({ sessionUpdate: "tool_call_update", status: "completed", rawOutput: { output: "built" } });
        // ACP v1 lets an update name a tool call that already finished.
        update({ sessionUpdate: "tool_call_update", toolCallId: "tc-done", status: "completed",
          content: [{ type: "image", data: ONE_PIXEL_PNG, mimeType: "image/png" }] });
        update({ sessionUpdate: "agent_message_chunk", content: { text: "done" } });
        complete();
        return;
      }
      const promptText = String(msg.params?.prompt?.[0]?.text ?? "");
      // A hand-off result wake is synthesized: the mode's usual flow would loop on an unprompted turn.
      const runtimeReport = /\[From @[^\]\n·]* \(bot\) · report on assignment [^\]\n·]*?: ([a-z_]+)/.exec(promptText);
      const wokeFromDelegation = promptText.includes("[A delegated task just completed]")
        || promptText.includes("[A delegated task failed]")
        || runtimeReport !== null;
      if (wokeFromDelegation) {
        fakeLog("branch: wokeFromDelegation");
        const failed = promptText.includes("[A delegated task failed]")
          || (runtimeReport !== null && runtimeReport[1] !== "done" && runtimeReport[1] !== "completed");
        const sawResult = promptText.includes("replied to the delegated task") || /\nWhat @\S+ said: "/.test(promptText);
        const redelegateFile = process.env.FAKE_ACP_REDELEGATE_FILE;
        if (mode === "delegate-peer" && agentsMcp && sawResult && redelegateFile && !existsSync(redelegateFile)) {
          writeFileSync(redelegateFile, "1");
          void driveMcp(agentsMcp, [
            { name: "list_bots", args: () => ({}) },
            handoffCall("second delegated task"),
          ])
            .then((reply) => {
              out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `delegated again: ${reply}` } } } });
              complete();
            })
            .catch((e) => {
              out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `delegate error: ${(e as Error).message}` } } } });
              complete();
            });
          return;
        }
        out({
          jsonrpc: "2.0",
          method: "session/update",
          params: {
            update: {
              sessionUpdate: "agent_message_chunk",
              content: {
                text: failed
                  ? "woke after delegation failure: will tell the user"
                  : sawResult
                    ? "woke after delegation: saw the result"
                    : "woke after delegation: no result",
              },
            },
          },
        });
        complete();
        return;
      }
      if (mode === "chief-delegate" && agentsMcp && promptText.includes("CHECK_STATUS")) {
        fakeLog(`branch: CHECK_STATUS savedTaskId=${JSON.stringify(savedDelegatedTaskId())}`);
        const savedTaskId = savedDelegatedTaskId();
        if (!savedTaskId) {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "status check skipped: no delegated task id" } } } });
          complete();
          return;
        }
        void driveMcp(agentsMcp, [statusCall(savedTaskId)])
          .then((reply) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `status: ${reply}` } } } });
            complete();
          })
          .catch((e) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `status error: ${(e as Error).message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "chief-delegate" && promptText.includes("CHIEF_RESULT_CONTEXT")) {
        const sawDelegatedResult =
          promptText.includes("@LongWorker replied to the delegated task")
          && promptText.includes("long delegated task");
        out({
          jsonrpc: "2.0",
          method: "session/update",
          params: {
            update: {
              sessionUpdate: "agent_message_chunk",
              content: {
                text: sawDelegatedResult
                  ? "chief saw delegated result: long delegated task"
                  : "chief did not see delegated result",
              },
            },
          },
        });
        complete();
        return;
      }
      if (
        mode === "chief-delegate"
        && agentsMcp
        && promptText.includes("ASSIGN_TO_PEER")
        && !promptText.includes("CHIEF_FOLLOW_UP")
      ) {
        void driveMcp(agentsMcp, [
          { name: "list_bots", args: () => ({}) },
          handoffCall("long delegated task", { reason: "background assignment" }),
        ])
          .then((reply) => {
            rememberDelegatedTaskId(handoffIdOf(reply));
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `assigned: ${reply}` } } } });
            complete();
          })
          .catch((e) => {
            const message = e instanceof Error ? e.message : String(e);
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `delegate error: ${message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "list-peers" && agentsMcp) {
        void driveMcp(agentsMcp, [{ name: "list_bots", args: () => ({}) }])
          .then((list) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `peers: ${list}` } } } });
            complete();
          })
          .catch((e) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `peers error: ${(e as Error).message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "mcp-tools") {
        const handshake = process.env.FAKE_ACP_MCP_HANDSHAKE ? JSON.parse(process.env.FAKE_ACP_MCP_HANDSHAKE) : GROK_MCP_HANDSHAKE;
        void Promise.all(sessionMcp.map((entry) => listMcpTools(entry, handshake).then(
          (tools) => `${entry.name}: ${tools.join(",")}`,
          (error: Error) => `${entry.name} failed: ${error.message}`,
        ))).then((lines) => {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: lines.join("\n") || "No MCP servers configured." } } } });
          complete();
        });
        return;
      }
      if (mode === "mcp-calls") {
        const calls = JSON.parse(readFileSync(process.env.FAKE_ACP_MCP_CALLS_FILE!, "utf8")) as Array<{ server: string; name: string; arguments: Record<string, unknown> }>;
        void (async () => {
          for (const call of calls) {
            const server = sessionMcp.find((entry) => entry.name === call.server);
            if (!server?.command && !server?.url) throw new Error(`Fixture MCP server unavailable: ${call.server}`);
            const text = server.url ? await httpMcpCall(server, call.name, call.arguments) : await driveMcp({ command: server.command!, args: server.args, env: server.env },
              [{ name: call.name, args: () => call.arguments }], true);
            out({ jsonrpc: "2.0", method: "session/update", params: { update: {
              sessionUpdate: "agent_message_chunk", content: { text: `${call.name}: ${text}\n` },
            } } });
          }
          complete();
        })().catch((error) => out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: String(error) } }));
        return;
      }
      if (mode === "ask-peer" && agentsMcp) {
        void driveMcp(agentsMcp, [
          { name: "list_bots", args: () => ({}) },
          handoffCall("ping from fake", { consult: true }),
        ])
          .then((reply) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `peer says: ${reply}` } } } });
            complete();
          })
          .catch((e) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `peer error: ${(e as Error).message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "create-peer" && agentsMcp) {
        let listed = "";
        void driveMcp(agentsMcp, [
          { name: "list_bots", args: () => ({}) },
          {
            name: "create_bot",
            args: (bots) => {
              listed = bots;
              return {
                name: "Pixel",
                role: "Product designer",
                instructions: "Design and review the user experience.",
              };
            },
          },
          {
            name: "coordinate_bots",
            args: (created) => {
              const id = /id: ([\w-]+)/.exec(created)?.[1] ?? /^- Pixel\b.*?\[id: ([\w-]+)/m.exec(listed)?.[1] ?? "";
              return handoffCall("Review the new onboarding flow.", { reason: "design review" }).args(`id: ${id}`);
            },
          },
        ])
          .then((reply) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `team created: ${reply}` } } } });
            complete();
          })
          .catch((e) => {
            const message = e instanceof Error ? e.message : String(e);
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `create error: ${message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "echo-gated") {
        const finish = () => {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `echo: ${promptText}` } } } });
          complete();
        };
        const gate = process.env.FAKE_ACP_GATE_FILE;
        // FAKE_ACP_STARTED_FILE: written when the prompt arrives, so a test can act during the turn.
        const started = process.env.FAKE_ACP_STARTED_FILE;
        if (started) writeFileSync(started, String(Date.now()));
        if (gate && !existsSync(gate)) {
          const poll = setInterval(() => {
            if (!existsSync(gate)) return;
            clearInterval(poll);
            finish();
          }, 50);
          return;
        }
        finish();
        return;
      }
      if (mode === "delegate-peer" && agentsMcp) {
        void driveMcp(agentsMcp, [
          { name: "list_bots", args: () => ({}) },
          handoffCall("delegated task", { reason: "followup", doneWhen: "the export opens in Numbers" }),
        ])
          .then((reply) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `delegated: ${reply}` } } } });
            complete();
          })
          .catch((e) => {
            out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `delegate error: ${(e as Error).message}` } } } });
            complete();
          });
        return;
      }
      if (mode === "image") {
        out({
          jsonrpc: "2.0",
          method: "session/update",
          params: {
            update: {
              sessionUpdate: "agent_message_chunk",
              content: { type: "image", data: ONE_PIXEL_PNG, mimeType: "image/png" },
            },
          },
        });
      } else if (mode === "interleave") playInterleaveTurn();
      else if (mode === "reasoning-only") playReasoningTurn();
      else if (mode !== "empty-reply") playTurn();
      if (mode === "safe-agent-reads" && agentsMcp) {
        const entry = agentsMcp;
        void (async () => {
          for (const name of ["list_bots", "session_search", "list_bots"]) {
            const nativeAuto = ["--permission-mode", "--approval-mode"].some((flag) => argv.includes(flag) && argv[argv.indexOf(flag) + 1] === "auto");
            if (!nativeAuto) {
              const allowed = await new Promise<boolean>((resolve) => {
                pendingPermissionId = 9100;
                onPermissionAnswered = resolve;
                out({ jsonrpc: "2.0", id: pendingPermissionId, method: "session/request_permission", params: {
                  toolCall: { toolCallId: `fixture-${name}`, kind: "other", title: `agents__${name}`, rawInput: {} },
                  options: [{ optionId: "allow-once", kind: "allow_once" }, { optionId: "reject", kind: "reject_once" }],
                } });
              });
              if (!allowed) { complete(); return; }
            }
            const text = await driveMcp(entry, [{ name, args: () => name === "session_search" ? { query: "approval fixture" } : {} }], true);
            out({ jsonrpc: "2.0", method: "session/update", params: { update: {
              sessionUpdate: "agent_message_chunk", content: { text: `${name}: ${text}\n` },
            } } });
          }
          complete();
        })().catch((error) => out({ jsonrpc: "2.0", id: msg.id, error: { code: -32000, message: String(error) } }));
        return;
      }
      if (mode === "permission") {
        pendingPermissionId = 9001;
        const priorUpdates = process.env.FAKE_ACP_PERMISSION_PRIOR_UPDATES && !permissionPriorSent ? JSON.parse(process.env.FAKE_ACP_PERMISSION_PRIOR_UPDATES) : [];
        permissionPriorSent = true;
        for (const update of priorUpdates) out({ jsonrpc: "2.0", method: "session/update", params: { update } });
        onPermissionAnswered = () => {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "handled the permission decision" } } } });
          complete();
        };
        out({
          jsonrpc: "2.0",
          id: pendingPermissionId,
          method: "session/request_permission",
          params: {
            toolCall: process.env.FAKE_ACP_PERMISSION_TOOL_CALL
              ? JSON.parse(process.env.FAKE_ACP_PERMISSION_TOOL_CALL)
              : { kind: "execute", rawInput: { command: "echo hi" }, title: "echo hi" },
            options: process.env.FAKE_ACP_PERMISSION_OPTIONS ? JSON.parse(process.env.FAKE_ACP_PERMISSION_OPTIONS) : [
              { optionId: "allow-once", kind: "allow_once" },
              // Grok offers a session-wide allow on some requests and omits it on others.
              ...(process.env.FAKE_ACP_ALLOW_ALWAYS ? [{ optionId: "allow-always", kind: "allow_always" }] : []),
              { optionId: "reject", kind: "reject_once" },
            ],
          },
        });
        return;
      }
      if (mode === "lend-question" && agentsMcp) {
        const entry = agentsMcp;
        const list = () => driveMcp(entry, [{ name: "list_shared_computers", args: () => ({}) }]).catch((e) => `error ${(e as Error).message}`);
        void (async () => {
          const before = await list();
          pendingPermissionId = 9003;
          onPermissionAnswered = () => {
            void list().then((after) => {
              out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: `before: ${before} | after: ${after}` } } } });
              complete();
            });
          };
          out({ jsonrpc: "2.0", id: pendingPermissionId, method: "session/request_permission", params: {
            toolCall: { toolCallId: "interaction_folder", kind: "other", title: "Which folder should I read?" },
            options: [{ optionId: "docs-id", kind: "allow_once", name: "Docs" }, { optionId: "other-id", kind: "allow_once", name: "Other" }],
          } });
        })();
        return;
      }
      if (mode === "question") {
        pendingPermissionId = 9002;
        onPermissionAnswered = () => {
          // FAKE_ACP_HOLD_ON_CANCELLED: a cancelled question holds the prompt until session/cancel, as Antigravity does.
          if (process.env.FAKE_ACP_HOLD_ON_CANCELLED && lastPermissionCancelled) {
            hangingPromptId = msg.id;
            return;
          }
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "answered the question" } } } });
          complete();
        };
        out({
          jsonrpc: "2.0",
          id: pendingPermissionId,
          method: "session/request_permission",
          params: {
            toolCall: { toolCallId: "interaction_color", kind: "other", title: "Which color?" },
            options: process.env.FAKE_ACP_QUESTION_OPTIONS ? JSON.parse(process.env.FAKE_ACP_QUESTION_OPTIONS) : [
              { optionId: "blue-id", kind: "allow_once", name: "Blue" },
              {
                optionId: "green-id",
                kind: "allow_once",
                name: process.env.FAKE_ACP_PAD_QUESTION_OPTION ? " Green " : "Green",
              },
            ],
          },
        });
        return;
      }
      if (mode === "ask-question") {
        const ask = () => {
          pendingAskQuestionId = 9300;
          out({
            jsonrpc: "2.0",
            id: pendingAskQuestionId,
            method: process.env.FAKE_ACP_ASK_METHOD ?? "cursor/ask_question",
            params: process.env.FAKE_ACP_ASK_PARAMS ? JSON.parse(process.env.FAKE_ACP_ASK_PARAMS) : {
              toolCallId: "ask-1",
              title: "Pick a color",
              questions: [{ id: "color", prompt: "Which color?", options: [{ id: "blue-id", label: "Blue" }, { id: "green-id", label: "Green" }], allowMultiple: false }],
            },
          });
        };
        const timing = process.env.FAKE_ACP_ASK_TIMING;
        if (timing === "with-result" || timing === "after-result") {
          onAskQuestionAnswered = null;
          if (timing === "with-result") ask();
          complete();
          // late enough that the driver has settled the turn first
          if (timing === "after-result") setTimeout(ask, 300);
          return;
        }
        onAskQuestionAnswered = () => {
          out({ jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "agent_message_chunk", content: { text: "answered the question" } } } });
          complete();
        };
        ask();
        return;
      }
      complete();
      break;
    }
    case "_session/steering":
      sessionSteering(msg);
      break;
    case "session/cancel":
      if (gatedTurn) {
        for (const id of gatedTurn.pending) result(id, { stopReason: "cancelled", _meta: {} });
        gatedTurn = null;
      }
      if (streamingPromptId !== null) {
        result(streamingPromptId, { stopReason: "cancelled", _meta: {} });
        streamingPromptId = null;
      }
      if (hangingPromptId !== null && process.env.FAKE_ACP_IGNORE_CANCEL !== "1") {
        result(hangingPromptId, { stopReason: process.env.FAKE_ACP_CANCEL_STOP_REASON ?? "cancelled", _meta: {} });
        if (hangKeepAlive) clearInterval(hangKeepAlive);
        hangingPromptId = null;
        hangKeepAlive = null;
      }
      break;
    default:
      if (msg.id !== undefined) out({ jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: "method not found" } });
  }
}