Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(write-through): another OS's absolute local_path is not relative to the leak guard; resolve-path refusal asks the user
garrytan64b0eb15927c
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex a5efcd8c06..796b13a1e6 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1303,7 +1303,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.460.0', text: '`gbrain repair duplicate-facts` keeps the copy that is a row of a page\'s `## Facts` table over an older database-only copy of the same fact, so the legacy copy is superseded onto the fence row instead of holding it (which left the legacy row unfenced and its page out of fact reconciliation). With no fenced copy it still keeps the oldest row. It stays preview-first and explicit.' },{ since: '0.60.460.0', text: 'Dream consolidation on a managed brain now closes a fenced fact in its page: the closing `valid_until` is written into the fact\'s row in the entity page\'s `## Facts` fence, published with the take, and the database row takes its value from that cell, so a later sync or re-import keeps the fact closed. Before, only the database row was closed and the next re-import of the page reopened it. A fact with no fence row is still closed in the database. A fenced fact whose fence row is on another page is left open. Facts an earlier release closed in the database only are not changed (GBRA-190).' },// GBRA-207 (#7630). `since` is stamped at the merge slot.- { since: 'TBD', id: 'sync_refuses_relative_anchor', text: 'A sync whose source directory (`sources.local_path`) or legacy `sync.repo_path` is a relative path such as `.` now refuses `sync_anchor_relative` (exit 3) before any Git probe or import, instead of treating whatever folder it ran from as the brain repo (Git init, a baseline commit of the whole folder, an import of its markdown). `gbrain sources set-path <id> <absolute-directory>` sets the path, `gbrain sources set-path <id> --clear` now also clears a relative path on any source, and doctor\'s `default_source_local_path` warns on any source with a relative path, and `gbrain config set sync.repo_path` refuses one. Until the path is set, page and fact writes skip the file (database only), and so do page writes of any other source without its own path that write under `sync.repo_path` (that relative path could name this tree), matching a folder to its source ignores it, the dream, autopilot and maintenance cycles run only database phases (a cycle whose directory matches no source refuses its sync phase while `default`\'s path is relative), fence repair edits stored pages instead of files, managed activation refuses it, `autopilot --install` needs `--repo`, a remote `submit_job` is refused and a managed brain records no managed root there. A few other local commands may still read it from the folder they run in, so set the path before relying on them.' },+ { since: 'TBD', id: 'sync_refuses_relative_anchor', text: 'A sync whose source directory (`sources.local_path`) or legacy `sync.repo_path` is a relative path such as `.` now refuses `sync_anchor_relative` (exit 3) before any Git probe or import, instead of treating whatever folder it ran from as the brain repo (Git init, a baseline commit of the whole folder, an import of its markdown). `gbrain sources set-path <id> <absolute-directory>` sets the path, `gbrain sources set-path <id> --clear` now also clears a relative path on any source, and doctor\'s `default_source_local_path` warns on any source with a relative path, and `gbrain config set sync.repo_path` refuses one. Until the path is set, page and fact writes skip the file (database only), and so do page writes of any other source without its own path that write under `sync.repo_path` (that relative path could name this tree), matching a folder to its source ignores it, the dream, autopilot and maintenance cycles run only database phases (a cycle whose directory matches no source refuses its sync phase while `default`\'s path is relative), fence repair edits stored pages instead of files, `gbrain sources resolve-path` refuses it (exit 3), managed activation refuses it, `autopilot --install` needs `--repo`, a remote `submit_job` is refused and a managed brain records no managed root there. A few other local commands may still read it from the folder they run in, so set the path before relying on them.' },{ since: 'TBD', text: 'A foreground write (`remember`, `put_page`, `add_tag`, `takes_add` and the other caller-submitted page mutations) that ends with a retryable fault such as `preparation_stalled` now carries a diagnosis that inspects the writer read-only (`gbrain sources writer status --source <id> --json`) and says to resubmit the same write with a new request_id, instead of telling the user to run `gbrain sync`. A managed write (an import, repair, quarantine clear or grandfather) says to rerun its command, an operation such as `extract_facts` or writer administration gets the read-only inspect alone, and a canonical reconcile, a maintenance job (stored as `submit_job`) and a recorded sync fault keep their sync retry. `gbrain sources writer failures --group-by category` splits these by operation, and `--operation` narrows a listing to one; a row for a reconcile or managed `put_page` now reads `canonical_reconcile` or its `managed_*` intent in `operation`.' },// GBRA-95 RLS backfill. `since` is stamped at the merge slot.{ since: '0.60.463.0', id: 'rls_backfill_unguarded_tables', text: 'On Postgres, a schema migration now enables row-level security on every gbrain table still without it. A table could be left without RLS because its RLS came only from a full schema replay, which `gbrain apply-migrations --yes` does not run (the four incremental-derivation tables, `page_segment_derivations`, `page_derivation_spend` and the two purge tables), or only from the auto-RLS event trigger, which needs a superuser and is absent on many managed brains. Supabase gives public tables anon and authenticated grants, so with the Data API on such a table was reachable with the anon key. Only a table still without RLS changes; one with a `GBRAIN:RLS_EXEMPT` comment is left alone; no row is read or written. Under a role that does not bypass RLS or does not own a table, the migration names the table on stderr and leaves it, and `gbrain doctor` (rls) keeps naming it with the fix. `gbrain repair missing-tables` now recreates a cache table with RLS on. A role that neither owns a table nor bypasses RLS reads no rows from it and cannot insert or update it once RLS is on (a read-only reporting role or a scoped write role, for example): give that role policies before upgrading (docs/ENGINES.md has the source-scope recipe), or, to keep a table open to it on purpose, add the `GBRAIN:RLS_EXEMPT reason=...` comment to that table. A write role set up with an earlier copy of that recipe also needs its new policies on `page_derivation_spend` and the two purge tables: without them its segmented derivation writes are refused, and the derivation purge triggers its writes fire read no purge, so they do not hold back a purged claim.' },diff --git a/src/core/persistence/resolve-paths.ts b/src/core/persistence/resolve-paths.tsindex 0be90c7980..79d174dfd5 100644--- a/src/core/persistence/resolve-paths.ts+++ b/src/core/persistence/resolve-paths.ts@@ -116,7 +116,11 @@ export async function resolveSourcePaths(engine: BrainEngine, sourceId: string,if (!source || source.archived) throw opError('not_found', `Source ${sourceId} is not registered or is archived.`, 'List the registered sources and pass one of their ids.',{ fix: readFix('Lists the registered sources.', ['gbrain', 'sources', 'list', '--json'], ['gbrain', 'sources', 'list', '--json']) });if (source.local_path && !isAbsolute(source.local_path)) throw opError('sync_anchor_relative', `Source ${sourceId} has a relative local_path, which names no fixed directory, so its paths cannot be resolved.`,- 'Ask the user which directory holds the checkout, then run gbrain sources set-path on the host that holds it.', { fix: readFix('Shows the source and its stored path.', status, status) });+ 'Ask the user which directory holds the checkout, then run gbrain sources set-path on the host that holds it.',+ { fix: { argv: ['gbrain', 'sources', 'set-path', sourceId, '<dir>'], consent: [], actor: 'user', ask_user: true, requires_exclusive: false,+ inputs: [{ name: 'dir', how: "The absolute path of the directory that holds this source's checkout, from the user; never the current directory by default." }],+ user_message: `Source "${sourceId}" has a relative path as its directory, so gbrain cannot tell which folder holds its files. Which directory holds them? I'll point the source there (gbrain sources set-path ${sourceId} <absolute-directory>).`,+ why: 'Points the source at an absolute directory; it only updates the pointer, no file or page changes.', verify: { argv: status }, docs: DOCS } });if (!source.local_path || !existsSync(source.local_path)) throw opError('not_found', `Source ${sourceId} has no Git checkout on this host, so its paths cannot be resolved here.`,'Run this on the host that holds the checkout (gbrain sources status names its path).', { fix: readFix('Shows where the source checkout lives.', status, status) });const root = realpathSync(source.local_path);diff --git a/src/core/write-through.ts b/src/core/write-through.tsindex 58360aea71..ef842e50eb 100644--- a/src/core/write-through.ts+++ b/src/core/write-through.ts@@ -23,7 +23,7 @@ import { hasSourceFilesystemLock, withSourceFilesystemLock, assertSourceFilesyst*/import { existsSync, statSync, mkdirSync, unlinkSync, readdirSync, readFileSync } from 'fs';-import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'path';+import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep, win32 } from 'path';import { atomicWriteFileSync } from './atomic-write.ts';import type { BrainEngine } from './engine.ts';import { isRelativeFileUri, serializePageToMarkdown, resolvePageFilePath, resolveSourceLocalFilePath } from './markdown.ts';@@ -296,7 +296,9 @@ export async function otherSourceOwning(engine: BrainEngine, sourceId: string, pconst target = identity(path);return rows.find(row => {// #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 relativeOwns && !row.archived;+ // An absolute path written by another OS (C:\ on POSIX) is not relative, the same reading doctor and --clear use.+ const native = msysToNativePath(row.local_path);+ if (!posix.isAbsolute(native) && !win32.isAbsolute(native)) return relativeOwns && !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.tsindex 75560b8028..99a5eed1e7 100644--- a/test/sync-relative-anchor-refusal-7630.test.ts+++ b/test/sync-relative-anchor-refusal-7630.test.ts@@ -513,6 +513,8 @@ for (const backend of testBackends()) {expect(refused?.code).toBe('sync_anchor_relative');expect(refused?.message).toContain('relative local_path');expect(refused?.message).not.toContain('"."');+ expect((refused as { fix?: { ask_user?: boolean; argv?: string[]; user_message?: string } } | null)?.fix).toMatchObject({ ask_user: true, argv: ['gbrain', 'sources', 'set-path', 'default', '<dir>'] });+ expect((refused as { fix?: { user_message?: string } } | null)?.fix?.user_message).not.toContain('"."');const { runFenceCensus } = await import('../src/core/fence-repair/census.ts');const [run] = await runFenceCensus(engine, { sourceIds: ['default'], deadline: Date.now() + 60_000 });expect(run?.files).toBe('none');@@ -585,6 +587,18 @@ for (const backend of testBackends()) {expect(await otherSourceOwning(engine, 'default', join(folder, 'notes.md'))).toBeNull();}, 120_000);+ // Value: protects=a sibling whose local_path is absolute for another OS (a Windows drive path read on POSIX) never blocks default+ // write-through, the same reading doctor and set-path --clear use; fails_when=the leak guard calls it relative with the host's+ // isAbsolute, so every sync.repo_path write goes database-only while doctor reports ok; why_new=the case above has only a "."+ // sibling; seam=none; no_claim=what a Windows host does with a POSIX path (posix.isAbsolute covers it there)+ test('a sibling with another OS\'s absolute local_path does not make the leak guard fail closed', async () => {+ await engine.executeRaw("INSERT INTO sources (id, name, local_path) VALUES ('win-host', 'win-host', $1)", ['C:\\brain-example']);+ const { otherSourceOwning } = await import('../src/core/write-through.ts');+ expect(await otherSourceOwning(engine, 'default', join(folder, 'notes.md'))).toBeNull();+ await engine.executeRaw("INSERT INTO sources (id, name, local_path) VALUES ('notes-a', 'notes-a', '.')");+ expect(await otherSourceOwning(engine, 'default', join(folder, 'notes.md'))).toBe('notes-a');+ }, 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