Skip to content

Commit 2d0a9df

Browse files
StiensWoutcodexclaude
authored
fix(provider-acp): preserve UTF-8 across interleaved terminal streams (pingdotgg#32391)
Co-authored-by: GPT 6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent c864b1c commit 2d0a9df

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎packages/provider-acp/src/server/clientTerminals.test.ts‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,52 @@ describe("AcpClientTerminals", () => {
5151
),
5252
);
5353

54+
it.effect("preserves UTF-8 when stderr interleaves a split stdout character", () =>
55+
Effect.gen(function* () {
56+
const stdoutStarted = yield* Deferred.make<void>();
57+
const stderrWritten = yield* Deferred.make<void>();
58+
const child = ChildProcessSpawner.makeHandle({
59+
pid: ChildProcessSpawner.ProcessId(1),
60+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
61+
isRunning: Effect.succeed(false),
62+
kill: () => Effect.void,
63+
stdin: Sink.drain,
64+
stdout: Stream.make(new Uint8Array([0xe2])).pipe(
65+
Stream.concat(Stream.fromEffectDrain(Deferred.succeed(stdoutStarted, undefined))),
66+
Stream.concat(Stream.fromEffectDrain(Deferred.await(stderrWritten))),
67+
Stream.concat(Stream.make(new Uint8Array([0x82, 0xac]))),
68+
),
69+
stderr: Stream.fromEffectDrain(Deferred.await(stdoutStarted)).pipe(
70+
Stream.concat(Stream.make(new Uint8Array([0x58]))),
71+
Stream.concat(Stream.fromEffectDrain(Deferred.succeed(stderrWritten, undefined))),
72+
),
73+
all: Stream.empty,
74+
getInputFd: () => Sink.drain,
75+
getOutputFd: () => Stream.empty,
76+
unref: Effect.succeed(Effect.void),
77+
});
78+
const terminals = yield* makeAcpClientTerminals({ defaultCwd: process.cwd() }).pipe(
79+
Effect.provideService(
80+
ChildProcessSpawner.ChildProcessSpawner,
81+
ChildProcessSpawner.make(() => Effect.succeed(child)),
82+
),
83+
);
84+
yield* Effect.addFinalizer(() => terminals.disposeAll);
85+
const terminal = yield* terminals.create({ sessionId: "session", command: "fixture" });
86+
const request = { sessionId: "session", ...terminal };
87+
yield* terminals.waitForExit(request);
88+
89+
const output = yield* terminals.output(request);
90+
expect(output.output).toContain("€");
91+
expect(output.output).toContain("X");
92+
expect(output.output).not.toContain("\uFFFD");
93+
expect(output.truncated).toBe(false);
94+
expect(terminals.readOutputSnapshot("session", terminal.terminalId)?.output).toBe(
95+
output.output,
96+
);
97+
}).pipe(Effect.scoped),
98+
);
99+
54100
it.live("resolves terminal environment from the requesting ACP session", () =>
55101
Effect.gen(function* () {
56102
const terminals = yield* makeAcpClientTerminals({
@@ -96,14 +142,17 @@ describe("AcpClientTerminals", () => {
96142
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
97143
);
98144

99-
it.live("truncates buffered output from the beginning at the byte limit", () =>
145+
it.live.each([
146+
{ text: "a".repeat(64) + "TAIL", limit: 16 },
147+
{ text: "€".repeat(16) + "TAIL", limit: 6 },
148+
])("truncates buffered output from the beginning at the $limit byte limit", ({ text, limit }) =>
100149
withTerminals((terminals) =>
101150
Effect.gen(function* () {
102151
const created = yield* terminals.create({
103152
sessionId: "session",
104153
command: process.execPath,
105-
args: ["-e", "process.stdout.write('a'.repeat(64) + 'TAIL');"],
106-
outputByteLimit: 16,
154+
args: ["-e", `process.stdout.write(${JSON.stringify(text)});`],
155+
outputByteLimit: limit,
107156
});
108157
yield* terminals.waitForExit({ sessionId: "session", terminalId: created.terminalId });
109158

@@ -113,7 +162,8 @@ describe("AcpClientTerminals", () => {
113162
});
114163
expect(output.truncated).toBe(true);
115164
expect(output.output.endsWith("TAIL")).toBe(true);
116-
expect(output.output.length).toBeLessThanOrEqual(16);
165+
expect(Buffer.byteLength(output.output)).toBeLessThanOrEqual(limit);
166+
expect(output.output).not.toContain("\uFFFD");
117167
}),
118168
),
119169
);

‎packages/provider-acp/src/server/clientTerminals.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Channel from "effect/Channel";
12
import * as Deferred from "effect/Deferred";
23
import * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
@@ -277,6 +278,9 @@ export const makeAcpClientTerminals = Effect.fn("makeAcpClientTerminals")(functi
277278

278279
const pump = (stream: typeof child.stdout) =>
279280
stream.pipe(
281+
// Keep each pipe's UTF-8 decoder independent and preserve literal BOMs.
282+
Stream.pipeThroughChannel(Channel.decodeText("utf-8", { ignoreBOM: true })),
283+
Stream.encodeText,
280284
Stream.runForEach((chunk) =>
281285
Effect.sync(() => {
282286
appendOutput(record.buffer, chunk);

0 commit comments

Comments
 (0)