kraal 0.2.0

A terminal workspace manager for SSH fleets and coding agents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Coding-agent awareness: which pane runs an agent, and whether it is working, blocked on a
//! question, done, or idle.
//!
//! Two sources feed the state. Hooks installed in the agent (`kraal hook claude`) report exact
//! transitions; for agents without hooks, the server watches the foreground process and the
//! screen and infers the state.

use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AgentState {
    #[default]
    Unknown,
    /// Waiting for a prompt, and the user has already seen the last result.
    Idle,
    Working,
    /// Waiting for the user to answer a question or approve an action.
    Blocked,
    /// Finished a turn the user has not looked at yet.
    Done,
}

impl AgentState {
    pub fn parse(s: &str) -> Option<AgentState> {
        Some(match s.to_ascii_lowercase().as_str() {
            "unknown" => AgentState::Unknown,
            "idle" => AgentState::Idle,
            "working" | "busy" => AgentState::Working,
            "blocked" | "waiting" | "input" => AgentState::Blocked,
            "done" | "finished" => AgentState::Done,
            _ => return None,
        })
    }

    pub fn icon(self) -> &'static str {
        match self {
            AgentState::Unknown => "·",
            AgentState::Idle => "○",
            AgentState::Working => "●",
            AgentState::Blocked => "▲",
            AgentState::Done => "✓",
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            AgentState::Unknown => "unknown",
            AgentState::Idle => "idle",
            AgentState::Working => "working",
            AgentState::Blocked => "blocked",
            AgentState::Done => "done",
        }
    }

    /// Whether the user should look at this pane.
    pub fn needs_attention(self) -> bool {
        matches!(self, AgentState::Blocked | AgentState::Done)
    }
}

/// Executable names of the agents kraal recognises (or the name of the script an interpreter
/// runs for them), and the name kraal gives each agent.
const KNOWN_AGENTS: &[(&str, &str)] = &[
    ("claude", "claude"),
    ("codex", "codex"),
    ("opencode", "opencode"),
    ("opencode2", "opencode"),
    ("gemini", "gemini"),
    ("aider", "aider"),
    ("cursor-agent", "cursor"),
    ("amp", "amp"),
    ("pi", "pi"),
    ("grok", "grok"),
    ("copilot", "copilot"),
    ("goose", "goose"),
    ("crush", "crush"),
    ("qwen", "qwen"),
    ("kimi", "kimi"),
    ("droid", "droid"),
    ("auggie", "auggie"),
    ("kiro-cli", "kiro"),
    ("kilo", "kilo"),
    ("kilocode", "kilo"),
    ("cline", "cline"),
    ("codebuff", "codebuff"),
    ("freebuff", "codebuff"),
    ("qoder", "qoder"),
    ("qodercli", "qoder"),
    ("codebuddy", "codebuddy"),
    ("junie", "junie"),
    ("llxprt", "llxprt"),
    ("kode", "kode"),
    ("openhands", "openhands"),
    ("vibe-rs", "vibe"),
    // Mistral Vibe's Python launcher renames its process.
    ("Vibe CLI", "vibe"),
];

/// Names other programs use too: they count only as the script of the runtime an agent's
/// package runs on (Letta's server is a Python `letta`, Letta Code a Node one).
const SCRIPT_ONLY: &[(&str, &str, &str)] =
    &[("cn", "node", "continue"), ("letta", "node", "letta"), ("vibe", "python", "vibe")];

/// Agents that run as a subcommand of a broader program: the program, the words that must
/// follow it, and the agent.
const SUBCOMMANDS: &[(&str, &[&str], &str)] = &[("acli", &["rovodev", "run"], "rovodev"), ("q", &["chat"], "kiro")];

const INTERPRETERS: &[&str] = &["node", "bun", "deno", "python", "python3", "uv", "npx", "sh", "bash", "zsh", "dash"];

/// Agent name for a process command line, looking through script interpreters.
pub fn detect_agent(argv: &[String]) -> Option<&'static str> {
    let base = |s: &str| s.rsplit('/').next().unwrap_or(s).to_string();
    let first = base(argv.first()?);
    // Cursor's launcher runs its bundled Node on the agent's script, under the name it was
    // called by (`agent`, too common a word to go by).
    if argv.iter().take(4).any(|a| a.contains("/cursor-agent/versions/") && a.ends_with("/index.js")) {
        return Some("cursor");
    }
    if let Some((_, words, name)) = SUBCOMMANDS.iter().find(|(program, ..)| *program == first) {
        let rest: Vec<&str> = argv.iter().skip(1).filter(|a| !a.starts_with('-')).map(String::as_str).collect();
        return rest.starts_with(words).then_some(*name);
    }
    let interpreted = INTERPRETERS.iter().any(|i| first.starts_with(i));
    let candidates: Vec<String> = if interpreted {
        argv.iter().skip(1).filter(|a| !a.starts_with('-')).take(2).map(|a| base(a)).collect()
    } else {
        vec![first.clone()]
    };
    let script_name =
        |c: &str| [".js", ".mjs", ".cjs"].iter().find_map(|ext| c.strip_suffix(ext)).unwrap_or(c).to_string();
    if interpreted
        && let Some(script) = candidates.first().map(|c| script_name(c))
        && let Some((.., name)) = SCRIPT_ONLY.iter().find(|(s, runtime, _)| *s == script && first.starts_with(runtime))
    {
        return Some(name);
    }
    for c in candidates {
        let c = script_name(&c);
        if let Some((_, name)) = KNOWN_AGENTS.iter().find(|(exe, _)| *exe == c) {
            return Some(name);
        }
    }
    None
}

/// What an agent shows at the bottom of its screen while it works, and while it waits for
/// the user, beyond what many agents share (`esc to interrupt`, the questions in
/// [`classify_screen`]). From each agent's source, or its documentation when it is closed.
fn screen_patterns(agent: &str) -> (&'static [&'static str], &'static [&'static str]) {
    const GEMINI_ASKS: &[&str] =
        &["apply this change?", "allow execution of", "waiting for user confirmation", "no, suggest changes (esc)"];
    match agent {
        "claude" | "codex" => (&["esc to cancel"], &[]),
        "gemini" | "llxprt" => (&["esc to cancel"], GEMINI_ASKS),
        "qwen" => (
            &["esc to cancel"],
            &["apply this change?", "allow execution of", "yes, allow once", "no, suggest changes (esc)"],
        ),
        "opencode" | "kilo" => (
            &["esc interrupt", "esc again to interrupt", "esc to cancel"],
            &["permission required", "allow once", "allow always", "always allow", "type your own answer"],
        ),
        "crush" => (&[], &["permission required", "allow for session"]),
        "goose" => (&["ctrl+c to interrupt"], &["would like to call the above tool", "do you allow this tool call?"]),
        "pi" => (&["to interrupt)"], &[]),
        "cline" => {
            (&["esc to cancel"], &["cline needs permission", "approve tool call?", "cline is asking a question"])
        }
        "continue" => (&[], &["would you like to continue?"]),
        "openhands" => (&["esc: pause"], &["action(s)?"]),
        "kode" => (&[], &["allow this tool use?", "allow this command?", "allow this edit to"]),
        "vibe" => (
            &["esc/ctrl+c to interrupt"],
            &["permission for the", "allow for remainder of this session", "waiting for approval"],
        ),
        "letta" => (&[], &["run this command?", "don't ask again for this project"]),
        "kimi" => (&["working…"], &["approve once", "reject with feedback"]),
        "aider" => (&[], &["(y)es/(n)o"]),
        "copilot" => (
            &["esc to cancel"],
            &["for the rest of the running session", "no, and tell copilot what to do differently"],
        ),
        "cursor" => (&[], &["skip & tell the agent what to do instead"]),
        "grok" => (&["send a message to interrupt"], &["no, reject (type to add feedback)", "reject once"]),
        "kiro" => (&[], &["allow this action?"]),
        _ => (&[], &[]),
    }
}

/// Infer the state of an agent from the text currently on its screen.
pub fn classify_screen(agent: &str, screen: &str) -> Option<AgentState> {
    // Agents draw their status and prompts at the bottom; older output higher up may quote
    // the same words.
    let tail: Vec<&str> = screen.lines().filter(|l| !l.trim().is_empty()).collect();
    let tail = tail[tail.len().saturating_sub(12)..].join("\n");
    let lower = tail.to_lowercase();
    let (working, asks) = screen_patterns(agent);
    let blocked_common = [
        "do you want to proceed",
        "do you want to make this edit",
        "do you want to create",
        "do you want to allow",
        "would you like to run the following command",
        "would you like to make the following edits",
        "allow command?",
        "approve this",
        "enter to confirm",
        "do you trust",
        "(y/n)",
        "[y/n]",
    ];
    if blocked_common.iter().chain(asks).any(|p| lower.contains(p)) {
        return Some(AgentState::Blocked);
    }
    if ["esc to interrupt"].iter().chain(working).any(|p| lower.contains(p)) {
        return Some(AgentState::Working);
    }
    None
}

/// Per-pane agent bookkeeping.
#[derive(Debug, Clone)]
pub struct AgentTracker {
    pub kind: Option<String>,
    pub state: AgentState,
    pub since: Instant,
    /// Hooks have reported for the current agent run; heuristics are then ignored.
    pub hooked: bool,
    pub session_id: Option<String>,
    pub message: Option<String>,
}

impl Default for AgentTracker {
    fn default() -> Self {
        Self {
            kind: None,
            state: AgentState::Unknown,
            since: Instant::now(),
            hooked: false,
            session_id: None,
            message: None,
        }
    }
}

impl AgentTracker {
    /// Set a new state; returns true if it changed.
    pub fn set(&mut self, state: AgentState) -> bool {
        if self.state == state {
            return false;
        }
        self.state = state;
        self.since = Instant::now();
        if state != AgentState::Blocked {
            self.message = None;
        }
        true
    }

    /// The foreground process changed. Returns true if the agent appeared or went away.
    pub fn set_process(&mut self, agent: Option<&str>) -> bool {
        match (self.kind.as_deref(), agent) {
            (Some(a), Some(b)) if a == b => false,
            (None, None) => false,
            (_, Some(b)) => {
                self.kind = Some(b.to_string());
                self.hooked = false;
                self.set(AgentState::Idle);
                true
            }
            (Some(kind), None) => {
                // Hooked agents can run under a wrapper we don't recognise; keep them, as long
                // as their hooks say when the session ends (OpenCode's plugins cannot).
                if self.hooked && !matches!(kind, "opencode" | "kilo") {
                    return false;
                }
                self.kind = None;
                self.set(AgentState::Unknown);
                true
            }
        }
    }

    /// Heuristic update from the screen and output activity. `quiet` is the time since the
    /// pane last produced output.
    pub fn observe(&mut self, screen: &str, quiet: Duration) -> bool {
        let Some(kind) = self.kind.clone() else { return false };
        if self.hooked {
            return false;
        }
        match classify_screen(&kind, screen) {
            Some(s) => self.set(s),
            None => match self.state {
                AgentState::Working if quiet > Duration::from_millis(1500) => self.set(AgentState::Done),
                AgentState::Blocked if quiet > Duration::from_millis(1500) => self.set(AgentState::Done),
                AgentState::Unknown => self.set(AgentState::Idle),
                _ => false,
            },
        }
    }

    /// The user looked at the pane.
    pub fn mark_seen(&mut self) -> bool {
        if self.state == AgentState::Done {
            return self.set(AgentState::Idle);
        }
        false
    }
}

/// Whether an agent's hooks report every state change (working, blocked, done), so the screen
/// heuristics can be switched off for it. Codex's notify, Cursor's and Augment's hooks have no
/// event for a question to the user: the screen still tells that.
pub fn hooks_cover_lifecycle(agent: &str) -> bool {
    matches!(
        agent,
        "claude" | "gemini" | "qwen" | "droid" | "codebuddy" | "copilot" | "kimi" | "opencode" | "kilo" | "pi"
    )
}

/// Map an agent's hook payload to a state change. `event` is the hook's event, for agents that
/// do not name it in the payload (Copilot's own format).
pub fn hook_state(agent: &str, payload: &serde_json::Value, event: Option<&str>) -> Option<AgentState> {
    use AgentState::*;
    let field = |k: &str| payload.get(k).and_then(|v| v.as_str()).unwrap_or("");
    let event = event.unwrap_or_else(|| field("hook_event_name"));
    Some(match (agent, event) {
        // Claude Code's events, and those of the agents that copied them.
        ("claude" | "droid" | "codebuddy" | "qwen", _) => match event {
            "SessionStart" => Idle,
            "UserPromptSubmit" | "PreToolUse" | "PostToolUse" | "PreCompact" | "SubagentStart" => Working,
            "Stop" | "StopFailure" => Done,
            "SessionEnd" => Unknown,
            "Notification" => {
                let msg = field("message").to_lowercase();
                let asks = matches!(field("notification_type"), "permission_prompt" | "elicitation_dialog")
                    || ["permission", "approve", "question"].iter().any(|w| msg.contains(w));
                return asks.then_some(Blocked);
            }
            _ => return None,
        },
        ("gemini", "SessionStart") => Idle,
        ("gemini", "BeforeAgent" | "BeforeTool" | "AfterTool" | "PreCompress") => Working,
        ("gemini", "Notification") if field("notification_type") == "ToolPermission" => Blocked,
        ("gemini", "AfterAgent") => Done,
        ("gemini", "SessionEnd") => Unknown,
        ("kimi", "SessionStart") => Idle,
        ("kimi", "UserPromptSubmit" | "PermissionResult") => Working,
        ("kimi", "PermissionRequest") => Blocked,
        ("kimi", "Stop" | "StopFailure") => Done,
        // Esc: the user is looking at it.
        ("kimi", "Interrupt") => Idle,
        ("kimi", "SessionEnd") => Unknown,
        ("copilot", "sessionStart") => Idle,
        ("copilot", "userPromptSubmitted" | "postToolUse") => Working,
        ("copilot", "notification")
            if matches!(field("notification_type"), "permission_prompt" | "elicitation_dialog") =>
        {
            Blocked
        }
        ("copilot", "agentStop") => Done,
        ("copilot", "sessionEnd") => Unknown,
        ("cursor", "sessionStart") => Idle,
        ("cursor", "beforeSubmitPrompt" | "postToolUse") => Working,
        ("cursor", "stop") => Done,
        ("cursor", "sessionEnd") => Unknown,
        ("auggie", "SessionStart") => Idle,
        ("auggie", "PreToolUse" | "PostToolUse") => Working,
        ("auggie", "Stop") if field("agent_stop_cause") == "interrupted" => Idle,
        ("auggie", "Stop") => Done,
        ("auggie", "SessionEnd") => Unknown,
        _ => return None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn argv(s: &str) -> Vec<String> {
        s.split_whitespace().map(String::from).collect()
    }

    #[test]
    fn detects_agents_through_interpreters() {
        assert_eq!(detect_agent(&argv("claude")), Some("claude"));
        assert_eq!(detect_agent(&argv("/home/u/.local/bin/claude --resume x")), Some("claude"));
        assert_eq!(detect_agent(&argv("node /usr/lib/node_modules/codex/bin/codex.js")), Some("codex"));
        assert_eq!(detect_agent(&argv("zsh")), None);
        assert_eq!(detect_agent(&argv("ssh claude@host")), None);
    }

    /// Command lines as each agent's package or installer starts it.
    #[test]
    fn detects_agents_as_they_are_installed() {
        let cases = [
            // npm launchers: a Node script named after the agent.
            ("node /usr/bin/gemini --yolo", Some("gemini")),
            ("node /home/u/.npm-global/bin/qwen", Some("qwen")),
            ("node /usr/local/bin/copilot --resume", Some("copilot")),
            ("node /usr/bin/auggie", Some("auggie")),
            ("node /usr/bin/kilocode", Some("kilo")),
            ("node /usr/bin/cline", Some("cline")),
            ("node /usr/bin/codebuff", Some("codebuff")),
            ("node /usr/bin/llxprt", Some("llxprt")),
            ("node /usr/bin/kode", Some("kode")),
            ("node /usr/lib/node_modules/@opencode/cli/bin/opencode.cjs", Some("opencode")),
            ("node /usr/bin/opencode2", Some("opencode")),
            // Native binaries.
            ("/home/u/.local/bin/droid", Some("droid")),
            ("/home/u/.local/bin/kiro-cli", Some("kiro")),
            ("/home/u/.kimi-code/bin/kimi", Some("kimi")),
            ("/home/u/.local/bin/qodercli", Some("qoder")),
            ("/usr/local/bin/codebuddy", Some("codebuddy")),
            ("/home/u/.local/bin/junie", Some("junie")),
            ("/home/u/.local/bin/openhands", Some("openhands")),
            ("/usr/lib/python3.12/site-packages/vibe/_bin/vibe-rs", Some("vibe")),
            // Python entry points.
            ("/home/u/.local/share/uv/tools/aider-chat/bin/python3 /home/u/.local/bin/aider --model x", Some("aider")),
            ("/home/u/.venv/bin/python /home/u/.venv/bin/vibe", Some("vibe")),
            // Cursor's launcher, as `agent` (exec -a) and with its bundled Node.
            (
                "/home/u/.local/bin/agent --use-system-ca /home/u/.local/share/cursor-agent/versions/2026.10.01-e373342/index.js",
                Some("cursor"),
            ),
            (
                "/home/u/.local/share/cursor-agent/versions/2025.09.12-1/node /home/u/.local/share/cursor-agent/versions/2025.09.12-1/index.js",
                Some("cursor"),
            ),
            // Agents behind a broader program's subcommand.
            ("acli rovodev run", Some("rovodev")),
            ("acli jira workitem view X-1", None),
            ("q chat --resume", Some("kiro")),
            ("q", None),
            // Names other programs share count only on the agent's runtime.
            ("node /usr/bin/cn", Some("continue")),
            ("node /usr/bin/letta", Some("letta")),
            ("/usr/bin/python3 /usr/local/bin/letta server", None),
            ("cn", None),
            ("vibe", None),
            ("sh ./cn", None),
            // Neither an editor nor a shell, nor `agent` alone.
            ("nvim src/agent.rs", None),
            ("bash", None),
            ("agent", None),
            ("node /srv/app/index.js", None),
            ("cb", None),
        ];
        for (line, want) in cases {
            assert_eq!(detect_agent(&argv(line)), want, "{line}");
        }
        // The process title Mistral Vibe gives itself: one word with a space in it.
        assert_eq!(detect_agent(&["Vibe CLI".to_string()]), Some("vibe"));
    }

    #[test]
    fn screen_classification() {
        assert_eq!(classify_screen("claude", "✻ Thinking… (esc to interrupt)"), Some(AgentState::Working));
        assert_eq!(classify_screen("claude", "Do you want to proceed?\n❯ 1. Yes"), Some(AgentState::Blocked));
        assert_eq!(classify_screen("claude", "> "), None);
        assert_eq!(
            classify_screen("claude", "❯ No, exit\n  Yes, I trust this folder\nEnter to confirm · Esc to cancel"),
            Some(AgentState::Blocked)
        );
        let old = format!("esc to interrupt\n{}> ", "output\n".repeat(20));
        assert_eq!(classify_screen("claude", &old), None);
    }

    /// Screens built from the text each agent's source (or documentation) draws.
    #[test]
    fn screen_classification_per_agent() {
        use AgentState::{Blocked, Working};
        let cases: &[(&str, &str, Option<AgentState>)] = &[
            ("gemini", "⠏ Reading files (esc to cancel, 12s)", Some(Working)),
            (
                "gemini",
                "Allow execution of [Shell]?\n● 1. Allow once\n  2. Allow for this session\n  4. No, suggest changes (esc)",
                Some(Blocked),
            ),
            ("gemini", "Apply this change?\n● 1. Allow once", Some(Blocked)),
            ("llxprt", "Allow execution of: 'ls'?", Some(Blocked)),
            ("qwen", "⠋ Thinking (3s · ↓ 120 tokens · esc to cancel)", Some(Working)),
            ("qwen", "Allow execution of: 'npm test'?\n● 1. Yes, allow once", Some(Blocked)),
            ("opencode", "┃ Build  claude-sonnet\n esc interrupt", Some(Working)),
            ("opencode", "esc again to interrupt", Some(Working)),
            (
                "opencode",
                "△ Permission required\n→ Allow once   Allow always   Reject\n⇆ select  enter confirm",
                Some(Blocked),
            ),
            ("kilo", "△ Permission required\nAllow   Reject", Some(Blocked)),
            ("crush", "Permission Required\n Allow   Allow for Session   Deny", Some(Blocked)),
            ("goose", "◇ Goose would like to call the above tool, do you allow?\n│ ● Allow", Some(Blocked)),
            ("goose", "Thinking...  (Ctrl+C to interrupt)", Some(Working)),
            ("pi", "⠋ Working (escape to interrupt)", Some(Working)),
            ("cline", "Thinking... (esc to cancel)", Some(Working)),
            ("cline", "Cline needs permission\nApprove tool call?\n[y] Approve  [n] Deny", Some(Blocked)),
            ("continue", "Would you like to continue?\n> Continue (tab)", Some(Blocked)),
            ("continue", "⠋ (4s • esc to interrupt)", Some(Working)),
            ("openhands", "⠋ Working (12s • ESC: pause)", Some(Working)),
            ("openhands", "🔍 Confirm 2 action(s)?\n> Yes\n  No", Some(Blocked)),
            ("kode", "Allow this command?\nEnter to confirm · Esc to reject", Some(Blocked)),
            ("vibe", "Generating… (0s Esc/Ctrl+C to interrupt)", Some(Working)),
            ("vibe", "Permission for the bash tool\n1. Allow once", Some(Blocked)),
            ("letta", "Run this command?\n> Yes\n  Yes, and don't ask again for this project", Some(Blocked)),
            ("kimi", "⠋ Working…", Some(Working)),
            ("kimi", "▶ Run this command?\n> Approve once\n  Approve for this session\n  Reject", Some(Blocked)),
            ("aider", "Run shell command? (Y)es/(N)o/(D)on't ask again [Yes]:", Some(Blocked)),
            ("copilot", "Esc to cancel", Some(Working)),
            (
                "copilot",
                "1. Yes\n2. Yes, and approve shell for the rest of the running session\n3. No, and tell Copilot what to do differently (Esc)",
                Some(Blocked),
            ),
            ("cursor", "  Skip & tell the agent what to do instead", Some(Blocked)),
            ("grok", "Thinking… · send a message to interrupt", Some(Working)),
            (
                "kiro",
                "Allow this action? Use 't' to trust (always allow) this tool for the session. [y/n/t]:",
                Some(Blocked),
            ),
            // An agent's own words do not count for another, and a quiet prompt says nothing.
            ("claude", "esc interrupt", None),
            ("aider", "> ", None),
            ("opencode", "┃ Ask anything", None),
        ];
        for (agent, screen, want) in cases {
            assert_eq!(classify_screen(agent, screen), *want, "{agent}: {screen}");
        }
    }

    #[test]
    fn heuristics_reach_done_after_quiet_working() {
        let mut t = AgentTracker::default();
        t.set_process(Some("claude"));
        assert_eq!(t.state, AgentState::Idle);
        t.observe("esc to interrupt", Duration::ZERO);
        assert_eq!(t.state, AgentState::Working);
        t.observe("> ", Duration::from_secs(3));
        assert_eq!(t.state, AgentState::Done);
        assert!(t.mark_seen());
        assert_eq!(t.state, AgentState::Idle);
    }

    #[test]
    fn hooks_override_heuristics() {
        let mut t = AgentTracker::default();
        t.set_process(Some("claude"));
        t.hooked = true;
        t.set(AgentState::Working);
        assert!(!t.observe("> ", Duration::from_secs(10)));
        assert_eq!(t.state, AgentState::Working);
    }

    #[test]
    fn claude_hooks_map() {
        let v = |s: &str| serde_json::from_str::<serde_json::Value>(s).unwrap();
        let claude = |s: &str| hook_state("claude", &v(s), None);
        assert_eq!(claude(r#"{"hook_event_name":"Stop"}"#), Some(AgentState::Done));
        assert_eq!(
            claude(r#"{"hook_event_name":"Notification","message":"Claude needs your permission to use Bash"}"#),
            Some(AgentState::Blocked)
        );
        assert_eq!(claude(r#"{"hook_event_name":"Notification","message":"Claude is waiting for your input"}"#), None);
    }

    /// Payloads as each agent's hook reference describes them.
    #[test]
    fn other_agents_hooks_map() {
        let v = |s: &str| serde_json::from_str::<serde_json::Value>(s).unwrap();
        let state = |agent: &str, s: &str| hook_state(agent, &v(s), None);
        // Gemini CLI: a tool waiting for approval is a notification of its own type.
        assert_eq!(
            state("gemini", r#"{"hook_event_name":"BeforeAgent","prompt":"fix it"}"#),
            Some(AgentState::Working)
        );
        let permission = r#"{"hook_event_name":"Notification","notification_type":"ToolPermission","message":"Tool Shell requires execution","details":{"type":"exec"}}"#;
        assert_eq!(state("gemini", permission), Some(AgentState::Blocked));
        assert_eq!(
            state("gemini", r#"{"hook_event_name":"AfterAgent","stop_hook_active":false}"#),
            Some(AgentState::Done)
        );
        assert_eq!(state("gemini", r#"{"hook_event_name":"SessionEnd","reason":"exit"}"#), Some(AgentState::Unknown));
        assert_eq!(state("gemini", r#"{"hook_event_name":"Stop"}"#), None);
        // Qwen Code: Claude's names, its own messages; idle notifications say nothing new.
        let qwen = r#"{"hook_event_name":"Notification","notification_type":"permission_prompt","title":"Permission needed","message":"Qwen Code needs your permission to use run_shell_command"}"#;
        assert_eq!(state("qwen", qwen), Some(AgentState::Blocked));
        let idle = r#"{"hook_event_name":"Notification","notification_type":"idle_prompt","title":"Waiting for input","message":"Qwen Code is waiting for your input"}"#;
        assert_eq!(state("qwen", idle), None);
        assert_eq!(state("qwen", r#"{"hook_event_name":"StopFailure"}"#), Some(AgentState::Done));
        // Factory's droid: cancelling a turn is an idle notification, not a stop.
        let droid = r#"{"hook_event_name":"Notification","notification_type":"permission_prompt","message":"Droid is waiting for permission"}"#;
        assert_eq!(state("droid", droid), Some(AgentState::Blocked));
        assert_eq!(
            state(
                "droid",
                r#"{"hook_event_name":"Notification","notification_type":"idle_prompt","message":"Droid is waiting for input"}"#
            ),
            None
        );
        // Kimi Code: approval requests, their answers and Esc have events of their own.
        assert_eq!(
            state("kimi", r#"{"hook_event_name":"PermissionRequest","tool_name":"Shell"}"#),
            Some(AgentState::Blocked)
        );
        assert_eq!(
            state("kimi", r#"{"hook_event_name":"PermissionResult","decision":"approved"}"#),
            Some(AgentState::Working)
        );
        assert_eq!(state("kimi", r#"{"hook_event_name":"Interrupt","reason":"cancelled"}"#), Some(AgentState::Idle));
        // Copilot names the event in its configuration only: kraal passes it along.
        let copilot = |event: &str, s: &str| hook_state("copilot", &v(s), Some(event));
        assert_eq!(copilot("userPromptSubmitted", r#"{"sessionId":"s","prompt":"fix it"}"#), Some(AgentState::Working));
        let asks = r#"{"sessionId":"s","hook_event_name":"Notification","notification_type":"permission_prompt","message":"x"}"#;
        assert_eq!(copilot("notification", asks), Some(AgentState::Blocked));
        let finished = r#"{"sessionId":"s","hook_event_name":"Notification","notification_type":"shell_completed"}"#;
        assert_eq!(copilot("notification", finished), None);
        assert_eq!(copilot("agentStop", r#"{"sessionId":"s","stopReason":"end_turn"}"#), Some(AgentState::Done));
        // Cursor and Augment: turns, no questions.
        assert_eq!(
            state("cursor", r#"{"hook_event_name":"stop","status":"completed","loop_count":0}"#),
            Some(AgentState::Done)
        );
        assert_eq!(
            state("cursor", r#"{"hook_event_name":"beforeSubmitPrompt","prompt":"x"}"#),
            Some(AgentState::Working)
        );
        assert_eq!(
            state("auggie", r#"{"hook_event_name":"Stop","agent_stop_cause":"end_turn"}"#),
            Some(AgentState::Done)
        );
        assert_eq!(
            state("auggie", r#"{"hook_event_name":"Stop","agent_stop_cause":"interrupted"}"#),
            Some(AgentState::Idle)
        );
        assert!(!hooks_cover_lifecycle("cursor") && !hooks_cover_lifecycle("codex") && hooks_cover_lifecycle("gemini"));
    }
}