Skip to content

Commit eb14ddb

Browse files
fix(opencode): OpenCode 1.x threads report context-window usage (#36430)
1 parent ab9e6a2 commit eb14ddb

3 files changed

Lines changed: 88 additions & 18 deletions

File tree

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,24 +267,23 @@ export interface ParsedOpenCodeModelSlug {
267267
readonly modelID: string;
268268
}
269269

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

272-
export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
275+
export type OpenCodeVariantCache = Map<string, OpenCodeCatalogModel | undefined>;
276+
277+
/** The model's catalog entry; a failed read returns undefined and is not cached. */
278+
export const readOpenCodeCatalogModel = Effect.fnUntraced(function* (
273279
client: OpencodeClient,
274-
model: ParsedOpenCodeModelSlug | undefined,
275-
variant: string | undefined,
280+
model: ParsedOpenCodeModelSlug,
276281
abortSignal?: AbortSignal,
277282
cache?: OpenCodeVariantCache,
278283
) {
279-
if (!variant || !model) return variant;
280-
281284
const key = `${model.providerID}/${model.modelID}`;
282-
const cached = cache?.get(key);
283-
if (cache?.has(key)) {
284-
return cached !== undefined && !cached.has(variant) ? undefined : variant;
285-
}
285+
if (cache?.has(key)) return cache.get(key);
286286

287-
// Only a confirmed catalog mismatch can invalidate a persisted selection.
288287
const response = yield* runOpenCodeSdk("provider.list", (signal) =>
289288
client.provider.list(undefined, {
290289
signal: abortSignal ? AbortSignal.any([signal, abortSignal]) : signal,
@@ -293,16 +292,30 @@ export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
293292
Effect.timeout("10 seconds"),
294293
Effect.orElseSucceed(() => undefined),
295294
);
296-
if (response?.data === undefined) return variant;
295+
if (response?.data === undefined) return undefined;
297296

298297
const catalogModel = response.data.all.find((provider) => provider.id === model.providerID)
299298
?.models[model.modelID];
300-
const variants = catalogModel ? new Set(Object.keys(catalogModel.variants ?? {})) : undefined;
301-
if (cache !== undefined) {
302-
// Keep only this model's names; provider.list can return several megabytes.
303-
cache.set(key, variants);
304-
}
305-
return variants !== undefined && !variants.has(variant) ? undefined : variant;
299+
const entry: OpenCodeCatalogModel | undefined = catalogModel && {
300+
variants: new Set(Object.keys(catalogModel.variants ?? {})),
301+
contextWindow: catalogModel.limit?.input ?? catalogModel.limit?.context,
302+
};
303+
// Keep only what this model needs; provider.list can return several megabytes.
304+
cache?.set(key, entry);
305+
return entry;
306+
});
307+
308+
export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
309+
client: OpencodeClient,
310+
model: ParsedOpenCodeModelSlug | undefined,
311+
variant: string | undefined,
312+
abortSignal?: AbortSignal,
313+
cache?: OpenCodeVariantCache,
314+
) {
315+
if (!variant || !model) return variant;
316+
// Only a confirmed catalog mismatch can invalidate a persisted selection.
317+
const entry = yield* readOpenCodeCatalogModel(client, model, abortSignal, cache);
318+
return entry !== undefined && !entry.variants.has(variant) ? undefined : variant;
306319
});
307320

308321
export interface OpenCodeSkill {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,18 @@ describe("OpenCodeAdapterV2", () => {
775775
let promptId = "";
776776
const harness = yield* makeOpenCodeRuntimeHarness(`usage-${ending}`, "root", {
777777
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
778+
provider: {
779+
list: async () => ({
780+
data: {
781+
all: [
782+
{
783+
id: "anthropic",
784+
models: { "claude-sonnet": { limit: { context: 200_000, output: 8_000 } } },
785+
},
786+
],
787+
},
788+
}),
789+
},
778790
session: {
779791
create: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
780792
promptAsync: async (input: { messageID: string }) => {
@@ -895,6 +907,11 @@ describe("OpenCodeAdapterV2", () => {
895907
hasSubagents: false,
896908
},
897909
);
910+
// The thread's context window reads tokenUsage: the last step's context fill.
911+
if (ending === "completed") {
912+
assert.equal(completed?.providerTurn.tokenUsage?.usedTokens, 22);
913+
assert.equal(completed?.providerTurn.tokenUsage?.maxTokens, 200_000);
914+
}
898915
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))),
899916
);
900917

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

Lines changed: 40 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);
@@ -2094,6 +2124,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
20942124
yield* resolveRuntimeRequest(pending.nativeRequestId, "cancelled");
20952125
}
20962126
}
2127+
// Prompts read the catalog only for variants; a completed turn needs its context window.
2128+
const parsedModel = OpenCodeRuntime.parseOpenCodeModelSlug(turn.modelSelection.model);
2129+
if (status === "completed" && turn.usage.lastStep !== null && parsedModel !== null) {
2130+
yield* OpenCodeRuntime.readOpenCodeCatalogModel(
2131+
client,
2132+
parsedModel,
2133+
undefined,
2134+
variantCache,
2135+
);
2136+
}
20972137
yield* emitProviderTurn(state, turn, status, completedAt);
20982138
const threadDisposition = terminal?.threadDisposition ?? "reusable";
20992139
yield* updateProviderThread(state, {

0 commit comments

Comments
 (0)