fix: send v2 terminal output with CR LF, as a PTY would (#1632)

ACP v2 display terminals carry the bytes a terminal emulator receives.
Claude Code runs a command with its output piped, so no line discipline
turns each LF into CR LF (ONLCR), and the adapter sent the bare LFs
through. An emulator then moves down a row without returning to column
0, so each line starts where the previous one ended. Zed feeds v2 bytes
to its emulator unchanged and showed this staircase; on v1, Zed adds the
CRs itself.

V2Terminals now adds a CR before every LF of terminal_update snapshots
and terminal_output_chunks, which covers foreground commands and the
output files of background commands.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
John Tur
2026-11-16 01:43:00 -05:00
committed by GitHub
parent b1c198754d
commit 29dde3633a
4 changed files with 44 additions and 12 deletions

View File

@@ -10,7 +10,7 @@
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"rawInput":{"command":"false"},"sessionUpdate":"tool_call_update","title":"false","toolCallId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"_meta":{"_claude/model":"claude-sonnet-4-6"},"sessionUpdate":"usage_update","size":200000,"used":3}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"rawInput":{"command":"false","description":"Fail"},"sessionUpdate":"tool_call_update","toolCallId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"output":{"data":"RXhpdCBjb2RlIDEKYm9vbQ=="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"output":{"data":"RXhpdCBjb2RlIDENCmJvb20="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"exitStatus":{"exitCode":1},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"sessionUpdate":"tool_call_update","status":"failed","toolCallId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"_meta":{"_claude/model":"claude-sonnet-4-6"},"cost":{"amount":0,"currency":"USD"},"sessionUpdate":"usage_update","size":200000,"used":2}}}

View File

@@ -13,7 +13,7 @@
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","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":"<session>","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":"<session>","update":{"background":[],"sessionUpdate":"state_update","state":"running"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"output":{"data":"YS50cwpiLnRz"},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"output":{"data":"YS50cw0KYi50cw=="},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"exitStatus":{"exitCode":0},"sessionUpdate":"terminal_update","terminalId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"sessionUpdate":"tool_call_update","status":"completed","toolCallId":"toolu_bash"}}}
{"kind":"notification","method":"session/update","payload":{"sessionId":"<session>","update":{"content":{"text":"Do","type":"text"},"messageId":"msg_done","sessionUpdate":"agent_message_chunk"}}}

View File

@@ -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", {

View File

@@ -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");
}