Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { randomUUID } from "node:crypto";
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, unlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ensureDirs, NATIVE_DIR } from "../../config.ts";
import { BUILT_IN_DATA_SYSTEM_PROMPT } from "../../data/instructions.ts";
import type { ProviderInstance } from "../../contracts.ts";
import { TurnNotStartedError } from "../../contracts.ts";
import { recordEvents, type EventRecorder } from "../../testing/events.ts";
import { productName } from "../../brand.ts";
import { publishedMessages } from "../../harness/text-items.testkit.ts";
import { promptSplitFingerprints, writePromptSplitReceipt } from "../prompt-split.ts";
import { classifyError } from "../retry.ts";
import { createAcpDriver, skipSubscriptionAuthForLocalInject, type AcpSupport } from "./core.ts";
import { acpMediaUri } from "./media-links.ts";
import { DEFAULT_ACP_PROMPT_IDLE_MS } from "./quiet-status.ts";
import { GrokAgentDriver, grokAcceptsUnadvertisedImages } from "./grok.ts";
import { openCodePromptUnreachedModel } from "./opencode-go.ts";
import { GeminiAgentDriver } from "./gemini.ts";
import { KimiAgentDriver } from "./kimi.ts";
import { DroidAgentDriver } from "./droid.ts";
import { CursorAgentDriver } from "./cursor.ts";
import { QwenAgentDriver } from "./qwen.ts";
import { antigravityUpdateFilter } from "../antigravity.ts";
import { removeTempDir } from "../../testing/cleanup.ts";
import { forgetTextOnlyTools } from "../../mcp-text-only.ts";
import { TurnWatchdog } from "../../turn-watchdog.ts";
import { startFakeHttpMcp } from "../../testing/fake-http-mcp-server.ts";
import { wideSchema } from "../../testing/oversized-tool-schema.ts";
import * as procs from "../../procs.ts";
import * as quietStatus from "./quiet-status.ts";
const FAKE_CLI = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "testing", "fake-acp-cli.ts");
const SELECT_MODEL_SUPPORT: AcpSupport = {
driverKind: "selectModelTest",
displayName: "Select Model Test",
models: { default: "m-one", options: [{ id: "m-one", label: "One" }, { id: "m-two", label: "Two" }] },
defaultCli: "fake-select-model",
nativeSource: "test.acp",
loginNote: "never reached",
selectModel: { configId: "model" },
spawnArgs: () => [],
pickAuthMethod: () => null,
authFailure: "continue",
isAuthenticated: () => true,
};
const SelectModelDriver = createAcpDriver(SELECT_MODEL_SUPPORT);
/** Image input is a per-harness opt-in, kept apart from production harnesses whose support is unverified. */
const NativeImageDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "nativeImageTest",
images: true,
});
/** Media named in the turn text is a per-harness opt-in for agents that read a linked file themselves. */
const MediaLinksDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "mediaLinksTest",
mediaLinks: true,
});
const EnvPolicyDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "envPolicyTest",
selectModel: undefined,
transformEnv: (env, config) => {
env.TEST_POLICY = config.fullAuto ? "auto" : "ask";
},
});
/** A CLI that needs a newer Node.js than any there is, and one that runs on every Node.js. */
const NodeTooOldDriver = createAcpDriver({ ...SELECT_MODEL_SUPPORT, driverKind: "nodeTooOldTest", selectModel: undefined, minNodeVersion: "999.0.0" });
const NodeOkDriver = createAcpDriver({ ...SELECT_MODEL_SUPPORT, driverKind: "nodeOkTest", selectModel: undefined, minNodeVersion: "1.0.0" });
const AsyncAuthDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "asyncAuthTest",
selectModel: undefined,
isAuthenticated: async () => true,
});
const ReasoningReplayDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "reasoningReplayTest",
selectModel: undefined,
resendsReasoningReply: true,
});
const ClassifiedErrorDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "classifiedErrorTest",
selectModel: undefined,
classifyError: (error) =>
error && typeof error === "object" && (error as { code?: unknown }).code === -32000
? "invalid_credentials"
: undefined,
});
const TextStartDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "textStartTest",
selectModel: undefined,
updateFilter: antigravityUpdateFilter,
});
const CONTROL_PLANE_FIXTURE = {
OMB_CLOUD_READY_TOKEN: "ready-should-not-leak", OMB_CLOUD_BOOTSTRAP: "bootstrap-should-not-leak",
OMB_LICENSE_KEY: "license-should-not-leak", OMB_INSTALLATION_CREDENTIAL: "fleet-should-not-leak",
};
describe("skipSubscriptionAuthForLocalInject", () => {
it("is true only for a host:: inject id", () => {
expect(skipSubscriptionAuthForLocalInject("omlx::MiniMax-M3-4bit")).toBe(true);
expect(skipSubscriptionAuthForLocalInject("unsloth::orcarouter/Qwen3.8-27B-Uncensored-GGUF")).toBe(true);
expect(skipSubscriptionAuthForLocalInject("grok-4.6")).toBe(false);
expect(skipSubscriptionAuthForLocalInject(undefined)).toBe(false);
});
});
describe("ACP decodeConfig", () => {
it("resolves a dynamic model catalog when a support provides one", async () => {
const support: AcpSupport = {
driverKind: "dynamic-test",
displayName: "Dynamic Test",
models: { default: "fallback", options: [{ id: "fallback", label: "Fallback" }] },
defaultCli: FAKE_CLI,
nativeSource: "dynamic-test.acp",
loginNote: "not authenticated",
spawnArgs: () => [],
pickAuthMethod: () => null,
authFailure: "continue",
isAuthenticated: () => true,
resolveModels: async () => ({
default: "dynamic-model",
options: [{ id: "dynamic-model", label: "Dynamic model" }],
}),
};
const driver = createAcpDriver(support);
const instance = await driver.create({
instanceId: "dynamic-test",
displayName: "Dynamic Test",
environment: {},
enabled: true,
config: driver.defaultConfig(),
});
expect(instance.models).toEqual({
default: "dynamic-model",
options: [{ id: "dynamic-model", label: "Dynamic model" }],
});
await instance.dispose();
});
it("says its list is unconfirmed until the engine gives one, and shares a read already under way", async () => {
let answer!: (catalog: { default: string; options: Array<{ id: string; label: string }> }) => void;
let reads = 0;
const freshReads: boolean[] = [];
const support: AcpSupport = {
driverKind: "unconfirmed-test",
displayName: "Unconfirmed Test",
models: { default: "fixed", options: [{ id: "fixed", label: "Fixed" }] },
defaultCli: FAKE_CLI,
nativeSource: "unconfirmed-test.acp",
loginNote: "not authenticated",
spawnArgs: () => [],
pickAuthMethod: () => null,
authFailure: "continue",
isAuthenticated: () => true,
resolveModelsOnCreate: false,
resolveModels: (_env, _config, _id, fresh) => {
reads += 1;
freshReads.push(fresh === true);
return new Promise((resolve) => { answer = resolve; });
},
};
const driver = createAcpDriver(support);
const instance = await driver.create({
instanceId: `unconfirmed-${randomUUID()}`,
displayName: "Unconfirmed Test",
environment: {},
enabled: true,
config: driver.defaultConfig(),
});
try {
expect(instance.modelsUnconfirmed).toBe(true);
const first = instance.refreshModels!();
const second = instance.refreshModels!();
await vi.waitFor(() => expect(reads).toBe(1));
answer({ default: "account", options: [{ id: "account", label: "Account" }] });
await Promise.all([first, second]);
expect(reads).toBe(1);
expect(instance.models.default).toBe("account");
expect(instance.modelsUnconfirmed).toBe(false);
expect(freshReads).toEqual([true]);
} finally {
await instance.dispose();
}
});
it("grok defaults to the grok binary", () => {
expect(GrokAgentDriver.decodeConfig({})).toEqual({ cli: "grok", fullAuto: false, workspace: undefined });
});
it("gemini defaults to the gemini binary", () => {
expect(GeminiAgentDriver.decodeConfig(undefined)).toEqual({ cli: "gemini", fullAuto: false, workspace: undefined });
});
it("kimi defaults to the kimi binary and declares cross-platform setup", () => {
expect(KimiAgentDriver.decodeConfig(undefined)).toEqual({ cli: "kimi", fullAuto: false, workspace: undefined });
expect(KimiAgentDriver.install?.command).toMatchObject({
darwin: expect.stringContaining("install.sh"),
linux: expect.stringContaining("install.sh"),
win32: expect.stringContaining("install.ps1"),
});
expect(KimiAgentDriver.install?.signInCommand).toBe("kimi login");
});
it("droid defaults to the droid binary and declares cross-platform setup", () => {
expect(DroidAgentDriver.decodeConfig(undefined)).toEqual({ cli: "droid", fullAuto: false, workspace: undefined });
expect(DroidAgentDriver.install?.command).toMatchObject({
darwin: expect.stringContaining("factory.ai/cli"),
linux: expect.stringContaining("factory.ai/cli"),
win32: expect.stringContaining("factory.ai/cli"),
});
expect(DroidAgentDriver.install?.signInCommand).toBe("droid");
});
it("cursor defaults to its unambiguous binary and declares cross-platform setup", () => {
expect(CursorAgentDriver.decodeConfig(undefined)).toEqual({
cli: "cursor-agent",
fullAuto: false,
workspace: undefined,
});
expect(CursorAgentDriver.install?.command).toMatchObject({
darwin: expect.stringContaining("cursor.com/install"),
linux: expect.stringContaining("cursor.com/install"),
win32: expect.stringContaining("cursor.com/install"),
});
expect(CursorAgentDriver.install?.signInCommand).toBe("cursor-agent login");
});
it("fullAuto only when explicitly true", () => {
expect(GrokAgentDriver.decodeConfig({ fullAuto: "yes" }).fullAuto).toBe(false);
expect(GrokAgentDriver.decodeConfig({ fullAuto: true }).fullAuto).toBe(true);
});
it("advertises per-bot local CUA but rejects legacy full-auto turns without a mode", async () => {
const fullAuto = await GrokAgentDriver.create({
instanceId: "grok-full-auto",
displayName: "Grok Full Auto",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
expect(fullAuto.adapter.capabilities.localComputerMcp).toBe(true);
await expect(
fullAuto.adapter.sendTurn({
threadId: "t-full-auto-local",
text: "click",
integrations: {
localComputer: {
command: "/cua-driver",
args: ["mcp"],
env: {},
platform: "linux",
scope: "local-computer",
},
},
}),
).rejects.toThrow(/interactive provider approvals/);
await fullAuto.dispose();
});
});
describe("ACP turns (fake CLI)", () => {
let instance: ProviderInstance;
let recorder: EventRecorder;
let scratch: string;
const create = async (driver = GrokAgentDriver, mode?: string) => {
if (mode) process.env.FAKE_ACP_MODE = mode;
instance = await driver.create({
instanceId: "acp-test",
displayName: "ACP Test",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
};
let savedAgyModel: string | undefined;
beforeEach(() => {
savedAgyModel = process.env.AGY_ACP_DEFAULT_MODEL;
delete process.env.AGY_ACP_DEFAULT_MODEL;
ensureDirs();
chmodSync(FAKE_CLI, 0o755);
scratch = mkdtempSync(join(tmpdir(), "omb-acp-test-"));
});
afterEach(async () => {
delete process.env.FAKE_ACP_MODE;
delete process.env.FAKE_ACP_AUTH_METHOD;
delete process.env.FAKE_ACP_DUMP;
forgetTextOnlyTools();
delete process.env.FAKE_ACP_RPC_DUMP;
delete process.env.FAKE_ACP_RPC_APPEND_FILE;
delete process.env.FAKE_ACP_RPC_FAILURE_FILE;
delete process.env.FAKE_ACP_RPC_FAILURE_METHOD;
delete process.env.FAKE_ACP_RPC_FAILURE_ONCE;
delete process.env.FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT;
delete process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE;
delete process.env.FAKE_ACP_LOAD_ERROR;
delete process.env.FAKE_ACP_ALLOW_ALWAYS;
delete process.env.FAKE_ACP_PERMISSION_ANSWER;
delete process.env.FAKE_ACP_PERMISSION_TOOL_CALL;
delete process.env.FAKE_ACP_PERMISSION_PRIOR_UPDATES;
delete process.env.FAKE_ACP_PERMISSION_OPTIONS;
delete process.env.FAKE_ACP_QUESTION_OPTIONS;
delete process.env.FAKE_ACP_ASK_METHOD;
delete process.env.FAKE_ACP_ASK_PARAMS;
delete process.env.FAKE_ACP_ASK_ANSWER;
delete process.env.FAKE_ACP_ASK_TIMING;
delete process.env.FAKE_ACP_HOLD_ON_CANCELLED;
delete process.env.FAKE_ACP_CANCEL_STOP_REASON;
delete process.env.FAKE_ACP_TEXT_REPLY;
delete process.env.XAI_API_KEY;
delete process.env.OPENCODE_API_KEY;
delete process.env.CLINE_API_KEY;
delete process.env.CURSOR_API_KEY;
delete process.env.CURSOR_AUTH_TOKEN;
delete process.env.BOX_TOKEN;
delete process.env.OMB_TTS_KEY;
if (savedAgyModel !== undefined) process.env.AGY_ACP_DEFAULT_MODEL = savedAgyModel;
else delete process.env.AGY_ACP_DEFAULT_MODEL;
for (const name of Object.keys(CONTROL_PLANE_FIXTURE)) delete process.env[name];
delete process.env.FAKE_ACP_MODELS;
delete process.env.FAKE_ACP_MODEL_STICKS;
delete process.env.FAKE_ACP_USAGE_ROOT;
delete process.env.FAKE_ACP_LOAD_NULL;
delete process.env.FAKE_ACP_REJECT_LIVE_LOAD_FILE;
delete process.env.FAKE_ACP_IMAGE_CAPABILITY;
delete process.env.FAKE_ACP_GROK_VERSION;
delete process.env.FAKE_ACP_TOOL_MS;
delete process.env.FAKE_ACP_USAGE_UPDATES_FILE;
delete process.env.FAKE_ACP_UPDATES_FILE;
delete process.env.OMB_ACP_PLAN_LINES;
delete process.env.FAKE_ACP_DUMP_PROMPT;
delete process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS;
delete process.env.OMB_ACP_QUIET_NOTICE_MS;
delete process.env.OMB_ACP_QUIET_TICK_MS;
delete process.env.FAKE_ACP_QUIET_MS;
delete process.env.FAKE_ACP_QWEN_LOG;
delete process.env.FAKE_ACP_LOG_EVERY_MS;
delete process.env.QWEN_HOME;
delete process.env.OMB_ACP_SESSION_IDLE_MS;
delete process.env.OMB_ACP_SESSION_IDLE_MIN_MS;
delete process.env.FAKE_ACP_LAUNCH_COUNT_FILE;
recorder?.stop();
await instance?.dispose();
vi.restoreAllMocks();
await removeTempDir(scratch);
});
it("names the resolved executable when spawning it fails", async () => {
const missing = join(scratch, "resolved-managed-runtime");
const driver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
selectModel: undefined,
resolveCommand: async () => ({ command: missing }),
});
instance = await driver.create({
instanceId: "resolved-spawn-error",
displayName: "Resolved spawn error",
environment: {},
enabled: true,
config: { cli: "managed-alias", fullAuto: false },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({ threadId: "t-resolved-spawn-error", text: "go" });
await recorder.until((event) => event.type === "turn.completed");
const error = recorder.events.find((event) => event.type === "runtime.error");
expect(error?.message).toContain(missing);
expect(error?.message).not.toContain("managed-alias");
});
it("pairs an unkeyed tool call's lifecycle events on one stable itemId", async () => {
await create(GrokAgentDriver, "unkeyed-tool");
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-unkeyed-tool", text: "go" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
const started = recorder.events.find((e) => e.type === "item.started" && e.itemType === "tool");
const completed = recorder.events.find((e) => e.type === "item.completed" && e.itemType === "tool");
expect(typeof started?.itemId).toBe("string");
expect(started?.itemId).toBeTruthy();
expect(completed?.itemId).toBe(started?.itemId);
});
it("ends a tool call that arrives already finished, so nothing counts it as running", async () => {
await create(GrokAgentDriver, "finished-tools");
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-finished-tools", text: "go" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
const started = recorder.events.flatMap((e) => e.type === "item.started" && e.itemType === "tool" ? [e.itemId] : []);
const completed = new Map(recorder.events.flatMap((e) =>
e.type === "item.completed" && e.itemType === "tool" ? [[e.itemId, e] as const] : []));
expect(started).toHaveLength(5);
expect(new Set(started).size).toBe(5);
expect([...completed.keys()].sort()).toEqual([...started].sort());
expect(completed.get("tc-done")).toMatchObject({ ok: true, output: expect.stringContaining("ok") });
expect(completed.get("tc-denied")).toMatchObject({ ok: false, output: expect.stringContaining("Permission denied") });
expect(completed.get(started[0])).toMatchObject({ ok: true, output: expect.stringContaining("built") });
expect(completed.get(started[3])).toMatchObject({ ok: true, output: expect.stringContaining("unkeyed") });
expect(started[4]).toMatch(/^acp-tool-unkeyed-/);
expect(completed.get(started[4])).toMatchObject({ ok: false });
const failedEnd = recorder.events.findIndex((e) => e.type === "item.completed" && e.itemType === "tool" && e.itemId === started[4]);
const image = recorder.events.findIndex((e) => e.type === "item.completed" && e.itemType === "assistant_image");
expect(image).toBe(failedEnd + 1);
expect(recorder.events[image]).toMatchObject({ fromTool: true });
// The stall watchdog sees no tool left running, so the turn's own silence limit applies.
let now = 0;
const limits: number[] = [];
const dog = new TurnWatchdog({ stallMs: 10, toolStallMs: 1_000, parkedStallMs: 10_000, engineExitStallMs: 10_000, checkMs: 60_000, now: () => now, onStall: (_turn, silentMs) => limits.push(silentMs) });
dog.watch("t-finished-tools", "bot");
for (const event of recorder.events) dog.observe(event);
now = 10;
dog.sweep();
expect(limits).toEqual([10]);
});
it("completes a finished tool call once, though a later terminal update names it again", async () => {
await create(GrokAgentDriver, "finished-tools");
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-finished-twice", text: "go" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
const ends = recorder.events.filter((e) => e.type === "item.completed" && e.itemType === "tool" && e.itemId === "tc-done");
expect(ends).toHaveLength(1);
expect(ends[0]).toMatchObject({ ok: true, output: expect.stringContaining("ok") });
expect(recorder.events.filter((e) => e.type === "item.completed" && e.itemType === "assistant_image")).toHaveLength(1);
});
it("normalizes a full turn into the canonical event sequence", async () => {
await create();
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-happy", text: "hi", model: "grok-4.5" });
await recorder.until((e) => e.type === "turn.completed");
const types = recorder.events.map((e) => e.type);
expect(types).toEqual([
"turn.started",
"session.started",
"content.delta",
"item.completed",
"item.started",
"item.completed",
"thread.token-usage.updated",
"turn.completed",
]);
expect(recorder.events.every((e) => e.turnId === turnId && e.provider === "grokAgent")).toBe(true);
expect(recorder.events.filter((event) => event.itemId === "tc-1")).toMatchObject([
{ type: "item.started", input: expect.stringContaining("/fixture/readme.md") },
{ type: "item.completed", output: expect.stringContaining("fixture file content") },
]);
expect(JSON.stringify(recorder.events)).not.toContain("acp-input-secret");
expect(JSON.stringify(recorder.events)).not.toContain("acp-output-secret");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated")!;
expect(usage).toMatchObject({ input: 10, output: 5 });
const text = recorder.events.find((e) => e.type === "item.completed" && (e as any).itemType === "assistant_text")!;
expect((text as any).text).toBe("hello from fake acp");
const done = recorder.events.at(-1)!;
expect(done).toMatchObject({ type: "turn.completed", ok: true });
expect(instance.adapter.hasSession("t-happy")).toBe(false);
});
it("sends images as negotiated ACP content blocks without copying bytes into diagnostics", async () => {
const dump = join(scratch, "image-prompt.json");
const imagePath = join(scratch, "tiny.webp");
const bytes = Buffer.from("private-acp-image-bytes");
const base64 = bytes.toString("base64");
writeFileSync(imagePath, bytes);
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
process.env.FAKE_ACP_IMAGE_CAPABILITY = "1";
await create(NativeImageDriver);
const { turnId } = await instance.adapter.sendTurn({
threadId: "t-acp-native-image",
text: "What is this?",
images: [{ path: imagePath, mime: "image/webp", bytes: bytes.length }],
});
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
expect(JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"))).toEqual([
{ type: "text", text: "What is this?" },
{ type: "image", data: base64, mimeType: "image/webp" },
]);
const nativeLog = readFileSync(join(NATIVE_DIR, "t-acp-native-image.ndjson"), "utf8");
expect(nativeLog).not.toContain(base64);
expect(nativeLog).toContain(`[image data: ${base64.length} base64 chars]`);
});
it("links local media named in the turn text as resource_link blocks the agent reads itself", async () => {
const dump = join(scratch, "media-link-prompt.json");
const clipPath = join(scratch, "media link clip.mp4");
writeFileSync(clipPath, Buffer.from("private-acp-video-bytes"));
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create(MediaLinksDriver);
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-acp-media-link", text: `What happens in ${clipPath}?` });
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
const prompt = JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"));
expect(prompt).toEqual([
{ type: "text", text: expect.stringContaining(`What happens in ${clipPath}?
(${productName()} attached these files`) },
{ type: "resource_link", uri: acpMediaUri(clipPath), name: "media link clip.mp4" },
]);
expect(JSON.stringify(prompt)).not.toContain("private-acp-video-bytes");
});
it("links media another bot handed on, named by its inbox/... path in the turn's own folder", async () => {
const dump = join(scratch, "media-link-inbox-prompt.json");
const folder = join(scratch, "media-link-thread-folder");
const handed = join(folder, "inbox", "bot-a", "task-1", "handed clip.mp4");
mkdirSync(dirname(handed), { recursive: true });
writeFileSync(handed, Buffer.from("private-acp-video-bytes"));
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create(MediaLinksDriver);
const named = join("inbox", "bot-a", "task-1", "handed clip.mp4");
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-acp-media-link-inbox", cwd: folder, text: `Describe the video.\n- ${named} (video/mp4, 1 KB)` });
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
const prompt = JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"));
expect(prompt[1]).toEqual({ type: "resource_link", uri: acpMediaUri(handed), name: "handed clip.mp4" });
expect(JSON.stringify(prompt)).not.toContain("private-acp-video-bytes");
});
it("delivers the full prompt once per native session and rides volatile changes as notes", async () => {
const dump = join(scratch, "acp-prompt-split.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create();
// Receipts are keyed by thread and session id, so a unique thread keeps the run hermetic.
const threadId = "t-acp-prompt-split-" + randomUUID();
const promptOf = () =>
(JSON.parse(readFileSync(dump + ".prompt.json", "utf8")) as Array<{ type: string; text: string }>)[0]?.text;
const send = async (text: string, volatile: string) => {
const { turnId } = await instance.adapter.sendTurn({
threadId,
text,
system: "Standing rules.\n\n" + volatile,
systemStable: "Standing rules.",
systemVolatile: volatile,
});
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
return promptOf();
};
expect(await send("first", "Memory: likes quiet hours."))
.toBe("Standing rules.\n\nMemory: likes quiet hours.\n\nfirst");
expect(await send("second", "Memory: likes quiet hours.")).toBe("second");
expect(await send("third", "Memory: moved to Toronto."))
.toBe("Context from OpenMausBot updated since this conversation started; it replaces any earlier copy:\n\nMemory: moved to Toronto.\n\nthird");
expect(await send("fourth", "")).toContain("have been cleared");
expect(await send("fifth", "")).toBe("fifth");
});
it("re-anchors the full prompt after eight bare turns on one native session", async () => {
const dump = join(scratch, "acp-prompt-reanchor.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create();
const threadId = "t-acp-prompt-reanchor-" + randomUUID();
const promptOf = () =>
(JSON.parse(readFileSync(dump + ".prompt.json", "utf8")) as Array<{ type: string; text: string }>)[0]?.text;
const messages: string[] = [];
for (let i = 0; i < 10; i++) {
const { turnId } = await instance.adapter.sendTurn({
threadId,
text: "turn " + i,
system: "Standing rules.\n\nMemory: likes quiet hours.",
systemStable: "Standing rules.",
systemVolatile: "Memory: likes quiet hours.",
});
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
messages.push(promptOf()!);
}
const full = "Standing rules.\n\nMemory: likes quiet hours.";
expect(messages[0]).toBe(full + "\n\nturn 0");
for (let i = 1; i <= 8; i++) expect(messages[i]).toBe("turn " + i);
expect(messages[9]).toBe(full + "\n\nturn 9");
});
// usage() sets the context sizes the fake reports next turn; send() returns the prompt the agent received.
const FULL = "Standing rules.\n\nMemory: likes quiet hours.";
const usageThread = async (name: string) => {
const dump = join(scratch, `acp-${name}.json`);
const usageFile = join(scratch, `acp-${name}-usage.json`);
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
process.env.FAKE_ACP_USAGE_UPDATES_FILE = usageFile;
writeFileSync(usageFile, "[]");
await create();
const threadId = `t-acp-${name}-` + randomUUID();
const usage = (...used: number[]) => writeFileSync(usageFile, JSON.stringify(used));
const send = async (text: string, ok = true) => {
const { turnId } = await instance.adapter.sendTurn({
threadId,
text,
system: FULL,
systemStable: "Standing rules.",
systemVolatile: "Memory: likes quiet hours.",
});
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId)).toMatchObject({ ok });
return (JSON.parse(readFileSync(dump + ".prompt.json", "utf8")) as Array<{ type: string; text: string }>)[0]?.text;
};
return { threadId, usage, send };
};
it("re-anchors the full prompt on the turn after the reported context collapses", async () => {
const { usage, send } = await usageThread("compaction");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(100000);
expect(await send("turn 2")).toBe("turn 2");
usage(120000, 45000);
expect(await send("turn 3")).toBe("turn 3");
usage();
expect(await send("turn 4")).toBe(FULL + "\n\nturn 4");
expect(await send("turn 5")).toBe("turn 5");
});
it("keeps the split through an ordinary context dip", async () => {
const { usage, send } = await usageThread("ordinary-dip");
usage(108641);
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(90258);
expect(await send("turn 2")).toBe("turn 2");
expect(await send("turn 3")).toBe("turn 3");
});
it("detects a collapse against the previous turn's context", async () => {
const { usage, send } = await usageThread("cross-turn");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(150000);
expect(await send("turn 2")).toBe("turn 2");
usage(50000);
expect(await send("turn 3")).toBe("turn 3");
usage();
expect(await send("turn 4")).toBe(FULL + "\n\nturn 4");
});
it("detects a cumulative collapse against an earlier turn's high-water mark", async () => {
const { usage, send } = await usageThread("cumulative-collapse");
usage(100000, 75000);
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(50000);
expect(await send("turn 2")).toBe("turn 2");
usage();
expect(await send("turn 3")).toBe(FULL + "\n\nturn 3");
expect(await send("turn 4")).toBe("turn 4");
});
it.each([40000, 75000])("invalidates a rejected prompt's receipt only after compaction (usage: %s)", async (used) => {
const failureFile = join(scratch, "compaction-failure.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE = "1";
const { usage, send } = await usageThread("rejected-compaction");
usage(100000);
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(used);
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error after compaction" }));
expect(await send("turn 2", false)).toBe("turn 2");
unlinkSync(failureFile);
usage();
expect(await send("turn 3")).toBe(used < 60000 ? FULL + "\n\nturn 3" : "turn 3");
expect(await send("turn 4")).toBe("turn 4");
});
it("ignores a zero usage report", async () => {
const { usage, send } = await usageThread("zero-usage");
usage(150000);
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
usage(0);
expect(await send("turn 2")).toBe("turn 2");
expect(await send("turn 3")).toBe("turn 3");
});
it("never triggers from a receipt written before lastUsed existed", async () => {
const { threadId, usage, send } = await usageThread("legacy-receipt");
writePromptSplitReceipt(
"grokAgent",
JSON.stringify([threadId, "fake-acp-session"]),
promptSplitFingerprints("Standing rules.", "Memory: likes quiet hours."),
);
usage(50000);
expect(await send("turn 1")).toBe("turn 1");
expect(await send("turn 2")).toBe("turn 2");
});
const reportingThread = async (name: string, driver?: typeof GrokAgentDriver) => {
const dump = join(scratch, `acp-${name}.json`);
const updatesFile = join(scratch, `acp-${name}-updates.json`);
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
process.env.FAKE_ACP_UPDATES_FILE = updatesFile;
writeFileSync(updatesFile, "[]");
await create(driver);
const threadId = `t-acp-${name}-` + randomUUID();
let lastTurnId = "";
/** Whole session/update params, for a `_meta` beside the update. */
const reportParams = (...params: object[]) => writeFileSync(updatesFile, JSON.stringify(params));
const report = (...updates: object[]) => reportParams(...updates.map((update) => ({ update })));
const send = async (text: string, ok = true) => {
const { turnId } = await instance.adapter.sendTurn({
threadId,
text,
system: FULL,
systemStable: "Standing rules.",
systemVolatile: "Memory: likes quiet hours.",
});
lastTurnId = turnId;
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId)).toMatchObject({ ok });
return (JSON.parse(readFileSync(dump + ".prompt.json", "utf8")) as Array<{ type: string; text: string }>)[0]?.text;
};
return { threadId, report, reportParams, send, lastTurn: () => recorder.events.filter((event) => event.turnId === lastTurnId) };
};
/** A turn's usage events, down to their figures: an absent key stays absent. */
const usageFigures = (events: EventRecorder["events"]) => events.flatMap((event) => {
if (event.type !== "thread.token-usage.updated") return [];
const usage = event;
const keys = ["input", "output", "cachedInput", "contextTokens", "contextWindow"] as const;
return [Object.fromEntries(keys.filter((key) => key in usage).map((key) => [key, usage[key]]))];
});
it("reports the context an agent says it holds, and the window when it names one", async () => {
const { report, send, lastTurn } = await reportingThread("context-report");
report(
{ sessionUpdate: "usage_update", used: 48_000, size: 200_000 },
// opencode nests its figures under `usage`
{ sessionUpdate: "usage_update", usage: { used: 61_000, size: 128_000 } },
);
await send("turn 1");
// The turn's token count is still the prompt result's, the last usage event.
expect(usageFigures(lastTurn())).toStrictEqual([
{ input: 0, output: 0, contextTokens: 48_000, contextWindow: 200_000 },
{ input: 0, output: 0, contextTokens: 61_000, contextWindow: 128_000 },
{ input: 10, output: 5 },
]);
});
it("reports the context alone when the agent names no usable window", async () => {
const { report, send, lastTurn } = await reportingThread("context-only");
report(
{ sessionUpdate: "usage_update", used: 48_000 },
{ sessionUpdate: "usage_update", used: 49_000, size: 0 },
{ sessionUpdate: "usage_update", used: 50_000, size: -200_000 },
{ sessionUpdate: "usage_update", used: 51_000, size: "200000" },
{ sessionUpdate: "usage_update", usage: { used: 52_000 } },
);
await send("turn 1");
expect(usageFigures(lastTurn())).toStrictEqual([
{ input: 0, output: 0, contextTokens: 48_000 },
{ input: 0, output: 0, contextTokens: 49_000 },
{ input: 0, output: 0, contextTokens: 50_000 },
{ input: 0, output: 0, contextTokens: 51_000 },
{ input: 0, output: 0, contextTokens: 52_000 },
{ input: 10, output: 5 },
]);
});
it("reports no context for a usage update that is not a positive count", async () => {
const { report, send, lastTurn } = await reportingThread("context-not-positive");
report(
{ sessionUpdate: "usage_update", used: 0, size: 200_000 },
{ sessionUpdate: "usage_update", used: -48_000, size: 200_000 },
{ sessionUpdate: "usage_update", used: "48000", size: 200_000 },
{ sessionUpdate: "usage_update", used: null, size: 200_000 },
{ sessionUpdate: "usage_update", size: 200_000 },
);
await send("turn 1");
expect(usageFigures(lastTurn())).toStrictEqual([{ input: 10, output: 5 }]);
});
/** Hermes's notice that it rotated its session, as hermes-agent sends it. */
const provenance = (fields: object) => ({
sessionUpdate: "session_info_update",
_meta: { hermes: { sessionProvenance: { acpSessionId: "fake-acp-session", sessionKind: "continuation", ...fields } } },
});
it("re-anchors the full prompt on the turn after Hermes reports a compression", async () => {
const { report, send } = await reportingThread("hermes-compression");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
// Hermes compressed its history mid-turn and said so, so the next turn is full
report(provenance({ reason: "compression", compressionDepth: 1 }));
expect(await send("turn 2")).toBe("turn 2");
report();
expect(await send("turn 3")).toBe(FULL + "\n\nturn 3");
expect(await send("turn 4")).toBe("turn 4");
});
it("re-anchors once for each rise in Hermes's compression depth", async () => {
const { report, send } = await reportingThread("hermes-depth");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
// no stated reason: a depth above the last one seen is the marker
report(provenance({ compressionDepth: 1 }));
expect(await send("turn 2")).toBe("turn 2");
// the same depth again is the same compaction, not another
report(provenance({ compressionDepth: 1 }));
expect(await send("turn 3")).toBe(FULL + "\n\nturn 3");
report(provenance({ compressionDepth: 2 }));
expect(await send("turn 4")).toBe("turn 4");
report();
expect(await send("turn 5")).toBe(FULL + "\n\nturn 5");
expect(await send("turn 6")).toBe("turn 6");
});
it("reads Hermes's marker beside the update as well as inside it", async () => {
const { reportParams, send } = await reportingThread("hermes-params-meta");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
reportParams({
update: { sessionUpdate: "session_info_update", title: "Continued" },
_meta: { hermes: { sessionProvenance: { reason: "compression" } } },
});
expect(await send("turn 2")).toBe("turn 2");
reportParams();
expect(await send("turn 3")).toBe(FULL + "\n\nturn 3");
});
it("invalidates a rejected prompt's receipt after Hermes reports a compression", async () => {
const failureFile = join(scratch, "hermes-compression-failure.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE = "1";
const { report, send } = await reportingThread("hermes-rejected");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
report(provenance({ reason: "compression", compressionDepth: 1 }));
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error after compression" }));
expect(await send("turn 2", false)).toBe("turn 2");
unlinkSync(failureFile);
report();
expect(await send("turn 3")).toBe(FULL + "\n\nturn 3");
expect(await send("turn 4")).toBe("turn 4");
});
it("ignores session info whose meta is unknown or malformed", async () => {
const { report, send, lastTurn } = await reportingThread("hermes-malformed");
expect(await send("turn 1")).toBe(FULL + "\n\nturn 1");
report(
{ sessionUpdate: "session_info_update", title: "Renamed" },
{ sessionUpdate: "session_info_update", _meta: "compression" },
{ sessionUpdate: "session_info_update", _meta: { hermes: null } },
{ sessionUpdate: "session_info_update", _meta: { hermes: { sessionProvenance: "compression" } } },
{ sessionUpdate: "session_info_update", _meta: { hermes: { sessionProvenance: ["compression"] } } },
{ sessionUpdate: "session_info_update", _meta: { other: { sessionProvenance: { reason: "compression" } } } },
provenance({ reason: "rename" }),
provenance({ reason: ["compression"] }),
provenance({ sessionKind: "root", compressionDepth: 0 }),
provenance({ compressionDepth: "3" }),
provenance({ compressionDepth: 2.5 }),
provenance({ compressionDepth: -1 }),
provenance({ compressionDepth: null }),
);
expect(await send("turn 2")).toBe("turn 2");
// silently: nothing about it reaches the transcript
expect(lastTurn().filter((event) => event.type === "runtime.error" || event.type === "runtime.notice")).toEqual([]);
report();
expect(await send("turn 3")).toBe("turn 3");
});
describe("a reply re-sent with its reasoning under the streamed messageId", () => {
const messageId = randomUUID();
const chunk = (sessionUpdate: string, text: string) => ({ sessionUpdate, messageId, content: { type: "text", text } });
const say = (...texts: string[]) => texts.map((text) => chunk("agent_message_chunk", text));
const reasoned = "💭 **Reasoning:**\n```\nSix times seven.\n```\n\nThe answer is 42.";
/** Each turn's replies after the fake's own "hello from fake acp". */
const replyingThread = async (name: string, driver?: typeof GrokAgentDriver) => {
const { report, send, lastTurn } = await reportingThread(name, driver);
return async (...updates: object[]) => {
report(...updates);
await send("turn");
return lastTurn().flatMap((event) =>
event.type === "item.completed" && event.itemType === "assistant_text" ? [event.text] : []).slice(1);
};
};
it("shows once from an engine that re-sends it", async () => {
const repliesTo = await replyingThread("reasoning-replay", ReasoningReplayDriver);
expect(await repliesTo(chunk("agent_thought_chunk", "Six times seven."), ...say("The answer", " is 42.", reasoned)))
.toEqual(["The answer is 42."]);
// hermes-agent 0.22.4 sends the re-sent reply after a blank line
expect(await repliesTo(...say("The answer", " is 42.", `\n\n${reasoned}`))).toEqual(["The answer is 42."]);
expect(await repliesTo(...say(" indented", "> 💭 **Reasoning:**\n> Six times seven.\n\n indented")))
.toEqual(["indented"]);
// chunks that only end with the text so far are deltas
expect(await repliesTo(...say("a", "ba"))).toEqual(["aba"]);
expect(await repliesTo(...say("42.", "Explanation:\n\n42."))).toEqual(["42.Explanation:\n\n42."]);
});
it("is appended for any other engine", async () => {
const repliesTo = await replyingThread("reasoning-append");
expect(await repliesTo(...say("The answer is 42.", reasoned))).toEqual([`The answer is 42.${reasoned}`]);
});
});
it("drops the escape artifact a text run opens with, again after a tool call", async () => {
const { report, send, lastTurn } = await reportingThread("text-start", TextStartDriver);
const say = (text: string) => ({ sessionUpdate: "agent_message_chunk", content: { type: "text", text } });
report(
say("&_a_m_p_;_\n&_a_m_p_;_"),
say("Prepared the reply. A mid-run &_a_m_p_;_ stays."),
{ sessionUpdate: "tool_call", toolCallId: "call-1", title: "Ran a thing", status: "completed" },
say("&_a_m_p_;_ Done."),
);
await send("turn");
const texts = lastTurn().flatMap((event) =>
event.type === "item.completed" && event.itemType === "assistant_text" ? [event.text] : []);
// the fake's own greeting is the first assistant_text of the turn
expect(texts.slice(1)).toEqual(["Prepared the reply. A mid-run &_a_m_p_;_ stays.", "Done."]);
const streamed = lastTurn().flatMap((event) => event.type === "content.delta" ? [event.delta] : []);
expect(streamed.some((delta) => delta.startsWith("&_a_m_p_;_"))).toBe(false);
});
const plan = (...entries: Array<[status: string, content: string]>) => ({
sessionUpdate: "plan",
entries: entries.map(([status, content]) => ({ content, priority: "medium", status })),
});
/** A turn's plan lines: each started step's title with how it completed. */
const planSteps = (events: EventRecorder["events"]) => events.flatMap((event) => {
if (event.type !== "item.started" || event.itemType !== "tool" || !event.title?.startsWith("Plan:")) return [];
const done = events.find((other) => other.type === "item.completed" && other.itemType === "tool" && other.itemId === event.itemId);
return [{ title: event.title, ok: done?.type === "item.completed" && done.itemType === "tool" ? done.ok : undefined }];
});
it("keeps an agent's plan out of the transcript unless plan lines are switched on", async () => {
const { report, send, lastTurn } = await reportingThread("plan-off");
report(plan(["in_progress", "Read the brief"], ["pending", "Write the tests"]));
await send("turn 1");
expect(planSteps(lastTurn())).toEqual([]);
// the fake's own tool call is the turn's only step
expect(lastTurn().filter((event) => event.type === "item.started")).toHaveLength(1);
});
it("shows one finished step each time an agent's plan reads differently", async () => {
process.env.OMB_ACP_PLAN_LINES = "1";
const { report, send, lastTurn } = await reportingThread("plan-lines");
report(
plan(["pending", "Read the brief"], ["pending", "Write the tests"], ["pending", "Ship it"]),
// the same list again says nothing new
plan(["pending", "Read the brief"], ["pending", "Write the tests"], ["pending", "Ship it"]),
plan(["in_progress", "Read the brief"], ["pending", "Write the tests"], ["pending", "Ship it"]),
plan(["completed", "Read the brief"], ["in_progress", "Write the tests"], ["pending", "Ship it"]),
// a later entry reworded: the line a person reads is the same
plan(["completed", "Read the brief"], ["in_progress", "Write the tests"], ["pending", "Ship it on Friday"]),
// not a plan anyone can read: nothing is shown, and nothing is forgotten
{ sessionUpdate: "plan", entries: "Read the brief" },
{ sessionUpdate: "plan" },
{ sessionUpdate: "plan", entries: [] },
plan(["completed", "Read the brief"], ["in_progress", "Write the tests"], ["pending", "Ship it on Friday"]),
plan(["completed", "Read the brief"], ["completed", "Write the tests"], ["completed", "Ship it on Friday"]),
);
await send("turn 1");
expect(planSteps(lastTurn())).toEqual([
{ title: "Plan: 0 of 3 done — next: Read the brief", ok: true },
{ title: "Plan: 0 of 3 done — Read the brief", ok: true },
{ title: "Plan: 1 of 3 done — Write the tests", ok: true },
{ title: "Plan: 3 of 3 done", ok: true },
]);
// each line is its own step, with the whole list one click away
const started = lastTurn().filter((event) => event.type === "item.started" && event.itemType === "tool" && event.title?.startsWith("Plan:"));
expect(new Set(started.map((event) => event.itemId)).size).toBe(4);
expect(started[2]).toMatchObject({ input: expect.stringContaining("Ship it") });
report(plan(["completed", "Read the brief"], ["completed", "Write the tests"], ["completed", "Ship it on Friday"]));
await send("turn 2");
expect(planSteps(lastTurn())).toEqual([]);
report(plan(["completed", "Read the brief"], ["completed", "Write the tests"], ["completed", "Ship it on Friday"], ["pending", "Tell the team"]));
await send("turn 3");
expect(planSteps(lastTurn())).toEqual([{ title: "Plan: 3 of 4 done — next: Tell the team", ok: true }]);
});
it("bounds a plan line and redacts it before it is cut", async () => {
process.env.OMB_ACP_PLAN_LINES = "1";
const { report, send, lastTurn } = await reportingThread("plan-redacted");
const secret = "xai-" + "a".repeat(40);
report(plan(["in_progress", `Rotate the deploy key ${secret} on every runner in the fleet, then check each one of them twice`]));
await send("turn 1");
const [step] = planSteps(lastTurn());
expect(step!.title).toMatch(/^Plan: 0 of 1 done — Rotate the deploy key «redacted \d+ chars» on every runner.*…$/);
expect(step!.title.length).toBeLessThanOrEqual("Plan: 0 of 1 done — ".length + 80);
expect(JSON.stringify(lastTurn())).not.toContain(secret);
});
it("does not take a plan for the turn's reply", async () => {
process.env.OMB_ACP_PLAN_LINES = "1";
process.env.FAKE_ACP_MODE = "empty-reply";
const { report, send, lastTurn } = await reportingThread("plan-no-reply");
report(plan(["in_progress", "Read the brief"]));
// a turn that only planned is still a lost turn, not a success
await send("turn 1", false);
expect(planSteps(lastTurn())).toEqual([{ title: "Plan: 0 of 1 done — Read the brief", ok: true }]);
expect(lastTurn().at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "empty_turn" });
});
it("names a turn that streams reasoning past its last tool, then ends without a reply", async () => {
const { report, send, lastTurn } = await reportingThread("ended-after-tools");
report(
{ sessionUpdate: "tool_call", toolCallId: "call-eat-1", title: "Ran a thing", status: "completed" },
{ sessionUpdate: "agent_thought_chunk", content: { type: "text", text: "still deliberating" } },
);
await send("turn 1", false);
expect(lastTurn().at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "ended_after_tools" });
});
it("keeps that name when a finished tool's update repeats after its reasoning", async () => {
const { report, send, lastTurn } = await reportingThread("ended-after-tools-repeat");
report(
{ sessionUpdate: "tool_call", toolCallId: "call-eat-2", title: "Ran a thing", status: "completed" },
{ sessionUpdate: "agent_thought_chunk", content: { type: "text", text: "still deliberating" } },
{ sessionUpdate: "tool_call", toolCallId: "call-eat-2", title: "Ran a thing", status: "completed" },
);
await send("turn 1", false);
expect(lastTurn().at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "ended_after_tools" });
});
it("fails clearly when an image-capable adapter meets an older ACP runtime", async () => {
const imagePath = join(scratch, "tiny.png");
writeFileSync(imagePath, "not-read-before-capability-check");
await create(NativeImageDriver);
const { turnId } = await instance.adapter.sendTurn({
threadId: "t-acp-image-unsupported",
text: "Inspect this",
images: [{ path: imagePath, mime: "image/png", bytes: 32 }],
});
const done = await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
expect(done).toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(recorder.events.find((event) => event.type === "runtime.error")?.message).toMatch(
/does not advertise ACP image input/,
);
});
it("keeps text-path fallback for adapters that do not advertise image input", async () => {
const dump = join(scratch, "text-fallback.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create(SelectModelDriver);
const text = '<attached-image path="/private/image.png" name="image.png" />';
const { turnId } = await instance.adapter.sendTurn({
threadId: "t-acp-image-fallback",
text,
// A missing path proves the driver did not attempt native ingestion.
images: [{ path: join(scratch, "missing.png"), mime: "image/png", bytes: 12 }],
});
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
expect(JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"))).toEqual([{ type: "text", text }]);
});
it("sends native images on Grok's verified runtime even with the false capability", async () => {
const dump = join(scratch, "grok-image.json");
const imagePath = join(scratch, "grok.png");
const bytes = Buffer.from("synthetic-private-image");
writeFileSync(imagePath, bytes);
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
process.env.FAKE_ACP_GROK_VERSION = "1.0.25";
await create(GrokAgentDriver);
expect(instance.adapter.capabilities.images).toBe(true);
const { turnId } = await instance.adapter.sendTurn({
threadId: "t-grok-image", text: "Inspect", images: [{ path: imagePath, mime: "image/png", bytes: bytes.length }],
});
const done = await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
expect(done).toMatchObject({ ok: true });
expect(JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"))).toContainEqual({ type: "image", data: bytes.toString("base64"), mimeType: "image/png" });
expect(readFileSync(join(NATIVE_DIR, "t-grok-image.ndjson"), "utf8")).not.toContain(bytes.toString("base64"));
});
it.each([null, {}, { _meta: { grokShell: true, agentVersion: "1.0.24" } },
{ _meta: { grokShell: true, agentVersion: "1.0.25-preview" } },
{ _meta: { grokShell: false, agentVersion: "1.0.25" } }])("does not assume image support for unknown Grok runtime %j", (init) => {
expect(grokAcceptsUnadvertisedImages(init)).toBe(false);
});
it("emits each assistant text block before the tool that follows it", async () => {
await create(GrokAgentDriver, "interleave");
await instance.adapter.sendTurn({ threadId: "t-interleave", text: "go", model: "grok-4.5" });
await recorder.until((e) => e.type === "turn.completed");
const types = recorder.events.map((e) => e.type);
expect(types).toEqual([
"turn.started",
"session.started",
"content.delta",
"item.completed",
"item.started",
"item.completed",
"content.delta",
"item.completed",
"item.started",
"item.completed",
"content.delta",
"thread.token-usage.updated",
"item.completed",
"turn.completed",
]);
const texts = recorder.events
.filter((e) => e.type === "item.completed" && (e as { itemType?: string }).itemType === "assistant_text")
.map((e) => (e as { text: string }).text);
expect(texts).toEqual(["before one", "before two", "after"]);
expect(publishedMessages(recorder.events), "each message's words and its completion share one id of their own").toEqual([
{ itemId: expect.any(String), streamed: "before one", completed: "before one" },
{ itemId: expect.any(String), streamed: "before two", completed: "before two" },
{ itemId: expect.any(String), streamed: "after", completed: "after" },
]);
});
it("normalizes a structured ACP image block without treating it as text", async () => {
await create(GeminiAgentDriver, "image");
await instance.adapter.sendTurn({ threadId: "t-image", text: "draw it" });
await recorder.until((event) => event.type === "turn.completed");
const image = recorder.events.find(
(event) => event.type === "item.completed" && event.itemType === "assistant_image",
);
expect(image).toMatchObject({
type: "item.completed",
itemType: "assistant_image",
alt: "Generated image",
});
expect(image).not.toHaveProperty("fromTool");
expect(image && "data" in image ? image.data : "").toMatch(/^iVBOR/);
expect(
recorder.events.some(
(event) => event.type === "item.completed" && event.itemType === "assistant_text",
),
).toBe(false);
});
it("reads token usage from the root of the prompt result", async () => {
process.env.FAKE_ACP_USAGE_ROOT = "1";
await create();
await instance.adapter.sendTurn({ threadId: "t-usage-root", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated");
expect(usage).toMatchObject({ input: 10, output: 5 });
});
/** The fake hardcodes its prompt result, so these tests rewrite the session/prompt result in flight. */
const splicePromptResult = (rewrite: (result: Record<string, unknown>) => Record<string, unknown>) => {
const spawn = procs.spawnCli;
vi.spyOn(procs, "spawnCli").mockImplementation((...args) => {
const child = spawn(...args);
const stdout = child.stdout;
const splice = (message: any) => {
if (message?.result && typeof message.result === "object" && "stopReason" in message.result) message.result = rewrite(message.result);
return message;
};
const rewriteChunk = (chunk: string) =>
chunk.split("\n").map((line) => {
if (!line.includes("stopReason")) return line;
try {
return JSON.stringify(splice(JSON.parse(line)));
} catch {
return line; // not a complete wire message: pass it through untouched
}
}).join("\n");
const realOn = stdout.on.bind(stdout);
vi.spyOn(stdout, "on").mockImplementation(((event: string, listener: (chunk: string) => void) =>
realOn(event, event === "data" ? (chunk: string) => listener(rewriteChunk(String(chunk))) : listener)) as never);
return child;
});
};
/** The usage at the root of the result or under _meta. */
const stubPromptResultUsage = (usage: Record<string, unknown>, at: "root" | "meta") =>
splicePromptResult((result) => at === "root"
? { ...result, usage: { ...usage }, _meta: {} }
: { ...result, _meta: { ...(result._meta as Record<string, unknown> | undefined), ...usage } });
it("fails a refused turn in plain words that keep ACP's stop reason", async () => {
splicePromptResult((result) => ({ ...result, stopReason: "refusal" }));
await create();
await instance.adapter.sendTurn({ threadId: "t-refusal", text: "go" });
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false, stopReason: "refusal" });
const error = recorder.events.find((e) => e.type === "runtime.error") as { message: string } | undefined;
expect(error?.message).toContain("Grok refused to go on with this request (stop reason: refusal).");
});
it("fails a max_tokens turn in plain words that advise continuing", async () => {
splicePromptResult((result) => ({ ...result, stopReason: "max_tokens" }));
await create();
await instance.adapter.sendTurn({ threadId: "t-max-tokens", text: "go" });
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false, stopReason: "max_tokens" });
const error = recorder.events.find((e) => e.type === "runtime.error") as { message: string } | undefined;
expect(error?.message).toContain("Grok's reply reached its maximum length before it finished (stop reason: max_tokens). Ask it to continue, or ask for a shorter answer.");
});
it("folds opencode cache reads into input and names them", async () => {
stubPromptResultUsage(
{ inputTokens: 5030, outputTokens: 3, cachedReadTokens: 152960, totalTokens: 157993 },
"root",
);
await create();
await instance.adapter.sendTurn({ threadId: "t-usage-cache-exclusive", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated");
expect(usage).toMatchObject({ input: 157990, output: 3, cachedInput: 152960 });
});
it("leaves input alone when totalTokens shows cache reads already inside it", async () => {
stubPromptResultUsage(
{ inputTokens: 1000, outputTokens: 10, cachedReadTokens: 800, totalTokens: 1010 },
"root",
);
await create();
await instance.adapter.sendTurn({ threadId: "t-usage-cache-inclusive", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated");
expect(usage).toMatchObject({ input: 1000, output: 10, cachedInput: 800 });
});
it("emits no cachedInput when the result reports no cache reads", async () => {
stubPromptResultUsage({ inputTokens: 900, outputTokens: 20, totalTokens: 920 }, "root");
await create();
await instance.adapter.sendTurn({ threadId: "t-usage-cache-absent", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated");
expect(usage).toMatchObject({ input: 900, output: 20 });
expect(usage && "cachedInput" in usage).toBe(false);
});
it("reads cache reads from _meta usage too", async () => {
stubPromptResultUsage(
{ inputTokens: 5030, outputTokens: 3, cachedReadTokens: 152960, totalTokens: 157993 },
"meta",
);
await create();
await instance.adapter.sendTurn({ threadId: "t-usage-cache-meta", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const usage = recorder.events.find((e) => e.type === "thread.token-usage.updated");
expect(usage).toMatchObject({ input: 157990, output: 3, cachedInput: 152960 });
});
it("passes ACP stdio flags and strips foreign provider keys from the child env", async () => {
await create();
const dump = join(scratch, "dump.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.XAI_API_KEY = "xai-should-not-leak";
process.env.OPENCODE_API_KEY = "opencode-should-not-leak";
process.env.CLINE_API_KEY = "cline-should-not-leak";
process.env.CURSOR_API_KEY = "cursor-should-not-leak";
process.env.CURSOR_AUTH_TOKEN = "cursor-token-should-not-leak";
process.env.BOX_TOKEN = "box-should-not-leak";
process.env.OMB_TTS_KEY = "tts-should-not-leak";
Object.assign(process.env, CONTROL_PLANE_FIXTURE);
await instance.adapter.sendTurn({ threadId: "t-hygiene", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.argv).toContain("agent");
expect(seen.argv).toContain("stdio");
expect(seen.argv).toContain("--permission-mode");
expect(seen.env.XAI_API_KEY).toBeUndefined();
expect(seen.env.OPENCODE_API_KEY).toBeUndefined();
expect(seen.env.CLINE_API_KEY).toBeUndefined();
expect(seen.env.CURSOR_API_KEY).toBeUndefined();
expect(seen.env.CURSOR_AUTH_TOKEN).toBeUndefined();
expect(seen.env.BOX_TOKEN).toBeUndefined();
expect(seen.env.OMB_TTS_KEY).toBeUndefined();
for (const name of Object.keys(CONTROL_PLANE_FIXTURE)) expect(seen.env[name]).toBeUndefined();
});
it("mounts connected apps as a stdio MCP server", async () => {
await create();
const dump = join(scratch, "composio.json");
process.env.FAKE_ACP_DUMP = dump;
expect(instance.adapter.capabilities.composioMcp).toBe(true);
await instance.adapter.sendTurn({
threadId: "t-composio",
text: "go",
integrations: {
composio: {
command: process.execPath,
args: ["/tmp/connector-proxy.js"],
env: { OMB_CONNECTOR_UPSTREAM_URL: "http://127.0.0.1:8799/api/internal/connectors/mcp" },
},
},
});
await recorder.until((event) => event.type === "turn.completed");
expect(JSON.parse(readFileSync(`${dump}.mcp.json`, "utf8"))).toContainEqual({
name: "composio",
command: process.execPath,
args: ["/tmp/connector-proxy.js"],
env: [{ name: "OMB_CONNECTOR_UPSTREAM_URL", value: "http://127.0.0.1:8799/api/internal/connectors/mcp" }],
});
});
it("hands an agent that reaches MCP servers by URL OpenMausBot's own servers as endpoints of this server, no proxy process", async () => {
await create(createAcpDriver({ ...SELECT_MODEL_SUPPORT, driverKind: "httpMcpTest", selectModel: undefined, ownMcpOverHttp: true }));
const dump = join(scratch, "own-http.json");
process.env.FAKE_ACP_DUMP = dump;
const harness = "http://127.0.0.1:8799";
await instance.adapter.sendTurn({
threadId: "t-own-http", text: "go",
integrations: {
browser: { command: process.execPath, args: ["/app/dist-server/harness-mcp-proxy.js", "browser"], env: { OMB_HARNESS_URL: harness, OMB_MCP_TOKEN_BROWSER: "synthetic-browser-capability" } },
custom: { notes: { command: process.execPath, args: ["/tmp/notes-mcp.js"], env: {} } },
},
});
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
expect(JSON.parse(readFileSync(`${dump}.mcp.json`, "utf8"))).toEqual([
{ type: "http", name: "browser", url: `${harness}/api/internal/mcp-http/browser`, headers: [{ name: "authorization", value: "Bearer synthetic-browser-capability" }] },
{ name: "notes", command: process.execPath, args: ["/tmp/notes-mcp.js"], env: [] },
]);
// The agent reaches this server's loopback URL around any proxy it was given.
const { env } = JSON.parse(readFileSync(dump, "utf8")) as { env: Record<string, string> };
for (const name of ["NO_PROXY", "no_proxy"]) expect(env[name]!.split(",")).toEqual(expect.arrayContaining(["127.0.0.1", "localhost", "::1"]));
});
it.each([
{ advertised: "http", setting: undefined, form: "url" },
{ advertised: undefined, setting: undefined, form: "stdio" },
{ advertised: "http", setting: "0", form: "stdio" },
])("gives Grok OpenMausBot's own servers by URL in its first session when its initialize says it takes them (advertised=$advertised, OMB_OWN_MCP_HTTP=$setting)", async ({ advertised, setting, form }) => {
if (advertised) process.env.FAKE_ACP_MCP_TRANSPORTS = advertised;
if (setting) vi.stubEnv("OMB_OWN_MCP_HTTP", setting);
try {
await create(GrokAgentDriver);
const dump = join(scratch, "grok-own-http.json");
process.env.FAKE_ACP_DUMP = dump;
const harness = "http://127.0.0.1:8799";
const browser = { command: process.execPath, args: ["/app/dist-server/harness-mcp-proxy.js", "browser"], env: { OMB_HARNESS_URL: harness, OMB_MCP_TOKEN_BROWSER: "synthetic-browser-capability" } };
const notes = { command: process.execPath, args: ["/tmp/notes-mcp.js"], env: {} };
await instance.adapter.sendTurn({ threadId: "t-grok-own-http", text: "go", integrations: { browser, custom: { notes } } });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
const sent = JSON.parse(readFileSync(`${dump}.mcp.json`, "utf8"));
expect(sent[1]).toEqual({ name: "notes", command: process.execPath, args: ["/tmp/notes-mcp.js"], env: [] });
expect(sent[0]).toEqual(form === "url"
? { type: "http", name: "browser", url: `${harness}/api/internal/mcp-http/browser`, headers: [{ name: "authorization", value: "Bearer synthetic-browser-capability" }] }
: { name: "browser", command: process.execPath, args: browser.args, env: [{ name: "OMB_HARNESS_URL", value: harness }, { name: "OMB_MCP_TOKEN_BROWSER", value: "synthetic-browser-capability" }] });
const { env } = JSON.parse(readFileSync(dump, "utf8")) as { env: Record<string, string> };
if (setting !== "0") for (const name of ["NO_PROXY", "no_proxy"]) expect(env[name]!.split(",")).toEqual(expect.arrayContaining(["127.0.0.1", "localhost", "::1"]));
} finally {
delete process.env.FAKE_ACP_MCP_TRANSPORTS;
vi.unstubAllEnvs();
}
});
it.each([false, true])("mounts Data through the existing stdio transport unless excluded (excluded=%s)", async (excluded) => {
await create();
const dump = join(scratch, "data.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
const data = { command: process.execPath, args: [join(scratch, "data-proxy.mjs")], env: { OMB_DATA_TOKEN: "synthetic-turn-capability" } };
expect(instance.adapter.capabilities.dataMcp).toBe(true);
await instance.adapter.sendTurn({
threadId: "t-data", text: "Update the existing result.", system: BUILT_IN_DATA_SYSTEM_PROMPT,
integrations: { data }, ...(excluded ? { toolScope: { allow: ["native:*"] } } : {}),
});
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
expect(JSON.parse(readFileSync(`${dump}.mcp.json`, "utf8"))).toEqual(excluded ? [] : [{
name: "data", ...data, env: [{ name: "OMB_DATA_TOKEN", value: "synthetic-turn-capability" }],
}]);
const prompt = JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8")) as Array<{ type: string; text?: string }>;
expect(prompt.find(block => block.type === "text")?.text).toContain(BUILT_IN_DATA_SYSTEM_PROMPT.trim());
});
it("droid takes model and autonomy over the wire, never through argv", async () => {
// `droid exec -m <id> -o acp` ignores the flag (0.196.0).
instance = await DroidAgentDriver.create({
instanceId: "droid-test",
displayName: "Droid Test",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
recorder = recordEvents(instance.adapter);
const dump = join(scratch, "droid-dump.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({ threadId: "t-droid", text: "go", model: "claude-sonnet-5" });
await recorder.until((e) => e.type === "turn.completed");
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.argv).toEqual(["exec", "-o", "acp"]);
expect(seen.argv).not.toContain("-m");
const applied = JSON.parse(readFileSync(`${dump}.config.json`, "utf8"));
expect(applied).toEqual([
{ method: "session/set_mode", params: { sessionId: "fake-acp-session", modeId: "auto-high" } },
{ method: "session/set_model", params: { sessionId: "fake-acp-session", modelId: "claude-sonnet-5" } },
]);
});
it("droid pins read-only mode when fullAuto is off", async () => {
instance = await DroidAgentDriver.create({
instanceId: "droid-safe",
displayName: "Droid Safe",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
const dump = join(scratch, "droid-safe.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({ threadId: "t-droid-safe", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(`${dump}.config.json`, "utf8"))).toEqual([
{ method: "session/set_mode", params: { sessionId: "fake-acp-session", modeId: "normal" } },
{ method: "session/set_model", params: { sessionId: "fake-acp-session", modelId: "claude-opus-5" } },
]);
});
it("droid names the rejected setting when the agent predates session config", async () => {
process.env.FAKE_ACP_MODE = "no-session-config";
instance = await DroidAgentDriver.create({
instanceId: "droid-old-cli",
displayName: "Droid Old CLI",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({ threadId: "t-droid-skew", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false, stopReason: "rpc_error" });
const err = recorder.events.find((e) => e.type === "runtime.error")!;
expect(err.message).toContain("session/set_mode");
expect(err.message).toContain('autonomy mode "normal"');
expect(err.message).toMatch(/`droid` is current/);
expect(recorder.events.some((e) => e.type === "session.started")).toBe(true);
});
it("mounts local CUA only on an approval-capable ACP instance", async () => {
await create();
const dump = join(scratch, "local-dump.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({
threadId: "t-local",
text: "inspect",
integrations: {
localComputer: {
command: "/opt/cua driver/cua-driver",
args: ["mcp", "--embedded", "--socket", "/run/user/1000/driver.sock"],
env: { CUA_DRIVER_EMBEDDED: "1" },
platform: "linux",
generation: "generation-1",
scope: "local-computer",
},
},
});
await recorder.until((event) => event.type === "turn.completed");
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.mcpServers).toContainEqual({
name: "computer",
command: "/opt/cua driver/cua-driver",
args: ["mcp", "--embedded", "--socket", "/run/user/1000/driver.sock"],
env: [{ name: "CUA_DRIVER_EMBEDDED", value: "1" }],
});
expect(instance.adapter.capabilities.localComputerMcp).toBe(true);
});
it("mounts user-configured custom MCP servers after the built-ins", async () => {
await create();
const dump = join(scratch, "custom-dump.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({
threadId: "t-custom-mcp",
text: "go",
integrations: {
custom: {
notes: { command: "npx", args: ["-y", "@x/notes-mcp"], env: { NOTES_TOKEN: "tok-1" } },
},
},
});
await recorder.until((event) => event.type === "turn.completed");
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.mcpServers).toContainEqual({
name: "notes",
command: "npx",
args: ["-y", "@x/notes-mcp"],
env: [{ name: "NOTES_TOKEN", value: "tok-1" }],
});
expect(instance.adapter.capabilities.customMcp).toBe(true);
});
const remoteServers = {
docs: { type: "http" as const, url: "https://docs.example/mcp", headers: { Authorization: "Bearer tok-docs" } },
legacy: { type: "sse" as const, url: "https://old.example/sse", headers: {} },
notes: { command: "npx", args: [], env: {} },
};
const proxied = (server: { command?: string; args?: string[]; env?: Array<{ name: string; value: string }> }) => {
const value = server.env?.find((entry) => entry.name === "OMB_REMOTE_MCP_SERVER")?.value;
return value === undefined ? undefined : JSON.parse(value);
};
it.each([
["advertises no remote transport", undefined],
["advertises http and sse", "http,sse"],
])("mounts every url server as OpenMausBot's connector for an agent that %s", async (_name, transports) => {
if (transports) process.env.FAKE_ACP_MCP_TRANSPORTS = transports;
try {
await create();
const dump = join(scratch, "remote-mcp.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({ threadId: "t-remote-mcp", text: "go", integrations: { custom: remoteServers } });
await recorder.until((event) => event.type === "turn.completed");
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.mcpServers.map((server: { name: string }) => server.name)).toEqual(["docs", "legacy", "notes"]);
expect(seen.mcpServers.some((server: object) => "url" in server || "type" in server)).toBe(false);
const docs = seen.mcpServers.find((server: { name: string }) => server.name === "docs");
expect(docs.args.join(" ")).toContain("mcp-remote-proxy");
expect(docs.args.join(" ")).not.toContain("tok-docs");
expect(proxied(docs)).toEqual(remoteServers.docs);
expect(docs.env.some((entry: { name: string }) => entry.name === "OMB_REMOTE_MCP_DIRECTORY")).toBe(false);
expect(proxied(seen.mcpServers.find((server: { name: string }) => server.name === "legacy"))).toEqual(remoteServers.legacy);
} finally {
delete process.env.FAKE_ACP_MCP_TRANSPORTS;
}
});
// The fake agent connects each mounted server with grok 1.0.25's handshake, which a strict server refuses.
it("lists a strict url server's tools through OpenMausBot's connector, even for an agent that speaks http", async () => {
const strict = await startFakeHttpMcp({ strictInitialize: "http-400", strictSchema: "2025-11-25",
tools: [{ name: "report", inputSchema: { type: "object" } }, { name: "campaigns", inputSchema: { type: "object" } }],
requireHeader: { name: "Authorization", value: "Bearer tok-voluum" } });
process.env.FAKE_ACP_MCP_TRANSPORTS = "http,sse";
try {
await create(GrokAgentDriver, "mcp-tools");
await instance.adapter.sendTurn({ threadId: "t-strict-acp", text: "go", integrations: { custom: {
voluum: { type: "http", url: strict.url, headers: { Authorization: "Bearer tok-voluum" } },
} } });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
const text = recorder.events.find((event) => event.type === "item.completed" && (event as { itemType?: string }).itemType === "assistant_text");
expect((text as { text?: string } | undefined)?.text).toBe("voluum: report,campaigns");
expect(strict.initializes).toEqual([{ protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "OpenMausBot tool proxy", version: "1" } }]);
} finally {
delete process.env.FAKE_ACP_MCP_TRANSPORTS;
await strict.close();
}
});
it.each([
{ engine: "Kimi", driver: KimiAgentDriver, outcome: "fitted to Moonshot's per-tool limit" },
{ engine: "Grok", driver: GrokAgentDriver, outcome: "as the server sent them" },
])("hands $engine a person's stdio MCP tools $outcome", async ({ driver }) => {
const script = join(scratch, "wide-mcp.mjs");
const tools = [{ name: "small", inputSchema: { type: "object", properties: { q: { type: "string" } } } }, { name: "configure", inputSchema: wideSchema(110) }];
writeFileSync(script, `
import { createInterface } from "node:readline";
createInterface({ input: process.stdin }).on("line", (line) => {
const frame = JSON.parse(line);
if (frame.id === undefined) return;
const result = frame.method === "tools/list" ? { tools: ${JSON.stringify(tools)} } : { protocolVersion: "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "wide", version: "1" } };
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: frame.id, result }) + "\\n");
});`);
process.env.FAKE_ACP_MCP_SCHEMA_BYTES = "1";
try {
await create(driver, "mcp-tools");
await instance.adapter.sendTurn({ threadId: `t-wide-${driver.driverKind}`, text: "go", integrations: { custom: { wide: { command: process.execPath, args: [script], env: {} } } } });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
const text = (recorder.events.find((event) => event.type === "item.completed" && (event as { itemType?: string }).itemType === "assistant_text") as { text?: string } | undefined)?.text;
const sizes = Object.fromEntries(text!.replace(/^wide: /, "").split(",").map((entry) => entry.split("=")).map(([name, bytes]) => [name, Number(bytes)]));
const raw = JSON.stringify(tools[1]!.inputSchema).length;
expect(raw).toBeGreaterThan(12_000);
expect(sizes.small).toBe(JSON.stringify(tools[0]!.inputSchema).length);
if (driver === KimiAgentDriver) expect(sizes.configure).toBeLessThanOrEqual(12_000);
else expect(sizes.configure).toBe(raw);
} finally {
delete process.env.FAKE_ACP_MCP_SCHEMA_BYTES;
}
});
it("tells the person in plain words when Kimi still refuses a tool's schema", async () => {
const failureFile = join(scratch, "schema-refusal.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error",
data: { details: "400 tools.function.parameters is not a valid moonshot flavored json schema, details: <At path 'root': schema exceeds maximum allowed size>" } }));
await create(KimiAgentDriver);
await instance.adapter.sendTurn({ threadId: "t-schema-refusal", text: "go" });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: false });
expect(recorder.events.find((event) => event.type === "runtime.error")).toMatchObject({
message: "Kimi refused this turn as an invalid request because the input schema of one of this bot's tools is too large, so nothing ran. "
+ "Turn off the MCP server or app that tool comes from for this bot, or switch the bot to another AI.",
});
});
it("tells the person where to turn tool selection off when Grok has its own MCP server", async () => {
const rpcFile = join(scratch, "grok-native-mcp-rpc.json");
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
process.env.FAKE_ACP_GROK_NATIVE_MCP = "github";
try {
await create(GrokAgentDriver);
await instance.adapter.sendTurn({ threadId: "t-grok-native-mcp", text: "go", toolScope: { allow: ["native:*"] } });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: false });
expect(recorder.events.find((event) => event.type === "runtime.error")).toMatchObject({
message: "Grok has an MCP connection outside the bot's selection. Remove the MCP servers from Grok's own settings, "
+ "or set Tool selection back to all tools in this bot's settings (Access). No prompt was sent.",
});
expect(JSON.parse(readFileSync(rpcFile, "utf8"))).not.toContain("session/prompt");
} finally {
delete process.env.FAKE_ACP_GROK_NATIVE_MCP;
}
});
it("keeps a tool selection's gate in front of the connector", async () => {
const strict = await startFakeHttpMcp({ strictInitialize: "http-400", tools: [{ name: "report", inputSchema: { type: "object" } }, { name: "campaigns", inputSchema: { type: "object" } }] });
try {
// Kimi: Grok also checks its own catalog over _x.ai/mcp/list, which the fake does not keep.
await create(KimiAgentDriver, "mcp-tools");
await instance.adapter.sendTurn({ threadId: "t-strict-acp-scoped", text: "go", toolScope: { allow: ["native:*", "mcp:voluum:report"] },
integrations: { custom: { voluum: { type: "http", url: strict.url, headers: {} } } } });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
const text = recorder.events.find((event) => event.type === "item.completed" && (event as { itemType?: string }).itemType === "assistant_text");
expect((text as { text?: string } | undefined)?.text).toBe("voluum: report");
expect(strict.initializes).toHaveLength(1);
} finally {
await strict.close();
}
});
it("surfaces a permission ask as request.opened and completes once allowed", async () => {
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({
threadId: "t-perm",
text: "go",
cwd: scratch,
integrations: {
localComputer: {
command: "/cua-driver",
args: ["mcp"],
env: {},
platform: "linux",
scope: "local-computer",
},
},
});
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({
requestType: "permission",
tool: "shell",
approvalScope: "local-computer",
command: { command: "echo hi", cwd: realpathSync(scratch) },
});
expect(await instance.adapter.respondToRequest("t-perm", (opened as any).requestId, { behavior: "allow" })).toBe("allowed-once");
const resolved = await recorder.until((e) => e.type === "request.resolved");
expect(resolved).toMatchObject({
behavior: "allow",
source: "user",
approvalScope: "local-computer",
});
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
});
it.each(["mcp__brain-project__recall", "mcp__composio__gmail_search_emails"])("preserves MCP identity on ACP permission requests: %s", async (name) => {
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify({ kind: "other", title: name, rawInput: {} });
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-mcp-identity", text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened");
expect(opened).toMatchObject({ tool: name, requestType: "permission" });
expect(opened).toHaveProperty("command", undefined);
await instance.adapter.respondToRequest("t-mcp-identity", opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
});
const touch = { sessionUpdate: "tool_call", toolCallId: "call_1", title: "bash: touch marker", kind: "execute", status: "pending", rawInput: { command: "touch marker" } };
// A test can open several cards; afterEach disposes only the last instance, so each card disposes the one it replaces.
let earlierCard: { instance: ProviderInstance; recorder: EventRecorder } | undefined;
afterEach(() => { earlierCard = undefined; });
const permissionCard = async (threadId: string, prior: unknown[], toolCall: unknown) => {
if (earlierCard) {
earlierCard.recorder.stop();
await earlierCard.instance.dispose();
}
process.env.FAKE_ACP_PERMISSION_PRIOR_UPDATES = JSON.stringify(prior);
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify(toolCall);
await create(GrokAgentDriver, "permission");
earlierCard = { instance, recorder };
const { turnId } = await instance.adapter.sendTurn({ threadId, text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened" && event.turnId === turnId);
await instance.adapter.respondToRequest(threadId, opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
return { turnId, opened };
};
it("shows the earlier tool_call on a permission request that names the call only by toolCallId", async () => {
const { opened } = await permissionCard("t-acp-card-id-only", [touch], { toolCallId: "call_1" });
expect(opened).toMatchObject({ requestType: "permission", tool: "tool", summary: "touch marker" }); // tool stays the request's: it drives policy
expect(opened).toHaveProperty("command", undefined);
expect(opened).toHaveProperty("requiresExplicitApproval", undefined);
});
it("merges tool_call_update into the card, a null field keeping the earlier value", async () => {
const { opened } = await permissionCard("t-acp-card-update", [
{ sessionUpdate: "tool_call", toolCallId: "call_1", title: "Bash", kind: "execute", status: "pending" },
{ sessionUpdate: "tool_call_update", toolCallId: "call_1", title: null, kind: null, rawInput: { command: "ls -la" } },
], { toolCallId: "call_1", title: "Bash" });
expect(opened).toMatchObject({ tool: "tool", summary: "ls -la" });
});
it("lets the permission request's own fields win over the earlier tool_call", async () => {
const { opened } = await permissionCard("t-acp-card-own-fields", [touch], { toolCallId: "call_1", kind: "edit", title: "write notes.md" });
expect(opened).toMatchObject({ tool: "edit", summary: "touch marker" });
const own = await permissionCard("t-acp-card-own-command", [touch], { toolCallId: "call_1", kind: "execute", rawInput: { command: "echo hi" } });
expect(own.opened).toMatchObject({ tool: "shell", summary: "echo hi", command: { command: "echo hi", cwd: realpathSync(scratch) } });
});
it("leaves a permission request for an unknown toolCallId as it was", async () => {
const { opened } = await permissionCard("t-acp-card-unknown", [touch], { toolCallId: "call_2" });
expect(opened).toMatchObject({ tool: "tool", summary: "tool" });
});
it("never takes an MCP name or a command grant from an earlier tool_call", async () => {
const mcp = { sessionUpdate: "tool_call", toolCallId: "call_1", title: "mcp__composio__gmail_send_email", kind: "other", status: "pending", rawInput: {} };
const { opened } = await permissionCard("t-acp-card-mcp", [mcp], { toolCallId: "call_1" });
expect(opened.type === "request.opened" && opened.tool.startsWith("mcp__")).toBe(false);
const sandbox = { ...touch, rawInput: { command: "touch marker", sandbox_permissions: "require_escalated" } };
const shell = await permissionCard("t-acp-card-sandbox", [sandbox], { toolCallId: "call_1", kind: "execute" });
expect(shell.opened).toHaveProperty("command", undefined);
expect(shell.opened).toHaveProperty("requiresExplicitApproval", undefined);
for (const kind of ["web_search", "mcp__notes__read_note"]) {
const earlier = { sessionUpdate: "tool_call", toolCallId: "call_1", title: "lookup", kind, status: "pending", rawInput: {} };
const card = await permissionCard(`t-acp-card-kind-${kind}`, [earlier], { toolCallId: "call_1" });
expect(card.opened).toMatchObject({ requestType: "permission", tool: "tool", summary: "lookup" });
}
});
it("keeps a question's own text when an earlier tool_call with the same id ran a command", async () => {
const earlier = { sessionUpdate: "tool_call", toolCallId: "interaction_1", title: "bash", kind: "execute", status: "pending", rawInput: { command: "echo something" } };
const { opened } = await permissionCard("t-acp-card-question", [earlier], { toolCallId: "interaction_1", title: "Which folder?" });
expect(opened).toMatchObject({ requestType: "question", summary: "Which folder?" });
});
it("starts afresh when a new tool_call reuses a toolCallId in the same turn", async () => {
const { opened } = await permissionCard("t-acp-card-reused-id", [
touch,
{ sessionUpdate: "tool_call", toolCallId: "call_1", title: "read notes.md", kind: "read", status: "pending" },
], { toolCallId: "call_1" });
expect(opened).toMatchObject({ tool: "tool", summary: "read notes.md" });
});
it("forgets a turn's tool calls when the next turn asks about the same toolCallId", async () => {
const first = await permissionCard("t-acp-card-turns", [touch], { toolCallId: "call_1" });
expect(first.opened).toMatchObject({ tool: "tool", summary: "touch marker" });
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-acp-card-turns", text: "again", cwd: scratch });
const second = await recorder.until((event) => event.type === "request.opened" && event.turnId === turnId);
expect(second).toMatchObject({ tool: "tool", summary: "tool" });
await instance.adapter.respondToRequest("t-acp-card-turns", second.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);
});
it.each([
{ toolCallId: "call-1", justification: "Reason ".repeat(60), requestType: "permission" },
{ toolCallId: "call-1", justification: 42, requestType: "permission" },
{ toolCallId: "interaction_1", justification: "Question context", requestType: "question" },
])("keeps the $requestType summary limit and appends only a permission's text justification", async ({ toolCallId, justification, requestType }) => {
const command = "echo " + "x".repeat(300);
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify({ toolCallId, kind: "execute", rawInput: { command, justification } });
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-acp-justification", text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened");
const expected = requestType === "question" ? command : command.slice(0, 200)
+ (typeof justification === "string" ? `\nReason: ${justification.slice(0, 300)}` : "");
expect(opened).toMatchObject({ requestType, summary: expected });
await instance.adapter.respondToRequest("t-acp-justification", opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
});
it.each([
{ kind: "execute", title: "mcp__browser__navigate;[email protected]$IFS//x.example" },
{ kind: "edit", title: "mcp__browser__navigate" },
{ kind: "other", title: "mcp__browser__navigate (with a note)" },
])("never takes a title that only looks like an MCP name as one: $kind $title", async (toolCall) => {
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify({ ...toolCall, rawInput: {} });
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-mcp-lookalike", text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened");
expect(opened.type === "request.opened" && opened.tool.startsWith("mcp__")).toBe(false);
await instance.adapter.respondToRequest("t-mcp-lookalike", opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
});
it.each([
{ name: "full raw command", toolCall: { kind: "execute", rawInput: { command: `printf ' ${"complete input ".repeat(30)}'\n pwd ` } }, descriptor: true },
{ name: "title without raw input", toolCall: { kind: "execute", title: "echo display only" }, descriptor: false },
{ name: "argv raw input", toolCall: { kind: "execute", rawInput: { command: ["echo", "do not join argv"] } }, descriptor: false },
{ name: "MCP tool", toolCall: { kind: "other", title: "mcp__example__run", rawInput: { command: "echo not a shell approval" } }, descriptor: false },
{ name: "MCP execute tool", toolCall: { kind: "execute", title: "mcp__example__run", rawInput: { command: "echo not a native shell approval" } }, descriptor: false },
{ name: "question", toolCall: { toolCallId: "interaction_command", kind: "execute", rawInput: { command: "echo not a shell approval" } }, descriptor: false },
{ name: "relative cwd", toolCall: { kind: "execute", rawInput: { command: "pwd", cwd: "unknown-relative-directory" } }, descriptor: false },
])("keeps command grants scoped to complete shell input: $name", async ({ toolCall, descriptor }) => {
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify(toolCall);
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-acp-command-descriptor", text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened");
expect(opened).toHaveProperty("command", descriptor ? { command: toolCall.rawInput?.command, cwd: realpathSync(scratch) } : undefined);
await instance.adapter.respondToRequest("t-acp-command-descriptor", opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
});
it("uses an explicit ACP shell directory and rejects conflicting directory metadata", async () => {
const command = "pwd";
const directory = join(scratch, "execution-directory");
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify({ kind: "execute", rawInput: { command, workdir: directory } });
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-acp-explicit-cwd", text: "go", cwd: scratch });
const opened = await recorder.until((event) => event.type === "request.opened");
expect(opened).toHaveProperty("command", { command, cwd: directory });
await instance.adapter.respondToRequest("t-acp-explicit-cwd", opened.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
// A separate process observes a new fake provider payload.
await instance.dispose();
recorder.stop();
process.env.FAKE_ACP_PERMISSION_TOOL_CALL = JSON.stringify({ kind: "execute", rawInput: { command, cwd: scratch, workdir: directory } });
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-acp-conflicting-cwd", text: "go", cwd: scratch });
const conflict = await recorder.until((event) => event.type === "request.opened");
expect(conflict).toHaveProperty("command", undefined);
await instance.adapter.respondToRequest("t-acp-conflicting-cwd", conflict.requestId!, { behavior: "deny" });
await recorder.until((event) => event.type === "turn.completed");
});
it("emits a structured question beside the flat choices", async () => {
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-shape", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({
requestType: "question",
summary: "Which color?",
choices: ["Blue", "Green"],
questions: [{ question: "Which color?", options: [{ label: "Blue" }, { label: "Green" }] }],
});
});
it("answers a structured card reply by recovering the picked option", async () => {
const answer = join(scratch, "question-answer.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-answer", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
// Exactly what the tabbed QuestionCard submits: one Q:/A: block, not a bare option label.
const outcome = await instance.adapter.respondToRequest("t-question-answer", (opened as { requestId: string }).requestId, {
behavior: "answer",
message: "The user answered your questions.\n\nQ: Which color?\nA: Green",
});
expect(outcome).toBe("answered");
const resolved = await recorder.until((e) => e.type === "request.resolved");
expect(resolved).toMatchObject({ behavior: "answer", source: "user" });
await recorder.until((e) => e.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe("green-id");
});
it("cancels a question whose answer matches no offered option", async () => {
const answer = join(scratch, "question-cancel.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-cancel", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
await instance.adapter.respondToRequest("t-question-cancel", (opened as { requestId: string }).requestId, {
behavior: "answer",
message: "Purple",
});
const resolved = await recorder.until((e) => e.type === "request.resolved");
expect(resolved).toMatchObject({ behavior: "deny", source: "system" });
const error = recorder.events.find((e) => e.type === "runtime.error") as { message: string } | undefined;
expect(error?.message).toContain("isn't one of the choices");
expect(error?.message).not.toContain("Agent");
await recorder.until((e) => e.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe("cancelled");
});
it("marks an ACP question as options-only, so the card offers no free text", async () => {
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-custom", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({ questions: [{ question: "Which color?", custom: false }] });
});
// Antigravity's shape: options carry their own ids and names, and after "cancelled" it waits for session/cancel.
it.each([
{ name: "a typed answer", response: { behavior: "answer", message: "The user answered your questions.\n\nQ: Which article?\nA: my own article" }, error: true },
{ name: "a closed question", response: { behavior: "deny" }, error: false },
])("ends the turn instead of hanging after $name matches no option", async ({ response, error }) => {
process.env.FAKE_ACP_HOLD_ON_CANCELLED = "1";
process.env.FAKE_ACP_QUESTION_OPTIONS = JSON.stringify([
{ optionId: "opt-0", kind: "allow_once", name: "Machines of Loving Grace (Recommended)" },
{ optionId: "opt-1", kind: "allow_once", name: "A new piece on reasoning models" },
]);
const answer = join(scratch, "question-hold.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-hold", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
await instance.adapter.respondToRequest("t-question-hold", (opened as { requestId: string }).requestId, response as never);
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ stopReason: "cancelled" });
expect(readFileSync(answer, "utf8")).toBe("cancelled");
expect(recorder.events.some((e) => e.type === "runtime.error")).toBe(error);
});
it("still sends the picked option id on Antigravity's option shape", async () => {
process.env.FAKE_ACP_HOLD_ON_CANCELLED = "1";
process.env.FAKE_ACP_QUESTION_OPTIONS = JSON.stringify([
{ optionId: "opt-0", kind: "allow_once", name: "Machines of Loving Grace (Recommended)" },
{ optionId: "opt-1", kind: "allow_once", name: "A new piece on reasoning models" },
]);
const answer = join(scratch, "question-pick.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-question-pick", text: "go" });
const opened = await recorder.until((e) => e.type === "request.opened");
expect(await instance.adapter.respondToRequest("t-question-pick", (opened as { requestId: string }).requestId, {
behavior: "answer",
message: "The user answered your questions.\n\nQ: Which color?\nA: A new piece on reasoning models",
})).toBe("answered");
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
expect(readFileSync(answer, "utf8")).toBe("opt-1");
});
it.each([false, true])("maps capped labels back to their option id, refusing collisions (%s)", async collision => {
const label = "Green ".repeat(30);
const answer = join(scratch, "long-question-answer.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
process.env.FAKE_ACP_QUESTION_OPTIONS = JSON.stringify([
{ optionId: "green-id", kind: "allow_once", name: label },
{ optionId: "other-id", kind: "allow_once", name: collision ? label + "other" : "Blue" },
]);
await create(GrokAgentDriver, "question");
await instance.adapter.sendTurn({ threadId: "t-long-question", text: "go" });
const opened = await recorder.until(e => e.type === "request.opened");
expect(opened).toMatchObject({ choices: expect.arrayContaining([label.trim().slice(0, 120).trim()]) });
await instance.adapter.respondToRequest("t-long-question", (opened as { requestId: string }).requestId, {
behavior: "answer",
message: `The user answered your questions.\n\nQ: Which color?\nA: ${label.trim().slice(0, 120)}`,
});
await recorder.until(e => e.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe(collision ? "cancelled" : "green-id");
});
describe("Grok ask_user_question", () => {
// The request Grok's shell sends when its ask_user_question tool runs
// (xai-org/grok-build AskUserQuestionExtRequest, camelCase on the wire).
const GROK_ASK = {
sessionId: "fake-acp-session",
toolCallId: "call-1",
mode: "default",
questions: [
{ question: "Which database?", options: [{ label: "Redis", description: "In-memory store" }, { label: "Postgres", description: "Relational" }] },
{ question: "Which caches?", multiSelect: true, options: [{ label: "Hot path", description: "" }, { label: "Cold path", description: "" }] },
{ question: "Anything else?", options: [{ label: "No", description: "" }, { label: "Yes", description: "" }] },
],
};
const ask = async (threadId: string, env: Record<string, string> = {}, approvalMode?: "full") => {
Object.assign(process.env, {
FAKE_ACP_ASK_METHOD: "_x.ai/ask_user_question",
FAKE_ACP_ASK_PARAMS: JSON.stringify(GROK_ASK),
FAKE_ACP_ASK_ANSWER: join(scratch, "grok-ask.json"),
...env,
});
await create(GrokAgentDriver, "ask-question");
await instance.adapter.sendTurn({ threadId, text: "go", ...(approvalMode ? { approvalMode } : {}) });
};
const reply = () => JSON.parse(readFileSync(join(scratch, "grok-ask.json"), "utf8"));
it("asks every question on one card and keys the answers by Grok's question text", async () => {
await ask("t-grok-ask");
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({
requestType: "question",
tool: "ask_user",
summary: "Which database? (+2 more questions)",
questions: [
{ question: "Which database?", options: [{ label: "Redis", description: "In-memory store" }, { label: "Postgres", description: "Relational" }] },
{ question: "Which caches?", multiSelect: true, options: [{ label: "Hot path" }, { label: "Cold path" }] },
{ question: "Anything else?", options: [{ label: "No" }, { label: "Yes" }] },
],
});
const outcome = await instance.adapter.respondToRequest("t-grok-ask", (opened as { requestId: string }).requestId, {
behavior: "answer",
message: "The user answered your questions.\n\nQ: Which database?\nA: Postgres\n\nQ: Which caches?\nA: Hot path, Cold path, and the CDN\n\nQ: Anything else?\nA: Use version 16",
});
expect(outcome).toBe("answered");
await recorder.until((e) => e.type === "turn.completed");
expect(reply()).toEqual({ result: {
outcome: "accepted",
answers: { "Which database?": ["Postgres"], "Which caches?": ["Hot path", "Cold path", "Other"], "Anything else?": ["Other"] },
annotations: { "Which caches?": { notes: "and the CDN" }, "Anything else?": { notes: "Use version 16" } },
} });
});
it("still asks under Full access, reads multi_select, and accepts the bare method name", async () => {
await ask("t-grok-ask-full", {
FAKE_ACP_ASK_METHOD: "x.ai/ask_user_question",
FAKE_ACP_ASK_PARAMS: JSON.stringify({ ...GROK_ASK, questions: [{ question: "Which caches?", multi_select: true, options: [{ label: "Hot path" }, { label: "Cold path" }] }] }),
}, "full");
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({ requestType: "question", questions: [{ question: "Which caches?", multiSelect: true }] });
// a multi-select question has no flat one-tap answer
expect(opened).not.toHaveProperty("choices");
await instance.adapter.respondToRequest("t-grok-ask-full", (opened as { requestId: string }).requestId, { behavior: "answer", message: "Cold path" });
await recorder.until((e) => e.type === "turn.completed");
expect(reply()).toEqual({ result: { outcome: "accepted", answers: { "Which caches?": ["Cold path"] } } });
});
it("cancels a denied card and refuses malformed params", async () => {
await ask("t-grok-ask-deny");
const opened = await recorder.until((e) => e.type === "request.opened");
expect(await instance.adapter.respondToRequest("t-grok-ask-deny", (opened as { requestId: string }).requestId, { behavior: "deny" })).toBe("rejected");
expect(await recorder.until((e) => e.type === "request.resolved")).toMatchObject({ behavior: "deny", source: "user" });
await recorder.until((e) => e.type === "turn.completed");
expect(reply()).toEqual({ result: { outcome: "cancelled" } });
process.env.FAKE_ACP_ASK_PARAMS = JSON.stringify({ ...GROK_ASK, questions: [{ options: [{ label: "Redis" }] }] });
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-grok-ask-deny", text: "again" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
expect(reply()).toMatchObject({ error: { code: -32602 } });
expect(recorder.events.filter((e) => e.type === "request.opened")).toHaveLength(1);
// More questions than one card holds: refused whole, not partly answered.
const seven = Array.from({ length: 7 }, (_, i) => ({ question: `Question ${i}?`, options: [{ label: "Yes" }, { label: "No" }] }));
process.env.FAKE_ACP_ASK_PARAMS = JSON.stringify({ ...GROK_ASK, questions: seven });
const many = await instance.adapter.sendTurn({ threadId: "t-grok-ask-deny", text: "seven" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === many.turnId);
expect(reply()).toMatchObject({ error: { code: -32602, message: "ask needs 1 to 6 distinct questions with question text" } });
expect(recorder.events.filter((e) => e.type === "request.opened")).toHaveLength(1);
// Two questions with the same text: Grok keys answers by text, so refused.
process.env.FAKE_ACP_ASK_PARAMS = JSON.stringify({ ...GROK_ASK, questions: [GROK_ASK.questions[0], GROK_ASK.questions[0]] });
const twins = await instance.adapter.sendTurn({ threadId: "t-grok-ask-deny", text: "twins" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === twins.turnId);
expect(reply()).toMatchObject({ error: { code: -32602 } });
expect(recorder.events.filter((e) => e.type === "request.opened")).toHaveLength(1);
});
it("cancels a question that arrives between turns", async () => {
await ask("t-grok-ask-idle", { FAKE_ACP_ASK_TIMING: "after-result" });
await recorder.until((e) => e.type === "turn.completed");
for (let i = 0; i < 80 && !existsSync(join(scratch, "grok-ask.json")); i++) await new Promise((resolve) => setTimeout(resolve, 25));
expect(reply()).toEqual({ result: { outcome: "cancelled" } });
expect(recorder.events.some((e) => e.type === "request.opened")).toBe(false);
});
});
it("per-bot Ask surfaces permissions from a legacy full-auto instance", async () => {
process.env.FAKE_ACP_MODE = "permission";
const dump = join(scratch, "ask-permission-overrides-full-auto.json");
process.env.FAKE_ACP_DUMP = dump;
instance = await GrokAgentDriver.create({
instanceId: "grok-ask-override",
displayName: "Grok Ask Override",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({
threadId: "t-ask-permission-override",
text: "go",
approvalMode: "ask",
});
const opened = await recorder.until((e) => e.type === "request.opened");
const argv = JSON.parse(readFileSync(dump, "utf8")).argv as string[];
expect(argv[argv.indexOf("--permission-mode") + 1]).toBe("default");
await instance.adapter.respondToRequest(
"t-ask-permission-override",
(opened as { requestId: string }).requestId,
{ behavior: "allow" },
);
await recorder.until((e) => e.type === "turn.completed");
});
it("maps Auto-accept edits to Grok's native acceptEdits", async () => {
await create(GrokAgentDriver);
const dump = join(scratch, "grok-edits.json");
process.env.FAKE_ACP_DUMP = dump;
const { turnId } = await instance.adapter.sendTurn({ threadId: "t-grok-edits", text: "go", approvalMode: "edits" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
const argv = JSON.parse(readFileSync(dump, "utf8")).argv as string[];
expect(argv[argv.indexOf("--permission-mode") + 1]).toBe("acceptEdits");
});
it.each([false, true])("returns rejected when an allow has no native permission option (always: %s)", async (always) => {
process.env.FAKE_ACP_PERMISSION_OPTIONS = JSON.stringify([{ optionId: "reject", kind: "reject_once" }]);
const answer = join(scratch, "reject-only-answer.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "permission");
const threadId = "t-reject-only";
await instance.adapter.sendTurn({ threadId, text: "go", approvalMode: "ask" });
const opened = await recorder.until((event) => event.type === "request.opened");
expect(await instance.adapter.respondToRequest(threadId, "unknown-request", { behavior: "allow" })).toBe("unavailable");
expect(await instance.adapter.respondToRequest(threadId, opened.requestId!, { behavior: "allow", always })).toBe("rejected");
const resolved = await recorder.until((event) => event.type === "request.resolved" && event.requestId === opened.requestId);
expect(resolved).toMatchObject({ behavior: "deny", source: "system" });
await recorder.until((event) => event.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe("cancelled");
expect(await instance.adapter.respondToRequest(threadId, opened.requestId!, { behavior: "allow" })).toBe("unavailable");
});
it("hands 'Always allow this session' to the agent's own allow_always option", async () => {
process.env.FAKE_ACP_ALLOW_ALWAYS = "1";
const answer = join(scratch, "permission-answer.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GeminiAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-always-native", text: "go", approvalMode: "ask" });
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toHaveProperty("allowSession", true);
await instance.adapter.respondToRequest("t-always-native", (opened as { requestId: string }).requestId, { behavior: "allow", always: true });
await recorder.until((e) => e.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe("allow-always");
});
it("remembers the exact operation for the session when the agent offers no allow_always", async () => {
// Grok 4.6 often omits allow_always.
const answer = join(scratch, "permission-answer-once.txt");
process.env.FAKE_ACP_PERMISSION_ANSWER = answer;
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-always-memory", text: "go", approvalMode: "ask" });
const opened = await recorder.until((e) => e.type === "request.opened");
await instance.adapter.respondToRequest("t-always-memory", (opened as { requestId: string }).requestId, { behavior: "allow", always: true });
await recorder.until((e) => e.type === "turn.completed");
expect(readFileSync(answer, "utf8")).toBe("allow-once");
const second = await instance.adapter.sendTurn({ threadId: "t-always-memory", text: "again", approvalMode: "ask", resumeCursor: "fake-acp-session" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(recorder.events.filter((e) => e.type === "request.opened")).toHaveLength(1);
expect(readFileSync(answer, "utf8")).toBe("allow-once");
// The pool keeps the native session alive; stopAll closes it so the third turn gets a fresh one.
await instance.adapter.stopAll();
const third = await instance.adapter.sendTurn({ threadId: "t-always-memory", text: "fresh", approvalMode: "ask" });
const reopened = await recorder.until((e) => e.type === "request.opened" && e.turnId === third.turnId);
await instance.adapter.respondToRequest("t-always-memory", (reopened as { requestId: string }).requestId, { behavior: "deny" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === third.turnId);
expect(recorder.events.filter((e) => e.type === "request.opened")).toHaveLength(2);
});
it.each(["grok-4.7", "grok-4.6", "grok-4.5", "local-model"])(
"keeps native Grok Auto when selecting and resuming %s",
async (model) => {
await create(GrokAgentDriver);
const agents = { command: "node", args: ["/fixture/agents-proxy.mjs"], env: {} };
for (const resumeCursor of [undefined, "fake-acp-session"]) {
const dump = join(scratch, `grok-auto-${resumeCursor ? "resume" : "new"}.json`);
const threadId = `t-grok-auto-${model}-${resumeCursor ? "resume" : "new"}`;
process.env.FAKE_ACP_DUMP = dump;
const { turnId } = await instance.adapter.sendTurn({
threadId,
text: "read the roster",
model,
effort: "high",
approvalMode: "auto",
resumeCursor,
integrations: {
agents,
custom: { agents: { command: "must-not-shadow-agents", args: [], env: {} } },
},
});
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
const seen = JSON.parse(readFileSync(dump, "utf8"));
expect(seen.argv).toEqual([
"--permission-mode", "auto",
"agent", "-m", model, "--reasoning-effort", "high", "stdio",
]);
const sessionMethod = resumeCursor ? "session/load" : "session/new";
const sent = readFileSync(join(NATIVE_DIR, `${threadId}.ndjson`), "utf8")
.trim().split("\n").map((line) => JSON.parse(line))
.find((entry) => entry.dir === "out" && entry.msg.method === sessionMethod);
expect(sent.msg.params.mcpServers).toEqual([{ name: "agents", ...agents, env: [] }]);
expect(JSON.parse(readFileSync(`${dump}.config.json`, "utf8"))).toEqual([
{ method: "session/set_model", params: { sessionId: "fake-acp-session", modelId: model } },
]);
expect(recorder.events.find((e) => e.type === "session.started" && e.turnId === turnId))
.toMatchObject({ model });
}
},
);
it("sends Grok session/set_model only when the session runs another model", async () => {
await create(GrokAgentDriver);
process.env.FAKE_ACP_SESSION_MODELS = "grok-4.7|Grok 4.7,grok-4.6|Grok 4.6";
try {
const configCalls = async (threadId: string, model: string) => {
const dump = join(scratch, `${threadId}.json`);
process.env.FAKE_ACP_DUMP = dump;
const { turnId } = await instance.adapter.sendTurn({
threadId, text: "use a skill", model, approvalMode: "auto",
integrations: { agents: { command: "node", args: ["/fixture/agents-proxy.mjs"], env: {} } },
});
await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
return existsSync(`${dump}.config.json`) ? JSON.parse(readFileSync(`${dump}.config.json`, "utf8")) : [];
};
expect(await configCalls("t-grok-same-model", "grok-4.7")).toEqual([]);
expect(await configCalls("t-grok-other-model", "grok-4.6")).toEqual([
{ method: "session/set_model", params: { sessionId: "fake-acp-session", modelId: "grok-4.6" } },
]);
} finally {
delete process.env.FAKE_ACP_SESSION_MODELS;
}
});
it.each([
["ask", true, "default"],
["full", true, "bypassPermissions"],
["custom", true, "default"],
["auto", false, "auto"],
] as const)("Grok %s with agents mounted=%s overrides a legacy full-auto setting", async (approvalMode, mounted, nativeMode) => {
instance = await GrokAgentDriver.create({
instanceId: "grok-mode-override",
displayName: "Grok",
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
recorder = recordEvents(instance.adapter);
const dump = join(scratch, "grok-mode-override.json");
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({
threadId: "t-grok-mode-override",
text: "continue",
approvalMode,
resumeCursor: "fake-acp-session",
integrations: mounted ? { agents: { command: "node", args: [], env: {} } } : undefined,
});
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(dump, "utf8")).argv).toEqual([
"--permission-mode", nativeMode, "agent", "stdio",
]);
});
it("keeps residual Grok Auto permissions interactive", async () => {
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({
threadId: "t-grok-auto-permission",
text: "run the command",
approvalMode: "auto",
integrations: { agents: { command: "node", args: [], env: {} } },
});
const opened = await recorder.until((e) => e.type === "request.opened");
expect(opened).toMatchObject({ requestType: "permission", tool: "shell" });
expect(recorder.events.some((e) => e.type === "request.resolved")).toBe(false);
await instance.adapter.respondToRequest("t-grok-auto-permission", opened.requestId!, { behavior: "deny" });
expect(await recorder.until((e) => e.type === "request.resolved"))
.toMatchObject({ behavior: "deny", source: "user" });
await recorder.until((e) => e.type === "turn.completed");
});
it("grok accepts the grok.com authentication method offered by current CLI versions", async () => {
process.env.FAKE_ACP_AUTH_METHOD = "grok.com";
await create(GrokAgentDriver);
await instance.adapter.sendTurn({ threadId: "t-grok-current-auth", text: "go" });
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: true });
});
it("grok fails closed when the CLI advertises no supported authentication method", async () => {
await create(GrokAgentDriver, "no-auth");
await instance.adapter.sendTurn({ threadId: "t-auth", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false, stopReason: "auth_required" });
const err = recorder.events.find((e) => e.type === "runtime.error")!;
expect(err.message).toMatch(/not signed in/);
});
it("grok local inject does not require grok.com login", async () => {
process.env.FAKE_ACP_MODE = "no-auth";
mkdirSync(join(scratch, ".grok"), { recursive: true });
instance = await GrokAgentDriver.create({
instanceId: "acp-test",
displayName: "ACP Test",
environment: { HOME: scratch, GROK_HOME: join(scratch, ".grok") },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({
threadId: "t-local-auth",
text: "go",
model: "omlx::MiniMax-M3-4bit",
});
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
expect(recorder.events.some((e) => e.type === "runtime.error")).toBe(false);
});
it("gemini proceeds through a missing auth method (lenient login)", async () => {
await create(GeminiAgentDriver, "no-auth");
await instance.adapter.sendTurn({ threadId: "t-lenient", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
expect(recorder.events.some((e) => e.provider === "geminiAgent")).toBe(true);
});
it("starts Gemini CLI on its stable ACP surface", async () => {
const dump = join(scratch, "gemini-acp.json");
process.env.FAKE_ACP_DUMP = dump;
await create(GeminiAgentDriver);
await instance.adapter.sendTurn({ threadId: "t-gemini-acp", text: "go", model: "gemini-test" });
await recorder.until((e) => e.type === "turn.completed");
const argv = JSON.parse(readFileSync(dump, "utf8")).argv as string[];
expect(argv).toEqual(["--acp", "-m", "gemini-test"]);
expect(argv).not.toContain("--experimental-acp");
});
it("rejects a second turn while one is in flight", async () => {
await create(GrokAgentDriver, "hang");
await instance.adapter.sendTurn({ threadId: "t-busy", text: "one" });
await recorder.until((e) => e.type === "session.started");
await expect(instance.adapter.sendTurn({ threadId: "t-busy", text: "two" })).rejects.toThrow(/already running/);
await instance.adapter.interruptTurn("t-busy");
await recorder.until((e) => e.type === "turn.completed");
});
it("interrupt settles a hung turn as cancelled", async () => {
await create(GrokAgentDriver, "hang");
await instance.adapter.sendTurn({ threadId: "t-int", text: "go" });
await recorder.until((e) => e.type === "session.started");
await instance.adapter.interruptTurn("t-int");
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ type: "turn.completed" });
});
it("a Stop the agent answers with end_turn is the person's stop, never a lost turn", async () => {
// OpenCode answers session/cancel by ending the prompt with end_turn, not cancelled.
process.env.FAKE_ACP_CANCEL_STOP_REASON = "end_turn";
const rpc = join(scratch, "rpc.json");
process.env.FAKE_ACP_RPC_DUMP = rpc;
await create(GrokAgentDriver, "hang");
await instance.adapter.sendTurn({ threadId: "t-stop-end-turn", text: "go" });
// The fake CLI starts as a new process and shakes hands before the prompt: the file's other waits on it allow 10 s.
await vi.waitFor(() => expect(JSON.parse(readFileSync(rpc, "utf8"))).toContain("session/prompt"), { timeout: 10_000 });
await instance.adapter.interruptTurn("t-stop-end-turn");
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true, stopReason: "cancelled" });
expect(recorder.events.filter((e) => e.type === "runtime.error")).toEqual([]);
});
it("a Stop before the prompt is sent ends quietly, with no error", async () => {
await create(GrokAgentDriver, "hang-initialize");
await instance.adapter.sendTurn({ threadId: "t-stop-startup", text: "go" });
await recorder.until((e) => e.type === "turn.started");
await instance.adapter.interruptTurn("t-stop-startup");
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true, stopReason: "cancelled" });
expect(recorder.events.filter((e) => e.type === "runtime.error")).toEqual([]);
});
it("an exit before result becomes runtime.error + failed turn", async () => {
await create(GrokAgentDriver, "exit-early");
await instance.adapter.sendTurn({ threadId: "t-crash", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false });
expect(recorder.events.some((e) => e.type === "runtime.error")).toBe(true);
});
it("an exit before result on a Node.js older than the engine needs names both versions", async () => {
await create(NodeTooOldDriver, "exit-early");
await instance.adapter.sendTurn({ threadId: "t-old-node", text: "go" });
const failed = await recorder.until((e) => e.type === "runtime.error") as { message: string; text?: unknown };
expect(failed.text).toEqual({ key: "chat.notice.acpNodeTooOld",
params: { provider: SELECT_MODEL_SUPPORT.displayName, needed: "999.0.0", found: expect.stringMatching(/^\d+\.\d+\.\d+$/) } });
expect(failed.message).toMatch(/ needs Node\.js 999\.0\.0 or newer, but this computer has Node\.js \d+\.\d+\.\d+\. Update Node\.js/);
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false });
});
it("an exit before result on a new enough Node.js keeps the exit words", async () => {
await create(NodeOkDriver, "exit-early");
await instance.adapter.sendTurn({ threadId: "t-ok-node", text: "go" });
const failed = await recorder.until((e) => e.type === "runtime.error") as { message: string; text?: unknown };
expect(failed.message).toMatch(/ exited 3 before the prompt result/);
expect(failed.text).toBeUndefined();
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false });
});
it("an engine that aborts says so in plain words, with its last output masked", async () => {
await create(GrokAgentDriver, "abort-in-prompt");
await instance.adapter.sendTurn({ threadId: "t-abort", text: "go" });
const failed = await recorder.until((e) => e.type === "runtime.error") as { message: string };
expect(failed.message).toMatch(/ exited 134 before the prompt result: it stopped itself \(abort\)\. Last output: Open https:\/\/accounts\.google\.com\/o\/oauth2\/v2\/auth\?«redacted \d+ chars» \| 2 third_party\/tcmalloc\/internal\/sysinfo\.h:76\] CHECK in NumCPUs: maybe_cpus\.has_value\(\) \(false\)$/u);
for (const secret of ["private-state", "private-code-value"]) expect(failed.message).not.toContain(secret);
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false });
});
it("does not expire an agent while a person is answering an approval", async () => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "150";
await create(GrokAgentDriver, "permission");
await instance.adapter.sendTurn({ threadId: "t-idle-approval", text: "go", approvalMode: "ask" });
const opened = await recorder.until(e => e.type === "request.opened");
await new Promise(resolve => setTimeout(resolve, 450));
expect(recorder.events.some(e => e.type === "turn.completed")).toBe(false);
await instance.adapter.respondToRequest("t-idle-approval", (opened as { requestId: string }).requestId, { behavior: "allow" });
expect(await recorder.until(e => e.type === "turn.completed")).toMatchObject({ ok: true });
await new Promise(resolve => setTimeout(resolve, 300));
expect(recorder.events.some(e => e.type === "runtime.error")).toBe(false);
});
it.each([GrokAgentDriver, QwenAgentDriver])("does not expire $driverKind while a tool it started is still running", async (driver) => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "150";
process.env.FAKE_ACP_TOOL_MS = "600";
await create(driver, "slow-tool");
await instance.adapter.sendTurn({ threadId: "t-slow-tool", text: "go" });
expect(await recorder.until(e => e.type === "turn.completed")).toMatchObject({ ok: true });
expect(recorder.events.some(e => e.type === "runtime.error")).toBe(false);
expect(recorder.events.find(e => e.type === "item.completed" && e.itemType === "tool")).toMatchObject({ ok: true });
});
it("reports a running tool's kind for the chat's status line while it runs silently", async () => {
process.env.FAKE_ACP_TOOL_MS = "300";
await create(QwenAgentDriver, "slow-tool");
await instance.adapter.sendTurn({ threadId: "t-slow-kind", text: "go" });
await recorder.until(e => e.type === "turn.completed");
const types = recorder.events.map(e => e.type);
const progress = recorder.events.filter(e => e.type === "turn.progress");
expect(progress[0]).toMatchObject({ activity: "command", itemId: "tc-slow" });
expect(types.indexOf("turn.progress")).toBeGreaterThan(types.indexOf("item.started"));
expect(types.lastIndexOf("turn.progress")).toBeLessThan(types.indexOf("item.completed"));
});
it("shows a running tool's output and names the agent process while the tool runs", async () => {
const gate = join(scratch, "slow-tool-done");
Object.assign(process.env, { FAKE_ACP_TOOL_GATE: gate, FAKE_ACP_TOOL_OUTPUT: "Collecting 12 items\npage 3 of 40" });
try {
await create(QwenAgentDriver, "slow-tool");
await instance.adapter.sendTurn({ threadId: "t-slow-output", text: "go" });
expect(await recorder.until(e => e.type === "item.output")).toMatchObject({ itemId: "tc-slow", text: "Collecting 12 items\npage 3 of 40", whole: true });
const pid = instance.adapter.enginePid?.("t-slow-output");
expect(pid).toEqual(expect.any(Number));
expect(() => process.kill(pid!, 0)).not.toThrow();
writeFileSync(gate, "done");
await recorder.until(e => e.type === "turn.completed");
expect(instance.adapter.enginePid?.("t-slow-output")).toBeUndefined();
} finally {
delete process.env.FAKE_ACP_TOOL_GATE;
delete process.env.FAKE_ACP_TOOL_OUTPUT;
}
});
it.each([GrokAgentDriver, QwenAgentDriver])("still fails $driverKind when it goes silent once its tool has finished", async (driver) => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "150";
await create(driver, "stall-after-tool");
await instance.adapter.sendTurn({ threadId: "t-stall-tool", text: "go" });
expect(await recorder.until(e => e.type === "turn.completed")).toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(recorder.events.find(e => e.type === "runtime.error")?.message).toMatch(/no tool running/i);
expect(instance.adapter.hasSession("t-stall-tool")).toBe(false);
});
it("tells the person what a quiet agent is doing, then finishes the turn", async () => {
// Test the driver's notices, not how fast this OS starts ps/PowerShell.
vi.spyOn(quietStatus, "sampleProcessTree").mockResolvedValue({ cpuMs: 0, connections: 1 });
process.env.OMB_ACP_QUIET_NOTICE_MS = "150";
process.env.OMB_ACP_QUIET_TICK_MS = "50";
process.env.FAKE_ACP_QUIET_MS = "700";
await create(GrokAgentDriver, "quiet-then-answer");
await instance.adapter.sendTurn({ threadId: "t-quiet", text: "go" });
expect(await recorder.until(e => e.type === "turn.completed")).toMatchObject({ ok: true });
const notices = recorder.events.filter(e => e.type === "runtime.notice") as Array<{ message: string }>;
expect(notices.length).toBeGreaterThanOrEqual(1);
expect(notices[0].message).toMatch(/^Grok (is waiting on its model|is busy|has sent nothing)/);
expect(recorder.events.some(e => e.type === "runtime.error")).toBe(false);
});
it("does not count idle time before a new prompt toward the quiet notice", async () => {
vi.spyOn(quietStatus, "sampleProcessTree").mockResolvedValue({ cpuMs: 0, connections: 1 });
process.env.OMB_ACP_QUIET_NOTICE_MS = "400";
process.env.OMB_ACP_QUIET_TICK_MS = "50";
process.env.FAKE_ACP_QUIET_MS = "200";
await create(GrokAgentDriver, "quiet-then-answer");
await instance.adapter.sendTurn({ threadId: "t-quiet-idle", text: "one" });
await recorder.until(e => e.type === "turn.completed");
await new Promise(r => setTimeout(r, 800));
const before = recorder.events.length;
await instance.adapter.sendTurn({ threadId: "t-quiet-idle", text: "two" });
await recorder.until(e => e.type === "turn.completed" && recorder.events.indexOf(e) >= before);
expect(recorder.events.filter(e => e.type === "runtime.notice")).toEqual([]);
});
it("reads Qwen's debug log: a logged rate-limit retry is reported and keeps the turn alive", async () => {
vi.spyOn(quietStatus, "sampleProcessTree").mockResolvedValue({ cpuMs: 0, connections: 1 });
process.env.QWEN_HOME = scratch;
// Margins wide enough for a slow Windows runner: the log must be read well inside the idle guard.
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "1500";
process.env.OMB_ACP_QUIET_NOTICE_MS = "150";
process.env.OMB_ACP_QUIET_TICK_MS = "50";
process.env.FAKE_ACP_QUIET_MS = "3500";
process.env.FAKE_ACP_QWEN_LOG = JSON.stringify([
"[ERROR] [OPENAI_ERROR] OpenAI API Error: 429 rate limited {",
"[WARN] [RETRY] Attempt 1 failed with status 429. Retrying after explicit delay of 20000ms... {",
]);
await create(QwenAgentDriver, "quiet-then-answer");
await instance.adapter.sendTurn({ threadId: "t-qwen-retry", text: "go" });
expect(await recorder.until(e => e.type === "turn.completed")).toMatchObject({ ok: true });
const notices = (recorder.events.filter(e => e.type === "runtime.notice") as Array<{ message: string }>).map(n => n.message);
expect(notices.some(m => /Qwen hit a rate limit \(HTTP 429\) and is retrying \(attempt 2\)\. Next try in 20 s\./.test(m))).toBe(true);
expect(recorder.events.some(e => e.type === "runtime.error")).toBe(false);
});
it("preserves a streamed partial reply before disposal removes its listeners", async () => {
await create(GrokAgentDriver, "stall-after-text");
await instance.adapter.sendTurn({ threadId: "t-dispose-partial", text: "go" });
await recorder.until((event) => event.type === "content.delta" && event.streamKind === "assistant_text");
await instance.dispose();
expect(recorder.events.filter((event) => event.type === "item.completed" && event.itemType === "assistant_text"))
.toEqual([expect.objectContaining({ text: "half an answer, then silence" })]);
expect(recorder.events.filter((event) => event.type === "turn.completed"))
.toEqual([expect.objectContaining({ threadId: "t-dispose-partial", stopReason: "cancelled" })]);
});
it("rejects turns started by completion listeners during disposal", async () => {
await create(GrokAgentDriver, "stall-after-text");
await instance.adapter.sendTurn({ threadId: "t-dispose-listener", text: "go" });
await recorder.until((event) => event.type === "content.delta" && event.streamKind === "assistant_text");
let followup: Promise<unknown> | undefined;
instance.adapter.onEvent((event) => {
if (event.type === "turn.completed" && event.threadId === "t-dispose-listener") {
followup = instance.adapter.sendTurn({ threadId: "t-dispose-followup", text: "next" });
void followup.catch(() => {});
}
});
await instance.dispose();
expect(followup).toBeDefined();
await expect(followup).rejects.toThrow(/shutting down/);
expect(instance.adapter.hasSession("t-dispose-followup")).toBe(false);
await expect(instance.adapter.sendTurn({ threadId: "t-after-dispose", text: "late" })).rejects.toThrow(/shutting down/);
});
it("rejects a turn whose command resolution finishes after disposal", async () => {
let markResolving!: () => void;
const resolving = new Promise<void>((resolve) => { markResolving = resolve; });
let finishCommand!: (command: { command: string }) => void;
const command = new Promise<{ command: string }>((resolve) => { finishCommand = resolve; });
await create(createAcpDriver({
...SELECT_MODEL_SUPPORT,
resolveCommand: () => { markResolving(); return command; },
}));
const sending = instance.adapter.sendTurn({ threadId: "t-dispose-resolving", text: "go" });
void sending.catch(() => {});
await resolving;
await instance.dispose();
finishCommand({ command: FAKE_CLI });
await expect(sending).rejects.toThrow(/shutting down/);
expect(instance.adapter.hasSession("t-dispose-resolving")).toBe(false);
expect(recorder.events).toEqual([]);
});
it("defaults the prompt idle guard above an agent's longest normal silence", () => {
expect(DEFAULT_ACP_PROMPT_IDLE_MS).toBeGreaterThan(600_000);
});
it("an agent that goes silent mid-answer is failed and closed by the prompt idle guard", async () => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "150";
await create(GrokAgentDriver, "stall-after-text");
await instance.adapter.sendTurn({ threadId: "t-stall", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ type: "turn.completed", ok: false, stopReason: "rpc_error" });
const err = recorder.events.find((e) => e.type === "runtime.error");
expect(err?.message).toMatch(/no tool running/i);
// Plain words for the person: no setting names.
expect(err?.message).toMatch(/Send the message again to retry\.$/);
expect(err?.message).not.toContain("OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS");
expect(err).toMatchObject({ stalled: true });
expect(recorder.events.some((e) => e.type === "content.delta")).toBe(true);
expect(instance.adapter.hasSession("t-stall")).toBe(false);
});
it("an end_turn with no reply, image, or tool result becomes runtime.error + failed turn", async () => {
await create(GrokAgentDriver, "empty-reply");
await instance.adapter.sendTurn({ threadId: "t-empty", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false, stopReason: "empty_turn" });
const message = recorder.events.find((e) => e.type === "runtime.error")?.message;
// The person reads the AI's name in plain words, never the internal id.
expect(message).toBe("Grok finished without replying.");
expect(message).not.toContain("grokAgent");
expect(recorder.events.find((e) => e.type === "runtime.error")).toMatchObject({
text: { key: "chat.notice.acpEmpty", params: { provider: "Grok" } },
});
});
// omp 18.12's ACP mode sends a provider's refusal as the reply's text and ends the turn with end_turn.
describe("a reply that is only a provider's error body", () => {
const OAUTH_REFUSED = '403 {"type":"error","error":{"type":"permission_error","message":"OAuth authentication is currently not allowed for this organization.","details":{"error_code":"oauth_not_allowed_for_organization"}},"request_id":"req_011CgY35mSFzqEa1NBU5nq8i"}';
const WEEKLY_LIMIT = '429 {"type":"error","error":{"type":"rate_limit_error","code":"1310","message":"[1310][Weekly/Monthly Limit Exhausted. Your limit will reset at 2026-10-23 09:13:38][20261022010620e41a1d021c2d4ce4]"},"request_id":"20261022010620e41a1d021c2d4ce4"} retry-after-ms=115636000';
const turn = async (threadId: string, reply: string) => {
process.env.FAKE_ACP_TEXT_REPLY = reply;
await create(GrokAgentDriver);
await instance.adapter.sendTurn({ threadId, text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
const replies = recorder.events.filter((e) => e.type === "item.completed" && e.itemType === "assistant_text");
const errors = recorder.events.flatMap((e) => e.type === "runtime.error" ? [e] : []);
return { done, replies, errors };
};
it("fails the turn on a 403 and says the refusal in plain words, never the JSON", async () => {
const { done, replies, errors } = await turn("t-error-403", OAUTH_REFUSED);
expect(done).toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(replies).toEqual([]);
expect(errors).toEqual([expect.objectContaining({
message: 'Grok stopped with an error (HTTP 403; Grok says: "OAuth authentication is currently not allowed for this organization").',
})]);
expect(errors[0]).not.toHaveProperty("limit");
expect(classifyError({ text: errors[0]!.message }).reason).toBe("auth");
});
it("fails the turn on a 429 that names its wait as a usage limit held until then", async () => {
const before = Date.now();
const { done, replies, errors } = await turn("t-error-429", WEEKLY_LIMIT);
const after = Date.now();
expect(done).toMatchObject({ ok: false, stopReason: "quota" });
expect(replies).toEqual([]);
expect(errors).toHaveLength(1);
const error = errors[0]!;
expect(error.limit).toBe("engine");
expect(error.limitResetsAt).toBeGreaterThanOrEqual(before + 115_636_000);
expect(error.limitResetsAt).toBeLessThanOrEqual(after + 115_636_000);
expect(error.text).toMatchObject({ key: "chat.notice.usageLimitResets", params: {
provider: "Grok", resetsAt: error.limitResetsAt, timeZone: expect.any(String),
} });
expect(error.message).toMatch(/^This Grok account has reached its usage limit \(Grok says: "\[1310\]\[Weekly\/Monthly Limit Exhausted"\), which resets .+\. Until then, switch this bot to another AI\.$/);
expect(error.message).not.toMatch(/rate_limit_error|retry-after-ms|request_id/);
expect(classifyError({ text: error.message }).reason).toBe("quota");
});
it("keeps a reply that quotes such an error among its own words", async () => {
const quoted = `The deploy failed with this response:\n\n${OAUTH_REFUSED}\n\nI'll retry with the API key.`;
const { done, replies, errors } = await turn("t-error-quoted", quoted);
expect(done).toMatchObject({ ok: true, stopReason: null });
expect(replies).toEqual([expect.objectContaining({ text: quoted })]);
expect(errors).toEqual([]);
});
});
it("names the AI, never its internal id, when it exits before answering", async () => {
await create(GrokAgentDriver, "exit-early");
await instance.adapter.sendTurn({ threadId: "t-exit-name", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
const errors = recorder.events.filter((e) => e.type === "runtime.error").map((e) => (e as { message: string }).message);
expect(errors.length).toBeGreaterThan(0);
for (const message of errors) expect(message).not.toContain("grokAgent");
});
it("reasoning with no answer is a lost turn, not a success", async () => {
await create(GrokAgentDriver, "reasoning-only");
await instance.adapter.sendTurn({ threadId: "t-reasoning", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false, stopReason: "empty_turn" });
expect(recorder.events.some((e) => e.type === "content.delta" && (e as any).streamKind === "reasoning_text")).toBe(true);
expect(recorder.events.some((e) => e.type === "item.completed")).toBe(false);
expect(recorder.events.find((e) => e.type === "runtime.error")?.message)
.toBe("Grok finished without replying.");
});
it("preserves ACP error codes for provider setup classification", async () => {
await create(ClassifiedErrorDriver, "auth-required");
await instance.adapter.sendTurn({ threadId: "t-auth-required", text: "go" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done, "refused at session/new: the prompt never went out").toMatchObject({ ok: false, stopReason: "auth_required", unsent: true });
expect(recorder.events.find((e) => e.type === "runtime.error")).toMatchObject({ setup: true });
});
it("selectModel confirms the requested model before prompting", async () => {
process.env.FAKE_ACP_MODELS = "m-one,m-two";
await create(SelectModelDriver);
await instance.adapter.sendTurn({ threadId: "t-model", text: "go", model: "m-two" });
const started = await recorder.until((e) => e.type === "session.started");
expect(started).toMatchObject({ model: "m-two" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
});
it("a model the session does not advertise fails the turn instead of running another", async () => {
process.env.FAKE_ACP_MODELS = "m-one,m-two";
await create(SelectModelDriver);
await instance.adapter.sendTurn({ threadId: "t-bad-model", text: "go", model: "m-nope" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false });
const err = recorder.events.find((e) => e.type === "runtime.error")!;
expect(err.message).toMatch(/model not found/);
expect(recorder.events.some((e) => e.type === "content.delta")).toBe(false);
});
// The silent case: the agent acknowledges the switch and keeps its old model, with no error.
it("a model switch acknowledged but not applied fails the turn", async () => {
process.env.FAKE_ACP_MODELS = "m-one,m-two";
process.env.FAKE_ACP_MODEL_STICKS = "1";
await create(SelectModelDriver);
await instance.adapter.sendTurn({ threadId: "t-stuck-model", text: "go", model: "m-two" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: false });
const err = recorder.events.find((e) => e.type === "runtime.error")!;
expect(err.message).toMatch(/did not switch to m-two \(still m-one\)/);
expect(recorder.events.some((e) => e.type === "content.delta")).toBe(false);
});
it("selects the model on a resumed session too, not just a new one", async () => {
process.env.FAKE_ACP_MODELS = "m-one,m-two";
await create(SelectModelDriver);
await instance.adapter.sendTurn({
threadId: "t-resume-model",
text: "go",
model: "m-two",
// Not "fake-acp-session": a load that fell back to session/new would emit the same id.
resumeCursor: "resumed-thread-1",
});
const started = await recorder.until((e) => e.type === "session.started");
expect(started).toMatchObject({ sessionId: "resumed-thread-1", model: "m-two" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
});
it("reuses a resume cursor when session/load returns a session", async () => {
await create(GrokAgentDriver);
await instance.adapter.sendTurn({
threadId: "t-resume-truthy",
text: "go",
resumeCursor: "resumed-cursor-1",
});
const started = await recorder.until((e) => e.type === "session.started");
expect(started).toMatchObject({ sessionId: "resumed-cursor-1" });
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ ok: true });
});
it("fails a resumed turn with no recovery text when session/load returns null", async () => {
process.env.FAKE_ACP_LOAD_NULL = "1";
const rpcFile = join(scratch, "null-load-no-recovery.json");
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
await create(GrokAgentDriver);
await instance.adapter.sendTurn({
threadId: "t-resume-null",
text: "go",
resumeCursor: "gone-cursor",
});
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false });
expect(recorder.events.some((e) => e.type === "session.started")).toBe(false);
const methods = JSON.parse(readFileSync(rpcFile, "utf8"));
expect(methods).toContain("session/load");
expect(methods).not.toContain("session/new");
expect(methods).not.toContain("session/prompt");
});
it.each(["null", "opencode-not-found", "cursor-message"])("rebuilds a missing native session from the canonical recovery text once (%s)", async (failure) => {
if (failure === "null") process.env.FAKE_ACP_LOAD_NULL = "1";
else if (failure === "cursor-message") process.env.FAKE_ACP_LOAD_ERROR = JSON.stringify({ code: -32602, message: "Invalid params: Session \"missing-cursor\" not found" });
else process.env.FAKE_ACP_LOAD_ERROR = JSON.stringify({ code: -32602, message: "Session not found", data: { sessionId: "missing-cursor" } });
const dump = join(scratch, "recovery.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
await create(GrokAgentDriver);
const recoveryText = "User: Remember ALPHA.\nAssistant: Remembered.\nUser: What did I say?";
await instance.adapter.sendTurn({
threadId: "t-resume-history", text: "What did I say?", resumeCursor: "missing-cursor",
recoveryText, system: "Keep current bot instructions.",
});
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: true });
expect(recorder.events.find((e) => e.type === "session.started")).toMatchObject({ rebuilt: true });
const prompt = JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"));
expect(prompt).toEqual([{ type: "text", text: `Keep current bot instructions.\n\n${recoveryText}` }]);
});
it.each([
[-32000, "authentication required", "auth_required"],
[-32602, "Invalid params", "rpc_error"],
])("does not replace native history after a resume refusal (%s)", async (code, message, stopReason) => {
process.env.FAKE_ACP_LOAD_ERROR = JSON.stringify({ code, message });
const rpcFile = join(scratch, "refused-load.json");
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
const countFile = join(scratch, "launches");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
await create(ClassifiedErrorDriver);
const turn = {
threadId: "t-load-refused", text: "Continue", resumeCursor: "saved-session", recoveryText: "Prior messages\nContinue",
};
await instance.adapter.sendTurn(turn);
expect(await recorder.until((e) => e.type === "turn.completed")).toMatchObject({ ok: false, stopReason });
const methods = JSON.parse(readFileSync(rpcFile, "utf8"));
expect(methods).toContain("session/load");
expect(methods).not.toContain("session/new");
expect(methods).not.toContain("session/prompt");
const retry = await instance.adapter.sendTurn(turn);
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === retry.turnId)).toMatchObject({ ok: false, stopReason });
expect(Number(readFileSync(countFile, "utf8"))).toBe(1);
});
it("applyTurnEnv sees the picker model after resolveTurnModel", async () => {
const dump = join(scratch, "turn-env.json");
process.env.FAKE_ACP_DUMP = dump;
const TurnEnvDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT,
driverKind: "turnEnvTest",
selectModel: undefined,
resolveTurnModel: (model) => (model ? `resolved/${model}` : model),
applyTurnEnv: (env, { model, requestedModel }) => {
env.TEST_TURN_MODEL = `${model ?? ""}|${requestedModel ?? ""}`;
},
});
instance = await TurnEnvDriver.create({
instanceId: "turn-env-test",
displayName: undefined,
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({
threadId: "t-turn-env",
text: "go",
model: "ollama::ornith:35b-bf16",
});
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(dump, "utf8")).env.TEST_TURN_MODEL).toBe(
"resolved/ollama::ornith:35b-bf16|ollama::ornith:35b-bf16",
);
});
it("transformEnv sees the instance config", async () => {
const dump = join(scratch, "policy.json");
process.env.FAKE_ACP_DUMP = dump;
instance = await EnvPolicyDriver.create({
instanceId: "policy-test",
displayName: undefined,
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({ threadId: "t-policy", text: "go" });
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(dump, "utf8")).env.TEST_POLICY).toBe("auto");
});
it("per-bot Ask overrides a legacy full-auto instance for the whole turn", async () => {
const dump = join(scratch, "ask-overrides-full-auto.json");
process.env.FAKE_ACP_DUMP = dump;
instance = await EnvPolicyDriver.create({
instanceId: "policy-override-test",
displayName: undefined,
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: true },
});
recorder = recordEvents(instance.adapter);
await instance.adapter.sendTurn({
threadId: "t-policy-override",
text: "go",
approvalMode: "ask",
});
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(dump, "utf8")).env.TEST_POLICY).toBe("ask");
});
it("declares effort levels for Grok only", async () => {
await create(GrokAgentDriver);
expect(instance.adapter.capabilities.effortLevels).toEqual(["low", "medium", "high"]);
await create(GeminiAgentDriver);
expect(instance.adapter.capabilities.effortLevels).toBeUndefined();
await create(KimiAgentDriver);
expect(instance.adapter.capabilities.effortLevels).toBeUndefined();
});
it("passes effort to Grok, and omits the flag when unset", async () => {
const withEffort = join(scratch, "grok-effort.json");
await create(GrokAgentDriver);
process.env.FAKE_ACP_DUMP = withEffort;
await instance.adapter.sendTurn({ threadId: "t-effort", text: "hi", effort: "high" });
await recorder.until((e) => e.type === "turn.completed");
const seen = JSON.parse(readFileSync(withEffort, "utf8"));
expect(seen.argv).toContain("--reasoning-effort");
expect(seen.argv[seen.argv.indexOf("--reasoning-effort") + 1]).toBe("high");
const without = join(scratch, "grok-no-effort.json");
await create(GrokAgentDriver);
process.env.FAKE_ACP_DUMP = without;
await instance.adapter.sendTurn({ threadId: "t-no-effort", text: "hi" });
await recorder.until((e) => e.type === "turn.completed");
expect(JSON.parse(readFileSync(without, "utf8")).argv).not.toContain("--reasoning-effort");
});
it("puts Grok -m after agent so ACP stdio binds the local slug", async () => {
const dump = join(scratch, "grok-argv-order.json");
await create(GrokAgentDriver);
process.env.FAKE_ACP_DUMP = dump;
await instance.adapter.sendTurn({ threadId: "t-argv", text: "hi", model: "grok-4.5", effort: "high" });
await recorder.until((e) => e.type === "turn.completed");
const argv = JSON.parse(readFileSync(dump, "utf8")).argv as string[];
const agent = argv.indexOf("agent");
const modelFlag = argv.indexOf("-m");
const stdio = argv.indexOf("stdio");
expect(agent).toBeGreaterThan(-1);
expect(modelFlag).toBeGreaterThan(agent);
expect(stdio).toBeGreaterThan(modelFlag);
expect(argv[modelFlag + 1]).toBe("grok-4.5");
expect(argv.indexOf("--reasoning-effort")).toBeGreaterThan(agent);
expect(argv.indexOf("--permission-mode")).toBeLessThan(agent);
});
it("asks session/new once more after the engine's own transient refusal, and the turn runs", async () => {
const failureFile = join(scratch, "session-new-once.json"), rpcFile = join(scratch, "session-new-once-rpc.json");
Object.assign(process.env, { FAKE_ACP_RPC_FAILURE_FILE: failureFile, FAKE_ACP_RPC_FAILURE_METHOD: "session/new", FAKE_ACP_RPC_FAILURE_ONCE: "1", FAKE_ACP_RPC_DUMP: rpcFile });
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error", data: { details: "Internal service failure", errorName: "ServiceUnavailableError" } }));
await create();
await instance.adapter.sendTurn({ threadId: "t-session-new-once", text: "hi" });
expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
expect(recorder.events.some((event) => event.type === "runtime.error")).toBe(false);
const methods = JSON.parse(readFileSync(rpcFile, "utf8")) as string[];
expect(methods.filter((method) => method.startsWith("session/new"))).toEqual(["session/new", "session/new.error", "session/new"]);
});
describe("opt-in startup recovery", () => {
const failStartup = async (message = "503 Service Unavailable") => {
const failureFile = join(scratch, "startup-failure.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
process.env.FAKE_ACP_RPC_FAILURE_METHOD = "session/new";
writeFileSync(failureFile, JSON.stringify({ code: -32603, message }));
await create();
};
it.each([
"503 Service Unavailable",
"Internal error: Internal service failure (session/new, ServiceUnavailableError)",
"Internal error: fetchAvailableModels failed: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1082)",
])("offers startup recovery only after its exact child stops: %s", async (message) => {
await failStartup(message);
const kill = vi.spyOn(procs, "killCliTree");
const failed = await instance.adapter.sendTurn({ threadId: "startup", text: "go", startupRecovery: true }).catch((error) => error);
expect(failed).toBeInstanceOf(TurnNotStartedError);
expect(failed.turnId).toBe(recorder.events.find((event) => event.type === "turn.started")?.turnId);
expect(recorder.events.some((event) => event.type === "runtime.error" || event.type === "turn.completed")).toBe(false);
expect(kill).toHaveBeenCalled();
expect(await kill.mock.results[0].value).toBe(true);
const child = kill.mock.calls[0][0];
expect(child.exitCode !== null || child.signalCode !== null).toBe(true);
expect(instance.adapter.hasSession("startup")).toBe(false);
});
it.each([
"503 invalid api key", "503 quota exceeded", "503 unknown model", "503 blocked by our safety systems", "unrecognized failure",
"503 permission denied", "approval required after timeout", "503 policy restriction", "timeout blocked by policy",
])("keeps normal completion for %s", async (message) => {
await failStartup(message);
const ack = await instance.adapter.sendTurn({ threadId: "startup-terminal", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId)).toMatchObject({ ok: false });
expect(recorder.events.some((event) => event.type === "runtime.error")).toBe(true);
});
it("acknowledges before a prompt timeout and never offers startup recovery afterward", async () => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "100";
await create(GrokAgentDriver, "stall-after-text");
const ack = await instance.adapter.sendTurn({ threadId: "startup-prompt", text: "go", startupRecovery: true });
expect(recorder.events.some((event) => event.type === "turn.completed")).toBe(false);
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
});
it("resolves the pending ACK when Stop interrupts initialization", async () => {
await create(GrokAgentDriver, "hang-initialize");
const pending = instance.adapter.sendTurn({ threadId: "startup-stop", text: "go", startupRecovery: true });
let acknowledged = false;
void pending.then(() => { acknowledged = true; });
await recorder.until((event) => event.type === "turn.started");
expect(acknowledged).toBe(false);
await instance.adapter.interruptTurn("startup-stop");
const ack = await pending;
await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId);
expect(instance.adapter.hasSession("startup-stop")).toBe(false);
});
it("honors Stop while confirming cleanup instead of rejecting for recovery", async () => {
await failStartup();
const spawn = procs.spawnCli;
let startupChild: ReturnType<typeof procs.spawnCli>;
vi.spyOn(procs, "spawnCli").mockImplementation((...args) => startupChild = spawn(...args));
let release!: () => void;
let stopping!: () => void;
const gate = new Promise<void>((resolve) => { release = resolve; });
const startedStopping = new Promise<void>((resolve) => { stopping = resolve; });
const kill = procs.killCliTree;
vi.spyOn(procs, "killCliTree").mockImplementation(async (child, timeout) => {
// Delayed cleanup from another test must not release this turn's gate.
if (child === startupChild) { stopping(); await gate; }
return kill(child, timeout);
});
try {
const pending = instance.adapter.sendTurn({ threadId: "startup-cleanup-stop", text: "go", startupRecovery: true });
await startedStopping;
await instance.adapter.interruptTurn("startup-cleanup-stop");
release();
const ack = await pending;
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: true, stopReason: "cancelled" });
expect(recorder.events.some((event) => event.type === "runtime.error")).toBe(false);
} finally { release(); }
});
it("fails normally when process cleanup cannot be confirmed", async () => {
await failStartup();
const kill = procs.killCliTree;
const stop = vi.spyOn(procs, "killCliTree").mockResolvedValue(false);
try {
const ack = await instance.adapter.sendTurn({ threadId: "startup-kill-failed", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId)).toMatchObject({ ok: false });
expect(recorder.events.some((event) => event.type === "runtime.error")).toBe(true);
} finally {
await Promise.all(stop.mock.calls.map(([child]) => kill(child, 0)));
}
});
it.each(["request", "output"])("does not recover after pre-prompt %s activity", async (kind) => {
const spawn = procs.spawnCli;
vi.spyOn(procs, "spawnCli").mockImplementation((...args) => {
const child = spawn(...args);
const write = child.stdin.write.bind(child.stdin);
vi.spyOn(child.stdin, "write").mockImplementation((...writeArgs) => {
const message = JSON.parse(String(writeArgs[0]));
if (message.method === "session/new") {
const activity = kind === "request"
? { jsonrpc: "2.0", id: "before-prompt", method: "fs/read_text_file", params: { path: join(scratch, "absent") } }
: { jsonrpc: "2.0", method: "session/update", params: { update: { sessionUpdate: "tool_call", toolCallId: "before-prompt" } } };
child.stdout.emit("data", Buffer.from(`${JSON.stringify(activity)}\n`));
}
return write(...writeArgs);
});
return child;
});
await failStartup();
const ack = await instance.adapter.sendTurn({ threadId: "startup-activity", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId)).toMatchObject({ ok: false });
});
describe("a prompt that never reached its model", () => {
const UnreachedModelDriver = createAcpDriver({
...SELECT_MODEL_SUPPORT, driverKind: "unreachedModelTest", selectModel: undefined, promptUnreachedModel: openCodePromptUnreachedModel,
});
// opencode 1.19.4 with its OpenAI-compatible baseURL on a closed port
const refused = { code: -32603, message: "Internal error: Cannot connect to API: ECONNREFUSED: Unable to connect. Is the computer able to access the url?", data: { service: "session", errorName: "APIError" } };
const refusePrompt = async (error: object = refused, mode?: string) => {
const failureFile = join(scratch, "prompt-failure.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
process.env.FAKE_ACP_RPC_FAILURE_METHOD = "session/prompt";
process.env.FAKE_ACP_RPC_FAILURE_GATE = join(scratch, "release-prompt-failure");
writeFileSync(failureFile, JSON.stringify(error));
await create(UnreachedModelDriver, mode);
};
const releaseFailure = () => writeFileSync(process.env.FAKE_ACP_RPC_FAILURE_GATE!, "");
const acknowledged = (pending: Promise<unknown>) => {
let settled = false;
void pending.then(() => { settled = true; }, () => { settled = true; });
return () => settled;
};
afterEach(() => {
delete process.env.FAKE_ACP_RPC_FAILURE_GATE; delete process.env.FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT;
delete process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE; delete process.env.FAKE_ACP_UPDATES_FILE; delete process.env.OMB_ACP_QUIET_TICK_MS;
});
it("holds the ACK through the prompt and rejects once its child stops, without terminal events", async () => {
process.env.FAKE_ACP_RPC_DUMP = join(scratch, "rpc.json");
await refusePrompt();
const kill = vi.spyOn(procs, "killCliTree");
const pending = instance.adapter.sendTurn({ threadId: "unreached", text: "go", startupRecovery: true });
const settled = acknowledged(pending);
await vi.waitFor(() => expect(readFileSync(process.env.FAKE_ACP_RPC_DUMP!, "utf8")).toContain("session/prompt"), { timeout: 10_000 });
await new Promise((resolve) => setTimeout(resolve, 50));
expect(settled()).toBe(false);
releaseFailure();
const failed = await pending.catch((error) => error);
expect(failed).toBeInstanceOf(TurnNotStartedError);
expect(failed.message).toContain("ECONNREFUSED");
expect(recorder.events.some((event) => event.type === "runtime.error" || event.type === "turn.completed")).toBe(false);
expect(await kill.mock.results[0].value).toBe(true);
expect(instance.adapter.hasSession("unreached")).toBe(false);
});
it("fails normally when the same refusal follows text and a tool result", async () => {
process.env.FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT = "1";
await refusePrompt();
releaseFailure();
const ack = await instance.adapter.sendTurn({ threadId: "unreached-after-output", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(recorder.events.some((event) => event.type === "runtime.error" && event.message.includes("ECONNREFUSED"))).toBe(true);
});
it("acknowledges at the prompt and fails normally without startup recovery", async () => {
await refusePrompt();
const ack = await instance.adapter.sendTurn({ threadId: "unreached-no-recovery", text: "go" });
expect(recorder.events.some((event) => event.type === "turn.completed")).toBe(false);
releaseFailure();
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
});
it("fails normally when the same refusal follows a plan", async () => {
process.env.FAKE_ACP_RPC_FAILURE_AFTER_USAGE = "1";
process.env.FAKE_ACP_UPDATES_FILE = join(scratch, "plan-updates.json");
writeFileSync(process.env.FAKE_ACP_UPDATES_FILE, JSON.stringify([{ update: { sessionUpdate: "plan", entries: [{ content: "Read the brief", status: "in_progress", priority: "medium" }] } }]));
await refusePrompt();
releaseFailure();
const ack = await instance.adapter.sendTurn({ threadId: "unreached-after-plan", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
});
it("stops the prompt's quiet watch when it hands the turn to recovery", async () => {
process.env.OMB_ACP_QUIET_TICK_MS = "4321";
const started = vi.spyOn(globalThis, "setInterval");
const cleared = vi.spyOn(globalThis, "clearInterval");
await refusePrompt();
const pending = instance.adapter.sendTurn({ threadId: "unreached-quiet", text: "go", startupRecovery: true });
await vi.waitFor(() => expect(started.mock.calls.some(([, ms]) => ms === 4321)).toBe(true));
releaseFailure();
expect(await pending.catch((error) => error)).toBeInstanceOf(TurnNotStartedError);
const timer = started.mock.results[started.mock.calls.findIndex(([, ms]) => ms === 4321)].value;
expect(cleared.mock.calls.some(([handle]) => handle === timer)).toBe(true);
});
describe("a model OpenCode has no route to", () => {
// OpenCode 2.2.5's answer for a model declared under a provider it has no SDK package for.
const noRoute = { code: -32603, message: "Internal error: Unsupported package for chutes/deepseek-ai/DeepSeek-V4.1-Flash-1017: unknown", data: { service: "session", errorName: "provider.no-route" } };
it("hands the turn to the bot's backup, as nothing reached a model", async () => {
await refusePrompt(noRoute);
releaseFailure();
const failed = await instance.adapter.sendTurn({ threadId: "no-route-backup", text: "go", startupRecovery: true }).catch((error) => error);
expect(failed).toBeInstanceOf(TurnNotStartedError);
expect(recorder.events.some((event) => event.type === "runtime.error" || event.type === "turn.completed")).toBe(false);
});
it("without a backup, fails in one plain sentence naming the model", async () => {
await refusePrompt(noRoute);
releaseFailure();
const ack = await instance.adapter.sendTurn({ threadId: "no-route-alone", text: "go" });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(recorder.events.filter((event) => event.type === "runtime.error" && event.turnId === ack.turnId)).toEqual([expect.objectContaining({
message: "Select Model Test can't use the model chutes/deepseek-ai/DeepSeek-V4.1-Flash-1017 here, so choose another model for this bot.",
text: { key: "chat.notice.modelNoRoute", params: { provider: "Select Model Test", model: "chutes/deepseek-ai/DeepSeek-V4.1-Flash-1017" } },
})]);
});
});
describe("an image a text-only model cannot read", () => {
// Z.ai GLM's refusal of a request whose message content holds an image part, reported from a chat where a tool
// had read a picture into the conversation.
const imageOnly = { code: -32603, message: "Internal error: messages.content.type is invalid, allowed values: ['text']", data: { service: "session", errorName: "provider.invalid-request" } };
it("fails in plain words that say why every message in this conversation fails", async () => {
await refusePrompt(imageOnly);
releaseFailure();
const ack = await instance.adapter.sendTurn({ threadId: "text-only-image", text: "go" });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(recorder.events.filter((event) => event.type === "runtime.error" && event.turnId === ack.turnId)).toEqual([expect.objectContaining({
message: "Select Model Test refused this turn as an invalid request because this model reads text only and this conversation carries an image — one attached in chat or read by a tool — so every message fails the same way. Switch this bot to a model that can see images, or start a new conversation.",
text: { key: "chat.notice.imageContent", params: { provider: "Select Model Test" } },
})]);
});
it("rebuilds the conversation on the next message instead of resuming the poisoned session", async () => {
const dump = join(scratch, "poisoned.json");
process.env.FAKE_ACP_DUMP = dump;
process.env.FAKE_ACP_DUMP_PROMPT = "1";
const methods = join(scratch, "poisoned-rpc.json");
process.env.FAKE_ACP_RPC_DUMP = methods;
await refusePrompt(imageOnly);
releaseFailure();
const failed = await instance.adapter.sendTurn({ threadId: "text-only-rebuild", text: "show me the design" });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === failed.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
// A fresh fake answers this turn: the poisoned gate applied only to the first process.
delete process.env.FAKE_ACP_RPC_FAILURE_FILE;
delete process.env.FAKE_ACP_RPC_FAILURE_GATE;
const recoveryText = "User: Show me the design.\nAssistant: I read design.png.\nUser: and now?";
const ack = await instance.adapter.sendTurn({
threadId: "text-only-rebuild", text: "and now?", resumeCursor: "fake-acp-session", recoveryText,
});
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: true });
// The dump file holds the last process's methods: the fresh fake this turn launched never loads the
// poisoned session, and its prompt is the rebuilt conversation plus the text-only note.
const sent = JSON.parse(readFileSync(methods, "utf8"));
expect(sent).not.toContain("session/load");
expect(sent.filter((method: string) => method === "session/new")).toHaveLength(1);
expect(recorder.events.find((event) => event.type === "session.started" && event.turnId === ack.turnId))
.toMatchObject({ rebuilt: true });
const prompt = JSON.parse(readFileSync(`${dump}.prompt.json`, "utf8"));
expect(prompt).toEqual([
{ type: "text", text: recoveryText },
{ type: "text", text: "(Your model for this conversation reads text only. Reading an image file with a file tool, or keeping a picture a tool returned, makes every later reply fail. Say you cannot see it, or attach the file unchanged.)" },
]);
});
});
it("fails normally for another prompt error before any activity", async () => {
await refusePrompt({ code: -32603, message: "Internal error: Invalid API key.", data: { service: "session", errorName: "APIError" } });
releaseFailure();
const ack = await instance.adapter.sendTurn({ threadId: "unreached-other", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId)).toMatchObject({ ok: false });
});
it("never offers a stalled prompt to recovery", async () => {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "100";
await refusePrompt(refused, "hang");
const ack = await instance.adapter.sendTurn({ threadId: "unreached-stall", text: "go", startupRecovery: true });
expect(await recorder.until((event) => event.type === "turn.completed" && event.turnId === ack.turnId))
.toMatchObject({ ok: false, stopReason: "rpc_error" });
});
});
});
describe("an engine's account and HTTP failures", () => {
/** The provider's refusal as the CLI answers session/prompt: JSON-RPC's internal error around its own line. */
const refuse = (details: string) => {
const failureFile = join(scratch, "prompt-failure.json");
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error", data: { details, service: "session", errorName: "APIError" } }));
};
const failed = async (threadId: string) => {
const ack = await instance.adapter.sendTurn({ threadId, text: "Run the morning report" });
const completed = await recorder.until((e) => e.type === "turn.completed" && e.turnId === ack.turnId);
const errors = recorder.events.filter((e) => e.type === "runtime.error" && e.turnId === ack.turnId);
return { completed, errors };
};
it("names a Grok account out of credits, with Grok's own reason, as the account failure it is", async () => {
// what Cloud routine runs recorded, as far as the aggregate kept it
refuse("API error (status 402 Payment Required): Grok Build us…");
await create(GrokAgentDriver);
const { completed, errors } = await failed("t-grok-402");
expect(errors).toEqual([expect.objectContaining({
message: 'Your Grok account is out of credits (Grok says: "Grok Build us…"). Add credits with Grok, then run it again.',
setup: true,
})]);
expect(completed).toMatchObject({ ok: false, stopReason: "auth_required" });
expect(completed, "refused after the prompt went out").not.toHaveProperty("unsent");
});
it("names any other ACP engine out of credits the same way", async () => {
refuse("API error (status 402 Payment Required): Your plan has no credits left. Ask an admin to add more.");
await create(GeminiAgentDriver);
const { errors } = await failed("t-gemini-402");
expect(errors).toEqual([expect.objectContaining({
message: 'Your Gemini account is out of credits (Gemini says: "Your plan has no credits left"). Add credits with Gemini, then run it again.',
})]);
});
it("puts another HTTP failure in plain words that keep its class, and leaves an unknown one as it was", async () => {
refuse("API error (status 503 Service Unavailable): upstream connect error");
await create(GrokAgentDriver);
const outage = await failed("t-grok-503");
expect(outage.errors).toEqual([expect.objectContaining({
message: 'Grok had a temporary server problem (HTTP 503; Grok says: "upstream connect error"). Try again in a few minutes.',
})]);
expect(outage.errors[0]).not.toHaveProperty("setup");
expect(outage.completed).toMatchObject({ ok: false, stopReason: "rpc_error" });
refuse("Grok could not finish the session");
await create(GrokAgentDriver);
const unknown = await failed("t-grok-unknown");
expect(unknown.errors).toEqual([expect.objectContaining({
message: "Internal error: Grok could not finish the session (session/prompt, service: session, APIError)",
})]);
});
});
describe("ACP session pool (persistent child)", () => {
let countFile: string;
let rpcFile: string;
const launches = () => Number(readFileSync(countFile, "utf8"));
const rpc = () => JSON.parse(readFileSync(rpcFile, "utf8")) as string[];
it("recovers on the next explicit turn after session establishment fails", async () => {
countFile = join(scratch, "launches");
const appendFile = join(scratch, "rpc-all.jsonl");
const failureFile = join(scratch, "failure.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_APPEND_FILE = appendFile;
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
process.env.FAKE_ACP_RPC_FAILURE_METHOD = "session/new";
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error", data: {
service: "directory", details: "OpenCode service failure", errorName: "Error",
} }));
await create();
const threadId = "t-new-rpc-recovery";
const first = await instance.adapter.sendTurn({ threadId, text: "Hello" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId)).toMatchObject({ ok: false, stopReason: "rpc_error" });
const calls = () => readFileSync(appendFile, "utf8").trim().split("\n").map((line) => JSON.parse(line));
expect(launches()).toBe(1);
expect(calls().filter((entry) => entry.method === "session/prompt")).toHaveLength(0);
expect(recorder.events.some((e) => e.type === "session.started")).toBe(false);
unlinkSync(failureFile);
const next = await instance.adapter.sendTurn({ threadId, text: "Hello again" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === next.turnId)).toMatchObject({ ok: true });
expect(launches()).toBe(2);
expect(calls().filter((entry) => entry.method === "session/prompt")).toHaveLength(1);
expect(recorder.events.filter((e) => e.type === "turn.completed" && e.turnId === first.turnId)).toHaveLength(1);
});
it.each([false, true])("evicts internal-error processes without replaying a failed prompt (output: %s)", async (afterOutput) => {
countFile = join(scratch, "launches");
const appendFile = join(scratch, "rpc-all.jsonl");
const failureFile = join(scratch, "failure.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_APPEND_FILE = appendFile;
process.env.FAKE_ACP_RPC_FAILURE_FILE = failureFile;
if (afterOutput) process.env.FAKE_ACP_RPC_FAILURE_AFTER_OUTPUT = "1";
await create();
const threadId = "t-rpc-recovery";
const first = await instance.adapter.sendTurn({ threadId, text: "Remember ALPHA" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId)).toMatchObject({ ok: true });
writeFileSync(failureFile, JSON.stringify({ code: -32603, message: "Internal error", data: {
details: "OpenCode service failure; api_key=fixture-secret",
service: "session", errorName: "APIError",
responseBody: "PRIVATE RESPONSE BODY MUST NOT APPEAR",
} }));
const failed = await instance.adapter.sendTurn({ threadId, text: "Do work", resumeCursor: "fake-acp-session" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === failed.turnId)).toMatchObject({ ok: false, stopReason: "rpc_error" });
expect(launches()).toBe(1);
expect(instance.adapter.hasSession(threadId)).toBe(false);
const error = recorder.events.find((e) => e.type === "runtime.error" && e.turnId === failed.turnId);
const message = error?.type === "runtime.error" ? error.message : "";
expect(message).toContain("Internal error: OpenCode service failure");
expect(message).toContain("session/prompt, service: session, APIError");
expect(message).not.toContain("fixture-secret");
expect(message).not.toContain("PRIVATE RESPONSE");
expect(recorder.events.filter((e) => e.type === "item.completed" && e.itemType === "tool" && e.turnId === failed.turnId)).toHaveLength(afterOutput ? 1 : 0);
unlinkSync(failureFile);
const next = await instance.adapter.sendTurn({ threadId, text: "Continue after the interruption", resumeCursor: "fake-acp-session" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === next.turnId)).toMatchObject({ ok: true });
expect(launches()).toBe(2);
const calls = readFileSync(appendFile, "utf8").trim().split("\n").map((line) => JSON.parse(line));
expect(calls.filter((entry) => entry.method === "session/prompt")).toHaveLength(3);
expect(calls.filter((entry) => entry.method === "session/load")).toHaveLength(1);
expect(recorder.events.filter((e) => e.type === "turn.completed" && e.turnId === failed.turnId)).toHaveLength(1);
});
it("honors explicit session reset even when a healthy child and cursor are retained", async () => {
countFile = join(scratch, "launches");
rpcFile = join(scratch, "rpc.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
await create();
const first = await instance.adapter.sendTurn({ threadId: "t-reset", text: "one" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
const second = await instance.adapter.sendTurn({ threadId: "t-reset", text: "rebuilt conversation", sessionReset: true, resumeCursor: "fake-acp-session" });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId)).toMatchObject({ ok: true });
expect(launches()).toBe(2);
expect(rpc()).toContain("session/new");
expect(rpc()).not.toContain("session/load");
});
it("reuses one agent process across turns and skips the handshake", async () => {
countFile = join(scratch, "launches");
rpcFile = join(scratch, "rpc.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
await create();
const first = await instance.adapter.sendTurn({ threadId: "t-pool-reuse", text: "one" });
const firstDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
expect(firstDone).toMatchObject({ ok: true });
const second = await instance.adapter.sendTurn({
threadId: "t-pool-reuse",
text: "two",
resumeCursor: "fake-acp-session",
});
const secondDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(secondDone).toMatchObject({ ok: true });
expect(launches()).toBe(1);
expect(rpc().filter((m) => m === "initialize")).toHaveLength(1);
expect(rpc().filter((m) => m === "session/new")).toHaveLength(1);
expect(rpc().filter((m) => m === "session/load")).toHaveLength(0);
expect(rpc().filter((m) => m === "session/prompt")).toHaveLength(2);
expect(recorder.events.filter((e) => e.type === "session.started")).toMatchObject([
{ sessionId: "fake-acp-session" },
{ sessionId: "fake-acp-session" },
]);
});
it("closes the idle process and resumes on the next turn", async () => {
// Ten seconds is the floor; give the poll room past it.
process.env.OMB_ACP_SESSION_IDLE_MIN_MS = "10000";
process.env.OMB_ACP_SESSION_IDLE_MS = "10000";
countFile = join(scratch, "launches");
rpcFile = join(scratch, "rpc.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
await create();
const first = await instance.adapter.sendTurn({ threadId: "t-pool-idle", text: "one" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
// The close reason is only logged, so poll the native log instead of sleeping past the deadline.
await new Promise<void>((resolve, reject) => {
const deadline = Date.now() + 20_000;
const log = join(NATIVE_DIR, "t-pool-idle.ndjson");
const check = () => {
if (Date.now() > deadline) return reject(new Error("idle close was never logged"));
try {
if (readFileSync(log, "utf8").includes('"close":"idle"')) return resolve();
} catch (error) {
// appendNative() suppresses append errors, so the file may not exist yet
if ((error as NodeJS.ErrnoException).code !== "ENOENT") return reject(error);
}
setTimeout(check, 25);
};
check();
});
expect(launches()).toBe(1);
const second = await instance.adapter.sendTurn({
threadId: "t-pool-idle",
text: "two",
resumeCursor: "fake-acp-session",
});
const done = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(done).toMatchObject({ ok: true });
expect(launches()).toBe(2);
// the dump is per-process and overwritten on spawn, so this is the resumed child
expect(rpc()).toContain("session/load");
expect(rpc()).toContain("initialize");
}, 30_000);
it("respawns when the spawn contract changes", async () => {
countFile = join(scratch, "launches");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
const dirA = mkdtempSync(join(scratch, "a-"));
const dirB = mkdtempSync(join(scratch, "b-"));
await create();
const first = await instance.adapter.sendTurn({ threadId: "t-pool-contract", text: "one", cwd: dirA });
const firstDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
expect(firstDone).toMatchObject({ ok: true });
const second = await instance.adapter.sendTurn({
threadId: "t-pool-contract",
text: "two",
cwd: dirB,
resumeCursor: "fake-acp-session",
});
const secondDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(secondDone).toMatchObject({ ok: true });
expect(launches()).toBe(2);
});
it.each([
{ driver: GrokAgentDriver, expectedLaunches: 1 },
{ driver: QwenAgentDriver, expectedLaunches: 2 },
])("rotating credentials refreshes $driver.driverKind with $expectedLaunches process(es)", async ({ driver, expectedLaunches }) => {
countFile = join(scratch, "launches");
const appendFile = join(scratch, "rpc-all.jsonl");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_APPEND_FILE = appendFile;
instance = await driver.create({
instanceId: "acp-test", displayName: "ACP Test", enabled: true,
environment: { HOME: scratch, USERPROFILE: scratch },
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
const integration = (token: string) => ({
command: process.execPath,
args: [FAKE_CLI],
env: { OMB_COMMS_TOKEN: token },
});
const first = await instance.adapter.sendTurn({
threadId: "t-pool-token",
text: "one",
integrations: { agents: integration("token-one") },
});
const firstDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
expect(firstDone).toMatchObject({ ok: true });
const second = await instance.adapter.sendTurn({
threadId: "t-pool-token",
text: "two",
resumeCursor: "fake-acp-session",
integrations: { agents: integration("token-two") },
});
const secondDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(secondDone).toMatchObject({ ok: true });
const third = await instance.adapter.sendTurn({
threadId: "t-pool-token", text: "unchanged MCP inputs",
resumeCursor: "fake-acp-session",
integrations: { agents: integration("token-two") },
});
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === third.turnId)).toMatchObject({ ok: true });
expect(launches()).toBe(expectedLaunches);
const calls = readFileSync(appendFile, "utf8").trim().split("\n").map((line) => JSON.parse(line).method);
expect(calls.filter((m) => m === "initialize")).toHaveLength(expectedLaunches);
expect(calls.filter((m) => m === "session/new")).toHaveLength(1);
expect(calls.filter((m) => m === "session/load")).toHaveLength(1);
expect(calls.filter((m) => m === "session/prompt")).toHaveLength(3);
expect(recorder.events.filter((e) => e.type === "session.started").map((e) => e.sessionId)).toEqual([
"fake-acp-session", "fake-acp-session", "fake-acp-session",
]);
});
it("does not load or prompt Qwen after Stop during old-process cleanup", async () => {
const appendFile = join(scratch, "rpc-all.jsonl");
process.env.FAKE_ACP_RPC_APPEND_FILE = appendFile;
instance = await QwenAgentDriver.create({
instanceId: "qwen-stop", displayName: "Qwen Stop", enabled: true,
environment: { HOME: scratch, USERPROFILE: scratch },
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
const integration = (token: string) => ({ command: process.execPath, args: [FAKE_CLI], env: { OMB_COMMS_TOKEN: token } });
const first = await instance.adapter.sendTurn({ threadId: "qwen-stop", text: "one", integrations: { agents: integration("one") } });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
let stopped!: () => void;
let release!: () => void;
const stopping = new Promise<void>((resolve) => { stopped = resolve; });
const gate = new Promise<void>((resolve) => { release = resolve; });
const kill = procs.killCliTree;
vi.spyOn(procs, "killCliTree").mockImplementationOnce(async (child) => {
stopped();
await gate;
return kill(child);
});
try {
const second = await instance.adapter.sendTurn({
threadId: "qwen-stop", text: "two", resumeCursor: "fake-acp-session",
integrations: { agents: integration("two") },
});
await stopping;
await instance.adapter.interruptTurn("qwen-stop");
release();
// The person's Stop ends the turn as a stop, never as an error.
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId)).toMatchObject({ ok: true, stopReason: "cancelled" });
expect(recorder.events.filter((e) => e.type === "runtime.error" && e.turnId === second.turnId)).toEqual([]);
const calls = readFileSync(appendFile, "utf8").trim().split("\n").map((line) => JSON.parse(line).method);
expect(calls.filter((method) => method === "initialize")).toHaveLength(1);
expect(calls.filter((method) => method === "session/load")).toHaveLength(0);
expect(calls.filter((method) => method === "session/prompt")).toHaveLength(1);
expect(instance.adapter.hasSession("qwen-stop")).toBe(false);
} finally { release(); }
});
it.each(["Stop", "prompt stall", "failed cleanup"])("waits for Qwen cleanup before an immediate retry after %s", async (reason) => {
const children: ReturnType<typeof procs.spawnCli>[] = [];
const spawn = procs.spawnCli;
vi.spyOn(procs, "spawnCli").mockImplementation((...args) => {
const child = spawn(...args);
vi.spyOn(child.stdin, "write");
children.push(child);
return child;
});
const calls = () => children.flatMap((child) => vi.mocked(child.stdin.write).mock.calls
.map(([chunk]) => JSON.parse(String(chunk)) as { method: string; params: { sessionId?: string } }));
let release!: () => void;
let stopping!: () => void;
const gate = new Promise<void>((resolve) => { release = resolve; });
const startedStopping = new Promise<void>((resolve) => { stopping = resolve; });
const kill = procs.killCliTree;
let rejectCleanup = reason === "failed cleanup";
vi.spyOn(procs, "killCliTree").mockImplementation(async (child, timeout) => {
// Hold only the original writer: Stop must still close its uninitialized replacement.
if (child === children[0]) {
stopping();
await gate;
if (rejectCleanup) return false;
}
return kill(child, timeout);
});
if (reason === "prompt stall") {
process.env.OPENMAUS_ACP_PROMPT_IDLE_TIMEOUT_MS = "100";
process.env.FAKE_ACP_MODE = "stall-after-text";
}
instance = await QwenAgentDriver.create({
instanceId: "qwen-retry", displayName: "Qwen Retry", enabled: true,
environment: { HOME: scratch, USERPROFILE: scratch },
config: { cli: FAKE_CLI, fullAuto: false },
});
recorder = recordEvents(instance.adapter);
const integration = (token: string) => ({ agents: { command: process.execPath, args: [FAKE_CLI], env: { OMB_COMMS_TOKEN: token } } });
try {
const first = await instance.adapter.sendTurn({ threadId: "qwen-retry", text: "one", integrations: integration("one") });
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId))
.toMatchObject({ ok: reason !== "prompt stall" });
if (reason !== "prompt stall") {
const second = await instance.adapter.sendTurn({
threadId: "qwen-retry", text: "two", resumeCursor: "fake-acp-session", integrations: integration("two"),
});
await startedStopping;
if (reason === "Stop") await instance.adapter.interruptTurn("qwen-retry");
else release();
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId))
.toMatchObject(reason === "Stop" ? { ok: true, stopReason: "cancelled" } : { ok: false });
if (rejectCleanup) {
expect(recorder.events.find((e) => e.type === "runtime.error" && e.turnId === second.turnId))
.toMatchObject({ message: expect.stringMatching(/could not close its previous tool session/) });
rejectCleanup = false;
}
}
process.env.FAKE_ACP_MODE = "happy";
const retry = await instance.adapter.sendTurn({
threadId: "qwen-retry", text: "retry", resumeCursor: "fake-acp-session", integrations: integration("three"),
});
// Observe writes directly: the retry must not even initialize while cleanup is unconfirmed.
expect(calls().filter((call) => call.method === "initialize")).toHaveLength(1);
expect(calls().filter((call) => call.method === "session/load")).toHaveLength(0);
release();
expect(await recorder.until((e) => e.type === "turn.completed" && e.turnId === retry.turnId)).toMatchObject({ ok: true });
expect(calls().filter((call) => call.method === "session/new")).toHaveLength(1);
expect(calls().filter((call) => call.method === "session/load")).toEqual([
expect.objectContaining({ params: expect.objectContaining({ sessionId: "fake-acp-session" }) }),
]);
expect(calls().filter((call) => call.method === "session/prompt")).toHaveLength(2);
} finally {
release();
await instance.dispose();
await Promise.all(children.map((child) => kill(child, 0)));
}
});
it("an agent that refuses to re-load its live session gets one fresh process, then resumes", async () => {
countFile = join(scratch, "launches");
const appendFile = join(scratch, "rpc-all.jsonl");
const rejectFile = join(scratch, "reject-live-load");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
// The per-process dump holds only the replacement's calls; the append log keeps both, pid-tagged.
process.env.FAKE_ACP_RPC_APPEND_FILE = appendFile;
process.env.FAKE_ACP_REJECT_LIVE_LOAD_FILE = rejectFile;
await create();
const rpcAll = () =>
readFileSync(appendFile, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line) as { pid: number; method: string });
const integration = (token: string) => ({
command: process.execPath,
args: [FAKE_CLI],
env: { OMB_COMMS_TOKEN: token },
});
const first = await instance.adapter.sendTurn({
threadId: "t-pool-reject",
text: "one",
integrations: { agents: integration("token-one") },
});
const firstDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
expect(firstDone).toMatchObject({ ok: true });
writeFileSync(rejectFile, "1");
const second = await instance.adapter.sendTurn({
threadId: "t-pool-reject",
text: "two",
resumeCursor: "fake-acp-session",
integrations: { agents: integration("token-two") },
});
const secondDone = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(secondDone).toMatchObject({ ok: true });
expect(launches()).toBe(2);
const calls = rpcAll();
expect(calls.filter((c) => c.method === "initialize")).toHaveLength(2);
expect(calls.filter((c) => c.method === "session/new")).toHaveLength(1);
expect(calls.filter((c) => c.method === "session/load")).toHaveLength(2);
expect(calls.filter((c) => c.method === "session/prompt")).toHaveLength(2);
expect(new Set(calls.map((c) => c.pid)).size).toBe(2);
});
it("stopAll closes the pooled process; the next turn resumes", async () => {
countFile = join(scratch, "launches");
rpcFile = join(scratch, "rpc.json");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
process.env.FAKE_ACP_RPC_DUMP = rpcFile;
await create();
const first = await instance.adapter.sendTurn({ threadId: "t-pool-stop", text: "one" });
await recorder.until((e) => e.type === "turn.completed" && e.turnId === first.turnId);
await instance.adapter.stopAll();
const second = await instance.adapter.sendTurn({
threadId: "t-pool-stop",
text: "two",
resumeCursor: "fake-acp-session",
});
const done = await recorder.until((e) => e.type === "turn.completed" && e.turnId === second.turnId);
expect(done).toMatchObject({ ok: true });
expect(launches()).toBe(2);
expect(rpc()).toContain("session/load");
});
it("an interrupt that the agent honors keeps the process pooled", async () => {
countFile = join(scratch, "launches");
process.env.FAKE_ACP_LAUNCH_COUNT_FILE = countFile;
await create(GrokAgentDriver, "hang");
await instance.adapter.sendTurn({ threadId: "t-pool-interrupt", text: "go" });
await recorder.until((e) => e.type === "session.started");
await instance.adapter.interruptTurn("t-pool-interrupt");
const done = await recorder.until((e) => e.type === "turn.completed");
expect(done).toMatchObject({ stopReason: "cancelled" });
// hang never resolves prompts, so the pooled process shows in the launch count, not another prompt.
expect(launches()).toBe(1);
});
});
});
describe("ACP snapshot", () => {
it("a missing binary is unavailable", async () => {
const instance = await GrokAgentDriver.create({
instanceId: "grok-missing",
displayName: undefined,
environment: {},
enabled: true,
config: { cli: "definitely-not-a-real-grok-binary", fullAuto: false },
});
const snap = await instance.snapshot();
expect(snap.state).toBe("unavailable");
await instance.dispose();
});
it("kimi checks KIMI_CODE_HOME before the child HOME", async () => {
const scratch = mkdtempSync(join(tmpdir(), "omb-kimi-auth-"));
const kimiHome = join(scratch, "custom-kimi-home");
const childHome = join(scratch, "child-home");
mkdirSync(join(childHome, ".kimi-code", "credentials"), { recursive: true });
writeFileSync(join(childHome, ".kimi-code", "credentials", "kimi-code.json"), "{}");
const instance = await KimiAgentDriver.create({
instanceId: "kimi-custom-home",
displayName: undefined,
environment: { KIMI_CODE_HOME: kimiHome, HOME: childHome },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
try {
expect((await instance.snapshot()).authenticated).toBe(false);
mkdirSync(join(kimiHome, "credentials"), { recursive: true });
writeFileSync(join(kimiHome, "credentials", "kimi-code.json"), "{}");
expect((await instance.snapshot()).authenticated).toBe(true);
} finally {
await instance.dispose();
await removeTempDir(scratch);
}
});
it("droid resolves the signed-in CLI before falling back to FACTORY_API_KEY", async () => {
const scratch = mkdtempSync(join(tmpdir(), "omb-droid-auth-"));
const overrideHome = join(scratch, "custom-home");
const childHome = join(scratch, "child-home");
mkdirSync(join(childHome, ".factory"), { recursive: true });
writeFileSync(join(childHome, ".factory", "auth.v2.file"), "{}");
// The child env inherits process.env, so a real exported FACTORY_API_KEY would satisfy every case;
// Windows resolves the home from USERPROFILE first, so both are pinned to the scratch home.
const make = (environment: Record<string, string>) =>
DroidAgentDriver.create({
instanceId: "droid-auth",
displayName: undefined,
environment: { FACTORY_API_KEY: "", ...(environment.HOME ? { USERPROFILE: environment.HOME } : {}), ...environment },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
const instances: ProviderInstance[] = [];
try {
const overridden = await make({ FACTORY_HOME_OVERRIDE: overrideHome, HOME: childHome });
instances.push(overridden);
expect((await overridden.snapshot()).authenticated).toBe(false);
mkdirSync(join(overrideHome, ".factory"), { recursive: true });
writeFileSync(join(overrideHome, ".factory", "auth.v2.file"), "{}");
expect((await overridden.snapshot()).authenticated).toBe(true);
const loggedOutWithKey = await make({
FACTORY_HOME_OVERRIDE: join(scratch, "empty-home"),
HOME: childHome,
FACTORY_API_KEY: "fk-test",
});
instances.push(loggedOutWithKey);
expect((await loggedOutWithKey.snapshot()).authenticated).toBe(true);
const fromHome = await make({ HOME: childHome });
instances.push(fromHome);
expect((await fromHome.snapshot()).authenticated).toBe(true);
// secure_auth_storage writes the keychain/keyring variant, so a fresh macOS login has only this one.
const keychainHome = join(scratch, "keychain-home");
mkdirSync(join(keychainHome, ".factory"), { recursive: true });
writeFileSync(join(keychainHome, ".factory", "auth.v2.loginkeychain"), "{}");
const fromKeychain = await make({ HOME: keychainHome });
instances.push(fromKeychain);
expect((await fromKeychain.snapshot()).authenticated).toBe(true);
const neither = await make({ HOME: join(scratch, "empty") });
instances.push(neither);
expect((await neither.snapshot()).authenticated).toBe(false);
} finally {
for (const i of instances) await i.dispose();
await removeTempDir(scratch);
}
});
it("droid reads custom models, favourites order, and the configured default", async () => {
const scratch = mkdtempSync(join(tmpdir(), "omb-droid-models-"));
mkdirSync(join(scratch, ".factory"), { recursive: true });
writeFileSync(
join(scratch, ".factory", "settings.json"),
JSON.stringify({
customModels: [
{ id: "custom:LMStudio-Qwen-0", displayName: "Qwen (local)" },
{ id: "custom:Azure-Opus-0", displayName: "Azure Opus" },
],
modelFavorites: ["custom:Azure-Opus-0", "custom:LMStudio-Qwen-0"],
sessionDefaultSettings: { model: "custom:LMStudio-Qwen-0" },
}),
);
const instance = await DroidAgentDriver.create({
instanceId: "droid-models",
displayName: undefined,
environment: { HOME: scratch, USERPROFILE: scratch },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
try {
expect(instance.models.options.slice(0, 2)).toEqual([
{ id: "custom:Azure-Opus-0", label: "Azure Opus", custom: true },
{ id: "custom:LMStudio-Qwen-0", label: "Qwen (local)", custom: true },
]);
expect(instance.models.options.some((o) => o.id === "claude-opus-5")).toBe(true);
expect(instance.models.default).toBe("custom:LMStudio-Qwen-0");
} finally {
await instance.dispose();
await removeTempDir(scratch);
}
});
it("droid falls back to the built-in catalog when settings.json is unreadable", async () => {
const scratch = mkdtempSync(join(tmpdir(), "omb-droid-nosettings-"));
mkdirSync(join(scratch, ".factory"), { recursive: true });
writeFileSync(join(scratch, ".factory", "settings.json"), "{ not json");
const instance = await DroidAgentDriver.create({
instanceId: "droid-models-fallback",
displayName: undefined,
environment: { HOME: scratch },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
try {
expect(instance.models.default).toBe("claude-opus-5");
expect(instance.models.options.every((o) => !o.id.startsWith("custom:"))).toBe(true);
} finally {
await instance.dispose();
await removeTempDir(scratch);
}
});
it("kimi resolves default credentials from the child HOME", async () => {
const scratch = mkdtempSync(join(tmpdir(), "omb-kimi-home-"));
const credentialDir = join(scratch, ".kimi-code", "credentials");
mkdirSync(credentialDir, { recursive: true });
writeFileSync(join(credentialDir, "kimi-code.json"), "{}");
const instance = await KimiAgentDriver.create({
instanceId: "kimi-child-home",
displayName: undefined,
environment: { HOME: scratch },
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
try {
expect((await instance.snapshot()).authenticated).toBe(true);
} finally {
await instance.dispose();
await removeTempDir(scratch);
}
});
it("awaits an async isAuthenticated", async () => {
const instance = await AsyncAuthDriver.create({
instanceId: "async-auth",
displayName: undefined,
environment: {},
enabled: true,
config: { cli: FAKE_CLI, fullAuto: false },
});
try {
// without the await this is a Promise: truthy, but not `true`
expect((await instance.snapshot()).authenticated).toBe(true);
} finally {
await instance.dispose();
}
});
});