Skip to content

feat: Group a session's model calls into user prompts, in the recording and the timeline #1922

Open

Description

@michaljacovi

Part of #1920.

Problem

The timeline's unit is one model call (Turn,
core/session/timeline/timeline.go:78). A user prompt usually causes many
calls: tool loops, subagents, retries. Some calls are not the user's at all:
the agent's own title, suggestion and away-summary calls show up as main.
Replay and analysis need "what did the person ask, and what did it cost",
plus the user's think time between prompts. Nothing in the archive or the
timeline groups calls that way today.

Proposal

We have the heuristic measured on real sessions: derived think time matched
the agent's own telemetry within 0.4 s on 13 of 13 gaps.

Open questions

  • The name. "Turn" is taken. We use "prompt" here.
  • Is the grouping computed on demand, like the timeline, or stored with the
    session?
  • Agents other than Claude Code: the same rules per agent, or Claude Code
    first?

Out of scope

  • Changing what Turn means.
  • Agents whose request bodies we have not checked.

Assisted-By: Claude (Anthropic AI) [email protected]

Activity

  1. self-assigned this
    on Nov 17, 2026
  2. added
    enhancement
    on Nov 17, 2026
  3. michaljacovi commented on Nov 20, 2026

    @michaljacovi
    ContributorAuthor

    Design before the first PR

    A prompt is the group of model calls caused by one human instruction: the tool loop, the subagents and the retries until the agent answers. Cortex's timeline Turn is one model call (core/session/timeline/timeline.go:78), so this note says "prompt". The name is open (§7).

    1. What we already know (measured, in our own recorder)

    • The text is in the bodies. Claude Code resends the whole conversation on each call, so the human's text is in the request bodies the proxy sees.
    • A body-derived reconstruction works. On our own recorded Claude Code sessions, the rule below recovered about 96% of the turns that Claude Code's own telemetry records (claude_code.interaction). Most of the misses were correctly not human: slash commands with no model call (/clear, /exit), and task notifications.
    • Think time (end of the previous prompt's last call to the start of the next prompt's first call) matched Claude Code's claude_code.interaction timing within 0.4 s on 13 of 13 gaps. Prompt duration matched within 0.4 s on 14 of 15. That's a small sample, so the parity test below re-checks it.
    • What the bodies can't give: the human time inside a prompt (a permission wait vs tool runtime), and permission decisions. Those need telemetry (feat: Add Claude Code telemetry to a recorded session: tool times, permission waits, call labels, retries #1924).
    • We have a working version of this rule in Go from an earlier recorder of ours; it's what this note ports, plus the fixes below.

    2. The heuristic (Claude Code)

    Input: the timeline's main-agent calls, in order (agentRole == main), with Cortex's existing filters already applied. Progress labels (progressPrompt) and away recaps (recapPrompt) are already told apart in timeline/agents.go, and Resent already marks retries.

    Rule. A new prompt starts at a main-agent request whose newest user message holds new human text: text that no earlier request of the session carried at that position. It does NOT start for:

    • a tool result: a user message made only of tool_result blocks, images included. An image returned by Read is a tool result, not a caption;

    • injected look-alikes:

      • <system-reminder> blocks;
      • command scaffolding (<command-name>, <command-message>, <local-command-stdout>);
      • <task-notification> and <agent-message …>;
      • the away recap, [SUGGESTION MODE, the progress label, and the "new conversation topic" title probe;
      • skill and tool documents that Claude Code inlines.

      A message holding only these is no prompt. A message holding these and human text is one, judged on the text left after removing them;

    • a resend (Resent): the same conversation again;

    • side calls: title generation, suggestion mode and the topic probe. They come from the same agent, but their own request is not the main conversation. Detect them by their last user message, as progressPrompt is.

    Compaction. After a compaction the first request starts with the summary (isCompactSummary in the transcript; in the body, a first user message that is the summary). That request is not a new prompt. The prompt it serves continues.

    --resume. A resumed session's first request carries the old history. The "new text" test compares against what this recording saw. With an archive the earlier calls are there; without one, the resume's first request would look new. So: a request whose newest human text appears in earlier messages of the same request (it's history) doesn't start a prompt.

    Subagents. A subagent's calls belong to the prompt that was open when its spawning Agent tool call was made. The timeline already links them (SpawnTool).

    Think time for prompt n is start(n) − end(n−1), where the end is the last main-agent call of n−1 to finish. Away recaps and suggestion calls in between don't count; without that exclusion, 5 gaps were about 187 s short. Unset for the first prompt.

    3. Where it lives

    Computed on demand, like the timeline. Prompts are another span kind built in timeline.Build from the same events: Timeline.Prompts []Prompt, omitted when empty so an older agentop is unaffected. Not stored. Reasons:

    • the rules will change (each new injected marker is a rule change), and stored prompts would go stale;
    • the archive's fold already replays history;
    • the timeline is cached per session.
    type Prompt struct {
        Index     int        `json:"index"`             // 1-based within the session
        Start     time.Time  `json:"start"`             // first call's request
        End       *time.Time `json:"end,omitempty"`     // last call's response; nil while running
        FirstSeq  uint64     `json:"firstSeq"`
        LastSeq   uint64     `json:"lastSeq,omitempty"`
        ThinkMs   int64      `json:"thinkMs,omitempty"` // gap since the previous prompt ended
        Source    string     `json:"source"`            // "derived" | "telemetry"
    }

    4. With telemetry (#1924)

    When Claude Code's claude_code.interaction span is present for the session, it wins:

    • Boundaries and duration come from the span (interaction.sequence, duration_ms), with Source: "telemetry".
    • Calls are assigned to it by time: a call whose request falls inside the span.
    • The heuristic still runs, for prompts whose spans were lost (span pipelines can drop under load; we've seen it), and to cross-check. A disagreement is a test signal, not shown to users.
    • Human time inside a prompt (tool.blocked_on_user) can then be subtracted, which the bodies alone can't do.

    5. Tests

    • Golden sessions: real Claude Code bodies, already in core/plugins/inferenceparser/testdata/claude-code/…, plus new short fixtures. Each needs the expected prompt boundaries and think time. Cases:
      • a tool loop;
      • a subagent;
      • a retry;
      • an away recap and a suggestion call between prompts;
      • a compaction;
      • a --resume;
      • an image returned by Read;
      • a <task-notification>;
      • a slash command (no prompt);
      • a message that is both a system reminder and human text.
    • Rule tests for each injected marker: alone (no prompt) and mixed with text (a prompt).
    • Think-time tests: gaps that contain a recap or suggestion call.
    • Parity (dev-only, not CI): the 13-gap measurement, re-run against a dual-recorded session: within 0.4 s.
    • A shape test: Prompts is omitted when empty, and an older agentop decodes the timeline unchanged.

    6. PR split (small)

    1. PR A: timeline.Prompts: the heuristic for Claude Code, think time, and the golden and rule tests. Docs in session-api.md (timeline section). No UI, no export.
    2. PR B: agentop shows prompts above the turns, with think time.
    3. With feat: Export a session with its content, in replay formats #1925: promptIndex on exported calls.
    4. With feat: Add Claude Code telemetry to a recorded session: tool times, permission waits, call labels, retries #1924: telemetry-sourced prompts (Source: "telemetry") and human time inside a prompt.
    5. Later: other agents, each with its own injected markers (Codex, OpenCode, Pi, Bob), checked against real bodies first.

    7. Open questions

    1. Name: Cortex's Turn is one model call. We need a name for "everything from one Enter to the next". "Prompt", or "Interaction" (Claude Code's span name)?
    2. Where: a layer in /timeline above the turns (proposed), or a route of its own?
    3. Computed or stored: derived when the timeline is built (proposed; one cheap pass, and old sessions benefit when the rules improve), or stored with the session?
    4. Text preview: show a short preview of what the user typed, like the session title, or only the prompt number? It's content, so a privacy call.
    5. Content-free export (feature: export session events for post-processing — content-free per-event projection, NDJSON sink, archive-replayable #1810): may it carry the prompt number and think time? No content, but they describe how the person worked.
    6. Other agents: Claude Code first (proposed), or a per-agent rule table from the start?
    7. Filter rules: the title fold (session/fold.go) already skips the same scaffolding. Share one list with timeline/agents.go, or keep two?

    Comments welcome, especially on the open questions in section 7.

    Assisted-By: Claude (Anthropic AI) [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions