mirror of
https://github.com/agentclientprotocol/claude-agent-acp.git
synced 2026-12-02 14:11:58 +00:00
fix: preserve existing line endings in ACP v2 terminal output (#1697)
Claude Code pipes command output, so a v2 display terminal needs a carriage return before a bare line feed to start the next line at column zero. Bash output can also contain CRLF and standalone carriage returns used for progress updates. The encoder added an extra CR to existing CRLF. Convert only bare LF in v2 terminal chunks and snapshots. Track the preceding CR on each running terminal so CRLF can span two chunks. A replacement snapshot starts fresh and supplies the trailing character for the next chunk. This also covers background output, where the output-file reader can send a snapshot ending in CR and then append a chunk starting with LF. The regression test checks mixed line endings, progress updates, empty chunks, and snapshot replacement. The corresponding Codex fix is [codex-acp#795](https://github.com/agentclientprotocol/codex-acp/pull/795).
This commit is contained in:
@@ -10234,30 +10234,39 @@ 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", () => {
|
||||
it("adds CR only to bare line feeds in terminal chunks and snapshots", () => {
|
||||
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");
|
||||
const emit = (key: "terminal_output" | "terminal_output_delta", data: string) =>
|
||||
terminals.split("s", {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "toolu_bash",
|
||||
_meta: { [key]: { terminal_id: "term", data } },
|
||||
}).terminal[0];
|
||||
const chunk = (text: string, expected: string) =>
|
||||
expect(emit("terminal_output_delta", text)).toMatchObject({
|
||||
sessionUpdate: "terminal_output_chunk",
|
||||
data: Buffer.from(expected).toString("base64"),
|
||||
});
|
||||
const snapshot = (text: string, expected: string) =>
|
||||
expect(emit("terminal_output", text)).toMatchObject({
|
||||
sessionUpdate: "terminal_update",
|
||||
output: { data: Buffer.from(expected).toString("base64") },
|
||||
});
|
||||
|
||||
chunk("a\nb\r\n10%\r20%\r", "a\r\nb\r\n10%\r20%\r");
|
||||
chunk("", "");
|
||||
chunk("\nend\r", "\nend\r");
|
||||
// A snapshot replaces the preceding bytes, including their trailing CR.
|
||||
snapshot("\ndone\r\n", "\r\ndone\r\n");
|
||||
chunk("\n", "\r\n");
|
||||
snapshot("waiting\r", "waiting\r");
|
||||
chunk("\n", "\n");
|
||||
});
|
||||
|
||||
it("leaves an exit code that the agent does not know out of the terminal's exit", () => {
|
||||
|
||||
@@ -33,13 +33,17 @@ const TERMINAL_KEYS = [
|
||||
"terminal_exit",
|
||||
] as const;
|
||||
|
||||
interface RunningTerminal {
|
||||
terminalId: string;
|
||||
command?: string;
|
||||
cancellable?: boolean;
|
||||
endsInCR: boolean;
|
||||
}
|
||||
|
||||
/** The terminal updates of the tool call reports of one v2 connection. */
|
||||
export class V2Terminals {
|
||||
/** The terminal and the last reported command of each running command, by session and tool call. */
|
||||
private readonly running = new Map<
|
||||
string,
|
||||
{ terminalId: string; command?: string; cancellable?: boolean }
|
||||
>();
|
||||
/** The display terminal of each running command, by session and tool call. */
|
||||
private readonly running = new Map<string, RunningTerminal>();
|
||||
|
||||
/**
|
||||
* The terminal updates that a tool call report carries in the extension's
|
||||
@@ -63,7 +67,7 @@ export class V2Terminals {
|
||||
meta.terminal_info &&
|
||||
this.running.get(key)?.terminalId !== meta.terminal_info.terminal_id
|
||||
) {
|
||||
this.running.set(key, { terminalId: meta.terminal_info.terminal_id });
|
||||
this.running.set(key, { terminalId: meta.terminal_info.terminal_id, endsInCR: false });
|
||||
}
|
||||
const terminal: v2.SessionUpdate[] = [];
|
||||
const running = this.running.get(key);
|
||||
@@ -77,7 +81,7 @@ export class V2Terminals {
|
||||
sessionUpdate: "terminal_update",
|
||||
terminalId: running.terminalId,
|
||||
...(newCommand ? { command } : {}),
|
||||
...(output ? { output: { data: terminalBytes(output.data) } } : {}),
|
||||
...(output ? { output: { data: terminalBytes(output.data, running, "snapshot") } } : {}),
|
||||
...(exit
|
||||
? {
|
||||
exitStatus: {
|
||||
@@ -94,7 +98,7 @@ export class V2Terminals {
|
||||
terminal.push({
|
||||
sessionUpdate: "terminal_output_chunk",
|
||||
terminalId: running.terminalId,
|
||||
data: terminalBytes(delta.data),
|
||||
data: terminalBytes(delta.data, running, "chunk"),
|
||||
});
|
||||
}
|
||||
if (exit) this.running.delete(key);
|
||||
@@ -161,13 +165,16 @@ function commandOf(rawInput: unknown): string | undefined {
|
||||
return typeof command === "string" && command.length > 0 ? command : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
/** Insert a carriage return before bare LF so the next line starts at column zero. */
|
||||
function terminalBytes(
|
||||
text: string,
|
||||
terminal: RunningTerminal,
|
||||
kind: "snapshot" | "chunk",
|
||||
): string {
|
||||
const followsCR = kind === "chunk" && terminal.endsInCR;
|
||||
const normalized = text.replace(/(?<!\r)\n/g, (lf, offset) =>
|
||||
offset === 0 && followsCR ? lf : "\r\n",
|
||||
);
|
||||
if (kind === "snapshot" || text.length > 0) terminal.endsInCR = text.endsWith("\r");
|
||||
return Buffer.from(normalized, "utf8").toString("base64");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user