Skip to content

fix(frontend): subscribe event stream directories after connecting instead of in the URL - #499

Merged
chriswritescode-dev merged 1 commit into
mainfrom
fix/sse-stream-url-length
Nov 29, 2026
Merged

chriswritescode-dev merged 1 commit into
mainfrom
fix/sse-stream-url-length

Conversation

@chriswritescode-dev

@chriswritescode-dev chriswritescode-dev commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

User description

Summary

The browser's event stream no longer sends its subscribed directories in the URL. It opens /api/sse/stream without a query and subscribes every directory through POST /api/sse/subscribe once the connected event provides a client id.

Problem

buildUrl() added every subscribed directory to ?directories=. With repos plus every linked server's project directories, the URL grew to about 16 KB (221 directories) and Cloudflare rejected it with 431. An open stream kept working, but every reconnect (focus, resize, network change) failed, so the session page stayed on "Reconnecting… live updates paused" and the backend had no browser clients.

Change

  • buildUrl() returns the bare stream URL.
  • connect() marks all current directories as pending, so the first connection and every reconnect (after an error, a stall, an explicit reconnect or a bfcache restore) resubscribe them through the existing subscribe request. The backend replays state for subscribed directories the same way it did for directories in the URL.
  • Removed the now-redundant pending repopulation in disconnectConnectionOnly.
  • The backend still accepts ?directories= for older clients.

Tests

  • Stream tests assert the URL has no directories parameter and that the subscribe request carries every directory, including on a bfcache reconnect.
  • New test: a reconnect after a connection error resubscribes all current directories.
  • Frontend stream, context and hook tests pass (926). Typecheck and lint are clean.
  • Against the live deployment behind Cloudflare, the bare stream URL connects and a subscribe request with all 221 directories returns 200.

PR Type

Bug fix


Description

  • Move directory subscriptions out of SSE stream URL

  • Subscribe all directories via POST /api/sse/subscribe on connect

  • Resubscribe directories on every reconnect, including bfcache restores

  • Add tests covering bare URL and reconnect resubscription


Diagram Walkthrough

flowchart LR
  A["OpenCodeEventStream.connect"] -- "builds bare URL" --> B["/api/sse/stream"]
  B -- "connected event with clientId" --> C["POST /api/sse/subscribe"]
  C -- "sends all directoryRefCounts" --> D["Backend replays state"]
  E["Reconnect / bfcache restore"] -- "re-runs connect" --> A
Loading

File Walkthrough

Relevant files
Bug fix
openCodeEventStream.ts
Strip directories from stream URL and resubscribe on connect

frontend/src/lib/opencode-event-stream/openCodeEventStream.ts
  • buildUrl() now returns the bare /api/sse/stream URL without a
    directories query parameter.
  • connect() seeds pendingDirectories from all current directoryRefCounts
    so the first connection and every reconnect resubscribe through the
    existing subscribe flow.
  • Removed the redundant pending-directory repopulation from
    disconnectConnectionOnly.
+3/-7     
Tests
openCodeEventStream.test.ts
Update stream tests for post-connect directory subscriptions

frontend/src/lib/opencode-event-stream/__tests__/openCodeEventStream.test.ts
  • Reworked initial-connection tests to assert the URL has no directories
    parameter and that a subscribe request carries every directory.
  • Updated the bfcache restore test to expect a bare reconnect URL
    followed by resubscription for the new client id.
  • Added a test that a reconnect after a connection error resubscribes
    all current directories.
+64/-15 

@lil-worker-cstech

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
⚠️ Risk level: Low
✅ Merge recommendation: No concerns found
Merge recommendation checks
✅ CI on reviewed head: passed
✅ Head is current with its base: up to date
✅ Human authors: all authors are human
✅ No sensitive paths or risky changes: none detected
✅ Changed lines within cap: 89 changed lines (cap 100)
🔎 Failure modes

What: Directories are now subscribed only by the `POST /api/sse/subscribe` request sent after `connected`. Before this change, the URL subscribed them when the stream opened. If that request fails or is rejected, the stream reports connected and healthy but receives no directory events. Nothing in the visible code retries it before the next reconnect.
Where: frontend/src/lib/opencode-event-stream/openCodeEventStream.ts:connect
Trigger: The subscribe POST fails right after the connection opens, for example from a brief network error, a 5xx, or a WAF or body-size limit on the large directory list.
Detected by: Nothing apparent. The session would stop getting live updates without showing the "Reconnecting" state.
Covered in this PR: No

What: Events sent between the stream opening and the subscribe request finishing are no longer delivered for those directories. The change relies on the backend replaying state on subscribe, as it did for directories in the URL.
Where: frontend/src/lib/opencode-event-stream/openCodeEventStream.ts:connect
Trigger: A session event arrives during the short gap between `connected` and the subscribe request being handled, on any reconnect.
Detected by: Backend replay tests for subscribe, if any exist. The tests in this PR only check that the request is sent.
Covered in this PR: No

🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

@chriswritescode-dev
chriswritescode-dev merged commit 7ed857f into main Nov 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant