Skip to content

ui-react: tool-input deltas bypass frame batching and walk the whole buffer on every write #2589

Open

Description

@schlessera

Context

Text and thinking deltas are coalesced into one store write per animation frame (enqueueDelta, packages/ui-react/src/connection.ts:179-198). Tool-input deltas skip that path. The handler writes every WebSocket frame straight to the store (tool_input_delta, packages/ui-react/src/hooks/websocket-handlers/chat.ts:323-325), and handleFrame flushes pending text before each one, as it does for any non-delta frame (handleFrame, packages/ui-react/src/connection.ts:385-390). A tool whose input is large, such as a file write, streams many input_json_delta chunks, and each chunk becomes a separate store write and render.

Each write is also O(messages):

  • mutateBuffer (mutateBuffer, packages/ui-react/src/stores/chat-state.ts:1170-1200) copies the buffer. Whenever journals is set, it rebuilds a Map over every message and re-maps the whole messages array (:1177-1185). That work happens on every write, although only the last assistant message changed.
  • Subscribers to the active transcript re-render on each write:
    • ChatPage (packages/ui-react/src/components/chat/chat-page.tsx:112)
    • AnswerSuggestions (packages/ui-react/src/components/chat/answer-suggestions.tsx:35)
    • TrackerAnnouncer, which subscribes to every buffer (TrackerAnnouncer, chat-page.tsx:790-813; the subscription is at :794)
  • ChatPage computes journalVisible per visible message by slicing and scanning every later message (chat-page.tsx:713), which is quadratic in the rendered window.

(no guide)

Scope

  • Add a "tool" kind to the pending-delta queue (PendingDelta, connection.ts:147; the handler type, hooks/websocket-handlers/types.ts:16). Route tool_input_delta through enqueueDelta, and apply it in flushDeltas with appendToolInput.
  • Treat tool_input_delta as a delta in handleFrame, so it no longer forces a flush. Keep the flush-before-read rule for every other frame. tool_use_start and tool_use_complete still flush first, so input can never be attributed to the wrong tool call.
  • In mutateBuffer, skip the full journal pass when the update did not change journals and did not add or re-id a message. A write that only changes the last assistant message's parts or tool calls must not walk the whole buffer.
  • Compute journalVisible for the whole window in one reverse pass that records the last assistant index per turn id, and replace the per-message slice().some().

Out of scope

  • Changing how text and thinking deltas are batched.
  • Rendering partial tool-input JSON differently (KeyValueView, packages/ui-react/src/components/chat/tool-views.tsx:380-392).
  • Narrowing the selectors of AnswerSuggestions or TrackerAnnouncer. File that separately if a profile shows it matters after this change.
  • Subagent (nested) tool calls, which never reach the flat store.

Acceptance criteria

  • A burst of N tool_input_delta frames within one animation frame produces exactly one store write, and the tool's inputJson equals the concatenated chunks. Extend packages/ui-react/tests/delta-batching.test.ts; the test fails before the change.
  • Interleaved text_delta, tool_use_start, tool_input_delta and tool_use_complete frames keep their order: text before the tool stays before it, and input lands on the right tool call.
  • Reading the transcript in a test or handler after a tool-input frame sees the input applied (flushChatDeltas covers the tool kind).
  • The journal restamp still applies whenever it should: a new receipt in journals, and a later assistant message taking over a turn id. Existing journal tests pass, and a new test covers the takeover. The PR records the time per write for a 500-message buffer with journals set, before and after. Message identity is not the criterion, because the current code already preserves it; the waste is the full pass.
  • journalVisible values are unchanged for a fixture with repeated turn ids (retry/replay), checked against the old expression.

Verification

bun run test packages/ui-react/tests/delta-batching.test.ts
bun run test packages/ui-react/tests/chat-store.test.ts
bun run test packages/ui-react/tests
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

The wire protocol is unchanged; only client-side scheduling changes. Patch changeset for @schlessera/brain-ui-react.

Activity

  1. added
    type: fix
    area: ui-react
    priority: p2
    agent-ready
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactpriority: p2type: fix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions