feat(protocol): distinguish reported chat metrics from unavailable values

This commit is contained in:
2026-10-21 00:01:17 +01:00
parent 8899dd150f
commit 3e62ed7979
2 changed files with 37 additions and 4 deletions
+15 -4
View File
@@ -461,11 +461,22 @@ type ChatStep struct {
Intermediate bool `json:"intermediate,omitempty"`
}
// ChatUsage is the token and cost total of a turn.
// ChatUsage carries native usage snapshots. Rich metrics are pointers so zero
// reported by a harness remains distinguishable from an unavailable value.
// Session snapshots replace previous values; consumers must not sum updates.
type ChatUsage struct {
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
Cost float64 `json:"cost,omitempty"`
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
Cost float64 `json:"cost,omitempty"`
Scope string `json:"scope,omitempty"`
TotalTokens *int64 `json:"total_tokens,omitempty"`
ContextTokens *int64 `json:"context_tokens,omitempty"`
ContextWindow *int64 `json:"context_window,omitempty"`
CostUSD *float64 `json:"cost_usd,omitempty"`
OutputTokensPerSecond *float64 `json:"output_tokens_per_second,omitempty"`
// RateBasis distinguishes a generation interval from a whole turn (which
// can include tools/waits). Never estimate tokens from text/character counts.
RateBasis string `json:"rate_basis,omitempty"`
}
// ChatShell is the state of one shell command the agent started, carried by a
+22
View File
@@ -0,0 +1,22 @@
package protocol
import (
"encoding/json"
"strings"
"testing"
)
func TestChatUsageUnknownAndReportedZeroAreDistinct(t *testing.T) {
unknown, _ := json.Marshal(ChatUsage{})
if strings.Contains(string(unknown), "total_tokens") || strings.Contains(string(unknown), "cost_usd") {
t.Fatal("unknown metrics became zero")
}
zeroTokens := int64(0)
zeroCost := float64(0)
known, _ := json.Marshal(ChatUsage{Scope: "session", TotalTokens: &zeroTokens, CostUSD: &zeroCost})
for _, want := range []string{`"total_tokens":0`, `"cost_usd":0`, `"scope":"session"`} {
if !strings.Contains(string(known), want) {
t.Fatalf("missing %s in %s", want, known)
}
}
}