From 682959ef9a291f8e581f5f0dd2acc158572f11ce Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 22 Nov 2026 04:36:23 +0100 Subject: [PATCH 1/4] [Fix] opencode-go no longer overrides curated contextWindow with live API value The /models endpoint reports a blanket context_window (200000) for every model. parseOpencodeGoModel spread the live value over the curated registry entry, so curated models (e.g. deepseek-v4-flash, 1M) had their real context window clobbered to 200k in the model picker and downstream context management. The registry context window is now authoritative for curated models; unknown models still take the live value when present, and live max-tokens/image-support merging is unchanged. Spec: docs/architecture/tla/OpencodeGoModelMerge.tla (+ cfg + 3 sabotage configs). TLC: fixed variant holds in 12 states; LiveOverride, NoLiveMaxTokens and UnknownIgnoresLive sabotage variants all fail as expected. Closes #723 --- .../tla/OpencodeGoModelMerge.LiveOverride.cfg | 5 ++ .../OpencodeGoModelMerge.NoLiveMaxTokens.cfg | 5 ++ ...pencodeGoModelMerge.UnknownIgnoresLive.cfg | 5 ++ .../architecture/tla/OpencodeGoModelMerge.cfg | 5 ++ .../architecture/tla/OpencodeGoModelMerge.tla | 80 +++++++++++++++++++ packages/types/src/providers/opencode-go.ts | 7 +- .../fetchers/__tests__/opencode-go.spec.ts | 63 ++++++++++++--- src/api/providers/fetchers/opencode-go.ts | 13 +-- 8 files changed, 164 insertions(+), 19 deletions(-) create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.cfg create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.tla diff --git a/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg b/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg new file mode 100644 index 00000000000..5b6d6c744de --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "LiveOverride" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg new file mode 100644 index 00000000000..3b43a5ac647 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoLiveMaxTokens" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg b/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg new file mode 100644 index 00000000000..d8742da58fa --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "UnknownIgnoresLive" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.cfg b/docs/architecture/tla/OpencodeGoModelMerge.cfg new file mode 100644 index 00000000000..a01bcf13669 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "Fixed" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.tla b/docs/architecture/tla/OpencodeGoModelMerge.tla new file mode 100644 index 00000000000..9e22ee7f5e3 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.tla @@ -0,0 +1,80 @@ +---- MODULE OpencodeGoModelMerge ---- +\* Source: parseOpencodeGoModel (src/api/providers/fetchers/opencode-go.ts). +\* Sink: ModelInfo.contextWindow published by getOpencodeGoModels for the model picker. +\* Failure: a curated model gets its registry contextWindow replaced by the live +\* context_window value. The Go endpoint reports a blanket 200000 (issue 723). +\* Invariant sentences came before the model and before the fix: +\* S1 a registry model must keep the registry contextWindow. +\* S2 an unknown model must keep the live value, and the default when absent. +\* S3 a registry model must still take a live max-token value. +\* S4 an unknown model must keep the live max-token rule and the default. +\* Invariants: CuratedContextWindowFromRegistry, UnknownContextWindowCorrect, +\* CuratedLiveMaxTokensMerged, UnknownMaxTokensCorrect. +\* Expect: Fixed holds. LiveOverride models the old code and must fail S1. +\* Expect: UnknownIgnoresLive fails S2. NoLiveMaxTokens fails S3. +\* Result: Fixed holds in 12 states. LiveOverride fails S1 on a curated model +\* with the blanket 200000: [curated |-> TRUE, liveContextWindow |-> 200000]. +\* Result: UnknownIgnoresLive fails S2. NoLiveMaxTokens fails S3. Not vacuous. +\* Replay: the unfixed parseOpencodeGoModel fails 6 tests, with 200000 in +\* place of a curated 1M window. The fix passes all. No false alarm. +\* Review: 0 covered, 0 added, 0 gap. No review round has run yet. +\* Cost: four TLC runs take about 4 seconds. Focused tests take about 5 seconds. +\* Agent work: about 15 minutes, including tests. +\* Not modeled: the parser, the field-name pair, capability flags, and pricing. +\* The model checks the merge contract, not the vendor facts. + +CONSTANT Variant \* "Fixed", "LiveOverride", "UnknownIgnoresLive", "NoLiveMaxTokens" + +VARIABLE in + +\* The live payload omits a field or reports a value. Nil is 0, not a real limit. +\* 200000 is the blanket value the endpoint reports for every model. +\* 2000000 is a live value larger than the registry one. +Inputs == [curated : BOOLEAN, + liveContextWindow : {0, 200000, 2000000}, + liveMaxTokens : {0, 65536}] + +\* Registry limits for one curated model, as deepseek-v4-flash. +CuratedContextWindow == 1000000 +CuratedMaxTokens == 384000 +\* Default limits for an unknown model, as opencodeGoDefaultModelInfo. +DefaultContextWindow == 200000 +DefaultMaxTokens == 32768 + +Nil == 0 + +\* One value models the context_window and context_length pair. +PublishedContextWindow(i) == + IF i.curated + THEN IF Variant = "LiveOverride" /\ i.liveContextWindow # Nil + THEN i.liveContextWindow + ELSE CuratedContextWindow + ELSE IF i.liveContextWindow = Nil \/ Variant = "UnknownIgnoresLive" + THEN DefaultContextWindow + ELSE i.liveContextWindow + +PublishedMaxTokens(i) == + IF i.curated + THEN IF Variant = "NoLiveMaxTokens" \/ i.liveMaxTokens = Nil + THEN CuratedMaxTokens + ELSE i.liveMaxTokens + ELSE IF i.liveMaxTokens = Nil + THEN DefaultMaxTokens + ELSE i.liveMaxTokens + +TypeOk == in \in Inputs +CuratedContextWindowFromRegistry == + in.curated => PublishedContextWindow(in) = CuratedContextWindow +UnknownContextWindowCorrect == + ~in.curated => PublishedContextWindow(in) = + IF in.liveContextWindow = Nil THEN DefaultContextWindow ELSE in.liveContextWindow +CuratedLiveMaxTokensMerged == + in.curated /\ in.liveMaxTokens # Nil => PublishedMaxTokens(in) = in.liveMaxTokens +UnknownMaxTokensCorrect == + ~in.curated => PublishedMaxTokens(in) = + IF in.liveMaxTokens = Nil THEN DefaultMaxTokens ELSE in.liveMaxTokens + +Init == in \in Inputs +Next == UNCHANGED in +Spec == Init /\ [][Next]_in +==== diff --git a/packages/types/src/providers/opencode-go.ts b/packages/types/src/providers/opencode-go.ts index f7f23c1aa60..1de52808693 100644 --- a/packages/types/src/providers/opencode-go.ts +++ b/packages/types/src/providers/opencode-go.ts @@ -41,8 +41,11 @@ export const OPENCODE_GO_DEFAULT_TEMPERATURE = 0 * https://opencode.ai/docs/go/#usage-limits. It also includes explicitly * curated Responses-format models whose gateway metadata is not available * through `/models`. The fetcher merges the live `/models` payload on top of - * these defaults so that context-window and max-token values stay in sync - * with the gateway while capability flags and pricing remain correct. + * these defaults so that max-token and image-support values stay in sync + * with the gateway while capability flags, pricing, and the curated context + * windows remain correct. The registry context window is authoritative for + * curated models: the `/models` endpoint reports a blanket `context_window` + * for every model, so a live value must not override it. * * `supportsPromptCache` has two distinct meanings depending on the wire format: * diff --git a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts index 869304dcc12..169379a9043 100644 --- a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts @@ -39,17 +39,18 @@ describe("Opencode Go Fetchers", () => { }) expect(Object.keys(models).sort()).toEqual(["deepseek-v4-pro", "glm-5.1"]) - // Live endpoint values override the native registry for volatile fields, - // while capability flags and pricing come from the native registry. + // The registry context window stays authoritative for curated models: + // the endpoint reports a blanket context_window, so live limits only + // apply to max tokens while capability flags come from the registry. expect(models["glm-5.1"]).toMatchObject({ - contextWindow: 202752, + contextWindow: 204_800, maxTokens: 32768, supportsPromptCache: true, supportsReasoningEffort: ["disable", "medium"], preserveReasoning: true, description: "Zhipu GLM 5.1", }) - expect(models["deepseek-v4-pro"].contextWindow).toBe(1048576) + expect(models["deepseek-v4-pro"].contextWindow).toBe(1_000_000) expect(models["deepseek-v4-pro"].supportsReasoningEffort).toEqual([ "disable", "low", @@ -89,10 +90,11 @@ describe("Opencode Go Fetchers", () => { const models = await getOpencodeGoModels("k") // Live limits never override the curated temperature capability - // (Zoo-Code-Org/Zoo-Code#2055). - expect(models["grok-4.6"]).toMatchObject({ contextWindow: 400_000, supportsTemperature: true }) + // (Zoo-Code-Org/Zoo-Code#2055), and never override the curated + // context window (Zoo-Code-Org/Zoo-Code#723). + expect(models["grok-4.6"]).toMatchObject({ contextWindow: 500_000, supportsTemperature: true }) expect(models["muse-spark-1.2-contributor"]).toMatchObject({ maxTokens: 65_536, supportsTemperature: true }) - expect(models["gpt-5.6-luna"]).toMatchObject({ contextWindow: 900_000, supportsTemperature: false }) + expect(models["gpt-5.6-luna"]).toMatchObject({ contextWindow: 1_050_000, supportsTemperature: false }) }) it("uses curated gpt-6-luna metadata when the live model omits metadata", async () => { @@ -313,10 +315,11 @@ describe("Opencode Go Fetchers", () => { } }) - it("merges live endpoint values over the native registry for a curated model", () => { + it("keeps the registry context window and merges live max tokens for a curated model", () => { const info = parseOpencodeGoModel({ id: "glm-5.1", context_window: 150000, max_output_tokens: 8000 }) - // Live values win for volatile fields. - expect(info.contextWindow).toBe(150000) + // The registry context window stays authoritative (Zoo-Code-Org/Zoo-Code#723). + expect(info.contextWindow).toBe(204_800) + // Live max-token values still win. expect(info.maxTokens).toBe(8000) // Capability flags and pricing come from the native registry. expect(info.supportsPromptCache).toBe(true) @@ -326,20 +329,56 @@ describe("Opencode Go Fetchers", () => { expect(info.inputPrice).toBe(1.4) }) - it("keeps gpt-6-luna live limits authoritative over its curated metadata", () => { + it("keeps the curated gpt-6-luna context window over a smaller live value", () => { const info = parseOpencodeGoModel({ id: "gpt-6-luna", context_window: 1_000_000, max_output_tokens: 64_000, }) - expect(info.contextWindow).toBe(1_000_000) + expect(info.contextWindow).toBe(1_050_000) expect(info.maxTokens).toBe(64_000) expect(info.supportsImages).toBe(true) expect(info.inputPrice).toBe(0.1) expect(info.cacheWritesPrice).toBe(0.125) }) + it("keeps the curated context window when the endpoint reports the blanket 200000 (#723)", () => { + // The Go /models endpoint reports context_window: 200000 for every + // model. deepseek-v4-flash is curated with a 1M window and must keep it. + const info = parseOpencodeGoModel({ id: "deepseek-v4-flash", context_window: 200_000 }) + expect(info.contextWindow).toBe(1_000_000) + expect(info.maxTokens).toBe(384_000) + expect(info.supportsPromptCache).toBe(true) + }) + + it("the registry context window is authoritative for every curated model, whatever the live value", () => { + for (const [id, native] of Object.entries(opencodeGoModels)) { + const blanket = parseOpencodeGoModel({ id, context_window: 200_000 }) + expect(blanket.contextWindow, id).toBe(native.contextWindow) + + // A live value larger than the registry one must not win either. + const larger = parseOpencodeGoModel({ id, context_length: 2_000_000 }) + expect(larger.contextWindow, id).toBe(native.contextWindow) + } + }) + + it("an unknown model keeps the live context value in both field forms", () => { + const window = parseOpencodeGoModel({ id: "x", context_window: 123_456 }) + expect(window.contextWindow).toBe(123_456) + + const length = parseOpencodeGoModel({ id: "x", context_length: 654_321 }) + expect(length.contextWindow).toBe(654_321) + }) + + it("live max tokens still override the curated maxTokens in both field forms", () => { + const output = parseOpencodeGoModel({ id: "glm-5.1", max_output_tokens: 9_999 }) + expect(output.maxTokens).toBe(9_999) + + const max = parseOpencodeGoModel({ id: "glm-5.1", max_tokens: 8_888 }) + expect(max.maxTokens).toBe(8_888) + }) + it("uses native registry defaults when the live payload omits volatile fields", () => { const info = parseOpencodeGoModel({ id: "deepseek-v4-flash" }) const native = getOpencodeGoModelInfo("deepseek-v4-flash")! diff --git a/src/api/providers/fetchers/opencode-go.ts b/src/api/providers/fetchers/opencode-go.ts index 68ac3d2d454..1e15b127ee3 100644 --- a/src/api/providers/fetchers/opencode-go.ts +++ b/src/api/providers/fetchers/opencode-go.ts @@ -44,11 +44,14 @@ const opencodeGoModelsResponseSchema = z.object({ * * Resolution order for a fully-populated {@link ModelInfo}: * 1. Use the native registry ({@link getOpencodeGoModelInfo}) when the model - * is curated, including its capabilities and pricing. - * 2. Override static limits and image support with live `/models` values when - * present, keeping the gateway authoritative for volatile fields. + * is curated, including its context window, capabilities and pricing. + * 2. Override max tokens and image support with live `/models` values when + * present. The registry context window stays authoritative: the endpoint + * reports a blanket `context_window` (200k) for every model, so a live + * value would clobber curated windows (Zoo-Code-Org/Zoo-Code#723). * 3. Fall back to {@link opencodeGoDefaultModelInfo} for an unknown model, * ensuring downstream consumers always receive a fully-populated object. + * Unknown models still take the live context window when present. * * @param model - Validated model entry from the `/models` response. * @returns Normalised model metadata suitable for the model picker. @@ -56,7 +59,8 @@ const opencodeGoModelsResponseSchema = z.object({ export const parseOpencodeGoModel = (model: OpencodeGoModel): ModelInfo => { const native = getOpencodeGoModelInfo(model.id) - // Live endpoint values take precedence over the registry for volatile fields. + // Live endpoint values take precedence for max tokens and image support. + // The registry context window stays authoritative for curated models. const liveContextWindow = model.context_window ?? model.context_length const liveMaxTokens = model.max_output_tokens ?? model.max_tokens const liveSupportsImages = model.supports_images @@ -64,7 +68,6 @@ export const parseOpencodeGoModel = (model: OpencodeGoModel): ModelInfo => { if (native) { return { ...native, - ...(liveContextWindow !== undefined && { contextWindow: liveContextWindow }), ...(liveMaxTokens !== undefined && { maxTokens: liveMaxTokens }), ...(liveSupportsImages !== undefined && { supportsImages: liveSupportsImages }), description: model.description ?? model.name ?? native.description, From 075162ed6b38e29abc09f0ebfe6cbef1fdbf44ff Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 22 Nov 2026 11:46:53 +0100 Subject: [PATCH 2/4] spec(tla): model a live zero as a real value, not absence (#723 review) --- .../architecture/tla/OpencodeGoModelMerge.tla | 22 +++++++++++-------- .../fetchers/__tests__/opencode-go.spec.ts | 14 ++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/docs/architecture/tla/OpencodeGoModelMerge.tla b/docs/architecture/tla/OpencodeGoModelMerge.tla index 9e22ee7f5e3..079601be26e 100644 --- a/docs/architecture/tla/OpencodeGoModelMerge.tla +++ b/docs/architecture/tla/OpencodeGoModelMerge.tla @@ -1,4 +1,5 @@ ---- MODULE OpencodeGoModelMerge ---- +EXTENDS Integers \* Source: parseOpencodeGoModel (src/api/providers/fetchers/opencode-go.ts). \* Sink: ModelInfo.contextWindow published by getOpencodeGoModels for the model picker. \* Failure: a curated model gets its registry contextWindow replaced by the live @@ -12,12 +13,14 @@ \* CuratedLiveMaxTokensMerged, UnknownMaxTokensCorrect. \* Expect: Fixed holds. LiveOverride models the old code and must fail S1. \* Expect: UnknownIgnoresLive fails S2. NoLiveMaxTokens fails S3. -\* Result: Fixed holds in 12 states. LiveOverride fails S1 on a curated model -\* with the blanket 200000: [curated |-> TRUE, liveContextWindow |-> 200000]. -\* Result: UnknownIgnoresLive fails S2. NoLiveMaxTokens fails S3. Not vacuous. +\* Result: Fixed holds in 24 states. LiveOverride fails S1 on a curated model +\* with a live 0: [curated |-> TRUE, liveContextWindow |-> 0, liveMaxTokens |-> -1]. +\* Result: UnknownIgnoresLive fails S2 on a live 0. NoLiveMaxTokens fails S3 on +\* liveMaxTokens 0. Each sabotage run fails on a live zero. Not vacuous. \* Replay: the unfixed parseOpencodeGoModel fails 6 tests, with 200000 in \* place of a curated 1M window. The fix passes all. No false alarm. -\* Review: 0 covered, 0 added, 0 gap. No review round has run yet. +\* Review (PR 2646, thread 4656589514): one finding, added 1 (input rule: +\* 0 is a live value; the sentinel -1 marks absence), covered 0, gap 0. \* Cost: four TLC runs take about 4 seconds. Focused tests take about 5 seconds. \* Agent work: about 15 minutes, including tests. \* Not modeled: the parser, the field-name pair, capability flags, and pricing. @@ -27,12 +30,15 @@ CONSTANT Variant \* "Fixed", "LiveOverride", "UnknownIgnoresLive", "NoLiveMax VARIABLE in -\* The live payload omits a field or reports a value. Nil is 0, not a real limit. +\* The live payload omits a field or reports a value. Nil is the -1 sentinel. +\* 0 is a real live value: ?? falls back only on null and undefined. \* 200000 is the blanket value the endpoint reports for every model. \* 2000000 is a live value larger than the registry one. +Nil == -1 + Inputs == [curated : BOOLEAN, - liveContextWindow : {0, 200000, 2000000}, - liveMaxTokens : {0, 65536}] + liveContextWindow : {Nil, 0, 200000, 2000000}, + liveMaxTokens : {Nil, 0, 65536}] \* Registry limits for one curated model, as deepseek-v4-flash. CuratedContextWindow == 1000000 @@ -41,8 +47,6 @@ CuratedMaxTokens == 384000 DefaultContextWindow == 200000 DefaultMaxTokens == 32768 -Nil == 0 - \* One value models the context_window and context_length pair. PublishedContextWindow(i) == IF i.curated diff --git a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts index 169379a9043..b1cd8f3a83c 100644 --- a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts @@ -379,6 +379,20 @@ describe("Opencode Go Fetchers", () => { expect(max.maxTokens).toBe(8_888) }) + it("keeps a live zero max-token value for a curated model", () => { + const info = parseOpencodeGoModel({ id: "glm-5.1", context_window: 150_000, max_output_tokens: 0 }) + // 0 is a live value, not an absent field: ?? falls back only on null and undefined. + expect(info.maxTokens).toBe(0) + // The registry context window stays authoritative. + expect(info.contextWindow).toBe(204_800) + }) + + it("keeps a live zero context window for an unknown model", () => { + const info = parseOpencodeGoModel({ id: "x", context_window: 0 }) + expect(info.contextWindow).toBe(0) + expect(info.maxTokens).toBe(opencodeGoDefaultModelInfo.maxTokens) + }) + it("uses native registry defaults when the live payload omits volatile fields", () => { const info = parseOpencodeGoModel({ id: "deepseek-v4-flash" }) const native = getOpencodeGoModelInfo("deepseek-v4-flash")! From 3b23a79341e4846f01c03859457e521770bd72ed Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 24 Nov 2026 14:29:08 +0100 Subject: [PATCH 3/4] feat(providers): read opencode-go limit/modalties objects, live values for unknown, skip non-chat (#2646 maintainer review) Maintainer review (edelauna) items: - opencodeGoModelSchema now parses limit.context/limit.output, modalities.input, and the type discriminator; the flat fields stay parsed as a fallback for older responses. - Unknown models take the live limit values (context window, max tokens, image support) instead of the 200k/32k/no-image defaults; the nested objects win over the flat fallback. - Catalog entries whose type is present and not 'chat' are skipped; untyped entries stay listed for older responses. - New tests: nested live values for unknown models, nested-over-flat precedence, both flat alias fields set, modalities-driven image support, and the type filter. Spec ratchet: OpencodeGoModelMerge gains the liveShape (absent/flat/nested) and modelType (chat/embedding/untyped) inputs, the Listed guard, and the OnlyChatListed invariant; the expected sides use raw payload values so a dropped nested read cannot vacuously pass. Fixed gate 150 states; NoNestedLimit and NoTypeFilter fail their invariants; the three prior sabotages unchanged. Red-verified: 3 failures pre-fix (no type filter, 200k default for an unknown model with live limits, flat winning over stripped nested). 34/34 + tsc + eslint clean. --- .../OpencodeGoModelMerge.NoNestedLimit.cfg | 5 ++ .../tla/OpencodeGoModelMerge.NoTypeFilter.cfg | 5 ++ .../architecture/tla/OpencodeGoModelMerge.cfg | 2 +- .../architecture/tla/OpencodeGoModelMerge.tla | 72 ++++++++++++++----- .../fetchers/__tests__/opencode-go.spec.ts | 60 ++++++++++++++++ src/api/providers/fetchers/opencode-go.ts | 52 ++++++++++---- 6 files changed, 166 insertions(+), 30 deletions(-) create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg create mode 100644 docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg new file mode 100644 index 00000000000..6be294bc9ad --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoNestedLimit" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg new file mode 100644 index 00000000000..f0edf6cb302 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoTypeFilter" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.cfg b/docs/architecture/tla/OpencodeGoModelMerge.cfg index a01bcf13669..4c6577fef65 100644 --- a/docs/architecture/tla/OpencodeGoModelMerge.cfg +++ b/docs/architecture/tla/OpencodeGoModelMerge.cfg @@ -1,5 +1,5 @@ SPECIFICATION Spec CONSTANTS Variant = "Fixed" -INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.tla b/docs/architecture/tla/OpencodeGoModelMerge.tla index 079601be26e..568db8afbe2 100644 --- a/docs/architecture/tla/OpencodeGoModelMerge.tla +++ b/docs/architecture/tla/OpencodeGoModelMerge.tla @@ -21,6 +21,14 @@ EXTENDS Integers \* place of a curated 1M window. The fix passes all. No false alarm. \* Review (PR 2646, thread 4656589514): one finding, added 1 (input rule: \* 0 is a live value; the sentinel -1 marks absence), covered 0, gap 0. +\* Maintainer round (edelauna, review 6033833085): the endpoint publishes +\* limit.context/limit.output and modalities.input with a `type` +\* discriminator; unknown models must take the live values, and non-chat +\* types must be skipped. Added the liveShape (absent/flat/nested) and +\* modelType (chat/embedding/untyped) inputs, the Listed guard, and the +\* OnlyChatListed invariant. NoNestedLimit fails the live-merge invariants +\* on nested-only entries; NoTypeFilter fails OnlyChatListed. 0 covered, +\* 1 added, 0 gap. Fixed gate grows 24 to 150 states. \* Cost: four TLC runs take about 4 seconds. Focused tests take about 5 seconds. \* Agent work: about 15 minutes, including tests. \* Not modeled: the parser, the field-name pair, capability flags, and pricing. @@ -36,9 +44,14 @@ VARIABLE in \* 2000000 is a live value larger than the registry one. Nil == -1 -Inputs == [curated : BOOLEAN, +\* The absent shape carries no values at all; a present shape may still +\* omit either field individually (Nil marks that field's absence). +Inputs == {i \in [curated : BOOLEAN, liveContextWindow : {Nil, 0, 200000, 2000000}, - liveMaxTokens : {Nil, 0, 65536}] + liveMaxTokens : {Nil, 0, 65536}, + liveShape : {"absent", "flat", "nested"}, + modelType : {"chat", "embedding", "untyped"}] : + (i.liveShape = "absent" => i.liveContextWindow = Nil /\ i.liveMaxTokens = Nil)} \* Registry limits for one curated model, as deepseek-v4-flash. CuratedContextWindow == 1000000 @@ -48,35 +61,62 @@ DefaultContextWindow == 200000 DefaultMaxTokens == 32768 \* One value models the context_window and context_length pair. +\* The parser reads the nested limit objects; the flat fields stay a +\* fallback. A nested-only payload loses its live values when the nested +\* read is sabotaged (NoNestedLimit). +LiveContext(i) == + IF i.liveShape = "absent" THEN Nil + ELSE IF i.liveShape = "nested" /\ Variant = "NoNestedLimit" THEN Nil + ELSE i.liveContextWindow + +LiveMaxTokens(i) == + IF i.liveShape = "absent" THEN Nil + ELSE IF i.liveShape = "nested" /\ Variant = "NoNestedLimit" THEN Nil + ELSE i.liveMaxTokens + +\* Fixed code: an entry whose type is present and not "chat" is skipped +\* from the catalog. Untyped entries stay listed for older responses. +\* Sabotage NoTypeFilter lists every parsed entry. +Listed(i) == ~(Variant # "NoTypeFilter" /\ i.modelType = "embedding") + PublishedContextWindow(i) == IF i.curated - THEN IF Variant = "LiveOverride" /\ i.liveContextWindow # Nil - THEN i.liveContextWindow + THEN IF Variant = "LiveOverride" /\ LiveContext(i) # Nil + THEN LiveContext(i) ELSE CuratedContextWindow - ELSE IF i.liveContextWindow = Nil \/ Variant = "UnknownIgnoresLive" + ELSE IF LiveContext(i) = Nil \/ Variant = "UnknownIgnoresLive" THEN DefaultContextWindow - ELSE i.liveContextWindow + ELSE LiveContext(i) PublishedMaxTokens(i) == IF i.curated - THEN IF Variant = "NoLiveMaxTokens" \/ i.liveMaxTokens = Nil + THEN IF Variant = "NoLiveMaxTokens" \/ LiveMaxTokens(i) = Nil THEN CuratedMaxTokens - ELSE i.liveMaxTokens - ELSE IF i.liveMaxTokens = Nil + ELSE LiveMaxTokens(i) + ELSE IF LiveMaxTokens(i) = Nil THEN DefaultMaxTokens - ELSE i.liveMaxTokens + ELSE LiveMaxTokens(i) TypeOk == in \in Inputs +\* A skipped entry never reaches the merge, so the merge contract applies +\* only to listed entries. The EXPECTED side uses the raw payload value, not +\* the read operator, so a dropped read (NoNestedLimit) cannot vacuously +\* pass by flipping both sides. CuratedContextWindowFromRegistry == - in.curated => PublishedContextWindow(in) = CuratedContextWindow + Listed(in) /\ in.curated => PublishedContextWindow(in) = CuratedContextWindow UnknownContextWindowCorrect == - ~in.curated => PublishedContextWindow(in) = - IF in.liveContextWindow = Nil THEN DefaultContextWindow ELSE in.liveContextWindow + Listed(in) /\ ~in.curated => PublishedContextWindow(in) = + IF in.liveShape = "absent" \/ in.liveContextWindow = Nil THEN DefaultContextWindow ELSE in.liveContextWindow CuratedLiveMaxTokensMerged == - in.curated /\ in.liveMaxTokens # Nil => PublishedMaxTokens(in) = in.liveMaxTokens + Listed(in) /\ in.curated /\ in.liveShape # "absent" /\ in.liveMaxTokens # Nil => + PublishedMaxTokens(in) = in.liveMaxTokens UnknownMaxTokensCorrect == - ~in.curated => PublishedMaxTokens(in) = - IF in.liveMaxTokens = Nil THEN DefaultMaxTokens ELSE in.liveMaxTokens + Listed(in) /\ ~in.curated => PublishedMaxTokens(in) = + IF in.liveShape = "absent" \/ in.liveMaxTokens = Nil THEN DefaultMaxTokens ELSE in.liveMaxTokens + +\* Maintainer round: an entry whose declared type is not "chat" is never +\* published to the picker. +OnlyChatListed == in.modelType = "embedding" => ~Listed(in) Init == in \in Inputs Next == UNCHANGED in diff --git a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts index b1cd8f3a83c..78ebf042f64 100644 --- a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts @@ -228,6 +228,23 @@ describe("Opencode Go Fetchers", () => { warnSpy.mockRestore() }) + it("skips models whose type is not chat (maintainer review)", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [ + { id: "chat-model", type: "chat" }, + { id: "embed-model", type: "embedding" }, + { id: "untyped-model" }, + ], + }, + }) + + const models = await getOpencodeGoModels() + + // Untyped entries stay listed for older responses. + expect(Object.keys(models).sort()).toEqual(["chat-model", "untyped-model"]) + }) + it("forwards the caller's abort signal to the request", async () => { mockedAxios.get.mockResolvedValue({ data: { data: [] } }) const controller = new AbortController() @@ -488,5 +505,48 @@ describe("Opencode Go Fetchers", () => { expect(info.description).toBeTruthy() } }) + + it("an unknown model takes its live values from limit and modalities (maintainer review)", () => { + const info = parseOpencodeGoModel({ + id: "x", + type: "chat", + limit: { context: 123_456, output: 4_096 }, + modalities: { input: ["text", "image"] }, + }) + expect(info.contextWindow).toBe(123_456) + expect(info.maxTokens).toBe(4_096) + expect(info.supportsImages).toBe(true) + }) + + it("nested limit values win over the flat fallback fields", () => { + const info = parseOpencodeGoModel({ + id: "x", + limit: { context: 555_000, output: 5_555 }, + context_window: 666_000, + max_output_tokens: 6_666, + }) + expect(info.contextWindow).toBe(555_000) + expect(info.maxTokens).toBe(5_555) + }) + + it("keeps the first flat alias when both fields of a pair are set", () => { + const info = parseOpencodeGoModel({ + id: "x", + context_window: 111_111, + context_length: 222_222, + max_output_tokens: 444_444, + max_tokens: 333_333, + }) + expect(info.contextWindow).toBe(111_111) + expect(info.maxTokens).toBe(444_444) + }) + + it("modalities.input drives image support with the flat field as fallback", () => { + const fromModalities = parseOpencodeGoModel({ id: "x", modalities: { input: ["text"] } }) + expect(fromModalities.supportsImages).toBe(false) + + const fromFlat = parseOpencodeGoModel({ id: "x", supports_images: true }) + expect(fromFlat.supportsImages).toBe(true) + }) }) }) diff --git a/src/api/providers/fetchers/opencode-go.ts b/src/api/providers/fetchers/opencode-go.ts index 1e15b127ee3..f920ab59573 100644 --- a/src/api/providers/fetchers/opencode-go.ts +++ b/src/api/providers/fetchers/opencode-go.ts @@ -10,11 +10,15 @@ const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1" // The Opencode Go `/models` endpoint follows the OpenAI `/models` shape. The // `id` is the only guaranteed field; metadata is optional and best-effort, so -// the schema is intentionally permissive. Pricing is intentionally NOT parsed: -// the units returned by the endpoint aren't documented, and reporting a wrong -// cost is worse than reporting "unknown" — so cost stays sourced from the -// native registry (or undefined for unknown models) until the pricing shape is -// confirmed against the live endpoint. +// the schema is intentionally permissive. The current endpoint publishes +// limits under `limit.context`/`limit.output`, image support under +// `modalities.input`, and a `type` discriminator; the flat fields stay +// parsed as a fallback for older responses (maintainer review, PR #2646). +// Pricing is intentionally NOT parsed: the units returned by the endpoint +// aren't documented, and reporting a wrong cost is worse than reporting +// "unknown" — so cost stays sourced from the native registry (or undefined +// for unknown models) until the pricing shape is confirmed against the live +// endpoint. const opencodeGoModelSchema = z.object({ id: z.string(), name: z.string().optional(), @@ -24,6 +28,18 @@ const opencodeGoModelSchema = z.object({ max_tokens: z.number().optional(), max_output_tokens: z.number().optional(), supports_images: z.boolean().optional(), + limit: z + .object({ + context: z.number().optional(), + output: z.number().optional(), + }) + .optional(), + modalities: z + .object({ + input: z.array(z.string()).optional(), + }) + .optional(), + type: z.string().optional(), }) export type OpencodeGoModel = z.infer @@ -36,11 +52,13 @@ const opencodeGoModelsResponseSchema = z.object({ * Maps a raw Opencode Go model entry to the internal {@link ModelInfo} shape. * * The Go `/models` endpoint only reliably returns `id` and (sometimes) - * `context_window`/`max_tokens`. It does NOT advertise capability flags - * (`supportsReasoningEffort`, `preserveReasoning`, `supportsMaxTokens`, - * `supportsPromptCache`) or pricing, all of which the extension needs to drive - * reasoning controls, interleaved-thinking tool calls, the max-output-tokens - * slider, and accurate cost reporting. + * limits and image support — under `limit.context`/`limit.output` and + * `modalities.input` on the current deployment, or the flat + * `context_window`/`max_tokens` fields on older ones. It does NOT advertise + * capability flags (`supportsReasoningEffort`, `preserveReasoning`, + * `supportsMaxTokens`, `supportsPromptCache`) or pricing, all of which the + * extension needs to drive reasoning controls, interleaved-thinking tool + * calls, the max-output-tokens slider, and accurate cost reporting. * * Resolution order for a fully-populated {@link ModelInfo}: * 1. Use the native registry ({@link getOpencodeGoModelInfo}) when the model @@ -61,9 +79,11 @@ export const parseOpencodeGoModel = (model: OpencodeGoModel): ModelInfo => { // Live endpoint values take precedence for max tokens and image support. // The registry context window stays authoritative for curated models. - const liveContextWindow = model.context_window ?? model.context_length - const liveMaxTokens = model.max_output_tokens ?? model.max_tokens - const liveSupportsImages = model.supports_images + // The nested limit/modalties objects are the documented shape; the flat + // fields stay as a fallback (maintainer review, PR #2646). + const liveContextWindow = model.limit?.context ?? model.context_window ?? model.context_length + const liveMaxTokens = model.limit?.output ?? model.max_output_tokens ?? model.max_tokens + const liveSupportsImages = model.modalities?.input?.includes("image") ?? model.supports_images if (native) { return { @@ -123,6 +143,12 @@ export async function getOpencodeGoModels( console.warn(`Skipping invalid Opencode Go model entry: ${JSON.stringify(rawModel)}`) continue } + // Only chat models belong in the picker; an embedding or other + // type would surface as an unusable choice. Entries without a + // type stay listed for older responses (maintainer review). + if (parsed.data.type !== undefined && parsed.data.type !== "chat") { + continue + } models[parsed.data.id] = parseOpencodeGoModel(parsed.data) } } catch (error) { From 91dd07beac38bef48282025b080b01c0fde128cd Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 24 Nov 2026 15:09:43 +0100 Subject: [PATCH 4/4] test(opencode-go): exercise nested metadata through the fetcher path CodeRabbit round on PR 2646 (review 6040141115, thread 4680745542): the nested-metadata tests called parseOpencodeGoModel directly and never exercised the endpoint schema or the fetcher. Add two tests at the real axios seam: curated and unknown entries carrying nested limit/modalties metadata plus conflicting flat values (nested wins), and a modalities entry without input that must fall back to supports_images. These kill the three surviving mutants from report 7133201488, each apply/revert-verified on the focused spec: - L32 limit ObjectLiteral->{}: falls back to flat max_tokens -> fail - L38 modalities ObjectLiteral->{}: falls back to supports_images -> fail - L86 dropped ?. on modalities.input: crashes on missing input -> fail Spec ratchet: OpencodeGoModelMerge header records the round (covered 1, added 1, gap 0); gate re-run: base 150 states pass, all sabotage cfgs fail as designed. --- .../architecture/tla/OpencodeGoModelMerge.tla | 16 +++++ .../fetchers/__tests__/opencode-go.spec.ts | 58 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/docs/architecture/tla/OpencodeGoModelMerge.tla b/docs/architecture/tla/OpencodeGoModelMerge.tla index 568db8afbe2..1100747fffe 100644 --- a/docs/architecture/tla/OpencodeGoModelMerge.tla +++ b/docs/architecture/tla/OpencodeGoModelMerge.tla @@ -29,6 +29,22 @@ EXTENDS Integers \* OnlyChatListed invariant. NoNestedLimit fails the live-merge invariants \* on nested-only entries; NoTypeFilter fails OnlyChatListed. 0 covered, \* 1 added, 0 gap. Fixed gate grows 24 to 150 states. +\* CodeRabbit round (review 6040141115, thread 4680745542, mutation +\* report 7133201488): finding A — the nested-metadata tests called the +\* parser directly and never exercised the endpoint schema or fetcher. +\* The nested merge contract was already covered (liveShape "nested", +\* the NoNestedLimit sabotage); added 2 fetcher-path tests at the real +\* axios seam: curated + unknown entries carrying nested limit and +\* modalities plus conflicting flat fields, and a modalities entry +\* without input that must fall back to supports_images. The 3 +\* surviving mutants are killed, each apply/revert-verified on the +\* focused spec: L32/L38 (a nested schema object emptied) fail the +\* precedence test, L86 (dropped ?.) fails the missing-input test. +\* Image support stays with the not-modeled list (L86 is killed at +\* test level). Scope check (pre-merge "Out of Scope Changes"): the +\* type filter stays — maintainer-requested (review 6033833085) — and +\* is now linked as issue 2679 in the PR Scope section. covered 1, +\* added 1, gap 0. Header-only .tla edit; the gate re-ran unchanged. \* Cost: four TLC runs take about 4 seconds. Focused tests take about 5 seconds. \* Agent work: about 15 minutes, including tests. \* Not modeled: the parser, the field-name pair, capability flags, and pricing. diff --git a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts index 78ebf042f64..3ac7367c74c 100644 --- a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts @@ -271,6 +271,64 @@ describe("Opencode Go Fetchers", () => { await expect(fetchPromise).rejects.toMatchObject({ name: "AbortError" }) }) + + it("nested limit and modalities win over conflicting flat fields through the fetcher path", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [ + { + id: "glm-5.1", + type: "chat", + limit: { context: 999_000, output: 7_777 }, + modalities: { input: ["text", "image"] }, + // Conflicting flat values must lose to the nested shape. + context_window: 888_000, + max_tokens: 6_666, + supports_images: false, + }, + { + id: "some-unknown-model", + type: "chat", + limit: { context: 123_456, output: 4_096 }, + modalities: { input: ["text", "image"] }, + context_window: 666_000, + max_tokens: 6_666, + supports_images: false, + }, + ], + }, + }) + + const models = await getOpencodeGoModels("k") + + // Curated: the registry context window stays authoritative, while the + // nested limit.output and modalities.input win over the flat fields. + expect(models["glm-5.1"]).toMatchObject({ + contextWindow: 204_800, + maxTokens: 7_777, + supportsImages: true, + }) + // Unknown: the nested limit.context drives the published window too. + expect(models["some-unknown-model"]).toMatchObject({ + contextWindow: 123_456, + maxTokens: 4_096, + supportsImages: true, + }) + }) + + it("falls back to flat supports_images when modalities omits input", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [{ id: "another-unknown-model", type: "chat", modalities: {}, supports_images: true }], + }, + }) + + const models = await getOpencodeGoModels("k") + + // modalities.input is optional: the fetcher must fall back to the flat + // field instead of dereferencing the missing input array. + expect(models["another-unknown-model"].supportsImages).toBe(true) + }) }) describe("parseOpencodeGoModel", () => {