rhei-cli 0.2.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
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
//! Keyboard handling for the Flow surface: the two-level selection model, view
//! switching, filters, and the live intervene/gate composers.
//! §FS-rhei-run-tui.1.5.2 §FS-rhei-run-tui.1.5.5

use crossterm::event::{KeyCode, KeyModifiers};

use crate::rhei_tui::event::MessageLevel;

use super::derive::{inspector_sections, ChipAction};
use super::state::{Composer, ComposerKind, FlowFocus, UiState, View};

/// What the render loop should do after a key event.
pub(super) enum InputAction {
    Continue,
    ForwardSigint,
    Quit,
}

pub(super) fn handle_key_event(
    state: &mut UiState,
    code: KeyCode,
    modifiers: KeyModifiers,
) -> InputAction {
    // Ctrl+C restores the terminal and re-raises SIGINT (§1.8) — unless this
    // surface is only attached, in which case it disconnects and the run never
    // hears about it. §FS-rhei-run-headless.5.1
    if code == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL) {
        if state.attached {
            return InputAction::Quit;
        }
        state.push_journal(MessageLevel::Info, "(ctrl+c received — forwarding SIGINT)".to_string());
        return InputAction::ForwardSigint;
    }

    // Modal inputs intercept everything else.
    if state.composer.is_some() {
        return handle_composer(state, code);
    }
    if state.gate_active {
        return handle_gate(state, code);
    }
    if state.filter_editing {
        return handle_filter(state, code);
    }
    if state.help {
        if matches!(code, KeyCode::Char('?') | KeyCode::Esc) {
            state.help = false;
        }
        return InputAction::Continue;
    }

    match code {
        KeyCode::Char('?') => state.help = true,
        KeyCode::Char('q') => {
            // Quit only once the run has finished; during a live run the
            // operator stops with Ctrl+C (§1.5.2). An attached surface detaches
            // whenever it likes: it is not the thing the run is waiting on.

            // §FS-rhei-run-headless.5.1
            if state.finished || state.attached {
                return InputAction::Quit;
            }
        }
        KeyCode::Char('1') => switch_view(state, View::Flow),
        KeyCode::Char('2') => switch_view(state, View::Machine),
        KeyCode::Char('3') => switch_view(state, View::Cost),
        KeyCode::Char('4') => switch_view(state, View::Journal),
        KeyCode::Char('h') | KeyCode::Left => cycle_view(state, -1),
        KeyCode::Char('l') | KeyCode::Right => cycle_view(state, 1),
        KeyCode::Char('j') | KeyCode::Down => move_focus(state, 1),
        KeyCode::Char('k') | KeyCode::Up => move_focus(state, -1),
        KeyCode::PageDown => move_focus(state, 10),
        KeyCode::PageUp => move_focus(state, -10),
        KeyCode::Tab => {
            if state.view == View::Flow {
                state.flow_focus = match state.flow_focus {
                    FlowFocus::Outline => FlowFocus::Inspector,
                    FlowFocus::Inspector => {
                        state.inspector_item = None;
                        FlowFocus::Outline
                    }
                };
            }
        }
        KeyCode::Esc => {
            if state.view == View::Flow
                && state.flow_focus == FlowFocus::Inspector
                && state.inspector_item.is_some()
            {
                state.inspector_item = None;
            }
        }
        KeyCode::Enter => handle_enter(state),
        KeyCode::Char('/') => {
            state.filter_editing = true;
            state.filter = Some(state.filter.clone().unwrap_or_default());
        }
        KeyCode::Char('g') => {
            if state.view == View::Cost {
                state.cost_group = state.cost_group.next();
                state.cost_cursor = 0;
            }
        }
        KeyCode::Char('f') => {
            if state.view == View::Journal {
                state.journal_filter = state.journal_filter.next();
                state.journal_scroll = 0;
            }
        }
        KeyCode::Char('m') => open_composer(state),
        _ => {}
    }

    InputAction::Continue
}

fn switch_view(state: &mut UiState, view: View) {
    state.view = view;
    if view == View::Flow {
        state.flow_focus = FlowFocus::Outline;
    }
}

fn cycle_view(state: &mut UiState, delta: isize) {
    let idx = state.view.index() as isize;
    let len = View::ORDER.len() as isize;
    let next = ((idx + delta) % len + len) % len;
    switch_view(state, View::ORDER[next as usize]);
}

