Skip to content

fix(opencode): reject unknown --variant instead of silently dropping it - #40250

Closed
C0d3N1nja97342 wants to merge 2 commits into
anomalyco:devfrom
C0d3N1nja97342:fix/variant-validation
Closed

C0d3N1nja97342 wants to merge 2 commits into
anomalyco:devfrom
C0d3N1nja97342:fix/variant-validation

Conversation

@C0d3N1nja97342

@C0d3N1nja97342 C0d3N1nja97342 commented Aug 3, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #40182

Type of change

  • Bug fix

What does this PR do?

opencode run --variant <value> accepted any string. When the value was not among the model's declared variants, the LLM request prep did a bracket lookup model.variants[variant] that returned undefined and merged nothing - so the run proceeded at the model's default effort level while the session record claimed an effort level that was never applied. Exit was 0 with no warning or log line. The reporter captured the outgoing HTTP request body to confirm reasoning_effort was absent for unknown values.

Why withVariant didn't catch this: packages/core/src/session/runner/model.ts already returns VariantUnavailableError for unknown variants - but the opencode run path uses its own LLM service (packages/opencode/src/session/llm/request.ts:80-83) which never calls withVariant. It does the bracket lookup directly, so the core guard does not cover opencode run. The two paths also use different representations of variants (array vs record).

Fix: validate an explicit --variant against the model's declared variants in createUserMessage (packages/opencode/src/session/prompt.ts) before it is recorded on the session. When a variant is supplied, fetch the full catalog model and throw a NamedError listing the valid variants if the value is unknown. This makes opencode run --variant <typo> exit non-zero with an actionable message, and stops the unapplied value from being persisted to session metadata.

"default" is passed through unchecked (it is resolved later to model.request.variant), matching the existing semantics.

How did you verify your code works?

Added a regression test in packages/opencode/test/session/prompt.test.ts (mirroring the existing "applies agent variant" and "unknown agent throws typed error" tests): it supplies variant: "totally-invalid-xyz" against a model declaring variants: { xhigh: {}, high: {} } and asserts the prompt fails with a NamedError.Unknown whose message contains Unknown variant "totally-invalid-xyz", the test/test-model identifier, and the valid values (high, xhigh).

Caveat: I could not run the packages/opencode test suite locally - the package depends on @opentui/solid, whose transitive deps (@babel/core/gensync) fail to resolve under Bun on Windows + Docker volume mounts (symlink/EACCES issues). The test file and the change both transpile cleanly (bun build --no-bundle), and the test follows the exact assertion pattern of the existing variant/agent-error tests in the same file. I'd appreciate a maintainer running bun test test/session/prompt.test.ts in CI to confirm.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

An interrupted tool run can leave a second tool part that reuses an
existing callID (a "completed" part plus an "error"/"Tool execution
aborted" part with metadata.interrupted). Serializing both to the
provider yields a duplicate tool_call_id, which every provider rejects
with a 400. Because compaction re-sends the full history head, the
duplicate is always present, so the session can never compact again -
every subsequent auto-compaction fails the same way and the token counter
keeps growing.

Deduplicate tool parts by callID in the assistant() lowering in
to-llm-message.ts before building provider messages, keeping the most
informative state (completed > error > running > pending) and preserving
first-occurrence position so tool/text/reasoning order stays stable.

Fixes anomalyco#40235
`opencode run --variant <value>` accepted any string. When the value was
not among the model's declared variants, the LLM request prep did a
bracket lookup `model.variants[variant]` that returned undefined and
merged nothing - so the run proceeded at the default effort level while
the session record claimed an effort level that was never applied. Exit
was 0 with no warning or log line.

Validate an explicit variant against the model's declared variants in
`createUserMessage` (prompt.ts) before it is recorded on the session:
fetch the full catalog model when a variant is supplied, and throw a
NamedError listing the valid variants when the value is unknown. This
makes `opencode run --variant <typo>` exit non-zero with an actionable
message, and stops the unapplied value from being persisted to session
metadata.

Note: `withVariant` in packages/core/src/session/runner/model.ts already
returns VariantUnavailableError for unknown variants, but the opencode
`run` path uses its own LLM service (packages/opencode/src/session/llm/
request.ts) which never calls withVariant - it does the bracket lookup
directly. So the core guard does not cover `opencode run`.

Fixes anomalyco#40182
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Aug 3, 2026
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--variant silently ignored when the value is not in the model's reasoning_options

1 participant