From 348f6e62b653452ebeda5c51a8fef428fee62a14 Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:38:01 -0500 Subject: [PATCH] fix: reserve log lock quarantines without overwriting foreign files --- scripts/hooks/post-bash-command-log.js | 13 ++++++++-- tests/lib/helpers/command-log-worker.js | 5 ++-- .../post-bash-command-log-concurrency.test.js | 26 ++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index aee65cfac..42197c42a 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -213,6 +213,15 @@ function restoreLogLock(quarantine, lockPath) { fs.unlinkSync(quarantine); } +function reserveLogQuarantine(lockPath, phase) { + const quarantine = `${lockPath}.${phase}-${process.pid}-${crypto.randomBytes(8).toString('hex')}`; + // Reserve exclusively before rename, which otherwise overwrites a colliding + // foreign file. A failed reservation must never enter path cleanup. + const descriptor = fs.openSync(quarantine, 'wx', LOG_FILE_MODE); + fs.closeSync(descriptor); + return quarantine; +} + function publishLogLock(lockPath) { const temporary = `${lockPath}.create-${process.pid}-${crypto.randomBytes(8).toString('hex')}`; let descriptor = fs.openSync(temporary, 'wx', LOG_FILE_MODE); @@ -229,7 +238,7 @@ function publishLogLock(lockPath) { fs.rmSync(temporary, { force: true }); } return () => { - const quarantine = `${lockPath}.release-${process.pid}-${crypto.randomBytes(8).toString('hex')}`; + const quarantine = reserveLogQuarantine(lockPath, 'release'); fs.renameSync(lockPath, quarantine); if (!sameLogLock(fs.lstatSync(quarantine, { bigint: true }), identity)) { restoreLogLock(quarantine, lockPath); @@ -266,7 +275,7 @@ function recoverLogLock(lockPath) { try { const identity = inspectLogLock(lockPath); if (!identity) return null; // Never take a live or unverifiable owner's lock. - const quarantine = `${lockPath}.stale-${process.pid}-${crypto.randomBytes(8).toString('hex')}`; + const quarantine = reserveLogQuarantine(lockPath, 'stale'); fs.renameSync(lockPath, quarantine); if (!sameLogLock(fs.lstatSync(quarantine, { bigint: true }), identity)) { restoreLogLock(quarantine, lockPath); diff --git a/tests/lib/helpers/command-log-worker.js b/tests/lib/helpers/command-log-worker.js index 70a48d47d..269a6bd66 100644 --- a/tests/lib/helpers/command-log-worker.js +++ b/tests/lib/helpers/command-log-worker.js @@ -3,9 +3,10 @@ const fs = require('fs'); const path = require('path'); const [root, role] = process.argv.slice(2); const file = path.join(root, '.claude', 'bash-commands.log'); -if (role === 'collision') { +if (['collision', 'release-collision', 'stale-collision'].includes(role)) { require('crypto').randomBytes = size => Buffer.alloc(size); - fs.writeFileSync(`${file}.lock.create-${process.pid}-${'00'.repeat(8)}`, 'foreign-creation', { flag: 'wx', mode: 0o600 }); + const phase = { collision: 'create', 'release-collision': 'release', 'stale-collision': 'stale' }[role]; + fs.writeFileSync(`${file}.lock.${phase}-${process.pid}-${'00'.repeat(8)}`, `foreign-${phase}`, { flag: 'wx', mode: 0o600 }); } const signal = name => fs.writeFileSync(path.join(root, name), '', { mode: 0o600 }); const rename = fs.renameSync; diff --git a/tests/scripts/post-bash-command-log-concurrency.test.js b/tests/scripts/post-bash-command-log-concurrency.test.js index 92ee9ed48..6c8d46ae7 100644 --- a/tests/scripts/post-bash-command-log-concurrency.test.js +++ b/tests/scripts/post-bash-command-log-concurrency.test.js @@ -108,7 +108,7 @@ test('an exclusive-create collision never removes another file', () => fixture(a const child = start(root, 'collision', children); await finished(child); const temporary = `${file}.lock.create-${child.pid}-${'00'.repeat(8)}`; - assert.equal(fs.readFileSync(temporary, 'utf8'), 'foreign-creation'); + assert.equal(fs.readFileSync(temporary, 'utf8'), 'foreign-create'); assert.match(fs.readFileSync(file, 'utf8'), /echo collision/); })); @@ -129,3 +129,27 @@ test('an orphan recovery guard preserves its locks and records later commands wi assert.ok(retained.startsWith('old-history\n')); assert.match(retained, /echo orphan-guard/); })); + +test('release quarantine collisions preserve foreign bytes and the owned lock', () => fixture(async (root, file, children) => { + const child = start(root, 'release-collision', children); + await finished(child); + const quarantine = `${file}.lock.release-${child.pid}-${'00'.repeat(8)}`; + assert.equal(fs.readFileSync(quarantine, 'utf8'), 'foreign-release'); + assert.equal(fs.readFileSync(`${file}.lock`, 'utf8'), JSON.stringify({ pid: child.pid })); + assert.match(fs.readFileSync(file, 'utf8'), /echo release-collision/); +})); + +test('stale quarantine collisions preserve foreign bytes and defer recovery without losing the event', () => fixture(async (root, file, children) => { + const owner = start(root, 'owner', children); + await waitFor(root, ['owner-rotate-ready']); + owner.kill('SIGKILL'); + await owner.done; + const lockBytes = fs.readFileSync(`${file}.lock`, 'utf8'); + const child = start(root, 'stale-collision', children); + await finished(child); + const quarantine = `${file}.lock.stale-${child.pid}-${'00'.repeat(8)}`; + assert.equal(fs.readFileSync(quarantine, 'utf8'), 'foreign-stale'); + assert.equal(fs.readFileSync(`${file}.lock`, 'utf8'), lockBytes); + assert.equal(fs.existsSync(`${file}.1`), false); + assert.match(fs.readFileSync(file, 'utf8'), /echo stale-collision/); +}));