fn move_focus(state: &mut UiState, delta: isize) {
    match state.view {
        View::Flow => match state.flow_focus {
            FlowFocus::Outline => {
                let order = state.visible_task_indices();
                state.move_selected_in(&order, delta);
            }
            FlowFocus::Inspector => {
                let sections = state
                    .selected
                    .clone()
                    .map(|id| inspector_sections(state, &id))
                    .unwrap_or_default();
                if sections.is_empty() {
                    return;
                }
                state.inspector_section =
                    state.inspector_section.min(sections.len().saturating_sub(1));
                if let Some(item) = state.inspector_item {
                    let item_count = sections[state.inspector_section].items.len();
                    if item_count == 0 {
                        state.inspector_item = None;
                    } else {
                        let next = (item as isize + delta).clamp(0, item_count as isize - 1);
                        state.inspector_item = Some(next as usize);
                    }
                } else {
                    let cur = state.inspector_section as isize;
                    state.inspector_section =
                        (cur + delta).clamp(0, sections.len() as isize - 1) as usize;
                }
            }
        },
        View::Machine => {
            let order = state.machine_view_order();
            if !order.is_empty() {
                let current = order.iter().position(|idx| *idx == state.machine_focus).unwrap_or(0);
                let next = (current as isize + delta).clamp(0, order.len() as isize - 1);
                state.machine_focus = order[next as usize];
            }
        }
        View::Cost => {
            if matches!(state.cost_group, super::state::CostGroup::Task) {
                let order = state.visible_task_indices();
                state.move_selected_in(&order, delta);
            } else {
                state.cost_cursor = (state.cost_cursor as isize + delta).max(0) as usize;
            }
        }
        View::Journal => {
            state.journal_scroll = clamp_scroll(state.journal_scroll, delta);
        }
    }
}

fn clamp_scroll(current: u16, delta: isize) -> u16 {
    (current as isize + delta).max(0) as u16
}

fn handle_enter(state: &mut UiState) {
    match state.view {
        View::Flow if state.flow_focus == FlowFocus::Outline => {
            // Enter on a gating task opens the human-gate chooser (§1.5.5).
            open_gate(state);
        }
        View::Flow => {
            let Some(id) = state.selected.clone() else { return };
            let sections = inspector_sections(state, &id);
            if sections.is_empty() {
                return;
            }
            state.inspector_section = state.inspector_section.min(sections.len() - 1);
            let section = &sections[state.inspector_section];
            let Some(item) = state.inspector_item else {
                if !section.items.is_empty() {
                    state.inspector_item = Some(0);
                }
                return;
            };
            let Some(chip) = section.items.get(item) else {
                state.inspector_item = None;
                return;
            };
            match &chip.action {
                ChipAction::SelectTask(target) => {
                    state.select_task(target);
                }
                ChipAction::MarkState(target) => {
                    // Mark the target state in the Machine view, keeping the
                    // selected task in context.
                    if let Some(pos) =
                        state.plan.machine.states.iter().position(|s| &s.name == target)
                    {
                        state.machine_focus = pos;
                        switch_view(state, View::Machine);
                    }
                }
                ChipAction::None => {}
            }
        }
        _ => {}
    }
}

fn handle_filter(state: &mut UiState, code: KeyCode) -> InputAction {
    match code {
        KeyCode::Esc => {
            state.filter = None;
            state.filter_editing = false;
            state.reconcile_filter_focus();
        }
        KeyCode::Enter => {
            state.filter_editing = false;
            if state.filter.as_deref() == Some("") {
                state.filter = None;
            }
            state.reconcile_filter_focus();
        }
        KeyCode::Backspace => {
            if let Some(f) = state.filter.as_mut() {
                f.pop();
            }
        }
        KeyCode::Char(c) => {
            state.filter.get_or_insert_with(String::new).push(c);
        }
        _ => {}
    }
    InputAction::Continue
}

/// Open the intervene composer for the selected live task, when its agent is
/// reachable. Otherwise leave a journal note naming the remedy.
/// §FS-rhei-run-tui.1.5.5
fn open_composer(state: &mut UiState) {
    if state.finished {
        return;
    }
    let Some(id) = state.selected.clone() else { return };
    let Some((slot, _)) = state.running_slot(&id) else {
        return;
    };
    let reachable =
        state.intervene.as_ref().map(|sink| sink.reachable(&id, Some(slot))).unwrap_or(false);
    if !reachable {
        state.push_journal(
            MessageLevel::Warn,
            format!("{id}: agent is not reachable — set intervene_stdin and rerun"),
        );
        return;
    }
    state.composer = Some(Composer {
        task: id,
        slot: Some(slot),
        input: String::new(),
        kind: ComposerKind::Intervene,
    });
}

