fix: comprehensive agent tracking and cleanup improvements

This commit fixes all 4 reported agent tracking bugs and adds comprehensive test coverage:

**Bug Fixes:**
1. Agents stuck in "running" state after completion
   - Added SubagentStop hook parsing in wsl_bridge.rs
   - Emits claude:agent-end events when SubagentStop hooks detected
   - Includes 8 new Rust tests for hook parsing

2. Agents persisting after disconnect
   - Added clearConversation() call on disconnect in tauri.ts
   - Prevents agents from persisting across sessions

3. "Kill All" button doing nothing
   - Added markAllErrored() call in AgentMonitorPanel after interrupt
   - Updates UI state immediately after killing process

4. Badge persisting after closing tab
   - Added clearConversation() call in conversations.ts deleteConversation()
   - Properly cleans up agent tracking when tab is closed

**Test Coverage:**
- Added comprehensive agents.test.ts with 24 new tests
- Tests all store methods: addAgent, updateAgentId, endAgent, markAllErrored, clearCompleted, clearConversation, runningAgentCount
- Added 8 Rust tests for SubagentStart/Stop hook parsing
- All 387 frontend tests pass
- All 426 backend tests pass

**Documentation:**
- Updated CLAUDE.md with comprehensive testing guidelines
- Documents coverage goals, console mocking strategies, and E2E integration testing patterns

 This fix was implemented with help from Hikari~ 🌸
This commit is contained in:
2026-02-07 18:45:12 -08:00
committed by Naomi Carrigan
parent d5485e616f
commit 9a8816f6a0
6 changed files with 484 additions and 0 deletions
+146
View File
@@ -678,6 +678,34 @@ fn handle_stderr(
}
}
// Check if this is a SubagentStop hook message
if line.contains("[SubagentStop Hook]") {
if let Some(stop_data) = parse_subagent_stop_hook(&line) {
tracing::debug!("Parsed SubagentStop hook: tool_use_id={:?}",
stop_data.parent_tool_use_id);
// Emit agent-end event if we have a tool_use_id
if let Some(tool_use_id) = stop_data.parent_tool_use_id {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64;
let _ = app.emit(
"claude:agent-end",
AgentEndEvent {
tool_use_id,
ended_at: now,
is_error: false,
conversation_id: conversation_id.clone(),
duration_ms: None,
num_turns: None,
},
);
}
}
}
// Still emit the stderr line as output
let _ = app.emit(
"claude:output",
@@ -732,6 +760,30 @@ fn parse_subagent_start_hook(line: &str) -> Option<SubagentStartData> {
})
}
#[derive(Debug)]
struct SubagentStopData {
parent_tool_use_id: Option<String>,
}
fn parse_subagent_stop_hook(line: &str) -> Option<SubagentStopData> {
// Parse: [SubagentStop Hook] ... parent_tool_use_id=Some("toolu_xxx"), ...
// Extract parent_tool_use_id if present
let parent_tool_use_id = if line.contains("parent_tool_use_id=Some") {
line.split("parent_tool_use_id=Some(\"")
.nth(1)?
.split('"')
.next()
.map(|s| s.to_string())
} else {
None
};
Some(SubagentStopData {
parent_tool_use_id,
})
}
fn process_json_line(
line: &str,
app: &AppHandle,
@@ -1823,4 +1875,98 @@ mod tests {
let manager = shared.lock();
assert!(manager.get_active_conversations().is_empty());
}
// SubagentStart hook parsing tests
#[test]
fn test_parse_subagent_start_hook_with_parent() {
let line = r#"[SubagentStart Hook] agent_id=agent-abc123, parent_tool_use_id=Some("toolu_01XYZ789"), session_id=123"#;
let result = parse_subagent_start_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.agent_id, "agent-abc123");
assert_eq!(data.parent_tool_use_id, Some("toolu_01XYZ789".to_string()));
}
#[test]
fn test_parse_subagent_start_hook_without_parent() {
let line = r#"[SubagentStart Hook] agent_id=agent-xyz789, parent_tool_use_id=None, session_id=456"#;
let result = parse_subagent_start_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.agent_id, "agent-xyz789");
assert_eq!(data.parent_tool_use_id, None);
}
#[test]
fn test_parse_subagent_start_hook_invalid() {
let line = "[SubagentStart Hook] invalid data";
let result = parse_subagent_start_hook(line);
assert!(result.is_none());
}
#[test]
fn test_parse_subagent_start_hook_with_extra_fields() {
let line = r#"[SubagentStart Hook] agent_id=agent-test, parent_tool_use_id=Some("toolu_test"), session_id=789, cwd=/home/user"#;
let result = parse_subagent_start_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.agent_id, "agent-test");
assert_eq!(data.parent_tool_use_id, Some("toolu_test".to_string()));
}
// SubagentStop hook parsing tests
#[test]
fn test_parse_subagent_stop_hook_with_parent() {
let line = r#"[SubagentStop Hook] stop_hook_active=true, parent_tool_use_id=Some("toolu_01ABC123"), session_id=123"#;
let result = parse_subagent_stop_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.parent_tool_use_id, Some("toolu_01ABC123".to_string()));
}
#[test]
fn test_parse_subagent_stop_hook_without_parent() {
let line = r#"[SubagentStop Hook] stop_hook_active=true, parent_tool_use_id=None, session_id=456"#;
let result = parse_subagent_stop_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.parent_tool_use_id, None);
}
#[test]
fn test_parse_subagent_stop_hook_minimal() {
let line = r#"[SubagentStop Hook] parent_tool_use_id=Some("toolu_minimal")"#;
let result = parse_subagent_stop_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.parent_tool_use_id, Some("toolu_minimal".to_string()));
}
#[test]
fn test_parse_subagent_stop_hook_with_extra_fields() {
let line = r#"[SubagentStop Hook] stop_hook_active=false, parent_tool_use_id=Some("toolu_extra"), session_id=789, transcript_path=/path/to/transcript"#;
let result = parse_subagent_stop_hook(line);
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.parent_tool_use_id, Some("toolu_extra".to_string()));
}
#[test]
fn test_parse_subagent_stop_hook_empty() {
let line = "[SubagentStop Hook]";
let result = parse_subagent_stop_hook(line);
// Should still return Some with None parent_tool_use_id
assert!(result.is_some());
let data = result.unwrap();
assert_eq!(data.parent_tool_use_id, None);
}
}