Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(policy): keep temps of live writers and raise the orphan age floor to 10 min

Dragan Spiridonov547ae87d47a1
diff --git a/v3/@claude-flow/cli/__tests__/policy-orphan-temp-reap-3164.test.ts b/v3/@claude-flow/cli/__tests__/policy-orphan-temp-reap-3164.test.ts
index e729558d70..595312157a 100644
--- a/v3/@claude-flow/cli/__tests__/policy-orphan-temp-reap-3164.test.ts
+++ b/v3/@claude-flow/cli/__tests__/policy-orphan-temp-reap-3164.test.ts
@@ -7,9 +7,10 @@ import './helpers/isolated-policy-trust.js';
* On a mature ledger each one is ~250 MB.
*
* Every writer of these files holds the project's state.lock, so a
- * transaction that holds the lock may remove temp files of exactly those
- * names. It must leave everything else alone: fresh temps (age floor),
- * other files' temps, symlinks, and names that only look similar.
+ * transaction or verify that holds the lock may remove temp files of exactly
+ * those names. It must leave everything else alone: temps younger than the
+ * 10-minute floor, temps whose writer pid is alive, other files' temps,
+ * symlinks, and names that only look similar.
*/
import { afterEach, describe, expect, it } from 'vitest';
@@ -42,7 +43,7 @@ function project(): string {
return root;
}
-function aged(file: string, contents = 'partial', ageMs = 10 * 60_000): string {
+function aged(file: string, contents = 'partial', ageMs = 60 * 60_000): string {
writeFileSync(file, contents);
const then = (Date.now() - ageMs) / 1000;
utimesSync(file, then, then);
@@ -62,7 +63,7 @@ describe('#3164 orphaned atomic-write temp files', () => {
const dir = join(root, '.claude-flow', 'policy');
const orphans = [
aged(join(dir, temp('state.json'))),
- aged(join(dir, temp('state.json', 1))),
+ aged(join(dir, temp('state.json', 999_998))),
aged(join(dir, temp('ledger-anchors.json'))),
];
@@ -97,6 +98,9 @@ describe('#3164 orphaned atomic-write temp files', () => {
symlinkSync(target, link);
const kept = [
aged(join(dir, temp('state.json')), 'in flight', 1_000),
+ aged(join(dir, temp('state.json')), 'suspended writer', 5 * 60_000),
+ aged(join(dir, temp('state.json', process.pid)), 'live writer'),
+ aged(join(dir, temp('ledger-anchors.json', process.pid)), 'live writer'),
aged(join(dir, temp('receipts.ledger.jsonl'))),
aged(join(dir, temp('other.json'))),
aged(join(dir, 'state.json.tmp')),
@@ -112,14 +116,26 @@ describe('#3164 orphaned atomic-write temp files', () => {
expect(existsSync(target)).toBe(true);
});
- it('never fails the transaction when the policy directory cannot be listed cleanly', async () => {
+ it('skips a directory with a temp-like name without failing the transaction', async () => {
const root = project();
await transact(root);
- const dir = join(root, '.claude-flow', 'policy');
- // A directory with a temp-like name cannot be unlinked; it must be skipped.
- mkdirSync(join(dir, temp('state.json')));
+ const dir = join(root, '.claude-flow', 'policy', temp('state.json'));
+ mkdirSync(dir);
+ const then = (Date.now() - 60 * 60_000) / 1000;
+ utimesSync(dir, then, then);
await expect(transact(root)).resolves.toBeUndefined();
+ expect(existsSync(dir)).toBe(true);
+ expect((await verifyPolicyLedger(root)).valid).toBe(true);
+ });
+
+ it('removes orphans during a verify that holds the lock, without a transaction', async () => {
+ const root = project();
+ await transact(root);
+ const orphan = aged(join(root, '.claude-flow', 'policy', temp('state.json')));
+
expect((await verifyPolicyLedger(root)).valid).toBe(true);
+
+ expect(existsSync(orphan)).toBe(false);
});
});
diff --git a/v3/@claude-flow/cli/src/services/policy-runtime.ts b/v3/@claude-flow/cli/src/services/policy-runtime.ts
index 2bd6244d43..755ee202b2 100644
--- a/v3/@claude-flow/cli/src/services/policy-runtime.ts
+++ b/v3/@claude-flow/cli/src/services/policy-runtime.ts
@@ -335,10 +335,13 @@ async function writeJsonAtomic(file: string, value: unknown): Promise<void> {
// cleanup, so a full-size copy of the file stays behind forever (~250 MB each
// on a mature ledger). Every writer of these files holds the project's
// state.lock, so while the lock is held no temp file for them can belong to a
-// write in progress. The age floor keeps removal conservative should that
-// invariant ever be broken; a skipped orphan is removed by a later call.
-const ORPHAN_TEMP_MIN_AGE_MS = 60_000;
-const ATOMIC_TEMP = /^(.+)\.\d+\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp$/;
+// write in progress. That invariant has known gaps: released versions before
+// the dead-owner fix take over any lock older than 30 s, even a live owner's
+// (a writer suspended mid-write, e.g. a sleeping laptop). So a temp is also
+// kept while its writer pid is alive on this host, and until it is
+// ORPHAN_TEMP_MIN_AGE_MS old; a skipped orphan is removed by a later call.
+const ORPHAN_TEMP_MIN_AGE_MS = 10 * 60_000;
+const ATOMIC_TEMP = /^(.+)\.(\d+)\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp$/;
const PROJECT_ATOMIC_FILES = new Set([POLICY_FILE, ANCHOR_LOG_FILE]);
const TRUST_ATOMIC_FILES = new Set([STATE_ANCHOR_FILE, ANCHOR_MIRROR_FILE]);
@@ -349,6 +352,8 @@ function reapOrphanedTemps(dir: string, files: ReadonlySet<string>): void {
for (const name of names) {
const match = ATOMIC_TEMP.exec(name);
if (!match || !files.has(match[1])) continue;
+ const pid = Number(match[2]);
+ if (Number.isSafeInteger(pid) && pid > 0 && isPidAlive(pid)) continue;
const file = join(dir, name);
try {
const stat = lstatSync(file);