Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Every source checkout converges on its remote: gbrain sources git-reconcile, run by autopilot each tick
garrytan6cd49e109a7a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f21dcdf367..fce4bbe2c4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,15 @@ identifiers and attribution are available in the pre-removal Git revision## [Unreleased]+**Every source checkout now converges on its remote by itself: `gbrain sources git-reconcile`, run by autopilot each tick.** One idempotent call brings every source checkout on the host level with its upstream. A checkout that is only behind fast-forwards; one with local commits publishes them merged with the upstream in memory (the same secret scan and rewritten-upstream refusal as refresh, never a rebase or force-push); a managed checkout goes through `gbrain sources refresh` and its Git durability rule. A level checkout costs one fetch and reports `in_sync`, and callers on the same checkout take turns, so any agent, cron or hook can call it as often and as concurrently as it likes. Autopilot runs it each tick in a bounded child process, so a slow push never stalls the daemon; `autopilot.git_pull false` skips it, and `sources.refresh_publish_diverged false` leaves local commits `unpublished` while checkouts behind still fast-forward. Exit 0 means level, 1 that a checkout needs the user, 4 that a retry can fix it.++- `src/core/git/reconcile.ts` (new): one checkout under its own reconcile lock (outermost; no other path takes it, so it cannot deadlock a publish); fetch with the tracking ref's move logged, then fast-forward, publish with `scanSecrets`, or refuse `upstream_rewritten`; a fetch that loses the tracking-ref race to a concurrent fetch retries.+- `src/core/git/upstream-history.ts` (new): the rewritten-upstream check, moved out of `src/core/persistence/worktree-refresh.ts` so refresh and reconcile share it.+- `src/commands/sources-git-reconcile.ts` (new), `src/commands/sources.ts`: the command, grouping sources by checkout; a managed checkout waits out a running refresh up to `--wait`.+- `src/commands/autopilot-git-reconcile.ts` (new), `src/commands/autopilot-daemon.ts`: the per-tick child, bounded at 15 minutes and logged.+- Docs and skills: `docs/guides/live-sync.md`, `docs/guides/shared-checkout-git.md`, `docs/guides/cron-schedule.md` (allow minutes for fetch and push calls), `skills/cron-scheduler/SKILL.md` (the managed cron line runs `sources git-reconcile`).+- Tests: `test/sources-git-reconcile.test.ts` (level, stranded, behind, diverged, conflict, secret, rewritten upstream, eight concurrent callers while another clone pushes, managed with durability on and off, no checkout, publishing off), `test/autopilot-git-reconcile.test.ts` (the daemon's event loop keeps ticking during the child; a refusal is reported; a hung child is killed).+**A diverged managed checkout now publishes its local commits on refresh by default when Git durability is on, after a secret scan of those commits.** Commits a script made with plain `git commit` in a managed checkout used to strand there whenever the remote moved: every `gbrain sources refresh` refused `refresh_diverged` and asked the user, unless `sources.refresh_publish_diverged` was set to `true`. With the setting unset, a worktree whose Git durability is on (`gbrain sources writer git-durability <source> --enable`, or the legacy hook) now publishes them itself: the local commits merged with the upstream in memory, never a rebase or force-push, then the fast-forward. The publish child scans, under its publish lock and for the exact commits it pushes, what they add: commit messages, added lines (including a value a later commit removed) and merge-commit conflict resolutions. A value that looks like a secret, or added text too large to scan, refuses the new `publish_secret_found` naming the file, line, rule and commit, never the value, with nothing pushed. The rewritten-upstream refusal and the `--expect-remote` bound are unchanged. `false` turns it off; `true` still publishes with durability off.### Itemized changesdiff --git a/docs/guides/cron-schedule.md b/docs/guides/cron-schedule.mdindex 1882c49c2b..3d0a866568 100644--- a/docs/guides/cron-schedule.md+++ b/docs/guides/cron-schedule.md@@ -144,7 +144,10 @@ Rules for the wrapper and everything that calls it:Set caller timeouts of at least 10 s in the shell and in the languageruntime that runs it (a runtime's exec timeout can override the shell'swithout saying so), and turn on the [warm CLI socket](cli-socket.md) so- reads skip the cold start.+ reads skip the cold start. A call that fetches or pushes+ (`gbrain sources git-reconcile`, `gbrain sources refresh`) can take minutes+ on a slow remote: give it at least 15 minutes rather than killing it+ mid-push, and let overlapping runs take turns instead of skipping them.6. **Quote the env file.** A file a shell sources is shell syntax. An unquoted`&` in a URL ends the assignment and backgrounds the rest, so the variablesilently keeps its old value. Quote every value, keep Postgres URLs todiff --git a/docs/guides/live-sync.md b/docs/guides/live-sync.mdindex fdb96705f9..96144b7f06 100644--- a/docs/guides/live-sync.md+++ b/docs/guides/live-sync.md@@ -698,6 +698,19 @@ vars — incident-time escape hatches, not everyday knobs.an upstream rewritten by a force-push (or one gbrain has no earlier recordof) still refuses `refresh_diverged`, and `--dry-run` never publishes.+ `gbrain sources git-reconcile` does this for every source checkout on the+ host at once: a managed checkout goes through the refresh above, any other+ checkout with an upstream publishes its local commits the same way (secret+ scan, rewrite check, merge in memory) and fast-forwards when it is only+ behind. A level checkout costs one fetch and reports `in_sync`, so any agent,+ cron or hook can call it as often and as concurrently as it likes; callers+ on one checkout take turns and end with the same upstream. Autopilot runs it+ each tick in a bounded child process (`autopilot.git_pull false` skips it),+ and `sources.refresh_publish_diverged false` leaves local commits+ `unpublished` while checkouts behind still fast-forward. Exit 0 means every+ checkout is level, 1 that one needs the user (the result names the refusal),+ 4 that a retry can fix it.+**Say to your agent:** *"Bring my notes source up to date with its remote."*9. **Turn automatic pulls off for every source.** On an unmanaged brain,diff --git a/docs/guides/shared-checkout-git.md b/docs/guides/shared-checkout-git.mdindex 51db647699..c61ea2a277 100644--- a/docs/guides/shared-checkout-git.md+++ b/docs/guides/shared-checkout-git.md@@ -293,6 +293,8 @@ commits come in. Four rules keep that checkout able to move:on (`sources.refresh_publish_diverged` unset; `false` turns it off, `true` publisheswith durability off too), after scanning the local commits for secrets([live sync](live-sync.md)); it never publishes over a rewritten upstream.+ `gbrain sources git-reconcile` runs that publish for every source checkout+ on the host, and autopilot runs it each tick.3. **Stop syncing a source before rewriting its history.** Before aforce-push or `git filter-repo`, run `gbrain sources set-sync <id> off`(with the user's agreement) so automatic sync skips it, anddiff --git a/llms-full.txt b/llms-full.txtindex b8356e3356..4e2a24e307 100644--- a/llms-full.txt+++ b/llms-full.txt@@ -7703,6 +7703,19 @@ vars — incident-time escape hatches, not everyday knobs.an upstream rewritten by a force-push (or one gbrain has no earlier recordof) still refuses `refresh_diverged`, and `--dry-run` never publishes.+ `gbrain sources git-reconcile` does this for every source checkout on the+ host at once: a managed checkout goes through the refresh above, any other+ checkout with an upstream publishes its local commits the same way (secret+ scan, rewrite check, merge in memory) and fast-forwards when it is only+ behind. A level checkout costs one fetch and reports `in_sync`, so any agent,+ cron or hook can call it as often and as concurrently as it likes; callers+ on one checkout take turns and end with the same upstream. Autopilot runs it+ each tick in a bounded child process (`autopilot.git_pull false` skips it),+ and `sources.refresh_publish_diverged false` leaves local commits+ `unpublished` while checkouts behind still fast-forward. Exit 0 means every+ checkout is level, 1 that one needs the user (the result names the refusal),+ 4 that a retry can fix it.+**Say to your agent:** *"Bring my notes source up to date with its remote."*9. **Turn automatic pulls off for every source.** On an unmanaged brain,@@ -7946,7 +7959,10 @@ Rules for the wrapper and everything that calls it:Set caller timeouts of at least 10 s in the shell and in the languageruntime that runs it (a runtime's exec timeout can override the shell'swithout saying so), and turn on the [warm CLI socket](cli-socket.md) so- reads skip the cold start.+ reads skip the cold start. A call that fetches or pushes+ (`gbrain sources git-reconcile`, `gbrain sources refresh`) can take minutes+ on a slow remote: give it at least 15 minutes rather than killing it+ mid-push, and let overlapping runs take turns instead of skipping them.6. **Quote the env file.** A file a shell sources is shell syntax. An unquoted`&` in a URL ends the assignment and backgrounds the rest, so the variablesilently keeps its old value. Quote every value, keep Postgres URLs todiff --git a/plugin/skills/cron-scheduler/SKILL.md b/plugin/skills/cron-scheduler/SKILL.mdindex 6e3802df2b..f975e70d70 100644--- a/plugin/skills/cron-scheduler/SKILL.md+++ b/plugin/skills/cron-scheduler/SKILL.md@@ -108,14 +108,15 @@ On a managed brain, install this line instead, on the host that owns thesources:```cron-*/15 * * * * gbrain-run sources refresh <id> >> /tmp/gbrain-cron.log 2>&1; gbrain-run sync --all --no-pull --hard-deadline 13m >> /tmp/gbrain-cron.log 2>&1+*/15 * * * * gbrain-run sources git-reconcile >> /tmp/gbrain-cron.log 2>&1; gbrain-run sync --all --no-pull --hard-deadline 13m >> /tmp/gbrain-cron.log 2>&1```-- `gbrain sources refresh <id>` is the only way a managed checkout takes- upstream commits (a fetch and `git merge --ff-only`, then a managed sync of- the sources on that checkout). Add one refresh per checkout that tracks a- remote, with its real source id; drop it for checkouts with no remote. The- `;` is deliberate: the sync still runs when a refresh refuses.+- `gbrain sources git-reconcile` brings every checkout on the host level with+ its upstream; a managed checkout goes through `gbrain sources refresh` (a+ fetch and `git merge --ff-only`, then a managed sync of its sources), the+ only way it takes upstream commits. A level checkout costs one fetch, and+ autopilot already runs it each tick. The `;` is deliberate: the sync still+ runs when a reconcile refuses.- `refresh_diverged`: the checkout has commits its upstream lacks, sosomething commits outside gbrain (usually a script that writes files andruns `git commit` in the checkout). Fix that writer so it commits withdiff --git a/scripts/core-world-reads-baseline.tsv b/scripts/core-world-reads-baseline.tsvindex 27774d22a7..e294e2edc3 100644--- a/scripts/core-world-reads-baseline.tsv+++ b/scripts/core-world-reads-baseline.tsv@@ -444,6 +444,7 @@ src/core/git/commit-push.ts fs_import 1 W7: filesystem boundary moves to the locsrc/core/git/commit-push.ts module_collection 1 W6: per-engine state, or mark as a constant tablesrc/core/git/commit-push.ts process_env 7 W6: settings plane out of the coresrc/core/git/commit-push.ts spawn_import 1 W7: process-wide effects move to src/cli+src/core/git/upstream-history.ts fs_import 1 W7: filesystem boundary moves to the local app; reads the tail of the checkout reflog to tell a rewritten upstream from a fast-forward (lifted out of worktree-refresh.ts)src/core/github-app-token.ts fs_import 1 W7: filesystem boundary moves to the local appsrc/core/github-source.ts fs_import 2 W7: filesystem boundary moves to the local appsrc/core/github-source.ts load_config 1 W6: settings plane out of the corediff --git a/scripts/module-size-limits.tsv b/scripts/module-size-limits.tsvindex 556c946bfc..1948cc4fc7 100644--- a/scripts/module-size-limits.tsv+++ b/scripts/module-size-limits.tsv@@ -30,7 +30,7 @@ src/commands/integrations.ts 1739 ratchet grown v0.46.26.0 megawave (#4039 connesrc/core/minions/handlers/subagent.ts 1925 ratchet merged: master v131 settle rework + dream-wave C5 peel + C6 accounting + C7 lease heartbeats/lost-lease aborts + C9 oneshot dispatch + adversarial fix batches; grown by #4078 subagent-loop capability fix; +6 lines (reasoning-item replay: adaptContentBlocksToChatBlocks handles 'reasoning' blocks); +3 wave G #4514: Anthropic-via-OpenRouter allowed on the subagent loop; +36 lines (code review, same PR): isPromptTooLongError walks a bounded .cause chain so it still matches post-normalizeAIError() wrapping, plus extractPromptTooLongDetail so the dead-lettered job's error carries the matched provider detail instead of the generic normalizeAIError() wrapper text; master integration: harness support: boundary wiring to shared grants, isolated setup, withdrawal and delegated guards; #5105: +13 call-site wiring to shared tool-write-identity helper across fresh and replay dispatch; +7 fix wave 9 lane B #4921: ai.chat.per_turn_timeout_ms read once and passed to the oneshot and gateway-loop paths (resolver lives in minions/handler-timeouts.ts)src/commands/bootstrap.ts 2154 ratchet grandfathered at merge (grew past the 1500 cap on master); raised for #4066 HOME_WORKSPACE_GUARD_EXEMPT (status/uninstall/harness); +5 for the harnessDetect test seam (bulk lives in core/bootstrap/harness.ts); +23 for the contract subcommand dispatcher glue (#4105 absorb — bulk lives in core/bootstrap/contract.ts); +codex hooks-consent gate + mcp+hooks receipt registration + uninstall arm; +42 ambient-writeback WP8: interview-time SURFACE_MULTIUSER audience apply (pre-init ordering) + applyDeclaredAudienceFromInterview; +3 #4897 harness usage clause documenting --source ID semantics; +5 #5878: --refresh-skills dispatch and usage lines (logic in core/bootstrap/harness-skills.ts); +11 #5488: SUBCOMMAND_HELP harness entry (gate on --help / -h / help) — CLI help line, cannot move; +11 #4618 seat: --seat/--no-seat help lines and the runHooks parse (parse + notes live in core/bootstrap/hooks.ts parseSeatFlags); +13 #4082: runAdoptHooks (the --adopt branch of the hooks subcommand, kept out of runHooks for the function-size ratchet) and its two help lines (adoption logic lives in core/bootstrap/adopt-connection.ts + adopted-connections.ts); +4 #8426/#8428: the user-scope MCP registration, its verify and the workspace and Codex SessionEnd hooks start gbrain through the hook launcher (logic in core/setup/hook-launcher.ts); +2 #8435: the user-global opencode entry starts serve through the hook launcher (logic in core/setup/hook-launcher.ts); +1 GBRA-224: INSTALL_PIN import (core/install-ref.ts; re-summed at the GBRA-346 merge: master 2153 + 1)src/core/minions/worker.ts 1795 ratchet grandfathered at merge (grew past the 1500 cap on master, #4170); grown v0.46.11.0 five-issue wave; security: durable queued authority and confined filesystem dispatch; typed readiness, configuration-blocked lifecycle and bounded release; post-validation readiness publication and confirmed isolated-execution stop evidence; latch typed lock-renewal faults before generic retry; +9 Lane D: JobDeferredError routing (deferJob helper); +1 spend authorization: runWithJobSpend import (both handler seams call it in place of the bare handler); +3 watchdog: gracefulShutdown records its reason and launchJob aborts a claim committed before it fired; +5 GBRA-130 A-30: watchdog-attribution import, InFlightJob.shared (interface field), the suspect-alone claim gate and option, and the watchdog settle call in the executeJob catch (logic in minions/watchdog-attribution.ts); +6 GBRA-133 2a.4 sync-queue wake: IdleWaiter import and field, claimStarted before each claim, the idle poll through it, and stop/requestShutdown ending its wait (logic in minions/idle-wait.ts); +1 GBRA-158 #7239: refusal.ts import (a deterministic refusal ends the job at once; classification in minions/refusal.ts); +3 #7242 lane reservation: lanes import and the longJobNames/shortJobReservedSlots defaults (the claim passes claimExclusions; policy in minions/lanes.ts); +2 #7242 terminal hook: onJobSettled default and the settle call (logic in terminal-hook.ts); #7252 x #7249 merge: both sides grew the file, re-summed (1794 measured); GBRA-151 #7183/#7187/#7188 re-summed at the v0.60.402.0 merge (1795 measured)-src/commands/sources.ts 2113 ratchet +3 PR B: sources guard dispatch and help rows (body in sources-guard.ts); +3 G7b-2: sources host-repo dispatch and help row (body in sources-host-repo.ts and core/persistence/hosted-repo.ts); +5 PR #5955 / fix wave 14 P2.7: sources set-strategy dispatch and help row, the strategy field of sources list --json (body in sources-strategy.ts); +5 #5988: sources status/retry-held help rows describe Git holds (help lines cannot move); +2 #5988: sources status <id> filter and Git held files (view in persistence/connector-status.ts); +5 F0: sources refresh dispatch and help rows (command body in sources-refresh.ts, protocol in persistence/worktree-refresh.ts); +6 fix wave 5 #5409: mirror-readonly/mirror-writable dispatch, help rows and the list --json mirror_read_only field (body in sources-mirror.ts); grown v0.46.22.0 phone wave: github source kind (#4104); +129 v0.47 gmail-loops: google source-kind flags + vault preflight + duplicate-account warn + access-mode validation; +8 set-path dispatch + help rows (#4739 — command body peeled to sources-set-path.ts); +29 #4698 adoption: --calendar-id flag + duplicate-account warn scoped to overlapping services; #5105: trusted writer administration route; company handlers and receipt status wiring; parsing stays in dedicated modules; canonical reconciliation: +5 engine-free sibling-command dispatch and help row, implementation remains in source-reconcile.ts; +4 fix wave 3 lane A: sources status connector recovery state and run counts (view lives in persistence/connector-status.ts); +3 fix wave 4 lane B: sources retry-held dispatch and help row (command body in sources-retry-held.ts) and the connector first-sync note on add; bodies in source-delete.ts and sources-set-path.ts); +4 #5984: sources status shows the managed sync backlog (logic in persistence/sync-drain.ts); v0.60.48.0 merge: #5984 growth re-summed over v0.60.47.0 (2031 measured); +11 W-B4: sources shared-skills dispatch, its --help guard and help rows, and the shared_skills field/lines in sources status (body in sources-shared-skills.ts, view in shared-skills/source-opt-out.ts); +4 #6188: uncommitted legacy fence-repair notices in sources status (logic in fence-repair/uncommitted.ts); +7 #6317: data_moving/movement_state on sources status and its human line (reader in persistence/sync-movement.ts), writer movement help row; +1 GBRA-133: sources reset-upstream help row (body in sources-reset-upstream.ts); fix wave 20 integration: lane 3 (#6827) and master growth compose at 2062 measured; +3 GBRA-150: sources resolve-path dispatch and help rows (body in sources-resolve-path.ts); +10 #7326: sources status parses --holds/--limit/--cursor and passes the git_holds listing mode, plus 4 help lines (bounding logic in persistence/connector-status.ts gitHoldStatusJson); +2 GBRA-133 Q1: sources set-sync dispatch and help row (body in sources-sync-enabled.ts); +4 G1 git-canonical: sources git-gap dispatch, doc line and help rows (body in sources-git-gap.ts); +4 G3 git-canonical: sources reindex dispatch, doc line and help rows (body in sources-reindex.ts); +1 GBRA-156: the sources add non-federated warning line; re-summed at the master 33d73cb5 merge (2086 measured); +4 G8a git-canonical: sources canonical dispatch, doc line and help rows (body in sources-canonical.ts, plan and apply in persistence/git-conversion.ts); GBRA-156: sources add --json and its next step (logic in sources-add-next.ts); re-summed at the master ade6d638 merge (2100 measured); +2 GBRA-220 PR 2: the unmanaged sources add routes --kind meetings|slack to the shared lifecycle parser (body in sources-add-api.ts); re-summed at the master b85dd645 merge (2102 measured); +2 #7701: the archived field of SourceListEntry and its list entry (sources list now marks archived sources; an interface field cannot move): re-summed at the 0.60.583.0 merge: 2102 + 2; +1 GST-1113: sources federate refuses a QM scope source (one line; the check lives in core/access/attest-sources.ts), re-summed at the 0.60.594.0 gate-ahead: 2107 + 1; +5 GST-1113 /ship fix round: sources federate checks the scope mark and writes the config in one transaction under the source row lock (the check lives in core/access/attest-sources.ts)+src/commands/sources.ts 2119 ratchet +3 PR B: sources guard dispatch and help rows (body in sources-guard.ts); +3 G7b-2: sources host-repo dispatch and help row (body in sources-host-repo.ts and core/persistence/hosted-repo.ts); +5 PR #5955 / fix wave 14 P2.7: sources set-strategy dispatch and help row, the strategy field of sources list --json (body in sources-strategy.ts); +5 #5988: sources status/retry-held help rows describe Git holds (help lines cannot move); +2 #5988: sources status <id> filter and Git held files (view in persistence/connector-status.ts); +5 F0: sources refresh dispatch and help rows (command body in sources-refresh.ts, protocol in persistence/worktree-refresh.ts); +6 fix wave 5 #5409: mirror-readonly/mirror-writable dispatch, help rows and the list --json mirror_read_only field (body in sources-mirror.ts); grown v0.46.22.0 phone wave: github source kind (#4104); +129 v0.47 gmail-loops: google source-kind flags + vault preflight + duplicate-account warn + access-mode validation; +8 set-path dispatch + help rows (#4739 — command body peeled to sources-set-path.ts); +29 #4698 adoption: --calendar-id flag + duplicate-account warn scoped to overlapping services; #5105: trusted writer administration route; company handlers and receipt status wiring; parsing stays in dedicated modules; canonical reconciliation: +5 engine-free sibling-command dispatch and help row, implementation remains in source-reconcile.ts; +4 fix wave 3 lane A: sources status connector recovery state and run counts (view lives in persistence/connector-status.ts); +3 fix wave 4 lane B: sources retry-held dispatch and help row (command body in sources-retry-held.ts) and the connector first-sync note on add; bodies in source-delete.ts and sources-set-path.ts); +4 #5984: sources status shows the managed sync backlog (logic in persistence/sync-drain.ts); v0.60.48.0 merge: #5984 growth re-summed over v0.60.47.0 (2031 measured); +11 W-B4: sources shared-skills dispatch, its --help guard and help rows, and the shared_skills field/lines in sources status (body in sources-shared-skills.ts, view in shared-skills/source-opt-out.ts); +4 #6188: uncommitted legacy fence-repair notices in sources status (logic in fence-repair/uncommitted.ts); +7 #6317: data_moving/movement_state on sources status and its human line (reader in persistence/sync-movement.ts), writer movement help row; +1 GBRA-133: sources reset-upstream help row (body in sources-reset-upstream.ts); fix wave 20 integration: lane 3 (#6827) and master growth compose at 2062 measured; +3 GBRA-150: sources resolve-path dispatch and help rows (body in sources-resolve-path.ts); +10 #7326: sources status parses --holds/--limit/--cursor and passes the git_holds listing mode, plus 4 help lines (bounding logic in persistence/connector-status.ts gitHoldStatusJson); +2 GBRA-133 Q1: sources set-sync dispatch and help row (body in sources-sync-enabled.ts); +4 G1 git-canonical: sources git-gap dispatch, doc line and help rows (body in sources-git-gap.ts); +4 G3 git-canonical: sources reindex dispatch, doc line and help rows (body in sources-reindex.ts); +1 GBRA-156: the sources add non-federated warning line; re-summed at the master 33d73cb5 merge (2086 measured); +4 G8a git-canonical: sources canonical dispatch, doc line and help rows (body in sources-canonical.ts, plan and apply in persistence/git-conversion.ts); GBRA-156: sources add --json and its next step (logic in sources-add-next.ts); re-summed at the master ade6d638 merge (2100 measured); +2 GBRA-220 PR 2: the unmanaged sources add routes --kind meetings|slack to the shared lifecycle parser (body in sources-add-api.ts); re-summed at the master b85dd645 merge (2102 measured); +2 #7701: the archived field of SourceListEntry and its list entry (sources list now marks archived sources; an interface field cannot move): re-summed at the 0.60.583.0 merge: 2102 + 2; +1 GST-1113: sources federate refuses a QM scope source (one line; the check lives in core/access/attest-sources.ts), re-summed at the 0.60.594.0 gate-ahead: 2107 + 1; +5 GST-1113 /ship fix round: sources federate checks the scope mark and writes the config in one transaction under the source row lock (the check lives in core/access/attest-sources.ts); +6 self-healing publish: sources git-reconcile dispatch, doc line and help rows (body in sources-git-reconcile.ts, protocol in core/git/reconcile.ts)src/core/link-extraction.ts 2424 ratchet grown v0.46.26.0 megawave: link-aware timeline split + delimiter-outside-links (#3957 #4062 #3737); +37 wave-k #3908 reimpl: isCrossSourceLinksEnabled config ladder; +10 wave #4277: parseTimelineEntries skips generated backlink receipts; +17 #4855: latin-fold in normalizeBasename + dir-hint step tries both slug grammars; +17: #4855 Latin stroke-letter fold; +5: #4873 ./-relative markdown links kept as live references; +34 wave review: `../y.md` sibling links resolve on the DB path + folded bare-wikilink grammar (#4873 class); +62 #4977: role-prior suppression in machine-list sections (rolePriorSuppressedRanges, EntityRef.index); +3 composed with #4985 (variation-selector strip) in the same wave; +4 #4995: markdown-link anchor strip + lowercase-candidate rationale comments; +6 pre-landing review: #4977 suppression on code-stripped content + truthful watermark/section-list comments; T2: source-qualified inference and atomic origin-scoped derived links; canonical attendance evidence, source-qualified orientation, strict attendee resolution, and relocated filesystem link scanner (command export preserved); +4: pack-owned fallback and ambiguous attendee guards Display-label attendance exclusion, indexed evidence membership, and bounded strict resolver caching; attendance-boundary review.; complete attendance resolution and fence-safe evidence traversal; frontmatter discovery, canonical basename admission and code-aware attendance comment masking; exclude inline HTML comment spans from admitted attendance ranges; +17 write-path wave: same-name rule for entity fuzzy matches + rename-alias fallback in makeResolver; +1 #5567: materialized marker ends bullet detail; +30 #5749: unique global-basename step before fuzzy and live keyword resolution (ResolveOptions, watermark note); #5765: bold **Attendees:** label accepted + LINK_EXTRACTOR_VERSION_TS bump note; +10 eval-wave N9-2/N9-3/N12-7: pack frontmatter mappings inherit FRONTMATTER_LINK_MAP direction, page-type-bound pack attended follows canonical attendance, LINK_EXTRACTOR_VERSION_TS bump note; +1 #5623: normalizeBasename collapses hyphen runs like slugifySegment (extractor watermark note); +11 #5562: parseTimelineEntries accepts dated ### headings (FS-extractor parity); +3 eval-wave N12-6: Participants accepted as attendance evidence + LINK_EXTRACTOR_VERSION_TS bump note; +22 P5 wanted pages: AuthoredRef on body link candidates; -33 lowered: P5 moved machine-section ranges to core/machine-sections.ts and stated relation types to core/line-grammar.ts; +7 P5: ExtractPageLinksOptions interface + wikilinkAuthoredRef helper (keeps extractPageLinks under the function-size limit); +32 P5: per-edge verb attachment (attachedVerb, VERB_RULES) + joined-as works_at + extractor watermark note; P5 x temporal-edges merge: verb attachment reads the verb at the link's own position in the window (excerptAnchor) and the within-page dedup (now dedupeCandidates) drops a mention of a target the page also links with a typed edge; links joined only by commas/conjunctions share the verb before the first (coordinated); board roles (BOARD_RE, BOARD_AFTER_RE) resolve to invested_in for an investor page and advises otherwise, never works_at; +1 fix wave 11 #6191: imports targetTakesVerb (role-verb target guard lives in link-target-roles.ts); v0.60.105.0 merge: #5984 Phase 5 link preparation (+6) re-summed over wave 11 at 2211; +109 Q2 Track C: stable verb-rule ids, traceLinkType/explainLinkType (reasons for every typing decision), per-occurrence typer hoisted out of extractPageLinks (makeTypeFor); unit logic lives in core/link-typing-units.ts; +25 Q2 Track C: typing-unit verb rules and vetoes in attachedVerb/traceLinkType, validity stints in explainLinkType; +4 U3: board-seat rule split out of INVESTED_RE (same verb and precedence) and investor-prior/link offsets passed to unit vetoes; +11 U2: locateLink shared by attachedVerb and the U2 post-pass in traceLinkType; -11 lowered: Q2 verdict reverted U2 (locateLink and the post-pass); +21 fix wave 13 P4.10 #6266: resolveSameDirSlug + exported resolveMarkdownLinkSlug, shared with the link validator; +41 Wave B PR 2 lane 2e: extractPageLinks split into extractBodyLinks (window-scoped, offset-shifted, pass-tagged candidates) and frontmatterLinks, plus entityRefPasses, candidateKey, rolePriorPhrases and the roleText/regexBudget/window options, so a segmented page streams through the same extractor (window and merge logic in core/page-body/segment-links.ts); +1 at the #6869 gate-ahead merge: the #6803 wave EXTRACTOR_BUMP_NOTES comment composed with lane 2e (2420 + 1); +3 G5 (GBRA-60): extractBodyLinks masks the ## Links fence (links-fence.ts), whose rows are projected as their own edgessrc/core/embedding-migration.ts 1587 ratchet new row: crossed 1500 in v0.46.25.0 fix waves (#4305 #4306 chunk-model truth helpers); retired provider integration removed; safety wave: canonical projection readiness and fact-aware completion; file configuration publication retains lease-row locks; +8 transactional retained-vector rebuildability checks before destructive migration boundaries; archived eligibility: source-locked reconciliation and live/current smoke revalidation; +2 #5680: authorization_generation field and its carry-forward on same-target re-apply (settle idempotency); +10 #5226: plan carries chunks_to_restamp beside chunks_to_embed; +9 #5088: schema transition defers HNSW builds into the marker (deferred_ann_indexes), carry-forward on re-apply, verify blocks on pending/INVALID ANN indexes (build phase in embedding-ann-build.ts); +10 #5088: dim-pinned tables (query_cache, facts, and any later entry of TEXT_EMBEDDING_DIM_PINNED_TABLES) defer their HNSW through the same worklist (DEFERRED_ANN_TABLES, recordDeferredAnnIndexes); +25 wave 8 lane D1 #5885: takes in the dim-pinned column list, plan (takes_to_embed), status (takes_pending) and completion verify; +17 #5334: smoke check samples one representative chunk per distinct page (bounded index-ordered batch walk, longest chunk re-read under the page lock, title-prefixed bounded query); +1 #5334: the smoke-query length limits are shared with the worst-case budgetsrc/core/github-source.ts 1530 ratchet new row: crossed the 1500 unlisted cap in v0.46.23.0 review fix wave (scope-state gating, client hardening, symlink containment); merged reliability/shared-skills source integration; managed sweep lease wrapper; fix wave 3 lane A: config parser peeled to github-source-config.ts (-47), +account pin wiring and pinned-installation refresh@@ -47,4 +47,4 @@ src/commands/doctor/checks/extraction-sync.ts 1500 ratchet +7 GBRA-130 A-1: linksrc/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 1684 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 refresh_publish_diverged_default_gbra386 row (since TBD until the merge slot; rows are data and cannot move)+src/core/behavior-change-notice.ts 1685 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 refresh_publish_diverged_default_gbra386 row (since TBD until the merge slot; rows are data and cannot move); +1 self-healing publish: the autopilot git-reconcile disclosure rowdiff --git a/skills/cron-scheduler/SKILL.md b/skills/cron-scheduler/SKILL.mdindex ac015aafe8..bbe8d09060 100644--- a/skills/cron-scheduler/SKILL.md+++ b/skills/cron-scheduler/SKILL.md@@ -107,14 +107,15 @@ On a managed brain, install this line instead, on the host that owns thesources:```cron-*/15 * * * * gbrain-run sources refresh <id> >> /tmp/gbrain-cron.log 2>&1; gbrain-run sync --all --no-pull --hard-deadline 13m >> /tmp/gbrain-cron.log 2>&1+*/15 * * * * gbrain-run sources git-reconcile >> /tmp/gbrain-cron.log 2>&1; gbrain-run sync --all --no-pull --hard-deadline 13m >> /tmp/gbrain-cron.log 2>&1```-- `gbrain sources refresh <id>` is the only way a managed checkout takes- upstream commits (a fetch and `git merge --ff-only`, then a managed sync of- the sources on that checkout). Add one refresh per checkout that tracks a- remote, with its real source id; drop it for checkouts with no remote. The- `;` is deliberate: the sync still runs when a refresh refuses.+- `gbrain sources git-reconcile` brings every checkout on the host level with+ its upstream; a managed checkout goes through `gbrain sources refresh` (a+ fetch and `git merge --ff-only`, then a managed sync of its sources), the+ only way it takes upstream commits. A level checkout costs one fetch, and+ autopilot already runs it each tick. The `;` is deliberate: the sync still+ runs when a reconcile refuses.- `refresh_diverged`: the checkout has commits its upstream lacks, sosomething commits outside gbrain (usually a script that writes files andruns `git commit` in the checkout). Fix that writer so it commits withdiff --git a/skills/skills.lock.json b/skills/skills.lock.jsonindex 76abef9092..8fffbe70d4 100644--- a/skills/skills.lock.json+++ b/skills/skills.lock.json@@ -66,7 +66,7 @@"conversation-archive/routing-eval.jsonl": "ae087a84b1fd5b108b7cdab8d035a09b3ccecd8aad53ba5f71e463059108cfca","correction-pipeline/SKILL.md": "04b4aee06d7fcdd75db314da432ed7c08b5fbc20f117f18062c64aaf795bc564","correction-pipeline/routing-eval.jsonl": "7f8d96606a8d7bed3d79fdcee6904764c8abb9fa0b506adb414b5c4805b69d0b",- "cron-scheduler/SKILL.md": "0f5c3148ca419efe6c3e3c781e3bac4e107732b9bf7d55c05c2385686acfa7fd",+ "cron-scheduler/SKILL.md": "f336cd032ee61d88c40605ddfe884c91623b29bdfb20d78d6b2764f502c9a999","cross-modal-review/SKILL.md": "fc00a04e2ff9f498bee9b0b636bceba9dcfdc869c35d9a983109f408170f0807","daily-task-manager/SKILL.md": "e93264a83b5fd0a19b2f06a322b3a4ad92d418f6a968c1cd51f4bea79cbca4d9","daily-task-manager/routing-eval.jsonl": "e98b2592e65c26952242436a960d8b2061e375b269004a18c230cbcf1d692ff9",diff --git a/src/commands/autopilot-daemon.ts b/src/commands/autopilot-daemon.tsindex 443e2b0684..8cecf77a20 100644--- a/src/commands/autopilot-daemon.ts+++ b/src/commands/autopilot-daemon.ts@@ -330,6 +330,12 @@ export async function runAutopilotDaemon(engine: BrainEngine, args: string[]): P// (single SELECT). See the NO_WORKER_WARN_TICKS comment for caveats.if (noWorker && useMinionsDispatch) await probeNoWorkerPeer(engine, state, baseInterval);+ if (!(await (await import('../core/persistence/automatic-sync-policy.ts')).automaticGitPullDisabled(engine))) {+ const reconciled = await (await import('./autopilot-git-reconcile.ts')).reconcileGitInChild();+ for (const line of reconciled.lines) (reconciled.ok ? console.log : console.error)(`[autopilot] git-reconcile: ${line}`);+ if (configurationBlocked()) continue;+ }+const tick = useMinionsDispatch? await dispatchAutopilotTick(engine, state, { repoPath, baseInterval, jsonMode }): { cycleOk: await runInlineCycle(engine, state, repoPath, shutdownAbort, jsonMode) };diff --git a/src/commands/autopilot-git-reconcile.ts b/src/commands/autopilot-git-reconcile.tsnew file mode 100644index 0000000000..b76bd6425a--- /dev/null+++ b/src/commands/autopilot-git-reconcile.ts@@ -0,0 +1,27 @@+/**+ * Autopilot's reconcile: each daemon tick runs `gbrain sources git-reconcile --json` in a bounded child, so every source+ * checkout on this host converges on its upstream without anyone calling it. The publish inside is synchronous (fetches,+ * pushes and lock waits block for minutes); in a child it never stalls the daemon's event loop, its lease renewals or its+ * heartbeat. A failure is logged and the tick goes on: the next tick retries, and a refusal that needs the user (a+ * conflict, a secret, a rewritten upstream) is reported the same way `gbrain sources git-reconcile` reports it.+ */+import { execFileBounded } from '../core/bounded-child-exec.ts';+import { CHILD_ENV, resolveChildCliInvocation } from '../core/minions/job-isolation.ts';++export const AUTOPILOT_RECONCILE_TIMEOUT_MS = 900_000;++export interface AutopilotReconcileOutcome { ok: boolean; exit: number | null; lines: string[] }++export async function reconcileGitInChild(timeoutMs = AUTOPILOT_RECONCILE_TIMEOUT_MS): Promise<AutopilotReconcileOutcome> {+ const cli = resolveChildCliInvocation(process.env, process.execPath, process.argv[1], () => null);+ if (!cli) return { ok: false, exit: null, lines: [`could not locate the gbrain CLI to reconcile Git checkouts; set ${CHILD_ENV.childCliOverride} to its path`] };+ const child = await execFileBounded(cli.cmd, [...cli.argsPrefix, 'sources', 'git-reconcile', '--json', '--wait', '60'],+ { timeout: timeoutMs, termGraceMs: 10_000, maxBuffer: 4 * 1024 * 1024, env: process.env });+ const exit = child.error ? (typeof child.error.code === 'number' ? child.error.code : null) : 0;+ let doc: { results?: Array<{ sources?: string[]; root?: string; status?: string; error?: { code?: string; message?: string } }> } | null = null;+ try { doc = JSON.parse(String(child.stdout).trim()); } catch { /* reported below */ }+ if (!doc?.results) return { ok: false, exit, lines: [`sources git-reconcile ${exit === null ? 'did not finish' : `exited ${exit}`}: ${String(child.stderr).trim().slice(0, 300) || 'no output'}`] };+ const lines = doc.results.filter(r => r.status !== 'in_sync' && r.status !== 'no_repo' && r.status !== 'no_upstream')+ .map(r => `${(r.sources ?? []).join(', ') || r.root}: ${r.status}${r.error ? ` ${r.error.code}: ${r.error.message}` : ''}`);+ return { ok: exit === 0, exit, lines };+}diff --git a/src/commands/sources-git-reconcile.ts b/src/commands/sources-git-reconcile.tsnew file mode 100644index 0000000000..5bf4b88a0d--- /dev/null+++ b/src/commands/sources-git-reconcile.ts@@ -0,0 +1,133 @@+/**+ * `gbrain sources git-reconcile [<source-id>...] [--dry-run] [--wait <seconds>] [--json]`: bring every source checkout on+ * this host level with its upstream, idempotently, so any thread, agent, cron or hook can call it as often and as+ * concurrently as it likes. A level checkout costs one fetch and reports in_sync. A managed checkout (a persistence+ * worktree) is reconciled by `gbrain sources refresh`, which publishes its local commits (secret gate, rewrite check,+ * Git durability) and fast-forwards; a refresh already running is waited for, then the checkout is checked again. Any+ * other checkout goes through reconcileCheckout (src/core/git/reconcile.ts). Merges are in memory; nothing rebases or+ * force-pushes. A brain with no Git checkout reports no_repo and exits 0.+ *+ * Exit: 0 when every checkout is level (or has nothing to reconcile), 1 when one needs the user (a conflict, a+ * secret, a rewritten upstream, durability off), 4 when one failed in a way a retry can fix (network, a busy lock).+ */+import type { BrainEngine } from '../core/engine.ts';+import { setCliExitVerdict } from '../core/cli-force-exit.ts';+import { reconcileCheckout, fetchLevel, gitReader, withReconcileLock, type CheckoutReconcileStatus } from '../core/git/reconcile.ts';+import { isTruthy } from '../core/connectors/config-keys.ts';+import { classifyGitCheckout } from '../core/git-checkout.ts';+import { OperationError } from '../core/ops/contract.ts';+import { existingHostIdFor } from '../core/persistence/identity.ts';+import { getWorktreeBinding, managedPersistenceEnabled } from '../core/persistence/ownership.ts';+import { REFRESH_PUBLISH_DIVERGED_KEY, refreshWorktree } from '../core/persistence/worktree-refresh.ts';+import { listSources } from '../core/sources-ops.ts';++const USAGE = 'Usage: gbrain sources git-reconcile [<source-id>...] [--dry-run] [--wait <seconds>] [--json]';+const RETRYABLE = new Set(['fetch_failed', 'push_unconfirmed', 'commit_lock_busy', 'refresh_in_progress', 'refresh_lock_timeout', 'refresh_drain_timeout', 'git_unavailable']);++export type SourceReconcileStatus = CheckoutReconcileStatus | 'needs_user' | 'failed';++export interface SourceReconcile {+ sources: string[]; root: string; managed: boolean; status: SourceReconcileStatus; ahead: number; behind: number;+ sha?: string; integrated?: number; reason?: string; refresh_id?: string | null;+ error?: { code: string; message: string; suggestion?: string; fix?: unknown };+}++export interface ReconcileOptions { sourceIds?: string[]; dryRun?: boolean; waitMs?: number; sleep?: (ms: number) => Promise<void> }++async function reconcileManaged(engine: BrainEngine, sourceId: string, root: string, opts: ReconcileOptions): Promise<Omit<SourceReconcile, 'sources' | 'root' | 'managed'>> {+ const level = await fetchLevel(root, gitReader(root));+ if (!level) return { status: 'no_upstream', ahead: 0, behind: 0 };+ if (!level.ahead && !level.behind) return { status: 'in_sync', ahead: 0, behind: 0, sha: level.target };+ if (opts.dryRun) return { status: level.ahead ? 'published' : 'fast_forwarded', ahead: level.ahead, behind: level.behind, sha: level.target, reason: 'dry run: nothing changed' };+ const sleep = opts.sleep ?? ((ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms)));+ const deadline = Date.now() + (opts.waitMs ?? 300_000);+ for (;;) {+ try {+ const done = await refreshWorktree(engine, sourceId);+ return { status: done.published ? 'published' : 'fast_forwarded', ahead: level.ahead, behind: level.behind, sha: done.target_head ?? undefined, refresh_id: done.refresh_id,+ ...(done.published ? { integrated: done.published.integrated } : {}) };+ } catch (error) {+ if (!(error instanceof OperationError) || error.code !== 'refresh_in_progress' || Date.now() >= deadline) throw error;+ await sleep(2000);+ const again = await fetchLevel(root, gitReader(root));+ if (again && !again.ahead && !again.behind) return { status: 'in_sync', ahead: 0, behind: 0, sha: again.target };+ }+ }+}++export async function reconcileSources(engine: BrainEngine, opts: ReconcileOptions = {}): Promise<SourceReconcile[]> {+ const listed = (await listSources(engine)).filter(s => s.local_path && (!opts.sourceIds?.length || opts.sourceIds.includes(s.id)));+ const unknown = (opts.sourceIds ?? []).filter(id => !listed.some(s => s.id === id));+ if (unknown.length) throw new OperationError('unknown_source', `No source with a local checkout named ${unknown.join(', ')}.`, 'gbrain sources list shows each source and its local path.');+ const managed = await managedPersistenceEnabled(engine);+ const configured = (await engine.getConfig(REFRESH_PUBLISH_DIVERGED_KEY))?.trim();+ const publish = !configured || isTruthy(configured);+ const groups = new Map<string, { sources: string[]; root: string; managed: boolean }>();+ for (const source of listed) {+ const binding = managed ? await getWorktreeBinding(engine, source.id, existingHostIdFor(engine)) : null;+ const root = binding?.local_path ?? source.local_path!;+ if (classifyGitCheckout(root) === 'not_git') continue;+ const key = binding ? `worktree:${binding.worktree_id}` : `path:${root}`;+ const group = groups.get(key) ?? { sources: [], root, managed: !!binding };+ group.sources.push(source.id);+ groups.set(key, group);+ }+ if (!groups.size) return [{ sources: [], root: '', managed: false, status: 'no_repo', ahead: 0, behind: 0 }];+ const out: SourceReconcile[] = [];+ for (const group of groups.values()) {+ try {+ const result = group.managed+ ? await withReconcileLock(group.root, opts.waitMs ?? 300_000, () => reconcileManaged(engine, group.sources[0], group.root, opts))+ : await reconcileCheckout(group.root, { dryRun: opts.dryRun, publish, ...(opts.waitMs !== undefined ? { waitMs: opts.waitMs } : {}) });+ out.push({ ...group, ...result });+ } catch (error) {+ if (!(error instanceof OperationError)) throw error;+ out.push({ ...group, status: RETRYABLE.has(error.code) ? 'failed' : 'needs_user', ahead: 0, behind: 0,+ error: { code: error.code, message: error.message, ...(error.suggestion ? { suggestion: error.suggestion } : {}), ...(error.fix ? { fix: error.fix } : {}) } });+ }+ }+ return out;+}++export function parseReconcileArgs(args: string[]): { json: boolean; options: ReconcileOptions } {+ const sourceIds: string[] = [];+ let waitMs: number | undefined;+ for (let i = 0; i < args.length; i++) {+ const arg = args[i];+ if (arg === '--json' || arg === '--dry-run') continue;+ if (arg === '--wait') {+ const value = Number(args[++i]);+ if (!Number.isFinite(value) || value < 0) throw new OperationError('invalid_params', `--wait needs a non-negative number of seconds; got ${args[i] ?? 'nothing'}.`, USAGE);+ waitMs = Math.round(value * 1000);+ continue;+ }+ if (arg.startsWith('-')) throw new OperationError('invalid_params', `Unknown sources git-reconcile flag: ${arg}.`, USAGE);+ sourceIds.push(arg);+ }+ return { json: args.includes('--json'), options: { sourceIds, dryRun: args.includes('--dry-run'), ...(waitMs !== undefined ? { waitMs } : {}) } };+}++function line(r: SourceReconcile): string {+ const who = r.sources.length ? `${r.sources.join(', ')} (${r.root})` : 'this brain';+ const counts = r.ahead || r.behind ? ` [${r.ahead} ahead, ${r.behind} behind]` : '';+ switch (r.status) {+ case 'in_sync': return `${who}: in sync with its upstream`;+ case 'published': return `${who}: published local commits${r.integrated ? ` merged with ${r.integrated} upstream commit(s)` : ''}${counts}${r.reason ? ` (${r.reason})` : ''}`;+ case 'fast_forwarded': return `${who}: brought up to its upstream${counts}${r.reason ? ` (${r.reason})` : ''}`;+ case 'lagging': return `${who}: published, but the checkout did not advance: ${r.reason ?? 'local edits block it'}`;+ case 'unpublished': return `${who}: ${r.ahead} local commit(s) left unpublished: ${r.reason}${counts}`;+ case 'no_repo': return `${who}: no Git checkout, nothing to reconcile`;+ case 'no_upstream': return `${who}: the branch has no upstream, nothing to reconcile`;+ default: return `${who}: ${r.status === 'needs_user' ? 'needs the user' : 'failed, retry'}: ${r.error?.code}: ${r.error?.message}${r.error?.suggestion ? ` Next: ${r.error.suggestion}` : ''}`;+ }+}++export async function runSourcesReconcile(engine: BrainEngine, args: string[]): Promise<void> {+ if (args.includes('--help') || args.includes('-h')) { console.log(USAGE); return; }+ const { json, options } = parseReconcileArgs(args);+ const results = await reconcileSources(engine, options);+ const exit = results.some(r => r.status === 'needs_user') ? 1 : results.some(r => r.status === 'failed') ? 4 : 0;+ if (json) console.log(JSON.stringify({ results, exit }, null, 2));+ else for (const r of results) console.log(line(r));+ if (exit) setCliExitVerdict(exit);+}diff --git a/src/commands/sources.ts b/src/commands/sources.tsindex 0989fde8c7..8947c8430c 100644--- a/src/commands/sources.ts+++ b/src/commands/sources.ts@@ -20,6 +20,7 @@* gbrain sources unfederate <id> — sources.config.federated = false* gbrain sources mirror-readonly|mirror-writable <id> — sources.config.mirror_read_only (#5409)* gbrain sources refresh <id> — drained worktree-wide ff-only refresh on a managed brain (F0)+ * gbrain sources git-reconcile [<id>...] — idempotent publish/fast-forward of every source checkout (sources-git-reconcile.ts)* gbrain sources git-gap <id> — read-only census of the database against the commit at HEAD (sources-git-gap.ts)* gbrain sources reindex <id> --from-git — a strict source's re-derive from Git, preview then apply (sources-reindex.ts)* gbrain sources canonical <id> --to git|mixed — convert a source to git-canonical or back, preview then apply (sources-canonical.ts)@@ -1928,6 +1929,7 @@ export async function runSources(engine: BrainEngine, args: string[]): Promise<vcase 'set-pull-strategy': return (await import('./sources-pull-strategy.ts')).runSetPullStrategy(engine, rest);case 'set-sync': return (await import('./sources-sync-enabled.ts')).runSetSync(engine, rest);case 'refresh': return (await import('./sources-refresh.ts')).runSourcesRefresh(engine, rest);+ case 'git-reconcile': return (await import('./sources-git-reconcile.ts')).runSourcesReconcile(engine, rest);case 'git-gap': return (await import('./sources-git-gap.ts')).runSourcesGitGap(engine, rest);case 'host-repo': return (await import('./sources-host-repo.ts')).runHostRepo(engine, rest);case 'guard': return (await import('./sources-guard.ts')).runSourcesGuard(engine, rest);@@ -2041,6 +2043,10 @@ Subcommands:Managed brains: drain writes to the source's worktree, fast-forwardit to its upstream (git merge --ff-only) and sync every source boundto it. --dry-run fetches and previews only.+ git-reconcile [<id>...] [--dry-run] [--wait <s>] [--json]+ Bring every source checkout level with its upstream: publish local+ commits (merged in memory, secret-scanned) and fast-forward; in sync+ is a no-op. Safe to run concurrently and repeatedly.git-gap <id> [--sample <n>] [--json]Read-only: how far the database is from the commit at HEAD, page by page.reindex <id> --from-git [--dry-run | --apply --expect <hash>] [--json]diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.tsindex 1dbec06493..8fefceee88 100644--- a/src/core/behavior-change-notice.ts+++ b/src/core/behavior-change-notice.ts@@ -1486,6 +1486,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;{ since: '0.60.614.0', id: 'refresh_barrier_aborted_refresh_sync_gst1853', text: 'While a checkout\'s last `gbrain sources refresh` ended without completing (aborted, abandoned, or rolled back after its process died), every refresh run on the owner host, including one refused `refresh_diverged`, now ends by running the managed `--no-pull` sync of each source behind the checkout HEAD, listed in its `synced` (a refusal\'s message names them). With `persistence.refresh_barrier` on, a write held behind that refresh for a commit sync had not imported is therefore decided in that run instead of waiting for a later refresh: a `put_page` with `expected_revision` ends `revision_conflict`, a blind one lands and names what it replaced in `supersedes`, and a `remember` or `add_timeline_entry` applies on top.' },{ since: '0.60.608.0', id: 'refresh_barrier_later_write_gst1623', text: 'With `persistence.refresh_barrier` on, a write that waited behind a refresh or an import (other than a `remember` or `add_timeline_entry`, which still applies on top) now ends `revision_conflict` instead of applying when a later write to the same page committed first, which a consumer from an older release can cause; read the page again and resubmit with a new `request_id`. A write held again and again behind imports now expires once it has waited four wait limits (at least 4 hours) since its admission, even when each hold is shorter than one wait limit. With the barrier on or off, a plain-git commit that lands on the start commit right before a refresh merges makes git refuse the fast-forward, and the refresh now ends `refresh_source_changed` with the fence lifted and the commit kept, instead of leaving the checkout `recovery_required`; one that lands right after the merge is recorded and synced.' },{ since: '0.60.616.0', id: 'refresh_publish_diverged_default_gbra386', text: 'A diverged managed checkout now publishes its local commits on `gbrain sources refresh` by default when the worktree\'s Git durability is on: `sources.refresh_publish_diverged` unset now means on there (it used to mean off), merged with the upstream in memory, never rebased or forced. Every automatic publish first scans what the local commits add (messages, added lines, merge resolutions) and refuses the new `publish_secret_found` naming the file, line and rule, never the value, when one looks like a secret; `gbrain git commit-push --push-only --scan-secrets` runs the same gate. Unset with durability not on still refuses, naming `gbrain sources writer git-durability <source> --enable`; `false` turns it off.' },+ { since: '0.60.616.0', id: 'autopilot_git_reconcile_gbra386', text: 'Autopilot now runs the new `gbrain sources git-reconcile` each tick in a bounded child process: every source checkout on the host with an upstream publishes its local commits (secret scan, rewrite check, merged with the upstream in memory, never rebased or forced) and fast-forwards when it is only behind; a managed checkout goes through `gbrain sources refresh`, with its Git durability rule. A refusal is logged and retried next tick. `autopilot.git_pull false` skips it; `sources.refresh_publish_diverged false` leaves local commits unpublished.' },];/** The newest disclosed change's release: the notice id moves only when a release adds rows. */diff --git a/src/core/cli-flag-registry.generated.ts b/src/core/cli-flag-registry.generated.tsindex fce071d59d..dddfd8bbf9 100644--- a/src/core/cli-flag-registry.generated.ts+++ b/src/core/cli-flag-registry.generated.ts@@ -21,7 +21,7 @@ export const CLI_FLAG_REGISTRY: Record<string, readonly string[]> = {'append': ['--brain', '--expect-tail', '--expected-revision', '--file', '--force', '--help', '--json', '--region', '--request-id', '--separator', '--source'],'apply-migrations': ['--accept-reviewed-inventory', '--acknowledge-no-backup', '--apply', '--apply-rewrites', '--backup-confirmed', '--brain', '--break-lock', '--confirm-quiesced', '--content-root', '--dry-run', '--export-db-only', '--export-source', '--force', '--force-all', '--force-orchestrator', '--force-retry', '--force-schema', '--help', '--host-dir', '--http', '--install', '--json', '--legacy-lock', '--list', '--locks', '--migrate-only', '--migration', '--mode', '--no-autopilot-install', '--non-interactive', '--only', '--path', '--prefer-postgres', '--probe', '--repo', '--require-db', '--skip-verify', '--source', '--statement-timeout', '--uninstall', '--url', '--yes'],'auth': ['--add', '--admin-intent', '--adopt-columns', '--adopt-permissions', '--all', '--all-new', '--allowed-operations', '--apply', '--attest-actor', '--attest-ops', '--attest-sources', '--before', '--bound-brain', '--bound-max-concurrent', '--bound-slug-prefixes', '--bound-source', '--bound-tools', '--brain', '--budget-usd-per-day', '--category', '--cleanup-dead-local-locks', '--client', '--code', '--confirm-quiesced', '--confirm-relocated-root', '--days', '--delegated-namespace', '--delegated-slug-prefixes', '--disable', '--dry-run', '--enable', '--enable-dcr-insecure', '--expect', '--expected-epoch', '--expected-state', '--federated-read', '--fix', '--force', '--grant-types', '--group-by', '--help', '--id', '--if-version', '--json', '--limit', '--manifest', '--max-concurrent', '--may-act-for', '--member', '--members', '--migrate-legacy', '--min-trust', '--no-pull', '--operation', '--operations', '--origin', '--parked-code', '--parked-pushes', '--pat-file', '--path', '--private-pages', '--probe', '--profile', '--read-sources', '--redirect-uri', '--refresh-operations', '--repair', '--replace', '--request-id', '--reset-default', '--retire', '--scopes', '--self-transfer', '--shared-skills', '--since', '--slug-prefixes', '--source', '--source-ids', '--sources', '--state', '--surface', '--takes-holders', '--token', '--token-endpoint-auth-method', '--token-ttl', '--unsettle', '--usage', '--wait', '--warn-only', '--yes'],- 'autopilot': ['--apply', '--batch', '--brain', '--channel', '--check', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--exit-code', '--expect', '--explain', '--ff-only', '--force', '--force-retry', '--from-pages', '--help', '--inject-bootstrap', '--inline', '--install', '--interval', '--is-ancestor', '--json', '--markdown', '--max-cost', '--max-rss', '--max-runtime', '--max-usd', '--migrate-only', '--model', '--no-autopilot-install', '--no-bun-floor-check', '--no-embed', '--no-embed-cache', '--no-inject', '--no-restart', '--no-worker', '--non-interactive', '--now', '--once', '--only', '--output', '--params', '--pause', '--phase', '--quiet', '--reader-mode', '--reason', '--refresh-cache', '--refs', '--remediation-plan', '--repo', '--restart-only', '--resume', '--retry-global', '--slot-a-model', '--slot-b-model', '--slot-c-model', '--source', '--stale', '--status', '--stop-mcp', '--swap-only', '--tags', '--target', '--trust', '--uninstall', '--user', '--verify', '--version', '--yes'],+ 'autopilot': ['--apply', '--batch', '--brain', '--channel', '--check', '--dimensions', '--dir', '--drain', '--dry-run', '--embedding-dimensions', '--embedding-model', '--exit-code', '--expect', '--explain', '--ff-only', '--force', '--force-retry', '--from-pages', '--help', '--inject-bootstrap', '--inline', '--install', '--interval', '--is-ancestor', '--json', '--markdown', '--max-cost', '--max-rss', '--max-runtime', '--max-usd', '--migrate-only', '--model', '--no-autopilot-install', '--no-bun-floor-check', '--no-embed', '--no-embed-cache', '--no-inject', '--no-restart', '--no-worker', '--non-interactive', '--now', '--once', '--only', '--output', '--params', '--pause', '--phase', '--quiet', '--reader-mode', '--reason', '--refresh-cache', '--refs', '--remediation-plan', '--repo', '--restart-only', '--resume', '--retry-global', '--slot-a-model', '--slot-b-model', '--slot-c-model', '--source', '--stale', '--status', '--stop-mcp', '--swap-only', '--tags', '--target', '--trust', '--uninstall', '--user', '--verify', '--version', '--wait', '--yes'],'backfill': ['--batch-size', '--brain', '--concurrency', '--dry-run', '--fresh', '--help', '--json', '--keep-index', '--list', '--max-errors', '--max-rows', '--resume', '--source'],'backup': ['--accept-pending', '--as', '--background', '--brain', '--confirm-authority-reviewed', '--confirm-backup-compatible', '--confirm-quiesced', '--dir', '--explain', '--follow', '--full', '--help', '--into', '--json', '--mode', '--no-accept-pending', '--output', '--path', '--progress-interval', '--progress-json', '--purge-mirror', '--quiet', '--restore-only', '--source', '--timeout', '--timings', '--wait'],'beliefs': ['--apply', '--archived-at', '--brain', '--from-url-env', '--help', '--json', '--probe', '--purge-mirror', '--since', '--source'],diff --git a/src/core/git/reconcile.ts b/src/core/git/reconcile.tsnew file mode 100644index 0000000000..4a68e53fd4--- /dev/null+++ b/src/core/git/reconcile.ts@@ -0,0 +1,103 @@+/**+ * One checkout brought level with its upstream, the engine-free half of `gbrain sources git-reconcile` (the managed half+ * runs `gbrain sources refresh`). Idempotent: a checkout already level costs one fetch and reports in_sync. Behind:+ * the shared pull (fast-forward, never a rebase). Ahead or diverged: the upstream-rewrite check, then the shared+ * publish with the secret gate (merged in memory, never a rebase or force-push, refusing on a conflict). Concurrent+ * callers serialize on the checkout's commit and publish locks and end level with the same upstream.+ */+import { execFileBounded } from '../bounded-child-exec.ts';+import { classifyGitCheckout } from '../git-checkout.ts';+import { gitChildEnv } from '../git-env.ts';+import { opError } from '../ops/contract.ts';+import { acquireNativeLock } from '../persistence/native-lock.ts';+import { commitAndPush, pullShared } from './commit-push.ts';+import { upstreamBeforeFetch, upstreamRewriteReason, type GitRead } from './upstream-history.ts';++export type CheckoutReconcileStatus = 'in_sync' | 'published' | 'fast_forwarded' | 'lagging' | 'unpublished' | 'no_repo' | 'no_upstream';++export interface CheckoutLevel { branch: string; upstream: string; head: string; target: string; ahead: number; behind: number }++export interface CheckoutReconcile {+ root: string; status: CheckoutReconcileStatus; ahead: number; behind: number;+ /** The upstream tip after the run (what the remote holds), when there is an upstream. */+ sha?: string; integrated?: number; reason?: string;+}++export function gitReader(root: string, timeoutMs = 120_000): GitRead {+ return async (args, ...expected) => {+ const out = await execFileBounded('git', ['-C', root, ...args], { timeout: timeoutMs, env: gitChildEnv({ GIT_TERMINAL_PROMPT: '0' }), maxBuffer: 64 * 1024 * 1024 });+ const code = out.error ? (typeof out.error.code === 'number' ? out.error.code : -1) : 0;+ if (code === 0 || expected.includes(code)) return { stdout: String(out.stdout) };+ throw opError('git_unavailable', `git ${args.join(' ')} failed in ${root}: ${String(out.stderr).trim().split('\n')[0] || `exit ${code}`}`,+ `Run git -C ${root} status, fix what git reports, then retry.`);+ };+}++/** Fetch the branch's upstream (logging the tracking ref's move, so a later rewrite check has a record) and count both sides; null without an upstream. */+export async function fetchLevel(root: string, read: GitRead, fetchTimeoutMs = 120_000): Promise<CheckoutLevel | null> {+ const branch = (await read(['symbolic-ref', '--quiet', '--short', 'HEAD'], 1)).stdout.trim();+ if (!branch) return null;+ const upstream = (await read(['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'], 128)).stdout.trim();+ if (!upstream) return null;+ const remote = (await read(['config', '--get', `branch.${branch}.remote`], 1)).stdout.trim();+ const merge = (await read(['config', '--get', `branch.${branch}.merge`], 1)).stdout.trim();+ if (remote && remote !== '.') {+ let fetched = await execFileBounded('git', ['-C', root, '-c', 'core.logAllRefUpdates=true', 'fetch', '--no-tags', '--quiet', remote, merge || branch],+ { timeout: fetchTimeoutMs, env: gitChildEnv({ GIT_TERMINAL_PROMPT: '0' }) });+ for (let attempt = 1; fetched.error && attempt < 4 && /cannot lock ref|unable to update local ref/.test(String(fetched.stderr)); attempt++) {+ await new Promise(resolve => setTimeout(resolve, 250 * attempt));+ fetched = await execFileBounded('git', ['-C', root, '-c', 'core.logAllRefUpdates=true', 'fetch', '--no-tags', '--quiet', remote, merge || branch],+ { timeout: fetchTimeoutMs, env: gitChildEnv({ GIT_TERMINAL_PROMPT: '0' }) });+ }+ if (fetched.error) throw opError('fetch_failed', `git fetch ${remote} failed in ${root} (${String(fetched.stderr).trim().split('\n')[0] || fetched.error.message}); nothing changed.`,+ 'Check the remote and its credentials, then retry.', { retryable: true });+ }+ const head = (await read(['rev-parse', 'HEAD'])).stdout.trim();+ const target = (await read(['rev-parse', '@{upstream}'])).stdout.trim();+ const [behind, ahead] = (await read(['rev-list', '--left-right', '--count', `${target}...${head}`])).stdout.trim().split(/\s+/).map(Number);+ return { branch, upstream, head, target, ahead, behind };+}++/** One reconcile of a checkout at a time: concurrent callers wait (up to `waitMs`), then find it level. Only reconcile takes this lock, and it takes it outermost, so it never waits on a lock held by a publish that waits on it. */+export async function withReconcileLock<T>(root: string, waitMs: number, fn: () => Promise<T>): Promise<T> {+ const path = (await gitReader(root)(['rev-parse', '--path-format=absolute', '--git-path', 'gbrain-reconcile.lock'])).stdout.trim();+ const lock = await acquireNativeLock(path, { timeoutMs: waitMs, pollMs: 100 });+ if (!lock) throw opError('commit_lock_busy', `Another reconcile of ${root} held its lock for ${Math.round(waitMs / 1000)}s; nothing changed.`,+ 'Retry: the running reconcile brings the checkout level, after which a new one reports in_sync.', { retryable: true });+ try { return await fn(); } finally { await lock.release(); }+}++export interface ReconcileCheckoutOptions {+ fetchTimeoutMs?: number; dryRun?: boolean; waitMs?: number;+ /** False leaves local commits where they are and reports them unpublished (the user turned automatic publishing off); behind still fast-forwards. */+ publish?: boolean;+}++export async function reconcileCheckout(root: string, opts: ReconcileCheckoutOptions = {}): Promise<CheckoutReconcile> {+ if (classifyGitCheckout(root) === 'not_git') return { root, status: 'no_repo', ahead: 0, behind: 0 };+ return withReconcileLock(root, opts.waitMs ?? 300_000, () => reconcileLocked(root, opts));+}++async function reconcileLocked(root: string, opts: ReconcileCheckoutOptions): Promise<CheckoutReconcile> {+ const read = gitReader(root);+ const hasUpstream = (await read(['rev-parse', '--verify', '-q', '@{upstream}'], 1, 128)).stdout.trim();+ if (!hasUpstream) return { root, status: 'no_upstream', ahead: 0, behind: 0 };+ const before = await upstreamBeforeFetch(read);+ const level = await fetchLevel(root, read, opts.fetchTimeoutMs);+ if (!level) return { root, status: 'no_upstream', ahead: 0, behind: 0 };+ const { ahead, behind, target, head, upstream } = level;+ if (!ahead && !behind) return { root, status: 'in_sync', ahead, behind, sha: target };+ if (ahead && opts.publish === false) return { root, status: 'unpublished', ahead, behind, sha: target, reason: 'automatic publishing is off (sources.refresh_publish_diverged false)' };+ if (opts.dryRun) return { root, status: ahead ? 'published' : 'fast_forwarded', ahead, behind, sha: target, reason: 'dry run: nothing changed' };+ if (!ahead) {+ const pulled = pullShared(root);+ return pulled.status === 'lagging' ? { root, status: 'lagging', ahead, behind, sha: target, ...(pulled.reason ? { reason: pulled.reason } : {}) }+ : { root, status: 'fast_forwarded', ahead, behind, sha: pulled.to ?? target };+ }+ const rewrite = await upstreamRewriteReason(read, before, upstream, target, head);+ if (rewrite) throw opError('upstream_rewritten', `${rewrite} Nothing was pushed from ${root}.`,+ `Ask the user: git -C ${root} log --stat ${upstream}..HEAD lists the local commits; publishing them anyway is gbrain git commit-push --push-only --scan-secrets --path ${root}.`);+ const pushed = commitAndPush({ repo: root, cwd: root, paths: [], pushOnly: true, scanSecrets: true });+ return { root, status: pushed.checkout === 'lagging' ? 'lagging' : 'published', ahead, behind, sha: pushed.sha ?? undefined, integrated: pushed.integrated,+ ...(pushed.lagging_reason ? { reason: pushed.lagging_reason } : {}) };+}diff --git a/src/core/git/upstream-history.ts b/src/core/git/upstream-history.tsnew file mode 100644index 0000000000..47006af74d--- /dev/null+++ b/src/core/git/upstream-history.ts@@ -0,0 +1,48 @@+/**+ * Has the upstream been rewritten under this checkout? An automatic publish merges the remote into the local commits+ * and pushes; after a force-push that removed a commit (to purge a page or a secret) the local branch may still hold+ * it, and the merge would bring it back. Before the fetch, upstreamBeforeFetch collects every commit the upstream is+ * recorded at (its reflog, the reflog file's first and last lines, and any record the caller adds); after the fetch,+ * upstreamRewriteReason names a local commit the upstream held then and lacks now, or says there is no record at all.+ * The fetch in between must log ref updates (`-c core.logAllRefUpdates=true`), so the next run has a record.+ */+import { closeSync, fstatSync, openSync, readSync } from 'node:fs';++/** Runs git in the checkout; a non-zero exit outside `expected` throws. */+export type GitRead = (args: string[], ...expected: number[]) => Promise<{ stdout: string }>;++export interface UpstreamRecord { held: string[]; recorded: boolean }++const isSha = (sha: string) => /^[0-9a-f]{40,64}$/.test(sha) && !/^0+$/.test(sha);+const lines = (out: string) => out.split('\n').map(sha => sha.trim());++export async function upstreamBeforeFetch(read: GitRead, alsoRecorded: readonly string[] = []): Promise<UpstreamRecord> {+ const upstreamFull = (await read(['rev-parse', '--symbolic-full-name', '@{upstream}'])).stdout.trim();+ const logged = lines((await read(['log', '-g', '-n', '1000', '--format=%H', upstreamFull, '--'], 128)).stdout);+ let reflog = '';+ try {+ const fd = openSync((await read(['rev-parse', '--path-format=absolute', '--git-path', `logs/${upstreamFull}`])).stdout.trim(), 'r');+ try {+ const size = fstatSync(fd).size;+ const first = Buffer.alloc(Math.min(size, 512));+ const tail = Buffer.alloc(Math.min(size, 262_144));+ readSync(fd, first, 0, first.length, 0);+ readSync(fd, tail, 0, tail.length, size - tail.length);+ reflog = `${first.toString('utf8').split('\n')[0]}\n${tail.toString('utf8')}`;+ } finally { closeSync(fd); }+ } catch { /* no reflog file: a reftable repository, or no reflog */ }+ const columns = reflog.split('\n').flatMap(line => line.split(' ').slice(0, 2));+ const recorded = [...logged, ...columns, ...alsoRecorded].filter(isSha);+ const preFetch = (await read(['rev-parse', '--verify', '-q', '@{upstream}^{commit}'], 1)).stdout.trim();+ const unlogged = ['false', 'no', 'off', '0'].includes((await read(['config', '--get', 'core.logAllRefUpdates'], 1)).stdout.trim().toLowerCase());+ return { held: [...new Set([preFetch, ...recorded].filter(isSha))], recorded: recorded.length > 0 || (isSha(preFetch) && !unlogged) };+}++export async function upstreamRewriteReason(read: GitRead, before: UpstreamRecord, upstreamRef: string, targetHead: string, oldHead: string): Promise<string | null> {+ const tips = before.held.filter(sha => sha !== targetHead);+ const held = new Set(tips.length ? lines((await read(['rev-list', '--ignore-missing', `^${targetHead}`, ...tips])).stdout) : []);+ const removed = lines((await read(['rev-list', `${targetHead}..${oldHead}`])).stdout).find(sha => sha && held.has(sha));+ if (removed) return `${upstreamRef} no longer contains ${removed}, which it held before: its history was rewritten (a force-push), and publishing would merge back the commits the rewrite removed.`;+ if (!before.recorded) return `gbrain has no record of what ${upstreamRef} held before this fetch (no reflog entry and no completed refresh), so it cannot tell a force-push from local commits.`;+ return null;+}diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex 4f83509688..7144b4f452 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -50,6 +50,7 @@ import { classifyPgAccessError } from '../pg-access-classify.ts';import { classifyOriginUniqueViolation } from './origin-guard.ts';import { refreshProgress, refreshRetryHint, seconds } from './refresh-retry-hint.ts';import { ledgerLocation, readLedger } from '../git/commit-ledger.ts';+import { upstreamBeforeFetch, upstreamRewriteReason } from '../git/upstream-history.ts';import { gitDurabilityPolicy } from './git-durability-policy.ts';export interface WorktreeRefreshRow {@@ -444,32 +445,9 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTiconst upstreamRef = upstream.stdout.trim();const remote = (await read(['config', '--get', `branch.${branch}.remote`], 1)).stdout.trim();const merge = (await read(['config', '--get', `branch.${branch}.merge`], 1)).stdout.trim();- const heldUpstream = new Set<string>();- let upstreamRecorded = false;- if (publishDiverged) {- const isSha = (sha: string) => /^[0-9a-f]{40,64}$/.test(sha) && !/^0+$/.test(sha);- const upstreamFull = (await read(['rev-parse', '--symbolic-full-name', '@{upstream}'])).stdout.trim();- const logged = (await read(['log', '-g', '-n', '1000', '--format=%H', upstreamFull, '--'], 128)).stdout.split('\n').map(sha => sha.trim());- let reflog = '';- try {- const fd = openSync((await read(['rev-parse', '--path-format=absolute', '--git-path', `logs/${upstreamFull}`])).stdout.trim(), 'r');- try {- const size = fstatSync(fd).size;- const first = Buffer.alloc(Math.min(size, 512));- const tail = Buffer.alloc(Math.min(size, 262_144));- readSync(fd, first, 0, first.length, 0);- readSync(fd, tail, 0, tail.length, size - tail.length);- reflog = `${first.toString('utf8').split('\n')[0]}\n${tail.toString('utf8')}`;- } finally { closeSync(fd); }- } catch { /* no reflog file: a reftable repository, or no reflog */ }- const columns = reflog.split('\n').flatMap(line => line.split(' ').slice(0, 2));- const rows = await engine.executeRaw<{ target_head: string }>(`SELECT target_head FROM persistence_worktree_refreshes WHERE worktree_id=$1::uuid AND upstream_ref=$2 AND state='completed' ORDER BY created_at DESC LIMIT 100`, [binding.worktree_id, upstreamRef]);- const recorded = [...logged, ...columns, ...rows.map(r => r.target_head)].filter(isSha);- const preFetch = (await read(['rev-parse', '--verify', '-q', '@{upstream}^{commit}'], 1)).stdout.trim();- const unlogged = ['false', 'no', 'off', '0'].includes((await read(['config', '--get', 'core.logAllRefUpdates'], 1)).stdout.trim().toLowerCase());- upstreamRecorded = recorded.length > 0 || (isSha(preFetch) && !unlogged);- for (const sha of [preFetch, ...recorded]) if (isSha(sha)) heldUpstream.add(sha);- }+ const before = publishDiverged ? await upstreamBeforeFetch(read, (await engine.executeRaw<{ target_head: string }>(+ `SELECT target_head FROM persistence_worktree_refreshes WHERE worktree_id=$1::uuid AND upstream_ref=$2 AND state='completed' ORDER BY created_at DESC LIMIT 100`,+ [binding.worktree_id, upstreamRef])).map(r => r.target_head)) : null;if (remote && remote !== '.') {const fetched = await git(['-C', root, '-c', 'core.logAllRefUpdates=true', 'fetch', '--no-tags', '--quiet', remote, merge || branch], fetchTimeoutMs);if (fetched.code !== 0) throw refusal('fetch_failed',@@ -483,12 +461,8 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTi`git -C ${q(root)} fetch ${remote || 'origin'}, then gbrain sources refresh ${sourceId}`);if (oldHead !== targetHead && !await isAncestor(git, root, oldHead, targetHead)) {if (!publishDiverged) throw await divergedRefusal(git, root, sourceId, upstreamRef, oldHead, targetHead);- const tips = [...heldUpstream].filter(sha => sha !== targetHead);- const held = new Set(tips.length ? (await read(['rev-list', '--ignore-missing', `^${targetHead}`, ...tips])).stdout.split('\n').map(sha => sha.trim()) : []);- const removed = (await read(['rev-list', `${targetHead}..${oldHead}`])).stdout.split('\n').map(sha => sha.trim()).find(sha => sha && held.has(sha));- if (!upstreamRecorded || removed) throw await divergedRefusal(git, root, sourceId, upstreamRef, oldHead, targetHead, removed- ? `${upstreamRef} no longer contains ${removed}, which it held before: its history was rewritten (a force-push), and publishing would merge back the commits the rewrite removed.`- : `gbrain has no record of what ${upstreamRef} held before this fetch (no reflog entry and no completed refresh), so it cannot tell a force-push from local commits.`);+ const rewrite = await upstreamRewriteReason(read, before!, upstreamRef, targetHead, oldHead);+ if (rewrite) throw await divergedRefusal(git, root, sourceId, upstreamRef, oldHead, targetHead, rewrite);const base = (await read(['merge-base', oldHead, targetHead], 1)).stdout.trim() || oldHead;const incoming = await dirtyOverlap(git, root, base, targetHead);if (incoming.overlap.length) throw dirtyRefusal(sourceId, incoming.overlap);diff --git a/test/autopilot-git-reconcile.test.ts b/test/autopilot-git-reconcile.test.tsnew file mode 100644index 0000000000..27cfb331ca--- /dev/null+++ b/test/autopilot-git-reconcile.test.ts@@ -0,0 +1,50 @@+/**+ * Autopilot reconciles Git checkouts in a bounded child: the daemon's event loop keeps running (lease renewals,+ * heartbeat) while the child fetches and pushes, a refusal comes back as a log line, and a hung child is killed.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { reconcileGitInChild } from '../src/commands/autopilot-git-reconcile.ts';+import { withEnv } from './helpers/with-env.ts';++let dir = '';+beforeAll(() => { dir = mkdtempSync(join(tmpdir(), 'autopilot-git-reconcile-')); });+afterAll(() => { rmSync(dir, { recursive: true, force: true }); });++function fakeCli(name: string, body: string): string {+ const path = join(dir, name);+ writeFileSync(path, `#!/bin/sh\n${body}\n`);+ chmodSync(path, 0o755);+ return path;+}++test('the event loop keeps ticking while the reconcile child runs, and a publish is logged', async () => {+ const cli = fakeCli('slow.sh', `[ "$1 $2 $3" = "sources git-reconcile --json" ] || exit 9+sleep 2+echo '{"results":[{"sources":["source-a"],"root":"/r","status":"published"},{"sources":["source-b"],"root":"/s","status":"in_sync"}],"exit":0}'`);+ let ticks = 0;+ const timer = setInterval(() => { ticks++; }, 50);+ const started = Date.now();+ const outcome = await withEnv({ GBRAIN_JOB_CHILD_CLI: cli }, () => reconcileGitInChild()).finally(() => clearInterval(timer));+ expect(Date.now() - started).toBeGreaterThanOrEqual(1900);+ expect(ticks).toBeGreaterThanOrEqual(25);+ expect(outcome).toEqual({ ok: true, exit: 0, lines: ['source-a: published'] });+}, 30_000);++test('a refusal that needs the user is reported, not thrown', async () => {+ const cli = fakeCli('refuse.sh', `echo '{"results":[{"sources":["source-a"],"root":"/r","status":"needs_user","error":{"code":"publish_secret_found","message":"a secret"}}],"exit":1}'+exit 1`);+ expect(await withEnv({ GBRAIN_JOB_CHILD_CLI: cli }, () => reconcileGitInChild()))+ .toEqual({ ok: false, exit: 1, lines: ['source-a: needs_user publish_secret_found: a secret'] });+}, 30_000);++test('a child that hangs past the bound is killed and reported', async () => {+ const cli = fakeCli('hang.sh', 'exec sleep 30');+ const started = Date.now();+ const outcome = await withEnv({ GBRAIN_JOB_CHILD_CLI: cli }, () => reconcileGitInChild(500));+ expect(Date.now() - started).toBeLessThan(15_000);+ expect(outcome.ok).toBe(false);+ expect(outcome.lines[0]).toStartWith('sources git-reconcile ');+}, 30_000);diff --git a/test/sources-git-reconcile.test.ts b/test/sources-git-reconcile.test.tsnew file mode 100644index 0000000000..d05ac532a8--- /dev/null+++ b/test/sources-git-reconcile.test.ts@@ -0,0 +1,212 @@+/**+ * `gbrain sources git-reconcile`: one idempotent call brings every source checkout level with its upstream.+ * Protects: a stranded local commit is published (merged in memory, the old remote tip kept as an ancestor, never a+ * force-push); a checkout behind fast-forwards; one level is a no-op that pushes nothing; a same-file conflict, a+ * secret in the local commits and a rewritten upstream each refuse with nothing pushed; eight concurrent callers+ * while another clone pushes mid-run all succeed and lose no commit, and a ninth call reports in_sync; a managed+ * worktree is reconciled through refresh (Git durability on publishes, off needs the user); a brain whose sources+ * have no Git checkout reports no_repo.+ * Seams: GBRAIN_JOB_CHILD_CLI for refresh's publish child; otherwise real repositories, a bare remote, a pushing+ * clone, child processes for the concurrent callers, and PGLite (Postgres too where configured) for the managed case.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { execFileSync, spawn } from 'node:child_process';+import { chmodSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { reconcileCheckout } from '../src/core/git/reconcile.ts';+import { reconcileSources } from '../src/commands/sources-git-reconcile.ts';+import { runSources } from '../src/commands/sources.ts';+import { REFRESH_PUBLISH_DIVERGED_KEY } from '../src/core/persistence/worktree-refresh.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';+import { page, withRefreshFixture } from './helpers/worktree-refresh-fixture.ts';++const dir = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-reconcile-')));+const identity = { GIT_AUTHOR_NAME: 'Example', GIT_AUTHOR_EMAIL: '[email protected]', GIT_COMMITTER_NAME: 'Example', GIT_COMMITTER_EMAIL: '[email protected]' };+const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, '-c', 'user.name=Example', '-c', '[email protected]',+ '-c', 'commit.gpgsign=false', ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();+const backends = testBackends();+const engines: BrainEngine[] = [];+let closePostgres: (() => Promise<void>) | undefined;+const childCli = join(dir, 'bin', 'gbrain');++beforeAll(async () => {+ mkdirSync(join(dir, 'bin'));+ writeFileSync(childCli, `#!/bin/sh\nexec '${process.execPath}' '${join(import.meta.dir, '..', 'src', 'cli.ts')}' "$@"\n`);+ chmodSync(childCli, 0o755);+ if (backends.includes('pglite')) { const engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); engines.push(engine); }+ if (backends.includes('postgres')) { const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!); engines.push(pg.engine); closePostgres = pg.close; }+}, 120_000);+afterAll(async () => {+ for (const engine of engines) if (engine.kind === 'pglite') await engine.disconnect();+ await closePostgres?.();+ rmSync(dir, { recursive: true, force: true });+});++interface Repo { remote: string; root: string; sibling: string }+function repo(name: string): Repo {+ const remote = join(dir, `${name}.git`), root = join(dir, name), sibling = join(dir, `${name}-sibling`);+ execFileSync('git', ['init', '-q', '--bare', '-b', 'main', remote]);+ execFileSync('git', ['clone', '-q', remote, root], { stdio: 'ignore' });+ git(root, 'checkout', '-q', '-b', 'main');+ writeFileSync(join(root, 'readme.md'), 'start\n');+ git(root, 'add', '.'); git(root, 'commit', '-qm', 'init'); git(root, 'push', '-q', '-u', 'origin', 'main');+ execFileSync('git', ['clone', '-q', '-b', 'main', remote, sibling], { stdio: 'ignore' });+ return { remote, root, sibling };+}+const commit = (root: string, file: string, content: string) => {+ mkdirSync(join(root, file, '..'), { recursive: true });+ writeFileSync(join(root, file), content); git(root, 'add', file); git(root, 'commit', '-qm', `edit ${file}`);+ return git(root, 'rev-parse', 'HEAD');+};+const siblingPush = (r: Repo, file: string, content: string) => { git(r.sibling, 'pull', '-q', '--no-rebase', 'origin', 'main'); const sha = commit(r.sibling, file, content); git(r.sibling, 'push', '-q', 'origin', 'main'); return sha; };+const tip = (r: Repo) => git(r.remote, 'rev-parse', 'refs/heads/main');+const onRemote = (r: Repo, sha: string) => { try { git(r.remote, 'merge-base', '--is-ancestor', sha, 'refs/heads/main'); return true; } catch { return false; } };+const refusal = async (p: Promise<unknown>) => p.then(() => { throw new Error('expected a refusal'); }, e => e as { code: string; message: string });+const reconcile = (root: string) => withEnv(identity, () => reconcileCheckout(root));++test('no Git checkout and no upstream report themselves and change nothing', async () => {+ const plain = join(dir, 'plain'); mkdirSync(plain);+ expect(await reconcileCheckout(plain)).toMatchObject({ status: 'no_repo' });+ const local = join(dir, 'local'); execFileSync('git', ['init', '-q', '-b', 'main', local]); commit(local, 'a.md', 'a\n');+ expect(await reconcileCheckout(local)).toMatchObject({ status: 'no_upstream' });+});++test('a level checkout is a no-op; a stranded commit is published; a checkout behind fast-forwards', async () => {+ const r = repo('level');+ expect(await reconcile(r.root)).toMatchObject({ status: 'in_sync', ahead: 0, behind: 0, sha: tip(r) });+ const local = commit(r.root, 'notes/a.md', 'stranded\n');+ expect(await reconcile(r.root)).toMatchObject({ status: 'published', ahead: 1, behind: 0 });+ expect(tip(r)).toBe(local);+ expect(await reconcile(r.root)).toMatchObject({ status: 'in_sync' });+ const theirs = siblingPush(r, 'notes/b.md', 'from the sibling\n');+ expect(await reconcile(r.root)).toMatchObject({ status: 'fast_forwarded', ahead: 0, behind: 1 });+ expect(git(r.root, 'rev-parse', 'HEAD')).toBe(theirs);+}, 60_000);++test('a diverged checkout publishes a merge that keeps both sides and the old remote tip; a same-file conflict pushes nothing', async () => {+ const r = repo('diverged');+ const theirs = siblingPush(r, 'notes/b.md', 'from the sibling\n');+ const ours = commit(r.root, 'notes/a.md', 'local\n');+ const result = await reconcile(r.root);+ expect(result).toMatchObject({ status: 'published', ahead: 1, behind: 1, integrated: 1 });+ expect(onRemote(r, ours) && onRemote(r, theirs)).toBe(true);+ expect(git(r.root, 'rev-parse', 'HEAD')).toBe(tip(r));++ siblingPush(r, 'readme.md', 'sibling version\n');+ commit(r.root, 'readme.md', 'local version\n');+ const before = tip(r);+ expect((await refusal(reconcile(r.root))).code).toBe('integrate_conflict');+ expect(tip(r)).toBe(before);+}, 60_000);++test('a secret in the local commits and a rewritten upstream each refuse with nothing pushed', async () => {+ const r = repo('guards');+ commit(r.root, 'notes/key.md', `token ${'ghp_' + 'Zt4q'.repeat(9)}\n`);+ const before = tip(r);+ expect((await refusal(reconcile(r.root))).code).toBe('publish_secret_found');+ expect(tip(r)).toBe(before);++ const w = repo('rewrite');+ const purged = commit(w.root, 'notes/purge-me.md', 'published, then purged\n');+ git(w.root, 'push', '-q', 'origin', 'main');+ git(w.sibling, 'fetch', '-q', 'origin'); git(w.sibling, 'reset', '-q', '--hard', `${purged}~1`);+ commit(w.sibling, 'notes/other.md', 'rewritten history\n'); git(w.sibling, 'push', '-q', '--force', 'origin', 'main');+ const rewritten = tip(w);+ commit(w.root, 'notes/local.md', 'local\n');+ const error = await refusal(reconcile(w.root));+ expect(error.code).toBe('upstream_rewritten');+ expect(error.message).toContain(purged);+ expect(tip(w)).toBe(rewritten);+}, 60_000);++test('eight concurrent callers while another clone pushes mid-run: all succeed, no commit is lost, and a ninth call is in_sync', async () => {+ const r = repo('concurrent');+ const start = tip(r);+ const ours = [commit(r.root, 'notes/l1.md', 'one\n'), commit(r.root, 'notes/l2.md', 'two\n')];+ const script = join(dir, 'reconcile-child.ts');+ writeFileSync(script, `import { reconcileCheckout } from '${join(import.meta.dir, '..', 'src', 'core', 'git', 'reconcile.ts')}';\n`+ + `try { console.log(JSON.stringify(await reconcileCheckout(process.argv[2]))); } catch (e) { console.log(JSON.stringify({ code: (e as { code?: string }).code, message: String((e as Error).message) })); process.exit(1); }\n`);+ const run = () => new Promise<{ code: number | null; out: string }>(resolve => {+ const child = spawn(process.execPath, [script, r.root], { env: { ...process.env, ...identity }, stdio: ['ignore', 'pipe', 'pipe'] });+ let out = ''; child.stdout.on('data', d => { out += d; });+ child.on('close', code => resolve({ code, out }));+ });+ const callers = Array.from({ length: 8 }, run);+ const theirs = siblingPush(r, 'notes/sibling.md', 'pushed mid-run\n');+ const results = await Promise.all(callers);+ for (const result of results) expect(result.code, result.out).toBe(0);+ const statuses = results.map(result => (JSON.parse(result.out) as { status: string }).status);+ expect(statuses.every(s => ['published', 'in_sync', 'fast_forwarded'].includes(s))).toBe(true);+ for (const sha of [start, ...ours]) expect(onRemote(r, sha)).toBe(true);+ const last = await reconcile(r.root);+ if (last.status !== 'in_sync') expect(last.status).toBe('published');+ expect(onRemote(r, theirs)).toBe(true);+ expect(await reconcile(r.root)).toMatchObject({ status: 'in_sync', sha: tip(r) });+ expect(git(r.root, 'rev-parse', 'HEAD')).toBe(tip(r));+}, 180_000);++test('managed worktree: Git durability on publishes through refresh and a repeat is in_sync; durability off needs the user', () => (async () => {+ for (const engine of engines) {+ await withEnv({ GBRAIN_JOB_CHILD_CLI: childCli }, () => withRefreshFixture(engine, async f => {+ f.push('alpha/two.md', page('Alpha two', 'Upstream side.'));+ git(f.root, 'config', 'user.name', 'Example'); git(f.root, 'config', 'user.email', '[email protected]');+ const local = commit(f.root, 'beta/local.md', page('Local', 'A local commit nobody pushed.'));+ const off = await reconcileSources(engine, { sourceIds: [f.alpha, f.beta] });+ expect(off).toHaveLength(1);+ expect(off[0]).toMatchObject({ managed: true, status: 'needs_user', error: { code: 'refresh_diverged' } });+ expect(off[0].sources.sort()).toEqual([f.alpha, f.beta].sort());+ await engine.executeRaw(`UPDATE persistence_host_bindings SET git_durability='enabled' WHERE local_path LIKE $1`, [`${f.root}%`]);+ try {+ const [done] = await reconcileSources(engine, { sourceIds: [f.alpha, f.beta] });+ expect(done).toMatchObject({ managed: true, status: 'published', ahead: 1, behind: 1 });+ git(f.root, 'merge-base', '--is-ancestor', local, 'HEAD');+ expect(f.head()).toBe(git(f.root, 'ls-remote', 'origin', 'refs/heads/main').split('\t')[0]);+ expect(await reconcileSources(engine, { sourceIds: [f.alpha, f.beta] })).toMatchObject([{ status: 'in_sync' }]);+ } finally {+ await engine.executeRaw(`UPDATE persistence_host_bindings SET git_durability=NULL WHERE local_path LIKE $1`, [`${f.root}%`]);+ await engine.unsetConfig(REFRESH_PUBLISH_DIVERGED_KEY);+ }+ }));+ }+})(), 240_000);++test('a brain whose sources have no Git checkout reports no_repo', async () => {+ const engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema();+ try {+ const path = join(dir, 'plain-source');+ mkdirSync(path, { recursive: true });+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', ['plain-source', path]);+ expect(await reconcileSources(engine, { sourceIds: ['plain-source'] })).toEqual([{ sources: [], root: '', managed: false, status: 'no_repo', ahead: 0, behind: 0 }]);+ expect(await reconcileSources(engine)).toEqual([{ sources: [], root: '', managed: false, status: 'no_repo', ahead: 0, behind: 0 }]);+ const printed: string[] = [];+ const log = console.log;+ console.log = (...args: unknown[]) => { printed.push(args.join(' ')); };+ try { await runSources(engine, ['git-reconcile', 'plain-source', '--json']); } finally { console.log = log; }+ expect(JSON.parse(printed.join('\n'))).toEqual({ results: [{ sources: [], root: '', managed: false, status: 'no_repo', ahead: 0, behind: 0 }], exit: 0 });+ } finally { await engine.disconnect(); }+}, 60_000);++test('with automatic publishing turned off, local commits stay unpublished while a checkout behind still fast-forwards', async () => {+ const engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema();+ try {+ const r = repo('optout');+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', ['optout-source', r.root]);+ await engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'false');+ const theirs = siblingPush(r, 'notes/theirs.md', 'from the sibling\n');+ expect(await withEnv(identity, () => reconcileSources(engine))).toMatchObject([{ sources: ['optout-source'], status: 'fast_forwarded', behind: 1 }]);+ expect(git(r.root, 'rev-parse', 'HEAD')).toBe(theirs);+ const local = commit(r.root, 'notes/mine.md', 'mine\n');+ expect(await withEnv(identity, () => reconcileSources(engine))).toMatchObject([{ sources: ['optout-source'], status: 'unpublished', ahead: 1, behind: 0 }]);+ expect(tip(r)).toBe(theirs);+ await engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, '');+ expect(await withEnv(identity, () => reconcileSources(engine))).toMatchObject([{ sources: ['optout-source'], status: 'published', ahead: 1 }]);+ expect(tip(r)).toBe(local);+ } finally { await engine.disconnect(); }+}, 60_000);