Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(crash-robot): wait for the claim pass after finish's probe so the last claim epoch is always reached (#6957)
garrytan914246fe92ce
diff --git a/scripts/persistence/crash-robot.ts b/scripts/persistence/crash-robot.tsindex 75c63d3879..4d5d2e6ea1 100644--- a/scripts/persistence/crash-robot.ts+++ b/scripts/persistence/crash-robot.ts@@ -103,6 +103,24 @@ export async function finish(world: World, model: ReferenceModel, label: string,{ slug: `notes/probe-${randomUUID().slice(0, 8)}`, content: pageBody('notes/probe', 'mk-probe0') });const seen = await executeOp(world, probe);if (seen.status !== 'committed') model.violate({ class: 'wedge', detail: `${label}: a fresh write after the schedule ended ${seen.status}/${seen.code}` });+ await awaitClaimPass(world);+}++/**+ * #6957: wait for a consumer claim pass after the probe was prepared. The consumer makes one after every write it+ * finishes, but a worker that returned at once stopped it first, unless timing let the pass in: a CI count role saw+ * that pass as claim epoch 5 of caller_bound_replay, every replay of the run exited before making it, and its fault+ * at consumer:claiming#5 went unreached. Waiting makes the last claim epoch part of every counted and run schedule.+ * Only a worker whose fault hook counts claim passes (`world.claimPasses`) waits.+ */+async function awaitClaimPass(world: World): Promise<void> {+ if (world.claimPasses === undefined) return;+ const before = world.claimPasses;+ const deadline = Date.now() + 30_000;+ while (world.claimPasses === before && Date.now() < deadline) {+ startPersistenceConsumer(world.engine, world.config).wake();+ await Bun.sleep(50);+ }}export async function runSchedule(world: World, schedule: Schedule): Promise<ScheduleResult> {@@ -183,7 +201,8 @@ const PER_REQUEST_SEAMS: ReadonlySet<FaultPoint> = new Set<FaultPoint>(['consume* saw 19, the run made fewer and its fault at #19 went unreached). It counts in claim epochs instead: the number of* distinct write request rows that have reached `consumer:preparing`. The first pass at epoch e or later advances* the count to e + 1, so slot n is the first claim attempt after n - 1 requests were prepared; requests of one root- * are claimed one after another, so every slot the count role saw comes again in the run.+ * are claimed one after another, and `finish` waits for a claim pass after its probe, the last request (#6957), so+ * every slot the count role saw comes again in the run.*//** How far this pass advances the seam's count: 1, 0 for a repeat of the same request at a per-request seam, the epochs passed at the claim seam. */export function seamCounter(): (point: FaultPoint, detail: FaultDetail) => number {@@ -390,8 +409,12 @@ export async function robotWorker(role: 'count' | 'run' | 'recover', config: RobinstallLockOrderTrace();const counts: Record<string, number> = {};const counted = seamCounter();- if (role === 'count') installFaultHook((point, detail) => { const advanced = counted(point, detail); if (advanced) counts[point] = (counts[point] ?? 0) + advanced; });- if (role === 'run' && config.fault) installFaultHook(robotFaultHook(config.fault));+ const hook = role === 'count' ? (point: FaultPoint, detail: FaultDetail) => { const advanced = counted(point, detail); if (advanced) counts[point] = (counts[point] ?? 0) + advanced; }+ : role === 'run' && config.fault ? robotFaultHook(config.fault) : undefined;+ if (hook) {+ world.claimPasses = 0;+ installFaultHook((point, detail) => { if (point === 'consumer:claiming') world.claimPasses!++; hook(point, detail); });+ }if (role === 'run') {let next = 0; let inFlight: string[] = [];world.onSubmit = () => writeState(config, world, model, checkouts, next, inFlight);diff --git a/scripts/persistence/ops.ts b/scripts/persistence/ops.tsindex 559d19f161..d6921104c8 100644--- a/scripts/persistence/ops.ts+++ b/scripts/persistence/ops.ts@@ -104,6 +104,8 @@ export interface World {sessionDrops?: SessionDrops;/** Write reply wait every op context carries; absent = the operation default (a probe shortens it to force `write_pending`). */writeWaitMs?: number;+ /** `consumer:claiming` passes a counting or crashing worker's fault hook saw; `finish` waits for one after its probe (#6957). */+ claimPasses?: number;}/**diff --git a/test/persistence-crash-robot.test.ts b/test/persistence-crash-robot.test.tsindex 1a808dc7ae..5527db2fdb 100644--- a/test/persistence-crash-robot.test.ts+++ b/test/persistence-crash-robot.test.ts@@ -24,7 +24,7 @@ import { FULL_ROBOT_SECONDS, ROBOT_TOPOLOGY, restrict, runRobotPhase, type Robotimport { robotFailure, robotFullGate, runValidation, spawnWorker } from '../scripts/persistence/validate.ts';import { faultPoint, installFaultHook, type FaultDetail, type FaultPoint } from '../src/core/persistence/fault-points.ts';import { shrinkRun } from '../scripts/persistence/shrink.ts';-import { runSchedule, runSteps } from '../scripts/persistence/crash-robot.ts';+import { finish, runSchedule, runSteps, seamCounter } from '../scripts/persistence/crash-robot.ts';import { installLockOrderTrace, lockOrderReport } from '../scripts/persistence/lock-order.ts';import { prepareTopology } from '../scripts/persistence/history-fixture.ts';import { claimPersistenceEffect, releaseAbandonedClaims } from '../src/core/persistence/effect-journal.ts';@@ -432,6 +432,23 @@ process.stdout.write('{"event":"continued"}\\n');} finally { child.kill('SIGKILL'); await child.exited; rmSync(dir, { recursive: true, force: true }); }}, 90_000);+ test('#6957: finish waits for the claim pass after its probe, so the last claim epoch a count role sees is reached in every run', async () => {+ await robotBrain(async ({ world }) => {+ const counted = seamCounter(); const prepared = new Set<string>(); let slots = 0;+ world.claimPasses = 0;+ // Every claim pass is slow to start, so the one the consumer makes after the probe commits lands only when finish waits for it.+ installFaultHook(async (point, detail) => {+ if (point === 'consumer:preparing' && detail.writeId) prepared.add(detail.writeId);+ if (point !== 'consumer:claiming') { counted(point, detail); return; }+ await Bun.sleep(1000);+ world.claimPasses!++; slots += counted(point, detail);+ });+ try { await finish(world, new ReferenceModel(world), 'probe only'); }+ finally { installFaultHook(undefined); }+ expect({ prepared: prepared.size, slots }).toEqual({ prepared: 1, slots: 2 });+ });+ }, 90_000);+test('a hook that cannot report its fault emits fault_hook_error and still freezes', async () => {const { lines, alive } = await hookChild(`const cyclic: Record<string, unknown> = {}; cyclic.self = cyclic;await faultPoint('effect:git:mid', { requestId: 'r1', signal: cyclic as never });`);