Skip to content

fix(#9228): sum Claude turns across result events - #9814

Open
AshwinUgale wants to merge 1 commit into
fullsend-ai:mainfrom
AshwinUgale:fix/9228-claude-num-turns
Open

AshwinUgale wants to merge 1 commit into
fullsend-ai:mainfrom
AshwinUgale:fix/9228-claude-num-turns

Conversation

@AshwinUgale

Copy link
Copy Markdown
Member

Summary

Claude Code writes another result event each time a background task resumes a finished session. num_turns covers only that segment, but fullsend kept the last one, so metrics.json and fullsend.num_turns undercounted.

Related Issue

Part of #9228. The single "→ Result" log block is left for a follow-up.

Changes

  • parseClaudeStream totals num_turns, so each ResultEvent carries the turns so far. recordClaudeMetrics stays last-wins, which also works with fix(#6806): capture num_turns from incomplete Claude streams #7687.
  • Cost and modelUsage are already running totals and are unchanged.
  • The log still prints one block per result, but turns now rise to the total.

Testing

  • make lint passes (stage changes first, then run): not run locally; go vet and gofmt are clean

  • Tests added/updated for new or modified logic

  • Replayed 355 Claude streams from the agents nightlies (Claude Code 2.1.306): 11 change, all review runs, from 2–11 turns to 23–30. Every other metric is identical.

  • New test with one of those runs' turn counts; the existing two-result test now checks turns. Both fail without the fix.

  • 53 internal/runtime tests fail under WSL here, all of which fail without the fix too.

Not covered

  • Results without modelUsage fall back to usage, which is also per segment. All 11 runs had modelUsage.
  • No resumed or steered sessions were in the data.

Checklist

  • PR title follows Conventional Commits (correct type, ! for breaking changes)
  • Commits are signed off (DCO) — human and human-directed agent sessions only
  • I wrote this contribution myself and can explain all changes in it

Signed-off-by: Ashwin Govind Ugale <ugaleashwin@gmail.com>
@AshwinUgale
AshwinUgale requested a review from a team as a code owner November 23, 2026 19:04
@qodo-code-review

qodo-code-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Sum Claude turns across result events

🐞 Bug fix 🧪 Tests 📝 Documentation 🕐 10-20 Minutes

Grey Divider

AI Description

• Sum segment-level Claude turns so resumed sessions report accurate run totals.
• Preserve last-result cost and model usage, which are already session totals.
• Test cumulative result events and final metrics across multiple results.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The parser, RunMetrics record, cross-iteration aggregate, metrics artifact, and telemetry represent one bounded end-to-end outcome: reporting usage for a Claude execution.
  • They are implementation hops of the same accounting behavior, not independent capabilities.
  • The cited parser and aggregation code directly connect the changed value to the final reported run metrics.
Behavioral complexity 🟡 Medium 🧩 Interacting branches

  • Before: one terminal Claude result normally supplied the final turn count directly.
  • After: the parser maintains totalTurns state across multiple result events and emits a progressively rewritten count, while cost and model usage are treated as running totals and later metrics overwrite earlier snapshots.
  • New correctness condition: the parser must distinguish segment-local result fields from session-cumulative fields and remain correct across resumed background-task segments, retries, steering, duplicate events, and the final aggregation step.
  • The added state interaction is the changed condition; existing metric aggregation infrastructure is not itself being classified as new complexity.
Potential damage 🟡 Medium 🩹 Incorrect results

  • The failure produces an incorrect count in execution metrics, metrics.json, aggregate run metrics, and related telemetry.
  • It does not alter the agent's task result, persistent application state, or external side effects.
  • The equal-importance baseline is used; the concrete impact is limited to observability and usage accounting for the affected Claude run.
  • Reverting the parser change restores the prior accounting behavior for future runs.
  • Previously emitted metrics and telemetry remain incorrect unless regenerated or manually corrected; rerunning the agent can produce a fresh correct record if the upstream event semantics are stable.
Assessed commit e286b31
Diagram

graph TD
  Stream["Claude stream"] --> Parser["Stream parser"] --> Turns["Turn accumulator"] --> Results["Result events"] --> Metrics["Run metrics"]
  Results --> Renderer["Progress renderer"]
  Parser -->|"session totals"| Results
Loading
High-Level Assessment

Accumulating turns in the parser is the best fit: both emitted result events and recorded metrics become accurate without summing already-cumulative cost or model usage. Accumulating only in the metrics recorder would leave progress output and other event consumers with incorrect turns.

Files changed (3) +65 / -7

Bug fix (1) +17 / -6
claude_progress.goAccumulate turns before emitting Claude results +17/-6

Accumulate turns before emitting Claude results

• Sums segment-level num_turns across result events so each emitted event carries turns so far. Clarifies why metric recording still replaces cost and model usage with the latest result.

internal/runtime/claude_progress.go

Tests (1) +44 / -0
claude_progress_test.goCover cumulative turns across multiple results +44/-0

Cover cumulative turns across multiple results

• Adds a six-result regression test for intermediate turns and final metrics. Extends the existing two-result test to check turn accumulation without double-counting model usage.

internal/runtime/claude_progress_test.go

Documentation (1) +4 / -1
event.goClarify result-event semantics +4/-1

Clarify result-event semantics

• Documents that Claude can emit multiple result events, with turns accumulated through each event and token totals conditional on modelUsage.

internal/runtime/event.go

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AshwinUgale

Copy link
Copy Markdown
Member Author

/fs-review

@fullsend-ai-review

fullsend-ai-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🤖 Finished Review · ✅ Success · Started 7:51 PM UTC · Completed 7:57 PM UTC

Commit: e286b31 · View workflow run →

Runtime: pi · Model: sonnet → claude-sonnet-5-5 · Effort: high · Cost: $0.90

@fullsend-ai-review fullsend-ai-review Bot added the risk/moderate PR risk: moderate label Nov 23, 2026
@fullsend-ai-review

Copy link
Copy Markdown

Risk Assessment: moderate (2/5)

Details

Small, focused telemetry bug fix with test coverage matching issue scope, though moderate risk is driven by high regression history and change coupling in touched runtime files.

@fullsend-ai-review

fullsend-ai-review Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Looks good to me

Previous run

<!-- fullsend:review-findings-v2:eyJ2ZXJzaW9uIjoyLCJhY3Rpb24iOiJhcHByb3ZlIiwiZmluZGluZ3MiOltdfQ== -->

Looks good to me

@fullsend-ai-review fullsend-ai-review Bot added the ready-for-merge All reviewers approved — ready to merge label Nov 23, 2026

This branch has not been deployed

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

Labels

ready-for-merge All reviewers approved — ready to merge risk/moderate PR risk: moderate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant