Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Set prompt.id in the Claude Code and Codex session backfills

Claude8db14ac580f3
diff --git a/cli/beacon/internal/claudesession/mapper.go b/cli/beacon/internal/claudesession/mapper.go
index ab83df16..12f307dd 100644
--- a/cli/beacon/internal/claudesession/mapper.go
+++ b/cli/beacon/internal/claudesession/mapper.go
@@ -45,6 +45,11 @@ type mapper struct {
out []MappedEvent
tools map[string]toolCall
usage map[int]usagePlan
+ // promptID is the prompt the records being read belong to. Claude Code stamps promptId on a
+ // prompt and on every tool result that follows it, but not on assistant entries, which belong
+ // to the same prompt by their place in the file. The whole file is read on every sync, so the
+ // value is right even for the first record past the cursor.
+ promptID string
}
func MapSession(ref SessionRef, records []Record, opts MapOptions) []MappedEvent {
@@ -62,6 +67,9 @@ func (m *mapper) consume(record Record) {
if emit && !m.opts.SkipSessionStarted && len(m.out) == 0 && entry.SessionID != "" {
m.emitSessionStarted(record)
}
+ if id := strings.TrimSpace(entry.PromptID); id != "" {
+ m.promptID = id
+ }
switch entry.Type {
case "user":
@@ -170,7 +178,7 @@ func (m *mapper) emitSessionStarted(record Record) {
func (m *mapper) emitPrompt(record Record, text string) {
ev := m.base(record, "prompt.submitted", "prompt", schema.SeverityInfo, schema.FidelityObserved, "Prompt submitted to Claude Code")
- ev.Prompt = &schema.PromptInfo{Text: text}
+ ev.Prompt = &schema.PromptInfo{ID: m.promptID, 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)
@@ -306,6 +314,7 @@ func (m *mapper) base(record Record, action, category string, severity schema.Se
if entry.IsSidechain || m.ref.IsSidechain {
ev.GenAI = mergeGenAI(ev.GenAI, &schema.GenAIInfo{Agent: m.agentInfo(entry)})
}
+ asymptoteobserve.SetPromptID(&ev, m.promptID)
return ev
}
diff --git a/cli/beacon/internal/claudesession/prompt_id_test.go b/cli/beacon/internal/claudesession/prompt_id_test.go
new file mode 100644
index 00000000..1be6323e
--- /dev/null
+++ b/cli/beacon/internal/claudesession/prompt_id_test.go
@@ -0,0 +1,71 @@
+package claudesession
+
+import (
+ "strings"
+ "testing"
+)
+
+// withPromptID stamps promptId on a transcript line the way Claude Code does on prompts and tool
+// results.
+func withPromptID(line, promptID string) string {
+ return strings.Replace(line, `{"parentUuid"`, `{"promptId":`+quote(promptID)+`,"parentUuid"`, 1)
+}
+
+// Claude Code stamps promptId on the prompt and its tool results but not on assistant entries.
+// Every event a prompt led to -- the assistant's message, its tool call, the usage it reported,
+// the command's result -- carries that prompt's id, and the next prompt starts a new one.
+func TestMappedEventsCarryTheirPromptsID(t *testing.T) {
+ ref := SessionRef{ID: "sess-1", Path: "/tmp/sess-1.jsonl", ProjectPath: "/tmp/repo"}
+ records := decodeFixture(t, []string{
+ withPromptID(userLine("sess-1", "u1", "add a health endpoint"), "p-1"),
+ assistantLine("sess-1", "a1"),
+ withPromptID(toolResultLine("sess-1", "r1", "toolu_bash", "ok", false), "p-1"),
+ withPromptID(userLine("sess-1", "u2", "now document it"), "p-2"),
+ })
+
+ mapped := MapSession(ref, records, MapOptions{})
+ var got []string
+ for _, item := range mapped {
+ id := ""
+ if item.Event.Prompt != nil {
+ id = item.Event.Prompt.ID
+ }
+ got = append(got, item.Event.Event.Action+"="+id)
+ }
+ want := []string{
+ "session.started=",
+ "prompt.submitted=p-1",
+ "tool.invoked=p-1",
+ "agent.message=p-1",
+ "token.usage=p-1",
+ "command.executed=p-1",
+ "prompt.submitted=p-2",
+ }
+ if strings.Join(got, ",") != strings.Join(want, ",") {
+ t.Fatalf("prompt ids = %v, want %v", got, want)
+ }
+ prompt := findAction(t, mapped, "prompt.submitted")
+ if prompt.Prompt.Text != "add a health endpoint" {
+ t.Fatalf("prompt text = %q, want it beside the id", prompt.Prompt.Text)
+ }
+}
+
+// A sync reads the whole file and emits only past its cursor, so the first records past the cursor
+// still learn which prompt they belong to from the records before it.
+func TestIncrementalMapKeepsThePromptFromBeforeTheCursor(t *testing.T) {
+ ref := SessionRef{ID: "sess-1", Path: "/tmp/sess-1.jsonl", ProjectPath: "/tmp/repo"}
+ records := decodeFixture(t, []string{
+ withPromptID(userLine("sess-1", "u1", "add a health endpoint"), "p-1"),
+ assistantLine("sess-1", "a1"),
+ })
+
+ mapped := MapSession(ref, records, MapOptions{MinLine: 1, SkipSessionStarted: true})
+ if len(mapped) == 0 {
+ t.Fatal("no events mapped past the cursor")
+ }
+ for _, item := range mapped {
+ if item.Event.Prompt == nil || item.Event.Prompt.ID != "p-1" {
+ t.Fatalf("%s prompt = %#v, want p-1", item.Event.Event.Action, item.Event.Prompt)
+ }
+ }
+}
diff --git a/cli/beacon/internal/codexsession/mapper.go b/cli/beacon/internal/codexsession/mapper.go
index 50ec8cac..fe8963f0 100644
--- a/cli/beacon/internal/codexsession/mapper.go
+++ b/cli/beacon/internal/codexsession/mapper.go
@@ -290,7 +290,7 @@ func (m *mapper) consumeTokenUsageRecord(record Record, emit bool) {
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.Prompt = &schema.PromptInfo{ID: m.turnID, 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)
@@ -368,6 +368,10 @@ func (m *mapper) base(record Record, action, category string, severity schema.Se
if m.model != "" {
ev.Model = m.model
}
+ // Codex brackets each turn with TurnStarted and TurnComplete, both carrying turn_id, and
+ // records the user's message inside the bracket, so the turn being read is the prompt every
+ // event here belongs to.
+ asymptoteobserve.SetPromptID(&ev, m.turnID)
return ev
}
diff --git a/cli/beacon/internal/codexsession/prompt_id_test.go b/cli/beacon/internal/codexsession/prompt_id_test.go
new file mode 100644
index 00000000..10a28e38
--- /dev/null
+++ b/cli/beacon/internal/codexsession/prompt_id_test.go
@@ -0,0 +1,64 @@
+package codexsession
+
+import (
+ "strings"
+ "testing"
+)
+
+func turnStartedLine(turnID string) string {
+ return `{"timestamp":"2026-09-19T22:00:01.000Z","type":"event_msg","payload":{"type":"task_started","turn_id":` + quote(turnID) + `}}`
+}
+
+// Codex brackets every turn with TurnStarted and TurnComplete and records the user's message inside
+// the bracket. Every event of a turn carries its turn_id as prompt.id, and the next turn starts a
+// new one.
+func TestMappedEventsCarryTheirTurnAsPromptID(t *testing.T) {
+ ref := SessionRef{ID: "codex-1", Path: "/tmp/codex-1.jsonl", Workspace: "/tmp/repo"}
+ records := decodeFixture(t, []string{
+ sessionMetaLine("codex-1", "/tmp/repo"),
+ turnStartedLine("turn-1"),
+ turnContextLine("turn-1", "gpt-5.3-codex"),
+ messageLine("user", "u1", "run the tests"),
+ toolCallLine("call_1", "shell", `{"command":"go test ./..."}`),
+ toolOutputLine("call_1", "ok", "completed"),
+ messageLine("assistant", "a1", "Tests pass."),
+ taskCompleteLine("turn-1"),
+ turnStartedLine("turn-2"),
+ turnContextLine("turn-2", "gpt-5.3-codex"),
+ messageLine("user", "u2", "now commit"),
+ })
+
+ mapped := MapSession(ref, records, MapOptions{})
+ var got []string
+ for _, item := range mapped {
+ id := ""
+ if item.Event.Prompt != nil {
+ id = item.Event.Prompt.ID
+ }
+ got = append(got, item.Event.Event.Action+"="+id)
+ }
+ for i, entry := range got {
+ action, id, _ := strings.Cut(entry, "=")
+ switch {
+ case action == "session.started":
+ if id != "" {
+ t.Fatalf("session.started prompt.id = %q, want none: %v", id, got)
+ }
+ case i == len(got)-1:
+ if action != "prompt.submitted" || id != "turn-2" {
+ t.Fatalf("last event = %s, want the second prompt under turn-2: %v", entry, got)
+ }
+ default:
+ if id != "turn-1" {
+ t.Fatalf("%s prompt.id = %q, want turn-1: %v", action, id, got)
+ }
+ }
+ }
+ if len(got) < 5 {
+ t.Fatalf("mapped events = %v, want the turn's prompt, tool, message and completion", got)
+ }
+ prompt := findAction(t, mapped, "prompt.submitted")
+ if prompt.Prompt.Text != "run the tests" {
+ t.Fatalf("prompt text = %q, want it beside the id", prompt.Prompt.Text)
+ }
+}
diff --git a/docs/runtimes/claude-code.mdx b/docs/runtimes/claude-code.mdx
index 4c716b39..2e85c51c 100644
--- a/docs/runtimes/claude-code.mdx
+++ b/docs/runtimes/claude-code.mdx
@@ -75,7 +75,7 @@ Beacon detects Claude Code through the `claude` executable and Claude settings p
| Area | Support |
|------|---------|
| Prompt telemetry | Supported when emitted through Claude Code OTLP or hooks |
-| Turn identity | Supported. Claude Code's `prompt.id` is kept as `prompt.id` on OTLP events, and hook events after the prompt read the same id from the session transcript. The hook-recorded prompt itself takes its id from the OTLP `user_prompt` event, because the hook runs before Claude Code records the prompt |
+| Turn identity | Supported. Claude Code's `prompt.id` is kept as `prompt.id` on OTLP events; hook events after the prompt read the same id from the session transcript, and `beacon endpoint claude sync` reads it from `promptId`. The hook-recorded prompt itself takes its id from the OTLP `user_prompt` event, because the hook runs before Claude Code records the prompt |
| Command, tool, and file telemetry | Supported through Claude Code OTLP with detailed tool inputs enabled |
| Token usage and cost | Supported through OTLP token and cost metrics, with per-model and per-session attribution |
| Hook lifecycle and tool telemetry | Optional through `beacon endpoint hooks install --harness claude` |
diff --git a/docs/runtimes/codex-cli.mdx b/docs/runtimes/codex-cli.mdx
index 2ed44b06..7a348649 100644
--- a/docs/runtimes/codex-cli.mdx
+++ b/docs/runtimes/codex-cli.mdx
@@ -106,7 +106,7 @@ Codex CLI endpoint hooks installed: /Users/you/.codex/hooks.json
| Area | Support |
|------|---------|
| Prompt telemetry | Supported when emitted through Codex semantic logs |
-| Turn identity | Supported. Codex's `turn_id` from hooks and `turn.id` from OTLP are promoted to `prompt.id` |
+| Turn identity | Supported. Codex's `turn_id` from hooks, `turn.id` from OTLP, and the turn bracket in `beacon endpoint codex sync` are promoted to `prompt.id` |
| Command, tool, and file telemetry | Supported when emitted through Codex semantic logs |
| Token usage | Supported per turn through a selective OTLP trace (input, output, cache read, cache creation, reasoning) |
| Cost attribution | Not available; Codex emits no cost signal |