Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
sync: read sync.apply_held_deletions once per pass in one statement (statement budget)
garrytan3d206a1818a4
diff --git a/src/core/persistence/sync-held-deletions.ts b/src/core/persistence/sync-held-deletions.tsindex 19df3c32b4..394bc84992 100644--- a/src/core/persistence/sync-held-deletions.ts+++ b/src/core/persistence/sync-held-deletions.ts@@ -46,19 +46,20 @@ const TRANSIENT: ReadonlySet<HeldDeletionRefusal> = new Set(['provenance_unknownexport interface HeldDeletionState { screened: Set<string>; applied: number; refused: number }export const newHeldDeletionState = (): HeldDeletionState => ({ screened: new Set(), applied: 0, refused: 0 });-/** Whether the switch is on for `sourceId` (the per-source key wins over the brain-wide one; only `true` turns it on). */-export async function heldDeletionsEnabled(engine: { getConfig(key: string): Promise<string | null> }, sourceId: string): Promise<boolean> {- const value = (await engine.getConfig(`${SYNC_APPLY_HELD_DELETIONS_KEY}.source.${sourceId}`)) ?? await engine.getConfig(SYNC_APPLY_HELD_DELETIONS_KEY);+/** Whether the switch is on for `sourceId` (the per-source key wins over the brain-wide one; only `true` turns it on): one read, once per pass. */+export async function heldDeletionsEnabled(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string): Promise<boolean> {+ const own = `${SYNC_APPLY_HELD_DELETIONS_KEY}.source.${sourceId}`;+ const rows = await engine.executeRaw<{ key: string; value: string | null }>('SELECT key, value FROM config WHERE key = ANY($1::text[])', [[own, SYNC_APPLY_HELD_DELETIONS_KEY]]);+ const value = rows.find(row => row.key === own)?.value ?? rows.find(row => row.key === SYNC_APPLY_HELD_DELETIONS_KEY)?.value;return value?.trim().toLowerCase() === 'true';}/*** Settles the round's held committed deletions the proof accepts as deletes (`heldDeletion` on the result) and marks* the ones it refuses (`heldDeletionRefused`), turning a settled hold back into a `delete_drift` so the pass records the- * refusal on it. A no-op while the switch is off.+ * refusal on it. The pass reads the switch once (`heldDeletionsEnabled`) and calls this only while it is on.*/export async function applyHeldDeletions(engine: BrainEngine, level: ShadowLevel, context: ManagedSyncContext, state: HeldDeletionState): Promise<void> {- if (!await heldDeletionsEnabled(engine, level.sourceId)) return;const holdPath = (entry: ShadowPath) => level.scope ? entry.path.slice(level.scope.length + 1) : entry.path;const page = (entry: ShadowPath) => entry.slug !== null && entry.pageId !== null && entry.revision !== null && entry.state.page?.live === true;const fresh = level.paths.filter(entry => entry.result.cls === 'delete_drift' && entry.result.state === 'file_gone' && !entry.result.uncommitted && !entry.result.settled && !entry.result.inFlight && page(entry));diff --git a/src/core/persistence/sync-pass-apply.ts b/src/core/persistence/sync-pass-apply.tsindex c21e36b145..66d4c09adc 100644--- a/src/core/persistence/sync-pass-apply.ts+++ b/src/core/persistence/sync-pass-apply.ts@@ -126,6 +126,8 @@ export interface PassApplyInput {screen?: ConvergenceScreen;/** GBRA-108: after each committed legacy-sweep slice (the pass saves its row and reports progress). */onSwept?: () => Promise<void>;+ /** GBRA-133: `sync.apply_held_deletions` is on for the source (read once per pass, sync-held-deletions.ts). */+ heldDeletions?: boolean;}/** The source-relative path (`SyncEntry.path`, the hold key) and the recorded origin of a level path. */@@ -154,7 +156,7 @@ export async function applyPassRound(engine: BrainEngine, level: ShadowLevel, inif (!grantSees(level, entry, input.grantPrefixes) && !entry.result.settled) settleAs(entry, { settled: true, action: 'none', classAction: 'none' });}}- if (!input.remote && input.admission === 'normal') await applyHeldDeletions(engine, level, input.context, state.heldDeletions);+ if (input.heldDeletions && !input.remote && input.admission === 'normal') await applyHeldDeletions(engine, level, input.context, state.heldDeletions);await applyDeleteValve(engine, level, input, state);if (input.remote) {// #6948: a remote pass writes no hold, so a path several live pages claim is left as it is for the owner's pass to hold.diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex 586a318b3d..76079e25a3 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -440,7 +440,7 @@ async function pinPass(engine: BrainEngine, input: SyncPassInput, lease: NonNull// The incarnation's legacy markers (§2.7 b1) are owed until a local pass wrote them, not only by its first row.const markersOwed = () => remote ? !prior : !value.obligations.legacy_markers_at;return { prior, admission, tail, retry, failed, holds, idleFacts, head, mode, selection, configured, selectionFp, parserFp, obligations, passId, optOutEpisode, episode,- processingOptions, inherited, pack, converged, cacheKey, kindStarted, working, kind, value, markersOwed };+ processingOptions, inherited, pack, converged, cacheKey, kindStarted, working, kind, value, markersOwed, heldDeletions };}/**@@ -686,7 +686,7 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prconst pinned = await pinPass(engine, input, lease, started);if (pinned.ended) return pinned.ended;const { prior, admission, tail, retry, failed, mode, selection, configured, selectionFp, parserFp, obligations, passId, optOutEpisode,- processingOptions, inherited, pack, converged, cacheKey, working, kind, value, markersOwed } = pinned;+ processingOptions, inherited, pack, converged, cacheKey, working, kind, value, markersOwed, heldDeletions } = pinned;let { head, episode } = pinned;if (kind === 'idle' && working.cache) rememberWorkingStatCache(cacheKey, working.cache);@@ -855,7 +855,7 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Prawait writeLegacyMarkers(engine, level);value.obligations.legacy_markers_at = new Date().toISOString();}- const roundInput = { passId, observedAt: startedAt, remote, admission, fullRound: kind === 'full', obligations: value.obligations, context, processingOptions, grantPrefixes: remote ? authority.writer.slugPrefixes : null, onSwept };+ const roundInput = { passId, observedAt: startedAt, remote, admission, fullRound: kind === 'full', obligations: value.obligations, context, processingOptions, grantPrefixes: remote ? authority.writer.slugPrefixes : null, onSwept, heldDeletions };await applyPassRound(engine, level, roundInput, applied);assertActive();if (!remote) value.obligations.deletes_withheld = applied.withheld?.set_hash ?? null;