Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): #6712 PR 4b leave pre-pass failed receipts as they were (lane C ruling); fence rows and doctor hot-check goldens follow the pass
garrytanfe8a2eabedd5
diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex 88a4c6cc2e..cb07f8a918 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -23,7 +23,7 @@ import type { Cursor, RetryWait } from './sync-run.ts';import { getWriteRequestById } from './journal.ts';import type { WriteRequest } from './model.ts';import { managedSyncWriteDiagnostic } from './sync-write-diagnostic.ts';-import { fileUnchangedSinceFreeze, recordedStop, replaysUnchangedRefusal } from './sync-stop-class.ts';+import { recordedStop, replaysUnchangedRefusal } from './sync-stop-class.ts';import { adoptLegacyPage, syncExcludePatterns, syncGit, type LegacyIdentity, type ManagedSyncContext, type SyncDiscovery, type SyncEntry, type SyncFileRefusal, type SyncSlugCollision } from './sync-discovery.ts';import { assertSyncDispatchActive, type SyncAuthority, type SyncProcessingOptions } from './sync-authority.ts';import { recordPassFenceTrend, type PassStamp, type SyncCursorOptions, type SyncIntent } from './sync-prepare.ts';@@ -493,32 +493,20 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prtry {// A page refusal the last pass stopped on replays, with no new request, while what decided it is unchanged (#6712 PR 3, L7).- // Under the hold policy the replay settles that receipt as a hold in place (converted_from_failed, as the cursor's resume- // did), so no failed receipt is left behind a newer request for the same bytes.+ // Under the hold policy the next pass re-admits it instead, and its refusal settles as a hold.const blocked = obligations.blocked;delete obligations.blocked;- if (blocked) {+ if (blocked && (await readSyncHoldPolicy(engine)).mode === 'fail') {const [row] = await engine.executeRaw<{ id: string }>('SELECT id FROM persistence_requests WHERE source_id=$1 AND request_id=$2', [sourceId, blocked.request_id]);const failed = row ? await getWriteRequestById(engine, row.id) : null;const intent = failed?.intent as unknown as SyncIntent | null | undefined;- const stopped = failed && intent && ['failed', 'conflict', 'cancelled'].includes(failed.state) ? intent : null;- const holds = stopped ? (await readSyncHoldPolicy(engine)).mode !== 'fail' : false;- // sync.holds=fail replays while what refused it is unchanged; under the hold policy, while the file has the bytes it froze- // (a legacy or compacted receipt included): its settle holds, re-freezes or records it, and the receipt is converted.- const recorded = stopped && !holds ? await recordedStop(engine, passFingerprint(sourceId, incarnation), blocked.pass_id) : null;+ const recorded = failed && intent && ['failed', 'conflict', 'cancelled'].includes(failed.state) ? await recordedStop(engine, passFingerprint(sourceId, incarnation), blocked.pass_id) : null;assertActive();- if (failed && stopped && (holds ? fileUnchangedSinceFreeze(context.root, stopped) : recorded && replaysUnchangedRefusal(failed, recorded, { root: context.root, intent: stopped, target: stopped.target ?? '', head: () => head }))) {- const intent = stopped;+ if (failed && intent && recorded && replaysUnchangedRefusal(failed, recorded, { root: context.root, intent, target: intent.target ?? '', head: () => head })) {obligations.blocked = blocked;- // The refused entry rides the replay, so a hold settles that receipt where the policy holds.- const refused: PassEntry[] = intent.path ? [{ path: intent.path, sourcePath: intent.sourcePath ?? intent.path, action: intent.kind === 'managed_sync_delete' ? 'delete' : 'import',- working: intent.working === true, slug: failed.slug, pageId: failed.page_id, revision: intent.expected_revision == null ? null : String(intent.expected_revision), level: intent.level ?? null }] : [];- // A replay that stops again is recorded under the stopped pass's run, which the next pass's replay check reads; one the- // hold policy settles is this pass's hold.- const replay: Cursor = { ...batchCursor(refused), runId: holds ? passId : blocked.pass_id, replayOf: blocked.pass_id, pending: { requestId: failed.request_id, slug: failed.slug, pageId: failed.page_id, intent } };+ const replay: Cursor = { ...batchCursor([]), runId: blocked.pass_id, pending: { requestId: failed.request_id, slug: failed.slug, pageId: failed.page_id, intent } };const drained = await input.drainBatch(`${PASS_BATCH_KEY_PREFIX}${passId}:${batchNo++}`, replay, sliceBase);if (!('entries' in drained)) return await stop(drained);- delete obligations.blocked;}}for (let round = 1; round <= PASS_MAX_ROUNDS; round++) {diff --git a/src/core/persistence/sync-run.ts b/src/core/persistence/sync-run.tsindex 28aa2f14a6..7ec2a99134 100644--- a/src/core/persistence/sync-run.ts+++ b/src/core/persistence/sync-run.ts@@ -148,9 +148,7 @@ export interface Cursor extends SyncDiscovery { runId: string; index: number; au*/passId?: string; stamp?: PassStamp; fullEpisode?: string; failureKey?: string; resumeArgs?: string[];/** #5751 on a pass: the batch indexes a legacy kernel waiver (`contextual_mode`, `canonical_file_differs`) passed; they record no import, so the pass settles them for itself. */- legacyWaived?: number[];- /** #6712 PR 4b: a pass's replay of the request its last blocked pass stopped on carries that pass's id, under which the stop was recorded. */- replayOf?: string; }+ legacyWaived?: number[]; }export interface CursorProgress { startedAt: number; startIndex: number; lastAt: number; lastIndex: number }/*** #6278 (1.5): the breaker state a run carries in its cursor. `stalled` lists the `preparation_stalled` receipts this run@@ -647,11 +645,6 @@ async function holdFailedFenceRequest(engine: BrainEngine, cursor: Cursor, key:const fault = stalled || fence ? null : await receiptPageFaultHold(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, entry, pending, done });const observedAt = run.observedAt ?? new Date().toISOString();if (!stalled && !fence && !fault && page && fileChangedAfterAdmission(done)) return deferChangedEntry(engine, { cursor, key, pending, assertActive, observedAt, waitMs, base });- // #6188 on a pass's replay: a compacted fence-family receipt (its message gone) converts only when the re-screen of the current bytes holds them.- if (!stalled && !fence && !fault && cursor.replayOf && done.compacted === true && done.error_message == null && ['invalid_params', 'take_row_collision'].includes(done.error_code ?? '')) {- const again = await freezeEntry(engine, { ...base, index: indexOf(cursor, pending) }, key, assertActive, run);- return 'hold' in again ? holdUnderGate(engine, { cursor, key, pending, hold: again.hold, assertActive, observedAt, waitMs, base }) : null;- }if (!stalled && !fence && !fault) return null;const hold = stalled ? preparationStalledHold(entry, pending.slug, pending.pageId, done, pending.intent.content ?? null, pending.intent.blobOid, resumeArgsOf(cursor, run)): fence ? prepareTimeFenceHold(entry, pending.slug, pending.pageId, fence, pending.intent.content!, pending.intent.blobOid) : fault!;@@ -752,7 +745,7 @@ async function settleFailedRequest(engine: BrainEngine, input: { cursor: Cursor;const revived = await reviveCheckpoint(engine, input);if (revived) { input.assertActive(); return { cursor: revived }; }const fault = receiptClass(done);- if (fault === 'transport' || isSyncKnockOn(done) || (fault === 'systemic' && resumableStop(done) && pending.intent.kind !== 'managed_sync_checkpoint' && await stopRecorded(engine, cursor.failureKey ?? key, cursor.replayOf ?? cursor.runId))) {+ if (fault === 'transport' || isSyncKnockOn(done) || (fault === 'systemic' && resumableStop(done) && pending.intent.kind !== 'managed_sync_checkpoint' && await stopRecorded(engine, cursor.failureKey ?? key, cursor.runId))) {const refrozen = await refreezeInPlace(engine, input);if (refrozen) { input.assertActive(); return { cursor: refrozen }; }}diff --git a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json b/test/fixtures/goldens/doctor/hot-checks-pglite-json.jsonindex d0d2d3c004..923c5c9ada 100644--- a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json+++ b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json@@ -512,18 +512,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 68719476736,- "max_day_7d": 194961,- "net_per_day_7d": 58816,+ "max_day_7d": 194775,+ "net_per_day_7d": 58864,"pct": 0,- "per_day_7d": 194961,- "reclaimable_bytes": 136145,+ "per_day_7d": 194775,+ "reclaimable_bytes": 135911,"recommended_limit": 137438953472,"resource": "terminal_bytes","scope": "brain","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 194961,+ "used": 194775,"window_days": 1},{@@ -549,18 +549,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 17179869184,- "max_day_7d": 194961,- "net_per_day_7d": 58816,+ "max_day_7d": 194775,+ "net_per_day_7d": 58864,"pct": 0,- "per_day_7d": 194961,- "reclaimable_bytes": 136145,+ "per_day_7d": 194775,+ "reclaimable_bytes": 135911,"recommended_limit": 34359738368,"resource": "terminal_bytes","scope": "principal:local_cli:<uuid>","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 194961,+ "used": 194775,"window_days": 1}],@@ -4653,18 +4653,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 68719476736,- "max_day_7d": 194961,- "net_per_day_7d": 58816,+ "max_day_7d": 194775,+ "net_per_day_7d": 58864,"pct": 0,- "per_day_7d": 194961,- "reclaimable_bytes": 136145,+ "per_day_7d": 194775,+ "reclaimable_bytes": 135911,"recommended_limit": 137438953472,"resource": "terminal_bytes","scope": "brain","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 194961,+ "used": 194775,"window_days": 1},{@@ -4690,18 +4690,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 17179869184,- "max_day_7d": 194961,- "net_per_day_7d": 58816,+ "max_day_7d": 194775,+ "net_per_day_7d": 58864,"pct": 0,- "per_day_7d": 194961,- "reclaimable_bytes": 136145,+ "per_day_7d": 194775,+ "reclaimable_bytes": 135911,"recommended_limit": 34359738368,"resource": "terminal_bytes","scope": "principal:local_cli:<uuid>","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 194961,+ "used": 194775,"window_days": 1}],diff --git a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json b/test/fixtures/goldens/doctor/hot-checks-postgres-json.jsonindex a82ed6afb8..53cde0e425 100644--- a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json+++ b/test/fixtures/goldens/doctor/hot-checks-postgres-json.json@@ -506,18 +506,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 68719476736,- "max_day_7d": 196014,- "net_per_day_7d": 58822,+ "max_day_7d": 195828,+ "net_per_day_7d": 58870,"pct": 0,- "per_day_7d": 196014,- "reclaimable_bytes": 137192,+ "per_day_7d": 195828,+ "reclaimable_bytes": 136958,"recommended_limit": 137438953472,"resource": "terminal_bytes","scope": "brain","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 196014,+ "used": 195828,"window_days": 1},{@@ -543,18 +543,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 17179869184,- "max_day_7d": 196014,- "net_per_day_7d": 58822,+ "max_day_7d": 195828,+ "net_per_day_7d": 58870,"pct": 0,- "per_day_7d": 196014,- "reclaimable_bytes": 137192,+ "per_day_7d": 195828,+ "reclaimable_bytes": 136958,"recommended_limit": 34359738368,"resource": "terminal_bytes","scope": "principal:local_cli:<uuid>","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 196014,+ "used": 195828,"window_days": 1}],@@ -4648,18 +4648,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 68719476736,- "max_day_7d": 196014,- "net_per_day_7d": 58822,+ "max_day_7d": 195828,+ "net_per_day_7d": 58870,"pct": 0,- "per_day_7d": 196014,- "reclaimable_bytes": 137192,+ "per_day_7d": 195828,+ "reclaimable_bytes": 136958,"recommended_limit": 137438953472,"resource": "terminal_bytes","scope": "brain","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 196014,+ "used": 195828,"window_days": 1},{@@ -4685,18 +4685,18 @@"days_to_exhaustion": null,"exhaustion_date": null,"limit": 17179869184,- "max_day_7d": 196014,- "net_per_day_7d": 58822,+ "max_day_7d": 195828,+ "net_per_day_7d": 58870,"pct": 0,- "per_day_7d": 196014,- "reclaimable_bytes": 137192,+ "per_day_7d": 195828,+ "reclaimable_bytes": 136958,"recommended_limit": 34359738368,"resource": "terminal_bytes","scope": "principal:local_cli:<uuid>","spike_day": null,"status": "ok","under_reserved_receipts": 0,- "used": 196014,+ "used": 195828,"window_days": 1}],diff --git a/test/persistence-sync-fence-holds.test.ts b/test/persistence-sync-fence-holds.test.tsindex 1e178bf2c7..4cfc60f370 100644--- a/test/persistence-sync-fence-holds.test.ts+++ b/test/persistence-sync-fence-holds.test.ts@@ -180,7 +180,11 @@ test('forced probe: a fence that passes the screen but collides with a stored taexpect(await s.failedRequests()).toHaveLength(1);}), 180_000);-test('sync.holds=fail blocks with the typed refusal; the next sync after the upgrade converts the legacy receipt with no --retry-failed', () => each(async engine => {+// #6712 PR 4b: a non-company source has no blocked cursor to convert (the persistence-sync-holds ruling): the next pass+// re-classifies the file and holds it under its own run, and the receipt an older release left stays exactly as it was.+const receiptRow = (engine: BrainEngine, requestId: string) => engine.executeRaw('SELECT state,error_code,error_message,error_detail,compacted FROM persistence_requests WHERE request_id=$1::uuid', [requestId]);++test('sync.holds=fail blocks with the typed refusal; the next sync after the upgrade holds the file with no --retry-failed and leaves the legacy receipt as it was', () => each(async engine => {await engine.setConfig('sync.holds', 'fail');const s = await source(engine, { 'people/malformed.md': MALFORMED, 'notes/ok.md': note('Ok') });const blocked = await s.sync();@@ -209,15 +213,17 @@ test('sync.holds=fail blocks with the typed refusal; the next sync after the upgexpectNoSecrets(JSON.stringify(remote));expect((await frontmatterHoldsBannerNote(engine)) ?? '').not.toContain(s.id);await engine.unsetConfig('sync.holds');+ const legacy = await receiptRow(engine, failed!.request_id);const converted = await s.sync();- expect(converted.converted_from_failed).toEqual([failed!.request_id]);+ expect(converted.converted_from_failed ?? []).not.toContain(failed!.request_id);+ expect(await receiptRow(engine, failed!.request_id)).toEqual(legacy);expect(converted).toMatchObject({ status: 'first_sync', held_count: 1 });expect(converted.held![0]).toMatchObject({ path: 'people/malformed.md', code: 'invalid_fence', fence: { fence: 'takes', section: 'body' } });expect(await engine.getPage('notes/ok', { sourceId: s.id })).not.toBeNull();expect(await fenceHoldsBannerNote(engine)).toBeNull();}), 180_000);-test('a legacy stored-row receipt holds the same bytes from the receipt; a compacted receipt converts only through the re-screen', () => each(async engine => {+test('a legacy stored-row receipt: the pass holds the same bytes by its own refusal; a compacted receipt: the re-screen holds them; both receipts stay as they were', () => each(async engine => {const s = await source(engine, { 'people/probe.md': takesPage('Probe', take(1)) });await s.sync();await s.storedTake('people/probe', 2, 'Database-only take');@@ -228,8 +234,11 @@ test('a legacy stored-row receipt holds the same bytes from the receipt; a compaawait engine.executeRaw('UPDATE persistence_requests SET error_message=$2,error_detail=NULL WHERE request_id=$1::uuid',[failed!.request_id, "A takes fence row number is already used by a different take that is not in this page's canonical fence."]);await engine.unsetConfig('sync.holds');+ const legacy = await receiptRow(engine, failed!.request_id);const converted = await s.sync();- expect(converted).toMatchObject({ status: 'synced', held_count: 1, converted_from_failed: [failed!.request_id] });+ expect(converted).toMatchObject({ status: 'synced', held_count: 1 });+ expect(converted.converted_from_failed ?? []).not.toContain(failed!.request_id);+ expect(await receiptRow(engine, failed!.request_id)).toEqual(legacy);// The legacy message named no section; it comes from the refused bytes.expect(converted.held![0]).toMatchObject({ code: 'invalid_fence', reason: 'prepare_time', fence: { reason: 'stored_row_collision', fence: 'takes', section: 'body' } });@@ -240,7 +249,11 @@ test('a legacy stored-row receipt holds the same bytes from the receipt; a compaconst [compacted] = await t.failedRequests();await engine.executeRaw('UPDATE persistence_requests SET error_message=NULL,error_detail=NULL,compacted=true WHERE request_id=$1::uuid', [compacted!.request_id]);await engine.unsetConfig('sync.holds');- expect(await t.sync()).toMatchObject({ status: 'first_sync', held_count: 1, converted_from_failed: [compacted!.request_id] });+ const compactedRow = await receiptRow(engine, compacted!.request_id);+ const rescreened = await t.sync();+ expect(rescreened).toMatchObject({ status: 'first_sync', held_count: 1 });+ expect(rescreened.converted_from_failed ?? []).not.toContain(compacted!.request_id);+ expect(await receiptRow(engine, compacted!.request_id)).toEqual(compactedRow);// ...but an arbitrary invalid_params receipt, compacted or not, is never a fence hold: once the file is clean, the next// plain sync retries it (#6712 PR 3, L7: no --retry-failed) and nothing is held or converted.