Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a Claude reply's tool blocks and usage are decoded from their object, not by a decoder copying the rest of the line

yetonea0f3b33b1f93
diff --git a/internal/sessions/claude.go b/internal/sessions/claude.go
index f9ba6adcb..7072f1752 100644
--- a/internal/sessions/claude.go
+++ b/internal/sessions/claude.go
@@ -171,11 +171,11 @@ func ccTools(s *state, m *claudeContribution, at time.Time, b []byte) {
ID, Name string
Input struct{ Skill any }
}
- dec := json.NewDecoder(bytes.NewReader(rest[i:]))
- if dec.Decode(&tool) != nil {
+ end := objectEnd(rest[i:])
+ if end < 0 || json.Unmarshal(rest[i:i+end], &tool) != nil {
return
}
- rest = rest[i+int(dec.InputOffset()):]
+ rest = rest[i+end:]
skill, _ := tool.Input.Skill.(string)
if tool.Name != "Skill" {
skill = ""
@@ -184,6 +184,38 @@ func ccTools(s *state, m *claudeContribution, at time.Time, b []byte) {
}
}
+// objectEnd is the length of the JSON object b starts with, or -1 when
+// it doesn't close. It only matches brackets outside strings; the object
+// is checked when it is decoded. A json.Decoder over the rest of the line
+// copied all of it, for each tool block and each usage: the most a cold
+// start allocated (1.6 GB of 11.6 GB in its first 2 minutes).
+func objectEnd(b []byte) int {
+ if len(b) == 0 || b[0] != '{' {
+ return -1
+ }
+ depth, in := 0, false
+ for i := 0; i < len(b); i++ {
+ c := b[i]
+ switch {
+ case in:
+ if c == '\\' {
+ i++
+ } else if c == '"' {
+ in = false
+ }
+ case c == '"':
+ in = true
+ case c == '{' || c == '[':
+ depth++
+ case c == '}' || c == ']':
+ if depth--; depth == 0 {
+ return i + 1
+ }
+ }
+ }
+ return -1
+}
+
// ccReply counts a reply's usage: its model and id lead the message, its
// usage follows the content.
func ccReply(s *state, at time.Time, msg, b []byte, main bool) {
@@ -213,7 +245,8 @@ func ccReply(s *state, at time.Time, msg, b []byte, main bool) {
CacheWrite int `json:"cache_creation_input_tokens"`
Creation *ccCacheCreation `json:"cache_creation"`
}
- if json.NewDecoder(bytes.NewReader(b[i+len(ccUsage)-1:])).Decode(&u) != nil {
+ usage := b[i+len(ccUsage)-1:]
+ if end := objectEnd(usage); end < 0 || json.Unmarshal(usage[:end], &u) != nil {
return
}
ccCount(s, m, at, model, ccTokens(u.Input, u.Output, u.CacheRead, u.CacheWrite, u.Creation), main)
diff --git a/internal/sessions/claude_object_end_test.go b/internal/sessions/claude_object_end_test.go
new file mode 100644
index 000000000..b20969f76
--- /dev/null
+++ b/internal/sessions/claude_object_end_test.go
@@ -0,0 +1,56 @@
+package sessions
+
+import (
+ "runtime"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestObjectEnd(t *testing.T) {
+ for _, c := range []struct {
+ in string
+ want int
+ }{
+ {`{}`, 2},
+ {`{"a":1},{"b":2}`, 7},
+ {`{"a":{"b":[1,{"c":2}]}}tail`, 23},
+ {`{"a":"}{]["}x`, 12},
+ {`{"a":"\"}"}x`, 11},
+ {`{"a":"\\"}x`, 10},
+ {`{"a":1`, -1},
+ {`{"a":"}`, -1},
+ {` {}`, -1},
+ {``, -1},
+ } {
+ if got := objectEnd([]byte(c.in)); got != c.want {
+ t.Errorf("objectEnd(%s) = %d, want %d", c.in, got, c.want)
+ }
+ }
+}
+
+// A reply's tool blocks and usage are decoded from the object itself. A
+// json.Decoder over the rest of the line copied all of it each time: a
+// reply with a large tool input cost its whole length again per block.
+func TestClaudeReplyDecodesWithoutCopyingTheLine(t *testing.T) {
+ at := time.Date(2026, 9, 20, 12, 0, 0, 0, time.UTC)
+ big := strings.Repeat("x", 1<<20)
+ content := `[{"type":"tool_use","id":"tool-1","name":"Write","input":{"content":"` + big + `"}},{"type":"tool_use","id":"tool-2","name":"Skill","input":{"skill":"review"}}]`
+ line := claudeUsageLine(at, "a", "req-a", "m", Tokens{Input: 10, Output: 2}, content, true)
+ s := &state{}
+ claudeLine(s, line, true)
+ if s.Models["m"] != (Tokens{Input: 10, Output: 2}) {
+ t.Fatalf("tokens %+v", s.Models["m"])
+ }
+ const n = 20
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ for range n {
+ claudeLine(&state{}, line, true)
+ }
+ runtime.ReadMemStats(&after)
+ // the old decoders copied ~1 MiB a block: 2+ MiB a line
+ if per := (after.TotalAlloc - before.TotalAlloc) / n; per > 256<<10 {
+ t.Fatalf("a %d KB reply allocated %d KB to read", len(line)>>10, per>>10)
+ }
+}