-
Notifications
You must be signed in to change notification settings - Fork 7
Forward explicit reasoning controls to compatible generators #117
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
Merged
Merged
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
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,111 @@ | ||
| """Explicit generation controls must reach the OpenAI-compatible wire unchanged.""" | ||
| import copy | ||
| from pathlib import Path | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from provider_adapters.openai_compatible import make_async_call_provider, stream_openai_compatible | ||
| from shim import ChatRequest, _request_to_contract, create_app | ||
| from llm_router_host import LLMRouterHost | ||
| from tests.test_compact import _PIN | ||
| from tests.test_streaming import OPENAI_REQ, FakeStreamClient, FakeStreamResponse, _openai_lines | ||
|
|
||
|
|
||
| CONTROLS = [{"reasoning": {"enabled": False}}, {"reasoning": {"effort": "low"}}, | ||
| {"reasoning_effort": "low"}, {}] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def host(): | ||
| root = Path(__file__).resolve().parents[1] | ||
| host = LLMRouterHost(router_path=root/'core/router.lua', | ||
| config_path=root/'core/config.example.lua', metrics_path=root/'core/metrics.example.lua', | ||
| env={"COMPUT3_API_KEY": "fixture-credential"}) | ||
| host.init() | ||
| return host | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("controls", CONTROLS) | ||
| def test_chat_contract_preserves_explicit_controls(controls): | ||
| contract = _request_to_contract(ChatRequest(**controls), "default") | ||
| assert {k: contract[k] for k in ("reasoning", "reasoning_effort") if k in contract} == controls | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("controls", CONTROLS) | ||
| @pytest.mark.asyncio | ||
| async def test_buffered_adapter_sends_controls(controls): | ||
| seen = [] | ||
| async def respond(request): | ||
| import json | ||
| seen.append(json.loads(request.content)) | ||
| return httpx.Response(200, json={"choices": [{"message": {"content": "ok"}}]}) | ||
| async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client: | ||
| result = await make_async_call_provider(client=client)({**OPENAI_REQ, **controls}) | ||
| assert result["ok"] | ||
| assert {k: seen[0][k] for k in ("reasoning", "reasoning_effort") if k in seen[0]} == controls | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("controls", CONTROLS) | ||
| @pytest.mark.asyncio | ||
| async def test_streaming_adapter_sends_controls(controls): | ||
| client = FakeStreamClient(FakeStreamResponse(200, _openai_lines("ok"))) | ||
| await stream_openai_compatible({**OPENAI_REQ, **controls}, lambda _: None, client=client) | ||
| body = client.requests[0]["json"] | ||
| assert {k: body[k] for k in ("reasoning", "reasoning_effort") if k in body} == controls | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("endpoint", ["/v1/chat/completions", "/v1/responses"]) | ||
| @pytest.mark.parametrize("stream", [False, True]) | ||
| @pytest.mark.parametrize("controls", CONTROLS[:3]) | ||
| def test_http_to_core_provider_preserves_controls(host, endpoint, stream, controls): | ||
| seen = [] | ||
| async def call(req): | ||
| seen.append(copy.deepcopy(req)) | ||
| return {"ok": True, "response": {"text": "ok", "tokens_in": 1, "tokens_out": 1}} | ||
| host.set_async_call_hook(call) | ||
| payload = {"policy_ir": _PIN, "stream": stream, **controls} | ||
| payload.update({"input": "hello"} if endpoint.endswith("responses") else | ||
| {"messages": [{"role": "user", "content": "hello"}]}) | ||
| response = TestClient(create_app(host)).post(endpoint, json=payload) | ||
| assert response.status_code == 200, response.text | ||
| assert len(seen) == 1 | ||
| assert {k: seen[0][k] for k in ("reasoning", "reasoning_effort") if k in seen[0]} == controls | ||
|
|
||
|
|
||
| def test_policy_can_set_reasoning_effort_before_provider_call(host): | ||
| seen = [] | ||
| async def call(req): | ||
| seen.append(req) | ||
| return {"ok": True, "response": {"text": "ok"}} | ||
| host.set_async_call_hook(call) | ||
| policy = copy.deepcopy(_PIN) | ||
| policy[4] = ["set_param", "reasoning_effort", "low"] | ||
| response = TestClient(create_app(host)).post( | ||
| "/v1/chat/completions", json={"messages": [], "policy_ir": policy}) | ||
| assert response.status_code == 200, response.text | ||
| assert seen[0]["reasoning_effort"] == "low" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("controls", CONTROLS[:3]) | ||
| def test_flow_controls_reach_generation_but_not_native_decisions(host, monkeypatch, controls): | ||
| from tests.test_flow_data import triage | ||
| seen = [] | ||
| async def execute(contract, **kwargs): | ||
| seen.append(copy.deepcopy(contract)) | ||
| if contract.get("protocol") == "decisions": | ||
| response = {"decision": {"model": "fixture", "answers": { | ||
| key: {"type": "choice", "choice": "support", | ||
| "probabilities": {"support": 1.0, "sales": 0.0}} for key in ("a", "b")}}} | ||
| else: | ||
| response = {"text": '{"a":"Reply A","b":"Reply B"}', "finish_reason": "stop"} | ||
| return {"ok": True, "response": response} | ||
| monkeypatch.setattr(host, "execute_async", execute) | ||
| response = TestClient(create_app(host)).post("/v1/chat/completions", json={ | ||
| "flow_ir": triage(), "flow_input": {"a": "Crash", "b": "Question"}, **controls}) | ||
| assert response.status_code == 200, response.text | ||
| assert len(seen) == 2 | ||
| assert seen[0]["protocol"] == "decisions" | ||
| assert not any(k in seen[0] for k in controls) | ||
| assert {k: seen[1][k] for k in controls} == controls | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: genlayerlabs/unhardcoded
Length of output: 4803
🏁 Script executed:
Repository: genlayerlabs/unhardcoded
Length of output: 35608
🏁 Script executed:
Repository: genlayerlabs/unhardcoded
Length of output: 42054
🏁 Script executed:
awk 'NR>=499 && NR<=655 {printf "%6d %s\n", NR, $0}' provider_adapters/openai_compatible.pyRepository: genlayerlabs/unhardcoded
Length of output: 8210
Use an async emitter and assert stream success.
stream_openai_compatibleawaitsemit. The synchronous lambda returnsNone, so the first content delta raisesTypeError. The adapter converts this to anetwork_error, but the test ignores the result and checks only the request body.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents