Skip to content

Commit 5b8860a

Browse files
committed
fix(opencode): wait for stdout writes before exit so piped JSON is not truncated
export, session list --format json and db --format json wrote their JSON and returned; src/index.ts then calls process.exit() and, when stdout is a pipe, anything still queued past the pipe buffer is dropped with exit code 0. Wait for the write callback (as generate.ts already does) before returning, and treat EPIPE as done so | head exits cleanly. Closes anomalyco#29330
1 parent b471c2b commit 5b8860a

5 files changed

Lines changed: 102 additions & 7 deletions

File tree

‎packages/opencode/src/cli/cmd/db.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Database } from "@opencode-ai/core/database/database"
44
import { Effect } from "effect"
55
import { sql } from "drizzle-orm"
66
import { effectCmd } from "../effect-cmd"
7+
import { writeStdout } from "../stdout"
78

89
const QueryCommand = effectCmd({
910
command: "$0 [query]",
@@ -27,7 +28,7 @@ const QueryCommand = effectCmd({
2728
if (query) {
2829
const { db } = yield* Database.Service
2930
const result = yield* db.all<Record<string, unknown>>(sql.raw(query)).pipe(Effect.orDie)
30-
if (args.format === "json") console.log(JSON.stringify(result, null, 2))
31+
if (args.format === "json") yield* writeStdout(JSON.stringify(result, null, 2) + "\n")
3132
else if (result.length > 0) {
3233
const keys = Object.keys(result[0])
3334
console.log(keys.join("\t"))

‎packages/opencode/src/cli/cmd/export.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SessionV1 } from "@opencode-ai/core/v1/session"
33
import { MessageV2 } from "../../session/message-v2"
44
import { SessionID } from "../../session/schema"
55
import { effectCmd, fail } from "../effect-cmd"
6+
import { writeStdout } from "../stdout"
67
import { UI } from "../ui"
78
import * as prompts from "@clack/prompts"
89
import { EOL } from "os"
@@ -280,13 +281,12 @@ const run = Effect.fn("Cli.export.body")(function* (args: { sessionID?: string;
280281

281282
// Match legacy try/catch — catches both typed failures and defects
282283
// (Session.Service.get throws NotFoundError as a defect, not a typed E).
283-
return yield* Effect.gen(function* () {
284+
const exportData = yield* Effect.gen(function* () {
284285
const sessionInfo = yield* svc.get(sessionID!)
285286
const messages = yield* svc.messages({ sessionID: sessionInfo.id })
286287

287-
const exportData = { info: sessionInfo, messages }
288-
289-
process.stdout.write(JSON.stringify(args.sanitize ? sanitize(exportData) : exportData, null, 2))
290-
process.stdout.write(EOL)
288+
return { info: sessionInfo, messages }
291289
}).pipe(Effect.catchCause(() => fail(`Session not found: ${sessionID!}`)))
290+
291+
yield* writeStdout(JSON.stringify(args.sanitize ? sanitize(exportData) : exportData, null, 2) + EOL)
292292
})

‎packages/opencode/src/cli/cmd/session.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Argv } from "yargs"
22
import { Effect } from "effect"
33
import { cmd } from "./cmd"
44
import { effectCmd, fail } from "../effect-cmd"
5+
import { writeStdout } from "../stdout"
56
import { Session } from "@/session/session"
67
import { SessionID } from "../../session/schema"
78
import { UI } from "../ui"
@@ -110,7 +111,7 @@ export const SessionListCommand = effectCmd({
110111
await proc.exited
111112
})
112113
} else {
113-
console.log(output)
114+
yield* writeStdout(output + "\n")
114115
}
115116
}),
116117
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Effect } from "effect"
2+
import { CliError } from "./effect-cmd"
3+
4+
// stdout is asynchronous when it is a pipe, and src/index.ts terminates
5+
// as soon as a command returns, so anything still queued on the stream is lost
6+
// (#29330). Wait for the write callback like generate.ts does. EPIPE means the
7+
// reader went away (`| head`) and counts as done; the 'error' event that follows a
8+
// failed write keeps its listener so it cannot crash the process.
9+
export const writeStdout = (text: string) =>
10+
Effect.tryPromise({
11+
try: () =>
12+
new Promise<void>((resolve, reject) => {
13+
const settle = (err?: Error | null) => {
14+
const code = (err as NodeJS.ErrnoException | null | undefined)?.code
15+
return !err || code === "EPIPE" ? resolve() : reject(err)
16+
}
17+
process.stdout.once("error", settle)
18+
process.stdout.write(text, settle)
19+
}),
20+
catch: (cause) => new CliError({ message: `failed to write to stdout: ${String(cause)}` }),
21+
})
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { afterEach, beforeEach, expect, test } from "bun:test"
2+
import { mkdtemp, rm } from "node:fs/promises"
3+
import { tmpdir } from "node:os"
4+
import path from "node:path"
5+
6+
const QUERY =
7+
"with recursive t(n) as (select 1 union all select n+1 from t where n < 15000) select n as n, '0123456789012345678901234567890123456789' as pad from t"
8+
const ROOT = path.resolve(import.meta.dir, "../..")
9+
10+
let tmp: string
11+
12+
beforeEach(async () => {
13+
tmp = await mkdtemp(path.join(tmpdir(), "opencode-stdout-"))
14+
})
15+
16+
afterEach(async () => {
17+
await rm(tmp, { recursive: true, force: true })
18+
})
19+
20+
function spawn(stdout: "pipe"): Bun.Subprocess<"ignore", "pipe", "pipe">
21+
function spawn(stdout: ReturnType<typeof Bun.file>): Bun.Subprocess<"ignore", ReturnType<typeof Bun.file>, "pipe">
22+
function spawn(stdout: "pipe" | ReturnType<typeof Bun.file>) {
23+
const isolated = {
24+
OPENCODE_TEST_HOME: tmp,
25+
HOME: tmp,
26+
XDG_CONFIG_HOME: path.join(tmp, ".config"),
27+
XDG_DATA_HOME: path.join(tmp, ".local/share"),
28+
XDG_STATE_HOME: path.join(tmp, ".local/state"),
29+
XDG_CACHE_HOME: path.join(tmp, ".cache"),
30+
OPENCODE_CONFIG_CONTENT: "{}",
31+
OPENCODE_DISABLE_PROJECT_CONFIG: "1",
32+
OPENCODE_PURE: "1",
33+
OPENCODE_DISABLE_AUTOUPDATE: "1",
34+
OPENCODE_DISABLE_AUTOCOMPACT: "1",
35+
OPENCODE_DISABLE_MODELS_FETCH: "1",
36+
OPENCODE_AUTH_CONTENT: "{}",
37+
}
38+
return Bun.spawn([process.execPath, "src/index.ts", "db", "--format", "json", QUERY], {
39+
cwd: ROOT,
40+
stdout,
41+
stderr: "pipe",
42+
env: { ...process.env, ...isolated },
43+
})
44+
}
45+
46+
// Delay the first read until the pipe fills so returning before stdout drains
47+
// deterministically exposes truncated output.
48+
test("large JSON output is complete when the reader is slow", async () => {
49+
const proc = spawn("pipe")
50+
await Bun.sleep(2000)
51+
const text = await new Response(proc.stdout).text()
52+
await proc.exited
53+
expect(proc.exitCode).toBe(0)
54+
expect(text.length).toBeGreaterThan(65536)
55+
expect(JSON.parse(text)).toHaveLength(15000)
56+
57+
const file = path.join(tmp, "full.json")
58+
const ctl = spawn(Bun.file(file))
59+
await ctl.exited
60+
expect(text).toBe(await Bun.file(file).text())
61+
}, 60_000)
62+
63+
test("exits cleanly when the reader goes away", async () => {
64+
const proc = spawn("pipe")
65+
const reader = proc.stdout.getReader()
66+
await reader.read()
67+
await reader.cancel()
68+
const code = await Promise.race([proc.exited, Bun.sleep(5000).then(() => "timeout")])
69+
expect(code).toBe(0)
70+
const err = await new Response(proc.stderr).text()
71+
expect(err).not.toMatch(/EPIPE|Unhandled/)
72+
}, 60_000)

0 commit comments

Comments
 (0)