[GH-ISSUE #5138] [Feature]: Consolidate generation SSE emission behind one canonical owner #2838

Closed
opened 2026-10-21 05:56:38 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Xelvanis on GitHub (Aug 16, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5138

Originally assigned to: @SpicyMarinara on GitHub.

What problem does this solve?

Generation-family SSE data frames are currently emitted from several places: the canonical sendSseEvent helper in packages/server/src/routes/generate/sse.ts, an exact alias (trySendSseEvent), and a handful of call sites that serialize data: ${JSON.stringify(...)} payloads directly onto reply.raw. The main /generate and /retry-agents handlers additionally monkey-patch reply.raw.write per request to duplicate the closed-socket guard.

This duplication means socket-lifecycle safety (destroyed/ended/finished checks plus write/serialization failure containment) has multiple apparent owners, native write identity is patched per-request, and every future event site has to remember the full framing/guard recipe or silently reintroduce unsafe writes.

Proposed solution

Give generation-family SSE data framing and closed-socket write safety one canonical owner:

  • 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 the now-obsolete route-local stream-closure catch layers.
  • Preserve exact payload bytes and property order, event order, keepalive bytes/cadence/cleanup, the distinct disconnect policies (main passive continuation vs impersonation/retry abort vs Professor Mari passive continuation), persistence, cancellation, and native backpressure booleans.

Alternatives considered

  • Keep the alias and per-site guards. That is the status quo: duplicated framing, two apparent safety owners, and patched native method identity.
  • Introduce a broader SSE transport/session abstraction. That would be a protocol redesign, not cleanup, and is out of scope for a behavior-preserving change.

Additional context

Follow-up to the behavior-preserving cleanup series in #5069 / #5070, applying the same one-commit-per-cleanup discipline. The change is purely internal: no event shapes, ordering, or disconnect behavior change. Reviewed with independent multi-lens review (correctness, ownership/structure, simplification, risk) plus cold triage; transport-level and live disconnect validation back the behavior-preservation claim.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Xelvanis on GitHub (Aug 16, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5138 Originally assigned to: @SpicyMarinara on GitHub. <!-- task-id: marinara-sse-emission-ownership-202608 --> ## What problem does this solve? Generation-family SSE data frames are currently emitted from several places: the canonical `sendSseEvent` helper in `packages/server/src/routes/generate/sse.ts`, an exact alias (`trySendSseEvent`), and a handful of call sites that serialize `data: ${JSON.stringify(...)}` payloads directly onto `reply.raw`. The main `/generate` and `/retry-agents` handlers additionally monkey-patch `reply.raw.write` per request to duplicate the closed-socket guard. This duplication means socket-lifecycle safety (destroyed/ended/finished checks plus write/serialization failure containment) has multiple apparent owners, native `write` identity is patched per-request, and every future event site has to remember the full framing/guard recipe or silently reintroduce unsafe writes. ## Proposed solution Give generation-family SSE data framing and closed-socket write safety one canonical owner: - 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 the now-obsolete route-local stream-closure catch layers. - Preserve exact payload bytes and property order, event order, keepalive bytes/cadence/cleanup, the distinct disconnect policies (main passive continuation vs impersonation/retry abort vs Professor Mari passive continuation), persistence, cancellation, and native backpressure booleans. ## Alternatives considered - Keep the alias and per-site guards. That is the status quo: duplicated framing, two apparent safety owners, and patched native method identity. - Introduce a broader SSE transport/session abstraction. That would be a protocol redesign, not cleanup, and is out of scope for a behavior-preserving change. ## Additional context Follow-up to the behavior-preserving cleanup series in #5069 / #5070, applying the same one-commit-per-cleanup discipline. The change is purely internal: no event shapes, ordering, or disconnect behavior change. Reviewed with independent multi-lens review (correctness, ownership/structure, simplification, risk) plus cold triage; transport-level and live disconnect validation back the behavior-preservation claim. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:56:38 +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#2838
No description provided.