Skip to content

Commit 049e627

Browse files
fix(cli): deliver console output larger than a pipe buffer
Route Effect Console through process stdout/stderr and wait for the last write on each stream before exit, so --completions fish is no longer cut at 65536 bytes when piped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1cfb8ab commit 049e627

4 files changed

Lines changed: 64 additions & 124 deletions

File tree

‎packages/cli/src/index.ts‎

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { ensurePluginRuntime } from "@opencode/plugin/runtime"
44
import { NodeRuntime, NodeServices } from "@effect/platform-node"
5-
import { Cause, Effect } from "effect"
5+
import { Cause, Console, Effect } from "effect"
66
import { getErrorReported } from "effect/Runtime"
77
import { Commands } from "./commands/commands"
88
import { Runtime } from "./framework/runtime"
@@ -17,86 +17,7 @@ import { Npm } from "@opencode/util/npm"
1717
import { EffectFlock } from "@opencode/util/effect-flock"
1818
import { Heap } from "./heap"
1919
import { CpuProfile } from "./cpu-profile"
20-
import fs from "node:fs"
21-
import { format } from "node:util"
22-
23-
// `packages/cli/bunfig.toml` loads `@opentui/solid/preload`, which leaves stdout
24-
// and stderr nonblocking. A single write larger than one pipe buffer is then
25-
// silently truncated past 65536 bytes (`--completions fish` is ~68 KiB, so
26-
// its tail was lost when piped). Retrying full pipes in guarded chunks keeps
27-
// that output from being lost.
28-
// See https://github.com/anomalyco/opencode/issues/55525
29-
const WRITE_CHUNK = 16 * 1024
30-
const WRITE_WAIT = new Int32Array(new SharedArrayBuffer(4))
31-
const WRITE_TIMEOUT_MS = 5_000
32-
function writeAll(fd: number, buffer: string | Buffer) {
33-
const data = typeof buffer === "string" ? Buffer.from(buffer) : buffer
34-
const deadline = Date.now() + WRITE_TIMEOUT_MS
35-
let offset = 0
36-
while (offset < data.length) {
37-
let written: number | undefined
38-
try {
39-
written = fs.writeSync(fd, data, offset, Math.min(WRITE_CHUNK, data.length - offset))
40-
} catch (error) {
41-
// a closed or broken pipe (EPIPE etc.) is not retryable; only pause on
42-
// a temporarily full one (EAGAIN) so the real exit code still wins.
43-
if ((error as NodeJS.ErrnoException)?.code !== "EAGAIN") return
44-
}
45-
if (written === undefined || written <= 0) {
46-
if (Date.now() > deadline) return
47-
Atomics.wait(WRITE_WAIT, 0, 0, 1)
48-
continue
49-
}
50-
offset += written
51-
}
52-
}
53-
for (const [stream, fd] of [
54-
[process.stdout, 1],
55-
[process.stderr, 2],
56-
] as const) {
57-
const write = stream.write.bind(stream)
58-
stream.write = ((chunk: string | Uint8Array, ...args: Array<string | (() => void)>) => {
59-
if (typeof chunk !== "string" && !(chunk instanceof Uint8Array))
60-
return (write as (...args: Array<unknown>) => boolean)(chunk, ...args)
61-
const lastArg = args.length > 0 ? args[args.length - 1] : undefined
62-
const hasCallback = typeof lastArg === "function"
63-
const callback = hasCallback ? (lastArg as () => void) : undefined
64-
const encoding = typeof args[0] === "string" ? (args[0] as BufferEncoding) : "utf8"
65-
const buffer = typeof chunk === "string" ? Buffer.from(chunk, encoding) : Buffer.from(chunk)
66-
if (buffer.length <= WRITE_CHUNK) return (write as (...args: Array<unknown>) => boolean)(chunk, ...args)
67-
writeAll(fd, buffer)
68-
if (callback) queueMicrotask(callback)
69-
return true
70-
}) as typeof stream.write
71-
}
72-
const defaultConsole = globalThis.console
73-
74-
// Console output does not go through the stream wrappers above under Bun.
75-
console.log = ((...args: Array<unknown>) => {
76-
const text = format(...args) + "\n"
77-
if (text.length <= WRITE_CHUNK) return defaultConsole.log(...args)
78-
writeAll(1, text)
79-
}) as typeof defaultConsole.log
80-
console.info = ((...args: Array<unknown>) => {
81-
const text = format(...args) + "\n"
82-
if (text.length <= WRITE_CHUNK) return defaultConsole.info(...args)
83-
writeAll(1, text)
84-
}) as typeof defaultConsole.info
85-
console.debug = ((...args: Array<unknown>) => {
86-
const text = format(...args) + "\n"
87-
if (text.length <= WRITE_CHUNK) return defaultConsole.debug(...args)
88-
writeAll(1, text)
89-
}) as typeof defaultConsole.debug
90-
console.warn = ((...args: Array<unknown>) => {
91-
const text = format(...args) + "\n"
92-
if (text.length <= WRITE_CHUNK) return defaultConsole.warn(...args)
93-
writeAll(2, text)
94-
}) as typeof defaultConsole.warn
95-
console.error = ((...args: Array<unknown>) => {
96-
const text = format(...args) + "\n"
97-
if (text.length <= WRITE_CHUNK) return defaultConsole.error(...args)
98-
writeAll(2, text)
99-
}) as typeof defaultConsole.error
20+
import { Output } from "./output"
10021

10122
ensurePluginRuntime()
10223

@@ -194,6 +115,7 @@ Effect.gen(function* () {
194115
}).pipe(Effect.andThen(Effect.failCause(cause))),
195116
),
196117
Effect.annotateLogs({ role: "cli" }),
118+
Effect.provideService(Console.Console, Output.console),
197119
Effect.provide(Config.layer),
198120
Effect.provide(Updater.layer),
199121
Effect.provide(
@@ -216,6 +138,7 @@ Effect.gen(function* () {
216138
),
217139
Effect.provide(NodeServices.layer),
218140
Effect.scoped,
141+
Effect.ensuring(Effect.promise(Output.flush)),
219142
Effect.tap(() => Effect.sync(() => process.exit(process.exitCode ?? 0))),
220143
// runMain's default reporter logs the fatal cause to stdout. Write it to stderr instead: the
221144
// desktop and `Service.ensure` only capture stderr from `serve --service`, so this is the only

‎packages/cli/src/output.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Console } from "effect"
2+
import { format } from "node:util"
3+
4+
// Bun's console drops whatever does not fit once a pipe is full, so `--completions fish` (~68 KiB)
5+
// stopped at 65536 bytes. Stream writes queue instead, but `process.exit` discards the queue unless
6+
// the last write has called back. An empty write calls back immediately, so the last real write per
7+
// stream is kept and awaited.
8+
// See https://github.com/anomalyco/opencode/issues/55525
9+
const pending = { stdout: Promise.resolve(), stderr: Promise.resolve() }
10+
11+
function write(stream: keyof typeof pending, args: ReadonlyArray<unknown>) {
12+
pending[stream] = new Promise((resolve) => process[stream].write(format(...args) + "\n", () => resolve()))
13+
}
14+
15+
export const console: Console.Console = Object.assign(Object.create(globalThis.console), {
16+
log: (...args: ReadonlyArray<unknown>) => write("stdout", args),
17+
info: (...args: ReadonlyArray<unknown>) => write("stdout", args),
18+
debug: (...args: ReadonlyArray<unknown>) => write("stdout", args),
19+
warn: (...args: ReadonlyArray<unknown>) => write("stderr", args),
20+
error: (...args: ReadonlyArray<unknown>) => write("stderr", args),
21+
})
22+
23+
export const flush = () => Promise.all([pending.stdout, pending.stderr])
24+
25+
export * as Output from "./output"

‎packages/cli/src/stdout-drain.test.ts‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎packages/cli/test/output.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "node:path"
3+
import { isolatedEnv } from "./fixture/environment"
4+
import { tmpdir } from "./fixture/tmpdir"
5+
6+
// Bun's spawn pipes are larger than an OS pipe and hide truncation, so output goes through a real
7+
// pipe built by `sh`. https://github.com/anomalyco/opencode/issues/55525
8+
async function sh(root: string, command: string) {
9+
const child = Bun.spawn(["sh", "-c", command], {
10+
env: { ...isolatedEnv(root), BUN: process.execPath, CLI: path.join(import.meta.dir, "../src/index.ts") },
11+
stdout: "pipe",
12+
stderr: "ignore",
13+
})
14+
const stdout = await new Response(child.stdout).text()
15+
return { stdout, exitCode: await child.exited }
16+
}
17+
18+
describe.skipIf(process.platform === "win32")("cli output", () => {
19+
test("delivers output larger than a pipe buffer to a slow reader before exit", async () => {
20+
await using directory = await tmpdir()
21+
const file = await sh(directory.path, `"$BUN" "$CLI" --completions fish > "$HOME/fish" && cat "$HOME/fish"`)
22+
const piped = await sh(directory.path, `"$BUN" "$CLI" --completions fish | (sleep 2; cat)`)
23+
24+
expect(Buffer.byteLength(file.stdout)).toBeGreaterThan(65536)
25+
expect(piped).toEqual({ stdout: file.stdout, exitCode: 0 })
26+
}, 60_000)
27+
28+
test("prints short output and exits", async () => {
29+
await using directory = await tmpdir()
30+
const result = await sh(directory.path, `"$BUN" "$CLI" --version | cat`)
31+
32+
expect(result.exitCode).toBe(0)
33+
expect(result.stdout).toStartWith("opencode ")
34+
}, 60_000)
35+
})

0 commit comments

Comments
 (0)