| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | |
| 6 | "reasonix/internal/event" |
| 7 | "reasonix/internal/provider" |
| 8 | "reasonix/internal/sessioninbox" |
| 9 | ) |
| 10 | |
| 11 | // goalUsageTee wraps the controller's event sink and attributes billable usage |
| 12 | // events to the admitted automatic goal round. Title generation and unrelated |
| 13 | // background calls are excluded. The tee forwards every event unchanged. |
| 14 | type goalUsageTee struct { |
| 15 | event.AuditForwarder |
| 16 | inner event.Sink |
| 17 | mu sync.Mutex |
| 18 | lifecycleUsage func(event.Event) |
| 19 | } |
| 20 | |
| 21 | // NewGoalUsageTee wraps inner in a usage-accounting tee. Pass the returned sink |
| 22 | // to both the agent/executor and the Controller (control.New detects it and |
| 23 | // attaches the goal machine). |
| 24 | func NewGoalUsageTee(inner event.Sink) event.Sink { |
| 25 | if inner == nil { |
| 26 | inner = event.Discard |
| 27 | } |
| 28 | return &goalUsageTee{AuditForwarder: event.AuditForwarder{Inner: inner}, inner: inner} |
| 29 | } |
| 30 | |
| 31 | // Emit forwards the event and, for billable usage while a goal turn is active, |
| 32 | // folds the tokens into the turn recorder. |
| 33 | func (t *goalUsageTee) Emit(e event.Event) { |
| 34 | if t == nil { |
| 35 | return |
| 36 | } |
| 37 | t.recordUsage(e) |
| 38 | if t.inner != nil { |
| 39 | t.inner.Emit(e) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // EmitChecked preserves durability-aware sink behavior through the usage tee. |
| 44 | // Prompt and dispatch commits must still fail closed when the inner ledger |
| 45 | // rejects an event. |
| 46 | func (t *goalUsageTee) EmitChecked(e event.Event) error { |
| 47 | if t == nil { |
| 48 | return nil |
| 49 | } |
| 50 | if err := event.EmitChecked(t.inner, e); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | t.recordUsage(e) |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func (t *goalUsageTee) recordUsage(e event.Event) { |
| 58 | if e.Kind == event.Usage && e.Usage != nil && e.UsageSource != event.UsageSourceTitle { |
| 59 | t.mu.Lock() |
| 60 | observe := t.lifecycleUsage |
| 61 | t.mu.Unlock() |
| 62 | if observe != nil { |
| 63 | observe(e) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func (t *goalUsageTee) setLifecycleUsageRecorder(record func(event.Event)) { |
| 69 | if t == nil { |
| 70 | return |
| 71 | } |
| 72 | t.mu.Lock() |
| 73 | t.lifecycleUsage = record |
| 74 | t.mu.Unlock() |
| 75 | } |
| 76 | |
| 77 | func (t *goalUsageTee) InboxChanged(snap sessioninbox.InboxSnapshot) { |
| 78 | if t == nil { |
| 79 | return |
| 80 | } |
| 81 | notifyInboxChanged(t.inner, snap) |
| 82 | } |
| 83 | |
| 84 | // usageTotalTokens prefers TotalTokens and falls back to the non-overlapping |
| 85 | // prompt + completion sum, so cache hit/miss tokens are never double-counted. |
| 86 | func usageTotalTokens(u *provider.Usage) int { |
| 87 | if u == nil { |
| 88 | return 0 |
| 89 | } |
| 90 | if u.TotalTokens > 0 { |
| 91 | return u.TotalTokens |
| 92 | } |
| 93 | return u.PromptTokens + u.CompletionTokens |
| 94 | } |
| 95 |