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(tasks): preserve current agent ownership during task retirement

Rudy Celeklie22b3c94148b
diff --git a/v3/@claude-flow/cli/__tests__/task-agent-ownership.test.ts b/v3/@claude-flow/cli/__tests__/task-agent-ownership.test.ts
new file mode 100644
index 0000000000..5be5c97904
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/task-agent-ownership.test.ts
@@ -0,0 +1,36 @@
+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: '' }));
+vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd }));
+import { taskTools } from '../src/mcp-tools/task-tools.js';
+const call = (name: string, input: any):Promise<any> => taskTools.find(t=>t.name===name)!.handler(input) as Promise<any>;
+const agentsPath=()=>join(state.cwd,'.claude-flow/agents/store.json');
+const agent=()=>JSON.parse(readFileSync(agentsPath(),'utf8')).agents.worker;
+beforeEach(() => {
+ state.cwd=mkdtempSync(join(tmpdir(),'ruflo-task-ownership-'));
+ mkdirSync(join(state.cwd,'.claude-flow/agents'),{recursive:true});
+ writeFileSync(agentsPath(),JSON.stringify({agents:{worker:{agentId:'worker',status:'idle',currentTask:null,taskCount:0}},version:'3.0.0'}));
+});
+afterEach(()=>rmSync(state.cwd,{recursive:true,force:true}));
+const create=()=>call('task_create',{type:'test',description:'task'});
+it.each(['task_complete','task_cancel','task_assign'])('%s for an old task does not erase a newer assignment',async name=>{
+ const a=await create(),b=await create();
+ await call('task_assign',{taskId:a.taskId,agentIds:['worker']});
+ await call('task_assign',{taskId:b.taskId,agentIds:['worker']});
+ await call(name,{taskId:a.taskId,unassign:true});
+ expect(agent()).toMatchObject({status:'busy',currentTask:b.taskId});
+});
+it('cancelling the owned task releases the worker',async()=>{
+ const a=await create();await call('task_assign',{taskId:a.taskId,agentIds:['worker']});
+ await call('task_cancel',{taskId:a.taskId});
+ expect(agent()).toMatchObject({status:'idle',currentTask:null});
+});
+it('repeated completion is idempotent and preserves result and completion time',async()=>{
+ const a=await create();await call('task_assign',{taskId:a.taskId,agentIds:['worker']});
+ const first=await call('task_complete',{taskId:a.taskId,result:{answer:42}});
+ const repeated=await call('task_complete',{taskId:a.taskId});
+ expect(agent()).toMatchObject({status:'idle',currentTask:null,taskCount:1});
+ expect(repeated).toEqual(first);
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
index 68e111c8f1..152ee9cbe6 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
@@ -250,6 +250,9 @@ export const taskTools: MCPTool[] = [
const task = store.tasks[taskId];
if (task) {
+ if (task.status === 'completed') {
+ return { taskId: task.taskId, status: task.status, completedAt: task.completedAt, result: task.result };
+ }
task.status = 'completed';
task.progress = 100;
task.completedAt = new Date().toISOString();
@@ -266,8 +269,10 @@ export const taskTools: MCPTool[] = [
}
for (const agentId of task.assignedTo) {
if (agentStore.agents[agentId]) {
- agentStore.agents[agentId].status = 'idle';
- agentStore.agents[agentId].currentTask = null;
+ if (agentStore.agents[agentId].currentTask === taskId) {
+ agentStore.agents[agentId].status = 'idle';
+ agentStore.agents[agentId].currentTask = null;
+ }
agentStore.agents[agentId].taskCount =
((agentStore.agents[agentId].taskCount as number) || 0) + 1;
}
@@ -388,7 +393,7 @@ export const taskTools: MCPTool[] = [
if (input.unassign) {
// Revert previously assigned agents to idle
for (const agentId of previouslyAssigned) {
- if (agentStore.agents[agentId]) {
+ if (agentStore.agents[agentId]?.currentTask === taskId) {
agentStore.agents[agentId].status = 'idle';
agentStore.agents[agentId].currentTask = null;
}
@@ -398,7 +403,7 @@ export const taskTools: MCPTool[] = [
const agentIds = (input.agentIds as string[]) || [];
// Revert old agents to idle
for (const agentId of previouslyAssigned) {
- if (!agentIds.includes(agentId) && agentStore.agents[agentId]) {
+ if (!agentIds.includes(agentId) && agentStore.agents[agentId]?.currentTask === taskId) {
agentStore.agents[agentId].status = 'idle';
agentStore.agents[agentId].currentTask = null;
}
@@ -406,7 +411,7 @@ export const taskTools: MCPTool[] = [
// Set new agents to active
for (const agentId of agentIds) {
if (agentStore.agents[agentId]) {
- agentStore.agents[agentId].status = 'active';
+ agentStore.agents[agentId].status = 'busy';
agentStore.agents[agentId].currentTask = taskId;
}
}
@@ -466,6 +471,20 @@ export const taskTools: MCPTool[] = [
task.completedAt = new Date().toISOString();
task.result = { cancelReason: input.reason || 'Cancelled by user' };
saveTaskStore(store);
+ const agentStorePath = join(getProjectCwd(), STORAGE_DIR, 'agents', 'store.json');
+ try {
+ if (existsSync(agentStorePath)) {
+ const agents = JSON.parse(readFileSync(agentStorePath, 'utf-8'));
+ for (const agentId of task.assignedTo) {
+ if (agents.agents[agentId]?.currentTask === taskId) {
+ agents.agents[agentId].status = 'idle';
+ agents.agents[agentId].currentTask = null;
+ }
+ }
+ writeFileSync(agentStorePath, JSON.stringify(agents, null, 2), 'utf-8');
+ }
+ } catch { /* best-effort agent sync, as in task_complete */ }
+
return {
success: true,