Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(refresh): log every refresh fetch so a refusal never erases what the upstream held (#7686)
garrytan118db5ec0e01
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 86d89a79b0..e05d2335ad 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,47 @@ credits are retained; no result has been reassigned to another provider. Originaidentifiers and attribution are available in the pre-removal Git revision`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).+## [Unreleased]++**A managed checkout that gets local commits while a sibling checkout pushes the same branch can now stay current without a person in the loop. Turn on `sources.refresh_publish_diverged` and a diverged `gbrain sources refresh` publishes the local commits merged with the upstream, then fast-forwards.**++Before, such a refresh always refused `refresh_diverged`, and the database lagged the upstream until someone ran the keep-checkout publish by hand or a scheduled publish caught up. That repeated every interval. The setting is the user's standing consent to push those local commits. It defaults to off, so nothing changes for existing brains.++### How to get it++```bash+gbrain config set sources.refresh_publish_diverged true+```++### What changed++| Situation | Before | Now |+|---|---|---|+| Diverged checkout, setting on | refused `refresh_diverged` | pushes HEAD merged with the upstream in memory (the `gbrain git commit-push --push-only --keep-checkout` publish), fast-forwards to the merge and reports `published` (sha, local commits, upstream commits merged) |+| Diverged checkout, setting off (the default) | refused `refresh_diverged` | unchanged; the refusal now also names the setting |+| A file both sides changed, setting on | n/a | refuses `integrate_conflict` naming the file; nothing is pushed, fenced or moved. With `gbrain.quarantine` on, the remote's version is published instead and the refresh names the ref that keeps this machine's version |+| An uncommitted edit to a file the upstream changed, setting on | n/a | refuses `refresh_dirty` before anything is pushed |+| An upstream rewritten by a force-push (it no longer holds a commit it held before), or no record of the upstream before the fetch, setting on | n/a | refuses `refresh_diverged` and pushes nothing, so the commits the rewrite removed never come back without the user |+| A refusal after the publish landed | n/a | the refusal says the remote already holds the local commits |+| `--dry-run` | refused `refresh_diverged` | unchanged: a dry run never publishes |+| `gbrain config set sources.refresh_fetch_timeout_ms` / `sources.refresh_drain_wait_ms` | refused as an unknown key, although the refresh reads both | accepted |++### Things to watch after you upgrade++- The publish needs a Git identity in the checkout for the merge commit, the same as the manual keep-checkout publish.+- It never uses a `--pull` that moves the checkout. The merge is built in memory and pushed, and the checkout moves only through the refresh's usual fast-forward.+- The push skips the checkout's pre-push hook, as the manual keep-checkout publish does. If you rely on a pre-push secret scanner, leave the setting off.+- The setting covers every managed checkout in the brain.+- If another publish of the same checkout is running, the refresh refuses at once instead of waiting; run it again.++### Itemized changes++- `src/core/persistence/worktree-refresh.ts`: when `sources.refresh_publish_diverged` is on and the run is not a dry run, the precheck runs the keep-checkout publish (`gbrain git commit-push --push-only --keep-checkout` in a bounded child, never on the PGLite owner's event loop) for a diverged checkout (after refusing `refresh_dirty` first when an uncommitted edit touches a file the upstream changed), re-fetches and continues; it refuses `refresh_diverged` instead when the checkout holds a commit the upstream held before (reachable from the remote-tracking ref before this fetch, from its reflog, or from a completed refresh's target for the same upstream) but no longer contains, or when no record exists (no reflog entry, no completed refresh, and `core.logAllRefUpdates` off, so the ref before the fetch may already be past a force-push); every refresh fetch, with the setting on or off and in a dry run, records the move in the reflog, so a refusal's own fetch can never erase what the upstream held; the result carries `published` (with any quarantined or superseded paths, which the text output names), and a refusal after the publish says the remote already holds the local commits. The `refresh_diverged` suggestion names the setting.+- `src/commands/sources-refresh.ts`: a text line for the publish.+- `src/core/config-runtime-keys.ts`: registers `sources.refresh_publish_diverged`, `sources.refresh_fetch_timeout_ms` and `sources.refresh_drain_wait_ms`.+- Docs: `docs/guides/live-sync.md`, `docs/guides/write-refusals.md`, `docs/guides/troubleshooting.md`, `docs/guides/shared-checkout-git.md`, `docs/architecture/canonical-writers.md` and its census.+- Tests: `test/refresh-publish-diverged-7686.test.ts` (setting on and off, a conflict, quarantine, a dirty overlap, a refusal after the publish, a publish child that fails, and a dry run, on PGLite and on Postgres through `test/e2e/worktree-refresh-postgres.test.ts`). Tests 7c and 7d in `test/persistence-worktree-refresh.test.ts` share one fixture across their cells and run in about half the time, with every assertion kept (#7782).+## [0.60.526.0] - 2026-10-31**The #7679 refresh state-matrix tests (7c and 7d) now run in about half the time, with every assertion kept (#7782).**diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex a650c0fd40..2cbbd44b14 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -300,7 +300,7 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTifor (const sha of [preFetch, ...recorded]) if (isSha(sha)) heldUpstream.add(sha);}if (remote && remote !== '.') {- const fetched = await git(['-C', root, ...(publishDiverged ? ['-c', 'core.logAllRefUpdates=true'] : []), 'fetch', '--no-tags', '--quiet', remote, merge || branch], fetchTimeoutMs);+ 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',fetched.timedOut ? `git fetch ${remote} did not finish within ${fetchTimeoutMs} ms; nothing changed.`: `git fetch ${remote} failed (${fetched.stderr.trim().split('\n')[0] || `exit ${fetched.code}`}); nothing changed.`,diff --git a/test/refresh-publish-diverged-7686.test.ts b/test/refresh-publish-diverged-7686.test.tsindex ce686311ac..d57db08da5 100644--- a/test/refresh-publish-diverged-7686.test.ts+++ b/test/refresh-publish-diverged-7686.test.ts@@ -250,6 +250,32 @@ test('setting on, reflog off and no completed refresh: the refusal\'s own fetchexpect(remoteHead(f)).toBe(rewritten);}), 120_000);+for (const first of ['setting off', 'dry run'] as const) {+ test(`reflog off: a ${first} refresh that refuses after a force-push logs its own fetch, so a later setting-on refresh never publishes the removed commit`, () => each(async f => {+ const reflog = git(f.root, 'rev-parse', '--path-format=absolute', '--git-path', 'logs/refs/remotes/origin/main').trim();+ git(f.root, 'config', 'core.logAllRefUpdates', 'false');+ rmSync(reflog, { force: true });+ const before = f.push('alpha/one.md', page('Alpha one', 'Upstream before the local push.'));+ expect(await refreshWorktree(f.engine, f.alpha)).toMatchObject({ status: 'completed' });+ writeFileSync(join(f.root, 'beta/pushed.md'), page('Pushed', 'Pushed from this checkout, then rewritten away.'));+ git(f.root, 'add', 'beta/pushed.md'); git(f.root, 'commit', '-qm', 'pushed then removed');+ const removed = f.head();+ git(f.root, 'push', '-q', 'origin', 'HEAD:main');+ git(f.upstream, 'fetch', '-q', 'origin');+ git(f.upstream, 'reset', '-q', '--hard', before);+ commitFile(f.upstream, 'alpha/two.md', page('Alpha two', 'Rewritten upstream.'));+ git(f.upstream, 'push', '-q', '--force', 'origin', 'main');+ const rewritten = remoteHead(f);+ if (first === 'dry run') await f.engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'true');+ await refusedWith(refreshWorktree(f.engine, f.alpha, { dryRun: first === 'dry run' }), 'refresh_diverged');+ await f.engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'true');+ expect((await refusedWith(refreshWorktree(f.engine, f.alpha), 'refresh_diverged')).message).toContain(`no longer contains ${removed}`);+ expect(remoteHead(f)).toBe(rewritten);+ expect(() => git(f.root, 'cat-file', '-e', 'origin/main:beta/pushed.md')).toThrow();+ expect(f.head()).toBe(removed);+ }), 120_000);+}+test('setting on: a local commit the upstream held inside a merge, then dropped by a force-push, is never published', () => each(async f => {const before = remoteHead(f);writeFileSync(join(f.root, 'beta/local.md'), page('Local', 'Merged upstream from this checkout, then rewritten away.'));