rhei-cli 0.3.0

Command-line driver for the Rhei agent runtime.
Documentation
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
// `rhei list` — the filter set, the walk it does over a loaded plan, and what
// it prints.
//
// Its own part because listing is read-only presentation: it decides nothing
// about state, resolves no target, and writes no file.

// §FS-rhei-list

/// Filter set for the `list` subcommand. See `Commands::List` for flag docs.
struct ListFilters {
    /// Narrow to named rheis; empty is the whole project. §FS-rhei-panta.6.4
    rhei: Vec<String>,
    states: Vec<String>,
    assignee: Option<String>,
    no_assignee: bool,
    kind: Option<String>,
    has_prior: Option<String>,
    parent: Option<String>,
    root: bool,
    contains: Option<String>,
    terminal: bool,
    non_terminal: bool,
    ready: bool,
    blocked: bool,
    limit: usize,
}

impl ListFilters {
    /// True when nothing narrows the listing, so it is showing the plan rather
    /// than answering a question about part of it. §FS-rhei-list.4.1
    fn none_active(&self) -> bool {
        self.rhei.is_empty()
            && self.states.is_empty()
            && self.assignee.is_none()
            && !self.no_assignee
            && self.kind.is_none()
            && self.has_prior.is_none()
            && self.parent.is_none()
            && !self.root
            && self.contains.is_none()
            && !self.terminal
            && !self.non_terminal
            && !self.ready
            && !self.blocked
            && self.limit == 0
    }
}

/// Name the rheis that hold no tickets, in the wording
/// `rhei render --format progress` already uses for them.
///
/// `rhei init` ends by pointing at `rhei new "<title>"`, which makes the very
/// next `rhei list` the moment a project holds one rhei and no tickets — and a
/// listing that showed nothing would read as though the create had not landed.
/// Text only, and only unfiltered: a filter asks a question about tickets, and
/// a rhei with none has no answer to give.
// §FS-rhei-list.4.1 §FS-rhei-new.5.4
fn report_empty_rheis(loaded: &LoadedPlan) {
    if !loaded.is_panta_project() {
        return;
    }
    for id in &loaded.rhei_ids {
        if rhei_holds_tickets(loaded, id) {
            continue;
        }
        println!();
        println!("{}: (no tickets yet)", empty_rhei_heading(loaded, id));
    }
}

/// True when any ticket in the merged graph belongs to rhei `id`.
fn rhei_holds_tickets(loaded: &LoadedPlan, id: &str) -> bool {
    let prefix = format!("{id}.");
    loaded.rhei.tasks.iter().any(|task| task.id.to_string().starts_with(&prefix))
}

/// What to print when nothing matched: the rheis `--rhei` named, when every one
/// of them is simply empty, and the generic filter line otherwise.
///
/// `--rhei` is a filter, so an empty rhei falls into "no tasks match the given
/// filters" — which reads as "your filters are wrong" about a rhei that has no
/// tickets for any filter to match. The distinction matters right after a
/// create, when the rhei someone just made is exactly the one they are listing.
// §FS-rhei-list.4.1 §FS-rhei-new.5.4
fn empty_listing_line(loaded: &LoadedPlan, filters: &ListFilters) -> String {
    let named: Vec<&str> = filters.rhei.iter().map(String::as_str).collect();
    if named.is_empty() || named.iter().any(|id| rhei_holds_tickets(loaded, id)) {
        return "(no tasks match the given filters)".to_string();
    }
    let quoted: Vec<String> = named.iter().map(|id| format!("'{id}'")).collect();
    match quoted.len() {
        1 => format!("(rhei {} holds no tickets yet)", quoted[0]),
        _ => format!("(rheis {} hold no tickets yet)", quoted.join(", ")),
    }
}

/// `Billing (billing)` — the rhei's title, carrying the id when the two
/// diverge, exactly as `rhei render --format progress` heads its blocks. The
/// merge emits one `Rhei <id>: <title>` section per rhei, which is where the
/// title comes from.
// §FS-rhei-render.3.4 §FS-rhei-panta.4
fn empty_rhei_heading(loaded: &LoadedPlan, id: &str) -> String {
    let marker = format!("Rhei {id}: ");
    let title = loaded
        .rhei
        .content_sections
        .iter()
        .filter(|section| section.rhei.as_deref() == Some(id))
        .find_map(|section| section.title.strip_prefix(&marker))
        .unwrap_or(id);
    if title.eq_ignore_ascii_case(id) {
        title.to_string()
    } else {
        format!("{title} ({id})")
    }
}

