Files
archived-orca/src/shared/agent-session/structured-agent-session-command-entry.ts
T
Brennan Benson dbb9a270d4 Keep long chats responsive: stop rescanning whole history on each status update and subagent name (#38627)
* perf(native-chat): stop rescanning whole chat history on every status update and subagent label

Status re-derived after every journal write built a map of every item body in the
chat to find conversation-command turns; index only command entries instead.
Same-named sibling subagents restarted their ordinal search at 2 for each sibling
(quadratic); resume each label's search where it stopped. Output is unchanged.

* perf(native-chat): find command turns through an indexed read on each status update

Status re-derived after every journal write still walked the whole history to
learn which turns belong to conversation commands. The host now passes a by-key
read of the same status rows (same display state, presentation membership and
stop-note projection, limited to the current /clear context), and the request,
preview and interruption readers check only the turns their backward scans reach,
the way the usage-limit reader already does. Clients without an index keep the
scan. A contract test drives real journal writes at 10x history and pins both the
constant indexed reads and the remaining per-row work.

* test(native-chat): count status body reads without Reflect.get or casts

* chore: drop accidental lockfile churn

* perf(native-chat): read the status display state once and pin indexed status parity

The host's status lookup now reads the journal's display state once per projection.
The status contract test covers the /clear boundary, an interruption verdict behind
a skipped command turn, a revised command turn, and the journal's per-row render
budget; the context-rows test expects the returned /clear sequence.

* test(native-chat): bound status work on the write that records an Orca stop

* test(native-chat): status follows a turn that becomes a command turn

* fix(native-chat): take the status lookup from the status read itself

Status read the journal's display state directly, which journals without queued
messages (test stand-ins, older wiring) do not have. The status read now returns
the by-key lookup bound to the state it rendered; readers whose status read has
none keep the scan.
2026-11-05 14:20:44 -08:00

89 lines
2.9 KiB
TypeScript

// Conversation commands (e.g. /compact) are not user requests; previews/verdicts/completion/resume skip their entries and turns.
import type {
AgentJournalItemBody,
AgentJournalRenderItem,
AgentJournalTurnLifecycle
} from './journal/agent-session-journal-types'
import { readAgentJournalTurn } from './lifecycle/agent-session-turn-record'
export function isStructuredAgentSessionCommandEntry(
body: AgentJournalItemBody | null | undefined
): boolean {
return body?.kind === 'message' && body.command !== undefined
}
export function isStructuredAgentSessionCommandTurn(
turn: Pick<AgentJournalTurnLifecycle, 'userItemId'> | null | undefined,
bodyOf: (itemId: string) => AgentJournalItemBody | null | undefined
): boolean {
return (
turn?.userItemId !== undefined && isStructuredAgentSessionCommandEntry(bodyOf(turn.userItemId))
)
}
export function structuredAgentSessionCommandTurnItemIds(
items: readonly AgentJournalRenderItem[]
): ReadonlySet<string> {
// Status re-derives on every journal write; index only the rare command entries, not every body in history.
const commands = new Map<string, AgentJournalItemBody>()
for (const item of items) {
if (isStructuredAgentSessionCommandEntry(item.body)) {
commands.set(item.itemId, item.body)
}
}
const ids = new Set<string>()
if (commands.size === 0) {
return ids
}
for (const item of items) {
if (
isStructuredAgentSessionCommandTurn(readAgentJournalTurn(item.body), (id) => commands.get(id))
) {
ids.add(item.itemId)
}
}
return ids
}
export type AgentJournalBodyLookup = (itemId: string) => AgentJournalItemBody | null | undefined
/** Command-turn membership read lazily through an indexed body lookup: only the turns a scan reaches are checked, not all history. */
export function commandTurnsByBody(
bodyOf: AgentJournalBodyLookup
): Pick<ReadonlySet<string>, 'has'> {
const known = new Map<string, boolean>()
return {
has: (itemId) => {
let command = known.get(itemId)
if (command === undefined) {
command = isStructuredAgentSessionCommandTurn(
readAgentJournalTurn(bodyOf(itemId) ?? undefined),
bodyOf
)
known.set(itemId, command)
}
return command
}
}
}
/** Lazy membership when the caller has an indexed body read; otherwise the scan of `items`. */
export function commandTurnsOf(
items: readonly AgentJournalRenderItem[],
bodyOf: AgentJournalBodyLookup | undefined
): Pick<ReadonlySet<string>, 'has'> {
return bodyOf ? commandTurnsByBody(bodyOf) : structuredAgentSessionCommandTurnItemIds(items)
}
export function isStructuredAgentSessionCommandRow(
item: Pick<AgentJournalRenderItem, 'body' | 'turnScope'>,
commandTurnItemIds: Pick<ReadonlySet<string>, 'has'>
): boolean {
const scope = item.turnScope
return (
isStructuredAgentSessionCommandEntry(item.body) ||
(scope?.kind === 'turn' && commandTurnItemIds.has(scope.turnItemId))
)
}