oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "encoding/json"
5 "math"
6 "strings"
7
8 "amadan.net/oficina/equipo/internal/equipo/team"
9 "amadan.net/oficina/equipo/internal/runner/provider"
10 "amadan.net/oficina/equipo/internal/runnerproto"
11)
12
13type directCounters struct {
14 Input, Output, Cached *int64
15 Bad bool
16}
17type directUsageState struct {
18 Baseline map[string]directCounters
19 Pending map[string]directCounters
20 Settled map[string]*team.Usage
21 Cost map[string]float64
22 CostUnknown map[string]bool
23 Estimated float64
24}
25
26func (a *UsageAccumulator) initDirect() {
27 if a.direct.Baseline == nil {
28 a.direct = directUsageState{Baseline: map[string]directCounters{}, Pending: map[string]directCounters{}, Settled: map[string]*team.Usage{}, Cost: map[string]float64{}, CostUnknown: map[string]bool{}}
29 }
30}
31func usageKeys(p UsageProfile, e provider.Event) (string, string) {
32 s := runnerproto.EventHash([]string{p.Provider, p.ProviderVersion, e.SessionID})
33 return s, runnerproto.EventHash([]string{s, e.TurnID})
34}
35func validCounter(v *int64) bool { return v != nil && *v >= 0 }
36func regress(v, old *int64) bool { return v != nil && old != nil && *v < *old }
37func (a *UsageAccumulator) Observe(e provider.Event) *team.Usage {
38 a.mu.Lock()
39 defer a.mu.Unlock()
40 a.initDirect()
41 p := a.profile
42 if p.Version != 2 || p.ProviderVersion == "" || e.SessionID == "" || e.TurnID == "" {
43 return nil
44 }
45 stream, key := usageKeys(p, e)
46 if u, ok := a.direct.Settled[key]; ok {
47 if u == nil {
48 return nil
49 }
50 copy := *u
51 return &copy
52 }
53 var u *team.Usage
54 switch p.Provider {
55 case "codex":
56 base := a.direct.Baseline[stream]
57 pending := a.direct.Pending[key]
58 var item struct {
59 Params struct{ Item struct{ Type string } }
60 }
61 _ = json.Unmarshal(e.Payload, &item)
62 if strings.Contains(strings.ToLower(e.Kind), "compact") || strings.Contains(strings.ToLower(item.Params.Item.Type), "compact") {
63 base.Bad = true
64 a.direct.Baseline[stream] = base
65 return nil
66 }
67 if e.Kind == "usage" {
68 var v struct {
69 Params struct {
70 TokenUsage struct {
71 Total struct {
72 Input *int64 `json:"inputTokens"`
73 Output *int64 `json:"outputTokens"`
74 Cached *int64 `json:"cachedInputTokens"`
75 } `json:"total"`
76 } `json:"tokenUsage"`
77 } `json:"params"`
78 }
79 err := json.Unmarshal(e.Payload, &v)
80 x := v.Params.TokenUsage.Total
81 bad := pending.Bad || base.Bad || err != nil || !validCounter(x.Input) || !validCounter(x.Output) || (x.Cached != nil && (!validCounter(x.Cached) || (x.Input != nil && *x.Cached > *x.Input))) || regress(x.Input, base.Input) || regress(x.Output, base.Output) || regress(x.Cached, base.Cached) || regress(x.Input, pending.Input) || regress(x.Output, pending.Output) || regress(x.Cached, pending.Cached)
82 a.direct.Pending[key] = directCounters{Input: x.Input, Output: x.Output, Cached: x.Cached, Bad: bad}
83 return nil
84 }
85 if e.Kind != "turn.completed" && e.Kind != "turn.failed" {
86 return nil
87 }
88 if !base.Bad && !pending.Bad && validCounter(pending.Input) && validCounter(pending.Output) && e.Kind == "turn.completed" {
89 u = &team.Usage{Calls: 1, InputTokens: *pending.Input, OutputTokens: *pending.Output, CostUnavailable: true}
90 if base.Input != nil {
91 u.InputTokens -= *base.Input
92 }
93 if base.Output != nil {
94 u.OutputTokens -= *base.Output
95 }
96 if pending.Cached != nil {
97 u.CachedInputTokens = *pending.Cached
98 if base.Cached != nil {
99 u.CachedInputTokens -= *base.Cached
100 }
101 }
102 // Cache detail may be absent in the pinned probe. Aggregate tokens are
103 // still known; the checkpoint retains the absent cache counter as nil.
104 } else {
105 pending.Bad = true
106 }
107 a.direct.Baseline[stream] = pending
108 case "claude":
109 if e.Kind != "turn.completed" && e.Kind != "turn.failed" {
110 return nil
111 }
112 var v struct {
113 Usage struct {
114 Input *int64 `json:"input_tokens"`
115 Read *int64 `json:"cache_read_input_tokens"`
116 Write *int64 `json:"cache_creation_input_tokens"`
117 Output *int64 `json:"output_tokens"`
118 } `json:"usage"`
119 Cost *float64 `json:"total_cost_usd"`
120 }
121 if json.Unmarshal(e.Payload, &v) == nil {
122 x := v.Usage
123 if e.Kind == "turn.completed" && validCounter(x.Input) && validCounter(x.Read) && validCounter(x.Write) && validCounter(x.Output) && *x.Input <= math.MaxInt64-*x.Read && *x.Input+*x.Read <= math.MaxInt64-*x.Write {
124 u = &team.Usage{Calls: 1, InputTokens: *x.Input + *x.Read + *x.Write, CachedInputTokens: *x.Read, OutputTokens: *x.Output, CostUnavailable: true}
125 }
126 if v.Cost != nil && *v.Cost >= 0 && !math.IsInf(*v.Cost, 0) && !math.IsNaN(*v.Cost) {
127 old := a.direct.Cost[stream]
128 if *v.Cost < old {
129 a.direct.CostUnknown[stream] = true
130 }
131 if !a.direct.CostUnknown[stream] {
132 a.direct.Estimated += *v.Cost - old
133 a.direct.Cost[stream] = *v.Cost
134 }
135 } else {
136 a.direct.CostUnknown[stream] = true
137 }
138 }
139 default:
140 return nil
141 }
142 a.direct.Settled[key] = u
143 if u == nil {
144 return nil
145 }
146 copy := *u
147 return &copy
148}
149