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): validate reassignment and preserve retained worker ownership

Rudy Celekli02f4c87dd95a
diff --git a/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts b/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts
index 2a5b67e949..57231ecf6f 100644
--- a/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts
+++ b/v3/@claude-flow/cli/__tests__/task-update-assignment.test.ts
@@ -2,11 +2,12 @@ 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 }));
+const state = vi.hoisted(() => ({ cwd: '', failSave: false, failAgentSave: 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');
+ if (state.failAgentSave && String(args[0]).endsWith('/agents/store.json')) throw new Error('agent save failed');
return fs.writeFileSync(...args);
} };
});
@@ -17,6 +18,7 @@ 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.failAgentSave = 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: {
@@ -25,7 +27,7 @@ beforeEach(() => {
} }));
});
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 () => {
+it('reassigns through task_update and synchronizes newly assigned workers 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'] });
@@ -68,3 +70,78 @@ it('does not free a former assignee who now holds another task', async () => {
await call('task_update', { taskId: first.taskId, assignTo: ['next'] });
for (const file of stores) expect(agents(file).old).toMatchObject({ status: 'busy', currentTask: second.taskId });
});
+
+const taskStorePath = () => join(state.cwd, '.claude-flow/tasks/store.json');
+it('an echoed assignee keeps the other task it acquired', 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, progress: 50, assignTo: ['old', 'next'] });
+ for (const file of stores) {
+ expect(agents(file).old).toMatchObject({ status: 'busy', currentTask: second.taskId });
+ expect(agents(file).next).toMatchObject({ status: 'busy', currentTask: first.taskId });
+ }
+});
+it.each(['task_update', 'task_assign'])('%s does not mutate an inherited prototype entry', async name => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ const previous = new Map(['status', 'currentTask'].map(key => [key, Object.getOwnPropertyDescriptor(Object.prototype, key)]));
+ try {
+ await call(name, name === 'task_update' ? { taskId, assignTo: ['__proto__'] } : { taskId, agentIds: ['__proto__'] });
+ expect(Object.getOwnPropertyDescriptor(Object.prototype, 'status')).toEqual(previous.get('status'));
+ expect(Object.getOwnPropertyDescriptor(Object.prototype, 'currentTask')).toEqual(previous.get('currentTask'));
+ for (const file of stores) expect(Object.keys(agents(file))).toEqual(['old', 'next']);
+ } finally {
+ for (const key of ['status', 'currentTask']) {
+ const descriptor = previous.get(key);
+ if (descriptor) Object.defineProperty(Object.prototype, key, descriptor);
+ else delete (Object.prototype as Record<string, unknown>)[key];
+ }
+ }
+});
+it.each(['old', 7, null, {}, [''], ['old', 7], ['old', '../bad']].map(value => [value]))('refuses malformed assignTo %j before either store changes', async assignTo => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ const paths = [taskStorePath(), ...stores.map(file => join(state.cwd, '.claude-flow', file))];
+ const before = paths.map(path => readFileSync(path, 'utf8'));
+ expect(await call('task_update', { taskId, progress: 50, assignTo })).toMatchObject({ success: false });
+ expect(paths.map(path => readFileSync(path, 'utf8'))).toEqual(before);
+});
+it('repairing an already malformed stored assignment succeeds', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ const store = JSON.parse(readFileSync(taskStorePath(), 'utf8'));
+ store.tasks[taskId].assignedTo = 7;
+ writeFileSync(taskStorePath(), JSON.stringify(store));
+ expect(await call('task_update', { taskId, progress: 50, assignTo: ['next'] })).toMatchObject({ success: true, assignedTo: ['next'], progress: 50 });
+ for (const file of stores) expect(agents(file).next).toMatchObject({ currentTask: taskId, status: 'busy' });
+});
+it('reopening with echoed assignees does not count the same completed work twice', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ await call('task_complete', { taskId });
+ await call('task_update', { taskId, status: 'pending', assignTo: ['old'] });
+ await call('task_update', { taskId, status: 'completed' });
+ for (const file of stores) expect(agents(file).old).toMatchObject({ currentTask: null, status: 'idle', taskCount: 1 });
+});
+it('an unchanged active assignee remains bound', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ await call('task_update', { taskId, progress: 50, assignTo: ['old'] });
+ for (const file of stores) expect(agents(file).old).toMatchObject({ currentTask: taskId, status: 'busy', taskCount: 0 });
+});
+it('characterizes the existing pending-task transition separately from worker synchronization', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_update', { taskId, assignTo: ['next'] });
+ expect(await call('task_status', { taskId })).toMatchObject({ status: 'pending', startedAt: null });
+ for (const file of stores) expect(agents(file).next).toMatchObject({ currentTask: taskId, status: 'busy' });
+});
+it('characterizes the existing best-effort agent-store write failure', async () => {
+ const { taskId } = await call('task_create', { type: 'test', description: 'work' });
+ await call('task_assign', { taskId, agentIds: ['old'] });
+ state.failAgentSave = true;
+ expect(await call('task_update', { taskId, assignTo: ['next'] })).toMatchObject({ success: true });
+ state.failAgentSave = false;
+ await call('task_update', { taskId, assignTo: ['next'] });
+ expect(agents(stores[0]).old).toMatchObject({ currentTask: taskId, status: 'busy' });
+ expect(agents(stores[1]).old).toMatchObject({ currentTask: null, status: 'idle' });
+});
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 167d4df28b..cac19fe731 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts
@@ -100,7 +100,7 @@ function updateAgents(agentIds: string[], mutate: (agent: AgentRow) => void): vo
if (!store.agents) continue;
let touched = false;
for (const id of agentIds) {
- if (store.agents[id]) {
+ if (Object.prototype.hasOwnProperty.call(store.agents, id) && store.agents[id]) {
mutate(store.agents[id]);
touched = true;
}
@@ -366,6 +366,11 @@ export const taskTools: MCPTool[] = [
return { success: false, error: `status must be one of ${TASK_STATUSES.join(', ')}` };
}
+ if (input.assignTo !== undefined && (!Array.isArray(input.assignTo)
+ || !input.assignTo.every(id => validateIdentifier(id, 'assignTo').valid))) {
+ return { success: false, error: 'assignTo must be an array of agent ids' };
+ }
+
const store = loadTaskStore();
const taskId = input.taskId as string;
const task = store.tasks[taskId];
@@ -374,7 +379,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];
+ const previouslyAssigned = Array.isArray(task.assignedTo) ? [...task.assignedTo] : [];
if (status !== undefined) {
const newStatus = status;
const wasCompleted = task.status === 'completed';
@@ -415,7 +420,8 @@ export const taskTools: MCPTool[] = [
if (!['completed', 'failed', 'cancelled'].includes(task.status)) {
afterSave = () => {
releaseAgents(previouslyAssigned.filter(id => !task.assignedTo.includes(id)), taskId);
- updateAgents(task.assignedTo, agent => {
+ // Echoing retained assignees must not take them from another task or re-credit completed work.
+ updateAgents(task.assignedTo.filter(id => !previouslyAssigned.includes(id)), agent => {
agent.status = 'busy';
agent.currentTask = taskId;
});