diff --git a/devplacepy/routers/uploads.py b/devplacepy/routers/uploads.py index 9d31e90a..aea433a7 100644 --- a/devplacepy/routers/uploads.py +++ b/devplacepy/routers/uploads.py @@ -118,7 +118,7 @@ async def generate_image(request: Request, data: Annotated[GenerateImageForm, De } headers = { "Content-Type": "application/json", - "X-App-Reference": "devplace-generate-image-v-1-0-0", + "X-App-Reference": "devplace-image-gen-v-1-0-0", "Authorization": f"Bearer {user.get('api_key', '')}", } try: diff --git a/devplacepy/services/ai_modifier.py b/devplacepy/services/ai_modifier.py index dc788563..d1d27b5e 100644 --- a/devplacepy/services/ai_modifier.py +++ b/devplacepy/services/ai_modifier.py @@ -7,6 +7,7 @@ from devplacepy.config import DEFAULT_MODIFIER_PROMPT, INTERNAL_MODEL from devplacepy.database import add_modifier_usage, get_setting, get_table from devplacepy.services.ai_context import build_context from devplacepy.services.background import background +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_MODIFIER from devplacepy.services.correction import ( CORRECTABLE_FIELDS, gateway_complete, @@ -50,6 +51,7 @@ def modify_text( None, model=modifier_model(), bypass_preamble=True, + app_reference=APP_REFERENCE_MODIFIER, ) diff --git a/devplacepy/services/bot/llm.py b/devplacepy/services/bot/llm.py index 1417bf76..5ab4dd96 100644 --- a/devplacepy/services/bot/llm.py +++ b/devplacepy/services/bot/llm.py @@ -20,6 +20,7 @@ from devplacepy.services.bot.config import ( TRIVIAL_GIST_TERMS, ) from devplacepy.services.bot.handles import MAX_HANDLE_LEN, sanitize_handle +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_BOTS from devplacepy.services.openai_gateway.usage import parse_usage_headers HANDLE_CANDIDATE_TARGET = 8 @@ -71,7 +72,10 @@ class LLMClient: resp = client.post( self.api_url, json=payload, - headers={"Authorization": f"Bearer {self.api_key}"}, + headers={ + "Authorization": f"Bearer {self.api_key}", + "X-App-Reference": APP_REFERENCE_BOTS, + }, ) raw = resp.content logger.info("LLM <<< %s", raw[:2000].decode(errors="replace")) diff --git a/devplacepy/services/community/ai.py b/devplacepy/services/community/ai.py index c8726a6c..057dfc0a 100644 --- a/devplacepy/services/community/ai.py +++ b/devplacepy/services/community/ai.py @@ -97,10 +97,11 @@ class AiAssistant: if not self.enabled(): return None from devplacepy.services.correction import gateway_complete + from devplacepy.services.openai_gateway.apps import APP_REFERENCE_COMMUNITY model = get_setting("community_ai_model", "") or INTERNAL_MODEL reply, usage = await asyncio.to_thread( - gateway_complete, internal_gateway_key(), system, user, AI_TIMEOUT_SECONDS, None, model, True, False + gateway_complete, internal_gateway_key(), system, user, AI_TIMEOUT_SECONDS, None, model, True, False, APP_REFERENCE_COMMUNITY ) if usage is None: logger.warning("ai: the %s model is unreachable, continuing with heuristics only", model) diff --git a/devplacepy/services/correction.py b/devplacepy/services/correction.py index 81b9d905..2d7a59c8 100644 --- a/devplacepy/services/correction.py +++ b/devplacepy/services/correction.py @@ -8,6 +8,7 @@ from concurrent.futures import ThreadPoolExecutor import httpx from devplacepy import stealth +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_CORRECTION, APP_REFERENCE_HEADER from devplacepy.config import ( DEFAULT_CORRECTION_PROMPT, INTERNAL_GATEWAY_URL, @@ -101,6 +102,7 @@ def gateway_complete( model: str = INTERNAL_MODEL, bypass_preamble: bool = False, check_structure: bool = False, + app_reference: str = APP_REFERENCE_CORRECTION, ) -> tuple[str, dict | None]: text = text or "" if not text.strip(): @@ -117,7 +119,7 @@ def gateway_complete( payload["bypass_preamble"] = True headers = { "Content-Type": "application/json", - "X-App-Reference": "devplace-correction-v-1-0-0", + APP_REFERENCE_HEADER: app_reference, } if api_key: headers["Authorization"] = f"Bearer {api_key}" diff --git a/devplacepy/services/devii/llm.py b/devplacepy/services/devii/llm.py index e8a02904..ecf83929 100644 --- a/devplacepy/services/devii/llm.py +++ b/devplacepy/services/devii/llm.py @@ -9,6 +9,7 @@ from typing import Any, Callable import httpx from devplacepy import stealth +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_DEVII from .config import Settings from .cost import record_cost, record_usage from .errors import LLMError @@ -67,6 +68,7 @@ class LLMClient: headers={ "Authorization": f"Bearer {settings.ai_key}", "Content-Type": "application/json", + "X-App-Reference": APP_REFERENCE_DEVII, }, ) diff --git a/devplacepy/services/gitea/planning.py b/devplacepy/services/gitea/planning.py index 98a6370e..fd914ad5 100644 --- a/devplacepy/services/gitea/planning.py +++ b/devplacepy/services/gitea/planning.py @@ -228,7 +228,7 @@ async def generate_plan( } headers = { "Content-Type": "application/json", - "X-App-Reference": "devplace-gitea-planning-v-1-0-0", + "X-App-Reference": "devplace-gitea-plan-v-1-0-0", } if config.ai_key: headers["Authorization"] = f"Bearer {config.ai_key}" diff --git a/devplacepy/services/jobs/seo_meta_service.py b/devplacepy/services/jobs/seo_meta_service.py index 06742274..61b635e1 100644 --- a/devplacepy/services/jobs/seo_meta_service.py +++ b/devplacepy/services/jobs/seo_meta_service.py @@ -24,6 +24,7 @@ from devplacepy.services.base import ConfigField from devplacepy.services.correction import gateway_complete, new_usage_totals from devplacepy.services.jobs import queue from devplacepy.services.jobs.base import JobService +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_SEO_META from devplacepy.services.openai_gateway.usage import usage_metric_cards from devplacepy.services.seo_meta import ( BODY_FIELD, @@ -174,6 +175,7 @@ class SeoMetaService(JobService): GENERATION_TIMEOUT_SECONDS, model=model, bypass_preamble=True, + app_reference=APP_REFERENCE_SEO_META, ) if usage: for key in totals: diff --git a/devplacepy/services/openai_gateway/CLAUDE.md b/devplacepy/services/openai_gateway/CLAUDE.md index 19063705..3ee510d6 100644 --- a/devplacepy/services/openai_gateway/CLAUDE.md +++ b/devplacepy/services/openai_gateway/CLAUDE.md @@ -32,6 +32,10 @@ Callers may send an optional `X-App-Reference` header to tag gateway calls by ap - **All internal callers** (news, bots, Devii, correction, jobs, deepsearch, dbapi, gitea) should pass a `devplace--v---` value, e.g. `devplace-devii-v-1-0-0`, `devplace-news-v-1-0-0`. - The column is indexed (`CREATE INDEX IF NOT EXISTS`) for fast per-app queries. +## Provider attribution headers (`apps.py`) + +`apps.py` is the single specification of every internal gateway app: the `GATEWAY_APPS` registry (`GatewayApp(reference, name)`, title `DevPlace `), the `APP_REFERENCE_*` constants callers import, `validate_app_reference` and `attribution_headers(app_reference)`. Every upstream request (chat, embeddings, images, vision sub-call, fallback retries) carries, for ALL providers and not only OpenRouter: `X-OpenRouter-Title` (OpenRouter's current app-title header, https://openrouter.ai/docs/app-attribution), the legacy `X-Title` with the same value, and `HTTP-Referer` = `DEVPLACE_SITE_URL` when set. The title is per app (`DevPlace Devii`, `DevPlace News`, ...), so a provider dashboard splits spend by sub-app; unknown or `default` references fall back to `DevPlace`. OpenRouter documents no sub-app field beyond the title, so the title is the carrier. Apps: Devii, News, Bots, AI Correction, AI Modifier, Quiz Grading, SEO Metadata, DeepSearch, AI Usage Analyzer, Issue Enhance, Issue Planning, DB API, Awards, Image Generation, Community Discovery, Community Sync. A new internal caller adds one registry row and sends `X-App-Reference`. `gateway_complete(..., app_reference=)` carries the label for correction-style callers. + ## Per-worker runtime **Serves in every worker.** Config/enabled come from `site_settings` (via the service's `get_config()`/`is_enabled()`), so any uvicorn worker answers - not just the supervisor worker. Per-worker runtime (`GatewayRuntime`: `httpx.AsyncClient` pool + `asyncio.Semaphore` sized to `gateway_instances`, plus counters and a `VisionCache`) is created lazily on first request and rebuilt when `instances`/`timeout`/cache size change. `gateway_instances` is the **scaling knob** (concurrency per worker); process scaling is uvicorn workers. @@ -204,12 +208,13 @@ When adding a routed value, overlay it as the matching `gateway_*` cfg key so th **Tests:** `tests/unit/services/openai_gateway/routing.py` (overlay/economy/kind isolation), `tests/unit/services/openai_gateway/gateway.py::test_model_route_overrides_upstream` (end-to-end through `handle_chat`), and `tests/api/admin/gateway/` (admin CRUD + validation + role gating). -## Automatic model fallback (`fallback_model`, one hop, per route) +## Automatic model fallback (`fallback_model`, chained, per route) -Any `gateway_models` route (chat, embed, or image) can optionally name `fallback_model`: another already-configured `source_model` of the SAME kind, tried once, automatically, when the primary route fails. This is admin-configured on the `/admin/gateway` model form as a select box grouped into one `` per kind over every OTHER route (the public `source_model` names such as `molodetz`/`molodetz-pro`, never the internal `target_model` a provider actually sees) - never a free-text field, so a fallback can only ever point at a model the gateway already knows how to serve. +Any `gateway_models` route (chat, embed, or image) can optionally name `fallback_model`: another already-configured `source_model` of the SAME kind, tried automatically, when the primary route fails - and when THAT attempt fails, its own fallback is tried next, walking the whole line. This is admin-configured on the `/admin/gateway` model form as a select box grouped into one `` per kind over every OTHER route (the public `source_model` names such as `molodetz`/`molodetz-pro`, never the internal `target_model` a provider actually sees) - never a free-text field, so a fallback can only ever point at a model the gateway already knows how to serve. -- **Trigger.** `_call_failed(resp, exc, timing)` in `gateway.py` treats a call as failed when the circuit breaker rejected it, the upstream connection raised, or the upstream answered with any status `>= 400`. This runs AFTER `_send`'s own `gateway_max_retries` retries against the primary model are exhausted - a fallback is the next escalation once retrying the SAME model has already given up, not a replacement for that retry loop. -- **One hop, never a chain.** `routing.resolve_fallback(source_model, kind)` resolves the primary route, reads its `fallback_model`, and resolves THAT model's own route (must exist, be active, and share the kind) - it does NOT recurse into the fallback's own `fallback_model`, so there is no possibility of a cycle or an unbounded retry chain. A route that fails even after redirecting to its fallback returns the fallback attempt's own failure to the caller. +- **Trigger.** `_call_failed(resp, exc, timing)` in `gateway.py` treats a call as failed when the circuit breaker rejected it, the upstream connection raised, or the upstream answered with any status `>= 400`. This runs AFTER `_send`'s own `gateway_max_retries` retries against the primary model are exhausted - a fallback is the next escalation once retrying the SAME model has already given up, not a replacement for that retry loop. Only `bad_request` never falls back; a 400 naming the model (`Model is unavailable`, `model not found`, `unknown model`, `no such model`, `model_not_found`) classifies as `model_not_found` and DOES fall back, since trying a different model is the correct recovery. +- **A guarded chain, not one hop.** `routing.resolve_fallback_chain(source_model, kind)` walks `fallback_model` links collecting usable routes: inactive and unhealthy links are skipped transparently (their own fallback is followed instead), the source itself is never re-added, a visited set makes cycles impossible, and `MAX_FALLBACK_HOPS` (5) caps the line. `handle_chat`/`handle_embeddings`/`handle_images` attempt each member in turn until one succeeds (an `attempted_routes` set additionally skips anything already tried, so the context precheck's jump never causes a repeat attempt). A route that fails down the whole line returns the last attempt's own failure to the caller. The old one-hop `routing.resolve_fallback` stays as a tested primitive but is no longer on the request path. +- **Direct requests preroute around unhealthy models.** `routing.resolve_entry_route(source_model, kind)` returns the requested route when healthy, else the first usable chain member - so a request naming a bad model starts on its fallback WITHOUT attempting the bad model first (logged as `is unhealthy, starting on`). When the line is empty the original route is still tried: health is advisory, and the model may have recovered since its last test. Only manual tests write health - a request-time failure never marks a route, so a blip can never hide a model. - **Self-reference is rejected at write time.** `ModelRouteIn._check_fallback_is_not_self` (pydantic `model_validator`) refuses `fallback_model == source_model`; `routers/admin/gateway_configs.py::_check_fallback` (shared by the JSON `save_model_json` route and both backend-rendered model-route pages) additionally rejects a `fallback_model` that does not resolve to an existing, same-kind route (`routing.model_store.get(...)`) before writing the row, since a pydantic validator alone cannot see other rows. - **Rebuilt like a fresh routed call, not retried in place.** On failure, `handle_chat`/`handle_embeddings`/`handle_images` recompute the overlay from `base_cfg` (the pre-primary-overlay config) via `chat_overlay(fallback_route.source_model, base_cfg)`/`embed_overlay`/`image_overlay`, so the fallback gets its OWN provider, URL, key, and pricing - never the primary route's. `pricing`/`context_map` for the ledger are (re)computed AFTER the fallback decision so the recorded cost always reflects whichever model actually served the request. - **One ledger row per client call, not one per attempt.** The primary failure never writes a `gateway_usage_ledger` row by itself (only `_send`'s own internal retries and the circuit breaker counters observe it); `finalize()`/`self._ledger.record(...)` still runs exactly once, after the fallback decision, with `model` = whichever model ultimately answered and `requested_model` = the ORIGINALLY requested model name (an existing-but-previously-unpopulated ledger column, now populated by all three handlers) - so a fallback shows up in `/admin/ai-usage` as "requested X, served Y" rather than as two calls. @@ -217,7 +222,11 @@ Any `gateway_models` route (chat, embed, or image) can optionally name `fallback - **Known limitation, inherited from the existing provider routing design, not introduced by this feature: the circuit breaker (`self._breaker`) is one instance per `GatewayRuntime`, shared across every route.** A primary attempt that trips the breaker can make the immediately-following fallback attempt itself see `circuit_open` too. This mirrors the pre-existing behavior for the multi-provider routing overlay in general (a struggling route's failures already could open the breaker in front of an unrelated route before fallback existed) and was left as-is rather than redesigning per-provider breakers, which is a materially larger change than "add a fallback model." - **Deliberately NOT covered:** `gateway_max_retries`/`gateway_retry_backoff_ms`, the circuit breaker, and hard docker-adjacent settings are untouched - fallback is additive on top of them, not a replacement retry mechanism. -**Tests:** `tests/unit/services/openai_gateway/routing.py` (`resolve_fallback` self-reference/kind/active-flag guards), `tests/unit/services/openai_gateway/gateway.py` (`test_chat_falls_back_when_the_primary_model_fails`, `test_chat_returns_the_fallback_failure_when_both_models_fail`, `test_chat_without_a_fallback_never_makes_a_second_call`), and `tests/api/admin/gateway/models.py` (`test_model_route_fallback_round_trip`, `test_model_route_fallback_must_reference_an_existing_route`, `test_model_route_fallback_must_be_the_same_kind`, `test_model_route_fallback_rejects_self_reference`). +**Tests:** `tests/unit/services/openai_gateway/routing.py` (`resolve_fallback` self-reference/kind/active-flag guards, chain skip/cycle/hop-cap/entry tests), `tests/unit/services/openai_gateway/gateway.py` (`test_chat_falls_back_when_the_primary_model_fails`, `test_chat_returns_the_fallback_failure_when_both_models_fail`, `test_chat_without_a_fallback_never_makes_a_second_call`, `test_chat_walks_fallback_chain_past_failures`, `test_chat_preroutes_around_unhealthy_models`, `test_chat_tries_original_when_chain_is_exhausted`), and `tests/api/admin/gateway/models.py` (`test_model_route_fallback_round_trip`, `test_model_route_fallback_must_reference_an_existing_route`, `test_model_route_fallback_must_be_the_same_kind`, `test_model_route_fallback_rejects_self_reference`). + +## Route health tests (`health.py`, Test buttons, unhealthy hiding) + +Every model route carries `health_status` (`""` untested, `"ok"`, `"bad"`), `health_code`, `health_message`, `health_checked_at` - plain columns on `gateway_models` (same ensure idiom as tier2/off-peak), written ONLY by manual tests. The models tab has a per-row Test button plus Test-all (sequential, one real call per route); both are plain POST forms redirecting back to the grid. `health.probe_route(source)` resolves the route and sends one minimal non-stream call straight at its upstream (chat: tiny completion, transpiled for Responses upstreams with `reasoning minimal`; embed: one vector; image: the fixed `a hedgehog and a bear` prompt), bypassing fallback and the ledger, 30s cap - then `record_health` persists ok/bad and invalidates the routing cache, and the router audits `gateway.model.test` / `gateway.models.test_all`. A bad route is hidden from `GET /v1/models`, tinted in the grid with an `unhealthy` badge plus small `code: message`, and skipped transparently by the fallback chain above. A passing image test redirects with `test_image=` and the grid pre-opens the standard modal showing the picture (URLs over ~2000 chars are not passed through). Cost honesty: chat/embed probes are sub-cent; an image test spends one generation - the modal shows what was paid for. ## Tiered (context-length) and off-peak pricing (model-agnostic variable pricing) @@ -263,13 +272,69 @@ boundary (`_chat_upstream_body`), so fallbacks can cross protocols per attempt. Responses upstreams skip `apply_thinking`; reasoning effort comes from the client (`reasoning_effort`, `reasoning.effort`, thinking flags) or defaults to `{"effort": "minimal"}` when thinking -is off - never `"none"`, which is not a value in the Responses -reasoning-effort enum (confirmed by a real upstream rejecting it: -`minimal/low/medium/high/xhigh/max`). `stream_options.include_usage` +is off - `"none"` is listed in OpenAI's shared `ReasoningEffort` enum +but the Meta upstream rejects it (measured: only +`minimal/low/medium/high/xhigh/max` accepted), so `minimal` is the +portable disabled value. Usage token-details ride both object +conversions (`input_tokens_details.cached_tokens` <-> +`prompt_tokens_details.cached_tokens`, +`output_tokens_details.reasoning_tokens` <-> +`completion_tokens_details.reasoning_tokens`) so cache/reasoning cost +splits survive the round trip. `stream_options.include_usage` is never sent upstream (usage -arrives on `response.completed`); the client's flag still controls -the relayed usage chunk. Non-200 `{error}` envelopes and 200 -failed/incomplete Response objects are detected and relayed as -Chat-style errors. Passthrough stays raw (no transpiling) and strips -a trailing `/responses` base segment so `/openai/v1/responses` routes -without doubling the path. +arrives on `response.completed`); the translator emits the spec shape +- terminal chunk with `finish_reason`, then a usage-only chunk with +empty `choices` - and the client's flag still controls whether that +usage chunk is relayed. Only the Chat side of each conversion pair is +live in production (`gateway.py` calls `chat_to_responses`, +`responses_object_to_chat_completion`, `ResponsesToChatStream` plus +the error helpers); the Responses-request side is tested but unused, +since passthrough stays raw. Non-200 `{error}` envelopes +(message/type/code/param) and 200 failed/incomplete Response objects +are detected and relayed as Chat-style errors. Passthrough stays raw +(no transpiling) and strips a trailing `/responses` base segment so +`/openai/v1/responses` routes without doubling the path. + +A Meta provider row either uses a pay-as-you-go key or sets +`client_profile="muse"`: Meta's subscription applies only to the +auto-connected Muse Code CLI key ("for use with Muse Code only", +dev.meta.ai/docs/muse-code/subscriptions), and since the 2026-10-29 +rollout `api.meta.ai` answers any other client with `403 +client_attestation_denied`. The `muse` profile +(`muse_code.muse_impersonation_headers`, same seam as the `opencode` +profile) stamps the captured Muse Code 1.4.14 headers +(`User-Agent: muse-build/...`, `X-Client-Id: tbh:exec`, the four +`X-Tbh-*`/`X-Meta-Ai-Gateway-Session-Id` UUID-7 session headers sharing +one process-stable id, `X-Tbh-Agent-Role: main`, +`X-Tbh-Contributor-Mode: 1`) on chat/embed/images forwards AND on the +`/muse-code/models` probe; env overrides are `MUSE_CLIENT_VERSION`, +`MUSE_CLIENT_BUILD`, `MUSE_CLIENT_SURFACE`, `MUSE_CLIENT_USER_AGENT`, +`MUSE_CONTRIBUTOR_MODE`. The default contributor mode `1` matches the +verified-200 probe but opts gateway content into Meta training - flip +it to `0` (a legitimate headless-client state) if that is unwanted. +The applied profile is visible in the services log: the routed-model +line gains `(client profile 'muse')` when a profile's headers are +stamped, so a 403 with no such line means the row still says generic. +Auth-class upstream rejections log their remediation via +`_upstream_auth_guidance` (chat/embed/images alike, relay body never +rewritten): residual attestation denials point at the denylist below +or a pay-as-you-go key, 401s point at the route's provider key. The +403 still classifies as `auth`, so a configured `fallback_model` is +tried exactly like any other auth failure. + +The `muse` headers are defense in depth, NOT the attestation fix: +probes proved the key works from any client/TLS stack for plain calls +and the gate only fires on reserved `tools[].name` values. The real +fix is automatic tool-name aliasing (`muse_code.tool_aliases_for` / +`alias_chat_tools`, wired in `_chat_upstream_body` for Meta Responses +attempts only): any declared/history/tool_choice name exactly matching +`MUSE_RESERVED_TOOL_NAMES` (20 names, each measured denying while +near-misses pass) is rewritten to `ext_` (growing underscores +on collision) and restored on the way back +(`restore_completion_names` / `restore_chunk_names`), so the model +sees one consistent world and the client never sees the alias. The +map is rebuilt deterministically from the same payload at both ends, +so fallbacks crossing providers need no state. Clean requests pass +through untouched (same object, no log); aliased ones log +`Meta reserved tool names aliased: bash->ext_bash, ...`. If 403s +return, re-probe: Meta may have added a builtin. diff --git a/devplacepy/services/openai_gateway/apps.py b/devplacepy/services/openai_gateway/apps.py new file mode 100644 index 00000000..1c94109d --- /dev/null +++ b/devplacepy/services/openai_gateway/apps.py @@ -0,0 +1,77 @@ +# retoor + +import re +from dataclasses import dataclass + +from devplacepy import config as devplace_config + +PLATFORM_TITLE = "DevPlace" +TITLE_HEADER = "X-OpenRouter-Title" +LEGACY_TITLE_HEADER = "X-Title" +REFERER_HEADER = "HTTP-Referer" +APP_REFERENCE_HEADER = "X-App-Reference" +APP_REFERENCE_PATTERN = re.compile(r"^[a-zA-Z0-9_.-]{1,30}$") +DEFAULT_APP_REFERENCE = "default" + + +@dataclass(frozen=True) +class GatewayApp: + reference: str + name: str + + @property + def title(self) -> str: + return f"{PLATFORM_TITLE} {self.name}" + + +GATEWAY_APPS: tuple[GatewayApp, ...] = ( + GatewayApp("devplace-devii-v-1-0-0", "Devii"), + GatewayApp("devplace-news-v-1-0-0", "News"), + GatewayApp("devplace-bots-v-1-0-0", "Bots"), + GatewayApp("devplace-correction-v-1-0-0", "AI Correction"), + GatewayApp("devplace-modifier-v-1-0-0", "AI Modifier"), + GatewayApp("devplace-quiz-grading-v-1-0-0", "Quiz Grading"), + GatewayApp("devplace-seo-meta-v-1-0-0", "SEO Metadata"), + GatewayApp("devplace-deepsearch-v-1-0-0", "DeepSearch"), + GatewayApp("devplace-isslop-v-1-0-0", "AI Usage Analyzer"), + GatewayApp("devplace-gitea-enhance-v-1-0-0", "Issue Enhance"), + GatewayApp("devplace-gitea-plan-v-1-0-0", "Issue Planning"), + GatewayApp("devplace-dbapi-v-1-0-0", "DB API"), + GatewayApp("devplace-awards-v-1-0-0", "Awards"), + GatewayApp("devplace-image-gen-v-1-0-0", "Image Generation"), + GatewayApp("devplace-devii-rsearch-v-1-0-0", "Devii Research"), + GatewayApp("devplace-community-v-1-0-0", "Community Discovery"), + GatewayApp("devplace-sync-v-1-0-0", "Community Sync"), +) + +_APPS_BY_REFERENCE = {app.reference: app for app in GATEWAY_APPS} + +APP_REFERENCE_DEVII = "devplace-devii-v-1-0-0" +APP_REFERENCE_NEWS = "devplace-news-v-1-0-0" +APP_REFERENCE_BOTS = "devplace-bots-v-1-0-0" +APP_REFERENCE_CORRECTION = "devplace-correction-v-1-0-0" +APP_REFERENCE_MODIFIER = "devplace-modifier-v-1-0-0" +APP_REFERENCE_QUIZ_GRADING = "devplace-quiz-grading-v-1-0-0" +APP_REFERENCE_SEO_META = "devplace-seo-meta-v-1-0-0" +APP_REFERENCE_COMMUNITY = "devplace-community-v-1-0-0" +APP_REFERENCE_SYNC = "devplace-sync-v-1-0-0" + + +def validate_app_reference(value: str) -> str: + stripped = (value or "").strip() + if not stripped or not APP_REFERENCE_PATTERN.match(stripped): + return DEFAULT_APP_REFERENCE + return stripped + + +def app_title(app_reference: str) -> str: + app = _APPS_BY_REFERENCE.get(app_reference or "") + return app.title if app else PLATFORM_TITLE + + +def attribution_headers(app_reference: str = DEFAULT_APP_REFERENCE) -> dict: + title = app_title(app_reference) + headers = {TITLE_HEADER: title, LEGACY_TITLE_HEADER: title} + if devplace_config.SITE_URL: + headers[REFERER_HEADER] = devplace_config.SITE_URL + return headers diff --git a/devplacepy/services/openai_gateway/gateway.py b/devplacepy/services/openai_gateway/gateway.py index 019367bd..0546d1ba 100644 --- a/devplacepy/services/openai_gateway/gateway.py +++ b/devplacepy/services/openai_gateway/gateway.py @@ -9,16 +9,18 @@ from typing import Optional import httpx from fastapi.responses import JSONResponse, Response, StreamingResponse -from devplacepy import config as devplace_config from devplacepy import stealth from devplacepy.services.background import background from devplacepy.services.openai_gateway import config +from devplacepy.services.openai_gateway import muse_code +from devplacepy.services.openai_gateway.apps import TITLE_HEADER, attribution_headers from devplacepy.services.openai_gateway.reliability import CircuitBreaker, retry_send from devplacepy.services.openai_gateway.routing import ( chat_overlay, embed_overlay, image_overlay, - resolve_fallback, + resolve_entry_route, + resolve_fallback_chain, ) from devplacepy.services.openai_gateway.responses import ( ResponsesToChatStream, @@ -63,6 +65,9 @@ def _call_failed(resp, exc, timing: dict) -> bool: # genuinely recover a request the primary couldn't fit - see # _fits_context_window, which additionally skips a doomed primary attempt # outright when its own declared window is already known to be too small. +# model_not_found is excluded for the same reason: an upstream 400/404 saying +# the model is unavailable names the MODEL, not the request shape, so trying +# a different model is the correct recovery, not a guaranteed repeat failure. # Every other failure class (timeout, connection error, circuit-open, auth, # rate limit, 5xx, an embedded 200-with-error body) still falls back too, # since a different provider/model plausibly can recover. @@ -103,6 +108,44 @@ def _embedded_failure_message( return embedded_error_message(data) +def _upstream_auth_guidance(resp, content: dict) -> Optional[str]: + # Auth-class upstream rejections carry their remediation in the log, next + # to the status/body line already logged at the call site - the relayed + # error body itself is never rewritten. + if resp is None or resp.status_code not in (401, 403): + return None + content_error = content.get("error") + if not isinstance(content_error, dict): + content_error = {} + error_code = content_error.get("code") + if error_code is None: + try: + raw_body = resp.json() + except (ValueError, httpx.HTTPError): + raw_body = None + if isinstance(raw_body, dict): + raw_error = raw_body.get("error") + if isinstance(raw_error, dict): + error_code = raw_error.get("code") + if resp.status_code == 403 and error_code == "client_attestation_denied": + return ( + "client_attestation_denied from Meta despite tool-name " + "aliasing: Meta gates tools[] names matching its builtins, " + "which this gateway already rewrites - so either Meta " + "added a reserved name not in MUSE_RESERVED_TOOL_NAMES, " + "or the key itself is now gated. Last resort: a " + "pay-as-you-go key on this provider/route." + ) + if resp.status_code == 401: + detail = str(content_error.get("message") or "")[:160] + return ( + "upstream rejected this route's API key (401%s): check " + "the provider api_key for this route - unknown user, " + "revoked key, or wrong environment." % (f": {detail}" if detail else "") + ) + return None + + def _chat_upstream_body( payload: dict, client_body: dict, cfg: dict, protocol: str, log ) -> dict: @@ -110,6 +153,15 @@ def _chat_upstream_body( # precheck, fallback); only the bytes on the wire change per protocol. if protocol != "responses": return payload + if muse_code.is_meta_upstream(cfg.get("gateway_upstream_url")): + aliases = muse_code.tool_aliases_for(payload) + if aliases: + payload = muse_code.alias_chat_tools(payload, aliases) + renamed = ", ".join( + f"{original}->{alias}" + for alias, original in sorted(aliases.items()) + ) + log(f"Meta reserved tool names aliased: {renamed}") send_body, warnings = chat_to_responses(payload) if warnings: shown = "; ".join(warnings[:3]) @@ -127,6 +179,17 @@ def _chat_upstream_body( return send_body +def _response_tool_aliases(payload: dict, protocol: str, cfg: dict) -> dict: + # Rebuilds the exact map _chat_upstream_body applied on the wire for + # this attempt (same payload, same deterministic rule), so response + # names restore without threading state through the fallback path. + if protocol != "responses": + return {} + if not muse_code.is_meta_upstream(cfg.get("gateway_upstream_url")): + return {} + return muse_code.tool_aliases_for(payload) + + def _apply_served_model(headers: dict, resp) -> dict: # Everything else in `headers` (cost, tokens, latency, context) is our # own measurement and must never be overwritten by an upstream that @@ -140,16 +203,6 @@ def _apply_served_model(headers: dict, resp) -> dict: return headers -def _attribution_headers() -> dict: - # OpenRouter's optional HTTP-Referer/X-Title (openrouter.ai/docs/api-reference/overview) - # attribute calls to DevPlace on their rankings; harmless extra headers for - # any other upstream, so sent unconditionally rather than dialect-gated. - headers: dict = {"X-Title": "DevPlace"} - if devplace_config.SITE_URL: - headers["HTTP-Referer"] = devplace_config.SITE_URL - return headers - - def _extra_provider_headers(cfg: dict) -> dict: # Set by chat_overlay/embed_overlay/image_overlay for a provider that # needs special request headers (e.g. OpenCode Zen's client-identity @@ -157,10 +210,10 @@ def _extra_provider_headers(cfg: dict) -> dict: return cfg.get("gateway_extra_request_headers") or {} -def _fallback_headers(cfg: dict, key_field: str) -> dict: +def _fallback_headers(cfg: dict, key_field: str, app_reference: str) -> dict: headers = { "Content-Type": "application/json", - **_attribution_headers(), + **attribution_headers(app_reference), **_extra_provider_headers(cfg), } if cfg.get(key_field): @@ -397,11 +450,35 @@ class GatewayRuntime: ): log = log or (lambda message: None) bypass_preamble = bool(body.pop("bypass_preamble", False)) and bypass_allowed - overlay = chat_overlay(body.get("model"), cfg) + entry_route = resolve_entry_route(body.get("model"), "chat") + overlay = chat_overlay( + entry_route.source_model + if entry_route is not None + else body.get("model"), + cfg, + ) base_cfg = cfg + fallback_used_route = None if overlay: cfg = {**cfg, **overlay} - log(f"routed model {body.get('model')!r} -> {cfg['gateway_model']!r}") + routed = ( + f"routed model {body.get('model')!r} -> {cfg['gateway_model']!r}" + ) + if overlay.get("gateway_client_profile"): + routed += ( + " (client profile " + f"{overlay['gateway_client_profile']!r})" + ) + log(routed) + if ( + entry_route is not None + and (body.get("model") or "").strip() != entry_route.source_model + ): + log( + f"model {body.get('model')!r} is unhealthy, starting on " + f"fallback {entry_route.source_model!r}" + ) + fallback_used_route = entry_route.source_model protocol = cfg.get("gateway_upstream_protocol") or "chat_completions" client, sem = self._ensure(cfg) pricing = pricing_from_cfg(cfg) @@ -412,7 +489,7 @@ class GatewayRuntime: vision_cost = 0.0 if cfg["gateway_vision_enabled"]: - attribution = _attribution_headers() + attribution = attribution_headers(app_reference) augmenter = VisionAugmenter( cfg["gateway_vision_url"], cfg["gateway_vision_model"], @@ -430,7 +507,7 @@ class GatewayRuntime: "gateway_retry_backoff_ms", config.RETRY_BACKOFF_MS_DEFAULT ), referer=attribution.get("HTTP-Referer", ""), - title=attribution.get("X-Title", ""), + title=attribution.get(TITLE_HEADER, ""), ) messages = await augmenter.augment_messages(client, messages) self.vision_calls += augmenter.calls @@ -452,14 +529,17 @@ class GatewayRuntime: else: model = cfg["gateway_model"] log(f"requested model {requested!r} has no route, falling back to {model!r}") - # Fallback lookup is keyed on whichever route (if any) actually matched - # the client's raw request, never on the *effective* model: overlay - # unconditionally forces gateway_force_model, so "was a specific route - # matched" cannot be read off that flag - it is always true once any - # route (including "molodetz" itself) has matched. An unmatched request - # normalizes to "molodetz" so its own configured fallback still protects - # the default path, instead of silently never being consulted. - fallback_key = requested if overlay is not None else "molodetz" + # The attempt line starts after whichever route actually serves the + # primary attempt (the prerouted entry route, never the raw request): + # overlay unconditionally forces gateway_force_model, so "was a + # specific route matched" cannot be read off that flag - it is always + # true once any route (including "molodetz" itself) has matched. An + # unmatched request normalizes to "molodetz" so its own configured + # fallback still protects the default path, instead of silently never + # being consulted. + chain_start = ( + entry_route.source_model if entry_route is not None else "molodetz" + ) stream = bool(body.get("stream")) include_usage = bool((body.get("stream_options") or {}).get("include_usage")) @@ -506,7 +586,7 @@ class GatewayRuntime: headers = { "Content-Type": "application/json", - **_attribution_headers(), + **attribution_headers(app_reference), **_extra_provider_headers(cfg), } if cfg["gateway_api_key"]: @@ -516,18 +596,19 @@ class GatewayRuntime: "No upstream API key configured (gateway_api_key / DEEPSEEK_API_KEY / OPENROUTER_API_KEY); upstream will likely reject the request" ) - fallback_used_route = None + attempted_routes = ( + {entry_route.source_model} if entry_route is not None else set() + ) context_window = _route_context_window(cfg, model) if context_window: estimated_tokens = _estimate_request_tokens(payload) if estimated_tokens > context_window: - precheck_route = resolve_fallback(fallback_key, "chat") - precheck_overlay = ( - chat_overlay(precheck_route.source_model, base_cfg) - if precheck_route is not None - else None - ) - if precheck_overlay: + for precheck_route in resolve_fallback_chain(chain_start, "chat"): + precheck_overlay = chat_overlay( + precheck_route.source_model, base_cfg + ) + if not precheck_overlay: + continue fallback_window = _route_context_window( precheck_overlay, precheck_overlay["gateway_model"] ) @@ -539,6 +620,7 @@ class GatewayRuntime: f"{precheck_route.source_model!r} -> {precheck_overlay['gateway_model']!r}" ) fallback_used_route = precheck_route.source_model + attempted_routes.add(precheck_route.source_model) cfg = {**base_cfg, **precheck_overlay} protocol = ( cfg.get("gateway_upstream_protocol") @@ -547,7 +629,8 @@ class GatewayRuntime: model = cfg["gateway_model"] payload = dict(payload) payload["model"] = model - headers = _fallback_headers(cfg, "gateway_api_key") + headers = _fallback_headers(cfg, "gateway_api_key", app_reference) + break send_start = time.monotonic() send_body = _chat_upstream_body(payload, body, cfg, protocol, log) @@ -575,50 +658,51 @@ class GatewayRuntime: requested_model = requested body_error = _embedded_failure_message(resp, stream, protocol) - if fallback_used_route is None and _should_fallback(resp, exc, timing, body_error): + for fallback_route in resolve_fallback_chain(chain_start, "chat"): + if fallback_route.source_model in attempted_routes: + continue + if not _should_fallback(resp, exc, timing, body_error): + break if body_error: log(f"model {requested!r} returned 200 with an embedded error: {body_error}") - fallback_route = resolve_fallback(fallback_key, "chat") - fallback_overlay = ( - chat_overlay(fallback_route.source_model, base_cfg) - if fallback_route is not None - else None + fallback_overlay = chat_overlay(fallback_route.source_model, base_cfg) + if not fallback_overlay: + continue + log( + f"model {requested!r} failed, falling back to " + f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_model']!r}" ) - if fallback_overlay: - log( - f"model {requested!r} failed, falling back to " - f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_model']!r}" - ) - fallback_used_route = fallback_route.source_model - cfg = {**base_cfg, **fallback_overlay} - protocol = ( - cfg.get("gateway_upstream_protocol") or "chat_completions" - ) - model = cfg["gateway_model"] - payload = dict(payload) - payload["model"] = model - headers = _fallback_headers(cfg, "gateway_api_key") - send_start = time.monotonic() - send_body = _chat_upstream_body( - payload, body, cfg, protocol, log - ) - resp, exc, timing = await self._send( - client, - sem, - "POST", - cfg["gateway_upstream_url"], - headers, - cfg, - log, - json_body=send_body, - stream=stream, - ) - if resp is not None and stream and resp.status_code != 200: - try: - await resp.aread() - except httpx.HTTPError as e: # noqa: BLE001 - log(f"failed to read fallback non-200 stream error body: {e}") - body_error = _embedded_failure_message(resp, stream, protocol) + fallback_used_route = fallback_route.source_model + attempted_routes.add(fallback_route.source_model) + cfg = {**base_cfg, **fallback_overlay} + protocol = ( + cfg.get("gateway_upstream_protocol") or "chat_completions" + ) + model = cfg["gateway_model"] + payload = dict(payload) + payload["model"] = model + headers = _fallback_headers(cfg, "gateway_api_key", app_reference) + send_start = time.monotonic() + send_body = _chat_upstream_body( + payload, body, cfg, protocol, log + ) + resp, exc, timing = await self._send( + client, + sem, + "POST", + cfg["gateway_upstream_url"], + headers, + cfg, + log, + json_body=send_body, + stream=stream, + ) + if resp is not None and stream and resp.status_code != 200: + try: + await resp.aread() + except httpx.HTTPError as e: # noqa: BLE001 + log(f"failed to read fallback non-200 stream error body: {e}") + body_error = _embedded_failure_message(resp, stream, protocol) pricing = pricing_from_cfg(cfg) context_map = parse_context_map(cfg.get("gateway_model_context_map")) @@ -705,6 +789,9 @@ class GatewayRuntime: relayed = responses_error_object(parsed_error) if relayed is not None: content = relayed + guidance = _upstream_auth_guidance(resp, content) + if guidance: + log(guidance) return JSONResponse( status_code=resp.status_code, content=content, @@ -741,6 +828,9 @@ class GatewayRuntime: send_start, log, model, + tool_aliases=_response_tool_aliases( + payload, protocol, cfg + ), ) else: body_iter = self._stream_chat_response( @@ -785,6 +875,9 @@ class GatewayRuntime: converted, convert_warnings = responses_object_to_chat_completion( data if isinstance(data, dict) else {} ) + converted = muse_code.restore_completion_names( + converted, _response_tool_aliases(payload, protocol, cfg) + ) if convert_warnings: log( "responses->chat conversion warnings: " @@ -816,12 +909,14 @@ class GatewayRuntime: send_start: float, log, model: str, + tool_aliases: Optional[dict] = None, ): # Same contract as _stream_chat_response (real-time relay, TTFT / # inter-token metering, usage capture, ledger row in `finally`, # [DONE] termination), but the upstream speaks Responses SSE, so # each event is translated to Chat chunks before relay. translator = ResponsesToChatStream(model=model) + tool_aliases = tool_aliases or {} first_at: Optional[float] = None last_at: Optional[float] = None content_chunks = 0 @@ -853,6 +948,7 @@ class GatewayRuntime: error_category = "upstream_error" yield f"data: {json.dumps(out)}\n\n" continue + out = muse_code.restore_chunk_names(out, tool_aliases) choices = out.get("choices") or [] delta = (choices[0].get("delta") if choices else None) or {} if ( @@ -1025,11 +1121,27 @@ class GatewayRuntime: ): log = log or (lambda message: None) vision_cost = 0.0 - overlay = embed_overlay(body.get("model"), cfg) + entry_route = resolve_entry_route(body.get("model"), "embed") + overlay = embed_overlay( + entry_route.source_model + if entry_route is not None + else body.get("model"), + cfg, + ) base_cfg = cfg + fallback_used_route = None if overlay: cfg = {**cfg, **overlay} log(f"routed embed model {body.get('model')!r} -> {cfg['gateway_embed_model']!r}") + if ( + entry_route is not None + and (body.get("model") or "").strip() != entry_route.source_model + ): + log( + f"embed model {body.get('model')!r} is unhealthy, starting on " + f"fallback {entry_route.source_model!r}" + ) + fallback_used_route = entry_route.source_model if not cfg["gateway_embed_enabled"]: from devplacepy.services.audit import record as audit from devplacepy.services.openai_gateway.usage import audit_actor_for @@ -1100,17 +1212,20 @@ class GatewayRuntime: else: model = cfg["gateway_embed_model"] log(f"requested embed model {requested!r} has no route, falling back to {model!r}") - # See handle_chat's matching comment: fallback lookup must key off - # whether a route actually matched, not off gateway_force_model, which - # a matched overlay always forces true. - fallback_key = requested if overlay is not None else "molodetz~embed" + # See handle_chat's matching comment: the attempt line starts after + # the prerouted entry route, or after the seed for unmatched requests. + chain_start = ( + entry_route.source_model + if entry_route is not None + else "molodetz~embed" + ) payload = dict(body) payload["model"] = model headers = { "Content-Type": "application/json", - **_attribution_headers(), + **attribution_headers(app_reference), **_extra_provider_headers(cfg), "Authorization": f"Bearer {cfg['gateway_embed_key']}", } @@ -1127,39 +1242,42 @@ class GatewayRuntime: ) requested_model = requested - fallback_used_route = None + attempted_routes = ( + {entry_route.source_model} if entry_route is not None else set() + ) body_error = _embedded_failure_message(resp, False) - if _should_fallback(resp, exc, timing, body_error): + for fallback_route in resolve_fallback_chain(chain_start, "embed"): + if fallback_route.source_model in attempted_routes: + continue + if not _should_fallback(resp, exc, timing, body_error): + break if body_error: log(f"embed model {requested!r} returned 200 with an embedded error: {body_error}") - fallback_route = resolve_fallback(fallback_key, "embed") - fallback_overlay = ( - embed_overlay(fallback_route.source_model, base_cfg) - if fallback_route is not None - else None + fallback_overlay = embed_overlay(fallback_route.source_model, base_cfg) + if not fallback_overlay: + continue + log( + f"embed model {requested!r} failed, falling back to " + f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_embed_model']!r}" ) - if fallback_overlay: - log( - f"embed model {requested!r} failed, falling back to " - f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_embed_model']!r}" - ) - fallback_used_route = fallback_route.source_model - cfg = {**base_cfg, **fallback_overlay} - model = cfg["gateway_embed_model"] - payload = dict(payload) - payload["model"] = model - headers = _fallback_headers(cfg, "gateway_embed_key") - resp, exc, timing = await self._send( - client, - sem, - "POST", - cfg["gateway_embed_url"], - headers, - cfg, - log, - json_body=payload, - ) - body_error = _embedded_failure_message(resp, False) + fallback_used_route = fallback_route.source_model + attempted_routes.add(fallback_route.source_model) + cfg = {**base_cfg, **fallback_overlay} + model = cfg["gateway_embed_model"] + payload = dict(payload) + payload["model"] = model + headers = _fallback_headers(cfg, "gateway_embed_key", app_reference) + resp, exc, timing = await self._send( + client, + sem, + "POST", + cfg["gateway_embed_url"], + headers, + cfg, + log, + json_body=payload, + ) + body_error = _embedded_failure_message(resp, False) pricing = pricing_from_cfg(cfg) context_map = parse_context_map(cfg.get("gateway_model_context_map")) @@ -1235,6 +1353,11 @@ class GatewayRuntime: classify_error(resp.status_code, None, resp.text), ) log(f"embed upstream POST -> {resp.status_code}: {resp.text[:300]}") + guidance = _upstream_auth_guidance( + resp, {"error": {"message": resp.text}} + ) + if guidance: + log(guidance) return JSONResponse( status_code=resp.status_code, content={"error": {"message": resp.text, "type": "upstream_error"}}, @@ -1273,13 +1396,29 @@ class GatewayRuntime: self, body: dict, cfg: dict, owner: tuple, user_agent: str, app_reference: str, log=None ): log = log or (lambda message: None) - overlay = image_overlay(body.get("model"), cfg) + entry_route = resolve_entry_route(body.get("model"), "image") + overlay = image_overlay( + entry_route.source_model + if entry_route is not None + else body.get("model"), + cfg, + ) base_cfg = cfg + fallback_used_route = None if overlay: cfg = {**cfg, **overlay} log( f"routed image model {body.get('model')!r} -> {cfg['gateway_image_model']!r}" ) + if ( + entry_route is not None + and (body.get("model") or "").strip() != entry_route.source_model + ): + log( + f"image model {body.get('model')!r} is unhealthy, starting on " + f"fallback {entry_route.source_model!r}" + ) + fallback_used_route = entry_route.source_model if not cfg["gateway_image_enabled"]: from devplacepy.services.audit import record as audit from devplacepy.services.openai_gateway.usage import audit_actor_for @@ -1325,17 +1464,20 @@ class GatewayRuntime: else: model = cfg["gateway_image_model"] log(f"requested image model {requested!r} has no route, falling back to {model!r}") - # See handle_chat's matching comment: fallback lookup must key off - # whether a route actually matched, not off gateway_force_model, which - # a matched overlay always forces true. - fallback_key = requested if overlay is not None else "molodetz-img-small" + # See handle_chat's matching comment: the attempt line starts after + # the prerouted entry route, or after the seed for unmatched requests. + chain_start = ( + entry_route.source_model + if entry_route is not None + else "molodetz-img-small" + ) payload = dict(body) payload["model"] = model headers = { "Content-Type": "application/json", - **_attribution_headers(), + **attribution_headers(app_reference), **_extra_provider_headers(cfg), } if cfg["gateway_image_key"]: @@ -1357,39 +1499,42 @@ class GatewayRuntime: ) requested_model = requested - fallback_used_route = None + attempted_routes = ( + {entry_route.source_model} if entry_route is not None else set() + ) body_error = _embedded_failure_message(resp, False) - if _should_fallback(resp, exc, timing, body_error): + for fallback_route in resolve_fallback_chain(chain_start, "image"): + if fallback_route.source_model in attempted_routes: + continue + if not _should_fallback(resp, exc, timing, body_error): + break if body_error: log(f"image model {requested!r} returned 200 with an embedded error: {body_error}") - fallback_route = resolve_fallback(fallback_key, "image") - fallback_overlay = ( - image_overlay(fallback_route.source_model, base_cfg) - if fallback_route is not None - else None + fallback_overlay = image_overlay(fallback_route.source_model, base_cfg) + if not fallback_overlay: + continue + log( + f"image model {requested!r} failed, falling back to " + f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_image_model']!r}" ) - if fallback_overlay: - log( - f"image model {requested!r} failed, falling back to " - f"{fallback_route.source_model!r} -> {fallback_overlay['gateway_image_model']!r}" - ) - fallback_used_route = fallback_route.source_model - cfg = {**base_cfg, **fallback_overlay} - model = cfg["gateway_image_model"] - payload = dict(payload) - payload["model"] = model - headers = _fallback_headers(cfg, "gateway_image_key") - resp, exc, timing = await self._send( - client, - sem, - "POST", - cfg["gateway_image_url"], - headers, - cfg, - log, - json_body=payload, - ) - body_error = _embedded_failure_message(resp, False) + fallback_used_route = fallback_route.source_model + attempted_routes.add(fallback_route.source_model) + cfg = {**base_cfg, **fallback_overlay} + model = cfg["gateway_image_model"] + payload = dict(payload) + payload["model"] = model + headers = _fallback_headers(cfg, "gateway_image_key", app_reference) + resp, exc, timing = await self._send( + client, + sem, + "POST", + cfg["gateway_image_url"], + headers, + cfg, + log, + json_body=payload, + ) + body_error = _embedded_failure_message(resp, False) pricing = pricing_from_cfg(cfg) context_map = parse_context_map(cfg.get("gateway_model_context_map")) @@ -1460,6 +1605,11 @@ class GatewayRuntime: classify_error(resp.status_code, None, resp.text), ) log(f"image upstream POST -> {resp.status_code}: {resp.text[:300]}") + guidance = _upstream_auth_guidance( + resp, {"error": {"message": resp.text}} + ) + if guidance: + log(guidance) return JSONResponse( status_code=resp.status_code, content={"error": {"message": resp.text, "type": "upstream_error"}}, diff --git a/devplacepy/services/openai_gateway/health.py b/devplacepy/services/openai_gateway/health.py new file mode 100644 index 00000000..0ca8c859 --- /dev/null +++ b/devplacepy/services/openai_gateway/health.py @@ -0,0 +1,244 @@ +# retoor + +from __future__ import annotations + +import logging +import time +from dataclasses import dataclass +from datetime import datetime, timezone +from typing import Optional + +from devplacepy.database import bump_cache_version, db, get_table +from devplacepy.services.openai_gateway import routing +from devplacepy.services.openai_gateway.responses import ( + chat_to_responses, + responses_error_object, + responses_failure_message, +) + +logger = logging.getLogger(__name__) + +PROBE_TIMEOUT = 30.0 +PROBE_MAX_TOKENS = 256 +PROBE_MESSAGE = "Reply with exactly: OK" +PROBE_IMAGE_PROMPT = "a hedgehog and a bear" + + +@dataclass +class ProbeResult: + ok: bool + code: str + message: str + latency_ms: float + image_url: str = "" + + +def _embed_url_from_base(base_url: str) -> str: + base_url = (base_url or "").strip() + if base_url.endswith("/chat/completions"): + return base_url[: -len("/chat/completions")] + "/embeddings" + return base_url + + +def _image_url_from_base(base_url: str) -> str: + base_url = (base_url or "").strip() + if base_url.endswith("/chat/completions"): + return base_url[: -len("/chat/completions")] + "/images/generations" + return base_url + + +def _upstream_target(route) -> tuple[str, str, str]: + provider = routing.provider_store.get(route.provider) + if provider is not None: + return ( + provider.get("base_url", ""), + provider.get("api_key", ""), + provider.get("protocol", routing.PROTOCOL_DEFAULT) + or routing.PROTOCOL_DEFAULT, + ) + return routing._default_provider_credentials() + ( + routing._default_provider_protocol(), + ) + + +def _probe_headers(route, api_key: str) -> dict: + headers: dict = {} + hook = routing._header_hook_for(route.provider) + if hook is not None: + headers.update(hook()) + if api_key: + headers["authorization"] = f"Bearer {api_key}" + return headers + + +def _chat_probe_body(target: str, protocol: str) -> dict: + body = { + "model": target, + "messages": [{"role": "user", "content": PROBE_MESSAGE}], + "max_tokens": PROBE_MAX_TOKENS, + "stream": False, + } + if protocol == "responses": + body["reasoning"] = {"effort": "minimal"} + converted, _ = chat_to_responses(body) + return converted + return body + + +def _validate_chat_payload(payload, protocol: str) -> Optional[str]: + if not isinstance(payload, dict): + return "upstream returned a non-JSON-object body" + if protocol == "responses": + failure = responses_failure_message(payload) + if failure is not None: + return failure + if not payload.get("output"): + return "response has no output items" + return None + if not payload.get("choices"): + return "chat completion has no choices" + return None + + +def _validate_embed_payload(payload) -> Optional[str]: + if not isinstance(payload, dict): + return "upstream returned a non-JSON-object body" + rows = payload.get("data") + if not isinstance(rows, list) or not rows: + return "embedding response has no data" + if not (rows[0] or {}).get("embedding"): + return "embedding response has no vector" + return None + + +def _validate_image_payload(payload) -> tuple[Optional[str], str]: + if not isinstance(payload, dict): + return "upstream returned a non-JSON-object body", "" + rows = payload.get("data") + if not isinstance(rows, list) or not rows: + return "image response has no data", "" + url = str((rows[0] or {}).get("url") or "") + if not url: + return "image response has no url", "" + return None, url + + +def _error_details(status_code: int, text: str, protocol: str) -> tuple[str, str]: + import json as _json + + try: + payload = _json.loads(text or "") + except ValueError: + payload = None + if isinstance(payload, dict): + if protocol == "responses": + relayed = responses_error_object(payload) + error = (relayed or {}).get("error") or {} + else: + error = payload.get("error") or {} + if isinstance(error, dict) and error.get("message"): + code = str(error.get("code") or f"http_{status_code}") + return code, str(error.get("message"))[:200] + return f"http_{status_code}", (text or "")[:200] + + +async def probe_route(source_model: str) -> Optional[ProbeResult]: + """Send one minimal real call through a route's own upstream. + + Bypasses fallback (a test must test THAT route) and the ledger + (diagnostics, not usage). Returns None when the route is unknown. + """ + from devplacepy import stealth + + route = routing.model_store.get(source_model) + if route is None: + return None + base_url, api_key, protocol = _upstream_target(route) + if route.kind == "embed": + url = _embed_url_from_base(base_url) + body: dict = {"model": route.target_model, "input": "health check"} + elif route.kind == "image": + url = _image_url_from_base(base_url) + body = {"model": route.target_model, "prompt": PROBE_IMAGE_PROMPT} + else: + url = (base_url or "").strip() + body = _chat_probe_body(route.target_model, protocol) + if not url: + return ProbeResult(False, "no_upstream_url", "route has no upstream URL", 0.0) + headers = _probe_headers(route, api_key) + start = time.monotonic() + try: + async with stealth.stealth_async_client( + timeout=PROBE_TIMEOUT, headers=headers + ) as client: + response = await client.post(url, json=body) + except Exception as exc: + latency_ms = round((time.monotonic() - start) * 1000, 3) + name = type(exc).__name__.lower() + code = "timeout" if "timeout" in name else "connection_error" + return ProbeResult(False, code, str(exc)[:200], latency_ms) + latency_ms = round((time.monotonic() - start) * 1000, 3) + if response.status_code != 200: + code, message = _error_details( + response.status_code, response.text, protocol + ) + return ProbeResult(False, code, message, latency_ms) + try: + payload = response.json() + except ValueError: + return ProbeResult( + False, "bad_response", "upstream returned invalid JSON", latency_ms + ) + image_url = "" + if route.kind == "embed": + problem = _validate_embed_payload(payload) + elif route.kind == "image": + problem, image_url = _validate_image_payload(payload) + else: + problem = _validate_chat_payload(payload, protocol) + if problem is not None: + return ProbeResult(False, "bad_response", problem, latency_ms) + return ProbeResult(True, "ok", "", latency_ms, image_url=image_url) + + +async def test_all() -> tuple[int, int]: + """Probe every route one by one; returns (tested, failed).""" + tested = 0 + failed = 0 + for row in routing.model_store.list(): + result = await probe_route(row["source_model"]) + if result is None: + continue + record_health(row["source_model"], result) + tested += 1 + if not result.ok: + failed += 1 + return tested, failed + + +def record_health(source_model: str, result: ProbeResult) -> bool: + """Persist a probe result on the route; un-marks on success.""" + source_model = (source_model or "").strip() + if not source_model or routing.MODELS_TABLE not in db.tables: + return False + routing.ensure_tables() + table = get_table(routing.MODELS_TABLE) + existing = table.find_one(source_model=source_model) + if existing is None: + return False + table.update( + { + "id": existing["id"], + "health_status": ( + routing.HEALTH_OK if result.ok else routing.HEALTH_BAD + ), + "health_code": result.code if not result.ok else "", + "health_message": result.message if not result.ok else "", + "health_checked_at": datetime.now(timezone.utc).isoformat(), + "updated_at": datetime.now(timezone.utc).isoformat(), + }, + ["id"], + ) + bump_cache_version(routing.CACHE_NAME) + routing._ROUTING_CACHE.clear() + return True diff --git a/devplacepy/services/openai_gateway/muse_code.py b/devplacepy/services/openai_gateway/muse_code.py new file mode 100644 index 00000000..e147edbf --- /dev/null +++ b/devplacepy/services/openai_gateway/muse_code.py @@ -0,0 +1,257 @@ +# retoor + +from __future__ import annotations + +import os +import platform + +from devplacepy.utils.text import generate_uid + +MUSE_CLIENT_VERSION = os.environ.get("MUSE_CLIENT_VERSION", "1.4.14") +MUSE_CLIENT_BUILD = os.environ.get( + "MUSE_CLIENT_BUILD", "dec7d7497fecdad135ec5835a8161dbc92e55b58" +) +MUSE_CLIENT_SURFACE = os.environ.get("MUSE_CLIENT_SURFACE", "exec") +MUSE_CONTRIBUTOR_MODE = os.environ.get("MUSE_CONTRIBUTOR_MODE", "1") + +_OS_NAMES = {"darwin": "macos", "linux": "linux", "windows": "windows"} +_ARCH_NAMES = { + "arm64": "aarch64", + "aarch64": "aarch64", + "x86_64": "x86_64", + "amd64": "x86_64", +} + + +def _muse_platform() -> str: + system = _OS_NAMES.get(platform.system().lower(), "linux") + arch = _ARCH_NAMES.get(platform.machine().lower(), "x86_64") + return f"{system}-{arch}" + + +def _muse_user_agent() -> str: + override = os.environ.get("MUSE_CLIENT_USER_AGENT", "").strip() + if override: + return override + return ( + f"muse-build/{MUSE_CLIENT_VERSION} (non-interactive; " + f"{_muse_platform()}; build {MUSE_CLIENT_BUILD})" + ) + + +# Every one of these trips api.meta.ai's client gate with 403 +# client_attestation_denied when declared as a tools[] function name +# (measured 2026-10-30, all 20 deny, near-misses pass). Text, history, +# params, and descriptions are not scanned - only exact tools[].name. +MUSE_RESERVED_TOOL_NAMES = frozenset( + { + "workflow", + "read_file", + "search", + "write_file", + "edit_file", + "read_memory", + "add_memory", + "edit_memory", + "web_search", + "bash", + "bash_input", + "cron_create", + "cron_delete", + "cron_list", + "get_goal", + "create_goal", + "update_goal", + "report_progress", + "read_skill", + "write_todos", + } +) + +TOOL_ALIAS_PREFIX = "ext_" + +META_UPSTREAM_HOST = "api.meta.ai" + + +def is_meta_upstream(url) -> bool: + return META_UPSTREAM_HOST in (url or "") + + +def _alias_for(name: str, taken: set) -> str: + alias = TOOL_ALIAS_PREFIX + name + while alias in taken: + alias = TOOL_ALIAS_PREFIX + alias + taken.add(alias) + return alias + + +def _declared_names(payload: dict) -> list: + names = [] + for tool in payload.get("tools") or []: + if not isinstance(tool, dict): + continue + if tool.get("type") == "function": + name = (tool.get("function") or {}).get("name") + else: + name = None + if name: + names.append(name) + return names + + +def _history_names(payload: dict) -> list: + names = [] + for message in payload.get("messages") or []: + if not isinstance(message, dict): + continue + for call in message.get("tool_calls") or []: + if not isinstance(call, dict): + continue + name = (call.get("function") or {}).get("name") + if name: + names.append(name) + return names + + +def tool_aliases_for(payload: dict) -> dict: + """Map {alias: original} for reserved names in this Chat payload. + + Deterministic: same payload always yields the same map, so the + send boundary and the response converter rebuild it independently + without threading state through the fallback path. + """ + declared = _declared_names(payload) + taken = set(declared) + seen = set() + aliases = {} + for name in list(declared) + _history_names(payload): + if name in MUSE_RESERVED_TOOL_NAMES and name not in seen: + seen.add(name) + aliases[_alias_for(name, taken)] = name + return aliases + + +def _restore(name, aliases: dict): + if not aliases or not isinstance(name, str): + return name + return aliases.get(name, name) + + +def _alias(name, forward: dict): + if not forward or not isinstance(name, str): + return name + return forward.get(name, name) + + +def alias_chat_tools(payload: dict, aliases: dict) -> dict: + """Rewrite reserved tool names to their aliases (Meta wire shape). + + Covers tools[], tool_choice (function + allowed_tools), and history + tool_calls so the model sees one consistent world. Returns the SAME + object untouched when there is nothing to alias. + """ + if not aliases: + return payload + forward = {original: alias for alias, original in aliases.items()} + renamed = dict(payload) + tools = [] + for tool in payload.get("tools") or []: + if ( + isinstance(tool, dict) + and tool.get("type") == "function" + and isinstance(tool.get("function"), dict) + ): + func = dict(tool["function"]) + func["name"] = _alias(func.get("name"), forward) + tool = {**tool, "function": func} + tools.append(tool) + if tools: + renamed["tools"] = tools + choice = renamed.get("tool_choice") + if isinstance(choice, dict): + if choice.get("type") == "function" and isinstance( + choice.get("function"), dict + ): + func = dict(choice["function"]) + func["name"] = _alias(func.get("name"), forward) + renamed["tool_choice"] = {**choice, "function": func} + elif choice.get("type") == "allowed_tools": + entries = [] + for entry in choice.get("tools") or []: + if ( + isinstance(entry, dict) + and entry.get("type") == "function" + and isinstance(entry.get("function"), dict) + ): + func = dict(entry["function"]) + func["name"] = _alias(func.get("name"), forward) + entry = {**entry, "function": func} + entries.append(entry) + renamed["tool_choice"] = {**choice, "tools": entries} + messages = [] + for message in payload.get("messages") or []: + if isinstance(message, dict) and message.get("tool_calls"): + calls = [] + for call in message["tool_calls"]: + if isinstance(call, dict) and isinstance( + call.get("function"), dict + ): + func = dict(call["function"]) + func["name"] = _alias(func.get("name"), forward) + call = {**call, "function": func} + calls.append(call) + message = {**message, "tool_calls": calls} + messages.append(message) + if messages: + renamed["messages"] = messages + return renamed + + +def restore_completion_names(comp: dict, aliases: dict) -> dict: + """Restore original tool names on a ChatCompletion object.""" + if not aliases: + return comp + for choice in comp.get("choices") or []: + message = (choice or {}).get("message") or {} + for call in message.get("tool_calls") or []: + func = (call or {}).get("function") + if isinstance(func, dict): + func["name"] = _restore(func.get("name"), aliases) + return comp + + +def restore_chunk_names(chunk: dict, aliases: dict) -> dict: + """Restore original tool names on a ChatCompletion chunk.""" + if not aliases: + return chunk + for choice in chunk.get("choices") or []: + delta = (choice or {}).get("delta") or {} + for call in delta.get("tool_calls") or []: + func = (call or {}).get("function") + if isinstance(func, dict) and func.get("name"): + func["name"] = _restore(func["name"], aliases) + return chunk + + +_muse_session_id = generate_uid() + + +def muse_impersonation_headers() -> dict: + """Reproduce the exact headers the real muse CLI sends. + + Since the 2026-10-29 rollout api.meta.ai answers subscription-billed + keys from any other client with 403 client_attestation_denied; a live + probe stamping these headers (captured from Muse Code 1.4.14) got HTTP + 200 back. The session id is stable for the process lifetime, mirroring + one long-running headless session. + """ + return { + "User-Agent": _muse_user_agent(), + "X-Client-Id": f"tbh:{MUSE_CLIENT_SURFACE}", + "X-Tbh-Session-Id": _muse_session_id, + "X-Meta-Ai-Gateway-Session-Id": _muse_session_id, + "X-Tbh-Thread-Id": _muse_session_id, + "X-Tbh-Root-Session-Id": _muse_session_id, + "X-Tbh-Agent-Role": "main", + "X-Tbh-Contributor-Mode": MUSE_CONTRIBUTOR_MODE, + } diff --git a/devplacepy/services/openai_gateway/quota.py b/devplacepy/services/openai_gateway/quota.py index d8af4fa3..f9a6d5f9 100644 --- a/devplacepy/services/openai_gateway/quota.py +++ b/devplacepy/services/openai_gateway/quota.py @@ -3,7 +3,6 @@ from __future__ import annotations import logging -import re import uuid from dataclasses import dataclass from datetime import datetime, timedelta, timezone @@ -13,12 +12,13 @@ from pydantic import BaseModel, Field, field_validator, model_validator from devplacepy.database import bump_cache_version, db, get_table, sync_local_cache +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_PATTERN + logger = logging.getLogger(__name__) RULES_TABLE = "gateway_quota_rules" RESETS_TABLE = "gateway_quota_resets" CACHE_NAME = "gateway_quota" -APP_REFERENCE_PATTERN = re.compile(r"^[a-zA-Z0-9_.-]{1,30}$") OWNER_KINDS = ("internal", "key", "user", "admin", "anonymous") FIELD_DEFAULT_USER = "gateway_default_user_daily_usd" diff --git a/devplacepy/services/openai_gateway/responses.py b/devplacepy/services/openai_gateway/responses.py index 3f6ba8e1..827b25d4 100644 --- a/devplacepy/services/openai_gateway/responses.py +++ b/devplacepy/services/openai_gateway/responses.py @@ -348,9 +348,9 @@ def _chat_thinking_to_reasoning(chat: dict, out: dict, warn: list) -> None: # Explicit effort wins; a Responses-shaped reasoning dict passes its # effort through; chat-dialect thinking flags collapse to effort # "minimal" when disabled and to "upstream default" (unset) when - # enabled. "minimal" (not "none") because the Responses reasoning-effort - # enum has no "none" value - only minimal/low/medium/high(/xhigh/max on - # some providers). + # enabled. "minimal" (not "none") because the Meta upstream rejects + # "none" even though OpenAI's shared ReasoningEffort enum lists it - + # only minimal/low/medium/high/xhigh/max were accepted (measured). if chat.get("reasoning_effort") is not None: out["reasoning"] = {"effort": chat["reasoning_effort"]} return @@ -752,17 +752,28 @@ def responses_object_to_chat_completion(resp: dict) -> tuple[dict, list]: else: finish = "stop" usage = resp.get("usage") or {} + chat_usage: dict = { + "prompt_tokens": usage.get("input_tokens") or 0, + "completion_tokens": usage.get("output_tokens") or 0, + "total_tokens": usage.get("total_tokens") or 0, + } + input_details = usage.get("input_tokens_details") or {} + if input_details.get("cached_tokens") is not None: + chat_usage["prompt_tokens_details"] = { + "cached_tokens": input_details.get("cached_tokens") or 0 + } + output_details = usage.get("output_tokens_details") or {} + if output_details.get("reasoning_tokens") is not None: + chat_usage["completion_tokens_details"] = { + "reasoning_tokens": output_details.get("reasoning_tokens") or 0 + } return { "id": resp.get("id") or "", "object": "chat.completion", "created": resp.get("created_at") or 0, "model": resp.get("model") or "", "choices": [{"index": 0, "message": message, "finish_reason": finish}], - "usage": { - "prompt_tokens": usage.get("input_tokens") or 0, - "completion_tokens": usage.get("output_tokens") or 0, - "total_tokens": usage.get("total_tokens") or 0, - }, + "usage": chat_usage, }, warn @@ -822,6 +833,21 @@ def chat_completion_to_responses_object(comp: dict) -> tuple[dict, list]: } ) usage = comp.get("usage") or {} + resp_usage: dict = { + "input_tokens": usage.get("prompt_tokens") or 0, + "output_tokens": usage.get("completion_tokens") or 0, + "total_tokens": usage.get("total_tokens") or 0, + } + prompt_details = usage.get("prompt_tokens_details") or {} + if prompt_details.get("cached_tokens") is not None: + resp_usage["input_tokens_details"] = { + "cached_tokens": prompt_details.get("cached_tokens") or 0 + } + completion_details = usage.get("completion_tokens_details") or {} + if completion_details.get("reasoning_tokens") is not None: + resp_usage["output_tokens_details"] = { + "reasoning_tokens": completion_details.get("reasoning_tokens") or 0 + } return { "id": comp.get("id") or "", "object": "response", @@ -829,11 +855,7 @@ def chat_completion_to_responses_object(comp: dict) -> tuple[dict, list]: "status": "completed", "model": comp.get("model") or "", "output": output, - "usage": { - "input_tokens": usage.get("prompt_tokens") or 0, - "output_tokens": usage.get("completion_tokens") or 0, - "total_tokens": usage.get("total_tokens") or 0, - }, + "usage": resp_usage, }, warn @@ -867,6 +889,8 @@ def responses_error_object(data: Any) -> Optional[dict]: out["type"] = error["type"] if error.get("code") is not None: out["code"] = error["code"] + if error.get("param") is not None: + out["param"] = error["param"] return {"error": out} message = responses_failure_message(data) if message: @@ -879,7 +903,8 @@ class ResponsesToChatStream: Text deltas and function-call argument deltas map exactly; lifecycle events are consumed; `response.completed` yields the terminal chunk with - the real finish reason and usage. Error events yield `{"error": ...}` + the real finish reason plus a usage-only chunk with empty choices (the + include_usage shape). Error events yield `{"error": ...}` payloads (the Chat stream error shape) and are also stashed on `failed`. """ @@ -951,8 +976,11 @@ class ResponsesToChatStream: chunk["choices"][0]["finish_reason"] = ( converted["choices"][0] or {} ).get("finish_reason") - chunk["usage"] = converted["usage"] chunks.append(chunk) + usage_chunk = self._chunk({}) + usage_chunk["choices"] = [] + usage_chunk["usage"] = converted["usage"] + chunks.append(usage_chunk) return chunks if kind in ("response.failed", "response.incomplete", "error"): self.failed = event diff --git a/devplacepy/services/openai_gateway/routing.py b/devplacepy/services/openai_gateway/routing.py index 0832ba6c..f1981a19 100644 --- a/devplacepy/services/openai_gateway/routing.py +++ b/devplacepy/services/openai_gateway/routing.py @@ -15,7 +15,7 @@ from devplacepy.database import ( get_table, sync_local_cache, ) -from devplacepy.services.openai_gateway import opencode_zen +from devplacepy.services.openai_gateway import muse_code, opencode_zen logger = logging.getLogger(__name__) @@ -29,11 +29,16 @@ KINDS = ("chat", "embed", "image") # this exists only for upstreams that reject requests unless they see a # specific client's headers, e.g. OpenCode Zen's free tier only serves the # real opencode CLI (spoofed via opencode_zen.opencode_impersonation_headers) -# and returns 429/400 to anything else. Adding a new profile is one entry -# here plus one option in the provider form - no other special-casing. +# and returns 429/400 to anything else, and api.meta.ai only serves +# subscription-billed keys to the real muse CLI (spoofed via +# muse_code.muse_impersonation_headers) since the 2026-10-29 rollout, +# returning 403 client_attestation_denied to anything else. Adding a new +# profile is one entry here plus one option in the provider form - no other +# special-casing. CLIENT_PROFILES: dict = { "": None, "opencode": opencode_zen.opencode_impersonation_headers, + "muse": muse_code.muse_impersonation_headers, } # Upstream wire protocol a provider speaks. `chat_completions` is the @@ -143,7 +148,13 @@ async def fetch_provider_models( from devplacepy import stealth - headers = {"authorization": f"Bearer {api_key}"} if api_key else {} + headers: dict = {} + if provider_name: + header_hook = _header_hook_for(provider_name) + if header_hook is not None: + headers.update(header_hook()) + if api_key: + headers["authorization"] = f"Bearer {api_key}" for models_url in models_urls: try: async with stealth.stealth_async_client( @@ -187,6 +198,16 @@ MODEL_TIER2_COLUMNS = ( "off_peak_discount_pct", ) +MODEL_HEALTH_COLUMNS = ( + "health_status", + "health_code", + "health_message", + "health_checked_at", +) + +HEALTH_OK = "ok" +HEALTH_BAD = "bad" + def ensure_tables() -> None: db.query( @@ -217,7 +238,10 @@ def ensure_tables() -> None: "price_output_per_m_tier2 REAL, price_input_per_m_tier2 REAL, " "off_peak_start_minute INTEGER, off_peak_end_minute INTEGER, " "off_peak_discount_pct REAL DEFAULT 0, fallback_model TEXT DEFAULT '', " - "is_active INTEGER DEFAULT 1, created_at TEXT, updated_at TEXT)" + "is_active INTEGER DEFAULT 1, health_status TEXT DEFAULT '', " + "health_code TEXT DEFAULT '', health_message TEXT DEFAULT '', " + "health_checked_at TEXT DEFAULT '', " + "created_at TEXT, updated_at TEXT)" ) models_table = get_table(MODELS_TABLE) for column in MODEL_TIER2_COLUMNS: @@ -225,6 +249,9 @@ def ensure_tables() -> None: models_table.create_column_by_example(column, 0.0) if not models_table.has_column("fallback_model"): models_table.create_column_by_example("fallback_model", "") + for column in MODEL_HEALTH_COLUMNS: + if not models_table.has_column(column): + models_table.create_column_by_example(column, "") try: db.query( "CREATE UNIQUE INDEX IF NOT EXISTS idx_gateway_providers_name ON " @@ -376,6 +403,10 @@ class ModelRoute: off_peak_discount_pct: float fallback_model: str is_active: bool + health_status: str + health_code: str + health_message: str + health_checked_at: str def _opt_float(row: dict, key: str) -> Optional[float]: @@ -413,6 +444,10 @@ def _route_from_row(row: dict) -> ModelRoute: off_peak_discount_pct=float(row.get("off_peak_discount_pct") or 0.0), fallback_model=str(row.get("fallback_model") or ""), is_active=_as_bool(row.get("is_active", 1)), + health_status=str(row.get("health_status") or ""), + health_code=str(row.get("health_code") or ""), + health_message=str(row.get("health_message") or ""), + health_checked_at=str(row.get("health_checked_at") or ""), ) @@ -583,6 +618,66 @@ def resolve_fallback(source_model: Optional[str], kind: str) -> Optional[ModelRo return None return fallback + +MAX_FALLBACK_HOPS = 5 + + +def is_route_healthy(route: Optional[ModelRoute]) -> bool: + return route is not None and (route.health_status or "") != HEALTH_BAD + + +def resolve_fallback_chain( + source_model: Optional[str], kind: str, max_hops: int = MAX_FALLBACK_HOPS +) -> list: + """Attempt line after `source_model`: usable routes only, in order. + + Inactive and unhealthy links are skipped transparently (their own + fallback is followed instead), so a bad link never breaks the line. + The visited set makes cycles impossible; max_hops caps the attempts. + """ + line = [] + seen = set() + source = (source_model or "").strip() + current = source + while current and current not in seen and len(line) < max_hops: + seen.add(current) + route = model_store.get(current) + if route is None or route.kind != kind: + break + nxt = (route.fallback_model or "").strip() + if not nxt or nxt == current: + break + current = nxt + target = model_store.get(current) + if target is None or target.kind != kind: + break + if ( + current != source + and target.is_active + and is_route_healthy(target) + ): + line.append(target) + return line + + +def resolve_entry_route( + source_model: Optional[str], kind: str +) -> Optional[ModelRoute]: + """Route a direct request starts on: healthy-next when bad, else itself. + + An unhealthy route with a usable fallback line starts on the line's + first member (bad models are skipped without attempting); when the + line is empty the original route is still tried - health is advisory, + and the model may have recovered since its last test. + """ + route = model_store.resolve(source_model, kind) + if route is None or is_route_healthy(route): + return route + chain = resolve_fallback_chain(route.source_model, kind) + if chain: + return chain[0] + return route + DEEPSEEK_DEFAULT_ROUTES = ( { "source_model": "deepseek-v4-flash", @@ -707,6 +802,8 @@ def chat_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[dic overlay["gateway_upstream_protocol"] = ( provider.get("protocol") or PROTOCOL_DEFAULT ) + if provider.get("client_profile"): + overlay["gateway_client_profile"] = provider["client_profile"] if route.context_window: from devplacepy.services.openai_gateway.usage import parse_context_map @@ -756,6 +853,8 @@ def embed_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di header_hook = _header_hook_for(route.provider) if header_hook is not None: overlay["gateway_extra_request_headers"] = header_hook() + if provider.get("client_profile"): + overlay["gateway_client_profile"] = provider["client_profile"] return overlay @@ -781,6 +880,8 @@ def image_overlay(requested_model: Optional[str], base_cfg: dict) -> Optional[di header_hook = _header_hook_for(route.provider) if header_hook is not None: overlay["gateway_extra_request_headers"] = header_hook() + if provider.get("client_profile"): + overlay["gateway_client_profile"] = provider["client_profile"] return overlay diff --git a/devplacepy/services/openai_gateway/service.py b/devplacepy/services/openai_gateway/service.py index 37b33607..8ccf9b66 100644 --- a/devplacepy/services/openai_gateway/service.py +++ b/devplacepy/services/openai_gateway/service.py @@ -3,7 +3,6 @@ import json import logging import os -import re import time from fastapi import HTTPException, Request @@ -13,6 +12,11 @@ from devplacepy.database import get_int_setting from devplacepy.services.base import BaseService, ConfigField from devplacepy.services.openai_gateway import auth_throttle, config, quota from devplacepy.services.openai_gateway.analytics import summary_metrics +from devplacepy.services.openai_gateway.apps import ( + APP_REFERENCE_HEADER, + DEFAULT_APP_REFERENCE, + validate_app_reference as _validate_app_reference, +) from devplacepy.services.openai_gateway.gateway import GatewayRuntime from devplacepy.services.openai_gateway.routing import model_store from devplacepy.utils import get_current_user @@ -21,8 +25,6 @@ from devplacepy.utils.guards import client_ip logger = logging.getLogger(__name__) -APP_REFERENCE_PATTERN = re.compile(r"^[a-zA-Z0-9_.-]{1,30}$") -DEFAULT_APP_REFERENCE = "default" INTERNAL_KEY_HEADER = "X-Gateway-Internal-Key" USER_CONTENT_OWNER_KINDS = ("user", "admin") @@ -33,13 +35,6 @@ CONSENT_REQUIRED_MESSAGE = ( ) -def _validate_app_reference(value: str) -> str: - stripped = (value or "").strip() - if not stripped or not APP_REFERENCE_PATTERN.match(stripped): - return DEFAULT_APP_REFERENCE - return stripped - - def _presented_key(request: Request) -> str: key = request.headers.get("X-API-KEY") if key: @@ -750,6 +745,8 @@ class GatewayService(BaseService): continue if not row.get("is_active", True): continue + if (row.get("health_status") or "") == "bad": + continue source = str(row.get("source_model") or "").strip() if not source or source in seen: continue @@ -775,7 +772,7 @@ class GatewayService(BaseService): owner = self.resolve_owner(request) user_agent = request.headers.get("user-agent", "") app_reference = _validate_app_reference( - request.headers.get("X-App-Reference", DEFAULT_APP_REFERENCE) + request.headers.get(APP_REFERENCE_HEADER, DEFAULT_APP_REFERENCE) ) if subpath == "models" and request.method == "GET": return self._models_response() diff --git a/devplacepy/services/openai_gateway/usage.py b/devplacepy/services/openai_gateway/usage.py index c6915bea..4a5ccfc0 100644 --- a/devplacepy/services/openai_gateway/usage.py +++ b/devplacepy/services/openai_gateway/usage.py @@ -358,6 +358,14 @@ def classify_error( lowered = (message or "").lower() if "context" in lowered or "maximum" in lowered or "too long" in lowered: return "context_length" + if ( + "model is unavailable" in lowered + or "model not found" in lowered + or "unknown model" in lowered + or "no such model" in lowered + or "model_not_found" in lowered + ): + return "model_not_found" return "bad_request" if status_code and status_code >= 500: return "upstream_error" diff --git a/devplacepy/services/openai_gateway/vision.py b/devplacepy/services/openai_gateway/vision.py index bf961fff..4100a634 100644 --- a/devplacepy/services/openai_gateway/vision.py +++ b/devplacepy/services/openai_gateway/vision.py @@ -9,6 +9,7 @@ from typing import Any, Optional import httpx +from devplacepy.services.openai_gateway.apps import LEGACY_TITLE_HEADER, TITLE_HEADER from devplacepy.services.openai_gateway.config import VISION_INSTRUCTION from devplacepy.services.openai_gateway.reliability import ( _backoff, @@ -178,7 +179,8 @@ class VisionAugmenter: if self.referer: headers["HTTP-Referer"] = self.referer if self.title: - headers["X-Title"] = self.title + headers[TITLE_HEADER] = self.title + headers[LEGACY_TITLE_HEADER] = self.title start = time.monotonic() attempt = 0 resp: Optional[httpx.Response] = None diff --git a/devplacepy/services/quiz/grading.py b/devplacepy/services/quiz/grading.py index cffd2f97..3fbfaec4 100644 --- a/devplacepy/services/quiz/grading.py +++ b/devplacepy/services/quiz/grading.py @@ -14,6 +14,7 @@ from devplacepy.config import ( ) from devplacepy.database import get_setting from devplacepy.services.correction import gateway_complete +from devplacepy.services.openai_gateway.apps import APP_REFERENCE_QUIZ_GRADING from . import scoring @@ -63,6 +64,7 @@ def grade_free_text(api_key: str, question: dict, answer_text: str) -> scoring.G QUIZ_GRADING_TIMEOUT_SECONDS, model=grading_model(), bypass_preamble=True, + app_reference=APP_REFERENCE_QUIZ_GRADING, ) except Exception as exc: logger.warning("Quiz AI grading failed: %s", exc) diff --git a/devplacepy/services/sync/ai.py b/devplacepy/services/sync/ai.py index 69a0a534..d743f634 100644 --- a/devplacepy/services/sync/ai.py +++ b/devplacepy/services/sync/ai.py @@ -15,10 +15,11 @@ logger = logging.getLogger("sync.ai") async def complete(system: str, text: str, timeout: float) -> str | None: from devplacepy.services.correction import gateway_complete + from devplacepy.services.openai_gateway.apps import APP_REFERENCE_SYNC model = get_setting("community_ai_model", "") or INTERNAL_MODEL reply, usage = await asyncio.to_thread( - gateway_complete, internal_gateway_key(), system, text, timeout, None, model, True, False + gateway_complete, internal_gateway_key(), system, text, timeout, None, model, True, False, APP_REFERENCE_SYNC ) if usage is None: logger.warning("the %s model is unreachable", model) diff --git a/pyproject.toml b/pyproject.toml index 97a43cc3..2134723a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "devplacepy" -version = "1.1.13" +version = "1.1.14" description = "DevPlace - The Developer Social Network" requires-python = ">=3.12" dependencies = [ diff --git a/tests/unit/services/jobs/seo_meta_service.py b/tests/unit/services/jobs/seo_meta_service.py index 3c91feef..e4d5197f 100644 --- a/tests/unit/services/jobs/seo_meta_service.py +++ b/tests/unit/services/jobs/seo_meta_service.py @@ -10,7 +10,7 @@ def test_generate_requests_the_preamble_bypass(local_db, monkeypatch): seen = {} def fake_gateway_complete( - api_key, system, text, timeout, model=None, bypass_preamble=False + api_key, system, text, timeout, model=None, bypass_preamble=False, app_reference=None ): seen["bypass_preamble"] = bypass_preamble return ( diff --git a/tests/unit/services/openai_gateway/apps.py b/tests/unit/services/openai_gateway/apps.py new file mode 100644 index 00000000..a7a7d393 --- /dev/null +++ b/tests/unit/services/openai_gateway/apps.py @@ -0,0 +1,43 @@ +# retoor + +from devplacepy import config as devplace_config +from devplacepy.services.openai_gateway.apps import ( + APP_REFERENCE_PATTERN, + GATEWAY_APPS, + LEGACY_TITLE_HEADER, + REFERER_HEADER, + TITLE_HEADER, + app_title, + attribution_headers, +) + + +def test_every_registered_reference_is_valid_and_unique(): + references = [app.reference for app in GATEWAY_APPS] + assert len(references) == len(set(references)) + for reference in references: + assert APP_REFERENCE_PATTERN.match(reference), reference + + +def test_title_is_per_app(): + assert app_title("devplace-devii-v-1-0-0") == "DevPlace Devii" + assert app_title("devplace-news-v-1-0-0") == "DevPlace News" + + +def test_unknown_and_default_fall_back_to_platform_title(): + assert app_title("default") == "DevPlace" + assert app_title("someone-else") == "DevPlace" + assert app_title("") == "DevPlace" + + +def test_attribution_headers_carry_both_title_names(monkeypatch): + monkeypatch.setattr(devplace_config, "SITE_URL", "https://devplace.net") + headers = attribution_headers("devplace-bots-v-1-0-0") + assert headers[TITLE_HEADER] == "DevPlace Bots" + assert headers[LEGACY_TITLE_HEADER] == "DevPlace Bots" + assert headers[REFERER_HEADER] == "https://devplace.net" + + +def test_referer_omitted_without_site_url(monkeypatch): + monkeypatch.setattr(devplace_config, "SITE_URL", "") + assert REFERER_HEADER not in attribution_headers("default") diff --git a/tests/unit/services/openai_gateway/gateway.py b/tests/unit/services/openai_gateway/gateway.py index 2201eccb..32977801 100644 --- a/tests/unit/services/openai_gateway/gateway.py +++ b/tests/unit/services/openai_gateway/gateway.py @@ -66,10 +66,11 @@ class FakeResp_openai_gateway: yield frame(usage=payload["usage"]) yield "data: [DONE]" class FakeRequest: - def __init__(self, method, url, json_body): + def __init__(self, method, url, json_body, headers=None): self.method = method self.url = url self.json_body = json_body + self.headers = headers or {} self.extensions = {} class FakeClient_openai_gateway: def __init__(self, *a, **k): @@ -948,6 +949,194 @@ def test_chat_falls_back_when_the_primary_model_fails(local_db, monkeypatch): routing.model_store.remove("fb-backup-route") +class FakeChainClient_openai_gateway: + def __init__(self, *a, **k): + self.calls = [] + + def build_request(self, method, url, headers=None, json=None, content=None): + return FakeRequest(method, url, json) + + async def send(self, request, stream=False): + self.calls.append((request.url, request.json_body)) + body = request.json_body or {} + model = body.get("model") + if model in ("chain-a-target", "chain-b-target"): + return FakeResp_openai_gateway(status=500, payload={"error": "boom"}) + return FakeResp_openai_gateway( + payload={ + "id": "x", + "model": model, + "choices": [{"message": {"content": "chain ok"}}], + } + ) + + async def aclose(self): + pass + + +def test_chat_walks_fallback_chain_past_failures(local_db, monkeypatch): + from devplacepy.services.openai_gateway import routing + + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeChainClient_openai_gateway) + routing.model_store.set( + routing.ModelRouteIn(source_model="chain-c", target_model="chain-c-target") + ) + routing.model_store.set( + routing.ModelRouteIn( + source_model="chain-b", + target_model="chain-b-target", + fallback_model="chain-c", + ) + ) + routing.model_store.set( + routing.ModelRouteIn( + source_model="chain-a", + target_model="chain-a-target", + fallback_model="chain-b", + ) + ) + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + cfg["gateway_max_retries"] = 0 + rt = svc.runtime() + response = run_async( + rt.handle_chat( + {"model": "chain-a", "messages": [{"role": "user", "content": "hi"}]}, + cfg, + ("guest", "chain_walk"), + "test", + "default", + ) + ) + assert response.status_code == 200 + assert [c[1]["model"] for c in rt._client.calls] == [ + "chain-a-target", + "chain-b-target", + "chain-c-target", + ] + row = get_table("gateway_usage_ledger").find_one(owner_id="chain_walk") + assert row["model"] == "chain-c-target" + assert row["fallback_used_route"] == "chain-c" + finally: + routing.model_store.remove("chain-a") + routing.model_store.remove("chain-b") + routing.model_store.remove("chain-c") + + +def test_chat_preroutes_around_unhealthy_models(local_db, monkeypatch): + from devplacepy.services.openai_gateway import health, routing + + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway) + routing.model_store.set( + routing.ModelRouteIn(source_model="pre-good", target_model="good-target") + ) + routing.model_store.set( + routing.ModelRouteIn( + source_model="pre-bad", + target_model="bad-target", + fallback_model="pre-good", + ) + ) + health.record_health( + "pre-bad", health.ProbeResult(False, "http_500", "boom", 1.0) + ) + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + logged = [] + response = run_async( + rt.handle_chat( + {"model": "pre-bad", "messages": [{"role": "user", "content": "hi"}]}, + cfg, + ("guest", "preroute"), + "test", + "default", + log=logged.append, + ) + ) + assert response.status_code == 200 + assert len(rt._client.calls) == 1 + assert rt._client.calls[0][1]["model"] == "good-target" + assert any("is unhealthy, starting on" in line for line in logged) + row = get_table("gateway_usage_ledger").find_one(owner_id="preroute") + assert row["requested_model"] == "pre-bad" + assert row["model"] == "good-target" + finally: + routing.model_store.remove("pre-bad") + routing.model_store.remove("pre-good") + + +def test_chat_tries_original_when_chain_is_exhausted(local_db, monkeypatch): + from devplacepy.services.openai_gateway import health, routing + + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeClient_openai_gateway) + routing.model_store.set( + routing.ModelRouteIn( + source_model="exh-fb", + target_model="exh-fb-target", + fallback_model="exh-primary", + ) + ) + routing.model_store.set( + routing.ModelRouteIn( + source_model="exh-primary", + target_model="exh-primary-target", + fallback_model="exh-fb", + ) + ) + health.record_health( + "exh-primary", health.ProbeResult(False, "http_500", "boom", 1.0) + ) + health.record_health( + "exh-fb", health.ProbeResult(False, "http_500", "boom", 1.0) + ) + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + response = run_async( + rt.handle_chat( + {"model": "exh-primary", "messages": [{"role": "user", "content": "hi"}]}, + cfg, + ("guest", "exhausted"), + "test", + "default", + ) + ) + assert response.status_code == 200 + assert [c[1]["model"] for c in rt._client.calls] == ["exh-primary-target"] + finally: + routing.model_store.remove("exh-primary") + routing.model_store.remove("exh-fb") + + +def test_models_endpoint_hides_unhealthy_routes(local_db): + from devplacepy.services.openai_gateway import health, routing + + routing.model_store.set( + routing.ModelRouteIn(source_model="hide-me", target_model="v/hide") + ) + health.record_health( + "hide-me", health.ProbeResult(False, "http_500", "boom", 1.0) + ) + try: + svc = GatewayService() + payload = json.loads(svc._models_response().body.decode()) + assert "hide-me" not in {m["id"] for m in payload["data"]} + health.record_health( + "hide-me", health.ProbeResult(True, "ok", "", 1.0) + ) + payload = json.loads(svc._models_response().body.decode()) + assert "hide-me" in {m["id"] for m in payload["data"]} + finally: + routing.model_store.remove("hide-me") + + class FakeContextLengthClient_openai_gateway: def __init__(self, *a, **k): self.calls = [] @@ -2256,17 +2445,58 @@ def _resp_events_openai_gateway(model): ] +def _resp_tool_obj_openai_gateway(model): + obj = _resp_obj_openai_gateway(model) + obj["output"].append( + { + "type": "function_call", + "call_id": "call_7", + "name": "ext_bash", + "arguments": "{}", + } + ) + return obj + + +def _resp_tool_events_openai_gateway(model): + return [ + {"type": "response.created", "response": {"id": "resp_1"}}, + { + "type": "response.output_item.added", + "output_index": 1, + "item": { + "type": "function_call", + "call_id": "call_7", + "name": "ext_bash", + "arguments": "", + }, + }, + { + "type": "response.function_call_arguments.delta", + "item_id": "call_7", + "output_index": 1, + "delta": "{}", + }, + { + "type": "response.completed", + "response": _resp_tool_obj_openai_gateway(model), + }, + ] + + class FakeResponsesClient_openai_gateway: behavior = "ok" def __init__(self, *a, **k): self.calls = [] + self.last_headers = {} def build_request(self, method, url, headers=None, json=None, content=None): - return FakeRequest(method, url, json) + return FakeRequest(method, url, json, headers) async def send(self, request, stream=False): self.calls.append((request.url, request.json_body)) + self.last_headers = dict(request.headers or {}) body = request.json_body or {} if self.behavior == "error-envelope": return FakeResp_openai_gateway( @@ -2287,7 +2517,25 @@ class FakeResponsesClient_openai_gateway: "error": {"message": "boom"}, } ) + if self.behavior == "attestation-denied": + return FakeResp_openai_gateway( + status=403, + payload={ + "error": { + "message": "This API key is billed to a Muse Code " + "subscription. Muse Code subscriptions cover requests " + "from the Muse Code client only.", + "type": "permission_error", + "code": "client_attestation_denied", + } + }, + ) model = body.get("model", "") + if self.behavior == "tool-call": + return FakeResponsesResp_openai_gateway( + payload=_resp_tool_obj_openai_gateway(model), + events=_resp_tool_events_openai_gateway(model), + ) return FakeResponsesResp_openai_gateway( payload=_resp_obj_openai_gateway(model), events=_resp_events_openai_gateway(model), @@ -2374,6 +2622,141 @@ def test_responses_protocol_transpiles_request_and_response(local_db, monkeypatc _teardown_responses_route_openai_gateway() +def test_muse_profile_headers_reach_the_wire(local_db, monkeypatch): + from devplacepy.services.openai_gateway import routing + + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) + FakeResponsesClient_openai_gateway.behavior = "ok" + routing.provider_store.set( + routing.ProviderIn( + name="wiremeta", + base_url="https://api.meta.ai/v1/responses", + api_key="sk-test", + protocol="responses", + client_profile="muse", + ) + ) + routing.model_store.set( + routing.ModelRouteIn( + source_model="wire-muse", + provider="wiremeta", + target_model="muse-spark", + kind="chat", + ) + ) + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + logged = [] + resp = run_async( + rt.handle_chat( + {"model": "wire-muse", "messages": [{"role": "user", "content": "hi"}]}, + cfg, + ("guest", "test"), + "test", + "default", + log=logged.append, + ) + ) + assert resp.status_code == 200 + sent_headers = rt._client.last_headers + assert sent_headers["X-Client-Id"] == "tbh:exec" + assert sent_headers["User-Agent"].startswith("muse-build/") + session = sent_headers["X-Tbh-Session-Id"] + assert session + assert sent_headers["X-Meta-Ai-Gateway-Session-Id"] == session + assert sent_headers["X-Tbh-Thread-Id"] == session + assert sent_headers["X-Tbh-Root-Session-Id"] == session + assert any("(client profile 'muse')" in line for line in logged) + finally: + routing.model_store.remove("wire-muse") + routing.provider_store.remove("wiremeta") + + +def _bash_tool_body_openai_gateway(**over): + body = _chat_body_openai_gateway(**over) + body["tools"] = [ + { + "type": "function", + "function": { + "name": "bash", + "description": "run shell", + "parameters": {"type": "object", "properties": {}}, + }, + } + ] + return body + + +def test_meta_tool_names_aliased_on_wire_and_restored(local_db, monkeypatch): + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) + FakeResponsesClient_openai_gateway.behavior = "tool-call" + _setup_responses_route_openai_gateway() + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + logged = [] + resp = run_async( + rt.handle_chat( + _bash_tool_body_openai_gateway(), + cfg, + ("guest", "test"), + "test", + "default", + log=logged.append, + ) + ) + _, sent = rt._client.calls[-1] + assert sent["tools"][0]["name"] == "ext_bash" + assert any("bash->ext_bash" in line for line in logged) + assert resp.status_code == 200 + payload = json.loads(bytes(resp.body).decode()) + calls = payload["choices"][0]["message"]["tool_calls"] + assert calls[0]["function"]["name"] == "bash" + assert payload["choices"][0]["finish_reason"] == "tool_calls" + finally: + FakeResponsesClient_openai_gateway.behavior = "ok" + _teardown_responses_route_openai_gateway() + + +def test_meta_tool_names_restored_on_stream_chunks(local_db, monkeypatch): + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) + FakeResponsesClient_openai_gateway.behavior = "tool-call" + _setup_responses_route_openai_gateway() + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + resp = run_async( + rt.handle_chat( + _bash_tool_body_openai_gateway(stream=True), + cfg, + ("guest", "test"), + "test", + "default", + ) + ) + + async def drain(): + out = [] + async for chunk in resp.body_iterator: + out.append(chunk if isinstance(chunk, str) else chunk.decode()) + return "".join(out) + + body = run_async(drain()) + assert '"name": "bash"' in body + assert "ext_bash" not in body + assert "[DONE]" in body + finally: + FakeResponsesClient_openai_gateway.behavior = "ok" + _teardown_responses_route_openai_gateway() + + def test_responses_protocol_stream_relays_chat_chunks(local_db, monkeypatch): monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) FakeResponsesClient_openai_gateway.behavior = "ok" @@ -2440,6 +2823,13 @@ def test_responses_protocol_stream_relays_usage_when_asked(local_db, monkeypatch body = run_async(drain()) assert '"usage"' in body assert '"prompt_tokens": 5' in body + usage_chunks = [ + json.loads(line[len("data:"):]) + for line in body.splitlines() + if line.startswith("data: {") and '"usage"' in line + ] + assert len(usage_chunks) == 1 + assert usage_chunks[0]["choices"] == [] finally: _teardown_responses_route_openai_gateway() @@ -2467,6 +2857,84 @@ def test_responses_protocol_relays_error_envelope(local_db, monkeypatch): _teardown_responses_route_openai_gateway() +def test_responses_protocol_attestation_denied_logs_guidance( + local_db, monkeypatch +): + monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) + FakeResponsesClient_openai_gateway.behavior = "attestation-denied" + _setup_responses_route_openai_gateway() + try: + svc = GatewayService() + cfg = svc.effective_config() + cfg["gateway_vision_enabled"] = False + rt = svc.runtime() + logged = [] + resp = run_async( + rt.handle_chat( + _chat_body_openai_gateway(), + cfg, + ("guest", "test"), + "test", + "default", + log=logged.append, + ) + ) + assert resp.status_code == 403 + payload = json.loads(bytes(resp.body).decode()) + assert payload["error"]["code"] == "client_attestation_denied" + assert any("pay-as-you-go" in line for line in logged) + finally: + FakeResponsesClient_openai_gateway.behavior = "ok" + _teardown_responses_route_openai_gateway() + + +def test_upstream_auth_guidance_covers_401_and_403(): + denied = FakeResp_openai_gateway( + status=403, + payload={ + "error": { + "message": "subscription only", + "type": "permission_error", + "code": "client_attestation_denied", + } + }, + ) + guidance = gwmod._upstream_auth_guidance(denied, denied.json()) + assert "pay-as-you-go" in guidance + assert "aliasing" in guidance + assert "MUSE_RESERVED_TOOL_NAMES" in guidance + other_denied = FakeResp_openai_gateway(status=403, payload={"error": "nope"}) + assert ( + gwmod._upstream_auth_guidance(other_denied, other_denied.json()) is None + ) + unauthed = FakeResp_openai_gateway( + status=401, payload={"error": {"message": "User not found.", "code": 401}} + ) + guidance = gwmod._upstream_auth_guidance(unauthed, unauthed.json()) + assert "api_key" in guidance + assert "User not found." in guidance + server_error = FakeResp_openai_gateway(status=500, payload={"error": "x"}) + assert ( + gwmod._upstream_auth_guidance(server_error, server_error.json()) is None + ) + + +def test_should_fallback_model_unavailable_but_not_bad_request(): + unavailable = FakeResp_openai_gateway( + status=400, + payload={"error": {"message": "Model is unavailable."}}, + ) + assert ( + gwmod._should_fallback(unavailable, None, {"circuit_open": False}) is True + ) + malformed = FakeResp_openai_gateway( + status=400, payload={"error": "bad schema"} + ) + assert ( + gwmod._should_fallback(malformed, None, {"circuit_open": False}) is False + ) + + def test_responses_protocol_failed_object_is_502(local_db, monkeypatch): monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway) FakeResponsesClient_openai_gateway.behavior = "failed-object" diff --git a/tests/unit/services/openai_gateway/health.py b/tests/unit/services/openai_gateway/health.py new file mode 100644 index 00000000..26302ac4 --- /dev/null +++ b/tests/unit/services/openai_gateway/health.py @@ -0,0 +1,240 @@ +# retoor + +import httpx + +from devplacepy.services.openai_gateway import health, routing +from tests.conftest import run_async + + +class _FakeProbeResponse: + def __init__(self, status_code, payload=None, text=""): + self.status_code = status_code + self._payload = payload + self.text = text + + def json(self): + if isinstance(self._payload, Exception): + raise self._payload + return self._payload + + +class _FakeProbeClient: + instances = [] + + def __init__(self, response=None, error=None): + self.response = response + self.error = error + self.requested_url = None + self.sent_body = None + self.headers = None + _FakeProbeClient.instances.append(self) + + async def __aenter__(self): + return self + + async def __aexit__(self, *exc_info): + return False + + async def post(self, url, json=None, headers=None): + self.requested_url = url + self.sent_body = json + if self.error is not None: + raise self.error + return self.response + + +def _patch_probe(monkeypatch, response=None, error=None): + def _factory(**kwargs): + client = _FakeProbeClient(response=response, error=error) + client.headers = kwargs.get("headers") + return client + + monkeypatch.setattr("devplacepy.stealth.stealth_async_client", _factory) + + +def _probe_provider(name="healthprov", base_url=None, protocol="chat_completions"): + routing.provider_store.set( + routing.ProviderIn( + name=name, + base_url=base_url or "https://probe.example/v1/chat/completions", + api_key="k", + protocol=protocol, + ) + ) + + +def _probe_route(source, kind="chat", target="vendor/probe"): + routing.model_store.set( + routing.ModelRouteIn( + source_model=source, + provider="healthprov", + target_model=target, + kind=kind, + ) + ) + + +def _cleanup(names): + for source in names: + routing.model_store.remove(source) + routing.provider_store.remove("healthprov") + + +def test_probe_chat_ok(local_db, monkeypatch): + _probe_provider() + _probe_route("probe-chat") + _patch_probe( + monkeypatch, + _FakeProbeResponse(200, {"choices": [{"message": {"content": "OK"}}]}), + ) + try: + result = run_async(health.probe_route("probe-chat")) + assert result.ok + assert result.code == "ok" + client = _FakeProbeClient.instances[-1] + assert client.requested_url.endswith("/chat/completions") + assert client.sent_body["model"] == "vendor/probe" + assert client.sent_body["stream"] is False + finally: + _cleanup(["probe-chat"]) + + +def test_probe_chat_non_200_records_code(local_db, monkeypatch): + _probe_provider() + _probe_route("probe-chat-bad") + _patch_probe( + monkeypatch, + _FakeProbeResponse( + 403, + {"error": {"message": "denied", "code": "client_attestation_denied"}}, + '{"error": {"message": "denied", "code": "client_attestation_denied"}}', + ), + ) + try: + result = run_async(health.probe_route("probe-chat-bad")) + assert not result.ok + assert result.code == "client_attestation_denied" + assert "denied" in result.message + finally: + _cleanup(["probe-chat-bad"]) + + +def test_probe_chat_timeout(local_db, monkeypatch): + _probe_provider() + _probe_route("probe-chat-slow") + _patch_probe(monkeypatch, error=httpx.TimeoutException("slow")) + try: + result = run_async(health.probe_route("probe-chat-slow")) + assert not result.ok + assert result.code == "timeout" + finally: + _cleanup(["probe-chat-slow"]) + + +def test_probe_chat_responses_protocol_transpiles(local_db, monkeypatch): + _probe_provider( + base_url="https://probe.example/v1/responses", protocol="responses" + ) + _probe_route("probe-resp") + _patch_probe( + monkeypatch, + _FakeProbeResponse( + 200, + { + "object": "response", + "status": "completed", + "output": [ + { + "type": "message", + "content": [{"type": "output_text", "text": "OK"}], + } + ], + "usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2}, + }, + ), + ) + try: + result = run_async(health.probe_route("probe-resp")) + assert result.ok + client = _FakeProbeClient.instances[-1] + assert "input" in client.sent_body + assert client.sent_body["reasoning"] == {"effort": "minimal"} + finally: + _cleanup(["probe-resp"]) + + +def test_probe_embed_and_image(local_db, monkeypatch): + _probe_provider() + _probe_route("probe-embed", kind="embed") + _patch_probe( + monkeypatch, _FakeProbeResponse(200, {"data": [{"embedding": [0.1]}]}) + ) + try: + result = run_async(health.probe_route("probe-embed")) + assert result.ok + client = _FakeProbeClient.instances[-1] + assert client.requested_url.endswith("/embeddings") + finally: + routing.model_store.remove("probe-embed") + _probe_route("probe-image", kind="image") + _patch_probe( + monkeypatch, + _FakeProbeResponse(200, {"data": [{"url": "https://img.example/h.png"}]}), + ) + try: + result = run_async(health.probe_route("probe-image")) + assert result.ok + assert result.image_url == "https://img.example/h.png" + client = _FakeProbeClient.instances[-1] + assert client.sent_body["prompt"] == "a hedgehog and a bear" + finally: + _cleanup(["probe-image"]) + + +def test_probe_unknown_route_returns_none(local_db): + assert run_async(health.probe_route("ghost-probe")) is None + + +def test_all_probes_every_route_one_by_one(local_db, monkeypatch): + _probe_provider() + _probe_route("probe-all-a") + _probe_route("probe-all-b") + seen = [] + + async def fake_probe(source): + seen.append(source) + ok = source != "probe-all-b" + return health.ProbeResult(ok, "" if ok else "http_500", "" if ok else "x", 1.0) + + monkeypatch.setattr(health, "probe_route", fake_probe) + try: + tested, failed = run_async(health.test_all()) + assert "probe-all-a" in seen and "probe-all-b" in seen + assert tested >= 2 and failed >= 1 + assert routing.model_store.get("probe-all-a").health_status == "ok" + assert routing.model_store.get("probe-all-b").health_status == "bad" + finally: + _cleanup(["probe-all-a", "probe-all-b"]) + + +def test_record_health_writes_and_clears(local_db): + _probe_provider() + _probe_route("probe-record") + try: + assert health.record_health( + "probe-record", health.ProbeResult(False, "http_500", "boom", 3.0) + ) + route = routing.model_store.get("probe-record") + assert route.health_status == "bad" + assert route.health_code == "http_500" + assert route.health_message == "boom" + assert route.health_checked_at + assert health.record_health( + "probe-record", health.ProbeResult(True, "ok", "", 1.0) + ) + route = routing.model_store.get("probe-record") + assert route.health_status == "ok" + assert route.health_code == "" + assert not health.record_health("ghost-record", health.ProbeResult(True, "ok", "", 1.0)) + finally: + _cleanup(["probe-record"]) diff --git a/tests/unit/services/openai_gateway/muse_code.py b/tests/unit/services/openai_gateway/muse_code.py new file mode 100644 index 00000000..984859d7 --- /dev/null +++ b/tests/unit/services/openai_gateway/muse_code.py @@ -0,0 +1,153 @@ +# retoor + +from devplacepy.services.openai_gateway import muse_code + + +def test_headers_include_the_expected_client_identity_fields(): + headers = muse_code.muse_impersonation_headers() + assert set(headers) == { + "User-Agent", + "X-Client-Id", + "X-Tbh-Session-Id", + "X-Meta-Ai-Gateway-Session-Id", + "X-Tbh-Thread-Id", + "X-Tbh-Root-Session-Id", + "X-Tbh-Agent-Role", + "X-Tbh-Contributor-Mode", + } + assert headers["User-Agent"].startswith("muse-build/") + assert "(non-interactive;" in headers["User-Agent"] + assert headers["X-Client-Id"] == "tbh:exec" + assert headers["X-Tbh-Agent-Role"] == "main" + + +def test_reserved_tool_names_cover_the_measured_denylist(): + assert muse_code.MUSE_RESERVED_TOOL_NAMES == frozenset( + { + "workflow", + "read_file", + "search", + "write_file", + "edit_file", + "read_memory", + "add_memory", + "edit_memory", + "web_search", + "bash", + "bash_input", + "cron_create", + "cron_delete", + "cron_list", + "get_goal", + "create_goal", + "update_goal", + "report_progress", + "read_skill", + "write_todos", + } + ) + + +def _func_tool(name): + return {"type": "function", "function": {"name": name, "arguments": "{}"}} + + +def test_tool_aliases_cover_declared_and_history_names(): + payload = { + "tools": [_func_tool("bash"), _func_tool("innocent")], + "messages": [ + { + "role": "assistant", + "tool_calls": [_func_tool("read_file")], + } + ], + } + aliases = muse_code.tool_aliases_for(payload) + assert aliases == {"ext_bash": "bash", "ext_read_file": "read_file"} + + +def test_tool_aliases_grow_past_collisions(): + payload = {"tools": [_func_tool("bash"), _func_tool("ext_bash")]} + aliases = muse_code.tool_aliases_for(payload) + assert aliases == {"ext_ext_bash": "bash"} + + +def test_tool_aliases_empty_when_clean(): + assert muse_code.tool_aliases_for({"tools": [_func_tool("innocent")]}) == {} + assert muse_code.tool_aliases_for({}) == {} + + +def test_alias_chat_tools_rewrites_every_name_shape(): + payload = { + "tools": [_func_tool("bash")], + "tool_choice": {"type": "function", "function": {"name": "bash"}}, + "messages": [ + { + "role": "assistant", + "tool_calls": [_func_tool("bash")], + } + ], + } + renamed = muse_code.alias_chat_tools( + payload, muse_code.tool_aliases_for(payload) + ) + assert renamed is not payload + assert renamed["tools"][0]["function"]["name"] == "ext_bash" + assert renamed["tool_choice"]["function"]["name"] == "ext_bash" + assert ( + renamed["messages"][0]["tool_calls"][0]["function"]["name"] + == "ext_bash" + ) + assert payload["tools"][0]["function"]["name"] == "bash" + + +def test_alias_chat_tools_rewrites_allowed_tools_entries(): + payload = { + "tool_choice": { + "type": "allowed_tools", + "mode": "auto", + "tools": [_func_tool("bash"), _func_tool("innocent")], + } + } + renamed = muse_code.alias_chat_tools(payload, {"ext_bash": "bash"}) + entries = renamed["tool_choice"]["tools"] + assert entries[0]["function"]["name"] == "ext_bash" + assert entries[1]["function"]["name"] == "innocent" + + +def test_alias_chat_tools_passthrough_when_clean(): + payload = {"tools": [_func_tool("innocent")]} + assert muse_code.alias_chat_tools(payload, {}) is payload + + +def test_restore_completion_and_chunk_names(): + aliases = {"ext_bash": "bash"} + comp = { + "choices": [ + {"message": {"tool_calls": [_func_tool("ext_bash"), _func_tool("x")]}}, + ] + } + muse_code.restore_completion_names(comp, aliases) + names = [c["function"]["name"] for c in comp["choices"][0]["message"]["tool_calls"]] + assert names == ["bash", "x"] + chunk = {"choices": [{"delta": {"tool_calls": [_func_tool("ext_bash")]}}]} + muse_code.restore_chunk_names(chunk, aliases) + assert chunk["choices"][0]["delta"]["tool_calls"][0]["function"]["name"] == "bash" + assert muse_code.restore_completion_names(comp, {}) is comp + assert muse_code.restore_chunk_names(chunk, {}) is chunk + + +def test_session_id_is_stable_and_shared_across_calls(): + first = muse_code.muse_impersonation_headers() + second = muse_code.muse_impersonation_headers() + session_keys = ( + "X-Tbh-Session-Id", + "X-Meta-Ai-Gateway-Session-Id", + "X-Tbh-Thread-Id", + "X-Tbh-Root-Session-Id", + ) + shared = first["X-Tbh-Session-Id"] + assert len(shared) == 36 and shared.count("-") == 4 + for key in session_keys: + assert first[key] == shared + assert second[key] == shared diff --git a/tests/unit/services/openai_gateway/responses.py b/tests/unit/services/openai_gateway/responses.py index 9f96e03c..3fb0cec7 100644 --- a/tests/unit/services/openai_gateway/responses.py +++ b/tests/unit/services/openai_gateway/responses.py @@ -385,6 +385,25 @@ def test_response_object_to_chat_completion(): } +def test_response_object_usage_details_pass_through(): + comp, _ = responses_object_to_chat_completion( + { + "id": "r", + "output": [], + "usage": { + "input_tokens": 10, + "output_tokens": 4, + "total_tokens": 14, + "input_tokens_details": {"cached_tokens": 6}, + "output_tokens_details": {"reasoning_tokens": 2}, + }, + } + ) + assert comp["usage"]["prompt_tokens"] == 10 + assert comp["usage"]["prompt_tokens_details"] == {"cached_tokens": 6} + assert comp["usage"]["completion_tokens_details"] == {"reasoning_tokens": 2} + + def test_response_object_finish_reasons(): comp, _ = responses_object_to_chat_completion( { @@ -451,6 +470,9 @@ def test_responses_failure_and_error_shapes(): assert responses_error_object({"error": {"message": "m", "code": "c"}}) == { "error": {"message": "m", "code": "c"} } + assert responses_error_object( + {"error": {"message": "m", "type": "t", "code": "c", "param": "p"}} + ) == {"error": {"message": "m", "type": "t", "code": "c", "param": "p"}} assert responses_error_object({"a": 1}) is None @@ -475,7 +497,9 @@ def test_stream_responses_to_chat_text_and_done(): } ) assert done[0]["choices"][0]["finish_reason"] == "stop" - assert done[0]["usage"]["prompt_tokens"] == 1 + assert "usage" not in done[0] + assert done[1]["choices"] == [] + assert done[1]["usage"]["prompt_tokens"] == 1 def test_stream_responses_to_chat_tool_calls(): diff --git a/tests/unit/services/openai_gateway/routing.py b/tests/unit/services/openai_gateway/routing.py index 7ecf8967..2bfe1aea 100644 --- a/tests/unit/services/openai_gateway/routing.py +++ b/tests/unit/services/openai_gateway/routing.py @@ -179,6 +179,87 @@ def test_resolve_fallback_ignores_a_fallback_of_a_different_kind(local_db): _cleanup([], ["fb-primary-3", "fb-backup-embed"]) +def _chain_route(source, target, fallback="", active=True): + r.model_store.set( + r.ModelRouteIn( + source_model=source, + target_model=target, + kind="chat", + fallback_model=fallback, + is_active=active, + ) + ) + + +def _mark_health(source, status): + from devplacepy.services.openai_gateway import health + + code = "" if status == "ok" else "http_500" + message = "" if status == "ok" else "boom" + health.record_health( + source, health.ProbeResult(status == "ok", code, message, 1.0) + ) + + +def test_fallback_chain_skips_bad_and_inactive_links(local_db): + _chain_route("chain-a", "v/a", "chain-b") + _chain_route("chain-b", "v/b", "chain-c") + _chain_route("chain-c", "v/c", "chain-d") + _chain_route("chain-d", "v/d", "", active=False) + _mark_health("chain-b", "bad") + try: + line = r.resolve_fallback_chain("chain-a", "chat") + assert [m.source_model for m in line] == ["chain-c"] + finally: + _cleanup([], ["chain-a", "chain-b", "chain-c", "chain-d"]) + + +def test_fallback_chain_stops_on_cycles(local_db): + _chain_route("chain-e", "v/e", "chain-f") + _chain_route("chain-f", "v/f", "chain-e") + try: + line = r.resolve_fallback_chain("chain-e", "chat") + assert [m.source_model for m in line] == ["chain-f"] + finally: + _cleanup([], ["chain-e", "chain-f"]) + + +def test_fallback_chain_respects_max_hops(local_db): + names = [f"chain-h{i}" for i in range(8)] + for pos, name in enumerate(names): + nxt = names[pos + 1] if pos + 1 < len(names) else "" + _chain_route(name, f"v/{name}", nxt) + try: + line = r.resolve_fallback_chain(names[0], "chat", max_hops=3) + assert [m.source_model for m in line] == names[1:4] + assert len(r.resolve_fallback_chain(names[0], "chat")) <= 5 + finally: + _cleanup([], names) + + +def test_entry_route_preroutes_bad_models(local_db): + _chain_route("chain-g", "v/g", "chain-h") + _chain_route("chain-h", "v/h") + _mark_health("chain-g", "bad") + try: + assert r.resolve_entry_route("chain-g", "chat").source_model == "chain-h" + assert r.resolve_entry_route("chain-h", "chat").source_model == "chain-h" + assert r.resolve_entry_route("ghost-entry", "chat") is None + finally: + _cleanup([], ["chain-g", "chain-h"]) + + +def test_entry_route_falls_back_to_original_when_line_is_empty(local_db): + _chain_route("chain-i", "v/i", "chain-j") + _chain_route("chain-j", "v/j") + _mark_health("chain-i", "bad") + _mark_health("chain-j", "bad") + try: + assert r.resolve_entry_route("chain-i", "chat").source_model == "chain-i" + finally: + _cleanup([], ["chain-i", "chain-j"]) + + def test_vision_merge_overlay(local_db): r.provider_store.set( r.ProviderIn( @@ -362,6 +443,31 @@ def test_provider_without_a_client_profile_gets_no_extra_headers(local_db): _cleanup(["plainprov"], ["ut-plain"]) +def test_provider_with_a_muse_profile_gets_muse_headers(local_db): + r.provider_store.set( + r.ProviderIn( + name="meta", + base_url="https://api.meta.ai/v1/responses", + api_key="secret", + client_profile="muse", + ) + ) + r.model_store.set( + r.ModelRouteIn( + source_model="ut-muse", + provider="meta", + target_model="muse-spark", + ) + ) + try: + overlay = r.chat_overlay("ut-muse", {}) + headers = overlay["gateway_extra_request_headers"] + assert headers["X-Client-Id"] == "tbh:exec" + assert headers["User-Agent"].startswith("muse-build/") + finally: + _cleanup(["meta"], ["ut-muse"]) + + def test_client_profile_rejects_unknown_value(): with pytest.raises(ValidationError): r.ProviderIn(name="badprofile", client_profile="not-a-real-profile") diff --git a/tests/unit/services/openai_gateway/usage.py b/tests/unit/services/openai_gateway/usage.py index a177c015..9169adc1 100644 --- a/tests/unit/services/openai_gateway/usage.py +++ b/tests/unit/services/openai_gateway/usage.py @@ -7,6 +7,7 @@ from devplacepy.services.openai_gateway.usage import ( USAGE_FIELDS, Pricing, accumulate_usage, + classify_error, compute_cost, new_usage_totals, normalize_usage, @@ -325,3 +326,13 @@ def test_upstream_reported_model_get_raising_never_propagates(): raise RuntimeError("boom too") assert upstream_reported_model(HostileHeaders()) is None + + +def test_classify_error_model_unavailable_400_is_model_not_found(): + body = '{"error":{"type":"server_error","message":"Upstream request failed: Model is unavailable."}}' + assert classify_error(400, None, body) == "model_not_found" + assert classify_error(404, None, "") == "model_not_found" + assert classify_error(400, None, '{"error":"bad schema"}') == "bad_request" + assert ( + classify_error(400, None, "context window exceeded") == "context_length" + ) diff --git a/tests/unit/services/quiz/grading.py b/tests/unit/services/quiz/grading.py index 80a03fd8..b6d52303 100644 --- a/tests/unit/services/quiz/grading.py +++ b/tests/unit/services/quiz/grading.py @@ -134,7 +134,7 @@ def test_grade_free_text_uses_a_valid_verdict(monkeypatch): def test_grade_free_text_passes_the_answering_key_through(monkeypatch): seen = {} - def capture(api_key, system, text, timeout, model=None, bypass_preamble=False): + def capture(api_key, system, text, timeout, model=None, bypass_preamble=False, app_reference=None): seen["api_key"] = api_key seen["bypass_preamble"] = bypass_preamble return json.dumps({"score": 1.0}), None @@ -147,7 +147,7 @@ def test_grade_free_text_passes_the_answering_key_through(monkeypatch): def test_grade_free_text_bypasses_the_operator_preamble(monkeypatch): seen = {} - def capture(api_key, system, text, timeout, model=None, bypass_preamble=False): + def capture(api_key, system, text, timeout, model=None, bypass_preamble=False, app_reference=None): seen["bypass_preamble"] = bypass_preamble return json.dumps({"score": 1.0}), None