plannotator-tui 0.9.5

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
#![allow(clippy::expect_used, reason = "tests assert by panicking")]

use super::*;
use crate::config::Config;
use crate::herdr::context::HerdrContext;

fn env(pane_id: Option<&str>, context: Option<HerdrContext>) -> HerdrEnv {
    HerdrEnv { in_herdr: true, pane_id: pane_id.map(str::to_owned), context, ..HerdrEnv::default() }
}

fn temp_folder(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("plannotator-tui-launch-{tag}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("docs")).expect("temp dir");
    std::fs::write(dir.join("docs/plan.md"), "# plan\n").expect("file");
    dir
}

#[test]
fn human_keybind_opens_the_focused_folder_and_delivers_to_its_agent() {
    let root = temp_folder("keybind");
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_cwd: Some(root.display().to_string()),
        focused_pane_agent: Some("claude".into()),
        workspace_cwd: Some("/elsewhere".into()),
        invocation_source: Some("keybinding".into()),
        ..HerdrContext::default()
    };
    let launch = plan(&env(None, Some(context)), &Config::default(), OpenArgs::default(), Path::new("/"))
        .expect("plans");
    assert_eq!(launch.file, root);
    assert_eq!(launch.cwd, root, "a folder is its own cwd");
    assert_eq!(launch.placement, Placement::Overlay);
    assert_eq!(launch.deliver, Some(Target { pane: "w1:p1".into(), agent: Some("claude".into()) }));
    assert_eq!(
        argv(&launch),
        vec![
            "plugin",
            "pane",
            "open",
            "--plugin",
            "annotate",
            "--entrypoint",
            "doc",
            "--placement",
            "overlay",
            "--focus",
            "--cwd",
            &root.display().to_string(),
            "--env",
            &format!("PLANNOTATOR_TUI_FILE={}", root.display()),
            "--env",
            "PLANNOTATOR_TUI_DELIVER_TO=w1:p1",
            "--env",
            "PLANNOTATOR_TUI_DELIVER_AGENT=claude",
        ]
    );
    std::fs::remove_dir_all(&root).expect("cleanup");
}

#[test]
fn agent_skill_delivers_to_and_splits_beside_the_calling_pane() {
    let root = temp_folder("skill");
    let config = Config::parse("[herdr]\nplacement = \"split\"\n").expect("config");
    let args = OpenArgs { path: Some(PathBuf::from("docs/plan.md")), ..OpenArgs::default() };
    let launch = plan(&env(Some("w2:p4"), None), &config, args, &root).expect("plans");
    assert_eq!(launch.file, root.join("docs/plan.md"), "relative paths resolve against the caller's cwd");
    assert_eq!(launch.cwd, root.join("docs"), "a file's cwd is its folder");
    assert_eq!(launch.deliver, Some(Target { pane: "w2:p4".into(), agent: None }));
    assert_eq!(launch.target_pane.as_deref(), Some("w2:p4"));
    let args = argv(&launch);
    let after_placement: Vec<&str> = args.iter().map(String::as_str).skip(7).take(6).collect();
    assert_eq!(after_placement, ["--placement", "split", "--direction", "right", "--target-pane", "w2:p4"]);
    assert!(args.iter().any(|a| a == "PLANNOTATOR_TUI_DELIVER_TO=w2:p4"));
    assert!(!args.iter().any(|a| a.starts_with("PLANNOTATOR_TUI_DELIVER_AGENT")));
    std::fs::remove_dir_all(&root).expect("cleanup");
}

#[test]
fn mirror_defaults_to_a_split_next_to_the_remote_invoking_pane() {
    let context = HerdrContext {
        focused_pane_id: Some("w2:p4".into()),
        focused_pane_agent: Some("codex".into()),
        invocation_source: Some("mirror".into()),
        ..HerdrContext::default()
    };
    let env = env(None, Some(context));
    let launch = plan(&env, &Config::default(), OpenArgs::default(), Path::new("/"))
        .expect("plans a mirrored document");
    assert_eq!(launch.placement, Placement::Split);
    assert!(argv(&launch).windows(2).any(|pair| pair == ["--target-pane", "w2:p4"]));
    let agent = r#"{"result":{"agent":{"agent":"codex","agent_session":{"kind":"path","value":"/sessions/exact.jsonl"}}}}"#;
    let last = plan_last(&env, &Config::default(), OpenArgs::default(), Path::new("/"), None, Some(agent))
        .expect("plans the same pane's reply");
    assert_eq!(last.placement, Placement::Split);
    assert_eq!(last.target_pane.as_deref(), Some("w2:p4"));
    assert_eq!(last.session, Some(AgentSession::Path("/sessions/exact.jsonl".into())));
}

#[test]
fn explicit_placement_still_wins_over_the_mirror_default() {
    let mut env =
        env(None, Some(HerdrContext { invocation_source: Some("mirror".into()), ..HerdrContext::default() }));
    env.placement = Some("popup".into());
    let launch =
        plan(&env, &Config::default(), OpenArgs::default(), Path::new("/")).expect("environment placement");
    assert_eq!(launch.placement, Placement::Popup);
    let args = OpenArgs { placement: Some(Placement::Overlay), ..OpenArgs::default() };
    let launch = plan(&env, &Config::default(), args, Path::new("/")).expect("explicit argument placement");
    assert_eq!(launch.placement, Placement::Overlay);
}

#[test]
fn ctrl_click_opens_the_linked_file() {
    let root = temp_folder("click");
    // `file:///abs/path` on Unix; `file:///C:/abs/path` on Windows.
    let url = format!(
        "file:///{}/docs/plan.md",
        root.display().to_string().replace('\\', "/").trim_start_matches('/')
    );
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("claude".into()),
        focused_pane_cwd: Some("/should/not/win".into()),
        clicked_url: Some(url),
        ..HerdrContext::default()
    };
    let launch = plan(&env(None, Some(context)), &Config::default(), OpenArgs::default(), Path::new("/"))
        .expect("plans");
    assert_eq!(launch.file, root.join("docs/plan.md"));
    assert_eq!(launch.cwd, root.join("docs"));
    assert_eq!(launch.deliver.map(|t| t.pane), Some("w1:p1".into()));
    std::fs::remove_dir_all(&root).expect("cleanup");
}

#[test]
fn only_file_urls_are_opened() {
    assert_eq!(file_url_path("file:///a/b%20c.md"), Some(PathBuf::from("/a/b c.md")));
    assert_eq!(file_url_path("file://localhost/a.md"), Some(PathBuf::from("/a.md")));
    assert_eq!(file_url_path("https://github.com/x/README.md"), None);
    assert_eq!(file_url_path("file://host/a.md"), None);
}

#[test]
fn popup_placement_emits_size_and_no_target_pane() {
    let config = Config::parse("[herdr]\npopup_width = \"100%\"\npopup_height = \"100%\"\n").expect("config");
    let args = OpenArgs {
        placement: Some(Placement::Popup),
        deliver_to: Some("w1:p1".into()),
        ..OpenArgs::default()
    };
    let launch = plan(&env(Some("w1:p9"), None), &config, args, Path::new("/tmp")).expect("plans");
    assert_eq!(launch.deliver, Some(Target { pane: "w1:p1".into(), agent: None }), "--deliver-to wins");
    let args = argv(&launch);
    assert!(args.windows(2).any(|w| w == ["--width", "100%"]));
    assert!(args.windows(2).any(|w| w == ["--height", "100%"]));
    assert!(!args.iter().any(|a| a == "--target-pane"));
}

#[test]
fn env_placement_beats_config_and_arg_beats_env() {
    let config = Config::parse("[herdr]\nplacement = \"popup\"\n").expect("config");
    let mut env = env(None, None);
    env.placement = Some("split".into());
    let launch = plan(&env, &config, OpenArgs::default(), Path::new("/tmp")).expect("plans");
    assert_eq!(launch.placement, Placement::Split);
    let args = OpenArgs { placement: Some(Placement::Overlay), ..OpenArgs::default() };
    assert_eq!(plan(&env, &config, args, Path::new("/tmp")).expect("plans").placement, Placement::Overlay);
    env.placement = Some("floating".into());
    assert!(plan(&env, &config, OpenArgs::default(), Path::new("/tmp")).is_err());
}

#[test]
fn run_refuses_outside_herdr() {
    let launch = plan(&HerdrEnv::default(), &Config::default(), OpenArgs::default(), Path::new("/tmp"))
        .expect("plans");
    let err = run(&HerdrEnv::default(), &launch).expect_err("refused");
    assert!(err.to_string().contains("not inside Herdr"));
}

#[test]
fn a_human_in_a_plain_shell_pane_gets_no_agent_target() {
    // A manifest action always carries a context; a focused pane without an agent is not a
    // target, and HERDR_PANE_ID (set for the action too) must not become one either.
    let context = HerdrContext { focused_pane_id: Some("w1:p2".into()), ..HerdrContext::default() };
    let env = env(Some("w1:p2"), Some(context));
    let launch = plan(&env, &Config::default(), OpenArgs::default(), Path::new("/tmp")).expect("plan");
    assert_eq!(launch.deliver, None);
    assert_eq!(launch.target_pane.as_deref(), Some("w1:p2"), "a split still opens beside the caller");
}

#[test]
fn the_plugin_id_comes_from_the_environment() {
    let env = HerdrEnv { in_herdr: true, plugin_id: Some("annotate".into()), ..HerdrEnv::default() };
    let launch = plan(&env, &Config::default(), OpenArgs::default(), Path::new("/tmp")).expect("plan");
    let args = argv(&launch);
    let plugin = args.iter().position(|a| a == "--plugin").and_then(|i| args.get(i + 1));
    assert_eq!(plugin.map(String::as_str), Some("annotate"));
}

const PROCESS_INFO: &str = r#"{"id":"cli:pane:process_info","result":{"process_info":{"foreground_process_group_id":91279,"foreground_processes":[{"argv":["claude"],"argv0":"claude","cmdline":"claude","cwd":"/w","name":"claude","pid":91279}],"pane_id":"w1:p1","shell_pid":84211},"type":"pane_process_info"}}"#;

#[test]
fn the_agent_pid_is_the_named_agent_process_else_the_group_leader() {
    assert_eq!(agent_pid(PROCESS_INFO), Some((91279, "claude".into())));
    let pi = PROCESS_INFO.replace(r#""name":"claude""#, r#""name":"pi""#);
    assert_eq!(agent_pid(&pi), Some((91279, "pi".into())));
    let unknown = PROCESS_INFO.replace(r#""name":"claude""#, r#""name":"aider""#);
    assert_eq!(agent_pid(&unknown), Some((91279, "aider".into())), "unknown agents keep their name");
    assert_eq!(agent_pid("{}"), None);
}

#[test]
fn a_last_launch_carries_the_pid_and_host_instead_of_a_file() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("claude".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let launch = plan_last(
        &env(None, Some(context)),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        Some(PROCESS_INFO),
        None,
    )
    .expect("plans");
    assert_eq!(launch.message, Some(AgentMessage { host: "claude".into(), pid: Some(91279) }));
    assert_eq!(launch.deliver.as_ref().map(|t| t.pane.as_str()), Some("w1:p1"));
    let args = argv(&launch);
    assert!(args.contains(&"PLANNOTATOR_TUI_MESSAGE_PID=91279".to_owned()));
    assert!(args.contains(&"PLANNOTATOR_TUI_HOST=claude".to_owned()));
    assert!(!args.iter().any(|a| a.starts_with("PLANNOTATOR_TUI_FILE=")));
}

/// Command Code under Herdr: agent `cmd`, no session reported, `node` in the foreground.
#[test]
fn herdrs_cmd_label_selects_command_code_while_the_pid_comes_from_the_process() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("cmd".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let node = PROCESS_INFO.replace(r#""name":"claude""#, r#""name":"node""#);
    assert_eq!(agent_pid(&node), Some((91279, "node".into())), "node alone is not an agent");
    let agent_get =
        r#"{"id":"cli:agent:get","result":{"agent":{"agent":"cmd","pane_id":"w1:p1"},"type":"agent_info"}}"#;
    let launch = plan_last(
        &env(None, Some(context)),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        Some(&node),
        Some(agent_get),
    )
    .expect("plans");
    assert_eq!(launch.message, Some(AgentMessage { host: "cmd".into(), pid: Some(91279) }));
    assert_eq!(launch.session, None);
    assert!(argv(&launch).contains(&"PLANNOTATOR_TUI_HOST=cmd".to_owned()));
}

const AGENT_GET_PATH: &str = r#"{"id":"cli:agent:get","result":{"agent":{"agent":"omp","agent_session":{"agent":"omp","kind":"path","source":"herdr:omp","value":"~/.omp/agent/sessions/--w--/2026-08-29T01-00-00-000Z_0199.jsonl"},"pane_id":"w1:p1"},"type":"agent_info"}}"#;
const AGENT_GET_ID: &str = r#"{"id":"cli:agent:get","result":{"agent":{"agent":"hermes","agent_session":{"agent":"hermes","kind":"id","source":"herdr:hermes","value":"sess_abc123"},"pane_id":"w1:p1"},"type":"agent_info"}}"#;
const AGENT_GET_PI: &str = r#"{"id":"cli:agent:get","result":{"agent":{"agent":"pi","agent_session":{"agent":"pi","kind":"path","source":"herdr:pi","value":"~/.pi/agent/sessions/--w--/session.jsonl"},"pane_id":"w1:p1"},"type":"agent_info"}}"#;

#[test]
fn herdrs_exact_agent_identity_needs_no_process_info() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("pi".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let launch = plan_last(
        &env(None, Some(context)),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        None,
        Some(AGENT_GET_PI),
    )
    .expect("plans");

    assert_eq!(launch.message, Some(AgentMessage { host: "pi".into(), pid: None }));
    assert!(!argv(&launch).iter().any(|arg| arg.starts_with("PLANNOTATOR_TUI_MESSAGE_PID=")));
    assert!(argv(&launch).contains(&"PLANNOTATOR_TUI_HOST=pi".to_owned()));
}

#[test]
fn herdrs_agent_session_is_passed_to_the_pane_as_a_path_or_an_id() {
    assert_eq!(
        agent_session(AGENT_GET_PATH),
        Some(AgentSession::Path("~/.omp/agent/sessions/--w--/2026-08-29T01-00-00-000Z_0199.jsonl".into()))
    );
    assert_eq!(agent_session(AGENT_GET_ID), Some(AgentSession::Id("sess_abc123".into())));
    assert_eq!(agent_session(r#"{"result":{"agent":{"agent":"pi"}}}"#), None);

    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("omp".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let launch = plan_last(
        &env(None, Some(context.clone())),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        None,
        Some(AGENT_GET_PATH),
    )
    .expect("plans");
    let args = argv(&launch);
    assert!(args.iter().any(|a| a.starts_with("PLANNOTATOR_TUI_SESSION=~/.omp/agent/sessions/")), "{args:?}");
    assert!(args.contains(&"PLANNOTATOR_TUI_HOST=omp".to_owned()));

    let launch = plan_last(
        &env(None, Some(context)),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        None,
        Some(AGENT_GET_ID),
    )
    .expect("plans");
    let args = argv(&launch);
    assert!(args.contains(&"PLANNOTATOR_TUI_SESSION_ID=sess_abc123".to_owned()), "{args:?}");
}

#[test]
fn newest_reaches_the_pane_only_when_last_was_asked_for_it() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("claude".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let last = |args| {
        plan_last(
            &env(None, Some(context.clone())),
            &Config::default(),
            args,
            Path::new("/"),
            None,
            Some(AGENT_GET_PI),
        )
        .expect("plans")
    };
    let asked = OpenArgs { newest: true, ..OpenArgs::default() };
    assert!(argv(&last(asked)).contains(&"PLANNOTATOR_TUI_NEWEST=1".to_owned()));
    assert!(!argv(&last(OpenArgs::default())).iter().any(|a| a.starts_with("PLANNOTATOR_TUI_NEWEST")));

    // `herdr open` has no picker to skip, so a document launch never carries the flag.
    let open = plan(&env(None, Some(context)), &Config::default(), OpenArgs::default(), Path::new("/"))
        .expect("plans");
    assert!(!argv(&open).iter().any(|a| a.starts_with("PLANNOTATOR_TUI_NEWEST")));
}

#[test]
fn terminal_review_reads_the_focused_pane_and_delivers_to_its_agent() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p1".into()),
        focused_pane_agent: Some("claude".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let read = TerminalRead { pane: "w1:p1".into(), lines: 200 };
    let launch = plan_terminal(
        &env(None, Some(context)),
        &Config::default(),
        OpenArgs::default(),
        Path::new("/"),
        read,
    )
    .expect("plans");
    let args = argv(&launch);
    assert!(args.contains(&"PLANNOTATOR_TUI_TERMINAL_PANE=w1:p1".to_owned()), "{args:?}");
    assert!(args.contains(&"PLANNOTATOR_TUI_TERMINAL_LINES=200".to_owned()), "{args:?}");
    assert!(args.contains(&"PLANNOTATOR_TUI_DELIVER_TO=w1:p1".to_owned()), "{args:?}");
    assert!(args.contains(&"PLANNOTATOR_TUI_DELIVER_AGENT=claude".to_owned()), "{args:?}");
    assert!(!args.iter().any(|a| a.starts_with("PLANNOTATOR_TUI_FILE=")), "a terminal review opens no file");
    assert!(!args.iter().any(|a| a.starts_with("PLANNOTATOR_TUI_HOST=")), "nor an agent message");
}

#[test]
fn terminal_review_of_a_shell_splits_beside_it_and_delivers_nowhere() {
    let context = HerdrContext {
        focused_pane_id: Some("w1:p4".into()),
        focused_pane_cwd: Some("/w".into()),
        ..HerdrContext::default()
    };
    let read = TerminalRead { pane: "w1:p4".into(), lines: 50 };
    let args = OpenArgs { placement: Some(Placement::Split), ..OpenArgs::default() };
    let launch =
        plan_terminal(&env(Some("w9:p9"), Some(context)), &Config::default(), args, Path::new("/"), read)
            .expect("plans");
    assert_eq!(launch.deliver, None, "no agent in the pane: the review falls back to the clipboard");
    let args = argv(&launch);
    let target = args.iter().position(|a| a == "--target-pane").and_then(|i| args.get(i + 1));
    assert_eq!(target.map(String::as_str), Some("w1:p4"));
    assert!(args.contains(&"PLANNOTATOR_TUI_TERMINAL_PANE=w1:p4".to_owned()), "{args:?}");
}