alishahryar1--free-claude-code
6455c63e1d
## Problem FCC reduced reasoning to global and route booleans, mixing client intent, configuration, provider wire capabilities, output visibility, and history replay. That discarded named client efforts, encouraged model-name checks, and made provider behavior inconsistent. ## Changes | Before | After | | --- | --- | | Admin exposed global and route thinking toggles. | Admin exposes **Off**, **From client**, **Low**, **Medium**, **High**, **X-High**, and **Max**; Fable, Opus, Sonnet, and Haiku also expose **Inherit**. | | Request intent was repeatedly reduced to a boolean across routing and providers. | The application boundary resolves one immutable `ReasoningPolicy` with independent control, named effort, and exact positive token budget. | | Provider adapters could infer reasoning behavior from upstream model names or versions. | Provider profiles translate only documented provider-wide wire capabilities; architecture and contributor rules prohibit model-specific reasoning branches. | | Gateway reasoning controls were ad hoc. | [OpenRouter](https://openrouter.ai/docs/guides/best-practices/reasoning-tokens) and [Vercel AI Gateway](https://vercel.com/docs/ai-gateway/models-and-providers) use documented reasoning objects, including exact budgets where representable. | | Named effort forwarding was inconsistent or absent. | [Gemini](https://ai.google.dev/gemini-api/docs/openai), [Ollama](https://docs.ollama.com/api/openai-compatibility), [LM Studio](https://lmstudio.ai/changelog/lmstudio-v0.4.8), [Fireworks](https://docs.fireworks.ai/guides/querying-text-models/reasoning), [Cohere](https://docs.cohere.com/docs/compatibility-api), [Wafer](https://docs.wafer.ai/serverless/api-reference), [Groq](https://console.groq.com/docs/reasoning), [Cerebras](https://inference-docs.cerebras.ai/capabilities/reasoning), [SambaNova](https://docs.sambanova.ai/docs/api-reference/chat-completions/create-chat-based-completion), and [Mistral](https://docs.mistral.ai/studio-api/conversations/reasoning) receive their documented named vocabularies with explicit provider-owned downgrades. | | Boolean thinking controls were mixed into shared conversion. | [DeepSeek](https://api-docs.deepseek.com/guides/thinking_mode/), [Kimi](https://platform.kimi.ai/docs/guide/use-kimi-k2-thinking-model), [Z.ai](https://docs.z.ai/guides/capabilities/thinking-mode), [Cloudflare Workers AI](https://developers.cloudflare.com/changelog/post/2026-04-20-kimi-k2-6-workers-ai/), and [NVIDIA NIM](https://docs.nvidia.com/nim/large-language-models/1.15.0/thinking-budget-control.html) use provider-owned thinking-object or chat-template controls. | | Effort names and output limits could become fabricated reasoning budgets. | Exact budgets remain exact and are forwarded only through documented fields for OpenRouter, Fireworks, LM Studio, NIM, and [llama.cpp](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md); named efforts and output limits are never converted into token budgets. | | New-turn reasoning and prior-turn replay shared one switch. | Every profile independently declares native reasoning replay, `<think>` tag replay, provider-specific replay, or no replay; **Off** suppresses new reasoning output without corrupting required history. | | Providers without a stable generic compute control received guessed controls. | [MiniMax](https://platform.minimax.io/docs/api-reference/text-openai-api) requests split output only, while [GitHub Models](https://docs.github.com/en/rest/models/inference), [Hugging Face Inference Providers](https://huggingface.co/docs/inference-providers/en/tasks/chat-completion), Codestral, and OpenCode keep provider defaults and use only their explicit replay profile. | | OpenAI Responses effort became a lossy Anthropic thinking boolean. | Responses preserves `reasoning.effort` through `output_config`, then resolves it through the same application policy as Messages without inventing a budget. | | Legacy booleans remained the persisted contract. | FCC-owned dotenv files migrate to typed `REASONING_*` values, explicit env files receive an actionable warning, documentation describes the ownership boundary, and the package advances to 4.8.0. | | Reasoning behavior was covered by scattered boolean assertions. | New policy, routing, encoder, provider, Admin, migration, Responses, and smoke contracts pass all five local CI checks: 2,368 tests passed, 40 skipped; 92 smoke tests collect and both live config migration checks pass. | <!-- greptile_comment --> <details open><summary><h3>Greptile Summary</h3></summary> This PR makes reasoning policy client-aware and independent of provider model names. The main changes are: - Adds one immutable reasoning policy resolved at the application boundary. - Adds typed root and route reasoning settings with Admin UI support. - Moves wire controls and history replay behavior into provider profiles. - Migrates owned dotenv files from legacy thinking booleans. - Expands provider, routing, migration, API, and smoke coverage. </details> <h3>Confidence Score: 5/5</h3> This looks safe to merge. No blocking issues found in the changed code. <details><summary><h3><a href="https://www.greptile.com/trex"><img alt="T-Rex" src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg" height="20" align="absmiddle"></a> T-Rex Logs</h3></summary> **What T-Rex did** - Ran the contract-validation test suite with the specified test modules, and the tests reported 78 passed in 1.53s with exit code 0. - Reviewed the complete captured output artifact reasoning-contract-02-after.log to verify the final test outcomes and successful contract validation. <a href="https://app.greptile.com/trex/runs/14792858/artifacts"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifactsDark.svg?v=4"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"><img alt="View all artifacts" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"></picture></a> <sub><a href="https://www.greptile.com/trex"><img alt="T-Rex" src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg" height="14" align="absmiddle"></a> Ran code and verified through T-Rex</sub> </details> <details open><summary><h3>Important Files Changed</h3></summary> | Filename | Overview | |----------|----------| | src/free_claude_code/config/env_migrations.py | Migrates legacy reasoning booleans in owned dotenv files and warns for explicit environment files. | | src/free_claude_code/application/reasoning.py | Resolves client controls and configured preferences into one provider-neutral reasoning policy. | | src/free_claude_code/application/routing.py | Carries route-level reasoning preferences into request-scoped policy resolution. | | src/free_claude_code/providers/openai_chat/reasoning.py | Provides shared provider encoders for reasoning controls and replay behavior. | </details> <sub>Reviews (2): Last reviewed commit: ["chore: release reasoning controls as 4.8..."](https://github.com/alishahryar1/free-claude-code/commit/9d4be767f7dbdca5709474012f43dcdc6f4347e3) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=44984039)</sub> <!-- /greptile_comment -->
185 行
5.5 KiB
Python
185 行
5.5 KiB
Python
"""Application-owned provider execution contracts."""
|
|
|
|
from collections.abc import AsyncIterator
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from free_claude_code.application.execution import ProviderExecutor
|
|
from free_claude_code.application.routing import ResolvedModel, RoutedMessagesRequest
|
|
from free_claude_code.config.reasoning import ReasoningPreference
|
|
from free_claude_code.core.anthropic.models import Message, MessagesRequest
|
|
from free_claude_code.core.async_iterators import AsyncCloseable
|
|
from free_claude_code.core.reasoning import ReasoningPolicy
|
|
|
|
|
|
class FakeProvider:
|
|
def __init__(self) -> None:
|
|
self.preflight_calls: list[tuple[MessagesRequest, ReasoningPolicy]] = []
|
|
self.stream_calls: list[dict[str, object]] = []
|
|
self.stream_close_calls = 0
|
|
|
|
def preflight_stream(
|
|
self,
|
|
request: MessagesRequest,
|
|
*,
|
|
reasoning: ReasoningPolicy,
|
|
) -> None:
|
|
self.preflight_calls.append((request, reasoning))
|
|
|
|
async def stream_response(
|
|
self,
|
|
request: MessagesRequest,
|
|
input_tokens: int = 0,
|
|
*,
|
|
request_id: str | None = None,
|
|
reasoning: ReasoningPolicy,
|
|
) -> AsyncIterator[str]:
|
|
self.stream_calls.append(
|
|
{
|
|
"request": request,
|
|
"input_tokens": input_tokens,
|
|
"request_id": request_id,
|
|
"reasoning": reasoning,
|
|
}
|
|
)
|
|
try:
|
|
yield "event: message_stop\ndata: {}\n\n"
|
|
finally:
|
|
self.stream_close_calls += 1
|
|
|
|
|
|
class FailingPreflightProvider(FakeProvider):
|
|
def preflight_stream(
|
|
self,
|
|
request: MessagesRequest,
|
|
*,
|
|
reasoning: ReasoningPolicy,
|
|
) -> None:
|
|
raise ValueError("invalid provider request")
|
|
|
|
|
|
class FailingStreamConstructionProvider(FakeProvider):
|
|
def stream_response(
|
|
self,
|
|
request: MessagesRequest,
|
|
input_tokens: int = 0,
|
|
*,
|
|
request_id: str | None = None,
|
|
reasoning: ReasoningPolicy,
|
|
) -> AsyncIterator[str]:
|
|
raise RuntimeError("stream construction failed")
|
|
|
|
|
|
def _routed_request() -> RoutedMessagesRequest:
|
|
request = MessagesRequest(
|
|
model="provider-model",
|
|
messages=[Message(role="user", content="hello")],
|
|
)
|
|
return RoutedMessagesRequest(
|
|
request=request,
|
|
resolved=ResolvedModel(
|
|
original_model="gateway-model",
|
|
provider_id="provider",
|
|
provider_model="provider-model",
|
|
provider_model_ref="provider/provider-model",
|
|
reasoning_preference=ReasoningPreference.CLIENT,
|
|
),
|
|
reasoning=ReasoningPolicy.on(),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_executor_uses_structural_provider_port_and_preflights_eagerly() -> None:
|
|
provider = FakeProvider()
|
|
routed = _routed_request()
|
|
request = routed.request
|
|
executor = ProviderExecutor(
|
|
lambda _provider_id: provider,
|
|
token_counter=lambda _messages, _system, _tools: 17,
|
|
)
|
|
|
|
stream = executor.stream(
|
|
routed,
|
|
wire_api="messages",
|
|
raw_log_label="FULL_PAYLOAD",
|
|
raw_log_payload=request.model_dump(),
|
|
request_id="req_application",
|
|
)
|
|
|
|
assert provider.preflight_calls == [(request, ReasoningPolicy.on())]
|
|
assert [chunk async for chunk in stream] == ["event: message_stop\ndata: {}\n\n"]
|
|
assert provider.stream_calls == [
|
|
{
|
|
"request": request,
|
|
"input_tokens": 17,
|
|
"request_id": "req_application",
|
|
"reasoning": ReasoningPolicy.on(),
|
|
}
|
|
]
|
|
assert provider.stream_close_calls == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_closing_executor_stream_closes_provider_stream_once() -> None:
|
|
provider = FakeProvider()
|
|
routed = _routed_request()
|
|
executor = ProviderExecutor(
|
|
lambda _provider_id: provider,
|
|
token_counter=lambda _messages, _system, _tools: 17,
|
|
)
|
|
stream = executor.stream(
|
|
routed,
|
|
wire_api="messages",
|
|
raw_log_label="FULL_PAYLOAD",
|
|
raw_log_payload={},
|
|
request_id="req_early_close",
|
|
)
|
|
|
|
assert await anext(stream) == "event: message_stop\ndata: {}\n\n"
|
|
assert isinstance(stream, AsyncCloseable)
|
|
await stream.aclose()
|
|
|
|
assert provider.stream_close_calls == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stream_construction_failure_remains_deferred_to_iteration() -> None:
|
|
provider = FailingStreamConstructionProvider()
|
|
executor = ProviderExecutor(
|
|
lambda _provider_id: provider,
|
|
token_counter=lambda _messages, _system, _tools: 17,
|
|
)
|
|
|
|
stream = executor.stream(
|
|
_routed_request(),
|
|
wire_api="messages",
|
|
raw_log_label="FULL_PAYLOAD",
|
|
raw_log_payload={},
|
|
request_id="req_deferred_construction",
|
|
)
|
|
|
|
with pytest.raises(RuntimeError, match="stream construction failed"):
|
|
await anext(stream)
|
|
|
|
|
|
def test_executor_preflight_failure_stays_before_token_count_and_stream() -> None:
|
|
provider = FailingPreflightProvider()
|
|
token_counter = MagicMock(return_value=17)
|
|
executor = ProviderExecutor(
|
|
lambda _provider_id: provider,
|
|
token_counter=token_counter,
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="invalid provider request"):
|
|
executor.stream(
|
|
_routed_request(),
|
|
wire_api="messages",
|
|
raw_log_label="FULL_PAYLOAD",
|
|
raw_log_payload={},
|
|
request_id="req_application",
|
|
)
|
|
|
|
token_counter.assert_not_called()
|
|
assert provider.stream_calls == []
|