[GH-ISSUE #2741] [Issue]: Stop/abort cannot cancel background, autonomous & retry-agent generations; regenerate-during-stream silently rejected #1382

Closed
opened 2026-10-21 05:32:42 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2741

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The client's cancellation surface only reaches the currently-active, foreground chat. stopGeneration keys solely on the global single-valued streamingChatId, so a generation running for a background chat (started, then navigated away) or an autonomous/scheduled send cannot be cancelled — the upstream LLM, agents, and persistence run to completion as a zombie (conversation mode does not abort on passive disconnect). The /retry-agents flow creates an AbortController but never calls .abort() and never stores it where the stop button can reach it. chatStore.reset() replaces the controller map without aborting in-flight controllers. The same-chat regenerate/swipe path has dead prev.abort() code that never runs because the concurrency guard already returned. Separately, the autonomous scheduler dispatches void evaluateChat(chat) with no concurrency limit.

Sub-bug 1: stopGeneration can't cancel background/autonomous gens (HIGH, abort-failure)

  • How it breaks: stopGeneration reads streamingChatId (set only when isActiveChat() is true) and aborts abortControllers.get(streamingChatId) + POSTs {chatId: streamingChatId}. A background chat's controller is in the map but streamingChatId is the active/other chat, so it's never aborted; the autonomous variant registers no controller and passes no signal at all, so it's wholly unstoppable. Conversation-mode passive disconnect intentionally does not abort, so the work continues.
  • Repro: Start a generation in chat A, navigate to chat B, press Stop — chat A keeps generating. Or trigger an autonomous send — it cannot be stopped.
  • Trace: chat.store.ts:346-359; use-generate.ts:939 (registers controller) vs 956-957 (setStreaming gated by isActiveChat); autonomous use-background-autonomous.ts:168 (no signal). Server generate-route-utils.ts:195-197 (conversation passive disconnect = no abort).

Sub-bug 2: /retry-agents has no user cancel (LOW, other)

  • How it breaks: retryAgents creates new AbortController() and passes its signal to streamEvents, but .abort() is never called and the controller is not stored in a ref/store; the catch handles AbortError but nothing produces it. Only tab close (server reply.raw 'close') halts it.
  • Trace: use-generate.ts:2444/2469-2483/2724-2745; server abort only on socket close retry-agents-route.ts:2896-2900.

Sub-bug 3: reset() drops controllers without aborting (LOW, leak)

  • How it breaks: reset() reassigns abortControllers: new Map() without iterating to .abort(). The live generation keeps its own controller, the fetch isn't cancelled, and its finally (stillOwner now false) writes into a reset/cleared cache. Only trigger is the destructive expunge/clear-all path.
  • Trace: chat.store.ts:639-669 (reset), 346-359 (stopGeneration only active chat); finally use-generate.ts:2294.

Sub-bug 4: dead same-chat 'abort previous' code (LOW, other)

  • How it breaks: generate() returns false when abortControllers.has(chatId), so const prev = abortControllers.get(chatId) is always undefined and if (prev) prev.abort() is unreachable. Same-chat regenerate/swipe during a stream is silently dropped (console.warn only), despite the comment claiming it aborts-and-replaces. Intentional concurrency protection, but misleading dead code.
  • Trace: use-generate.ts:926-934.

Sub-bug 5: autonomous scheduler unbounded fan-out (LOW, leak)

  • How it breaks: poll() does void evaluateChat(chat) for every eligible chat with no semaphore; each evaluateChat may app.inject a full /api/generate (buffered into memory). N eligible chats => N concurrent real generations, risking provider rate limits/429s/memory. Heavily gated by per-chat cooldowns, so blast radius is workload-dependent.
  • Trace: server-autonomous-scheduler.service.ts:275-291 (poll, 283 void dispatch), 123-137 (app.inject /api/generate).

Suggested Fix

  • Make stopGeneration cancel the chat the user intends: pass an explicit chatId from the stop button (or abort abortControllers.get(activeChatId) as well) and POST that chatId to /abort; for a global stop, iterate abortControllers and abort each.
  • Give /retry-agents a stored controller (ref or store keyed by chatId) wired to the stop control; the server already aborts on socket close, so .abort() will propagate.
  • In reset(), for (const c of state.abortControllers.values()) c.abort(); before replacing the map.
  • Remove the dead 933-934 lines and document same-chat rejection, OR relax the guard to abort+supersede the prior controller.
  • Bound autonomous fan-out with a small concurrency limiter (1-2) or process at most K chats per poll tick.

Root-cause throughline

Cancellation was modeled around a single active foreground stream (streamingChatId), but the app actually runs generations for background chats, autonomous sends, and retry-agent batches concurrently — none of which are reachable from that single-valued state — so the stop button and store reset silently miss them, leaving zombie upstream work that conversation-mode passive-disconnect was deliberately designed not to cancel.

Verification checklist (leave all UNCHECKED)

  • Manually verify: start gen in chat A, switch to chat B, press Stop — chat A actually stops
  • Manually verify: an autonomous/scheduled send can be cancelled
  • Manually verify: a tracker/agent retry can be cancelled by the user
  • Manually verify: clear-all/expunge aborts in-flight generations
  • Manually verify: same-chat regenerate during a stream behaves per the documented decision (reject or supersede)
  • Run pnpm check

Affected files

  • packages/client/src/stores/chat.store.ts
  • packages/client/src/hooks/use-generate.ts
  • packages/client/src/hooks/use-background-autonomous.ts
  • packages/server/src/routes/generate/retry-agents-route.ts
  • packages/server/src/services/conversation/server-autonomous-scheduler.service.ts

Audit 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 staging tree. Severity: HIGH.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2741 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The client's cancellation surface only reaches the currently-active, foreground chat. `stopGeneration` keys solely on the global single-valued `streamingChatId`, so a generation running for a background chat (started, then navigated away) or an autonomous/scheduled send cannot be cancelled — the upstream LLM, agents, and persistence run to completion as a zombie (conversation mode does not abort on passive disconnect). The `/retry-agents` flow creates an `AbortController` but never calls `.abort()` and never stores it where the stop button can reach it. `chatStore.reset()` replaces the controller map without aborting in-flight controllers. The same-chat regenerate/swipe path has dead `prev.abort()` code that never runs because the concurrency guard already returned. Separately, the autonomous scheduler dispatches `void evaluateChat(chat)` with no concurrency limit. ### Sub-bug 1: stopGeneration can't cancel background/autonomous gens (HIGH, abort-failure) - **How it breaks:** `stopGeneration` reads `streamingChatId` (set only when `isActiveChat()` is true) and aborts `abortControllers.get(streamingChatId)` + POSTs `{chatId: streamingChatId}`. A background chat's controller is in the map but `streamingChatId` is the active/other chat, so it's never aborted; the autonomous variant registers no controller and passes no signal at all, so it's wholly unstoppable. Conversation-mode passive disconnect intentionally does not abort, so the work continues. - **Repro:** Start a generation in chat A, navigate to chat B, press Stop — chat A keeps generating. Or trigger an autonomous send — it cannot be stopped. - **Trace:** `chat.store.ts:346-359`; `use-generate.ts:939` (registers controller) vs `956-957` (setStreaming gated by isActiveChat); autonomous `use-background-autonomous.ts:168` (no signal). Server `generate-route-utils.ts:195-197` (conversation passive disconnect = no abort). ### Sub-bug 2: /retry-agents has no user cancel (LOW, other) - **How it breaks:** `retryAgents` creates `new AbortController()` and passes its signal to `streamEvents`, but `.abort()` is never called and the controller is not stored in a ref/store; the catch handles AbortError but nothing produces it. Only tab close (server `reply.raw 'close'`) halts it. - **Trace:** `use-generate.ts:2444/2469-2483/2724-2745`; server abort only on socket close `retry-agents-route.ts:2896-2900`. ### Sub-bug 3: reset() drops controllers without aborting (LOW, leak) - **How it breaks:** `reset()` reassigns `abortControllers: new Map()` without iterating to `.abort()`. The live generation keeps its own controller, the fetch isn't cancelled, and its `finally` (`stillOwner` now false) writes into a reset/cleared cache. Only trigger is the destructive expunge/clear-all path. - **Trace:** `chat.store.ts:639-669` (reset), `346-359` (stopGeneration only active chat); finally `use-generate.ts:2294`. ### Sub-bug 4: dead same-chat 'abort previous' code (LOW, other) - **How it breaks:** `generate()` returns false when `abortControllers.has(chatId)`, so `const prev = abortControllers.get(chatId)` is always undefined and `if (prev) prev.abort()` is unreachable. Same-chat regenerate/swipe during a stream is silently dropped (console.warn only), despite the comment claiming it aborts-and-replaces. Intentional concurrency protection, but misleading dead code. - **Trace:** `use-generate.ts:926-934`. ### Sub-bug 5: autonomous scheduler unbounded fan-out (LOW, leak) - **How it breaks:** `poll()` does `void evaluateChat(chat)` for every eligible chat with no semaphore; each `evaluateChat` may `app.inject` a full `/api/generate` (buffered into memory). N eligible chats => N concurrent real generations, risking provider rate limits/429s/memory. Heavily gated by per-chat cooldowns, so blast radius is workload-dependent. - **Trace:** `server-autonomous-scheduler.service.ts:275-291` (poll, `283` void dispatch), `123-137` (app.inject /api/generate). ## Suggested Fix - Make `stopGeneration` cancel the chat the user intends: pass an explicit `chatId` from the stop button (or abort `abortControllers.get(activeChatId)` as well) and POST that chatId to `/abort`; for a global stop, iterate `abortControllers` and abort each. - Give `/retry-agents` a stored controller (ref or store keyed by chatId) wired to the stop control; the server already aborts on socket close, so `.abort()` will propagate. - In `reset()`, `for (const c of state.abortControllers.values()) c.abort();` before replacing the map. - Remove the dead `933-934` lines and document same-chat rejection, OR relax the guard to abort+supersede the prior controller. - Bound autonomous fan-out with a small concurrency limiter (1-2) or process at most K chats per poll tick. ## Root-cause throughline Cancellation was modeled around a single active foreground stream (`streamingChatId`), but the app actually runs generations for background chats, autonomous sends, and retry-agent batches concurrently — none of which are reachable from that single-valued state — so the stop button and store reset silently miss them, leaving zombie upstream work that conversation-mode passive-disconnect was deliberately designed not to cancel. ## Verification checklist (leave all UNCHECKED) - [ ] Manually verify: start gen in chat A, switch to chat B, press Stop — chat A actually stops - [ ] Manually verify: an autonomous/scheduled send can be cancelled - [ ] Manually verify: a tracker/agent retry can be cancelled by the user - [ ] Manually verify: clear-all/expunge aborts in-flight generations - [ ] Manually verify: same-chat regenerate during a stream behaves per the documented decision (reject or supersede) - [ ] Run `pnpm check` --- ### Affected files - `packages/client/src/stores/chat.store.ts` - `packages/client/src/hooks/use-generate.ts` - `packages/client/src/hooks/use-background-autonomous.ts` - `packages/server/src/routes/generate/retry-agents-route.ts` - `packages/server/src/services/conversation/server-autonomous-scheduler.service.ts` ### Audit 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 `staging` tree. Severity: **HIGH**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Addressed on PR #2747. stopGeneration now accepts an explicit chatId, falls back through active/current/all tracked controllers, and reset aborts every tracked generation before clearing state. Retry-agent streams and background autonomous client streams now register AbortControllers in the shared per-chat map and pass their signals to streamEvents. The dead same-chat abort branch was removed after the existing concurrency guard, and the server autonomous scheduler now caps concurrent evaluations. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.

<!-- gh-comment-id:4763225816 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on PR #2747. stopGeneration now accepts an explicit chatId, falls back through active/current/all tracked controllers, and reset aborts every tracked generation before clearing state. Retry-agent streams and background autonomous client streams now register AbortControllers in the shared per-chat map and pass their signals to streamEvents. The dead same-chat abort branch was removed after the existing concurrency guard, and the server autonomous scheduler now caps concurrent evaluations. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.
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#1382
No description provided.