From 7f57c0bc94c6d70741ed1782e2844d45a8d85d87 Mon Sep 17 00:00:00 2001 From: Dylan Trotter Date: Mon, 16 Nov 2026 06:31:23 +0000 Subject: [PATCH 1/2] stewards: fix two typed-wait shadow divergences (BB-333) The typed-wait shadow read a `date` wait live from the ref text even when `steward_report` had not turned it into the binding's `requeueUntil`, so a date wait claimed a wake the plugin never scheduled. It now reads the scheduled requeue, which is the design's rule. The divergence writer's stop touch overwrote a stopped row's inputs with the current fields, so a stopped `worker` disagreement read back with `waitLive: true` beside its `derived no-wake-source` pair. It now keeps the row's own inputs and only flips the transition. No gate, wait, wake, card or status result changes; no migration; retires nothing. --- plugins/stewards/assessment.test.ts | 35 ++++++++++++++++++++++++----- plugins/stewards/assessment.ts | 13 ++++++----- plugins/stewards/store.ts | 14 +++++++++++- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/plugins/stewards/assessment.test.ts b/plugins/stewards/assessment.test.ts index d3145b23..551c596a 100644 --- a/plugins/stewards/assessment.test.ts +++ b/plugins/stewards/assessment.test.ts @@ -394,14 +394,22 @@ describe("the shadow assessment: the typed wait (BB-333)", () => { expect(person.liveness.waitLive).toBe(true); }); - it("derives a date wait live while the date is ahead", () => { + it("derives a date wait live only while the scheduled requeue is ahead", () => { const now = Date.UTC(2026, 5, 1); - const ahead = assess(withWait({ kind: "date", ref: "2026-12-01" }, { now })); + const dateWait = (ref: string, requeueUntil: number | null) => assess(input({ now, binding: binding({ wait: { kind: "date", ref }, requeueUntil }) })); + const ahead = dateWait("2026-12-01", Date.UTC(2026, 11, 1)); expect(ahead.liveness.waitLive).toBe(true); expect(ahead.liveness.stalled).toBe(false); - const past = assess(withWait({ kind: "date", ref: "2000-01-01" }, { now })); + const past = dateWait("2000-01-01", Date.UTC(2000, 0, 1)); expect(past.liveness.waitLive).toBe(false); expect(past.liveness.stalled).toBe(true); + // A ref date the report did not turn into `requeueUntil` is not a wake + // source: the plugin never scheduled a wake there, so the wait stalls + // rather than reading live from the ref text (the `date` row the shadow + // showed as derived-live while `noWakeSource` said no source). + const unscheduled = dateWait("2026-12-01", null); + expect(unscheduled.liveness.waitLive).toBe(false); + expect(unscheduled.liveness.stalled).toBe(true); }); it("keeps the free-text rule exactly for a report with no typed wait", () => { @@ -460,6 +468,21 @@ describe("the typed-wait divergence store (BB-333)", () => { expect(s.divergences({ bindingId: "b2" })).toHaveLength(0); }); + it("keeps a stopped row's own wait kind and liveness, only flipping its transition", () => { + const { s } = store(); + // A worker disagreement: the worker was between turns, so the typed + // derivation stalled while the report-time flag said live. + s.recordWaitDivergence({ bindingId: "b1", derived: true, reported: false, waitKind: "worker", waitLive: false, inputs: { hasWait: false } }, 1_000); + // It stops disagreeing while the current fields say the worker is running + // (waitLive true). The stopped row must keep the disagreement's own + // `waitKind`/`waitLive`, never the current ones, or it reads as a live + // worker stalling. + s.recordWaitDivergence({ bindingId: "b1", derived: false, reported: false, waitKind: "worker", waitLive: true, inputs: { hasWait: true } }, 2_000); + const rows = s.divergences({ bindingId: "b1" }); + expect(rows).toHaveLength(1); + expect(rows[0]!.inputs).toMatchObject({ waitKind: "worker", waitLive: false, hasWait: false, transition: "stop" }); + }); + it("keeps the two directions' rows apart and treats a flip as a stop plus a start", () => { const { s } = store(); // start A, stop A, start B, ongoing B. @@ -497,9 +520,9 @@ describe("the typed-wait divergence store (BB-333)", () => { expect(rows).toHaveLength(2); expect(rows.find((r) => r.bindingId === "b1")).toEqual({ bindingId: "b1", derived: false, reported: true, kind: "pull", firstSeenAt: 2_000, lastSeenAt: 2_000, count: 1, at: 2_000 }); expect(rows.find((r) => r.bindingId === "b2")).toEqual({ bindingId: "b2", derived: false, reported: false, kind: null, firstSeenAt: null, lastSeenAt: null, count: 0, at: 3_000 }); - // A stopped divergence keeps its most recent row for the seen window; the - // stop touch overwrites its inputs, so the kind reads back null. + // A stopped divergence keeps its most recent row for the seen window, and + // that row keeps the wait kind it disagreed about, not the current one. s.recordWaitDivergence({ bindingId: "b1", derived: false, reported: false, waitKind: null, waitLive: false, inputs: {} }, 4_000); - expect(s.waitDivergences({ bindingId: "b1" })).toEqual([{ bindingId: "b1", derived: false, reported: false, kind: null, firstSeenAt: 2_000, lastSeenAt: 4_000, count: 1, at: 4_000 }]); + expect(s.waitDivergences({ bindingId: "b1" })).toEqual([{ bindingId: "b1", derived: false, reported: false, kind: "pull", firstSeenAt: 2_000, lastSeenAt: 4_000, count: 1, at: 4_000 }]); }); }); diff --git a/plugins/stewards/assessment.ts b/plugins/stewards/assessment.ts index cf3ac5a2..ad5b934c 100644 --- a/plugins/stewards/assessment.ts +++ b/plugins/stewards/assessment.ts @@ -15,7 +15,7 @@ import { latestReviewMarker, type PrGate, type ProjectApprovalRecord } from "./g import { parsePullRef } from "./github.js"; import { gateState } from "./ready-gate.js"; import { anyWorkerInFlight, idleInActiveStage, observedStall, productGateOptions, runningWorkerReason, stuckDue, type IdleBinding } from "./reconcile.js"; -import { hasLiveWakeSource, parseUntil, type WakeSourceInput } from "./wake-source.js"; +import { hasLiveWakeSource, type WakeSourceInput } from "./wake-source.js"; import type { Playbook } from "./config.js"; import type { EntitySnapshot } from "./source-contract.js"; import type { Binding, Escalation, Need, Observed, Worker } from "./store.js"; @@ -203,10 +203,13 @@ function typedWaitLive(wait: Wait, input: AssessmentInput): boolean { return input.workers.some((w) => w.threadId === wait.ref && w.status === "running"); case "binding": return input.hasOpenBinding?.(wait.ref) ?? false; - case "date": { - const at = parseUntil(wait.ref) ?? input.binding.requeueUntil; - return at !== null && at > input.now; - } + case "date": + // The design's rule: a `date` wait is live while the binding's own + // requeue is still ahead (`requeueUntil`), which `steward_report` sets + // from the ref (or `until`). Reading the ref here would claim a wake the + // plugin never scheduled, so a `date` wait whose report did not set + // `requeueUntil` is not live. + return input.binding.requeueUntil !== null && input.binding.requeueUntil > input.now; case "person": return input.hasOpenItem?.(wait.ref) ?? false; case "external": diff --git a/plugins/stewards/store.ts b/plugins/stewards/store.ts index 89aabdda..3ee966a1 100644 --- a/plugins/stewards/store.ts +++ b/plugins/stewards/store.ts @@ -2089,6 +2089,15 @@ export function createStore(db: Database.Database, now: () => number, options: S const touchWaitDivergenceStmt = lazy<[number, string, string, string, string], unknown>( "UPDATE assessment_divergences SET last_seen_at = ?, inputs = ? WHERE binding_id = ? AND surface = 'noWakeSource' AND old = ? AND new = ?", ); + // The stop touch keeps the row's own inputs (the disagreement it records) and + // only flips its `transition`, so a stopped row still reads back the wait + // kind and liveness it disagreed with. Writing the current fields here would + // make a stopped `worker` row read `waitLive: true` next to its `derived + // no-wake-source` pair (BB-333). `json_set` keeps this append-only: no + // migration for a display-only field. + const stopWaitDivergenceStmt = lazy<[number, string, string, string], unknown>( + "UPDATE assessment_divergences SET last_seen_at = ?, inputs = json_set(inputs, '$.transition', 'stop') WHERE binding_id = ? AND surface = 'noWakeSource' AND old = ? AND new = ?", + ); // BB-333: the reader's side of the state table. The `bb stewards // wait-divergences` command reads the current derived/reported pair per // binding; the `noWakeSource` divergence row the pair wrote rides along for @@ -3919,6 +3928,9 @@ export function createStore(db: Database.Database, now: () => number, options: S const touch = (transition: string, d: number, r: number): void => { touchWaitDivergenceStmt().run(at, note(transition), fields.bindingId, old(r), old(d)); }; + const stop = (d: number, r: number): void => { + stopWaitDivergenceStmt().run(at, fields.bindingId, old(r), old(d)); + }; const pairChanged = prevDerived !== derived || prevReported !== reported; if (!pairChanged) { // No transition. An ongoing disagreement keeps its open row fresh so @@ -3930,7 +3942,7 @@ export function createStore(db: Database.Database, now: () => number, options: S // stopped: note it on the old row, never as a row of its own (its `old` // would equal its `new`). A change from one disagreement to the other is // that stop plus a new start. - if (prevDisagree === true) touch("stop", prevDerived!, prevReported!); + if (prevDisagree === true) stop(prevDerived!, prevReported!); upsertWaitDivergenceStateStmt().run(fields.bindingId, derived, reported, at); if (!disagree) return false; // A start: the first observation of a disagreement, a move from From 411cd49bdf615ef504601ae533b336def9d03826 Mon Sep 17 00:00:00 2001 From: Dylan Trotter Date: Mon, 16 Nov 2026 06:38:47 +0000 Subject: [PATCH 2/2] stewards: correct the date-wait comment (BB-333) --- plugins/stewards/assessment.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/stewards/assessment.ts b/plugins/stewards/assessment.ts index ad5b934c..ffc3e989 100644 --- a/plugins/stewards/assessment.ts +++ b/plugins/stewards/assessment.ts @@ -205,10 +205,11 @@ function typedWaitLive(wait: Wait, input: AssessmentInput): boolean { return input.hasOpenBinding?.(wait.ref) ?? false; case "date": // The design's rule: a `date` wait is live while the binding's own - // requeue is still ahead (`requeueUntil`), which `steward_report` sets - // from the ref (or `until`). Reading the ref here would claim a wake the - // plugin never scheduled, so a `date` wait whose report did not set - // `requeueUntil` is not live. + // requeue is still ahead (`requeueUntil`). `steward_report` sets + // `requeueUntil` only from `until` today, not yet from the ref, so a + // `date` wait that sent only a ref is not live. Reading the ref here + // claimed a wake the plugin never scheduled; the report-path gap (the + // design has the ref set `requeueUntil` too) is a separate fix. return input.binding.requeueUntil !== null && input.binding.requeueUntil > input.now; case "person": return input.hasOpenItem?.(wait.ref) ?? false;