The platform could only talk to one vendor. Moving off Claude — for cost, or
because a client asks for Gemini — meant a rewrite behind an interface that
already had exactly the right shape and one implementation.
`openai` is not only OpenAI. Groq, Gemini's compatibility endpoint, OpenRouter,
Together, vLLM and a local Ollama all serve the chat-completions shape, so one
implementation reaches all of them and the difference between them is a base
URL and three model ids. That is why this is one file and not a package per
vendor.
`routing.go` had the vendor baked into the routing table every provider has to
read: effort was `anthropic.OutputConfigEffort`. Nothing was wrong with that
while there was one implementation; it became wrong the moment there were two,
because the OpenAI path would have had to import the Anthropic SDK to learn how
hard to think. Effort is now the platform's own three-value vocabulary and each
implementation maps it onto whatever its API calls the same idea.
THE ACCOUNTING DIFFERS BETWEEN THE TWO WIRES, and getting it wrong would have
been invisible. OpenAI reports prompt_tokens INCLUSIVE of the cached prefix;
Anthropic reports input tokens EXCLUSIVE of it and carries the cache
separately. Usage.Total() adds all four fields, so copying both numbers across
verbatim bills the cached prefix twice — worst on long conversations, which is
exactly where I3's budget matters most. The run would still answer; it would
just hit BudgetExceeded early, for no visible reason. normalise() subtracts,
and there is a test named after it.
Streamed tool calls are keyed by their wire index, not appended in arrival
order. Providers interleave the fragments of parallel calls, so appending
splices one call's arguments onto another's — and the result is usually two
calls that are each valid JSON and both wrong, which means the tools run with
inputs the model never chose and nothing errors. Mutation-checked: ignoring the
index produces `{"day"{"week":"friday"}:"next"}` and the test catches it.
Three configuration mistakes are refused at startup rather than at runtime:
- MODEL_BASE_URL without MODEL_PROVIDER=openai. The anthropic path has one
endpoint and ignores the field, so this is a deployment that believes it
switched providers and did not — every run still goes to Anthropic and is
still billed there, with nothing in the logs to say so. Cost is the whole
reason this change exists, and that is the one mistake that silently
defeats it.
- An unrecognised MODEL_PROVIDER, once at boot instead of once per run.
- A production deployment with no credential — except against localhost,
which needs none, and demanding one would make the free local path
impossible to configure.
reasoning_effort is opt-in via MODEL_REASONING_EFFORT. Reasoning models accept
it; most others reject the entire request with a 400 rather than ignoring an
unknown key, so every deployment would have had to opt out instead.
`make eval-live` now reads the same environment the service does and logs which
provider answered, because a suite that cannot say which model produced a
result is a suite whose result cannot be compared with another run's. That is
the point of this change: §12 leaves model hosting open, and this makes the
decision cheap to reverse and possible to settle on evidence. Weigh the I7 case
heaviest — a cheaper model that follows the planted injection is a security
regression, not a saving.
Default behaviour is unchanged: MODEL_PROVIDER unset means anthropic, and
ANTHROPIC_API_KEY still works, so no existing deployment needs an edit.
NOT verified against a live provider — no credential was available on this
machine. Tested against a fake endpoint covering both paths, and the three
guarantees above are mutation-checked.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJvibeSc1JYXjatankqM1g
215 lines
6.8 KiB
Go
215 lines
6.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/anthropics/anthropic-sdk-go"
|
|
|
|
"github.com/krow/krow-backend/go-api/internal/config"
|
|
)
|
|
|
|
func TestParseTier(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
want Tier
|
|
known bool
|
|
}{
|
|
{"fast", TierFast, true},
|
|
{"balanced", TierBalanced, true},
|
|
{"deep", TierDeep, true},
|
|
{" DEEP ", TierDeep, true},
|
|
// Unset means the default, and is not a drift signal: most specs
|
|
// simply do not declare a tier.
|
|
{"", DefaultTier, true},
|
|
// A tier that is not in the vocabulary still runs, at the default, but
|
|
// reports itself so a drifted definition stays visible.
|
|
{"thorough", DefaultTier, false},
|
|
}
|
|
for _, c := range cases {
|
|
got, known := ParseTier(c.in)
|
|
if got != c.want || known != c.known {
|
|
t.Errorf("ParseTier(%q) = (%q, %v), want (%q, %v)", c.in, got, known, c.want, c.known)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUsageTotalCountsCacheReads(t *testing.T) {
|
|
// A cache read is cheaper than fresh input, not free. Excluding it would
|
|
// make the budget drift further from the truth the longer a run went on.
|
|
u := Usage{InputTokens: 100, OutputTokens: 50, CacheReadTokens: 900, CacheCreationTokens: 10}
|
|
if got := u.Total(); got != 1060 {
|
|
t.Errorf("Total() = %d, want 1060", got)
|
|
}
|
|
}
|
|
|
|
func TestRequestValidate(t *testing.T) {
|
|
if err := (Request{}).Validate(); err == nil {
|
|
t.Error("a request with no messages should be refused")
|
|
}
|
|
|
|
blank := Request{Messages: []Message{{Role: RoleUser, Text: " "}}}
|
|
if err := blank.Validate(); err == nil {
|
|
t.Error("a whitespace-only message should be refused")
|
|
}
|
|
|
|
bad := Request{Messages: []Message{{Role: "system", Text: "hi"}}}
|
|
err := bad.Validate()
|
|
var gwErr *Error
|
|
if !errors.As(err, &gwErr) || gwErr.Code != CodeInvalidRequest {
|
|
t.Errorf("a bad role should give CodeInvalidRequest, got %v", err)
|
|
}
|
|
|
|
ok := Request{Messages: []Message{{Role: RoleUser, Text: "which shifts are uncovered?"}}}
|
|
if err := ok.Validate(); err != nil {
|
|
t.Errorf("a valid request was refused: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCompleteWithoutCredentialsIsStructured(t *testing.T) {
|
|
// The service boots without a key on purpose. The failure has to arrive as
|
|
// something a run can terminate with, not as a panic or a bare string.
|
|
g := NewAnthropic(Config{})
|
|
_, err := g.Complete(context.Background(), Request{
|
|
Messages: []Message{{Role: RoleUser, Text: "anything"}},
|
|
})
|
|
|
|
var gwErr *Error
|
|
if !errors.As(err, &gwErr) {
|
|
t.Fatalf("want a *gateway.Error, got %T: %v", err, err)
|
|
}
|
|
if gwErr.Code != CodeNotConfigured {
|
|
t.Errorf("Code = %q, want %q", gwErr.Code, CodeNotConfigured)
|
|
}
|
|
if gwErr.Retryable() {
|
|
t.Error("a missing key is not fixed by retrying")
|
|
}
|
|
}
|
|
|
|
func TestRetryable(t *testing.T) {
|
|
cases := map[*Error]bool{
|
|
{Code: CodeRateLimited}: true,
|
|
{Code: CodeTimeout}: true,
|
|
{Code: CodeUpstream, Status: 503}: true,
|
|
{Code: CodeUpstream, Status: 400}: false,
|
|
{Code: CodeUnauthorized, Status: 401}: false,
|
|
{Code: CodeInvalidRequest}: false,
|
|
// The one that matters: re-sending a request the model declined is how
|
|
// a loop spends a whole budget on a single turn.
|
|
{Code: CodeRefused, Category: "cyber"}: false,
|
|
}
|
|
for err, want := range cases {
|
|
if got := err.Retryable(); got != want {
|
|
t.Errorf("%s: Retryable() = %v, want %v", err.Code, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFromConfigPinsEffortPerTier(t *testing.T) {
|
|
cfg := FromConfig(config.ModelConfig{
|
|
APIKey: "test", Fast: "m-fast", Balanced: "m-balanced", Deep: "m-deep",
|
|
MaxOutputTokens: 8000,
|
|
})
|
|
|
|
if cfg.Fast.Effort != EffortLow {
|
|
t.Errorf("fast effort = %q, want low", cfg.Fast.Effort)
|
|
}
|
|
if cfg.Balanced.Effort != EffortHigh {
|
|
t.Errorf("balanced effort = %q, want high", cfg.Balanced.Effort)
|
|
}
|
|
if cfg.Deep.Effort != EffortXhigh {
|
|
t.Errorf("deep effort = %q, want xhigh", cfg.Deep.Effort)
|
|
}
|
|
if cfg.MaxOutputTokens != 8000 {
|
|
t.Errorf("MaxOutputTokens = %d, want 8000", cfg.MaxOutputTokens)
|
|
}
|
|
}
|
|
|
|
// The neutral effort vocabulary has to land on the vendor's own enum, and that
|
|
// mapping is the one thing FromConfig can no longer assert now that its result
|
|
// is provider-independent. Untested, a renamed SDK constant would silently
|
|
// route every tier to whatever the default arm returns.
|
|
func TestSDKEffortMapsToAnthropic(t *testing.T) {
|
|
cases := map[Effort]anthropic.OutputConfigEffort{
|
|
EffortLow: anthropic.OutputConfigEffortLow,
|
|
EffortHigh: anthropic.OutputConfigEffortHigh,
|
|
EffortXhigh: anthropic.OutputConfigEffortXhigh,
|
|
}
|
|
for neutral, want := range cases {
|
|
if got := sdkEffort(neutral); got != want {
|
|
t.Errorf("sdkEffort(%q) = %q, want %q", neutral, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRoutingSelectsPerTier(t *testing.T) {
|
|
g := NewAnthropic(Config{
|
|
Fast: Routing{Model: "m-fast"},
|
|
Balanced: Routing{Model: "m-balanced"},
|
|
Deep: Routing{Model: "m-deep"},
|
|
})
|
|
|
|
cases := map[Tier]string{
|
|
TierFast: "m-fast",
|
|
TierBalanced: "m-balanced",
|
|
TierDeep: "m-deep",
|
|
// A zero value routes to balanced rather than to an empty model id.
|
|
Tier(""): "m-balanced",
|
|
}
|
|
for tier, want := range cases {
|
|
if got := g.routing(tier).Model; got != want {
|
|
t.Errorf("routing(%q) = %q, want %q", tier, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ── Retrying what is worth retrying ────────────────────────────────────── */
|
|
|
|
func TestATransientOverloadIsWorthRetrying(t *testing.T) {
|
|
// The classification this asserts existed from the start and had ZERO
|
|
// callers, so a 529 killed runs that would have succeeded a moment later.
|
|
// Found by a real overload during live testing.
|
|
overloaded := &Error{Code: CodeUpstream, Message: "overloaded", Status: 529}
|
|
if !overloaded.Retryable() {
|
|
t.Error("a 529 overload should be retryable — it is the transient failure that actually happens")
|
|
}
|
|
|
|
for _, e := range []*Error{
|
|
{Code: CodeRateLimited, Status: 429},
|
|
{Code: CodeTimeout},
|
|
{Code: CodeUpstream, Status: 503},
|
|
} {
|
|
if !e.Retryable() {
|
|
t.Errorf("%s (status %d) should be retryable", e.Code, e.Status)
|
|
}
|
|
}
|
|
|
|
// And the ones that will fail identically every time must not be.
|
|
for _, e := range []*Error{
|
|
{Code: CodeInvalidRequest, Status: 400},
|
|
{Code: CodeUnauthorized, Status: 401},
|
|
{Code: CodeNotConfigured},
|
|
{Code: CodeRefused},
|
|
} {
|
|
if e.Retryable() {
|
|
t.Errorf("%s should NOT be retryable — the same call will fail the same way", e.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnUpstreamErrorNamesItsStatus(t *testing.T) {
|
|
// "the model call failed" cost an hour of debugging, because the trajectory
|
|
// records the message and the message did not say it was a 529. A failure
|
|
// an operator cannot classify is a failure they cannot act on.
|
|
e := &Error{
|
|
Code: CodeUpstream,
|
|
Message: "the model call failed (http 529)",
|
|
Status: 529,
|
|
}
|
|
if !strings.Contains(e.Error(), "529") {
|
|
t.Errorf("the rendered error hides its status: %s", e.Error())
|
|
}
|
|
}
|