Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Keep a refused-merge refresh fenced when git cannot finish reading the reflog
garrytan12149943b886
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex db2dded554..028b652f20 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -184,12 +184,20 @@ const REFLOG_SLACK_MS = 5_000;/*** 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):* true or false, or null when git fails or either reflog is missing, empty or does not end at `head` (core.logAllRefUpdates off, a deleted log).+ * 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.*/async function reachedTargetSince(git: Git, root: string, row: WorktreeRefreshRow, head: string, sinceMs: number): Promise<boolean | null> {- const branch = await git(['-C', root, 'symbolic-ref', '-q', 'HEAD']);+ const read = async (args: string[]) => {+ const out = await git(['-C', root, ...args]);+ if (out.timedOut) throw opError('git_unavailable', `git ${args.join(' ')} did not finish within its bound or could not start, so whether refresh ${row.id} merged is unknown; it stays ${row.state} and nothing changed.`,+ `Check that git works in the checkout (git -C ${q(root)} status), then retry gbrain sources refresh ${row.source_ids[0]} --resume.`,+ { fix: readFix('Git prints why it cannot read this checkout; read-only.', { argv: ['git', '-C', root, 'status'] }) });+ return out;+ };+ const branch = await read(['symbolic-ref', '-q', 'HEAD']);if (branch.code !== 0 && branch.code !== 1) return null;for (const ref of ['HEAD', ...(branch.code === 0 ? [branch.stdout.trim()] : [])]) {- const out = await git(['-C', root, 'log', '-g', '--format=%H %gd', '--date=unix', ref, '--']);+ const out = await read(['log', '-g', '--format=%H %gd', '--date=unix', ref, '--']);if (out.code !== 0) return null;const entries = out.stdout.split('\n').filter(Boolean).map(line => /^([0-9a-f]{40,64}) \S*@\{(\d+)\}$/.exec(line));if (!entries.length || entries.some(entry => !entry) || entries[0]![1] !== head) return null;@@ -202,7 +210,7 @@ async function reachedTargetSince(git: Git, root: string, row: WorktreeRefreshRo* old_head right before the merge, git refused the fast-forward, and the process was killed before the live refresh aborted. HEAD descends* from old_head without the target, no member synced off it, and the reflogs show HEAD never reached the target since the merge started.* Any doubt (no merge_started_at on an older row, no readable reflog) leaves the row to markRecoveryRequired, as does a merge that ran and- * was then reset (test 7).+ * was then reset (the reflog shows the target).*/async function refusedBeforeAbort(engine: BrainEngine, git: Git, root: string, row: WorktreeRefreshRow, head: string | null): Promise<boolean> {const startedAt = row.outcome.merge_started_at;diff --git a/test/persistence-worktree-refresh-restart.test.ts b/test/persistence-worktree-refresh-restart.test.tsindex 8912d3e516..7b2dd49031 100644--- a/test/persistence-worktree-refresh-restart.test.ts+++ b/test/persistence-worktree-refresh-restart.test.ts@@ -171,11 +171,14 @@ test('GST-1674: a refresh killed after git refused its fast-forward recovers by});}, 300_000);-test('GST-1674: the same crash stays recovery_required when the reflog is gone or the row predates merge_started_at', async () => {- for (const doubt of ['reflog', 'started_at'] as const) for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, foreign }) => {+test('GST-1674: the same crash stays recovery_required when the reflog is gone, the row predates merge_started_at, or HEAD reached the target and was reset', async () => {+ for (const doubt of ['reflog', 'started_at', 'reset'] as const) for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, target, foreign }) => {if (doubt === 'reflog') {git(f.root, 'config', 'core.logAllRefUpdates', 'false');rmSync(join(f.root, '.git', 'logs'), { recursive: true, force: true });+ } else if (doubt === 'reset') {+ git(f.root, 'reset', '-q', '--hard', target);+ git(f.root, 'reset', '-q', '--hard', foreign);} else {await f.engine.executeRaw("UPDATE persistence_worktree_refreshes SET outcome=outcome-'merge_started_at' WHERE worktree_id=$1::uuid", [f.worktreeId]);}@@ -187,6 +190,23 @@ test('GST-1674: the same crash stays recovery_required when the reflog is gone o});}, 300_000);+test('GST-1674: a reflog read that git cannot finish leaves the crash fenced for the next owner scan, which then aborts it', async () => {+ for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, foreign }) => {+ const bin = join(f.root, '..', 'wedged-git');+ mkdirSync(bin, { recursive: true });+ writeFileSync(join(bin, 'git'), `#!/bin/sh\nfor a in "$@"; do if [ "$a" = "-g" ]; then kill -9 $$; fi; done\nPATH='${process.env.PATH}' exec git "$@"\n`, { mode: 0o755 });+ const errors: string[] = [];+ const consoleError = console.error;+ console.error = (...args: unknown[]) => { errors.push(args.join(' ')); };+ try { await withEnv({ PATH: `${bin}:${process.env.PATH}` }, () => resumeWorktreeRefreshes(f.engine)); } finally { console.error = consoleError; }+ expect(((await f.refreshRows()) as Row[]).map(row => row.state)).toEqual(['fenced']);+ expect(errors.join('\n')).toContain('git_unavailable');+ await resumeWorktreeRefreshes(f.engine);+ const [row] = await f.refreshRows() as Row[];+ expect([row!.state, row!.outcome.observed_head]).toEqual(['aborted', foreign]);+ });+}, 300_000);+test('GST-1674: --abandon on a refresh killed after git refused its fast-forward abandons it with the commit kept, not recovery_required, and its owed sync releases the held write', async () => {for (const index of cases.keys()) await withRefusedMergeKill(index, async ({ f, foreign, orphan }) => {const requestId = randomUUID();