fn handle_composer(state: &mut UiState, code: KeyCode) -> InputAction {
    match code {
        KeyCode::Esc => {
            state.composer = None;
        }
        KeyCode::Backspace => {
            if let Some(c) = state.composer.as_mut() {
                c.input.pop();
            }
        }
        KeyCode::Char(ch) => {
            if let Some(c) = state.composer.as_mut() {
                c.input.push(ch);
            }
        }
        KeyCode::Enter => {
            let Some(composer) = state.composer.take() else {
                return InputAction::Continue;
            };
            let message = composer.input.trim().to_string();
            match &composer.kind {
                ComposerKind::Intervene => {
                    // Nothing to deliver: put the composer back rather than
                    // sending an empty line to the agent.
                    if message.is_empty() {
                        state.composer = Some(composer);
                        return InputAction::Continue;
                    }
                    let result = match &state.intervene {
                        Some(sink) => sink.deliver(Some(&composer.task), composer.slot, &message),
                        None => Err("intervene is not available".to_string()),
                    };
                    match result {
                        Ok(()) => state.push_journal(
                            MessageLevel::Info,
                            format!("⌨ intervene → {}: {message}", composer.task),
                        ),
                        Err(reason) => state.push_journal(
                            MessageLevel::Warn,
                            format!("intervene to {} failed: {reason}", composer.task),
                        ),
                    }
                }
                // An empty line submits with no message: the server decides
                // whether this edge needs one, and its refusal is echoed here.
                // §FS-rhei-run-tui.1.5.5 §FS-rhei-states.3.3
                ComposerKind::GateResult { from, to, .. } => {
                    submit_gate_transition(
                        state,
                        &composer.task,
                        from,
                        to,
                        (!message.is_empty()).then_some(message.as_str()),
                    );
                }
            }
        }
        _ => {}
    }
    InputAction::Continue
}

/// Send one gate decision and echo the outcome in the journal.
/// §FS-rhei-run-tui.1.5.5
fn submit_gate_transition(
    state: &mut UiState,
    task: &str,
    from: &str,
    to: &str,
    result: Option<&str>,
) {
    let outcome = match &state.gate {
        Some(sink) => sink.transition_gate(task, from, to, result),
        None => Err("gate transitions are not available".to_string()),
    };
    match outcome {
        Ok(effective) => {
            state.push_journal(MessageLevel::Info, format!("⮞ gate {task}: {from}{effective}"))
        }
        Err(reason) => state.push_journal(
            MessageLevel::Warn,
            format!("gate {task} {from}{to} rejected: {reason}"),
        ),
    }
}

/// Pick a human-gate transition: the digit keys select one of the gating
/// state's explicit outgoing transitions, then the composer collects the result
/// message that rides the move. §FS-rhei-run-tui.1.5.5
fn handle_gate(state: &mut UiState, code: KeyCode) -> InputAction {
    match code {
        KeyCode::Esc => {
            state.gate_active = false;
        }
        KeyCode::Char(ch) if ch.is_ascii_digit() => {
            let Some(choice) = ch.to_digit(10) else {
                return InputAction::Continue;
            };
            if choice == 0 {
                return InputAction::Continue;
            }
            let choices = gate_choices(state);
            if let Some((from, to)) = choices.get((choice - 1) as usize).cloned() {
                let Some(id) = state.selected.clone() else {
                    return InputAction::Continue;
                };
                // A human finishing a ticket by hand is the case where the
                // reason matters most, so the choice opens the composer instead
                // of firing straight away. §FS-rhei-states.3.3
                let terminal = state.machine_state(&to).map(|st| st.terminal).unwrap_or(false);
                state.composer = Some(Composer {
                    task: id,
                    slot: None,
                    input: String::new(),
                    kind: ComposerKind::GateResult { from, to, terminal },
                });
            }
            state.gate_active = false;
        }
        _ => {}
    }
    InputAction::Continue
}

/// The explicit outgoing `(from, to)` transitions for the selected task's
/// current gating state.
pub(super) fn gate_choices(state: &UiState) -> Vec<(String, String)> {
    let Some(task) = state.selected_task() else {
        return Vec::new();
    };
    let Some(st) = state.machine_state(&task.state) else {
        return Vec::new();
    };
    if !st.gating {
        return Vec::new();
    }
    st.transitions.iter().filter(|t| !t.wildcard).map(|t| (st.name.clone(), t.to.clone())).collect()
}

/// Whether the `m` intervene action currently applies (selected task is live).
pub(super) fn intervene_available(state: &UiState) -> bool {
    !state.finished && state.selected.as_ref().map(|id| state.is_live(id)).unwrap_or(false)
}

/// Open the gate chooser for the selected task when it sits in a live gating
/// state. Returns whether it opened (used by the action bar key).
pub(super) fn open_gate(state: &mut UiState) -> bool {
    if state.finished {
        return false;
    }
    if gate_choices(state).is_empty() || state.gate.is_none() {
        return false;
    }
    state.gate_active = true;
    true
}