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(learning): retain uncheckpointed steps and serialize updates

Rudy Celekli6413557bc3ab
diff --git a/v3/@claude-flow/cli/__tests__/trajectory-recovery-3199.test.ts b/v3/@claude-flow/cli/__tests__/trajectory-recovery-3199.test.ts
index f4f30e5f1b..fbebb2aeba 100644
--- a/v3/@claude-flow/cli/__tests__/trajectory-recovery-3199.test.ts
+++ b/v3/@claude-flow/cli/__tests__/trajectory-recovery-3199.test.ts
@@ -35,6 +35,46 @@ describe('pending trajectory recovery (#3199)', () => {
expect(await d.hooksTrajectoryEnd.handler({ trajectoryId: started.trajectoryId })).toMatchObject({ persisted: false });
});
+ it('retains an acknowledged live step when its checkpoint fails', async () => {
+ const a = await fresh();
+ const started = await a.hooksTrajectoryStart.handler({ task: 'dirty checkpoint' }) as any;
+ storeEntry.mockResolvedValueOnce({ success: false, id: '', error: 'disk full' });
+ expect(await a.hooksTrajectoryStep.handler({ trajectoryId: started.trajectoryId, action: 'must survive' })).toMatchObject({ recorded: true, persisted: false });
+ expect(await a.hooksTrajectoryEnd.handler({ trajectoryId: started.trajectoryId })).toMatchObject({ persisted: true, trajectory: { totalSteps: 1 } });
+ expect(JSON.parse(durable.get(`trajectory-${started.trajectoryId}`)!).steps[0].action).toBe('must survive');
+ });
+
+ it('keeps both concurrent same-process steps in the final outcome', async () => {
+ const a = await fresh();
+ const started = await a.hooksTrajectoryStart.handler({ task: 'concurrent steps' }) as any;
+ let release!: () => void;
+ let checkpointStarted!: () => void;
+ const waiting = new Promise<void>(resolve => { release = resolve; });
+ const startedCheckpoint = new Promise<void>(resolve => { checkpointStarted = resolve; });
+ storeEntry.mockImplementationOnce(async ({ key, value }) => {
+ checkpointStarted();
+ await waiting;
+ durable.set(key, value);
+ return { success: true, id: key };
+ });
+ const first = a.hooksTrajectoryStep.handler({ trajectoryId: started.trajectoryId, action: 'first' });
+ await startedCheckpoint;
+ const second = a.hooksTrajectoryStep.handler({ trajectoryId: started.trajectoryId, action: 'second' });
+ await new Promise(resolve => setImmediate(resolve));
+ release();
+ await Promise.all([first, second]);
+ expect(await a.hooksTrajectoryEnd.handler({ trajectoryId: started.trajectoryId })).toMatchObject({ persisted: true, trajectory: { totalSteps: 2 } });
+ expect(JSON.parse(durable.get(`trajectory-${started.trajectoryId}`)!).steps.map((step: any) => step.action)).toEqual(['first', 'second']);
+ });
+
+ it('reloads a clean cache after another process adds a step', async () => {
+ const a = await fresh();
+ const started = await a.hooksTrajectoryStart.handler({ task: 'cross-process step' }) as any;
+ const b = await fresh();
+ await b.hooksTrajectoryStep.handler({ trajectoryId: started.trajectoryId, action: 'from b' });
+ expect(await a.hooksTrajectoryEnd.handler({ trajectoryId: started.trajectoryId })).toMatchObject({ persisted: true, trajectory: { totalSteps: 1 } });
+ });
+
it('retains the pending trajectory when final persistence fails', async () => {
const a = await fresh();
const started = await a.hooksTrajectoryStart.handler({ task: 'retry work' }) as any;
diff --git a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
index d084d4abd0..8dbadeab6b 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts
@@ -511,14 +511,34 @@ interface TrajectoryData {
// Cache pending trajectories; the existing memory store also persists checkpoints.
const activeTrajectories = new Map<string, TrajectoryData>();
+const dirtyTrajectories = new Set<string>();
+const trajectoryOperations = new Map<string, Promise<void>>();
+
+/** Serialize read/modify/checkpoint operations for a trajectory in this process. */
+async function withTrajectoryOperation<T>(id: string, operation: () => Promise<T>): Promise<T> {
+ const previous = trajectoryOperations.get(id) ?? Promise.resolve();
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const current = previous.then(() => gate);
+ trajectoryOperations.set(id, current);
+ await previous;
+ try { return await operation(); }
+ finally {
+ release();
+ if (trajectoryOperations.get(id) === current) trajectoryOperations.delete(id);
+ }
+}
+
async function persistPendingTrajectory(trajectory: TrajectoryData): Promise<boolean> {
+ dirtyTrajectories.add(trajectory.id);
try {
const store = await getRealStoreFunction();
const result = await store?.({
key: `trajectory-pending-${trajectory.id}`, value: JSON.stringify(trajectory),
namespace: 'trajectories', tags: [trajectory.agent, 'pending', 'sona-trajectory'], upsert: true,
});
+ if (result?.success === true) dirtyTrajectories.delete(trajectory.id);
return result?.success === true;
} catch { return false; }
}
@@ -531,8 +551,12 @@ async function loadPendingTrajectory(id: string): Promise<TrajectoryData | undef
const completed = await getEntry({ key: `trajectory-${id}`, namespace: 'trajectories' });
if (completed.success && completed.found) {
activeTrajectories.delete(id);
+ dirtyTrajectories.delete(id);
return undefined;
}
+ // Failed checkpoints must not erase already-acknowledged live steps.
+ // Clean caches still reload so another process's later checkpoint is seen.
+ if (dirtyTrajectories.has(id) && activeTrajectories.has(id)) return activeTrajectories.get(id);
const pending = await getEntry({ key: `trajectory-pending-${id}`, namespace: 'trajectories' });
if (pending.success && pending.found) {
const value = JSON.parse(pending.entry?.content ?? 'null');
@@ -3071,7 +3095,7 @@ export const hooksTrajectoryStep: MCPTool = {
},
required: ['trajectoryId', 'action'],
},
- handler: async (params: Record<string, unknown>) => {
+ handler: async (params: Record<string, unknown>) => withTrajectoryOperation(params.trajectoryId as string, async () => {
const trajectoryId = params.trajectoryId as string;
// #14: scrub extended-thinking blocks so reasoning tokens don't contaminate
// the learning signal (DISTILL embeds this text).
@@ -3133,7 +3157,7 @@ export const hooksTrajectoryStep: MCPTool = {
totalSteps: trajectory?.steps.length || 0,
implementation: trajectory ? 'real-step-recording' : 'trajectory-not-found',
};
- },
+ }),
};
export const hooksTrajectoryEnd: MCPTool = {
@@ -3148,7 +3172,7 @@ export const hooksTrajectoryEnd: MCPTool = {
},
required: ['trajectoryId'],
},
- handler: async (params: Record<string, unknown>) => {
+ handler: async (params: Record<string, unknown>) => withTrajectoryOperation(params.trajectoryId as string, async () => {
const trajectoryId = params.trajectoryId as string;
{ const v = validateIdentifier(trajectoryId, 'trajectoryId'); if (!v.valid) return { success: false, error: v.error }; }
@@ -3192,6 +3216,7 @@ export const hooksTrajectoryEnd: MCPTool = {
// data from being replayed even if cleanup itself fails.
if (persistResult.success) {
activeTrajectories.delete(trajectoryId);
+ dirtyTrajectories.delete(trajectoryId);
try {
const { deleteEntry } = await import('../memory/memory-initializer.js');
await deleteEntry({ key: `trajectory-pending-${trajectoryId}`, namespace: 'trajectories' });
@@ -3419,7 +3444,7 @@ export const hooksTrajectoryEnd: MCPTool = {
? `SONA learned pattern "${sonaResult.patternKey}" with ${(sonaResult.confidence * 100).toFixed(1)}% confidence`
: (persistResult.success ? 'Trajectory persisted for future learning' : (persistResult.error || 'Trajectory not found')),
};
- },
+ }),
};
// Pattern store/search hooks - REAL implementation using storeEntry