Skip to content

Commit 0a95f1f

Browse files
fix(opencode): OpenCode 1.x threads report context-window usage
The 1.x adapter only set per-turn token totals, never tokenUsage, so the thread's context window had nothing to show. Terminal turns now report the last step's context fill, like the 2.x adapter, with the model's window from the session's catalog lookup when it has one. Fixes #8388 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dd95547 commit 0a95f1f

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎packages/provider-opencode/src/server/OpenCodeRuntime.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export interface ParsedOpenCodeModelSlug {
267267
readonly modelID: string;
268268
}
269269

270-
export type OpenCodeVariantCache = Map<string, ReadonlySet<string> | undefined>;
270+
export type OpenCodeVariantCache = Map<
271+
string,
272+
{ readonly variants: ReadonlySet<string>; readonly contextWindow: number | undefined } | undefined
273+
>;
271274

272275
export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
273276
client: OpencodeClient,
@@ -281,7 +284,7 @@ export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
281284
const key = `${model.providerID}/${model.modelID}`;
282285
const cached = cache?.get(key);
283286
if (cache?.has(key)) {
284-
return cached !== undefined && !cached.has(variant) ? undefined : variant;
287+
return cached !== undefined && !cached.variants.has(variant) ? undefined : variant;
285288
}
286289

287290
// Only a confirmed catalog mismatch can invalidate a persisted selection.
@@ -299,8 +302,16 @@ export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
299302
?.models[model.modelID];
300303
const variants = catalogModel ? new Set(Object.keys(catalogModel.variants ?? {})) : undefined;
301304
if (cache !== undefined) {
302-
// Keep only this model's names; provider.list can return several megabytes.
303-
cache.set(key, variants);
305+
// Keep only what this model needs; provider.list can return several megabytes.
306+
cache.set(
307+
key,
308+
catalogModel && variants
309+
? {
310+
variants,
311+
contextWindow: catalogModel.limit?.input ?? catalogModel.limit?.context,
312+
}
313+
: undefined,
314+
);
304315
}
305316
return variants !== undefined && !variants.has(variant) ? undefined : variant;
306317
});

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ interface OpenCodeTurnTokenUsageAccumulator {
226226
reasoningTokens: number;
227227
hasSubagents: boolean;
228228
complete: boolean;
229+
lastStep: OpenCodeStepUsage | null;
229230
}
230231

231232
function makeOpenCodeTurnTokenUsageAccumulator(): OpenCodeTurnTokenUsageAccumulator {
@@ -241,6 +242,7 @@ function makeOpenCodeTurnTokenUsageAccumulator(): OpenCodeTurnTokenUsageAccumula
241242
reasoningTokens: 0,
242243
hasSubagents: false,
243244
complete: true,
245+
lastStep: null,
244246
};
245247
}
246248

@@ -255,6 +257,29 @@ function accumulateOpenCodeStepUsage(
255257
accumulator.cacheCreationTokens += part.tokens.cache.write;
256258
accumulator.outputTokens += part.tokens.output + part.tokens.reasoning;
257259
accumulator.reasoningTokens += part.tokens.reasoning;
260+
accumulator.lastStep = part;
261+
}
262+
263+
// The last step's prompt plus output is the context the next request starts from.
264+
function openCodeContextUsage(
265+
lastStep: OpenCodeStepUsage | null,
266+
contextWindow: number | undefined,
267+
completedAt: DateTime.Utc,
268+
): Pick<OrchestrationV2ProviderTurn, "tokenUsage"> {
269+
if (lastStep === null) return {};
270+
const { tokens } = lastStep;
271+
const inputTokens = tokens.input + tokens.cache.read + tokens.cache.write;
272+
return {
273+
tokenUsage: {
274+
usedTokens: inputTokens + tokens.output,
275+
maxTokens: contextWindow ?? null,
276+
inputTokens,
277+
cachedInputTokens: tokens.cache.read,
278+
outputTokens: tokens.output,
279+
reasoningOutputTokens: tokens.reasoning,
280+
updatedAt: DateTime.formatIso(completedAt),
281+
},
282+
};
258283
}
259284

260285
interface ActiveOpenCodeTurn {
@@ -1179,6 +1204,11 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
11791204
reasoningTokens: turn.usage.reasoningTokens,
11801205
hasSubagents: turn.usage.hasSubagents,
11811206
},
1207+
...openCodeContextUsage(
1208+
turn.usage.lastStep,
1209+
variantCache.get(turn.modelSelection.model.trim())?.contextWindow,
1210+
completedAt,
1211+
),
11821212
}),
11831213
};
11841214
Object.assign(turn.providerTurn, providerTurn);

0 commit comments

Comments
 (0)