[PR #5139] [MERGED] refactor: consolidate SSE emission ownership #6648

Closed
opened 2026-10-23 04:12:24 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5139
Author: @Xelvanis
Created: 8/16/2026
Status: ✅ Merged
Merged: 8/16/2026
Merged by: @SpicyMarinara

Base: staging ← Head: cleanup/sse-emission-ownership-current


📝 Commits (3)

  • 98b85b3 refactor: consolidate SSE emission ownership
  • 3df9f5f refactor: use shared SSE writability guard in Professor Mari route
  • 2ae6c51 Merge branch 'staging' into cleanup/sse-emission-ownership-current

📊 Changes

7 files changed (+180 additions, -278 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+161 -245)
📝 packages/server/src/routes/generate/fallback-notification.ts (+2 -2)
📝 packages/server/src/routes/generate/retry-agents-route.ts (+2 -13)
📝 packages/server/src/routes/generate/sse.ts (+0 -4)
📝 packages/server/src/routes/professor-mari-workspace.routes.ts (+4 -4)
📝 packages/server/src/services/generation/turn-game-command-runtime.ts (+2 -1)
📝 packages/server/src/services/turn-games/turn-game-bot-runner.service.ts (+9 -9)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #5138

Why this change

Generation-family SSE data frames were emitted from several places: the canonical sendSseEvent helper in packages/server/src/routes/generate/sse.ts, an exact alias (trySendSseEvent), and call sites that serialized data: ${JSON.stringify(...)} payloads directly onto reply.raw, while the main /generate and /retry-agents handlers monkey-patched reply.raw.write per request to duplicate the closed-socket guard. Socket-lifecycle safety therefore had multiple apparent owners, native write identity was patched per-request, and every future event site had to remember the full framing/guard recipe or silently reintroduce unsafe writes.

What changed

Behavior-preserving consolidation, seven files, net −98 lines:

  • Route every generation-family data event through sendSseEvent, including the newer delayed, typing, and inventory game_state_patch events.
  • Delete the exact trySendSseEvent alias and update its callers.
  • Remove the route-local reply.raw.write interception and two obsolete character-tracker stream-closure catch layers.
  • Unchanged: exact payload bytes and property order, event order, keepalive bytes/cadence/cleanup, distinct disconnect policies (main passive continuation vs impersonation/retry abort vs Professor Mari passive continuation), persistence, cancellation, and native backpressure booleans.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • pnpm check exited 0 on a clean worktree at the current head 3df9f5fb8 (impeccable context, localization, workspace lint, full client+server builds).
  • Focused retry regression scripts/regressions/manual-agent-retry-resolution.regression.ts passed (1/1).
  • Disposable transport probe verified exact framing bytes, native true/false backpressure passthrough, destroyed/ended/finished rejection, thrown-write and serialization-failure containment, SSE headers, keepalive cadence/cleanup, and a delayed write after a real client disconnect; removing the guard's try/catch reproduced an escaping write EPIPE (negative efficacy), then the source was restored byte-identically.
  • Live integration probe against the real server and a local stub provider: client aborted mid-stream, generation continued passively, the completed assistant message was persisted, and the server stayed healthy on post-disconnect writes.
  • Not yet manually verified in a browser: streamed generation and a mid-generation tab close across roleplay and retry flows. Manually verify streaming generation and mid-generation disconnect in the browser.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not applicable — no UI change; wire behavior is byte-identical.

Summary by CodeRabbit

Bug Fixes

  • Improved reliability of real-time generation, workspace, and turn-based game updates delivered through streaming connections.
  • Prevented unnecessary event writes and response completion attempts after a connection is closed or unavailable.
  • Improved handling of client disconnects and completed responses during retries and ongoing streams.
  • Preserved existing event payloads and generation behavior.

🔄 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/5139 **Author:** [@Xelvanis](https://github.com/Xelvanis) **Created:** 8/16/2026 **Status:** ✅ Merged **Merged:** 8/16/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `cleanup/sse-emission-ownership-current` --- ### 📝 Commits (3) - [`98b85b3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/98b85b3c6e23b871adac2718cc7a2cdac81af8d5) refactor: consolidate SSE emission ownership - [`3df9f5f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3df9f5fb8bcf5f26b4745922938c044cab888b95) refactor: use shared SSE writability guard in Professor Mari route - [`2ae6c51`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2ae6c51cb4c4cf020139984961c8d3877f547214) Merge branch 'staging' into cleanup/sse-emission-ownership-current ### 📊 Changes **7 files changed** (+180 additions, -278 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+161 -245) 📝 `packages/server/src/routes/generate/fallback-notification.ts` (+2 -2) 📝 `packages/server/src/routes/generate/retry-agents-route.ts` (+2 -13) 📝 `packages/server/src/routes/generate/sse.ts` (+0 -4) 📝 `packages/server/src/routes/professor-mari-workspace.routes.ts` (+4 -4) 📝 `packages/server/src/services/generation/turn-game-command-runtime.ts` (+2 -1) 📝 `packages/server/src/services/turn-games/turn-game-bot-runner.service.ts` (+9 -9) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #5138 ## Why this change Generation-family SSE data frames were emitted from several places: the canonical `sendSseEvent` helper in `packages/server/src/routes/generate/sse.ts`, an exact alias (`trySendSseEvent`), and call sites that serialized `data: ${JSON.stringify(...)}` payloads directly onto `reply.raw`, while the main `/generate` and `/retry-agents` handlers monkey-patched `reply.raw.write` per request to duplicate the closed-socket guard. Socket-lifecycle safety therefore had multiple apparent owners, native `write` identity was patched per-request, and every future event site had to remember the full framing/guard recipe or silently reintroduce unsafe writes. ## What changed Behavior-preserving consolidation, seven files, net −98 lines: - Route every generation-family data event through `sendSseEvent`, including the newer `delayed`, `typing`, and inventory `game_state_patch` events. - Delete the exact `trySendSseEvent` alias and update its callers. - Remove the route-local `reply.raw.write` interception and two obsolete character-tracker stream-closure catch layers. - Unchanged: exact payload bytes and property order, event order, keepalive bytes/cadence/cleanup, distinct disconnect policies (main passive continuation vs impersonation/retry abort vs Professor Mari passive continuation), persistence, cancellation, and native backpressure booleans. ## Validation - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `pnpm check` exited 0 on a clean worktree at the current head `3df9f5fb8` (impeccable context, localization, workspace lint, full client+server builds). - Focused retry regression `scripts/regressions/manual-agent-retry-resolution.regression.ts` passed (1/1). - Disposable transport probe verified exact framing bytes, native true/false backpressure passthrough, destroyed/ended/finished rejection, thrown-write and serialization-failure containment, SSE headers, keepalive cadence/cleanup, and a delayed write after a real client disconnect; removing the guard's `try/catch` reproduced an escaping `write EPIPE` (negative efficacy), then the source was restored byte-identically. - Live integration probe against the real server and a local stub provider: client aborted mid-stream, generation continued passively, the completed assistant message was persisted, and the server stayed healthy on post-disconnect writes. - Not yet manually verified in a browser: streamed generation and a mid-generation tab close across roleplay and retry flows. Manually verify streaming generation and mid-generation disconnect in the browser. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not applicable — no UI change; wire behavior is byte-identical. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Bug Fixes - Improved reliability of real-time generation, workspace, and turn-based game updates delivered through streaming connections. - Prevented unnecessary event writes and response completion attempts after a connection is closed or unavailable. - Improved handling of client disconnects and completed responses during retries and ongoing streams. - Preserved existing event payloads and generation behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:12:24 +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#6648
No description provided.