Skip to content

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

acp: notice a turn that goes wordless after its tools

Manuel6ac013e314d7
diff --git a/server/chat-notices.ts b/server/chat-notices.ts
index f21d8a3c85..eee7dd0ce4 100644
--- a/server/chat-notices.ts
+++ b/server/chat-notices.ts
@@ -88,6 +88,7 @@ export const CHAT_NOTICE = {
"chat.notice.phone.write.PHONE_SEND_MESSAGE": "Send message on {phone}",
"chat.notice.phone.write.PHONE_SEND_WHATSAPP": "Send whatsapp on {phone}",
"chat.notice.acpEmpty": "{provider} finished without replying.",
+ "chat.notice.acpEndedAfterTools": "{provider} thought right past the last tool, then finished the turn without a last reply. Ask it to continue or summarize what it did.",
"chat.notice.acpNoReason": "{provider} stopped without saying why.",
"chat.notice.acpNodeTooOld": "{provider} needs Node.js {needed} or newer, but this computer has Node.js {found}. Update Node.js from nodejs.org, then send your message again.",
"chat.notice.acpMcpAppsFailed": "Connected apps couldn't be set up, so {provider} couldn't start this reply. Try again, or switch this bot to another AI.",
diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.ts
index 5e35ad53b7..38fa26499b 100644
--- a/server/drivers/acp/core.ts
+++ b/server/drivers/acp/core.ts
@@ -822,6 +822,8 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
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 });
};
switch (u.sessionUpdate) {
@@ -831,6 +833,8 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
if (content?.type === "image" && typeof content.data === "string" && content.data) {
current.flushAssistantText();
current.state.producedItem = true;
+ current.state.toolsSinceShown = false;
+ current.state.thoughtSinceTool = false;
emit({
...base(threadId, current.turnId),
type: "item.completed",
@@ -855,7 +859,12 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
}
case "agent_thought_chunk": {
const delta = u.content?.text;
- if (typeof delta === "string" && delta) emit({ ...base(threadId, current.turnId), type: "content.delta", streamKind: "reasoning_text", delta });
+ 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 });
+ }
break;
}
case "tool_call": {
@@ -1081,7 +1090,7 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
): Promise<any> =>
signIn.refused(session.host) ?? session.acp.request(method, params, timeoutMs, receive, idleMs, idleMessage);
- const state: AcpTurn["state"] = { settled: false, promptSent: false, text: "", textMessageId: null, producedItem: false, repliedText: false, startupActivity: false, stopped: false, usagePeak: null, usageLast: null, usageCompacted: false, cancelling: false };
+ const state: AcpTurn["state"] = { settled: false, promptSent: false, text: "", textMessageId: null, producedItem: false, repliedText: false, startupActivity: false, stopped: false, toolsSinceShown: false, thoughtSinceTool: false, usagePeak: null, usageLast: null, usageCompacted: false, cancelling: false };
let acknowledge = () => {};
let rejectStartup = (_error: TurnNotStartedError) => {};
const startupAck = turn.startupRecovery ? new Promise<{ turnId: string }>((resolve, reject) => {
@@ -1110,6 +1119,8 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
if (!text.trim()) return;
state.producedItem = true;
state.repliedText = true;
+ state.toolsSinceShown = false;
+ state.thoughtSinceTool = false;
emit({ ...base(threadId, turnId), type: "item.completed", itemType: "assistant_text", text });
};
const current: AcpTurn = {
diff --git a/server/drivers/acp/turn-state.test.ts b/server/drivers/acp/turn-state.test.ts
index 12a6d4df66..9acecc7d54 100644
--- a/server/drivers/acp/turn-state.test.ts
+++ b/server/drivers/acp/turn-state.test.ts
@@ -5,20 +5,35 @@ import { endTurn, type AcpTurn } from "./turn-state.ts";
const ERROR_BODY = '403 {"type":"error","error":{"type":"permission_error","message":"OAuth authentication is currently not allowed for this organization."}}';
/** A turn whose buffered text is `text`, flushed as core.ts does: a non-blank text becomes a reply. */
-function turnWith(text: string, state: { stopped?: boolean; replied?: string } = {}) {
+function turnWith(
+ text: string,
+ state: { stopped?: boolean; replied?: string; toolsEnded?: boolean; thoughtAfterTools?: boolean; replyAfterTools?: string } = {},
+) {
const replies: string[] = [];
const current = {
- state: { text: state.replied ?? "", stopped: false, producedItem: false, repliedText: false },
+ state: { text: state.replied ?? "", stopped: false, producedItem: false, repliedText: false, toolsSinceShown: false, thoughtSinceTool: false },
flushAssistantText() {
const said = current.state.text;
current.state.text = "";
if (!said.trim()) return;
current.state.producedItem = true;
current.state.repliedText = true;
+ current.state.toolsSinceShown = false;
+ current.state.thoughtSinceTool = false;
replies.push(said);
},
};
if (state.replied) current.flushAssistantText();
+ if (state.toolsEnded) {
+ current.state.producedItem = true;
+ current.state.toolsSinceShown = true;
+ current.state.thoughtSinceTool = false;
+ }
+ if (state.thoughtAfterTools) current.state.thoughtSinceTool = true;
+ if (state.replyAfterTools) {
+ current.state.text = state.replyAfterTools;
+ current.flushAssistantText();
+ }
current.state.text = text;
current.state.stopped = state.stopped ?? false;
return { current: current as unknown as AcpTurn, replies };
@@ -58,3 +73,42 @@ describe("endTurn on a reply that is a provider's error body", () => {
expect(replies).toEqual(["Let me check the deploy.", ERROR_BODY]);
});
});
+
+describe("endTurn on a turn that goes wordless after its tools", () => {
+ it("reads as a lost reply when a talking turn reasons after a tool and never speaks again", () => {
+ const { current } = turnWith("", { replied: "Working through the folder.", toolsEnded: true, thoughtAfterTools: true });
+ expect(endTurn(current, true, null, "Grok")).toEqual({
+ ok: false, stopReason: "ended_after_tools", error: {
+ message: "Grok thought right past the last tool, then finished the turn without a last reply. Ask it to continue or summarize what it did.",
+ text: { key: "chat.notice.acpEndedAfterTools", params: { provider: "Grok" } },
+ },
+ });
+ });
+
+ it("stays a success when a reply follows the reasoning after the tools", () => {
+ const { current, replies } = turnWith("", { replied: "Working through the folder.", toolsEnded: true, thoughtAfterTools: true, replyAfterTools: "All six files are copied." });
+ expect(endTurn(current, true, null, "Grok")).toEqual({ ok: true, stopReason: null });
+ expect(replies).toEqual(["Working through the folder.", "All six files are copied."]);
+ });
+
+ it("stays a success when a turn that only runs tools ends without thinking or speaking again", () => {
+ const { current } = turnWith("", { toolsEnded: true });
+ expect(endTurn(current, true, null, "Grok")).toEqual({ ok: true, stopReason: null });
+ });
+
+ it("stays a success when a talking turn ends right on its tools without more words", () => {
+ const { current } = turnWith("", { replied: "Working through the folder.", toolsEnded: true });
+ expect(endTurn(current, true, null, "Grok")).toEqual({ ok: true, stopReason: null });
+ });
+
+ it("keeps buffered text after the tools as the turn's reply", () => {
+ const { current, replies } = turnWith("Copied the last file.", { toolsEnded: true, thoughtAfterTools: true });
+ expect(endTurn(current, true, null, "Grok")).toEqual({ ok: true, stopReason: null });
+ expect(replies).toEqual(["Copied the last file."]);
+ });
+
+ it("after the person's Stop, stays a stop even mid-reasoning after a tool", () => {
+ const { current } = turnWith("", { replied: "Working through the folder.", toolsEnded: true, thoughtAfterTools: true, stopped: true });
+ expect(endTurn(current, true, null, "omp")).toEqual({ ok: true, stopReason: "cancelled" });
+ });
+});
diff --git a/server/drivers/acp/turn-state.ts b/server/drivers/acp/turn-state.ts
index ad1a87b379..67d4387b2d 100644
--- a/server/drivers/acp/turn-state.ts
+++ b/server/drivers/acp/turn-state.ts
@@ -22,6 +22,10 @@ export interface AcpTurn {
controlsHost: boolean;
state: {
settled: boolean; promptSent: boolean; text: string; textMessageId: string | null; producedItem: boolean; repliedText: boolean; startupActivity: boolean; stopped: boolean;
+ /** A tool call ended, and no reply reached the chat since (text or image). The turn still runs, but its end is wordless. */
+ toolsSinceShown: boolean;
+ /** Reasoning streamed in that wordless window, and no reply followed it: the model was answering and never landed it. */
+ thoughtSinceTool: boolean;
usagePeak: number | null;
usageLast: number | null;
usageCompacted: boolean;
@@ -85,7 +89,9 @@ export interface AcpTurnEnd {
/** How a settling turn ends, once its last text is out. A Stop the agent answers with end_turn (OpenCode does) is the
* person's stop. A reply that is only a provider's error body fails the turn in plain words. Otherwise `end_turn` with
- * nothing to show is a lost turn: a provider may cut a reasoning-only stream and still answer end_turn. */
+ * nothing to show is a lost turn: a provider may cut a reasoning-only stream and still answer end_turn, and a turn that
+ * was talking, then streamed reasoning after its tools without landing a reply after it, went out midstream. Tool-only
+ * turns that never said anything, and turns that plainly end on their tools, stay successes. */
export function endTurn(current: AcpTurn, ok: boolean, stopReason: string | null, ai: string): AcpTurnEnd {
const ended = ok && stopReason === null;
const errorReply = ended && !current.state.stopped && !current.state.repliedText ? errorReplyFailure(ai, current.state.text) : null;
@@ -96,5 +102,6 @@ export function endTurn(current: AcpTurn, ok: boolean, stopReason: string | null
return { ok: false, stopReason: failed, error };
}
if (ended && !current.state.producedItem) return { ok: false, stopReason: "empty_turn", error: noticeFailure("chat.notice.acpEmpty", { provider: ai }) };
+ if (ended && current.state.repliedText && current.state.thoughtSinceTool) return { ok: false, stopReason: "ended_after_tools", error: noticeFailure("chat.notice.acpEndedAfterTools", { provider: ai }) };
return { ok, stopReason };
}
diff --git a/src/locales/en.json b/src/locales/en.json
index be57fbd767..06f151a91c 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -9929,6 +9929,7 @@
"serverError.approval.provider": "Use the bot's provider on this thread before applying its approval level",
"serverError.engineCheck.timeout": "{name} didn't answer within {seconds} seconds.",
"chat.notice.acpEmpty": "{provider} finished without replying.",
+ "chat.notice.acpEndedAfterTools": "{provider} thought right past the last tool, then finished the turn without a last reply. Ask it to continue or summarize what it did.",
"chat.notice.acpNoReason": "{provider} stopped without saying why.",
"chat.notice.acpNodeTooOld": "{provider} needs Node.js {needed} or newer, but this computer has Node.js {found}. Update Node.js from nodejs.org, then send your message again.",
"chat.notice.acpMcpAppsFailed": "Connected apps couldn't be set up, so {provider} couldn't start this reply. Try again, or switch this bot to another AI.",