Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(windows): make fault probes portable and bound sharing-denial retries

haelyra15b51b6ca4fb
diff --git a/manifests/context-packs/[email protected] b/manifests/context-packs/[email protected]
index 3deeb9379..ae138de90 100644
--- a/manifests/context-packs/[email protected]
+++ b/manifests/context-packs/[email protected]
@@ -10,7 +10,7 @@
"id": "hand-seeded",
"source": "manual-curation-pending-regeneration"
},
- "registryDigest": "abe7be71a90fe080fee4e253b9ed30a258c9e051122f49803aa9633309846e59",
+ "registryDigest": "1fec77877bfa9bcb95c296459c582c4a09cff0729b619fb49683e230c30ec486",
"schemaVersion": 1,
"triggers": {
"skill:agent-runtime-gateway-smoke-test": [
diff --git a/skills/story-lifecycle/scripts/ledger.js b/skills/story-lifecycle/scripts/ledger.js
index 833b2d1be..6431276ab 100644
--- a/skills/story-lifecycle/scripts/ledger.js
+++ b/skills/story-lifecycle/scripts/ledger.js
@@ -157,6 +157,7 @@ function breakStaleLock(dir) {
function acquireLock(dir, waitMs) {
const lock = path.join(dir, '.lock');
const deadline = Date.now() + waitMs;
+ let accessError = null;
for (;;) {
try {
// A live descriptor keeps this inode allocated, even if a foreign writer
@@ -164,13 +165,16 @@ function acquireLock(dir, waitMs) {
const owner = writeNew(lock, JSON.stringify({ pid: process.pid, host: os.hostname(), at: new Date().toISOString() }), { keepOpen: true });
return () => { try { removeOwned(lock, owner); } finally { fs.closeSync(owner.fd); } };
} catch (err) {
- if (err.code !== 'EEXIST') throw err;
+ if (err.code === 'EEXIST') accessError = null;
+ else if (process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(err.code)) accessError = err.code;
+ else throw err;
}
const holder = readLock(dir); // null: lock vanished or half-written; retry
if (holder && isStale(holder) && breakStaleLock(dir)) continue;
if (Date.now() > deadline) {
const brk = fs.existsSync(path.join(dir, '.lock.break')) ? '; .lock.break is held (delete it only if no ledger command is running)' : '';
- throw new LedgerError(`ledger locked by pid ${holder && holder.pid} since ${holder && holder.at}${brk}`, 3);
+ const denied = accessError ? `; Windows file access ${accessError}; check active writers or directory permissions` : '';
+ throw new LedgerError(`ledger locked by pid ${holder && holder.pid} since ${holder && holder.at}${brk}${denied}`, 3);
}
sleep(25 + Math.floor(Math.random() * 50));
}
diff --git a/tests/docs/story-lifecycle-ledger.test.js b/tests/docs/story-lifecycle-ledger.test.js
index 13d3edab4..56f77f693 100644
--- a/tests/docs/story-lifecycle-ledger.test.js
+++ b/tests/docs/story-lifecycle-ledger.test.js
@@ -318,11 +318,14 @@ const tests = [
fs.writeFileSync(lock, JSON.stringify({ pid: 2 ** 22 + 1, host: os.hostname(), at: 'stale' }));
// Preload: right after the waiter reads the stale lock, another writer wins and holds a live lock.
const preload = path.join(root, 'race.js');
- fs.writeFileSync(preload, `const fs = require('fs'); const orig = fs.openSync; let raced = false;
-fs.openSync = function (p, ...a) { const r = orig.call(fs, p, ...a);
- if (!raced && String(p).endsWith('.lock') && a[0] === (fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0))) { raced = true; fs.unlinkSync(p);
+ fs.writeFileSync(preload, `const fs = require('fs'); const orig = fs.openSync; let raced = false; const observed = new Map();
+fs.openSync = function (p, ...a) { const fd = orig.call(fs, p, ...a);
+ if (String(p).endsWith('.lock') && a[0] === (fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0))) observed.set(fd, p);
+ return fd; };
+const close = fs.closeSync; fs.closeSync = function (fd) { const p = observed.get(fd); observed.delete(fd); close.call(fs, fd);
+ if (!raced && p) { raced = true; fs.unlinkSync(p);
fs.writeFileSync(p, JSON.stringify({ pid: ${process.pid}, host: require('os').hostname(), at: 'live' }), { flag: 'wx' }); }
- return r; };`);
+};`);
const r = spawnSync(process.execPath, ['-r', preload, LEDGER, 'allocate', '--epic', 'auth-flow', '--root', root, '--wait-ms', '200'], { encoding: 'utf8' });
assert.strictEqual(r.status, 3, `expected lock timeout, got ${r.status}: ${r.stderr}`);
assert.ok(fs.readFileSync(lock, 'utf8').includes('live'), 'live lock was deleted');
@@ -409,7 +412,7 @@ fs.openSync = function (p, ...a) { const r = orig.call(fs, p, ...a);
fs.writeFileSync(victim, 'ORIGINAL');
const preload = path.join(root, 'race.js');
fs.writeFileSync(preload, `const fs = require('fs'); let planted = false;
-const plant = (p) => { if (!planted && String(p).endsWith('/.stories/.gitignore')) {
+const plant = (p) => { if (!planted && String(p).split(require('path').sep).join('/').endsWith('/.stories/.gitignore')) {
planted = true; fs.symlinkSync(${JSON.stringify(victim)}, p); } };
const exists = fs.existsSync; fs.existsSync = function (p) { const result = exists.call(fs, p);
if (!result) plant(p); return result; };
@@ -473,7 +476,7 @@ for (const name of ['renameSync', 'linkSync']) { const original = fs[name]; fs[n
const victim = path.join(root, 'victim.txt'); fs.writeFileSync(victim, 'ORIGINAL');
const preload = path.join(root, 'stage-race.js');
fs.writeFileSync(preload, `const fs = require('fs'); let planted = false;
-const plant = (p) => { if (!planted && String(p).endsWith('/.txn/new-0')) {
+const plant = (p) => { if (!planted && String(p).split(require('path').sep).join('/').endsWith('/.txn/new-0')) {
planted = true; fs.symlinkSync(${JSON.stringify(victim)}, p); } };
const open = fs.openSync; fs.openSync = function (p, ...a) { plant(p); return open.call(fs, p, ...a); };
const write = fs.writeFileSync; fs.writeFileSync = function (p, ...a) { plant(p); return write.call(fs, p, ...a); };`);
@@ -501,10 +504,18 @@ const write = fs.writeFileSync; fs.writeFileSync = function (p, ...a) { plant(p)
const preload = path.join(root, 'lock-owner-race.js');
fs.writeFileSync(preload, `const fs = require('fs'); const original = fs.rmSync; let replaced = false;
fs.rmSync = function (p, ...args) { const result = original.call(fs, p, ...args);
- if (!replaced && String(p).endsWith('/.stories/.txn')) { replaced = true; fs.unlinkSync(${JSON.stringify(lock)});
+ if (!replaced && String(p).split(require('path').sep).join('/').endsWith('/.stories/.txn')) { replaced = true; fs.unlinkSync(${JSON.stringify(lock)});
fs.writeFileSync(${JSON.stringify(lock)}, JSON.stringify({pid:${process.pid},host:require('os').hostname(),at:'replacement'}), {flag:'wx'}); }
return result; };`);
const result = spawnSync(process.execPath, ['-r', preload, LEDGER, 'apply', '--input', input, '--root', root], { encoding: 'utf8' });
+ // Some Windows Node versions prevent re-creating an open delete-pending
+ // lock. The injected foreign writer is rejected before it can take ownership.
+ if (process.platform === 'win32' && result.status === 1 && /(?:EPERM|EACCES|EBUSY).*open[^\n]*\.lock/.test(result.stderr)) {
+ assert.strictEqual(storyFiles(root).length, 1, 'original publication completed');
+ assert.ok(read(root, storyFiles(root)[0]).includes('Ours'));
+ assert.ok(!fs.existsSync(lock), 'process exit releases the old delete-pending lock');
+ return;
+ }
assert.strictEqual(result.status, 0, result.stderr);
assert.ok(fs.readFileSync(lock, 'utf8').includes('replacement'));
}],
@@ -524,7 +535,7 @@ const close = fs.closeSync; fs.closeSync = function (value) {
if (value === fd) closed = true; return close.call(fs, value); };
const remove = fs.rmSync; fs.rmSync = function (p, ...a) {
const result = remove.call(fs, p, ...a);
- if (!replaced && String(p).endsWith('/.stories/.txn')) {
+ if (!replaced && String(p).split(require('path').sep).join('/').endsWith('/.stories/.txn')) {
replaced = true; fs.unlinkSync(${JSON.stringify(lock)});
fs.writeFileSync(${JSON.stringify(lock)}, JSON.stringify({ pid: ${process.pid}, host: require('os').hostname(), at: 'replacement' }), { flag: 'wx' });
} return result; };
@@ -533,6 +544,14 @@ const stat = fs.lstatSync; fs.lstatSync = function (p, ...a) {
return replaced && closed && String(p) === ${JSON.stringify(lock)} ? oldStat : result;
};`);
const result = spawnSync(process.execPath, ['-r', preload, LEDGER, 'apply', '--input', input, '--root', root], { encoding: 'utf8' });
+ // Some Windows Node versions prevent re-creating an open delete-pending
+ // lock. The injected foreign writer is rejected before it can take ownership.
+ if (process.platform === 'win32' && result.status === 1 && /(?:EPERM|EACCES|EBUSY).*open[^\n]*\.lock/.test(result.stderr)) {
+ assert.strictEqual(storyFiles(root).length, 1, 'original publication completed');
+ assert.ok(read(root, storyFiles(root)[0]).includes('Inode owner'));
+ assert.ok(!fs.existsSync(lock), 'process exit releases the old delete-pending lock');
+ return;
+ }
assert.strictEqual(result.status, 0, result.stderr);
assert.ok(fs.readFileSync(lock, 'utf8').includes('replacement'));
}],
@@ -547,7 +566,7 @@ const stat = fs.lstatSync; fs.lstatSync = function (p, ...a) {
const preload = path.join(root, 'recover-crash.js');
fs.writeFileSync(preload, `const fs = require('fs'); const original = fs.renameSync;
fs.renameSync = function (source, ...args) { const result = original.call(fs, source, ...args);
- if (String(source).endsWith('/.txn/restore.tmp')) process.exit(70); return result; };`);
+ if (String(source).split(require('path').sep).join('/').endsWith('/.txn/restore.tmp')) process.exit(70); return result; };`);
const recovery = spawnSync(process.execPath, ['-r', preload, LEDGER, 'recover', '--root', root], { encoding: 'utf8' });
assert.strictEqual(recovery.status, 70);
const retry = run(root, ['recover']);
@@ -556,6 +575,37 @@ fs.renameSync = function (source, ...args) { const result = original.call(fs, so
assert.deepStrictEqual(run(root, ['status']).out.drift, []);
}],
+ ['Windows sharing denial during lock publication is retried within the deadline', () => {
+ const root = newRepo();
+ const preload = path.join(root, 'sharing-retry.js');
+ fs.writeFileSync(preload, `require('path'); Object.defineProperty(process, 'platform', { value: 'win32' });
+const fs = require('fs'); const original = fs.openSync; let attempts = 0;
+fs.openSync = function (p, flags, ...args) {
+ if (String(p).endsWith('.lock') && (flags & fs.constants.O_EXCL) && attempts++ < 2) {
+ throw Object.assign(new Error('simulated delete-pending sharing denial'), { code: 'EPERM' });
+ } return original.call(fs, p, flags, ...args); };`);
+ const result = spawnSync(process.execPath, ['-r', preload, LEDGER, 'allocate', '--root', root,
+ '--epic', 'auth-flow', '--wait-ms', '1000'], { encoding: 'utf8', timeout: 3000 });
+ assert.strictEqual(result.status, 0, result.stderr);
+ assert.deepStrictEqual(JSON.parse(result.stdout).ids, ['auth-flow-1']);
+ }],
+
+ ['permanent Windows sharing denial expires without creating an allocation', () => {
+ const root = newRepo();
+ const preload = path.join(root, 'sharing-denied.js');
+ fs.writeFileSync(preload, `require('path'); Object.defineProperty(process, 'platform', { value: 'win32' });
+const fs = require('fs'); const original = fs.openSync;
+fs.openSync = function (p, flags, ...args) {
+ if (String(p).endsWith('.lock') && (flags & fs.constants.O_EXCL)) {
+ throw Object.assign(new Error('simulated permanent sharing denial'), { code: 'EACCES' });
+ } return original.call(fs, p, flags, ...args); };`);
+ const result = spawnSync(process.execPath, ['-r', preload, LEDGER, 'allocate', '--root', root,
+ '--epic', 'auth-flow', '--wait-ms', '40'], { encoding: 'utf8', timeout: 2000 });
+ assert.strictEqual(result.status, 3, result.stderr || result.error?.message);
+ assert.match(result.stderr, /Windows file access EACCES/);
+ assert.ok(!fs.existsSync(path.join(root, '.git', 'story-lifecycle', 'ids', 'auth-flow-1')));
+ }],
+
['ledger paths are confined to .stories/ file shapes', () => {
const dir = path.join(os.tmpdir(), '.stories');
for (const bad of ['../x-1.md', 'epics/../../x.md', 'Auth-1.md', 'a/b-1.md', 'epics/a.txt', 'sprints/sprint-0.md', '/abs-1.md']) {
diff --git a/tests/hooks/ecc-statusline-late.test.js b/tests/hooks/ecc-statusline-late.test.js
index bdbd3da45..e84ab5d6a 100644
--- a/tests/hooks/ecc-statusline-late.test.js
+++ b/tests/hooks/ecc-statusline-late.test.js
@@ -29,10 +29,10 @@ function registeredDisplay(t, payload, options = {}, bridge) {
fs.writeFileSync(settingsPath, JSON.stringify({ statusLine: { ...example.statusLine, command } }));
const before = fs.readFileSync(settingsPath);
const settings = JSON.parse(before);
- const env = { ...process.env, HOME: home, USERPROFILE: home, CLAUDE_CONFIG_DIR: configDir, TMPDIR: temporaryDir,
+ const env = { ...process.env, HOME: home, USERPROFILE: home, CLAUDE_CONFIG_DIR: configDir, TMPDIR: temporaryDir, TEMP: temporaryDir, TMP: temporaryDir,
ECC_STATUSLINE_USAGE: '1', ECC_STATUSLINE_COMPACT: '0', ECC_BAR_GLYPHS: 'ascii', ...options };
if (env.ECC_STATUSLINE_USAGE === undefined) delete env.ECC_STATUSLINE_USAGE;
- const result = spawnSync('/bin/sh', ['-c', settings.statusLine.command], {
+ const result = spawnSync(process.platform === 'win32' ? 'bash' : '/bin/sh', ['-c', settings.statusLine.command], {
input: JSON.stringify(payload), encoding: 'utf8', env, timeout: 5000, cwd: home,
});
assert.equal(result.status, 0, result.stderr);