Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Repair file-drift leaves an untracked or gitignored twin file alone

garrytan009818cf3f1b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 24dbecea03..ea64740117 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,15 @@ credits are retained; no result has been reassigned to another provider. Origina
identifiers and attribution are available in the pre-removal Git revision
`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).
+## [Unreleased]
+
+**`gbrain repair file-drift` no longer offers to adopt an untracked or gitignored file into an origin-twin page.** v0.60.629.0 taught the drift preview to read an origin twin with no recorded file against the file at its slug path, but only where sync admits that path. It didn't check that sync reads the file at all. Sync imports from the HEAD commit (and the working tree only with `sync.include_working_tree` or a detached HEAD, never an ignored file), so a slug-path file that Git doesn't track, or that `.gitignore` lists, is never imported or held. The preview still reported it as drift: it offered `--prefer file` to copy a possibly private, ignored file into the database, maintenance writes to the page were refused as drift, and doctor `maintenance_write_drift` counted it. Such a twin now stays `database_only`. An untracked file that sync does read from the working tree is still read as drift.
+
+### Itemized changes
+
+- `src/core/persistence/drift-conflicts.ts`: `twinReading` reads `adopt` only when sync reads the slug-path file: committed at HEAD, or, when sync reads the working tree, not ignored by Git.
+- Tests: `test/repair-file-drift-origin-twin-gbra108.test.ts` (PGLite, managed source, real git): an untracked slug-path file and a gitignored one under `sync.include_working_tree` stay `database_only` with no sync hold, and an untracked file sync reads from the working tree still reads drift while sync holds it.
+
## [0.60.629.0] - 2026-11-09
**`gbrain repair file-drift` now agrees with sync on a held origin-twin page.** A legacy page left as an origin twin with no recorded file (no import record) could be held `file_drift` by sync while `repair file-drift --slug <slug>` reported `no_drift`. The hold's own suggested fix couldn't clear it, and scripts that use the preview as a drift check passed falsely. The preview now reads such a twin against its slug-path file, the same way sync imports it, and lists it with an apply command.
diff --git a/src/core/persistence/drift-conflicts.ts b/src/core/persistence/drift-conflicts.ts
index 115fd47950..56ef6fdc68 100644
--- a/src/core/persistence/drift-conflicts.ts
+++ b/src/core/persistence/drift-conflicts.ts
@@ -134,8 +134,8 @@ export async function liveFileDrift(engine: BrainEngine, sourceId: string, slug:
* GBRA-108: how liveFileDrift reads an origin twin with no recorded file, in a source that is not a read-only mirror (no
* repair writes there): `db_only` when its slug path is under a declared storage.db_only directory and holds a file (one
* or several), which sync imports into the twin but no repair adopts; `adopt` when it adopts its slug path as reconcile
- * does (adoptableSlugOrigin) and sync admits that path (loadSyncEligibility: scope, sync.exclude, hidden and reserved
- * paths); null (prepareFileTarget's database-only reading) otherwise, including a slug path several files import as.
+ * does (adoptableSlugOrigin), sync admits that path (loadSyncEligibility: scope, sync.exclude, hidden and reserved
+ * paths) and sync reads its file (syncReadsFile: committed at HEAD, or not ignored in a working tree sync reads); null (prepareFileTarget's database-only reading) otherwise, including a slug path several files import as.
* Any other refusal or failure throws.
*/
async function twinReading(engine: BrainEngine, sourceId: string, root: string, page: PageSnapshot): Promise<'adopt' | 'db_only' | null> {
@@ -150,13 +150,26 @@ async function twinReading(engine: BrainEngine, sourceId: string, root: string,
const gitRoot = realpathSync(syncGit(real, ['rev-parse', '--show-toplevel']).trim());
const { nativeRoot, nativeGitRoot, eligible } = await loadSyncEligibility(engine, { root: real, gitRoot, sourceId,
source: { last_commit: source?.last_commit ?? null, config: source?.config ?? {} } });
- return eligible(relative(nativeGitRoot, join(nativeRoot, relative(real, derived.path))).split(sep).join('/')) ? 'adopt' : null;
+ const gitPath = relative(nativeGitRoot, join(nativeRoot, relative(real, derived.path))).split(sep).join('/');
+ return eligible(gitPath) && await syncReadsFile(engine, gitRoot, gitPath) ? 'adopt' : null;
} catch (error) {
if (error instanceof OperationError && error.detail === 'slug_path_ambiguous') return publishesDatabaseOnly(real, slug, page) ? 'db_only' : null;
throw error;
}
}
+/**
+ * GBRA-108: whether sync reads a Git path's file at all, as discoverManagedSync does: from the HEAD commit, or also the
+ * working tree (detached HEAD or sync.include_working_tree), where an untracked file counts unless Git ignores it.
+ */
+async function syncReadsFile(engine: BrainEngine, gitRoot: string, gitPath: string): Promise<boolean> {
+ try { syncGit(gitRoot, ['cat-file', '-e', `HEAD:${gitPath}`]); return true; } catch { /* not in the HEAD commit */ }
+ const working = syncGit(gitRoot, ['rev-parse', '--abbrev-ref', 'HEAD']).trim() === 'HEAD' || await engine.getConfig('sync.include_working_tree') === 'true';
+ if (!working) return false;
+ try { syncGit(gitRoot, ['check-ignore', '-q', '--', gitPath]); return false; }
+ catch (error) { if ((error as { status?: number }).status === 1) return true; throw error; }
+}
+
/**
* GBRA-108: whether another live page records an origin twin's slug-path file as its source path, so sync imports that
* file there and not into the twin. A page naming it only by file URI does not count: sync routes by source path. False
diff --git a/test/repair-file-drift-origin-twin-gbra108.test.ts b/test/repair-file-drift-origin-twin-gbra108.test.ts
index 7f8fe0a578..a3b271e478 100644
--- a/test/repair-file-drift-origin-twin-gbra108.test.ts
+++ b/test/repair-file-drift-origin-twin-gbra108.test.ts
@@ -184,3 +184,41 @@ test('an origin twin under a declared db_only path reads db_only_slug_path even
ambiguousSlugPath(root);
expect(await liveFileDrift(engine, 'default', SLUG)).toMatchObject({ still_present: null, reason: 'db_only_slug_path' });
}, { setup: ({ root }) => { setupRepo({ root }); writeFileSync(join(root, 'gbrain.yml'), 'storage:\n db_only:\n - notes/\n'); git(root, 'add', '-A'); git(root, 'commit', '-qm', 'db_only'); } }), 120000);
+
+/** The twin's slug-path file leaves Git's index (still on disk) and is optionally gitignored, then gains a bullet and a full sync runs. */
+async function untrackedEdit(engine: BrainEngine, root: string, ignored: boolean): Promise<void> {
+ git(root, 'rm', '-q', '--cached', PATH);
+ if (ignored) {
+ writeFileSync(join(root, '.gitignore'), `${PATH}\n`);
+ git(root, 'add', '.gitignore');
+ }
+ git(root, 'commit', '-qm', 'untrack the file');
+ appendFileSync(join(root, PATH), `${BULLET}\n`);
+ expect((await performManagedSync(engine, { ...OPTS, full: true })).status).toBe('synced');
+}
+
+test('an origin twin whose slug-path file Git does not track stays database-only, and sync holds nothing for it', () => managedBrain(async ({ engine, root }) => {
+ await legacyTwin(engine);
+ await untrackedEdit(engine, root, false);
+ expect(await holds(engine)).toEqual([]);
+ expect(await liveFileDrift(engine, 'default', SLUG)).toMatchObject(DATABASE_ONLY);
+ const planned = await preview(engine, 'file');
+ expect(planned.affected).toBe(0);
+}, { setup: setupRepo }), 120000);
+
+test('an origin twin whose slug-path file is gitignored stays database-only even when sync reads the working tree', () => managedBrain(async ({ engine, root }) => {
+ await legacyTwin(engine);
+ await engine.setConfig('sync.include_working_tree', 'true');
+ await untrackedEdit(engine, root, true);
+ expect(await holds(engine)).toEqual([]);
+ expect(await liveFileDrift(engine, 'default', SLUG)).toMatchObject(DATABASE_ONLY);
+}, { setup: setupRepo }), 120000);
+
+test('an origin twin whose untracked slug-path file sync reads from the working tree still reads drift while sync holds it', () => managedBrain(async ({ engine, root }) => {
+ await legacyTwin(engine);
+ await engine.setConfig('sync.include_working_tree', 'true');
+ await untrackedEdit(engine, root, false);
+ expect(await holds(engine)).toEqual([{ code: 'file_drift', path: PATH }]);
+ expect(await liveFileDrift(engine, 'default', SLUG)).toMatchObject({ still_present: true, reason: 'file_database_drift', path: PATH });
+}, { setup: setupRepo }), 120000);
+