fix(kiro): complete cache-only usage totals (#9753)

This commit is contained in:
backryun
2026-08-08 19:15:34 +09:00
committed by GitHub
parent b0a377e66c
commit d73a1e6fc8
2 changed files with 10 additions and 6 deletions

View File

@@ -61,7 +61,7 @@ type KiroStreamState = {
contextUsagePercentage?: number;
hasContextUsage?: boolean;
hasMeteringEvent?: boolean;
usage?: UsageSummary;
usage?: Partial<UsageSummary>;
hasReasoningContent?: boolean;
reasoningChunkCount?: number;
// Inline-thinking splitter state (populated only when thinkingExpected=true).
@@ -185,8 +185,7 @@ function resolveKiroMaxInputTokens(model: string): number {
* inflate `total_tokens`.
*/
function ensureKiroUsage(state: KiroStreamState, model: string) {
if (state.usage) return;
if (state.usage?.total_tokens !== undefined) return;
const estimatedOutputTokens =
state.totalContentLength && state.totalContentLength > 0
? Math.max(1, Math.floor(state.totalContentLength / 4))
@@ -198,11 +197,11 @@ function ensureKiroUsage(state: KiroStreamState, model: string) {
: 0;
if (estimatedTotalTokens <= 0 && estimatedOutputTokens <= 0) return;
// Without a percentage there is no total to split, so the output estimate is
// all that is known and stands on its own.
if (estimatedTotalTokens <= 0) {
state.usage = {
...state.usage,
prompt_tokens: 0,
completion_tokens: estimatedOutputTokens,
total_tokens: estimatedOutputTokens,
@@ -213,6 +212,7 @@ function ensureKiroUsage(state: KiroStreamState, model: string) {
const promptTokens = Math.max(0, estimatedTotalTokens - estimatedOutputTokens);
state.usage = {
...state.usage,
prompt_tokens: promptTokens,
completion_tokens: estimatedOutputTokens,
total_tokens: promptTokens + estimatedOutputTokens,

View File

@@ -339,8 +339,12 @@ test("KiroExecutor keeps cache tokens that arrive without input/output totals",
const chunks = parseSSEJsonChunks(await transformed.text());
const finish = chunks.find((chunk) => chunk.choices?.[0]?.finish_reason);
assert.equal(finish.usage.cache_read_input_tokens, 900);
assert.equal(finish.usage.cache_creation_input_tokens, undefined);
assert.deepEqual(finish.usage, {
prompt_tokens: 19999,
completion_tokens: 1,
total_tokens: 20000,
cache_read_input_tokens: 900,
});
});
// snake_case spellings appear on some Kiro frames; a cache count must not be