Skip to content

Commit 753b35f

Browse files
committed
🤖 fix: cancel active stream on message edit to prevent history corruption
When editing a compacting message to change parameters (e.g., token count) while the first compaction is still streaming, the old stream would continue and corrupt the chat history by replacing everything with [truncated]. Root cause: handleCompletion would find the NEW compaction request in history and proceed to perform compaction with the OLD stream's partial summary. Fix: Cancel any active stream before processing edits. This ensures only one stream runs at a time and aligns with user intent (edit = discard old). Impact: 9 lines added to agentSession.ts sendMessage method
1 parent d307930 commit 753b35f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

‎src/node/services/agentSession.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ export class AgentSession {
271271
}
272272

273273
if (options?.editMessageId) {
274+
// Interrupt an existing stream or compaction, if active
275+
if (this.aiService.isStreaming(this.workspaceId)) {
276+
// MUST use abandonPartial=true to prevent handleAbort from performing partial compaction
277+
// with mismatched history (since we're about to truncate it)
278+
const stopResult = await this.interruptStream(/* abandonPartial */ true);
279+
if (!stopResult.success) {
280+
return Err(createUnknownSendMessageError(stopResult.error));
281+
}
282+
}
274283
const truncateResult = await this.historyService.truncateAfterMessage(
275284
this.workspaceId,
276285
options.editMessageId

0 commit comments

Comments
 (0)