Skip to content

Commit 463ae65

Browse files
Merge branch 'fix/muse-token-followups' into dev
anomalyco#47 follow-ups: replay counts reasoning toward the threshold; prompt-probe stops the whole process tree on macOS/Linux. Architect-reviewed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHX6CpBR14wnFxJLDahwTr
2 parents cd14172 + a794c46 commit 463ae65

5 files changed

Lines changed: 156 additions & 11 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Stopping a child process and everything it started (#47, prompt-probe).
2+
//
3+
// `opencode run` starts MCP servers as its own children. Killing only the run
4+
// leaves them running, holding the temp directory and, for a stdio server, its
5+
// pipes. On Windows `taskkill /T` walks the tree. On macOS and Linux the child
6+
// is spawned as the leader of its own process group, and the whole group is
7+
// signalled. Either way only while the child still runs, so a PID the system
8+
// has since reused is never targeted.
9+
10+
import { spawn, type ChildProcess } from "node:child_process"
11+
12+
/** Spawn options that let stop() reach the child's whole tree. */
13+
export function spawnOptions(platform: NodeJS.Platform = process.platform) {
14+
// detached on Windows would open a console window for the child; taskkill does not need it
15+
return { detached: platform !== "win32" }
16+
}
17+
18+
export type Plan = { kind: "taskkill"; args: string[] } | { kind: "group"; pgid: number }
19+
20+
/** How to stop a child and its descendants, or undefined when there is nothing safe to target. */
21+
export function plan(input: {
22+
platform: NodeJS.Platform
23+
pid: number | undefined
24+
running: boolean
25+
}): Plan | undefined {
26+
if (!input.running || input.pid === undefined) return undefined
27+
if (input.platform === "win32") return { kind: "taskkill", args: ["/pid", String(input.pid), "/T", "/F"] }
28+
return { kind: "group", pgid: input.pid }
29+
}
30+
31+
/**
32+
* Stops a child spawned with spawnOptions() and waits for it to exit, at most
33+
* `graceMs`. On POSIX the group gets SIGTERM, and SIGKILL if the child has not
34+
* exited by then.
35+
*/
36+
export async function stop(child: ChildProcess, graceMs = 5000) {
37+
const exited =
38+
child.exitCode !== null || child.signalCode !== null
39+
? Promise.resolve()
40+
: new Promise<void>((done) => child.once("exit", () => done()))
41+
const step = plan({
42+
platform: process.platform,
43+
pid: child.pid,
44+
running: child.exitCode === null && child.signalCode === null,
45+
})
46+
if (step?.kind === "taskkill")
47+
await new Promise((done) => spawn("taskkill", step.args, { stdio: "ignore" }).once("exit", done))
48+
if (step?.kind === "group") signal(step.pgid, "SIGTERM")
49+
await Promise.race([exited, new Promise((done) => setTimeout(done, graceMs))])
50+
if (step?.kind === "group" && child.exitCode === null && child.signalCode === null) {
51+
signal(step.pgid, "SIGKILL")
52+
await Promise.race([exited, new Promise((done) => setTimeout(done, graceMs))])
53+
}
54+
}
55+
56+
function signal(pgid: number, name: NodeJS.Signals) {
57+
try {
58+
// a negative PID signals the whole process group
59+
process.kill(-pgid, name)
60+
} catch {
61+
// ESRCH: the group is already gone
62+
}
63+
}
64+
65+
export * as ProcessTree from "./process-tree"

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { mkdtempSync, rmSync } from "node:fs"
1818
import { tmpdir } from "node:os"
1919
import path from "node:path"
2020
import { parseArgs } from "node:util"
21+
import { ProcessTree } from "./process-tree"
2122

2223
const { values: args } = parseArgs({
2324
options: {
@@ -120,6 +121,8 @@ async function capture(dir: string) {
120121
OPENCODE_DB: ":memory:",
121122
},
122123
stdio: ["ignore", "ignore", "pipe"],
124+
// the run's MCP servers are its children: stop() reaches them through this
125+
...ProcessTree.spawnOptions(),
123126
},
124127
)
125128
let stderr = ""
@@ -137,13 +140,7 @@ async function capture(dir: string) {
137140
clearTimeout(timer)
138141
// The run starts MCP servers as its own children: stop the whole tree, and
139142
// 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)
142-
await new Promise((done) =>
143-
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" }).once("exit", done),
144-
)
145-
if (running && process.platform !== "win32") child.kill()
146-
await Promise.race([exited, new Promise((done) => setTimeout(done, 5000))])
143+
await ProcessTree.stop(child)
147144
server.stop(true)
148145
try {
149146
rmSync(temp, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 })

‎packages/opencode/script/token-usage.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ export interface Projection {
227227
* that step); for a full cache miss it is the whole simulated prompt, scaled by
228228
* the actual miss ratio; cache writes keep their actual size where they fit.
229229
* Compaction follows the product: before each request, overThreshold (the
230-
* product's function) looks at the last finished request's prompt plus output,
231-
* with the re-compaction floor reset by every summary, real or simulated.
230+
* product's function) looks at the last finished request's tokens.total (prompt,
231+
* output and reasoning), with the re-compaction floor reset by every summary, real or simulated.
232232
* A simulated compaction costs one summary request with the history sent
233233
* uncached (as observed: summary requests read no cache; the whole simulated
234234
* prompt is charged, which overstates it, since observed summaries send about
@@ -253,7 +253,7 @@ export function project(steps: ReadonlyArray<Step>, lever: Lever, options: Proje
253253
let carried = 0
254254
let missNext = false
255255
// What the product looks at before each request: the last finished request's
256-
// tokens (prompt + output), and the floor its re-compaction guard uses, the
256+
// tokens (prompt + output + reasoning), and the floor its re-compaction guard uses, the
257257
// first request after the latest summary (real or simulated).
258258
let last: { tokens: SessionV1.Assistant["tokens"]; summary: boolean } | undefined
259259
let floor: number | undefined
@@ -276,14 +276,18 @@ export function project(steps: ReadonlyArray<Step>, lever: Lever, options: Proje
276276
total.cacheWrite += request.write
277277
total.output += request.output
278278
total.reasoning += request.reasoning
279+
// the product counts tokens.total (the provider's totalTokens: prompt, output
280+
// and reasoning), both for the threshold and for the guard's floor
281+
const count = request.size + request.output + request.reasoning
279282
const tokens = {
283+
total: count,
280284
input: request.uncached,
281285
output: request.output,
282286
reasoning: request.reasoning,
283287
cache: { read, write: request.write },
284288
}
285289
if (floorPending && !request.summary) {
286-
floor = request.size + request.output
290+
floor = count
287291
floorPending = false
288292
}
289293
last = { tokens, summary: request.summary }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { spawn } from "node:child_process"
2+
import { describe, expect, test } from "bun:test"
3+
import { ProcessTree } from "../../script/process-tree"
4+
5+
describe("ProcessTree.plan", () => {
6+
test("Windows: taskkill walks the tree from the child", () => {
7+
expect(ProcessTree.plan({ platform: "win32", pid: 42, running: true })).toEqual({
8+
kind: "taskkill",
9+
args: ["/pid", "42", "/T", "/F"],
10+
})
11+
})
12+
13+
test("macOS and Linux: the child's whole process group is signalled, not the child alone", () => {
14+
expect(ProcessTree.plan({ platform: "linux", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 })
15+
expect(ProcessTree.plan({ platform: "darwin", pid: 42, running: true })).toEqual({ kind: "group", pgid: 42 })
16+
})
17+
18+
test("a child that has exited is never targeted: its PID may be reused", () => {
19+
expect(ProcessTree.plan({ platform: "win32", pid: 42, running: false })).toBeUndefined()
20+
expect(ProcessTree.plan({ platform: "linux", pid: 42, running: false })).toBeUndefined()
21+
expect(ProcessTree.plan({ platform: "linux", pid: undefined, running: true })).toBeUndefined()
22+
})
23+
24+
test("on macOS and Linux the child leads its own group; Windows gets no console window", () => {
25+
expect(ProcessTree.spawnOptions("linux").detached).toBe(true)
26+
expect(ProcessTree.spawnOptions("darwin").detached).toBe(true)
27+
expect(ProcessTree.spawnOptions("win32").detached).toBe(false)
28+
})
29+
})
30+
31+
const alive = (pid: number) => {
32+
try {
33+
process.kill(pid, 0)
34+
return true
35+
} catch {
36+
return false
37+
}
38+
}
39+
40+
describe("ProcessTree.stop", () => {
41+
// Runs the real path for this platform: taskkill on Windows, the group signal elsewhere.
42+
test("stops the child and the process it started", async () => {
43+
const script = `const g = Bun.spawn([process.execPath, "-e", "setTimeout(() => {}, 60000)"]); console.log(g.pid); setTimeout(() => {}, 60000)`
44+
const child = spawn(process.execPath, ["-e", script], {
45+
...ProcessTree.spawnOptions(),
46+
stdio: ["ignore", "pipe", "ignore"],
47+
})
48+
const grandchild = await new Promise<number>((done) =>
49+
child.stdout!.once("data", (data) => done(Number(data.toString().trim()))),
50+
)
51+
expect(alive(grandchild)).toBe(true)
52+
53+
await ProcessTree.stop(child, 5000)
54+
55+
expect(child.exitCode !== null || child.signalCode !== null).toBe(true)
56+
const deadline = Date.now() + 5000
57+
while (alive(grandchild) && Date.now() < deadline) await Bun.sleep(100)
58+
expect(alive(grandchild)).toBe(false)
59+
}, 30_000)
60+
})

‎packages/opencode/test/script/token-usage.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ describe("TokenUsage.project", () => {
150150
expect(TokenUsage.project(under, { compactAt: 100_000 }, options).compactions).toBe(0)
151151
})
152152

153+
test("reasoning counts toward the threshold, as tokens.total does in the product", () => {
154+
clock = 0
155+
// 95_000 prompt + 500 output + 4_500 reasoning = 100_000: the product's total reaches the threshold
156+
const steps = [step({ prompt: 95_000, reasoning: 4_500 }), step({ prompt: 95_100 })]
157+
expect(TokenUsage.project(steps, { compactAt: 100_000 }, options).compactions).toBe(1)
158+
clock = 0
159+
const under = [step({ prompt: 95_000, reasoning: 4_499 }), step({ prompt: 95_100 })]
160+
expect(TokenUsage.project(under, { compactAt: 100_000 }, options).compactions).toBe(0)
161+
})
162+
163+
test("the guard's floor counts reasoning too, as floorAfterCompaction does", () => {
164+
clock = 0
165+
// every request counts 100_000 + 500 + 50_000 = 150_500. After the first compaction the floor is
166+
// that same 150_500, so the next needs 200_500 and never comes; a floor without reasoning
167+
// (100_500) would let every later request compact again.
168+
const steps = Array.from({ length: 5 }, () => step({ prompt: 100_000, reasoning: 50_000 }))
169+
expect(TokenUsage.project(steps, { compactAt: 100_000 }, options).compactions).toBe(1)
170+
})
171+
153172
test("the product's guard: after a compaction the next one needs half a threshold over the first request after it", () => {
154173
clock = 0
155174
// 40 steps growing 10k each from 100k; the kept prompt (120k) is itself over the threshold

0 commit comments

Comments
 (0)