Repository navigation
Description
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. Wheneverjournalsis set, it rebuilds a Map over every message and re-maps the wholemessagesarray (: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)
ChatPagecomputesjournalVisibleper 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). Routetool_input_deltathroughenqueueDelta, and apply it influshDeltaswithappendToolInput. - Treat
tool_input_deltaas a delta inhandleFrame, so it no longer forces a flush. Keep the flush-before-read rule for every other frame.tool_use_startandtool_use_completestill 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 changejournalsand 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
journalVisiblefor the whole window in one reverse pass that records the last assistant index per turn id, and replace the per-messageslice().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
AnswerSuggestionsorTrackerAnnouncer. 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_deltaframes within one animation frame produces exactly one store write, and the tool'sinputJsonequals the concatenated chunks. Extendpackages/ui-react/tests/delta-batching.test.ts; the test fails before the change. - Interleaved
text_delta,tool_use_start,tool_input_deltaandtool_use_completeframes 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 (
flushChatDeltascovers 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 withjournalsset, before and after. Message identity is not the criterion, because the current code already preserves it; the waste is the full pass. -
journalVisiblevalues 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 lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT: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