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
//! Translating one `session.events` [`SessionEventEnvelope`] into
//! [`ReplEvent`]s (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).
//! `is_retryable_status` went with the HTTP transport in #6637 — a framed
//! JSON-RPC stream has no status code to classify, and every failure it can
//! report is worth the same bounded reconnect.
//! Test: `engine_tests::*` (in the sibling `engine_tests.rs`, included from
//! `engine.rs`), plus `session_events_forward_tests::*` (#4596 — the
//! tool-failure flag) in the sibling `session_events_forward_tests.rs`
//! included at the foot of this file. The similarly named
//! `session_events_tests.rs` is NOT this module's: it holds
//! `engine_state.rs`'s reconnect suite (#6637).
use ;
use UnboundedSender;
use ;
use crate;
/// 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`); `AgentMessageDelta` -> `AgentOutput`, keyed by
/// `(agent_id, turn_id)` (#7940); `ToolStarted` -> `ToolInvocation{result:
/// None, failed: false}`; `ToolFinished`/`ToolError` ->
/// `ToolInvocation{result: Some(..)}` carrying the backend's verdict in
/// `failed` (#4596 — `!success`, and always `true` for a `ToolError`; the
/// `FAILED: `/`ERROR: ` prefixes in the result text are for the reader, not
/// for the renderer), keyed by the SAME `call_id` so the tool-card renderer
/// can pair them; `PmDelegating`/`AgentSpawned` -> `DelegationStarted` and
/// `AgentDone`/`AgentFailed` -> `DelegationFinished` (#7940);
/// `PermissionRequested`/`PermissionResolved` -> the matching `ReplEvent`
/// halves of the TUI's permission prompt (#3422), field for field;
/// `SessionDone` -> a final `AssistantOutput{done: true}` (`is_error` iff
/// `status == "failed"`); `SessionCancelled` -> a status message. Every
/// other event kind (progress/telemetry this client doesn't render) is
/// silently ignored — forward-compatible with new `Event` variants (no
/// `match` arm needed per new kind, thanks to the catch-all). #7940 pins
/// what that catch-all may swallow: `engine_tests::
/// every_agent_attributed_event_maps_or_is_explicitly_ignored` fails if a
/// NEW agent-attributed variant lands in it unclassified.
/// 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`,
/// `engine_tests::forward_agent_spawned_opens_a_delegation`,
/// `engine_tests::forward_pm_delegating_opens_a_delegation_without_an_id`,
/// `engine_tests::forward_agent_done_closes_the_delegation`,
/// `engine_tests::forward_agent_failed_closes_with_the_error`,
/// `engine_tests::forward_permission_requested_opens_the_prompt`,
/// `engine_tests::forward_permission_resolved_closes_the_prompt`,
/// `engine_tests::every_agent_attributed_event_maps_or_is_explicitly_ignored`.
pub