coven 0.1.0

A minimal streaming display and workflow runner for Claude Code's -p mode
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
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
use std::io::Write;
use std::process::Command as StdCommand;

use anyhow::Result;
use crossterm::event::{Event, KeyEvent};
use crossterm::terminal;

use crate::display::input::{InputAction, InputHandler};
use crate::display::renderer::Renderer;
use crate::event::{AppEvent, InputMode};
use crate::fork::{self, ForkConfig};
use crate::handle_inbound;
use crate::protocol::types::InboundEvent;
use crate::session::runner::{SessionConfig, SessionRunner};
use crate::session::state::{SessionState, SessionStatus};
use crate::vcr::{Io, IoEvent, VcrContext};

/// How a session ended.
pub enum SessionOutcome {
    /// Session produced a result (normal completion).
    Completed { result_text: String },
    /// User pressed Ctrl+C.
    Interrupted,
    /// Claude process exited unexpectedly.
    ProcessExited,
}

/// Per-session transient state for event buffering and follow-ups.
struct SessionLocals {
    event_buffer: Vec<AppEvent>,
    pending_followups: Vec<String>,
    result_text: String,
}

/// Run a single session's event loop with full input support.
///
/// Handles streaming display, event buffering during input, steering messages,
/// follow-up messages, message viewing, and interrupt/end-session signals.
///
/// Returns when the session produces a Result event, the user interrupts,
/// or the process exits.
pub async fn run_session<W: Write>(
    runner: &mut SessionRunner,
    state: &mut SessionState,
    renderer: &mut Renderer<W>,
    input: &mut InputHandler,
    io: &mut Io,
    vcr: &VcrContext,
    fork_config: Option<&ForkConfig>,
) -> Result<SessionOutcome> {
    let mut locals = SessionLocals {
        event_buffer: Vec::new(),
        pending_followups: Vec::new(),
        result_text: String::new(),
    };

    loop {
        let io_event: IoEvent = vcr
            .call("next_event", (), async |(): &()| io.next_event().await)
            .await?;
        match io_event {
            IoEvent::Claude(app_event) => {
                if input.is_active() && state.status == SessionStatus::Running {
                    locals.event_buffer.push(app_event);
                } else {
                    let outcome = process_claude_event(
                        app_event,
                        state,
                        renderer,
                        runner,
                        &mut locals,
                        vcr,
                        fork_config,
                    )
                    .await?;
                    if let Some(outcome) = outcome {
                        return Ok(outcome);
                    }
                }
            }
            IoEvent::Terminal(Event::Key(key_event)) => {
                let action = handle_session_key_event(
                    &key_event,
                    input,
                    renderer,
                    runner,
                    state,
                    &mut locals,
                    vcr,
                )
                .await?;
                match action {
                    LoopAction::Continue => {}
                    LoopAction::Return(outcome) => return Ok(outcome),
                }
            }
            IoEvent::Terminal(_) => {}
        }
    }
}

/// Flow control signals from key event handlers.
enum LoopAction {
    Continue,
    Return(SessionOutcome),
}

/// What happened during event buffer flush that requires caller action.
enum FlushResult {
    /// No special action needed.
    Continue,
    /// A pending followup was dequeued after a buffered Result.
    /// Caller should send it and set state to Running.
    Followup(String),
    /// A Result was flushed with no pending followups — session completed.
    Completed(String),
    /// The process exited during the flush.
    ProcessExited,
}

