From cd4e7f7ee6de9274f98e016142070565e35c0e1a Mon Sep 17 00:00:00 2001 From: Dharit Shah Date: Thu, 8 Oct 2026 15:38:07 -0400 Subject: [PATCH 1/4] refactor(codex): name child transcripts by role A rollout whose session_meta names a parent thread is a native child's. It is saved as -sub-.jsonl, the name #6970 gives: seq counts the kept child rollouts in ascending basename order, role is the child's agent_role when it is a valid persona key and generic otherwise (an unnamed child records a null role). The root keeps -. Path containment, rollout validation and redaction are unchanged. main keeps codex's multi-agent tools off, so no run produces a child rollout yet. Signed-off-by: Dharit Shah --- internal/runtime/codex_transcript.go | 99 +++++++++++- internal/runtime/codex_transcript_test.go | 177 ++++++++++++++++++++++ 2 files changed, 273 insertions(+), 3 deletions(-) diff --git a/internal/runtime/codex_transcript.go b/internal/runtime/codex_transcript.go index 6a8cd8ba2f..57f9288e51 100644 --- a/internal/runtime/codex_transcript.go +++ b/internal/runtime/codex_transcript.go @@ -1,20 +1,29 @@ package runtime import ( + "bufio" + "bytes" + "encoding/json" "fmt" "io" "os" "path/filepath" + "slices" "strings" "time" + "github.com/fullsend-ai/fullsend/internal/config" "github.com/fullsend-ai/fullsend/internal/sandbox" ) // ExtractTranscripts downloads codex's rollout session files (written under // the runner-owned $CODEX_HOME/sessions/YYYY/MM/DD/, one per thread) into -// outputDir as -, with the same path containment as the -// Claude and pi handlers. +// outputDir, with the same path containment as the Claude and pi handlers. +// The root thread's rollout is saved as -; a child +// thread's (its session_meta names a parent_thread_id) as +// -sub-.jsonl, where seq counts the kept child +// rollouts in ascending basename order and role is the child's agent_role +// when it is a valid persona key, else codexGenericRole. // // Only `.jsonl` is collected. codex writes the running session's rollout // uncompressed and compresses older ones in place @@ -51,7 +60,12 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri fmt.Fprintf(os.Stderr, " [%s] No transcripts found\n", agentLabel) return nil } - for _, remotePath := range strings.Split(trimmed, "\n") { + // `find` prints directory order; a child's follows the rollout + // basename, which starts with the thread's start time. + paths := strings.Split(trimmed, "\n") + slices.SortFunc(paths, func(a, b string) int { return strings.Compare(filepath.Base(a), filepath.Base(b)) }) + children := 0 + for _, remotePath := range paths { remotePath = strings.TrimSpace(remotePath) if remotePath == "" { continue @@ -97,6 +111,29 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri os.Remove(stagePath) continue } + // A rollout whose first line names a parent thread is a child's. Its + // role enters the file name only as a valid persona key; anything + // else is codexGenericRole. The child name gets the same os.Root + // check as the root's. + isChild := false + if meta, metaErr := codexReadSessionMeta(stagePath); metaErr == nil && meta.ParentThreadID != "" { + role := codexGenericRole + if config.ValidSubagentKey(meta.AgentRole) { + role = meta.AgentRole + } + childName := codexChildTranscriptName(agentLabel, children+1, role) + cf, createErr := root.Create(childName) + if createErr != nil { + fmt.Fprintf(os.Stderr, " [%s] Skipping (path rejected): %s: %v\n", agentLabel, childName, createErr) + os.Remove(stagePath) + continue + } + cf.Close() + localName = childName + localPath = filepath.Join(outputDir, localName) + os.Remove(localPath) + isChild = true + } // The rollout carries the same raw tool output the stream does, and it // is uploaded as a run artifact, so it gets the same pattern redaction // (codex_redact.go). @@ -111,6 +148,9 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri os.Remove(stagePath) continue } + if isChild { + children++ + } fmt.Fprintf(os.Stderr, " [%s] Saved transcript: %s\n", agentLabel, localName) } return nil @@ -158,6 +198,59 @@ func codexValidSessionPath(sessionsDir, path string) error { return nil } +// codexGenericRole names a child transcript whose rollout carries no usable +// role: a null role, or one that is not a valid persona key. +const codexGenericRole = "generic" + +// codexChildTranscriptName renders -sub-.jsonl, the +// name #6970 gives a native child's rollout. +func codexChildTranscriptName(agentLabel string, seq int, role string) string { + return fmt.Sprintf("%s-sub%d-%s.jsonl", agentLabel, seq, role) +} + +// codexSessionMeta is the payload of a rollout's first line. Only a child +// thread's rollout carries parent_thread_id and agent_role; a null +// agent_role decodes to "". +type codexSessionMeta struct { + ID string `json:"id"` + ParentThreadID string `json:"parent_thread_id"` + AgentRole string `json:"agent_role"` +} + +// codexReadSessionMeta decodes the first non-blank line of a rollout. It is +// an error when that line is not a session_meta envelope. +func codexReadSessionMeta(path string) (codexSessionMeta, error) { + f, err := os.Open(path) + if err != nil { + return codexSessionMeta{}, err + } + defer f.Close() + + scanner := bufio.NewScanner(f) + scanner.Buffer(make([]byte, 0, 64*1024), maxTranscriptLineSize) + for scanner.Scan() { + line := bytes.TrimSpace(scanner.Bytes()) + if len(line) == 0 { + continue + } + var envelope struct { + Type string `json:"type"` + Payload codexSessionMeta `json:"payload"` + } + if err := json.Unmarshal(line, &envelope); err != nil { + return codexSessionMeta{}, fmt.Errorf("line 1 is not JSON") + } + if envelope.Type != "session_meta" { + return codexSessionMeta{}, fmt.Errorf("line 1 is %q, not session_meta", sanitizeOutput(envelope.Type)) + } + return envelope.Payload, nil + } + if err := scanner.Err(); err != nil { + return codexSessionMeta{}, fmt.Errorf("cannot read the first line: %w", err) + } + return codexSessionMeta{}, fmt.Errorf("no session_meta line") +} + // ParseTranscriptErrors scans every JSONL file in transcriptDir and reports // those whose run ended in error. // diff --git a/internal/runtime/codex_transcript_test.go b/internal/runtime/codex_transcript_test.go index e9d25906aa..60d3f1b274 100644 --- a/internal/runtime/codex_transcript_test.go +++ b/internal/runtime/codex_transcript_test.go @@ -4,8 +4,10 @@ import ( "bytes" "context" "encoding/json" + "fmt" "os" "path/filepath" + "strings" "testing" "time" @@ -589,3 +591,178 @@ func TestCodexRun_IgnoresATamperedManifestModel(t *testing.T) { assert.Contains(t, log, "--model 'gpt-5.6-luna'") assert.NotContains(t, log, "gpt-9-tampered") } + +func TestCodexChildTranscriptName(t *testing.T) { + t.Parallel() + + assert.Equal(t, "review-sub1-correctness.jsonl", codexChildTranscriptName("review", 1, "correctness")) + assert.Equal(t, "review-sub2-style-conventions.jsonl", codexChildTranscriptName("review", 2, "style-conventions"), + "a hyphenated role keeps its hyphens") + assert.Equal(t, "retro-sub12-generic.jsonl", codexChildTranscriptName("retro", 12, codexGenericRole), + "no zero padding") +} + +// TestCodexReadSessionMeta pins the two fields ExtractTranscripts names a +// child by. The child and root lines are from a real run, trimmed to the +// keys that matter plus `source`, whose type differs between root (a +// string) and child (an object) and must not trip the decoder. +func TestCodexReadSessionMeta(t *testing.T) { + t.Parallel() + + const root = "01a0eeb4-5338-7fe0-9d39-3c882b63090a" + const kid = "01a0eeb4-73f8-7a52-95cc-0116d80bf9f9" + childLine := `{"type":"session_meta","payload":{"session_id":"` + root + `","id":"` + kid + `",` + + `"parent_thread_id":"` + root + `","source":{"subagent":{"thread_spawn":{"parent_thread_id":"` + root + `",` + + `"depth":1,"agent_path":null,"agent_nickname":"Faraday","agent_role":"probe_ok"}}},` + + `"thread_source":"subagent","agent_nickname":"Faraday","agent_role":"probe_ok","multi_agent_version":"v1"},` + + `"timestamp":"2026-09-29T19:46:38.712Z","ordinal":0}` + rootLine := `{"type":"session_meta","payload":{"session_id":"` + root + `","id":"` + root + `",` + + `"source":"exec","thread_source":"user"},"timestamp":"2026-09-29T19:46:30.334Z","ordinal":0}` + + tests := []struct { + name string + body string + want codexSessionMeta + wantErr string + }{ + {name: "a child names its parent and role", body: childLine + "\n", + want: codexSessionMeta{ID: kid, ParentThreadID: root, AgentRole: "probe_ok"}}, + {name: "the root has no parent", body: rootLine + "\n", + want: codexSessionMeta{ID: root}}, + {name: "a null role decodes to empty", + body: `{"type":"session_meta","payload":{"id":"c","parent_thread_id":"` + root + `","agent_role":null}}` + "\n", + want: codexSessionMeta{ID: "c", ParentThreadID: root}}, + {name: "a leading blank line is skipped", body: "\n" + rootLine + "\n", + want: codexSessionMeta{ID: root}}, + {name: "another envelope first is an error", body: `{"type":"response_item","payload":{}}` + "\n" + childLine + "\n", + wantErr: "not session_meta"}, + {name: "not JSON is an error", body: "not json at all\n", wantErr: "not JSON"}, + {name: "an empty file is an error", body: "", wantErr: "no session_meta line"}, + {name: "an overlong first line is an error", body: strings.Repeat("x", maxTranscriptLineSize+1) + "\n", + wantErr: "cannot read the first line"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + path := filepath.Join(t.TempDir(), "rollout.jsonl") + require.NoError(t, os.WriteFile(path, []byte(tt.body), 0o644)) + + got, err := codexReadSessionMeta(path) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } + + _, err := codexReadSessionMeta(filepath.Join(t.TempDir(), "absent.jsonl")) + assert.Error(t, err, "a missing file is an error, not an empty meta") +} + +// codexFakeRollout is one entry the fake below serves: the remote path `find` +// prints and the body `download` writes for it. +type codexFakeRollout struct { + remote string + body string +} + +// fakeOpenshellCodexRollouts installs a fake "openshell" whose `find` lists +// the given remote paths in the given order and whose `download` serves each +// path's own body (fakeOpenshellCodex serves one body for every path). Bodies +// are copied from files so no body byte goes through shell quoting. +func fakeOpenshellCodexRollouts(t *testing.T, logPath string, rollouts []codexFakeRollout) { + t.Helper() + bodyDir := t.TempDir() + var finds, cases strings.Builder + for i, ro := range rollouts { + bodyPath := filepath.Join(bodyDir, fmt.Sprintf("body-%d", i)) + require.NoError(t, os.WriteFile(bodyPath, []byte(ro.body), 0o644)) + finds.WriteString(" '" + ro.remote + "'") + cases.WriteString(" *'" + filepath.Base(ro.remote) + "') cp '" + bodyPath + `' "$5/$(basename "$4")" ;;` + "\n") + } + binDir := t.TempDir() + script := `#!/bin/sh +echo "$@" >> '` + logPath + `' +if [ "$2" = "exec" ]; then + for last; do :; done + case "$last" in + find\ *) printf '%s\n'` + finds.String() + `; exit 0 ;; + esac + exit 0 +fi +if [ "$2" = "download" ]; then + mkdir -p "$5" + case "$4" in +` + cases.String() + ` esac + exit 0 +fi +exit 0 +` + require.NoError(t, os.WriteFile(filepath.Join(binDir, "openshell"), []byte(script), 0o755)) + t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH")) +} + +// TestCodexExtractTranscripts_NamesChildrenByRole covers the #6970 naming: a +// rollout whose session_meta names a parent thread is a child and is saved +// as -sub-.jsonl; the root keeps -. +func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { + r := CodexRuntime{} + day := r.codexSessionsDir() + "/2026/09/29/" + const root = "01a0eeb4-5338-7fe0-9d39-3c882b63090a" + meta := func(payload string) string { + return `{"type":"session_meta","payload":{` + payload + `}}` + "\n" + } + // A child rollout: the first line names the parent and the role (roleJSON + // is a JSON value, so null is a null role), followed by tool output that + // carries a secret, to show a child goes through the same redaction. + child := func(id, roleJSON string) string { + return meta(`"id":"`+id+`","parent_thread_id":"`+root+`","agent_role":`+roleJSON) + + `{"type":"response_item","payload":{"output":"token ` + codexTestSecret + `"}}` + "\n" + } + logPath := filepath.Join(t.TempDir(), "openshell.log") + // Listed out of basename order on purpose: `find` prints directory + // order, and must follow the rollout's start time, which leads + // the basename. + fakeOpenshellCodexRollouts(t, logPath, []codexFakeRollout{ + {day + "rollout-2026-09-29T15-46-44-0004.jsonl", child("0004", `"../correctness"`)}, // invalid role + {day + "rollout-2026-09-29T15-46-30-" + root + ".jsonl", meta(`"id":"` + root + `"`)}, + {day + "rollout-2026-09-29T15-46-42-0003.jsonl", child("0003", `null`)}, // null role + {day + "rollout-2026-09-29T15-46-38-0001.jsonl", child("0001", `"correctness"`)}, + {day + "rollout-2026-09-29T15-46-40-0002.jsonl", child("0002", `"correctness"`)}, + // A first line that is a rollout envelope but not a session_meta: the + // file is kept under its own basename; the session_meta on line 2 does + // not make it a child. + {day + "rollout-2026-09-29T15-46-46-0005.jsonl", `{"type":"response_item","payload":{}}` + "\n" + child("0005", `"security"`)}, + // Not a rollout at all: discarded. + {day + "rollout-2026-09-29T15-46-48-0006.jsonl", "not a rollout\n"}, + }) + + outDir := filepath.Join(t.TempDir(), "transcripts") + require.NoError(t, r.ExtractTranscripts("sb", "review", outDir)) + + entries, err := os.ReadDir(outDir) + require.NoError(t, err) + var names []string + for _, e := range entries { + names = append(names, e.Name()) + } + assert.ElementsMatch(t, []string{ + "review-rollout-2026-09-29T15-46-30-" + root + ".jsonl", + "review-sub1-correctness.jsonl", + "review-sub2-correctness.jsonl", + "review-sub3-generic.jsonl", + "review-sub4-generic.jsonl", + "review-rollout-2026-09-29T15-46-46-0005.jsonl", + }, names, "the root keeps its basename; children are numbered in basename order and named by their validated role, generic otherwise") + assert.Equal(t, 7, strings.Count(readFileString(t, logPath), " download "), + "every listed rollout is fetched; naming happens after validation, not before the download") + + got, err := os.ReadFile(filepath.Join(outDir, "review-sub1-correctness.jsonl")) + require.NoError(t, err) + assert.NotContains(t, string(got), codexTestSecret, "a child rollout is redacted like the root's") + assert.Contains(t, string(got), `"parent_thread_id"`, "the whole rollout is saved, not only renamed") + assert.Contains(t, string(got), `"id":"0001"`, "sub1 is the earliest child") +} From 431af66a18fb4a67cc550adc12e3ed57af1fc5ec Mon Sep 17 00:00:00 2001 From: Dharit Shah Date: Thu, 8 Oct 2026 18:25:43 -0400 Subject: [PATCH 2/4] refactor(codex): a reserved persona name is no role for a child transcript ValidSubagentKey admits "default", a config key that never names a persona; the reserved list is what refuses it. A child whose rollout records a reserved name is now named generic, like one with a null or malformed role. Signed-off-by: Dharit Shah --- internal/runtime/codex_transcript.go | 8 ++++---- internal/runtime/codex_transcript_test.go | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/runtime/codex_transcript.go b/internal/runtime/codex_transcript.go index 57f9288e51..7f5f25eb28 100644 --- a/internal/runtime/codex_transcript.go +++ b/internal/runtime/codex_transcript.go @@ -23,7 +23,7 @@ import ( // thread's (its session_meta names a parent_thread_id) as // -sub-.jsonl, where seq counts the kept child // rollouts in ascending basename order and role is the child's agent_role -// when it is a valid persona key, else codexGenericRole. +// when it is a legal persona name, else codexGenericRole. // // Only `.jsonl` is collected. codex writes the running session's rollout // uncompressed and compresses older ones in place @@ -112,13 +112,13 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri continue } // A rollout whose first line names a parent thread is a child's. Its - // role enters the file name only as a valid persona key; anything + // role enters the file name only as a legal persona name; anything // else is codexGenericRole. The child name gets the same os.Root // check as the root's. isChild := false if meta, metaErr := codexReadSessionMeta(stagePath); metaErr == nil && meta.ParentThreadID != "" { role := codexGenericRole - if config.ValidSubagentKey(meta.AgentRole) { + if config.ValidSubagentKey(meta.AgentRole) && !slices.Contains(config.ReservedSubagentKeys(), meta.AgentRole) { role = meta.AgentRole } childName := codexChildTranscriptName(agentLabel, children+1, role) @@ -199,7 +199,7 @@ func codexValidSessionPath(sessionsDir, path string) error { } // codexGenericRole names a child transcript whose rollout carries no usable -// role: a null role, or one that is not a valid persona key. +// role: a null role, or one that is not a legal persona name. const codexGenericRole = "generic" // codexChildTranscriptName renders -sub-.jsonl, the diff --git a/internal/runtime/codex_transcript_test.go b/internal/runtime/codex_transcript_test.go index 60d3f1b274..cc46d054c1 100644 --- a/internal/runtime/codex_transcript_test.go +++ b/internal/runtime/codex_transcript_test.go @@ -738,6 +738,7 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { {day + "rollout-2026-09-29T15-46-46-0005.jsonl", `{"type":"response_item","payload":{}}` + "\n" + child("0005", `"security"`)}, // Not a rollout at all: discarded. {day + "rollout-2026-09-29T15-46-48-0006.jsonl", "not a rollout\n"}, + {day + "rollout-2026-09-29T15-46-50-0007.jsonl", child("0007", `"default"`)}, // reserved name }) outDir := filepath.Join(t.TempDir(), "transcripts") @@ -756,8 +757,9 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { "review-sub3-generic.jsonl", "review-sub4-generic.jsonl", "review-rollout-2026-09-29T15-46-46-0005.jsonl", + "review-sub5-generic.jsonl", }, names, "the root keeps its basename; children are numbered in basename order and named by their validated role, generic otherwise") - assert.Equal(t, 7, strings.Count(readFileString(t, logPath), " download "), + assert.Equal(t, 8, strings.Count(readFileString(t, logPath), " download "), "every listed rollout is fetched; naming happens after validation, not before the download") got, err := os.ReadFile(filepath.Join(outDir, "review-sub1-correctness.jsonl")) From 357c3794255beef80b974d15b8cea583564586bd Mon Sep 17 00:00:00 2001 From: Dharit Shah Date: Thu, 8 Oct 2026 18:41:46 -0400 Subject: [PATCH 3/4] refactor(codex): keep saved transcripts and credential-shaped roles out of new names A rollout whose name this extraction already saved is skipped before the reservation would truncate the saved file, whichever of the two was listed first. A role the artifact redactor would mask is no role, so the child is named generic like one with a reserved name. A test covers a child name the output root refuses. Signed-off-by: Dharit Shah --- internal/runtime/codex_transcript.go | 27 ++++++++++++++----- internal/runtime/codex_transcript_test.go | 33 ++++++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/internal/runtime/codex_transcript.go b/internal/runtime/codex_transcript.go index 7f5f25eb28..ae94f53ba1 100644 --- a/internal/runtime/codex_transcript.go +++ b/internal/runtime/codex_transcript.go @@ -65,6 +65,7 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri paths := strings.Split(trimmed, "\n") slices.SortFunc(paths, func(a, b string) int { return strings.Compare(filepath.Base(a), filepath.Base(b)) }) children := 0 + saved := map[string]bool{} for _, remotePath := range paths { remotePath = strings.TrimSpace(remotePath) if remotePath == "" { @@ -83,6 +84,12 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri continue } localName := fmt.Sprintf("%s-%s", agentLabel, filepath.Base(remotePath)) + // A name this extraction already saved is never truncated or removed + // for a later rollout, whichever of the two was listed first. + if saved[localName] { + fmt.Fprintf(os.Stderr, " [%s] Skipping %s: already saved\n", agentLabel, localName) + continue + } f, createErr := root.Create(localName) if createErr != nil { fmt.Fprintf(os.Stderr, " [%s] Skipping (path rejected): %s: %v\n", agentLabel, localName, createErr) @@ -112,16 +119,21 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri continue } // A rollout whose first line names a parent thread is a child's. Its - // role enters the file name only as a legal persona name; anything - // else is codexGenericRole. The child name gets the same os.Root - // check as the root's. + // role enters the file name only as a legal persona name that the + // redactor leaves alone; anything else is codexGenericRole. The child + // name gets the same os.Root check as the root's. isChild := false if meta, metaErr := codexReadSessionMeta(stagePath); metaErr == nil && meta.ParentThreadID != "" { - role := codexGenericRole - if config.ValidSubagentKey(meta.AgentRole) && !slices.Contains(config.ReservedSubagentKeys(), meta.AgentRole) { - role = meta.AgentRole + role := meta.AgentRole + if !config.ValidSubagentKey(role) || slices.Contains(config.ReservedSubagentKeys(), role) || redactSummary(role) != role { + role = codexGenericRole } childName := codexChildTranscriptName(agentLabel, children+1, role) + if saved[childName] { + fmt.Fprintf(os.Stderr, " [%s] Skipping %s: already saved\n", agentLabel, childName) + os.Remove(stagePath) + continue + } cf, createErr := root.Create(childName) if createErr != nil { fmt.Fprintf(os.Stderr, " [%s] Skipping (path rejected): %s: %v\n", agentLabel, childName, createErr) @@ -151,6 +163,7 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri if isChild { children++ } + saved[localName] = true fmt.Fprintf(os.Stderr, " [%s] Saved transcript: %s\n", agentLabel, localName) } return nil @@ -199,7 +212,7 @@ func codexValidSessionPath(sessionsDir, path string) error { } // codexGenericRole names a child transcript whose rollout carries no usable -// role: a null role, or one that is not a legal persona name. +// role: null, reserved, not a legal persona name, or credential-shaped. const codexGenericRole = "generic" // codexChildTranscriptName renders -sub-.jsonl, the diff --git a/internal/runtime/codex_transcript_test.go b/internal/runtime/codex_transcript_test.go index cc46d054c1..2af0a7a4aa 100644 --- a/internal/runtime/codex_transcript_test.go +++ b/internal/runtime/codex_transcript_test.go @@ -738,7 +738,11 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { {day + "rollout-2026-09-29T15-46-46-0005.jsonl", `{"type":"response_item","payload":{}}` + "\n" + child("0005", `"security"`)}, // Not a rollout at all: discarded. {day + "rollout-2026-09-29T15-46-48-0006.jsonl", "not a rollout\n"}, - {day + "rollout-2026-09-29T15-46-50-0007.jsonl", child("0007", `"default"`)}, // reserved name + {day + "rollout-2026-09-29T15-46-50-0007.jsonl", child("0007", `"default"`)}, // reserved name + {day + "rollout-2026-09-29T15-46-52-0008.jsonl", child("0008", `"sk-abcdefghijklmnopqrstuvwxyz"`)}, // credential-shaped + // A crafted basename equal to a child name, listed after that child + // was saved: skipped, so the saved transcript is not truncated. + {day + "sub1-correctness.jsonl", meta(`"id":"planted"`)}, }) outDir := filepath.Join(t.TempDir(), "transcripts") @@ -758,9 +762,10 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { "review-sub4-generic.jsonl", "review-rollout-2026-09-29T15-46-46-0005.jsonl", "review-sub5-generic.jsonl", + "review-sub6-generic.jsonl", }, names, "the root keeps its basename; children are numbered in basename order and named by their validated role, generic otherwise") - assert.Equal(t, 8, strings.Count(readFileString(t, logPath), " download "), - "every listed rollout is fetched; naming happens after validation, not before the download") + assert.Equal(t, 9, strings.Count(readFileString(t, logPath), " download "), + "every listed rollout is fetched, except one whose name was already saved; naming happens after validation, not before the download") got, err := os.ReadFile(filepath.Join(outDir, "review-sub1-correctness.jsonl")) require.NoError(t, err) @@ -768,3 +773,25 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { assert.Contains(t, string(got), `"parent_thread_id"`, "the whole rollout is saved, not only renamed") assert.Contains(t, string(got), `"id":"0001"`, "sub1 is the earliest child") } + +// TestCodexExtractTranscripts_ChildNameRejected covers a child name the +// output root refuses: the child is skipped and nothing in outputDir changes. +func TestCodexExtractTranscripts_ChildNameRejected(t *testing.T) { + r := CodexRuntime{} + day := r.codexSessionsDir() + "/2026/09/29/" + logPath := filepath.Join(t.TempDir(), "openshell.log") + fakeOpenshellCodexRollouts(t, logPath, []codexFakeRollout{ + {day + "rollout-2026-09-29T15-46-38-0001.jsonl", + `{"type":"session_meta","payload":{"id":"0001","parent_thread_id":"root","agent_role":"correctness"}}` + "\n"}, + }) + outDir := filepath.Join(t.TempDir(), "transcripts") + blocker := filepath.Join(outDir, "review-sub1-correctness.jsonl") + require.NoError(t, os.MkdirAll(blocker, 0o755)) + + require.NoError(t, r.ExtractTranscripts("sb", "review", outDir)) + + entries, err := os.ReadDir(outDir) + require.NoError(t, err) + require.Len(t, entries, 1) + assert.True(t, entries[0].IsDir(), "the directory at the child name is untouched and nothing else was saved") +} From 751ffaf5556c070418ff57c65cc20b9f6c0777ea Mon Sep 17 00:00:00 2001 From: Dharit Shah Date: Thu, 8 Oct 2026 19:01:32 -0400 Subject: [PATCH 4/4] refactor(codex): skip a rollout whose basename is a saved transcript's name DownloadFile lands a file at outputDir/ before renaming it to the staging name, so a later rollout with a saved transcript's name as its basename would overwrite that transcript on the way in. The saved-name guard now covers the basename as well. A test with the triage label and a later triage-sub1-correctness.jsonl that fails validation shows the child's contents survive. Signed-off-by: Dharit Shah --- internal/runtime/codex_transcript.go | 6 ++++-- internal/runtime/codex_transcript_test.go | 26 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/internal/runtime/codex_transcript.go b/internal/runtime/codex_transcript.go index ae94f53ba1..c2bcde47c9 100644 --- a/internal/runtime/codex_transcript.go +++ b/internal/runtime/codex_transcript.go @@ -85,8 +85,10 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri } localName := fmt.Sprintf("%s-%s", agentLabel, filepath.Base(remotePath)) // A name this extraction already saved is never truncated or removed - // for a later rollout, whichever of the two was listed first. - if saved[localName] { + // for a later rollout, whichever of the two was listed first. The + // download lands at outputDir/ before the rename, so that + // name counts too. + if saved[localName] || saved[filepath.Base(remotePath)] { fmt.Fprintf(os.Stderr, " [%s] Skipping %s: already saved\n", agentLabel, localName) continue } diff --git a/internal/runtime/codex_transcript_test.go b/internal/runtime/codex_transcript_test.go index 2af0a7a4aa..1f48bfe5c2 100644 --- a/internal/runtime/codex_transcript_test.go +++ b/internal/runtime/codex_transcript_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/fullsend-ai/fullsend/internal/config" "github.com/fullsend-ai/fullsend/internal/harness" "github.com/fullsend-ai/fullsend/internal/sandbox" "github.com/fullsend-ai/fullsend/internal/security" @@ -723,6 +724,8 @@ func TestCodexExtractTranscripts_NamesChildrenByRole(t *testing.T) { `{"type":"response_item","payload":{"output":"token ` + codexTestSecret + `"}}` + "\n" } logPath := filepath.Join(t.TempDir(), "openshell.log") + require.True(t, config.ValidSubagentKey("sk-abcdefghijklmnopqrstuvwxyz"), + "the credential-shaped role passes the key check, so only the redactor turns it generic") // Listed out of basename order on purpose: `find` prints directory // order, and must follow the rollout's start time, which leads // the basename. @@ -795,3 +798,26 @@ func TestCodexExtractTranscripts_ChildNameRejected(t *testing.T) { require.Len(t, entries, 1) assert.True(t, entries[0].IsDir(), "the directory at the child name is untouched and nothing else was saved") } + +// TestCodexExtractTranscripts_DownloadLandingNameSaved covers the download's +// landing path: a file lands at outputDir/ before it is +// renamed to the staging name, so a remote basename equal to a saved name +// must be skipped before the download, or the saved transcript is lost. +func TestCodexExtractTranscripts_DownloadLandingNameSaved(t *testing.T) { + r := CodexRuntime{} + day := r.codexSessionsDir() + "/2026/09/29/" + logPath := filepath.Join(t.TempDir(), "openshell.log") + fakeOpenshellCodexRollouts(t, logPath, []codexFakeRollout{ + {day + "rollout-2026-09-29T15-46-38-0001.jsonl", + `{"type":"session_meta","payload":{"id":"0001","parent_thread_id":"root","agent_role":"correctness"}}` + "\n"}, + // Sorts after the rollout, so it is listed once the child is saved. + {day + "triage-sub1-correctness.jsonl", "not a rollout\n"}, + }) + outDir := filepath.Join(t.TempDir(), "transcripts") + require.NoError(t, r.ExtractTranscripts("sb", "triage", outDir)) + + got, err := os.ReadFile(filepath.Join(outDir, "triage-sub1-correctness.jsonl")) + require.NoError(t, err, "the saved child survives a later file whose basename is its name") + assert.Contains(t, string(got), `"id":"0001"`) + assert.Equal(t, 1, strings.Count(readFileString(t, logPath), " download "), "the colliding file is skipped before the download") +}