Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
refactor(#6340): receiptPageFaultHold and originFaultHold live in sync-page-fault.ts (sync-run.ts stays under the 1500-line cap)
garrytanb4f305509dee
diff --git a/src/core/persistence/sync-page-fault.ts b/src/core/persistence/sync-page-fault.tsindex 2270e7ee6e..5e14cf09ca 100644--- a/src/core/persistence/sync-page-fault.ts+++ b/src/core/persistence/sync-page-fault.ts@@ -38,6 +38,7 @@ import { readTreeBlobs } from './sync-blobs.ts';import type { SyncIntent } from './sync-prepare.ts';import type { HeldEntry } from './sync-screen.ts';import type { GitHoldMeta } from './sync-holds.ts';+import { concurrentWriteHold, concurrentWriteProof } from './sync-concurrent-write.ts';export { HOLD_ATTEMPTS_NEEDS_HUMAN } from './sync-fault-class.ts';@@ -145,3 +146,32 @@ export async function pageMovedSinceAdmission(engine: Pick<BrainEngine, 'executeconst revision = await liveRevision(engine, sourceId, pending);return revision === (pending.intent.expected_revision === null || pending.intent.expected_revision === undefined ? null : String(pending.intent.expected_revision)) ? null : { revision };}++/**+ * The hold a terminal receipt of a page request earns when its page moved under the sync, or null when the receipt is+ * not such a fault: #6194's proven race (`concurrent_write` with the competing request), a `revision_conflict` whose page+ * really moved (`concurrent_write` by revision), or a `pinned_git_worktree_conflict` (`worktree_dirty`). Shared by the+ * run's receipt branch and the start-of-run conversion of an older release's blocked cursor.+ */+export async function receiptPageFaultHold(engine: Pick<BrainEngine, 'executeRaw'>, input: { sourceId: string; incarnation: string; entry: HoldEntry;+ pending: { slug: string; pageId: number | null; intent: SyncIntent }; done: Pick<WriteRequest, 'state' | 'error_code' | 'error_message'> }): Promise<HeldEntry | null> {+ const { entry, pending, done } = input;+ const page = pending.intent.kind === 'managed_sync_import' && typeof pending.intent.content === 'string';+ const proof = page ? await concurrentWriteProof(engine, { sourceId: input.sourceId, incarnation: input.incarnation, pending, done }) : null;+ if (proof) return concurrentWriteHold(entry, pending.slug, pending.pageId!, pending.intent, proof);+ const moved = await pageMovedSinceAdmission(engine, input.sourceId, pending, done);+ if (moved) return pageChangedHold(entry, pending.slug, pending.pageId, pending.intent, { proof: null, revision: moved.revision, why: `page ${pending.slug} changed in the database` });+ return page && pinnedWorktreeConflict(done) ? worktreeDirtyHold(entry, pending.slug, pending.pageId, pending.intent) : null;+}++/**+ * The `concurrent_write` hold for an entry whose recorded origin no longer identifies the page this sync enumerated+ * (`assertSyncPageOrigin` refused at freeze time: the page at that origin was deleted, re-bound or replaced in the database).+ */+export async function originFaultHold(engine: Pick<BrainEngine, 'executeRaw'>, input: { sourceId: string; incarnation: string; entry: HoldEntry & Pick<SyncEntry, 'slug' | 'pageId' | 'revision'>;+ observed: { page: { id: number }; revision: string | null } | null; message: string }): Promise<HeldEntry> {+ const { entry } = input;+ const proof = await pageChangeProof(engine, { sourceId: input.sourceId, incarnation: input.incarnation, pageId: entry.pageId ?? null, expectedRevision: entry.revision ?? null });+ return pageChangedHold(entry, entry.slug!, input.observed?.page.id ?? entry.pageId ?? null, { content: null, blobOid: undefined, expected_revision: entry.revision ?? null },+ { proof, revision: input.observed?.revision ?? null, why: `the page recorded at ${entry.sourcePath} no longer identifies the one this sync enumerated (${input.message})` });+}diff --git a/src/core/persistence/sync-run.ts b/src/core/persistence/sync-run.tsindex be219f7339..4543398732 100644--- a/src/core/persistence/sync-run.ts+++ b/src/core/persistence/sync-run.ts@@ -43,8 +43,7 @@ import { isRetryableConnError, isStatementTimeoutError } from '../retry-matcher.import { SYNC_READ_BOUND, type TreeBlob } from './sync-blobs.ts';import { dryRunScreen, isSyncReadBound, loadSyncScreenRun, managedImageHold, pinnedBlob, preparationStalledHold, prepareTimeFenceHold, screenFrozenImport, type HeldEntry, type SyncScreenRun } from './sync-screen.ts';import { fenceReceiptLocation } from '../fence-repair/refusal.ts';-import { concurrentWriteHold, concurrentWriteProof } from './sync-concurrent-write.ts';-import { headCommittedBytes, pageChangeProof, pageChangedHold, pageMovedSinceAdmission, pinnedWorktreeConflict, worktreeDirtyHold } from './sync-page-fault.ts';+import { headCommittedBytes, originFaultHold, pageChangeProof, pageChangedHold, pinnedWorktreeConflict, receiptPageFaultHold } from './sync-page-fault.ts';import { faultPoint } from './fault-points.ts';import { pipelined } from '../page-state/transactions.ts';import { withCoordinatedWrite } from './context.ts';@@ -361,10 +360,7 @@ async function freezeEntry(engine: BrainEngine, cursor: Cursor, key: string, ass}), engine.readPageSnapshot(entry.slug!, { sourceId: cursor.sourceId, includeDeleted: true })]);assertActive();if (occupant && 'originFault' in occupant) {- const proof = await pageChangeProof(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, pageId: entry.pageId ?? null, expectedRevision: entry.revision ?? null });- assertActive();- return { hold: pageChangedHold(entry, entry.slug!, snapshot?.page.id ?? entry.pageId ?? null, { content: null, blobOid: undefined, expected_revision: entry.revision ?? null },- { proof, revision: snapshot?.revision ?? null, why: `the page recorded at ${entry.sourcePath} no longer identifies the one this sync enumerated (${occupant.originFault.message})` }), overtaken: true };+ return { hold: await originFaultHold(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, entry, observed: snapshot, message: occupant.originFault.message }), overtaken: true };}let bytes: Buffer | null = null;try { bytes = readSyncFile(cursor.root, entry.path); }@@ -635,13 +631,11 @@ async function convertBlockedCursor(engine: BrainEngine, blocked: Cursor, key: sconst compacted = !fence && failed.compacted === true && failed.error_message == null && ['invalid_params', 'take_row_collision'].includes(failed.error_code ?? '');// #6340: a cursor an older run left blocked on a page that moved under it converts the same way the run now does.if (!fence && !compacted && entry && entry.path === previous.intent.path && previous.intent.kind !== 'managed_sync_checkpoint' && !blocked.companyPlan) {- const proof = previous.intent.kind === 'managed_sync_import' ? await concurrentWriteProof(engine, { sourceId: blocked.sourceId, incarnation: blocked.incarnation, pending: previous, done: failed }) : null;- const moved = proof ? null : await pageMovedSinceAdmission(engine, blocked.sourceId, previous, failed);- const dirty = !proof && !moved && previous.intent.kind === 'managed_sync_import' && pinnedWorktreeConflict(failed);+ const hold = await receiptPageFaultHold(engine, { sourceId: blocked.sourceId, incarnation: blocked.incarnation, entry, pending: previous, done: failed });assertActive();- if (proof || moved || dirty) {+ if (hold) {// The file may have been committed meanwhile: a re-freeze then imports HEAD's bytes instead of holding the stale conflict.- if (dirty && headCommittedBytes(blocked, entry.path)?.rawHash !== undefined) {+ if (hold.code === 'worktree_dirty' && headCommittedBytes(blocked, entry.path)?.rawHash !== undefined) {const base: Cursor = { ...blocked }; delete base.pending;const again = await freezeEntry(engine, base, key, assertActive, run);const converted = [...(blocked.convertedFromFailed ?? []), previous.requestId];@@ -652,9 +646,6 @@ async function convertBlockedCursor(engine: BrainEngine, blocked: Cursor, key: sreturn saveCursor(engine, key, blocked, { ...blocked, convertedFromFailed: converted, pending: { ...again, converted: true } }, false, assertActive,tx => recordSyncConversion(tx, blocked.sourceId, blocked.incarnation, { request_id: previous.requestId, path: previous.intent.path ?? null, slug: previous.slug, run_id: blocked.runId, outcome: 'refrozen' }));}- const hold = proof ? concurrentWriteHold(entry, previous.slug, previous.pageId!, previous.intent, proof)- : moved ? pageChangedHold(entry, previous.slug, previous.pageId, previous.intent, { proof: null, revision: moved.revision, why: `page ${previous.slug} changed in the database` })- : worktreeDirtyHold(entry, previous.slug, previous.pageId, previous.intent);const held = await holdUnderGate(engine, { cursor: blocked, key, pending: previous, hold, assertActive, observedAt: run.observedAt!, waitMs: 5000, failedId: failed.id });return held === 'pending' ? blocked : held;}@@ -710,18 +701,13 @@ async function holdFailedFenceRequest(engine: BrainEngine, cursor: Cursor, key:base = { ...cursor, breaker }; delete base.pending; delete base.group; delete base.window;}const fence = stalled || !page ? null : fenceReceiptLocation(done);- // #6194 (D4): a revision conflict proven to come from a concurrent database-only write is held the same way (sync-concurrent-write.ts).- const proof = stalled || fence || !page ? null : await concurrentWriteProof(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, pending, done });- // #6340: a revision conflict whose page really moved (no proof of who moved it) and a pinned-worktree conflict (uncommitted- // local bytes) hold the page too (sync-page-fault.ts); a run-level revision conflict (the page is where it was) still blocks.- const moved = stalled || fence || proof ? null : await pageMovedSinceAdmission(engine, cursor.sourceId, pending, done);- const dirty = !stalled && !fence && !proof && !moved && page && pinnedWorktreeConflict(done);- if (!stalled && !fence && !proof && !moved && !dirty) return null;+ // #6194 (D4): a revision conflict proven to come from a concurrent database-only write is held the same way; #6340: so is one+ // whose page really moved with no proof of who moved it, and a pinned-worktree conflict (uncommitted local bytes) holds+ // `worktree_dirty` (sync-page-fault.ts). A run-level revision conflict (the page is where it was) still blocks.+ const fault = stalled || fence ? null : await receiptPageFaultHold(engine, { sourceId: cursor.sourceId, incarnation: cursor.incarnation, entry, pending, done });+ 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)- : proof ? concurrentWriteHold(entry, pending.slug, pending.pageId!, pending.intent, proof)- : moved ? pageChangedHold(entry, pending.slug, pending.pageId, pending.intent, { proof: null, revision: moved.revision, why: `page ${pending.slug} changed in the database` })- : worktreeDirtyHold(entry, pending.slug, pending.pageId, pending.intent);+ : fence ? prepareTimeFenceHold(entry, pending.slug, pending.pageId, fence, pending.intent.content!, pending.intent.blobOid) : fault!;return holdUnderGate(engine, { cursor, key, pending, hold, assertActive, observedAt: run.observedAt!, waitMs, failedId: done.id, base });}