| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "math" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | ) |
| 10 | |
| 11 | func usageEvent(turn, usage string) provider.RecordedEvent { |
| 12 | return provider.RecordedEvent{Kind: "turn_completed", ServerID: "server", AgentID: "agent", ProviderSessionID: "session", TurnID: turn, Payload: json.RawMessage(`{"event":{"type":"turn_completed","provider":"claude","turnId":"` + turn + `","usage":` + usage + `}}`)} |
| 13 | } |
| 14 | func TestUsageCumulativeAndMissing(t *testing.T) { |
| 15 | a := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "exclusive", CostSemantics: "cumulative", Subscription: true}) |
| 16 | first := usageEvent("one", `{"inputTokens":18,"cachedInputTokens":29591,"outputTokens":404,"totalCostUsd":0.0673131}`) |
| 17 | u := a.ObservePaseo(first) |
| 18 | if u == nil || u.InputTokens != 29609 || u.CachedInputTokens != 29591 || u.CostMicros != 0 || !u.CostUnavailable { |
| 19 | t.Fatal("normalization", u) |
| 20 | } |
| 21 | next := usageEvent("two", `{"inputTokens":2,"cachedInputTokens":3,"outputTokens":4,"totalCostUsd":0.0758928}`) |
| 22 | if a.ObservePaseo(next) == nil { |
| 23 | t.Fatal("second completion unknown") |
| 24 | } |
| 25 | if math.Abs(a.EstimatedCostUSD()-0.0758928) > 1e-10 { |
| 26 | t.Fatal("cumulative cost summed", a.EstimatedCostUSD()) |
| 27 | } |
| 28 | if a.ObservePaseo(usageEvent("missing", `null`)) != nil { |
| 29 | t.Fatal("missing usage became zero") |
| 30 | } |
| 31 | for _, kind := range []string{"agent_update", "queued", "idle", "timeline"} { |
| 32 | e := first |
| 33 | e.Kind = kind |
| 34 | e.Payload = json.RawMessage(`{"agent":{"lastUsage":{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}}}`) |
| 35 | if a.ObservePaseo(e) != nil { |
| 36 | t.Fatal("status proved completion", kind) |
| 37 | } |
| 38 | } |
| 39 | if got := a.ObservePaseo(first); got == nil || *got != *u { |
| 40 | t.Fatal("lastUsage erased completion", got) |
| 41 | } |
| 42 | unknown := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "unverified", InputSemantics: "exclusive", Subscription: true}) |
| 43 | if unknown.ObservePaseo(first) != nil { |
| 44 | t.Fatal("unverified cumulative semantics settled") |
| 45 | } |
| 46 | if got := a.ObservePaseo(usageEvent("zero", `{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}`)); got == nil || got.InputTokens != 0 { |
| 47 | t.Fatal("known zero lost") |
| 48 | } |
| 49 | for _, raw := range []string{`{"inputTokens":1,"outputTokens":2}`, `{"inputTokens":9223372036854775807,"cachedInputTokens":1,"outputTokens":1}`, `{"inputTokens":-1,"cachedInputTokens":0,"outputTokens":1}`} { |
| 50 | if a.ObservePaseo(usageEvent("invalid", raw)) != nil { |
| 51 | t.Fatal("partial or invalid usage settled") |
| 52 | } |
| 53 | } |
| 54 | inclusive := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "inclusive", Subscription: true}) |
| 55 | if v := inclusive.ObservePaseo(first); v != nil { |
| 56 | t.Fatal("cache exceeds inclusive input") |
| 57 | } |
| 58 | got := inclusive.ObservePaseo(usageEvent("inclusive", `{"inputTokens":20,"cachedInputTokens":5,"outputTokens":2}`)) |
| 59 | if got == nil || got.InputTokens != 20 || got.CachedInputTokens != 5 { |
| 60 | t.Fatal("inclusive cache counted twice", got) |
| 61 | } |
| 62 | tokens, err := NormalizeTokens(ProviderTokens{Input: 10, Cached: 3, CacheWrite: 4, Output: 2, InputKnown: true, CachedKnown: true, CacheWriteKnown: true, OutputKnown: true}, "exclusive", false) |
| 63 | mustJournal(t, err) |
| 64 | if tokens.UncachedInput != 14 || tokens.CachedInput != 3 { |
| 65 | t.Fatal("cache write normalization", tokens) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestUsageCumulativeCompletionGuards(t *testing.T) { |
| 70 | p := UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "exclusive", CostSemantics: "cumulative", Subscription: true} |
| 71 | e := usageEvent("turn", `{"inputTokens":5,"cachedInputTokens":3,"outputTokens":2,"totalCostUsd":0.0673131}`) |
| 72 | for _, kind := range []string{"idle", "queued", "timeline", "agent_update"} { |
| 73 | bad := e |
| 74 | bad.Kind = kind |
| 75 | if NewUsageAccumulator(p).ObservePaseo(bad) != nil { |
| 76 | t.Fatal("noncompletion kind settled", kind) |
| 77 | } |
| 78 | } |
| 79 | for _, field := range []string{"version", "paseo", "provider", "turn", "session", "input_mode"} { |
| 80 | bad := e |
| 81 | profile := p |
| 82 | switch field { |
| 83 | case "version": |
| 84 | profile.Version = 2 |
| 85 | case "paseo": |
| 86 | profile.PaseoVersion = "unknown" |
| 87 | case "provider": |
| 88 | profile.Provider = "codex" |
| 89 | case "turn": |
| 90 | bad.TurnID = "other" |
| 91 | case "session": |
| 92 | bad.ProviderSessionID = "" |
| 93 | case "input_mode": |
| 94 | profile.InputSemantics = "unknown" |
| 95 | } |
| 96 | if NewUsageAccumulator(profile).ObservePaseo(bad) != nil { |
| 97 | t.Fatal("unverified usage settled", field) |
| 98 | } |
| 99 | } |
| 100 | a := NewUsageAccumulator(p) |
| 101 | u := a.ObservePaseo(e) |
| 102 | if u == nil { |
| 103 | t.Fatal("completion missing") |
| 104 | } |
| 105 | zero := usageEvent("turn", `{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}`) |
| 106 | if got := a.ObservePaseo(zero); got == nil || *got != *u { |
| 107 | t.Fatal("zero erased completed evidence") |
| 108 | } |
| 109 | p.Subscription = false |
| 110 | if NewUsageAccumulator(p).ObservePaseo(e) != nil { |
| 111 | t.Fatal("unverified cumulative currency settled") |
| 112 | } |
| 113 | p.CostSemantics = "turn" |
| 114 | if got := NewUsageAccumulator(p).ObservePaseo(e); got == nil || got.CostMicros != 67314 || got.CostUnavailable { |
| 115 | t.Fatal("metered per-turn normalization", got) |
| 116 | } |
| 117 | partial, err := NormalizeTokens(ProviderTokens{Input: 5, InputKnown: true, OutputKnown: true}, "inclusive", true) |
| 118 | mustJournal(t, err) |
| 119 | if partial.UncachedKnown || partial.CachedKnown || !partial.OutputKnown { |
| 120 | t.Fatal("missing cache treated as zero", partial) |
| 121 | } |
| 122 | _, err = NormalizeTokens(ProviderTokens{Input: math.MaxInt64, InputKnown: true, CacheWrite: 1, CacheWriteKnown: true}, "exclusive", false) |
| 123 | if err == nil { |
| 124 | t.Fatal("cache-write overflow accepted") |
| 125 | } |
| 126 | } |
| 127 | |