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
//! Translating one `GET /sessions/{id}/events` [`SessionEventEnvelope`] into
//! [`ReplEvent`]s, and classifying which HTTP statuses are worth an SSE
//! reconnect (issue #3415).
//!
//! Why: split out of `engine.rs` (issue #610's 500-SLOC production-file cap)
//! — this is the one piece of `EngineState::pump_session_events`
//! (`engine_state.rs`) that is pure and directly unit-testable without an
//! HTTP round trip, so it earns its own file rather than staying inline.
//! What: [`forward_session_event`] (the `Event` -> `ReplEvent` mapping) and
//! [`is_retryable_status`] (502/503 -> reconnect, everything else -> fail).
//! Test: `engine_tests::*` (in the sibling `engine_tests.rs`, included from
//! `engine.rs`).
use ;
use UnboundedSender;
use ReplEvent;
use crate;
/// `true` for HTTP statuses worth retrying (daemon restarting) rather than
/// failing immediately.
pub
/// Build the terminal, VISIBLE-in-the-TUI event `pump_session_events` sends
/// when it gives up reconnecting (`SESSION_STREAM_MAX_RECONNECTS` exhausted)
/// without ever observing a `SessionDone`/`SessionCancelled` terminal event
/// (epic #3411's deferred Slice 3 review item, closed out by Slice 6).
///
/// Why: every `ReplEvent::ConnectionLost` sent DURING a retry already renders
/// visibly (`ReplApp::apply` pushes it to the status area), but
/// `ConnectionLost` alone never clears `ReplApp::busy`/`streaming_idx` — only
/// `AssistantOutput{done: true}` does. Without an event like this one, the
/// LAST reconnect attempt failing left the input composer stuck on
/// "streaming" forever with no error rendered in the chat itself: a silent
/// stall, exactly the failure mode the review flagged as one unit tests on
/// either side of the engine/TUI boundary would both pass while the
/// integrated behavior is wrong.
/// What: an `AssistantOutput{done: true, is_error: true}` chunk carrying
/// `reason` — reusing the SAME "terminal event" vocabulary
/// `forward_session_event` uses for a failed `SessionDone`, so `ReplApp`
/// needs no new rendering path to display it.
/// Test: `engine_tests::terminal_stream_failure_event_is_a_done_error_output`.
pub
/// Translate one [`SessionEventEnvelope`] into zero or more [`ReplEvent`]s
/// on `tx`. Returns `true` iff this event is terminal for the current chat
/// turn (`SessionDone`/`SessionCancelled`) — the caller stops pumping.
///
/// Why: kept as a free function (not a method) so it's directly unit
/// testable against a hand-built envelope, no HTTP/mock server needed.
/// What: `Message`/`AgentMessage`/`PmThinking` -> `AssistantOutput` chunks
/// (`done: false`); `ToolStarted` -> `ToolInvocation{result: None}`;
/// `ToolFinished`/`ToolError` -> `ToolInvocation{result: Some(..)}`, keyed
/// by the SAME `call_id` so the (future) tool-card renderer can pair them;
/// `SessionDone` -> a final `AssistantOutput{done: true}` (`is_error` iff
/// `status == "failed"`); `SessionCancelled` -> a status message. Every
/// other event kind (progress/telemetry/agent-lifecycle events this MVP
/// doesn't yet render) is silently ignored — forward-compatible with new
/// `Event` variants (no `match` arm needed per new kind, thanks to the
/// catch-all).
/// Test: `engine_tests::forward_message_emits_assistant_output_chunk_not_done`,
/// `engine_tests::forward_tool_started_emits_tool_invocation_with_call_id`,
/// `engine_tests::forward_tool_finished_carries_result_and_shares_call_id`,
/// `engine_tests::forward_session_done_is_terminal`,
/// `engine_tests::forward_session_done_failed_marks_is_error`,
/// `engine_tests::forward_session_cancelled_is_terminal`,
/// `engine_tests::forward_unrelated_event_is_ignored`,
/// `engine_tests::forward_agent_message_delta_not_done_appends`,
/// `engine_tests::forward_agent_message_delta_done_finalizes`,
/// `engine_tests::forward_agent_message_delta_distinct_agent_ids_not_merged`.
pub