sessions: a codex fork's rollout-<n> turn does not end the copied head (#5090)
ownTurn ended a Codex fork's copied head at the first turn id the parent's
file didn't hold (fb1f4856a). codex-rs names a turn that has no id of its
own "rollout-<n>" (the rollout canonicalizer's implicit turns; the
thread-history builder), and a copied head can carry one the parent's file
lacks. It was taken for the fork's first own turn, so every copied
checkpoint after it was booked as the fork's call: July and August usage
that was the parent's history over again. Reporter: SKYhuangjing, whose
fork branch codex-fork-head-marker (acb034645) this ports.
ownTurn now leaves a "rollout-" turn id alone. callParseVersions and
parseVersions raise codex to 3, so kept shards and call caches are read
again once (the same one-time re-read the codex 2 raise cost).
Changed from the patch: the code comment and the subsystem page
(docs/subsystems/gateway-sessions.md) say what codex-rs does, not what
the reporter's corpus showed; the test comment names #5090.
Checked, not changed: ZCode's fork path (zcDB.copied) matches copies by
the original message id and never reads a turn id; ownTurn has one
caller (codexUsageState.context).
Run (mote-run, at this tree on origin/main + this commit):
- TestCodexForkHeadMarkerDoesNotEndTheCopies red with ownTurn as on
origin/main (4 calls on the fork, want 1), green with the fix.
- go test -count=1 ./internal/sessions ./internal/usage ok; gofmt -l clean.
- GOOS=windows and GOOS=linux go vet ./internal/sessions clean.
Not tried: the reporter's real database (we have no copy of their
corpus). Not confirmed: codex-rs main has no writer that puts a
turn_context with "rollout-<n>" into a forked file from
record_initial_history, as the issue says; the prefix is confirmed from
the two codex-rs writers above, but which one produced the reporter's
files is unknown. Not handled: a fork whose own first turn is itself a
"rollout-<n>" (a legacy rollout migrated with no ids) still doesn't end
the copies by turn id; checkpoints still match it as before.
Full suite: run after the push, result on #5090.
Co-authored-by: SKYhuangjing <13705011+SKYhuangjing@users.noreply.github.com>
This commit is contained in:
@@ -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-<n>` 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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-<n>" 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
|
||||
}
|
||||
|
||||
@@ -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-<n>",
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
@@ -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"`
|
||||
|
||||
@@ -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."}]}}
|
||||
Reference in New Issue
Block a user