Skip to content

Commit 486ba94

Browse files
fix(script): the prompt probe stops only its own process tree and fails fast
Review of 2d87d50198 (prompt-probe): - taskkill /T /F ran even after the child had exited, so a reused PID could take down an unrelated process tree. The tree is stopped only while the child still runs. - An early exit without a request waited out the full 240 s timeout; it now fails at once with the child's stderr. - --config was parsed after the capture server and temp dir were created, so a bad value leaked both; it is parsed before anything starts. Ref LDE-40, anomalyco#47 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHX6CpBR14wnFxJLDahwTr
1 parent 2b3683d commit 486ba94

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎packages/opencode/script/prompt-probe.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const bytes = (value: unknown) => Buffer.byteLength(typeof value === "string" ?
3939
const k = (n: number) => (n >= 1e6 ? `${(n / 1e6).toFixed(2)}M` : n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : `${n}`)
4040
const tok = (n: number) => k(Math.round(n * ratio))
4141

42+
// Parsed before anything is started, so a bad --config leaks no server or temp dir.
43+
const extraConfig: Record<string, unknown> = args.config ? JSON.parse(args.config) : {}
44+
const capturedModel = "capture/probe"
45+
4246
async function capture(dir: string) {
4347
let body: any
4448
const got = Promise.withResolvers<void>()
@@ -66,7 +70,6 @@ async function capture(dir: string) {
6670
},
6771
})
6872
const temp = mkdtempSync(path.join(tmpdir(), "prompt-probe-"))
69-
const extraConfig: Record<string, unknown> = args.config ? JSON.parse(args.config) : {}
7073
const config = {
7174
provider: {
7275
capture: {
@@ -76,8 +79,8 @@ async function capture(dir: string) {
7679
models: { probe: { name: "Probe", tool_call: true, limit: { context: 1_048_576, output: 131_072 } } },
7780
},
7881
},
79-
model: "capture/probe",
80-
small_model: "capture/probe",
82+
model: capturedModel,
83+
small_model: capturedModel,
8184
// --config: extra opencode.json content to measure a change, e.g.
8285
// '{"permission":{"linear_*":"deny"}}' or a skill allowlist
8386
...extraConfig,
@@ -90,7 +93,7 @@ async function capture(dir: string) {
9093
name,
9194
{
9295
...((extraConfig.agent as Record<string, object> | undefined)?.[name] ?? {}),
93-
model: "capture/probe",
96+
model: capturedModel,
9497
},
9598
]),
9699
),
@@ -122,20 +125,24 @@ async function capture(dir: string) {
122125
let stderr = ""
123126
child.stderr?.on("data", (data) => (stderr += data.toString()))
124127
const exited = new Promise<void>((done) => child.once("exit", () => done()))
128+
// an early exit without a request fails now, not after the full timeout
129+
exited.then(() => got.reject(new Error(`opencode run exited before sending a request\n${stderr.slice(-2000)}`)))
125130
const timer = setTimeout(
126131
() => got.reject(new Error(`no request within ${args.timeout} ms\n${stderr.slice(-2000)}`)),
127132
Number(args.timeout),
128133
)
129134
try {
130-
await Promise.race([got.promise, exited.then(() => got.promise)])
135+
await got.promise
131136
} finally {
132137
clearTimeout(timer)
133-
// the run starts MCP servers as its own children: stop the whole tree
134-
if (process.platform === "win32" && child.pid)
138+
// The run starts MCP servers as its own children: stop the whole tree, and
139+
// only while the child still runs, so a reused PID is never targeted.
140+
const running = child.exitCode === null && child.signalCode === null
141+
if (running && process.platform === "win32" && child.pid)
135142
await new Promise((done) =>
136143
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" }).once("exit", done),
137144
)
138-
else child.kill()
145+
if (running && process.platform !== "win32") child.kill()
139146
await Promise.race([exited, new Promise((done) => setTimeout(done, 5000))])
140147
server.stop(true)
141148
try {

0 commit comments

Comments
 (0)