{"record":{"id":"0e5fa64d4bb039bd","repo":"Hmbown/CodeWhale","slug":"repeated-stop","errorCode":null,"errorMessage":"repeated Stop","messagePattern":"repeated Stop","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/tui/src/tools/subagent/tests.rs","lineNumber":6588,"sourceCode":"    let (completion_tx, mut completion_rx) = mpsc::channel::<SubAgentCompletion>(16);\n    let (mailbox, mut mailbox_rx) = Mailbox::new(CancellationToken::new());\n    let (event_tx, mut event_rx) = mpsc::channel(8);\n    let mut runtime = runtime_with_depth(1, Some(completion_tx));\n    runtime.mailbox = Some(mailbox);\n    runtime.event_tx = Some(event_tx);\n    agent.terminal_delivery = Some(SubAgentTerminalDeliveryContext::from_runtime(&runtime));\n    manager.agents.insert(agent_id.clone(), agent);\n    manager.register_worker(make_worker_spec(&agent_id, tmp.path().to_path_buf()));\n    manager.record_worker_event(\n        &agent_id,\n        AgentWorkerStatus::ModelWait,\n        Some(SUBAGENT_MODEL_WAIT_REASON.to_string()),\n        Some(1),\n        None,\n    );\n\n    let first = manager.cancel_agent(&agent_id).expect(\"first Stop\");\n    let second = manager.cancel_agent(&agent_id).expect(\"repeated Stop\");\n    assert_eq!(first.status, SubAgentStatus::Cancelled);\n    assert_eq!(second.status, SubAgentStatus::Cancelled);\n    assert_eq!(\n        first\n            .checkpoint\n            .as_ref()\n            .map(|checkpoint| checkpoint.reason.as_str()),\n        Some(\"test_checkpoint\")\n    );\n\n    let completion = completion_rx\n        .try_recv()\n        .expect(\"parent cancellation fan-in\");\n    assert!(completion.payload.contains(r#\"\"status\":\"cancelled\"\"#));\n    assert!(completion_rx.try_recv().is_err());\n\n    let terminal_mail = mailbox_rx\n        .drain()","sourceCodeStart":6570,"sourceCodeEnd":6606,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/subagent/tests.rs#L6570-L6606","documentation":"Test assertion `manager.cancel_agent(&agent_id).expect(\"repeated Stop\")` at `crates/tui/src/tools/subagent/tests.rs:6588`, immediately following the first cancel. It verifies that issuing Stop a second time on an already-cancelled agent is idempotent and still succeeds (returning a record whose status is Cancelled). A panic means the second cancel returned an error instead of being a no-op success.","triggerScenarios":"Calling cancel_agent twice when the manager treats an already-cancelled agent as an error (e.g. 'agent not in cancellable state') rather than returning the existing record; or record eviction after the first cancel leaving the lookup to fail.","commonSituations":"A refactor that adds strict state-transition validation to cancel_agent (rejecting transitions from terminal states) breaking the documented idempotency contract; or the record-removal bug shared with the 'worker record remains inspectable' assertion in the same suite.","solutions":["Inspect the Err from the second cancel_agent call (unwrap_or_else with the error in the panic message).","Ensure cancel_agent returns the existing Cancelled record for an already-terminal agent instead of an Err.","Verify the worker record is retained after the first cancel so the second call can find it.","Add/keep a regression test pinning cancel idempotency if it was removed."],"exampleFix":"// before\nlet second = manager.cancel_agent(&agent_id).expect(\"repeated Stop\");\n// after\nlet second = manager.cancel_agent(&agent_id)\n    .unwrap_or_else(|e| panic!(\"repeated Stop must be idempotent, got: {e:?}\"));","handlingStrategy":"type-guard","validationCode":"// After the first cancel, confirm the record still exists before re-cancelling:\nassert!(manager.get_worker_record(&agent_id).is_some(), \"record must survive first cancel\");","typeGuard":null,"tryCatchPattern":"let second = manager.cancel_agent(&agent_id).unwrap_or_else(|e| panic!(\"repeated Stop must be idempotent: {e:?}\"));","preventionTips":["Make cancel_agent idempotent for terminal states by returning the stored record.","Add a guard test for double-cancel whenever the state machine changes.","Do not prune records inside the cancel path."],"tags":["rust","test","panic","idempotency","cancellation"],"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-22T10:30:35.592Z"}