diff --git a/src/node/services/agentSession.ts b/src/node/services/agentSession.ts index adbe96ee30..df98d9b477 100644 --- a/src/node/services/agentSession.ts +++ b/src/node/services/agentSession.ts @@ -271,6 +271,15 @@ export class AgentSession { } if (options?.editMessageId) { + // Interrupt an existing stream or compaction, if active + if (this.aiService.isStreaming(this.workspaceId)) { + // MUST use abandonPartial=true to prevent handleAbort from performing partial compaction + // with mismatched history (since we're about to truncate it) + const stopResult = await this.interruptStream(/* abandonPartial */ true); + if (!stopResult.success) { + return Err(createUnknownSendMessageError(stopResult.error)); + } + } const truncateResult = await this.historyService.truncateAfterMessage( this.workspaceId, options.editMessageId @@ -362,6 +371,16 @@ export class AgentSession { return Ok(undefined); } + // Delete partial BEFORE stopping to prevent abort handler from committing it + // The abort handler in aiService.ts runs immediately when stopStream is called, + // so we must delete first to ensure it finds no partial to commit + if (abandonPartial) { + const deleteResult = await this.partialService.deletePartial(this.workspaceId); + if (!deleteResult.success) { + return Err(deleteResult.error); + } + } + const stopResult = await this.aiService.stopStream(this.workspaceId, abandonPartial); if (!stopResult.success) { return Err(stopResult.error); diff --git a/src/node/services/ipcMain.ts b/src/node/services/ipcMain.ts index 519a55ee58..a9c22f8338 100644 --- a/src/node/services/ipcMain.ts +++ b/src/node/services/ipcMain.ts @@ -1104,12 +1104,6 @@ export class IpcMain { return { success: false, error: stopResult.error }; } - // If abandonPartial is true, delete the partial instead of committing it - if (options?.abandonPartial) { - log.debug("Abandoning partial for workspace:", workspaceId); - await this.partialService.deletePartial(workspaceId); - } - return { success: true, data: undefined }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error);