/// Handle a key event during an active session.
async fn handle_session_key_event<W: Write>(
    key_event: &KeyEvent,
    input: &mut InputHandler,
    renderer: &mut Renderer<W>,
    runner: &mut SessionRunner,
    state: &mut SessionState,
    locals: &mut SessionLocals,
    vcr: &VcrContext,
) -> Result<LoopAction> {
    let action = input.handle_key(key_event);
    match action {
        InputAction::Activated(c) => {
            renderer.begin_input_line();
            renderer.write_raw(&c.to_string());
        }
        InputAction::Submit(text, mode) => {
            let flush = flush_event_buffer(locals, state, renderer);
            // Completed is intentionally not special-cased here: if the session
            // completed during the flush, state is WaitingForInput and the match
            // below will send the user's text as a follow-up.
            if let Some(action) = handle_flush_result(flush, state, renderer, runner, vcr).await? {
                return Ok(action);
            }
            match mode {
                InputMode::Steering => {
                    renderer.render_steering_sent(&text);
                    vcr.call("send_message", text, async |t: &String| {
                        runner.send_message(t).await
                    })
                    .await?;
                }
                InputMode::FollowUp => {
                    if state.status == SessionStatus::WaitingForInput {
                        renderer.render_user_message(&text);
                        state.suppress_next_separator = true;
                        vcr.call("send_message", text, async |t: &String| {
                            runner.send_message(t).await
                        })
                        .await?;
                        state.status = SessionStatus::Running;
                    } else {
                        renderer.render_followup_queued(&text);
                        locals.pending_followups.push(text);
                    }
                }
            }
        }
        InputAction::ViewMessage(ref query) => {
            view_message(renderer, query);
            let flush = flush_event_buffer(locals, state, renderer);
            if let FlushResult::Completed(ref result_text) = flush {
                return Ok(LoopAction::Return(SessionOutcome::Completed {
                    result_text: result_text.clone(),
                }));
            }
            if let Some(action) = handle_flush_result(flush, state, renderer, runner, vcr).await? {
                return Ok(action);
            }
            // Don't re-activate input — it was deactivated when the user submitted
            // the :N command. The user returns to inactive state and can type a
            // character to start new input naturally (via Activated(c)).
        }
        InputAction::Interrupt => {
            runner.kill().await?;
            return Ok(LoopAction::Return(SessionOutcome::Interrupted));
        }
        InputAction::EndSession => {
            runner.close_input();
        }
        InputAction::Cancel => {
            let flush = flush_event_buffer(locals, state, renderer);
            if let FlushResult::Completed(ref result_text) = flush {
                return Ok(LoopAction::Return(SessionOutcome::Completed {
                    result_text: result_text.clone(),
                }));
            }
            if let Some(action) = handle_flush_result(flush, state, renderer, runner, vcr).await? {
                return Ok(action);
            }
            if state.status == SessionStatus::WaitingForInput {
                renderer.show_prompt();
                input.activate();
            }
        }
        InputAction::None => {}
    }
    Ok(LoopAction::Continue)
}

/// Process a single claude event. Returns Some(outcome) if the session should end.
async fn process_claude_event<W: Write>(
    event: AppEvent,
    state: &mut SessionState,
    renderer: &mut Renderer<W>,
    runner: &mut SessionRunner,
    locals: &mut SessionLocals,
    vcr: &VcrContext,
    fork_config: Option<&ForkConfig>,
) -> Result<Option<SessionOutcome>> {
    match event {
        AppEvent::Claude(inbound) => {
            // Capture result text early (needed for fork detection)
            if let InboundEvent::Result(ref result) = *inbound {
                locals.result_text.clone_from(&result.result);
            }

            // Detect fork tag in result text (live mode only — fork children
            // spawn real sessions, which isn't compatible with VCR replay).
            let fork_tasks = if vcr.is_live() {
                if let InboundEvent::Result(_) = *inbound {
                    fork_config.and_then(|_| fork::parse_fork_tag(&locals.result_text))
                } else {
                    None
                }
            } else {
                None
            };

            // Suppress the Done line if fork detected or followups pending
            let has_pending = !locals.pending_followups.is_empty() || fork_tasks.is_some();
            handle_inbound(&inbound, state, renderer, has_pending);

            // Run fork flow: spawn children, collect results, send reintegration
            if let Some(tasks) = fork_tasks {
                let session_id = state.session_id.clone().unwrap_or_default();
                // Safety: fork_tasks is only set when fork_config is Some
                let Some(fork_cfg) = fork_config else {
                    unreachable!("fork_tasks set without fork_config");
                };
                let msg = fork::run_fork(&session_id, tasks, fork_cfg, renderer).await?;
                runner.send_message(&msg).await?;
                state.suppress_next_separator = true;
                state.status = SessionStatus::Running;
                return Ok(None);
            }

            // If result and there's a pending follow-up, send it (FIFO)
            if matches!(*inbound, InboundEvent::Result(_)) {
                if locals.pending_followups.is_empty() {
                    return Ok(Some(SessionOutcome::Completed {
                        result_text: locals.result_text.clone(),
                    }));
                }
                let text = locals.pending_followups.remove(0);
                renderer.render_followup_sent(&text);
                state.suppress_next_separator = true;
                vcr.call("send_message", text, async |t: &String| {
                    runner.send_message(t).await
                })
                .await?;
                state.status = SessionStatus::Running;
            }
        }
        AppEvent::ParseWarning(warning) => {
            renderer.render_warning(&warning);
        }
        AppEvent::ProcessExit(code) => {
            renderer.render_exit(code);
            state.status = SessionStatus::Ended;
            return Ok(Some(SessionOutcome::ProcessExited));
        }
    }
    Ok(None)
}

