diff --git a/src/message-filters.ts b/src/message-filters.ts index 4c81b986..3df2d6c4 100644 --- a/src/message-filters.ts +++ b/src/message-filters.ts @@ -87,6 +87,22 @@ function hasMarkerTag(text: string, tag: string): boolean { return markerTagText(text, tag) !== undefined; } +/** + * Whether the content of a user message is only the output of a local + * command, such as the `Set model to …` + * that Claude Code echoes when the model changes. + */ +export function isLocalCommandOutput(content: unknown): boolean { + if (typeof content !== "string") return false; + if ( + !hasMarkerTag(content, "local-command-stdout") && + !hasMarkerTag(content, "local-command-stderr") + ) { + return false; + } + return stripMarkerTags(content).trim() === ""; +} + function commandInvocationFromMarkerOnlyText(text: string): string | null { if (hasMarkerTag(text, "local-command-stdout") || hasMarkerTag(text, "local-command-stderr")) { return null; diff --git a/src/tests/v2/v2.test.ts b/src/tests/v2/v2.test.ts index 0791fb20..79ae441f 100644 --- a/src/tests/v2/v2.test.ts +++ b/src/tests/v2/v2.test.ts @@ -1012,6 +1012,30 @@ describe("ACP v2 core", () => { }); }); + it("stays idle at the echo of a model change's local command output", async () => { + const client = v2Client(); + await client.connect(async (agent) => { + const { claude } = await openSession(agent); + // As the bundled CLI sends it after `setModel`, with no turn: no + // `command_lifecycle` frame, result or `session_state_changed` follows. + claude.send({ + type: "user", + message: { + role: "user", + content: + "Set model to `haiku (claude-haiku-5-5)`", + }, + parent_tool_use_id: null, + uuid: randomUUID(), + session_id: "session", + timestamp: new Date().toISOString(), + isReplay: true, + }); + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(client.states()).toEqual([]); + }); + }); + it("forwards the result of a local command, which streamed no text", async () => { const client = v2Client(); await client.connect(async (agent) => { diff --git a/src/v2/session.ts b/src/v2/session.ts index d02da498..bfc2eb80 100644 --- a/src/v2/session.ts +++ b/src/v2/session.ts @@ -60,6 +60,7 @@ import { Pushable } from "../utils.js"; import { v1PromptRequest } from "./prompt.js"; import { V2Background, stateUpdate as currentStateUpdate } from "./background.js"; import type { V2UpdateTranslator } from "./update-translator.js"; +import { isLocalCommandOutput } from "../message-filters.js"; import { MODEL_CONFIG_ID } from "../session-model.js"; import { modelRefusalFallback, noticeUpdate } from "../session-notices.js"; import { planModeUpdate } from "../exit-plan.js"; @@ -939,6 +940,12 @@ export class V2Session implements RequestTarget { // in. A prompt Claude Code reported `started` for is inserted already. if ("isReplay" in message && message.isReplay) { if (message.uuid && message.parent_tool_use_id === null) { + // The output of a local command that Claude Code ran outside a + // turn, such as the "Set model to …" of a model change, is no + // prompt: no result or `command_lifecycle` frame would end its turn. + if (!this.pending.has(message.uuid) && isLocalCommandOutput(message.message.content)) { + return; + } // A message typed in another client of the session. const remote = this.remoteControl.remoteMessage(message); if (remote !== undefined) this.output.userMessage(remote, message.uuid);