Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): clear conversion holds only after the strict recheck passes (#7617)

garrytan54390429e312
diff --git a/src/core/persistence/git-conversion.ts b/src/core/persistence/git-conversion.ts
index 97c48a3022..f14eba6c95 100644
--- a/src/core/persistence/git-conversion.ts
+++ b/src/core/persistence/git-conversion.ts
@@ -27,9 +27,10 @@
* refuses (git_conversion_blocked, naming the categories) unless it is zero; there is no override.
* The preview hash binds HEAD, the source incarnation and every planned item with its revision.
*
- * The apply re-plans and refuses preview_changed on a different hash, adopts group by group (a crash leaves the source
- * non-strict and the rerun preview lists only what is left), clears the holds, retries or reclassifies the parked
- * effects, checks the strict level once more (an overwrite nobody accepted stops it), runs the full strict-equivalent
+ * The apply re-plans and refuses preview_changed on a different hash, adopts and withholds group by group (a crash leaves the source
+ * non-strict and the rerun preview lists only what is left), retries or reclassifies the parked effects, checks the
+ * strict level once more as it will read with the planned holds cleared (an overwrite nobody accepted stops it, and the
+ * holds stay), clears the holds, runs the full strict-equivalent
* pass (SyncOpts.canonicalOverride, internal) and sets `canonical: git` last, in the transaction that records the
* conversion. Writes can land between that pass and the flag, so it then runs one genuine strict pass and plans again:
* only a plan with no work left is a completed conversion; otherwise the source stays strict (Git wins) and the
@@ -405,18 +406,23 @@ export async function applyGitConversion(engine: BrainEngine, sourceId: string,
if (plan.preview.discard.count) throw blockedError(plan.preview);
const committed = await adoptGroups(engine, plan, previewCommand);
const withheld = await withholdGroups(engine, plan, previewCommand);
- let holdsCleared = 0;
- for (const hold of plan.holds) {
- if (await engine.transaction(tx => clearGitHold(tx, { sourceId, incarnation: plan.incarnation, path: hold.path, observedAt: hold.observed_at }))) holdsCleared++;
- }
const parked = await settleParked(engine, sourceId, plan.parked);
+ // The planned holds are cleared only once this check passes, so a refusal keeps them: a held path is judged as the
+ // strict pass will see it once its hold is cleared.
+ const held = new Set(plan.holds.map(hold => hold.path));
const recheck = await readShadowLevel(engine, { sourceId, full: true, context: strictContext(plan.context) });
const preview = { ...plan.preview, discard: emptyDiscard() };
for (const entry of recheck?.paths ?? []) {
const path = sourcePath(recheck!.scope, entry.path);
- if (!entry.result.settled && !entry.result.inFlight && entry.result.gitWins && !plan.settled.has(path)) tally(preview.discard, 'git_overwrites_database', path);
+ if (plan.settled.has(path)) continue;
+ const result = held.has(path) ? classifySyncPath({ ...entry.state, hold: null }, { strict: true }) : entry.result;
+ if (!result.settled && !result.inFlight && result.gitWins) tally(preview.discard, 'git_overwrites_database', path);
}
if (preview.discard.count) throw blockedError(preview);
+ let holdsCleared = 0;
+ for (const hold of plan.holds) {
+ if (await engine.transaction(tx => clearGitHold(tx, { sourceId, incarnation: plan.incarnation, path: hold.path, observedAt: hold.observed_at }))) holdsCleared++;
+ }
const processing = { noEmbed: opts.noEmbed, noExtract: opts.noExtract };
const { performManagedSync } = await import('./sync-run.ts');
passOrThrow(sourceId, await performManagedSync(engine, { sourceId, noPull: true, full: true, ...processing, canonicalOverride: 'git' }));
diff --git a/test/git-canonical-conversion-g8a.test.ts b/test/git-canonical-conversion-g8a.test.ts
index 9948634823..3349fb59f1 100644
--- a/test/git-canonical-conversion-g8a.test.ts
+++ b/test/git-canonical-conversion-g8a.test.ts
@@ -21,6 +21,7 @@ import { installFaultHook } from '../src/core/persistence/fault-points.ts';
import { runPersistenceEffects } from '../src/core/persistence/effects.ts';
import { localHostId } from '../src/core/persistence/identity.ts';
import { acquireWorktree } from '../src/core/persistence/ownership.ts';
+import { writeGitHold } from '../src/core/persistence/sync-holds.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { resolveManagedSyncContext } from '../src/core/persistence/sync-discovery.ts';
import { ADOPTION_BATCH, applyGitConversion, applyMixedConversion, CONVERSION_GROUP_MAX, planGitConversion, planMixedConversion } from '../src/core/persistence/git-conversion.ts';
@@ -450,6 +451,31 @@ for (const backend of testBackends()) describe(`[${backend}] G8a: sources canoni
expect(await revisions(engine)).toEqual(settled);
}, { databaseUrl, setup }), 180_000);
+ test('#7618: a refusal only the apply\'s recheck catches keeps the holds the plan would have cleared', () => managedBrain(async brain => {
+ const { engine, root } = brain;
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await databaseOnlyEdit(engine, 'notes/ahead', 'Written to the database alone.');
+ // notes/same: declared database-only, its row equal to its committed file, and held. The plan places nothing there.
+ await engine.transaction(tx => withCoordinatedWrite(tx, ['default'], () => tx.executeRaw(
+ "UPDATE pages SET database_only_reason='db_only' WHERE source_id='default' AND slug='notes/same'"), TEST_WRITE_ATTRIBUTION));
+ const [{ incarnation }] = await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text AS incarnation FROM sources WHERE id='default'");
+ await engine.transaction(tx => writeGitHold(tx, { source_id: 'default', incarnation, path: 'notes/same.md', source_path: 'notes/same.md', slug: 'notes/same',
+ page_id: null, code: 'concurrent_write', message: 'Held while another writer moved the page.', upstream_version: null, observed_at: new Date().toISOString(),
+ run_id: 'run-1', mode: 'managed', meta: { recovery_version: 1 } }));
+ const base = git(root, 'rev-parse', 'HEAD');
+ const { preview } = await planGitConversion(engine, 'default');
+ expect(preview).toMatchObject({ adopt: { count: 1 }, holds: { count: 1, unplaced: { count: 1 } }, discard: { count: 0 } });
+ // After the first group commits, a database edit lands on the held page: Git's import would now overwrite it.
+ installFaultHook(async point => { if (point === 'conversion:group_committed') await databaseOnlyEdit(engine, 'notes/same', 'A late database edit.'); });
+ try {
+ await expect(applyGitConversion(engine, 'default', preview.preview_hash, PROCESSING)).rejects.toMatchObject({ code: 'git_conversion_blocked', detail: 'git_overwrites_database' });
+ } finally { installFaultHook(undefined); }
+ expect(await holds(engine)).toEqual(['notes/same.md concurrent_write']);
+ expect(await strict(engine)).toBe(false);
+ expect(conversionCommits(root, base)).toHaveLength(1);
+ expect((await page(engine, 'notes/same'))!.body).toContain('A late database edit.');
+ }, { databaseUrl, setup }), 180_000);
+
test('work that lands after the strict-equivalent pass leaves the source strict and reports incomplete', () => managedBrain(async brain => {
const { engine, root } = brain;
expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');