[PR #5728] [MERGED] fix(client): Professor Mari survives backgrounding like regular chats do #6904

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5728
Author: @kolacheee
Created: 9/1/2026
Status: ✅ Merged
Merged: 9/1/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5719-mari-background-resilience


📝 Commits (3)

  • df57079 fix(client): recover Professor Mari from backgrounded-tab stream deaths (#5719)
  • d7dceba fix(client): close the recovery races the #5719 review confirmed
  • f7e714c Merge branch 'staging' into fix/5719-mari-background-resilience

📊 Changes

5 files changed (+246 additions, -21 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/components/chat/HomeProfessorMariChat.tsx (+121 -15)
📝 packages/client/src/hooks/use-generate.ts (+1 -6)
📝 packages/client/src/lib/api-client.ts (+22 -0)
➕ scripts/regressions/mari-background-stream.regression.ts (+101 -0)

📄 Description

Linked issue

Closes #5719.

Why this change

Professor Mari appeared to die the moment the window lost focus ("Rendering stoped because window was unfocused" in the report), while regular chats kept generating in the background. The investigation showed the server was never the problem: Mari's whole agent loop runs server-side and the /prompt route deliberately ignores passive disconnects and persists the finished reply. What died was the client. Regular chat classifies any plain transport error after the page was hidden as a passive disconnect and settles-then-reloads; Mari recovered from exactly one error class — the visible-tab resume watchdog's StreamResumeDisconnectError. Android browsers tear down a hidden tab's connection with a bare TypeError, so Mari toasted "could not answer right now" while the server finished a reply the UI never showed. A cleanly closed socket was worse: an immediate false "did not receive a reply" fired with no recovery path at all, and a reply that finished while the client was detached (stream killed, or Mini-Mari closed and reopened) stayed invisible until a manual chat switch.

What changed

  • One shared classifier: isPassiveStreamDisconnect is hoisted from use-generate.ts into api-client.ts (next to the watchdog error it complements) and used by both regular chat and Mari. The watchdog error short-circuits true; everything else requires hidden-page history and excludes caller aborts and HTTP-level ApiErrors.
  • Mari's send closure now tracks visibilitychange/pagehide exactly like use-generate does, and its catch recovers from every passive-disconnect shape — "Finishing in the background…", wait for the run to settle, reload the persisted reply.
  • A clean no-reply stream close consults the status endpoint before toasting: waitForWorkspaceRunToSettle now reports whether it ever observed the run active. If a live run is confirmed, Mari settles and reloads instead of showing the false "did not receive a reply"; a genuinely dead run still surfaces the honest toast.
  • Detached-run recovery: the existing 15-second status poll now watches for a run transitioning active → inactive while no local send closure is driving it, and reloads the persisted reply — so closing and reopening Mini-Mari (or losing the stream entirely) no longer hides a finished answer.
  • New source-pin regression mari-background-stream covers the shared classifier, Mari's tracking and recovery paths, the settle-and-confirm no-reply handling, the detached-run reload, and the server route's keep-running contract.

Known limitation (pre-existing, documented in the commit): the workspace status active flag is global rather than run-scoped, so the settle poll could in principle observe an unrelated concurrent Mari action; run-scoping the status endpoint is a candidate follow-up.

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 lint and the client production build are green locally; format:check fails repo-wide on this Windows checkout (pre-existing autocrlf CRLF; touched files were run through Prettier directly).
  • The new regression passes and was differentially verified to FAIL with the client changes stashed.
  • A two-lens adversarial review (listener/effect lifecycle and races; classifier semantics parity with regular chat) raised 7 findings; 4 were confirmed by independent verification and are fixed in the second commit: server-reported SSE errors could be swallowed as passive disconnects whenever the tab had been hidden (now a dedicated error class rethrown before classification); the clean-close settle could conclude "never ran" from one status poll racing the route's pre-run storage work (two consecutive inactive readings now required, and the no-reply toast is suppressed when visibility history makes a false negative likely — the reload is the authoritative surface); the detached-run recovery effect double-fired after every long local run, duplicating the reload and the app-wide query invalidation (arming now needs two remote-active observations with no local closure, pinned to a run id that also guards the reload against clobbering an optimistic message); and one vacuous regression pin was anchored. The 3 refuted findings included a claimed 30-minute composer lock (pre-existing shape, Stop button remains live) and a claimed regular-chat behavior change from the classifier hoist (the watchdog-error short-circuit is unreachable in a state where the flag differs).
  • Please verify manually (the real repro needs a phone): on Android Firefox or Chrome, start a Mari generation (a lorebook build is long enough), switch to another app for ~30 seconds, and come back — the reply should appear (possibly after a brief "Finishing in the background…") instead of the "could not answer right now" toast. Also: start a run in Mini-Mari, close Mini-Mari, reopen it — the finished reply should show up within one status-poll cycle (~15s).

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)

CHANGELOG updated under [Unreleased] → Fixed. No user-facing docs under docs/ touched.

UI evidence (if applicable)

Behavioral change only — the visible difference is recovery ("Finishing in the background…" then the reply) where there used to be an error toast, and replies appearing after reopening Mini-Mari.

Summary by CodeRabbit

  • Bug Fixes
    • Professor Mari chats now recover gracefully when the browser tab is hidden or loses focus during a response.
    • Completed responses are reloaded automatically after a temporary connection loss.
    • Users no longer see misleading errors when a backgrounded chat disconnects passively.
  • Tests
    • Added regression coverage for backgrounded-stream recovery and response restoration.
  • Documentation
    • Updated the unreleased changelog with these fixes.

🔄 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/5728 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/1/2026 **Status:** ✅ Merged **Merged:** 9/1/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5719-mari-background-resilience` --- ### 📝 Commits (3) - [`df57079`](https://github.com/Pasta-Devs/Marinara-Engine/commit/df570797b1ac601fd6b1b65427e409585e1a3573) fix(client): recover Professor Mari from backgrounded-tab stream deaths (#5719) - [`d7dceba`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d7dceba97f62cf1032e964ab28c3481ea20db461) fix(client): close the recovery races the #5719 review confirmed - [`f7e714c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f7e714c4a055d0d12f7a7a94daed0b3d665b7ba2) Merge branch 'staging' into fix/5719-mari-background-resilience ### 📊 Changes **5 files changed** (+246 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/components/chat/HomeProfessorMariChat.tsx` (+121 -15) 📝 `packages/client/src/hooks/use-generate.ts` (+1 -6) 📝 `packages/client/src/lib/api-client.ts` (+22 -0) ➕ `scripts/regressions/mari-background-stream.regression.ts` (+101 -0) </details> ### 📄 Description ## Linked issue Closes #5719. ## Why this change Professor Mari appeared to die the moment the window lost focus ("Rendering stoped because window was unfocused" in the report), while regular chats kept generating in the background. The investigation showed the server was never the problem: Mari's whole agent loop runs server-side and the `/prompt` route deliberately ignores passive disconnects and persists the finished reply. What died was the client. Regular chat classifies **any** plain transport error after the page was hidden as a passive disconnect and settles-then-reloads; Mari recovered from exactly one error class — the visible-tab resume watchdog's `StreamResumeDisconnectError`. Android browsers tear down a hidden tab's connection with a bare `TypeError`, so Mari toasted "could not answer right now" while the server finished a reply the UI never showed. A **cleanly** closed socket was worse: an immediate false "did not receive a reply" fired with no recovery path at all, and a reply that finished while the client was detached (stream killed, or Mini-Mari closed and reopened) stayed invisible until a manual chat switch. ## What changed - **One shared classifier**: `isPassiveStreamDisconnect` is hoisted from `use-generate.ts` into `api-client.ts` (next to the watchdog error it complements) and used by both regular chat and Mari. The watchdog error short-circuits true; everything else requires hidden-page history and excludes caller aborts and HTTP-level `ApiError`s. - **Mari's send closure** now tracks `visibilitychange`/`pagehide` exactly like `use-generate` does, and its catch recovers from every passive-disconnect shape — "Finishing in the background…", wait for the run to settle, reload the persisted reply. - **A clean no-reply stream close consults the status endpoint before toasting**: `waitForWorkspaceRunToSettle` now reports whether it ever observed the run active. If a live run is confirmed, Mari settles and reloads instead of showing the false "did not receive a reply"; a genuinely dead run still surfaces the honest toast. - **Detached-run recovery**: the existing 15-second status poll now watches for a run transitioning active → inactive while no local send closure is driving it, and reloads the persisted reply — so closing and reopening Mini-Mari (or losing the stream entirely) no longer hides a finished answer. - New source-pin regression `mari-background-stream` covers the shared classifier, Mari's tracking and recovery paths, the settle-and-confirm no-reply handling, the detached-run reload, and the server route's keep-running contract. Known limitation (pre-existing, documented in the commit): the workspace status `active` flag is global rather than run-scoped, so the settle poll could in principle observe an unrelated concurrent Mari action; run-scoping the status endpoint is a candidate follow-up. ## 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 lint` and the client production build are green locally; `format:check` fails repo-wide on this Windows checkout (pre-existing autocrlf CRLF; touched files were run through Prettier directly). - The new regression passes and was differentially verified to FAIL with the client changes stashed. - A two-lens adversarial review (listener/effect lifecycle and races; classifier semantics parity with regular chat) raised 7 findings; 4 were confirmed by independent verification and are fixed in the second commit: server-reported SSE errors could be swallowed as passive disconnects whenever the tab had been hidden (now a dedicated error class rethrown before classification); the clean-close settle could conclude "never ran" from one status poll racing the route's pre-run storage work (two consecutive inactive readings now required, and the no-reply toast is suppressed when visibility history makes a false negative likely — the reload is the authoritative surface); the detached-run recovery effect double-fired after every long local run, duplicating the reload and the app-wide query invalidation (arming now needs two remote-active observations with no local closure, pinned to a run id that also guards the reload against clobbering an optimistic message); and one vacuous regression pin was anchored. The 3 refuted findings included a claimed 30-minute composer lock (pre-existing shape, Stop button remains live) and a claimed regular-chat behavior change from the classifier hoist (the watchdog-error short-circuit is unreachable in a state where the flag differs). - **Please verify manually (the real repro needs a phone):** on Android Firefox or Chrome, start a Mari generation (a lorebook build is long enough), switch to another app for ~30 seconds, and come back — the reply should appear (possibly after a brief "Finishing in the background…") instead of the "could not answer right now" toast. Also: start a run in Mini-Mari, close Mini-Mari, reopen it — the finished reply should show up within one status-poll cycle (~15s). ## 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) CHANGELOG updated under **[Unreleased] → Fixed**. No user-facing docs under `docs/` touched. ## UI evidence (if applicable) Behavioral change only — the visible difference is recovery ("Finishing in the background…" then the reply) where there used to be an error toast, and replies appearing after reopening Mini-Mari. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Professor Mari chats now recover gracefully when the browser tab is hidden or loses focus during a response. * Completed responses are reloaded automatically after a temporary connection loss. * Users no longer see misleading errors when a backgrounded chat disconnects passively. * **Tests** * Added regression coverage for backgrounded-stream recovery and response restoration. * **Documentation** * Updated the unreleased changelog with these fixes. <!-- 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:14:45 +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#6904
No description provided.