Skip to content

Commit 04da0fa

Browse files
fix: tool list --json writes whole too, and a reader that leaves early isn't an error
`tool list --json` is over 64 KiB and could be cut the same way. Stopping at EPIPE keeps `| head` exiting 0, as it did before the whole-answer write. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YNQjkaiykbC1WtASw6Yegs
1 parent 1018bb8 commit 04da0fa

2 files changed

Lines changed: 78 additions & 37 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const ToolListCommand = cmd({
128128
const workspaceTools = await resolveWorkspaceTools(args["workspace-tools"], process.cwd())
129129
const tools = await withRunner(process.cwd(), args.connections, workspaceTools, (runner) => runner.list())
130130
if (args.json) {
131-
console.log(JSON.stringify(tools, null, 2))
131+
writeAll(JSON.stringify(tools, null, 2) + "\n")
132132
return
133133
}
134134
const width = Math.max(...tools.map((tool) => tool.id.length))
@@ -299,15 +299,18 @@ const ToolDaemonCommand = cmd({
299299
/**
300300
* All of a machine-read answer on stdout before returning. The CLI exits right after a command, and
301301
* console.log leaves what a pipe can't take yet in a buffer that exit throws away, which cuts off an
302-
* answer over the pipe's 64 KiB when its reader is slow. Only metadata gets that big: `output` is capped.
302+
* answer over the pipe's 64 KiB when its reader is slow. `tool run`'s `output` is capped, so there only
303+
* metadata gets that big.
303304
*/
304305
function writeAll(text: string) {
305306
const bytes = Buffer.from(text)
306307
for (let at = 0; at < bytes.length; ) {
307308
try {
308309
at += writeSync(1, bytes, at)
309310
} catch (error) {
310-
if ((error as NodeJS.ErrnoException).code !== "EAGAIN") throw error
311+
const code = (error as NodeJS.ErrnoException).code
312+
if (code === "EPIPE") return // The reader left, so nobody is waiting for the rest.
313+
if (code !== "EAGAIN") throw error
311314
Bun.sleepSync(2)
312315
}
313316
}

‎packages/opencode/test/altimate/tool-cli/cli.test.ts‎

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,57 @@ import { tmpdir } from "node:os"
44
import path from "node:path"
55

66
const entry = path.join(import.meta.dir, "../../../src/index.ts")
7+
const cliEnv = { ...process.env, ALTIMATE_TELEMETRY_DISABLED: "1", ALTIMATE_TOOL_DAEMON: "0" }
8+
9+
/** A manifest whose hub has 450 readers, so its `dbt_lineage` graph is far over a pipe's 64 KiB. */
10+
async function bigAnswerProject() {
11+
const dir = await mkdtemp(path.join(tmpdir(), "tool-cli-"))
12+
const model = (name: string, deps: string[]) => ({
13+
resource_type: "model",
14+
unique_id: `model.shop.${name}`,
15+
name,
16+
alias: name,
17+
database: "shop",
18+
schema: "main",
19+
original_file_path: `models/${name}.sql`,
20+
depends_on: { nodes: deps.map((dep) => `model.shop.${dep}`) },
21+
config: { materialized: "table" },
22+
compiled_code: "select 1 as id",
23+
columns: {},
24+
})
25+
const readers = Array.from({ length: 450 }, (_, i) => model(`reader_with_a_longish_name_${i}`, ["hub"]))
26+
const nodes = [model("hub", []), ...readers]
27+
const manifest = path.join(dir, "manifest.json")
28+
await writeFile(
29+
manifest,
30+
JSON.stringify({
31+
metadata: { adapter_type: "duckdb" },
32+
nodes: Object.fromEntries(nodes.map((node) => [node.unique_id, node])),
33+
sources: {},
34+
exposures: {},
35+
}),
36+
)
37+
await writeFile(path.join(dir, "args.json"), JSON.stringify({ manifest_path: manifest, model: "hub", include_graph: true }))
38+
return dir
39+
}
40+
41+
/**
42+
* Runs the CLI with a reader that waits for it to exit, up to 20 s, as a busy event loop can: the pipe is full
43+
* until then. A CLI that drops what the pipe can't take exits at once and leaves 64 KiB; one that writes it all waits.
44+
*/
45+
async function lateReader(dir: string, command: string) {
46+
const proc = Bun.spawn(
47+
[
48+
"sh",
49+
"-c",
50+
`{ "$0" --conditions=browser "$1" ${command}; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; }`,
51+
process.execPath,
52+
entry,
53+
],
54+
{ cwd: dir, stderr: "ignore", env: cliEnv },
55+
)
56+
return proc.exited
57+
}
758

859
async function cli(args: string[], stdin?: string) {
960
const dir = await mkdtemp(path.join(tmpdir(), "tool-cli-"))
@@ -30,49 +81,36 @@ describe("tool CLI", () => {
3081

3182
// Past the 64 KiB a pipe holds, a process that exits with the rest still buffered loses it.
3283
test("a big --json answer reaches a reader that starts late whole", async () => {
84+
const dir = await bigAnswerProject()
85+
expect(await lateReader(dir, "tool run dbt_lineage --args - --json < args.json")).toBe(0)
86+
const stdout = await Bun.file(path.join(dir, "out.json")).text()
87+
expect(stdout.length).toBeGreaterThan(100_000)
88+
expect(Object.keys(JSON.parse(stdout).metadata.graph.nodes).length).toBe(451)
89+
}, 60000)
90+
91+
test("tool list --json reaches a reader that starts late whole", async () => {
3392
const dir = await mkdtemp(path.join(tmpdir(), "tool-cli-"))
34-
const model = (name: string, deps: string[]) => ({
35-
resource_type: "model",
36-
unique_id: `model.shop.${name}`,
37-
name,
38-
alias: name,
39-
database: "shop",
40-
schema: "main",
41-
original_file_path: `models/${name}.sql`,
42-
depends_on: { nodes: deps.map((dep) => `model.shop.${dep}`) },
43-
config: { materialized: "table" },
44-
compiled_code: "select 1 as id",
45-
columns: {},
46-
})
47-
const readers = Array.from({ length: 450 }, (_, i) => model(`reader_with_a_longish_name_${i}`, ["hub"]))
48-
const nodes = [model("hub", []), ...readers]
49-
const manifest = path.join(dir, "manifest.json")
50-
await writeFile(
51-
manifest,
52-
JSON.stringify({
53-
metadata: { adapter_type: "duckdb" },
54-
nodes: Object.fromEntries(nodes.map((node) => [node.unique_id, node])),
55-
sources: {},
56-
exposures: {},
57-
}),
58-
)
59-
await writeFile(path.join(dir, "args.json"), JSON.stringify({ manifest_path: manifest, model: "hub", include_graph: true }))
60-
// The reader waits for the CLI to exit, up to 20 s, as a busy event loop can: the pipe is full until then.
61-
// A CLI that drops what the pipe can't take exits at once and leaves 64 KiB; one that writes it all waits.
93+
expect(await lateReader(dir, "tool list --json")).toBe(0)
94+
const stdout = await Bun.file(path.join(dir, "out.json")).text()
95+
expect(stdout.length).toBeGreaterThan(65_536)
96+
expect((JSON.parse(stdout) as unknown[]).length).toBeGreaterThan(0)
97+
}, 60000)
98+
99+
test("a reader that leaves early is not an error", async () => {
100+
const dir = await bigAnswerProject()
62101
const proc = Bun.spawn(
63102
[
64103
"sh",
65104
"-c",
66-
`{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; }`,
105+
`{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json 2> err.txt; echo $? > code.txt; } | head -c 10 > /dev/null`,
67106
process.execPath,
68107
entry,
69108
],
70-
{ cwd: dir, stderr: "ignore", env: { ...process.env, ALTIMATE_TELEMETRY_DISABLED: "1", ALTIMATE_TOOL_DAEMON: "0" } },
109+
{ cwd: dir, stderr: "ignore", env: cliEnv },
71110
)
72-
expect(await proc.exited).toBe(0)
73-
const stdout = await Bun.file(path.join(dir, "out.json")).text()
74-
expect(stdout.length).toBeGreaterThan(100_000)
75-
expect(Object.keys(JSON.parse(stdout).metadata.graph.nodes).length).toBe(451)
111+
await proc.exited
112+
expect((await Bun.file(path.join(dir, "code.txt")).text()).trim()).toBe("0")
113+
expect(await Bun.file(path.join(dir, "err.txt")).text()).toBe("")
76114
}, 60000)
77115

78116
test("rejects --args - with empty stdin", async () => {

0 commit comments

Comments
 (0)