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(session): restore imported state before reporting activation

Rudy Celekli3fb168e686d2
diff --git a/v3/@claude-flow/cli/__tests__/session-import-activation.test.ts b/v3/@claude-flow/cli/__tests__/session-import-activation.test.ts
new file mode 100644
index 0000000000..d612a083b2
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/session-import-activation.test.ts
@@ -0,0 +1,30 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+const state=vi.hoisted(()=>({cwd:'',storeEntry:vi.fn(async()=>({success:true}))}));
+vi.mock('../src/mcp-tools/types.js',()=>({getProjectCwd:()=>state.cwd}));
+vi.mock('../src/memory/memory-initializer.js',()=>({storeEntry:state.storeEntry}));
+import {sessionTools} from '../src/mcp-tools/session-tools.js';
+const call=(name:string,input:any):Promise<any>=>sessionTools.find(t=>t.name===name)!.handler(input) as Promise<any>;
+const file=(kind:string)=>join(state.cwd,'.claude-flow',kind,'store.json');
+beforeEach(()=>{
+ state.cwd=mkdtempSync(join(tmpdir(),'ruflo-import-'));state.storeEntry.mockClear();
+ for(const kind of ['tasks','agents','memory']){mkdirSync(join(state.cwd,'.claude-flow',kind),{recursive:true});writeFileSync(file(kind),JSON.stringify({live:kind}));}
+ writeFileSync(join(state.cwd,'input.json'),JSON.stringify({name:'imported',stats:{tasks:1,agents:1,memoryEntries:1,totalSize:0},data:{tasks:{tasks:{saved:{description:'snapshot'}}},agents:{agents:{saved:{agentId:'saved'}}},memory:{entries:{saved:{key:'saved',value:'snapshot'}}}}}));
+});
+afterEach(()=>rmSync(state.cwd,{recursive:true,force:true}));
+it('activates imported task, agent and memory state before reporting activated',async()=>{
+ const result=await call('session_import',{inputPath:join(state.cwd,'input.json'),activate:true});
+ expect(result.activated).toBe(true);
+ expect(JSON.parse(readFileSync(file('tasks'),'utf8')).tasks.saved.description).toBe('snapshot');
+ expect(JSON.parse(readFileSync(file('agents'),'utf8')).agents.saved.agentId).toBe('saved');
+ expect(JSON.parse(readFileSync(file('memory'),'utf8')).entries.saved.value).toBe('snapshot');
+ expect(state.storeEntry).toHaveBeenCalledWith(expect.objectContaining({key:'saved',value:'snapshot',upsert:true}));
+});
+it('imports without changing live stores when activation is omitted',async()=>{
+ const result=await call('session_import',{inputPath:join(state.cwd,'input.json')});expect(result.activated).toBe(false);
+ for(const kind of ['tasks','agents','memory'])expect(JSON.parse(readFileSync(file(kind),'utf8'))).toEqual({live:kind});
+ expect(state.storeEntry).not.toHaveBeenCalled();
+ expect(await call('session_info',{sessionId:result.sessionId})).toMatchObject({name:'imported'});
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
index 5d9af7682b..244d1e0be8 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/session-tools.ts
@@ -523,7 +523,7 @@ export const sessionTools: MCPTool[] = [
properties: {
inputPath: { type: 'string', description: 'Path to the session JSON file to import' },
name: { type: 'string', description: 'Override the imported session name' },
- activate: { type: 'boolean', description: 'Make the imported session the current one (advisory)' },
+ activate: { type: 'boolean', description: 'Restore the imported session into the active stores' },
},
required: ['inputPath'],
},
@@ -544,6 +544,15 @@ export const sessionTools: MCPTool[] = [
data: parsed.data,
};
saveSession(session);
+ let activated = false;
+ if (input.activate === true) {
+ const restore = sessionTools.find(tool => tool.name === 'session_restore')!;
+ const result = await restore.handler({ sessionId: newId }) as { restored?: boolean; error?: string };
+ if (result.restored !== true) {
+ return { sessionId: newId, activated: false, error: result.error || 'Imported session could not be restored' };
+ }
+ activated = true;
+ }
return {
sessionId: newId,
name: session.name,
@@ -553,7 +562,7 @@ export const sessionTools: MCPTool[] = [
tasksImported: stats.tasks,
memoryEntriesImported: stats.memoryEntries,
},
- activated: input.activate === true,
+ activated,
};
},
},