Skip to content

perf(tui): cache per-entry rendering and keep the Update loop responsive - #731

Merged
edenreich merged 9 commits into
mainfrom
refactor/codebase-health
Jul 3, 2026
Merged

edenreich merged 9 commits into
mainfrom
refactor/codebase-health

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Summary

Codebase health pass from a full audit (concurrency, architecture/dead code, TUI + A2A). The audit came back largely clean — no real data races, A2A protocol-consistent — so this lands the verified findings: two user-visible TUI defects, one goroutine-lifecycle gap, dead code, and hygiene. Nine independently revertable commits.

User-visible

  • Streaming no longer re-renders every message per chunk. ConversationView memoizes per-entry rendered output behind a fingerprint (entry identity, tool state, width, raw mode, expansion); only the streaming tail re-renders per chunk. Pending plan approvals bypass the cache (live button state); theme refresh clears it. Previously every chunk pushed the whole conversation through glamour — O(entries × chunks).
  • The GitHub Actions setup shortcut no longer freezes the TUI. Its three blocking gh subprocess checks moved out of Update() into a tea.Cmd reporting back via a result msg, with a status line while they run.

Correctness / lifecycle

  • Async MCP server startup is now cancelled on container Shutdown instead of running detached past it.
  • Four error-wrapping sites use %w (chains work with errors.Is/As); the SSH close path now uses errors.Join instead of formatting a []error that shadowed the errors package.
  • Agent session registry lock/unlock goes through deferred-unlock helpers.

Simplification

  • Deleted dead streamingMu/viewportMu from ConversationView — all access is TEA-loop-confined (documented on the struct); the concurrent-access test asserted a contract the component never had and became a sequential lifecycle test.
  • Deleted the unimplemented ToolFactory interface (+ generated fake + Taskfile entry) and the uncalled GetJobSupervisor accessor.
  • autocomplete and the message-history handler use the shared ANSI-aware formatting.TruncateText (the handler's local copy could slice negative on maxLen < 3).
  • All 93 tea.KeyMsg sites use the concrete v2 tea.KeyPressMsg — the app never enables key-release reporting, so this is behavior-preserving, drops per-site type assertions, and prevents release events double-firing handlers if keyboard enhancements are ever enabled.

Net: 30 files, +431/−454.

Verification

  • go test -race ./... — clean (gate for the mutex removal and lifecycle changes)
  • golangci-lint run — 0 issues; go vet clean
  • task mocks:generate — fake_tool_factory.go stays deleted, nothing else regenerates
  • New table subtests cover render-cache hit/invalidation (width, raw toggle, entry mutation, shrink, pending-plan bypass)
  • Suggested manual pass: stream a long markdown reply (no per-chunk lag; raw-mode + theme toggles still re-render) and trigger the GitHub Actions setup shortcut (UI stays responsive)

edenreich added 3 commits July 3, 2026 02:44
handleGithubActionSetupTrigger execd three gh CLI subprocesses inside the
key-press path, freezing the TUI for their combined duration. The checks now
run in a tea.Cmd and report back via githubSetupCheckedMsg; the branching
(org fast path, setup view transition) moved into the msg handler.
Every streaming chunk rebuilt the whole viewport, re-rendering every entry
through the markdown renderer - O(entries x chunks) glamour renders made
streaming lag on long conversations. Entries are now memoized by index with
a fingerprint over their render inputs (identity time, mutable entry state,
width, raw mode, expansion); the streaming tail stays uncached, pending
plan approvals bypass the cache (live button state), and theme refresh
clears it.
streamingMu and viewportMu guarded state that only the Bubble Tea event
loop ever touches (all entry points are tea msg handlers; the component
launches no goroutines), while sibling fields on the same paths were
unguarded. The half-locking implied a concurrency contract that never
existed. The struct doc now states the loop-confinement rule; the
concurrent-access test became a sequential lifecycle test since it
exercised a contract the component no longer claims.
@edenreich
edenreich requested a review from a team as a code owner July 3, 2026 01:20
edenreich added 6 commits July 3, 2026 03:22
The MCP StartServers goroutine ran on a detached context and could keep
starting servers through and past container shutdown; its cancel func is
now stored and invoked in Shutdown before the manager closes.
…sor accessor

ToolFactory had no implementation or consumer anywhere outside its
generated fake; GetJobSupervisor had no callers (the supervisor is
constructed internally by ensureBackgroundTaskRegistry).
autocomplete and the message-history handler each carried a byte-based
local truncator; the handler's could slice negative on maxLen < 3. Both
now use formatting.TruncateText, which is width-aware and clamps.
Four sites formatted causes with %v, breaking errors.Is/As. The SSH close
path also shadowed the errors package with a local []error; it now joins
the collected errors with errors.Join.
register/deregister now go through two small helpers using defer, so a
panic mid-critical-section can't leave sessionMux held.
In Bubble Tea v2 KeyMsg is an interface over press and release events;
this app never enables key releases, so handlers now take the concrete
KeyPressMsg. That drops the per-site type assertions and guards against
release events double-firing handlers if keyboard enhancements are ever
enabled.
@edenreich
edenreich force-pushed the refactor/codebase-health branch from 8499385 to 14e347e Compare July 3, 2026 01:22
@edenreich
edenreich merged commit 8c9db0d into main Jul 3, 2026
1 check passed
@edenreich
edenreich deleted the refactor/codebase-health branch July 3, 2026 01:27
inference-gateway-releaser Bot added a commit that referenced this pull request Jul 3, 2026
## [0.128.0](v0.127.0...v0.128.0) (2026-07-03)

### 🚀 Features

* **chat:** make status indicators selectable with /tools and /a2a views ([#732](#732)) ([6882de0](6882de0)), closes [#717](#717) [#717](#717)

### 🐛 Bug Fixes

* **agent:** drop redundant Idle transition that errored on every run ([#726](#726)) ([469ec64](469ec64)), closes [#722](#722) [#725](#725)
* **agent:** report session-cumulative total_turns in completion logs ([#728](#728)) ([0d8fb08](0d8fb08)), closes [#727](#727)
* **a2a:** route active A2A reads and HasPending through the job supervisor ([#693](#693)) ([#721](#721)) ([dcd0670](dcd0670)), closes [#718](#718) [#719](#719) [#720](#720)
* **keybindings:** stop "unknown keybinding action" log flood ([#724](#724)) ([c828426](c828426)), closes [#714](#714) [#722](#722)
* **a2a:** unify background-job liveness, clear, and cancel on the supervisor contract ([#730](#730)) ([eb37fd0](eb37fd0)), closes [#693](#693) [#720](#720) [#718](#718) [#719](#719)
* **logging:** use structured key/value form at misused call sites ([#729](#729)) ([cd96028](cd96028)), closes [#722](#722) [#724](#724) [#724](#724) [#723](#723)
* **tools:** wait for pipe readers before reaping detached bash shells ([#716](#716)) ([e1d0d84](e1d0d84)), closes [#696](#696)

### ⚡ Performance Improvements

* **tui:** cache per-entry rendering and keep the Update loop responsive ([#731](#731)) ([8c9db0d](8c9db0d))

### 📚 Documentation

* **a2a:** add SKILL.md documentation for Agent2Agent protocol ([defcd57](defcd57))

### 🔧 Build System

* **deps:** bump golang.org/x/net from 0.47.0 to 0.55.0 in /examples/mcp/mcp-server ([#715](#715)) ([990803b](990803b))

### 🧹 Maintenance

* **flox:** bump dev deps ([d0c8f6b](d0c8f6b))
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.128.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant