Skip to content

fix(core): propagate resolved output limit into model request generation - #47532

Closed
clopca wants to merge 1 commit into
anomalyco:v2from
clopca:bedrock-output-limit
Closed

clopca wants to merge 1 commit into
anomalyco:v2from
clopca:bedrock-output-limit

Conversation

@clopca

@clopca clopca commented Sep 5, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #46595

Note for triage: closingIssuesReferences stays empty because this PR targets v2, not the default branch (dev), so GitHub never materialises the Closes keyword link and the needs:issue label is a false positive. Every fix: PR against v2 by a non-team member currently gets this label (30 open at the time of writing).
Related: #47398 (same code site, reproduced on @ai-sdk/openai-compatible), #29363

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

SessionModelRequest.prepare built the model request with generation: undefined unless a session.context hook set a generation value. The model's resolved limit.output was never projected into the request, so no protocol received a maxTokens:

The fix always emits generation with maxTokens resolved in this precedence order: session.context hook value → model.defaults.generation.maxTokens → model.route.defaults.generation.maxTokens → resolved.limit.output. Hook and provider defaults keep winning; the catalog limit only fills the gap. Each protocol already serialized generation.maxTokens into its native field (inferenceConfig.maxTokens, max_tokens, max_output_tokens, generationConfig.maxOutputTokens), so no protocol changes are needed.

This is a re-submission of #46601, rebased on current v2 (the original was auto-closed by the compliance bot because the checklist section was missing). The SessionModelRequest.context options test block that the original PR extended was removed upstream in #46639, so the regression test now lives in the SessionRunnerLLM scenario harness.

Status

  • 2026-09-18: rebased onto current v2 (437cdc03a, 526 commits). The bug is still present there: SessionModelRequest.prepare emits generation: undefined unless a hook sets an option, so limit.output is never sent. Fix ported to the refactored prepare (model is now the Resolved, hook overrides live on event.options). Regression scenario updated to the event.options hook surface introduced in 2.0.x.
  • Field impact observed while this was unfixed (OpenCode 2.0.3, Bedrock global.anthropic.claude-fable-5-1, adaptive thinking): 146 turns in two weeks ended with finish=length at exactly 4096 output tokens; zero turns exceeded 4096. With inferenceConfig.maxTokens present the same model streams >6k-token turns without truncation.

How did you verify your code works?

  • New scenario in packages/core/test/session-runner.test.ts ("projects the resolved output limit into the request while preserving hook overrides"): asserts request.generation equals { maxTokens: <limit.output> } with no hooks, and that a hook-provided maxTokens/temperature win over the catalog limit. Confirmed it fails on v2 without the source change and passes with it.
  • Updated two existing expectations that asserted generation was undefined (session-compaction.test.ts, session-runner.test.ts).
  • bun run test test/session-compaction.test.ts test/session-model-request.test.ts test/session-runner.test.ts in packages/core: 228 pass, 0 fail (after rebase). Confirmed the new scenario fails on v2 HEAD without the source change (2 fail) and passes with it.
  • bun typecheck across the monorepo (33 packages) clean.

Screenshots / recordings

Not a UI change.

Checklist

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

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

The prepared model request only carried generation values set by hooks, so a
model's configured limit.output never reached the provider. On Bedrock the
request was sent without inferenceConfig.maxTokens and the service applied a
much lower implicit budget; with adaptive thinking the whole budget could be
consumed by reasoning, truncating responses at exactly 4,096 output tokens
despite limit.output=128000.

Project maxTokens with explicit precedence: hook/context override, model
defaults, route defaults, then resolved.limit.output. Each protocol already
lowers generation.maxTokens into its native field, so no protocol changes are
needed.

Closes anomalyco#46595
Related: anomalyco#47398
@github-actions

github-actions Bot commented Oct 5, 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.

1 participant