Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(refresh): hold only recovery_required rows on the HEAD-contains-member rule, and only for members on this refresh's line
garrytan6580891ed395
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fcd90d4922..c9f0b2e78e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,12 +14,12 @@ identifiers and attribution are available in the pre-removal Git revision**`gbrain sources refresh --abandon` no longer lets the next sync delete pages a member source already synced.**-A refresh interrupted after a member source synced the upstream commit, then moved to `recovery_required` with the checkout back at the commit it started from, could be abandoned: `--resume` refused and named `--abandon`, `--abandon` lifted the fence, and the next sync diffed that member back to the start commit and soft-deleted every page it had synced. `--abandon` now refuses such a row (`refresh_recovery_required`, nothing changed) once any member's `last_commit` is past the start commit (set, and neither that commit nor an ancestor of it; a commit git cannot find counts as past), and `--resume` no longer names `--abandon` there. Both name the one way out that keeps the pages, as an ask-the-user fix: preview with `git -C <checkout> status --short`, fast-forward the checkout with `git merge --ff-only` to the commit the members synced (the upstream commit, or a later one a member already synced), then `--resume`; when git refuses, `git reset --hard` to that commit is named as the destructive fallback. `--resume` now also finishes a `recovery_required` refresh whose HEAD descends from the upstream commit instead of refusing it, and never finishes one at a HEAD that does not contain every member source's synced commit. Writer status uses the same member check. A row whose members never moved past the start commit is abandoned exactly as before, and sync's deletion behaviour is unchanged.+A refresh interrupted after a member source synced the upstream commit, then moved to `recovery_required` with the checkout back at the commit it started from, could be abandoned: `--resume` refused and named `--abandon`, `--abandon` lifted the fence, and the next sync diffed that member back to the start commit and soft-deleted every page it had synced. `--abandon` now refuses such a row (`refresh_recovery_required`, nothing changed) once any member's `last_commit` is past the start commit (set, and neither that commit nor an ancestor of it; a commit git cannot find counts as past), and `--resume` no longer names `--abandon` there. Both name the one way out that keeps the pages, as an ask-the-user fix: preview with `git -C <checkout> status --short`, fast-forward the checkout with `git merge --ff-only` to the commit the members synced (the upstream commit, or a later one a member already synced), then `--resume`; when git refuses, `git reset --hard` to that commit is named as the destructive fallback. `--resume` now also finishes a `recovery_required` refresh whose HEAD descends from the upstream commit instead of refusing it, and never finishes one at a HEAD that does not contain the commit a member source synced on this refresh's line (or a commit git cannot place). Writer status uses the same member check. A row whose members never moved past the start commit is abandoned exactly as before, and sync's deletion behaviour is unchanged.### Itemized changes- `src/core/persistence/worktree-refresh.ts`: one member check (`membersPastStart`) feeds `--abandon`, the `--resume` refusal at the start commit, the stored `recovery_required` fix and writer status.-- Tests: `test/persistence-worktree-refresh.test.ts` 7r (the three-step chain: crash while syncing, member synced, HEAD reset; pages survive, then reset-to-target and `--resume` completes) 7s (no member moved: `--abandon` still works) 7t (a member behind the start commit still abandons; an unknown commit refuses), 7u (a member synced past the target keeps its pages), 7w and 7x (`--resume` never finishes at a HEAD that lacks a member's synced commit) and 7v (an unrelated HEAD names no `--abandon`; a HEAD past the target resumes), on PGLite and on Postgres through `test/e2e/worktree-refresh-postgres.test.ts`.+- Tests: `test/persistence-worktree-refresh.test.ts` 7r (the three-step chain: crash while syncing, member synced, HEAD reset; pages survive, then reset-to-target and `--resume` completes) 7s (no member moved: `--abandon` still works) 7t (a member behind the start commit still abandons; an unknown commit refuses), 7u (a member synced past the target keeps its pages), 7w and 7x (`--resume` never finishes at a HEAD that lacks a member's synced commit), 7y (a member already on rewritten history before the refresh never wedges crash recovery) and 7v (an unrelated HEAD names no `--abandon`; a HEAD past the target resumes), on PGLite and on Postgres through `test/e2e/worktree-refresh-postgres.test.ts`.## [0.60.583.0] - 2026-11-05diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex e3affd43a6..8330cd580e 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -130,13 +130,21 @@ async function keepPagesHead(engine: BrainEngine, row: Pick<WorktreeRefreshRow,return head;}).catch(() => row.target_head);}+/** `git merge-base --is-ancestor`: true or false, or null when git cannot tell (a missing object, an unreadable checkout). */+async function ancestry(git: Git, root: string, ancestor: string, descendant: string): Promise<boolean | null> {+ const out = await git(['-C', root, 'merge-base', '--is-ancestor', ancestor, descendant]);+ return out.code === 0 ? true : out.code === 1 ? false : null;+}/**- * GST-1457: a member whose last_commit HEAD does not contain (ahead of HEAD, on a sibling line, or a commit git cannot place); finishing- * the refresh there would let that member's next sync diff its commit to HEAD and soft-delete what HEAD lacks.+ * GST-1457: a member that synced on this refresh's line (its last_commit descends from old_head) to a commit HEAD does not contain (ahead+ * of HEAD or on a sibling line), or whose commit git cannot place; finishing the refresh there would let that member's next sync diff its+ * commit to HEAD and soft-delete what HEAD lacks. A member already off old_head's line before the refresh (rewritten history) is the state+ * an uninterrupted refresh completes on, so it does not hold the row.*/-async function memberOutsideHead(engine: BrainEngine, git: Git, root: string, row: Pick<WorktreeRefreshRow, 'source_ids'>, head: string): Promise<boolean> {+async function memberOutsideHead(engine: BrainEngine, git: Git, root: string, row: Pick<WorktreeRefreshRow, 'source_ids' | 'old_head'>, head: string): Promise<boolean> {for (const member of await engine.executeRaw<{ last_commit: string | null }>('SELECT last_commit FROM sources WHERE id = ANY($1::text[])', [row.source_ids])) {- if (member.last_commit && member.last_commit !== head && !await isAncestor(git, root, member.last_commit, head)) return true;+ if (!member.last_commit || member.last_commit === head || await ancestry(git, root, member.last_commit, head) === true) continue;+ if (await ancestry(git, root, row.old_head, member.last_commit) !== false) return true;}return false;}@@ -909,7 +917,7 @@ async function recoverOne(engine: BrainEngine, git: Git, row: WorktreeRefreshRow} else if (row.state === 'fenced' && head === row.old_head) {await engine.transaction(tx => transition(tx, row, ['fenced'], 'aborted', { refusal: { code: 'refresh_interrupted', cause: 'The refreshing process stopped before merging; HEAD is unchanged.' } }));} else if (['fenced', 'merged', 'recovery_required'].includes(row.state) && (head === row.target_head || (row.state === 'recovery_required' && head !== null && await isAncestor(git, root, row.target_head, head)))- && await isAncestor(git, root, row.old_head, row.target_head) && !await memberOutsideHead(engine, git, root, row, head!)) {+ && await isAncestor(git, root, row.old_head, row.target_head) && (row.state !== 'recovery_required' || !await memberOutsideHead(engine, git, root, row, head!))) {await bookkeeping(engine, row);} else if (row.state === 'syncing' && head !== null && (head === row.target_head || await isAncestor(git, root, row.target_head, head))) {// stays syncing; the member syncs run from --resume or any later cycle sync.diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.tsindex f973a645f0..f00b636bd4 100644--- a/test/persistence-worktree-refresh.test.ts+++ b/test/persistence-worktree-refresh.test.ts@@ -885,6 +885,29 @@ test('7x. GST-1457 outside voice: --resume never finishes at a sibling of the coexpect((await f.refreshRows()).map(r => r.state)).toEqual(['recovery_required']);}), 180_000);+test('7y. GST-1457 fix-check: a member already off the start commit\'s line before the refresh never wedges crash recovery at the target', () => each(async f => {+ // Value: protects=the HEAD-contains-every-member guard holds only recovery_required rows whose members synced on this refresh's line, so a+ // member left on rewritten history (force-push, squash, reset) before the refresh converges after a crash exactly as an uninterrupted refresh+ // completes on it; fails_when=the guard also gates fenced/merged rows, or treats any member HEAD lacks as synced past; why_new=7w/7x members+ // descend from the start commit; seam=last_commit set to a side-branch commit with the coordinated write; no_claim=a member commit git cannot find.+ f.push('alpha/two.md', page('Alpha two', 'Upstream.'));+ const old = f.head();+ git(f.root, 'checkout', '-q', '-b', 'side', `${old}~1`);+ const side = commitFile(f.root, 'alpha/side.md', page('Alpha side', 'Rewritten history.'));+ git(f.root, 'checkout', '-q', '-');+ await setLastCommit(f, f.alpha, side);+ const merged = await orphanAt(f, 'merged', 'merged');+ expect(await refreshWorktree(f.engine, f.alpha, { resume: true })).toMatchObject({ refresh_id: merged.id, status: 'completed' });+ f.push('alpha/three.md', page('Alpha three', 'Upstream again.'));+ await setLastCommit(f, f.alpha, side);+ await expect(refreshWorktree(f.engine, f.alpha, { hooks: { boundary: at => { if (at === 'merged') throw new Error('simulated crash at merged'); } } }))+ .rejects.toThrow('simulated crash');+ await disposePersistenceConsumer(f.engine);+ const held = (await f.refreshRows()).at(-1)!;+ await f.engine.executeRaw(`UPDATE persistence_worktree_refreshes SET state='recovery_required',completed_at=NULL WHERE id=$1::uuid`, [held.id]);+ expect(await refreshWorktree(f.engine, f.alpha, { resume: true })).toMatchObject({ refresh_id: held.id, status: 'completed' });+}), 180_000);+test('8. a queued git effect drains before fenced, and nothing on the worktree is claimed while fenced', () => each(async f => {f.push('alpha/two.md', page('Alpha two', 'Upstream.'));await f.put(f.beta, 'notes/effect', page('Effect', 'Has a git effect.'));