The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
add codex session syncing
Justin D'Souzaedda19167436
diff --git a/cli/beacon/cmd/endpoint_codex.go b/cli/beacon/cmd/endpoint_codex.gonew file mode 100644index 00000000..bf1f477c--- /dev/null+++ b/cli/beacon/cmd/endpoint_codex.go@@ -0,0 +1,224 @@+package cmd++import (+ "context"+ "encoding/json"+ "fmt"+ "path/filepath"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/codexsession"+ endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"+ "github.com/spf13/cobra"+)++var endpointCodexCmd = &cobra.Command{+ Use: "codex",+ Short: "Collect telemetry from Codex session files",+ Long: `Collect endpoint telemetry from Codex CLI's local rollout session files.++Beacon's normal Codex integration uses local OpenTelemetry logs and traces for live+capture. This command reads Codex's committed JSONL session files under ~/.codex/sessions+so historical sessions and sessions that ran without Beacon OTLP can still be represented+in the local endpoint log. Every emitted event is marked harness.collection_method=poll:+Beacon sees what Codex already wrote, so nothing here can hold or deny a tool call.`,+}++var endpointCodexSyncCmd = &cobra.Command{+ Use: "sync",+ Short: "Read new Codex session records into the runtime log",+ SilenceUsage: true,+ RunE: runEndpointCodexSync,+}++var endpointCodexStatusCmd = &cobra.Command{+ Use: "status",+ Short: "Show Codex sessions on this machine and how much Beacon collected",+ SilenceUsage: true,+ RunE: runEndpointCodexStatus,+}++var endpointCodexOpts struct {+ codexDir string+ statePath string+ logPath string+ print bool+ watch bool+ interval time.Duration+}++func init() {+ endpointCmd.AddCommand(endpointCodexCmd)+ endpointCodexCmd.AddCommand(endpointCodexSyncCmd)+ endpointCodexCmd.AddCommand(endpointCodexStatusCmd)++ for _, c := range []*cobra.Command{endpointCodexSyncCmd, endpointCodexStatusCmd} {+ f := c.Flags()+ f.StringVar(&endpointCodexOpts.codexDir, "codex-dir", "", "Codex directory (default ~/.codex)")+ f.StringVar(&endpointCodexOpts.statePath, "state", "", "Collector cursor file (default ~/.beacon/endpoint/state/codex.json)")+ f.BoolVar(&endpointOpts.jsonOutput, "json", false, "Print the result as JSON")+ f.BoolVar(&endpointOpts.userMode, "user", true, "Use per-user endpoint paths")+ f.BoolVar(&endpointOpts.systemMode, "system", false, "Use system endpoint paths")+ }++ sync := endpointCodexSyncCmd.Flags()+ sync.StringVar(&endpointCodexOpts.logPath, "log-path", "", "Runtime JSONL log path (default resolved endpoint log)")+ sync.BoolVar(&endpointCodexOpts.print, "print", false, "Print mapped events as JSON without writing them or advancing the cursor (dry run)")+ sync.BoolVar(&endpointCodexOpts.watch, "watch", false, "Sweep continuously on --interval (default: one sweep then exit)")+ sync.DurationVar(&endpointCodexOpts.interval, "interval", time.Minute, "Sweep interval for --watch")+}++func runEndpointCodexSync(cmd *cobra.Command, args []string) error {+ userMode := endpointUserMode()+ opts := codexsession.CollectOptions{+ CodexDir: endpointCodexOpts.codexDir,+ Print: endpointCodexOpts.print,+ Out: cmd.OutOrStdout(),+ Write: !endpointCodexOpts.print,+ UserMode: userMode,+ }+ if !endpointCodexOpts.print {+ opts.StatePath = resolveCodexStatePath(endpointCodexOpts.statePath, userMode)+ opts.LogPath = lifecycle.ResolveRuntimeLog(userMode, endpointCodexOpts.logPath).EffectiveLogPath+ }++ ctx := cmd.Context()+ if ctx == nil {+ ctx = context.Background()+ }+ if endpointCodexOpts.watch && !endpointCodexOpts.print {+ return watchCodex(ctx, cmd, opts)+ }++ summary, err := codexsession.CollectOnce(opts)+ reportCodexSweep(cmd, summary)+ return err+}++func watchCodex(ctx context.Context, cmd *cobra.Command, opts codexsession.CollectOptions) error {+ interval := endpointCodexOpts.interval+ if interval < 5*time.Second {+ interval = 5 * time.Second+ }+ for {+ summary, err := codexsession.CollectOnce(opts)+ if err != nil {+ fmt.Fprintf(cmd.ErrOrStderr(), "codex sync error: %v\n", err)+ }+ reportCodexSweep(cmd, summary)+ select {+ case <-ctx.Done():+ return nil+ case <-time.After(interval):+ }+ }+}++func reportCodexSweep(cmd *cobra.Command, summary codexsession.Summary) {+ if endpointCodexOpts.print {+ return+ }+ if endpointOpts.jsonOutput {+ _ = json.NewEncoder(cmd.OutOrStdout()).Encode(summary)+ return+ }+ fmt.Fprintf(cmd.OutOrStdout(), "codex sync: %d sessions, %d changed, %d events, %d errors\n",+ summary.Sessions, summary.SessionsChanged, summary.EventsEmitted, summary.Errors)+ if summary.MalformedLines > 0 {+ fmt.Fprintf(cmd.OutOrStdout(), " %d unreadable line(s) in Codex session logs\n", summary.MalformedLines)+ }+ if summary.PartialSessions > 0 {+ fmt.Fprintf(cmd.OutOrStdout(), " %d session(s) had a partly written record; it will be read on the next sweep\n", summary.PartialSessions)+ }+}++type codexSessionStatus struct {+ SessionID string `json:"session_id"`+ Path string `json:"path"`+ Workspace string `json:"workspace,omitempty"`+ UpdatedAt string `json:"updated_at,omitempty"`+ CollectedLine int `json:"collected_line"`+ Collected bool `json:"collected"`+ SizeBytes int64 `json:"size_bytes"`+ Title string `json:"title,omitempty"`+}++type codexStatusReport struct {+ CodexDir string `json:"codex_dir"`+ SessionsDir string `json:"sessions_dir"`+ Present bool `json:"present"`+ StatePath string `json:"state_path,omitempty"`+ Sessions []codexSessionStatus `json:"sessions"`+}++func runEndpointCodexStatus(cmd *cobra.Command, args []string) error {+ store, err := codexsession.NewStore(endpointCodexOpts.codexDir)+ if err != nil {+ return err+ }+ statePath := resolveCodexStatePath(endpointCodexOpts.statePath, endpointUserMode())+ state, err := codexsession.LoadState(statePath)+ if err != nil {+ return err+ }+ refs, err := store.List()+ if err != nil {+ return err+ }+ report := codexStatusReport{+ CodexDir: store.CodexDir,+ SessionsDir: store.SessionsDir,+ Present: store.Exists(),+ StatePath: statePath,+ Sessions: make([]codexSessionStatus, 0, len(refs)),+ }+ for _, ref := range refs {+ status := codexSessionStatus{+ SessionID: ref.ID,+ Path: ref.Path,+ Workspace: ref.Workspace,+ SizeBytes: ref.SizeBytes,+ }+ if ref.ModTimeUnixMS > 0 {+ status.UpdatedAt = time.UnixMilli(ref.ModTimeUnixMS).UTC().Format(time.RFC3339)+ }+ if ref.Index != nil {+ status.Title = ref.Index.ThreadName+ }+ if cursor := state.Files[ref.Path]; cursor != nil {+ status.CollectedLine = cursor.LastLine+ status.Collected = cursor.SizeBytes == ref.SizeBytes && cursor.ModTimeUnixMS == ref.ModTimeUnixMS+ }+ report.Sessions = append(report.Sessions, status)+ }+ if endpointOpts.jsonOutput {+ return json.NewEncoder(cmd.OutOrStdout()).Encode(report)+ }+ fmt.Fprintf(cmd.OutOrStdout(), "Codex sessions: %s\n", report.SessionsDir)+ if !report.Present {+ fmt.Fprintln(cmd.OutOrStdout(), "No Codex session directory found.")+ return nil+ }+ fmt.Fprintf(cmd.OutOrStdout(), "Collector state: %s\n", report.StatePath)+ fmt.Fprintf(cmd.OutOrStdout(), "Sessions: %d\n", len(report.Sessions))+ for _, session := range report.Sessions {+ marker := "pending"+ if session.Collected {+ marker = "collected"+ }+ fmt.Fprintf(cmd.OutOrStdout(), " %s %s line=%d size=%d workspace=%s\n",+ marker, session.SessionID, session.CollectedLine, session.SizeBytes, session.Workspace)+ }+ return nil+}++func resolveCodexStatePath(override string, userMode bool) string {+ if override != "" {+ return override+ }+ if userMode {+ return codexsession.DefaultStatePath()+ }+ return filepath.Join(endpointconfig.BaseDir(false), "state", "codex.json")+}diff --git a/cli/beacon/internal/codexsession/codexsession_test.go b/cli/beacon/internal/codexsession/codexsession_test.gonew file mode 100644index 00000000..3df9d708--- /dev/null+++ b/cli/beacon/internal/codexsession/codexsession_test.go@@ -0,0 +1,236 @@+package codexsession++import (+ "encoding/json"+ "os"+ "path/filepath"+ "strconv"+ "strings"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+)++func TestListFindsCodexRolloutSessions(t *testing.T) {+ root := t.TempDir()+ codexDir := filepath.Join(root, ".codex")+ sessionPath := filepath.Join(codexDir, "sessions", "2026", "09", "19", "rollout-2026-09-19T20-00-00-sess-1.jsonl")+ writeFile(t, sessionPath, sessionMetaLine("sess-1", "/tmp/repo")+"\n")+ writeFile(t, filepath.Join(codexDir, "session_index.jsonl"), `{"id":"sess-1","thread_name":"Add telemetry","updated_at":"2026-09-19T20:00:00Z"}`+"\n")++ store, err := NewStore(codexDir)+ if err != nil {+ t.Fatalf("NewStore: %v", err)+ }+ refs, err := store.List()+ if err != nil {+ t.Fatalf("List: %v", err)+ }+ if len(refs) != 1 {+ t.Fatalf("List returned %d refs, want 1: %+v", len(refs), refs)+ }+ if refs[0].ID != "sess-1" || refs[0].Workspace != "/tmp/repo" {+ t.Fatalf("ref metadata = %+v", refs[0])+ }+ if refs[0].Index == nil || refs[0].Index.ThreadName != "Add telemetry" {+ t.Fatalf("index not attached: %+v", refs[0].Index)+ }+}++func TestMapCodexTranscriptProducesEndpointEvents(t *testing.T) {+ ref := SessionRef{ID: "sess-1", Path: "/tmp/codex.jsonl", Workspace: "/tmp/repo"}+ records := decodeFixture(t, []string{+ sessionMetaLine("sess-1", "/tmp/repo"),+ turnContextLine("turn-1", "gpt-6-astra"),+ messageLine("user", "msg-user", "add a health endpoint"),+ messageLine("assistant", "msg-assistant", "I'll run tests."),+ toolCallLine("call-1", "exec", "npm test"),+ tokenUsageLine("sess-1", "turn-1", 120, 90, 10, 8, 2),+ toolOutputLine("call-1", "ok", "completed"),+ taskCompleteLine("turn-1"),+ })++ mapped := MapSession(ref, records, MapOptions{})+ got := actions(mapped)+ want := []string{"session.started", "prompt.submitted", "agent.message", "tool.invoked", "token.usage", "command.executed", "session.status"}+ if strings.Join(got, ",") != strings.Join(want, ",") {+ t.Fatalf("actions = %v, want %v", got, want)+ }+ for _, item := range mapped {+ ev := item.Event+ if ev.Harness.Name != Harness || ev.Harness.CollectionMethod != schema.CollectionMethodPoll {+ t.Fatalf("%s harness = %+v, want Codex poll", ev.Event.Action, ev.Harness)+ }+ if ev.Origin != schema.OriginLocal {+ t.Fatalf("%s origin = %q, want local", ev.Event.Action, ev.Origin)+ }+ if ev.Event.ID == "" {+ t.Fatalf("%s missing deterministic event id", ev.Event.Action)+ }+ if err := ev.Validate(); err != nil {+ t.Fatalf("%s failed validation: %v", ev.Event.Action, err)+ }+ }+ command := findAction(t, mapped, "command.executed")+ if command.Command == nil || command.Command.Command != "npm test" || command.Command.Output != "ok" {+ t.Fatalf("command = %+v, want npm test with output", command.Command)+ }+ usage := findAction(t, mapped, "token.usage")+ if usage.GenAI == nil || usage.GenAI.Usage == nil || usage.GenAI.Usage.InputTokens == nil || *usage.GenAI.Usage.InputTokens != 20 {+ t.Fatalf("usage = %+v, want uncached input tokens", usage.GenAI)+ }+ if usage.GenAI.Usage.CacheRead == nil || usage.GenAI.Usage.CacheCreation == nil {+ t.Fatalf("usage cache fields missing: %+v", usage.GenAI.Usage)+ }+}++func TestCollectOnceUsesCursorAndPrintDoesNotAdvance(t *testing.T) {+ dir := t.TempDir()+ codexDir := filepath.Join(dir, ".codex")+ sessionPath := filepath.Join(codexDir, "sessions", "2026", "09", "19", "rollout-2026-09-19T20-00-00-sess-1.jsonl")+ writeFile(t, sessionPath, sessionMetaLine("sess-1", "/tmp/repo")+"\n"+messageLine("user", "u1", "first")+"\n")+ statePath := filepath.Join(dir, "state", "codex.json")+ logPath := filepath.Join(dir, "runtime.jsonl")++ opts := CollectOptions{CodexDir: codexDir, StatePath: statePath, LogPath: logPath, Write: true, UserMode: true}+ first, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("first CollectOnce: %v", err)+ }+ if first.EventsEmitted == 0 {+ t.Fatal("first sweep emitted no events")+ }+ before := readLog(t, logPath)+ second, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("second CollectOnce: %v", err)+ }+ if second.EventsEmitted != 0 {+ t.Fatalf("second sweep emitted %d events, want 0", second.EventsEmitted)+ }+ if after := readLog(t, logPath); len(after) != len(before) {+ t.Fatalf("log grew from %d to %d events with unchanged cursor", len(before), len(after))+ }++ writeFile(t, sessionPath, sessionMetaLine("sess-1", "/tmp/repo")+"\n"+messageLine("user", "u1", "first")+"\n"+messageLine("user", "u2", "second")+"\n")+ var printed strings.Builder+ printSummary, err := CollectOnce(CollectOptions{CodexDir: codexDir, Print: true, Out: &printed})+ if err != nil {+ t.Fatalf("print CollectOnce: %v", err)+ }+ if printSummary.EventsEmitted == 0 || printed.Len() == 0 {+ t.Fatalf("print sweep emitted nothing: summary=%+v output=%q", printSummary, printed.String())+ }+ realSummary, err := CollectOnce(opts)+ if err != nil {+ t.Fatalf("real CollectOnce after print: %v", err)+ }+ if realSummary.EventsEmitted == 0 {+ t.Fatal("--print advanced the cursor; real sweep had nothing left")+ }+}++func decodeFixture(t *testing.T, lines []string) []Record {+ t.Helper()+ records, stats, err := decodeRecords(strings.NewReader(strings.Join(lines, "\n") + "\n"))+ if err != nil {+ t.Fatalf("decodeRecords: %v", err)+ }+ if stats.Malformed != 0 {+ t.Fatalf("malformed fixture: %+v", stats)+ }+ return records+}++func actions(mapped []MappedEvent) []string {+ out := make([]string, 0, len(mapped))+ for _, item := range mapped {+ out = append(out, item.Event.Event.Action)+ }+ return out+}++func findAction(t *testing.T, mapped []MappedEvent, action string) schema.Event {+ t.Helper()+ for _, item := range mapped {+ if item.Event.Event.Action == action {+ return item.Event+ }+ }+ t.Fatalf("no %s in %v", action, actions(mapped))+ return schema.Event{}+}++func readLog(t *testing.T, path string) []schema.Event {+ t.Helper()+ data, err := os.ReadFile(path)+ if err != nil {+ if os.IsNotExist(err) {+ return nil+ }+ t.Fatal(err)+ }+ var out []schema.Event+ for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {+ if line == "" {+ continue+ }+ var ev schema.Event+ if err := json.Unmarshal([]byte(line), &ev); err != nil {+ t.Fatalf("decode log line: %v", err)+ }+ out = append(out, ev)+ }+ return out+}++func writeFile(t *testing.T, path, content string) {+ t.Helper()+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(path, []byte(content), 0o600); err != nil {+ t.Fatal(err)+ }+}++func sessionMetaLine(sessionID, cwd string) string {+ return `{"timestamp":"2026-09-19T22:00:00.000Z","type":"session_meta","payload":{"session_id":` + quote(sessionID) + `,"id":` + quote(sessionID) + `,"timestamp":"2026-09-19T22:00:00.000Z","cwd":` + quote(cwd) + `,"originator":"codex-tui","cli_version":"0.153.4","source":"cli"}}`+}++func turnContextLine(turnID, model string) string {+ return `{"timestamp":"2026-09-19T22:00:01.000Z","type":"turn_context","payload":{"turn_id":` + quote(turnID) + `,"cwd":"/tmp/repo","model":` + quote(model) + `}}`+}++func messageLine(role, id, text string) string {+ blockType := "input_text"+ if role == "assistant" {+ blockType = "output_text"+ }+ return `{"timestamp":"2026-09-19T22:00:02.000Z","type":"response_item","payload":{"type":"message","id":` + quote(id) + `,"role":` + quote(role) + `,"content":[{"type":` + quote(blockType) + `,"text":` + quote(text) + `}]}}`+}++func toolCallLine(callID, name, input string) string {+ return `{"timestamp":"2026-09-19T22:00:03.000Z","type":"response_item","payload":{"type":"custom_tool_call","id":"ctc_1","status":"completed","call_id":` + quote(callID) + `,"name":` + quote(name) + `,"input":` + quote(input) + `}}`+}++func toolOutputLine(callID, output, status string) string {+ return `{"timestamp":"2026-09-19T22:00:04.000Z","type":"response_item","payload":{"type":"custom_tool_call_output","id":"ctco_1","call_id":` + quote(callID) + `,"output":[{"type":"output_text","text":` + quote(output) + `}],"status":` + quote(status) + `}}`+}++func tokenUsageLine(sessionID, turnID string, input, cached, cacheWrite, output, reasoning int64) string {+ return `{"timestamp":"2026-09-19T22:00:04.000Z","type":"token_usage_record","payload":{"thread_id":` + quote(sessionID) + `,"turn_id":` + quote(turnID) + `,"session_id":` + quote(sessionID) + `,"response_id":"resp_1","turn_token_usage":{"input_tokens":` + i64(input) + `,"cached_input_tokens":` + i64(cached) + `,"cache_write_input_tokens":` + i64(cacheWrite) + `,"output_tokens":` + i64(output) + `,"reasoning_output_tokens":` + i64(reasoning) + `,"total_tokens":130}}}`+}++func taskCompleteLine(turnID string) string {+ return `{"timestamp":"2026-09-19T22:00:05.000Z","type":"event_msg","payload":{"type":"task_complete","turn_id":` + quote(turnID) + `,"duration_ms":1234,"time_to_first_token_ms":99}}`+}++func quote(s string) string {+ data, _ := json.Marshal(s)+ return string(data)+}++func i64(v int64) string {+ return strconv.FormatInt(v, 10)+}diff --git a/cli/beacon/internal/codexsession/collect.go b/cli/beacon/internal/codexsession/collect.gonew file mode 100644index 00000000..c4e7ddf7--- /dev/null+++ b/cli/beacon/internal/codexsession/collect.go@@ -0,0 +1,209 @@+package codexsession++import (+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "os"+ "path/filepath"+ "strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"+)++const StateVersion = 1++type Cursor struct {+ LastLine int `json:"last_line"`+ SizeBytes int64 `json:"size_bytes,omitempty"`+ ModTimeUnixMS int64 `json:"mtime_unix_ms,omitempty"`+ Started bool `json:"started,omitempty"`+}++type State struct {+ Version int `json:"version"`+ Files map[string]*Cursor `json:"files"`+}++type CollectOptions struct {+ CodexDir string+ StatePath string+ Write bool+ LogPath string+ UserMode bool+ Print bool+ Out io.Writer+}++type Summary struct {+ Sessions int `json:"sessions"`+ SessionsChanged int `json:"sessions_changed"`+ EventsEmitted int `json:"events_emitted"`+ Errors int `json:"errors"`+ MalformedLines int `json:"malformed_lines"`+ PartialSessions int `json:"partial_sessions"`+}++func DefaultStatePath() string {+ base := filepath.Join(os.TempDir(), "beacon")+ if home, err := os.UserHomeDir(); err == nil && strings.TrimSpace(home) != "" {+ base = filepath.Join(home, ".beacon")+ }+ return filepath.Join(base, "endpoint", "state", "codex.json")+}++func LoadState(path string) (*State, error) {+ state := &State{Version: StateVersion, Files: map[string]*Cursor{}}+ if path == "" {+ return state, nil+ }+ data, err := os.ReadFile(path)+ if err != nil {+ if errors.Is(err, os.ErrNotExist) {+ return state, nil+ }+ return nil, err+ }+ var stored State+ if err := json.Unmarshal(data, &stored); err != nil {+ return nil, fmt.Errorf("read Codex collector state %s: %w", path, err)+ }+ if stored.Version != StateVersion || stored.Files == nil {+ return state, nil+ }+ state.Files = stored.Files+ return state, nil+}++func (s *State) Save(path string) error {+ if path == "" {+ return nil+ }+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ return err+ }+ s.Version = StateVersion+ data, err := json.MarshalIndent(s, "", " ")+ if err != nil {+ return err+ }+ tmp := path + ".tmp"+ if err := os.WriteFile(tmp, data, 0o600); err != nil {+ return err+ }+ return os.Rename(tmp, path)+}++func (s *State) cursor(path string) *Cursor {+ if s.Files == nil {+ s.Files = map[string]*Cursor{}+ }+ c := s.Files[path]+ if c == nil {+ c = &Cursor{}+ s.Files[path] = c+ }+ return c+}++func CollectOnce(opts CollectOptions) (summary Summary, err error) {+ store, err := NewStore(opts.CodexDir)+ if err != nil {+ return summary, err+ }+ refs, err := store.List()+ if err != nil {+ return summary, err+ }+ summary.Sessions = len(refs)+ if len(refs) == 0 {+ return summary, nil+ }+ state, err := LoadState(opts.StatePath)+ if err != nil {+ return summary, err+ }+ defer func() {+ if saveErr := state.Save(opts.StatePath); saveErr != nil && err == nil {+ err = fmt.Errorf("save Codex collector state: %w", saveErr)+ }+ }()++ var errs []error+ for _, ref := range refs {+ changed, collectErr := collectSession(store, ref, state, opts, &summary)+ if collectErr != nil {+ summary.Errors+++ errs = append(errs, fmt.Errorf("Codex session %s: %w", ref.ID, collectErr))+ continue+ }+ if changed {+ summary.SessionsChanged+++ }+ }+ if len(errs) > 0 {+ return summary, errors.Join(errs...)+ }+ return summary, nil+}++func collectSession(store *Store, ref SessionRef, state *State, opts CollectOptions, summary *Summary) (bool, error) {+ cursor := state.cursor(ref.Path)+ if ref.SizeBytes < cursor.SizeBytes {+ cursor.LastLine = 0+ cursor.Started = false+ }+ if ref.SizeBytes == cursor.SizeBytes && ref.ModTimeUnixMS == cursor.ModTimeUnixMS {+ return false, nil+ }+ records, stats, err := store.Read(ref)+ summary.MalformedLines += stats.Malformed+ if stats.PartialTail {+ summary.PartialSessions+++ }+ if err != nil {+ return false, err+ }+ mapped := MapSession(ref, records, MapOptions{MinLine: cursor.LastLine, SkipSessionStarted: cursor.Started})+ if len(mapped) == 0 {+ advanceCursor(cursor, ref, stats.Lines)+ return false, nil+ }+ for i, item := range mapped {+ if err := emit(item.Event, opts); err != nil {+ if i > 0 {+ cursor.LastLine = mapped[i-1].SourceLine+ }+ return true, err+ }+ summary.EventsEmitted+++ if item.Event.Event.Action == "session.started" {+ cursor.Started = true+ }+ }+ advanceCursor(cursor, ref, stats.Lines)+ return true, nil+}++func advanceCursor(cursor *Cursor, ref SessionRef, lines int) {+ cursor.LastLine = lines+ cursor.SizeBytes = ref.SizeBytes+ cursor.ModTimeUnixMS = ref.ModTimeUnixMS+}++func emit(event schema.Event, opts CollectOptions) error {+ if opts.Print {+ out := opts.Out+ if out == nil {+ out = io.Discard+ }+ return json.NewEncoder(out).Encode(event)+ }+ if !opts.Write {+ return nil+ }+ _, err := writer.AppendEvent(event, writer.Options{Path: opts.LogPath, UserMode: opts.UserMode})+ return err+}diff --git a/cli/beacon/internal/codexsession/mapper.go b/cli/beacon/internal/codexsession/mapper.gonew file mode 100644index 00000000..d71d9c83--- /dev/null+++ b/cli/beacon/internal/codexsession/mapper.go@@ -0,0 +1,556 @@+package codexsession++import (+ "crypto/sha1"+ "encoding/hex"+ "encoding/json"+ "fmt"+ "io"+ "path/filepath"+ "strings"+ "time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"+)++type MappedEvent struct {+ DedupID string+ SourceLine int+ Event schema.Event+}++type MapOptions struct {+ MinLine int+ SkipSessionStarted bool+}++type codexToolCall struct {+ ID string+ Name string+ Input interface{}+}++type mapper struct {+ ref SessionRef+ opts MapOptions+ out []MappedEvent+ tools map[string]codexToolCall+ sessionID string+ workspace string+ model string+ turnID string+ started bool+}++func MapSession(ref SessionRef, records []Record, opts MapOptions) []MappedEvent {+ m := &mapper{+ ref: ref,+ opts: opts,+ tools: map[string]codexToolCall{},+ sessionID: ref.ID,+ workspace: ref.Workspace,+ }+ for i := range records {+ m.consume(records[i])+ }+ return m.out+}++func (m *mapper) consume(record Record) {+ entry := record.Entry+ emit := record.Line > m.opts.MinLine++ switch {+ case entry.SessionMeta != nil:+ m.consumeSessionMeta(record, emit)+ case entry.TurnContext != nil:+ m.consumeTurnContext(record)+ case entry.ResponseItem != nil:+ m.consumeResponseItem(record, emit)+ case entry.EventMessage != nil:+ m.consumeEventMessage(record, emit)+ case entry.TokenUsageRecord != nil:+ m.consumeTokenUsageRecord(record, emit)+ }+}++func (m *mapper) consumeSessionMeta(record Record, emit bool) {+ meta := record.Entry.SessionMeta+ if id := firstNonEmpty(meta.ID, meta.SessionID); id != "" {+ m.sessionID = id+ }+ if meta.CWD != "" {+ m.workspace = meta.CWD+ }+ if emit && !m.opts.SkipSessionStarted && !m.started {+ ev := m.base(record, "session.started", "session", schema.SeverityInfo, schema.FidelityObserved, "Codex session started")+ ev.Raw = map[string]interface{}{"codex_session": m.raw(record)}+ m.append(record, "session.started", ev)+ m.started = true+ }+}++func (m *mapper) consumeTurnContext(record Record) {+ ctx := record.Entry.TurnContext+ if ctx.TurnID != "" {+ m.turnID = ctx.TurnID+ }+ if ctx.CWD != "" {+ m.workspace = ctx.CWD+ }+ if ctx.Model != "" {+ m.model = asymptoteobserve.NormalizeModelName(ctx.Model)+ }+}++func (m *mapper) consumeResponseItem(record Record, emit bool) {+ item := record.Entry.ResponseItem+ switch item.Type {+ case "message":+ m.consumeMessage(record, item, emit)+ case "custom_tool_call":+ call := codexToolCall{ID: item.CallID, Name: item.Name, Input: item.Input}+ if call.ID != "" {+ m.tools[call.ID] = call+ }+ if emit {+ m.emitToolInvoked(record, call)+ }+ case "custom_tool_call_output":+ if emit {+ m.emitToolResult(record, item)+ }+ }+}++func (m *mapper) consumeMessage(record Record, item *ResponseItem, emit bool) {+ if !emit {+ return+ }+ text := strings.TrimSpace(textFromContent(item.Content))+ if text == "" {+ return+ }+ switch strings.ToLower(item.Role) {+ case "user":+ m.emitPrompt(record, text)+ case "assistant":+ m.emitAgentMessage(record, text, item.ID)+ }+}++func (m *mapper) consumeEventMessage(record Record, emit bool) {+ msg := record.Entry.EventMessage+ if msg.TurnID != "" {+ m.turnID = msg.TurnID+ }+ if !emit || msg.Type != "task_complete" {+ return+ }+ ev := m.base(record, "session.status", "session", schema.SeverityInfo, schema.FidelityObserved, "Codex task completed")+ ev.Raw = map[string]interface{}{+ "codex_session": map[string]interface{}{+ "source_path": recordSourcePath(m.ref),+ "line": record.Line,+ "payload_type": msg.Type,+ "turn_id": msg.TurnID,+ "duration_ms": msg.DurationMS,+ "time_to_first_token_ms": msg.TimeToFirstTokenMS,+ },+ }+ m.append(record, "task.complete", ev)+}++func (m *mapper) consumeTokenUsageRecord(record Record, emit bool) {+ usageRecord := record.Entry.TokenUsageRecord+ if usageRecord.TurnID != "" {+ m.turnID = usageRecord.TurnID+ }+ if usageRecord.SessionID != "" {+ m.sessionID = usageRecord.SessionID+ }+ if !emit {+ return+ }+ usage := usageFromCodex(firstUsage(usageRecord.TurnTokenUsage, usageRecord.Usage))+ if usage == nil {+ return+ }+ ev := m.base(record, "token.usage", "metric", schema.SeverityInfo, schema.FidelityObserved, "Codex token usage observed")+ ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Usage: usage})+ if ev.GenAI == nil {+ ev.GenAI = &schema.GenAIInfo{}+ }+ ev.GenAI.Conversation = &schema.GenAIConversationInfo{ID: m.sessionID}+ if usageRecord.ResponseID != "" {+ ev.GenAI.Response = &schema.GenAIResponseInfo{ID: usageRecord.ResponseID, Model: m.model}+ }+ ev.Raw = map[string]interface{}{+ "codex_session": map[string]interface{}{+ "source_path": recordSourcePath(m.ref),+ "line": record.Line,+ "turn_id": usageRecord.TurnID,+ "source": "codex_session_token_usage_record",+ },+ }+ m.append(record, "usage."+firstNonEmpty(usageRecord.TurnID, itoa(record.Line)), ev)+}++func (m *mapper) emitPrompt(record Record, text string) {+ ev := m.base(record, "prompt.submitted", "prompt", schema.SeverityInfo, schema.FidelityObserved, "Prompt submitted to Codex")+ ev.Prompt = &schema.PromptInfo{Text: text}+ ev.Content = contentMarker(text)+ ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Input: &schema.GenAIInputInfo{Messages: asymptoteobserve.TextInputMessages(text)}})+ m.append(record, "prompt", ev)+}++func (m *mapper) emitAgentMessage(record Record, text, responseID string) {+ ev := m.base(record, "agent.message", "agent", schema.SeverityInfo, schema.FidelityObserved, "Codex assistant message")+ ev.Content = contentMarker(text)+ ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Output: &schema.GenAIOutputInfo{Messages: asymptoteobserve.TextOutputMessages(text)}})+ if responseID != "" {+ ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Response: &schema.GenAIResponseInfo{ID: responseID, Model: m.model}})+ }+ m.append(record, "agent.message", ev)+}++func (m *mapper) emitToolInvoked(record Record, call codexToolCall) {+ if call.Name == "" {+ return+ }+ ev := m.base(record, "tool.invoked", "tool", schema.SeverityInfo, schema.FidelityObserved, "Codex tool invocation observed")+ ev.Tool = &schema.ToolInfo{Name: call.Name}+ ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Tool: &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: call.ID, Arguments: call.Input}}})+ if command := commandFromTool(call); command != "" {+ ev.Tool.Command = command+ }+ m.append(record, "tool.invoked."+firstNonEmpty(call.ID, itoa(record.Line)), ev)+}++func (m *mapper) emitToolResult(record Record, item *ResponseItem) {+ call := m.tools[item.CallID]+ if call.ID == "" {+ call.ID = item.CallID+ }+ output := toolOutputText(item.Output)+ if isCommandTool(call) {+ ev := m.base(record, "command.executed", "command", severityForStatus(item.Status), schema.FidelityObserved, "Codex command completed")+ ev.Command = &schema.CommandInfo{Command: commandFromTool(call), Output: output}+ ev.Tool = &schema.ToolInfo{Name: call.Name, Command: commandFromTool(call)}+ ev.GenAI = toolResultGenAI(call, output)+ m.append(record, "tool.result."+firstNonEmpty(call.ID, itoa(record.Line)), ev)+ return+ }+ action := "tool.completed"+ if isErrorStatus(item.Status) {+ action = "tool.failed"+ }+ ev := m.base(record, action, "tool", severityForStatus(item.Status), schema.FidelityObserved, "Codex tool completed")+ ev.Tool = &schema.ToolInfo{Name: call.Name}+ ev.GenAI = toolResultGenAI(call, output)+ m.append(record, "tool.result."+firstNonEmpty(call.ID, itoa(record.Line)), ev)+}++func (m *mapper) base(record Record, action, category string, severity schema.Severity, fidelity, message string) schema.Event {+ ev := schema.NewEvent(schema.NewEventOptions{+ Action: action,+ Category: category,+ Severity: severity,+ Fidelity: fidelity,+ Message: message,+ Origin: schema.OriginLocal,+ Harness: schema.HarnessInfo{Name: Harness, CollectionMethod: schema.CollectionMethodPoll},+ })+ if ts := eventTimestamp(record.Entry); ts != "" {+ ev.Timestamp = ts+ }+ ev.Session = &schema.SessionInfo{ID: firstNonEmpty(m.sessionID, m.ref.ID), WorkingDirectory: m.workspace}+ if m.workspace != "" {+ ev.Repository = m.workspace+ }+ if m.model != "" {+ ev.Model = m.model+ }+ return ev+}++func (m *mapper) append(record Record, suffix string, ev schema.Event) {+ coordinate := firstNonEmpty(m.sessionID, m.ref.ID, m.ref.Path) + ":" + itoa(record.Line) + ":" + suffix+ ev.Event.ID = codexEventID(coordinate)+ m.out = append(m.out, MappedEvent{DedupID: ev.Event.ID, SourceLine: record.Line, Event: ev})+}++func (m *mapper) raw(record Record) map[string]interface{} {+ raw := map[string]interface{}{+ "source_path": recordSourcePath(m.ref),+ "line": record.Line,+ "entry_type": record.Entry.Type,+ }+ if record.Entry.SessionMeta != nil {+ raw["cli_version"] = record.Entry.SessionMeta.CLIVersion+ raw["originator"] = record.Entry.SessionMeta.Originator+ raw["source"] = record.Entry.SessionMeta.Source+ }+ return raw+}++func eventTimestamp(entry Entry) string {+ for _, value := range []string{entry.Timestamp, sessionMetaTimestamp(entry)} {+ if strings.TrimSpace(value) == "" {+ continue+ }+ if ts, err := time.Parse(time.RFC3339Nano, value); err == nil {+ return schema.FormatTimestamp(ts.UTC())+ }+ }+ return ""+}++func sessionMetaTimestamp(entry Entry) string {+ if entry.SessionMeta == nil {+ return ""+ }+ return entry.SessionMeta.Timestamp+}++func contentMarker(text string) *schema.ContentInfo {+ return asymptoteobserve.RetainedContent(text, asymptoteobserve.DefaultRawStringLimit)+}++func textFromContent(value interface{}) string {+ switch v := value.(type) {+ case string:+ return strings.TrimSpace(v)+ case []interface{}:+ var parts []string+ for _, raw := range v {+ block, ok := raw.(map[string]interface{})+ if !ok {+ continue+ }+ switch stringValue(block["type"]) {+ case "input_text", "output_text", "text", "summary_text":+ if text := strings.TrimSpace(firstString(block, "text", "content")); text != "" {+ parts = append(parts, text)+ }+ case "reasoning", "reasoning_text":+ if text := strings.TrimSpace(firstString(block, "text", "summary")); text != "" {+ parts = append(parts, text)+ }+ }+ }+ return strings.TrimSpace(strings.Join(parts, "\n"))+ default:+ return ""+ }+}++func toolOutputText(value interface{}) string {+ switch v := value.(type) {+ case string:+ return v+ case []interface{}:+ var parts []string+ for _, raw := range v {+ if block, ok := raw.(map[string]interface{}); ok {+ if text := strings.TrimSpace(firstString(block, "text", "content", "output")); text != "" {+ parts = append(parts, text)+ }+ continue+ }+ if s, ok := raw.(string); ok && strings.TrimSpace(s) != "" {+ parts = append(parts, s)+ }+ }+ return strings.TrimSpace(strings.Join(parts, "\n"))+ default:+ if value == nil {+ return ""+ }+ data, _ := json.Marshal(value)+ return string(data)+ }+}++func commandFromTool(call codexToolCall) string {+ switch v := call.Input.(type) {+ case string:+ return strings.TrimSpace(v)+ case map[string]interface{}:+ return firstString(v, "command", "cmd", "shell_command")+ default:+ if call.Input == nil {+ return ""+ }+ data, _ := json.Marshal(call.Input)+ return string(data)+ }+}++func isCommandTool(call codexToolCall) bool {+ switch strings.ToLower(call.Name) {+ case "exec", "exec_command", "shell", "bash":+ return true+ default:+ return false+ }+}++func toolResultGenAI(call codexToolCall, result string) *schema.GenAIInfo {+ return &schema.GenAIInfo{Tool: &schema.GenAIToolInfo{Name: call.Name, Call: &schema.GenAIToolCallInfo{ID: call.ID, Arguments: call.Input, Result: result}}}+}++func usageFromCodex(raw *TokenUsage) *schema.GenAIUsageInfo {+ if raw == nil {+ return nil+ }+ usage := &schema.GenAIUsageInfo{}+ uncached := raw.InputTokens - raw.CachedInputTokens - raw.CacheWriteInputTokens+ if uncached < 0 {+ uncached = 0+ }+ usage.InputTokens = &uncached+ cached := max64(raw.CachedInputTokens, 0)+ cacheWrite := max64(raw.CacheWriteInputTokens, 0)+ output := max64(raw.OutputTokens, 0)+ reasoning := max64(raw.ReasoningOutputTokens, 0)+ usage.CacheRead = &schema.GenAIUsageCacheReadInfo{InputTokens: &cached}+ usage.CacheCreation = &schema.GenAIUsageCacheCreationInfo{InputTokens: &cacheWrite}+ usage.OutputTokens = &output+ usage.Reasoning = &schema.GenAIUsageReasoningInfo{OutputTokens: &reasoning}+ return usage+}++func firstUsage(values ...*TokenUsage) *TokenUsage {+ for _, value := range values {+ if value != nil {+ return value+ }+ }+ return nil+}++func mergeGenAI(base, next *schema.GenAIInfo) *schema.GenAIInfo {+ if base == nil {+ return next+ }+ if next == nil {+ return base+ }+ if next.Input != nil {+ base.Input = next.Input+ }+ if next.Output != nil {+ base.Output = next.Output+ }+ if next.Response != nil {+ base.Response = next.Response+ }+ if next.Tool != nil {+ base.Tool = next.Tool+ }+ if next.Usage != nil {+ base.Usage = next.Usage+ }+ if next.Conversation != nil {+ base.Conversation = next.Conversation+ }+ return base+}++func severityForStatus(status string) schema.Severity {+ if isErrorStatus(status) {+ return schema.SeverityMedium+ }+ return schema.SeverityInfo+}++func isErrorStatus(status string) bool {+ switch strings.ToLower(status) {+ case "failed", "failure", "error":+ return true+ default:+ return false+ }+}++func recordSourcePath(ref SessionRef) string {+ return ref.Path+}++func stringValue(value interface{}) string {+ if s, ok := value.(string); ok {+ return s+ }+ return ""+}++func firstString(m map[string]interface{}, keys ...string) string {+ for _, key := range keys {+ if value := stringValue(m[key]); value != "" {+ return value+ }+ }+ return ""+}++func firstNonEmpty(values ...string) string {+ for _, value := range values {+ if strings.TrimSpace(value) != "" {+ return value+ }+ }+ return ""+}++func max64(value, floor int64) int64 {+ if value < floor {+ return floor+ }+ return value+}++func itoa(n int) string {+ if n == 0 {+ return "0"+ }+ var buf [20]byte+ i := len(buf)+ neg := n < 0+ if neg {+ n = -n+ }+ for n > 0 {+ i--+ buf[i] = byte('0' + n%10)+ n /= 10+ }+ if neg {+ i--+ buf[i] = '-'+ }+ return string(buf[i:])+}++var codexIDNamespace = [16]byte{+ 0xb6, 0xa8, 0x4d, 0x69, 0x26, 0xa6, 0x45, 0x9d,+ 0x8a, 0x89, 0x1b, 0xe6, 0x65, 0x2d, 0x14, 0x5c,+}++func codexEventID(dedupID string) string {+ digest := sha1.New()+ digest.Write(codexIDNamespace[:])+ io.WriteString(digest, dedupID)+ var id [16]byte+ copy(id[:], digest.Sum(nil))+ id[6] = (id[6] & 0x0f) | 0x50+ id[8] = (id[8] & 0x3f) | 0x80+ out := make([]byte, 32)+ hex.Encode(out, id[:])+ return fmt.Sprintf("%s-%s-%s-%s-%s", out[0:8], out[8:12], out[12:16], out[16:20], out[20:32])+}++func languageForPath(path string) string {+ return strings.TrimPrefix(filepath.Ext(path), ".")+}diff --git a/cli/beacon/internal/codexsession/store.go b/cli/beacon/internal/codexsession/store.gonew file mode 100644index 00000000..f73bc9a3--- /dev/null+++ b/cli/beacon/internal/codexsession/store.go@@ -0,0 +1,228 @@+package codexsession++import (+ "bufio"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "io/fs"+ "os"+ "path/filepath"+ "sort"+ "strings"+)++const MaxLineBytes = 8 * 1024 * 1024++type SessionRef struct {+ ID string+ Path string+ CodexDir string+ ModTimeUnixMS int64+ SizeBytes int64+ Workspace string+ Index *IndexEntry+}++type Store struct {+ CodexDir string+ SessionsDir string+}++type Stats struct {+ Lines int+ Decoded int+ Malformed int+ PartialTail bool+ FirstError error+}++type IndexEntry struct {+ ID string `json:"id"`+ ThreadName string `json:"thread_name,omitempty"`+ UpdatedAt string `json:"updated_at,omitempty"`+}++func DefaultCodexDir() (string, error) {+ home, err := os.UserHomeDir()+ if err != nil {+ return "", fmt.Errorf("resolve home directory: %w", err)+ }+ if strings.TrimSpace(home) == "" {+ return "", errors.New("home directory is empty")+ }+ return filepath.Join(home, ".codex"), nil+}++func NewStore(dir string) (*Store, error) {+ codexDir := strings.TrimSpace(dir)+ if codexDir == "" {+ def, err := DefaultCodexDir()+ if err != nil {+ return nil, err+ }+ codexDir = def+ }+ return &Store{CodexDir: codexDir, SessionsDir: filepath.Join(codexDir, "sessions")}, nil+}++func (s *Store) Exists() bool {+ info, err := os.Stat(s.SessionsDir)+ return err == nil && info.IsDir()+}++func (s *Store) List() ([]SessionRef, error) {+ index := s.readIndex()+ var refs []SessionRef+ err := filepath.WalkDir(s.SessionsDir, func(path string, d fs.DirEntry, walkErr error) error {+ if walkErr != nil {+ return nil+ }+ if d.IsDir() {+ return nil+ }+ ext := filepath.Ext(path)+ if ext != ".jsonl" && ext != ".json" {+ return nil+ }+ info, err := d.Info()+ if err != nil || info.IsDir() {+ return nil+ }+ ref := SessionRef{+ ID: sessionIDFromPath(path),+ Path: path,+ CodexDir: s.CodexDir,+ ModTimeUnixMS: info.ModTime().UnixMilli(),+ SizeBytes: info.Size(),+ }+ if meta := readMetadata(path); meta != nil {+ if id := firstNonEmpty(meta.ID, meta.SessionID); id != "" {+ ref.ID = id+ }+ ref.Workspace = meta.CWD+ }+ if idx := index[ref.ID]; idx != nil {+ ref.Index = idx+ }+ refs = append(refs, ref)+ return nil+ })+ if err != nil {+ if errors.Is(err, fs.ErrNotExist) {+ return nil, nil+ }+ return nil, fmt.Errorf("read Codex sessions directory: %w", err)+ }+ sort.SliceStable(refs, func(i, j int) bool {+ if refs[i].ModTimeUnixMS != refs[j].ModTimeUnixMS {+ return refs[i].ModTimeUnixMS < refs[j].ModTimeUnixMS+ }+ return refs[i].Path < refs[j].Path+ })+ return refs, nil+}++func (s *Store) Read(ref SessionRef) ([]Record, Stats, error) {+ f, err := os.Open(ref.Path)+ if err != nil {+ return nil, Stats{}, err+ }+ defer f.Close()+ return decodeRecords(f)+}++func decodeRecords(r io.Reader) ([]Record, Stats, error) {+ scanner := bufio.NewScanner(r)+ scanner.Buffer(make([]byte, 0, 64*1024), MaxLineBytes)+ var records []Record+ var stats Stats+ lineNo := 0+ for scanner.Scan() {+ lineNo+++ stats.Lines = lineNo+ line := scanner.Bytes()+ if len(strings.TrimSpace(string(line))) == 0 {+ continue+ }+ entry, err := decodeEntry(line)+ if err != nil {+ stats.Malformed+++ if stats.FirstError == nil {+ stats.FirstError = err+ }+ continue+ }+ stats.Decoded+++ records = append(records, Record{Line: lineNo, Entry: *entry})+ }+ if err := scanner.Err(); err != nil {+ if errors.Is(err, bufio.ErrTooLong) {+ stats.Malformed+++ if stats.FirstError == nil {+ stats.FirstError = fmt.Errorf("codex session line exceeds %d bytes", MaxLineBytes)+ }+ return records, stats, nil+ }+ return records, stats, err+ }+ return records, stats, nil+}++func (s *Store) readIndex() map[string]*IndexEntry {+ data, err := os.ReadFile(filepath.Join(s.CodexDir, "session_index.jsonl"))+ if err != nil {+ return nil+ }+ out := map[string]*IndexEntry{}+ for _, line := range strings.Split(string(data), "\n") {+ line = strings.TrimSpace(line)+ if line == "" {+ continue+ }+ var entry IndexEntry+ if err := json.Unmarshal([]byte(line), &entry); err != nil || entry.ID == "" {+ continue+ }+ copy := entry+ out[entry.ID] = ©+ }+ return out+}++func readMetadata(path string) *SessionMeta {+ f, err := os.Open(path)+ if err != nil {+ return nil+ }+ defer f.Close()+ scanner := bufio.NewScanner(f)+ scanner.Buffer(make([]byte, 0, 64*1024), MaxLineBytes)+ for scanner.Scan() {+ line := strings.TrimSpace(scanner.Text())+ if line == "" {+ continue+ }+ entry, err := decodeEntry([]byte(line))+ if err != nil {+ return nil+ }+ if entry.SessionMeta != nil {+ return entry.SessionMeta+ }+ return nil+ }+ return nil+}++func sessionIDFromPath(path string) string {+ base := strings.TrimSuffix(strings.TrimSuffix(filepath.Base(path), ".jsonl"), ".json")+ if strings.HasPrefix(base, "rollout-") {+ parts := strings.Split(base, "-")+ if len(parts) >= 7 {+ return strings.Join(parts[len(parts)-5:], "-")+ }+ }+ return base+}diff --git a/cli/beacon/internal/codexsession/types.go b/cli/beacon/internal/codexsession/types.gonew file mode 100644index 00000000..3bd94c17--- /dev/null+++ b/cli/beacon/internal/codexsession/types.go@@ -0,0 +1,149 @@+// Package codexsession reads Codex CLI's local rollout session files and turns them into+// Beacon endpoint telemetry.+//+// This is an after-the-fact collection path. Codex has already committed the JSONL row before+// Beacon sees it, so every mapped event carries harness.collection_method=poll. That is+// deliberately different from Beacon's live Codex OTLP path, which observes Codex logs and traces+// as the runtime emits them.+package codexsession++import (+ "encoding/json"+ "fmt"+)++const Harness = "codex_cli"++type Entry struct {+ Type string `json:"type"`+ Timestamp string `json:"timestamp,omitempty"`+ Payload json.RawMessage `json:"payload,omitempty"`++ SessionMeta *SessionMeta+ TurnContext *TurnContext+ ResponseItem *ResponseItem+ EventMessage *EventMessage+ TokenUsageRecord *TokenUsageRecord+}++type Record struct {+ Line int+ Entry Entry+}++type SessionMeta struct {+ SessionID string `json:"session_id,omitempty"`+ ID string `json:"id,omitempty"`+ Timestamp string `json:"timestamp,omitempty"`+ CWD string `json:"cwd,omitempty"`+ Originator string `json:"originator,omitempty"`+ CLIVersion string `json:"cli_version,omitempty"`+ Source string `json:"source,omitempty"`+ ThreadSource string `json:"thread_source,omitempty"`+ ModelProvider string `json:"model_provider,omitempty"`+ Git map[string]interface{} `json:"git,omitempty"`+}++type TurnContext struct {+ TurnID string `json:"turn_id,omitempty"`+ RootTurnID string `json:"root_turn_id,omitempty"`+ CWD string `json:"cwd,omitempty"`+ WorkspaceRoots []string `json:"workspace_roots,omitempty"`+ Model string `json:"model,omitempty"`+ Effort string `json:"effort,omitempty"`+ ApprovalPolicy string `json:"approval_policy,omitempty"`+ SandboxPolicy map[string]interface{} `json:"sandbox_policy,omitempty"`+}++type ResponseItem struct {+ Type string `json:"type"`+ ID string `json:"id,omitempty"`+ Role string `json:"role,omitempty"`+ Content interface{} `json:"content,omitempty"`+ Status string `json:"status,omitempty"`+ CallID string `json:"call_id,omitempty"`+ Name string `json:"name,omitempty"`+ Input interface{} `json:"input,omitempty"`+ Output interface{} `json:"output,omitempty"`+}++type EventMessage struct {+ Type string `json:"type"`+ ThreadID string `json:"thread_id,omitempty"`+ TurnID string `json:"turn_id,omitempty"`+ StartedAt int64 `json:"started_at,omitempty"`+ CompletedAt int64 `json:"completed_at,omitempty"`+ DurationMS int64 `json:"duration_ms,omitempty"`+ TimeToFirstTokenMS int64 `json:"time_to_first_token_ms,omitempty"`+ LastAgentMessage string `json:"last_agent_message,omitempty"`+ Info *TokenCountInfo `json:"info,omitempty"`+}++type TokenCountInfo struct {+ TotalTokenUsage *TokenUsage `json:"total_token_usage,omitempty"`+ LastTokenUsage *TokenUsage `json:"last_token_usage,omitempty"`+ ContextWindow int64 `json:"model_context_window,omitempty"`+}++type TokenUsageRecord struct {+ ThreadID string `json:"thread_id,omitempty"`+ TurnID string `json:"turn_id,omitempty"`+ SessionID string `json:"session_id,omitempty"`+ RootTurnID string `json:"root_turn_id,omitempty"`+ ResponseID string `json:"response_id,omitempty"`+ Usage *TokenUsage `json:"usage,omitempty"`+ TurnTokenUsage *TokenUsage `json:"turn_token_usage,omitempty"`+ ThreadTokenUsage *TokenUsage `json:"thread_token_usage,omitempty"`+}++type TokenUsage struct {+ InputTokens int64 `json:"input_tokens,omitempty"`+ CachedInputTokens int64 `json:"cached_input_tokens,omitempty"`+ CacheWriteInputTokens int64 `json:"cache_write_input_tokens,omitempty"`+ OutputTokens int64 `json:"output_tokens,omitempty"`+ ReasoningOutputTokens int64 `json:"reasoning_output_tokens,omitempty"`+ TotalTokens int64 `json:"total_tokens,omitempty"`+}++func decodeEntry(line []byte) (*Entry, error) {+ var entry Entry+ if err := json.Unmarshal(line, &entry); err != nil {+ return nil, fmt.Errorf("codex session entry: %w", err)+ }+ if entry.Type == "" {+ return nil, fmt.Errorf("codex session entry: missing type")+ }+ switch entry.Type {+ case "session_meta":+ var payload SessionMeta+ if err := json.Unmarshal(entry.Payload, &payload); err != nil {+ return nil, fmt.Errorf("codex session_meta: %w", err)+ }+ entry.SessionMeta = &payload+ case "turn_context":+ var payload TurnContext+ if err := json.Unmarshal(entry.Payload, &payload); err != nil {+ return nil, fmt.Errorf("codex turn_context: %w", err)+ }+ entry.TurnContext = &payload+ case "response_item":+ var payload ResponseItem+ if err := json.Unmarshal(entry.Payload, &payload); err != nil {+ return nil, fmt.Errorf("codex response_item: %w", err)+ }+ entry.ResponseItem = &payload+ case "event_msg":+ var payload EventMessage+ if err := json.Unmarshal(entry.Payload, &payload); err != nil {+ return nil, fmt.Errorf("codex event_msg: %w", err)+ }+ entry.EventMessage = &payload+ case "token_usage_record":+ var payload TokenUsageRecord+ if err := json.Unmarshal(entry.Payload, &payload); err != nil {+ return nil, fmt.Errorf("codex token_usage_record: %w", err)+ }+ entry.TokenUsageRecord = &payload+ }+ return &entry, nil+}