Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
withFacultRootScope,
} from "./paths";
import { redactReconciliationText } from "./reconciliation-adapters";
import { writeCliOutput } from "./util/cli-output";

const NEWLINE_RE = /\r?\n/;
const TRAILING_NEWLINE_RE = /\n$/;
Expand Down Expand Up @@ -3118,7 +3119,7 @@ async function loopCommand(argv: string[]) {
homeDir,
globalRootDir,
});
console.log(
await writeCliOutput(
json ? JSON.stringify(result, null, 2) : renderActivitySet(result)
);
return;
Expand All @@ -3131,7 +3132,7 @@ async function loopCommand(argv: string[]) {
if (!result) {
throw new Error("No evolution loop report has been recorded");
}
console.log(
await writeCliOutput(
json ? JSON.stringify(result, null, 2) : renderActivityFeed(result)
);
return;
Expand Down Expand Up @@ -3206,7 +3207,7 @@ async function loopCommand(argv: string[]) {
},
async () => await queryActivityHistory(queryArgs)
);
console.log(
await writeCliOutput(
json ? JSON.stringify(result, null, 2) : renderActivityHistory(result)
);
return;
Expand Down
28 changes: 28 additions & 0 deletions src/util/cli-output.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from "bun:test";

describe("writeCliOutput", () => {
it("flushes output larger than Bun's buffered stdout window", async () => {
const expectedBytes = 200_001;
const proc = Bun.spawn(
[
process.execPath,
"-e",
'import { writeCliOutput } from "./src/util/cli-output"; await writeCliOutput("x".repeat(200_000));',
],
{
cwd: process.cwd(),
stdout: "pipe",
stderr: "pipe",
}
);
const [exitCode, stdout, stderr] = await Promise.all([
proc.exited,
new Response(proc.stdout).arrayBuffer(),
new Response(proc.stderr).text(),
]);

expect(exitCode).toBe(0);
expect(stderr).toBe("");
expect(stdout.byteLength).toBe(expectedBytes);
});
});
19 changes: 19 additions & 0 deletions src/util/cli-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const BUFFERED_STDOUT_THRESHOLD_BYTES = 64 * 1024;

export async function writeCliOutput(output: string): Promise<void> {
const terminated = `${output}\n`;
if (Buffer.byteLength(terminated, "utf8") < BUFFERED_STDOUT_THRESHOLD_BYTES) {
console.log(output);
return;
}

await new Promise<void>((resolve, reject) => {
process.stdout.write(terminated, (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
Loading