@oakgreencc/cambium 0.0.19 → 0.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -38,7 +38,7 @@ Every command takes `--config <file>`; the default is `$CAMBIUM_CONFIG`, then `~
38
38
  Each step is bounded by `stepTimeoutSeconds`. A step that throws or times out is logged and recorded in the heartbeat, and the tick moves on. Nothing waits on a worker.
39
39
 
40
40
  1. **Read.** One GraphQL query per repo (`graft graphql`): open issues with any `q:` label, open and recently merged PRs on `agent/*` with their check rollup, merge-queue state and the issues they close. GitHub links closing references only for a PR into the default branch, so cambium also reads `Closes/Fixes/Resolves #n` from the PR body, fetching issues the first query did not read in one more. A worker's PR that names no issue (found by its branch) gets `Closes #n` added at submission.
41
- 2. **Reconcile.** For each tracked worker: alive and under its wall-clock cap → nothing. Over the cap → kill its process group. Gone → exit check (a PR on its branch, or closing its issue, that is open or merged and not still on the commit CI already failed) → `submitted`; otherwise requeue with the stderr tail (a failed prepare step requeues as `spawn-failed`). Then the worktree is torn down if `graft status` says GitHub holds everything in it, else kept with a `warn` saying why.
41
+ 2. **Reconcile.** For each tracked worker: alive and under its wall-clock cap → nothing. Over the cap → kill its process group and every descendant (Claude Code runs each Bash command in a group of its own, so a group kill alone left a test run loading the next attempt). Gone → exit check (a PR on its branch, or closing its issue, that is open or merged and not still on the commit CI already failed) → `submitted`; otherwise requeue with the stderr tail (a failed prepare step requeues as `spawn-failed`). Then the worktree is torn down if `graft status` says GitHub holds everything in it, else kept with a `warn` saying why.
42
42
  3. **Watch** (no agent), for agent PRs whose issue is in no queue: merged → close the issue, delete the branch; red → requeue with the failing checks' summaries; left the merge queue without merging → requeue; not merged `mergeQueueMinutes` after submission → `q:human`.
43
43
  4. **Sweep.** An issue labelled `running` with this host (or no host) in its marker and no tracked worker → requeue. This is what makes losing the SQLite file cost one requeue.
44
44
  5. **Schedule.** Start Task workers for `q:task` issues without `running`, by priority label then age, up to `phases.task.cap` counted from live workers. A task requeued earlier in the same tick can start in that tick. Then, with `engine: "graph"` and `phases.dispatch.cap > 0`, start triage workers for `q:intake` issues the same way ([the dispatch graph](#the-dispatch-graph)).
@@ -81,7 +81,7 @@ A worker is `claude -p --output-format stream-json --verbose --max-turns <maxTur
81
81
 
82
82
  - in its own worktree, by the target repo's [worktree convention](#target-repo-convention);
83
83
  - behind the prepare step: `node src/prepare.mjs -- claude …` runs install-fresh, hydrate and the repo's `prepare` commands in the worktree, writing to the worker's stderr log, then becomes the worker. It runs in the detached process so a long install never blocks a tick;
84
- - detached, leading its own process group (a kill takes its children), stdin from the prompt file;
84
+ - detached, leading its own process group (a kill takes the group and, by a `ps` snapshot, every descendant), stdin from the prompt file;
85
85
  - with `CAMBIUM_REPO`, `CAMBIUM_ISSUE`, `CAMBIUM_PHASE`, `CAMBIUM_BRANCH` set, `GRAFT_APP=<worker.graftApp>` when configured, then `worker.env`, then `TRACEPARENT` (the starting node's span) when [tracing](#tracing) is on;
86
86
  - with its prompt, stdout and stderr at `<paths.logs>/<owner>__<name>/issue-<n>/a<attempt>-<stamp>.{prompt.md,out.log,err.log}`, keeping the newest `logs.keepPerIssue` runs per issue.
87
87
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/cambium",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "The scheduler daemon: reads the q:* queues from GitHub every tick, runs headless claude workers in their own worktrees up to a cap, and makes every queue transition itself.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
package/src/workers.mjs CHANGED
@@ -5,7 +5,7 @@
5
5
  * group, behind the prepare step, with its output in rotated log files on this
6
6
  * host, and {@link Procs} to probe and kill by pid.
7
7
  */
8
- import { spawn } from 'node:child_process'
8
+ import { spawn, spawnSync } from 'node:child_process'
9
9
  import {
10
10
  closeSync,
11
11
  existsSync,
@@ -311,6 +311,40 @@ export function processSpawner({
311
311
  }
312
312
  }
313
313
 
314
+ /**
315
+ * Every pid under `root` in a `pid ppid` process table (`ps -A -o pid=,ppid=`), deepest last.
316
+ * @param {number} root @param {string} table @returns {number[]}
317
+ */
318
+ export function descendants(root, table) {
319
+ /** @type {Map<number, number[]>} */
320
+ const children = new Map()
321
+ for (const line of table.split('\n')) {
322
+ const [pid, ppid] = line.trim().split(/\s+/).map(Number)
323
+ if (!Number.isInteger(pid) || !Number.isInteger(ppid) || pid === ppid) continue
324
+ children.set(ppid, [...(children.get(ppid) ?? []), pid])
325
+ }
326
+ const out = []
327
+ const seen = new Set([root])
328
+ for (let i = -1; i < out.length; i++) {
329
+ for (const c of children.get(i < 0 ? root : out[i]) ?? []) {
330
+ if (seen.has(c)) continue
331
+ seen.add(c)
332
+ out.push(c)
333
+ }
334
+ }
335
+ return out
336
+ }
337
+
338
+ /** @param {number} target */
339
+ function sigkill(target) {
340
+ try {
341
+ process.kill(target, 'SIGKILL')
342
+ return true
343
+ } catch {
344
+ return false
345
+ }
346
+ }
347
+
314
348
  /** @type {Procs} */
315
349
  export const realProcs = {
316
350
  alive(pid) {
@@ -322,12 +356,13 @@ export const realProcs = {
322
356
  }
323
357
  },
324
358
  kill(pid) {
359
+ // The process group is not enough: Claude Code runs each Bash command in a group of its own, so a test
360
+ // run the worker started outlives the group kill and loads the next attempt. Snapshot the tree first
361
+ // (killed children reparent to launchd and drop out of it), then kill it and the group.
362
+ const ps = spawnSync('ps', ['-A', '-o', 'pid=,ppid='], { encoding: 'utf8' })
363
+ const tree = ps.status === 0 ? descendants(pid, ps.stdout) : []
325
364
  // A detached worker leads its own process group: the negative pid takes its children with it.
326
- for (const target of [-pid, pid]) {
327
- try {
328
- process.kill(target, 'SIGKILL')
329
- return
330
- } catch {}
331
- }
365
+ if (!sigkill(-pid)) sigkill(pid)
366
+ for (const p of tree) sigkill(p)
332
367
  },
333
368
  }