Skip to content

feat(delivery): carry per-run token counts onto the task file - #196

Merged
bborbe merged 3 commits into
masterfrom
feat/token-budget
Nov 15, 2026
Merged

bborbe merged 3 commits into
masterfrom
feat/token-budget

Conversation

@bborbe

@bborbe bborbe commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Task: 8c45c38f-abf5-43f2-9f05-00c1ea1d57ca

Adds the four token counts ClaudeResult already parses (input, output, cache-read, cache-creation) to AgentResultInfo and Result, populates them in claude/agent-step.go, copies them in agent_runner.go, and publishes them as the metrics_*_tokens frontmatter keys plus a per-run metrics_runs history list.

  • The flat keys are last-write-wins, so metrics_runs records every run (counts + completed_at) for a per-window budget to sum.
  • A run with no usage summary publishes no token key rather than a false zero, matching the existing turn-count absence rule.
  • Populated only when the CLI reported a usage summary (positive turn count), so a cache-read of zero is a real observation while an absent summary stays absent.

Unblocks bborbe/agent-task-executor's tokenCeiling admission check (follow-up).

Verified: make precommit green (golangci-lint 0 issues, gosec clean via #nosec G101 on the key-name constants, tests pass, helm render OK).

Add the four token counts ClaudeResult already parses (input, output,
cache-read, cache-creation) to AgentResultInfo and Result, populate them in
claude/agent-step.go, copy them in agent_runner.go, and publish them as the
metrics_*_tokens frontmatter keys plus a per-run metrics_runs history list
(the flat keys are last-write-wins). A run with no usage summary publishes no
token key, matching the existing turn-count absence rule.
@bborbe
bborbe marked this pull request as ready for review November 15, 2026 09:54

@ben-s-pull-request-reviewer ben-s-pull-request-reviewer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token-count plumbing is sound on the happy path, but two correctness gaps remain: metrics_runs is truncated for multi-delivery (multi-phase) Jobs, and token usage is discarded on the failed-run path and for partial-usage results. Selector-mode adjudication ran; intent unavailable (gh not permitted); precommit skipped in selector mode because CI covers lint+test.

…ry tokens on the failed path

Review findings on #196:
- metrics_runs was truncated for multi-phase Jobs: each delivery rebuilt the
  frontmatter from the Job-start content, so a second delivery dropped the
  first's entry. The list is now accumulated on the deliverer, seeded once from
  the Job-start content.
- Token usage was discarded on the failed-run path and for partial-usage
  results. runCounts now reads usage before the error check (the runner carries
  it on the error path) and treats a summary with any token count as present
  even when no turn count was reported; the failed Result carries the counts.
@ben-s-pull-request-reviewer
ben-s-pull-request-reviewer Bot dismissed their stale review November 15, 2026 10:29

superseded by new automated review

@ben-s-pull-request-reviewer ben-s-pull-request-reviewer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the token-count plumbing (ClaudeResult → Result → AgentResultInfo → Kafka frontmatter + metrics_runs history) against master. The change is additive, nil-safe, and the deliverer serializes runHistory under its existing mutex, so the per-deliverer mutable state is not racy and there is no re-delivery path that would double-count within the library. All 25 mechanical-funnel findings were verified against the worktree and are false positives/exempt: the go-composition hits are receiver method calls plus a pure stdlib fmt.Sprintf (explicitly ruled out by the rule), the go-enum-type hits are frontmatter map keys not enums, the go-k8s-binary 'secret' hits are int64 token COUNTS not credentials, and the go-testing bare-call hits are counterfeiter GenerateReturns, which returns nothing. No blocking defect found; three non-blocking nits and one Should-Fix test gap are noted.

@bborbe
bborbe merged commit 129edd3 into master Nov 15, 2026
5 checks passed
@bborbe
bborbe deleted the feat/token-budget branch November 15, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant