Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a refused derived commit inside a commit-push hook rolls back under the held lock

garrytana83c368e855b
diff --git a/src/core/cycle/derived-write-through.ts b/src/core/cycle/derived-write-through.ts
index 6735d1d483..91f9c6eda7 100644
--- a/src/core/cycle/derived-write-through.ts
+++ b/src/core/cycle/derived-write-through.ts
@@ -38,7 +38,7 @@ import { deletePageThrough, isWriteThroughDisabled, resolvePageWriteTarget, writ
import { sourceMirrorReadOnly } from '../persistence/mirror-read-only.ts';
import { gitChildEnv, literalPathspecs } from '../git-env.ts';
import { classifyGitCheckout, type GitCheckoutClass } from '../git-checkout.ts';
-import { acquireCommitLockAsync, checkoutRoot, commitAndPush, commitPathsLocked, COMMIT_LOCK_ENV, notePending, scopePaths, settlePending } from '../git/commit-push.ts';
+import { acquireCommitLock, acquireCommitLockAsync, checkoutRoot, commitAndPush, commitPathsLocked, COMMIT_LOCK_ENV, notePending, scopePaths, settlePending } from '../git/commit-push.ts';
import { managedFilesystemRootFor, refreshManagedFilesystemRoots } from '../persistence/filesystem-guard.ts';
import { withSourceFilesystemLock } from '../minions/source-filesystem.ts';
import { fileDatabaseDriftToken, replaceableFile } from '../facts/file-body-guard.ts';
@@ -258,12 +258,14 @@ function gitPaths(root: string, args: string[], input?: string): string[] {
* #8092: it runs under the checkout's commit lock (a short wait), so no writer commits between the HEAD check and the
* restore, and it settles the restored paths out of the pending ledger, so a later edit at one of them is never swept into
* a gbrain commit. When the lock stays busy nothing is restored: every file stays in the ledger for that writer's commit.
+ * Under a commit-push hook or child (BRAIN_COMMIT_LOCK_HELD naming this checkout's held lock) it runs under that lock.
*/
async function rollBackDerived(engine: BrainEngine, root: string, batch: readonly DerivedFileChange[], error: unknown): Promise<DerivedCommitFailure | null> {
const code = typeof (error as { code?: unknown }).code === 'string' ? (error as { code: string }).code : 'git_unavailable';
const message = error instanceof Error ? error.message : String(error);
const top = checkoutRoot(root);
- const lock = await acquireCommitLockAsync(top, DERIVED_COMMIT_LOCK_TAKE_MS).catch(() => null);
+ const held = process.env[COMMIT_LOCK_ENV] ? (() => { try { return acquireCommitLock(top, 0); } catch { return null; } })() : null;
+ const lock = held ? { release: async () => held.release() } : await acquireCommitLockAsync(top, DERIVED_COMMIT_LOCK_TAKE_MS).catch(() => null);
try {
const pending = batch.filter(change => !headHolds(root, change));
if (!pending.length) return null;
diff --git a/test/cycle/derived-write-through-commit.test.ts b/test/cycle/derived-write-through-commit.test.ts
index 02fa460376..fdb7aae813 100644
--- a/test/cycle/derived-write-through-commit.test.ts
+++ b/test/cycle/derived-write-through-commit.test.ts
@@ -345,5 +345,22 @@ for (const { backend, databaseUrl } of pathologicalArms()) {
expect(gap).toBeLessThan(1_000);
});
}, 180_000);
+
+ test('#8092: a refused commit inside a commit-push hook (BRAIN_COMMIT_LOCK_HELD) rolls back under the held lock', async () => {
+ await withBrain(databaseUrl, 'git', async b => {
+ refuseCommits(b);
+ const lockPath = join(b.root, '.git', 'brain-commit.lock');
+ const holder = spawn('flock', [lockPath, 'sleep', '60'], { stdio: 'ignore' });
+ try {
+ await Bun.sleep(300);
+ const head = b.git('rev-parse', 'HEAD');
+ const phase = await withEnv({ BRAIN_COMMIT_LOCK_HELD: lockPath }, () => b.extract(['Renders vs proof'], 'hash-one'));
+ const atoms = await b.atoms();
+ expect({ codes: phase.failed.map(f => f.code), head: b.git('rev-parse', 'HEAD'), dirty: dirty(b),
+ files: atoms.map(a => existsSync(b.path(a.slug))), bound: atoms.map(a => a.source_path) })
+ .toEqual({ codes: ['git_unavailable'], head, dirty: '', files: [false], bound: [null] });
+ } finally { holder.kill('SIGKILL'); }
+ });
+ }, 180_000);
});
}