{"record":{"id":"1f32d507caca6845","repo":"ruvnet/ruflo","slug":"task-taskid-not-assigned-to-this-agent","errorCode":null,"errorMessage":"Task ${taskId} not assigned to this agent","messagePattern":"Task (.+?) not assigned to this agent","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/swarm/src/domain/entities/agent.ts","lineNumber":267,"sourceCode":"    if (this._status === 'terminated') {\n      throw new Error('Cannot assign task to terminated agent');\n    }\n    if (this._currentTaskIds.size >= this._maxConcurrentTasks) {\n      throw new Error('Agent at maximum concurrent task capacity');\n    }\n\n    this._currentTaskIds.add(taskId);\n    this._status = 'busy';\n    this._lastActiveAt = new Date();\n    this._updatedAt = new Date();\n  }\n\n  /**\n   * Complete a task\n   */\n  completeTask(taskId: string): void {\n    if (!this._currentTaskIds.has(taskId)) {\n      throw new Error(`Task ${taskId} not assigned to this agent`);\n    }\n\n    this._currentTaskIds.delete(taskId);\n    this._completedTaskCount++;\n\n    if (this._currentTaskIds.size === 0) {\n      this._status = 'active';\n    }\n\n    this._lastActiveAt = new Date();\n    this._updatedAt = new Date();\n  }\n\n  /**\n   * Check if agent can accept more tasks\n   */\n  canAcceptTask(): boolean {\n    return (","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/swarm/src/domain/entities/agent.ts#L249-L285","documentation":"Agent.completeTask(taskId) requires the taskId to be present in the agent's current task set — it throws when asked to complete a task it was never assigned (or already completed). This guards the bookkeeping invariant between assignment and completion.","triggerScenarios":"Calling completeTask() twice with the same id (the first call removed it from the set)\nCompleting a task on the wrong agent instance after reassignment (task.fail() resets _assignedAgentId and re-queues)\nCoordinator tracking divergence: its task->agent map disagrees with the entity's set Task retry flow that re-assigned the task elsewhere but completion raced back to the old agent","commonSituations":"At-least-once delivery of completion events causing duplicate completeTask() calls\nConcurrent retries: fail() re-queues a task while a late success handler completes it on the original agent\nEvent-sourcing replays applying completion events against a rebuilt agent state Cross-service id mismatches (string taskId normalized differently)","solutions":["Make completion idempotent in the caller: check membership first or swallow this specific throw\nSingle-flight task execution: cancel retry timers once completion starts\nRoute completions through the coordinator that owns the task->agent mapping, not straight to the entity\nFor retried tasks, complete them on the CURRENT assigned agent only"],"exampleFix":"// before\nagent.completeTask(taskId); // throws on duplicate/misrouted completion\n\n// after\nfunction safeComplete(agent, taskId) {\n  try { agent.completeTask(taskId); }\n  catch (e) {\n    if (e instanceof Error && /not assigned to this agent/.test(e.message)) return; // idempotent no-op\n    throw e;\n  }\n}","handlingStrategy":"try-catch","validationCode":"// Route completion through the coordinator that owns the task->agent map:\nconst agent = coordinator.ownerOf(taskId);\nif (!agent) return; // unknown/stale task — nothing to complete\nagent.completeTask(taskId);","typeGuard":null,"tryCatchPattern":"try { agent.completeTask(taskId); }\ncatch (e) {\n  if (e instanceof Error && /not assigned to this agent/.test(e.message)) return; // idempotent duplicate/misroute\n  throw e;\n}","preventionTips":["Deduplicate completion events (idempotency key per taskId)\nCancel retry timers before completing to avoid complete/fail races\nAfter fail() re-queues a task, complete it on the NEW assignee only"],"tags":["agent","task-lifecycle","idempotency","domain-entity","typescript"],"backgroundTag":"state-machine-invalid-transition","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}