Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): a managed datastore never marks an enclosing checkout managed (#7338)

garrytan29ba5a38cb61
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 33a44f446a..905dd1cbec 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1121,7 +1121,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.366.0', id: 'calibration_profile_unchanged_skip', text: '`calibration_profile` makes no model call and writes no new profile row when the holder\'s resolved count, Brier score, accuracy, partial rate and model all equal the latest stored profile (`skipped: unchanged_scorecard`); a new profile prompt takes effect at the next scorecard change.' },
{ since: '0.60.366.0', id: 'grade_takes_window', text: '`grade_takes` now reads unresolved takes oldest-first, leaves takes too recent to grade (or with no date) out of its 50-take window, and puts takes with no verdict for the current prompt and judge ahead of graded ones, so with auto-resolve off older takes beyond the first 50 are graded on later passes. Its `too_recent` count now covers only takes whose date it could not read in SQL.' },
// #7338: Git edits to pages with no import record import when the database still holds the baseline file
- { since: 'TBD', text: 'A managed sync no longer holds a Git edit to a page an older gbrain imported (one with no import record) as `file_drift` with an unknown side when the page still holds its file as of the source\\\'s managed baseline: the database did not change, so the edit imports. Before, such edits waited for the bounded legacy sweep to reach the page (500 pages per pass), so on a large activated brain most edits never became searchable. A page whose database copy changed since the baseline is still held. Edits already held this way import after `gbrain sources retry-held <source>` and the next sync.' },
+ { since: 'TBD', text: 'A managed sync no longer holds a Git edit to a page an older gbrain imported (one with no import record) as `file_drift` with an unknown side when the page still holds its file as of the source\\\'s managed baseline: the database did not change, so the edit imports. Before, such edits waited for the bounded legacy sweep to reach the page (500 pages per pass), so on a large activated brain most edits never became searchable. A page whose database copy changed since the baseline is still held. Edits already held this way import after `gbrain sources retry-held <source>` and the next sync. A round with imports or deletes now runs the bounded legacy sweep after them, so the sweep no longer adds its cost to each commit\'s time to searchable. A managed PGLite brain whose database directory sits inside another Git checkout no longer marks that whole checkout managed (which refused every write in it with `writer_coordinator_required`); the marker goes in the database directory. A `gbrain-managed.json` an older gbrain already wrote into that checkout\'s `.git` directory stays until it is removed by hand.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/persistence/filesystem-guard.ts b/src/core/persistence/filesystem-guard.ts
index d814aae69d..c44381300c 100644
--- a/src/core/persistence/filesystem-guard.ts
+++ b/src/core/persistence/filesystem-guard.ts
@@ -31,7 +31,7 @@ export async function refreshManagedFilesystemRoots(engine: SqlEngine, databaseP
FROM persistence_host_bindings h JOIN persistence_source_bindings s ON s.worktree_id=h.worktree_id WHERE h.host_id=$1::uuid
UNION SELECT local_path,id,incarnation,NULL,NULL FROM sources WHERE local_path IS NOT NULL`, [localHostId()], { signal });
signal?.throwIfAborted();
- if (databasePath) rows.push({ local_path: databasePath });
+ if (databasePath) rows.push({ local_path: databasePath, datastore: true });
const roots = recordManagedRoots(brain.brain_id, rows, brain.mode_epoch == null ? undefined : Number(brain.mode_epoch));
managedRoots.set(brain.brain_id, new Map(roots.map(row => [row.local_path, row.source_id])));
}
diff --git a/src/core/persistence/root-registry.ts b/src/core/persistence/root-registry.ts
index a5c4c53904..00339a67fb 100644
--- a/src/core/persistence/root-registry.ts
+++ b/src/core/persistence/root-registry.ts
@@ -11,6 +11,8 @@ export interface ManagedRootRecord {
source_incarnation?: string;
worktree_id?: string;
topology_generation?: string | number;
+ /** #7338: the brain's own database path, marked in its own directory: it is not a checkout, so no enclosing repository is marked. */
+ datastore?: true;
}
function registryDirectory(): string { return join(configDir(), 'persistence', 'managed-roots'); }
function gitMetadataDirectory(root: string): string | null {
@@ -143,7 +145,8 @@ export function recordManagedRoots(brainId: string, records: ManagedRootRecord[]
...(modeEpoch !== undefined ? { mode_epoch: modeEpoch } : {}) });
changed = writePrivateRecord(file, value) || changed;
if (existsSync(root) && statSync(root).isDirectory()) {
- const metadata = enclosingGitMetadata(root);
+ // #7338: a datastore is marked in its own directory, never in the metadata of a repository that happens to enclose it.
+ const metadata = record.datastore ? null : enclosingGitMetadata(root);
const marker = metadata ? join(metadata, 'gbrain-managed.json') : join(root, '.gbrain-managed');
if (!existsSync(marker)) writePrivateRecord(marker, JSON.stringify({ version: 1, managed: true, brain_id: brainId, ...(modeEpoch !== undefined ? { mode_epoch: modeEpoch } : {}) }));
}
diff --git a/test/managed-datastore-marker-7338.test.ts b/test/managed-datastore-marker-7338.test.ts
new file mode 100644
index 0000000000..9438920b45
--- /dev/null
+++ b/test/managed-datastore-marker-7338.test.ts
@@ -0,0 +1,28 @@
+/**
+ * #7338: a managed PGLite brain whose database directory sits inside an unrelated Git checkout registered that directory
+ * as a managed root and wrote its Git marker into the enclosing repository's metadata. That marked the whole checkout
+ * managed, so every later write anywhere in it (another brain's sync, an ordinary file write) was refused
+ * `writer_coordinator_required`. The nightly freshness gate's classic cell, built inside the gbrain checkout after the
+ * managed PGLite cell, never synced. The datastore stays registered (a write inside it is still refused), but its
+ * enclosing repository is not marked.
+ */
+import { expect, test } from 'bun:test';
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { isManagedFilesystemPath, refreshManagedFilesystemRoots } from '../src/core/persistence/filesystem-guard.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+
+test('#7338: a managed datastore inside an unrelated checkout does not mark that checkout managed', () => managedBrain(async ({ engine }) => {
+ const repo = mkdtempSync(join(tmpdir(), 'gbrain-unrelated-checkout-'));
+ try {
+ execFileSync('git', ['-C', repo, 'init', '-q']);
+ const datastore = join(repo, '.context', 'brain.pglite');
+ mkdirSync(datastore, { recursive: true });
+ await refreshManagedFilesystemRoots(engine, datastore);
+ expect(isManagedFilesystemPath(join(datastore, 'base'))).toBe(true);
+ expect(existsSync(join(repo, '.git', 'gbrain-managed.json'))).toBe(false);
+ expect(isManagedFilesystemPath(join(repo, 'other-brain', 'notes', 'a.md'))).toBe(false);
+ } finally { rmSync(repo, { recursive: true, force: true }); }
+}));