mirror of
https://github.com/nightly-labs/openbot.git
synced 2026-11-24 10:13:55 +08:00
* Show OpenCode background turns and text before a tool call OpenCode 2.2.0 sends no session update for the turn that a finished background subagent starts. The answer was only in its store, and the chat did not show it (#3406). After a tool call leaves a background task running, the ACP client now watches `session/list`. When the session changes, it sends `session/load` and shows the new entries in a turn with `unprompted: true`. A message sent during that turn waits in the queue. Text that an ACP agent writes before a tool call is now a `narration` item. The chat shows it as a message, with the turn's steps between its messages. Narration is not the answer of the turn: it does not count as unread, set the preview, or show as a channel reply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix the review findings of the background replay and the steps rows - Show the steps of a running turn above each answer that it wrote. The turn's trace and facts go on its last answer that has steps. - A stop of a turn of the agent's own marks its open tool calls as stopped. For 30 seconds after any turn that did not complete, a replay does not show new entries. - The watch goes on while a turn of the agent's own is open. - The background `session/list` and `session/load` have the request time limit, so a prompt cannot wait for them with no end. - A prompt counts as opening before its session opens again. - Only OpenCode starts a new message at a new `messageId`. - The dynamic island does not show narration as a reply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep a failed background replay for the next poll - A failed or timed-out background `session/load` does not mark the session change as handled, so the next poll tries it again. - After a timeout, the agent goes on with its replay. The session's updates stay apart, a prompt waits, and no second replay starts until the load answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Release a background replay only after its entries apply A load that answered in time released the waiting prompt before the replay started the agent's turn. The prompt could then start its own turn first, and one of the two turns did not complete. Now such a load releases the prompt after its entries apply. Only a load that the watch stopped waiting for releases when it answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep text before a tool call in the thinking The developer does not want the agent's notes between tool calls as chat messages. Text before a tool call is commentary again, and only the last text of a turn is the answer. This removes the `narration` item type and the steps rows between answers. For OpenCode, the text of an earlier ACP message in a turn also ends as commentary, so the text of two steps does not show as one line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the text of two OpenCode steps in one answer The text of an earlier ACP message with no tool call after it went into the thinking, so an answer that the agent wrote in two steps lost its first part. Now the text of a new `messageId` starts a new paragraph of the same answer. An empty chunk does not start one. The background replay compares only the current message's text when that text grows. The E2E case is now `steps` and covers two steps with no tool call between them. The docs describe this under its own heading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Wait for a background replay before the session opens again A prompt that had to open its session again did that before it waited for a background replay. Opening the session replaces the thread, so a turn that the replay started on the old thread never completed. The prompt now waits for the replay first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
232 lines
9.8 KiB
TypeScript
232 lines
9.8 KiB
TypeScript
// Runs turns on the real OpenCode driver against a fake `opencode acp` that answers a finished
|
|
// background subagent in a turn it sends no update for, as OpenCode 2.2.0 does (#3406). Checks that
|
|
// the background turn shows as a turn of the agent's own, from `session/load`, with all its items;
|
|
// that text before a tool call stays thinking there too; that a message sent meanwhile waits for it;
|
|
// and that a stopped background turn does not come back.
|
|
// `bun run test:opencode-background-turn`. Writes .openbot-build/opencode-background-turn-e2e/report.json.
|
|
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 { type AgentClient, ProviderTurnActiveError } from "../src/backend/agent-client";
|
|
import type { AgentCliInfo } from "../src/backend/cli";
|
|
import { runCauseEffect } from "../src/backend/effect-boundary";
|
|
import { type AppServerNotification, getRecord, getString, isRecord } from "../src/backend/protocol";
|
|
import { NO_PROVIDER_CREDENTIALS, requireProviderDriver } from "../src/backend/provider-drivers";
|
|
import { installFakeAgent, readJsonl, runTurn, startThread, stopClients } from "./provider-fakes/host";
|
|
|
|
const OUT = resolve(import.meta.dirname, "../.openbot-build/opencode-background-turn-e2e");
|
|
/** The background turn ends when its session stays unchanged for 15 s; this leaves room for that. */
|
|
const WAIT_MS = 60_000;
|
|
|
|
interface Event {
|
|
method: string;
|
|
turnId: string | null;
|
|
unprompted: boolean;
|
|
status: string | null;
|
|
item: {
|
|
id: string | null;
|
|
type: string | null;
|
|
phase: string | null;
|
|
text: string | null;
|
|
status: string | null;
|
|
} | null;
|
|
}
|
|
|
|
interface Report {
|
|
passed: boolean;
|
|
events: Record<string, Event[]>;
|
|
}
|
|
|
|
const root = await mkdtemp(join(tmpdir(), "openbot-opencode-background-turn-e2e-"));
|
|
const release = join(root, "release");
|
|
const log = join(root, "fake.jsonl");
|
|
const report: Report = { passed: false, events: {} };
|
|
const clients: AgentClient[] = [];
|
|
|
|
/** The notifications of one thread, and a wait for one that matches. */
|
|
function record(client: AgentClient, thread: string) {
|
|
const events: Event[] = [];
|
|
report.events[thread] = events;
|
|
const waiters = new Set<() => void>();
|
|
client.on("notification", (notification: AppServerNotification) => {
|
|
const params = notification.params;
|
|
if (getString(params, "threadId") !== thread) return;
|
|
const turn = getRecord(params, "turn");
|
|
const item = isRecord(params) && isRecord(params.item) ? params.item : null;
|
|
events.push({
|
|
method: notification.method,
|
|
turnId: getString(params, "turnId") ?? getString(turn, "id"),
|
|
unprompted: isRecord(params) && params.unprompted === true,
|
|
status: getString(turn, "status"),
|
|
item: item && {
|
|
id: getString(item, "id"),
|
|
type: getString(item, "type"),
|
|
phase: getString(item, "phase"),
|
|
text: getString(item, "text"),
|
|
status: getString(item, "status"),
|
|
},
|
|
});
|
|
for (const waiter of waiters) waiter();
|
|
});
|
|
const until = (label: string, find: (event: Event) => boolean): Promise<Event> =>
|
|
new Promise((found, fail) => {
|
|
const check = () => {
|
|
const event = events.find(find);
|
|
if (!event) return;
|
|
waiters.delete(check);
|
|
clearTimeout(timer);
|
|
found(event);
|
|
};
|
|
const timer = setTimeout(() => {
|
|
waiters.delete(check);
|
|
fail(new Error(`Timed out waiting for ${label}.`));
|
|
}, WAIT_MS);
|
|
waiters.add(check);
|
|
check();
|
|
});
|
|
/** The completed agent messages and tool calls of one turn, in order. */
|
|
const items = (turnId: string) =>
|
|
events
|
|
.filter((event) => event.method === "item/completed" && event.turnId === turnId && event.item)
|
|
.map((event) => event.item);
|
|
return { events, until, items };
|
|
}
|
|
|
|
/** The turn that `runTurn` started: the last `turn/started` before its completion. */
|
|
function lastTurnId(events: readonly Event[]): string {
|
|
const started = events.filter((event) => event.method === "turn/started").at(-1);
|
|
assert.ok(started?.turnId, "A turn started.");
|
|
return started.turnId;
|
|
}
|
|
|
|
function causes(error: unknown): unknown[] {
|
|
const chain: unknown[] = [];
|
|
for (
|
|
let current = error;
|
|
current !== undefined && chain.length < 8;
|
|
current = isRecord(current) ? current.cause : undefined
|
|
)
|
|
chain.push(current);
|
|
return chain;
|
|
}
|
|
|
|
/** Waits until the fake has loaded the session again after the background turn's end. */
|
|
async function loadedAfterEnd(session: string): Promise<void> {
|
|
for (;;) {
|
|
const entries = await readJsonl<{ event: string; session: string }>(log);
|
|
const ended = entries.findIndex((entry) => entry.event === "background-ended" && entry.session === session);
|
|
if (ended >= 0 && entries.slice(ended).some((entry) => entry.event === "load" && entry.session === session)) return;
|
|
await new Promise((next) => setTimeout(next, 200));
|
|
}
|
|
}
|
|
|
|
try {
|
|
const workspace = join(root, "workspace");
|
|
await mkdir(workspace);
|
|
await mkdir(release);
|
|
const executable = join(root, "opencode", "bin", "opencode");
|
|
await installFakeAgent(executable, "opencode-background-turn", { FAKE_RELEASE: release, FAKE_LOG: log });
|
|
const cli: AgentCliInfo = { executable, version: "2.2.0", source: "managed" };
|
|
const client = requireProviderDriver("opencode").createClient(cli, 10_000, NO_PROVIDER_CREDENTIALS);
|
|
clients.push(client);
|
|
client.start();
|
|
|
|
// Text before a tool call is thinking. The text of the last steps is one answer, in paragraphs.
|
|
const first = await startThread(client, { cwd: workspace });
|
|
const one = record(client, first);
|
|
assert.equal((await runTurn(client, first, { text: "steps" })).status, "completed");
|
|
const stepped = one.items(lastTurnId(one.events));
|
|
assert.deepEqual(
|
|
stepped.filter((item) => item?.type === "agentMessage").map((item) => [item?.phase, item?.text]),
|
|
[
|
|
["commentary", "I will check the file."],
|
|
["final_answer", "The file is fine.\n\nNothing to change."],
|
|
],
|
|
"The text before the tool call is thinking, and the answer keeps the text of both last steps.",
|
|
);
|
|
|
|
// A background subagent's turn shows from `session/load`, as a turn of the agent's own.
|
|
assert.equal((await runTurn(client, first, { text: "background" })).status, "completed");
|
|
const own = await one.until("the background turn", (event) => event.method === "turn/started" && event.unprompted);
|
|
assert.ok(own.turnId);
|
|
await one.until(
|
|
"the background turn's open tool call",
|
|
(event) => event.turnId === own.turnId && event.method === "item/started" && event.item?.id === "tool_bg_check",
|
|
);
|
|
// A message sent meanwhile does not join the agent's turn: the drain queues it again.
|
|
const refused = await runTurn(client, first, { text: "ping", timeoutMs: WAIT_MS }).then(
|
|
() => null,
|
|
(error: unknown) => error,
|
|
);
|
|
assert.ok(
|
|
causes(refused).some((cause) => cause instanceof ProviderTurnActiveError),
|
|
`A prompt during the agent's own turn fails with ProviderTurnActiveError: ${String(refused)}`,
|
|
);
|
|
await writeFile(join(release, first), "");
|
|
const ended = await one.until(
|
|
"the background turn's end",
|
|
(event) => event.method === "turn/completed" && event.turnId === own.turnId,
|
|
);
|
|
assert.equal(ended.status, "completed");
|
|
assert.deepEqual(
|
|
one.items(own.turnId).map((item) => [item?.type, item?.phase ?? item?.status, item?.text ?? item?.id]),
|
|
[
|
|
["agentMessage", "commentary", "The subagent is done."],
|
|
["agentMessage", "commentary", "Checking its file."],
|
|
["toolCall", "completed", "tool_bg_check"],
|
|
["agentMessage", "final_answer", "The background task finished: DONE."],
|
|
],
|
|
"The background turn has its thought, its note, its tool call and its answer, each once.",
|
|
);
|
|
const after = await runTurn(client, first, { text: "ping" });
|
|
assert.equal(after.status, "completed");
|
|
assert.equal(after.reply, "pong");
|
|
assert.equal(
|
|
one.events.filter((event) => event.method === "turn/started" && event.unprompted).length,
|
|
1,
|
|
"The replay after the prompt finds nothing new.",
|
|
);
|
|
|
|
// A stopped background turn does not come back when the agent adds to it.
|
|
const second = await startThread(client, { cwd: workspace });
|
|
const two = record(client, second);
|
|
assert.equal((await runTurn(client, second, { text: "background" })).status, "completed");
|
|
const stopped = await two.until(
|
|
"the second background turn",
|
|
(event) => event.method === "turn/started" && event.unprompted,
|
|
);
|
|
await two.until(
|
|
"the second background turn's open tool call",
|
|
(event) => event.turnId === stopped.turnId && event.method === "item/started" && event.item?.id === "tool_bg_check",
|
|
);
|
|
await runCauseEffect(
|
|
client.request("turn/interrupt", { threadId: second, turnId: stopped.turnId }, (value) => value),
|
|
);
|
|
const interrupted = await two.until(
|
|
"the stopped background turn's end",
|
|
(event) => event.method === "turn/completed" && event.turnId === stopped.turnId,
|
|
);
|
|
assert.equal(interrupted.status, "interrupted");
|
|
await writeFile(join(release, second), "");
|
|
await loadedAfterEnd(second);
|
|
const pong = await runTurn(client, second, { text: "ping" });
|
|
assert.equal(pong.reply, "pong");
|
|
assert.equal(
|
|
two.events.filter((event) => event.method === "turn/started" && event.unprompted).length,
|
|
1,
|
|
"The rest of the stopped turn does not start another turn.",
|
|
);
|
|
assert.ok(
|
|
!two.events.some((event) => event.item?.text === "The background task finished: DONE."),
|
|
"The rest of the stopped turn is not shown.",
|
|
);
|
|
report.passed = true;
|
|
} 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(`PASS opencode-background-turn-e2e: ${join(OUT, "report.json")}\n`);
|