Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(record-origin): relock an already-remote source whatever its git-lfs rules hold; write lock-down keys with --replace-all
garrytan3ac30c5a0a78
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6e58af50a9..a4a09d3fc9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,12 +16,12 @@ identifiers and attribution are available in the pre-removal Git revision### Itemized changes-- `src/core/url-source-origin.ts`: on a managed brain each apply runs inside `withTopologyLocks` and `lockTopologyRows` (a held lock or a running refresh refuses `source_busy`); `--remote --apply` writes the clone-time lock-down (`lfs.fetchexclude=*`, `core.hooksPath=/dev/null`, `lfs.locksverify=false`) into the checkout's `.git/config` with `git config --file` before the row, into the repository's shared config (`--git-common-dir`, so a linked worktree is covered), then reads back the values Git uses there and refuses `write_failed`, naming the key, when a worktree-scoped or included setting overrides one; a source already recorded as remote whose checkout lacks the lock-down gets it (`recorded`), and one that has it stays `already_recorded`; a throw is reported per id as `write_failed`, saying whether the checkout already got all or part of the lock-down, and the batch continues. The committed-attributes read splits each `ls-tree -z` record at its first tab, reads only blobs, and ignores comment lines; a declared-only filter refuses `lfs_tracked` with its own wording. An unborn HEAD with an empty index counts as no committed attributes. The top-level comparison realpath-normalizes both sides. `lfs_unknown` reasons name the cause from Git's own error: checkout missing, not its own repository's top level (an enclosing repository, or none), owned by another user (`safe.directory`), or a Git read failed (for example a corrupt `.git`).+- `src/core/url-source-origin.ts`: on a managed brain each apply runs inside `withTopologyLocks` and `lockTopologyRows` (a held lock or a running refresh refuses `source_busy`); `--remote --apply` writes the clone-time lock-down (`lfs.fetchexclude=*`, `core.hooksPath=/dev/null`, `lfs.locksverify=false`) into the checkout's `.git/config` with `git config --file` before the row, into the repository's shared config (`--git-common-dir`, so a linked worktree is covered), then reads back the values Git uses there and refuses `write_failed`, naming the key, when a worktree-scoped or included setting overrides one; a source already recorded as remote whose checkout lacks the lock-down gets it (`recorded`, with no `lfs_tracked` refusal even when its files now use git-lfs), and one that has it stays `already_recorded`; each key is written with `--replace-all`, so one that holds several values gets the one clone-time value; a throw is reported per id as `write_failed`, saying whether the checkout already got all or part of the lock-down, and the batch continues. The committed-attributes read splits each `ls-tree -z` record at its first tab, reads only blobs, and ignores comment lines; a declared-only filter refuses `lfs_tracked` with its own wording. An unborn HEAD with an empty index counts as no committed attributes. The top-level comparison realpath-normalizes both sides. `lfs_unknown` reasons name the cause from Git's own error: checkout missing, not its own repository's top level (an enclosing repository, or none), owned by another user (`safe.directory`), or a Git read failed (for example a corrupt `.git`).- `src/core/hardened-git.ts`: `rev-parse --git-common-dir` joins the read-only allow-list, so the lock-down finds a linked worktree's shared config.- `src/core/persistence/topology-clone.ts`: a managed clone or reclone takes the source config it clones with from its locked admission read, not from the read before it took its locks.- `src/core/behavior-change-notice.ts`: one row for the changed `record-origin` behavior.- Docs: `docs/guides/repair.md#url-source-origin` (checkout lock-down and why, `source_busy`, `write_failed`, `lfs_tracked` and `lfs_unknown` causes), the key-files row in `docs/architecture/key-files/files-and-sync-2.md`, `docs/architecture/canonical-writers.tsv`.-- Tests: `test/url-source-origin.test.ts` (PGLite and Postgres): a record attempted while a reclone holds its locks is refused and writes nothing; a reclone that read the source before a record landed clones with the recorded settings; the existing checkout gets the lock-down on apply and not on preview; a tab-named directory's `.gitattributes` is read; commented rules are ignored and a declared-only rule is worded as declared; an empty-repository clone records; each `lfs_unknown` cause is named; a mid-batch write failure is reported for its id while the others record; a record during an active refresh is refused `source_busy`; a lock-down that fails part way is reported as a partial change; a worktree-scoped override is refused `write_failed` naming the key; a linked worktree gets the lock-down where Git reads it; a source recorded as remote before the lock-down existed gets it on the next apply; a corrupt `.git` and a `safe.directory` refusal each get their own `lfs_unknown` reason.+- Tests: `test/url-source-origin.test.ts` (PGLite and Postgres): a record attempted while a reclone holds its locks is refused and writes nothing; a reclone that read the source before a record landed clones with the recorded settings; the existing checkout gets the lock-down on apply and not on preview; a tab-named directory's `.gitattributes` is read; commented rules are ignored and a declared-only rule is worded as declared; an empty-repository clone records; each `lfs_unknown` cause is named; a mid-batch write failure is reported for its id while the others record; a record during an active refresh is refused `source_busy`; a lock-down that fails part way is reported as a partial change; a worktree-scoped override is refused `write_failed` naming the key; a linked worktree gets the lock-down where Git reads it; a source recorded as remote before the lock-down existed gets it on the next apply; a corrupt `.git` and a `safe.directory` refusal each get their own `lfs_unknown` reason; a source recorded as remote whose files now use git-lfs still gets the lock-down, and a key that already holds several values gets the one clone-time value.## [0.60.625.0] - 2026-11-09diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex a91d47c79c..1cf57e605d 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -2848,7 +2848,8 @@ and the command then reads back the values Git uses in the checkout: when aworktree-scoped or included setting still overrides one, it refuses`write_failed`, names the key, and says the settings were already written.A source already recorded as remote whose checkout lacks the lock-down (one-recorded before this) gets it on `--remote --apply` and reports `recorded`;+recorded before this) gets it on `--remote --apply` and reports `recorded`,+even when its files now use git-lfs (no `lfs_tracked` refusal for it);one that has it stays `already_recorded`, and the record is never removed.Only `.git/config` changes, never a tracked file; a gbrain frontmatterhook wired through `core.hooksPath` stops running there, as on any clone adiff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 3288b80a7f..9368f567d1 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1493,7 +1493,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.622.0', id: 'think_window_trajectory_gst731', text: '`think` on a question that names one month or day (or given `since`/`until`) still gets the entity\'s trajectory: each metric\'s latest reading before the window now joins the readings inside it, as the value in force when the window opens (unless its `valid_until` ended by then), and an entity page dated outside the window still names whose trajectory to read (it stays out of the evidence). Before, "MRR in April" with readings in March and June, or a company page last edited outside the month, injected no trajectory. `WINDOW_EXCLUDED_<n>_TRAJECTORY_POINTS` no longer counts the carried reading.' },{ since: '0.60.622.0', id: 'purge_cancels_deferred_write_gst1602', text: 'With `persistence.refresh_barrier` on, `gbrain forget <id> --purge` no longer refuses `purge_blocked_pending_recovery` while a write deferred behind a refresh holds the claim in its staged body: the deferred write was never claimed, so the purge cancels it with `purged_content`, as it does a queued one, and removes the content at once instead of up to 15 minutes later.' },{ since: '0.60.623.0', id: 'owner_reads_private_gst2059', text: 'A brain can now name its owner: `gbrain access owner set <member>` (config `access.owner_member`, an enabled member). Every remote credential or stdio server bound to that member reads the whole brain, `visibility: private` pages, privacy-alias pages (`access: private`, ...), derived and pinned-question pages included, through `get_page`, `search`, `query`, `recall` and the other page reads in the sources its grant reads, and `recall` returns its private facts. `readable_by` and folder rules still bind it, and it is not relaxed when it speaks for a shared-room audience. A page the owner\'s credential creates in `default` through `put_page` is not kept to the owner (no `readable_by: [member:<id>]`). Other members, unbound and public callers are unchanged. `_meta.access.relaxation` reads `owner` for such a call, and `gbrain access check --token <name>` reports it. `gbrain repair private-labels` (named runs only) previews the `access: private` labels stamped on ordinary pages with counts per category, keeps real restrictions, and clears the rest in reviewed batches (`--apply --expect <hash>`), recording each change so `--class restore` can put it back. The bulk-ingestion page template no longer writes an `access:` line; what each designation means is in docs/guides/access-designations.md.' },- { since: '0.60.626.0', id: 'record_origin_checkout_lockdown_8888', text: '`gbrain sources record-origin <id>... --remote --apply` now also turns git-lfs downloads and hooks off in each recorded source\'s existing checkout (its `.git/config`: `lfs.fetchexclude=*`, `core.hooksPath=/dev/null`, `lfs.locksverify=false`), not only on later reclones, in the repository\'s shared config, and refuses `write_failed` naming the key when a worktree-scoped or included setting still overrides one; a source already recorded as remote gets the lock-down on the next `--remote --apply` when its checkout lacks it. On a managed brain it takes the same worktree locks a reclone takes, refusing the id `source_busy` while a reclone, refresh or write holds them, and a reclone reads the record under its own locks. A write that fails is reported for its own id (`write_failed`) instead of ending the run. The git-lfs check reads a committed `.gitattributes` under a directory whose name holds a tab, ignores commented-out `filter=lfs` lines, says when a committed rule only declares an LFS filter, records a clone of an empty repository instead of refusing it, and names the cause of an `lfs_unknown` refusal.' },+ { since: '0.60.626.0', id: 'record_origin_checkout_lockdown_8888', text: '`gbrain sources record-origin <id>... --remote --apply` now also turns git-lfs downloads and hooks off in each recorded source\'s existing checkout (its `.git/config`: `lfs.fetchexclude=*`, `core.hooksPath=/dev/null`, `lfs.locksverify=false`), not only on later reclones, in the repository\'s shared config, and refuses `write_failed` naming the key when a worktree-scoped or included setting still overrides one; a source already recorded as remote gets the lock-down on the next `--remote --apply` when its checkout lacks it, even when its files now use git-lfs, and a key that holds several values in that config is replaced by the one clone-time value. On a managed brain it takes the same worktree locks a reclone takes, refusing the id `source_busy` while a reclone, refresh or write holds them, and a reclone reads the record under its own locks. A write that fails is reported for its own id (`write_failed`) instead of ending the run. The git-lfs check reads a committed `.gitattributes` under a directory whose name holds a tab, ignores commented-out `filter=lfs` lines, says when a committed rule only declares an LFS filter, records a clone of an empty repository instead of refusing it, and names the cause of an `lfs_unknown` refusal.' },];/** The newest disclosed change's release: the notice id moves only when a release adds rows. */diff --git a/src/core/url-source-origin.ts b/src/core/url-source-origin.tsindex 4398105e7b..594a861de5 100644--- a/src/core/url-source-origin.ts+++ b/src/core/url-source-origin.ts@@ -124,7 +124,7 @@ function lockDownCheckout(root: string, progress: { state: 'none' | 'started' |const file = join(resolve(root, commonDir.stdout.toString('utf8').replace(/\n$/, '')), 'config');progress.state = 'started';for (const [key, value] of LOCK_DOWN)- execFileSync('git', ['config', '--file', file, key, value], { cwd: dirname(file), env: hardenedGitEnvironment(), stdio: 'pipe', timeout: GIT_LIMITS.timeoutMs });+ execFileSync('git', ['config', '--file', file, '--replace-all', key, value], { cwd: dirname(file), env: hardenedGitEnvironment(), stdio: 'pipe', timeout: GIT_LIMITS.timeoutMs });progress.state = 'written';const gaps = lockDownGaps(root);if (gaps.length) throw new Error(`Git in the checkout at ${root} still reads another value for ${gaps.join(', ')} (a worktree-scoped or included config overrides ${file}).`);@@ -141,15 +141,19 @@ export interface RecordOriginResult {const refusal = (id: string, code: NonNullable<RecordOriginResult['code']>, reason: string): RecordOriginResult => ({ source_id: id, status: 'refused', code, reason });-/** The refusal a `--remote` record gets when the checkout is not known to be free of git-lfs, or null. */-function lfsRefusal(id: string, root: string): RecordOriginResult | null {+/**+ * The refusal a `--remote` record gets when the checkout is not known to be free of git-lfs, or null. A `relock` (a source+ * already recorded as remote) keeps its record whatever its files hold, so only a checkout Git cannot place refuses it.+ */+function lfsRefusal(id: string, root: string, relock: boolean): RecordOriginResult | null {const lfs = lfsTracked(root);+ if (relock && lfs.state !== 'unknown') return null;const why = 'A clone with git-lfs downloads off holds pointer files where this checkout holds their content, so its next reclone would not match the canonical manifest, '+ `and converting the checkout rewrites its files, which this command does not do; nothing changed. If the source should not keep these files, check that nothing in its checkout is unpushed or only local, then remove it (gbrain sources remove ${id} --confirm-destructive).`;if (lfs.state === 'tracked') return refusal(id, 'lfs_tracked', `Source ${id} tracks files with git-lfs. ${why}`);if (lfs.state === 'declared') return refusal(id, 'lfs_tracked', `A committed .gitattributes in source ${id} declares a git-lfs filter (filter=lfs), so a reclone would store matching files with git-lfs. ${why}`);if (lfs.state !== 'unknown') return null;- const unconfirmed = 'so gbrain cannot confirm that no file is tracked with git-lfs; nothing changed.';+ const unconfirmed = relock ? 'so gbrain cannot turn git-lfs downloads and hooks off in it; nothing changed.' : 'so gbrain cannot confirm that no file is tracked with git-lfs; nothing changed.';if (lfs.cause === 'missing') return refusal(id, 'lfs_unknown', `Source ${id}'s checkout at ${root} is missing, ${unconfirmed} Restore the checkout, then run this again.`);if (lfs.cause === 'not_top_level') return refusal(id, 'lfs_unknown', `Source ${id}'s clone directory ${root} is not the top level of its own Git repository (Git resolves it to an enclosing repository, or to none), ${unconfirmed} Check what that directory holds before you run this again.`);if (lfs.cause === 'unsafe_owner') return refusal(id, 'lfs_unknown', `Git refuses to read source ${id}'s checkout at ${root} because another user owns it (safe.directory), ${unconfirmed} Check who owns that checkout, then run this again as that user or after fixing its ownership.`);@@ -196,7 +200,7 @@ export async function recordSourceOrigin(engine: BrainEngine, ids: string[], optconst early = check(row, id, opts.origin);const relock = remote && early?.status === 'already_recorded' && originState(row!) === 'remote' && existsSync(row!.local_path!) && lockDownGaps(row!.local_path!).length > 0;if (early && !relock) { results.push(early); continue; }- const lfs = remote ? lfsRefusal(id, row!.local_path!) : null;+ const lfs = remote ? lfsRefusal(id, row!.local_path!, relock) : null;if (lfs) { results.push(lfs); continue; }if (!opts.apply) {results.push({ source_id: id, status: 'would_record', reason: relock@@ -211,7 +215,7 @@ export async function recordSourceOrigin(engine: BrainEngine, ids: string[], optif ((again && !(relock && again.status === 'already_recorded')) || (!again && relock) || locked!.local_path !== row!.local_path || config.remote_url !== parseSourceConfig(row!.config).remote_url)return refusal(id, 'source_changed', `Source ${id} changed while its origin was being recorded; nothing changed. Run the preview again.`);if (remote) {- const lfsNow = lfsRefusal(id, locked!.local_path!);+ const lfsNow = lfsRefusal(id, locked!.local_path!, relock);if (lfsNow) return lfsNow;lockDownCheckout(locked!.local_path!, lockDown);}diff --git a/test/url-source-origin.test.ts b/test/url-source-origin.test.tsindex 81b70eae95..62b87bf12b 100644--- a/test/url-source-origin.test.ts+++ b/test/url-source-origin.test.ts@@ -457,6 +457,29 @@ for (const flavor of ['pglite', ...(databaseUrl ? ['postgres'] : [])] as const)expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: true, remote: false })).results[0]).toMatchObject({ status: 'already_recorded' });}), 60_000);+ test('a source recorded as remote whose remote later committed a git-lfs rule still gets the lock-down, not an lfs_tracked refusal', () => brain(async () => {+ const root = defaultCloneDir('source-a');+ repoAt(root, { 'example.md': 'Canonical\n', '.gitattributes': '*.bin filter=lfs diff=lfs merge=lfs -text\n' });+ await insert('source-a', root, { ...URL_CONFIG, added_by_remote_caller: true });+ expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: false, remote: false })).results[0]).toMatchObject({ status: 'would_record' });+ const applied = (await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: true, remote: false })).results[0]!;+ expect(applied).toMatchObject({ status: 'recorded' });+ expect(applied.reason).not.toContain('sources remove');+ expect([effective(root, 'lfs.fetchexclude'), effective(root, 'core.hooksPath'), effective(root, 'lfs.locksverify')]).toEqual(['*', '/dev/null', 'false']);+ expect(await config('source-a')).toEqual({ ...URL_CONFIG, added_by_remote_caller: true });+ }), 60_000);++ test('a shared config that already holds several values for a lock-down key gets one clone-time value, so the record is written', () => brain(async () => {+ const root = defaultCloneDir('source-a');+ repoAt(root, { 'example.md': 'Canonical\n' });+ git(['-C', root, 'config', '--add', 'lfs.fetchexclude', 'docs/*']);+ git(['-C', root, 'config', '--add', 'lfs.fetchexclude', 'media/*']);+ await insert('source-a', root, URL_CONFIG);+ expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: true, remote: false })).results[0]).toMatchObject({ status: 'recorded' });+ expect(execFileSync('git', ['-C', root, 'config', '--get-all', 'lfs.fetchexclude'], { encoding: 'utf8' }).trim()).toBe('*');+ expect(await config('source-a')).toEqual({ ...URL_CONFIG, added_by_remote_caller: true });+ }), 60_000);+test('lfs_unknown tells a corrupt .git and a safe.directory refusal apart from a directory outside its own repository', () => brain(async () => {const root = defaultCloneDir('source-a');repoAt(root, { 'example.md': 'Canonical\n' });