fix(tui): coalesce editor top-border rebuild to render tempo

EventController.handleEvent rebuilt the editor's status-line top border
synchronously on every session event via updateEditorTopBorder(). During
a long-running eval that fires 5-10 events/s, each rebuild ran
StatusLine.getTopBorder → #buildSegmentContext → getCachedContextBreakdown
→ session.getContextUsage → estimateTokens (with JSON.stringify per
toolCall block) — the render pipeline is throttled to ~30 fps, so most
rebuilds were dropped before painting. Combined with a scheduler that
collapsed cadenceDelay to zero whenever a frame overran the 33ms budget,
the TUI busy-looped at ~40-50% CPU.

Fix:
- Editor gains setTopBorderProvider(): a lazy builder invoked once per
  editor render. InteractiveMode installs it in the constructor and on
  setEditorComponent, so the rebuild coalesces to the render tempo
  regardless of event rate.
- Delete updateEditorTopBorder wrapper (now equivalent to
  ui.requestRender) and inline every call site.
- Add adaptive render backpressure: a frame that exceeds
  MIN_RENDER_INTERVAL_MS inflates the next scheduling delay to
  2 * last_frame_cost, capped at 200 ms, targeting a 50% render duty
  cycle instead of pinning the CPU at t=0.

New regression tests:
- editor-top-border-provider.test.ts: provider fires exactly once per
  render, wins over eager setTopBorder, falls back when cleared, gets
  the correct availableWidth.
- adaptive-render-backpressure.test.ts: cheap frames keep the 33 ms
  cadence, a slow frame idles proportionally, pathological frames are
  capped at 200 ms.

Verified with bun test packages/tui/test (all 246 relevant tests pass)
and bun test packages/coding-agent/test/modes (455 tests pass). Three
pre-existing agent-session-handoff snapcompact failures on main are
unrelated (snapcompactSupportedChars binding).

Fixes #4145
This commit is contained in:
roboomp
2026-07-01 13:51:43 +00:00
parent f70e4f1570
commit 809a8a348d
16 changed files with 391 additions and 69 deletions
@@ -867,7 +867,7 @@ export class CommandController {
this.ctx.statusLine.invalidate();
this.ctx.statusLine.resetActiveTime();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
this.ctx.updateEditorBorderColor();
this.ctx.chatContainer.clear();
this.ctx.pendingMessagesContainer.clear();
@@ -893,7 +893,7 @@ export class CommandController {
}
const stateLabel = result.closedProviderSessions === 1 ? "provider state" : "provider states";
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
this.ctx.showStatus(`Fresh provider session started (${result.closedProviderSessions} ${stateLabel} pruned).`);
}
@@ -923,7 +923,7 @@ export class CommandController {
}
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
const sessionFile = this.ctx.session.sessionFile;
const shortPath = sessionFile ? sessionFile.split("/").pop() : "new session";
@@ -1156,7 +1156,7 @@ export class CommandController {
}
this.ctx.rebuildChatFromMessages();
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
this.ctx.showStatus(formatShakeSummary(result));
}
@@ -1219,7 +1219,7 @@ export class CommandController {
this.ctx.rebuildChatFromMessages();
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
} catch (error) {
if (error instanceof CompactionCancelledError) {
outcome = "cancelled";
@@ -1285,7 +1285,7 @@ export class CommandController {
this.ctx.rebuildChatFromMessages();
this.ctx.statusLine.invalidate();
this.ctx.updateEditorTopBorder();
this.ctx.ui.requestRender();
this.ctx.updateEditorBorderColor();
await this.ctx.reloadTodos();