Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(#7630): leak guard fails closed on a live relative sibling; doctor names it as relative; --operation through the CLI path

garrytance1cd23e5c96
diff --git a/src/commands/doctor/checks/default-source-path.ts b/src/commands/doctor/checks/default-source-path.ts
index a4ccf3691f..a51e8e77e8 100644
--- a/src/commands/doctor/checks/default-source-path.ts
+++ b/src/commands/doctor/checks/default-source-path.ts
@@ -11,8 +11,8 @@
* for every other source whose local_path is relative.
*
* Lives in the doctor module dir per the peeled-façade rule — new code goes
- * here, not back into doctor.ts. Returns null for the 'skip' verdict so the
- * caller pushes nothing.
+ * here, not back into doctor.ts. Returns null for the 'skip' verdict when no
+ * source has a relative local_path, so the caller pushes nothing.
*/
import { existsSync, statSync } from 'fs';
import { isAbsolute } from 'path';
@@ -31,7 +31,12 @@ export async function defaultSourceLocalPathCheck(engine: BrainEngine): Promise<
let repoPath: string | null = null;
let repoPathIsDir = false;
let collidingSourceId: string | null = null;
+ let blockedByRelative = false;
let sharedPages = 0;
+ // #7630: any other source with a relative local_path is refused by sync the same way, so name it here too.
+ const relative = (await engine.executeRaw<{ id: string; local_path: string }>(
+ `SELECT id, local_path FROM sources WHERE id <> 'default' AND local_path IS NOT NULL AND local_path <> '' AND archived IS NOT TRUE ORDER BY id`,
+ )).filter(row => !isAbsolute(row.local_path));
// Only gather the fallback-topology inputs when the pointer is actually
// null — the set case resolves from the row alone.
if (defaultSource && !defaultSource.local_path) {
@@ -53,6 +58,10 @@ export async function defaultSourceLocalPathCheck(engine: BrainEngine): Promise<
// Mirror resolvePageWriteTarget's #2018 leak guard: sync.repo_path
// being another source's own working tree silently skips the write.
collidingSourceId = await otherSourceOwning(engine, 'default', repoPath);
+ if (collidingSourceId && relative.some(row => row.id === collidingSourceId)) {
+ blockedByRelative = true;
+ collidingSourceId = null;
+ }
if (collidingSourceId) {
const [shared] = await engine.executeRaw<{ n: number }>(
`SELECT COUNT(*)::int AS n FROM pages a
@@ -74,15 +83,12 @@ export async function defaultSourceLocalPathCheck(engine: BrainEngine): Promise<
collidingSourceId,
sharedPages,
});
- // #7630: any other source with a relative local_path is refused by sync the same way, so name it here too.
- const relative = (await engine.executeRaw<{ id: string; local_path: string }>(
- `SELECT id, local_path FROM sources WHERE id <> 'default' AND local_path IS NOT NULL AND archived IS NOT TRUE ORDER BY id`,
- )).filter(row => !isAbsolute(row.local_path));
if (assessment.status === 'skip' && relative.length === 0) return null;
const relativeNote = relative.length === 0 ? '' :
`${relative.map(row => `source "${row.id}" has the relative local_path "${row.local_path}"`).join('; ')}, which names whatever directory ` +
'gbrain runs from, so sync refuses it (sync_anchor_relative). Ask the user which directory holds its files, then run ' +
- '`gbrain sources set-path <id> <absolute-directory>`, or clear the pointer with `gbrain sources set-path <id> --clear`.';
+ '`gbrain sources set-path <id> <absolute-directory>`, or clear the pointer with `gbrain sources set-path <id> --clear`.' +
+ (blockedByRelative ? ' Until then, default-source pages are saved to the database only, not written under sync.repo_path.' : '');
return {
name: 'default_source_local_path',
status: relative.length > 0 || assessment.status === 'skip' ? 'warn' : assessment.status,
diff --git a/src/core/write-through.ts b/src/core/write-through.ts
index 3f4fff3ad3..6cd10fbf09 100644
--- a/src/core/write-through.ts
+++ b/src/core/write-through.ts
@@ -287,15 +287,15 @@ export async function scannerSlugRootMode(engine: BrainEngine, sourceId: string,
* resolved counts as a match: the leak guard fails closed.
*/
export async function otherSourceOwning(engine: BrainEngine, sourceId: string, path: string): Promise<string | null> {
- const rows = await engine.executeRaw<{ id: string; local_path: string }>(
- `SELECT id, local_path FROM sources WHERE id <> $1 AND local_path IS NOT NULL AND local_path <> '' ORDER BY id`,
+ const rows = await engine.executeRaw<{ id: string; local_path: string; archived: boolean }>(
+ `SELECT id, local_path, archived IS TRUE AS archived FROM sources WHERE id <> $1 AND local_path IS NOT NULL AND local_path <> '' ORDER BY id`,
[sourceId],
);
const identity = (spelling: string) => { try { return canonicalFilesystemPath(msysToNativePath(spelling)); } catch { return null; } };
const target = identity(path);
return rows.find(row => {
- // #7630: a relative local_path names whatever directory gbrain runs from, never a tree, so it owns nothing.
- if (!isAbsolute(msysToNativePath(row.local_path))) return false;
+ // #7630: a live source's relative local_path names no known tree, so like any unresolvable spelling it counts as a match.
+ if (!isAbsolute(msysToNativePath(row.local_path))) return !row.archived;
const other = identity(row.local_path);
if (target === null || other === null) return true;
const rel = relative(other, target);
diff --git a/test/sync-relative-anchor-refusal-7630.test.ts b/test/sync-relative-anchor-refusal-7630.test.ts
index 4146be0e9b..7aec1ab898 100644
--- a/test/sync-relative-anchor-refusal-7630.test.ts
+++ b/test/sync-relative-anchor-refusal-7630.test.ts
@@ -497,19 +497,44 @@ for (const backend of testBackends()) {
expect(seen.find(s => s.sourceId === 'notes-a')).toEqual({ sourceId: 'notes-a', brainDir: null });
}, 120_000);
- // Value: protects=the repair command (sources set-path) and the #2018 write-through leak guard never treat a sibling's relative
- // local_path as the cwd's tree; fails_when=assertNoOverlappingPath or otherSourceOwning realpaths "." to the cwd, so set-path
- // refuses overlapping_path and default writes under sync.repo_path skip as another source's tree; why_new=no other case has a
- // relative sibling; seam=none; no_claim=company-brain admission (same helper)
- test('a sibling with a relative local_path overlaps nothing: set-path to the cwd passes and the leak guard ignores it', async () => {
+ // Value: protects=the repair command (sources set-path) never treats a sibling's relative local_path as the cwd's tree, and the
+ // #2018 write-through leak guard fails closed on it wherever gbrain runs; fails_when=assertNoOverlappingPath realpaths "." to the
+ // cwd, so set-path refuses overlapping_path, or otherSourceOwning ignores the relative sibling (or matches it only when run from
+ // its tree); why_new=no other case has a relative sibling; seam=none; no_claim=company-brain admission (same helper)
+ test('a sibling with a relative local_path: set-path to the cwd passes and the leak guard fails closed on it', async () => {
await engine.executeRaw("UPDATE sources SET local_path='.' WHERE id='default'");
await engine.executeRaw("INSERT INTO sources (id, name, local_path) VALUES ('notes-a', 'notes-a', '.')");
const { assertNoOverlappingPath } = await import('../src/core/sources-ops.ts');
expect(await assertNoOverlappingPath(engine, 'notes-a', folder).then(() => null, (error: { code?: string }) => error.code ?? String(error))).toBeNull();
const { otherSourceOwning } = await import('../src/core/write-through.ts');
+ expect(await otherSourceOwning(engine, 'default', join(folder, 'notes.md'))).toBe('notes-a');
+ expect(await otherSourceOwning(engine, 'default', join(tmpdir(), 'elsewhere', 'notes.md'))).toBe('notes-a');
+ await engine.executeRaw("UPDATE sources SET archived=true, archived_at=now() WHERE id='notes-a'");
expect(await otherSourceOwning(engine, 'default', join(folder, 'notes.md'))).toBeNull();
}, 120_000);
+ // /ship review fixes (GBRA-207, #7630).
+ // Value: protects=doctor reports a relative sibling that blocks default write-through as a relative path, never as a #2018 containment
+ // with an invented alias diagnosis, and ignores an empty local_path; fails_when=the wrapper passes the relative leak-guard match to
+ // the assessor ("own working tree") or the relative query keeps ''; why_new=no doctor case has a null default local_path beside a
+ // relative sibling; seam=none; no_claim=other doctor checks (only this check runs)
+ test('doctor names a relative sibling that blocks default writes as relative, not as a containment, and skips an empty local_path', async () => {
+ const repo = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-7630-repo-')));
+ try {
+ await engine.setConfig('sync.repo_path', repo);
+ await engine.putPage('notes/live', { type: 'note', title: 'Live', compiled_truth: 'A live default page.', timeline: '' }, { sourceId: 'default' });
+ await engine.executeRaw("INSERT INTO sources (id, name, local_path) VALUES ('notes-a', 'notes-a', '.'), ('notes-b', 'notes-b', '')");
+ const check = await defaultSourceLocalPathCheck(engine);
+ expect(check?.status).toBe('warn');
+ expect(check?.message).toContain('source "notes-a" has the relative local_path "."');
+ expect(check?.message).toContain('default-source pages are saved to the database only');
+ expect(check?.message).not.toContain('own working tree');
+ expect(check?.message).not.toContain('notes-b');
+ } finally {
+ rmSync(repo, { recursive: true, force: true });
+ }
+ }, 120_000);
+
test('gbrain import of the cwd into a source whose local_path is "." does not stamp it fresh', async () => {
await engine.executeRaw("INSERT INTO sources (id, name, local_path) VALUES ('notes-a', 'notes-a', '.')");
const { runImport } = await import('../src/commands/import.ts');
diff --git a/test/terminal-failures-6650.test.ts b/test/terminal-failures-6650.test.ts
index a9348536d4..80943dcb7a 100644
--- a/test/terminal-failures-6650.test.ts
+++ b/test/terminal-failures-6650.test.ts
@@ -27,6 +27,9 @@ import { categorizeCheck } from '../src/core/doctor-categories.ts';
import { WAVE_CHECKS } from '../src/commands/doctor/wave-checks.ts';
import { runRemoteDoctorTier } from '../src/commands/doctor/remote-tier.ts';
import { renderWriterFailures } from '../src/commands/sources-writer-failures.ts';
+import { parsePersistenceAdminArgs } from '../src/commands/persistence-admin.ts';
+import { runPersistenceAdministration } from '../src/core/persistence/administration.ts';
+import type { TerminalFailureListing } from '../src/core/persistence/terminal-failures.ts';
import type { OperationContext } from '../src/core/ops/contract.ts';
let engine: BrainEngine;
@@ -415,7 +418,9 @@ for (const kind of testBackends()) describe(`the aggregate, doctor and the drill
}
for (const g of stalled) {
expect(g.drill_down).toContain('--operation');
- const rows = await readTerminalFailures(engine, { sourceId: SOURCE, ...g.filter, origin: g.filter.origin ?? 'none' });
+ const parsed = parsePersistenceAdminArgs('writer', g.drill_down.slice(3));
+ expect(parsed.params.operation).toBe(g.operation);
+ const rows = await runPersistenceAdministration(engine, parsed.operation, parsed.params) as TerminalFailureListing;
if (rows.status !== 'ok') throw new Error('unknown');
expect(rows.rows!.map(r => r.operation)).toEqual([g.operation ?? '']);
}
diff --git a/test/writer-failures-cli-6650.test.ts b/test/writer-failures-cli-6650.test.ts
index b208b61b1e..ee9e98a4c5 100644
--- a/test/writer-failures-cli-6650.test.ts
+++ b/test/writer-failures-cli-6650.test.ts
@@ -50,6 +50,7 @@ describe('sources writer failures', () => {
operation: 'writer_failures', params: { source_id: SOURCE, group_by: 'category', limit: 5 }, json: true });
expect(parsePersistenceAdminArgs('writer', ['failures', SOURCE, '--state', 'all', '--since', '3d', '--origin', 'none', '--before', '12'])).toMatchObject({
operation: 'writer_failures', params: { source_id: SOURCE, state: 'all', since: '3d', origin: 'none', before: '12' }, json: false });
+ expect(parsePersistenceAdminArgs('writer', ['failures', SOURCE, '--code', 'preparation_stalled', '--operation', 'put_page']).params).toMatchObject({ code: 'preparation_stalled', operation: 'put_page' });
});
test('refuses bad options, a missing source and an archived one with the usage to fix it', async () => {
@@ -61,6 +62,7 @@ describe('sources writer failures', () => {
await expect(run({ source_id: SOURCE, group_by: 'slug' })).rejects.toMatchObject({ code: 'invalid_params', message: 'Invalid --group-by value.' });
await expect(run({ source_id: SOURCE, limit: 0 })).rejects.toMatchObject({ code: 'invalid_params', message: 'Invalid --limit value.' });
await expect(run({ source_id: SOURCE, before: 'abc' })).rejects.toMatchObject({ code: 'invalid_params', message: 'Invalid --before value.' });
+ await expect(run({ source_id: SOURCE, operation: 'Bad-Op' })).rejects.toMatchObject({ code: 'invalid_params', message: 'Invalid --operation value.' });
await expect(run({ source_id: SOURCE, slug: 'x' })).rejects.toMatchObject({ code: 'invalid_params' });
});