/// Flush all buffered events through the renderer.
///
/// Returns a `FlushResult` indicating whether the caller needs to take action:
/// sending a dequeued followup, handling a completion, or handling a process exit.
/// If multiple significant events are buffered, the last one wins (e.g. a
/// `ProcessExit` after a `Result` overrides the `Completed`/`Followup` result).
fn flush_event_buffer<W: Write>(
    locals: &mut SessionLocals,
    state: &mut SessionState,
    renderer: &mut Renderer<W>,
) -> FlushResult {
    let mut result = FlushResult::Continue;
    for event in locals.event_buffer.drain(..) {
        match event {
            AppEvent::Claude(inbound) => {
                let has_pending = !locals.pending_followups.is_empty();
                // Capture result text from buffered events too
                if let InboundEvent::Result(ref r) = *inbound {
                    locals.result_text.clone_from(&r.result);
                }
                handle_inbound(&inbound, state, renderer, has_pending);
                if matches!(*inbound, InboundEvent::Result(_)) {
                    if has_pending {
                        let text = locals.pending_followups.remove(0);
                        result = FlushResult::Followup(text);
                    } else {
                        result = FlushResult::Completed(locals.result_text.clone());
                    }
                }
            }
            AppEvent::ParseWarning(warning) => {
                renderer.render_warning(&warning);
            }
            AppEvent::ProcessExit(code) => {
                renderer.render_exit(code);
                state.status = SessionStatus::Ended;
                result = FlushResult::ProcessExited;
            }
        }
    }
    result
}

/// Handle the result of flushing the event buffer: send a dequeued followup
/// or return early on process exit.
async fn handle_flush_result<W: Write>(
    flush: FlushResult,
    state: &mut SessionState,
    renderer: &mut Renderer<W>,
    runner: &mut SessionRunner,
    vcr: &VcrContext,
) -> Result<Option<LoopAction>> {
    match flush {
        FlushResult::ProcessExited => Ok(Some(LoopAction::Return(SessionOutcome::ProcessExited))),
        FlushResult::Followup(text) => {
            renderer.render_followup_sent(&text);
            state.suppress_next_separator = true;
            vcr.call("send_message", text, async |t: &String| {
                runner.send_message(t).await
            })
            .await?;
            state.status = SessionStatus::Running;
            Ok(None)
        }
        FlushResult::Completed(_) | FlushResult::Continue => Ok(None),
    }
}

/// What the user chose to do after a session completed.
pub enum FollowUpAction {
    /// User sent a follow-up message; continue the session.
    Sent,
    /// User wants to end the session (Ctrl+D, Ctrl+C, etc.).
    Exit,
}

/// Show a prompt and wait for user to type a follow-up or exit.
pub async fn wait_for_followup<W: Write>(
    input: &mut InputHandler,
    renderer: &mut Renderer<W>,
    runner: &mut SessionRunner,
    state: &mut SessionState,
    io: &mut Io,
    vcr: &VcrContext,
) -> Result<FollowUpAction> {
    match wait_for_text_input(input, renderer, io, vcr).await? {
        Some(text) => {
            state.suppress_next_separator = true;
            vcr.call("send_message", text, async |t: &String| {
                runner.send_message(t).await
            })
            .await?;
            state.status = SessionStatus::Running;
            Ok(FollowUpAction::Sent)
        }
        None => Ok(FollowUpAction::Exit),
    }
}

