Skip to content

fix(realtime): tear down chat streams inside the request's workspace scope - #503

Merged
mortondev merged 1 commit into
mainfrom
fix/chat-stream-teardown-scope
Sep 6, 2026
Merged

fix(realtime): tear down chat streams inside the request's workspace scope#503
mortondev merged 1 commit into
mainfrom
fix/chat-stream-teardown-scope

Conversation

@mortondev

Copy link
Copy Markdown
Member

Summary

  • Under pooled tenancy, clearPresence in /api/chat/stream's cleanup threw WorkspaceScopeMissingError on every client disconnect: the abort listener and SSE cancel hook run outside the request's AsyncLocalStorage context. Logged as clear presence failed on each teardown.
  • Consequences beyond noise: the presence row was left to expire on its own, and wentOffline never came back true, so requeueUnansweredOnAgentOffline was skipped when an agent's last stream closed.
  • Fix: capture getWorkspaceScope() while the request is live and run teardown via runWithWorkspaceScope — the pattern auth/index.ts and resolved-claims-stash.ts already use for their detached sweeps. Single-tenant installs (no scope) are unchanged.

Test plan

  • New test: open the stream inside a workspace scope, abort from outside it, assert clearPresence observes the scope (fails on main, passes here)
  • Existing 31 stream tests, tsc, oxlint, prettier
  • After roll: clear presence failed warnings stop on web

Made with Cursor

…scope

The abort listener and the SSE cancel hook are invoked by the runtime outside
the request's AsyncLocalStorage context, so under pooled tenancy clearPresence
ran with no workspace scope and threw on every client disconnect. That left
the presence row to expire on its own and, because wentOffline never came
back true, skipped requeueUnansweredOnAgentOffline for an agent whose last
stream closed. Capture the scope while the request is live and re-enter it
for teardown, the same pattern the auth stash sweeps already use.

Co-authored-by: Cursor <cursoragent@cursor.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-06T19:11:32.299708Z 27de00d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@mortondev
mortondev merged commit 06b4c3f into main Sep 6, 2026
14 checks passed
@mortondev
mortondev deleted the fix/chat-stream-teardown-scope branch September 6, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant