Skip to content

Commit 211c1ed

Browse files
georgeglarsonclaude
andcommitted
Add truncated flag to RunResult for output exceeding 256 KB
Callers can now distinguish between normal output and silently truncated output. Two new tests verify the flag for small and oversized output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1f66083 commit 211c1ed

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

RestaurantReviews/src/server/__tests__/runner.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,19 @@ describe("spawnAndCapture", () => {
128128
expect(result.stdout).toBe("");
129129
expect(result.stderr).toBe("");
130130
});
131+
132+
it("sets truncated to false for small output", async () => {
133+
const result = await spawnAndCapture("/bin/echo", ["hello"]);
134+
expect(result.truncated).toBe(false);
135+
});
136+
137+
it("sets truncated to true when stdout exceeds 256 KB", async () => {
138+
// Generate 300 KB of output via dd
139+
const result = await spawnAndCapture("/bin/dd", [
140+
"if=/dev/zero",
141+
"bs=1024",
142+
"count=300",
143+
]);
144+
expect(result.truncated).toBe(true);
145+
});
131146
});

RestaurantReviews/src/server/exercises/runner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface RunResult {
1010
stdout: string;
1111
stderr: string;
1212
exitCode: number;
13+
truncated: boolean;
1314
}
1415

1516
/**
@@ -23,7 +24,7 @@ export async function runWithFile(
2324
options: { interpreter?: string; timeout?: number } = {}
2425
): Promise<RunResult> {
2526
if (Buffer.byteLength(input, "utf-8") > MAX_INPUT_BYTES) {
26-
return { stdout: "", stderr: "Input too large", exitCode: 1 };
27+
return { stdout: "", stderr: "Input too large", exitCode: 1, truncated: false };
2728
}
2829

2930
const dir = await mkdtemp(path.join(tmpdir(), "exercise-"));
@@ -70,6 +71,7 @@ export function spawnAndCapture(
7071
stdout: Buffer.concat(stdoutChunks).toString("utf-8"),
7172
stderr: Buffer.concat(stderrChunks).toString("utf-8"),
7273
exitCode: code ?? 1,
74+
truncated: stdoutLen > MAX_OUTPUT_BYTES || stderrLen > MAX_OUTPUT_BYTES,
7375
});
7476
});
7577
});

0 commit comments

Comments
 (0)