diff --git a/src/tests/acp-scenarios/__snapshots__/v2/bash-error.jsonl b/src/tests/acp-scenarios/__snapshots__/v2/bash-error.jsonl index 39b38953..f4f33db6 100644 --- a/src/tests/acp-scenarios/__snapshots__/v2/bash-error.jsonl +++ b/src/tests/acp-scenarios/__snapshots__/v2/bash-error.jsonl @@ -10,7 +10,7 @@ {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"rawInput":{"command":"false"},"sessionUpdate":"tool_call_update","title":"false","toolCallId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"_meta":{"_claude/model":"claude-sonnet-4-6"},"sessionUpdate":"usage_update","size":200000,"used":3}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"rawInput":{"command":"false","description":"Fail"},"sessionUpdate":"tool_call_update","toolCallId":"toolu_bash"}}} -{"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"output":{"data":"RXhpdCBjb2RlIDEKYm9vbQ=="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} +{"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"output":{"data":"RXhpdCBjb2RlIDENCmJvb20="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"exitStatus":{"exitCode":1},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"sessionUpdate":"tool_call_update","status":"failed","toolCallId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"_meta":{"_claude/model":"claude-sonnet-4-6"},"cost":{"amount":0,"currency":"USD"},"sessionUpdate":"usage_update","size":200000,"used":2}}} diff --git a/src/tests/acp-scenarios/__snapshots__/v2/bash-foreground.jsonl b/src/tests/acp-scenarios/__snapshots__/v2/bash-foreground.jsonl index 52901a5f..31b0a379 100644 --- a/src/tests/acp-scenarios/__snapshots__/v2/bash-foreground.jsonl +++ b/src/tests/acp-scenarios/__snapshots__/v2/bash-foreground.jsonl @@ -13,7 +13,7 @@ {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"background":[],"sessionUpdate":"state_update","state":"requires_action"}}} {"kind":"request","method":"session/request_permission","payload":{"options":[{"kind":"allow_once","name":"Yes","optionId":"allow-once"},{"kind":"reject_once","name":"No","optionId":"reject"}],"sessionId":"","subject":{"toolCall":{"content":[{"terminalId":"toolu_bash","type":"terminal"}],"kind":"execute","name":"Bash","rawInput":{"command":"ls -la","description":"List files"},"title":"ls -la","toolCallId":"toolu_bash"},"type":"tool_call"},"title":"ls -la"}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"background":[],"sessionUpdate":"state_update","state":"running"}}} -{"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"output":{"data":"YS50cwpiLnRz"},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} +{"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"output":{"data":"YS50cw0KYi50cw=="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"exitStatus":{"exitCode":0},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"sessionUpdate":"tool_call_update","status":"completed","toolCallId":"toolu_bash"}}} {"kind":"notification","method":"session/update","payload":{"sessionId":"","update":{"content":{"text":"Do","type":"text"},"messageId":"msg_done","sessionUpdate":"agent_message_chunk"}}} diff --git a/src/tests/v2/acp-v2.test.ts b/src/tests/v2/acp-v2.test.ts index 9be4e69f..341f3032 100644 --- a/src/tests/v2/acp-v2.test.ts +++ b/src/tests/v2/acp-v2.test.ts @@ -8409,11 +8409,11 @@ describe("ACP v2 terminal of a command in the background", () => { }); try { const client = await promptUntil("idle end_turn [toolu_bg]", async (_, __, client) => { - await vi.waitFor(() => expect(output(client)).toContain("output compiling\n"), { + await vi.waitFor(() => expect(output(client)).toContain("output compiling\r\n"), { timeout: 2000, }); await appendFile(outputFile, "tests passed\n"); - await vi.waitFor(() => expect(output(client)).toContain("chunk tests passed\n"), { + await vi.waitFor(() => expect(output(client)).toContain("chunk tests passed\r\n"), { timeout: 2000, }); // The command writes its last line as it ends. @@ -8424,9 +8424,9 @@ describe("ACP v2 terminal of a command in the background", () => { // The file's first text replaces the Bash result's placeholder. expect(output(client).slice(-4)).toEqual([ - "output compiling\n", - "chunk tests passed\n", - "chunk done\n", + "output compiling\r\n", + "chunk tests passed\r\n", + "chunk done\r\n", "exit", ]); } finally { @@ -9453,6 +9453,32 @@ describe("ACP v2 session translation", () => { expect(terminals.split("s", hook)).toEqual({ terminal: [], report: hook }); }); + it("sends piped output with each line feed as CR LF, as a PTY would", () => { + const terminals = new V2Terminals(); + const decoded = (data: unknown) => Buffer.from(String(data), "base64").toString(); + terminals.split("s", { + sessionUpdate: "tool_call", + toolCallId: "toolu_bash", + title: "Terminal", + _meta: { terminal_info: { terminal_id: "term" } }, + }); + const [chunk] = terminals.split("s", { + sessionUpdate: "tool_call_update", + toolCallId: "toolu_bash", + _meta: { terminal_output_delta: { terminal_id: "term", data: "a\nb\n" } }, + }).terminal; + expect(chunk).toMatchObject({ sessionUpdate: "terminal_output_chunk" }); + expect(decoded((chunk as { data: string }).data)).toBe("a\r\nb\r\n"); + // The line discipline adds a CR before every LF, also one that a CR precedes. + const [snapshot] = terminals.split("s", { + sessionUpdate: "tool_call_update", + toolCallId: "toolu_bash", + _meta: { terminal_output: { terminal_id: "term", data: "a\nb\r\n" } }, + }).terminal; + expect(snapshot).toMatchObject({ sessionUpdate: "terminal_update" }); + expect(decoded((snapshot as { output: { data: string } }).output.data)).toBe("a\r\nb\r\r\n"); + }); + it("leaves an exit code that the agent does not know out of the terminal's exit", () => { const terminals = new V2Terminals(); terminals.split("s", { diff --git a/src/v2/terminal.ts b/src/v2/terminal.ts index e018a138..aa319a32 100644 --- a/src/v2/terminal.ts +++ b/src/v2/terminal.ts @@ -77,7 +77,7 @@ export class V2Terminals { sessionUpdate: "terminal_update", terminalId: running.terminalId, ...(newCommand ? { command } : {}), - ...(output ? { output: { data: base64(output.data) } } : {}), + ...(output ? { output: { data: terminalBytes(output.data) } } : {}), ...(exit ? { exitStatus: { @@ -94,7 +94,7 @@ export class V2Terminals { terminal.push({ sessionUpdate: "terminal_output_chunk", terminalId: running.terminalId, - data: base64(delta.data), + data: terminalBytes(delta.data), }); } if (exit) this.running.delete(key); @@ -161,7 +161,13 @@ function commandOf(rawInput: unknown): string | undefined { return typeof command === "string" && command.length > 0 ? command : undefined; } -/** The output bytes, base64-encoded as a v2 terminal carries them. */ -function base64(text: string): string { - return Buffer.from(text, "utf8").toString("base64"); +/** + * The output as the bytes a PTY would have delivered, base64-encoded as a v2 + * terminal carries them. A v2 terminal carries the bytes a terminal emulator + * receives, but Claude Code runs a command with its output piped, so no line + * discipline turned each LF into CR LF (`ONLCR`). Without the CR, an emulator + * moves down a row and keeps the column: "a\nb" draws "b" under the end of "a". + */ +function terminalBytes(text: string): string { + return Buffer.from(text.replaceAll("\n", "\r\n"), "utf8").toString("base64"); }