Commit fec2bfc
authored
🤖 fix: cancel active stream on message edit to prevent history corruption (#717)
## Problem
While trying to edit the token count of a running compaction request,
the entire chat history was replaced with `[truncated]`.
## Root Cause
When editing a compacting message while the first compaction stream is
still running:
1. The edit truncates history and starts a new compaction stream
2. The **old stream continues running** and isn't cancelled
3. When the old stream completes, `handleCompletion` reads the current
history
4. It finds the **NEW** compaction request (from the edit), not the old
one
5. Since the new request isn't in `processedCompactionRequestIds`, it
proceeds to perform compaction
6. This clears ALL history and replaces it with the old stream's partial
summary → entire chat becomes `[truncated]`
## Solution
Cancel any active stream before processing message edits in
`AgentSession.sendMessage`:
```typescript
// Cancel any active stream when editing a message to prevent race conditions
if (options?.editMessageId && this.aiService.isStreaming(this.workspaceId)) {
const stopResult = await this.interruptStream(/* abandonPartial */ true);
if (!stopResult.success) {
return Err(createUnknownSendMessageError(stopResult.error));
}
}
```
This ensures:
- Only one stream runs at a time
- Edits truly discard the old stream (aligns with user intent)
- No orphaned stream completions that corrupt history
## Changes
- **src/node/services/agentSession.ts**: Added stream cancellation check
before processing edits (9 lines)
## Testing
- ✅ Typecheck passes
- ✅ Graceful error handling (interruptStream is idempotent)
- ✅ Fixes the specific scenario: editing compaction token count
mid-stream
_Generated with `mux`_1 parent 6460891 commit fec2bfc
2 files changed
+19
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
274 | 283 | | |
275 | 284 | | |
276 | 285 | | |
| |||
362 | 371 | | |
363 | 372 | | |
364 | 373 | | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
365 | 384 | | |
366 | 385 | | |
367 | 386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1141 | 1141 | | |
1142 | 1142 | | |
1143 | 1143 | | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
1148 | | - | |
1149 | | - | |
1150 | 1144 | | |
1151 | 1145 | | |
1152 | 1146 | | |
| |||
0 commit comments