Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(refresh): #7686 rewrite guard checks reachability and records its own fetch in the reflog

garrytan3f16b3632c35
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9eeb374bfe..f05e0c82dc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,7 +45,7 @@ gbrain config set sources.refresh_publish_diverged true
### 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 (the remote-tracking ref before this fetch, its reflog, or a completed refresh's target for the same upstream) but no longer contains, or when neither record exists; 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/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); its own fetch always records the move in the reflog; 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.
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 6e94402290..a650c0fd40 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -20,7 +20,7 @@
* so status can say who holds it, and a row whose lock is free is orphaned: the
* next plain refresh finishes it through the `--resume` path before refreshing.
*/
-import { mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { closeSync, fstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, rmSync } from 'node:fs';
import { hostname } from 'node:os';
import { join } from 'node:path';
import type { BrainEngine } from '../engine.ts';
@@ -274,18 +274,33 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTi
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 { reflog = readFileSync((await read(['rev-parse', '--path-format=absolute', '--git-path', `logs/${upstreamFull}`])).stdout.trim(), 'utf8'); } catch { /* no 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]);
- for (const sha of [(await read(['rev-parse', '--verify', '-q', '@{upstream}^{commit}'], 1)).stdout.trim(),
- ...reflog.split('\n').slice(-1000).flatMap(line => line.split(' ').slice(0, 2)), ...rows.map(r => r.target_head)]) {
- if (/^[0-9a-f]{40,64}$/.test(sha) && !/^0+$/.test(sha)) heldUpstream.add(sha);
- }
+ 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);
}
if (remote && remote !== '.') {
- const fetched = await git(['-C', root, 'fetch', '--no-tags', '--quiet', remote, merge || branch], fetchTimeoutMs);
+ const fetched = await git(['-C', root, ...(publishDiverged ? ['-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.`,
@@ -297,10 +312,12 @@ 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 removed = (await read(['rev-list', `${targetHead}..${oldHead}`])).stdout.split('\n').map(sha => sha.trim()).find(sha => heldUpstream.has(sha));
- if (!heldUpstream.size || removed) throw await divergedRefusal(git, root, sourceId, upstreamRef, oldHead, targetHead, removed
+ 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, so it cannot tell a force-push from local commits.`);
+ : `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 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/refresh-publish-diverged-7686.test.ts b/test/refresh-publish-diverged-7686.test.ts
index 442bc9c79d..ce686311ac 100644
--- a/test/refresh-publish-diverged-7686.test.ts
+++ b/test/refresh-publish-diverged-7686.test.ts
@@ -229,3 +229,43 @@ for (const [label, forget] of [['', false], [' even when the first fetch to see
expect((await f.refreshRows()).filter(r => r.state !== 'completed')).toEqual([]);
}), 120_000);
}
+
+test('setting on, reflog off and no completed refresh: the refusal\'s own fetch records the rewrite, so a second refresh refuses too, and with no record at all it refuses', () => 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 });
+ f.push('alpha/purged.md', page('Purged', 'A page the upstream later removed by rewriting history.'));
+ git(f.root, 'fetch', '-q', 'origin'); git(f.root, 'merge', '-q', '--ff-only', 'origin/main');
+ const purged = f.head();
+ git(f.upstream, 'reset', '-q', '--hard', 'HEAD~1');
+ commitFile(f.upstream, 'alpha/two.md', page('Alpha two', 'Rewritten upstream.'));
+ git(f.upstream, 'push', '-q', '--force', 'origin', 'main');
+ const rewritten = remoteHead(f);
+ writeFileSync(join(f.root, 'beta/local.md'), page('Local', 'A local commit nobody pushed.'));
+ git(f.root, 'add', 'beta/local.md'); git(f.root, 'commit', '-qm', 'local only');
+ await f.engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'true');
+ for (let run = 0; run < 2; run++) expect((await refusedWith(refreshWorktree(f.engine, f.alpha), 'refresh_diverged')).message).toContain(`no longer contains ${purged}`);
+ rmSync(reflog, { force: true });
+ expect((await refusedWith(refreshWorktree(f.engine, f.alpha), 'refresh_diverged')).message).toContain('no record of what');
+ expect(remoteHead(f)).toBe(rewritten);
+}), 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.'));
+ git(f.root, 'add', 'beta/local.md'); git(f.root, 'commit', '-qm', 'local merged upstream');
+ const local = f.head();
+ git(f.upstream, 'fetch', '-q', f.root, 'HEAD');
+ commitFile(f.upstream, 'alpha/side.md', page('Side', 'An upstream commit beside the merge.'));
+ git(f.upstream, 'merge', '-q', '--no-edit', 'FETCH_HEAD');
+ git(f.upstream, 'push', '-q', 'origin', 'main');
+ git(f.root, '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);
+ await f.engine.setConfig(REFRESH_PUBLISH_DIVERGED_KEY, 'true');
+ expect((await refusedWith(refreshWorktree(f.engine, f.alpha), 'refresh_diverged')).message).toContain(`no longer contains ${local}`);
+ expect(remoteHead(f)).toBe(rewritten);
+ expect(f.head()).toBe(local);
+}), 120_000);