Skip to content

Commit 9a63802

Browse files
juliusmarmingejetphamclaude
authored
perf(server): skip unused Git output line parsing (#23953)
Co-authored-by: Jet <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent a68e6e9 commit 9a63802

2 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,79 @@ const makeFailedHandle = (stderr: string) =>
8787
stderr: Stream.encodeText(Stream.make(stderr)),
8888
});
8989

90+
describe("Git output collection", () => {
91+
// Git writes in arbitrary chunk sizes; one byte per chunk splits every
92+
// multibyte character across reads.
93+
const byteChunks = (text: string) =>
94+
Stream.fromIterable(Array.from(new TextEncoder().encode(text), (byte) => Uint8Array.of(byte)));
95+
const makeDriver = (output: { stdout?: string; stderr?: string }) =>
96+
makeGitVcsDriverCore().pipe(
97+
Effect.provideService(
98+
ChildProcessSpawner.ChildProcessSpawner,
99+
ChildProcessSpawner.make(() =>
100+
Effect.succeed(
101+
ChildProcessSpawner.makeHandle({
102+
...makeSuccessfulHandle(""),
103+
stdout: byteChunks(output.stdout ?? ""),
104+
stderr: byteChunks(output.stderr ?? ""),
105+
}),
106+
),
107+
),
108+
),
109+
);
110+
111+
it.effect("returns the same split UTF-8 and NUL output with and without line callbacks", () =>
112+
Effect.gen(function* () {
113+
const stdout = "a\\b\0雪\nlast ";
114+
const stderr = "Receiving é\r\nUpdating 雪\rdone";
115+
const driver = yield* makeDriver({ stdout, stderr });
116+
const input = { operation: "test.chunkedGitOutput", cwd: "/repo", args: ["status"] };
117+
const expected = {
118+
exitCode: 0,
119+
stdout,
120+
stderr,
121+
stdoutTruncated: false,
122+
stderrTruncated: false,
123+
};
124+
125+
assert.deepEqual(yield* driver.execute(input), expected);
126+
127+
const stdoutLines: string[] = [];
128+
const stderrLines: string[] = [];
129+
const withCallbacks = yield* driver.execute({
130+
...input,
131+
progress: {
132+
onStdoutLine: (line) => Effect.sync(() => void stdoutLines.push(line)),
133+
onStderrLine: (line) => Effect.sync(() => void stderrLines.push(line)),
134+
},
135+
});
136+
assert.deepEqual(withCallbacks, expected);
137+
assert.deepEqual(stdoutLines, ["a\\b\0雪", "last "]);
138+
assert.deepEqual(stderrLines, ["Receiving é", "Updating 雪", "done"]);
139+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
140+
);
141+
142+
it.effect("caps output without line callbacks", () =>
143+
Effect.gen(function* () {
144+
const driver = yield* makeDriver({ stderr: "é雪\rdone" });
145+
const input = {
146+
operation: "test.cappedGitOutput",
147+
cwd: "/repo",
148+
args: ["status"],
149+
maxOutputBytes: 3,
150+
};
151+
152+
const truncated = yield* driver.execute({ ...input, appendTruncationMarker: true });
153+
assert.equal(truncated.stderr, "é\uFFFD");
154+
assert.isTrue(truncated.stderrTruncated);
155+
156+
const error = yield* driver.execute(input).pipe(Effect.flip);
157+
assert.equal(error.outputLength, 4);
158+
assert.include(error.detail, "exceeded 3 bytes");
159+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
160+
);
161+
});
162+
90163
const makeTmpDir = (
91164
prefix = "git-vcs-driver-test-",
92165
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,10 @@ const collectOutput = Effect.fnUntraced(function* (
900900

901901
const decoded = decoder.decode(chunkToDecode, { stream: !truncated });
902902
text += decoded;
903-
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
904-
yield* emitCompleteLines(false);
903+
if (onLine) {
904+
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
905+
yield* emitCompleteLines(false);
906+
}
905907
});
906908

907909
yield* Stream.runForEach(stream, processChunk).pipe(
@@ -917,9 +919,11 @@ const collectOutput = Effect.fnUntraced(function* (
917919

918920
const remainder = truncated ? "" : decoder.decode();
919921
text += remainder;
920-
lineBuffer += remainder;
921-
if (lineDecoder) lineBuffer += lineDecoder.decode();
922-
yield* emitCompleteLines(true);
922+
if (onLine) {
923+
lineBuffer += remainder;
924+
if (lineDecoder) lineBuffer += lineDecoder.decode();
925+
yield* emitCompleteLines(true);
926+
}
923927
return {
924928
text,
925929
truncated,

0 commit comments

Comments
 (0)