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): synchronize workers when task_update reassigns a task

Rudy Celekli3b66a3abbdab
diff --git a/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts b/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts
new file mode 100644
index 0000000000..2a5b67e949
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts
@@ -0,0 +1,70 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+const state = vi.hoisted(() => ({ cwd: '', failSave: false }));
+vi.mock('node:fs', async importOriginal => {
+ const fs = await importOriginal<typeof import('node:fs')>();
+ return { ...fs, writeFileSync: (...args: Parameters<typeof fs.writeFileSync>) => {
+ if (state.failSave && String(args[0]).endsWith('/tasks/store.json')) throw new Error('save failed');
+ return fs.writeFileSync(...args);
+ } };
+});
+vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd }));
+import { taskTools } from '../src/mcp-tools/task-tools.js';
+const call = (name: string, input: Record<string, unknown>) => taskTools.find(t => t.name === name)!.handler(input) as Promise<any>;
+const stores = ['agents/store.json', 'agents.json'];
+const agents = (file: string) => JSON.parse(readFileSync(join(state.cwd, '.claude-flow', file), 'utf8')).agents;
+beforeEach(() => {
+ state.failSave = false;
+ state.cwd = mkdtempSync(join(tmpdir(), 'ruflo-task-reassign-'));
+ mkdirSync(join(state.cwd, '.claude-flow/agents'), { recursive: true });
+ for (const file of stores) writeFileSync(join(state.cwd, '.claude-flow', file), JSON.stringify({ agents: {
+ old: { status: 'idle', currentTask: null, taskCount: 0 },
+ next: { status: 'idle', currentTask: null, taskCount: 0 },
+ } }));
+});
+afterEach(() => rmSync(state.cwd, { recursive: true, force: true }));
+it('reassigns through task_update with the same worker state as task_assign in both stores', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ await call('task_update', { taskId, assignTo: ['next'] });
+ for (const file of stores) {
+ expect(agents(file).old).toMatchObject({ status: 'idle', currentTask: null });
+ expect(agents(file).next).toMatchObject({ status: 'busy', currentTask: taskId });
+ }
+ await call('task_complete', { taskId });
+ for (const file of stores) expect(agents(file).next).toMatchObject({ status: 'idle', currentTask: null, taskCount: 1 });
+});
+it('an empty assignment releases the former worker', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ await call('task_update', { taskId, assignTo: [] });
+ for (const file of stores) expect(agents(file).old).toMatchObject({ status: 'idle', currentTask: null });
+});
+it.each(['completed', 'failed', 'cancelled'])('a simultaneous %s and reassignment frees the workers that actually held the task', async status => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ await call('task_update', { taskId, status, assignTo: ['next'] });
+ for (const file of stores) {
+ expect(agents(file).old).toMatchObject({ status: 'idle', currentTask: null, taskCount: status === 'completed' ? 1 : 0 });
+ expect(agents(file).next).toMatchObject({ status: 'idle', currentTask: null, taskCount: 0 });
+ }
+});
+
+it('preserves both worker stores when saving the reassignment fails', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ const before = stores.map(file => readFileSync(join(state.cwd, '.claude-flow', file), 'utf8'));
+ state.failSave = true;
+ await expect(call('task_update', { taskId, assignTo: ['next'] })).rejects.toThrow('save failed');
+ expect(stores.map(file => readFileSync(join(state.cwd, '.claude-flow', file), 'utf8'))).toEqual(before);
+});
+it('does not free a former assignee who now holds another task', async () => {
+ const first = await call('task_create', { type: 'test', description: 'first' });
+ const second = await call('task_create', { type: 'test', description: 'second' });
+ await call('task_assign', { taskId: first.taskId, agentIds: ['old'] });
+ await call('task_assign', { taskId: second.taskId, agentIds: ['old'] });
+ await call('task_update', { taskId: first.taskId, assignTo: ['next'] });
+ for (const file of stores) expect(agents(file).old).toMatchObject({ status: 'busy', currentTask: second.taskId });
+});
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 937e7454f5..167d4df28b 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
@@ -374,6 +374,7 @@ export const taskTools: MCPTool[] = [
// Worker effects run AFTER the task store is saved, as task_complete does: a failed save must not leave
// workers freed and counted against a task that still reads unfinished.
let afterSave: (() => void) | undefined;
+ const previouslyAssigned = [...task.assignedTo];
if (status !== undefined) {
const newStatus = status;
const wasCompleted = task.status === 'completed';
@@ -386,13 +387,13 @@ export const taskTools: MCPTool[] = [
// returns early because the status already reads completed.
if (newStatus === 'failed' || newStatus === 'cancelled') {
task.completedAt = new Date().toISOString();
- afterSave = () => releaseAgents(task.assignedTo, taskId);
+ afterSave = () => releaseAgents(previouslyAssigned, taskId);
}
if (newStatus === 'completed' && !wasCompleted) {
task.completedAt = new Date().toISOString();
// Credited only to a worker still holding this task, so complete -> pending -> complete
// (no reassignment in between) never counts the same work twice.
- afterSave = () => updateAgents(task.assignedTo, (agent) => {
+ afterSave = () => updateAgents(previouslyAssigned, (agent) => {
if (agent.currentTask === taskId) {
agent.status = 'idle';
agent.currentTask = null;
@@ -408,6 +409,20 @@ export const taskTools: MCPTool[] = [
if (task.status === 'completed') task.progress = 100;
if (input.assignTo) {
task.assignedTo = input.assignTo as string[];
+ // Reassignment must update the same worker stores as task_assign.
+ // Terminal transitions release/credit the workers that held the task
+ // before this update; replacement assignees did not perform that work.
+ if (!['completed', 'failed', 'cancelled'].includes(task.status)) {
+ afterSave = () => {
+ releaseAgents(previouslyAssigned.filter(id => !task.assignedTo.includes(id)), taskId);
+ updateAgents(task.assignedTo, agent => {
+ agent.status = 'busy';
+ agent.currentTask = taskId;
+ });
+ };
+ } else if (!afterSave) {
+ afterSave = () => releaseAgents(previouslyAssigned, taskId);
+ }
}
if (input.result && typeof input.result === 'object') {
task.result = input.result as Record<string, unknown>;