/// Execute the `list` subcommand: load a plan and print tasks matching the
/// provided filters. Modeled after `bd list` from beads, with a filter set
/// adapted to Rhei's data model (no priority/labels/timestamps).
fn list_command(
    input: &Path,
    state_machine_path: Option<&Path>,
    filters: ListFilters,
    as_json: bool,
) -> MietteResult<()> {
    // Listing is the surface an author reaches for *while* a plan is broken, so
    // it reports what it could not load and shows the rest. §FS-rhei-panta.6
    let loaded = load_plan_leniently(input)?;
    for skipped in &loaded.unloadable {
        eprintln!("warning: {skipped}");
    }
    let rhei_scope = resolve_rhei_scope(&loaded, &filters.rhei)?;
    let resolved = resolve_state_machines_for_loaded_plan(input, &loaded, state_machine_path)?;
    let machines = resolved.validator_set();

    // Flatten the task tree into (task, parent_id) pairs, preserving source order.
    let mut flat: Vec<(&rhei_core::ast::Task, Option<TaskId>)> = Vec::new();
    fn walk<'a>(
        task: &'a rhei_core::ast::Task,
        parent: Option<TaskId>,
        out: &mut Vec<(&'a rhei_core::ast::Task, Option<TaskId>)>,
    ) {
        out.push((task, parent));
        let parent_id = Some(task.id.clone());
        for child in &task.children {
            walk(child, parent_id.clone(), out);
        }
    }
    for task in &loaded.rhei.tasks {
        walk(task, None, &mut flat);
    }

    // §FS-rhei-panta.6: an empty project is a valid project, not an error —
    // say what it is and how to grow it.
    if flat.is_empty() {
        if as_json {
            println!("[]");
        } else if loaded.is_panta_project() {
            println!("(project has no tickets yet)");
            // A project that already holds a rhei needs its rheis named, not a
            // second invitation to add one — that is the state `rhei init`
            // plus one `rhei new` leaves behind. §FS-rhei-list.4.1
            if loaded.rhei_ids.is_empty() {
                println!("{}", add_a_rhei_help());
            } else if filters.none_active() {
                report_empty_rheis(&loaded);
            }
        } else {
            println!("(this rhei has no tickets yet)");
        }
        return Ok(());
    }

    // Pre-compute state map for ready/blocked checks (only top-level tasks
    // declare priors, but checking the full flat set is harmless).
    let state_map: HashMap<&TaskId, String> = flat
        .iter()
        .map(|(t, _)| (&t.id, normalized_state_name(t.state.as_str(), machines.for_task(&t.id))))
        .collect();

    let priors_satisfied = |task: &rhei_core::ast::Task| -> bool {
        task.prior.iter().all(|dep| {
            state_map
                .get(dep)
                .map(|s| dependency_is_satisfied(s, machines.for_task(dep)))
                .unwrap_or(false)
        })
    };

    // Built once for the whole listing rather than per row: the barrier walks
    // a task's ancestors and its subtree. §FS-rhei-supervision.3.2
    let all_tasks: Vec<&rhei_core::ast::Task> = flat.iter().map(|(task, _)| *task).collect();
    let supervision_index = task_index(&all_tasks);
    let no_run_spawned = HashSet::new();

    // Judged against every machine in the project rather than the `--rhei`
    // scope: a real state no in-scope rhei uses is an honest empty result.
    // §FS-rhei-list.2.1: a state no machine declares is an error, not silence.
    for requested in &filters.states {
        let requested = requested.trim();
        let known = machines
            .distinct()
            .into_iter()
            .any(|machine| machine.is_valid_state(normalized_state_name(requested, machine)));
        if !known {
            let mut available: BTreeSet<&str> = BTreeSet::new();
            for machine in machines.distinct() {
                available.extend(machine.allowed_states());
            }
            let known =
                available.iter().map(|state| state.to_string()).collect::<Vec<_>>();
            return Err(miette!(
                help = did_you_mean(requested, &known)
                    .unwrap_or_else(|| "this machine declares no states.".to_string()),
                "unknown state '{}'; states in this {}: {}",
                requested,
                if loaded.is_panta_project() { "project" } else { "plan" },
                available.into_iter().collect::<Vec<_>>().join(", ")
            ));
        }
    }

    // Normalize state filter values once per machine so users can pass either
    // canonical names or counted-visit forms; a filter value normalizes under
    // each distinct machine and matches per ticket. §DA-per-rhei-state-machines
    let state_filter: Vec<String> = filters
        .states
        .iter()
        .flat_map(|s| {
            machines
                .distinct()
                .into_iter()
                .map(|machine| normalized_state_name(s.as_str(), machine))
                .collect::<Vec<_>>()
        })
        .collect();
    // §FS-rhei-panta.6: ticket targets accept the qualified id or an
    // unambiguous rhei-local shorthand — including these filter values.
    let parent_filter = filters
        .parent
        .as_deref()
        .map(|id| resolve_cli_task_id(&loaded, id, &rhei_scope))
        .transpose()?
        .map(|id| parse_task_id(&id));
    let has_prior_filter = filters
        .has_prior
        .as_deref()
        .map(|id| resolve_cli_task_id(&loaded, id, &rhei_scope))
        .transpose()?
        .map(|id| parse_task_id(&id));
    let contains_lower = filters.contains.as_deref().map(|s| s.to_lowercase());

    let mut matches: Vec<&(&rhei_core::ast::Task, Option<TaskId>)> = Vec::new();
    for entry in &flat {
        let (task, parent_id) = entry;

        // §FS-rhei-panta.6.4: `--rhei` filters the listing to named rheis.
        if !task_in_rhei_scope(&rhei_scope, &task.id.to_string()) {
            continue;
        }

        if !state_filter.is_empty() {
            let task_state =
                normalized_state_name(task.state.as_str(), machines.for_task(&task.id));
            if !state_filter.iter().any(|s| s == &task_state) {
                continue;
            }
        }

        if let Some(want) = filters.assignee.as_deref() {
            if task.assignee.as_deref() != Some(want) {
                continue;
            }
        }
        if filters.no_assignee && task.assignee.is_some() {
            continue;
        }

        if let Some(want) = filters.kind.as_deref() {
            if !task.kind.eq_ignore_ascii_case(want) {
                continue;
            }
        }

        if let Some(prior_id) = &has_prior_filter {
            if !task.prior.iter().any(|p| p == prior_id) {
                continue;
            }
        }

        if let Some(parent_id_filter) = &parent_filter {
            if parent_id.as_ref() != Some(parent_id_filter) {
                continue;
            }
        }
        if filters.root && parent_id.is_some() {
            continue;
        }

        if let Some(needle) = &contains_lower {
            let title_hit = task.title.to_lowercase().contains(needle);
            let body_hit = task.content.to_lowercase().contains(needle);
            if !title_hit && !body_hit {
                continue;
            }
        }

        let machine = machines.for_task(&task.id);
        let is_terminal = is_terminal_state(task.state.as_str(), machine);
        if filters.terminal && !is_terminal {
            continue;
        }
        if filters.non_terminal && is_terminal {
            continue;
        }

        if filters.ready || filters.blocked {
            let normalized = normalized_state_name(task.state.as_str(), machine);
            let is_gating = machine.states.get(&normalized).map(|def| def.gating).unwrap_or(false);
            let satisfied = priors_satisfied(task);
            // A ticket whose subtree is still open is not work anyone can be
            // handed — its children are. §FS-rhei-list.3.1 §FS-rhei-next.3

            // Supervision refines both halves through the one verdict the
            // ready set and `rhei next` also ask, so the three surfaces cannot
            // disagree about what is work. §FS-rhei-supervision.3.2
            let subtree_done = subtree_admits_to_ready_set(
                task,
                &supervision_index,
                &machines,
                loaded.rhei.metadata.as_ref(),
                &no_run_spawned,
            );
            let task_ready = !is_terminal && !is_gating && satisfied && subtree_done;
            if filters.ready && !task_ready {
                continue;
            }
            if filters.blocked && (is_terminal || satisfied) {
                continue;
            }
        }

        matches.push(entry);
    }

    if filters.limit > 0 && matches.len() > filters.limit {
        matches.truncate(filters.limit);
    }

    if as_json {
        let payload: Vec<serde_json::Value> = matches
            .iter()
            .map(|(task, parent_id)| {
                serde_json::json!({
                    "id": task.id.to_string(),
                    "kind": task.kind,
                    "title": task.title,
                    "state": task.state,
                    "assignee": task.assignee,
                    "prior": task.prior.iter().map(TaskId::to_string).collect::<Vec<_>>(),
                    "parent": parent_id.as_ref().map(TaskId::to_string),
                    // Depth within the owning rhei: the Panta qualification
                    // segment is routing, not plan structure. §FS-rhei-list.4.2
                    "depth": task.profile_level(),
                })
            })
            .collect();
        let rendered = serde_json::to_string_pretty(&payload)
            .map_err(|err| miette!(
                help = internal_error_help(),
                "failed to serialize task list: {err}"
            ))?;
        println!("{rendered}");
        return Ok(());
    }

    if matches.is_empty() {
        println!("{}", empty_listing_line(&loaded, &filters));
        if filters.none_active() {
            report_empty_rheis(&loaded);
        }
        return Ok(());
    }

    for (task, _) in &matches {
        // Indent by depth within the owning rhei, so top-level tickets stay
        // flush-left after Panta qualification. §FS-rhei-list.4.1
        let indent = "  ".repeat(usize::from(task.profile_level()).saturating_sub(1));
        let mut line = format!(
            "{}{} {}: {} [{}]",
            indent,
            title_case_kind(&task.kind),
            task.id,
            task.title,
            task.state
        );
        if !task.prior.is_empty() {
            let priors: Vec<String> = task.prior.iter().map(TaskId::to_string).collect();
            line.push_str(&format!(" (prior: {})", priors.join(", ")));
        }
        if let Some(assignee) = &task.assignee {
            line.push_str(&format!(" @{}", assignee));
        }
        println!("{line}");
    }
    if filters.none_active() {
        report_empty_rheis(&loaded);
    }

    Ok(())
}