Skip to content

stewards: keep a waiting steward while a worker's descendant runs (BB-14) - #33

Merged
trotterdylan merged 5 commits into
mainfrom
bb/bb-14-stewards-waiting-archive-fix-thr_h7xntryc8v
Oct 19, 2026
Merged

trotterdylan merged 5 commits into
mainfrom
bb/bb-14-stewards-waiting-archive-fix-thr_h7xntryc8v

Conversation

@trotterdylan

@trotterdylan trotterdylan commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Implements BB-14. On BB-11 archiveWaiting saw no worker in status running, archived the steward, and bb archived the lifecycle-owned worker and, through it, its running reviewer. The build-tickets loop is normally an idle worker with a running reviewer child, so this hit every owner-reviewed PR.

Merging this moves the bb-server pin on bb-gate and builds a new image. That stages a restart on every developer's server, which happens when they press Restart now or in the 03:00–05:00 ET quiet window.

Classification

no-review: a narrow bug fix that makes archiveWaiting do what it was already meant to do, plus additive wake text. It touches no cross-repo contract (source-contract.ts is unchanged), store migration, dependency or infrastructure.

What changes

  • server.ts: archiveWaiting now refuses to archive while any worker of the binding is unfinished (its record is neither finished nor stopped) or while any worker thread, or any thread beneath a worker (walked from bb with threads.list({ parentThreadId })), is running or pending. The descendant clause catches the idle worker whose reviewer is running. New workerThreads, workersHaveWork and collectWorkerQuestions helpers.
  • server.ts: wake surfaces the questions the binding's finished workers ended their last turn with. A finished-but-archived worker is unarchived when its question is surfaced so the steward's bb thread tell reaches it; a worker whose thread is gone is reported as gone, needing a fresh one. (bb rejects a tell to an archived thread before the message.dispatch hook runs, so the plugin cannot catch the tell itself.)
  • wake.ts: workerQuestion extracts the last non-empty line of a final message that contains ?; formatWake renders **Worker thr_… asked:** … under "What changed" ahead of every other change, with how to answer.
  • server.test.ts: reproduces the BB-11 sequence (steward reports other, idle worker with a running reviewer child, nothing archived, question in the next wake), plus the archived-worker revival and gone-worker report. The fake host now models thread status on idle and parentage on created.
  • DESIGN.md §5 "Waiting": the new archive rule and the lifecycle-owned choice.

Testing

  • npm test in plugins/stewards: pass (110 tests; not in the CI matrix).
  • npm run typecheck in plugins/stewards: clean.
  • Not tested: a real bb thread tell against an archived worker; that path is core's and rejects before plugin hooks, so the plugin instead unarchives at wake time.

— from bb thread thr_h7xntryc8v

…-14)

archiveWaiting only looked at a worker's own record, so a steward whose
worker had ended its turn to wait while its reviewer child ran was
archived, and bb archived the lifecycle-owned worker and, through it, the
reviewer. Do not archive while any worker is unfinished or any thread
beneath a worker is running or pending; keep workers lifecycle-owned and
document the rule in DESIGN.md §5.

A worker's final message that asks a question is now named in the next
wake ("Worker thr_… asked: …") ahead of every other change, with an
instruction to answer via bb thread tell or escalate. A finished-but-
archived worker is brought back when its question is surfaced (bb rejects
a tell to an archived thread before any hook), and a worker whose thread
is gone is reported as gone, needing a fresh one.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Independent review running in bb thread @thread:thr_afbfn6amnw.

— from bb thread thr_h7xntryc8v

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 1 (GLM 5.3, at f6974f2)

Owner review required: no — none of the owner-reviewed triggers apply. This is a narrow bug fix that makes archiveWaiting do what its own doc comment already said (keep a steward with work visible), plus strictly additive wake text, tests and DESIGN.md docs. source-contract.ts and its copies are untouched (contract.test.ts passes), no store migration, no dependency, no settings default, no infra or identity change, and nothing in stewards/project.json's bigDecisions. The PR's Classification line agrees.

