diff --git a/internal/runtime/codex_transcript.go b/internal/runtime/codex_transcript.go index 6a8cd8ba2f..c2bcde47c9 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 legal persona name, else codexGenericRole. // // Only `.jsonl` is collected. codex writes the running session's rollout // uncompressed and compresses older ones in place @@ -51,7 +60,13 @@ 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 + saved := map[string]bool{} + for _, remotePath := range paths { remotePath = strings.TrimSpace(remotePath) if remotePath == "" { continue @@ -69,6 +84,14 @@ 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. 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 + } f, createErr := root.Create(localName) if createErr != nil { fmt.Fprintf(os.Stderr, " [%s] Skipping (path rejected): %s: %v\n", agentLabel, localName, createErr) @@ -97,6 +120,34 @@ 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 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 := 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) + 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 +162,10 @@ func (r CodexRuntime) ExtractTranscripts(sandboxName, agentLabel, outputDir stri os.Remove(stagePath) continue } + if isChild { + children++ + } + saved[localName] = true fmt.Fprintf(os.Stderr, " [%s] Saved transcript: %s\n", agentLabel, localName) } return nil @@ -158,6 +213,59 @@ func codexValidSessionPath(sessionsDir, path string) error { return nil } +// codexGenericRole names a child transcript whose rollout carries no usable +// role: null, reserved, not a legal persona name, or credential-shaped. +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..1f48bfe5c2 100644 --- a/internal/runtime/codex_transcript_test.go +++ b/internal/runtime/codex_transcript_test.go @@ -4,14 +4,17 @@ import ( "bytes" "context" "encoding/json" + "fmt" "os" "path/filepath" + "strings" "testing" "time" "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" @@ -589,3 +592,232 @@ 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") + 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. + 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"}, + {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") + 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", + "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, 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) + 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") +} + +// 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") +} + +// 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") +}