brain: Propagate terminal input errors

7ce49fb131bef3cc5fd793a0450f3d0eada79383
Terminal input failures were rendered as transient status messages. If
the input channel then disconnected, the session returned success.

Classify input failures at the channel boundary so they escape the event
loop while user-action errors remain recoverable.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent a20f7fd2
crates/brain/src/session.rs +2 -2
197 197
        session.reconcile_state();
198 198
        session.activate()?;
199 199
    }
200 200
    session.redraw()?;
201 201
202 -
    while let Ok(event) = events_rx.recv() {
202 +
    while let Some(event) = TerminalEvent::receive(events_rx.recv())? {
203 203
        let mut redraw = false;
204 204
        let mut next = Some(event);
205 205
        // Drain queued events before repainting so bursts coalesce into one
206 206
        // frame.
207 207
        while let Some(event) = next {
212 212
                Err(error) => {
213 213
                    session.error = Some(error.to_string().replace('\n', " · "));
214 214
                    redraw = true;
215 215
                }
216 216
            }
217 -
            next = events_rx.try_recv().ok();
217 +
            next = TerminalEvent::receive(events_rx.try_recv())?;
218 218
        }
219 219
        if redraw {
220 220
            session.redraw()?;
221 221
        }
222 222
    }
crates/brain/src/session/event.rs +35 -0
58 58
    /// Background highlighter output, tagged with its generation.
59 59
    Highlight(u64, HighlightOutput),
60 60
}
61 61
62 62
impl TerminalEvent {
63 +
    /// Receives one channel event, propagating input failures.
64 +
    pub(super) fn receive<E>(event: Result<Self, E>) -> io::Result<Option<Self>> {
65 +
        match event {
66 +
            Ok(Self::Input(Err(error))) => Err(error),
67 +
            Ok(event) => Ok(Some(event)),
68 +
            Err(_) => Ok(None),
69 +
        }
70 +
    }
71 +
63 72
    /// Spawns the blocking input reader.
64 73
    pub(super) fn spawn_input_worker(tx: Sender<Self>) {
65 74
        thread::spawn(move || {
66 75
            for event in tui_termion::term::events() {
67 76
                if tx.send(Self::Input(event)).is_err() {
89 98
        }) if modifiers.contains(Modifiers::CONTROL) => Some(ContextChange::Decrease),
90 99
        Event::Key(Key::Char('x')) => Some(ContextChange::ToggleFile),
91 100
        _ => None,
92 101
    }
93 102
}
103 +
104 +
#[cfg(test)]
105 +
mod tests {
106 +
    use std::sync::mpsc;
107 +
108 +
    use super::*;
109 +
110 +
    #[test]
111 +
    fn terminal_input_error_precedes_channel_disconnect() {
112 +
        let (tx, rx) = mpsc::channel();
113 +
        tx.send(TerminalEvent::Input(Err(io::Error::other(
114 +
            "terminal input failed",
115 +
        ))))
116 +
        .unwrap();
117 +
        drop(tx);
118 +
119 +
        let result = (|| -> io::Result<()> {
120 +
            while TerminalEvent::receive(rx.recv())?.is_some() {}
121 +
            Ok(())
122 +
        })();
123 +
124 +
        let error = result.unwrap_err();
125 +
        assert_eq!(error.kind(), io::ErrorKind::Other);
126 +
        assert_eq!(error.to_string(), "terminal input failed");
127 +
    }
128 +
}