Repository navigation
Fix/moa per slot max tokens #60391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fix/moa per slot max tokens #60391
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ def _coerce_fanout(value: Any) -> str: | |
| return mode if mode in {"per_iteration", "user_turn"} else "per_iteration" | ||
|
|
||
|
|
||
| def _clean_slot(slot: Any) -> dict[str, str] | None: | ||
| def _clean_slot(slot: Any) -> dict[str, Any] | None: | ||
| if not isinstance(slot, dict): | ||
| return None | ||
| provider = str(slot.get("provider") or "").strip() | ||
|
|
@@ -87,7 +87,16 @@ def _clean_slot(slot: Any) -> dict[str, str] | None: | |
| # an invalid slot is dropped, falling back to the preset's defaults. | ||
| if provider.lower() == "moa": | ||
| return None | ||
| return {"provider": provider, "model": model} | ||
| cleaned: dict[str, Any] = {"provider": provider, "model": model} | ||
| # Optional per-slot max_tokens: overrides the preset-level | ||
| # reference_max_tokens for this specific reference model. None (the | ||
| # default) = no cap, so existing slots are unaffected. Allows tuning | ||
| # each advisor's output length independently β useful when one model | ||
| # is verbose and another is terse. | ||
| slot_mt = _coerce_int_or_none(slot.get("max_tokens")) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new user-facing per-slot |
||
| if slot_mt is not None: | ||
| cleaned["max_tokens"] = slot_mt | ||
| return cleaned | ||
|
|
||
|
|
||
| def _default_preset() -> dict[str, Any]: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Tests for per-slot max_tokens in MoA reference calls. | ||
|
|
||
| Verifies that a ``max_tokens`` field on a reference slot dict takes | ||
| precedence over the preset-level ``reference_max_tokens``, and that | ||
| slot-level max_tokens=None falls back to the preset-level cap. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class TestRunReferenceSlotMaxTokens: | ||
| """_run_reference should prefer slot-level max_tokens over preset-level.""" | ||
|
|
||
| def test_slot_max_tokens_overrides_preset_level(self): | ||
| """When slot has max_tokens, it overrides the preset-level cap.""" | ||
| from agent.moa_loop import _run_reference | ||
|
|
||
| captured_kwargs: dict = {} | ||
|
|
||
| def fake_call_llm(**kwargs): | ||
| captured_kwargs.update(kwargs) | ||
| mock_resp = MagicMock() | ||
| mock_resp.choices = [MagicMock(message=MagicMock(content="advice"))] | ||
| mock_resp.usage = None | ||
| return mock_resp | ||
|
|
||
| slot = {"provider": "openrouter", "model": "deepseek/deepseek-v4-pro", "max_tokens": 600} | ||
|
|
||
| with patch("agent.moa_loop._slot_runtime", return_value={"provider": "openrouter", "model": "deepseek/deepseek-v4-pro"}), \ | ||
| patch("agent.moa_loop.call_llm", side_effect=fake_call_llm), \ | ||
| patch("agent.moa_loop._maybe_apply_moa_cache_control", side_effect=lambda msgs, rt: msgs): | ||
| _run_reference(slot, [{"role": "user", "content": "hi"}], max_tokens=2000) | ||
|
|
||
| assert captured_kwargs.get("max_tokens") == 600 | ||
|
|
||
| def test_slot_max_tokens_absent_falls_back_to_preset(self): | ||
| """When slot has no max_tokens, the preset-level cap is used.""" | ||
| from agent.moa_loop import _run_reference | ||
|
|
||
| captured_kwargs: dict = {} | ||
|
|
||
| def fake_call_llm(**kwargs): | ||
| captured_kwargs.update(kwargs) | ||
| mock_resp = MagicMock() | ||
| mock_resp.choices = [MagicMock(message=MagicMock(content="advice"))] | ||
| mock_resp.usage = None | ||
| return mock_resp | ||
|
|
||
| slot = {"provider": "openrouter", "model": "deepseek/deepseek-v4-pro"} | ||
|
|
||
| with patch("agent.moa_loop._slot_runtime", return_value={"provider": "openrouter", "model": "deepseek/deepseek-v4-pro"}), \ | ||
| patch("agent.moa_loop.call_llm", side_effect=fake_call_llm), \ | ||
| patch("agent.moa_loop._maybe_apply_moa_cache_control", side_effect=lambda msgs, rt: msgs): | ||
| _run_reference(slot, [{"role": "user", "content": "hi"}], max_tokens=2000) | ||
|
|
||
| assert captured_kwargs.get("max_tokens") == 2000 | ||
|
|
||
| def test_both_none_means_uncapped(self): | ||
| """When neither slot nor preset has max_tokens, it's None (uncapped).""" | ||
| from agent.moa_loop import _run_reference | ||
|
|
||
| captured_kwargs: dict = {} | ||
|
|
||
| def fake_call_llm(**kwargs): | ||
| captured_kwargs.update(kwargs) | ||
| mock_resp = MagicMock() | ||
| mock_resp.choices = [MagicMock(message=MagicMock(content="advice"))] | ||
| mock_resp.usage = None | ||
| return mock_resp | ||
|
|
||
| slot = {"provider": "openrouter", "model": "deepseek/deepseek-v4-pro"} | ||
|
|
||
| with patch("agent.moa_loop._slot_runtime", return_value={"provider": "openrouter", "model": "deepseek/deepseek-v4-pro"}), \ | ||
| patch("agent.moa_loop.call_llm", side_effect=fake_call_llm), \ | ||
| patch("agent.moa_loop._maybe_apply_moa_cache_control", side_effect=lambda msgs, rt: msgs): | ||
| _run_reference(slot, [{"role": "user", "content": "hi"}], max_tokens=None) | ||
|
|
||
| assert captured_kwargs.get("max_tokens") is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This broadens explicit output-cap forwarding to every non-Anthropic/NIM auxiliary route, but current main intentionally omits those caps to avoid OpenAI-compatible and ZAI failures (
2062a840;tests/agent/test_auxiliary_client.py:278). Please scope the repair to the documented MoA reference-cap path, or preserve the existing omission behavior for unrelated auxiliary tasks.