import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { Effect } from "effect"; import { steerNoteFor } from "../src/backend/agent/queue-controls"; import type { AgentClient } from "../src/backend/agent-client"; import { runCauseEffect } from "../src/backend/effect-boundary"; import { type AppServerNotification, getRecord, getString, isRecord, type ThreadItem } from "../src/backend/protocol"; import { requireProviderDriver } from "../src/backend/provider-drivers"; import { installFakeAgent, readJsonl, startThread, stopClients } from "./provider-fakes/host"; const OUT = resolve(import.meta.dirname, "../.openbot-build/pi-steer-e2e"); const TURN_TIMEOUT_MS = 20_000; interface LogEntry { event: string; text?: string; streamingBehavior?: string; images?: number; steering?: string; } interface Report { passed: boolean; steered?: { turnId: string; reply: string; status: string | null; steer: LogEntry }; noted?: { sentToPi: string; savedUserMessages: string[]; providersWithOwnNote: string[] }; refused?: { refused: string; completedBeforeNextSteer: boolean; status: string | null; reply: string }; timedOut?: { refused: string; queueCleared: boolean; status: string | null; reply: string }; wrongTurn?: { refused: string; steers: number }; afterTurn?: { refused: string; steers: number; prompts: number }; failure?: string; } const root = await mkdtemp(join(tmpdir(), "openbot-pi-steer-e2e-")); const report: Report = { passed: false }; const clients: AgentClient[] = []; const saved = new Map(); async function open(): Promise<{ client: AgentClient; log: string }> { const executable = join(root, "bin", "pi"); const log = join(root, "agent.jsonl"); await installFakeAgent(executable, "pi-steer", { FAKE_LOG: log, FAKE_DIR: root }); const client = requireProviderDriver("deepseek").createClient( { executable, version: "1.4.0", source: "managed" }, 2_000, { apiKey: () => null, customProviders: () => [], mcpServers: () => [], history: () => ({ append: (_threadId, fragment) => Effect.sync(() => void saved.set(fragment.turnId, fragment.items)), }), }, ); clients.push(client); client.start(); return { client, log }; } const count = async (log: string, event: string) => (await readJsonl(log)).filter((entry) => entry.event === event).length; async function waitForCount(log: string, event: string, total: number): Promise { const deadline = Date.now() + 10_000; while ((await count(log, event)) < total) { if (Date.now() > deadline) throw new Error(`The fake Pi did not log ${event}.`); await new Promise((settle) => setTimeout(settle, 25)); } } interface RunningTurn { id: string; completed: () => boolean; done: Promise<{ reply: string; status: string | null; error: string | null }>; } async function startWork(client: AgentClient, thread: string, log: string): Promise { let reply = ""; let error: string | null = null; let completed = false; let listener: ((notification: AppServerNotification) => void) | undefined; const done = new Promise<{ reply: string; status: string | null; error: string | null }>((finish, fail) => { const timer = setTimeout(() => fail(new Error("The turn did not complete.")), TURN_TIMEOUT_MS); listener = (notification) => { if (getString(notification.params, "threadId") !== thread) return; if (notification.method === "item/completed" && isRecord(notification.params)) reply += getString(notification.params.item, "text") ?? ""; if (notification.method === "error") error = getString(getRecord(notification.params, "error"), "message") ?? "unknown"; if (notification.method === "turn/completed") { completed = true; clearTimeout(timer); if (listener) client.off("notification", listener); finish({ reply, status: getString(getRecord(notification.params, "turn"), "status"), error }); } }; client.on("notification", listener); }); done.catch(() => undefined); const prompts = await count(log, "prompt"); const response = await runCauseEffect( client.request( "turn/start", { threadId: thread, input: [{ type: "text", text: "work", text_elements: [] }] }, (value) => value, ), ); const id = getString(getRecord(response, "turn"), "id"); if (!id) throw new Error("turn/start answers no turn id."); await waitForCount(log, "prompt", prompts + 1); return { id, completed: () => completed, done }; } function steer( client: AgentClient, thread: string, turnId: string, text: string, note: string | null = null, ): Promise { return runCauseEffect( client.request( "turn/steer", { threadId: thread, expectedTurnId: turnId, clientUserMessageId: "delivery-1", input: [...(note ? [{ type: "text", text: note }] : []), { type: "text", text, text_elements: [] }], }, (value) => getString(value, "turnId"), ), ); } async function refusal(operation: Promise): Promise { const outcome = await operation.then( () => null, (error: unknown) => (error instanceof Error ? error.message : String(error)), ); assert.ok(outcome !== null, "The steer must be refused."); return outcome; } try { const { client, log } = await open(); const thread = await startThread(client, { cwd: root }); const turn = await startWork(client, thread, log); const answer = await steer(client, thread, turn.id, "use the other branch"); assert.equal(answer, turn.id); const finished = await turn.done; const entries = await readJsonl(log); const sent = entries.find((entry) => entry.event === "steer"); assert.ok(sent, "The steer must send Pi's steer command."); assert.equal(sent.text, "use the other branch"); assert.equal(finished.status, "completed"); assert.equal(finished.error, null); assert.match(finished.reply, /steered: use the other branch/u); assert.equal(await count(log, "prompt"), 1, "The steer must not send a prompt."); report.steered = { turnId: turn.id, reply: finished.reply, status: finished.status, steer: sent }; const note = steerNoteFor("deepseek", { kind: "user" }, "keep the tests"); assert.ok(note, "A provider with no steer note of its own must get OpenBot's."); assert.ok( steerNoteFor("pi", { kind: "agent", agentId: "teammate" }, "keep the tests"), "A teammate's steer must get a note.", ); assert.equal(steerNoteFor("pi", { kind: "user" }, "/skill:review"), null, "A slash command must get no note."); const providersWithOwnNote = (["claude", "grok"] as const).filter( (provider) => steerNoteFor(provider, { kind: "user" }, "keep the tests") === null, ); assert.deepEqual(providersWithOwnNote, ["claude", "grok"]); const noted = await startWork(client, thread, log); assert.equal(await steer(client, thread, noted.id, "keep the tests", note), noted.id); const notedFinished = await noted.done; assert.equal(notedFinished.status, "completed"); const sentToPi = (await readJsonl(log)).filter((entry) => entry.event === "steer").at(-1)?.text ?? ""; assert.equal(sentToPi, `${note}\nkeep the tests`); assert.match(sentToPi, /does not cancel it/u); const savedUserMessages = (saved.get(noted.id) ?? []) .filter((item) => item.type === "userMessage") .map((item) => (item.content ?? []).map((part) => part.text ?? "").join("\n")); assert.deepEqual(savedUserMessages, ["work", "keep the tests"]); report.noted = { sentToPi, savedUserMessages, providersWithOwnNote }; const second = await startWork(client, thread, log); const refused = await refusal(steer(client, thread, second.id, "refuse this")); assert.match(refused, /Pi refused the steer/u); const completedBeforeNextSteer = second.completed(); assert.equal(completedBeforeNextSteer, false, "A refused steer must not end the running turn."); assert.equal(await steer(client, thread, second.id, "go on"), second.id); const secondFinished = await second.done; assert.equal(secondFinished.status, "completed"); assert.equal(secondFinished.error, null); assert.match(secondFinished.reply, /steered: go on/u); report.refused = { refused, completedBeforeNextSteer, status: secondFinished.status, reply: secondFinished.reply, }; const slow = await startWork(client, thread, log); const cleared = await count(log, "clear_queue"); assert.equal(cleared, 0, "A refused steer queues nothing, so it must not clear Pi's queue."); assert.equal(await steer(client, thread, slow.id, "hold this"), slow.id); const steersBefore = await count(log, "steer"); const timedOut = await refusal(steer(client, thread, slow.id, "slow steer")); await waitForCount(log, "clear_queue", cleared + 1); await waitForCount(log, "steer", steersBefore + 2); const entries2 = await readJsonl(log); assert.equal(entries2.find((entry) => entry.event === "clear_queue")?.steering, "hold this, slow steer"); assert.equal(entries2.filter((entry) => entry.event === "steer").at(-1)?.text, "hold this"); assert.equal(slow.completed(), false, "A timed-out steer must not end the running turn."); assert.equal(await steer(client, thread, slow.id, "after the timeout"), slow.id); const slowFinished = await slow.done; assert.equal(slowFinished.status, "completed"); assert.match(slowFinished.reply, /steered: hold this, after the timeout/u); report.timedOut = { refused: timedOut, queueCleared: true, status: slowFinished.status, reply: slowFinished.reply }; const third = await startWork(client, thread, log); const before = await count(log, "steer"); const wrongTurn = await refusal(steer(client, thread, "another-turn", "late")); assert.equal(await count(log, "steer"), before); report.wrongTurn = { refused: wrongTurn, steers: (await count(log, "steer")) - before }; await steer(client, thread, third.id, "end"); await third.done; const steers = await count(log, "steer"); const prompts = await count(log, "prompt"); const afterTurn = await refusal(steer(client, thread, third.id, "too late")); assert.equal(await count(log, "steer"), steers); assert.equal(await count(log, "prompt"), prompts); report.afterTurn = { refused: afterTurn, steers: (await count(log, "steer")) - steers, prompts: (await count(log, "prompt")) - prompts, }; report.passed = true; } catch (error) { report.failure = error instanceof Error ? (error.stack ?? error.message) : String(error); process.exitCode = 1; } finally { await stopClients(clients); await mkdir(OUT, { recursive: true }); await writeFile(join(OUT, "report.json"), `${JSON.stringify(report, null, 2)}\n`); await rm(root, { recursive: true, force: true }); process.stdout.write(`${JSON.stringify(report, null, 2)}\n`); }