Files
openbot/scripts/opencode-thought-interleave-e2e.ts
Aleksander DygońandClaude Opus 5.5 53af9d8e5c Move leading tagged reasoning of OpenCode replies into the thinking steps (#17409)
* Move leading tagged reasoning of OpenCode replies into the thinking steps

Some models that OpenCode runs through a proxy or custom provider send
their reasoning inside the answer text, between <think> or <thinking>
tags. OpenBot showed the tags and the reasoning in the reply.

The ACP client now holds the start of each OpenCode answer message. When
the message starts with a closed <think> or <thinking> block, the block
goes into the collapsed thinking steps and the reply shows only the text
after it. A tag split between chunks is handled. A block that never
closes is shown as the answer, at the end of the message or turn, before
the empty-answer check. The background replay compare and the history
replay keep the same split. Other providers do not change.

Council vote: option A (OpenCode only, start of each message), scores
8, 7 and 8. The stray "****" marks and the repeated progress notes in
the report are not fixed by this change.

Fixes #16943

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Start a new reasoning check after each released OpenCode message

The leading-thought splitter changed to pass-through mode when the app
released it at the end of a message, also when it held no text. After a
tool step with no answer text before it, or after a stream error, the
message was empty, so the app did not start a new splitter. A <think>
block in the next answer then showed in the reply.

Now each release starts a new splitter. A stream error also drops the
held part of an unfinished reasoning block, so its raw tags do not show
as a step. The E2E script covers a tool step before the answer and a
stream error inside a reasoning block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the growth of a background OpenCode answer while its reasoning is held

In a background turn, OpenCode text comes only from session loads. When
a load ended inside a leading <think> block, the held text did not set
the message ID of the answer. The next load then had no message to
compare, so the rest of the answer was lost, and the turn ended with
the raw "<think>" text as the reply.

Now, while the splitter holds text, the background compare uses the
message ID of the splitter and adds the answer text of that message
only when it is the same message. The background-turn E2E script has a
tagged answer that grows across loads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drop a held reasoning block when OpenCode retries an attempt

The attempt retry from main (#19704) did not start a new leading-thought
splitter. When a failed attempt left an unclosed <think> block, the
next attempt released it into the reply, with its tags.

Now the retry drops the held part of the failed attempt, as the stream
error path does. The E2E script has a retried attempt with a cut
reasoning block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Attach a steer to the reply whose reasoning block is held

While an OpenCode message held its leading <think> block, the turn
still named the earlier message as the open reply. A steer in that time
marked the earlier message as the steered reply. The held message,
written before the steer, then showed below the steer, and the answer
to the steer joined it.

Now the open reply is the message of the splitter while it holds text.
The OpenCode steer E2E has a reply with an open thinking block at the
steer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:03:25 +01:00

128 lines
5.3 KiB
TypeScript

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 } from "../src/backend/agent-client";
import type { AgentCliInfo } from "../src/backend/cli";
import { getString, isRecord } from "../src/backend/protocol";
import { NO_PROVIDER_CREDENTIALS, requireProviderDriver } from "../src/backend/provider-drivers";
import { installFakeAgent, runTurn, startThread, stopClients } from "./provider-fakes/host";
const OUT = resolve(import.meta.dirname, "../.openbot-build/opencode-thought-interleave-e2e");
interface CompletedItem {
phase: string | null;
text: string | null;
}
interface Report {
passed: boolean;
turns: Record<string, CompletedItem[]>;
}
const root = await mkdtemp(join(tmpdir(), "openbot-opencode-thought-interleave-e2e-"));
const report: Report = { passed: false, turns: {} };
const clients: AgentClient[] = [];
async function turnItems(client: AgentClient, thread: string, text: string): Promise<CompletedItem[]> {
const items: CompletedItem[] = [];
const listener = (notification: { method: string; params?: unknown }) => {
if (notification.method !== "item/completed" || !isRecord(notification.params)) return;
if (getString(notification.params, "threadId") !== thread || !isRecord(notification.params.item)) return;
items.push({
phase: getString(notification.params.item, "phase"),
text: getString(notification.params.item, "text"),
});
};
client.on("notification", listener);
try {
const turn = await runTurn(client, thread, { text });
assert.equal(turn.error, null, `${text}: the turn failed`);
} finally {
client.off("notification", listener);
}
return items;
}
try {
const workspace = join(root, "workspace");
await mkdir(workspace);
const executable = join(root, "opencode", "bin", "opencode");
await installFakeAgent(executable, "opencode-thought-interleave");
const cli: AgentCliInfo = { executable, version: "2.1.4", source: "managed" };
const client = requireProviderDriver("opencode").createClient(cli, 10_000, NO_PROVIDER_CREDENTIALS);
clients.push(client);
client.start();
const thread = await startThread(client, { cwd: workspace });
const interleaved = await turnItems(client, thread, "interleave");
report.turns.interleave = interleaved;
assert.deepEqual(
interleaved.filter((item) => item.phase === "final_answer").map((item) => item.text),
["Baca gambar. Gambar: tabel Rencana (prototype)."],
"The answer is one item with whole words.",
);
assert.ok(
interleaved.filter((item) => item.phase === "commentary").every((item) => /^(?:hmm)+$/u.test(item.text ?? "")),
"Only the thoughts are commentary.",
);
const harness = await turnItems(client, thread, "harness");
report.turns.harness = harness;
assert.deepEqual(
harness.filter((item) => item.phase === "final_answer").map((item) => item.text),
["Full answer."],
"The retried answer replaces the partial answer.",
);
const answers = async (prompt: string) => {
const items = await turnItems(client, thread, prompt);
report.turns[prompt] = items;
return {
answers: items.filter((item) => item.phase === "final_answer").map((item) => item.text),
thoughts: items.filter((item) => item.phase === "commentary").map((item) => item.text),
};
};
assert.deepEqual(
await answers("tagged"),
{ answers: ["Clean answer."], thoughts: ["Plan the reply."] },
"A leading thinking block split across chunks moves into the thoughts.",
);
assert.deepEqual(
await answers("messages"),
{ answers: ["First part.\n\nSecond part."], thoughts: ["First plan.", "Second plan."] },
"Each message moves its own leading thinking block.",
);
assert.deepEqual(
await answers("unclosed"),
{ answers: ["<think>Never closed."], thoughts: [] },
"An unclosed thinking block stays in the answer.",
);
assert.deepEqual(
await answers("mention"),
{ answers: ["Use the <think> tag like this. <thinker> stays."], thoughts: [] },
"A tag inside the answer stays in the answer.",
);
const toolFirst = await answers("tool first");
assert.deepEqual(toolFirst.answers, ["Answer after the tool."], "A thinking block after a tool step moves too.");
assert.ok(toolFirst.thoughts.includes("The file is read."), "The thinking after a tool step is a thought.");
const retried = await answers("retried");
assert.deepEqual(retried.answers, ["Full answer."], "A retried attempt drops the cut thinking block.");
assert.ok(
retried.thoughts.every((text) => !text?.includes("<think>") && !text?.includes("Partial")),
"The cut thinking block of a retried attempt shows nowhere.",
);
const broken = await answers("broken");
assert.deepEqual(broken.answers, ["Full answer."], "The retried answer has no reasoning in it.");
assert.ok(
broken.thoughts.every((text) => !text?.includes("<think>")),
"A thinking block that a stream error cut off shows no tags.",
);
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-thought-interleave-e2e: ${join(OUT, "report.json")}\n`);