Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions plugins/stewards/assessment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 }]);
});
});
14 changes: 9 additions & 5 deletions plugins/stewards/assessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -203,10 +203,14 @@ 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`). `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;
case "external":
Expand Down
14 changes: 13 additions & 1 deletion plugins/stewards/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading