{"record":{"id":"0545d1620a94b783","repo":"ruvnet/ruflo","slug":"agent-has-currenttasks-active-tasks-use-force","errorCode":null,"errorMessage":"Agent has ${currentTasks} active tasks. Use force=true to terminate anyway.","messagePattern":"Agent has (.+?) active tasks\\. Use force=true to terminate anyway\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts","lineNumber":110,"sourceCode":"  tasksReassigned: number;\n}\n\n/**\n * Terminate Agent Command Handler\n */\nexport class TerminateAgentCommandHandler {\n  constructor(private readonly repository: IAgentRepository) {}\n\n  async execute(input: TerminateAgentInput): Promise<TerminateAgentResult> {\n    const agent = await this.repository.findById(input.agentId);\n    if (!agent) {\n      throw new Error(`Agent '${input.agentId}' not found`);\n    }\n\n    const currentTasks = agent.currentTaskCount;\n\n    if (currentTasks > 0 && !input.force) {\n      throw new Error(`Agent has ${currentTasks} active tasks. Use force=true to terminate anyway.`);\n    }\n\n    agent.terminate();\n    await this.repository.save(agent);\n\n    return {\n      success: true,\n      agentId: input.agentId,\n      tasksReassigned: currentTasks,\n    };\n  }\n}\n","sourceCodeStart":92,"sourceCodeEnd":123,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts#L92-L123","documentation":"TerminateAgentCommandHandler refuses to terminate an agent whose currentTaskCount > 0 unless input.force === true, protecting in-flight tasks from being orphaned. The message itself documents the escape hatch: re-issue the command with force=true when losing active work is acceptable.","triggerScenarios":"Terminating a busy agent without force while it still holds tasks; tasks that are stuck and never complete, blocking cleanup; a shutdown script that does not drain agent work queues first; scale-down logic selecting agents at random regardless of load.","commonSituations":"Autoscaler scale-down picking busy agents; operators killing agents during an incident with queued work; tests terminating agents without force while mock tasks are assigned.","solutions":["Let the agent's tasks complete (or reassign them) and terminate again without force.","If losing in-flight work is acceptable, re-send the command with force: true.","Cancel the agent's outstanding tasks first, then terminate.","In shutdown paths, drain or reassign task queues before terminating agents."],"exampleFix":"// before\nawait terminateAgent.execute({ agentId }); // agent has N active tasks\n\n// after — option A: accept losing in-flight tasks\nawait terminateAgent.execute({ agentId, force: true });\n\n// after — option B: drain first, then terminate normally\nawait drainOrReassignTasks(agentId);\nawait terminateAgent.execute({ agentId });","handlingStrategy":"validation","validationCode":"const agent = await agentRepository.findById(agentId);\nif (!agent) throw new Error(`unknown agent ${agentId}`);\nconst force = agent.currentTaskCount > 0 && inFlightWorkIsExpendable;\nif (agent.currentTaskCount > 0 && !force) {\n  await drainOrReassignTasks(agentId); // wait for tasks first\n}\nawait terminateAgentHandler.execute({ agentId, force });","typeGuard":null,"tryCatchPattern":"try {\n  await terminateAgentHandler.execute({ agentId });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('active tasks. Use force=true')) {\n    // either drain/reassign tasks and retry without force,\n    // or retry once with force: true if losing the work is acceptable\n  } else {\n    throw e;\n  }\n}","preventionTips":["Drain or reassign tasks before terminating agents in orchestration code.","Reserve force: true for shutdown/incident paths and log whenever it is used.","Check currentTaskCount before issuing terminate so the decision is explicit, not exceptional."],"tags":["agent","termination","force-flag","safety-guard"],"backgroundTag":"resource-busy","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}