Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Count any recent move to the target in the reflog tail, and read the reflog date from the identity field only

garrytan54a64dd9e54d
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 31efcc9cb3..bc57c84065 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -185,8 +185,9 @@ const REFLOG_SLACK_MS = 5_000;
const REFLOG_TAIL_BYTES = 1024 ** 2;
/**
* GST-1674: whether the reflogs of HEAD and the branch it names record a move to the target at or after `sinceMs` (less REFLOG_SLACK_MS).
- * Each reflog file's last REFLOG_TAIL_BYTES are read raw and walked newest first; every entry's new value must equal the next entry's old
- * value, back to an entry older than the window or to the log's first entry (a creation from zeros). It answers true or false only then,
+ * Each reflog file's last REFLOG_TAIL_BYTES are read raw. Any entry in that tail that moved the ref to the target inside the window answers
+ * true, wherever it sits (a later entry may carry an older date). Otherwise the tail is walked newest first; every entry's new value must
+ * equal the next entry's old value, back to an entry older than the window or to the log's first entry (a creation from zeros). It answers false only then,
* and null on any doubt: no readable log (core.logAllRefUpdates off, a deleted log, reftable), a log that does not end at `head`, a gap in
* the chain (entries expired out of the middle), or a tail that ends inside the window. A git call that does not finish within its bound or
* cannot start throws git_unavailable instead, so the row stays and a later run retries it.
@@ -215,14 +216,14 @@ async function reachedTargetSince(git: Git, root: string, row: WorktreeRefreshRo
} catch { return null; }
const lines = text.split('\n');
if (!whole) lines.shift();
- const entries = lines.filter(Boolean).map(line => /^([0-9a-f]{40,64}) ([0-9a-f]{40,64}) .*> (\d+) [+-]\d{4}(?:\t|$)/.exec(line));
+ const entries = lines.filter(Boolean).map(line => /^([0-9a-f]{40,64}) ([0-9a-f]{40,64}) [^\t]*> (\d+) [+-]\d{4}(?:\t|$)/.exec(line));
if (!entries.length || entries.some(entry => !entry) || entries.at(-1)![2] !== head) return null;
+ if (entries.some(entry => entry![2] === row.target_head && Number(entry![3]) * 1000 >= sinceMs - REFLOG_SLACK_MS)) return true;
let complete = false;
for (let i = entries.length - 1; i >= 0 && !complete; i--) {
const [, old, now, at] = entries[i]!;
if (i < entries.length - 1 && now !== entries[i + 1]![1]) return null;
if (Number(at) * 1000 < sinceMs - REFLOG_SLACK_MS) complete = true;
- else if (now === row.target_head) return true;
else if (i === 0 && whole && /^0+$/.test(old!)) complete = true;
}
if (!complete) return null;
diff --git a/test/persistence-worktree-refresh-restart.test.ts b/test/persistence-worktree-refresh-restart.test.ts
index f44320fad6..1fa6644043 100644
--- a/test/persistence-worktree-refresh-restart.test.ts
+++ b/test/persistence-worktree-refresh-restart.test.ts
@@ -184,15 +184,21 @@ function backfillHeadReflog(f: RefreshFixture, at: number): void {
writeFileSync(log, `${'0'.repeat(first.length)} ${first} Example <[email protected]> ${at} +0000\tbackfill\n${line.repeat(Math.ceil(33 * 1024 ** 2 / line.length))}${real.join('\n')}\n`);
}
-test('GST-1674: the same crash stays recovery_required when the reflog is gone, the row predates merge_started_at, HEAD reached the target and was reset (also onto another commit with those entries expired), or the reflog tail holds no entry from before the merge', async () => {
- for (const doubt of ['reflog', 'started_at', 'reset', 'expired', 'recent'] as const) for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, old, target, foreign, orphan }) => {
- if (doubt === 'recent') {
+test('GST-1674: the same crash stays recovery_required when the reflog is gone, the row predates merge_started_at, HEAD reached the target and was reset (also onto another commit with those entries expired, or under a later entry with a backdated or misleading line), or the reflog tail holds no entry from before the merge', async () => {
+ for (const doubt of ['reflog', 'started_at', 'reset', 'expired', 'recent', 'subject', 'backdated'] as const) for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, old, target, foreign, orphan }) => {
+ const commit = (message: string) => git(f.root, '-c', 'user.name=Example', '-c', '[email protected]', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', message);
+ if (doubt === 'subject' || doubt === 'backdated') {
+ git(f.root, 'reset', '-q', '--hard', target);
+ git(f.root, 'reset', '-q', '--hard', old);
+ if (doubt === 'subject') commit('fix> 1 +0000');
+ else await withEnv({ GIT_COMMITTER_DATE: '2001-01-01T00:00:00Z' }, async () => { commit('backdated'); });
+ } else if (doubt === 'recent') {
backfillHeadReflog(f, Math.floor(orphan.outcome.merge_started_at / 1000));
} else if (doubt === 'expired') {
git(f.root, 'branch', 'keep-foreign', foreign);
git(f.root, 'reset', '-q', '--hard', target);
git(f.root, 'reset', '-q', '--hard', old);
- git(f.root, '-c', 'user.name=Example', '-c', '[email protected]', '-c', 'commit.gpgsign=false', 'commit', '-q', '--allow-empty', '-m', 'after reset');
+ commit('after reset');
for (const ref of git(f.root, 'for-each-ref', '--format=%(refname)', 'refs/remotes').split('\n').filter(Boolean)) git(f.root, 'update-ref', '-d', ref);
git(f.root, 'reflog', 'expire', '--expire-unreachable=all', '--all');
expect(git(f.root, 'log', '-g', '--format=%H', 'HEAD', '--').split('\n')).not.toContain(target);
@@ -207,7 +213,7 @@ test('GST-1674: the same crash stays recovery_required when the reflog is gone,
}
await resumeWorktreeRefreshes(f.engine);
const [row] = await f.refreshRows() as Row[];
- const head = doubt === 'expired' ? f.head() : foreign;
+ const head = ['expired', 'subject', 'backdated'].includes(doubt) ? f.head() : foreign;
expect([doubt, row!.state]).toEqual([doubt, 'recovery_required']);
expect(row!.outcome.observed_head).toBe(head);
expect(f.head()).toBe(head);