Skip to content

fix(observe): reduceConversation copies a map only when a record writes it - #301

Merged
xxchan merged 1 commit into
mainfrom
fix/conversation-copy-on-write
Oct 10, 2026
Merged

xxchan merged 1 commit into
mainfrom
fix/conversation-copy-on-write

Conversation

@xxchan

@xxchan xxchan commented Oct 10, 2026

Copy link
Copy Markdown
Member

Closes #300.

reduceConversation copied inputs, drops, requests and actions into new Maps on every record, though most records (assistant frames, tool calls, deltas) change none of them. inputs and requests only grow over a session, so folding a long session cost records × inputs. Ferry measured reduceConversation as 4.7 s of 7.1 s self time for a 600-turn session in a jitless run (close to JavaScriptCore on iOS).

Change: each map is wrapped in a small copy-on-write helper. It is copied on its first write in a record, and a record that writes nothing returns the previous maps unchanged, keeping their identity, so a consumer can tell the conversation didn't change. The fold stays a pure function, and the previous state is never mutated. cursors is still copied; it has one entry per stream.

Measured locally: 36,000 synthetic records (600 turns of a prompt, its acceptance and 58 text frames), conversationOf:

before after
2.4 s 0.13 s

Tests: a new case in tests/conversation.test.ts checks that a text frame keeps all four maps by identity, and that a later write copies without touching the previous state. It fails without the fix. Full suite, lint and typecheck pass locally.

🤖 Generated with Claude Code

…es it

reduceConversation copied inputs, drops, requests and actions into new
Maps on every record, though most records (assistant frames, tool calls,
deltas) change none of them; inputs and requests only grow, so folding a
long session was quadratic. Each map is now copied on its first write in a
record, and a record that writes nothing keeps the previous maps (and their
identity). The fold stays pure; the previous state is never mutated.

36,000 synthetic records (600 turns): 2.4 s -> 0.13 s locally. Ferry
measured 6.7 s -> 2.1-2.4 s for its whole iOS-like (jitless) fold.

Closes #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xxchan
xxchan merged commit b214f4e into main Oct 10, 2026
17 checks passed
@xxchan
xxchan deleted the fix/conversation-copy-on-write branch October 10, 2026 05:21
@xxchan xxchan mentioned this pull request Oct 10, 2026
xxchan added a commit that referenced this pull request Oct 10, 2026
Release OAR and CLI 0.52.0. This PR changes only the two package version
fields.

**Added: streamed tool arguments** (#299, #302)
- claude streams a tool call's arguments while the model writes them. A
streamed call now starts with `tool_call_started` without input. Each
fragment arrives as a new append-only `tool_call_input_delta { callId,
delta }`, where `delta` is claude's raw `partial_json` text and may be
incomplete JSON. The complete arguments then arrive as
`tool_call_input`, replacing the fragments.
- `SessionView`: a tool part's `input` grows with the fragments and
carries `inputPartial: true` until the complete input arrives. An
interrupted call keeps `inputPartial`. OAR never parses partial JSON; a
host that renders early uses a tolerant parser and acts only once
`inputPartial` clears.
- Only claude streams arguments. Its subagents' tool calls, codex and
the ACP runtimes still give the whole input at once.

**Fixed**
- **`reduceConversation` no longer copies its maps on every record**
(#300, #301). A map is copied only on its first write in a record, and a
record that changes nothing keeps the previous maps, and their identity.
A 600-turn session (35,400 records) folds about 4.5× faster: 2.4 s →
0.52 s with JIT, 2.7 s → 0.60 s without.

**Behavior changes**
- A claude `tool_call_started` may now carry no `input`; read the later
`tool_call_input` (or the view) before parsing arguments. Hosts that
already handle ACP runtimes, whose input also arrives later, are
unaffected.
- `classifyTool` should be given only complete input; `tool-groups`
already skips partial input.
- Hosts with an exhaustive `switch` over event kinds need a case for
`tool_call_input_delta`.

Validation: #301 and #302 each passed all 17 checks on their final heads
(`5885611`, `a712e02`). #301 merged with a tree equal to its CI tree.
#302 was checked on the previous main; it and #301 touch different
files, so the combination was verified locally (typecheck, lint, full
tests: 1507 passed), and the merged tree equals that verified tree.
Lookout verified #301 with a differential fold of 61,429 steps (0
differences) and #302 on four cases against the real claude CLI with a
mock provider (two tools streaming in one message, an interrupt
mid-argument, a resume, a subagent). After this PR passes CI and merges,
tag its exact squash commit as `v0.52.0` to trigger npm trusted
publishing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

observe: reduceConversation copies four Maps per record, so a long session folds in quadratic time

1 participant