Skip to content

fix(app): resync session stream after iOS PWA returns from background - #26471

Closed
ysm-dev wants to merge 1 commit into
anomalyco:devfrom
ysm-dev:fix/ios-pwa-stream-reconnect
Closed

ysm-dev wants to merge 1 commit into
anomalyco:devfrom
ysm-dev:fix/ios-pwa-stream-reconnect

Conversation

@ysm-dev

@ysm-dev ysm-dev commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #17769

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When the web app runs as a PWA on iOS and is backgrounded, Safari suspends JS and quietly closes the SSE connection. On resume the existing visibility heuristic skipped reconnect because the last event timestamp was within the heartbeat window right before suspension, so the session UI stayed frozen until the user manually refreshed.

Changes:

  • `global-sdk.tsx`: track the `hidden` flag explicitly; on `visibilitychange` to visible (and `pageshow` with `event.persisted` for bfcache), force-reconnect with a 1s debounce. Detect successful reconnects (`server.connected` arriving while already connected) and emit a new `event.reconnect`.
  • `session.tsx`: subscribe to `event.reconnect` and force-resync the active session messages. The directory bootstrap re-fetches the session list but not message bodies, so events arriving during the gap would otherwise be lost.
  • `sync.tsx`: extend `runInflight` so a `force: true` call still re-runs after a non-force task currently in flight (queues after success or failure). Wired into `session.sync()`.
  • `sync-optimistic.test.ts`: tests for the new `runInflight` behavior.

How did you verify your code works?

Manual: installed the web app to home screen on iOS, started a streaming session, switched apps for ~30s, returned — the stream resumes and the session catches up without a refresh.

Automated:

  • `bun test src/context/sync-optimistic.test.ts` from `packages/app` (7 pass)
  • `bun typecheck` from `packages/app`

Screenshots / recordings

N/A — no UI surface changed.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

iOS suspends JS and closes the SSE connection when the PWA is backgrounded. The existing visibility heuristic skipped reconnect because the last event was recent right before suspension, leaving the session frozen. Force-reconnect on visibility/pageshow, detect successful reconnects via repeated server.connected, and resync the active session messages so events emitted during the gap are recovered.
@ysm-dev
ysm-dev requested a review from adamdotdevin as a code owner May 9, 2026 03:39
@github-actions github-actions Bot added contributor needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels May 9, 2026
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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.

Session state stale after device sleep/resume — heartbeat mismatch causes premature SSE disconnect

1 participant