Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#6712 PR 4b): a committed file missing from the working tree imports from its blob (GBRA-133 S-27/S-28)

garrytan7eb3eb5cd0b1
diff --git a/src/core/persistence/sync-classify.ts b/src/core/persistence/sync-classify.ts
index 224035ab8e..12342c43de 100644
--- a/src/core/persistence/sync-classify.ts
+++ b/src/core/persistence/sync-classify.ts
@@ -149,9 +149,9 @@ function afterHold(s: SyncPathState): Matched {
if ((tombstone || deletedMarker) && !s.published) return { cls: 'deleted_edited', settled: false, action: 'hold', side: 'deleted', state: tombstone ? 'tombstone_edited' : 'purged_differs' };
if (s.marker?.kind === 'legacy' && s.marker.deletion && !s.fullObligation && s.level !== null && s.marker.blobOids.includes(s.level)) return { cls: 'legacy_same', settled: true, action: 'none' };
}
- if (s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty)) {
+ if (s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty) && s.dirty !== 'deleted') {
const absorbed = (s.tree !== null && !!s.record && s.record.blobOids.includes(s.tree)) || (s.diskOid !== null && !!s.record && s.record.blobOids.includes(s.diskOid));
- const settled = absorbed || s.published || (s.dirty === 'deleted' && !live);
+ const settled = absorbed || s.published;
return { cls: 'dirty', settled, action: settled ? 'none' : 'hold' };
}
if (live) {
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index df5e63343e..be298ac665 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -371,6 +371,7 @@ test/shared-skills-writer-boundary.test.ts
test/source-retire-missing-checkout.test.ts
test/source-row-diagnostics.test.ts
test/stats-health-source-scope.test.ts
+test/sync-absent-from-worktree-s27.test.ts
test/sync-admission-6712.test.ts
test/sync-capacity-preflight.test.ts
test/sync-checkpoint-moved-6834.test.ts
diff --git a/test/sync-absent-from-worktree-s27.test.ts b/test/sync-absent-from-worktree-s27.test.ts
new file mode 100644
index 0000000000..f729af39bb
--- /dev/null
+++ b/test/sync-absent-from-worktree-s27.test.ts
@@ -0,0 +1,62 @@
+/**
+ * GBRA-133 S-27/S-28 (#6712 PR 4b): a committed file missing from the working tree (deleted and not committed, marked
+ * skip-worktree, or outside a sparse checkout) was dropped silently by the 4b managed pass: the committed-mode classifier
+ * settled a page-less path whose dirtiness probe said `deleted` as `dirty` with no action, so the file got no page and
+ * no hold. Master's managed sync imports it from the commit's blob; so does the pass now (committed mode decides from T).
+ *
+ * Protects (red before: hidden/b has no page and no hold after the sync): each shape imports hidden/b from the committed
+ * blob with the rest of the commit, holds nothing and leaves the working tree as it was, and the next sync has nothing to do; once the deletion is committed
+ * the page goes. Runs on PGLite and, with DATABASE_URL, on Postgres.
+ */
+import { describe, expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { performManagedSync } from '../src/core/persistence/sync-run.ts';
+import { readSyncStatus } from '../src/core/persistence/sync-status.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+import { testBackends } from './helpers/test-backends.ts';
+
+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
+const identity = ['-c', 'user.name=Example', '-c', '[email protected]'];
+const commit = (root: string, message: string) => { git(root, 'add', '-A'); git(root, ...identity, 'commit', '-qm', message); };
+const note = (title: string) => '---\ntype: concept\ntitle: ' + title + '\n---\n\nThe body of ' + title + '.\n';
+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };
+const setup = ({ root }: { root: string }) => {
+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q');
+ writeFileSync(join(root, 'notes/seed.md'), note('notes/seed'));
+ commit(root, 'seed');
+};
+const live = async (engine: BrainEngine) => (await engine.executeRaw<{ slug: string }>("SELECT slug FROM pages WHERE source_id='default' AND deleted_at IS NULL ORDER BY slug")).map(row => row.slug);
+type Shape = 'deleted, not committed' | 'skip-worktree' | 'outside a sparse checkout';
+const hide: Record<Shape, (root: string) => void> = {
+ 'deleted, not committed': root => rmSync(join(root, 'hidden/b.md')),
+ 'skip-worktree': root => { git(root, 'update-index', '--skip-worktree', 'hidden/b.md'); rmSync(join(root, 'hidden/b.md')); },
+ 'outside a sparse checkout': root => git(root, 'sparse-checkout', 'set', '--no-cone', '/notes/', '/*.md'),
+};
+
+for (const backend of testBackends()) describe('[' + backend + '] GBRA-133 S-27/S-28: a committed file missing from the working tree', () => {
+ const databaseUrl = backend === 'postgres' ? process.env.DATABASE_URL : undefined;
+ for (const shape of Object.keys(hide) as Shape[]) test(shape + ': imported from the committed blob, nothing held, the next sync has nothing to do', () => managedBrain(async ({ engine, root }) => {
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ writeFileSync(join(root, 'notes/a.md'), note('notes/a'));
+ mkdirSync(join(root, 'hidden'), { recursive: true });
+ writeFileSync(join(root, 'hidden/b.md'), note('hidden/b'));
+ commit(root, 'a and b');
+ hide[shape](root);
+ expect(existsSync(join(root, 'hidden/b.md'))).toBe(false);
+ const synced = await performManagedSync(engine, OPTS);
+ expect(synced).toMatchObject({ status: 'synced', added: 2, pass: { outcome: 'synced' } });
+ expect(await live(engine)).toEqual(['hidden/b', 'notes/a', 'notes/seed']);
+ expect((await engine.getPage('hidden/b', { sourceId: 'default' }))?.compiled_truth).toContain('The body of hidden/b.');
+ expect((await readSyncStatus(engine, 'default', { detail: false })).holds).toEqual([]);
+ expect(existsSync(join(root, 'hidden/b.md'))).toBe(false);
+ expect((await performManagedSync(engine, OPTS)).added ?? 0).toBe(0);
+ expect(await live(engine)).toEqual(['hidden/b', 'notes/a', 'notes/seed']);
+ if (shape !== 'deleted, not committed') return;
+ git(root, ...identity, 'rm', '-q', '--cached', 'hidden/b.md'); git(root, ...identity, 'commit', '-qm', 'delete b');
+ await performManagedSync(engine, OPTS);
+ expect(await live(engine)).toEqual(['notes/a', 'notes/seed']);
+ }, { databaseUrl, setup }), 180_000);
+});
diff --git a/test/sync-classify-6712.test.ts b/test/sync-classify-6712.test.ts
index cb78158ed4..f047b04ac8 100644
--- a/test/sync-classify-6712.test.ts
+++ b/test/sync-classify-6712.test.ts
@@ -28,8 +28,7 @@ function reference(s: SyncPathState): SyncClass {
const rest = referenceAfterHold(s);
if (!s.hold) return rest;
if (s.level !== null && (rest === 'matched' || rest === 'import')) return 'held';
- const live = s.page?.live === true;
- const dirtySettled = (s.tree !== null && !!s.record?.blobOids.includes(s.tree)) || (s.diskOid !== null && !!s.record?.blobOids.includes(s.diskOid)) || s.published || (s.dirty === 'deleted' && !live);
+ const dirtySettled = (s.tree !== null && !!s.record?.blobOids.includes(s.tree)) || (s.diskOid !== null && !!s.record?.blobOids.includes(s.diskOid)) || s.published;
return SETTLED.has(rest) || (rest === 'dirty' && dirtySettled) ? 'held' : rest;
}
@@ -39,7 +38,7 @@ function referenceAfterHold(s: SyncPathState): SyncClass {
const fileSame = s.level !== null && absorbed.has(s.level);
const dbSame = s.page !== null && s.record !== null && s.record.revision !== null && s.record.revision === s.page.revision;
const proof = dbSame || s.attested !== null;
- const dirtyCommitted = s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty);
+ const dirtyCommitted = s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty) && s.dirty !== 'deleted';
const dirtyClass = (): SyncClass => 'dirty';
const state = s.page === null ? 'none' : s.page.live ? 'live' : 'tombstone';
if (state !== 'live') {
@@ -230,14 +229,23 @@ describe('P0 properties', () => {
}
});
- test('fail closed: a committed path that is not clean never imports, adopts or writes DB-wins', () => {
+ test('fail closed: a committed path with uncommitted bytes never imports, adopts or writes DB-wins', () => {
fc.assert(fc.property(stateArb, s => {
- if (s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty) && !s.spelling && !(s.hold?.atLevel && !s.hold.rescreenDue && !s.hold.retried)) {
+ if (s.mode === 'committed' && s.tree !== null && !CLEAN.has(s.dirty) && s.dirty !== 'deleted' && !s.spelling && !(s.hold?.atLevel && !s.hold.rescreenDue && !s.hold.retried)) {
expect(['import', 'adopt', 'reparse', 'keep_db']).not.toContain(classifySyncPath(s).action);
}
}), { numRuns: 40_000, seed: 67122 });
});
+ test('GBRA-133 S-27/S-28: a committed path no file backs (deleted, skip-worktree, sparse) classifies at its committed level, as a clean one does, except for the DB-wins write', () => {
+ fc.assert(fc.property(stateArb, s => {
+ if (s.mode !== 'committed' || s.tree === null || s.dirty !== 'deleted') return;
+ const got = classifySyncPath(s), clean = classifySyncPath({ ...s, dirty: 'clean' });
+ expect(got.cls === 'keep_db' ? 'drift' : got.cls).toBe(clean.cls === 'keep_db' ? 'drift' : clean.cls);
+ expect(got.action).not.toBe('keep_db');
+ }), { numRuns: 40_000, seed: 67133 });
+ });
+
test('no DB-wins write without keep_db on a clean committed path, and never for unproven pages', () => {
fc.assert(fc.property(stateArb, s => {
const got = classifySyncPath(s);