Suggestions

  1. server.ts:1471 (workerThreads) / 1495 (workersHaveWork) — both fail open: a threads.list error logs a warning and continues without the children, and threadInfo(...).catch(() => null) treats an erroring lookup as gone, so a transient SDK failure lets archiveWaiting archive over a possibly-running reviewer — the exact BB-11 recurrence this PR exists to prevent. Consider failing safe: treat a non-404 list/get error as "has work" and skip the archive this pass. (The existing .catch(() => null) convention elsewhere in the file is fine for cosmetic reads; this one gates the destructive action.)
  2. server.ts:1535 (collectWorkerQuestions) — the same swallow means a transient non-404 error on threads.get reports a live worker as gone, and the wake tells the steward to start a fresh one — a duplicate worker doing the same work. Telling the two apart (404/410 vs other) would match send/threadInfo's own distinction.
  3. wake.ts:27 (workerQuestion) — scanning every line backwards picks up a ? asked early in a long final message that the message itself later resolves ("Should I do X? — never mind, done."), surfacing it as an open question (and possibly unarchiving the worker for it). Bounding the scan to the last few lines would match the doc comment's "the message the worker ended with".

Checked

  • Ran: npm install, npm run typecheck (clean) and npm test in plugins/stewards — 110 tests, all pass. stewards is not in the plugins workflow's CI matrix (only devbox-provider, github-watcher, html-preview, image-copy, kubernetes-provider, team-setup, thread-briefs, workstreams), so none of the green checks exercised this diff; I ran the suite myself.
  • Verified the BB-11 test is a real regression test: with the base server.ts/wake.ts swapped in, it fails exactly at archivedAt being non-null (the steward gets archived over the idle worker's running child).
  • Verified against ticket:
    • Criterion 1: met — workersHaveWork refuses on any record other than finished/stopped (running, failed) and on any worker or descendant thread whose status is busy (active/starting/pending/stopping), walked recursively from bb.
    • Criterion 2: met — workers stay lifecycle-owned, the plugin stops them only on binding end, and the choice plus the rule are documented in DESIGN.md §5 "Waiting", per the ticket's Spec comment.
    • Criterion 3: met — **Worker thr_… asked:** … renders first under "## 5. What changed", ahead of the changes (the test asserts the ordering), with "answer it with bb thread tell … <answer>, or escalate it, before anything else". Questions live in formatWake's tail, which fit never cuts.
    • Criterion 4: met — a finished-but-archived worker is unarchived when its question is surfaced (bb rejects a tell to an archived thread before plugin hooks, so wake-time revival is the right place; same convention as sendTo reviving an archived steward), and a gone worker is reported plainly with "start a fresh worker". Both paths tested.
    • Criterion 5: met — the first new test reproduces the full BB-11 sequence (waiting report, idle worker with a running child, nothing archived, question in the next wake); a second covers revival and gone.
  • Restart behaviour: no store changes, the new logic is stateless, so a server restart is harmless.

— from bb thread thr_afbfn6amnw

…ting-archive-fix-thr_h7xntryc8v

# Conflicts:
#	plugins/stewards/server.ts
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 2: pushed 05979ce. I took all three suggestions, and merged main (which brought BB-17, "treat a worker as in flight between its turns").

  • Suggestion 1 (fail-open around the archive gate): fixed in 05979ce. workerThreads no longer swallows a threads.list failure; workersHaveWork catches it and returns "has work" so the archive is skipped, and a non-404 threadInfo failure on any worker thread likewise returns "has work". A transient SDK failure can no longer archive over a possibly-running reviewer.
  • Suggestion 2 (transient error reported "gone"): fixed in 05979ce. collectWorkerQuestions now lets threadInfo distinguish not-found (null → gone) from any other error, which is treated as live and logged. Claiming a live worker is gone would have told the steward to spawn a duplicate.
  • Suggestion 3 (unbounded backward scan): fixed in 05979ce. workerQuestion scans only the last three non-empty lines, with a unit test for a question buried above them.
  • Merge with main (BB-17): the first clause is now anyWorkerInFlight(store.workers(b.id)), so finished still counts as in flight and the archive gate is not weakened. The descendant check is layered on top, and the new does not archive over a failed worker's still-running child test exercises it with a worker that is not in flight. DESIGN.md §5 now says "in flight" rather than "unfinished".

Tests: npm run typecheck clean; npm test in plugins/stewards 125 passed (7 files).

— from bb thread thr_h7xntryc8v

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 2 (GLM 5.3, at 05979ce)

Reviewed the merge of main (BB-16, BB-17 — both already reviewed on their own PRs) plus the round-1 fixes. The three round-1 suggestions are cleanly resolved, but the BB-17 merge introduced one real interaction bug with this PR's criterion 3/4 mechanism.

Owner review required: no — none of the owner-reviewed triggers apply. Still a narrow bug fix plus additive wake text; the merged BB-16/BB-17 content carries no new trigger here (no contract, migration, dependency, infra or access change), and this PR's own delta over round 1 is error handling, a scan bound, and test/docs.

Blockers

  1. server.ts:1552 (collectWorkerQuestions) × server.ts:1625 (thread.archived handler) — the merged BB-17 semantics make criterion 4's mechanism unreachable through any archive bb actually reports. workerInFlight now counts finished, so when a question-holding worker is archived — a user tidying the sidebar, or the cascade from a manually archived steward — the thread.archived handler flips its record from finished to stopped before the next wake, and collectWorkerQuestions skips every worker whose record is not finished. Result: the question is never named under "What changed", the worker is never brought back, and there is no "gone"/unreachable line — while DESIGN.md §5 still promises "a worker archived since it asked is brought back on that wake so the tell reaches it". The steward can still read the question in the §4 snapshot (every worker's result is quoted there) and will reasonably try bb thread tell per §1 — getting the exact 409 criterion 4 exists to handle, with nothing in the wake to stop it. I verified this empirically: adding a real thread.archived emission to the revival test (instead of the raw archivedAt = 1 write that bypasses the event system) makes it fail at expect(...archivedAt).toBeNull() — no surfacing, no revival. The test as written models a state the event system can no longer produce. Fix direction, either is fine: (a) in collectWorkerQuestions, don't skip on record stopped while the binding is open — for a stopped worker whose result asks a question, check its thread: archived → bring it back as now, or at minimum say plainly that bb thread tell cannot reach it and a fresh worker is needed; binding-end stops never reach here since wake only runs on open bindings. And make the test emit thread.archived so it exercises the real path. (b) Keep the gate but have the thread.archived handler preserve a pending question's surfacing (that touches BB-17's reviewed semantics — (a) is simpler).

Resolved since round 1

  • Suggestion 1 (fail-open archive gate): resolved — workerThreads now throws, and workersHaveWork fails safe at both call sites (list error and per-thread read error each warn "not archiving" and return true).
  • Suggestion 2 (transient error reported as "gone"): resolved — a non-404 threadInfo error is treated as live with a warning; only a real not-found reports gone.
  • Suggestion 3 (unbounded backward ? scan): resolved — workerQuestion scans only the last three non-empty lines, with unit tests in wake.test.ts covering both the hit and the "asked early, resolved later" miss.

Checked

  • Ran: npm run typecheck (clean) and npm test in plugins/stewards — 125 tests, all pass, matching the PR's claim. stewards is still not in the CI matrix; I ran it myself.
  • Verified the blocker above by emitting thread.archived in a scratch copy of the revival test (restored afterwards; nothing pushed).
  • The new failed-worker test is right and valuable: a failed record is not in flight under BB-17, so only the descendant walk protects its still-running reviewer — and it does.
  • Verified against ticket: criteria 1 (now via anyWorkerInFlight plus the layered descendant walk — strengthened, not weakened, by BB-17), 2 and 5 remain met; criterion 3 met for in-flight workers only; criterion 4 unmet for any archive bb reports to the plugin (blocker 1).
  • BB-16/BB-17 content itself came through main's own review loops; I checked only its interplay with this PR's code, and the merge resolution is otherwise clean.

— from bb thread thr_afbfn6amnw

… BB-17 (BB-14)

The BB-17 merge made workerInFlight count finished, so thread.archived flips
a finished worker to stopped before the next wake, and collectWorkerQuestions
skipped it: criterion 4's revive/report never ran. Surface a question from a
stopped worker when its thread is archived (revive it and restore the record
to finished) or gone, and leave a worker stopped on purpose with its thread
still there alone. The revival test now goes through the thread.archived
event rather than setting archivedAt directly.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 3: pushed f3a7b67.

  • Blocker 1 (archived worker is stopped by BB-17 before the wake, so criterion 4 never runs): fixed in f3a7b67. collectWorkerQuestions no longer requires the record to be finished. It now surfaces a question from a worker whose thread is archived — the thread.archived handler having flipped it to stopped — unarchives the thread and restores the record to finished so the question stays visible until answered. A worker whose thread is gone is still reported as gone. A worker stopped on purpose, with its thread still there, is left alone (its question is not waiting on an answer). The revival test now goes through the real path: it sets archivedAt, emits thread.archived, asserts the record is stopped, then rings and asserts the thread is unarchived and the record is back to finished. It fails against the round-2 code.

Tests: npm run typecheck clean; npm test in plugins/stewards 125 passed (7 files).

— from bb thread thr_h7xntryc8v

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 3 (GLM 5.3, at f3a7b67)

Owner review required: no — none of the owner-reviewed triggers apply. The delta over round 2 is a 24-line fix in collectWorkerQuestions plus its test: no contract, migration, dependency, infra or access change.

Blockers

None.

Suggestions

  • The new "stopped on purpose, live thread" skip (if (w.status === "stopped" && !archived && !gone) continue) has no test — the revival test covers the stopped→archived and gone branches, but not the one where a stopped worker's thread is still there and must be left alone. A third case in the same test (stop the record with setWorker, leave the thread live, expect no question item) would pin all three branches.

Resolved since round 2

  • Blocker 1: resolved — collectWorkerQuestions now skips only running/failed records; a stopped worker whose thread is archived is unarchived and its record restored to finished (so the question stays visible until answered, and the worker is in flight again for the archive gate); a gone thread is reported plainly; a purposefully stopped worker with a live thread is left alone. The transient-error branch correctly stays conservative for stopped records (skip rather than guess). The revival test now emits thread.archived, asserting the full stopped→revived→finished path — I confirmed the equivalent emission fails the round-2 code in round 2, so this is a real regression test for the real event path.

Checked

  • Ran: npm run typecheck (clean) and npm test in plugins/stewards — 125 tests, all pass, matching the PR's claim. stewards is still not in the CI matrix; I ran it myself.
  • Re-read the whole changed function for follow-up bugs: store.workers returns a fresh array, so the setWorker inside the loop is safe; a revived worker answered via bb thread tell flips back to running through the existing thread.active handler; a stopped worker without a question is still never touched.
  • Verified against ticket: all five criteria now met on the realistic event paths — 1 (anyWorkerInFlight + layered descendant walk), 2 (lifecycle-owned, documented in DESIGN.md §5), 3 (question named first under "What changed", last-three-lines heuristic), 4 (real-archive path now surfaced and revived, record restored; gone reported plainly; purposeful stop respected), 5 (BB-11 sequence test plus revival/gone tests, the revival one through the event system).

— from bb thread thr_afbfn6amnw

…(BB-14)

Pin the third branch of collectWorkerQuestions: a worker stopped on purpose,
whose thread is still there, is left alone and its question is not surfaced.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 4: pushed 40951fd.

  • Suggestion (purposeful-stop skip untested): taken in 40951fd. The revival test now also creates a second worker, stops its record with its thread still live, and asserts the wake neither names its question nor revives it — pinning the stopped→archived, gone, and purposeful-stop branches.

Tests: npm run typecheck clean; npm test in plugins/stewards 125 passed (7 files).

— from bb thread thr_h7xntryc8v

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 4 (GLM 5.3, at 40951fd)

Owner review required: no — none of the owner-reviewed triggers apply. The delta over round 3 is test-only.

Blockers

None.

Resolved since round 3

  • Suggestion (untested purposeful-stop skip): resolved — the revival test now stops a second worker on purpose with its thread live and asserts its question is neither named in the wake nor its thread archived, pinning all three branches of collectWorkerQuestions (finished/live, stopped/archived → revived, stopped/live → skipped, gone → reported). The comment-cursor bump (17:00 → 18:00) keeps the later gone-wake firing correctly.

Checked

  • Ran: npm run typecheck (clean) and npm test in plugins/stewards — 125 tests, all pass. stewards is still not in the CI matrix; I ran it myself.
  • No production code changed since the approved round 3; the ticket's five criteria remain met.

The gate is met for the no-review lane: this marker plus green checks. Merge away.

— from bb thread thr_afbfn6amnw

…ting-archive-fix-thr_h7xntryc8v

# Conflicts:
#	plugins/stewards/server.ts
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 5: pushed b355ab0. Merged main (BB-13, "one spec approval from any surface"). One conflict in wake: kept main's specHashFor(b, observed.entity) for the gate and my collectWorkerQuestions(b) line above it. No other conflicts; the rest is main's. npm run typecheck clean; npm test 143 passed (7 files).

— from bb thread thr_h7xntryc8v

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 5 (GLM 5.3, at b355ab0)

Owner review required: no — none of the owner-reviewed triggers apply. The delta over round 4 is the merge of main's BB-13 (already reviewed on its own PR loop) plus a one-hunk conflict resolution.

Blockers

None.

Checked

  • Merge resolution confirmed, as requested: in wake the tree keeps main's gateState(playbook, b.ready, specHashFor(b, observed.entity)) (BB-13) and this PR's const workerQuestions = await collectWorkerQuestions(b) plus workerQuestions in the formatWake input — both sides of the conflict survived, in the right order, nothing else of main's dropped.
  • Verified all four BB-14 functions (workerThreads, workersHaveWork, archiveWaiting, collectWorkerQuestions) are byte-identical to the approved round-4 code through the merge; the rest of the server.ts/wake.ts/store.ts delta is BB-13's own content from main, outside BB-14's regions, with no interplay (BB-14's question collection runs before the gate line it was merged next to and depends on nothing BB-13 changed).
  • Ran: npm run typecheck (clean) and npm test in plugins/stewards — 143 tests, all pass (18 new ones are BB-13's, from main). stewards is still not in the CI matrix; I ran it myself.
  • Ticket criteria 1–5 all remain met; the BB-14 tests (BB-11 sequence, revival/gone, purposeful-stop) all still pass on the merged tree.

The no-review gate is met at this sha: this marker plus green checks. Merge away.

— from bb thread thr_afbfn6amnw

@trotterdylan
trotterdylan merged commit 19d2c7c into main Oct 19, 2026
16 checks passed
@trotterdylan
trotterdylan deleted the bb/bb-14-stewards-waiting-archive-fix-thr_h7xntryc8v branch October 19, 2026 22:47
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Merged: no-review lane, reviewer approved b355ab0, checks green.

— from bb thread thr_h7xntryc8v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant