Skip to content

fix(terminal): stop repeated history refresh replays - #342

Open
tailong-wu wants to merge 2 commits into
Ark0N:masterfrom
tailong-wu:fix/terminal-refresh-replay-loop
Open

fix(terminal): stop repeated history refresh replays#342
tailong-wu wants to merge 2 commits into
Ark0N:masterfrom
tailong-wu:fix/terminal-refresh-replay-loop

Conversation

@tailong-wu

Copy link
Copy Markdown

Summary

  • scope session:needsRefresh SSE frames to the active session
  • ignore anonymous SSE backpressure recovery when the active WebSocket already owns the complete terminal stream
  • coalesce overlapping refresh fetch/clear/replay cycles per session
  • preserve SSE fallback refresh behavior and OpenCode resize recovery

Reproduction

The new regression harness failed in three independent ways on master:

  1. a refresh for a background session called _onSessionNeedsRefresh for the active tab
  2. an anonymous SSE drain refresh replayed history even while the active terminal WebSocket was complete
  3. two refresh signals started two concurrent full-history fetch/replay cycles

Together, sustained terminal output can backpressure the duplicate SSE stream, whose drain signal makes the browser fetch, clear, and replay history. That replay can cause more backpressure and visibly loop old terminal history.

Verification

  • npm test (312 files passed, 6097 tests passed)
  • npm run typecheck
  • npm run lint
  • npm run format:check
  • npm run check:frontend-syntax
  • npm run test:browser -- test/opencode-resize.test.ts -t "does not replay history"

The Playwright regression dispatches both a background-session refresh and an anonymous SSE backpressure refresh with an active terminal WebSocket, then verifies that the page makes zero /terminal history requests.

Copilot AI lite review requested due to automatic review settings August 25, 2026 09:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Add per-request generation or token validation to prevent stale refresh responses from mutating the active terminal.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This pull request prevents repeated terminal history replays by routing refreshes correctly, suppressing duplicate SSE recovery, and coalescing refresh cycles.

Changes:

  • Scope refresh events to the active session.
  • Avoid redundant SSE recovery when WebSocket output is complete.
  • Add concurrency and browser regression coverage.
File summaries
File Summary
test/terminal-scroll-intent.test.ts Updates refresh-path assertions.
test/terminal-refresh-routing.test.ts Adds routing and coalescing tests.
test/opencode-resize.test.ts Adds browser regression coverage.
src/web/public/app.js Implements refresh routing and replay locking; a critical stale in-flight request race remains unresolved.
Review details

Suppressed comments (1)

src/web/public/app.js:1869

  • _wsReady only says that the current socket is open; it does not prove it received the output that was dropped while a previous socket was reconnecting. During that gap SSE is the fallback, and its anonymous drain frame can arrive after onopen flips _wsReady; returning here then discards the only history-recovery signal, while _connectWs() only sends a resize and does not reload history. Track WS continuity/fallback state (or force one recovery after reconnect) so a drain covering a WS gap is still honored, and add a reconnect-gap regression test.
    if (this._wsReady && this._wsSessionId === this.activeSessionId) return;
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/web/public/app.js
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.

2 participants