Commit cc64299
authored
🤖 fix: clear todos on stream end with smart reconnection handling (#498)
## Problem
**TODOs were not being cleared when streams ended**, causing stale todos
to persist in the UI until the next user message. Additionally, on page
reload, TODOs were incorrectly reconstructed from history even for
completed streams.
The challenge: We need **different behavior for two reload scenarios**:
- **Reconnection during active stream**: Should reconstruct TODOs (work
in progress)
- **Reload after completed stream**: Should NOT reconstruct TODOs (clean
slate)
## Solution
### 1. Clear TODOs on stream end
Modified `cleanupStreamState()` to clear `currentTodos` when streams
complete (end/abort/error). TODOs are now truly stream-scoped.
### 2. Smart reconstruction on reload
**Key insight**: Check buffered events for `stream-start` to detect
active streams.
- `loadHistoricalMessages()` now accepts `hasActiveStream` parameter
- `WorkspaceStore` checks buffered events before loading history
- **Active stream** (hasActiveStream=true) → Reconstruct TODOs ✅
- **Completed stream** (hasActiveStream=false) → Don't reconstruct TODOs
✅
- **agentStatus** always reconstructed (persists across sessions) ✅
### 3. Improved separation of concerns
Centralized tool persistence logic in `processToolResult`:
- Added `context` parameter: `"streaming" | "historical"`
- `loadHistoricalMessages` no longer knows about specific tool behaviors
- Each tool declares its own persistence policy in one place
## Implementation
**WorkspaceStore checks for active streams:**
```typescript
const pendingEvents = this.pendingStreamEvents.get(workspaceId) ?? [];
const hasActiveStream = pendingEvents.some(
(event) => "type" in event && event.type === "stream-start"
);
aggregator.loadHistoricalMessages(historicalMsgs, hasActiveStream);
```
**StreamingMessageAggregator handles context:**
```typescript
loadHistoricalMessages(messages: CmuxMessage[], hasActiveStream = false) {
const context = hasActiveStream ? "streaming" : "historical";
// Process tool results with context
this.processToolResult(toolName, input, output, context);
}
```
**processToolResult decides based on tool + context:**
```typescript
private processToolResult(toolName, input, output, context: "streaming" | "historical") {
// TODOs: stream-scoped (only during streaming)
if (toolName === "todo_write" && context === "streaming") {
this.currentTodos = args.todos;
}
// agentStatus: persistent (always reconstruct)
if (toolName === "status_set") {
this.agentStatus = { emoji, message, url };
}
}
```
## Testing
Comprehensive test coverage for the full todo lifecycle:
✅ Clear todos on stream end
✅ Clear todos on stream abort
✅ Reconstruct todos when `hasActiveStream=true` (reconnection)
✅ Don't reconstruct todos when `hasActiveStream=false` (completed)
✅ Always reconstruct agentStatus (persists across sessions)
✅ Clear todos on new user message
All 146 message-related tests pass.
## Behavior Matrix
| Scenario | TODOs | agentStatus |
|----------|-------|-------------|
| During streaming | ✅ Visible | ✅ Visible |
| Stream ends | ❌ Cleared | ✅ Persists |
| Reload (active stream) | ✅ Reconstructed | ✅ Reconstructed |
| Reload (completed) | ❌ Not reconstructed | ✅ Reconstructed |
| New user message | ❌ Cleared | ❌ Cleared |
## Key Insight
The **reconnection scenario** is the critical edge case: when a user
reloads during an active stream, historical messages contain completed
tool calls from the *current* stream, and buffered events contain
`stream-start`. In this case, we *should* reconstruct TODOs to show work
in progress.
This is fundamentally different from reloading after stream completion,
where TODOs should remain cleared.
---
_Generated with `cmux`_1 parent 0f870a6 commit cc64299
File tree
4 files changed
+309
-34
lines changed- src
- components
- stores
- utils/messages
4 files changed
+309
-34
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 31 | + | |
| 32 | + | |
41 | 33 | | |
42 | 34 | | |
43 | 35 | | |
| |||
57 | 49 | | |
58 | 50 | | |
59 | 51 | | |
60 | | - | |
| 52 | + | |
61 | 53 | | |
62 | 54 | | |
63 | 55 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
910 | 910 | | |
911 | 911 | | |
912 | 912 | | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
913 | 919 | | |
914 | 920 | | |
915 | | - | |
| 921 | + | |
916 | 922 | | |
917 | 923 | | |
918 | 924 | | |
919 | 925 | | |
920 | 926 | | |
921 | 927 | | |
922 | 928 | | |
923 | | - | |
924 | 929 | | |
925 | 930 | | |
926 | 931 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 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 | + | |
140 | 382 | | |
0 commit comments