Files
orca/src/shared/native-chat-tool-pairing.ts
T
Brennan Benson 91e6e1f355 fix(native-chat): collapse a finished turn to its answer (#22029)
* fix(native-chat): collapse a finished turn to its answer

A finished turn's "Worked for N" row hid the turn's tool runs and nothing
else. Every sentence the agent said on the way to its answer stayed in the
transcript, so the resting state of a long chat was the narration, not the
reply — one 16m 56s review turn left 21 assistant messages and roughly
seven screens of scrolling behind a control that reads as if it had put
the work away.

The fold's unit is now the turn. A settled turn draws its prompt, its
duration, and its answer; the narration and activity that produced it sit
behind the caret. The answer is the turn's last assistant row that renders
prose — derived, because the journal carries no marker saying which message
is the reply.

Collapsed stays derived rather than stored: nothing closes the disclosure
when a turn ends, it arrives closed because the turn gained a duration. A
running turn therefore folds nothing and the reader watches the work as it
happens, which is what already happened and is now stated rather than
inherited.

Rows that outlive the turn that started them stay outside the fold — a
spawn roster and a background task are often the only record of how that
work ended. So does the reader's own message, question receipts, and the
turn's diff rollup. A turn that produced no prose folds whole, its status
row standing as the anchor.

Two presentation changes come with it, both about the opened view:

- A settled run's header was a call count followed by a monospace list of
  tool names and arguments. It is now one sentence in the transcript's own
  type — "Read 7 files, ran 17 commands, and searched 4 times" — built on
  the tool-category vocabulary that already picks the row's glyph, so the
  words and the icon cannot claim different things. A run of one command
  keeps that command as its header.
- A tool call now owns its result instead of standing beside a separate
  `Result` row, so an opened run lists the work rather than twice as many
  rows half of which say `Result`. Output is one more click. Pairing is
  positional — a result answers the most recent unanswered call — because
  result blocks carry no call identifier to match on.

Command previews also lose the `/bin/zsh -lc "…"` wrapper they all opened
with. The unwrap happens inside `summarizeToolInput`, before truncation,
because the clip at 80 characters removes the closing quote that proves the
wrapper; one site fixes the header, the rows, and the running label.

Measured on a real session journal at 1200x900: the turn above goes from
6,300px across 51 rows to 452px across 2, the whole session from 8,151px
to 2,138px, and the same turn opened from 18,540px to 11,131px.

The fold derivation lives in `src/shared` so the mobile transcript can read
the same rule; wiring mobile's list to it is not part of this change.

* fix(native-chat): preserve FIFO tool result pairing

* fix(native-chat): keep tools collapsed when opening turn

* test(native-chat): clarify independent tool disclosures
2026-09-21 12:13:30 -07:00

61 lines
2.1 KiB
TypeScript

// Which result belongs to which call inside one run.
//
// A run's blocks arrive as a flat stream of calls and results, and the renderer
// used to draw both: every call line was followed by a `Result` line of its own.
// That doubles the height of an opened run and puts the least interesting row —
// a result whose preview is usually the first line of stdout — at the same level
// as the command that produced it. Pairing lets the call own its output, so the
// run reads as the work it did.
//
// Pairing is positional, the same FIFO rule `dropUnattributableToolResults`
// already uses to decide a result is attributable at all: a result answers the
// oldest call that has not been answered yet.
import {
isToolCallBlock,
isToolResultBlock,
type NativeChatBlock,
type NativeChatToolCallBlock,
type NativeChatToolResultBlock
} from './native-chat-types'
export type NativeChatToolPairing = {
/** The result each call owns. A call still running has none. */
resultByCall: ReadonlyMap<NativeChatToolCallBlock, NativeChatToolResultBlock>
/** Results a call now owns, so the run does not also draw them as rows. */
pairedResults: ReadonlySet<NativeChatBlock>
}
export const NO_NATIVE_CHAT_TOOL_PAIRING: NativeChatToolPairing = {
resultByCall: new Map(),
pairedResults: new Set()
}
export function pairNativeChatToolResults(
blocks: readonly NativeChatBlock[]
): NativeChatToolPairing {
const resultByCall = new Map<NativeChatToolCallBlock, NativeChatToolResultBlock>()
const pairedResults = new Set<NativeChatBlock>()
const unanswered: NativeChatToolCallBlock[] = []
let answered = 0
for (const block of blocks) {
if (isToolCallBlock(block)) {
unanswered.push(block)
continue
}
if (!isToolResultBlock(block)) {
continue
}
// Results carry no call id, so the journal's FIFO order is the only stable
// attribution available when calls are interleaved.
const call = unanswered[answered]
if (call === undefined) {
continue
}
answered += 1
resultByCall.set(call, block)
pairedResults.add(block)
}
return { resultByCall, pairedResults }
}