Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

sync: the stamp checks a released deletion hold's page in the database, not the level's origin map (a narrowed and a whole level agree)

garrytana979b40b60d8
diff --git a/src/core/persistence/sync-holds.ts b/src/core/persistence/sync-holds.ts
index 009bb0fae2..ce7ec1673c 100644
--- a/src/core/persistence/sync-holds.ts
+++ b/src/core/persistence/sync-holds.ts
@@ -451,6 +451,21 @@ export async function clearDeletionHold(tx: Exec, input: { sourceId: string; inc
return clearGitHold(tx, { ...input, observedAt: hold.observed_at });
}
+/** GBRA-133: a deleted-side `file_drift` hold whose file is gone from the level and the disk (sync-pass.ts `deletionCandidates`). */
+export interface DeletionCandidate { path: string; page_id: number | null; slug: string | null }
+
+/**
+ * GBRA-133: the candidates whose page is no longer live (no live page has its id or its slug), so both sides of the held
+ * deletion agree and the stamp releases the hold. One statement; none without candidates.
+ */
+export async function settledDeletionHolds(tx: Exec, sourceId: string, candidates: readonly DeletionCandidate[] | undefined): Promise<string[]> {
+ if (!candidates?.length) return [];
+ const live = await tx.executeRaw<{ id: string; slug: string }>('SELECT id::text AS id, slug FROM pages WHERE source_id=$1 AND deleted_at IS NULL AND (id = ANY($2::bigint[]) OR slug = ANY($3::text[]))',
+ [sourceId, candidates.flatMap(c => c.page_id === null ? [] : [c.page_id]), candidates.flatMap(c => c.slug === null ? [] : [c.slug])]);
+ const ids = new Set(live.map(row => row.id)), slugs = new Set(live.map(row => row.slug));
+ return candidates.filter(c => (c.page_id === null || !ids.has(String(c.page_id))) && (c.slug === null || !slugs.has(c.slug))).map(c => c.path);
+}
+
/** #5984: which of these paths have a hold row (one indexed read), so a batched waiver run clears only those. */
export async function heldGitPaths(tx: Exec, sourceId: string, incarnation: string, paths: readonly string[]): Promise<Set<string>> {
if (!paths.length) return new Set();
diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.ts
index f58200784b..a23e1be055 100644
--- a/src/core/persistence/sync-pass-run.ts
+++ b/src/core/persistence/sync-pass-run.ts
@@ -42,7 +42,7 @@ import { assertDrainLease, currentDrainLease } from './sync-lease.ts';
import { idleClear, readIdleFacts } from './sync-idle-skips.ts';
import { digest } from './digest.ts';
import { readSyncTail } from './sync-tail-record.ts';
-import { clearGitHold, clearGitHoldRetryPaths, holdRescreenDue, readGitHoldRetryPaths, readSyncHoldPolicy, readGitSourceHolds, readSyncImportProvenances, syncImportParserFor } from './sync-holds.ts';
+import { clearGitHold, clearGitHoldRetryPaths, holdRescreenDue, readGitHoldRetryPaths, readSyncHoldPolicy, readGitSourceHolds, readSyncImportProvenances, settledDeletionHolds, syncImportParserFor } from './sync-holds.ts';
import { loadActivePackForEngine } from '../schema-pack/engine-resolution.ts';
import { loadConfig } from '../config.ts';
import { startPersistenceConsumer } from './service.ts';
@@ -551,7 +551,7 @@ async function stampUnchangedPass(engine: BrainEngine, input: SyncPassInput, rou
await assertDrainLease(tx, sourceId);
await tx.executeRaw('UPDATE sources SET last_sync_at=now() WHERE id=$1 AND incarnation=$2::uuid', [sourceId, incarnation]);
// Holds on paths that left the level go with it here too, as a stamp releases them.
- for (const path of level.releasableHolds) await clearGitHold(tx, { sourceId, incarnation, path, observedAt: startedAt });
+ for (const path of [...level.releasableHolds, ...await settledDeletionHolds(tx, sourceId, level.deletionCandidates)]) await clearGitHold(tx, { sourceId, incarnation, path, observedAt: startedAt });
await recordPassFenceTrend(tx, { worktreeId: context.binding.worktree_id, sourceId, passId, since: converged?.request_hwm ?? null });
const watermark = await readConvergedWatermark(tx, { worktreeId: context.binding.worktree_id, sourceId, incarnation }, null, OPEN_AT_STAMP_CAP, { prior: converged?.writeback });
value.obligations.converged = { target: head, at, ...watermark, parser_fp: parserFp, pack, selection_fp: selectionFp,
@@ -623,7 +623,7 @@ async function passStampFor(engine: BrainEngine, input: { context: ManagedSyncCo
const closesEpisode = closure?.closes === true;
if (closure?.released) counts.episodeReleased = closure.released;
return { leaseToken: input.leaseToken, incarnation: context.incarnation, configured, stampChunker: configured && (context.source.last_commit === null || (closesEpisode && !!episode?.full_id && closure!.stampChunker)),
- closesEpisode, retryTaken: [...input.retry], releasedHolds: input.level.releasableHolds, selectionFp: input.selectionFp, parserFp: input.parserFp, pack: input.pack, full: input.full,
+ closesEpisode, retryTaken: [...input.retry], releasedHolds: input.level.releasableHolds, ...(input.level.deletionCandidates.length ? { deletionCandidates: input.level.deletionCandidates } : {}), selectionFp: input.selectionFp, parserFp: input.parserFp, pack: input.pack, full: input.full,
changed: counts.added + counts.modified + counts.deleted + counts.renamed, extract: input.extract, since: input.since, ...(input.rescreen ? { rescreen: true as const } : {}) };
}
diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.ts
index e90dd7580e..1aa2ffbee5 100644
--- a/src/core/persistence/sync-pass.ts
+++ b/src/core/persistence/sync-pass.ts
@@ -32,7 +32,7 @@ import { sourceMirrorReadOnly } from './mirror-read-only.ts';
import { colonFileRefusal, COLON_REFUSAL_MESSAGES, readSyncFile, resolveManagedSyncContext, syncExcludePatterns, syncGitAsync, type ManagedSyncContext, type SyncFileRefusal } from './sync-discovery.ts';
import { isWindowsColonTarget } from './native-file-target.ts';
import { confirmedSyncDelta } from '../sync-rename-identity.ts';
-import { fileDriftSide, holdBasisMoved, holdRescreenDue, readGitHoldRetryPaths, readGitSourceHolds, SYNC_IMPORT_PROVENANCE_OP, syncImportParser, syncImportParsers,
+import { fileDriftSide, holdBasisMoved, holdRescreenDue, type DeletionCandidate, readGitHoldRetryPaths, readGitSourceHolds, SYNC_IMPORT_PROVENANCE_OP, syncImportParser, syncImportParsers,
type GitHoldRecord, type SyncImportParser, type SyncImportProvenance } from './sync-holds.ts';
import { movedDriftHolds, sameParser } from './sync-prescreen.ts';
import { sameSyncOrigin, selectOriginClaimant, syncOriginPath, type SyncOriginScope } from './sync-origin.ts';
@@ -268,6 +268,11 @@ export interface ShadowLevel {
heldRenames: Array<{ path: string; rename: { sourcePath: string; slug: string; pageId: number; revision: string } }>;
/** Holds on paths outside the level's domain (no file at L, no page, or no longer eligible): the stamp releases them, as discovery did (#6278 B7). */
releasableHolds: string[];
+ /**
+ * GBRA-133: deleted-side `file_drift` holds whose file is gone from `L` and the disk. The stamp releases each whose page is
+ * no longer live (`settledDeletionHolds`): a soft delete, a purge or the file's removal decided it.
+ */
+ deletionCandidates: DeletionCandidate[];
/** Working mode: the stat cache this round's rehash left (`hashWorkingLevel`); the stamp's round is the one a pass remembers. */
workingCache: WorkingStatCache | null;
/** Live file-backed pages whose origin is eligible: the mass-delete valve's denominator (#2828). */
@@ -894,11 +899,12 @@ export async function readShadowLevel(engine: BrainEngine, input: { sourceId: st
}
return { sourceId, incarnation, root, gitRoot, scope, slugMode, mode, head, baselineCommit, full: input.full === true, kind: kind.kind, kindBlockers: kind.blockers,
heldRenames: holds.flatMap(hold => hold.meta.rename_from ? [{ path: toGit(hold.path), rename: hold.meta.rename_from }] : []),
- releasableHolds: holds.filter(hold => {
- const path = toGit(hold.path), at = byOrigin.get(path);
- return !domain.has(path) || deletionSettled(hold, tree.has(path) || (mode === 'working' && (untracked.has(path) || index.has(path))),
- at !== undefined && !pages.deleted(at), () => readPresent(root, path, scope));
- }).map(hold => hold.path),
+ releasableHolds: holds.filter(hold => !domain.has(toGit(hold.path))).map(hold => hold.path),
+ deletionCandidates: holds.filter(hold => {
+ const path = toGit(hold.path);
+ return hold.code === 'file_drift' && fileDriftSide(hold.meta) === 'deleted' && domain.has(path) && !tree.has(path)
+ && !(mode === 'working' && (untracked.has(path) || index.has(path))) && !readPresent(root, path, scope);
+ }).map(hold => ({ path: hold.path, page_id: hold.page_id ?? null, slug: hold.slug ?? null })),
paths, reconcilable, ...(colon.refusals.size ? { fileRefusals: [...colon.refusals.values()] } : {}), timings_ms: timings, statements: statements + attestationStatements + 3,
...(confirm ? { confirm: narrow ? { ...confirm, narrow: narrow === 'taken' && !reuse ? 'classified' : narrow } : confirm } : {}), ...(reuse ? { reused } : {}), ...(treeReuse ? { treeReuse } : {}), pack: pack?.identity ?? null,
selection: { exclude: [...exclude].sort(), includeHidden: [...includeHidden].sort(), strategy, srcSubpath: scope || null, mode },
@@ -1163,14 +1169,6 @@ function readPresent(root: string, gitPath: string, scope: string): boolean {
try { return readSyncFile(root, path) !== null; } catch { return true; }
}
-/**
- * GBRA-133: a deleted-side `file_drift` hold whose two sides agree again (no file at `L` or on disk, no live page at the
- * origin) waits for nothing: a soft delete, a purge or the file's removal decided it, so the stamp releases it.
- */
-function deletionSettled(hold: GitHoldRecord, inLevel: boolean, live: boolean, present: () => boolean): boolean {
- return hold.code === 'file_drift' && fileDriftSide(hold.meta) === 'deleted' && !inLevel && !live && !present();
-}
-
/** Whether a hold still describes the path at level `L`, in the hold's own domain (a pinned blob, working bytes, or a deletion). */
function holdAtLevel(hold: GitHoldRecord, level: string | null, diskSha256: string | null): boolean {
if (hold.meta.deleted) return level === null;
diff --git a/src/core/persistence/sync-prepare.ts b/src/core/persistence/sync-prepare.ts
index 644c9fdb4e..e15a5d4981 100644
--- a/src/core/persistence/sync-prepare.ts
+++ b/src/core/persistence/sync-prepare.ts
@@ -57,7 +57,7 @@ import { readConvergedWatermark } from './sync-pass-batch.ts';
import { foldPassCheckpoint, type PassFold } from './sync-pass-fold.ts';
import { frontmatterSlugConflictMessage } from './verb-errors.ts';
import { chunkerStamp } from '../chunkers/code.ts';
-import { addFencesNormalized, clearGitHold, clearGitHoldRetryPaths, countGitHolds, recordSyncImportProvenance, type FencesTally, syncDiskProvenance, syncImportParser, syncImportParserFor, type SyncImportProvenanceBase } from './sync-holds.ts';
+import { addFencesNormalized, clearGitHold, clearGitHoldRetryPaths, countGitHolds, recordSyncImportProvenance, settledDeletionHolds, type DeletionCandidate, type FencesTally, syncDiskProvenance, syncImportParser, syncImportParserFor, type SyncImportProvenanceBase } from './sync-holds.ts';
import { clearSyncSlice, writeSyncSlice, type SyncSliceState } from './sync-slices.ts';
import { CHECKPOINT_MOVED_MESSAGE, emptySyncScope, pinnedSlugModeMismatch, slugModeMismatchCause } from './sync-slug-mode.ts';
import { readPagePurgeTombstones } from './page-purge.ts';
@@ -134,6 +134,8 @@ export interface PassStamp {
closesEpisode: boolean;
/** `sources retry-held` paths this pass re-screened (compare-and-delete) and held paths that left the source. */
retryTaken: string[]; releasedHolds: string[];
+ /** GBRA-133: deleted-side holds whose file is gone; the stamp releases those whose page is no longer live. */
+ deletionCandidates?: DeletionCandidate[];
selectionFp: string; parserFp: string; pack: string | null;
/** The pass ran a full round (resets `passes_since_full`). */
full: boolean;
@@ -547,7 +549,7 @@ export async function publishPassStamp(tx: BrainEngine, row: WriteRequest, p: Sy
}
await recordPassTail(tx, row.source_id, p.passId!, { changed: read.changed, extract: stamp.extract, ...(stamp.since ? { since: stamp.since } : {}) });
await recordPassFenceTrend(tx, { worktreeId: row.worktree_id!, sourceId: row.source_id, passId: p.passId!, since: pass.value.obligations.converged?.request_hwm ?? null });
- for (const path of stamp.releasedHolds) await releaseHold(tx, path);
+ for (const path of [...stamp.releasedHolds, ...await settledDeletionHolds(tx, row.source_id, stamp.deletionCandidates)]) await releaseHold(tx, path);
if (stamp.retryTaken.length) await clearGitHoldRetryPaths(tx, row.source_id, stamp.incarnation, stamp.retryTaken);
const watermark = read.watermark ?? await readConvergedWatermark(tx, { worktreeId: row.worktree_id!, sourceId: row.source_id, incarnation: row.source_incarnation }, row.request_id, OPEN_AT_STAMP_CAP,
{ prior: pass.value.obligations.converged?.writeback });