mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
The paid lane was one switch for every caller. FreeOnly stays the platform's switch; under it each request is put on a lane by LaneFilter, after the balance gate, from the grant the host's usage policy left on it. A priced call that a paid plan's included usage pays for, or the payer's prepaid or granted credit past it, is served on the paid lane; every other caller is on the free lane: no plan, a free model, a model's free daily cap, a policy that said nothing, and a request that never passed the filter. Every read on the request path asks FreeOnlyFor(ctx); candidates, openrouterTail and paying take its answer. PAID_LANE_DAILY (USD, default 10, 0 closes) bounds what all paid-lane calls spend in a UTC day, counted from each usage record at the most of its price and cost. Past it payers are on the free lane, and a chat for a third-party model is answered by the free model in limited mode. A paid plan's chat past its included usage (plan_allowance_used, model_cap) is answered in limited mode on any client. Responses carry X-Hanzo-Lane (paid|free) and X-Hanzo-Lane-Reason (paid_lane_ceiling). Co-authored-by: Hanzo Dev <[email protected]>
175 lines
7.2 KiB
Go
175 lines
7.2 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// `down` is the whole of "may this request be moved at all", and it is a
|
|
// statement about the VENDOR rather than about the request.
|
|
//
|
|
// Every case here is a status a real upstream answers with, and the split is the
|
|
// point: an account we cannot pay with and a vendor that fell over are facts that
|
|
// outlast the request, so it may be carried elsewhere; a body we malformed, a
|
|
// model they never had, content they refused and a queue that is briefly full are
|
|
// facts about THIS request, so moving it would hand the caller a smaller model
|
|
// instead of the error they need to read.
|
|
func TestDownIsAFactAboutTheVendor(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
what string
|
|
err error
|
|
want bool
|
|
}{
|
|
{"our account with them is spent", &apiError{status: http.StatusPaymentRequired, msg: "Insufficient credits."}, true},
|
|
{"they fell over", &apiError{status: http.StatusInternalServerError, msg: "internal server error"}, true},
|
|
{"a gateway in front of them fell over", &apiError{status: http.StatusBadGateway, msg: "bad gateway"}, true},
|
|
{"they are unavailable", &apiError{status: http.StatusServiceUnavailable, msg: "service unavailable"}, true},
|
|
{"a gateway in front of them timed out", &apiError{status: http.StatusGatewayTimeout, msg: "gateway timeout"}, true},
|
|
|
|
{"busy, and it says so", &apiError{status: http.StatusTooManyRequests, msg: "rate limit exceeded"}, false},
|
|
{"busy, but the reason is money", &apiError{status: http.StatusTooManyRequests, msg: "insufficient credit"}, true},
|
|
|
|
{"no status to read, and the reason is money", errors.New("insufficient_quota"), true},
|
|
{"no status to read, and the wire simply broke", errors.New("connection reset by peer"), false},
|
|
|
|
{"we malformed the body", &apiError{status: http.StatusBadRequest, msg: "malformed request"}, false},
|
|
{"they have not got that model", &apiError{status: http.StatusNotFound, msg: "no such model"}, false},
|
|
{"they refused the content", &apiError{status: http.StatusUnprocessableEntity, msg: "content policy"}, false},
|
|
{"they would not take our credential", &apiError{status: http.StatusUnauthorized, msg: "unauthorized"}, false},
|
|
} {
|
|
t.Run(tc.what, func(t *testing.T) {
|
|
if got := down(tc.err, strings.ToLower(tc.err.Error())); got != tc.want {
|
|
t.Errorf("down(%v) = %v, want %v", tc.err, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// fallback answers WHERE a refused request may go, and the empty answer — the
|
|
// refusal standing as itself — is as much a result as a list of routes.
|
|
//
|
|
// It is asserted directly because it is a decision and not a walk: no vendor is
|
|
// stood up here, nothing is dialled, and every branch is reachable by stating the
|
|
// three facts it reads.
|
|
func TestFallbackNamesWhereARefusalMayGo(t *testing.T) {
|
|
// A pool with one borrowed route in it, and our own compute switched off, so
|
|
// "borrowed" and "our own" are told apart by which list comes back rather than
|
|
// by both happening to be empty.
|
|
stage := func(t *testing.T, ourOwn bool) {
|
|
t.Helper()
|
|
fam := freeFamily()
|
|
restore(t, fam)
|
|
restore(t, engineFam)
|
|
fam.spares = []string{"v/borrowed:free"}
|
|
fam.loaded, fam.fetchedAt = true, time.Now()
|
|
if ourOwn {
|
|
t.Setenv("TEST_ENGINE_URL", "http://engine.invalid")
|
|
engineFam.urlKey = "TEST_ENGINE_URL"
|
|
} else {
|
|
engineFam.urlKey = "TEST_ENGINE_URL_UNSET"
|
|
}
|
|
engineFam.providerFn = nil
|
|
}
|
|
|
|
spent := &apiError{status: http.StatusPaymentRequired, msg: "Insufficient credits."}
|
|
|
|
t.Run("a vendor that cannot serve moves onto the pool", func(t *testing.T) {
|
|
stage(t, true)
|
|
fam := otherFamily(t, "http://vendor.invalid") // states no terms
|
|
got := fallback(fam, "enso", spent, nil)
|
|
if len(got) != 2 {
|
|
t.Fatalf("routes=%v, want the borrowed route and our own", ids(got))
|
|
}
|
|
})
|
|
|
|
// A third-party model is served as itself or not at all: whatever its vendor
|
|
// says, nothing stands in for it — not an Enso SKU, not the free pool, not our own
|
|
// compute.
|
|
for _, ourOwn := range []bool{true, false} {
|
|
t.Run(fmt.Sprintf("a named third-party model is never stood in for (compute of our own: %v)", ourOwn), func(t *testing.T) {
|
|
stage(t, ourOwn)
|
|
restore(t, ensoFam)
|
|
ensoFam.providerFn = func() *object.Provider {
|
|
return &object.Provider{Owner: "admin", Name: "enso", Type: "Enso", ProviderUrl: "http://enso.invalid"}
|
|
}
|
|
fam := spareFamily(t, "http://vendor.invalid", "v/borrowed:free", "v/paid")
|
|
for _, tc := range []struct {
|
|
err error
|
|
what string
|
|
}{
|
|
{spent, "spent"},
|
|
{&apiError{status: http.StatusTooManyRequests, msg: "rate limit exceeded"}, "every account busy"},
|
|
{&apiError{status: http.StatusBadGateway, msg: "bad gateway"}, "down"},
|
|
{errors.New("dial tcp: connection refused"), "unreachable"},
|
|
} {
|
|
for _, sku := range []string{"v/paid", "v/borrowed:free"} {
|
|
if got := ids(fallback(fam, sku, tc.err, nil)); len(got) != 0 {
|
|
t.Errorf("%s %s: routes=%v, want none", tc.what, sku, got)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("a refusal that is not the vendor's stands", func(t *testing.T) {
|
|
stage(t, true)
|
|
fam := otherFamily(t, "http://vendor.invalid")
|
|
missing := &apiError{status: http.StatusNotFound, msg: "no such model"}
|
|
if got := fallback(fam, "enso", missing, nil); len(got) != 0 {
|
|
t.Errorf("routes=%v, want none — a model they have not got is not an outage", ids(got))
|
|
}
|
|
})
|
|
|
|
t.Run("our own spend gate reaches the customer", func(t *testing.T) {
|
|
stage(t, true)
|
|
fam := otherFamily(t, "http://vendor.invalid")
|
|
// The same 402, carrying OUR code: the customer owes money, and answering
|
|
// that with a free model would say their payment problem had fixed itself.
|
|
body := []byte(`{"error":{"code":"insufficient_balance","message":"add funds"}}`)
|
|
if got := fallback(fam, "enso", spent, body); len(got) != 0 {
|
|
t.Errorf("routes=%v, want none — this bill is the customer's to read", ids(got))
|
|
}
|
|
})
|
|
|
|
t.Run("no refusal is nothing to move", func(t *testing.T) {
|
|
stage(t, true)
|
|
fam := otherFamily(t, "http://vendor.invalid")
|
|
if got := fallback(fam, "enso", nil, nil); len(got) != 0 {
|
|
t.Errorf("routes=%v, want none", ids(got))
|
|
}
|
|
})
|
|
}
|
|
|
|
// ids renders a route list for a failure message.
|
|
func ids(routes []spare) []string {
|
|
out := make([]string, 0, len(routes))
|
|
for _, r := range routes {
|
|
out = append(out, r.id)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// With the paid lane closed to a request, a third-party model's own vendors and its
|
|
// priced OpenRouter copy are never offered, and nothing stands in for them: nothing is
|
|
// offered. Open, its own vendors are.
|
|
func TestWithThePaidLaneOffAThirdPartyRouteIsOfferedNothing(t *testing.T) {
|
|
cooled.forget()
|
|
spareFamily(t, "http://vendor.invalid", "v/borrowed:free", "openai/gpt-4o")
|
|
route := &modelRoute{providerName: "do-ai", upstreamModel: "openai-gpt-4o", premium: false,
|
|
fallbacks: []modelRouteFallback{{providerName: "anthropic", upstreamModel: "claude"}}}
|
|
if got := candidates("acme", route, nil, true); len(got) != 0 {
|
|
t.Fatalf("candidates = %+v, want none", got)
|
|
}
|
|
if got := candidates("acme", route, nil, false); len(got) < 2 || got[0].provider != "do-ai" || got[1].provider != "anthropic" {
|
|
t.Fatalf("candidates = %+v, want the route's own vendors first", got)
|
|
}
|
|
}
|