Skip to content

Commit 1ee2eef

Browse files
fix(opencode): OpenCode 1.x context usage shows the model's window
Prompts read the OpenCode catalog only when a variant is selected, so most 1.x threads reported context used with no maximum. A completed turn with step usage now reads the model's window through the same per-session catalog cache, leaving prompt admission unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0a95f1f commit 1ee2eef

3 files changed

Lines changed: 54 additions & 29 deletions

File tree

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

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

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

275-
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* (
276279
client: OpencodeClient,
277-
model: ParsedOpenCodeModelSlug | undefined,
278-
variant: string | undefined,
280+
model: ParsedOpenCodeModelSlug,
279281
abortSignal?: AbortSignal,
280282
cache?: OpenCodeVariantCache,
281283
) {
282-
if (!variant || !model) return variant;
283-
284284
const key = `${model.providerID}/${model.modelID}`;
285-
const cached = cache?.get(key);
286-
if (cache?.has(key)) {
287-
return cached !== undefined && !cached.variants.has(variant) ? undefined : variant;
288-
}
285+
if (cache?.has(key)) return cache.get(key);
289286

290-
// Only a confirmed catalog mismatch can invalidate a persisted selection.
291287
const response = yield* runOpenCodeSdk("provider.list", (signal) =>
292288
client.provider.list(undefined, {
293289
signal: abortSignal ? AbortSignal.any([signal, abortSignal]) : signal,
@@ -296,24 +292,30 @@ export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
296292
Effect.timeout("10 seconds"),
297293
Effect.orElseSucceed(() => undefined),
298294
);
299-
if (response?.data === undefined) return variant;
295+
if (response?.data === undefined) return undefined;
300296

301297
const catalogModel = response.data.all.find((provider) => provider.id === model.providerID)
302298
?.models[model.modelID];
303-
const variants = catalogModel ? new Set(Object.keys(catalogModel.variants ?? {})) : undefined;
304-
if (cache !== undefined) {
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-
);
315-
}
316-
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;
317319
});
318320

319321
export interface OpenCodeSkill {

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

Lines changed: 13 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 }) => {
@@ -898,6 +910,7 @@ describe("OpenCodeAdapterV2", () => {
898910
// The thread's context window reads tokenUsage: the last step's context fill.
899911
if (ending === "completed") {
900912
assert.equal(completed?.providerTurn.tokenUsage?.usedTokens, 22);
913+
assert.equal(completed?.providerTurn.tokenUsage?.maxTokens, 200_000);
901914
}
902915
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))),
903916
);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,16 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
21242124
yield* resolveRuntimeRequest(pending.nativeRequestId, "cancelled");
21252125
}
21262126
}
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+
}
21272137
yield* emitProviderTurn(state, turn, status, completedAt);
21282138
const threadDisposition = terminal?.threadDisposition ?? "reusable";
21292139
yield* updateProviderThread(state, {

0 commit comments

Comments
 (0)