[PR #13026] fix(opencode): coalesce stream updates and add reconnect backoff #14482

Open
opened 2026-02-16 18:19:16 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13026

State: open
Merged: No


Summary

This PR narrows the scope to surgical TUI performance fixes in the existing OpenTUI path. It focuses on stream event coalescing, reconnect backoff, and lower-cost part updates during streaming.

Related to #13027.

What Changed

1) Redraw coalescing for stream updates

  • File: packages/opencode/src/cli/cmd/tui/context/sdk.tsx
  • message.part.updated events now use a slightly longer batch window than non-stream events.
  • Non-stream events still flush quickly.
  • Added a queue cap that forces an immediate flush to avoid unbounded backlog.

2) Idle CPU guard for SSE reconnects

  • File: packages/opencode/src/cli/cmd/tui/context/sdk.tsx
  • Added reconnect delay/backoff after stream failures.
  • Added a short pause after stream completion before reconnecting.
  • This avoids tight reconnect loops when the stream drops repeatedly.

3) Lower-cost stream part updates

  • File: packages/opencode/src/cli/cmd/tui/context/sync.tsx
  • For existing parts, use direct replacement instead of reconcile(...) on every message.part.updated event.
  • Skip no-op replacement when object identity is unchanged.

4) Stream append path optimization in message rendering

  • File: packages/opencode/src/cli/cmd/tui/routes/session/index.tsx
  • Removed per-render trim() calls on streaming text content.
  • Reused a memoized content accessor for markdown/code renderables.
  • This avoids repeated full-string scans/copies as streamed text grows.

Why This Scope

The previous branch attempted a broader architecture path. This version keeps the implementation in the current renderer/data flow and addresses known hotspots directly.

Notes

  • No new rendering subsystem.
  • No framework rewrite.
  • No behavior change to message ordering or event semantics.
  • The branch now contains a single focused commit touching 3 files.

Related Context

These changes align with common TUI guidance to avoid continuous redraw pressure and busy reconnect loops:

Validation

I ran local typecheck, but the workspace currently has many unrelated baseline errors in this environment. The changes in this PR are small and isolated to TUI event/render paths.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13026 **State:** open **Merged:** No --- ## Summary This PR narrows the scope to surgical TUI performance fixes in the existing OpenTUI path. It focuses on stream event coalescing, reconnect backoff, and lower-cost part updates during streaming. Related to #13027. ## What Changed ### 1) Redraw coalescing for stream updates - File: `packages/opencode/src/cli/cmd/tui/context/sdk.tsx` - `message.part.updated` events now use a slightly longer batch window than non-stream events. - Non-stream events still flush quickly. - Added a queue cap that forces an immediate flush to avoid unbounded backlog. ### 2) Idle CPU guard for SSE reconnects - File: `packages/opencode/src/cli/cmd/tui/context/sdk.tsx` - Added reconnect delay/backoff after stream failures. - Added a short pause after stream completion before reconnecting. - This avoids tight reconnect loops when the stream drops repeatedly. ### 3) Lower-cost stream part updates - File: `packages/opencode/src/cli/cmd/tui/context/sync.tsx` - For existing parts, use direct replacement instead of `reconcile(...)` on every `message.part.updated` event. - Skip no-op replacement when object identity is unchanged. ### 4) Stream append path optimization in message rendering - File: `packages/opencode/src/cli/cmd/tui/routes/session/index.tsx` - Removed per-render `trim()` calls on streaming text content. - Reused a memoized `content` accessor for markdown/code renderables. - This avoids repeated full-string scans/copies as streamed text grows. ## Why This Scope The previous branch attempted a broader architecture path. This version keeps the implementation in the current renderer/data flow and addresses known hotspots directly. ## Notes - No new rendering subsystem. - No framework rewrite. - No behavior change to message ordering or event semantics. - The branch now contains a single focused commit touching 3 files. ## Related Context These changes align with common TUI guidance to avoid continuous redraw pressure and busy reconnect loops: - Ratatui high draw-rate CPU issue (`terminal.draw`): https://github.com/ratatui/ratatui/issues/1338 - Streaming markdown optimization via partial updates (Will McGugan): https://willmcgugan.github.io/streaming-markdown/ ## Validation I ran local typecheck, but the workspace currently has many unrelated baseline errors in this environment. The changes in this PR are small and isolated to TUI event/render paths.
yindo added the pull-request label 2026-02-16 18:19:16 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14482