Skip to content

fix(opencode): coalesce stream updates and add reconnect backoff#13026

Open
chindris-mihai-alexandru wants to merge 1 commit intoanomalyco:devfrom
chindris-mihai-alexandru:feat/rfc-terminal-optimization
Open

fix(opencode): coalesce stream updates and add reconnect backoff#13026
chindris-mihai-alexandru wants to merge 1 commit intoanomalyco:devfrom
chindris-mihai-alexandru:feat/rfc-terminal-optimization

Conversation

@chindris-mihai-alexandru
Copy link

@chindris-mihai-alexandru chindris-mihai-alexandru commented Feb 10, 2026

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.

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@chindris-mihai-alexandru chindris-mihai-alexandru changed the title feat: TUI Performance Optimization with EnhancedTextBuffer (3.9x faster) feat: TUI Performance Optimization with EnhancedTextBuffer (Fixes #13027) Feb 10, 2026
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@pkuxkxjason
Copy link

Please merge this PR. I am suffering for the performance issue of TUI everyday!

@chindris-mihai-alexandru
Copy link
Author

hey @thdxr @rekram1-node @adamdotdevin - I think this is worth taking a look at and would greatly benefit the OpenCode community. Lmk your thoughts / suggestions / objections.

@chindris-mihai-alexandru chindris-mihai-alexandru force-pushed the feat/rfc-terminal-optimization branch from 6964e3a to 546238a Compare February 16, 2026 18:01
@chindris-mihai-alexandru chindris-mihai-alexandru changed the title feat: TUI Performance Optimization with EnhancedTextBuffer (Fixes #13027) perf(tui): coalesce stream updates and add reconnect backoff Feb 16, 2026
@github-actions
Copy link
Contributor

Hey! Your PR title perf(tui): coalesce stream updates and add reconnect backoff doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@chindris-mihai-alexandru
Copy link
Author

I narrowed this PR to a surgical scope and force-pushed the branch.

Current diff is only 3 files in existing TUI paths:

  • packages/opencode/src/cli/cmd/tui/context/sdk.tsx
  • packages/opencode/src/cli/cmd/tui/context/sync.tsx
  • packages/opencode/src/cli/cmd/tui/routes/session/index.tsx

The changes focus on:

  • stream event coalescing,
  • reconnect backoff to avoid busy loops,
  • lower-cost updates for message.part.updated,
  • removing per-render trim() in streaming text rendering.

No new rendering subsystem is introduced in this version.

@chindris-mihai-alexandru chindris-mihai-alexandru changed the title perf(tui): coalesce stream updates and add reconnect backoff fix(opencode): coalesce stream updates and add reconnect backoff Feb 16, 2026
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.

2 participants