🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(policy): fail fast on lock path errors instead of timing out (#4343)
ordinary being828e6efc50e6
diff --git a/v3/@claude-flow/cli/__tests__/policy-lock-dead-owner.test.ts b/v3/@claude-flow/cli/__tests__/policy-lock-dead-owner.test.tsindex 39a82f9396..c13417c1b1 100644--- a/v3/@claude-flow/cli/__tests__/policy-lock-dead-owner.test.ts+++ b/v3/@claude-flow/cli/__tests__/policy-lock-dead-owner.test.ts@@ -25,6 +25,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';import { execFileSync, spawn } from 'node:child_process';import {existsSync,+ lstatSync,mkdirSync,mkdtempSync,readFileSync,@@ -37,6 +38,7 @@ import {import { join } from 'node:path';import { tmpdir } from 'node:os';import {+ acquireLock,authorizeMcpTool,autoMigratePolicyStateIfNeeded,classifyMcpTool,@@ -318,14 +320,43 @@ describe('policy state.lock unsafe age-only takeover is refused', () => {children.push(spawn('sh', ['-c', 'sleep 2; : > "$1"', 'sh', fifo], { stdio: 'ignore' }));const started = Date.now();- await expect(authorizeRead(root)).rejects.toThrow('policy-state-lock-timeout');- expect(Date.now() - started).toBeLessThan(LOCK_WAIT_MS + 1_000);+ // A non-regular lock path is a path-type error, not lock contention: fail+ // immediately instead of waiting the full lock budget and misreporting a+ // timeout. O_NOFOLLOW also prevents create-through-symlink.+ await expect(authorizeRead(root)).rejects.toThrow('policy-state-lock-error: lock path is not a regular file');+ expect(Date.now() - started).toBeLessThan(LOCK_WAIT_MS / 5);// No nonregular owner or target is removed.expect(existsSync(fifo)).toBe(true);}, 15_000);});+describe('lock path type and creation failures', () => {+ it('fails immediately when the lock path is a directory', async () => {+ const root = mkdtempSync(join(tmpdir(), 'ruflo-policy-lock-dir-'));+ roots.push(root);+ const lock = join(root, 'state.lock');+ mkdirSync(lock);++ const started = Date.now();+ await expect(acquireLock(lock)).rejects.toThrow('policy-state-lock-error: lock path is not a regular file');+ expect(Date.now() - started).toBeLessThan(LOCK_WAIT_MS / 5);+ expect(existsSync(lock)).toBe(true);+ });++ it.skipIf(process.platform === 'win32')('does not create through a dangling symlink', async () => {+ const root = mkdtempSync(join(tmpdir(), 'ruflo-policy-lock-link-'));+ roots.push(root);+ const lock = join(root, 'state.lock');+ const target = join(root, 'attacker-target');+ symlinkSync(target, lock);++ await expect(acquireLock(lock)).rejects.toThrow(/policy-state-lock-error/);+ expect(existsSync(target)).toBe(false);+ expect(lstatSync(lock).isSymbolicLink()).toBe(true);+ });+});+describe('serialized dead-owner reclamation', () => {it('a competing reclaimer sees the guard before it can remove a new owner', async () => {const { root, lock, owner } = await setup(); const dead = await deadPid();diff --git a/v3/@claude-flow/cli/src/services/policy-runtime.ts b/v3/@claude-flow/cli/src/services/policy-runtime.tsindex 755ee202b2..6cf01b3975 100644--- a/v3/@claude-flow/cli/src/services/policy-runtime.ts+++ b/v3/@claude-flow/cli/src/services/policy-runtime.ts@@ -196,9 +196,13 @@ function reclaimDeadLock(lockPath: string): void {*/export async function acquireLock(lockPath: string): Promise<() => void> {const started = Date.now();+ // O_NOFOLLOW closes the create-through-a-symlink path on platforms that+ // support it; lstat below still rejects non-regular existing paths.+ const createFlags = fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL+ | (process.platform === 'win32' ? 0 : (fsConstants.O_NOFOLLOW ?? 0));while (Date.now() - started < LOCK_WAIT_MS) {try {- const fd = openSync(lockPath, 'wx', 0o600);+ const fd = openSync(lockPath, createFlags, 0o600);const ownership = JSON.stringify({pid: process.pid,acquiredAt: Date.now(),@@ -212,7 +216,28 @@ export async function acquireLock(lockPath: string): Promise<() => void> {return () => {try { if (readLockFile(lockPath) === ownership) unlinkSync(lockPath); } catch { /* already released */ }};- } catch {+ } catch (error) {+ const code = (error as NodeJS.ErrnoException)?.code;+ // Only EEXIST is normal lock contention. Permission/path/IO failures+ // must not masquerade as a five-second timeout on every MCP call.+ // ELOOP is O_NOFOLLOW rejecting a symlink at the lock path.+ if (code === 'ELOOP') {+ throw new Error('policy-state-lock-error: lock path is not a regular file');+ }+ if (code !== 'EEXIST') {+ throw new Error(`policy-state-lock-error: ${code ?? (error instanceof Error ? error.message : String(error))}`);+ }+ // EEXIST can also mean a directory/FIFO/symlink was planted at the lock+ // path. Those are never a live lock: fail immediately instead of+ // waiting, and never follow/remove the non-regular target.+ try {+ if (!lstatSync(lockPath).isFile()) {+ throw new Error('policy-state-lock-error: lock path is not a regular file');+ }+ } catch (statError) {+ if (statError instanceof Error && statError.message.startsWith('policy-state-lock-error:')) throw statError;+ // The lock vanished between open() and lstat(); retry immediately.+ }reclaimDeadLock(lockPath);await sleep(10);}