fix: reserve log lock quarantines without overwriting foreign files

This commit is contained in:
haelyra committed 2026-11-19 17:38:01 -05:00
1 parent 6c3ebeab96
commit 348f6e62b6
3 files changed
+39 -5

No files matched your search

+11 -2
View File
@@ -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);
+3 -2
View File
@@ -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;
@@ -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/);
}));