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(memory): distinguish recent dead locks from live writers (#4389)

ordinary being6e213d5bb39c
diff --git a/v3/@claude-flow/cli/package.json b/v3/@claude-flow/cli/package.json
index 0ad0e023ac..feaba7a889 100644
--- a/v3/@claude-flow/cli/package.json
+++ b/v3/@claude-flow/cli/package.json
@@ -126,11 +126,11 @@
"ws": "^8.21.0",
"yaml": "^2.8.0",
"zod": "^3.22.0",
- "@claude-flow/memory": "3.0.8"
+ "@claude-flow/memory": "3.0.9"
},
"optionalDependencies": {
"@agntcy/slim-bindings": "2.1.1",
- "@claude-flow/memory": "3.0.8",
+ "@claude-flow/memory": "3.0.9",
"@metaharness/darwin": "~0.12.0",
"@metaharness/flywheel": "~0.1.10",
"@metaharness/radio": "~0.1.0",
diff --git a/v3/@claude-flow/memory/package.json b/v3/@claude-flow/memory/package.json
index afa4e0837c..45ed50a41e 100644
--- a/v3/@claude-flow/memory/package.json
+++ b/v3/@claude-flow/memory/package.json
@@ -1,6 +1,6 @@
{
"name": "@claude-flow/memory",
- "version": "3.0.8",
+ "version": "3.0.9",
"type": "module",
"description": "Memory module - AgentDB unification, HNSW indexing, vector search, hybrid SQLite+AgentDB backend (ADR-009)",
"main": "dist/index.js",
diff --git a/v3/@claude-flow/memory/src/agentdb-lock-guard.test.ts b/v3/@claude-flow/memory/src/agentdb-lock-guard.test.ts
index 3b998026bd..afbcf465cc 100644
--- a/v3/@claude-flow/memory/src/agentdb-lock-guard.test.ts
+++ b/v3/@claude-flow/memory/src/agentdb-lock-guard.test.ts
@@ -9,6 +9,7 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
+ AGENTDB_CONCURRENT_MODIFICATION_CODE,
AgentdbLockError,
closeWithAgentdbLockRecovery,
clearStaleAgentdbLock,
@@ -99,10 +100,12 @@ describe('inspectAgentdbLock / clearStaleAgentdbLock', () => {
plantLock(`${process.pid}\n`);
expect(inspectAgentdbLock(dbFile).state).toBe('live');
});
- it('dead PID but fresh lock is not cleared', () => {
+ it('dead PID but fresh lock is recent, not live, and is not cleared', () => {
plantLock(`${DEAD_PID}\n`, new Date());
const r = clearStaleAgentdbLock(dbFile);
expect(r.cleared).toBe(false);
+ expect(r.inspection.state).toBe('recent');
+ expect(r.inspection.reason).toMatch(/dead but the lock is only \d+ms old/);
expect(fs.existsSync(lockFile)).toBe(true);
});
it('non-PID content is unverifiable and untouched', () => {
@@ -172,6 +175,23 @@ describe('withAgentdbLockRecovery', () => {
expect(await rows(dbFile)).toEqual(['from-A']);
});
+ it('a recent dead lock surfaces recent guidance, not another-process guidance', async () => {
+ plantLock(`${DEAD_PID}\n`, new Date());
+ const e = Object.assign(new Error('concurrent'), { code: AGENTDB_CONCURRENT_MODIFICATION_CODE });
+ let err: unknown;
+ try {
+ await withAgentdbLockRecovery(dbFile, () => { throw e; });
+ } catch (x) {
+ err = x;
+ }
+ expect(err).toBeInstanceOf(AgentdbLockError);
+ expect((err as AgentdbLockError).lockState).toBe('recent');
+ expect((err as Error).message).toMatch(/dead but the lock is only \d+ms old/);
+ expect((err as Error).message).toMatch(/retry in a moment/i);
+ expect((err as Error).message).not.toMatch(/another process is writing/i);
+ expect(fs.existsSync(lockFile)).toBe(true);
+ });
+
it('passes unrelated errors through untouched and never inspects the filesystem for them', async () => {
const boom = new Error('boom');
await expect(withAgentdbLockRecovery(dbFile, () => { throw boom; })).rejects.toBe(boom);
diff --git a/v3/@claude-flow/memory/src/agentdb-lock-guard.ts b/v3/@claude-flow/memory/src/agentdb-lock-guard.ts
index 97576fb200..da696b2d71 100644
--- a/v3/@claude-flow/memory/src/agentdb-lock-guard.ts
+++ b/v3/@claude-flow/memory/src/agentdb-lock-guard.ts
@@ -27,7 +27,7 @@ export const AGENTDB_LOCK_SUFFIX = '.agentdb.lock';
/** A lock younger than this is never considered stale (saves take milliseconds). */
export const DEFAULT_MIN_STALE_AGE_MS = 10_000;
-export type LockState = 'absent' | 'live' | 'stale' | 'unverifiable';
+export type LockState = 'absent' | 'live' | 'recent' | 'stale' | 'unverifiable';
export interface LockInspection {
state: LockState;
@@ -142,9 +142,14 @@ export function inspectAgentdbLock(dbPath: string, options: LockGuardOptions = {
const pid = parsePid(snap.content);
if (pid === null) return { state: 'unverifiable', lockPath, reason: 'lock content is not a PID; refusing to touch it' };
if (pid === process.pid || isAlive(pid)) return { state: 'live', lockPath, pid, reason: `lock holder PID ${pid} is alive` };
- const age = now - snap.mtimeMs;
+ const age = Math.max(0, now - snap.mtimeMs);
if (age < minAge) {
- return { state: 'live', lockPath, pid, reason: `holder PID ${pid} is dead but the lock is only ${Math.round(age)}ms old` };
+ return {
+ state: 'recent',
+ lockPath,
+ pid,
+ reason: `holder PID ${pid} is dead but the lock is only ${Math.round(age)}ms old; a writer may still be finishing`,
+ };
}
return { state: 'stale', lockPath, pid, reason: `holder PID ${pid} is dead and the lock is ${Math.round(age / 1000)}s old` };
}
@@ -204,9 +209,11 @@ export async function withAgentdbLockRecovery<T>(
`AgentDB could not save "${path.basename(dbPath)}": ${inspection.reason}. ` +
(inspection.state === 'live'
? 'Another process is writing this database; retry when it finishes.'
- : inspection.state === 'absent'
- ? 'The database changed on disk after it was opened; reopen it and retry (no data was overwritten).'
- : `If no other process uses this database, remove "${path.basename(inspection.lockPath)}" manually and retry.`),
+ : inspection.state === 'recent'
+ ? 'The holder is gone but the lock is still recent; a writer may be finishing. Retry in a moment (the guard removes it once it is old enough).'
+ : inspection.state === 'absent'
+ ? 'The database changed on disk after it was opened; reopen it and retry (no data was overwritten).'
+ : `If no other process uses this database, remove "${path.basename(inspection.lockPath)}" manually and retry.`),
inspection.state,
{ cause: error },
);
diff --git a/v3/pnpm-lock.yaml b/v3/pnpm-lock.yaml
index 884747ac80..9232fff4c7 100644
--- a/v3/pnpm-lock.yaml
+++ b/v3/pnpm-lock.yaml
@@ -209,7 +209,7 @@ importers:
specifier: 2.1.1
version: 2.1.1
'@claude-flow/memory':
- specifier: 3.0.8
+ specifier: 3.0.9
version: link:../memory
'@metaharness/darwin':
specifier: ~0.12.0