Repository navigation
fix(provider-acp): merge per-turn PromptResponse usage into contextUsage #42101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -414,6 +414,69 @@ export function acpPerTurnPromptUsage( | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Merges a `usage_update` context snapshot (`usedTokens` / `maxTokens` / `cost`) on top of an | ||
| * existing `ThreadTokenUsageSnapshot`, preserving per-turn token counters previously merged from | ||
| * `PromptResponse.usage`. | ||
| */ | ||
| export function mergeAcpUsageUpdate( | ||
| previous: ThreadTokenUsageSnapshot | null, | ||
| update: ThreadTokenUsageSnapshot, | ||
| ): ThreadTokenUsageSnapshot { | ||
| if (previous === null) return update; | ||
| return { | ||
| ...previous, | ||
| ...update, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Merges a normalized per-turn `TurnTokenUsage` (from `PromptResponse.usage` on flavors with | ||
| * `reportsPerTurnPromptUsage`) into the thread's `ThreadTokenUsageSnapshot`, populating both | ||
| * `last*` per-turn counters and cumulative session totals while preserving `usedTokens` / | ||
| * `maxTokens` / `cost` from `usage_update`. | ||
| */ | ||
| export function mergeAcpTurnTokenUsage( | ||
| previous: ThreadTokenUsageSnapshot | null, | ||
| turnUsage: TurnTokenUsage, | ||
| ): ThreadTokenUsageSnapshot | null { | ||
| if ( | ||
| turnUsage.usageStatus === "unavailable" || | ||
| turnUsage.inputTokens === undefined || | ||
| turnUsage.outputTokens === undefined | ||
| ) { | ||
| return previous; | ||
| } | ||
| const lastInputTokens = turnUsage.inputTokens; | ||
| const lastOutputTokens = turnUsage.outputTokens; | ||
| const lastUsedTokens = lastInputTokens + lastOutputTokens; | ||
| const cachedInputTokens = | ||
| previous?.cachedInputTokens === undefined && turnUsage.cachedInputTokens === undefined | ||
| ? undefined | ||
| : (previous?.cachedInputTokens ?? 0) + (turnUsage.cachedInputTokens ?? 0); | ||
| const reasoningOutputTokens = | ||
| previous?.reasoningOutputTokens === undefined && turnUsage.reasoningTokens === undefined | ||
| ? undefined | ||
| : (previous?.reasoningOutputTokens ?? 0) + (turnUsage.reasoningTokens ?? 0); | ||
| return { | ||
| ...previous, | ||
| usedTokens: previous?.usedTokens ?? lastUsedTokens, | ||
| inputTokens: (previous?.inputTokens ?? 0) + lastInputTokens, | ||
| outputTokens: (previous?.outputTokens ?? 0) + lastOutputTokens, | ||
| ...(cachedInputTokens === undefined ? {} : { cachedInputTokens }), | ||
| ...(reasoningOutputTokens === undefined ? {} : { reasoningOutputTokens }), | ||
| lastUsedTokens, | ||
| lastInputTokens, | ||
| lastOutputTokens, | ||
| ...(turnUsage.cachedInputTokens === undefined | ||
| ? {} | ||
| : { lastCachedInputTokens: turnUsage.cachedInputTokens }), | ||
| ...(turnUsage.reasoningTokens === undefined | ||
| ? {} | ||
| : { lastReasoningOutputTokens: turnUsage.reasoningTokens }), | ||
| }; | ||
| } | ||
|
|
||
| interface AcpPromptOutcomeFailure { | ||
| readonly failure: OrchestrationV2ProviderFailure; | ||
| readonly threadDisposition: "reusable" | "broken"; | ||
|
|
@@ -5974,11 +6037,15 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | |
| ); | ||
| if (stateEvent === undefined) return; | ||
| if (stateEvent._tag === "UsageUpdated") { | ||
| yield* Ref.update(contextUsageBySessionId, (current) => | ||
| new Map(current).set(notification.sessionId, stateEvent.usage), | ||
| ); | ||
| const mergedUsage = yield* Ref.modify(contextUsageBySessionId, (current) => { | ||
| const merged = mergeAcpUsageUpdate( | ||
| current.get(notification.sessionId) ?? null, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve restored counters on the first usage update. After a restart, Line 9920 can initialize Seed the session cache from the restored snapshot, or use the matching active snapshot when the cache has no entry. Add a focused case for a restored snapshot followed by a usage update.
|
||
| stateEvent.usage, | ||
| ); | ||
| return [merged, new Map(current).set(notification.sessionId, merged)] as const; | ||
| }); | ||
| if (context?.nativeThreadId === notification.sessionId) { | ||
| context.contextUsage = stateEvent.usage; | ||
| context.contextUsage = mergedUsage; | ||
| } | ||
| } else { | ||
| const metadata = yield* Ref.modify(nativeMetadataBySessionId, (current) => { | ||
|
|
@@ -8901,6 +8968,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( | |
| } | ||
| yield* reportAgentModeChanges(context.input.threadId); | ||
| const turn = providerTurnPayload(context, settledStatus, now); | ||
| if (turn.turnTokenUsage !== undefined) { | ||
| const mergedUsage = mergeAcpTurnTokenUsage(context.contextUsage, turn.turnTokenUsage); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retain session totals across context compaction. Completed compaction clears Keep cumulative accounting separate from context-window invalidation. Compaction must invalidate the window estimate without deleting session totals. Extend the existing accumulation case to cover that reset.
|
||
| if (mergedUsage !== context.contextUsage) { | ||
| context.contextUsage = mergedUsage; | ||
| if (mergedUsage !== null) { | ||
| yield* Ref.update(contextUsageBySessionId, (current) => | ||
| new Map(current).set(context.nativeThreadId, mergedUsage), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| yield* emitProviderEvent({ | ||
| type: "provider_turn.updated", | ||
| driver, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear optional last-turn counters when the current turn omits them.
If one turn reports cached or reasoning tokens and the next turn omits those counters,
...previousretains the first turn'slastCachedInputTokensandlastReasoningOutputTokens. The otherlast*fields then describe the second turn.Remove the previous optional
last*fields before adding the current turn's counters. Keep the cumulative counters unchanged. Extend the existing multi-turn test with this transition.