{"record":{"id":"a2869a1cac672a7c","repo":"ruvnet/ruflo","slug":"agent-input-agentid-not-found","errorCode":null,"errorMessage":"Agent '${input.agentId}' not found","messagePattern":"Agent '(.+?)' not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts","lineNumber":104,"sourceCode":"/**\n * Terminate Agent Command Result\n */\nexport interface TerminateAgentResult {\n  success: boolean;\n  agentId: string;\n  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}","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts#L86-L122","documentation":"TerminateAgentCommandHandler resolves the agent with repository.findById(input.agentId); when no agent exists it throws before agent.terminate() or save() runs. Standard entity-not-found guard — terminate ids must reference agents currently registered in the repository.","triggerScenarios":"Terminating an already-terminated agent whose record was removed; a mistyped or truncated id; an in-memory repository reset between spawn and terminate; concurrent cleanup loops where another worker terminated the agent first.","commonSituations":"Stale UI state after a refresh; shutdown/cleanup scripts racing each other; restart losing in-memory registrations; ids copy-pasted from logs of a previous run.","solutions":["Confirm the id via repository.findById or the agent listing before terminating.","Treat not-found as success in cleanup paths (idempotent termination).","Refresh ids from the authoritative agent registry before acting."],"exampleFix":"// before\nawait terminateAgent.execute({ agentId }); // throws on unknown id\n\n// after — idempotent cleanup\nif (await agentRepository.findById(agentId)) {\n  await terminateAgent.execute({ agentId });\n}","handlingStrategy":"validation","validationCode":"if (!(await agentRepository.findById(agentId))) {\n  return; // already terminated/removed: nothing to do\n}\nawait terminateAgentHandler.execute({ agentId });","typeGuard":null,"tryCatchPattern":"try {\n  await terminateAgentHandler.execute({ agentId });\n} catch (e) {\n  if (e instanceof Error && /^Agent '.*' not found$/.test(e.message)) {\n    return; // idempotent termination\n  }\n  throw e;\n}","preventionTips":["Make cleanup loops idempotent: not-found means the goal is already achieved.","Fetch agent ids from the live registry right before acting on them.","Guard against concurrent cleanup racing on the same agent id."],"tags":["agent","not-found","termination"],"backgroundTag":"resource-not-found","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}