diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 4bc444fac..634beb699 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -585,6 +585,10 @@ on Usage and Sessions both (`codexRolloutOf`, `codexCheckpointsOf` in are the head of the fork only: its first turn the parent never had (a `turn_id` of its own) ends them, so a call of the fork's own is counted even when its numbers are those of one the parent made after the fork. +A `rollout-` turn id is not a turn of the fork's own: codex-rs names +a turn that has no id of its own that way (its canonicalizer for older +rollouts, its thread-history builder), and a copied head can hold one +the parent doesn't (`ownTurn`). The parent is looked for in the same Codex home, by the UUIDv7 day of its id, then every day, then `archived_sessions`; one not found leaves the fork read diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index e1af18451..f2261be5b 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -64,7 +64,7 @@ const callCacheVersion = "calls-v15" // parent's (codex_fork.go, zcDB.copied). // codex 2, zcode 2: only the copies are; a fork's own calls are its own // (zcode 1 left out every call of a session holding one copy). -var callParseVersions = map[string]int{"cindy": 1, "codex": 2, "zcode": 2} +var callParseVersions = map[string]int{"cindy": 1, "codex": 3, "zcode": 2} // CallCacheVersion is callCacheVersion and callParseVersions, for a cache // of what is made of the calls (usage's requests-v1.bin and heatmap): a diff --git a/internal/sessions/codex_fork.go b/internal/sessions/codex_fork.go index 4eda7b19d..d4f6f94d6 100644 --- a/internal/sessions/codex_fork.go +++ b/internal/sessions/codex_fork.go @@ -49,8 +49,17 @@ func (s *codexUsageState) forkedFrom(path, parent string) { } } -// ownTurn ends the copies at a turn of the fork's own. +// ownTurn ends the copies at a turn of the fork's own. A turn id of +// "rollout-" is not one: codex-rs gives that name to a turn a rollout +// line belongs to when it has no id of its own (the rollout canonicalizer's +// implicit turns, the thread-history builder), where a parent's own ids are +// uuids. A fork's copied head can hold such a marker the parent's file +// doesn't, and ending the copies there booked every copied checkpoint after +// it as the fork's call (#5090). func (s *codexUsageState) ownTurn(turn string) { + if strings.HasPrefix(turn, "rollout-") { + return + } if turn != "" && s.Copied != nil && s.CopiedTurns != nil && !s.CopiedTurns[turn] { s.Copied, s.CopiedTurns = nil, nil } diff --git a/internal/sessions/codex_fork_headmarker_test.go b/internal/sessions/codex_fork_headmarker_test.go new file mode 100644 index 000000000..911111a8c --- /dev/null +++ b/internal/sessions/codex_fork_headmarker_test.go @@ -0,0 +1,41 @@ +package sessions + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// A copied head can hold a turn of codex-rs's own naming, "rollout-", +// that the parent's file doesn't (a parent's turn ids are uuids). It is not +// the fork's work: with it ending the copies, every copied checkpoint after +// it was booked as the fork's own call (#5090, SKYhuangjing: one parent's 29 +// subagents put 1.1B of the parent's tokens on their day a second time). +func TestCodexForkHeadMarkerDoesNotEndTheCopies(t *testing.T) { + d := setupCalls(t) + const parent, fork = "019d186b-a100-7aaa-8bbb-cccdddeeefff", "019d1f2d-f9c0-7111-8222-333444555666" + place := func(fixture, day, stamp, id string) { + b, err := os.ReadFile(filepath.Join("testdata", fixture)) + if err != nil { + t.Fatal(err) + } + writeLines(t, filepath.Join(d.codex, "sessions", "2026", "03", day, "rollout-2026-03-"+day+"T"+stamp+"-"+id+".jsonl"), strings.TrimSpace(string(b))) + } + place("codex-fork-parent.jsonl", "23", "10-00-00", parent) + place("codex-fork-child-headmarker.jsonl", "24", "17-30-00", fork) + + var onFork Tokens + var forkCalls int + for _, c := range Calls(time.Time{}) { + if strings.Contains(c.File, fork) { + onFork.add(c.Tokens) + forkCalls++ + } + } + want := Tokens{Input: 47328 - 44672, CacheRead: 44672, Output: 550} + if forkCalls != 1 || onFork != want { + t.Fatalf("the fork booked %d calls %+v, want its own one call %+v (the marker must not end the copies)", forkCalls, onFork, want) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 5f46c4e12..b36297884 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -1123,7 +1123,7 @@ const cacheVersion = 29 // // codex 2: only a fork's copies are; its own calls after them are its own, // whatever their numbers. -var parseVersions = map[string]int{"codex": 2, "zcode": 1} +var parseVersions = map[string]int{"codex": 3, "zcode": 1} type cacheFile struct { Version int `json:"version"` diff --git a/internal/sessions/testdata/codex-fork-child-headmarker.jsonl b/internal/sessions/testdata/codex-fork-child-headmarker.jsonl new file mode 100644 index 000000000..303318687 --- /dev/null +++ b/internal/sessions/testdata/codex-fork-child-headmarker.jsonl @@ -0,0 +1,17 @@ +{"timestamp":"2026-03-24T09:30:00.500Z","type":"session_meta","payload":{"id":"019d1f2d-f9c0-7111-8222-333444555666","forked_from_id":"019d186b-a100-7aaa-8bbb-cccdddeeefff","timestamp":"2026-03-24T09:30:00.500Z","cwd":"/Users/sky/proj","originator":"codex_cli_rs","cli_version":"0.117.0","source":{"subagent":{"thread_spawn":{"parent_thread_id":"019d186b-a100-7aaa-8bbb-cccdddeeefff","depth":1}}},"model_provider":"openai","base_instructions":{"text":"You are Codex."}}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"session_meta","payload":{"id":"019d186b-a100-7aaa-8bbb-cccdddeeefff","timestamp":"2026-03-23T02:00:00.120Z","cwd":"/Users/sky/proj","originator":"codex_cli_rs","cli_version":"0.117.0","source":"cli","model_provider":"openai","base_instructions":{"text":"You are Codex."}}} +{"timestamp":"2026-03-24T09:30:00.511Z","type":"turn_context","payload":{"turn_id":"rollout-2","cwd":"/Users/sky/proj","approval_policy":"on-request","sandbox_policy":{"type":"workspace-write"},"model":"gpt-5.4","effort":"high","summary":"auto"}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"turn_context","payload":{"turn_id":"t1","cwd":"/Users/sky/proj","approval_policy":"on-request","sandbox_policy":{"type":"workspace-write"},"model":"gpt-5.4","effort":"high","summary":"auto"}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"review the parser"}]}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":41200,"cached_input_tokens":3072,"output_tokens":812,"reasoning_output_tokens":256,"total_tokens":42012},"last_token_usage":{"input_tokens":41200,"cached_input_tokens":3072,"output_tokens":812,"reasoning_output_tokens":256,"total_tokens":42012},"model_context_window":258400},"rate_limits":{"primary":{"used_percent":5.0,"window_minutes":300,"resets_in_seconds":1200},"secondary":{"used_percent":42.0,"window_minutes":10080,"resets_in_seconds":400000},"credits":null,"plan_type":"pro"}}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Looking."}]}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":84880,"cached_input_tokens":44160,"output_tokens":1500,"reasoning_output_tokens":400,"total_tokens":86380},"last_token_usage":{"input_tokens":43680,"cached_input_tokens":41088,"output_tokens":688,"reasoning_output_tokens":144,"total_tokens":44368},"model_context_window":258400},"rate_limits":{"primary":{"used_percent":5.0,"window_minutes":300,"resets_in_seconds":1200},"secondary":{"used_percent":42.0,"window_minutes":10080,"resets_in_seconds":400000},"credits":null,"plan_type":"pro"}}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"turn_context","payload":{"turn_id":"t2","cwd":"/Users/sky/proj","approval_policy":"on-request","sandbox_policy":{"type":"workspace-write"},"model":"gpt-5.4","effort":"high","summary":"auto"}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"now fix it"}]}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":131072,"cached_input_tokens":88832,"output_tokens":2400,"reasoning_output_tokens":512,"total_tokens":133472},"last_token_usage":{"input_tokens":46192,"cached_input_tokens":44672,"output_tokens":900,"reasoning_output_tokens":112,"total_tokens":47092},"model_context_window":258400},"rate_limits":{"primary":{"used_percent":5.0,"window_minutes":300,"resets_in_seconds":1200},"secondary":{"used_percent":42.0,"window_minutes":10080,"resets_in_seconds":400000},"credits":null,"plan_type":"pro"}}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Fixed."}]}} +{"timestamp":"2026-03-24T09:30:00.512Z","type":"response_item","payload":{"type":"function_call_output","call_id":"call_fork1","output":"You are the newly spawned agent. The prior conversation history was forked from your parent agent. Treat the next user message as your new task, and use the forked history only as background context."}} +{"timestamp":"2026-03-24T09:30:01.000Z","type":"turn_context","payload":{"turn_id":"t4","cwd":"/Users/sky/proj","approval_policy":"on-request","sandbox_policy":{"type":"workspace-write"},"model":"gpt-5.4","effort":"high","summary":"auto"}} +{"timestamp":"2026-03-24T09:30:01.010Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"check the docs"}]}} +{"timestamp":"2026-03-24T09:30:12.000Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":178400,"cached_input_tokens":133504,"output_tokens":2950,"reasoning_output_tokens":560,"total_tokens":181350},"last_token_usage":{"input_tokens":47328,"cached_input_tokens":44672,"output_tokens":550,"reasoning_output_tokens":48,"total_tokens":47878},"model_context_window":258400},"rate_limits":{"primary":{"used_percent":5.0,"window_minutes":300,"resets_in_seconds":1200},"secondary":{"used_percent":42.0,"window_minutes":10080,"resets_in_seconds":400000},"credits":null,"plan_type":"pro"}}} +{"timestamp":"2026-03-24T09:30:12.100Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Docs ok."}]}}