Skip to content

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

acp: open the wordless window only on emitted tool ends; move config decode out

Manuel3e8b9a7daf48
diff --git a/server/drivers/acp/acp.test.ts b/server/drivers/acp/acp.test.ts
index 31657c7493..05f21c782c 100644
--- a/server/drivers/acp/acp.test.ts
+++ b/server/drivers/acp/acp.test.ts
@@ -945,6 +945,27 @@ describe("ACP turns (fake CLI)", () => {
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");
diff --git a/server/drivers/acp/config-decode.ts b/server/drivers/acp/config-decode.ts
new file mode 100644
index 0000000000..0b17c88dd2
--- /dev/null
+++ b/server/drivers/acp/config-decode.ts
@@ -0,0 +1,13 @@
+// The ACP driver's config decode, moved out of core.ts (file-size ratchet).
+import type { AcpConfig } from "./core.ts";
+
+export function decodeAcpConfig(defaultCli: string) {
+ return (raw: unknown): AcpConfig => {
+ const o = (raw ?? {}) as Record<string, unknown>;
+ return {
+ cli: typeof o.cli === "string" ? o.cli : defaultCli,
+ fullAuto: o.fullAuto === true,
+ workspace: typeof o.workspace === "string" ? o.workspace : undefined,
+ };
+ };
+}
diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.ts
index 38fa26499b..98782a475c 100644
--- a/server/drivers/acp/core.ts
+++ b/server/drivers/acp/core.ts
@@ -67,7 +67,8 @@ import { commandSummary, toolDetailPreview } from "../../tool-summary.ts";
import { acpToolEndEvents } from "./tool-items.ts";
import { AcpHost } from "./host.ts"; import { SignInRequests, type AcpDriverHooks } from "./driver-hooks.ts";
import { exitBeforeResult } from "./exit-report.ts"; import { readNodeVersion } from "./node-version.ts";
-import { endTurn, trackRunningTool, type AcpAskFinish, type AcpSession, type AcpTurn } from "./turn-state.ts";
+import { decodeAcpConfig } from "./config-decode.ts";
+import { endTurn, trackAssistantImage, trackAssistantWords, trackThoughtAfterTool, trackRunningTool, trackToolEndShown, type AcpAskFinish, type AcpSession, type AcpTurn } from "./turn-state.ts";
import { recoveryPromptFor } from "../../resume-recovery.ts";
import { noticeFailure } from "../../chat-notices.ts";
import { armIdleClose, engineIdleMs, sessionIdlePolicy, stopIdleClose } from "../session-idle.ts";
@@ -302,17 +303,6 @@ async function readAcpImageBlocks(images: readonly TurnImageInput[]) {
})));
}
-function decodeAcpConfig(defaultCli: string) {
- return (raw: unknown): AcpConfig => {
- const o = (raw ?? {}) as Record<string, unknown>;
- return {
- cli: typeof o.cli === "string" ? o.cli : defaultCli,
- fullAuto: o.fullAuto === true,
- workspace: typeof o.workspace === "string" ? o.workspace : undefined,
- };
- };
-}
-
export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig> {
if (support.sessionScopedApproval && !support.configureSession) {
throw new Error(`${support.displayName}: sessionScopedApproval needs configureSession to apply each turn's mode`);
@@ -821,10 +811,11 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
if (!u || (u.sessionUpdate === "agent_message_chunk" && typeof u.content?.text === "string" && current.steering?.swallow(u.content.text))) return;
const finished = u.status === "completed" || u.status === "failed";
const toolEnded = (itemId: string | undefined) => {
- current.state.producedItem = true;
- current.state.toolsSinceShown = true;
- current.state.thoughtSinceTool = false;
- for (const event of acpToolEndEvents(u, itemId, current.endedToolIds)) emit({ ...base(threadId, current.turnId), ...event });
+ // a repeat update names an already-ended tool and emits nothing; the window only opens on a real end
+ for (const event of acpToolEndEvents(u, itemId, current.endedToolIds)) {
+ trackToolEndShown(current.state);
+ emit({ ...base(threadId, current.turnId), ...event });
+ }
};
switch (u.sessionUpdate) {
case "agent_message_chunk": {
@@ -832,9 +823,7 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
const delta = content?.text;
if (content?.type === "image" && typeof content.data === "string" && content.data) {
current.flushAssistantText();
- current.state.producedItem = true;
- current.state.toolsSinceShown = false;
- current.state.thoughtSinceTool = false;
+ trackAssistantImage(current.state);
emit({
...base(threadId, current.turnId),
type: "item.completed",
@@ -859,12 +848,8 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
}
case "agent_thought_chunk": {
const delta = u.content?.text;
- if (typeof delta === "string" && delta) {
- // Reasoning in the wordless window after a tool is the model at work again; if no reply follows it,
- // the turn ends midstream and the person sees nothing.
- if (current.state.toolsSinceShown) current.state.thoughtSinceTool = true;
- emit({ ...base(threadId, current.turnId), type: "content.delta", streamKind: "reasoning_text", delta });
- }
+ if (typeof delta === "string" && delta) trackThoughtAfterTool(current.state);
+ if (typeof delta === "string" && delta) emit({ ...base(threadId, current.turnId), type: "content.delta", streamKind: "reasoning_text", delta });
break;
}
case "tool_call": {
@@ -1117,10 +1102,7 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
state.text = "";
const text = stripLeakedSystemHarnessMessages(raw);
if (!text.trim()) return;
- state.producedItem = true;
- state.repliedText = true;
- state.toolsSinceShown = false;
- state.thoughtSinceTool = false;
+ trackAssistantWords(state);
emit({ ...base(threadId, turnId), type: "item.completed", itemType: "assistant_text", text });
};
const current: AcpTurn = {
diff --git a/server/drivers/acp/turn-state.ts b/server/drivers/acp/turn-state.ts
index 67d4387b2d..fd8e06394d 100644
--- a/server/drivers/acp/turn-state.ts
+++ b/server/drivers/acp/turn-state.ts
@@ -81,6 +81,33 @@ export function trackRunningTool(current: AcpTurn, update: { toolCallId?: unknow
else if (status === "pending" || status === "in_progress") current.runningTools.add(update.toolCallId);
}
+/** Words the person can see: the turn has replied, and the wordless window after a tool is closed. */
+export function trackAssistantWords(state: AcpTurn["state"]): void {
+ state.producedItem = true;
+ state.repliedText = true;
+ state.toolsSinceShown = false;
+ state.thoughtSinceTool = false;
+}
+
+/** An assistant image is itself a reply the person sees, so it closes the wordless window after a tool. */
+export function trackAssistantImage(state: AcpTurn["state"]): void {
+ state.producedItem = true;
+ state.toolsSinceShown = false;
+ state.thoughtSinceTool = false;
+}
+
+/** A tool end that is actually emitted opens the wordless window; a repeat that names an already-ended tool emits nothing. */
+export function trackToolEndShown(state: AcpTurn["state"]): void {
+ state.producedItem = true;
+ state.toolsSinceShown = true;
+ state.thoughtSinceTool = false;
+}
+
+/** Reasoning in the wordless window: if no reply lands after it, the turn ends midstream (#6684). */
+export function trackThoughtAfterTool(state: AcpTurn["state"]): void {
+ if (state.toolsSinceShown) state.thoughtSinceTool = true;
+}
+
export interface AcpTurnEnd {
ok: boolean;
stopReason: string | null;