From 178fb86095828ba34d785109a4fbadb12bf5d8b8 Mon Sep 17 00:00:00 2001 From: kevin9327 <5299031+kevin9327@users.noreply.github.com> Date: Thu, 5 Nov 2026 04:42:11 +0900 Subject: [PATCH] fix(tasks): mark agents assigned in task_create as busy --- .../__tests__/task-create-agent-sync.test.ts | 43 +++++++++++++++++++ .../cli/src/mcp-tools/task-tools.ts | 12 ++++++ 2 files changed, 55 insertions(+) create mode 100644 v3/@claude-flow/cli/__tests__/task-create-agent-sync.test.ts diff --git a/v3/@claude-flow/cli/__tests__/task-create-agent-sync.test.ts b/v3/@claude-flow/cli/__tests__/task-create-agent-sync.test.ts new file mode 100644 index 0000000000..d6b7a894d1 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/task-create-agent-sync.test.ts @@ -0,0 +1,43 @@ +import { afterEach, beforeEach, expect, it, vi } from 'vitest'; +import { existsSync, 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 => taskTools.find(t => t.name === name)!.handler(input) as Promise; +const agentsPath = () => join(state.cwd, '.claude-flow/agents/store.json'); +const hivePath = () => join(state.cwd, '.claude-flow/agents.json'); +const agent = (path = agentsPath()) => JSON.parse(readFileSync(path, 'utf8')).agents.worker; +beforeEach(() => { + state.cwd = mkdtempSync(join(tmpdir(), 'ruflo-task-create-sync-')); + mkdirSync(join(state.cwd, '.claude-flow/agents'), { recursive: true }); + const worker = { agentId: 'worker', status: 'idle', currentTask: null, taskCount: 0 }; + writeFileSync(agentsPath(), JSON.stringify({ agents: { worker }, version: '3.0.0' })); + writeFileSync(hivePath(), JSON.stringify({ agents: { worker } })); +}); +afterEach(() => rmSync(state.cwd, { recursive: true, force: true })); + +it('task_create with assignTo marks the worker busy in both agent stores', async () => { + const task = await call('task_create', { type: 'test', description: 'task', assignTo: ['worker'] }); + expect(agent()).toMatchObject({ status: 'busy', currentTask: task.taskId }); + expect(agent(hivePath())).toMatchObject({ status: 'busy', currentTask: task.taskId }); +}); + +it.each(['task_complete', 'task_cancel'])('%s frees a worker assigned at creation', async (name) => { + const task = await call('task_create', { type: 'test', description: 'task', assignTo: ['worker'] }); + await call(name, { taskId: task.taskId }); + expect(agent()).toMatchObject({ status: 'idle', currentTask: null }); +}); + +it('task_create without assignTo leaves workers alone', async () => { + await call('task_create', { type: 'test', description: 'task' }); + expect(agent()).toMatchObject({ status: 'idle', currentTask: null }); +}); + +it.each(['worker', [42], [{}]])('task_create refuses assignTo %j that is not an array of agent ids', async (assignTo) => { + const result = await call('task_create', { type: 'test', description: 'task', assignTo }); + expect(result).toEqual({ success: false, error: 'assignTo must be an array of agent ids' }); + expect(existsSync(join(state.cwd, '.claude-flow/tasks/store.json'))).toBe(false); + expect(agent()).toMatchObject({ status: 'idle', currentTask: null }); +}); 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 99d958be37..ab9fc0a354 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/task-tools.ts @@ -144,6 +144,10 @@ export const taskTools: MCPTool[] = [ if (!vType.valid) return { success: false, error: vType.error }; const vDesc = validateText(input.description, 'description'); if (!vDesc.valid) return { success: false, error: vDesc.error }; + 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 = `task-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; @@ -165,6 +169,14 @@ export const taskTools: MCPTool[] = [ store.tasks[taskId] = task; saveTaskStore(store); + // Assigning at creation must mark the workers busy, as task_assign and task_update do; + // otherwise they read idle while holding the task, and task_complete or task_cancel, + // which only release a worker whose currentTask is this task, never free them either. + updateAgents(task.assignedTo, (agent) => { + agent.status = 'busy'; + agent.currentTask = taskId; + }); + return { taskId, type: task.type,