Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(crash-robot): count re-run preparations once per request so count and run agree on nth

garrytan5f8ca7864ace
diff --git a/scripts/persistence/crash-robot.ts b/scripts/persistence/crash-robot.ts
index 43c1b78b74..497a9766d4 100644
--- a/scripts/persistence/crash-robot.ts
+++ b/scripts/persistence/crash-robot.ts
@@ -165,6 +165,26 @@ function freeze(event: Record<string, unknown>): never {
for (;;) Atomics.wait(blocked, 0, 0);
}
+/**
+ * Seams one request can reach more than once by design: a preparation runs
+ * again after a revision conflict or a group cut, as often as timing
+ * dictates (7 to 10 preparations for the same 8-op schedule). Counting every
+ * pass made the count run and the run role disagree on nth, so a fault could
+ * go unreached. These seams count each request once (its first pass).
+ */
+const PER_REQUEST_SEAMS: ReadonlySet<FaultPoint> = new Set<FaultPoint>(['consumer:preparing']);
+/** True when this pass counts toward the seam's nth: always, except a repeat of the same request at a per-request seam. */
+export function seamCounter(): (point: FaultPoint, detail: FaultDetail) => boolean {
+ const seen = new Set<string>();
+ return (point, detail) => {
+ if (!PER_REQUEST_SEAMS.has(point) || !detail.requestId) return true;
+ const key = `${point}\u0000${detail.requestId}`;
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ };
+}
+
/**
* The `run` role's hook: freeze at the nth matching seam. A hook that throws
* would unwind into the code under test, which then keeps running as if no
@@ -174,8 +194,9 @@ function freeze(event: Record<string, unknown>): never {
*/
export function robotFaultHook(fault: NonNullable<RobotConfig['fault']>): (point: FaultPoint, detail: FaultDetail) => void {
let seen = 0;
+ const counts = seamCounter();
return (point, detail) => {
- if (point !== fault.point || (fault.operation && detail.operation !== fault.operation) || ++seen !== fault.nth) return;
+ if (point !== fault.point || (fault.operation && detail.operation !== fault.operation) || !counts(point, detail) || ++seen !== fault.nth) return;
try { freeze({ event: 'fault', point, nth: seen, detail }); }
catch (error) { freeze({ event: 'fault_hook_error', point, nth: seen, message: error instanceof Error ? error.message : String(error) }); }
};
@@ -345,7 +366,8 @@ export async function robotWorker(role: 'count' | 'run' | 'recover', config: Rob
world.descriptors = new Map(config.schedule.ops.map(d => [d.id, d]));
installLockOrderTrace();
const counts: Record<string, number> = {};
- if (role === 'count') installFaultHook(point => { counts[point] = (counts[point] ?? 0) + 1; });
+ const counted = seamCounter();
+ if (role === 'count') installFaultHook((point, detail) => { if (counted(point, detail)) counts[point] = (counts[point] ?? 0) + 1; });
if (role === 'run' && config.fault) installFaultHook(robotFaultHook(config.fault));
if (role === 'run') {
let next = 0; let inFlight: string[] = [];
diff --git a/test/persistence-crash-robot.test.ts b/test/persistence-crash-robot.test.ts
index d6f6321a1b..3dc4cc5dea 100644
--- a/test/persistence-crash-robot.test.ts
+++ b/test/persistence-crash-robot.test.ts
@@ -357,6 +357,43 @@ process.stdout.write('{"event":"continued"}\\n');
expect({ lines, alive, stderr }).toEqual({ lines: [{ event: 'fault', point: 'effect:git:mid', nth: 2, detail: { effectId: '42', requestId: 'r1' } }], alive: true, stderr: '' });
}, 90_000);
+ test('a re-run preparation of the same request does not advance nth: count and run agree', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'gbrain-crash-robot-prep-'));
+ const path = join(dir, 'prep.ts');
+ writeFileSync(path, `import { faultPoint, installFaultHook } from ${JSON.stringify(join(import.meta.dir, '../src/core/persistence/fault-points.ts'))};
+import { robotFaultHook, seamCounter } from ${JSON.stringify(join(import.meta.dir, '../scripts/persistence/crash-robot.ts'))};
+const counted = seamCounter(); const counts: Record<string, number> = {};
+installFaultHook((point, detail) => { if (counted(point, detail)) counts[point] = (counts[point] ?? 0) + 1; });
+for (const requestId of ['a', 'a', 'b', 'b', 'b']) await faultPoint('consumer:preparing', { requestId });
+for (const requestId of ['a', 'a']) await faultPoint('publication:after_commit', { requestId });
+process.stdout.write(JSON.stringify({ event: 'counts', counts }) + '\\n');
+installFaultHook(robotFaultHook({ point: 'consumer:preparing', nth: 2 }));
+await faultPoint('consumer:preparing', { requestId: 'a' });
+await faultPoint('consumer:preparing', { requestId: 'a' });
+await faultPoint('consumer:preparing', { requestId: 'b' });
+process.stdout.write('{"event":"continued"}\\n');
+`);
+ const child = Bun.spawn([process.execPath, '--no-env-file', path], { stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' });
+ try {
+ const lines: Record<string, unknown>[] = [];
+ const reader = child.stdout.getReader(); const decoder = new TextDecoder(); let pending = '';
+ const deadline = Date.now() + 60_000;
+ while (lines.length < 2 && Date.now() < deadline) {
+ const chunk = await Promise.race([reader.read(), Bun.sleep(deadline - Date.now()).then(() => null)]);
+ if (!chunk || chunk.done) break;
+ pending += decoder.decode(chunk.value, { stream: true });
+ for (let newline = pending.indexOf('\n'); newline >= 0; newline = pending.indexOf('\n')) {
+ lines.push(JSON.parse(pending.slice(0, newline))); pending = pending.slice(newline + 1);
+ }
+ }
+ reader.releaseLock();
+ expect(lines).toEqual([
+ { event: 'counts', counts: { 'consumer:preparing': 2, 'publication:after_commit': 2 } },
+ { event: 'fault', point: 'consumer:preparing', nth: 2, detail: { requestId: 'b' } },
+ ]);
+ } finally { child.kill('SIGKILL'); await child.exited; rmSync(dir, { recursive: true, force: true }); }
+ }, 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 });`);