Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(refresh): --resume records the merge only when HEAD contains every member's synced commit

garrytan86a9f4c9b065
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88422df734..fcd90d4922 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 a member source already synced past. 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 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.
### 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 (`--resume` never finishes at a HEAD a member synced past) 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) 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-05
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 0f43b81a0e..e3affd43a6 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -130,10 +130,13 @@ async function keepPagesHead(engine: BrainEngine, row: Pick<WorktreeRefreshRow,
return head;
}).catch(() => row.target_head);
}
-/** GST-1457: a member whose last_commit strictly descends from HEAD; finishing the refresh there would let that member's next sync roll it back to HEAD. */
-async function memberAheadOf(engine: BrainEngine, git: Git, root: string, row: Pick<WorktreeRefreshRow, 'source_ids'>, head: string): Promise<boolean> {
+/**
+ * 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.
+ */
+async function memberOutsideHead(engine: BrainEngine, git: Git, root: string, row: Pick<WorktreeRefreshRow, 'source_ids'>, 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, head, member.last_commit)) return true;
+ if (member.last_commit && member.last_commit !== head && !await isAncestor(git, root, member.last_commit, head)) return true;
}
return false;
}
@@ -906,7 +909,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 memberAheadOf(engine, git, root, row, head!)) {
+ && await isAncestor(git, root, row.old_head, row.target_head) && !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.ts
index 75b18d7ba5..f973a645f0 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -862,6 +862,29 @@ test('7w. GST-1457 fix-check: --resume never finishes a refresh at a HEAD a memb
expect((await f.refreshRows()).map(r => r.state)).toEqual(['recovery_required']);
}), 180_000);
+test('7x. GST-1457 outside voice: --resume never finishes at a sibling of the commit a member synced, even when it descends from the target', () => each(async f => {
+ // Value: protects=recoverOne records the merge only when HEAD contains every member's last_commit, so a HEAD on a sibling line of the
+ // target leaves the fence up instead of completing and letting the member's next sync soft-delete what HEAD lacks; fails_when=the member
+ // check only refuses members strictly ahead of HEAD; why_new=7w's member is ahead of HEAD on the same line; seam=two local commits on top
+ // of the target, one synced by the real managed sync; no_claim=none.
+ const target = f.push('alpha/two.md', page('Alpha two', 'Synced to the target.'));
+ const old = f.head();
+ await orphanAt(f, 'syncing', 'syncing');
+ const synced = commitFile(f.root, 'alpha/three.md', page('Alpha three', 'Synced on one line past the target.'));
+ await performManagedSync(f.engine, { sourceId: f.alpha, noPull: true, noEmbed: true });
+ expect(await f.lastCommit(f.alpha)).toBe(synced);
+ const pages = await livePages(f, f.alpha);
+ git(f.root, 'reset', '-q', '--hard', old);
+ await refusedWith(refreshWorktree(f.engine, f.alpha, { resume: true }), 'refresh_recovery_required');
+ git(f.root, 'reset', '-q', '--hard', target);
+ commitFile(f.root, 'alpha/sibling.md', page('Alpha sibling', 'A sibling line past the target.'));
+ const sibling = await settle(refreshWorktree(f.engine, f.alpha, { resume: true }));
+ await settle(performManagedSync(f.engine, { sourceId: f.alpha, noPull: true, noEmbed: true }));
+ expect(await livePages(f, f.alpha)).toEqual(pages);
+ expect(sibling.error?.code).toBe('refresh_recovery_required');
+ expect((await f.refreshRows()).map(r => r.state)).toEqual(['recovery_required']);
+}), 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.'));