[PR #2427] [MERGED] Persist partial assistant message when generation is stopped mid-stream #5601

Closed
opened 2026-10-22 04:17:44 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2427
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/persist-partial-on-stop


📝 Commits (1)

  • 163b5e9 Persist partial assistant message when generation is stopped mid-stream

📊 Changes

1 file changed (+169 additions, -41 deletions)

View changed files

📝 src/engine/generation/start-generation.ts (+169 -41)

📄 Description

Linked issue

Closes #2320

Why this change

  • Pressing Stop mid-stream discarded the entire partial assistant message. The post-loop throwIfAborted gate propagated before saveAssistantMessage on both the main/agent and direct generation paths, so streamed text was thrown away instead of persisted.

What changed

  • src/engine/generation/start-generation.ts: streamMainGenerationLoop now exposes its accumulated turn through a StreamPartialSink filled in a finally — including the in-flight turn's tokens that hadn't yet been committed to content (the content += turnContent commit only runs once a turn's stream completes, so a mid-turn abort needs the separate inFlightTurn mirror).
  • Each caller wraps the stream in try/catch: on abort it persists the partial via the existing saveAssistantMessage (with agentResults: [], preserving regenerate/impersonate routing via existingExtra) and emits the assistant_message event for the saved row before rethrowing, so the client upserts it before clearing the streaming buffer.
  • Double-save is avoided: the catch runs only when the loop throws (abort); normal completion returns without throwing, leaving the existing single save path. The heavier post-save abort gates (agents/illustration/tracker) are unchanged and still skipped on abort.

Refactor impact

Primary owner: engine generation

Impact areas reviewed:

  • src/engine/generation/start-generation.ts (streamMainGenerationLoop, both stream callsites, new persistPartialOnAbort + StreamPartialSink)

Boundary notes:

  • None. Engine-layer only; no new imports/exports, client unchanged, mode-agnostic. content.trim() guard prevents an empty-content save; the partial-save is wrapped in try/catch so the original AbortError still propagates even if the save fails.

Pressure points touched:

  • Runtime generation.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm vitest run src/engine/generation/start-generation.test.ts — 16 existing pass. Local-only tests confirmed: an abort mid-stream on both the main and direct paths persists exactly one assistant row carrying the partial token and emits its assistant_message event; an abort before any token saves nothing; and normal completion still saves exactly once (no double-save).
  • pnpm exec tsc -b and pnpm exec eslint …start-generation.ts — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a data-loss bugfix.

Reason:

  • No new discoverable surface.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2427 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/persist-partial-on-stop` --- ### 📝 Commits (1) - [`163b5e9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/163b5e919f7753a8781dc608e8429eacc3c28043) Persist partial assistant message when generation is stopped mid-stream ### 📊 Changes **1 file changed** (+169 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/generation/start-generation.ts` (+169 -41) </details> ### 📄 Description ## Linked issue Closes #2320 ## Why this change - Pressing Stop mid-stream discarded the entire partial assistant message. The post-loop `throwIfAborted` gate propagated before `saveAssistantMessage` on both the main/agent and direct generation paths, so streamed text was thrown away instead of persisted. ## What changed - `src/engine/generation/start-generation.ts`: `streamMainGenerationLoop` now exposes its accumulated turn through a `StreamPartialSink` filled in a `finally` — including the in-flight turn's tokens that hadn't yet been committed to `content` (the `content += turnContent` commit only runs once a turn's stream completes, so a mid-turn abort needs the separate `inFlightTurn` mirror). - Each caller wraps the stream in try/catch: on abort it persists the partial via the existing `saveAssistantMessage` (with `agentResults: []`, preserving regenerate/impersonate routing via `existingExtra`) and emits the `assistant_message` event for the saved row before rethrowing, so the client upserts it before clearing the streaming buffer. - Double-save is avoided: the catch runs only when the loop throws (abort); normal completion returns without throwing, leaving the existing single save path. The heavier post-save abort gates (agents/illustration/tracker) are unchanged and still skipped on abort. ## Refactor impact Primary owner: engine generation Impact areas reviewed: - `src/engine/generation/start-generation.ts` (`streamMainGenerationLoop`, both stream callsites, new `persistPartialOnAbort` + `StreamPartialSink`) Boundary notes: - None. Engine-layer only; no new imports/exports, client unchanged, mode-agnostic. `content.trim()` guard prevents an empty-content save; the partial-save is wrapped in try/catch so the original AbortError still propagates even if the save fails. Pressure points touched: - Runtime generation. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm vitest run src/engine/generation/start-generation.test.ts` — 16 existing pass. Local-only tests confirmed: an abort mid-stream on both the main and direct paths persists exactly one assistant row carrying the partial token and emits its `assistant_message` event; an abort before any token saves nothing; and normal completion still saves exactly once (no double-save). - `pnpm exec tsc -b` and `pnpm exec eslint …start-generation.ts` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a data-loss bugfix. Reason: - No new discoverable surface. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:44 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5601
No description provided.