Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
crash robot: consumer:prepared counts once per write request row (count/run nth drift)
garrytand736799aa806
diff --git a/scripts/persistence/crash-robot.ts b/scripts/persistence/crash-robot.tsindex 497a9766d4..c8e70010e0 100644--- a/scripts/persistence/crash-robot.ts+++ b/scripts/persistence/crash-robot.ts@@ -168,17 +168,22 @@ function freeze(event: Record<string, unknown>): never {/*** 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).+ * dictates (7 to 10 preparations for the same 8-op schedule), and a prepared+ * request whose publication is requeued (`writer_lock_unavailable`) is+ * prepared again (caller_bound_replay counted 4 or 5 `consumer:prepared`).+ * Counting every pass made the count run and the run role disagree on nth,+ * so a fault could go unreached. These seams count each write request row+ * once (its first pass), keyed on `writeId`: a caller-bound replay's rows+ * share one `requestId`.*/-const PER_REQUEST_SEAMS: ReadonlySet<FaultPoint> = new Set<FaultPoint>(['consumer:preparing']);+const PER_REQUEST_SEAMS: ReadonlySet<FaultPoint> = new Set<FaultPoint>(['consumer:preparing', 'consumer:prepared']);/** 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}`;+ const request = detail.writeId ?? detail.requestId;+ if (!PER_REQUEST_SEAMS.has(point) || !request) return true;+ const key = `${point}\u0000${request}`;if (seen.has(key)) return false;seen.add(key);return true;diff --git a/src/core/persistence/consumer.ts b/src/core/persistence/consumer.tsindex 35649c743b..80ae4d37e7 100644--- a/src/core/persistence/consumer.ts+++ b/src/core/persistence/consumer.ts@@ -951,7 +951,7 @@ export class PersistenceConsumer implements PersistenceConsumerLike {}const prep = startPreparation(async signal => {signal.addEventListener('abort', () => cancel.abort(signal.reason), { once: true });- await faultPoint('consumer:preparing', { requestId: row.request_id, sourceId: row.source_id, operation: row.operation, signal });+ await faultPoint('consumer:preparing', { requestId: row.request_id, writeId: row.id, sourceId: row.source_id, operation: row.operation, signal });// Phase 4.3: a single page write prepares against one config read instead of one per key.return this.prepare(this.engine.kind === 'postgres' && singleWrite(row)&& await writeSwitchOn(this.engine, 'single_write_group').catch(() => true) ? await preparationConfigView(this.engine) : this.engine,@@ -996,7 +996,7 @@ export class PersistenceConsumer implements PersistenceConsumerLike {this.preparing.delete(row.id);preparationActive = false;enterClaimPhase(clock, 'publishing');- await faultPoint('consumer:prepared', { requestId: row.request_id, sourceId: row.source_id, operation: row.operation });+ await faultPoint('consumer:prepared', { requestId: row.request_id, writeId: row.id, sourceId: row.source_id, operation: row.operation });const ceilingMs = await readPublicationCeilingMs(this.engine);await holdClaimForPublication(lease, () => renewWriteClaim({ executeRaw: this.engine.executeRawDirect.bind(this.engine) }, row.id, row.execution_token!,publicationLeaseMs(ceilingMs), undefined, claimPhaseStamp(clock, row.execution_token)));diff --git a/src/core/persistence/fault-points.ts b/src/core/persistence/fault-points.tsindex 6f967d6e58..936094e8d1 100644--- a/src/core/persistence/fault-points.ts+++ b/src/core/persistence/fault-points.ts@@ -41,7 +41,8 @@ export type FaultPoint = `publication:${PublicationBoundary}` | `effect:${Effect* a stalling hook can model both the reporter's cases: a preparer that honours* cancellation (settle on abort) and one that ignores it (hang on).*/-export interface FaultDetail { requestId?: string; effectId?: string; sourceId?: string; operation?: string | null; signal?: AbortSignal; slug?: string; path?: string }+/** `writeId` is the write request row: rows of a caller-bound replay share one `requestId` (crash-robot per-request seams key on the row). */+export interface FaultDetail { requestId?: string; writeId?: string; effectId?: string; sourceId?: string; operation?: string | null; signal?: AbortSignal; slug?: string; path?: string }/** Postgres returns `persistence_effects.id` (bigserial) as a BigInt; `faultPoint` hands hooks its decimal string (#6584). */export type FaultDetailInput = Omit<FaultDetail, 'effectId'> & { effectId?: string | number | bigint };type FaultHook = (point: FaultPoint, detail: FaultDetail) => Promise<void> | void;diff --git a/src/core/persistence/group-publish.ts b/src/core/persistence/group-publish.tsindex 719db0336a..022007dfee 100644--- a/src/core/persistence/group-publish.ts+++ b/src/core/persistence/group-publish.ts@@ -651,7 +651,7 @@ export async function executeClaimedGroup(engine: BrainEngine, rows: WriteReques// The member's signal reaches its preparer through its clock (enterClaimStep), never as a blanket statement signal: see the memo rule.const prep = startPreparation(async signal => {signal.addEventListener('abort', () => cancels[offset]!.abort(signal.reason), { once: true });- await faultPoint('consumer:preparing', { requestId: row.request_id, sourceId: row.source_id, operation: row.operation, signal });+ await faultPoint('consumer:preparing', { requestId: row.request_id, writeId: row.id, sourceId: row.source_id, operation: row.operation, signal });return run.prepare(row, reads, undefined, clocks[i]);}, budget, { onDeadline: () => {if (!ordered) return;diff --git a/test/persistence-crash-robot.test.ts b/test/persistence-crash-robot.test.tsindex 3dc4cc5dea..ab01767193 100644--- a/test/persistence-crash-robot.test.ts+++ b/test/persistence-crash-robot.test.ts@@ -366,6 +366,7 @@ 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 });+for (const writeId of ['w1', 'w1', 'w2']) await faultPoint('consumer:prepared', { requestId: 'shared', writeId });process.stdout.write(JSON.stringify({ event: 'counts', counts }) + '\\n');installFaultHook(robotFaultHook({ point: 'consumer:preparing', nth: 2 }));await faultPoint('consumer:preparing', { requestId: 'a' });@@ -388,7 +389,7 @@ process.stdout.write('{"event":"continued"}\\n');}reader.releaseLock();expect(lines).toEqual([- { event: 'counts', counts: { 'consumer:preparing': 2, 'publication:after_commit': 2 } },+ { event: 'counts', counts: { 'consumer:preparing': 2, 'publication:after_commit': 2, 'consumer:prepared': 2 } },{ event: 'fault', point: 'consumer:preparing', nth: 2, detail: { requestId: 'b' } },]);} finally { child.kill('SIGKILL'); await child.exited; rmSync(dir, { recursive: true, force: true }); }