diff --git a/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection.ts b/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection.ts index 14e22c4c0a4..278d29bfd46 100644 --- a/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection.ts +++ b/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection.ts @@ -1,6 +1,10 @@ -import { latestNativeChatUsageLimitCut } from '../../../../shared/native-chat/native-chat-usage-limit-cut' import { - hostUsageLimitCut, + latestNativeChatUsageLimitCut, + type NativeChatUsageLimitCut +} from '../../../../shared/native-chat/native-chat-usage-limit-cut' +import { + usageLimitContinueChain, + usageLimitContinueIdentity, type HostUsageLimitCut } from '../recovery/structured-agent-session-usage-limit-chain' import { agentJournalSubmissionKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' @@ -39,6 +43,34 @@ export type StructuredAgentSessionJournalProjection = { stopping: boolean } +/** The chain reads only history before the cut, plus the cut turn's opener and its send's acceptance; + * a rebuild that changed none of those reuses it instead of walking the journal again. */ +function usageLimitCutWithChain( + journal: AgentSessionJournal, + snapshot: ReturnType, + cut: NativeChatUsageLimitCut | null, + previous: HostUsageLimitCut | null | undefined +): HostUsageLimitCut | null { + if (!cut) { + return null + } + // A late receipt can attribute the opener after the turn ended: the turn row or the newest send changes. + const newest = snapshot.submissions.at(-1) + const chainKey = JSON.stringify([ + usageLimitContinueIdentity(cut), + journal.item(cut.turnItemId)?.revision, + snapshot.submissions.length, + newest?.clientMessageId, + newest?.dispatchState, + newest?.providerItemId + ]) + const chain = + previous?.chainKey === chainKey + ? previous.chain + : usageLimitContinueChain(snapshot.items, snapshot.submissions, cut) + return { ...cut, chain, chainKey } +} + export class StructuredAgentSessionJournalProjections { // Key by journal lifetime; task-progress frames must not rescan unchanged conversations. private readonly byJournal = new WeakMap< @@ -123,7 +155,8 @@ export class StructuredAgentSessionJournalProjections { projection = { ...cursor, contextId, - usageLimitCut: hostUsageLimitCut( + usageLimitCut: usageLimitCutWithChain( + journal, snapshot, latestNativeChatUsageLimitCut(snapshot.items, snapshot.submissions, journal.itemBody), sameContext?.usageLimitCut diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.test.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.test.ts index e1d1531ce75..228fd505366 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.test.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.test.ts @@ -7,7 +7,6 @@ import type { import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure' import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' import { - hostUsageLimitCut, usageLimitContinueChain, usageLimitContinueMessageId, usageLimitContinueTag @@ -136,35 +135,6 @@ it('reads only the chain, never the older conversation, at N and 10N rows of his expect(small.reads).toBeLessThanOrEqual(40) }) -it('rebuilding the status for the same cut reads nothing, however long its turn ran', () => { - const measure = (outputRows: number) => { - const { items, submissions, cut } = conversation(10, 0) - // A person's limited turn with a long output: its chain is 0, and stays 0 for this cut. - const limitRow = items.pop()! - for (let index = 0; index < outputRows; index += 1) { - items.push({ - itemId: `output-${index}`, - revision: 0, - sequence: items.length + 1, - observedAt: items.length, - turnScope: { kind: 'turn', turnItemId: cut.turnItemId }, - body: { kind: 'message', role: 'assistant', blocks: [] } - }) - } - items.push(limitRow) - const first = hostUsageLimitCut({ items, submissions }, cut) - const reads = new Set() - const rebuilt = hostUsageLimitCut( - { items: countedItems(items, reads), submissions: countedSubmissions(submissions, reads) }, - cut, - first - ) - return { chain: rebuilt?.chain, reads: reads.size } - } - expect(measure(1_000)).toEqual({ chain: 0, reads: 0 }) - expect(measure(10_000)).toEqual({ chain: 0, reads: 0 }) -}) - it('counts a continue across a slash-command turn run after the cut it answers', () => { const { items, submissions, cut } = conversation(2, 1) // A /compact between the person's limited turn and the automatic continue, as the latest-turn read skips it. diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.ts index 681731f18a7..c4bcae29a03 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-usage-limit-chain.ts @@ -163,26 +163,20 @@ export function usageLimitContinueChain( return chain } -/** A cut as the host's sweep reads it: with the chain that led to it. */ -export type HostUsageLimitCut = NativeChatUsageLimitCut & { chain: number } +/** A cut as the host's sweep reads it: with the chain that led to it, and what that chain was read + * from so a rebuild can tell whether it still holds. */ +export type HostUsageLimitCut = NativeChatUsageLimitCut & { chain: number; chainKey?: string } -/** The chain depends only on history before the cut, so a rebuild for the same cut reuses it. */ export function hostUsageLimitCut( snapshot: { items: readonly AgentJournalRenderItem[] submissions: readonly ChainSubmission[] }, - cut: NativeChatUsageLimitCut | null, - previous?: HostUsageLimitCut | null + cut: NativeChatUsageLimitCut | null ): HostUsageLimitCut | null { - if (!cut) { - return null - } - const chain = - previous && usageLimitContinueIdentity(previous) === usageLimitContinueIdentity(cut) - ? previous.chain - : usageLimitContinueChain(snapshot.items, snapshot.submissions, cut) - return { ...cut, chain } + return cut + ? { ...cut, chain: usageLimitContinueChain(snapshot.items, snapshot.submissions, cut) } + : null } /** Reads a chat's saved conversation without opening it, as main does for unopened chats. */