{"record":{"id":"d3df5d51f5845ffe","repo":"Hmbown/CodeWhale","slug":"agent-agent-id-not-found","errorCode":null,"errorMessage":"Agent {agent_id} not found","messagePattern":"Agent (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/tools/subagent/mod.rs","lineNumber":5671,"sourceCode":"                .checkpoint\n                .as_ref()\n                .is_some_and(|checkpoint| checkpoint.parked_at_turn_end);\n            match &result.status {\n                SubAgentStatus::Failed(err) => record.error = Some(err.clone()),\n                SubAgentStatus::BudgetExhausted => record.error = message.clone(),\n                _ => {}\n            }\n        }\n        self.record_worker_event(worker_id, status, message, Some(result.steps_taken), None);\n    }\n\n    pub fn cancel_agent(&mut self, agent_ref: &str) -> Result<SubAgentResult> {\n        let agent_id = self.resolve_agent_ref(agent_ref)?;\n        let mut terminal = {\n            let agent = self\n                .agents\n                .get(&agent_id)\n                .ok_or_else(|| anyhow!(\"Agent {agent_id} not found\"))?;\n            if agent.status != SubAgentStatus::Running {\n                let snapshot = self.snapshot_for_listing(agent);\n                return Ok(self.terminalize_settled_worker_on_cancel(&agent_id, snapshot));\n            }\n            if agent.completion_claimed {\n                // Still running, with its terminal transition already claimed:\n                // mid-flight, not settled. Stealing it here would record a\n                // second terminal outcome for one child.\n                return Ok(self.snapshot_for_listing(agent));\n            }\n            self.snapshot_for_listing(agent)\n        };\n        terminal.status = SubAgentStatus::Cancelled;\n        terminal.result = Some(\"Cancelled by parent request.\".to_string());\n        terminal.needs_input = None;\n        if !self.finish_terminal_result(&agent_id, terminal, true, true) {\n            return self.get_result(&agent_id);\n        }","sourceCodeStart":5653,"sourceCodeEnd":5689,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/subagent/mod.rs#L5653-L5689","documentation":"cancel_agent first resolves the caller-supplied agent reference to an agent ID, then looks the ID up in the in-memory agent map. If no live (tracked) agent matches, the cancel cannot proceed and the library throws this not-found error, naming the resolved agent_id.","triggerScenarios":"Calling cancel_agent(ref) with a reference to an agent that has already terminated and been reaped, was never spawned in this session, or whose ID was mistyped/aliased.","commonSituations":"Cancelling after the sub-agent already completed and its record was cleared; using an agent ref from a previous session after state restore; a stale UI handle to a finished agent.","solutions":["List the currently tracked agents first and cancel using a ref from that list.","Check whether the agent already finished; a completed agent needs no cancellation.","Re-run the sub-agent spawn if the intent was to restart it, then cancel the new ref.","Verify the ref/ID spelling and that you are in the same session that spawned the agent."],"exampleFix":"// before: cancelling a possibly-finished agent blindly\nmanager.cancel_agent(\"agent-7\")?;\n// after: check liveness first\nif manager.list_agents().iter().any(|a| a.id == \"agent-7\") {\n    manager.cancel_agent(\"agent-7\")?;\n}","handlingStrategy":"validation","validationCode":"fn can_cancel(mgr: &SubAgentManager, agent_ref: &str) -> bool {\n    mgr.resolve_agent_ref(agent_ref).ok()\n        .map(|id| mgr.find_agent(&id).is_some())\n        .unwrap_or(false)\n}","typeGuard":"fn live_agent<'a>(mgr: &'a SubAgentManager, agent_ref: &str) -> Option<&'a SubAgent> {\n    let id = mgr.resolve_agent_ref(agent_ref).ok()?;\n    mgr.find_agent(&id)\n}","tryCatchPattern":"match manager.cancel_agent(agent_ref) {\n    Err(e) if e.to_string().contains(\"not found\") => info!(\"agent already finished\"),\n    r => r?,\n}","preventionTips":["Always cancel using refs obtained from the current session's list of live agents.","Skip cancellation for agents already reported terminal.","Do not persist and reuse agent refs across session restarts."],"tags":["subagent","not-found","lifecycle"],"backgroundTag":"entity-not-found","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}