Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: purge scans the default source's sync.repo_path repository for reason trailers when its local_path is NULL

garrytan17bd3bf7192e
diff --git a/src/core/facts/purge-reasons.ts b/src/core/facts/purge-reasons.ts
index 56ac2c88a1..f5c5322be3 100644
--- a/src/core/facts/purge-reasons.ts
+++ b/src/core/facts/purge-reasons.ts
@@ -5,6 +5,7 @@
* messages already in Git, which purge never rewrites: their (commit, path) pairs are tombstoned, text-free, so no later
* sync, cold re-import or re-derive stores that reason again.
*/
+import { isAbsolute } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import { opError } from '../ops/contract.ts';
import { REASON_TRAILER_KEY } from '../persistence/change-reason.ts';
@@ -31,8 +32,10 @@ const scanFailed = (sourceId: string, rawId: string, detail: string) => opError(
export async function scanPurgeTrailers(engine: BrainEngine, input: { sourceId: string; claim: string; rawId: string; limit?: number }): Promise<PurgeTrailerPlan> {
const none: PurgeTrailerPlan = { sourceRoot: null, head: null, hits: [], commits: 0, limited: false };
const [source] = await engine.executeRaw<{ local_path: string | null }>('SELECT local_path FROM sources WHERE id = $1', [input.sourceId]);
- if (!source?.local_path) return none;
- const scan = await scanTrailersCarrying(source.local_path, [input.claim], input.limit === undefined ? {} : { limit: input.limit });
+ // The default source's repository may be the legacy sync.repo_path (local_path NULL); a relative path names no repository.
+ const root = source?.local_path || (input.sourceId === 'default' ? await engine.getConfig('sync.repo_path') : null);
+ if (!root || !isAbsolute(root)) return none;
+ const scan = await scanTrailersCarrying(root, [input.claim], input.limit === undefined ? {} : { limit: input.limit });
if (scan.status === 'no_repository') return none;
if (scan.status === 'failed') throw scanFailed(input.sourceId, input.rawId, scan.detail);
if (scan.status === 'capped') {
@@ -42,7 +45,7 @@ export async function scanPurgeTrailers(engine: BrainEngine, input: { sourceId:
why: `Purges and tombstones the reasons found in the newest ${scan.cap} commits; older commits are not read.`,
user_message: `This brain's Git history is longer than ${scan.cap} commits. Purge now and check only the newest commits for change reasons quoting this claim (choose how many), or stop?` } });
}
- return { sourceRoot: source.local_path, head: scan.head, hits: scan.hits, commits: scan.commits, limited: scan.limited };
+ return { sourceRoot: root, head: scan.head, hits: scan.hits, commits: scan.commits, limited: scan.limited };
}
/**
diff --git a/test/reason-trailers-gbra217.test.ts b/test/reason-trailers-gbra217.test.ts
index 58a17cdb4d..4c625e650e 100644
--- a/test/reason-trailers-gbra217.test.ts
+++ b/test/reason-trailers-gbra217.test.ts
@@ -317,6 +317,44 @@ describe('A7: purge reaches change reasons', () => {
}
}), 180_000);
+ test('the default source whose repository is sync.repo_path (local_path NULL) gets its reason tombstoned like any other', async () => inHome(async () => {
+ for (const engine of engines) {
+ const claim = `Gate code is ${5500 + engines.indexOf(engine)}`;
+ const root = newRepo({ 'notes/legacy-a.md': page('A one.') });
+ const [before] = await engine.executeRaw<{ local_path: string | null }>("SELECT local_path FROM sources WHERE id = 'default'");
+ const configured = await engine.getConfig('sync.repo_path');
+ await engine.executeRaw("UPDATE sources SET local_path = NULL WHERE id = 'default'");
+ await engine.setConfig('sync.repo_path', root);
+ try {
+ const legacy: Source = { id: 'default', root };
+ await sync(engine, legacy);
+ write(root, { 'notes/legacy-a.md': page('A two.') });
+ const pushed = commit(root, 'edit', [`notes/legacy-a.md: Changed once ${claim} was rotated.`]);
+ await sync(engine, legacy);
+ expect((await reasonsOf(engine, 'default', 'notes/legacy-a')).reasons).toEqual([`Changed once ${claim} was rotated.`]);
+ await engine.executeRaw("UPDATE sources SET local_path = NULL WHERE id = 'default'");
+ const id = await seedFact(engine, 'default', 'people/legacy-holder-example', claim);
+
+ const done = await purge(engine, 'default', id);
+ expect(done.state).toBe('committed');
+ expect(done.purge.removed).toMatchObject({ reason_purge_tombstones: 1 });
+ expect(await engine.executeRaw("SELECT commit_sha, path FROM reason_purge_tombstones WHERE source_id = 'default'"))
+ .toEqual([{ commit_sha: pushed, path: 'notes/legacy-a.md' }]);
+ expect((await reasonsOf(engine, 'default', 'notes/legacy-a')).reasons).toEqual(['[purged]']);
+
+ await importFromContent(engine, 'notes/legacy-a', page('A one.'), { noEmbed: true, sourceId: 'default' });
+ await engine.executeRaw("UPDATE sources SET last_commit = NULL, local_path = NULL WHERE id = 'default'");
+ await sync(engine, legacy);
+ expect(await bodyOf(engine, 'default', 'notes/legacy-a')).toContain('A two.');
+ expect((await reasonsOf(engine, 'default', 'notes/legacy-a')).reasons.at(-1)).toBeNull();
+ } finally {
+ await engine.executeRaw("DELETE FROM reason_purge_tombstones WHERE source_id = 'default'");
+ await engine.executeRaw("UPDATE sources SET local_path = $1, last_commit = NULL WHERE id = 'default'", [before?.local_path ?? null]);
+ if (configured === null) await engine.unsetConfig('sync.repo_path'); else await engine.setConfig('sync.repo_path', configured);
+ }
+ }
+ }), 180_000);
+
test('a running Git effect carrying the claim blocks the purge with the pending-recovery refusal', async () => inHome(async () => {
for (const engine of engines) {
const claim = `Vault pin is ${7100 + engines.indexOf(engine)}`;