mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2742] [Issue]: SSE keepalive omitted across most streaming routes + missing X-Accel-Buffering + no client watchdog (proxy idle-timeout / stuck spinner) #1381
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1381
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2742
Originally assigned to: @SpicyMarinara on GitHub.
Summary
The only SSE keepalive in the codebase is a 15s
: keepalive\n\nsetIntervalscoped insidegenerateForCharacter(generate.routes.ts:5969, cleared at6856). Its own comment says it exists because "Cloudflare and other reverse proxies often kill idle connections after ~100s." Every other long byte-silent phase runs outside that window: pre-generation (smart-group selector, lorebook/memory recall), post-generation (prose-guardian rewrite + auto-summary), the turn-game bot loop, the/retry-agentsroute, and the Professor-Mari/promptagent loop./retry-agentsalso omits theX-Accel-Buffering: noheader that the three other SSE routes set. Separately, the clientstreamEvents()never settles streaming state on{type:'done'}and has no idle watchdog, so the post-done illustration tail keeps the UI in a streaming state. Behind a reverse proxy with a short idle timeout, these drop the connection or freeze the UI.Sub-bug 1: No keepalive during pre-generation (MEDIUM, hang)
selectSmartGroupRespondersruns a blocking non-streamingchatCompletebefore anygenerateForCharacter, so the selector model (possibly a slow reasoning model on the same connection) can run for tens of seconds with zero keepalive bytes. Applies to all pre-gen work (recall, sequential agents, prompt assembly) too.generate.routes.ts:5720-5734(blocking selector), awaited5772; keepalive only at5969insidegenerateForCharacter(first invoked6931).Sub-bug 2: No keepalive during post-generation rewrite/summary (MEDIUM, hang)
6856); the prose-guardian/text-rewriteexecuteAgentLLM call (8791) and auto-summarychatComplete(7137/awaited8874) run with the socket idle. WhenholdForProseGuardianRewriteis true,message_savedcarries thePROSE_GUARDIAN_PENDING_MESSAGEplaceholder and the real text only arrives via the latertext_rewriteevent — if the proxy kills the idle connection in between, the client stays on "Prose Guardian is working!".generate.routes.ts:5969/6856(keepalive lifetime),6741-6754(placeholder message_saved),8765-8868(rewrite emit),10656(done).Sub-bug 3: No keepalive on turn-game bot path (MEDIUM, hang/truncation)
turnGameBotsbranch early-returns at1398-1410, never enteringgenerateForCharacter.runTurnGameBotTurnsdoes two blockingchatCompletecalls per bot turn (move + narration) with no: keepalivebetweengroup_turnandmessage_saved. Disconnect is NOT propagated in conversation mode (see abort issue), so the loop also continues as a zombie.generate.routes.ts:1398-1410;turn-game-bot-runner.service.ts:170/197-203/244/259/274; MAX_BOT_TURNS=100.Sub-bug 4: No keepalive on /retry-agents + missing X-Accel-Buffering (MEDIUM, hang/truncation)
startSseReplythe route sends oneagent_start, then runs all agent batches + lorebook-keeper backfill (N sequential LLM calls) before the firstagent_result— no keepalive in between. It also callsstartSseReply(reply)with no extra header, so nginx buffers the whole stream (the earlyagent_startprogress signal is held, UI looks frozen, idle-timeout risk).retry-agents-route.ts:2880(startSseReply(reply)no header),3063(agent_start),3078-3103(batches),3174(first result); contrastgenerate.routes.ts:1331+5969.Sub-bug 5: No keepalive on Professor-Mari /prompt (MEDIUM, truncation)
/promptflushes headers and wires abort, but a multi-step tool loop / 10-min approval wait produces dead air with no: keepalive; the client stays in-progress untildone.professor-mari-workspace.routes.ts:93-129,await service.prompt115-120.Sub-bug 6: Client never settles on done; post-done tail hangs UI (MEDIUM, hang)
streamEvents()only ends on[DONE]/error, yielding{type:'done'}through; the consumer'sdonecase doesn'tsetStreaming(false). The server emitsdonebefore awaiting the slow illustration tail and onlyend()s after, soisStreamingstays true (stop button shown) for the whole image-gen duration.api-client.ts:275-298; consumeruse-generate.ts:2106-2114; servergenerate.routes.ts:10653-10666(done beforeawait pendingIllustration).Suggested Fix
setInterval(() => { if (!reply.raw.destroyed) reply.raw.write(': keepalive\n\n'); }, 15000)right afterstartSseReplyand clear it once in the outerfinally, covering pre-gen, gen, and post-gen uniformly.runTurnGameBotTurns, the/retry-agentshandler, and the Professor-Mari/promptagent loop (clear in finally).retry-agents-route.ts:2880tostartSseReply(reply, { 'X-Accel-Buffering': 'no' }).streamEvents(), treat{type:'done'}as terminal (return after yielding) likestream()does, or arm a bounded post-done watchdog; ensure the consumer settles streaming state on done.Root-cause throughline
The keepalive and proxy-buffering protections were added narrowly to the per-character token loop and were never generalized to the request lifecycle, so every other long byte-silent phase (selector, recall, rewrite, summary, bot turns, retries, workspace tool loops, and the post-done illustration tail) runs unprotected — and the client compounds it by never treating
doneas terminal.Verification checklist (leave all UNCHECKED)
pnpm checkAffected files
packages/server/src/routes/generate.routes.tspackages/server/src/routes/generate/retry-agents-route.tspackages/server/src/services/turn-games/turn-game-bot-runner.service.tspackages/server/src/routes/professor-mari-workspace.routes.tspackages/client/src/lib/api-client.tsAudit provenance
Found by a multi-agent static audit of the streaming/SSE subsystem (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged
stagingtree. Severity: MEDIUM.@SpicyMarinara commented on GitHub (Jun 21, 2026):
Addressed on PR #2747. Added a shared SSE keepalive helper and wired request-scoped heartbeats into /generate, /generate/retry-agents, and Professor Mari /prompt; /retry-agents now also sends X-Accel-Buffering: no. The old per-character generate keepalive was hoisted so pre-generation, turn-game bots, post-generation rewrites/summaries, and illustration tails are covered by one outer cleanup path. On the client, the generation consumer now settles visible streaming state on the done event while keeping streamEvents open for late illustration events; dry-run already had its own keepalive/header coverage. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.