Skip to content

feat(roadmap): run records give the run, step and path class of tool calls #5460

Open

Description

@dan-petty

Problem or Motivation

A review of the start context of the roadmap jobs (2026-11-13) could not measure most of its proposals for each run, because the records do not split the calls or the tokens by run, by step or by kind:

  • The stage comment lists RunTrace.combined over all planner and review runs of an item. So nobody can split the calls into the first run, a retry after a gate return, and a review run.
  • fix-plan, refine and alternatives write no list of their tool calls.
  • The runner logs no line at the start of a step. Its read_file, grep, list_files and edit_file lines log no path (fix(roadmap): an implement step that stops on UnexpectedModelBehavior hides the cause and logs no tool call #2758). Since 2026-11-12 04:00Z, the 10 runner runs logged 288 read_file lines and 90 grep lines with no path class. So repeat reads, and reads of rule documents or tasks.md through read_file, are not visible.
  • devops_cli_ai_tokens_total has the types prompt, completion and reasoning only (src/devops_cli/ai/spend/ledger.py). The ledger receives cache_read_tokens, but the counter does not emit them. The cache share of a row is estimated from the spend divided by the price.
  • A runner run that stops at a limit logs limit_reached with no limit class. It occurred 16 times in 7 days, and each stopped run cost about 2.9M input tokens.
  • No record gives the input of the first request of a run.

Proposed Solution & Architectural Design

  1. Stage and fix-plan: add the run index and the row (plan or review) to each ToolCallRecord (src/devops_cli/roadmap/agent_run.py). The stage comment groups the calls by run. Fix-plan writes the same counts on its result line in Loki, not in a pull request comment, because a plan pull request can merge minutes after it opens.
  2. Refine and alternatives: the result line gives the count of tool calls for each tool name and each run index.
  3. Implement:
    • Log one step start line with the group number and no other content.
    • Each read line (read_file, grep, list_files, edit_file) gives a path class (named file, test file, rule document, change folder, other) and a keyed digest of the path.
    • Make a new random key for each run. Never store or log the key, because the paths of a public repository can be listed.
    • The stop line gives the limit class: refusals, turns, input, time.
  4. Add the type cache_read to devops_cli_ai_tokens_total, by row, from the cache_read_tokens that the ledger already receives. feat(ai): cache Claude prompts and price the cache tokens #3658 step 3 adds cache read and cache write counts to the ledger records for Claude. Do the counter part here, and do not change the ledger records.
  5. Each agent run records the input count of its first request.

All records hold names, counts, classes and sizes only, as the telemetry privacy defaults require. No prompt or tool content goes into spans or logs.

Target Subsystem

SDLC, Git & PR Governance: src/devops_cli/roadmap/agent_run.py, src/devops_cli/roadmap/stage/, src/devops_cli/roadmap/steps/fix_plan.py, src/devops_cli/roadmap/refine/, src/devops_cli/roadmap/alternatives.py, src/devops_cli/roadmap/implement/, src/devops_cli/ai/spend/ledger.py, src/devops_cli/roadmap/job_result.py.

Existing Tool Considered

OpenTelemetry already carries the counters and the Loki lines; this change adds labels and fields, not a tool. pydantic-ai gives the usage of each request (cache_read_tokens, input_tokens), so no count is computed by hand. hmac and secrets of the standard library give the keyed digest. Cost to build: about 1.5M tokens and 4 hours (estimate), about 150 lines and 8 tests. Keep cost: low, the fields follow the existing record types.

Acceptance Criteria

  • Test first: a stub stage item with one gate return gives tool records with run indexes 1 and 2 and the row of each run, and the stage comment groups them by run.
  • Test first: two runner runs that read the same path log the same path class and different digests. No log line holds the key or the path.
  • Test first: a request with cache-read input emits devops_cli_ai_tokens_total{type="cache_read"} with its row.
  • A test: the fix-plan, refine and alternatives result lines hold the tool counts and no tool argument.
  • Measure: the next run of the start-context review gives, for each run, the first 10 calls by tool, the calls of retry runs, the repeat reads, and the cache-read share of each row.
  • Live proof after release: on real Service runs, one Prometheus query gives the cache-read share of the rows stage and refine, and the count of step start lines of each implement run equals its count of groups.

Dependencies

None. #4385, #4386 and #5168 use these records. #3658 changes the ledger records.

Standards Compliance

  • I confirm this proposal aligns with AGENTS.md engineering principles (cyclomatic complexity <= 10, nesting <= 4 blocks, zero bare exceptions, tests that cover the changed lines).

Alternatives check of 2026-11-23: none.
Tool: none found.
Reason: Searches found only general LLM observability platforms (Langfuse) and OpenTelemetry collector/operator images; none adds a run index and row to the project's tool-call records, path class with keyed digest, limit class, or cache_read series, so the need is internal instrumentation.

Lacks:

  • Run index and row on each ToolCallRecord
  • Path class with per-run keyed digest in read log lines
  • limit_class on the limit_reached line
  • cache_read type on the existing tokens counter
  • First-request input count on the agent run record

Proposed design

Problem statement

Run records do not split tool calls or tokens by run, step or path class. A review cannot see retries, repeat reads, cache-read share or the first request input. The change adds those fields and labels to the existing records and counter. It defers the post-release measurement to a new issue.

Acceptance criteria

Requirement: Run index and row on tool calls

Each ToolCallRecord SHALL hold the run index and the row of its tool call.

Scenario: A plan retry and a review run

  • WHEN A stub stage item runs one plan run, returns at a gate, runs a second plan run and then a review run.
  • THEN The tool records have run indexes 1, 2 and 3, and their rows are plan, plan and review.

Requirement: Stage comment groups by run

The stage comment SHALL group the tool calls by run index and name each group's row.

Scenario: Two runs in one stage comment

  • WHEN A stage item has tool calls in run 1 of row plan and run 2 of row review.
  • THEN The rendered stage comment has one group for run 1 and one for run 2, each group names its row, and each call is under its run only.

Requirement: Fix-plan result line counts

The fix-plan result line SHALL give the tool-call count for each tool name and each run index.

Scenario: Fix-plan with two runs

  • WHEN A fix-plan run records read_file and grep calls in two runs.
  • THEN Its JSON result line holds the counts keyed by run index and tool name, holds no tool argument, and writes no pull request comment.

Requirement: Refine and alternatives result line counts

The refine and alternatives result lines SHALL give the tool-call count for each tool name and each run index.

Scenario: Refine and alternatives runs

  • WHEN A refine run and an alternatives run each call two tools over two runs.
  • THEN Each result line holds the counts keyed by run index and tool name and holds no tool argument.

Requirement: Step start line

The runner SHALL log one step start line at the start of each step with the group number only.

Scenario: Two task groups

  • WHEN An implement run starts two task groups.
  • THEN The log has two step start lines, each holds one group number, and no line holds prompt or tool content.

Requirement: Path class and keyed digest

Each read_file, grep, list_files and edit_file log line SHALL give a path class and a keyed digest of the path.

Scenario: Same path in two runs

  • WHEN Two runner runs with different per-run keys read src/app.py.
  • THEN Both lines give the class named file, the digests differ, and neither line holds the path or the key.

Requirement: Path class set

The path class SHALL be one of named file, test file, rule document, change folder or other.

Scenario: One path of each class

  • WHEN The path classifier receives src/app.py, tests/test_app.py, AGENTS.md, openspec/changes/5460-run-records/tasks.md and src/.
  • THEN The lines give named file, test file, rule document, change folder and other, in that order.

Requirement: Stop line limit class

The stop line SHALL give the limit class as refusals, turns, input or time.

Scenario: Each limit class

  • WHEN A runner stops at a refusal, a turn limit, an input limit and a time limit.
  • THEN Each limit_reached line names the matching class and holds no prompt or tool content.

Requirement: Cache-read counter series

The devops_cli_ai_tokens_total metric SHALL emit a type="cache_read" series from the ledger cache_read_tokens of each row.

Scenario: A ledger row with cache-read tokens

  • WHEN A spend record with cache_read_tokens=40 and row stage is exported.
  • THEN The metric holds a devops_cli_ai_tokens_total sample with type="cache_read", a row label value of stage, and value 40, and the ledger record fields do not change.

Requirement: First request input count

Each agent run SHALL record the input token count of its first request.

Scenario: First request with cache-read input

  • WHEN The first request of a run reports input_tokens=100, cache_read_tokens=40 and cache_write_tokens=10.
  • THEN The run record holds first_request_input_tokens=150, and a later request does not change it.

Requirement: Records hold no content

The new records SHALL hold names, counts, classes and sizes only and no prompt or tool content.

Scenario: A prompt and a file content

  • WHEN A runner logs a step start, a read line and a stop line while the prompt holds PRIVATE_PROMPT and the file holds PRIVATE_FILE.
  • THEN No new log line or result line holds PRIVATE_PROMPT, PRIVATE_FILE or a tool argument.

Existing Tool Considered

No open, maintained tool covers the need. The alternatives check of 2026-11-17 in the issue body gives verdict none and lists what is missing: no tool adds a run index and row to ToolCallRecord, writes tool-call counts on the result lines, emits step start, a path class with a keyed digest or a limit class, or adds a cache_read type to the project counter. OpenTelemetry already carries the counters and the Loki lines. pydantic-ai supplies request usage, and hmac and secrets supply the digest, but these are libraries already in use, not a covering tool. Build cost from the issue: about 1.5M tokens and 4 hours, about 150 lines and 8 tests. Keep cost is low.

Key questions

  • Does the ledger already hold cache-read token counts?: Yes. SpendLedger.record_request accepts cache_read_tokens and cache_write_tokens and writes both columns. The missing part is the cache_read series on the counter. [sources: src/devops_cli/ai/spend/ledger.py, tests/ai/spend/test_ai_spend_ledger_cache_tokens.py]
  • How should each tool call record carry its run and row?: Add run_index and row to ToolCallRecord. Use run_index=1 and row="plan" as defaults so older records still load. (best practice) [sources: src/devops_cli/roadmap/agent_run.py:219, tests/ai/client/test_roadmap_agent_run.py]
    • Chosen: Add fields to ToolCallRecord. One record type serves the stage comment and the result lines. Defaults keep older records readable.
    • Rejected: Derive run and row from log context. The context is absent after the run, so the result lines cannot group the calls.
    • Rejected: Use a separate side table keyed by call id. This adds a second record and a join with no benefit.
  • How should the stage comment show the calls of each run?: Render one group for each run_index in order. The group heading names the row and run index. Each call appears under its run only. (best practice) [sources: tests/roadmap/stage/test_roadmap_stage_trace_runs.py]
    • Chosen: Group by run index in one comment. This meets the split into the first run, a retry and a review run.
    • Rejected: Keep one combined list with a run column. The reader still cannot compare the runs, and the issue asks for groups.
    • Rejected: Write one comment per run. This adds several comments, but the issue names the stage comment.
  • How should a read log line name a path without leaking it?: Use a closed classifier: test file under tests/; rule document for AGENTS.md, CONTEXT.md and docs/guidelines/; change folder under openspec/changes/; named file for another path with a file suffix; other otherwise. Compute hmac.new(run_key, path.encode(), sha256).hexdigest()[:16] with a secrets.token_bytes(32) key for each run. (best practice) [sources: src/devops_cli/roadmap/agent_run.py]
    • Chosen: HMAC-SHA-256 digest with a per-run key. The same path gives different digests across runs. The log holds no path and no key. The standard library supplies both parts.
    • Rejected: Plain SHA-256 digest. A public repository path can be listed, so a dictionary attack reveals the path.
    • Rejected: Log the path. This leaks repository content and breaks the telemetry privacy defaults.
  • What should the step start line hold?: Log step start group=<n> once at the start of each step. It holds the group number only, with no prompt, tool argument or path. (best practice) [sources: src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/job_result.py]
    • Chosen: One key-value line. Loki can parse it, and it holds no content beyond the group number.
    • Rejected: A JSON object. This adds fields that the issue does not ask for.
    • Rejected: Log at the end of the step. The start time is needed to measure the step.
  • How should the stop line name the limit class?: Map the existing stop reason to refusals, turns, input or time, and write limit_class=<class> on the limit_reached line. Keep the mapping in implement_limits.py next to the limit definitions. (best practice) [sources: src/devops_cli/roadmap/implement/implement_limits.py, tests/roadmap/implement/test_roadmap_implement_limits.py]
    • Chosen: Closed mapping in implement_limits.py. One place holds the mapping, and a test can check each class.
    • Rejected: Infer the class from free text at log time. Free text is brittle and gives no stable class.
    • Rejected: Add a fifth other class. This hides an unmapped limit, but a test must fail on an unknown reason.
  • How should the cache-read tokens reach devops_cli_ai_tokens_total?: Add cache_read to the counter's type label. Read SpendRecord.cache_read_tokens for each row. Keep prompt as it is, so cache-read tokens are not counted twice. Do not change the ledger schema. (best practice) [sources: src/devops_cli/ai/spend/ledger.py, src/devops_cli/ai/spend/prometheus.py, tests/ai/spend/test_ai_spend_prometheus.py]
    • Chosen: Add a cache_read type from the existing ledger field. The ledger already stores the count, so the counter needs no schema change.
    • Rejected: Add cache read to prompt. This hides the cache share and double counts if prompt already holds it.
    • Rejected: Compute cache read from spend and price. This gives an estimate, not a measured count.
  • Which token count should the run record for its first request?: Record first_request_input_tokens from the first Usage as input_tokens + cache_read_tokens + cache_write_tokens. Set it once, before later requests. The field holds a count only. (best practice) [sources: src/devops_cli/ai/client/models.py:286, src/devops_cli/ai/spend/ledger.py]
    • Chosen: Sum the three input fields. This gives the total prompt size, including cache input. pydantic-ai keeps the cache fields separate.
    • Rejected: Record input_tokens alone. This misses the cache-read input that the issue names.
    • Rejected: Record each field in a nested object. This adds more data than the issue asks for. One count is enough.
  • Can the post-release measurement and live proof be delivered by the implementation pull request?: No. They wait on a release and on real Service runs. Defer them to a new issue with the query and the thresholds. The implementation pull request delivers the code and the offline tests. (best practice) [sources: CONTEXT.md, src/devops_cli/roadmap/job_result.py]
    • Chosen: Defer the post-release measurement. A pull request cannot prove real runs before a release and deployment.
    • Rejected: Keep it in the acceptance criteria. The item cannot be Ready until after deployment, so it blocks the merge.
    • Rejected: Drop it. This loses the proof that the change works on real runs.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/feature
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: none

    Reason: The searches turned up only general agent-observability platforms (Langfuse) and unrelated pages; Langfuse traces tool calls and token usage but is a separate self-hosted platform that would not add a run index, path class, limit class or a cache_read series to the project's own records. The issue is a schema change to the project's own record types and counter, whose data sources it already names (pydantic-ai usage, stdlib hmac/secrets), so no external tool covers it.

    Tool: none found

    Lacks:

    • No tool adds a run index and row (plan or review) to the project's ToolCallRecord or groups the stage comment by run
    • No tool writes the tool-call counts of the fix-plan, refine and alternatives result lines
    • No tool emits a step start line, a path class with a keyed digest, or a limit class in the runner's log lines
    • No tool adds a cache_read type to the project's own devops_cli_ai_tokens_total counter or records the input count of the first request of each run

    Tools considered:

    Searches run:

    • open source LLM observability tool record tool calls per run token usage self-hosted, results: 5
    • OpenTelemetry GenAI semantic conventions cache read tokens token usage metric, results: 5
    • OpenLLMetry Traceloop OpenTelemetry instrumentation LLM tool calls, results: 5
    • Langfuse trace tool calls per run token usage cache read, results: 5
    • prometheus counter token usage label type cache_read open source library, results: 5
    • pydantic-ai usage cache_read_tokens instrumentation, results: 5
    • opentelemetry-instrumentation openai instrumentation gen_ai usage attributes, results: 5
    • arize phoenix llm tracing open source, results: 5
    • langfuse, results: 5

    Sources:

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  5. added
    status/ready
    and removed
    status/new
    on Nov 18, 2026
  6. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at b466159 (see proposed design in issue body).

    Moved to new items: #6238.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/roadmapsource/agentstatus/readytype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions