plannotator-tui 0.13.0

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
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
//! The agents running in Rex blocks, for the agent picker (`docs/spec-pi-link.md`).
//!
//! Rex has no agent list, so this builds one from reads. `rex ls` names the sessions and
//! `rex window ls` each session's windows (Rex's tabs) and the blocks in them. Each terminal
//! block's `process` and `program_status` methods then say which agent runs there, in which
//! folder and in what state, and its `title` what the agent calls its work. A block whose
//! program is no agent is left out, and so is the review's own block. A read that fails
//! drops only what it would have shown: an unreadable session lists none of its agents, an
//! unreadable title leaves the name out.

use std::path::PathBuf;
use std::process::{Command, Stdio};

use serde_json::Value;

use super::command::{self, short_id};
use super::launch::{foreground, reported_host};

/// The creator of terminal blocks, the only blocks an agent runs in.
const TERMINAL: &str = "com.superlogical.terminal";
/// How many blocks are read at once. Each read is a `rex` process; twenty agents take one
/// round instead of sixty calls in a row.
const PARALLEL: usize = 16;
/// The longest title a row shows, in characters, so the place column stays on screen.
const NAME_CHARS: usize = 28;

/// The state of an agent that has not reported one to Rex. Send refuses it, since Enter
/// could answer a dialog Rex cannot see, so the row says so before you pick it.
pub(crate) const NO_STATE: &str = "no state";

/// An agent in a Rex block, as the picker shows it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BlockAgent {
    /// The session the block is in, by ID: Send scopes its calls to it.
    pub(crate) session_id: String,
    /// The session's label, else its short ID.
    pub(crate) session: String,
    /// The window's (tab's) label, else its position.
    pub(crate) window: String,
    /// The block Send pastes into.
    pub(crate) block: String,
    /// The agent: `claude`, `codex`, `pi`, ...
    pub(crate) agent: String,
    /// The agent's process. Send refuses once another program runs in the block.
    pub(crate) pid: u32,
    /// The block's label when someone gave it one, else the title the agent set.
    pub(crate) name: Option<String>,
    /// `idle`, `working`, `blocked`, `error`, `done`, or [`NO_STATE`] when the agent has not
    /// reported a state to Rex.
    pub(crate) status: String,
    /// The agent's folder.
    pub(crate) cwd: Option<PathBuf>,
    /// In the window the review runs in.
    pub(crate) here: bool,
    /// Rex's own order: session, window, block. Breaks ties between rows with one label.
    pub(crate) rank: (usize, usize, usize),
}

impl BlockAgent {
    /// `claude`, or `claude · Fix login redirect` when the block has a name.
    pub(crate) fn label(&self) -> String {
        match &self.name {
            Some(name) => format!("{} · {name}", self.agent),
            None => self.agent.clone(),
        }
    }

    /// Where the block is: `session › window · block`, the block as `rex ls` prints it.
    pub(crate) fn place(&self) -> String {
        format!("{} › {} · {}", self.session, self.window, short_id(&self.block))
    }
}

/// A terminal block that may hold an agent, with where it is.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Candidate {
    session_id: String,
    session: String,
    window: String,
    block: String,
    /// The block's label, when it is not the one Rex gives every block of its flavor.
    label: Option<String>,
    here: bool,
    rank: (usize, usize, usize),
}

/// Reads the agents from one Rex: its binary, the session and block the review runs in.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RexAgents {
    pub(crate) bin: PathBuf,
    /// The review's session, or a block in that session (see [`command::session_from`]).
    /// The picker lists this session's agents first.
    pub(crate) session: String,
    /// `REX_BLOCK`: never listed, it is the review's own block. Its window comes first.
    pub(crate) own_block: Option<String>,
}

impl RexAgents {
    /// Inside Rex (`REX_BLOCK` or `REX_SESSION` set), the agents Rex runs; else `None`. Rex
    /// sets `REX_BLOCK` and `REX_BIN_DIR` in every block, so the review's own pane has them
    /// whether a launcher opened it or you ran it there.
    pub(crate) fn from_lookup(env: impl Fn(&str) -> Option<String>) -> Option<Self> {
        let session = command::session_from(&env)?;
        let own_block = env("REX_BLOCK").filter(|v| !v.is_empty());
        Some(Self { bin: command::bin_from(&env), session, own_block })
    }

    /// Every block with an agent except our own, the review's session first, then in Rex's
    /// order. Empty when Rex cannot be read.
    pub(crate) fn list(&self) -> Vec<BlockAgent> {
        let listed = self.read(&["ls", "--json"]);
        let mut found = listed.as_ref().map(sessions).unwrap_or_default();
        // `rex ls` lists sessions by their session IDs. If `session` is not in that list, it is
        // a block's ID, or `rex ls` failed. The window list of `session` gives the real session
        // ID. It is also that session's own window list, so the loop below does not read it again.
        // When it cannot be read, the other sessions are still listed, as for any unreadable
        // session.
        let mut own_view = None;
        let session = if found.iter().any(|(id, _)| *id == self.session) {
            self.session.clone()
        } else if let Some((id, view)) = self.own_window_list() {
            if !found.iter().any(|(listed, _)| *listed == id) {
                found.push((id.clone(), label_or(&view, short_id(&id))));
            }
            own_view = Some(view);
            id
        } else {
            self.session.clone()
        };
        // The review's session first; the sort is stable, so the rest keep Rex's order.
        found.sort_by_key(|(id, _)| *id != session);
        let candidates: Vec<Candidate> = found
            .iter()
            .enumerate()
            .filter_map(|(at, (id, label))| {
                let own = if *id == session { own_view.take() } else { None };
                let view = own.or_else(|| self.read(&["window", "ls", "-s", id, "--json"]))?;
                Some(blocks(&view, at, label, self.own_block.as_deref()))
            })
            .flatten()
            .collect();
        candidates
            .chunks(PARALLEL)
            .flat_map(|chunk| {
                std::thread::scope(|scope| {
                    let reads: Vec<_> = chunk.iter().map(|c| scope.spawn(move || self.agent(c))).collect();
                    reads.into_iter().filter_map(|read| read.join().ok().flatten()).collect::<Vec<_>>()
                })
            })
            .collect()
    }

    /// `rex window ls` of the review's session or block, with the session ID it names.
    fn own_window_list(&self) -> Option<(String, Value)> {
        let view = self.read(&["window", "ls", "-s", &self.session, "--json"])?;
        let id = view.get("session_id").and_then(Value::as_str).filter(|id| !id.is_empty())?.to_owned();
        Some((id, view))
    }

    /// The agent in `candidate`'s block, if one runs there. Only an agent's title is read.
    fn agent(&self, candidate: &Candidate) -> Option<BlockAgent> {
        let call = |method: &str| {
            let method = format!("{TERMINAL}.{method}");
            self.read(&["do", &method, "-b", &candidate.block, "-s", &candidate.session_id])
        };
        let process = call("process")?;
        let status = call("program_status");
        let fg = foreground(&process)?;
        status.as_ref().and_then(|s| reported_host(s, fg.pid)).or(fg.host)?;
        let title = call("title");
        agent(candidate, &process, status.as_ref(), title.as_ref())
    }

    fn read(&self, args: &[&str]) -> Option<Value> {
        let output = Command::new(&self.bin)
            .args(args)
            .stdin(Stdio::null())
            .stderr(Stdio::null())
            .output()
            .ok()
            .filter(|o| o.status.success())?;
        serde_json::from_slice(&output.stdout).ok()
    }
}

/// The sessions in `rex ls --json`, as (ID, label or short ID), in Rex's order.
fn sessions(listed: &Value) -> Vec<(String, String)> {
    let listed = listed.get("sessions").and_then(Value::as_array).into_iter().flatten();
    listed
        .filter_map(|session| {
            let id = session.get("session_id").and_then(Value::as_str).filter(|id| !id.is_empty())?;
            Some((id.to_owned(), label_or(session, short_id(id))))
        })
        .collect()
}

/// `entry`'s label, trimmed, else `fallback`.
fn label_or(entry: &Value, fallback: &str) -> String {
    entry
        .get("label")
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|label| !label.is_empty())
        .unwrap_or(fallback)
        .to_owned()
}

/// The terminal blocks in one session's `rex window ls --json`, window by window, in every
/// layer (tiled and floating), leaving out `own_block`. The session is the `at`th listed.
fn blocks(view: &Value, at: usize, session: &str, own_block: Option<&str>) -> Vec<Candidate> {
    let Some(session_id) = view.get("session_id").and_then(Value::as_str) else { return Vec::new() };
    let windows = view.get("windows").and_then(Value::as_array).into_iter().flatten();
    let mut found = Vec::new();
    for (position, window) in windows.enumerate() {
        let in_window: Vec<&Value> = window
            .get("layers")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .flat_map(|layer| layer.get("blocks").and_then(Value::as_array).into_iter().flatten())
            .collect();
        let id = |block: &Value| block.get("block_id").and_then(Value::as_str).map(str::to_owned);
        let here = own_block.is_some_and(|own| in_window.iter().any(|b| id(b).as_deref() == Some(own)));
        let name = label_or(window, &(position + 1).to_string());
        for (index, block) in in_window.into_iter().enumerate() {
            let Some(block_id) = id(block).filter(|b| Some(b.as_str()) != own_block) else { continue };
            if block.get("creator_name").and_then(Value::as_str).is_some_and(|c| c != TERMINAL) {
                continue;
            }
            found.push(Candidate {
                session_id: session_id.to_owned(),
                session: session.to_owned(),
                window: name.clone(),
                block: block_id,
                label: given_label(block),
                here,
                rank: (at, position, index),
            });
        }
    }
    found
}

/// A block's label when someone gave it one: not empty, and not the name of its flavor,
/// which is what Rex labels every new block (`shell` for `com.superlogical.terminal.shell`).
fn given_label(block: &Value) -> Option<String> {
    let label = block.get("label").and_then(Value::as_str).map(str::trim).filter(|l| !l.is_empty())?;
    let flavor = block.get("flavor").and_then(Value::as_str).unwrap_or_default();
    let default = flavor.rsplit('.').next().unwrap_or_default();
    (label != default).then(|| label.to_owned())
}

/// The agent in `candidate`'s block from its `process`, `program_status` and `title`
/// replies; `None` when its foreground program is no agent. The agent is the one its status
/// report names, else the one its process names, as a launcher finds it.
fn agent(
    candidate: &Candidate,
    process: &Value,
    status: Option<&Value>,
    title: Option<&Value>,
) -> Option<BlockAgent> {
    let fg = foreground(process)?;
    let host = status.and_then(|s| reported_host(s, fg.pid)).or(fg.host)?;
    let name = candidate.label.clone().or_else(|| title.and_then(|t| agent_title(t, fg.pid, host, status)));
    Some(BlockAgent {
        session_id: candidate.session_id.clone(),
        session: candidate.session.clone(),
        window: candidate.window.clone(),
        block: candidate.block.clone(),
        agent: host.to_owned(),
        pid: fg.pid,
        name,
        status: state(status, fg.pid),
        cwd: fg.cwd,
        here: candidate.here,
        rank: candidate.rank,
    })
}

/// The agent's state from `program_status`: of the records process `pid` owns, the one that
/// most needs you. [`NO_STATE`] when it owns none: it has not reported, or another program did.
pub(crate) fn state(status: Option<&Value>, pid: u32) -> String {
    const URGENCY: [&str; 5] = ["blocked", "error", "working", "idle", "done"];
    let records = status.and_then(|s| s.get("records")).and_then(Value::as_array).into_iter().flatten();
    records
        .filter(|r| r.pointer("/owner/pid").and_then(Value::as_u64) == Some(u64::from(pid)))
        // Rex: treat a state you don't know as idle.
        .map(|r| r.get("state").and_then(Value::as_str).filter(|s| URGENCY.contains(s)).unwrap_or("idle"))
        .min_by_key(|s| URGENCY.iter().position(|u| u == s))
        .unwrap_or(NO_STATE)
        .to_owned()
}

/// The title the agent, process `pid`, set: without the spinner or mark an agent puts in
/// front, cut to [`NAME_CHARS`]. `None` when another program set it, or it only names the
/// agent (`codex`, or `Claude Code` for `claude-code`).
fn agent_title(title: &Value, pid: u32, host: &str, status: Option<&Value>) -> Option<String> {
    let owner = title.pointer("/owner/pid").and_then(Value::as_u64);
    if owner.is_some_and(|owner| owner != u64::from(pid)) {
        return None;
    }
    let text = title.get("title").and_then(Value::as_str)?;
    let text = text.trim_start_matches(|c: char| !c.is_alphanumeric()).trim();
    let plain = text.to_lowercase();
    let app = status
        .and_then(|s| s.get("records"))
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter(|r| r.pointer("/owner/pid").and_then(Value::as_u64) == Some(u64::from(pid)))
        .find_map(|r| r.get("effective_app").or_else(|| r.get("app")).and_then(Value::as_str));
    if text.is_empty() || plain == host || app.is_some_and(|app| plain.replace(' ', "-") == app) {
        return None;
    }
    Some(if text.chars().count() > NAME_CHARS {
        let cut: String = text.chars().take(NAME_CHARS - 1).collect();
        format!("{}…", cut.trim_end())
    } else {
        text.to_owned()
    })
}

/// The recorded fixtures in `tests/fixtures/rex-agents`: one Rex with sessions `work`
/// (window `api`: a shell, Claude, Codex; window `web`: a Claude at a permission prompt, pi)
/// and `side` (a Claude in a block labelled `release`), read from Rex 0.1.0-preview.22 with
/// stand-in agents and paths rewritten; `live-window-ls.json` is from a live Rex
/// 0.1.0-preview.21 with one shell.
#[cfg(test)]
pub(crate) fn fixture_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/rex-agents")
}

/// The block in the fixtures where the review runs: the shell in `work › api`.
#[cfg(test)]
pub(crate) const FIXTURE_OWN_BLOCK: &str = "block:01a1f658-7c0b-702a-a296-443d917d7f85";
/// The fixtures' `work` session.
#[cfg(test)]
pub(crate) const FIXTURE_SESSION: &str = "session:01a1f658-7c0a-71ad-8ff6-a1bcbc31504f";

/// One recorded reply from the fixtures.
#[cfg(test)]
#[allow(clippy::expect_used, reason = "test fixtures assert by panicking")]
fn fixture(name: &str) -> Value {
    let text = std::fs::read_to_string(fixture_dir().join(name)).expect("fixture");
    serde_json::from_str(&text).expect("fixture JSON")
}

/// The agents in the fixtures, read the way [`RexAgents::list`] reads them, without a `rex`.
#[cfg(test)]
pub(crate) fn fixture_agents(own_block: Option<&str>) -> Vec<BlockAgent> {
    let mut listed = sessions(&fixture("ls.json"));
    listed.sort_by_key(|(id, _)| id != FIXTURE_SESSION);
    listed
        .iter()
        .enumerate()
        .flat_map(|(at, (_, label))| {
            blocks(&fixture(&format!("window-ls-{label}.json")), at, label, own_block)
        })
        .filter_map(|c| {
            let short = short_id(&c.block).to_owned();
            let read = |m: &str| fixture(&format!("{short}.{m}.json"));
            agent(&c, &read("process"), Some(&read("program_status")), Some(&read("title")))
        })
        .collect()
}

/// A fake `rex` in `dir` for tests that answers the reads from the fixtures and records a
/// send. Every call's arguments go to `dir/calls`; `api call` writes its stdin to
/// `dir/pastes`, one line per paste.
#[cfg(all(test, unix))]
#[allow(clippy::expect_used, reason = "test fixtures assert by panicking")]
pub(crate) fn fake_rex(dir: &std::path::Path) -> PathBuf {
    let _ = std::fs::remove_dir_all(dir);
    std::fs::create_dir_all(dir).expect("fake rex dir");
    for entry in std::fs::read_dir(fixture_dir()).expect("fixtures") {
        let path = entry.expect("fixture").path();
        std::fs::copy(&path, dir.join(path.file_name().expect("name"))).expect("copy fixture");
    }
    let script = format!(
        r#"#!/bin/sh
d='{d}'
echo "$*" >> "$d/calls"
case "$1 $2" in
  'ls --json') cat "$d/ls.json" ;;
  'window ls') f=$(grep -Fl "\"$4\"" "$d"/window-ls-*.json | head -1); [ -n "$f" ] || exit 3; cat "$f" ;;
  do\ com.superlogical.terminal.*)
    m=${{2#com.superlogical.terminal.}}; b=$4; f="$d/${{b#"${{b%??????}}"}}.$m.json"
    [ -f "$f" ] || exit 3; cat "$f" ;;
  'api call') cat >> "$d/pastes"; echo >> "$d/pastes"; echo '{{}}' ;;
  'send-key -b'|'focus '*) ;;
  *) exit 2 ;;
esac
"#,
        d = dir.display()
    );
    let bin = dir.join("rex");
    crate::test_support::write_script(&bin, &script);
    bin
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::indexing_slicing, reason = "tests assert by panicking")]
mod tests {
    use std::path::Path;

    use serde_json::json;

    use super::*;

    fn fixture(name: &str) -> Value {
        super::fixture(name)
    }

    fn recorded(own_block: Option<&str>) -> Vec<BlockAgent> {
        fixture_agents(own_block)
    }

    #[test]
    fn every_agent_in_every_window_and_session_is_found_and_the_shells_are_not() {
        let agents = recorded(Some(FIXTURE_OWN_BLOCK));
        let rows: Vec<String> =
            agents.iter().map(|a| format!("{} | {} | {}", a.label(), a.status, a.place())).collect();
        assert_eq!(
            rows,
            [
                "claude · Fix login redirect | idle | work › api · 9ba798",
                "codex | working | work › api · 31496a",
                "claude · Migrate styles | blocked | work › web · 63239c",
                "pi · π - notes | idle | work › web · eda7cc",
                "claude · release | idle | side › Window 122adf3e · fd82e9",
            ]
        );
        let here: Vec<bool> = agents.iter().map(|a| a.here).collect();
        assert_eq!(here, [true, true, false, false, false], "the review runs in work › api");
        assert_eq!(agents[0].cwd.as_deref(), Some(Path::new("/Users/me/work/api")));
        assert_eq!(agents[1].cwd.as_deref(), Some(Path::new("/Users/me/work/api/src")));
        assert_eq!(agents[0].pid, 94903);
        assert_eq!(agents[0].session_id, FIXTURE_SESSION);
        assert_eq!(agents[3].agent, "pi", "pi runs under node; its status report names it");
    }

    #[test]
    fn the_reviews_own_block_is_never_listed_and_without_it_no_window_is_here() {
        let own = "block:01a1f658-997c-79c0-ae26-1bf77b9ba798";
        let agents = recorded(Some(own));
        assert!(!agents.iter().any(|a| a.block == own));
        assert_eq!(agents.iter().filter(|a| a.here).count(), 1, "the Codex beside it");
        let anywhere = recorded(None);
        assert_eq!(anywhere.len(), 5);
        assert_eq!(anywhere.iter().filter(|a| a.here).count(), 0);
    }

    #[test]
    fn a_rex_with_only_a_shell_has_one_block_to_look_at() {
        // Recorded from a live Rex (session label changed).
        let live = blocks(&fixture("live-window-ls.json"), 0, "home", None);
        assert_eq!(live.len(), 1);
        assert_eq!(live[0].label, None, "`shell` is the flavor's own label");
        assert_eq!(live[0].window, "Window 0eab11cc");
        let shell = agent(
            &live[0],
            &fixture("7d7f85.process.json"),
            Some(&fixture("7d7f85.program_status.json")),
            None,
        );
        assert_eq!(shell, None, "a shell is no agent");
    }

    #[test]
    fn the_state_is_the_agents_own_most_urgent_record() {
        let pid = 7;
        let status = |records: Value| json!({ "records": records });
        assert_eq!(state(None, pid), "no state");
        assert_eq!(state(Some(&status(json!([]))), pid), "no state");
        let other = status(json!([{"state": "working", "owner": {"pid": 8}}]));
        assert_eq!(state(Some(&other), pid), "no state", "another program's record says nothing");
        let mixed = status(json!([
            {"state": "idle", "owner": {"pid": 7}},
            {"id": "build", "state": "blocked", "kind": "question", "owner": {"pid": 7}},
            {"id": "x", "state": "working", "owner": {"pid": 7}},
        ]));
        assert_eq!(state(Some(&mixed), pid), "blocked");
        let odd = status(json!([{"state": "dreaming", "owner": {"pid": 7}}]));
        assert_eq!(state(Some(&odd), pid), "idle", "Rex: an unknown state reads as idle");
    }

    #[test]
    fn a_title_loses_its_spinner_and_is_kept_only_when_the_agent_set_it_and_says_more() {
        let title = |text: &str, owner: u32| json!({"title": text, "owner": {"pid": owner, "name": "x"}});
        let claude = json!({"records": [{"state": "idle", "app": "claude-code", "effective_app": "claude-code", "owner": {"pid": 7}}]});
        let named = |t: &Value| agent_title(t, 7, "claude", Some(&claude));
        assert_eq!(named(&title("⠐ Fix login redirect", 7)).as_deref(), Some("Fix login redirect"));
        assert_eq!(named(&title("✳ Claude Code", 7)), None, "only the agent's name");
        assert_eq!(named(&title("claude", 7)), None);
        assert_eq!(named(&title("✳ Fix login redirect", 8)), None, "set by another program");
        assert_eq!(named(&json!({"title": ""})), None);
        assert_eq!(named(&json!({"title": "no owner known"})).as_deref(), Some("no owner known"));
        let long = named(&title("✳ Refactor the session restore path for windows", 7)).expect("named");
        assert_eq!(long, "Refactor the session restor…");
        assert_eq!(long.chars().count(), NAME_CHARS);
    }

    #[test]
    fn only_a_label_someone_gave_names_the_block() {
        let block = |label: &str, flavor: &str| json!({"label": label, "flavor": flavor});
        assert_eq!(given_label(&block("shell", "com.superlogical.terminal.shell")), None);
        assert_eq!(given_label(&block(" ", "com.superlogical.terminal.shell")), None);
        assert_eq!(
            given_label(&block("reviewer", "com.superlogical.terminal.shell")).as_deref(),
            Some("reviewer")
        );
    }

    #[test]
    fn outside_rex_there_is_nothing_to_read() {
        let env = |vars: &'static [(&'static str, &'static str)]| {
            move |k: &str| vars.iter().find(|(name, _)| *name == k).map(|(_, v)| (*v).to_owned())
        };
        assert_eq!(RexAgents::from_lookup(env(&[])), None);
        assert_eq!(RexAgents::from_lookup(env(&[("REX_SESSION", ""), ("REX_BLOCK", "")])), None);
        let block_only = RexAgents::from_lookup(env(&[("REX_BLOCK", "block:b")])).expect("rex");
        assert_eq!(block_only.session, "block:b");
        assert_eq!(block_only.own_block.as_deref(), Some("block:b"));
        let inside =
            RexAgents::from_lookup(env(&[("REX_SESSION", "session:s"), ("REX_BIN_DIR", "/opt/rex")]))
                .expect("rex");
        assert_eq!(inside.bin, PathBuf::from("/opt/rex/rex"));
        assert_eq!(inside.own_block, None);
    }

    #[test]
    fn sessions_without_an_id_are_skipped_and_garbage_lists_none() {
        let listed =
            json!({"sessions": [{"session_id": "session:abcdef123456", "label": " "}, {"label": "x"}]});
        assert_eq!(sessions(&listed), [("session:abcdef123456".to_owned(), "123456".to_owned())]);
        assert_eq!(sessions(&json!("nonsense")), []);
        assert_eq!(blocks(&json!({"windows": []}), 0, "s", None), []);
    }

    #[cfg(unix)]
    #[test]
    fn list_reads_every_session_and_only_reads() {
        let dir = std::env::temp_dir().join(format!("plannotator-tui-rex-agents-{}", std::process::id()));
        let bin = fake_rex(&dir);
        let rex =
            RexAgents { bin, session: FIXTURE_SESSION.into(), own_block: Some(FIXTURE_OWN_BLOCK.into()) };
        assert_eq!(rex.list(), recorded(Some(FIXTURE_OWN_BLOCK)));
        let calls = std::fs::read_to_string(dir.join("calls")).expect("calls");
        let verbs: std::collections::BTreeSet<String> =
            calls.lines().map(|l| l.split_whitespace().take(2).collect::<Vec<_>>().join(" ")).collect();
        assert_eq!(
            verbs.into_iter().collect::<Vec<_>>(),
            [
                "do com.superlogical.terminal.process",
                "do com.superlogical.terminal.program_status",
                "do com.superlogical.terminal.title",
                "ls --json",
                "window ls",
            ],
            "only reads"
        );
        assert!(!calls.contains(&format!("-b {FIXTURE_OWN_BLOCK}")), "our own block is not read");
        let titles = calls.lines().filter(|l| l.contains(".title")).count();
        assert_eq!(titles, 5, "one title per agent: {calls}");
        let gone = RexAgents { bin: dir.join("absent"), session: FIXTURE_SESSION.into(), own_block: None };
        assert_eq!(gone.list(), []);
        std::fs::remove_dir_all(&dir).expect("cleanup");
    }

    #[cfg(unix)]
    #[test]
    fn a_review_known_only_by_its_block_lists_its_own_session_first_and_once() {
        let dir =
            std::env::temp_dir().join(format!("plannotator-tui-rex-agents-by-block-{}", std::process::id()));
        let bin = fake_rex(&dir);
        // `rex ls` lists `side` second, so only the sort can put its agent first.
        let side_block = "block:01a1f658-9a2c-7d0b-a3ae-7899dcfd82e9";
        let rex = RexAgents { bin, session: side_block.into(), own_block: None };
        let agents = rex.list();
        assert_eq!(agents.len(), 5, "each session once: {agents:?}");
        assert_eq!(agents[0].place(), "side › Window 122adf3e · fd82e9");
        assert_eq!(agents[0].rank.0, 0, "the review's session comes first");
        assert!(agents[1..].iter().all(|a| a.rank.0 == 1));
        let calls = std::fs::read_to_string(dir.join("calls")).expect("calls");
        let window_reads: Vec<&str> = calls.lines().filter(|l| l.starts_with("window ls")).collect();
        assert_eq!(
            window_reads,
            [
                format!("window ls -s {side_block} --json"),
                "window ls -s session:01a1f658-7c0a-71ad-8ff6-a1bcbc31504f --json".to_owned(),
            ],
            "the block's window list is the session's, so it is read once"
        );
        std::fs::remove_dir_all(&dir).expect("cleanup");
    }

    #[cfg(unix)]
    #[test]
    fn a_review_whose_own_session_cannot_be_read_still_lists_the_other_sessions() {
        let dir =
            std::env::temp_dir().join(format!("plannotator-tui-rex-agents-unread-{}", std::process::id()));
        let bin = fake_rex(&dir);
        // A block Rex no longer knows: its window list fails, `rex ls` still answers.
        let rex =
            RexAgents { bin, session: "block:01a1f658-0000-0000-0000-000000000000".into(), own_block: None };
        assert_eq!(rex.list(), recorded(None), "every listed session, in Rex's order");
        std::fs::remove_dir_all(&dir).expect("cleanup");
    }
}