Files
ai/controllers/probe_test.go
zeekay 531e9f423a lane: no platform ceiling on the paid lane; a payer spends what they bought
The paid lane capped every paid-lane call together at PAID_LANE_DAILY ($10 a UTC
day by default) and each org at PAID_LANE_ORG_SHARE (25% of it), and counted
wallet-paid calls against both: a customer with $1,000 prepaid was held to $2.50 a
day, and every payer together to $10. The rule is no ceiling but what a payer
prepays, or their usage. Free users get free models; payers spend their own money.

Deleted: the platform day and the org share (paidLaneDaily, orgShare, the day,
spent and per-org accounting in laneBook), the paid_day store (object.PaidDay, its
table registration, the writer, flush, drain, load and turn), Settled's drain of
it, laneSpend, PaidLaneOpen (the gate reads !FreeOnly()), and the refusals only the
day produced ("closed", "after 00:00 UTC"). paid_lane_ceiling named a platform
ceiling that no longer exists: a payer the lane has no room for is told
paid_lane_full, the code its refusal already carried, in X-Hanzo-Lane-Reason and
X-Hanzo-Usage-Reason. Stores keep an orphan paid_day table nothing reads.

Kept: the wallet hold (reserveFor), the plan class's LimitGrant.Spend less the
payer's calls in flight, quote sizing against it, 8 seats a payer, lapse and
stream-idle give-back, race losers holding the seat, BYO, and the free-model
hand-off for a payer with no room.

The replica guard. The lane was closed unless CLOUD_API_REPLICAS=1 because the day
lived in this process. No deployment sets CLOUD_API_REPLICAS: the cloud chart
injects CLOUD_REPLICAS (1 in production). So the boot assertion meant to stop a
second replica double-spending the in-pod wallet ledger never fired, and the lane
guard was the one live check; it also kept the paid lane closed in production. The
plan holds that remain are in-pod exactly as the wallet ledger is, so they take its
guard: SinglePodReplicaHint reads CLOUD_REPLICAS, and bootstrap refuses to start
when the deployment states more than one replica. A declared second replica stops
the process instead of closing the lane, and no switch is left whose only purpose
was the day.
2026-10-09 14:08:38 -07:00

113 lines
4.3 KiB
Go

// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"bufio"
"bytes"
"context"
"math"
"net"
"github.com/zap-proto/zip"
)
// How a test in this package gets a controller. One way.
//
// A controller IS its request — the resolved principal, the org, the writer it
// answers through — so a test needs a real context to hold one, and zip builds that
// over a synthetic fasthttp request. The thing under test therefore sees exactly
// what production hands it.
//
// It replaced an httptest request, an httptest recorder, a router context, a Reset
// to marry them and an Init to register the controller against a name — five steps
// whose only purpose was to hand a handler somewhere to read from and write to.
// Both live on the context now.
// visit builds a controller for one request at target — a path, and the query string
// if it has one.
//
// THE TARGET IS SET WHOLE, because zip's TestCtx sets the PATH: hand it
// "/x?org=maxpower" and the question mark lands in the path while Queries() stays
// empty, so a handler reads no parameters at all. Tests written that way do not fail
// — they quietly exercise the no-parameter case and agree with themselves. Setting
// the request URI parses it where a request is parsed.
func visit(method, target string) *ApiController {
c := &ApiController{
Ctx: zip.New(zip.Config{DisableStartupMessage: true, ReadBufferSize: 32 << 10}).TestCtx(method, target),
}
c.Fiber().Request().SetRequestURI(target)
c.SetContext(paidSeat(c.Context()))
return c
}
// paidSeat puts ctx on the paid lane, as every request in this package's tests is:
// they exercise what the paid lane serves. Its seat holds without bound on a book of
// its own, so nothing a test holds reaches the paid lane's. A test of the lanes seats
// its request itself (Seat), which gives this seat back first.
func paidSeat(ctx context.Context) context.Context {
return context.WithValue(ctx, laneKey{}, &laneState{seat: &seat{book: &laneBook{}, held: math.MaxInt64}})
}
// from sets the socket peer, the one address on a request nobody but the network
// can set. The public lane counts by it, so a test that does not set it is testing
// a visitor with no address.
func from(c *ApiController, addr string) *ApiController {
if a, err := net.ResolveTCPAddr("tcp", addr); err == nil {
c.Fiber().RequestCtx().SetRemoteAddr(a)
}
return c
}
// sent is the body the controller wrote.
func sent(c *ApiController) string { return string(c.Fiber().Response().Body()) }
// answered is the status the controller set, or 200 when it set none.
func answered(c *ApiController) int { return c.Fiber().Response().StatusCode() }
// stream is a destination for the SSE relays, which take the writer they write to
// rather than reaching for one. Read it with String once the relay has returned.
type stream struct {
buf bytes.Buffer
w *bufio.Writer
}
func toStream() *stream {
s := &stream{}
s.w = bufio.NewWriter(&s.buf)
return s
}
// String flushes and returns everything the relay wrote.
func (s *stream) String() string {
_ = s.w.Flush()
return s.buf.String()
}
// presenting gives c the credential a caller arrives with.
//
// ONE VERB, because there is one way to be somebody here: a signed token, verified
// the way production verifies it. Pass authtest.Bearer for a caller who should be
// accepted, or the literal thing a test wants REFUSED — a publishable key, a forgery,
// an empty string for nobody at all. What it replaced was four different harnesses
// seeding a fake session store with whatever identity each test wanted to be true,
// which is not an authorisation test but a test of the fake.
func presenting(c *ApiController, authorization string) *ApiController {
if authorization != "" {
c.Fiber().Request().Header.Set("Authorization", authorization)
}
return c
}