Skip to content

Commit a099f85

Browse files
committed
perf(server): skip unused Git output line parsing
1 parent 07cb902 commit a099f85

2 files changed

Lines changed: 106 additions & 5 deletions

File tree

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,103 @@ const makeSuccessfulHandle = (stdout: string) =>
8080
getOutputFd: () => Stream.empty,
8181
});
8282

83+
describe("Git output collection", () => {
84+
it.effect.each([false, true])(
85+
"preserves chunked UTF-8 and NUL output with callbacks=%s",
86+
(callbacks) =>
87+
Effect.gen(function* () {
88+
const stdout = "a\\b\0雪\nlast ";
89+
const stderr = "Receiving é\r\nUpdating 雪\rdone";
90+
const chunks = (text: string) =>
91+
Stream.fromIterable(
92+
Array.from(new TextEncoder().encode(text), (byte) => Uint8Array.of(byte)),
93+
);
94+
const spawner = ChildProcessSpawner.make(() =>
95+
Effect.succeed(
96+
ChildProcessSpawner.makeHandle({
97+
...makeSuccessfulHandle(""),
98+
stdout: chunks(stdout),
99+
stderr: chunks(stderr),
100+
}),
101+
),
102+
);
103+
const driver = yield* makeGitVcsDriverCore().pipe(
104+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
105+
);
106+
const stdoutLines: string[] = [];
107+
const stderrLines: string[] = [];
108+
const result = yield* driver.execute({
109+
operation: "test.chunkedGitOutput",
110+
cwd: "/repo",
111+
args: ["status"],
112+
...(callbacks
113+
? {
114+
progress: {
115+
onStdoutLine: (line: string) => Effect.sync(() => void stdoutLines.push(line)),
116+
onStderrLine: (line: string) => Effect.sync(() => void stderrLines.push(line)),
117+
},
118+
}
119+
: {}),
120+
});
121+
assert.deepEqual(result, {
122+
exitCode: 0,
123+
stdout,
124+
stderr,
125+
stdoutTruncated: false,
126+
stderrTruncated: false,
127+
});
128+
assert.deepEqual(stdoutLines, callbacks ? ["a\\b\0雪", "last "] : []);
129+
assert.deepEqual(stderrLines, callbacks ? ["Receiving é", "Updating 雪", "done"] : []);
130+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
131+
);
132+
133+
it.effect.each([
134+
{ callbacks: false, keep: false },
135+
{ callbacks: false, keep: true },
136+
{ callbacks: true, keep: false },
137+
{ callbacks: true, keep: true },
138+
])(
139+
"preserves byte caps with callbacks=$callbacks and continued callbacks=$keep",
140+
({ callbacks, keep }) =>
141+
Effect.gen(function* () {
142+
const bytes = new TextEncoder().encode("é雪\rdone");
143+
const spawner = ChildProcessSpawner.make(() =>
144+
Effect.succeed(
145+
ChildProcessSpawner.makeHandle({
146+
...makeSuccessfulHandle(""),
147+
stderr: Stream.fromIterable(Array.from(bytes, (byte) => Uint8Array.of(byte))),
148+
}),
149+
),
150+
);
151+
const driver = yield* makeGitVcsDriverCore().pipe(
152+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
153+
);
154+
const lines: string[] = [];
155+
const input = {
156+
operation: "test.cappedGitOutput",
157+
cwd: "/repo",
158+
args: ["status"],
159+
maxOutputBytes: 3,
160+
keepLineCallbacksAfterTruncation: keep,
161+
...(callbacks
162+
? {
163+
progress: {
164+
onStderrLine: (line: string) => Effect.sync(() => void lines.push(line)),
165+
},
166+
}
167+
: {}),
168+
};
169+
const result = yield* driver.execute({ ...input, appendTruncationMarker: true });
170+
assert.equal(result.stderr, "é\uFFFD");
171+
assert.isTrue(result.stderrTruncated);
172+
assert.deepEqual(lines, callbacks ? (keep ? ["é雪", "done"] : ["é\uFFFD"]) : []);
173+
const error = yield* driver.execute(input).pipe(Effect.flip);
174+
assert.equal(error.outputLength, 4);
175+
assert.include(error.detail, "exceeded 3 bytes");
176+
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
177+
);
178+
});
179+
83180
const makeTmpDir = (
84181
prefix = "git-vcs-driver-test-",
85182
): 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
@@ -883,8 +883,10 @@ const collectOutput = Effect.fnUntraced(function* (
883883

884884
const decoded = decoder.decode(chunkToDecode, { stream: !truncated });
885885
text += decoded;
886-
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
887-
yield* emitCompleteLines(false);
886+
if (onLine) {
887+
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
888+
yield* emitCompleteLines(false);
889+
}
888890
});
889891

890892
yield* Stream.runForEach(stream, processChunk).pipe(
@@ -900,9 +902,11 @@ const collectOutput = Effect.fnUntraced(function* (
900902

901903
const remainder = truncated ? "" : decoder.decode();
902904
text += remainder;
903-
lineBuffer += remainder;
904-
if (lineDecoder) lineBuffer += lineDecoder.decode();
905-
yield* emitCompleteLines(true);
905+
if (onLine) {
906+
lineBuffer += remainder;
907+
if (lineDecoder) lineBuffer += lineDecoder.decode();
908+
yield* emitCompleteLines(true);
909+
}
906910
return {
907911
text,
908912
truncated,

0 commit comments

Comments
 (0)