Git designed for AI agents.
graft-resolve: the AI budget counts money in exact nano-dollars
Federico Elia33de6c6086e0
diff --git a/tools/graft-agents/resolve/bench/pricing.go b/tools/graft-agents/resolve/bench/pricing.goindex aab96f2..7b30b5d 100644--- a/tools/graft-agents/resolve/bench/pricing.go+++ b/tools/graft-agents/resolve/bench/pricing.go@@ -7,6 +7,7 @@ import ("fmt""io""math"+ "math/big""net/http""net/url""strconv"@@ -39,12 +40,28 @@ type Pricing struct {// prices on every output token can only overstate the cost.func (p Pricing) Prices() resolve.Prices {return resolve.Prices{- InputPerMillion: p.Prompt * 1e6,- OutputPerMillion: (p.Completion + p.Reasoning) * 1e6,+ InputPerMillion: PerMillion(p.Prompt),+ OutputPerMillion: PerMillion(p.Completion, p.Reasoning),PerRequest: p.Request,}}+// PerMillion returns the sum of per-token prices times a million, computed in decimal from the+// shortest representation of each price (the decimal OpenRouter wrote), so that 0.0000002 per+// token is exactly 0.2 per million rather than 0.19999999999999998.+func PerMillion(perToken ...float64) float64 {+ sum := new(big.Rat)+ for _, v := range perToken {+ r, ok := new(big.Rat).SetString(strconv.FormatFloat(v, 'g', -1, 64))+ if !ok {+ return math.NaN()+ }+ sum.Add(sum, r)+ }+ f, _ := sum.Mul(sum, big.NewRat(1_000_000, 1)).Float64()+ return f+}+type openRouterEndpoints struct {Data struct {ID string `json:"id"`diff --git a/tools/graft-agents/resolve/bench/pricing_test.go b/tools/graft-agents/resolve/bench/pricing_test.goindex 31c43fd..55612e4 100644--- a/tools/graft-agents/resolve/bench/pricing_test.go+++ b/tools/graft-agents/resolve/bench/pricing_test.go@@ -58,6 +58,10 @@ func TestOpenRouterPricing(t *testing.T) {if real.Prompt != 0.0000002 || real.Completion != 0.000001 || real.Reasoning != 0 || real.Request != 0 {t.Fatalf("pricing %+v", real)}+ // Per million in decimal: 2e-7 per token is 0.2, not 0.19999999999999998.+ if got := real.Prices(); got.InputPerMillion != 0.2 || got.OutputPerMillion != 1 {+ t.Fatalf("prices %+v", got)+ }// Without a bound every tier counts.all, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/real", 0)if err != nil {diff --git a/tools/graft-agents/resolve/bench/report.go b/tools/graft-agents/resolve/bench/report.goindex 551bc94..f56925e 100644--- a/tools/graft-agents/resolve/bench/report.go+++ b/tools/graft-agents/resolve/bench/report.go@@ -122,7 +122,7 @@ func (rep Report) WriteMarkdown(w io.Writer) error {if p := rep.Pricing; p != nil {fmt.Fprintf(&b, "Model %s, priced at run time (%s, %s, highest of %d endpoints): $%g per million input tokens, $%g per million output tokens (completion $%g + internal reasoning $%g), $%g per request.\n\n",p.Model, p.Source, p.FetchedAt.Format(time.RFC3339), p.Endpoints,- p.Prompt*1e6, (p.Completion+p.Reasoning)*1e6, p.Completion*1e6, p.Reasoning*1e6, p.Request)+ PerMillion(p.Prompt), PerMillion(p.Completion, p.Reasoning), PerMillion(p.Completion), PerMillion(p.Reasoning), p.Request)}b.WriteString("| family | cases | correct (exact) | wrong | rejected by checks | not sent | model error | not run (cap) | abstained | error rate |\n")b.WriteString("|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|\n")diff --git a/tools/graft-agents/resolve/budget.go b/tools/graft-agents/resolve/budget.goindex 8414de2..55e4185 100644--- a/tools/graft-agents/resolve/budget.go+++ b/tools/graft-agents/resolve/budget.go@@ -1,15 +1,14 @@package resolveimport (+ "bytes""context""encoding/json""errors""fmt""io/fs"- "math""os""path/filepath"- "strconv""sync""sync/atomic""time"@@ -17,8 +16,8 @@ import ("github.com/feder-cr/git-ai/tools/graft-agents/llm")-// Prices are the prices of a model in USD. OutputPerMillion applies to every output token,-// reasoning included (providers count reasoning tokens as output).+// Prices are the prices of a model in USD, as configured. OutputPerMillion applies to every+// output token, reasoning included (providers count reasoning tokens as output).type Prices struct {InputPerMillion float64OutputPerMillion float64@@ -26,20 +25,40 @@ type Prices struct {PerRequest float64}-func (p Prices) cost(inputTokens, outputTokens int) float64 {- return float64(inputTokens)*p.InputPerMillion/1e6 + float64(outputTokens)*p.OutputPerMillion/1e6 + p.PerRequest+// prices are Prices in nano-dollars, converted exactly (usdToNano).+type prices struct {+ inputPerMillion, outputPerMillion, perRequest nanoUSD}-func (p Prices) validate() error {- for _, v := range []float64{p.InputPerMillion, p.OutputPerMillion, p.PerRequest} {- if v < 0 || math.IsNaN(v) || math.IsInf(v, 0) {- return fmt.Errorf("invalid price %v", v)+func (p Prices) exact() (prices, error) {+ var out prices+ for _, f := range []struct {+ usd float64+ dst *nanoUSD+ }{{p.InputPerMillion, &out.inputPerMillion}, {p.OutputPerMillion, &out.outputPerMillion}, {p.PerRequest, &out.perRequest}} {+ n, err := usdToNano(f.usd)+ if err != nil {+ return prices{}, fmt.Errorf("invalid price: %w", err)}+ *f.dst = n}- if p.InputPerMillion == 0 && p.OutputPerMillion == 0 && p.PerRequest == 0 {- return errors.New("no price is set")+ if out == (prices{}) {+ return prices{}, errors.New("no price is set")}- return nil+ return out, nil+}++// cost returns the cost of a call with the given tokens, rounded up.+func (p prices) cost(inputTokens, outputTokens int) (nanoUSD, error) {+ in, err := tokenCost(inputTokens, p.inputPerMillion)+ if err != nil {+ return 0, err+ }+ out, err := tokenCost(outputTokens, p.outputPerMillion)+ if err != nil {+ return 0, err+ }+ return addNano(in, out, p.perRequest)}// Budget tracks the AI spend of a calendar month (UTC) and caps it (PLAN10 3.5). Before every@@ -48,17 +67,23 @@ func (p Prices) validate() error {// cost of the usage the provider reports. So the recorded spend is never below the real one, and// a process that dies between the two steps leaves its reservation booked.//+// Money is counted in whole nano-dollars: the prices and the cap are converted exactly from+// their decimal form, a call's cost is rounded up, and sums and the comparison with the cap are+// exact.+//// With a state file the spend survives restarts and is shared by every graft-resolve process// that uses the file: each update holds an exclusive lock (flock on state + ".lock"), reads the// file, and writes it again atomically (temporary file, fsync, rename, fsync of the directory).// Without a state file the spend lives in memory (the benchmark).type Budget struct {- // MonthlyUSD is the cap; 0 means no cap (the spend is still tracked).+ // MonthlyUSD is the cap as configured; 0 means no cap (the spend is still tracked).MonthlyUSD float64Prices Prices- state string- now func() time.Time+ monthly nanoUSD+ prices prices+ state string+ now func() time.Timemu sync.Mutexmem budgetState@@ -67,18 +92,46 @@ type Budget struct {exhausted atomic.Int64}+// budgetState is the content of the state file: {"month": "2026-10", "spent_usd": 1.250000000},+// the spend written as an exact decimal number.type budgetState struct {- Month string `json:"month"`- SpentUSD float64 `json:"spent_usd"`+ Month string+ Spent nanoUSD+}++type budgetStateJSON struct {+ Month string `json:"month"`+ SpentUSD json.Number `json:"spent_usd"`+}++func (st budgetState) MarshalJSON() ([]byte, error) {+ return json.Marshal(budgetStateJSON{Month: st.Month, SpentUSD: json.Number(st.Spent.String())})+}++func (st *budgetState) UnmarshalJSON(data []byte) error {+ var w budgetStateJSON+ dec := json.NewDecoder(bytes.NewReader(data))+ dec.UseNumber()+ if err := dec.Decode(&w); err != nil {+ return err+ }+ spent, err := parseNanoUSD(w.SpentUSD.String())+ if err != nil {+ return fmt.Errorf("spent_usd: %w", err)+ }+ *st = budgetState{Month: w.Month, Spent: spent}+ return nil}// NewBudget returns a budget with the cap monthlyUSD (0: none) and the prices of the model,// kept in stateFile ("" keeps it in memory).-func NewBudget(monthlyUSD float64, prices Prices, stateFile string) (*Budget, error) {- if monthlyUSD < 0 || math.IsNaN(monthlyUSD) || math.IsInf(monthlyUSD, 0) {- return nil, fmt.Errorf("budget: invalid monthly cap %v", monthlyUSD)+func NewBudget(monthlyUSD float64, p Prices, stateFile string) (*Budget, error) {+ monthly, err := usdToNano(monthlyUSD)+ if err != nil {+ return nil, fmt.Errorf("budget: invalid monthly cap: %w", err)}- if err := prices.validate(); err != nil {+ exact, err := p.exact()+ if err != nil {return nil, fmt.Errorf("budget: %w", err)}if stateFile != "" {@@ -89,7 +142,7 @@ func NewBudget(monthlyUSD float64, prices Prices, stateFile string) (*Budget, erreturn nil, err}}- return &Budget{MonthlyUSD: monthlyUSD, Prices: prices, state: stateFile, now: time.Now}, nil+ return &Budget{MonthlyUSD: monthlyUSD, Prices: p, monthly: monthly, prices: exact, state: stateFile, now: time.Now}, nil}func (b *Budget) month() string {@@ -145,9 +198,6 @@ func readBudgetState(file string) (budgetState, error) {if err := json.Unmarshal(data, &st); err != nil {return st, fmt.Errorf("budget: state file %s: %w", file, err)}- if st.SpentUSD < 0 || math.IsNaN(st.SpentUSD) || math.IsInf(st.SpentUSD, 0) {- return st, fmt.Errorf("budget: state file %s: invalid spend %v", file, st.SpentUSD)- }return st, nil}@@ -191,32 +241,37 @@ func writeBudgetState(file string, st budgetState) error {type Reservation struct {b *Budgetmonth string- usd float64+ worst nanoUSD}// Reserve books the worst case of a call with at most inputTokens of input and maxOutputTokens of// output. It fails with ErrBudgetExhausted, without booking anything, when the month's spend plus// the worst case would exceed the cap.func (b *Budget) Reserve(inputTokens, maxOutputTokens int) (*Reservation, error) {- worst := b.Prices.cost(inputTokens, maxOutputTokens)+ worst, err := b.prices.cost(inputTokens, maxOutputTokens)+ if err != nil {+ return nil, fmt.Errorf("budget: %w", err)+ }st, err := b.update(func(st *budgetState) error {- // The margin absorbs the rounding of sums of float prices (49 calls of $0.01 add up- // to a little more than $0.49); it lets the spend pass the cap by a nano-dollar at most.- if b.MonthlyUSD > 0 && st.SpentUSD+worst > b.MonthlyUSD+1e-9 {+ total, err := addNano(st.Spent, worst)+ if err != nil {+ return err+ }+ if b.monthly > 0 && total > b.monthly {return ErrBudgetExhausted}- st.SpentUSD += worst+ st.Spent = totalreturn nil})if errors.Is(err, ErrBudgetExhausted) {b.exhausted.Add(1)- return nil, fmt.Errorf("%w: $%.4f spent of $%.2f this month, the next call may cost up to $%.4f", ErrBudgetExhausted, st.SpentUSD, b.MonthlyUSD, worst)+ return nil, fmt.Errorf("%w: $%s spent of $%s this month, the next call may cost up to $%s", ErrBudgetExhausted, st.Spent, b.monthly, worst)}if err != nil {- return nil, err+ return nil, fmt.Errorf("budget: %w", err)}b.calls.Add(1)- return &Reservation{b: b, month: st.Month, usd: worst}, nil+ return &Reservation{b: b, month: st.Month, worst: worst}, nil}// Book replaces the reservation with the cost of the usage the provider reported. A usage with no@@ -226,12 +281,21 @@ func (r *Reservation) Book(u llm.Usage) error {if r == nil || (u.InputTokens == 0 && u.OutputTokens == 0) {return nil}- actual := r.b.Prices.cost(u.InputTokens, u.OutputTokens)- _, err := r.b.update(func(st *budgetState) error {+ actual, err := r.b.prices.cost(u.InputTokens, u.OutputTokens)+ if err != nil {+ return fmt.Errorf("budget: %w", err)+ }+ _, err = r.b.update(func(st *budgetState) error {if st.Month != r.month {return nil}- st.SpentUSD = math.Max(0, st.SpentUSD+actual-r.usd)+ // The worst case was booked; the reported usage replaces it. A usage above the worst case+ // (a provider that passed max_tokens) is booked in full.+ spent, err := addNano(st.Spent-min(st.Spent, r.worst), actual)+ if err != nil {+ return err+ }+ st.Spent = spentreturn nil})return err@@ -239,11 +303,13 @@ func (r *Reservation) Book(u llm.Usage) error {// BudgetSnapshot is the state of a budget, for metrics and reports.type BudgetSnapshot struct {- Month string- SpentUSD float64- MonthlyUSD float64- Calls int64- Exhausted int64+ Month string+ // SpentUSD and MonthlyUSD are for display; SpentNanoUSD is exact.+ SpentUSD float64+ SpentNanoUSD int64+ MonthlyUSD float64+ Calls int64+ Exhausted int64}// Snapshot returns the spend of the current month.@@ -252,7 +318,10 @@ func (b *Budget) Snapshot() (BudgetSnapshot, error) {if err != nil && !errors.Is(err, errReadOnly) {return BudgetSnapshot{}, err}- return BudgetSnapshot{Month: st.Month, SpentUSD: st.SpentUSD, MonthlyUSD: b.MonthlyUSD, Calls: b.calls.Load(), Exhausted: b.exhausted.Load()}, nil+ return BudgetSnapshot{+ Month: st.Month, SpentUSD: st.Spent.USD(), SpentNanoUSD: int64(st.Spent), MonthlyUSD: b.monthly.USD(),+ Calls: b.calls.Load(), Exhausted: b.exhausted.Load(),+ }, nil}var errReadOnly = errors.New("read only")@@ -261,7 +330,7 @@ var errReadOnly = errors.New("read only")func (s BudgetSnapshot) String() string {limit := "no cap"if s.MonthlyUSD > 0 {- limit = "cap $" + strconv.FormatFloat(s.MonthlyUSD, 'f', 2, 64)+ limit = fmt.Sprintf("cap $%.2f", s.MonthlyUSD)}return fmt.Sprintf("AI spend %s: $%.4f (%s)", s.Month, s.SpentUSD, limit)}diff --git a/tools/graft-agents/resolve/budget_money.go b/tools/graft-agents/resolve/budget_money.gonew file mode 100644index 0000000..9732da2--- /dev/null+++ b/tools/graft-agents/resolve/budget_money.go@@ -0,0 +1,100 @@+package resolve++import (+ "errors"+ "fmt"+ "math"+ "math/big"+ "math/bits"+ "strconv"+ "strings"+)++// nanoUSD is an amount of money in billionths of a US dollar. The budget counts in integers, so+// that sums are exact and a comparison with the cap needs no tolerance: a float64 sum of 49 calls+// of $0.01 is a little more than $0.49, and 50 such calls would pass a $0.50 cap.+type nanoUSD int64++const nanoPerUSD = 1_000_000_000++// maxNanoUSD bounds every amount (about $9.2 billion), far above any cap or call.+const maxNanoUSD = nanoUSD(math.MaxInt64)++// usdToNano converts a configured amount in USD exactly, from the shortest decimal that+// represents the float64 (the number as written in the configuration), rounding a fraction of a+// nano-dollar up.+func usdToNano(usd float64) (nanoUSD, error) {+ if usd < 0 || math.IsNaN(usd) || math.IsInf(usd, 0) {+ return 0, fmt.Errorf("invalid amount %v", usd)+ }+ r, ok := new(big.Rat).SetString(strconv.FormatFloat(usd, 'g', -1, 64))+ if !ok {+ return 0, fmt.Errorf("invalid amount %v", usd)+ }+ r.Mul(r, big.NewRat(nanoPerUSD, 1))+ n := new(big.Int).Quo(r.Num(), r.Denom())+ if new(big.Rat).SetInt(n).Cmp(r) < 0 {+ n.Add(n, big.NewInt(1))+ }+ if !n.IsInt64() {+ return 0, fmt.Errorf("amount %v is too large", usd)+ }+ return nanoUSD(n.Int64()), nil+}++// USD returns the amount in dollars, for display.+func (n nanoUSD) USD() float64 { return float64(n) / nanoPerUSD }++// String writes the amount as an exact decimal number of dollars, "1.250000000".+func (n nanoUSD) String() string {+ sign := ""+ if n < 0 {+ sign, n = "-", -n+ }+ return fmt.Sprintf("%s%d.%09d", sign, n/nanoPerUSD, n%nanoPerUSD)+}++// parseNanoUSD reads an exact decimal number of dollars, as String writes it.+func parseNanoUSD(s string) (nanoUSD, error) {+ r, ok := new(big.Rat).SetString(strings.TrimSpace(s))+ if !ok || r.Sign() < 0 {+ return 0, fmt.Errorf("invalid amount %q", s)+ }+ r.Mul(r, big.NewRat(nanoPerUSD, 1))+ if !r.IsInt() || !r.Num().IsInt64() {+ return 0, fmt.Errorf("amount %q is not a whole number of nano-dollars", s)+ }+ return nanoUSD(r.Num().Int64()), nil+}++// tokenCost returns tokens × perMillion / 1e6, rounded up, without overflow: the cost of tokens+// at a price per million tokens.+func tokenCost(tokens int, perMillion nanoUSD) (nanoUSD, error) {+ if tokens < 0 {+ return 0, errors.New("negative token count")+ }+ hi, lo := bits.Mul64(uint64(tokens), uint64(perMillion))+ if hi >= 1_000_000 {+ return 0, errors.New("cost overflows")+ }+ q, rem := bits.Div64(hi, lo, 1_000_000)+ if rem != 0 {+ q+++ }+ if q > uint64(maxNanoUSD) {+ return 0, errors.New("cost overflows")+ }+ return nanoUSD(q), nil+}++// addNano adds amounts, failing on overflow.+func addNano(a ...nanoUSD) (nanoUSD, error) {+ var sum nanoUSD+ for _, v := range a {+ if v > maxNanoUSD-sum {+ return 0, errors.New("amount overflows")+ }+ sum += v+ }+ return sum, nil+}diff --git a/tools/graft-agents/resolve/budget_money_test.go b/tools/graft-agents/resolve/budget_money_test.gonew file mode 100644index 0000000..df543df--- /dev/null+++ b/tools/graft-agents/resolve/budget_money_test.go@@ -0,0 +1,97 @@+package resolve++import (+ "encoding/json"+ "math"+ "testing"+)++func TestUSDToNanoIsExact(t *testing.T) {+ for usd, want := range map[float64]nanoUSD{+ 0: 0,+ 0.2: 200_000_000, // 0.2 is not exact in binary; its shortest decimal is+ 0.30000000000000004: 300_000_001, // the float sum 0.1+0.2: the fraction of a nano-dollar rounds up+ 10: 10_000_000_000,+ 0.0000000001: 1, // a tenth of a nano-dollar rounds up to one+ 1e-9: 1,+ } {+ got, err := usdToNano(usd)+ if err != nil || got != want {+ t.Errorf("usdToNano(%v) = %d, %v; want %d", usd, got, err, want)+ }+ }+ for _, bad := range []float64{-1, math.NaN(), math.Inf(1), 1e12} {+ if _, err := usdToNano(bad); err == nil {+ t.Errorf("usdToNano(%v) accepted", bad)+ }+ }+}++func TestTokenCostRoundsUpWithoutOverflow(t *testing.T) {+ for _, tc := range []struct {+ tokens int+ perMillion nanoUSD+ want nanoUSD+ }{+ {1_000_000, 200_000_000, 200_000_000},+ {1, 200_000_000, 200},+ {3, 1, 1}, // 3e-6 of a nano-dollar is booked as one+ {0, 200_000_000, 0},+ {1 << 30, 1_000_000_000_000, 1_073_741_824_000_000}, // a billion tokens at $1000 per million+ } {+ got, err := tokenCost(tc.tokens, tc.perMillion)+ if err != nil || got != tc.want {+ t.Errorf("tokenCost(%d, %d) = %d, %v; want %d", tc.tokens, tc.perMillion, got, err, tc.want)+ }+ }+ if _, err := tokenCost(math.MaxInt64, maxNanoUSD); err == nil {+ t.Error("overflow not detected")+ }+ if _, err := tokenCost(-1, 1); err == nil {+ t.Error("negative tokens accepted")+ }+ if _, err := addNano(maxNanoUSD, 1); err == nil {+ t.Error("sum overflow not detected")+ }+}++func TestBudgetStateJSONIsExact(t *testing.T) {+ st := budgetState{Month: "2026-10", Spent: 1_250_000_001}+ data, err := json.Marshal(st)+ if err != nil {+ t.Fatal(err)+ }+ if string(data) != `{"month":"2026-10","spent_usd":1.250000001}` {+ t.Fatalf("state %s", data)+ }+ var back budgetState+ if err := json.Unmarshal(data, &back); err != nil || back != st {+ t.Fatalf("round trip: %+v, %v", back, err)+ }+ // A hand-written state file with a short decimal is read exactly.+ if err := json.Unmarshal([]byte(`{"month":"2026-10","spent_usd":4.9999}`), &back); err != nil || back.Spent != 4_999_900_000 {+ t.Fatalf("short decimal: %+v, %v", back, err)+ }+ for _, bad := range []string{`{"month":"2026-10","spent_usd":-1}`, `{"month":"2026-10","spent_usd":0.0000000001}`, `{"month":"2026-10","spent_usd":"x"}`} {+ if err := json.Unmarshal([]byte(bad), &back); err == nil {+ t.Errorf("%s accepted", bad)+ }+ }+}++// TestBudgetCapIsExactAtTheBoundary is the case that float sums got wrong: fifty calls of $0.01+// fit a $0.50 cap exactly, and the fifty-first does not.+func TestBudgetCapIsExactAtTheBoundary(t *testing.T) {+ b, err := NewBudget(0.5, Prices{InputPerMillion: 1}, "")+ if err != nil {+ t.Fatal(err)+ }+ for i := 0; i < 50; i++ {+ if _, err := b.Reserve(10_000, 0); err != nil { // $0.01+ t.Fatalf("call %d refused: %v", i+1, err)+ }+ }+ if _, err := b.Reserve(1, 0); err == nil {+ t.Fatal("a call past the cap was allowed")+ }+}diff --git a/tools/graft-agents/resolve/budget_test.go b/tools/graft-agents/resolve/budget_test.goindex c4ac6eb..f833c01 100644--- a/tools/graft-agents/resolve/budget_test.go+++ b/tools/graft-agents/resolve/budget_test.go@@ -6,7 +6,7 @@ import ("context""encoding/json""errors"- "math"+"os""path/filepath""strings"@@ -20,15 +20,24 @@ import (// wp4Prices costs $1 per million input tokens and $10 per million output tokens.var wp4Prices = Prices{InputPerMillion: 1, OutputPerMillion: 10}-func wp4Near(a, b float64) bool { return math.Abs(a-b) < 1e-9 }+// wp4USD returns an exact amount written in dollars.+func wp4USD(t *testing.T, usd string) nanoUSD {+ t.Helper()+ n, err := parseNanoUSD(usd)+ if err != nil {+ t.Fatal(err)+ }+ return n+}-func wp4Spent(t *testing.T, b *Budget) float64 {+// wp4Spent returns the exact spend of the month.+func wp4Spent(t *testing.T, b *Budget) nanoUSD {t.Helper()s, err := b.Snapshot()if err != nil {t.Fatal(err)}- return s.SpentUSD+ return nanoUSD(s.SpentNanoUSD)}func TestBudgetReserveAndBook(t *testing.T) {@@ -41,13 +50,13 @@ func TestBudgetReserveAndBook(t *testing.T) {if err != nil {t.Fatal(err)}- if got := wp4Spent(t, b); !wp4Near(got, 0.2) {+ if got := wp4Spent(t, b); got != wp4USD(t, "0.2") {t.Fatalf("spent after reserve = %v, want the worst case 0.2", got)}if err := r.Book(llm.Usage{InputTokens: 30_000, OutputTokens: 2_000}); err != nil {t.Fatal(err)}- if got := wp4Spent(t, b); !wp4Near(got, 0.05) {+ if got := wp4Spent(t, b); got != wp4USD(t, "0.05") {t.Fatalf("spent after book = %v, want the real cost 0.05", got)}}@@ -65,7 +74,7 @@ func TestBudgetRefusesBeyondCap(t *testing.T) {t.Fatalf("message %q", err)}s, _ := b.Snapshot()- if !wp4Near(s.SpentUSD, 0.2) || s.Exhausted != 1 || s.Calls != 1 {+ if nanoUSD(s.SpentNanoUSD) != wp4USD(t, "0.2") || s.Exhausted != 1 || s.Calls != 1 {t.Fatalf("snapshot %+v: a refused call must book nothing", s)}// A smaller call still fits.@@ -80,7 +89,7 @@ func TestBudgetUnknownUsageKeepsReservation(t *testing.T) {if err := r.Book(llm.Usage{}); err != nil {t.Fatal(err)}- if got := wp4Spent(t, b); !wp4Near(got, 0.2) {+ if got := wp4Spent(t, b); got != wp4USD(t, "0.2") {t.Fatalf("spent = %v, want the reservation kept", got)}}@@ -92,7 +101,7 @@ func TestBudgetNoCapTracksSpend(t *testing.T) {t.Fatalf("call %d refused without a cap: %v", i, err)}}- if got := wp4Spent(t, b); !wp4Near(got, 100) {+ if got := wp4Spent(t, b); got != wp4USD(t, "100") {t.Fatalf("spent = %v", got)}}@@ -108,7 +117,7 @@ func TestBudgetStateFileSurvivesRestartAndIsShared(t *testing.T) {// Another process (or a restart) sees the spend.b, _ := NewBudget(1, wp4Prices, state)- if got := wp4Spent(t, b); !wp4Near(got, 0.1) {+ if got := wp4Spent(t, b); got != wp4USD(t, "0.1") {t.Fatalf("restarted budget spent = %v, want 0.1", got)}if _, err := b.Reserve(800_000, 0); err != nil { // 0.90 total@@ -123,7 +132,7 @@ func TestBudgetStateFileSurvivesRestartAndIsShared(t *testing.T) {if err != nil {t.Fatal(err)}- if err := json.Unmarshal(data, &st); err != nil || st.Month != time.Now().UTC().Format("2006-01") || !wp4Near(st.SpentUSD, 0.9) {+ if err := json.Unmarshal(data, &st); err != nil || st.Month != time.Now().UTC().Format("2006-01") || st.Spent != wp4USD(t, "0.9") {t.Fatalf("state %s (%v)", data, err)}if matches, _ := filepath.Glob(state + ".tmp-*"); len(matches) != 0 {@@ -159,7 +168,7 @@ func TestBudgetConcurrentReservationsNeverExceedCap(t *testing.T) {if granted != 50 {t.Fatalf("granted %d calls of $0.01 under a $0.50 cap, want 50", granted)}- if got := wp4Spent(t, budgets[0]); !wp4Near(got, 0.5) {+ if got := wp4Spent(t, budgets[0]); got != wp4USD(t, "0.5") {t.Fatalf("spent = %v", got)}}@@ -230,7 +239,7 @@ func TestBudgetProviderReservesInputBytesAndMaxTokens(t *testing.T) {t.Fatal(err)}// No usage reported: the reservation stays, 1 + 1000 bytes + 256 of framing.- if got := wp4Spent(t, b); got != 1257 {+ if got := wp4Spent(t, b); got != wp4USD(t, "1257") {t.Fatalf("reserved %v input tokens, want 1257", got)}}@@ -279,7 +288,7 @@ func TestLLMStrategyBudgetConfig(t *testing.T) {t.Fatal(err)}// 1000 × $2/M + 100 × $10.5/M = $0.00305.- if got := wp4Spent(t, b); !wp4Near(got, 0.00305) {+ if got := wp4Spent(t, b); got != wp4USD(t, "0.00305") {t.Fatalf("spent = %v", got)}if !strings.Contains(resp.Explanation, "llm: AI spend ") || !strings.Contains(resp.Explanation, "cap $5.00") {@@ -361,7 +370,7 @@ func TestResolverUseBudget(t *testing.T) {if _, err := r.Resolve(context.Background(), wp19Request(wp19Conflict("a.txt"))); err != nil {t.Fatal(err)}- if got := wp4Spent(t, b); !wp4Near(got, 1) {+ if got := wp4Spent(t, b); got != wp4USD(t, "1") {t.Fatalf("spent = %v, want $1", got)}