Skip to content

fix(action): restore token aggregation broken by result-event filter - #266

Merged
max-sixty merged 2 commits into
mainfrom
daily/review-runs-24333446265
Apr 13, 2026
Merged

max-sixty merged 2 commits into
mainfrom
daily/review-runs-24333446265

Conversation

@tend-agent

@tend-agent tend-agent commented Apr 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Every token-usage.json since #249 reports zero tokens. Confirmed across all 61 tend CI runs in the past 24 h on this repo (12 tend-review, 19 tend-mention, 22 review-reviewers, 6 tend-notifications, 1 tend-nightly, 1 tend-weekly).

Sample artifact (run 24330280717, tend-review):

{"input_tokens":0,"output_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"turns":0,"model":"opus","cost_usd":0}

Root cause

#249 added a select(.type == "result") filter that targets the SDK's per-session result event — which carries total_cost_usd, num_turns, and a session-level usage object. That event is real, but it isn't written to the per-turn session JSONL under /home/runner/.claude/projects/; claude-code-action writes it (along with the rest of the SDK message stream) to $RUNNER_TEMP/claude-execution-output.json and exposes that path as the execution_file action output. Reading from /home/runner/.claude/projects/ therefore matched zero entries and every numeric field fell back to 0.

This was masked yesterday because the 24 h analysis window straddled the #249 merge time (2026-04-11 20:28 UTC) — most artifacts in that window pre-dated the regression. Today's window is fully post-#249, so every run reports zeros.

Fix

Read ${{ steps.claude.outputs.execution_file }} and pull total_cost_usd, num_turns, and usage.* directly from the result entry. Cost is now whatever the SDK reports (no hard-coded model rate table), so Sonnet/Haiku adopters and 1-hour cache usage are accounted for correctly.

Verified with a synthetic execution-file array containing a real result entry from run 24330280717: produces 34 input / 4954 output / 1035105 cache_read / 39191 cache_creation, 24 turns, $0.89 — matches the total_cost_usd printed in that run's job log (0.886... rounded to 2dp).

#249 switched the token-usage jq filter to `select(.type == "result")`, an
event type that claude-code-action's session JSONL does not emit. Every
`token-usage.json` since the merge has reported zero tokens — confirmed
across all 61 runs in the past 24h on this repo.

Restore the per-assistant-event aggregation from #188 and compute cost
from token totals × Claude Opus 4 list prices, since `total_cost_usd`
isn't available in this JSONL format.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@tend-agent tend-agent added the review-runs Findings from review-runs skill label Apr 13, 2026
@max-sixty

Copy link
Copy Markdown
Owner

but there is a cost field at the end of the logs! can we search for and use that?

…file

Switches the token-usage aggregation to read `$RUNNER_TEMP/claude-execution-output.json`
(exposed via `steps.claude.outputs.execution_file`) instead of summing per-turn
assistant events from the session JSONL. The SDK writes a `type: "result"`
entry at the end of that array with `total_cost_usd`, `num_turns`, and
per-session usage totals — so cost is now accurate regardless of the model
in use, and the hard-coded Opus 4 rate table goes away.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@tend-agent

Copy link
Copy Markdown
Collaborator Author

Good catch — you are right. The result event carrying total_cost_usd does exist; I was looking in the wrong file. claude-code-action writes the full SDK message stream to $RUNNER_TEMP/claude-execution-output.json (path exposed via its execution_file output), and the last entry there is the type: "result" summary with total_cost_usd, num_turns, and a session-level usage object.

Pushed 250ebe9 which switches to that source: cost comes straight from total_cost_usd, so the model-rate table goes away and Sonnet/Haiku adopters plus 1-hour-cache usage are accounted for automatically. PR description updated to match.

@max-sixty
max-sixty merged commit 5a27c18 into main Apr 13, 2026
3 checks passed
@max-sixty
max-sixty deleted the daily/review-runs-24333446265 branch April 13, 2026 14:16
max-sixty added a commit that referenced this pull request Apr 14, 2026
Bumps generator version to 0.0.12 and syncs lockfile.

50 commits since 0.0.11: notifications cost cut + freshness gate (#251, #254),
token-report per-run table and cost estimate (#249, #250), review-reviewers
outcome-focused Haiku analysis (#255), config workflow_extra/jobs pass-through
(#227), canonical author_association tiers across skills (#261, #263), action
token aggregation fix (#266), plus numerous review/mention/ci-runner fixes.

Co-Authored-By: Claude <[email protected]>
@max-sixty max-sixty mentioned this pull request Apr 14, 2026
max-sixty added a commit that referenced this pull request Apr 14, 2026
Bumps generator version to 0.0.12 and syncs lockfile.

50 commits since 0.0.11. Highlights:
- feat: notifications cost cut + freshness gate (#251, #254)
- feat: token-report per-run table + cost estimate (#249, #250)
- feat: review-reviewers outcome-focused Haiku analysis (#255)
- feat: config `workflow_extra` / `jobs` pass-through (#227)
- skills: canonical `author_association` tiers (#261, #263)
- fix: action token aggregation (#266), plus numerous
review/mention/ci-runner fixes

> _This was written by Claude Code on behalf of Maximilian_

Co-authored-by: Claude <[email protected]>
max-sixty pushed a commit that referenced this pull request Sep 21, 2026
…t marked for removal (#1361)

`claude_step` copied the raw stream-json into the session-log artifact
as `claude-stream.json`, gated to `max-sixty/tend`. Its comment named
the condition for removing it — "Drop once #302 is resolved" — and #302
was resolved on 2026-05-12 by the result-event summing in #266, which is
what the module docstring's path 1 now describes: sum `usage.*` and
`num_turns` across every `result` event, take `total_cost_usd` from the
last.

Nothing reads the copy. `claude-stream.json` is written in one place and
read in none — not by `token_report.py`, and not by
`/tend-ci-runner:read-session-logs` or `install-tend:debug-tend-run`,
which both parse the session JSONL instead.

The stream itself is unaffected: `STREAM_JSON` still feeds
`claude_usage`, and `launch_agent.py` still exports it out of the
sandbox for that. What goes is the extra artifact copy and the repo gate
that guarded it, which was the only reason `GITHUB_REPOSITORY` meant
anything on the Claude path beyond the record's own `repo` field.

No regression test — this removes an unexercised diagnostic side effect
rather than changing behavior, and the surviving path is already covered
by `shared/steps/test_token_usage.py`.

Refs #302.

---------

Co-authored-by: tend-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-runs Findings from review-runs skill

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants