Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): a reclaim refusal names the process holding the lock before any uninspectable git; tests wait out another user's git (#6932)
garrytand2d011560da5
diff --git a/src/core/persistence/index-lock-reclaim.ts b/src/core/persistence/index-lock-reclaim.tsindex 6f64bae0f9..1fc973e6c9 100644--- a/src/core/persistence/index-lock-reclaim.ts+++ b/src/core/persistence/index-lock-reclaim.ts@@ -88,8 +88,14 @@ export function processTableBlind(): string | null {/** The git environment variables that place a process's repository or index. */const GIT_LOCATION_VARIABLES = ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_COMMON_DIR', 'GIT_INDEX_FILE'];-/** A process that holds the lock or runs git in the checkout, or a git process this process cannot inspect. */+/**+ * A process that holds the lock or runs git in the checkout, or a git process this process cannot inspect.+ * A process that has the lock open is named first, then a git in the checkout, then a git that cannot be+ * inspected, so a refusal names the actual holder when there is one.+ */function liveHolder(lock: { dev: number; ino: number }, roots: string[]): string | null {+ const opaque: string[] = [];+ const inRepository: string[] = [];for (const pid of readdirSync('/proc')) {if (!/^\d+$/.test(pid)) continue;let comm: string;@@ -102,25 +108,28 @@ function liveHolder(lock: { dev: number; ino: number }, roots: string[]): stringif (opened.dev === lock.dev && opened.ino === lock.ino) return `process ${pid} (${comm}) has the lock open`;}if (!comm.startsWith('git')) continue;- if (fds === null) return `git process ${pid} (${comm}) belongs to another user, so its open files cannot be checked`;+ if (fds === null) { opaque.push(`git process ${pid} (${comm}) belongs to another user, so its open files cannot be checked`); continue; }let cwd: string;let environ: string;try {cwd = readlinkSync(`/proc/${pid}/cwd`).replace(/ \(deleted\)$/, '');environ = readFileSync(`/proc/${pid}/environ`, 'utf8');} catch (error) {- if (gone(error)) continue;- return `git process ${pid} (${comm}) could not be inspected`;+ if (!gone(error)) opaque.push(`git process ${pid} (${comm}) could not be inspected`);+ continue;}- if (inside(cwd, roots)) return `git process ${pid} (${comm}) is running in the checkout`;+ if (inside(cwd, roots)) { inRepository.push(`git process ${pid} (${comm}) is running in the checkout`); continue; }for (const entry of environ.split('\0')) {const at = entry.indexOf('=');if (at < 0 || !GIT_LOCATION_VARIABLES.includes(entry.slice(0, at))) continue;const value = entry.slice(at + 1);- if (value && inside(canonical(isAbsolute(value) ? value : resolve(cwd, value)), roots)) return `git process ${pid} (${comm}) uses this repository through ${entry.slice(0, at)}`;+ if (value && inside(canonical(isAbsolute(value) ? value : resolve(cwd, value)), roots)) {+ inRepository.push(`git process ${pid} (${comm}) uses this repository through ${entry.slice(0, at)}`);+ break;+ }}}- return null;+ return inRepository[0] ?? opaque[0] ?? null;}/**diff --git a/test/persistence-index-lock-reclaim-6932.test.ts b/test/persistence-index-lock-reclaim-6932.test.tsindex ea7cc4982f..847c01b47f 100644--- a/test/persistence-index-lock-reclaim-6932.test.ts+++ b/test/persistence-index-lock-reclaim-6932.test.ts@@ -13,7 +13,7 @@*/import { afterEach, expect, test } from 'bun:test';import { execFileSync, spawn, type ChildProcess } from 'node:child_process';-import { existsSync, mkdtempSync, openSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync, closeSync } from 'node:fs';+import { existsSync, mkdtempSync, openSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync, closeSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { commitGitTargets, gitStepTimeoutMs, INDEX_LOCK_GRACE_FOR_TESTS } from '../src/core/persistence/effect-git.ts';@@ -57,6 +57,18 @@ const until = async (done: () => boolean, ms = 20_000) => {for (const deadline = Date.now() + ms; !done() && Date.now() < deadline;) await Bun.sleep(10);expect(done()).toBe(true);};+/** Another user's git on this host (a CI agent, a system service) cannot be inspected, so it blocks every proof while it runs. */+const foreignGit = () => process.platform === 'linux' && readdirSync('/proc').some(pid => {+ if (!/^\d+$/.test(pid)) return false;+ try { if (!readFileSync(`/proc/${pid}/comm`, 'utf8').startsWith('git')) return false; } catch { return false; }+ try { readdirSync(`/proc/${pid}/fd`); return false; } catch (error) { return (error as NodeJS.ErrnoException).code === 'EACCES'; }+});+/** Whether a reclaim can be proven here now: the process table is visible and no uninspectable git runs (waited out up to 30 s). */+async function canProve(): Promise<boolean> {+ if (blind) return false;+ for (const deadline = Date.now() + 30_000; foreignGit() && Date.now() < deadline;) await Bun.sleep(100);+ return !foreignGit();+}const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch { return false; } };async function writerLock(home: string) {@@ -103,7 +115,7 @@ test('probe: a git SIGKILLed while it holds the index lock leaves a 0-byte lock,await expect(commitGitTargets(root, ['a.md'], undefined, undefined, owner)).rejects.toMatchObject({ code: 'git_index_locked' });expect(existsSync(lock)).toBe(true);age(lock, RECLAIM_MIN_AGE_MS + 1_000);- if (blind) {+ if (!(await canProve())) {await expect(commitGitTargets(root, ['a.md'], undefined, undefined, owner)).rejects.toMatchObject({ code: 'git_index_locked' });expect(existsSync(lock)).toBe(true);expect(reclaims).toEqual([]);@@ -179,7 +191,7 @@ test('probe: a live git running in the checkout, or pointed at it through GIT_DIlive.kill('SIGKILL');await until(() => !alive(live.pid!));}- if (!blind) {+ if (await canProve()) {expect((await commitGitTargets(root, ['a.md'], undefined, undefined, { writerLock: writer })).get('a.md')).toEqual({ git: 'committed' });expect(existsSync(lock)).toBe(false);}@@ -210,7 +222,7 @@ test('probe: a git SIGKILLed mid-commit (holding index.lock in its pre-commit hoawait expect(commitGitTargets(root, ['a.md'], undefined, undefined, { writerLock: writer })).rejects.toMatchObject({ code: 'git_index_locked' });expect(existsSync(lock)).toBe(true);age(lock, INDEX_LOCK_GRACE_FOR_TESTS + 60_000);- if (blind) {+ if (!(await canProve())) {await expect(commitGitTargets(root, ['a.md'], undefined, undefined, { writerLock: writer })).rejects.toMatchObject({ code: 'git_index_stale' });expect(existsSync(lock)).toBe(true);} else {@@ -230,7 +242,7 @@ test('a lock replaced between the proof and the reclaim (a new git took it) is page(lock, RECLAIM_MIN_AGE_MS + 1_000);await withEnv({ GBRAIN_HOME: home }, async () => {const proof = proveIndexLockAbandoned(lock, root, INDEX_LOCK_GRACE_FOR_TESTS);- if (blind) { expect(proof).toMatchObject({ abandoned: false, reason: blind }); return; }+ if (!(await canProve())) { expect(proof).toMatchObject({ abandoned: false }); return; }expect(proof).toMatchObject({ abandoned: true, size: 0 });// A new git removes nothing of ours: the old lock goes and a new one takes its name (a new inode).rmSync(lock);@@ -263,7 +275,7 @@ test('without the writer lock, or with a lock that holds content and is not stalconst held = await writerLock(home);await expect(commitGitTargets(root, ['a.md'], undefined, undefined, { writerLock: held })).rejects.toMatchObject({ code: 'git_index_locked' });expect(readFileSync(lock, 'utf8')).toBe('partial index');- if (!blind) {+ if (await canProve()) {age(lock, INDEX_LOCK_GRACE_FOR_TESTS + 60_000);expect((await commitGitTargets(root, ['a.md'], undefined, undefined, { writerLock: held })).get('a.md')).toEqual({ git: 'committed' });expect(existsSync(lock)).toBe(false);@@ -319,7 +331,7 @@ test('probe: the writer recovers on its own: a Git effect parked by a held lockawait put(ctx, 'notes/second');const second = await gitEffect(engine, 'notes/second');await runOnly(engine, ctx, second.id);- if (blind) {+ if (!(await canProve())) {expect(existsSync(lock)).toBe(true);expect(await gitEffect(engine, 'notes/first')).toMatchObject({ state: 'failed', error_code: 'targets_parked' });return;