Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Record local URL adds and close the git-lfs gaps in record-origin
garrytan86f8c180a36a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 683aa28607..f00d5b0f5c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,15 +12,16 @@ identifiers and attribution are available in the pre-removal Git revision## [0.60.617.0] - 2026-11-08-**Older URL sources can get the same reclone settings as new ones remote callers add.** URL sources added before gbrain recorded who added them carry no record, and nothing stored tells a remote caller's add from the local operator's, so their reclones used the clone settings of a local add. The new doctor check `url_source_origin` lists them and asks the user who added each one. `gbrain sources record-origin <id>... --remote|--local` records that answer, as a preview until `--apply`. With `--remote`, every later reclone of the source clones with git-lfs downloads and hooks off, like a source a remote caller adds today. A checkout that tracks git-lfs files is refused, because changing its settings would make the next reclone fail to match its canonical content. Nothing changes on upgrade.+**Older URL sources can get the same reclone settings as new ones remote callers add.** URL sources added before gbrain recorded who added them carry no record, and nothing stored tells a remote caller's add from the local operator's, so their reclones used the clone settings of a local add. The new doctor check `url_source_origin` lists them and asks the user who added each one. `gbrain sources record-origin <id>... --remote|--local` records that answer, as a preview until `--apply`. A URL source the local operator adds from now on records that it was a local add, so doctor never asks about it. With `--remote`, every later reclone of the source clones with git-lfs downloads and hooks off, like a source a remote caller adds today. A checkout that tracks git-lfs files is refused, because changing its settings would make the next reclone fail to match its canonical content. Nothing changes on upgrade.**`gbrain frontmatter install-hook` reads a clone's hooks-off setting from that clone's own config only, and its skip message names only that setting.** The check that leaves a clone with `core.hooksPath=/dev/null` alone now runs with `GIT_DIR`, `GIT_WORK_TREE` and the `GIT_CONFIG*` variables scrubbed, so a variable inherited from the caller's shell cannot point the read at another repository or config file. The `skipped — …` line says the clone's own config turns hooks off, without naming a cause it did not check.### Itemized changes- `src/commands/frontmatter-install-hook.ts`: `hooksOffInClone` runs `git config --local --get core.hooksPath` with `gitChildEnv()` minus `GIT_CONFIG_GLOBAL`, `GIT_CONFIG_SYSTEM` and `GIT_CONFIG_NOSYSTEM`. `hooksPathBlocker` returns `hooks are off in this clone's own config (core.hooksPath=/dev/null), and gbrain leaves them off`. Other git reads in the file keep the caller's environment.-- `src/core/url-source-origin.ts`, `src/commands/sources-record-origin.ts`, `src/commands/doctor/checks/url-source-origin.ts`, `src/commands/doctor/wave-checks.ts`: doctor `url_source_origin` (host-only, read-only) lists active URL sources in the default clone directory with no `added_by_remote_caller` record; a clone in another directory is a local add and is not listed. `gbrain sources record-origin` is trusted local CLI only (`trusted_local_only` for any other caller) and writes the record in one transaction that re-reads the row. `--remote` refuses `lfs_tracked` (a tracked path with `filter=lfs`) and `lfs_unknown` (a checkout that is missing or Git cannot list); `--local` never replaces a remote record. Without git-lfs files the tracked bytes of a reclone are unchanged, so the stored writer manifest is not rewritten.-- Tests: `test/frontmatter-install-hook.test.ts`; `test/url-source-origin.test.ts` on PGLite and Postgres; the doctor goldens gain the `url_source_origin` row.+- `src/core/url-source-origin.ts`, `src/commands/sources-record-origin.ts`, `src/commands/doctor/checks/url-source-origin.ts`, `src/commands/doctor/wave-checks.ts`: doctor `url_source_origin` (host-only, read-only) lists active URL sources in the default clone directory with no `added_by_remote_caller` record, including a clone there from before the ownership marker; a clone in another directory is a local add and is not listed. Only an explicit `true` reads as a remote caller's add. `gbrain sources record-origin` is trusted local CLI only (`trusted_local_only` for any other caller) and writes the record in one transaction that re-reads the row. `--remote` refuses `lfs_tracked` (a tracked path with `filter=lfs`, or a committed `.gitattributes` naming `filter=lfs`, so clone-local attributes cannot hide git-lfs files) and inspects the checkout again under the row lock before it writes, and refuses `lfs_unknown` (a checkout that is missing or Git cannot list); `--local` never replaces a remote record. Without git-lfs files the tracked bytes of a reclone are unchanged, so the stored writer manifest is not rewritten.+- `src/core/sources-ops.ts`, `src/core/persistence/managed-sources.ts`: a URL add records `added_by_remote_caller: false` when the local operator adds it, as both add paths already recorded `true` for a remote caller.+- Tests: `test/frontmatter-install-hook.test.ts`; `test/sources-remote-lfs.test.ts`; `test/url-source-origin.test.ts` on PGLite and Postgres; the doctor goldens gain the `url_source_origin` row.## [0.60.616.0] - 2026-11-08diff --git a/docs/architecture/key-files/files-and-sync-2.md b/docs/architecture/key-files/files-and-sync-2.mdindex aa55f85b6f..15b0461862 100644--- a/docs/architecture/key-files/files-and-sync-2.md+++ b/docs/architecture/key-files/files-and-sync-2.md@@ -29,5 +29,5 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL- `src/core/persistence/sync-entry-fault.ts` — GBRA-133 (#6712 PR 4b): the per-entry faults of a managed sync freeze, so one odd file never stops its source. `nullBytesRefusal` holds a Markdown text with U+0000 `content_null_bytes` (line of the first NUL; a UTF-16 byte order mark says save as UTF-8), from `screenFrozenImport` with holds on and as a typed run stop under `sync.holds=fail`. `committedContentOrMissing` (called by `pinnedContent` when the pinned read fails) imports a committed file whose blob object is gone from working-tree bytes that hash to that blob id, else throws `git_object_missing`; a committed name with U+FFFD that names nothing on disk (a name that is not UTF-8) throws `path_not_utf8` first. `directoryAt` lets a deletion whose name became a directory apply (freeze and the owner's raw-hash check). `freezeFaultHold` is `freezeEntry`'s catch (`sync-run.ts` wraps `freezeEntryOnce`): `git_object_missing` and `path_not_utf8` are held under their codes; a page-body fault (`PAGE_BODY_FAULT_CODES`: `page_body_segmented`, `page_body_rows_unreadable`, e.g. a segmented page whose segment rows don't cover its body) is held `page_write_failed` with `write_failure.code` set to that code, which `sync status` classifies by the `freeze_page_body_*` rows of `SYNC_FAULT_TABLE`; any other `OperationError` and every run-scoped error (`classifyItemFailure` scope run, connection, statement timeout, lock, back-pressure, abort) rethrows, and an unexpected throw is held `page_write_failed` with `write_failure.origin: 'freeze'` and `request_id: null`, re-tested by every sync until held `HOLD_ATTEMPTS_NEEDS_HUMAN` times; a run with `ITEM_BREAKER_MIN_ITEMS` faults and no more clean freezes than faults is systemic and rethrows.- `src/core/url-clone.ts` — gbrain-owned `sources add --url` clones (#6946, GBRA-130 C-2). `isOwnedClone` (config `managed_clone` or a `local_path` under the default clone dir) and `defaultCloneDir`; `urlCloneReadOnly` is the one predicate (owned clone on a brain without managed persistence) the fence writer (`fence-write.ts`, `databaseOnlyReason: 'url_clone'`), write-through (`skipped: 'url_clone'`), timeline write-through, unfenced-facts adoption, the empty-fence guard and `sourceMirrorReadOnly` consult, so nothing gbrain writes lands in a checkout it updates with `git pull --ff-only`. `urlClonePullBlock` reads the tracked changes of a clone whose pull failed and returns `pull_blocked_local_changes` with an `ask_user` fix naming the preview `gbrain repair url-clone-fences --source <id>`; `formatUrlClonePullBlock` renders it for sync's stderr (`sync/preflight.ts`) and `cycle.ts` puts it in the sync phase detail as `pull_blocked`. Test: `test/pathological/c2-url-clone-fences.test.ts`.-- `src/core/url-source-origin.ts`, `src/commands/sources-record-origin.ts` and `src/commands/doctor/checks/url-source-origin.ts` — who added a URL source (`sources.config.added_by_remote_caller`). URL sources added before that record existed carry none, and nothing stored tells a remote caller's add from the local operator's (same row, same default clone directory); a clone outside `$GBRAIN_HOME/clones/<id>` (or a `clones/<id>` under an earlier home) proves a local add. `urlSourcesWithoutOrigin` (read-only) feeds the wave check `url_source_origin` (host-only, `resolution: 'operator'`, `warn` with an `ask_user` fix whose argv is the preview). `recordSourceOrigin(engine, ids, { origin, apply, remote })` backs `gbrain sources record-origin <id>... --remote|--local [--apply] [--json]`: it throws `trusted_local_only` unless `remote === false`, previews unless `apply`, writes `true` (`--remote`) or `false` (`--local`) in one transaction that re-reads the row `FOR UPDATE` and refuses `source_changed` if its path or URL moved, never removes a `true`, and refuses `--remote` for a checkout with any tracked `filter=lfs` path (`lfs_tracked`, read with `hardenedGitSync` `ls-files -- ':(attr:filter=lfs)'`) or one Git cannot list (`lfs_unknown`), because a lock-down reclone would not match the stored writer manifest there. Without git-lfs files the tracked bytes are the same, so no manifest is written. Pinned by `test/url-source-origin.test.ts` (PGLite and Postgres).+- `src/core/url-source-origin.ts`, `src/commands/sources-record-origin.ts` and `src/commands/doctor/checks/url-source-origin.ts` — who added a URL source (`sources.config.added_by_remote_caller`). Both add paths write it (`true` for a remote caller, `false` for the local operator); only an explicit `true` reads as remote. URL sources added before that record existed carry none, and nothing stored tells a remote caller's add from the local operator's (same row, same default clone directory); a clone outside `$GBRAIN_HOME/clones/<id>` (or a `clones/<id>` under an earlier home) proves a local add. A URL clone counts when `isOwnedClone` accepts it, so an unmarked clone at the default directory is listed too. `urlSourcesWithoutOrigin` (read-only) feeds the wave check `url_source_origin` (host-only, `resolution: 'operator'`, `warn` with an `ask_user` fix whose argv is the preview). `recordSourceOrigin(engine, ids, { origin, apply, remote })` backs `gbrain sources record-origin <id>... --remote|--local [--apply] [--json]`: it throws `trusted_local_only` unless `remote === false`, previews unless `apply`, writes `true` (`--remote`) or `false` (`--local`) in one transaction that re-reads the row `FOR UPDATE` and refuses `source_changed` if its path or URL moved, never removes a `true`, and refuses `--remote` for a checkout with any tracked `filter=lfs` path (`lfs_tracked`, read with `hardenedGitSync` `ls-files -- ':(attr:filter=lfs)'` plus every committed `.gitattributes` from `ls-tree`/`cat-file`, so clone-local attributes cannot hide one, and read again under the row lock) or one Git cannot list (`lfs_unknown`), because a lock-down reclone would not match the stored writer manifest there. Without git-lfs files the tracked bytes are the same, so no manifest is written. Pinned by `test/url-source-origin.test.ts` (PGLite and Postgres).- `src/core/write-through.ts` — shared atomic disk write-through for the canonical ingestion path. `resolvePageWriteTarget(engine, slug, sourceId, { includeDeleted? })` (active rows by default; `includeDeleted: true` resolves a tombstone's recorded `source_path` for the purge remediation retry) is the exported single source of truth for the ONE file a (source, slug) pair lives in on disk: a source with its own `local_path` writes at that tree's ROOT (never nested under `.sources/`); a source without one nests under the host repo (`sync.repo_path`, default at root, non-default under `.sources/<id>/`) unless that path is another source's own working tree (leak guard → `source_repo_belongs_to_other_source`; a live source whose `local_path` is relative counts as owning every tree, and a relative `local_path` or `sync.repo_path` target is `repo_not_found`); the page's recorded `source_path` (or a contained absolute `file://` `source_uri`; a relative or host-only `file://` names no local file and is read as absent) is preferred over a slug-derived name so writes land in the file of record instead of minting a twin; and `isWriteTargetContained` rejects hostile rows escaping the tree (`path_escapes_source_root`). The ok-result also carries `sourcePathToBind` — the target expressed in the file scanner's `source_path` convention for the source's pinned `slug_root_mode` (or the mode its first sync would pin, via `scannerSlugRootMode`): GIT-ROOT-relative under `git-root` when the scan root sits inside a git repo, scan-root-relative under `source-root` and for non-git or git-root scan roots, so a put-born page binds the same form managed sync and delete-reconcile key on. Every recorded-path reader (write targets, takes, reconcile, audit, export restore, doctor) decodes with the same mode (`resolveSourceLocalFilePath(..., slugRootMode)`): under `source-root` the scope prefix is removed only from the legacy Git-root form whose remainder names the page slug, and basename rows resolve under the unstripped slug directory. The resolver is shared by `writePageThrough`, the facts fence writer (`src/core/facts/fence-write.ts`), and fence forget (`src/core/facts/forget.ts`) — the fence appends to the page's file, so all writers MUST compute the identical path or the fence lands in a file sync never reads back and the next extract_facts reconcile deletes the fence-owned DB rows. `writePageThrough(engine, slug, {sourceId?, frontmatterOverrides?, inlineOnly?, logger?, expectedFileBytes?, deferCommit?})` resolves the target through it, re-reads the just-written DB row (`getPage`), renders an inline page via `serializePageToMarkdown`, and writes the `.md`. A page stored in segments is never rendered whole: its file is measured from its segments at the snapshot's revision (`unreadPageFile`; a page that moved since refuses `revision_conflict`), checked against `PUSH_SCAN_MAX_FILE_BYTES` before `source_path` is bound (a body already over the cap by `body_bytes` is refused without streaming), streamed through `stageStreamedContent` on the engine it was given (so a caller inside a transaction works) keeping the existing file's mode, and renamed over the target after the file-changed check; `frontmatterOverrides` on such a page return an error result, and `inlineOnly` (the derived writer) skips it as `segmented_page` so the brain has a committable artifact that round-trips through `gbrain sync`. Rendering from the row keeps the serialized content consistent at write time. The fsync-backed `atomicWriteFileSync` stages and renames the file after required source-path bookkeeping; lock/lease checks run before publication. `put_page` holds the root lock before importing and invokes write-through inside its database transaction, so ordinary filesystem rejection rolls back existing content, tags, chunks, and versions. A crash or database COMMIT failure after rename can still leave canonical Markdown ahead of the index; this is not distributed ACID or a durable queue. Never throws — returns `WriteThroughResult { written, path?, content? (the bytes written; inline pages only), skipped?: 'disabled_by_config' | 'url_clone' | 'no_repo_configured' | 'repo_not_found' | 'source_repo_belongs_to_other_source' | 'page_not_found_after_write' | 'path_escapes_source_root' | 'case_insensitive_collision' | 'file_changed' | 'over_push_scan_cap' | 'segmented_page', warning?, error? }` (`file_changed`: the caller's `expectedFileBytes` preimage, null for absent, no longer matches the file under the source filesystem lock, so the file is left untouched) so the caller decides messaging + exit codes. `withNoRepoWriteThroughWarning(result, sourceId)` decorates an unwritten `no_repo_configured` result with `warning` (the write landed only in the database; bind the token/server to a git-backed source or configure `local_path`/`sync.repo_path`); `put_page` applies it for untrusted callers only (`ctx.remote !== false`), so local CLI responses and the `disabled_by_config` operator opt-out stay silent. `isWriteThroughDisabled` (shared with the fence lane, ~30s per-engine cache) honors the `sync.write_through` opt-out. Trust gating (subagent sandbox, dry-run) stays at the CALLER. On a durability-hardened repo (`isDurabilityHardened` — the gbrain post-commit hook is installed, i.e. the user ran `gbrain sources harden`), a successful write is best-effort COMMITTED via `commitWriteThroughFile` (path-limited `git commit -- <file>`, never sweeps unrelated edits; the hook then background-pushes) unless the caller passes `deferCommit` (the derived write-through commits its batch itself, #8073; `deletePageThrough` takes it too) so write-through content reaches git instead of accumulating uncommitted forever; result carries `committed?: boolean`. Unhardened repos keep write-only behavior. `deletePageThrough(engine, slug, {sourceId?, target?, logger?})` is the delete-side sink (sole caller: the `delete_page` op): resolves the same target, unlinks the `.md`, and on a hardened repo best-effort commits the removal path-limited through the same helper with subject `gbrain: delete write-through <slug>`, reporting `committed?: boolean` on `DeleteThroughResult` (absent when unhardened or when the commit did not land — the unlink still stands; a never-committed page leaves nothing to commit, so no empty commit). Pinned by `test/delete-write-through-commit.test.ts`. Consumers: `put_page` op and `gbrain brainstorm/lsd --save` via `persistSavedIdea`. Pinned by `test/write-through.test.ts` + `test/write-through-commit.test.ts`. The `put_page` op is that deciding caller for direct writes: a skip outside the deliberate DB-only set (`no_repo_configured`/`disabled_by_config`/`subagent_sandbox`/`dry_run`) throws `storage_error` inside the import transaction, rolling back the page revision, tags, chunks and version snapshot instead of deleting a just-created row afterward. #6946: on a gbrain-owned `--url` clone of an unmanaged brain (`urlCloneReadOnly`), `writePageThrough` returns `skipped: 'url_clone'` and `deletePageThrough` returns `removed: false, skipped: 'url_clone'`; `timeline-write-through.ts` skips the same way.diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex 84a27c5fe5..5184f4732f 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -2815,7 +2815,8 @@ remediation plan, the maintenance cycle or an upgrade.A URL source a remote caller (an MCP client) adds records that on its row, andevery later reclone of it, managed or not, clones with git-lfs downloads and-hooks off. URL sources added before that record existed carry none, and nothing+hooks off; a URL source the local operator adds records that too, so doctor+never asks about it. URL sources added before that record existed carry none, and nothingstored tells a remote caller's add from the local operator's: both wrote thesame row and cloned into the default clone directory (`$GBRAIN_HOME/clones/<id>`).Their reclones use the clone settings of a local add. Doctor@@ -2843,7 +2844,9 @@ stored writer manifest needs no change and the reclone verifies as before.`--remote` refuses, writing nothing:- `lfs_tracked`: the checkout tracks files with git-lfs (any tracked path with- `filter=lfs`). A clone with git-lfs downloads off holds pointer files where+ `filter=lfs`, or a committed `.gitattributes` that names `filter=lfs`; a+ reclone gets only the committed attributes, so clone-local ones do not count).+ An apply inspects the checkout again under the row lock before it writes. A clone with git-lfs downloads off holds pointer files wherethis checkout holds their content, so its next reclone would stop at`writer_manifest_mismatch`, and converting the checkout rewrites its files,which this command does not do. If the source should not keep those files,diff --git a/src/core/persistence/managed-sources.ts b/src/core/persistence/managed-sources.tsindex 1e1d33bb3c..7fec36793c 100644--- a/src/core/persistence/managed-sources.ts+++ b/src/core/persistence/managed-sources.ts@@ -33,7 +33,7 @@ export function managedSourceAddInput(opts:AddSourceOpts):SourceLifecycleInput{throw new OperationError('invalid_params','force and federated must be boolean values.','Pass force and federated as true or false, or omit them.');let path=opts.localPath?resolve(msysToNativePath(opts.localPath)):undefined;let config:Record<string,unknown>=opts.federated==null?{}:{federated:opts.federated};- if(opts.remoteUrl){path=resolve(opts.cloneDir??defaultCloneDir(opts.id));config={...config,remote_url:opts.remoteUrl,managed_clone:true,...(opts.addedByRemoteCaller?{added_by_remote_caller:true}:{})};}+ if(opts.remoteUrl){path=resolve(opts.cloneDir??defaultCloneDir(opts.id));config={...config,remote_url:opts.remoteUrl,managed_clone:true,added_by_remote_caller:opts.addedByRemoteCaller===true};}if(opts.github){const gh=opts.github;path=resolve(msysToNativePath(gh.dir));config={kind:'github',gh_token_env:gh.tokenEnv,gh_handle:gh.handle,gh_scope:gh.scope,gh_repos:gh.repos.join(','),gh_involvement:gh.involvement,diff --git a/src/core/sources-ops.ts b/src/core/sources-ops.tsindex f721eea66a..afcc59530c 100644--- a/src/core/sources-ops.ts+++ b/src/core/sources-ops.ts@@ -577,7 +577,7 @@ export async function addSource(// a --clone-dir path. A user-tree row (created by an external INSERT, no// --url) never carries this, so it can never be deleted by reclone.const config: Record<string, unknown> = { remote_url: parsedUrl.url, managed_clone: true };- if (opts.addedByRemoteCaller) config.added_by_remote_caller = true;+ config.added_by_remote_caller = opts.addedByRemoteCaller === true;if (opts.federated !== null && opts.federated !== undefined) {config.federated = opts.federated;}diff --git a/src/core/url-source-origin.ts b/src/core/url-source-origin.tsindex f2574760bf..da86bea37d 100644--- a/src/core/url-source-origin.ts+++ b/src/core/url-source-origin.ts@@ -5,7 +5,8 @@* not, clones with git-lfs downloads and hooks off (`lfsDownloadsOff`). Sources added before that record existed* carry no record, and nothing else stored on the row, the clone or the request log says whether a remote caller* or the local operator added them: both wrote the same row and cloned into the default clone directory. Only a- * clone outside that directory proves a local add (remote callers cannot choose `clone_dir`).+ * clone outside that directory proves a local add (remote callers cannot choose `clone_dir`). A local URL add now+ * records `false`; only an explicit `true` reads as a remote caller's add, and no record stays undecided.** `urlSourcesWithoutOrigin` lists the undecided ones for the `url_source_origin` doctor check, read-only.* `recordSourceOrigin` (`gbrain sources record-origin`, trusted local CLI only) records the operator's answer:@@ -21,7 +22,7 @@ import type { BrainEngine } from './engine.ts';import { opError } from './ops/contract.ts';import { hardenedGitSync } from './hardened-git.ts';import { normalizeSourceConfig, parseSourceConfig } from './sources-load.ts';-import { defaultCloneDir } from './url-clone.ts';+import { defaultCloneDir, isOwnedClone } from './url-clone.ts';export type SourceOrigin = 'remote' | 'local';type OriginState = 'not_url_clone' | 'custom_clone_dir' | 'remote' | 'local' | 'unknown';@@ -38,10 +39,10 @@ function atDefaultCloneDir(id: string, localPath: string): boolean {function originState(row: SourceRowLike): OriginState {const config = parseSourceConfig(row.config);- if (config.managed_clone !== true || typeof config.remote_url !== 'string' || !row.local_path) return 'not_url_clone';+ if (typeof config.remote_url !== 'string' || !isOwnedClone(row)) return 'not_url_clone';if (config.added_by_remote_caller === true) return 'remote';if (config.added_by_remote_caller === false) return 'local';- return atDefaultCloneDir(row.id, row.local_path) ? 'unknown' : 'custom_clone_dir';+ return atDefaultCloneDir(row.id, row.local_path!) ? 'unknown' : 'custom_clone_dir';}/** Active URL sources in the default clone directory with no record of who added them. */@@ -52,15 +53,28 @@ export async function urlSourcesWithoutOrigin(engine: BrainEngine): Promise<Arra}/**- * `none` only when the checkout is its own repository's top level, Git lists it and no tracked path has `filter=lfs`. A- * missing or unreadable checkout, or a directory Git resolves to an enclosing repository, is not known to be free of git-lfs.+ * `none` only when the checkout is its own repository's top level, Git lists it, no tracked path has `filter=lfs` and no+ * committed `.gitattributes` names `filter=lfs`. The committed files decide because a reclone gets only those: clone-local+ * attributes (`info/attributes`, an edited `.gitattributes`) can hide git-lfs paths from the listing. A missing or+ * unreadable checkout, or a directory Git resolves to an enclosing repository, is not known to be free of git-lfs.*/function lfsTracked(root: string): 'none' | 'tracked' | 'unknown' {- const top = hardenedGitSync(root, ['rev-parse', '--show-toplevel'], { timeoutMs: 30_000, maxBytes: 1024 * 1024 });+ const limits = { timeoutMs: 30_000, maxBytes: 64 * 1024 * 1024 };+ const top = hardenedGitSync(root, ['rev-parse', '--show-toplevel'], limits);if (!top.ok || top.stdout.toString('utf8').trim() !== realpathSync(root)) return 'unknown';- const listed = hardenedGitSync(root, ['ls-files', '-z', '--cached', '--', ':(attr:filter=lfs)'], { timeoutMs: 30_000, maxBytes: 64 * 1024 * 1024 });+ const listed = hardenedGitSync(root, ['ls-files', '-z', '--cached', '--', ':(attr:filter=lfs)'], limits);if (!listed.ok) return 'unknown';- return listed.stdout.length ? 'tracked' : 'none';+ if (listed.stdout.length) return 'tracked';+ const tree = hardenedGitSync(root, ['ls-tree', '-r', '-z', '--full-tree', 'HEAD'], limits);+ if (!tree.ok) return 'unknown';+ for (const entry of tree.stdout.toString('utf8').split('\0')) {+ const [meta, path] = entry.split('\t');+ if (!path || (path !== '.gitattributes' && !path.endsWith('/.gitattributes'))) continue;+ const blob = hardenedGitSync(root, ['cat-file', 'blob', meta!.split(' ')[2]!], limits);+ if (!blob.ok) return 'unknown';+ if (/(^|\s)filter=lfs(\s|$)/m.test(blob.stdout.toString('utf8'))) return 'tracked';+ }+ return 'none';}export interface RecordOriginResult {@@ -72,6 +86,14 @@ 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 {+ const lfs = lfsTracked(root);+ if (lfs === 'tracked') return refusal(id, 'lfs_tracked', `Source ${id} tracks files with git-lfs. 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 === 'unknown') return refusal(id, 'lfs_unknown', `Source ${id}'s checkout is missing or Git could not list it, so gbrain cannot confirm that no file is tracked with git-lfs; nothing changed. Restore the checkout, then run this again.`);+ return null;+}+/** The outcome that needs no checkout inspection, or null when the source can take the record. */function check(row: SourceRowLike | undefined, id: string, origin: SourceOrigin): RecordOriginResult | null {if (!row) return refusal(id, 'not_found', `No source ${id} is registered.`);@@ -88,7 +110,8 @@ function check(row: SourceRowLike | undefined, id: string, origin: SourceOrigin)/*** Records who added each named URL source. Preview unless `apply`; each apply re-reads and locks the row, so a source- * changed since its check is refused `source_changed` and left alone. Trusted local CLI only: `remote` must be false.+ * changed since its check is refused `source_changed` and left alone, and a `remote` record inspects the checkout again+ * under that lock. Trusted local CLI only: `remote` must be false.*/export async function recordSourceOrigin(engine: BrainEngine, ids: string[], opts: { origin: SourceOrigin; apply: boolean; remote?: boolean }): Promise<{ origin: SourceOrigin; apply: boolean; results: RecordOriginResult[] }> {@@ -102,11 +125,8 @@ export async function recordSourceOrigin(engine: BrainEngine, ids: string[], optconst [row] = await engine.executeRaw<SourceRowLike>('SELECT id, local_path, config, archived FROM sources WHERE id=$1', [id]);const early = check(row, id, opts.origin);if (early) { results.push(early); continue; }- if (opts.origin === 'remote') {- const lfs = lfsTracked(row!.local_path!);- if (lfs === 'tracked') { results.push(refusal(id, 'lfs_tracked', `Source ${id} tracks files with git-lfs. 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).`)); continue; }- if (lfs === 'unknown') { results.push(refusal(id, 'lfs_unknown', `Source ${id}'s checkout is missing or Git could not list it, so gbrain cannot confirm that no file is tracked with git-lfs; nothing changed. Restore the checkout, then run this again.`)); continue; }- }+ const lfs = opts.origin === 'remote' ? lfsRefusal(id, row!.local_path!) : null;+ if (lfs) { results.push(lfs); continue; }if (!opts.apply) {results.push({ source_id: id, status: 'would_record', reason: `Source ${id} would be recorded as added by ${recordedAs}${opts.origin === 'remote' ? '; its next reclone clones with git-lfs downloads and hooks off' : ''}.` });continue;@@ -116,6 +136,8 @@ export async function recordSourceOrigin(engine: BrainEngine, ids: string[], optconst config = locked ? parseSourceConfig(locked.config) : {};if (check(locked, id, opts.origin) || 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.`);+ const lfsNow = opts.origin === 'remote' ? lfsRefusal(id, locked!.local_path!) : null;+ if (lfsNow) return lfsNow;await tx.executeRaw('UPDATE sources SET config=$2::text::jsonb WHERE id=$1',[id, JSON.stringify(normalizeSourceConfig({ ...config, added_by_remote_caller: opts.origin === 'remote' }))]);return { source_id: id, status: 'recorded' as const, reason: `Source ${id} is recorded as added by ${recordedAs}.` };diff --git a/test/sources-remote-lfs.test.ts b/test/sources-remote-lfs.test.tsindex b84c3d40e6..f1b471dc78 100644--- a/test/sources-remote-lfs.test.ts+++ b/test/sources-remote-lfs.test.ts@@ -185,7 +185,7 @@ if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: truetest('the trusted local CLI keeps LFS downloads', async () => {await add(false, 'local-added');expect(clones()).toEqual([false]);- expect((await config('local-added')).added_by_remote_caller).toBeUndefined();+ expect((await config('local-added')).added_by_remote_caller).toBe(false);});test('a remote caller\'s failed clone gets a fixed message; the local CLI keeps git\'s error', async () => {diff --git a/test/url-source-origin.test.ts b/test/url-source-origin.test.tsindex ad2065f58c..f98210669d 100644--- a/test/url-source-origin.test.ts+++ b/test/url-source-origin.test.ts@@ -19,6 +19,7 @@ import { lfsDownloadsOff } from '../src/core/sources-ops.ts';import { defaultCloneDir } from '../src/core/url-clone.ts';import { operations } from '../src/core/operations.ts';import { recordSourceOrigin } from '../src/core/url-source-origin.ts';+import { managedSourceAddInput } from '../src/core/persistence/managed-sources.ts';import { urlSourceOriginCheck } from '../src/commands/doctor/checks/url-source-origin.ts';import { runRecordOrigin } from '../src/commands/sources-record-origin.ts';import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';@@ -202,4 +203,52 @@ for (const flavor of ['pglite', ...(databaseUrl ? ['postgres'] : [])] as const)expect(await config('source-a')).toEqual({ ...URL_CONFIG, added_by_remote_caller: false });} finally { exit.mockRestore(); errors.mockRestore(); logs.mockRestore(); _resetCliExitVerdictForTests(); }}), 60_000);++ test('--remote refuses a checkout whose clone-local attributes hide its git-lfs files, and writes nothing', () => brain(async home => {+ const { source, root } = await managedUrlSource(home, true);+ mkdirSync(join(root, '.git', 'info'), { recursive: true });+ writeFileSync(join(root, '.git', 'info', 'attributes'), '*.bin -filter\n');+ for (const apply of [false, true])+ expect((await recordSourceOrigin(engine, [source], { origin: 'remote', apply, remote: false })).results[0]).toMatchObject({ status: 'refused', code: 'lfs_tracked' });+ expect(await config(source)).toEqual(URL_CONFIG);+ }), 120_000);++ test('--remote --apply inspects the checkout again under the row lock, so git-lfs files that arrive after the first check are refused', () => brain(async home => {+ const { source, root } = await managedUrlSource(home, false);+ const transaction = engine.transaction.bind(engine);+ const patched = spyOn(engine, 'transaction').mockImplementation(async fn => {+ writeFileSync(join(root, '.gitattributes'), '*.bin filter=lfs diff=lfs merge=lfs -text\n');+ writeFileSync(join(root, 'blob.bin'), 'binary-example');+ git(['-C', root, 'add', '-A']);+ git(['-C', root, 'commit', '-q', '-m', 'lfs']);+ return transaction(fn);+ });+ try {+ expect((await recordSourceOrigin(engine, [source], { origin: 'remote', apply: true, remote: false })).results[0]).toMatchObject({ status: 'refused', code: 'lfs_tracked' });+ } finally { patched.mockRestore(); }+ expect(await config(source)).toEqual(URL_CONFIG);+ }), 120_000);++ test('doctor lists a URL clone at the default clone directory that has no ownership marker, and record-origin accepts it', () => brain(async home => {+ const unmarked = { remote_url: URL_CONFIG.remote_url };+ await insert('source-a', defaultCloneDir('source-a'), unmarked);+ await insert('source-c', join(home, 'elsewhere', 'source-c'), unmarked);+ expect(((await check()).details as { sources: Array<{ id: string }> }).sources.map(source => source.id)).toEqual(['source-a']);+ expect((await recordSourceOrigin(engine, ['source-a', 'source-c'], { origin: 'local', apply: true, remote: false })).results.map(result => result.code ?? result.status))+ .toEqual(['recorded', 'not_url_clone']);+ expect(await config('source-a')).toEqual({ ...unmarked, added_by_remote_caller: false });+ expect(await config('source-c')).toEqual(unmarked);+ expect((await check()).status).toBe('ok');+ }), 60_000);++ test('a local URL add records the local operator; a row with no record stays unknown and never reads as added by a remote caller', () => brain(async () => {+ expect(managedSourceAddInput({ id: 'source-a', remoteUrl: URL_CONFIG.remote_url }).config).toMatchObject({ added_by_remote_caller: false });+ expect(managedSourceAddInput({ id: 'source-b', remoteUrl: URL_CONFIG.remote_url, addedByRemoteCaller: true }).config).toMatchObject({ added_by_remote_caller: true });+ await insert('source-a', defaultCloneDir('source-a'), URL_CONFIG);+ expect(lfsDownloadsOff(await config('source-a'))).toBe(false);+ const found = await check();+ expect(found.status).toBe('warn');+ expect((found.details as { sources: Array<{ id: string }> }).sources.map(source => source.id)).toEqual(['source-a']);+ expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'local', apply: false, remote: false })).results[0]).toMatchObject({ status: 'would_record' });+ }), 60_000);});