🌊 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): retry transient rename failures in writeJsonAtomic, never orphan the temp file (#3398) (#3404)
rUv5f3f4df408cf
diff --git a/v3/@claude-flow/cli/__tests__/issue-3398-policy-state-rename-retry.test.ts b/v3/@claude-flow/cli/__tests__/issue-3398-policy-state-rename-retry.test.tsnew file mode 100644index 0000000000..f5025a92c3--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/issue-3398-policy-state-rename-retry.test.ts@@ -0,0 +1,131 @@+/**+ * #3398 — policy-runtime writeJsonAtomic: a single renameSync that fails with+ * EPERM (Windows: destination briefly held open by another reader, AV, an+ * indexer) failed the whole MCP call and left `state.json.<pid>.<uuid>.tmp`+ * behind.+ *+ * Windows' rename-over-open-file semantics cannot be reproduced on Linux, so+ * this test simulates them the way the reporter did: `renameSync` throws+ * EPERM/EBUSY for the first N attempts at the policy state file. What it+ * proves is the retry + cleanup logic, not the OS interaction.+ */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync } from 'node:fs';+import { join } from 'node:path';+import { tmpdir, userInfo } from 'node:os';+import { createHash } from 'node:crypto';++const renameFault = vi.hoisted(() => ({+ failures: 0,+ code: 'EPERM',+ attempts: 0,+}));++vi.mock('node:fs', async (importOriginal) => {+ const actual = await importOriginal<typeof import('node:fs')>();+ const renameSync: typeof actual.renameSync = (from, to) => {+ if (String(to).endsWith(join('.claude-flow', 'policy', 'state.json'))) {+ renameFault.attempts += 1;+ if (renameFault.failures > 0) {+ renameFault.failures -= 1;+ const error = new Error(`${renameFault.code}: operation not permitted, rename '${String(from)}' -> '${String(to)}'`) as NodeJS.ErrnoException;+ error.code = renameFault.code;+ throw error;+ }+ }+ return actual.renameSync(from, to);+ };+ return { ...actual, default: { ...actual, renameSync }, renameSync };+});++const { autoMigratePolicyStateIfNeeded, evaluatePolicyRequest } = await import('../src/services/policy-runtime.js');++const roots: Array<{ root: string; trust: string }> = [];+function project(): string {+ const root = mkdtempSync(join(tmpdir(), 'ruflo-3398-'));+ mkdirSync(join(root, '.claude-flow'), { recursive: true });+ const projectId = createHash('sha256').update(realpathSync(root)).digest('hex');+ roots.push({ root, trust: join(userInfo().homedir, '.config', 'ruflo', 'policy-trust', projectId) });+ return root;+}++function policyDir(root: string): string {+ return join(root, '.claude-flow', 'policy');+}++function orphanTemps(root: string): string[] {+ return readdirSync(policyDir(root)).filter((name) => name.endsWith('.tmp'));+}++const request = {+ identity: { id: 'agent-1', type: 'agent' as const },+ action: { type: 'memory.read', resource: 'memory_search', tool: 'memory_search' },+};++beforeEach(() => {+ renameFault.failures = 0;+ renameFault.code = 'EPERM';+ renameFault.attempts = 0;+});++afterEach(() => {+ for (const item of roots.splice(0)) {+ rmSync(item.trust, { recursive: true, force: true });+ rmSync(item.root, { recursive: true, force: true });+ }+});++describe('#3398 policy state write survives transient rename failures', () => {+ it('retries a transient EPERM rename and commits the state (no orphan .tmp)', async () => {+ const root = project();+ await autoMigratePolicyStateIfNeeded(root);+ const before = JSON.parse(readFileSync(join(policyDir(root), 'state.json'), 'utf8'));++ renameFault.failures = 2;+ renameFault.attempts = 0;+ const decision = await evaluatePolicyRequest(request, root);++ expect(decision.enforcedOutcome).toBe('allowed');+ expect(renameFault.attempts).toBe(3);+ const after = JSON.parse(readFileSync(join(policyDir(root), 'state.json'), 'utf8'));+ expect(after.receipts.length).toBe(before.receipts.length + 1);+ expect(orphanTemps(root)).toEqual([]);+ });++ it('retries EBUSY/EACCES the same way', async () => {+ const root = project();+ await autoMigratePolicyStateIfNeeded(root);+ for (const code of ['EBUSY', 'EACCES']) {+ renameFault.code = code;+ renameFault.failures = 1;+ await expect(evaluatePolicyRequest(request, root)).resolves.toMatchObject({ enforcedOutcome: 'allowed' });+ }+ expect(orphanTemps(root)).toEqual([]);+ });++ it('gives up after a bounded number of attempts, still fails closed, and removes the temp file', async () => {+ const root = project();+ await autoMigratePolicyStateIfNeeded(root);+ renameFault.failures = Number.MAX_SAFE_INTEGER;+ renameFault.attempts = 0;++ const started = Date.now();+ await expect(evaluatePolicyRequest(request, root)).rejects.toMatchObject({ code: 'EPERM' });+ expect(renameFault.attempts).toBeGreaterThan(1);+ // Bounded: must stay well inside the 5s state.lock wait other callers use.+ expect(Date.now() - started).toBeLessThan(4_000);+ expect(orphanTemps(root)).toEqual([]);+ });++ it('does not retry a non-transient error, and removes the temp file', async () => {+ const root = project();+ await autoMigratePolicyStateIfNeeded(root);+ renameFault.code = 'EXDEV';+ renameFault.failures = Number.MAX_SAFE_INTEGER;+ renameFault.attempts = 0;++ await expect(evaluatePolicyRequest(request, root)).rejects.toMatchObject({ code: 'EXDEV' });+ expect(renameFault.attempts).toBe(1);+ expect(orphanTemps(root)).toEqual([]);+ });+});diff --git a/v3/@claude-flow/cli/src/services/policy-runtime.ts b/v3/@claude-flow/cli/src/services/policy-runtime.tsindex d68014d4a1..c0359dd2af 100644--- a/v3/@claude-flow/cli/src/services/policy-runtime.ts+++ b/v3/@claude-flow/cli/src/services/policy-runtime.ts@@ -67,11 +67,35 @@ async function acquireLock(lockPath: string): Promise<() => void> {throw new Error('policy-state-lock-timeout');}-function writeJsonAtomic(file: string, value: unknown): void {+// #3398: Windows rename over a file another process holds open fails with+// EPERM/EBUSY/EACCES. Retry (~1.3s total, inside LOCK_WAIT_MS), never leave the+// temp file; a final failure still throws — the state holds the receipt ledger+// and consumed approval uses, so dropping it would let an approval be reused.+const RENAME_RETRY_DELAYS_MS = [25, 50, 100, 150, 250, 300, 400];+const TRANSIENT_RENAME_CODES = new Set(['EPERM', 'EBUSY', 'EACCES']);++async function writeJsonAtomic(file: string, value: unknown): Promise<void> {mkdirSync(dirname(file), { recursive: true, mode: 0o700 });const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;- writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });- renameSync(temporary, file);+ let renamed = false;+ try {+ writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });+ for (let attempt = 0; ; attempt++) {+ try {+ renameSync(temporary, file);+ renamed = true;+ return;+ } catch (error) {+ const code = (error as NodeJS.ErrnoException)?.code ?? '';+ if (!TRANSIENT_RENAME_CODES.has(code) || attempt >= RENAME_RETRY_DELAYS_MS.length) throw error;+ await sleep(RENAME_RETRY_DELAYS_MS[attempt]);+ }+ }+ } finally {+ if (!renamed) {+ try { unlinkSync(temporary); } catch { /* never created or already gone */ }+ }+ }}function trustPaths(projectRoot: string): { key: string; anchor: string } {@@ -112,7 +136,7 @@ function verifyStateAnchor(projectRoot: string, state: PolicyState | undefined):}}-function writePolicyState(projectRoot: string, statePath: string, state: PolicyState): void {+async function writePolicyState(projectRoot: string, statePath: string, state: PolicyState): Promise<void> {const anchorPath = trustPaths(projectRoot).anchor;if (state.mode === 'enforce' || existsSync(anchorPath)) {const key = trustKey(projectRoot, true)!;@@ -127,15 +151,15 @@ function writePolicyState(projectRoot: string, statePath: string, state: PolicyS// crash then leaves either a valid pair or an anchored mismatch that// fails closed; it can never leave enforce state silently unanchored.if (!existsSync(anchorPath)) {- writeJsonAtomic(anchorPath, anchor);- writeJsonAtomic(statePath, state);+ await writeJsonAtomic(anchorPath, anchor);+ await writeJsonAtomic(statePath, state);return;}- writeJsonAtomic(statePath, state);- writeJsonAtomic(anchorPath, anchor);+ await writeJsonAtomic(statePath, state);+ await writeJsonAtomic(anchorPath, anchor);return;}- writeJsonAtomic(statePath, state);+ await writeJsonAtomic(statePath, state);}function detectLegacyCapabilities(projectRoot: string): string {@@ -209,7 +233,7 @@ export async function autoMigratePolicyStateIfNeeded(projectRoot = process.cwd()state.mode = configured;state.configuredMode = configured;}- writePolicyState(projectRoot, target.state, state);+ await writePolicyState(projectRoot, target.state, state);}} finally {release();@@ -237,7 +261,7 @@ export async function withPolicyTransaction<T>(const result = await operation(engine);const nextState = engine.exportState();if (!engine.verifyLedger().valid) throw new Error('policy-ledger-verification-failed');- writePolicyState(projectRoot, target.state, nextState);+ await writePolicyState(projectRoot, target.state, nextState);return result;} finally {release();