mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 12:22:14 +03:00
* fix(sse): Gemini TPM classification, combo-cooldown-wait for auto/quota-share, and target-timeout floor
Gemini TPM/RPM 429s were misclassified as QUOTA_EXHAUSTED because
sanitizeErrorMessage() truncates to the first line, hiding Google's
metric name and retry hint on lines 2-3. Added a rawMessage field
(internal-only, never reaches the client) and classifyGeminiQuotaMetricFromText()
to classify from the untruncated text, reordered ahead of the generic
credits/daily-quota checks.
Widened comboCooldownWaitEnabled (wait out a short transient cooldown
instead of crystallizing a 429/503) from quota-share-only to also cover
auto-strategy combos, and raised the wait ceiling to 65s/130s-budget/90s-cap
to match Gemini's ~60s TPM/RPM windows.
The per-target timeout (DEFAULT_COMBO_TARGET_TIMEOUT_MS, 120s) was shorter
than the new 130s cooldown-wait budget, so a target could get cut off
mid-wait with a synthetic 524 instead of completing the retry. Added
resolveComboTargetTimeoutMsForCombo()/isComboCooldownWaitEligible() in
comboConfig.ts to raise the per-target floor to budgetMs+buffer only for
wait-eligible strategies (auto/quota-share), verified live: a 12-request
concurrent burst against a TPM-exhausted combo went from 2/12 succeeding
(10 x 524) to 12/12 succeeding with zero 503/524.
Also: liveGeminiShared.ts's sendAndValidate now fails fast on a 503
instead of retrying past it, and the health dashboard + request logger
surface TPM stats alongside RPM/RPD.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* fix(sse): combo-exhausted rejection logs now capture request body + attempted models
recordRejectedRequestUsage() (the fast path for combo requests that never
reach handleChatCore, e.g. all targets locked by resilience cooldown)
hardcoded provider: "-" and never passed a request body to saveCallLog(),
so /dashboard/logs entries for these failures were nearly useless for
debugging: no way to see the client's request or which models were tried.
- recordRejectedRequestUsage() now accepts requestBody and persists it
through the existing saveCallLog() artifact mechanism (same path
handleChatCore's own logging uses).
- Added summarizeComboAttemptedModels(), which reads the combo's own model
list (always available, unlike the response's combo-diagnostics headers —
a model-level resilience-lockout skip never touches the
exhaustedProviders/exhaustedConnections sets those headers are built
from) to populate a real "provider" value instead of "-".
- Wired both into the call site in src/sse/handlers/chat.ts.
NOTE: unrelated to the Gemini TPM/combo-cooldown-wait fix on this branch —
landed here per operator request, to be split into its own branch/PR.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* feat(sse): synthetic streaming keep-alive event + 5-minute Gemini cooldown-wait ceiling
Many clients enforce a first-SSE-byte timeout, which made it unsafe to wait
out a longer upstream rate-limit cooldown on a streaming request — the
client would abandon the connection before any bytes arrived. This landed
in two parts:
1. Synthetic startup "thinking" event (OpenAI chat/completions format):
the already-existing withEarlyStreamKeepalive wrapper (open-sse/utils/
earlyStreamKeepalive.ts, wired into /v1/chat/completions, /v1/messages,
/v1/responses since #2544) opens the SSE stream immediately once a
request runs past its threshold, but only ever sent empty/no-op
keepalive frames. Added a `startupFrame` option (defaults to
`keepaliveFrame` — zero behavior change unless a route opts in) so the
very first frame can carry real content instead. Wired
OPENAI_STARTUP_THINKING_FRAME (a reasoning_content delta: "OmniRoute:
got request, sending to provider") into /v1/chat/completions only —
Claude Messages and Responses API formats both require a preceding
envelope event (message_start / response.created) that a synthetic
pre-dispatch frame can't safely fabricate without risking a duplicate
envelope once the real stream arrives, so those two routes keep their
existing (safe, proven) keepalive frames unchanged.
2. Raised the "wait out a known cooldown, then retry" ceiling to 5 minutes
for both retry mechanisms, now that a client-side first-byte timeout is
no longer a risk on the (opted-in) route:
- comboCooldownWait (auto/quota-share combos, open-sse/services/combo.ts):
maxWaitMs hard clamp raised 90s -> 300s (src/lib/resilience/settings/
normalize.ts); defaults raised to maxWaitMs:90s/maxAttempts:5/
budgetMs:300s. comboConfig.ts's resolveComboTargetTimeoutMsForCombo
already derives the per-target timeout floor from budgetMs, so it
tracks the new ceiling with no further changes.
- waitForCooldown (direct, non-combo model requests, src/sse/handlers/
chat.ts): this mechanism had NO cumulative cap before — only a
per-wait cap (maxRetryWaitMs) and a retry count (maxRetries), so
maxRetries x maxRetryWaitMs could exceed 5 minutes with no ceiling.
Added a budgetMs field (mirrors comboCooldownWait) to
WaitForCooldownSettings/CooldownAwareRetrySettings, threaded a
requestRetryBudgetLeftMs tracker through chat.ts's requestAttemptLoop
(mirrors combo.ts's comboCooldownBudgetLeftMs), and made
getCooldownAwareRetryDecision refuse to wait once the cumulative
budget is exhausted even if the single wait is under maxRetryWaitMs.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* fix(sse): extend the synthetic keep-alive thinking event to /v1/responses
Live incident (OpenClaw, log id 1784407081908-cbc24f): a /v1/responses
request to gemini/gemma-4-31b-it took 56s to produce a first byte and the
client disconnected (499 request_signal_aborted) — the same client-first-byte-
timeout problem the previous commit fixed for /v1/chat/completions, but
/v1/responses only had the generic bare-comment keepalive (no content), so it
wasn't covered.
Added RESPONSES_STARTUP_THINKING_FRAME: a self-contained synthetic reasoning
item (response.output_item.added -> reasoning_summary_part.added ->
reasoning_summary_text.delta -> reasoning_summary_part.done), opened AND
closed within this one frame rather than left dangling — it never carries a
response_id, so it can't collide with the real upstream response's own
independent response.created lifecycle that follows. Mirrors the abbreviated
delta+part.done close pattern open-sse/utils/stream.ts's own
emitSyntheticResponsesReasoningSummary already uses for real mid-stream
reasoning content.
Wired into src/app/api/v1/responses/route.ts via the startupFrame option
added in the previous commit.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* fix(sse): combo cooldown-wait vars reset every setTry, crystallizing a bogus 503 instead of waiting
Live incident (log id 1784416706646-51): a request to the "default" combo
(strategy=auto, maxSetRetries=3) hit a real Gemini TPM 429 on both gemma-4
targets, correctly classified as a short 40s rate_limit lockout — then
crystallized a 503 "all upstream accounts are inactive" in 6.9s instead of
ever reaching the cooldown-aware wait.
Root cause: `lastError`/`earliestRetryAfter`/`lastStatus` were declared with
`let` INSIDE the `for (setTry...)` loop body, so they reset to null at the
start of every set-try. When both targets lock out on setTry 0, every
subsequent setTry (1..maxSetRetries) pre-skips both targets via the
isModelLocked check with no real dispatch — so on the FINAL setTry (the only
one whose values the post-loop decision reads, since it's gated behind
`if (setTry < maxSetRetries) continue`), lastStatus was null, hitting the
"!lastStatus" branch (ALL_ACCOUNTS_INACTIVE 503) and completely bypassing the
comboCooldownWaitEnabled / earliestRetryAfter wait logic — even though a
real 429 with a known ~40s retry-after WAS observed on setTry 0.
This bug predates today's Gemini TPM work (any combo with maxSetRetries > 0
whose targets all lock out on the first pass was affected) but was masked in
existing tests: the "auto strategy (2 models...)" regression test uses
maxSetRetries: 0, so it only ever runs ONE setTry iteration and never
exercises the reset-on-retry path. It also explains why the dedicated
12-concurrent-request burst test passed cleanly — with concurrent requests,
timing variance meant some request's FINAL setTry iteration still had a live
target to dispatch to, giving lastStatus/earliestRetryAfter fresh data. A
single isolated request has no such luck.
Fix: hoist lastError/earliestRetryAfter/lastStatus to just inside
dispatchWithCooldownRetry, before the setTry loop, so they persist across
set-tries (still reset fresh on each recursive dispatchWithCooldownRetry()
call after a wait, which is correct). recordedAttempts/fallbackCount/
exhaustedProviders etc. are intentionally left per-iteration (unrelated to
this bug).
New regression test in tests/unit/combo-quota-share-cooldown-wait.test.ts
reproduces the exact live scenario (2 targets, both lock out on setTry 0,
maxSetRetries: 3) — confirmed red (503) against the pre-fix code, green
(200, waits and retries) against the fix.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* test(sse): extend live Gemini workload to Responses API + add large-context TPM test
Two additions to the live Gemini test suite, both live-verified against the
dev instance:
1. sendAndValidate() (tests/integration/liveGeminiShared.ts) now accepts an
apiFormat: "chat" | "responses" parameter, building the Responses-API
request shape (input array, max_output_tokens) and parsing its SSE events
(response.output_text.delta / response.reasoning_summary_text.delta /
response.completed) via the new readResponsesSSEStream(). Wired into two
new tests in live-gemini-workload.test.ts ([30]/[31]), mirroring the
existing Chat Completions streaming coverage. Verified live: 24/25 + 5/5
payloads succeeded end-to-end through the new code path (the one failure
was a ~300s test-client fetch timeout unrelated to the Responses API code
itself — a separate, not-yet-addressed test-harness limitation).
2. genHugeContextMessage() builds a single message large enough (~4
chars/token estimate) to approach or exceed Gemini's free-tier TPM ceiling
(16000 input tokens/min for gemma-4) by itself. Every other prompt
generator in this file tops out around 1-2k tokens — nowhere near that
ceiling — so none of the existing workload tests ever exercised a REAL TPM
429, only RPM-style rate limiting. tests/integration/gemini-large-context-tpm.test.ts
sends two ~12-13k-token requests back-to-back (comfortably exceeding
16000/min together) to exercise the full path against production Gemini:
TPM classification, the comboCooldownWait retry, and the synthetic
keep-alive frame on a genuinely slow request.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* fix(sse): abandoned combo target dispatch now observes its own per-target timeout, fixing a permanent "pending" dashboard leak
Live incident (dashboard log id 1784418258231-14961a, reported as "an ongoing
request even though there's already a 200"): a combo target dispatch
abandoned by comboTargetTimeoutMs (open-sse/services/combo/targetTimeoutRunner.ts)
left a permanent phantom "pending" entry in the dashboard, even after the
overall combo request had already succeeded via a different retry.
Root cause: chatCore.ts's createStreamController — and everything downstream
that depends on it (withRateLimit's Promise.race against Bottleneck,
acquireAccountSemaphore) — only ever watches clientRawRequest.signal, which
is the ORIGINAL client's request signal (set once via buildClientRawRequest
and reused unchanged across every target dispatch in a combo). It has no
connection to targetTimeoutRunner.ts's OWN AbortController
(target.modelAbortSignal), which is what actually fires when
comboTargetTimeoutMs (300s) elapses. src/sse/handlers/chat.ts's
handleSingleModel bridge between combo.ts and handleSingleModelChat received
`target.modelAbortSignal` but silently dropped it — never forwarded it
anywhere. So when a target got abandoned (e.g. stuck inside a wedged
Bottleneck rate-limiter queue, see the WEDGED force-reset log line from the
same incident), its per-target timeout fired and let the COMBO move on and
retry successfully elsewhere — but the abandoned dispatch's own promise
chain never learned it had been superseded, so it hung forever waiting on a
signal that was never going to fire, and trackPendingRequest(false) (the
finalize call) never ran.
Fix: thread target.modelAbortSignal through as a new modelAbortSignal
runtimeOption, and merge it into clientRawRequest.signal (via the existing
mergeAbortSignals helper from open-sse/executors/base.ts) right before
dispatch, so an abandoned target's own promise chain now observes its abort
and can reach its cleanup path — new resolveDispatchClientRawRequest() makes
this mechanically testable in isolation.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* fix(sse): combo cooldown-wait state recording, rate-limit wedge recovery, OpenAI-format SSE error frames
Five related fixes surfaced by live incidents (dashboard log ids 1784457764961-73,
1784465227489-a2cbc0, 1784504040241-6f8b9a) while validating the Gemini TPM/cooldown-wait
work on this branch against real OpenClaw traffic:
- combo.ts: the model-lockout bail-out branches in dispatchWithCooldownRetry never
recorded lastStatus, so once every target in a set hit an existing lockout the final
check crystallized a bogus ALL_ACCOUNTS_INACTIVE 503 instead of reaching the
cooldown-wait decision, even with a real 429 + short retry-after observed.
- combo.ts/combo/types.ts: the "all credentials cooling down" pre-dispatch rejection
(buildModelCooldownBody) nests its retry hint as error.retry_after/reset_seconds, not
the top-level retryAfter every other 429 shape uses — combo's extraction only read the
latter, so earliestRetryAfter stayed null for this shape even after lastStatus was fixed.
- rateLimitManager.ts: the wedge-recovery watchdog used disconnect(), which releases the
heartbeat timer but never rejects jobs already QUEUED on that instance — orphaned
dispatches hung until the outer ~300s per-target timeout, well past real clients'
patience. Switched to stop({ dropWaitingJobs: true }), safe because the wedge condition
already requires RUNNING===0 && EXECUTING===0.
- earlyStreamKeepalive.ts: the in-band error frame emitted after committing to a 200 SSE
stream was hardcoded to Anthropic's `event: error` convention for every route, including
the OpenAI-format ones (/v1/chat/completions, /v1/responses) where that framing is
either invisible or malformed to a plain data-line parser. Added per-route
OPENAI_CHAT_ERROR_FRAME / OPENAI_RESPONSES_ERROR_FRAME and wired them in.
- chatCore.ts: persisted a synthetic clientResponse error body even when the client had
already disconnected (AbortError) before that body was ever computed — misleading the
dashboard into showing "what the client received" for a response that was never sent.
Also: RequestLoggerDetail.tsx — Provider/Client Event Stream panes lost their collapse
toggle when StreamSection replaced the collapsible PayloadSection (692d6be80, unifying
active/finished request views) without carrying the toggle over.
Each fix has a TDD regression test with a confirmed red-before-green cycle.
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
* test(sse): free-tier model + gemma-4 TPM-ceiling benchmark harness
Adds a live benchmark comparing free models OmniRoute exposes across
configured providers plus previously-unexercised no-auth providers
(felo-web, aihorde, opencode, duckduckgo-web — none need a connection
row, they were just never tried). Reuses liveGeminiShared.ts's SSE
parsers and CASE_BUILDERS instead of duplicating them.
Also adds a targeted TPM-stress test firing back-to-back large-context
prompts at the gemma-4-31b model across its 3 free hosts (Gemini,
NVIDIA, AI Horde) to isolate whether the documented 16k-tokens/minute
free-tier ceiling is Gemini-specific enforcement or an inherent
model property.
FORCE_TOOL_CHOICE_REQUIRED is a test-only, default-off env flag added
to liveGeminiShared.ts and live-gemini-agentic-loop.test.ts for an
earlier live A/B comparison of tool_choice: required vs unset — kept
as a reusable knob for future runs.
Co-Authored-By: Markus Hartung <markus.hartung@gmail.com>
* test(sse): benchmark for the 2026-07-22 newly-enabled provider batch
Adds NEWLY_ENABLED_MODELS to freeModelBenchmarkShared.ts (Mistral
Leanstral, OpenRouter's live "free"-tagged roster, OpenCode Zen's
current free models — refetched live from
https://opencode.ai/zen/v1/models since the static catalog had
drifted) and a dedicated workload benchmark test for them.
Co-Authored-By: Markus Hartung <markus.hartung@gmail.com>
* test(sse): sync geminiRateLimitTracker tests with e74a1722b's corrected Gemma 4 limits
e74a1722b updated geminiRateLimits.json's gemma-4-* entries from the stale
15/1500/-1 (rpm/rpd/tpm) to the real published free-tier values
16000/14400/16000, but never updated the tests asserting the old numbers.
Surfaced by running the full test:unit suite as a post-rebase sanity check.
Co-Authored-By: Markus Hartung <markus.hartream@gmail.com>
* chore(quality): file-size baseline for own-growth (#8213)
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
---------
Co-authored-by: Markus Hartung <markus.hartung@gmail.com>
Co-authored-by: Markus Hartung <markus.hartream@gmail.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
528 lines
17 KiB
TypeScript
528 lines
17 KiB
TypeScript
/**
|
|
* tests/integration/freeModelBenchmarkShared.ts
|
|
*
|
|
* Shared helpers for the free-model workload benchmark. Reuses the request
|
|
* generators, SSE parsers, and BASE_URL/API_KEY plumbing already built for
|
|
* the live Gemini workload tests (liveGeminiShared.ts) instead of
|
|
* duplicating them — those helpers are already provider-agnostic (they just
|
|
* happened to only ever be called with the Gemini "default" combo model).
|
|
*
|
|
* Unlike sendAndValidate() in liveGeminiShared.ts, benchmarkRequest() never
|
|
* throws/asserts on a single request failure — a free-tier model timing out
|
|
* or 429ing is an expected, recordable data point for a benchmark, not a
|
|
* regression. Per-model reliability is the thing being measured here.
|
|
*/
|
|
import {
|
|
API_KEY,
|
|
BASE_URL,
|
|
CASE_BUILDERS,
|
|
genHugeContextMessage,
|
|
readResponsesSSEStream,
|
|
readSSEStream,
|
|
type Message,
|
|
} from "./liveGeminiShared.ts";
|
|
|
|
export interface FreeModelSpec {
|
|
/** OmniRoute provider id, matching provider_connections.provider */
|
|
provider: string;
|
|
/** Full "provider/modelId" string sent as the `model` field */
|
|
model: string;
|
|
displayName: string;
|
|
}
|
|
|
|
// Providers resolved directly from the static NOAUTH_PROVIDERS registry
|
|
// (src/shared/constants/providers/noauth.ts) — no provider_connections row
|
|
// exists or is needed for these (see src/sse/services/auth.ts's noAuth
|
|
// resolution path). getActiveProviders() only sees configured *connections*,
|
|
// so these have to be unioned in separately or every no-auth model gets
|
|
// filtered out as "not active" even though they work with zero setup.
|
|
export const NO_AUTH_PROVIDER_IDS = new Set(["felo-web", "aihorde", "opencode", "duckduckgo-web"]);
|
|
|
|
// Curated from open-sse/config/freeModelCatalog.data.ts: the original 5
|
|
// providers configured+active on this deployment (checked via GET
|
|
// /api/providers — see getActiveProviders()), PLUS the no-auth providers
|
|
// above, which needed no configuration at all — they were just never
|
|
// exercised. duckduckgo-web is kept in despite being currently broken
|
|
// upstream (400 ERR_BAD_REQUEST as of this writing) because that's a real,
|
|
// reportable data point, not benchmark noise. theoldllm was tried and
|
|
// dropped: this deployment's egress IP is blocked by Vercel for it (403),
|
|
// an environment limitation, not a model worth benchmarking here.
|
|
//
|
|
// One or two representative models per provider, not the full catalog: a
|
|
// full sweep of every free model across every provider would be a multi-hour
|
|
// run hammering everyone's free-tier quota for marginal extra signal.
|
|
export const FREE_MODELS: FreeModelSpec[] = [
|
|
{
|
|
provider: "gemini",
|
|
model: "gemini/gemini-3.1-flash-lite",
|
|
displayName: "Gemini 3.1 Flash-Lite",
|
|
},
|
|
{ provider: "gemini", model: "gemini/gemma-4-31b-it", displayName: "Gemma 4 31B (Gemini)" },
|
|
{ provider: "nvidia", model: "nvidia/openai/gpt-oss-20b", displayName: "GPT OSS 20B (NVIDIA)" },
|
|
{ provider: "nvidia", model: "nvidia/z-ai/glm-5.1", displayName: "GLM 5.1 (NVIDIA)" },
|
|
{
|
|
provider: "nvidia",
|
|
model: "nvidia/google/gemma-4-31b-it",
|
|
displayName: "Gemma 4 31B (NVIDIA)",
|
|
},
|
|
{ provider: "mistral", model: "mistral/mistral-small-latest", displayName: "Mistral Small 4" },
|
|
{ provider: "mistral", model: "mistral/codestral-latest", displayName: "Codestral" },
|
|
{
|
|
provider: "pollinations",
|
|
model: "pollinations/openai-fast",
|
|
displayName: "OpenAI Fast (Pollinations)",
|
|
},
|
|
{
|
|
provider: "pollinations",
|
|
model: "pollinations/deepseek",
|
|
displayName: "DeepSeek (Pollinations)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/auto",
|
|
displayName: "Auto — Best Available (OpenRouter free pool)",
|
|
},
|
|
{ provider: "felo-web", model: "felo-web/felo-chat", displayName: "Felo Chat (no-auth)" },
|
|
{
|
|
provider: "aihorde",
|
|
model: "aihorde/google/gemma-4-31b",
|
|
displayName: "Gemma 4 31B (AI Horde)",
|
|
},
|
|
{
|
|
provider: "opencode",
|
|
model: "opencode/deepseek-v4-flash-free",
|
|
displayName: "DeepSeek V4 Flash Free (OpenCode)",
|
|
},
|
|
{
|
|
provider: "duckduckgo-web",
|
|
model: "duckduckgo-web/gpt-4o-mini",
|
|
displayName: "GPT-4o Mini (DuckDuckGo, known-broken)",
|
|
},
|
|
{
|
|
provider: "mistral",
|
|
model: "mistral/labs-leanstral-1-5-1",
|
|
displayName: "Leanstral 1.5.1 (Mistral)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/poolside/laguna-s-2.1:free",
|
|
displayName: "Laguna S 2.1 (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/nvidia/nemotron-3-super-120b-a12b:free",
|
|
displayName: "Nemotron 3 Super 120B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/nvidia/nemotron-3-ultra-550b-a55b:free",
|
|
displayName: "Nemotron 3 Ultra 550B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free",
|
|
displayName: "Nemotron 3 Nano Omni 30B Reasoning (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/google/gemma-4-26b-a4b-it:free",
|
|
displayName: "Gemma 4 26B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/google/gemma-4-31b-it:free",
|
|
displayName: "Gemma 4 31B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/nvidia/nemotron-3-nano-30b-a3b:free",
|
|
displayName: "Nemotron 3 Nano 30B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/openai/gpt-oss-20b:free",
|
|
displayName: "GPT OSS 20B (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/poolside/laguna-xs-2.1:free",
|
|
displayName: "Laguna XS 2.1 (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/poolside/laguna-m.1:free",
|
|
displayName: "Laguna M.1 (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/cohere/north-mini-code:free",
|
|
displayName: "North Mini Code (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "openrouter",
|
|
model: "openrouter/nvidia/nemotron-nano-9b-v2:free",
|
|
displayName: "Nemotron Nano 9B v2 (OpenRouter)",
|
|
},
|
|
{
|
|
provider: "opencode",
|
|
model: "opencode/nemotron-3-ultra-free",
|
|
displayName: "Nemotron 3 Ultra Free (OpenCode)",
|
|
},
|
|
{
|
|
provider: "opencode",
|
|
model: "opencode/north-mini-code-free",
|
|
displayName: "North Mini Code Free (OpenCode)",
|
|
},
|
|
{
|
|
provider: "opencode",
|
|
model: "opencode/laguna-s-2.1-free",
|
|
displayName: "Laguna S 2.1 Free (OpenCode)",
|
|
},
|
|
{ provider: "opencode", model: "opencode/big-pickle", displayName: "Big Pickle (OpenCode)" },
|
|
{
|
|
provider: "opencode",
|
|
model: "opencode/mimo-v2.5-free",
|
|
displayName: "MiMo V2.5 Free (OpenCode)",
|
|
},
|
|
];
|
|
|
|
// The batch the operator just enabled/added (2026-07-22): a Cerebras key,
|
|
// "free"-tagged OpenRouter models, and OpenCode's currently-live free roster
|
|
// (its old catalog entries had drifted — refetched from
|
|
// https://opencode.ai/zen/v1/models and cross-checked live before adding).
|
|
// Cerebras itself couldn't be smoke-tested here: the new key hit a live 402
|
|
// Payment Required (testStatus: credits_exhausted) — an account/billing
|
|
// issue on Cerebras' side, not addressable from this deployment.
|
|
export const NEWLY_ENABLED_MODELS: FreeModelSpec[] = FREE_MODELS.filter((m) =>
|
|
[
|
|
"mistral/labs-leanstral-1-5-1",
|
|
"openrouter/poolside/laguna-s-2.1:free",
|
|
"openrouter/nvidia/nemotron-3-super-120b-a12b:free",
|
|
"openrouter/nvidia/nemotron-3-ultra-550b-a55b:free",
|
|
"openrouter/nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free",
|
|
"openrouter/google/gemma-4-26b-a4b-it:free",
|
|
"openrouter/google/gemma-4-31b-it:free",
|
|
"openrouter/nvidia/nemotron-3-nano-30b-a3b:free",
|
|
"openrouter/openai/gpt-oss-20b:free",
|
|
"openrouter/poolside/laguna-xs-2.1:free",
|
|
"openrouter/poolside/laguna-m.1:free",
|
|
"openrouter/cohere/north-mini-code:free",
|
|
"openrouter/nvidia/nemotron-nano-9b-v2:free",
|
|
"opencode/nemotron-3-ultra-free",
|
|
"opencode/north-mini-code-free",
|
|
"opencode/laguna-s-2.1-free",
|
|
"opencode/big-pickle",
|
|
"opencode/mimo-v2.5-free",
|
|
].includes(m.model)
|
|
);
|
|
|
|
// The gemma-4-31b family across its 3 free hosts on this deployment — the
|
|
// specific model documented (docs/architecture/RESILIENCE_GUIDE.md context,
|
|
// tests/integration/gemini-large-context-tpm.test.ts) as hitting a hard
|
|
// 16000 tokens/minute free-tier ceiling on Gemini. Benchmarking the same
|
|
// model across hosts isolates whether the TPM wall is a gemma-4 property or
|
|
// specific to Gemini's free-tier enforcement.
|
|
export const TPM_STRESS_MODELS: FreeModelSpec[] = FREE_MODELS.filter((m) =>
|
|
m.displayName.includes("Gemma 4 31B")
|
|
);
|
|
|
|
// A representative slice of the 25 general-workload CASE_BUILDERS: one plain
|
|
// chat case, one code-review case, one long-context case, one agentic/
|
|
// multi-turn case, one structured-output case. Running the full 25 against
|
|
// every model in FREE_MODELS would multiply request count ~5x for marginal
|
|
// extra coverage over what already runs continuously in live-gemini-workload.
|
|
const BENCHMARK_CASE_NAMES = [
|
|
"basic coding question",
|
|
"code review request",
|
|
"long document analysis",
|
|
"agentic planning task",
|
|
"JSON-heavy structured data prompt",
|
|
];
|
|
|
|
export const BENCHMARK_CASES = CASE_BUILDERS.filter((tc) => BENCHMARK_CASE_NAMES.includes(tc.name));
|
|
|
|
export interface BenchmarkResult {
|
|
model: string;
|
|
displayName: string;
|
|
case: string;
|
|
ok: boolean;
|
|
status: number;
|
|
durationMs: number;
|
|
tokens: number;
|
|
contentLength: number;
|
|
finishReason: string;
|
|
error?: string;
|
|
}
|
|
|
|
export interface ModelBenchmarkSummary {
|
|
model: string;
|
|
displayName: string;
|
|
attempted: number;
|
|
succeeded: number;
|
|
successRate: number;
|
|
avgDurationMs: number;
|
|
avgTokens: number;
|
|
avgMsPerToken: number | null;
|
|
results: BenchmarkResult[];
|
|
}
|
|
|
|
/**
|
|
* Fetch the set of provider ids usable right now: providers with an active,
|
|
* non-expired connection, unioned with NO_AUTH_PROVIDER_IDS (those need no
|
|
* connection row at all — see the comment on that constant).
|
|
*/
|
|
export async function getActiveProviders(): Promise<Set<string>> {
|
|
const active = new Set<string>(NO_AUTH_PROVIDER_IDS);
|
|
if (!API_KEY) return active;
|
|
try {
|
|
const res = await fetch(`${BASE_URL}/api/providers`, {
|
|
headers: { Authorization: `Bearer ${API_KEY}` },
|
|
});
|
|
if (!res.ok) return active;
|
|
const data = await res.json();
|
|
const connections = data.connections || data;
|
|
for (const c of Array.isArray(connections) ? connections : []) {
|
|
if (c.isActive && c.testStatus !== "expired" && c.testStatus !== "error") {
|
|
active.add(c.provider);
|
|
}
|
|
}
|
|
} catch {
|
|
// network/setup failure — no-auth providers are still usable, keep them
|
|
}
|
|
return active;
|
|
}
|
|
|
|
/**
|
|
* Send one benchmark request against a specific model. Never throws — a
|
|
* failure (timeout, 429, 5xx, malformed stream) is recorded as `ok: false`
|
|
* with the reason, not surfaced as a test failure.
|
|
*/
|
|
export async function benchmarkRequest(
|
|
spec: FreeModelSpec,
|
|
tcName: string,
|
|
buildMessages: () => Message[],
|
|
timeoutMs = 60_000
|
|
): Promise<BenchmarkResult> {
|
|
const messages = buildMessages();
|
|
const start = performance.now();
|
|
|
|
try {
|
|
const res = await fetch(`${BASE_URL}/v1/chat/completions`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${API_KEY}`,
|
|
},
|
|
body: JSON.stringify({
|
|
model: spec.model,
|
|
messages,
|
|
stream: true,
|
|
max_tokens: 2048,
|
|
temperature: 0.3,
|
|
}),
|
|
signal: AbortSignal.timeout(timeoutMs),
|
|
});
|
|
|
|
const status = res.status;
|
|
if (status !== 200) {
|
|
const body = await res.text().catch(() => "");
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok: false,
|
|
status,
|
|
durationMs: Math.round(performance.now() - start),
|
|
tokens: 0,
|
|
contentLength: 0,
|
|
finishReason: "unknown",
|
|
error: body.slice(0, 200),
|
|
};
|
|
}
|
|
|
|
const { fullContent, finishReason, totalTokens } = await readSSEStream(res);
|
|
const durationMs = Math.round(performance.now() - start);
|
|
const ok = fullContent.length > 0 && (finishReason === "stop" || finishReason === "length");
|
|
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok,
|
|
status,
|
|
durationMs,
|
|
tokens: totalTokens,
|
|
contentLength: fullContent.length,
|
|
finishReason,
|
|
error: ok ? undefined : `empty or bad finish: ${finishReason}`,
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok: false,
|
|
status: 0,
|
|
durationMs: Math.round(performance.now() - start),
|
|
tokens: 0,
|
|
contentLength: 0,
|
|
finishReason: "error",
|
|
error: err instanceof Error ? err.message : String(err),
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TPM-stress round: `rounds` back-to-back large-context requests (~12-16k
|
|
* tokens each, via genHugeContextMessage — the same generator
|
|
* gemini-large-context-tpm.test.ts uses to trip Gemini's real 16000 TPM
|
|
* free-tier ceiling) fired with NO inter-request delay, so their token cost
|
|
* accumulates within the same provider-side one-minute window instead of
|
|
* spreading out. This is what distinguishes it from benchmarkRequest(),
|
|
* which deliberately paces requests apart — TPM ceilings are a *rate*
|
|
* property, invisible unless load is concentrated into a short window.
|
|
*/
|
|
export async function benchmarkTpmStress(
|
|
spec: FreeModelSpec,
|
|
approxTokensPerPrompt = 14_000,
|
|
rounds = 2,
|
|
timeoutMs = 90_000
|
|
): Promise<BenchmarkResult[]> {
|
|
const results: BenchmarkResult[] = [];
|
|
for (let i = 0; i < rounds; i++) {
|
|
const r = await benchmarkRequest(
|
|
spec,
|
|
`tpm-stress round ${i + 1}/${rounds} (~${approxTokensPerPrompt} tok)`,
|
|
() => [genHugeContextMessage(approxTokensPerPrompt)],
|
|
timeoutMs
|
|
);
|
|
results.push(r);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
/** Read one Responses-API benchmark request, mirroring benchmarkRequest(). */
|
|
export async function benchmarkResponsesRequest(
|
|
spec: FreeModelSpec,
|
|
tcName: string,
|
|
buildMessages: () => Message[],
|
|
timeoutMs = 60_000
|
|
): Promise<BenchmarkResult> {
|
|
const messages = buildMessages();
|
|
const start = performance.now();
|
|
|
|
try {
|
|
const res = await fetch(`${BASE_URL}/v1/responses`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${API_KEY}`,
|
|
},
|
|
body: JSON.stringify({
|
|
model: spec.model,
|
|
input: messages,
|
|
stream: true,
|
|
max_output_tokens: 2048,
|
|
temperature: 0.3,
|
|
}),
|
|
signal: AbortSignal.timeout(timeoutMs),
|
|
});
|
|
|
|
const status = res.status;
|
|
if (status !== 200) {
|
|
const body = await res.text().catch(() => "");
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok: false,
|
|
status,
|
|
durationMs: Math.round(performance.now() - start),
|
|
tokens: 0,
|
|
contentLength: 0,
|
|
finishReason: "unknown",
|
|
error: body.slice(0, 200),
|
|
};
|
|
}
|
|
|
|
const { fullContent, finishReason, totalTokens } = await readResponsesSSEStream(res);
|
|
const durationMs = Math.round(performance.now() - start);
|
|
const ok = fullContent.length > 0;
|
|
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok,
|
|
status,
|
|
durationMs,
|
|
tokens: totalTokens,
|
|
contentLength: fullContent.length,
|
|
finishReason,
|
|
error: ok ? undefined : `empty content, finish=${finishReason}`,
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
case: tcName,
|
|
ok: false,
|
|
status: 0,
|
|
durationMs: Math.round(performance.now() - start),
|
|
tokens: 0,
|
|
contentLength: 0,
|
|
finishReason: "error",
|
|
error: err instanceof Error ? err.message : String(err),
|
|
};
|
|
}
|
|
}
|
|
|
|
export function summarize(spec: FreeModelSpec, results: BenchmarkResult[]): ModelBenchmarkSummary {
|
|
const succeeded = results.filter((r) => r.ok);
|
|
const avgDurationMs =
|
|
results.length > 0
|
|
? Math.round(results.reduce((s, r) => s + r.durationMs, 0) / results.length)
|
|
: 0;
|
|
const avgTokens =
|
|
succeeded.length > 0
|
|
? Math.round(succeeded.reduce((s, r) => s + r.tokens, 0) / succeeded.length)
|
|
: 0;
|
|
const tokenRates = succeeded.filter((r) => r.tokens > 0).map((r) => r.durationMs / r.tokens);
|
|
const avgMsPerToken =
|
|
tokenRates.length > 0
|
|
? Math.round((tokenRates.reduce((s, v) => s + v, 0) / tokenRates.length) * 10) / 10
|
|
: null;
|
|
|
|
return {
|
|
model: spec.model,
|
|
displayName: spec.displayName,
|
|
attempted: results.length,
|
|
succeeded: succeeded.length,
|
|
successRate: results.length > 0 ? succeeded.length / results.length : 0,
|
|
avgDurationMs,
|
|
avgTokens,
|
|
avgMsPerToken,
|
|
results,
|
|
};
|
|
}
|
|
|
|
export function formatBenchmarkTable(summaries: ModelBenchmarkSummary[]): string {
|
|
const rows = summaries
|
|
.slice()
|
|
.sort((a, b) => b.successRate - a.successRate || a.avgDurationMs - b.avgDurationMs)
|
|
.map((s) => {
|
|
const rate = `${s.succeeded}/${s.attempted}`.padStart(5);
|
|
const pct = `${Math.round(s.successRate * 100)}%`.padStart(4);
|
|
const dur = `${s.avgDurationMs}ms`.padStart(8);
|
|
const tok = `${s.avgTokens}`.padStart(6);
|
|
const rate2 = s.avgMsPerToken !== null ? `${s.avgMsPerToken}ms/tok` : "n/a".padStart(10);
|
|
return ` ${s.displayName.padEnd(38)} ${rate} (${pct}) | avg ${dur} | avg ${tok} tok | ${rate2}`;
|
|
});
|
|
|
|
return [
|
|
"\n Free-model workload benchmark:",
|
|
" " + "model".padEnd(38) + " success | latency | tokens | throughput",
|
|
...rows,
|
|
].join("\n");
|
|
}
|