Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync-holds): a stale mirror_read_only page's file_drift hold has an exit (#7931)

garrytan669aee3961f6
diff --git a/docs/guides/repair.md b/docs/guides/repair.md
index 147b21056b..3b1a626ba4 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -2462,7 +2462,9 @@ no recorded file origin (no source path, and a source URI that is not a
`file:` URI) is compared with, and adopts, the file at its slug path, spelled
as sync imports it (`README.md` for slug `readme`); the apply records that
path. A sync re-screens a held page that changed since it was held even when
-Git did not move.
+Git did not move. A page a read-only mirror stored database-only (`mirror_read_only`)
+is compared with the file at its slug path once its source is writable again;
+the apply records that path and clears the stamp.
A held preview covers one window of the selected holds, in held-path order:
at most 200 pages, and it starts no page after 60 seconds of work (each page
diff --git a/src/core/engine-sql/pages.ts b/src/core/engine-sql/pages.ts
index 2587abc644..dad26a6148 100644
--- a/src/core/engine-sql/pages.ts
+++ b/src/core/engine-sql/pages.ts
@@ -180,7 +180,7 @@ export async function putPage(
import_filename = COALESCE(EXCLUDED.import_filename, pages.import_filename),
chunker_version = ${trustedSql(bodyWriteChunkVersion('EXCLUDED.compiled_truth', 'EXCLUDED.timeline'))},
source_path = COALESCE(EXCLUDED.source_path, pages.source_path),
- database_only_reason = CASE WHEN EXCLUDED.source_path IS NOT NULL AND pages.database_only_reason IN ('detached', 'origin_twin') THEN NULL ELSE pages.database_only_reason END,
+ database_only_reason = CASE WHEN EXCLUDED.source_path IS NOT NULL AND pages.database_only_reason IN ('detached', 'origin_twin', 'mirror_read_only') THEN NULL ELSE pages.database_only_reason END,
source_kind = COALESCE(EXCLUDED.source_kind, pages.source_kind),
source_uri = COALESCE(EXCLUDED.source_uri, pages.source_uri),
ingested_via = COALESCE(EXCLUDED.ingested_via, pages.ingested_via),
diff --git a/src/core/persistence/drift-conflicts.ts b/src/core/persistence/drift-conflicts.ts
index 256422ea5c..69809f35e3 100644
--- a/src/core/persistence/drift-conflicts.ts
+++ b/src/core/persistence/drift-conflicts.ts
@@ -19,7 +19,7 @@
* updates and releases it once the file and database agree again;
* - doctor `maintenance_write_drift` and `gbrain repair file-drift`.
*/
-import { readFileSync } from 'node:fs';
+import { existsSync, readFileSync } from 'node:fs';
import { join, relative, sep } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import { OperationError, opError } from '../ops/contract.ts';
@@ -35,6 +35,7 @@ import type { PageSnapshot } from '../page-state/types.ts';
import { sha256 } from './digest.ts';
import { existingLocalHostId } from './identity.ts';
import { getWorktreeBinding } from './ownership.ts';
+import { staleMirrorPage } from './mirror-read-only.ts';
import { prepareFileTarget } from './page-prepare.ts';
import { readReconcileState } from './reconcile-state.ts';
import { reconcileCanonical } from './reconcile-merge.ts';
@@ -82,6 +83,12 @@ export async function liveFileDrift(engine: BrainEngine, sourceId: string, slug:
const page = snapshot === undefined ? await engine.readPageSnapshot(slug, { sourceId }) : snapshot;
if (!page || page.page.deleted_at) return { still_present: null, reason: 'page_missing' };
const root = join(binding.local_path, binding.relative_path);
+ // GBRA-133 (#7931): a stale mirror page publishes database-only but reconciles against the file at its slug path.
+ if (await staleMirrorPage(engine, sourceId, page.page)) {
+ const path = slugPathFile(root, slug);
+ if (!existsSync(path)) return { still_present: false, reason: 'database_only', db_revision: page.revision };
+ return { still_present: true, reason: 'file_database_drift', db_revision: page.revision, path: relative(root, path).split(sep).join('/'), file_hash: sha256(readFileSync(path)) };
+ }
try {
const file = await prepareFileTarget(engine, { source_id: sourceId, worktree_id: binding.worktree_id, slug }, page, null, hostId!);
if (!file) return { still_present: false, reason: 'database_only', db_revision: page.revision };
diff --git a/src/core/persistence/mirror-read-only.ts b/src/core/persistence/mirror-read-only.ts
index be0e12ce3d..ac930fbe09 100644
--- a/src/core/persistence/mirror-read-only.ts
+++ b/src/core/persistence/mirror-read-only.ts
@@ -7,7 +7,12 @@
* "database_only"` on the receipt), so `git pull --ff-only` keeps working.
* A page created that way (no recorded file) carries
* `pages.database_only_reason='mirror_read_only'` and stays database-only
- * after `mirror-writable`, as an unbound-source page does (#5254).
+ * after `mirror-writable`, as an unbound-source page does (#5254). GBRA-133
+ * (#7931): once its source is writable again (`mirror-writable`, managed
+ * persistence activated, an owned clone no longer owned) the stamp is stale:
+ * reconcile and `repair file-drift` compare the page with the file at its
+ * slug path, as they do an unbound-source page, and the apply that records
+ * that origin clears the stamp.
*
* #6946: on an unmanaged brain a clone gbrain owns (`sources add --url`,
* url-clone.ts) is read-only the same way without the flag, because gbrain
@@ -35,3 +40,8 @@ export async function isMirrorOnlyPage(engine: SqlEngine, sourceId: string, slug
'SELECT database_only_reason FROM pages WHERE source_id=$1 AND slug=$2', [sourceId, slug]);
return row?.database_only_reason === 'mirror_read_only';
}
+
+/** GBRA-133 (#7931): a `mirror_read_only` page with no recorded file whose source is no longer a read-only mirror. */
+export async function staleMirrorPage(engine: SqlEngine, sourceId: string, page: { slug: string; source_path?: string | null }): Promise<boolean> {
+ return !page.source_path && await isMirrorOnlyPage(engine, sourceId, page.slug) && !await sourceMirrorReadOnly(engine, sourceId);
+}
diff --git a/src/core/persistence/reconcile-prepare.ts b/src/core/persistence/reconcile-prepare.ts
index 7813bc6fa9..2f23b2d9b0 100644
--- a/src/core/persistence/reconcile-prepare.ts
+++ b/src/core/persistence/reconcile-prepare.ts
@@ -183,7 +183,7 @@ export async function prepareReconcileMutation(engine: BrainEngine, row: WriteRe
}
// The page has a canonical origin (adopted above, or already recorded, #6536): drop the #5254 database-only classification.
await tx.executeRaw(`UPDATE pages SET database_only_reason=NULL WHERE source_id=$1 AND slug=$2 AND deleted_at IS NULL
- AND database_only_reason IN ('unbound_source', 'origin_twin') AND source_path IS NOT NULL`, [row.source_id, row.slug]);
+ AND database_only_reason IN ('unbound_source', 'origin_twin', 'mirror_read_only') AND source_path IS NOT NULL`, [row.source_id, row.slug]);
if (!ready.noop) { await prepared.project!(tx); await sealPageTextProjection(tx, row.slug, row.source_id); }
const final = await tx.readPageSnapshot(row.slug, { sourceId: row.source_id });
const scanStateTransferred = final ? await transferLegacyAtomPageState(tx, state.snapshot, final) : false;
diff --git a/src/core/persistence/reconcile-state.ts b/src/core/persistence/reconcile-state.ts
index 469fc669f3..275cdfb5fa 100644
--- a/src/core/persistence/reconcile-state.ts
+++ b/src/core/persistence/reconcile-state.ts
@@ -26,7 +26,7 @@ import { reconcileCanonical, strictReconcileKeys, validateReconcileJson, type Re
import type { ParsedPage } from '../import-file.ts';
import type { AutoDecision } from './reconcile-additive.ts';
import { publishesDatabaseOnly, readCanonicalAsImported } from './page-prepare.ts';
-import { isMirrorOnlyPage } from './mirror-read-only.ts';
+import { isMirrorOnlyPage, staleMirrorPage } from './mirror-read-only.ts';
import { hardenedGitSync } from '../hardened-git.ts';
export interface ReconcilePins {
@@ -236,8 +236,8 @@ export async function readReconcileState(engine: BrainEngine, sourceId: string,
const recorded = recordedPathFromFileUri(snapshot.page.source_uri, root);
const mode = await scannerSlugRootMode(engine, sourceId, root);
const recordedPath = recordedReconcilePath(root, snapshot.page, mode);
- const marked = !recordedPath && isDatabaseOnlyPage({ ...snapshot.page,
- database_only_reason: await isUnboundSourcePage(engine, sourceId, slug) ? 'unbound_source' : null });
+ const marked = !recordedPath && (isDatabaseOnlyPage({ ...snapshot.page,
+ database_only_reason: await isUnboundSourcePage(engine, sourceId, slug) ? 'unbound_source' : null }) || await staleMirrorPage(engine, sourceId, snapshot.page));
const adoptable = !recordedPath && !marked && opts.adoptSlugPath === true && recordsNoFileOrigin(snapshot.page)
&& !publishesDatabaseOnly(root, slug, snapshot) && !await isMirrorOnlyPage(engine, sourceId, slug);
const derived = marked || adoptable ? slugDerivedOrigin(root, slug, mode) : null;
diff --git a/test/sync-holds-wave-gbra133.test.ts b/test/sync-holds-wave-gbra133.test.ts
index df19f15cd3..4218e963ee 100644
--- a/test/sync-holds-wave-gbra133.test.ts
+++ b/test/sync-holds-wave-gbra133.test.ts
@@ -15,6 +15,10 @@
* protected frontmatter key) rewrote the whole file through the serializer: keys reordered, lists re-emitted, the body
* re-rendered. It now keeps the file's bytes and appends only the protected parts; a result that would need any other
* change (here a database-only tag) refuses `file_wins_rewrite` in the preview and writes nothing.
+ * 5. A page a read-only mirror stamped `mirror_read_only` (no recorded file) kept that stamp after its source became
+ * writable, and its `file_drift` hold had no exit: both previews called it database-only. Once the source is writable it
+ * reconciles against the file at its slug path (as an unbound-source page does); the apply records that path and clears
+ * the stamp. While the source is still read-only the page stays database-only.
*
* Holds exist only where sync imports a Git checkout, so there is no repo-less arm.
*/
@@ -211,4 +215,25 @@ for (const backend of testBackends()) {
expect(await holds(engine)).toEqual([]);
expect(readFileSync(join(root, 'notes/alpha.md'), 'utf8')).toBe(after);
}, { databaseUrl, setup }), 180_000);
+
+ test(`GBRA-133 [${backend}]: a stale mirror_read_only page's hold resolves with --prefer database, which records its path and clears the stamp`, () => managedBrain(async ({ engine, root }) => {
+ expect((await performManagedSync(engine, OPTS)).status).toBe('first_sync');
+ await coordinated(engine, `INSERT INTO pages(source_id, slug, type, title, compiled_truth, timeline, frontmatter, database_only_reason)
+ VALUES('default', 'notes/mirror-page', 'concept', 'notes/mirror-page', 'The database copy, written while the source was a mirror.', '', '{}'::jsonb, 'mirror_read_only')`);
+ writeFileSync(join(root, 'notes/mirror-page.md'), note('notes/mirror-page', 'The upstream file.')); commitAll(root, 'upstream');
+ await performManagedSync(engine, OPTS);
+ expect(await holds(engine)).toEqual(['notes/mirror-page.md file_drift unknown']);
+ await engine.executeRaw(`UPDATE sources SET config = config || '{"mirror_read_only": true}'::jsonb WHERE id='default'`);
+ expect(listing(await heldPreview(engine, 'database'))).toEqual(['default:notes/mirror-page no_drift']);
+ await engine.executeRaw(`UPDATE sources SET config = config - 'mirror_read_only' WHERE id='default'`);
+ const preview = await heldPreview(engine, 'database');
+ expect(listing(preview)).toEqual(['default:notes/mirror-page prefer_database']);
+ expect((await heldPreview(engine, 'database', preview.preview_hash!)).outcomes).toEqual({ repaired: 1 });
+ expect(readFileSync(join(root, 'notes/mirror-page.md'), 'utf8')).toContain('The database copy, written while the source was a mirror.');
+ expect(await engine.executeRaw("SELECT source_path, database_only_reason FROM pages WHERE source_id='default' AND slug='notes/mirror-page'"))
+ .toEqual([{ source_path: 'notes/mirror-page.md', database_only_reason: null }]);
+ commitAll(root, 'repair');
+ await performManagedSync(engine, OPTS);
+ expect(await holds(engine)).toEqual([]);
+ }, { databaseUrl, setup }), 180_000);
}