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 278d29bfd46..a779d91aae9 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 @@ -3,9 +3,10 @@ import { type NativeChatUsageLimitCut } from '../../../../shared/native-chat/native-chat-usage-limit-cut' import { - usageLimitContinueChain, + readUsageLimitContinueChain, usageLimitContinueIdentity, - type HostUsageLimitCut + type HostUsageLimitCut, + type UsageLimitContinueChainRead } from '../recovery/structured-agent-session-usage-limit-chain' import { agentJournalSubmissionKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' import { isRootAgentJournalItem } from '../../../../shared/agent-session/journal/agent-session-journal-producer' @@ -43,8 +44,25 @@ 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. */ +/** Everything a chain read could have seen change: the turn rows it looked up and the join evidence + * of every submission from the oldest it examined. Rows before the cut are otherwise fixed. */ +function usageLimitChainKey( + journal: AgentSessionJournal, + snapshot: ReturnType, + cut: NativeChatUsageLimitCut, + read: Omit +): string { + return JSON.stringify([ + usageLimitContinueIdentity(cut), + read.turnsRead.map((itemId) => journal.item(itemId)?.revision), + snapshot.submissions.length, + snapshot.submissions + .slice(read.submissionsFrom) + .map((entry) => [entry.clientMessageId, entry.dispatchState, entry.providerItemId]) + ]) +} + +/** A rebuild whose chain inputs are unchanged reuses the count instead of walking the journal again. */ function usageLimitCutWithChain( journal: AgentSessionJournal, snapshot: ReturnType, @@ -54,21 +72,16 @@ function usageLimitCutWithChain( 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 } + const reused = previous?.chainRead + if (reused && usageLimitChainKey(journal, snapshot, cut, reused) === reused.key) { + return { ...cut, chain: previous.chain, chainRead: reused } + } + const read = readUsageLimitContinueChain(snapshot.items, snapshot.submissions, cut) + return { + ...cut, + chain: read.chain, + chainRead: { ...read, key: usageLimitChainKey(journal, snapshot, cut, read) } + } } export class StructuredAgentSessionJournalProjections { diff --git a/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-usage-limit-chain.test.ts b/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-usage-limit-chain.test.ts index f765386df3f..0e4c3577a3f 100644 --- a/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-usage-limit-chain.test.ts +++ b/src/main/native-chat/agent-session-wire/observations/structured-agent-session-status-usage-limit-chain.test.ts @@ -4,10 +4,13 @@ import { agentSessionFailureFact } from '../../../../shared/agent-session/failur import { agentSessionFailureWords } from '../../../../shared/agent-session/failures/agent-session-failure-words' import { createFixture } from '../../agent-session-journal/store-lifecycle/journal-store-contract-test-fixture' import type { AgentSessionJournal } from '../../agent-session-journal/journal-store' -import { agentJournalItemKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key' +import { + agentJournalItemKey, + agentJournalSubmissionKey +} from '../../../../shared/agent-session/journal/agent-session-journal-item-key' import type * as UsageLimitChain from '../recovery/structured-agent-session-usage-limit-chain' import { - usageLimitContinueChain, + readUsageLimitContinueChain, usageLimitContinueMessageId, usageLimitContinueTag } from '../recovery/structured-agent-session-usage-limit-chain' @@ -15,13 +18,13 @@ import { StructuredAgentSessionJournalProjections } from './structured-agent-ses vi.mock('../recovery/structured-agent-session-usage-limit-chain', async (importOriginal) => { const actual = await importOriginal() - return { ...actual, usageLimitContinueChain: vi.fn(actual.usageLimitContinueChain) } + return { ...actual, readUsageLimitContinueChain: vi.fn(actual.readUsageLimitContinueChain) } }) const fixture = createFixture() -const derivations = () => vi.mocked(usageLimitContinueChain).mock.calls.length +const derivations = () => vi.mocked(readUsageLimitContinueChain).mock.calls.length beforeEach(() => { - vi.mocked(usageLimitContinueChain).mockClear() + vi.mocked(readUsageLimitContinueChain).mockClear() }) async function limitedTurn( @@ -100,20 +103,20 @@ it.each([100, 1_000])( } ) -it('counts an automatic continue whose receipt attributes its opener after the turn ended', async () => { - const journal = await fixture.open() - const projections = new StructuredAgentSessionJournalProjections() - const first = await limitedTurn(journal, 'first', 0) +async function hostContinue( + journal: AgentSessionJournal, + cut: { turnItemId: string; resetsAt: number }, + at: number +) { const clientMessageId = usageLimitContinueMessageId( - 2_000, - usageLimitContinueTag({ turnItemId: first.turnScope.turnItemId, resetsAt: 60_000 }), + at, + usageLimitContinueTag(cut), '0123456789abcdef' ) - const body = { kind: 'message' as const, role: 'user' as const, blocks: [] } await journal.appendSubmission({ clientMessageId, - payloadFingerprint: 'continue', - body, + payloadFingerprint: `continue-${at}`, + body: { kind: 'message', role: 'user', blocks: [] }, origin: 'host', fence: 1 }) @@ -123,15 +126,59 @@ it('counts an automatic continue whose receipt attributes its opener after the t turnScope: AGENT_JOURNAL_THREAD_SCOPE, fence: 1 }) - await journal.markPendingSubmissionsUnknown(1) - // The provider's echo opens the turn before any receipt names it. - const echo = { provider: 'orca' as const, clientMessageId: 'provider-echo' } - await journal.appendItem(echo, body, { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE }) - await limitedTurn(journal, 'second', 0, { - userItemId: agentJournalItemKey(echo), - resetsAt: 120_000 - }) - expect(projections.read(journal, null).usageLimitCut?.chain).toBe(0) - await journal.acceptLate({ clientMessageId, state: 'accepted', providerIdentity: echo, fence: 1 }) - expect(projections.read(journal, null).usageLimitCut?.chain).toBe(1) -}) + return clientMessageId +} + +it.each([ + ['the newest link', 0], + ['an older link', 1] +])( + 'recounts when a late receipt attributes the opener of %s after its turn ended', + async (_, laterLinks) => { + const journal = await fixture.open() + const projections = new StructuredAgentSessionJournalProjections() + const first = await limitedTurn(journal, 'first', 0) + const late = await hostContinue( + journal, + { turnItemId: first.turnScope.turnItemId, resetsAt: 60_000 }, + 2_000 + ) + await journal.markPendingSubmissionsUnknown(1) + // The provider's echo opens the turn before any receipt names it. + const echo = { provider: 'orca' as const, clientMessageId: 'provider-echo' } + await journal.appendItem( + echo, + { kind: 'message', role: 'user', blocks: [] }, + { fence: 1, turnScope: AGENT_JOURNAL_THREAD_SCOPE } + ) + let cut = await limitedTurn(journal, 'second', 0, { + userItemId: agentJournalItemKey(echo), + resetsAt: 120_000 + }) + for (let link = 0; link < laterLinks; link += 1) { + const sent = await hostContinue( + journal, + { turnItemId: cut.turnScope.turnItemId, resetsAt: 120_000 + link * 60_000 }, + 3_000 + link + ) + await journal.resolveDispatch({ + clientMessageId: sent, + state: 'accepted', + providerIdentity: null, + fence: 1 + }) + cut = await limitedTurn(journal, `later-${link}`, 0, { + userItemId: agentJournalSubmissionKey(sent), + resetsAt: 180_000 + link * 60_000 + }) + } + expect(projections.read(journal, null).usageLimitCut?.chain).toBe(laterLinks) + await journal.acceptLate({ + clientMessageId: late, + state: 'accepted', + providerIdentity: echo, + fence: 1 + }) + expect(projections.read(journal, null).usageLimitCut?.chain).toBe(laterLinks + 1) + } +) 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 c4bcae29a03..2d066d8a097 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 @@ -60,7 +60,12 @@ function openerOf( } /** Whether the turn at `index` was opened by a slash command, read from its opener just before it. */ -function isCommandTurnAt(items: readonly AgentJournalRenderItem[], index: number): boolean { +function isCommandTurnAt( + items: readonly AgentJournalRenderItem[], + index: number, + turnsRead: string[] +): boolean { + turnsRead.push(items[index]!.itemId) const userItemId = readAgentJournalTurn(items[index]!.body)?.userItemId for (let before = index - 1; userItemId && before >= 0; before -= 1) { const item = items[before]! @@ -79,7 +84,8 @@ function isCommandTurnAt(items: readonly AgentJournalRenderItem[], index: number function previousTurnIndex( items: readonly AgentJournalRenderItem[], turnIndex: number, - openerKeys: ReadonlySet + openerKeys: ReadonlySet, + turnsRead: string[] ): number { for (let index = turnIndex - 1; index >= 0; index -= 1) { const item = items[index]! @@ -91,7 +97,7 @@ function previousTurnIndex( continue } if (readAgentJournalTurn(item.body)) { - if (isCommandTurnAt(items, index)) { + if (isCommandTurnAt(items, index, turnsRead)) { continue } return index @@ -123,31 +129,44 @@ function usageLimitBetween( return null } +/** What a chain was read from: the turn rows whose opener it looked up and every submission from + * the oldest it examined, so a reader can tell whether the count still holds. */ +export type UsageLimitContinueChainRead = { + chain: number + turnsRead: readonly string[] + submissionsFrom: number +} + /** How many automatic continues in a row ended in another usage limit, read from the journal so a * restart or reopen agrees. Each step needs the exact join: the turn opened by a host submission * whose message id names the cut before it. Walks back from the end, so the work is the chain's * own tail, never the conversation's length. */ -export function usageLimitContinueChain( +export function readUsageLimitContinueChain( items: readonly AgentJournalRenderItem[], submissions: readonly ChainSubmission[], cut: Pick -): number { +): UsageLimitContinueChainRead { let turnIndex = items.length - 1 while (turnIndex >= 0 && items[turnIndex]!.itemId !== cut.turnItemId) { turnIndex -= 1 } + const turnsRead: string[] = [] let submissionBound = submissions.length + let submissionsFrom = submissions.length let chain = 0 while (chain < USAGE_LIMIT_CONTINUE_CHAIN_LIMIT && turnIndex >= 0) { + turnsRead.push(items[turnIndex]!.itemId) const userItemId = readAgentJournalTurn(items[turnIndex]!.body)?.userItemId const openerIndex = userItemId ? openerOf(submissions, userItemId, submissionBound) : -1 + // A missed join searched every earlier submission; a late receipt on any of them can complete it. + submissionsFrom = userItemId ? Math.max(openerIndex, 0) : submissionsFrom const opener = openerIndex >= 0 ? submissions[openerIndex]! : null const tag = opener?.origin === 'host' ? tagOfMessageId(opener.clientMessageId) : null if (!opener || !tag || !userItemId) { break } const openerKeys = new Set([userItemId, agentJournalSubmissionKey(opener.clientMessageId)]) - const previous = previousTurnIndex(items, turnIndex, openerKeys) + const previous = previousTurnIndex(items, turnIndex, openerKeys, turnsRead) const limit = previous >= 0 ? usageLimitBetween(items, previous, turnIndex) : null if ( limit?.resetsAt === undefined || @@ -160,12 +179,19 @@ export function usageLimitContinueChain( turnIndex = previous submissionBound = openerIndex } - return chain + return { chain, turnsRead, submissionsFrom } } +export const usageLimitContinueChain = ( + ...args: Parameters +): number => readUsageLimitContinueChain(...args).chain + /** 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 } +export type HostUsageLimitCut = NativeChatUsageLimitCut & { + chain: number + chainRead?: UsageLimitContinueChainRead & { key: string } +} export function hostUsageLimitCut( snapshot: {