Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): release a concurrent_write hold once its file and page are both deleted (#8751)
garrytanf995740aed51
diff --git a/src/core/persistence/sync-holds.ts b/src/core/persistence/sync-holds.tsindex b4eda66d98..4801cd82db 100644--- a/src/core/persistence/sync-holds.ts+++ b/src/core/persistence/sync-holds.ts@@ -456,7 +456,7 @@ export async function clearDeletionHold(tx: Exec, input: { sourceId: string; increturn 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`). */+/** GBRA-133: a deleted-side `file_drift` or a `concurrent_write` 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 }/**diff --git a/src/core/persistence/sync-pass.ts b/src/core/persistence/sync-pass.tsindex bf8d3a277c..7ae07c11d2 100644--- a/src/core/persistence/sync-pass.ts+++ b/src/core/persistence/sync-pass.ts@@ -287,7 +287,7 @@ export interface ShadowLevel {/** 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+ * GBRA-133: deleted-side `file_drift` and `concurrent_write` 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[];@@ -627,9 +627,12 @@ async function reclassifyByOrigin(gitRoot: string, tips: Array<string | null | ureturn true;}-/** GBRA-133: the deleted-side `file_drift` holds whose file is gone (`gone`, on the Git path); the stamp releases those whose page is no longer live. */+/**+ * GBRA-133: the deleted-side `file_drift` holds and the `concurrent_write` holds (#8751) whose file is gone (`gone`, on the+ * Git path); the stamp releases those whose page is no longer live, where both sides agree the path is deleted.+ */function deletionCandidates(holds: readonly GitHoldRecord[], gone: (path: string) => boolean, toGit: (path: string) => string): DeletionCandidate[] {- return holds.filter(hold => hold.code === 'file_drift' && fileDriftSide(hold.meta) === 'deleted' && gone(toGit(hold.path)))+ return holds.filter(hold => (hold.code === 'concurrent_write' || hold.code === 'file_drift' && fileDriftSide(hold.meta) === 'deleted') && gone(toGit(hold.path))).map(hold => ({ path: hold.path, page_id: hold.page_id ?? null, slug: hold.slug ?? null }));}diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 72ad5657dd..ca9b3bc330 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -524,6 +524,7 @@ test/sync-apply-held-deletions-gbra133.test.tstest/sync-capacity-preflight.test.tstest/sync-checkpoint-moved-6834.test.tstest/sync-classic-drift-7199.test.ts+test/sync-concurrent-write-deleted-hold-8751.test.tstest/sync-disabled-cycle-window.test.tstest/sync-discovery-legacy-origin-7130.test.tstest/sync-drain-claims.test.tsdiff --git a/test/sync-concurrent-write-deleted-hold-8751.test.ts b/test/sync-concurrent-write-deleted-hold-8751.test.tsnew file mode 100644index 0000000000..342d4b4324--- /dev/null+++ b/test/sync-concurrent-write-deleted-hold-8751.test.ts@@ -0,0 +1,79 @@+/**+ * #8751: a `concurrent_write` hold on a file that ended deleted while its page was soft-deleted is released.+ *+ * Protects: a stamping managed sync pass releases a `concurrent_write` hold once both sides agree the path is gone+ * (the file is gone from Git and no live page has the hold's id or slug), the way it releases a deleted-side+ * `file_drift` hold; the same hold on a page that is still live stays held.+ * Fails when: the pass's deletion candidates keep only `file_drift` holds again; the hold then stays `needs_human`+ * forever and its `sources reconcile` fix refuses `page_not_found`.+ * Seam: the hold is written with `writeGitHold` as the import race writes it (sync-page-fault.ts `pageChangedHold`),+ * after the deletion, as in the report; the page delete is a real `delete_page` request and every sync is a real+ * managed pass over real Git.+ */+import { expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { randomUUID } from 'node:crypto';+import { existsSync, mkdirSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { RECOVERY_VERSION } from '../src/core/markdown.ts';+import { withCoordinatedWrite } from '../src/core/persistence/context.ts';+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';+import { writeGitHold } from '../src/core/persistence/sync-holds.ts';+import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { managedBrain, settleManagedGitEffects } from './helpers/managed-brain.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';+import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';++const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, '-c', 'user.name=Example', '-c', '[email protected]',+ '-c', 'commit.gpgsign=false', ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();+const note = (slug: string, body: string) => `---\ntype: concept\ntitle: ${slug}\n---\n\n${body}\n`;+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };+const SLUGS = ['notes/retired-example', 'notes/live-example', 'notes/other-example'];++const heldPaths = async (engine: BrainEngine) => (await engine.executeRaw<{ path: string; code: string }>(+ "SELECT completed_keys->0->>'path' AS path, completed_keys->0->>'code' AS code FROM op_checkpoints WHERE op='sync-hold' ORDER BY 1"))+ .map(row => `${row.path} ${row.code}`);++for (const backend of testBackends()) {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;+ const setup = ({ root }: { root: string }) => {+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q');+ for (const slug of SLUGS) writeFileSync(join(root, `${slug}.md`), note(slug, `The committed body of ${slug}.`));+ git(root, 'add', '-A'); git(root, 'commit', '-qm', 'notes');+ };++ test(`#8751 [${backend}]: a concurrent_write hold whose file and page both ended deleted is released; one on a live page stays`, () => managedBrain(async ({ engine, ctx, root }) => {+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');+ // notes/live-example changes in the database only; notes/retired-example is deleted through a real delete_page.+ const coordinated = (sql: string, params: unknown[]) => engine.transaction(tx => withCoordinatedWrite(tx, ['default'], () => tx.executeRaw(sql, params), TEST_WRITE_ATTRIBUTION));+ await coordinated(`DELETE FROM op_checkpoints WHERE op='sync-import-provenance'+ AND completed_keys->0->>'page_id' IN (SELECT id::text FROM pages WHERE source_id='default' AND slug=$1)`, ['notes/live-example']);+ await coordinated(`UPDATE pages SET knowledge_revision=$1::uuid, revision_write_request_id=NULL, updated_at=now() - interval '1 hour'+ WHERE source_id='default' AND slug=$2`, [randomUUID(), 'notes/live-example']);+ const retired = (await engine.readPageSnapshot('notes/retired-example', { sourceId: 'default' }))!;+ await submitPageMutation(ctx, { operation: 'delete_page', params: { slug: 'notes/retired-example', request_id: randomUUID(), expected_revision: retired.revision } });+ await settleManagedGitEffects(engine);+ // Both files leave Git in one commit, and a pass sees it.+ for (const path of ['notes/retired-example.md', 'notes/live-example.md']) if (existsSync(join(root, path))) git(root, 'rm', '-q', '--', path);+ git(root, 'commit', '-qm', 'retire and remove');+ await performManagedSync(engine, OPTS);++ // The race's concurrent_write holds land after the deletion.+ for (const slug of ['notes/retired-example', 'notes/live-example']) {+ const snapshot = (await engine.readPageSnapshot(slug, { sourceId: 'default', includeDeleted: true }))!;+ const path = `${slug}.md`;+ await engine.transaction(tx => writeGitHold(tx, { source_id: 'default', incarnation: snapshot.sourceIncarnation, path, source_path: path, slug,+ page_id: snapshot.page.id, code: 'concurrent_write', message: `${path}: page ${slug} changed after this sync enumerated it.`, upstream_version: null,+ observed_at: new Date().toISOString(), run_id: null, mode: 'managed', meta: { recovery_version: RECOVERY_VERSION, page_revision: snapshot.revision } }));+ }+ expect(await heldPaths(engine)).toEqual(['notes/live-example.md concurrent_write', 'notes/retired-example.md concurrent_write']);++ writeFileSync(join(root, 'notes/other-example.md'), note('notes/other-example', 'An unrelated edit.'));+ git(root, 'commit', '-qam', 'edit other');+ await performManagedSync(engine, OPTS);+ // The live page's deletion stays held (the pass re-screens it as its deleted-side file_drift hold); only the agreed deletion is released.+ expect(await heldPaths(engine)).toEqual(['notes/live-example.md file_drift']);+ expect((await engine.readPageSnapshot('notes/live-example', { sourceId: 'default' }))!.page.deleted_at).toBeFalsy();+ }, { databaseUrl, setup }), 180_000);+}