{"record":{"id":"ac16a914698d3ed0","repo":"Hmbown/CodeWhale","slug":"cancel-wins-race","errorCode":null,"errorMessage":"cancel wins race","messagePattern":"cancel wins race","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/tools/subagent/tests.rs","lineNumber":6795,"sourceCode":"    let mut manager = SubAgentManager::new(tmp.path().to_path_buf(), 2);\n    let agent_id = \"agent_cancel_completion_race\".to_string();\n    let (input_tx, _input_rx) = mpsc::unbounded_channel();\n    let agent = SubAgent::new(\n        agent_id.clone(),\n        FleetRole::Worker,\n        \"race\".to_string(),\n        make_assignment(),\n        \"deepseek-v4-flash\".to_string(),\n        None,\n        None,\n        input_tx,\n        tmp.path().to_path_buf(),\n        manager.current_session_boot_id.clone(),\n    );\n    manager.agents.insert(agent_id.clone(), agent);\n    manager.register_worker(make_worker_spec(&agent_id, tmp.path().to_path_buf()));\n\n    manager.cancel_agent(&agent_id).expect(\"cancel wins race\");\n    let mut late = manager\n        .get_result(&agent_id)\n        .expect(\"cancelled snapshot exists\");\n    late.status = SubAgentStatus::Completed;\n    late.result = Some(\"late success\".to_string());\n    assert!(\n        !manager.update_from_result(&agent_id, late),\n        \"late completion must lose the terminal transition\"\n    );\n\n    let snapshot = manager\n        .get_result(&agent_id)\n        .expect(\"terminal snapshot remains\");\n    assert_eq!(snapshot.status, SubAgentStatus::Cancelled);\n    assert_eq!(\n        snapshot.result.as_deref(),\n        Some(\"Cancelled by parent request.\")\n    );","sourceCodeStart":6777,"sourceCodeEnd":6813,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/subagent/tests.rs#L6777-L6813","documentation":"Test panic: `manager.cancel_agent(&agent_id)` returned Err, failing `.expect(\"cancel wins race\")`. The cancel path must win races against late completions, so an Err means the manager refused or failed to cancel a registered, running worker.","triggerScenarios":"Calling `cancel_agent` on an agent inserted into `manager.agents` with a registered worker spec, when the cancel path requires a state (e.g. Running) the agent does not have, or its input channel is closed.","commonSituations":"Regression where cancel requires a live input_tx sender but the test's receiver was dropped; terminal-state guard rejecting an agent already in a terminal status.","solutions":["Confirm the agent's status allows cancellation at the call site (not already terminal).","Check `input_tx` is still open if cancel signals via the input channel.","Read the Err payload to distinguish unknown-agent from state-conflict refusals."],"exampleFix":"// before\nmanager.cancel_agent(&agent_id).expect(\"cancel wins race\");\n// after\nmanager.cancel_agent(&agent_id)\n    .unwrap_or_else(|e| panic!(\"cancel wins race: {e:#}\"));","handlingStrategy":"try-catch","validationCode":"let cancelable = manager.agents.get(&agent_id)\n    .map(|a| !matches!(a.status, SubAgentStatus::Cancelled | SubAgentStatus::Completed))\n    .unwrap_or(false);\nassert!(cancelable, \"agent must exist and be non-terminal to cancel\");","typeGuard":null,"tryCatchPattern":"manager.cancel_agent(&agent_id)\n    .unwrap_or_else(|e| panic!(\"cancel failed for {agent_id}: {e:#}\"));","preventionTips":["Keep input_tx alive while the agent can be cancelled","Only reject cancel for unknown agents, never for state the manager controls","Test cancel-against-late-completion races whenever the result path changes"],"tags":["test-assertion","cancel","race-condition","subagent"],"backgroundTag":"invalid-state-transition","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"}