Skip to content
TypeScript

perf(server): skip unused Git output line parsing (#23953)

Julius Marminge9a6380263b03
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index 84861d6dd4a..6bd0137154d 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -87,6 +87,79 @@ const makeFailedHandle = (stderr: string) =>
stderr: Stream.encodeText(Stream.make(stderr)),
});
+describe("Git output collection", () => {
+ // Git writes in arbitrary chunk sizes; one byte per chunk splits every
+ // multibyte character across reads.
+ const byteChunks = (text: string) =>
+ Stream.fromIterable(Array.from(new TextEncoder().encode(text), (byte) => Uint8Array.of(byte)));
+ const makeDriver = (output: { stdout?: string; stderr?: string }) =>
+ makeGitVcsDriverCore().pipe(
+ Effect.provideService(
+ ChildProcessSpawner.ChildProcessSpawner,
+ ChildProcessSpawner.make(() =>
+ Effect.succeed(
+ ChildProcessSpawner.makeHandle({
+ ...makeSuccessfulHandle(""),
+ stdout: byteChunks(output.stdout ?? ""),
+ stderr: byteChunks(output.stderr ?? ""),
+ }),
+ ),
+ ),
+ ),
+ );
+
+ it.effect("returns the same split UTF-8 and NUL output with and without line callbacks", () =>
+ Effect.gen(function* () {
+ const stdout = "a\\b\0雪\nlast ";
+ const stderr = "Receiving é\r\nUpdating 雪\rdone";
+ const driver = yield* makeDriver({ stdout, stderr });
+ const input = { operation: "test.chunkedGitOutput", cwd: "/repo", args: ["status"] };
+ const expected = {
+ exitCode: 0,
+ stdout,
+ stderr,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ };
+
+ assert.deepEqual(yield* driver.execute(input), expected);
+
+ const stdoutLines: string[] = [];
+ const stderrLines: string[] = [];
+ const withCallbacks = yield* driver.execute({
+ ...input,
+ progress: {
+ onStdoutLine: (line) => Effect.sync(() => void stdoutLines.push(line)),
+ onStderrLine: (line) => Effect.sync(() => void stderrLines.push(line)),
+ },
+ });
+ assert.deepEqual(withCallbacks, expected);
+ assert.deepEqual(stdoutLines, ["a\\b\0雪", "last "]);
+ assert.deepEqual(stderrLines, ["Receiving é", "Updating 雪", "done"]);
+ }).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
+ );
+
+ it.effect("caps output without line callbacks", () =>
+ Effect.gen(function* () {
+ const driver = yield* makeDriver({ stderr: "é雪\rdone" });
+ const input = {
+ operation: "test.cappedGitOutput",
+ cwd: "/repo",
+ args: ["status"],
+ maxOutputBytes: 3,
+ };
+
+ const truncated = yield* driver.execute({ ...input, appendTruncationMarker: true });
+ assert.equal(truncated.stderr, "é\uFFFD");
+ assert.isTrue(truncated.stderrTruncated);
+
+ const error = yield* driver.execute(input).pipe(Effect.flip);
+ assert.equal(error.outputLength, 4);
+ assert.include(error.detail, "exceeded 3 bytes");
+ }).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
+ );
+});
+
const makeTmpDir = (
prefix = "git-vcs-driver-test-",
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 62798374d2a..b5a523876a5 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -900,8 +900,10 @@ const collectOutput = Effect.fnUntraced(function* (
const decoded = decoder.decode(chunkToDecode, { stream: !truncated });
text += decoded;
- lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
- yield* emitCompleteLines(false);
+ if (onLine) {
+ lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
+ yield* emitCompleteLines(false);
+ }
});
yield* Stream.runForEach(stream, processChunk).pipe(
@@ -917,9 +919,11 @@ const collectOutput = Effect.fnUntraced(function* (
const remainder = truncated ? "" : decoder.decode();
text += remainder;
- lineBuffer += remainder;
- if (lineDecoder) lineBuffer += lineDecoder.decode();
- yield* emitCompleteLines(true);
+ if (onLine) {
+ lineBuffer += remainder;
+ if (lineDecoder) lineBuffer += lineDecoder.decode();
+ yield* emitCompleteLines(true);
+ }
return {
text,
truncated,