/// Show a prompt and wait for user input. Returns the text, or None to exit.
///
/// Unlike `wait_for_followup`, this doesn't send the message to a runner —
/// the caller decides what to do with the text (e.g. spawn a resumed session).
pub async fn wait_for_user_input<W: Write>(
    input: &mut InputHandler,
    renderer: &mut Renderer<W>,
    io: &mut Io,
    vcr: &VcrContext,
) -> Result<Option<String>> {
    wait_for_text_input(input, renderer, io, vcr).await
}

/// Wait for user to type and submit text, or exit.
///
/// Shows the prompt, activates input, and loops on events.
/// Returns the submitted text, or None if the user interrupted/ended.
async fn wait_for_text_input<W: Write>(
    input: &mut InputHandler,
    renderer: &mut Renderer<W>,
    io: &mut Io,
    vcr: &VcrContext,
) -> Result<Option<String>> {
    renderer.show_prompt();
    input.activate();

    loop {
        let io_event: IoEvent = vcr
            .call("next_event", (), async |(): &()| io.next_event().await)
            .await?;
        match io_event {
            IoEvent::Terminal(Event::Key(key_event)) => {
                let action = input.handle_key(&key_event);
                match action {
                    InputAction::Submit(text, _) => {
                        renderer.render_user_message(&text);
                        return Ok(Some(text));
                    }
                    InputAction::ViewMessage(ref query) => {
                        view_message(renderer, query);
                    }
                    InputAction::Cancel => {
                        renderer.show_prompt();
                        input.activate();
                    }
                    InputAction::Interrupt | InputAction::EndSession => {
                        return Ok(None);
                    }
                    InputAction::Activated(c) => {
                        renderer.begin_input_line();
                        renderer.write_raw(&c.to_string());
                    }
                    InputAction::None => {}
                }
            }
            IoEvent::Claude(AppEvent::ProcessExit(_)) => return Ok(None),
            IoEvent::Terminal(_) | IoEvent::Claude(_) => {}
        }
    }
}

/// Spawn a new Claude session via VCR.
pub async fn spawn_session(
    config: SessionConfig,
    io: &mut Io,
    vcr: &VcrContext,
) -> Result<SessionRunner> {
    vcr.call("spawn", config, async |c: &SessionConfig| {
        let tx = io.replace_event_channel();
        SessionRunner::spawn(c.clone(), tx).await
    })
    .await
}

/// Open a message in $PAGER, looked up by label query (e.g. "3" or "2/1").
pub fn view_message<W: Write>(renderer: &mut Renderer<W>, query: &str) {
    use crate::display::renderer::format_message;

    let Some(content) = format_message(renderer.messages(), query) else {
        renderer.write_raw(&format!("No message {query}\r\n"));
        return;
    };

    // Leave raw mode so the pager can handle keyboard input.
    // The pager manages its own alternate screen.
    terminal::disable_raw_mode().ok();

    let pager = std::env::var("PAGER").unwrap_or_else(|_| "less".to_string());
    let mut child = StdCommand::new(&pager)
        .arg("-R") // handle ANSI colors
        .stdin(std::process::Stdio::piped())
        .spawn();

    if let Ok(ref mut child) = child {
        if let Some(ref mut stdin) = child.stdin {
            stdin.write_all(content.as_bytes()).ok();
        }
        // Close stdin so pager reads EOF
        child.stdin.take();
        child.wait().ok();
    }

    // Discard any keystrokes buffered in the kernel's terminal input queue
    // while the pager was active — prevents stale keys from leaking into the prompt.
    // SAFETY: tcflush on STDIN_FILENO with TCIFLUSH is a POSIX syscall that
    // discards buffered input bytes — no memory or resource safety concerns.
    unsafe { libc::tcflush(libc::STDIN_FILENO, libc::TCIFLUSH) };
    terminal::enable_raw_mode().ok();
}