Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Stop record-origin --remote from reporting success while a reclone still clones with git-lfs on

garrytan34f91d34ce72
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a2cfb7c3bd..6f12d08439 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,18 @@ credits are retained; no result has been reassigned to another provider. Origina
identifiers and attribution are available in the pre-removal Git revision
`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).
+## [Unreleased]
+
+**`gbrain sources record-origin --remote` can no longer report success while a reclone still clones with git-lfs on, and it locks down the existing checkout too.** On a managed brain the record took only the source row lock, so a reclone that had already read the source, or held its worktree locks, could clone with git-lfs downloads and hooks on after the command said the source was recorded. The record now takes the worktree locks and rows a reclone takes (refusing the id `source_busy` while one runs), and the reclone reads the record under its own locks. `--remote --apply` now also turns git-lfs downloads and hooks off in the existing checkout's `.git/config`, because a reclone only happens when a checkout is lost or replaced, and until then doctor read green while the checkout still fetched git-lfs objects and ran hooks. The git-lfs check now reads a committed `.gitattributes` under a directory whose name holds a tab (it was cut at the tab and skipped), ignores commented-out `filter=lfs` lines, says "declares a git-lfs filter" when a rule only declares one, and records a clone of an empty repository instead of telling the operator to restore an intact checkout. A failed write is reported for its own id instead of ending the run without saying which ids were recorded.
+
+### Itemized changes
+
+- `src/core/url-source-origin.ts`: on a managed brain each apply runs inside `withTopologyLocks` and `lockTopologyRows` (a held lock 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; a throw is reported per id as `write_failed` 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: checkout missing, not its own repository's top level (an enclosing repository, or none), or a Git read failed.
+- `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.
+
## [0.60.619.0] - 2026-11-08
**`gbrain git commit-push` on a managed checkout no longer reports commits the remote lacks as already pushed.** On a managed canonical worktree, commit-push commits only the named paths and leaves publishing to the persistence owner. When the commit already existed (a script's bare commit, or one an earlier call made), it printed `nothing to commit; origin/main at -`, exited 0 and logged `[push] ok-already-on-remote`, which the push-log reader behind `put_page`'s `lastPushStatus` and doctor counts as a push that landed, while the branch sat ahead of (or diverged from) its upstream. It now says nothing was pushed, reports how many commits the upstream lacks as last fetched (`unpublished` in `--json`), names the command that publishes them, and writes no push-log line. Nothing is pushed or moved; exit codes are unchanged.
diff --git a/docs/architecture/canonical-writers.tsv b/docs/architecture/canonical-writers.tsv
index d546f89093..839426dd2f 100644
--- a/docs/architecture/canonical-writers.tsv
+++ b/docs/architecture/canonical-writers.tsv
@@ -144,7 +144,7 @@ src/core/think/index.ts 1 engine_guard Canonical table SQL trigger requires coor
src/core/timeline-dedup-repair.ts 3 engine_guard Canonical table SQL trigger requires coordinator capability; legacy calls fail before canonical DB publication.
src/core/timeline-write-through.ts 2 filesystem_guard Managed semantic operation journals page changes; legacy atomic writes check root.
src/core/transcripts/ingest.ts 2 engine_guard On writer-claimed brains the CLI ingest submits rendered parts as put_page write requests (submitPageMutation with its trusted-local context). The remaining direct importFromContent (unmanaged brains) and stale-part deletePage calls refuse on managed brains before any write (assertUnmanagedCanonicalWriter / canonical table trigger).
-src/core/url-source-origin.ts 1 projection Source settings (the sources.config merge in recordSourceOrigin, for the trusted local sources record-origin command, under a FOR UPDATE row lock in one transaction); canonical fields remain guarded by SQL triggers.
+src/core/url-source-origin.ts 1 projection Source settings (the sources.config merge in recordSourceOrigin, for the trusted local sources record-origin command, under a FOR UPDATE row lock in one transaction, inside the reclone's topology locks on a managed brain); canonical fields remain guarded by SQL triggers.
src/core/workspace-push.ts 0 filesystem_guard The resolved git root is checked before staging, committing or pulling.
src/core/write-through.ts 1 filesystem_guard Source filesystem lock and canonical page SQL guards reject legacy publication.
src/eval/brainbench/seed.ts 3 isolated_eval Explicit isolated evaluation database only; never opened against the active managed brain. Direct importFromContent calls without prepare refuse on managed brains before any write (assertUnmanagedCanonicalWriter).
diff --git a/docs/architecture/key-files/files-and-sync-2.md b/docs/architecture/key-files/files-and-sync-2.md
index 15b0461862..cd305b0bd6 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`). 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/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; on a managed brain that transaction runs inside `withTopologyLocks` after `lockTopologyRows`, the locks a reclone takes (a held lock refuses the id `source_busy`), and `runManagedSourceClone` takes the config it clones with from its own locked admission read, so a record and a reclone never interleave. `--remote --apply` also writes `LFS_OFF_CLONE_CONFIG` into the checkout's own `.git/config` (`git config --file`, not run in the checkout) before the row; a throw is reported for its id as `write_failed` and the batch continues. It never removes a `true`, and refuses `--remote` for a checkout with any tracked `filter=lfs` path or a committed `.gitattributes` declaring `filter=lfs` outside a comment line (`lfs_tracked`, worded per case; read with `hardenedGitSync` `ls-files -- ':(attr:filter=lfs)'` plus every committed `.gitattributes` blob from `ls-tree -z` records split at their first tab, then `cat-file`, so clone-local attributes cannot hide one, and read again under the locks), or one it cannot inspect (`lfs_unknown`, whose reason names the cause: checkout missing, not its own repository's top level by a realpath comparison, or a Git read failed), because a lock-down reclone would not match the stored writer manifest there. An unborn HEAD with an empty index (a clone of an empty repository) has no committed attributes and passes. 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.md
index 5184f4732f..9970caca5c 100644
--- a/docs/guides/repair.md
+++ b/docs/guides/repair.md
@@ -2835,25 +2835,45 @@ gbrain sources record-origin <id>... --remote --apply # records the id
gbrain sources record-origin <id>... --local --apply # records the rest as local adds
```
-`--remote` writes the same record a remote add writes, so the next reclone
-clones with git-lfs downloads and hooks off; `--local` records a local add, and
+`--remote` writes the same record a remote add writes, so every later reclone
+clones with git-lfs downloads and hooks off, and it turns the same settings on
+in the existing checkout's own `.git/config` now (`lfs.fetchexclude=*`,
+`core.hooksPath=/dev/null`, `lfs.locksverify=false`). That is the safer of the
+two choices: a reclone happens only when a checkout is lost or replaced, so
+without it the checkout would keep fetching git-lfs objects from the endpoint
+its repository names, and keep running hooks, while doctor already reads
+green. Only `.git/config` changes, never a tracked file; a gbrain frontmatter
+hook wired through `core.hooksPath` stops running there, as on any clone a
+remote caller adds. `--local` records a local add and changes no checkout, and
doctor stops listing either. Each apply re-reads the source inside one
-transaction and refuses `source_changed` if it moved since the check. Without
-git-lfs files a clone with these settings holds the same tracked bytes, so the
-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`, 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 where
+transaction and refuses `source_changed` if it moved since the check. On a
+managed brain it first takes the worktree locks and rows a reclone takes, so
+no reclone runs between the record and the clone; while a reclone, refresh or
+write holds them the id is refused `source_busy` (nothing changed; run it
+again once that finishes). A write that fails is reported for its own id as
+`write_failed`, and the other ids keep their results. Without git-lfs files a
+clone with these settings holds the same tracked bytes, so the 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 (a tracked path with
+ `filter=lfs`), or a committed `.gitattributes` declares an LFS filter (a
+ rule naming `filter=lfs`, even one that matches no file yet; comment lines
+ do not count). A reclone gets only the committed attributes, so clone-local
+ ones do not count either.
+ An apply inspects the checkout again under its locks before it writes. A clone with git-lfs downloads off holds pointer files where
this 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,
check that nothing in its checkout is unpushed or only local, then remove it
with `gbrain sources remove <id> --confirm-destructive`.
-- `lfs_unknown`: the checkout is missing or Git cannot list it, so gbrain
- cannot confirm it has no git-lfs files. Restore the checkout and run it again.
+- `lfs_unknown`: gbrain cannot confirm the checkout has no git-lfs files,
+ and the reason names which cause applies: the checkout is missing (restore
+ it, then run this again); the clone directory is not the top level of its
+ own Git repository, because Git resolves it to an enclosing repository or to
+ none (check what the directory holds); or Git could not list or read it. A
+ clone of an empty repository (no commit yet) has no committed attributes,
+ so it is not refused.
- `custom_clone_dir`: the clone sits where only the local CLI can clone.
- `recorded_remote` (`--local`): the source is already recorded as a remote
add; this command never removes that record.
diff --git a/scripts/core-world-reads-baseline.tsv b/scripts/core-world-reads-baseline.tsv
index 67fc274669..7dfe994c3e 100644
--- a/scripts/core-world-reads-baseline.tsv
+++ b/scripts/core-world-reads-baseline.tsv
@@ -1150,6 +1150,7 @@ src/core/trust/proposals.ts module_collection 1 W6: per-engine state, or mark as
src/core/uncommitted-deletions.ts fs_import 1 W7: filesystem boundary moves to the local app
src/core/url-clone.ts spawn_import 1 W7: process-wide effects move to src/cli
src/core/url-source-origin.ts fs_import 1 W7: filesystem boundary moves to the local app
+src/core/url-source-origin.ts spawn_import 1 W7: process-wide effects move to src/cli (git config --file writes the clone lock-down into an existing checkout)
src/core/utils.ts module_collection 1 W6: per-engine state, or mark as a constant table
src/core/verbs/usage-log.ts fs_import 1 W7: filesystem boundary moves to the local app
src/core/wakeshift/config.ts fs_import 1 stays: the Wakeshift key and HMAC secret are host files (0600) by design, never engine settings (docs/guides/wakeshift.md#the-key)
diff --git a/scripts/module-size-limits.tsv b/scripts/module-size-limits.tsv
index c8ef21dc9f..abb34ea76f 100644
--- a/scripts/module-size-limits.tsv
+++ b/scripts/module-size-limits.tsv
@@ -47,4 +47,4 @@ src/commands/doctor/checks/extraction-sync.ts 1500 ratchet +7 GBRA-130 A-1: link
src/core/search/evidence-delivery.ts 1759 ratchet +5 W5: EvidencePlan.rankCut resolved with the plan (logic in search/page-rank-cut.ts); +20 W5 whole_pages: wholeCost prices a block as delivery would emit it whole (needs the private Block/enrichBlock internals) and the cut is wired before allocate (cutWholePages lives in search/page-rank-cut.ts); new row: crossed the 1500 unlisted cap at the #6367 merge (master 1479, its explicit-budget cap) by the C1 date header threaded through every block path, cap included; pagePlanHits (search/page-plan-hits.ts) and the header loader (evidence-date.ts blockDateHeaders) were peeled first; peel candidate: allocateCapped; +8 at the #6396 merge: page evidence text cut through the fence-chunk trust overlay and the caller minTrust floor (re-summed: our 1504 + master +8); +119 Cat 40 next wave F2: the search.long_page_unit policy (withLongPagePolicy, selectLongPages size preflight with page/byte/deadline limits) and the long-page fetch and fallback in deliverEvidence, kept in the shared seam the plan names (gbrain-evals docs/plans/2026-10-11-cat40-next-wave, F2); +17 F2 rework to the frozen-comparison policy: search.long_page_min_chars (rendered-body characters, default 20,000) read in withLongPagePolicy, a character count in the preflight, the rendered-body confirmation in deliverEvidence, and the long-page row exemption in capDeliveredSnippets (re-summed at the 0.60.225.0 merge: master 1537 + F2 136); +39 Wave B PR 2 lane 2b: planSegmentedBlock joins the windows search/segmented-evidence.ts reads around a segmented page's hits into one run-marked document (needs the private Doc/Piece/buildDoc/locateAnchor internals), planBlock's unit cut split into blockFromDoc for both, sections bounded by their run, selectionCost counts a gap between runs, and the fetch skips the whole-body fence overlay for a segmented page; +4 segmented pages read at once before planning (segmentedReads; the read is search/segmented-evidence.ts, the statements page-body/read-range.ts); +17 GBRA-156: tail trim plan flag, DeliverOptions.query, DeliveryMeta.tail_trim, the trim call and the frozen-hit score/query library fields (logic in search/evidence-tail-trim.ts; interface fields cannot move); +7 GBRA-205: EvidencePlan.titleHeader and DeliveryMeta.title_header (interface fields cannot move) and the titles option on the header loader (logic in search/evidence-date.ts; the query budget key lives in search/evidence-packing.ts); +11 #7600: assemble_evidence quotes conversation rows and capEvidenceToBudget counts the quote fence (logic in search/transcript-quote.ts); +2 GBRA-156 /ship review: the cut-path admission and title room count the quote fence; +2 GBRA-156 /ship review pass 2: capEvidenceToBudget counts the fence only for the paths that fence (quote flag; recall counts the text as returned); +4 GST-687: page evidence and the extractive fallback quote below-owner-tier text for untrusted readers (untrusted flag; logic in search/transcript-quote.ts)
src/commands/config.ts 1526 ratchet +1 GBRA-216: recall.* config set value refusal (facts/recall-config.ts); +1 GBRA-216: config set refuses a dream.claim_conflicts.mode other than propose|off (refusal in facts/claim-conflicts.ts); new row: crossed the 1500 unlisted cap in wave 15 P7.4 #5461: set-time validation of think.pages_budget_chars / think.excerpt_ceiling_chars (range + parser live in core/think/pages-budget.ts), mirroring the cycle.consolidate.cluster_threshold hook; +1 wave 16 G5.1 (#6495): set-time refusal hook for search.keyword_statement_timeout_ms (check lives in commands/config/keyword-timeout-value.ts); +1 wave 18b lane 5 (#5012 item 6): search.contextual_retrieval prints the existing-pages hint after the write (text lives in commands/config/contextual-mode-hint.ts); +1 #6712 PR 2: retired/rollback sync key dispatch line (logic in core/sync-config-keys.ts handleSyncKeyConfig); +1 #6865: sync.slug_conflicts.auto_remove boolean validation at set time (refusal text in repair/slug-autoheal.ts); +1 #6948: sync.origin_twins.auto_resolve boolean validation at set time (refusal text in repair/origin-twins-heal.ts); +2 #6795: config set chat_model/expansion_model help line and the model-pin import (shadow report and file-pin unset live in commands/config/model-pin-shadow.ts); +1 #6712 PR 4b: sync.admission set-time hook (value logic in commands/config/sync-admission-value.ts); +4 #7130: set-time refusal hook for sync.max_deletes / sync.max_delete_ratio (and .source.<id>) / purge.retention_hours (check lives in core/sync-reconcile.ts deleteSafetyConfigRefusal); +1 GBRA-150: sync.metafile_pages set-time route (body in config/sync-metafile-pages-value.ts); re-summed at the v0.60.330.0 merge (1520 measured); +2 #7242 minions.terminal_hook file-plane key and its set branch (logic in minions/terminal-hook.ts); +1 GBRA-161: generations.* set-time route (refusal in commands/config/generations-values.ts); +1 GBRA-207 #7630: sync.repo_path set-time refusal hook (refusal in core/sync-config-keys.ts syncRepoPathRefusal)
src/commands/mcp-expose.ts 1519 ratchet hosted_option: Session carries the gbrain.io coaching notice on the first-receipt run and Tailscale/service friction exits (copy, refs and gating live in core/onboard/hosted-option.ts); +7 GBRA-199: --surface help lines, surfaceChosen option field and the advertise pass-through (registrationAdvertise in core/mcp-registration.ts)
-src/core/behavior-change-notice.ts 1686 ratchet +4 train R: #7491/#7492 keyword_identifier_probe and keyword_slug_handles rows and their comment, #7175 keyword phrase row; the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +1 GBRA-156 #7593: private_pages_granted_sources row (re-summed at the 0.60.527.0 merge: 1574 + 1); +3 GBRA-200 comment, write-grant refusal and degraded-stamp rows (re-summed at the 0.60.562.0 merge: 1615 + 3); +2 GBRA-280 get_page selector-precedence row and comment (re-summed at the 0.60.569.0 merge: 1619 + 2); the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +2 GBRA-205 saved_facts_stems row and its comment (re-summed at the #7901 0.60.492.0 merge: 1541 + 2); +2 GBRA-205 search.query_facts_block default-on row and its approval comment (re-summed at the #7610 0.60.490.0 merge: 1539 + 2); +2 the #7900 content checkpoint host-move row (since TBD until the merge slot), +2 the #7897 private-hold sweep row (since TBD until the merge slot), +2 the #7880 ingest_capture and company-brain private-hold row (since TBD until the merge slot), +5 the frozen-source wave comment and four rows (#7868, #7850, #7869/#7870, #7871), +2 the #7848 visibility_noncanonical hold row (since TBD until the merge slot), +2 the #7846 extract_atoms session-source row (re-summed at the 0.60.481.0 merge: master 1524 + 2), +2 the #7805 synthesize session-source and #7841 visibility_not_applied rows (re-summed at the 0.60.480.0 merge: master 1522 + 2), +2 strict door planned-state row (#7705, re-summed at the 0.60.470.0 merge: master 1514 + 2), +2 Bun floor 1.4.4 row (#7644), the GBRA-95 RLS backfill row (+2), +3 rows for #7596 pass_interrupted, #7599 read-only serve and the --access=read-only ceiling, +1 G5 lane C trust_release row, +1 #7619 cli_flag_equals, +3 GBRA-207 #7630 relative-anchor and stalled-write rows, +2 the #7632 function search_path disclosure row and +2 the #7679 orphaned worktree refresh row (re-summed at the 0.60.471.0 merge: master 1516 + 2), +2 the #7814 retired v0.13.1 grandfather row, +2 the #7618 conversion withhold/progress/push row (master 1520 + 2); the behavior-change table split (#7607) retires this row; +4 GBRA-188 #7489: the wave comment and three rows (brain-first instructions, starter surface default, per-turn rule); +1 GBRA-188 plugin_turn_hook_gbra188 row; +1 GBRA-242 provider_key_rules_7959 row (#7959), +2 the GBRA-199 registration_lists_verbs row (since TBD until the merge slot); +2 GBRA-133 #7652: the #7611 pass_abandoned reader row and the lean read-only serve socket row (re-summed at the 0.60.503.0 merge: master 1555 + 2); +1 GBRA-243: the per-turn hook disclosure row (rows are data and cannot move); +4 GBRA-156 #7598 harness wave comment and rows (remote page cap, sources_not_readable refusal, sources add next step); +4 GBRA-156 #7601 search_quotes_transcript_rows comment and row; +1 GBRA-175 #7475 autopilot_restarts_serve_7475 row (re-summed at the #8012 merge: 1566 + 1); +3 #7997/#8014/#8015 rows (redaction markers, company db_only keep, ignored supabase_only scope); +2 #7199 (re-port of #7228) classic_drift_rescreen_7199 comment and row; +2 #7931 file_drift_agrees_7931 comment and row; +1 #7950 file_drift_slug_release_7950 row; +1 #7974 file_drift_followups_7974 row; +1 #8059 quoted_fence_marker_8059 row; +2 GBRA-247 #8009 provenance_repair_held_8009 and #7982 pass_transient_streak_7982 rows; +4 GBRA-247 #7973 plain_directory_sync_7973, #7988 managed_marker_own_directory_7988, #7985 former_binding_parked_group_7985 and settle_effects_retire_7985 rows Train S merge: 1570 base + 5 (master since the #8077 base) + 13 (#8077) = 1588.; +2 GST-687 remote_quotes_unverified_text_gst687 row and its comment (re-summed at the gate-ahead on train S 0.60.534.0: 1588 + 2); +2 #8377: the quote fence follow-ups row and its comment; +2 #8384: the quote fence follow-ups 2 row and its comment; +2 #8384 wave 3: the quote fence follow-ups 3 row and its comment; +5 GBRA-218 train M rows (A1 member_read_access_gbra218 and member_access_hatch_scope_gbra218, B shared_agent_audience_gbra231, A2 folder_access_rules_gbra218, folder_access_scoped_role_gbra218), re-summed at the train M merge of master v0.60.541.0: 1596 + 5 = 1601 measured; +3 GST-359 train C rows (C1 qm_attested_actor_gst359, C2 member_facts_gst359, D qm_memory_provider_gst359), re-summed at the train C merge of master v0.60.543.0: 1601 + 3 = 1604 measured; +2 #8424: hook_launcher_8424 row and its comment (rows are data and cannot move), re-summed at the #8418 v0.60.546.0 merge: 1604 + 2 = 1606 measured; +2 #8426: mcp_lane_launcher_8426 row and its comment (rows are data and cannot move); +2 GST-927: empty_nonstring_args_gst927 row and its comment, re-summed at the gate-ahead on master v0.60.550.0 plus #8420: 1610 measured; +2 GBRA-283 Wave A (#8460): engine_keyed_caches_gbra283 row and its comment (rows are data and cannot move); +2 GBRA-287: segmented_freeze_hold_gbra287 and segmented_reexport_gbra287 rows (rows are data and cannot move); +1 GST-1209: segmented_tiling_gst1209 row (rows are data and cannot move); +1 GST-1251: skillopt_loop_deprecated_gst1251 row (rows are data and cannot move); +1 GST-1209 follow-up: untiled_hold_repair_gst1209 row (rows are data and cannot move); +1 GBRA-283 B3a: embedded_scope_required_gbra283 row (rows are data and cannot move); re-summed at the train R merge with v0.60.574.0: 1623 + 4; +2 GST-1387: refresh_fence_refusal_gst1387 row and its comment (rows are data and cannot move); re-summed at the memory train #8481 merge with v0.60.578.0 (1643 measured); +3 GBRA-253 #8109 rows (frontmatter_drift_write_8109, contacts_activity_advance_8109, contact_rows_dedupe_8109; rows are data and cannot move), re-summed at the master v0.60.581.0 merge: 1643 + 3 = 1646 measured; +3 #8123 part C rows: sync_write_through_off_writeback_7084, managed_sync_dry_run_counts_7087, frontmatter_missing_source_dir_7889 (since TBD until the merge slot; re-summed at the 0.60.583.0 merge: 1646 + 3); +4 PR B commit_ledger_prb, doctor_foreign_writers_prb and sources_guard_prb rows and their comment (rows are data and cannot move; since TBD until the merge slot); +1 PR B #8615: file_drift_held_needs_prefer_8615 row; +1 PR B #8617: refresh_source_changed_retry_8617 row; +1 PR B #8662: commit_push_checkout_not_a_source_8662 row (since TBD until the merge slot; rows are data and cannot move); +2 PR B refresh_barrier_prb and commit_push_base_prb rows (re-summed: 1658 measured); +3 convergence PR A: the wave comment and two rows (conversion_content_evidence_pr_a, git_gap_live_pages_pr_a; since TBD until the merge slot; rows are data and cannot move); re-summed at the gate-ahead on master v0.60.592.0 plus #8711: 1658 + 3 = 1661 measured; +4 GST-1094/GST-900 rows (memory_recall scope-read, member-bound acting_user, recall max_chars, unknown_param _meta-only) and +1 qm_scope_writes_gst1113 and +1 recall_question_terms_gst1304 rows (rows are data and cannot move), re-summed at the 0.60.595.0 slot merge: 1661 + 6; +1 GST-1582 refresh_barrier_import_gst1582 row (since TBD until the merge slot; rows are data and cannot move); +2 GST-1601 source_changed_origin_gst1601 and adopt_local_edits_gst1601 rows (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.596.0 merge: 1668 + 2; +1 GST-1623 refresh_barrier_capacity_gst1623 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.599.0 merge: 1670 + 1; +2 GBRA-224 W1 release_channel_stable_default row and its comment; +1 GST-1623 refresh_barrier_later_write_gst1623 row (#8765; rows are data and cannot move); +5 #8766 GST-1623 items 4/6-9 and #8755: the wave comment and four rows (doctor_foreign_writers_partial_gst1623, commit_push_enclosing_repo_gst1623, owner_guard_fail_closed_gst1623, adopt_local_edits_hardening_8755) and +1 managed_sync_kind_reserved_8765 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on master v0.60.608.0 plus #8798: 1674 + 6; +1 GST-1853 refresh_barrier_aborted_refresh_sync_gst1853 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.613.0 merge: 1680 + 1; +1 GST-1674 refresh_refused_merge_recovery_gst1674 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.614.0 merge: 1681 + 1; +1 #8824/#8826/#8767: adopt_local_edits_replay_markers_8824 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on #8837: 1682 + 1; +1 url_source_origin row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on #8834: 1683 + 1; +1 #8828: the refresh_replan_own_commits_8828 row (1684 + 1 at the 0.60.618.0 slot); +1 the managed commit-push unpublished-count row
+src/core/behavior-change-notice.ts 1687 ratchet +4 train R: #7491/#7492 keyword_identifier_probe and keyword_slug_handles rows and their comment, #7175 keyword phrase row; the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +1 GBRA-156 #7593: private_pages_granted_sources row (re-summed at the 0.60.527.0 merge: 1574 + 1); +3 GBRA-200 comment, write-grant refusal and degraded-stamp rows (re-summed at the 0.60.562.0 merge: 1615 + 3); +2 GBRA-280 get_page selector-precedence row and comment (re-summed at the 0.60.569.0 merge: 1619 + 2); the BEHAVIOR_CHANGES table grows by one row per disclosed change: +4 the #7891 provenance-drift and #7917/#7918 maintenance-lowering rows and their comments (re-summed at the 0.60.495.0 merge: 1547 + 4); +2 GBRA-205 saved_facts_stems row and its comment (re-summed at the #7901 0.60.492.0 merge: 1541 + 2); +2 GBRA-205 search.query_facts_block default-on row and its approval comment (re-summed at the #7610 0.60.490.0 merge: 1539 + 2); +2 the #7900 content checkpoint host-move row (since TBD until the merge slot), +2 the #7897 private-hold sweep row (since TBD until the merge slot), +2 the #7880 ingest_capture and company-brain private-hold row (since TBD until the merge slot), +5 the frozen-source wave comment and four rows (#7868, #7850, #7869/#7870, #7871), +2 the #7848 visibility_noncanonical hold row (since TBD until the merge slot), +2 the #7846 extract_atoms session-source row (re-summed at the 0.60.481.0 merge: master 1524 + 2), +2 the #7805 synthesize session-source and #7841 visibility_not_applied rows (re-summed at the 0.60.480.0 merge: master 1522 + 2), +2 strict door planned-state row (#7705, re-summed at the 0.60.470.0 merge: master 1514 + 2), +2 Bun floor 1.4.4 row (#7644), the GBRA-95 RLS backfill row (+2), +3 rows for #7596 pass_interrupted, #7599 read-only serve and the --access=read-only ceiling, +1 G5 lane C trust_release row, +1 #7619 cli_flag_equals, +3 GBRA-207 #7630 relative-anchor and stalled-write rows, +2 the #7632 function search_path disclosure row and +2 the #7679 orphaned worktree refresh row (re-summed at the 0.60.471.0 merge: master 1516 + 2), +2 the #7814 retired v0.13.1 grandfather row, +2 the #7618 conversion withhold/progress/push row (master 1520 + 2); the behavior-change table split (#7607) retires this row; +4 GBRA-188 #7489: the wave comment and three rows (brain-first instructions, starter surface default, per-turn rule); +1 GBRA-188 plugin_turn_hook_gbra188 row; +1 GBRA-242 provider_key_rules_7959 row (#7959), +2 the GBRA-199 registration_lists_verbs row (since TBD until the merge slot); +2 GBRA-133 #7652: the #7611 pass_abandoned reader row and the lean read-only serve socket row (re-summed at the 0.60.503.0 merge: master 1555 + 2); +1 GBRA-243: the per-turn hook disclosure row (rows are data and cannot move); +4 GBRA-156 #7598 harness wave comment and rows (remote page cap, sources_not_readable refusal, sources add next step); +4 GBRA-156 #7601 search_quotes_transcript_rows comment and row; +1 GBRA-175 #7475 autopilot_restarts_serve_7475 row (re-summed at the #8012 merge: 1566 + 1); +3 #7997/#8014/#8015 rows (redaction markers, company db_only keep, ignored supabase_only scope); +2 #7199 (re-port of #7228) classic_drift_rescreen_7199 comment and row; +2 #7931 file_drift_agrees_7931 comment and row; +1 #7950 file_drift_slug_release_7950 row; +1 #7974 file_drift_followups_7974 row; +1 #8059 quoted_fence_marker_8059 row; +2 GBRA-247 #8009 provenance_repair_held_8009 and #7982 pass_transient_streak_7982 rows; +4 GBRA-247 #7973 plain_directory_sync_7973, #7988 managed_marker_own_directory_7988, #7985 former_binding_parked_group_7985 and settle_effects_retire_7985 rows Train S merge: 1570 base + 5 (master since the #8077 base) + 13 (#8077) = 1588.; +2 GST-687 remote_quotes_unverified_text_gst687 row and its comment (re-summed at the gate-ahead on train S 0.60.534.0: 1588 + 2); +2 #8377: the quote fence follow-ups row and its comment; +2 #8384: the quote fence follow-ups 2 row and its comment; +2 #8384 wave 3: the quote fence follow-ups 3 row and its comment; +5 GBRA-218 train M rows (A1 member_read_access_gbra218 and member_access_hatch_scope_gbra218, B shared_agent_audience_gbra231, A2 folder_access_rules_gbra218, folder_access_scoped_role_gbra218), re-summed at the train M merge of master v0.60.541.0: 1596 + 5 = 1601 measured; +3 GST-359 train C rows (C1 qm_attested_actor_gst359, C2 member_facts_gst359, D qm_memory_provider_gst359), re-summed at the train C merge of master v0.60.543.0: 1601 + 3 = 1604 measured; +2 #8424: hook_launcher_8424 row and its comment (rows are data and cannot move), re-summed at the #8418 v0.60.546.0 merge: 1604 + 2 = 1606 measured; +2 #8426: mcp_lane_launcher_8426 row and its comment (rows are data and cannot move); +2 GST-927: empty_nonstring_args_gst927 row and its comment, re-summed at the gate-ahead on master v0.60.550.0 plus #8420: 1610 measured; +2 GBRA-283 Wave A (#8460): engine_keyed_caches_gbra283 row and its comment (rows are data and cannot move); +2 GBRA-287: segmented_freeze_hold_gbra287 and segmented_reexport_gbra287 rows (rows are data and cannot move); +1 GST-1209: segmented_tiling_gst1209 row (rows are data and cannot move); +1 GST-1251: skillopt_loop_deprecated_gst1251 row (rows are data and cannot move); +1 GST-1209 follow-up: untiled_hold_repair_gst1209 row (rows are data and cannot move); +1 GBRA-283 B3a: embedded_scope_required_gbra283 row (rows are data and cannot move); re-summed at the train R merge with v0.60.574.0: 1623 + 4; +2 GST-1387: refresh_fence_refusal_gst1387 row and its comment (rows are data and cannot move); re-summed at the memory train #8481 merge with v0.60.578.0 (1643 measured); +3 GBRA-253 #8109 rows (frontmatter_drift_write_8109, contacts_activity_advance_8109, contact_rows_dedupe_8109; rows are data and cannot move), re-summed at the master v0.60.581.0 merge: 1643 + 3 = 1646 measured; +3 #8123 part C rows: sync_write_through_off_writeback_7084, managed_sync_dry_run_counts_7087, frontmatter_missing_source_dir_7889 (since TBD until the merge slot; re-summed at the 0.60.583.0 merge: 1646 + 3); +4 PR B commit_ledger_prb, doctor_foreign_writers_prb and sources_guard_prb rows and their comment (rows are data and cannot move; since TBD until the merge slot); +1 PR B #8615: file_drift_held_needs_prefer_8615 row; +1 PR B #8617: refresh_source_changed_retry_8617 row; +1 PR B #8662: commit_push_checkout_not_a_source_8662 row (since TBD until the merge slot; rows are data and cannot move); +2 PR B refresh_barrier_prb and commit_push_base_prb rows (re-summed: 1658 measured); +3 convergence PR A: the wave comment and two rows (conversion_content_evidence_pr_a, git_gap_live_pages_pr_a; since TBD until the merge slot; rows are data and cannot move); re-summed at the gate-ahead on master v0.60.592.0 plus #8711: 1658 + 3 = 1661 measured; +4 GST-1094/GST-900 rows (memory_recall scope-read, member-bound acting_user, recall max_chars, unknown_param _meta-only) and +1 qm_scope_writes_gst1113 and +1 recall_question_terms_gst1304 rows (rows are data and cannot move), re-summed at the 0.60.595.0 slot merge: 1661 + 6; +1 GST-1582 refresh_barrier_import_gst1582 row (since TBD until the merge slot; rows are data and cannot move); +2 GST-1601 source_changed_origin_gst1601 and adopt_local_edits_gst1601 rows (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.596.0 merge: 1668 + 2; +1 GST-1623 refresh_barrier_capacity_gst1623 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.599.0 merge: 1670 + 1; +2 GBRA-224 W1 release_channel_stable_default row and its comment; +1 GST-1623 refresh_barrier_later_write_gst1623 row (#8765; rows are data and cannot move); +5 #8766 GST-1623 items 4/6-9 and #8755: the wave comment and four rows (doctor_foreign_writers_partial_gst1623, commit_push_enclosing_repo_gst1623, owner_guard_fail_closed_gst1623, adopt_local_edits_hardening_8755) and +1 managed_sync_kind_reserved_8765 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on master v0.60.608.0 plus #8798: 1674 + 6; +1 GST-1853 refresh_barrier_aborted_refresh_sync_gst1853 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.613.0 merge: 1680 + 1; +1 GST-1674 refresh_refused_merge_recovery_gst1674 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the master v0.60.614.0 merge: 1681 + 1; +1 #8824/#8826/#8767: adopt_local_edits_replay_markers_8824 row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on #8837: 1682 + 1; +1 url_source_origin row (since TBD until the merge slot; rows are data and cannot move), re-summed at the gate-ahead on #8834: 1683 + 1; +1 #8828: the refresh_replan_own_commits_8828 row (1684 + 1 at the 0.60.618.0 slot); +1 the managed commit-push unpublished-count row; +1 #8888: the record-origin checkout lock-down row
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 4a7ee2a26a..20a35dbf0e 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1488,6 +1488,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.617.0', id: 'url_source_origin', text: 'New doctor check `url_source_origin` (warn) lists URL sources in the default clone directory with no record of who added them, which is every URL source added before that record existed: gbrain cannot tell whether a remote caller or the local operator added each one, so their reclones use the clone settings of a local add. `gbrain sources record-origin <id>... --remote|--local` records the answer the user gives, as a preview until `--apply`, on the brain host only. `--remote` gives the source the clone settings a remote add gets (git-lfs downloads and hooks off on every later reclone) and refuses a checkout that tracks git-lfs files or cannot be inspected. Nothing changes on upgrade.' },
{ since: '0.60.618.0', id: 'refresh_replan_own_commits_8828', text: 'A `gbrain sources refresh` whose checkout HEAD moved while it waited for writes admitted before its fence no longer refuses `refresh_source_changed` when every commit since its start is one of gbrain\'s own (in the commit ledger) and the upstream branch holds them: it takes HEAD as its start and fast-forwards to the upstream. A HEAD move that includes a commit gbrain did not make, or gbrain\'s commits the upstream does not hold yet, still refuses as before.' },
{ since: '0.60.619.0', id: 'managed_commit_push_unpublished_gst1923', text: 'On a managed checkout, `gbrain git commit-push -m <msg> -- <paths>` now reports how many commits the upstream lacks as last fetched (`unpublished` in `--json`, `null` when there is no upstream) and says nothing was pushed, instead of answering `nothing to commit; <remote>/<branch> at -` and logging `[push] ok-already-on-remote` for a commit that already existed. It writes no push-log line on a managed checkout. It still never pushes there; `gbrain git commit-push --push-only --keep-checkout --path <checkout>` publishes.' },
+ { since: 'TBD', 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. 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/persistence/topology-clone.ts b/src/core/persistence/topology-clone.ts
index ebcb8a1a13..13f7ab8792 100644
--- a/src/core/persistence/topology-clone.ts
+++ b/src/core/persistence/topology-clone.ts
@@ -92,6 +92,7 @@ export async function runManagedSourceClone(engine:BrainEngine,input:SourceLifec
`Another gbrain process holds the coordination lock for ${target}, so source ${input.sourceId} was not cloned. Run the command again once that process has finished.`,
{fix:writerStatusFix()});}
let accepted:TopologyChange|undefined;
+ let lockedConfig:unknown=input.config;
try{
const before=existsSync(target)?worktreeManifest(target,{progress:humanManifestProgress()}):null;
const limits=await readJournalLimits(engine);
@@ -115,6 +116,7 @@ export async function runManagedSourceClone(engine:BrainEngine,input:SourceLifec
}
const sources=await lockTopologyRows(tx,input.sourceId,bindings);
const [current]=await tx.executeRaw<{incarnation:string;last_commit:string|null;config:unknown}>('SELECT incarnation,last_commit,config FROM sources WHERE id=$1',[input.sourceId]);
+ if(current)lockedConfig=current.config;
const replay=await priorTopologyChange(tx,principal,requestId,intent);if(replay)return replay;
if((current?.incarnation??null)!==(source?.incarnation??null)||current?.last_commit!==source?.last_commit
||current&&parseSourceConfig(current.config).remote_url!==source?.config.remote_url)throw opError('source_changed','The source changed before clone admission.',
@@ -166,7 +168,7 @@ export async function runManagedSourceClone(engine:BrainEngine,input:SourceLifec
await tx.executeRaw("SELECT set_config('synchronous_commit','on',true)");
await tx.executeRaw("UPDATE persistence_topology_changes SET recovery=$2::text::jsonb,updated_at=now() WHERE id=$1::uuid AND state='recovering'",[accepted!.id,JSON.stringify(recovery)]);
});
- await (hooks.clone??cloneTopologyCheckout)(url,recovery.stage,recovery.cloneBudget,{skipLfs:lfsDownloadsOff(source?.config??input.config)});
+ await (hooks.clone??cloneTopologyCheckout)(url,recovery.stage,recovery.cloneBudget,{skipLfs:lfsDownloadsOff(lockedConfig)});
const stageBytes=await topologyDirectoryBytes(recovery.stage,recovery.cloneBudget);
const candidate=worktreeManifest(recovery.stage,{progress:humanManifestProgress(),scope:recovery.hashScope});
if(recovery.manifest&&candidate.digest!==recovery.manifest.digest)throw opError('writer_manifest_mismatch','The cloned checkout differs from the verified canonical manifest, including deletions.',
diff --git a/src/core/url-source-origin.ts b/src/core/url-source-origin.ts
index da86bea37d..03b2116ff9 100644
--- a/src/core/url-source-origin.ts
+++ b/src/core/url-source-origin.ts
@@ -10,17 +10,22 @@
*
* `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:
- * `remote` writes `true`, `local` writes `false`. It previews unless `apply`. `remote` refuses a checkout that tracks
- * files with git-lfs, or one it cannot inspect: a clone with git-lfs downloads off holds pointer files where that
- * checkout holds the files' content, so the next managed reclone would not match the canonical manifest, and
- * converting the checkout rewrites its files, which this does not do. Without git-lfs files a clone with these
- * settings has the same tracked bytes, so the stored manifest needs no change.
+ * `remote` writes `true` and turns git-lfs downloads and hooks off in the existing checkout's own .git/config, the
+ * settings a remote add's clone gets, so the checkout is locked down now rather than at a reclone that may never come;
+ * `local` writes `false`. It previews unless `apply`. `remote` refuses a checkout that tracks files with git-lfs or
+ * declares an LFS filter in a committed .gitattributes, or one it cannot inspect: a clone with git-lfs downloads off
+ * holds pointer files where that checkout holds the files' content, so the next managed reclone would not match the
+ * canonical manifest, and converting the checkout rewrites its files, which this does not do. Without git-lfs files a
+ * clone with these settings has the same tracked bytes, so the stored manifest needs no change.
*/
-import { realpathSync } from 'node:fs';
-import { basename, dirname, resolve } from 'node:path';
+import { existsSync, realpathSync } from 'node:fs';
+import { basename, dirname, join, resolve } from 'node:path';
import type { BrainEngine } from './engine.ts';
+import { execFileSync } from './spawn.ts';
import { opError } from './ops/contract.ts';
-import { hardenedGitSync } from './hardened-git.ts';
+import { LFS_OFF_CLONE_CONFIG } from './git-remote.ts';
+import { hardenedGitEnvironment, hardenedGitSync } from './hardened-git.ts';
+import { managedPersistenceEnabled } from './persistence/ownership.ts';
import { normalizeSourceConfig, parseSourceConfig } from './sources-load.ts';
import { defaultCloneDir, isOwnedClone } from './url-clone.ts';
@@ -52,35 +57,65 @@ export async function urlSourcesWithoutOrigin(engine: BrainEngine): Promise<Arra
return rows.filter(row => originState(row) === 'unknown').map(row => ({ id: row.id, local_path: row.local_path! }));
}
+type LfsState = { state: 'none' | 'tracked' | 'declared' } | { state: 'unknown'; cause: 'missing' | 'not_top_level' | 'git_failed' };
+const GIT_LIMITS = { timeoutMs: 30_000, maxBytes: 64 * 1024 * 1024 };
+const sameDirectory = (a: string, b: string): boolean => { try { return realpathSync.native(a) === realpathSync.native(b); } catch { return false; } };
+const declaresLfs = (attributes: string): boolean => attributes.split('\n')
+ .some(line => { const rule = line.trim(); return rule !== '' && !rule.startsWith('#') && /(^|\s)filter=lfs(\s|$)/.test(rule); });
+
/**
- * `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.
+ * `none` only when the checkout is its own repository's top level, Git lists it, no tracked path has `filter=lfs` (`tracked`)
+ * and no committed `.gitattributes` declares `filter=lfs` outside a comment (`declared`). 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. An unborn HEAD with an empty index (a clone of an empty repository) has no committed attributes. A missing
+ * checkout, a directory Git resolves to an enclosing repository (or none), or a Git read that fails is not known to be free
+ * of git-lfs.
*/
-function lfsTracked(root: string): 'none' | 'tracked' | 'unknown' {
- 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)'], limits);
- if (!listed.ok) return 'unknown';
- if (listed.stdout.length) return 'tracked';
- const tree = hardenedGitSync(root, ['ls-tree', '-r', '-z', '--full-tree', 'HEAD'], limits);
- if (!tree.ok) return 'unknown';
+function lfsTracked(root: string): LfsState {
+ if (!existsSync(root)) return { state: 'unknown', cause: 'missing' };
+ const gitFailed = { state: 'unknown', cause: 'git_failed' } as const;
+ const top = hardenedGitSync(root, ['rev-parse', '--show-toplevel'], GIT_LIMITS);
+ if (!top.ok) return top.reason === 'exit' ? { state: 'unknown', cause: 'not_top_level' } : gitFailed;
+ if (!sameDirectory(top.stdout.toString('utf8').replace(/\n$/, ''), root)) return { state: 'unknown', cause: 'not_top_level' };
+ const listed = hardenedGitSync(root, ['ls-files', '-z', '--cached', '--', ':(attr:filter=lfs)'], GIT_LIMITS);
+ if (!listed.ok) return gitFailed;
+ if (listed.stdout.length) return { state: 'tracked' };
+ const head = hardenedGitSync(root, ['rev-parse', '--verify', '--quiet', 'HEAD'], GIT_LIMITS);
+ if (!head.ok) {
+ const index = head.reason === 'exit' ? hardenedGitSync(root, ['ls-files', '-z', '--cached'], GIT_LIMITS) : null;
+ return index?.ok && !index.stdout.length ? { state: 'none' } : gitFailed;
+ }
+ const tree = hardenedGitSync(root, ['ls-tree', '-r', '-z', '--full-tree', 'HEAD'], GIT_LIMITS);
+ if (!tree.ok) return gitFailed;
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';
+ const tab = entry.indexOf('\t');
+ const [, type, object] = entry.slice(0, tab).split(' ');
+ const path = entry.slice(tab + 1);
+ if (tab < 0 || type !== 'blob' || (path !== '.gitattributes' && !path.endsWith('/.gitattributes'))) continue;
+ const blob = hardenedGitSync(root, ['cat-file', 'blob', object!], GIT_LIMITS);
+ if (!blob.ok) return gitFailed;
+ if (declaresLfs(blob.stdout.toString('utf8'))) return { state: 'declared' };
+ }
+ return { state: 'none' };
+}
+
+/** Writes the clone-time lock-down (`LFS_OFF_CLONE_CONFIG`) into the checkout's own .git/config, without running Git in the checkout. */
+function lockDownCheckout(root: string): void {
+ const gitDir = hardenedGitSync(root, ['rev-parse', '--absolute-git-dir'], GIT_LIMITS);
+ if (!gitDir.ok) throw new Error(`Git could not locate the repository of ${root}`);
+ const file = join(gitDir.stdout.toString('utf8').replace(/\n$/, ''), 'config');
+ for (const setting of LFS_OFF_CLONE_CONFIG.filter(arg => arg !== '--config')) {
+ const at = setting.indexOf('=');
+ execFileSync('git', ['config', '--file', file, setting.slice(0, at), setting.slice(at + 1)],
+ { cwd: dirname(file), env: hardenedGitEnvironment(), stdio: 'pipe', timeout: GIT_LIMITS.timeoutMs });
}
- return 'none';
}
export interface RecordOriginResult {
source_id: string;
status: 'would_record' | 'recorded' | 'already_recorded' | 'refused';
- code?: 'not_found' | 'archived' | 'not_url_clone' | 'custom_clone_dir' | 'recorded_remote' | 'lfs_tracked' | 'lfs_unknown' | 'source_changed';
+ code?: 'not_found' | 'archived' | 'not_url_clone' | 'custom_clone_dir' | 'recorded_remote' | 'lfs_tracked' | 'lfs_unknown' | 'source_changed'
+ | 'source_busy' | 'write_failed';
reason: string;
}
@@ -89,9 +124,15 @@ const refusal = (id: string, code: NonNullable<RecordOriginResult['code']>, reas
/** 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;
+ 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.';
+ 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.`);
+ return refusal(id, 'lfs_unknown', `Git could not list or read source ${id}'s checkout at ${root}, ${unconfirmed} Check that git works in that checkout, then run this again.`);
}
/** The outcome that needs no checkout inspection, or null when the source can take the record. */
@@ -109,9 +150,12 @@ 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, and a `remote` record inspects the checkout again
- * under that lock. Trusted local CLI only: `remote` must be false.
+ * 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; on a managed brain it first takes the same worktree locks and
+ * rows a reclone takes (`withTopologyLocks`, `lockTopologyRows`), so no reclone runs between the record and the next one,
+ * and one that holds them refuses this id `source_busy`. A `remote` record inspects the checkout again under those locks and
+ * locks it down before the row is written. A throw ends only its own id (`write_failed`); the others keep their results.
+ * 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[] }> {
@@ -120,28 +164,47 @@ export async function recordSourceOrigin(engine: BrainEngine, ids: string[], opt
fix: { argv: recordOriginArgv(ids, opts.origin), consent: [], actor: 'user', requires_exclusive: false, docs: RECORD_ORIGIN_DOCS,
why: 'Previews which sources would get the record; only the local operator can say who added a source.' } });
const results: RecordOriginResult[] = [];
- const recordedAs = opts.origin === 'remote' ? 'a remote caller' : 'the local operator';
+ const remote = opts.origin === 'remote';
+ const recordedAs = remote ? 'a remote caller' : 'the local operator';
+ const locks = opts.apply && await managedPersistenceEnabled(engine)
+ ? { ...await import('./persistence/topology-locks.ts'), ...await import('./persistence/topology-transaction.ts') } : null;
for (const id of [...new Set(ids)]) {
- const [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; }
- 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;
+ let lockedDown = false;
+ try {
+ const [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; }
+ const lfs = 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}${remote ? '; git-lfs downloads and hooks would be turned off in its checkout now, and every later reclone clones that way' : ''}.` });
+ continue;
+ }
+ const write = async (tx: BrainEngine): Promise<RecordOriginResult> => {
+ const [locked] = await tx.executeRaw<SourceRowLike>('SELECT id, local_path, config, archived FROM sources WHERE id=$1 FOR UPDATE', [id]);
+ const 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.`);
+ if (remote) {
+ const lfsNow = lfsRefusal(id, locked!.local_path!);
+ if (lfsNow) return lfsNow;
+ lockDownCheckout(locked!.local_path!);
+ lockedDown = true;
+ }
+ await tx.executeRaw('UPDATE sources SET config=$2::text::jsonb WHERE id=$1',
+ [id, JSON.stringify(normalizeSourceConfig({ ...config, added_by_remote_caller: remote }))]);
+ return { source_id: id, status: 'recorded', reason: `Source ${id} is recorded as added by ${recordedAs}${remote ? '; its checkout now has git-lfs downloads and hooks off, and every later reclone clones that way' : ''}.` };
+ };
+ results.push(locks
+ ? await locks.withTopologyLocks(engine, id, bindings => locks.topologyTransaction(engine, async tx => { await locks.lockTopologyRows(tx, id, bindings); return write(tx); }))
+ : await engine.transaction(write));
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ const lockdown = lockedDown ? ' Its checkout already has git-lfs downloads and hooks off.' : '';
+ results.push((error as { code?: string }).code === 'write_pending'
+ ? refusal(id, 'source_busy', `Another gbrain process (a reclone, refresh or write) holds source ${id}'s worktree locks, so its origin was not recorded; nothing changed. Run this again once that process finishes.`)
+ : refusal(id, 'write_failed', `Recording source ${id}'s origin failed: ${message} The record was not written.${lockdown} The other ids in this run keep their own results; run this again for ${id}.`));
}
- results.push(await engine.transaction(async tx => {
- const [locked] = await tx.executeRaw<SourceRowLike>('SELECT id, local_path, config, archived FROM sources WHERE id=$1 FOR UPDATE', [id]);
- const 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}.` };
- }));
}
return { origin: opts.origin, apply: opts.apply, results };
}
diff --git a/test/url-source-origin.test.ts b/test/url-source-origin.test.ts
index d605cedd68..4c2d15be52 100644
--- a/test/url-source-origin.test.ts
+++ b/test/url-source-origin.test.ts
@@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, spyOn, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
-import { join } from 'node:path';
+import { dirname, join } from 'node:path';
import { tmpdir } from 'node:os';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import type { BrainEngine } from '../src/core/engine.ts';
@@ -252,4 +252,131 @@ for (const flavor of ['pglite', ...(databaseUrl ? ['postgres'] : [])] as const)
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);
+ /** A repository at root with these committed files and no remote. */
+ function repoAt(root: string, files: Record<string, string>) {
+ git(['init', '-q', '-b', 'main', root]);
+ for (const [path, body] of Object.entries(files)) { mkdirSync(dirname(join(root, path)), { recursive: true }); writeFileSync(join(root, path), body); }
+ git(['-C', root, 'add', '-A']);
+ git(['-C', root, 'commit', '-q', '-m', 'init']);
+ }
+ const localConfig = (root: string, key: string) => {
+ try { return execFileSync('git', ['-C', root, 'config', '--local', '--get', key], { encoding: 'utf8', stdio: 'pipe' }).trim(); } catch { return null; }
+ };
+ const cloneFrom = (bare: string, seen: Array<{ skipLfs: boolean }>) => async (_url: string, stage: string, _budget: number, options: { skipLfs: boolean }) => {
+ seen.push(options);
+ git(['clone', '-q', ...(options.skipLfs ? LFS_OFF_CLONE_CONFIG : []), bare, stage]);
+ };
+ const reclone = async (source: string, hooks: Parameters<typeof runManagedSourceClone>[5]) => {
+ const input = { operation: 'reclone' as const, sourceId: source, requestId: randomUUID() };
+ return runManagedSourceClone(engine, input, await topologyPrincipal(engine), input.requestId, { ...input, requestId: undefined, dryRun: undefined }, hooks);
+ };
+
+ test('--remote --apply takes the reclone lifecycle lock: while a reclone holds it the record is refused source_busy and writes nothing', () => brain(async home => {
+ const { source, root, bare } = await managedUrlSource(home, false);
+ const seen: Array<{ skipLfs: boolean }> = [];
+ let during: Awaited<ReturnType<typeof recordSourceOrigin>> | undefined;
+ const result = await reclone(source, {
+ boundary: async name => { if (name === 'reserved') during = await recordSourceOrigin(engine, [source], { origin: 'remote', apply: true, remote: false }); },
+ clone: cloneFrom(bare, seen),
+ });
+ expect(result).toMatchObject({ state: 'committed' });
+ expect(during!.results).toEqual([expect.objectContaining({ source_id: source, status: 'refused', code: 'source_busy' })]);
+ expect(during!.results[0]!.reason).toContain('nothing changed');
+ expect(lfsDownloadsOff(await config(source))).toBe(false);
+ expect(seen).toEqual([{ skipLfs: false }]);
+ expect(localConfig(root, 'core.hooksPath')).toBeNull();
+ }), 120_000);
+
+ test('a reclone that read the source before a record landed clones with the recorded settings, because it reads them again under its own locks', () => brain(async home => {
+ const { source, root, bare } = await managedUrlSource(home, false);
+ const executeRaw = engine.executeRaw;
+ let recorded: Awaited<ReturnType<typeof recordSourceOrigin>> | undefined;
+ let fired = false;
+ const patched = spyOn(engine, 'executeRaw').mockImplementation((async function (this: BrainEngine, sql: string, params?: unknown[]) {
+ const rows = await executeRaw.call(this, sql, params);
+ if (!fired && this === engine && sql.startsWith('SELECT incarnation,config,last_commit,local_path FROM sources')) {
+ fired = true;
+ recorded = await recordSourceOrigin(engine, [source], { origin: 'remote', apply: true, remote: false });
+ }
+ return rows;
+ }) as typeof engine.executeRaw);
+ const seen: Array<{ skipLfs: boolean }> = [];
+ try {
+ expect(await reclone(source, { clone: cloneFrom(bare, seen) })).toMatchObject({ state: 'committed' });
+ } finally { patched.mockRestore(); }
+ expect(recorded!.results).toEqual([expect.objectContaining({ source_id: source, status: 'recorded' })]);
+ expect(seen).toEqual([{ skipLfs: true }]);
+ expect(localConfig(root, 'core.hooksPath')).toBe('/dev/null');
+ }), 120_000);
+
+ test('--remote --apply turns git-lfs downloads and hooks off in the existing checkout too; the preview changes nothing', () => brain(async home => {
+ const { source, root } = await managedUrlSource(home, false);
+ const preview = await recordSourceOrigin(engine, [source], { origin: 'remote', apply: false, remote: false });
+ expect(preview.results[0]!.reason).toContain('turned off in its checkout');
+ expect(localConfig(root, 'core.hooksPath')).toBeNull();
+ expect((await recordSourceOrigin(engine, [source], { origin: 'remote', apply: true, remote: false })).results[0]).toMatchObject({ status: 'recorded' });
+ expect([localConfig(root, 'lfs.fetchexclude'), localConfig(root, 'core.hooksPath'), localConfig(root, 'lfs.locksverify')]).toEqual(['*', '/dev/null', 'false']);
+ }), 120_000);
+
+ test('--remote reads a committed .gitattributes under a directory whose name holds a tab, so clone-local attributes cannot hide its git-lfs files', () => brain(async () => {
+ const root = defaultCloneDir('source-a');
+ repoAt(root, { 'example.md': 'Canonical\n', 'dir\tname/.gitattributes': '*.bin filter=lfs diff=lfs merge=lfs -text\n', 'dir\tname/blob.bin': 'binary-example' });
+ mkdirSync(join(root, '.git', 'info'), { recursive: true });
+ writeFileSync(join(root, '.git', 'info', 'attributes'), '*.bin -filter\n');
+ await insert('source-a', root, URL_CONFIG);
+ for (const apply of [false, true])
+ expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply, remote: false })).results[0]).toMatchObject({ status: 'refused', code: 'lfs_tracked' });
+ expect(await config('source-a')).toEqual(URL_CONFIG);
+ }), 60_000);
+
+ test('--remote ignores commented-out filter=lfs lines, and says an LFS filter is declared when a committed rule names one that matches no file', () => brain(async () => {
+ repoAt(defaultCloneDir('source-a'), { 'example.md': 'Canonical\n', '.gitattributes': '# *.bin filter=lfs diff=lfs merge=lfs -text\n # *.psd filter=lfs\n*.md text\n' });
+ repoAt(defaultCloneDir('source-b'), { 'example.md': 'Canonical\n', 'assets/.gitattributes': '*.psd filter=lfs diff=lfs merge=lfs -text\n' });
+ await insert('source-a', defaultCloneDir('source-a'), URL_CONFIG);
+ await insert('source-b', defaultCloneDir('source-b'), URL_CONFIG);
+ const report = await recordSourceOrigin(engine, ['source-a', 'source-b'], { origin: 'remote', apply: false, remote: false });
+ expect(report.results.map(result => result.code ?? result.status)).toEqual(['would_record', 'lfs_tracked']);
+ expect(report.results[1]!.reason).toContain('declares a git-lfs filter');
+ expect(report.results[1]!.reason).not.toContain('tracks files with git-lfs');
+ }), 60_000);
+
+ test('--remote records a clone of an empty repository: an unborn HEAD has no committed attributes, so there is nothing to restore', () => brain(async home => {
+ const bare = join(home, 'empty.git');
+ git(['init', '-q', '--bare', '-b', 'main', bare]);
+ mkdirSync(join(home, 'clones'), { recursive: true });
+ git(['clone', '-q', bare, defaultCloneDir('source-a')]);
+ await insert('source-a', defaultCloneDir('source-a'), URL_CONFIG);
+ expect((await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: true, remote: false })).results[0]).toMatchObject({ status: 'recorded' });
+ expect(lfsDownloadsOff(await config('source-a'))).toBe(true);
+ }), 60_000);
+
+ test('lfs_unknown names its cause: a missing checkout, or a clone directory that is not the top level of its own repository', () => brain(async home => {
+ await insert('source-a', defaultCloneDir('source-a'), URL_CONFIG);
+ const missing = (await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: false, remote: false })).results[0]!;
+ expect(missing).toMatchObject({ code: 'lfs_unknown' });
+ expect(missing.reason).toContain('is missing');
+ git(['init', '-q', home]);
+ mkdirSync(defaultCloneDir('source-a'), { recursive: true });
+ const enclosed = (await recordSourceOrigin(engine, ['source-a'], { origin: 'remote', apply: false, remote: false })).results[0]!;
+ expect(enclosed).toMatchObject({ code: 'lfs_unknown' });
+ expect(enclosed.reason).toContain('not the top level of its own Git repository');
+ expect(enclosed.reason).not.toContain('is missing');
+ }), 60_000);
+
+ test('a write that throws mid-batch is reported for its id, and the ids before and after it keep their own results', () => brain(async () => {
+ for (const id of ['source-a', 'source-b', 'source-c']) await insert(id, defaultCloneDir(id), URL_CONFIG);
+ const transaction = engine.transaction.bind(engine);
+ let calls = 0;
+ const patched = spyOn(engine, 'transaction').mockImplementation(async fn => {
+ if (++calls === 2) throw new Error('connection reset (example)');
+ return transaction(fn);
+ });
+ let report: Awaited<ReturnType<typeof recordSourceOrigin>>;
+ try { report = await recordSourceOrigin(engine, ['source-a', 'source-b', 'source-c'], { origin: 'local', apply: true, remote: false }); }
+ finally { patched.mockRestore(); }
+ expect(report.results.map(result => [result.source_id, result.code ?? result.status])).toEqual([['source-a', 'recorded'], ['source-b', 'write_failed'], ['source-c', 'recorded']]);
+ expect(report.results[1]!.reason).toContain('connection reset (example)');
+ expect(await config('source-b')).toEqual(URL_CONFIG);
+ expect(await config('source-c')).toEqual({ ...URL_CONFIG, added_by_remote_caller: false });
+ }), 60_000);
});