Skip to content

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

sessions: a Codex line's marks are read in one pass, not one bytes.Contains each (#3493)

yetone120264a49fb9
diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go
index dee31962d..07a8eae01 100644
--- a/internal/sessions/calls.go
+++ b/internal/sessions/calls.go
@@ -452,11 +452,89 @@ func callHead(st *callFile, b []byte) bool {
return false
}
compacted := typeAfter(b[:min(len(b), 1024)], cxType) == "compacted"
- if !compacted && (bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) || bytes.Contains(b, cxToolOut) || bytes.Contains(b, cxCustomOut)) {
+ m := cxMarksOf(b)
+ if !compacted && m&cxInput != 0 {
codexCallLine(st, b)
return false
}
- return bytes.Contains(b, cxMeta) || bytes.Contains(b, cxTurn) || bytes.Contains(b, cxRecord) || bytes.Contains(b, cxCompacted) || bytes.Contains(b, cxCount) || bytes.Contains(b, cxSettings) || bytes.Contains(b, cxStarted) || bytes.Contains(b, cxDone)
+ return m&cxFollowed != 0
+}
+
+// cxMarks are the "type" values a rollout line holds anywhere in it that
+// codexCallLine follows, and whether an object in it has the user's role.
+type cxMarks uint16
+
+const (
+ cxMarkUserMsg cxMarks = 1 << iota
+ cxMarkUserRole
+ cxMarkToolOut
+ cxMarkMeta
+ cxMarkTurn
+ cxMarkRecord
+ cxMarkCompacted
+ cxMarkCount
+ cxMarkSettings
+ cxMarkStarted
+ cxMarkDone
+
+ // what the model is given: the prompt, a tool's output
+ cxInput = cxMarkUserMsg | cxMarkUserRole | cxMarkToolOut
+ // the lines codexCallLine reads whole
+ cxFollowed = cxMarkMeta | cxMarkTurn | cxMarkRecord | cxMarkCompacted | cxMarkCount | cxMarkSettings | cxMarkStarted | cxMarkDone
+)
+
+var (
+ cxKeyEnd = []byte(`":"`)
+ cxTypeKey = []byte(`"type`)
+ cxRoleKey = []byte(`"role`)
+ cxUserName = []byte(`user"`)
+)
+
+// cxMarksOf reads b's marks in one pass: every `"type":"<v>"` and
+// `"role":"user"` that a bytes.Contains of each would find, as one each
+// had been (up to twelve passes over a line of megabytes, most of a cold
+// usage read's CPU on Windows, #3493). In a JSON string the quotes are
+// escaped, so the `":"` ending a key is never inside one.
+func cxMarksOf(b []byte) (m cxMarks) {
+ for {
+ i := bytes.Index(b, cxKeyEnd)
+ if i < 0 {
+ return m
+ }
+ key, val := b[:i], b[i+len(cxKeyEnd):]
+ b = val
+ switch {
+ case bytes.HasSuffix(key, cxTypeKey):
+ j := bytes.IndexByte(val, '"')
+ if j < 0 {
+ return m
+ }
+ switch string(val[:j]) {
+ case "user_message":
+ m |= cxMarkUserMsg
+ case "function_call_output", "custom_tool_call_output":
+ m |= cxMarkToolOut
+ case "session_meta":
+ m |= cxMarkMeta
+ case "turn_context":
+ m |= cxMarkTurn
+ case "token_usage_record":
+ m |= cxMarkRecord
+ case "compacted":
+ m |= cxMarkCompacted
+ case "token_count":
+ m |= cxMarkCount
+ case "thread_settings_applied":
+ m |= cxMarkSettings
+ case "task_started":
+ m |= cxMarkStarted
+ case "task_complete":
+ m |= cxMarkDone
+ }
+ case bytes.HasSuffix(key, cxRoleKey) && bytes.HasPrefix(val, cxUserName):
+ m |= cxMarkUserRole
+ }
+ }
}
// sessionOfPath is the session a Claude Code file belongs to by its name:
@@ -743,13 +821,10 @@ func firstChars(s string, n int) string {
}
var (
- ccIdentity = []byte(`"identity":{"modelId":"`)
- ccUserLine = []byte(`"type":"user"`)
- ccCompact = []byte(`"subtype":"compact_boundary"`)
- cxToolOut = []byte(`"type":"function_call_output"`)
- cxCustomOut = []byte(`"type":"custom_tool_call_output"`)
- cxStarted = []byte(`"type":"task_started"`)
- cxDone = []byte(`"type":"task_complete"`)
+ ccIdentity = []byte(`"identity":{"modelId":"`)
+ ccUserLine = []byte(`"type":"user"`)
+ ccCompact = []byte(`"subtype":"compact_boundary"`)
+ cxStarted = []byte(`"type":"task_started"`)
)
// cxCall is a line of a Codex rollout that says something about calls.
@@ -807,18 +882,21 @@ func codexCallLine(st *callFile, b []byte) {
st.codexChanged(r.Usage.compacted(tsAt(b, false), r.Model, cxCompactionIn(b)))
return
}
- if bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) || bytes.Contains(b, cxToolOut) || bytes.Contains(b, cxCustomOut) {
+ m := cxMarksOf(b)
+ if m&cxInput != 0 {
if at := tsAt(b, false); !at.IsZero() {
r.LastIn = at
- if bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) {
+ if m&(cxMarkUserMsg|cxMarkUserRole) != 0 {
r.Usage.Pending = 0
}
r.saveContext()
}
return
}
- if !(!r.Meta && bytes.Contains(b, cxMeta) || bytes.Contains(b, cxTurn) || bytes.Contains(b, cxRecord) || bytes.Contains(b, cxCompacted) || bytes.Contains(b, cxCount) ||
- bytes.Contains(b, cxSettings) || bytes.Contains(b, cxStarted) || bytes.Contains(b, cxDone)) {
+ if r.Meta {
+ m &^= cxMarkMeta
+ }
+ if m&cxFollowed == 0 {
return
}
var l cxCall
diff --git a/internal/sessions/cx_marks_test.go b/internal/sessions/cx_marks_test.go
new file mode 100644
index 000000000..25eadcb75
--- /dev/null
+++ b/internal/sessions/cx_marks_test.go
@@ -0,0 +1,68 @@
+package sessions
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+)
+
+// cxMarksByContains is what callHead and codexCallLine found with a
+// bytes.Contains of each value, a pass over the line each.
+func cxMarksByContains(b []byte) (m cxMarks) {
+ for v, mark := range map[string]cxMarks{
+ `"type":"user_message"`: cxMarkUserMsg, `"role":"user"`: cxMarkUserRole,
+ `"type":"function_call_output"`: cxMarkToolOut, `"type":"custom_tool_call_output"`: cxMarkToolOut,
+ `"type":"session_meta"`: cxMarkMeta, `"type":"turn_context"`: cxMarkTurn,
+ `"type":"token_usage_record"`: cxMarkRecord, `"type":"compacted"`: cxMarkCompacted,
+ `"type":"token_count"`: cxMarkCount, `"type":"thread_settings_applied"`: cxMarkSettings,
+ `"type":"task_started"`: cxMarkStarted, `"type":"task_complete"`: cxMarkDone,
+ } {
+ if bytes.Contains(b, []byte(v)) {
+ m |= mark
+ }
+ }
+ return m
+}
+
+// One pass finds what each value's own pass found, wherever it is in the
+// line: the head, nested past a long string, a string's escaped copy (not
+// found by either), a key whose name only ends in "type".
+func TestCxMarksAsContains(t *testing.T) {
+ long := strings.Repeat(`x\":\"y \"type\":\"token_count\" `, 2000)
+ lines := []string{
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":1}}}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"response_item","payload":{"type":"function_call_output","call_id":"c","output":"` + long + `"}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"event_msg","payload":{"type":"exec_command_end","aggregated_output":"` + long + `"}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"event_msg","payload":{"type":"agent_message","text":"` + long + `","inner":{"type":"task_complete"}}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"compacted","payload":{"replacement_history":[{"type":"message","role":"user"},{"type":"message","role":"assistant"}]}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"session_meta","payload":{"id":"s","base_instructions":{"text":"` + long + `"}}}`,
+ `{"timestamp":"2026-10-25T08:00:00.000Z","type":"turn_context","payload":{"model":"gpt-5.5","subtype":"user_message","xtype":"task_started"}}`,
+ `{"type":"token_usage_record","payload":{"type":"thread_settings_applied","role":"users","r":{"role":"user"}}}`,
+ `{"a":":","type":"custom_tool_call_output","b":"\"role\":\"user\""}`,
+ `"type":"task_started"`, `"type":"`, `"role":"`, `":"`, ``, `{"type":"user_message`,
+ }
+ for _, l := range lines {
+ b := []byte(l)
+ if got, want := cxMarksOf(b), cxMarksByContains(b); got != want {
+ t.Errorf("cxMarksOf(%.120q) = %b, the passes of each found %b", l, got, want)
+ }
+ }
+}
+
+// A line that names none of the values is passed over once: no more than
+// a bytes.Index of `":"` would cost, not one pass a value.
+func BenchmarkCxMarks(b *testing.B) {
+ line := []byte(`{"timestamp":"2026-10-25T08:00:00.000Z","type":"event_msg","payload":{"type":"exec_command_end","aggregated_output":"` + strings.Repeat(`line of output \"quoted\" `, 40000) + `"}}`)
+ b.SetBytes(int64(len(line)))
+ b.Run("one pass", func(b *testing.B) {
+ for range b.N {
+ cxMarksOf(line)
+ }
+ })
+ b.Run("a pass each", func(b *testing.B) {
+ for range b.N {
+ cxMarksByContains(line)
+ }
+ })
+}