Skip to content

fix(observability): count turns_completed in the Linear and Jira comments - #1082

Open
abhiramshaji-cyber wants to merge 5 commits into
aws-samples:mainfrom
abhiramshaji-cyber:fix/919-turns-completed-comments
Open

abhiramshaji-cyber wants to merge 5 commits into
aws-samples:mainfrom
abhiramshaji-cyber:fix/919-turns-completed-comments

Conversation

@abhiramshaji-cyber

@abhiramshaji-cyber abhiramshaji-cyber commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

The Linear and Jira completion comments printed a different turn count from bgagent status, sometimes above the limit, and now they print the same count the limit applies to.

Hey @krokoko, hope you're doing well! Thank you for the breakdown of what was left after #1075, it made this one really easy to pick up. I switched exactly the four reads you listed and kept everything else as it was.

Area

  • cdk — infrastructure, handlers, constructs
  • agent — Python runtime / Docker image
  • cli — bgagent client
  • docs — guides or design sources (docs/guides/, docs/design/)
  • tooling — root mise.toml, scripts, CI workflows

Related

Closes #919

Changes

What this code is for

When a task ends, dispatchToLinear and dispatchToJira in fanout-task-events.ts post a completion comment with cost • turns • duration. For orchestration iterations, renderIterationComment and the reconciler's parseTerminalTaskRecord build the same Jira status line. All of them read the turn count off the task record.

Before this PR

A Jira user starts a task with max_turns 80. The model answers 62 times; the SDK's own counter reaches 90.

  1. The agent writes turns_completed: 62 and turns_attempted: 90 on the task record.
  2. bgagent status reads turns_completed (format.ts#L643) and shows 62 / 80.
  3. dispatchToJira reads task.turns_attempted (fanout-task-events.ts#L2030), and the Linear path does the same (#L1692).
  4. renderIterationComment uses metric('turns_attempted') (jira-iteration-comment.ts#L53) and parseTerminalTaskRecord reads img.turns_attempted (orchestration-reconciler.ts#L291).
  5. The Jira comment says turns: 90 / 80 while the CLI says 62 / 80.

After this PR

Same task, same record.

  1. Steps 1 and 2 are unchanged.
  2. All four reads take turns_completed instead, through the same coerceNumericOrNull (or the stream image N/S parse in the reconciler), and then through one shared completedTurnsOrNull.
  3. The Jira and Linear comments say turns: 62 / 80, matching the CLI and the console.
  4. A record whose turns_completed is missing, 0, negative or not a number gets the existing turns: —, never turns_attempted.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: comment says 90 / 80"]
direction TB
  B1["task ends, record has 62 and 90"] --> B2["dispatchToJira / renderIterationComment"]
  B2 --> B3["coerceNumericOrNull"]
  B3 --> B4{"read task.turns_attempted"}
  B4 -->|"SDK count, not what max_turns limits"| B5["turns: 90 / 80"]
  B4 -->|"field missing"| B6["turns: —"]
end

subgraph AFTER["AFTER: comment says 62 / 80"]
direction TB
  A1["task ends, record has 62 and 90"] --> A2["dispatchToJira / renderIterationComment"]
  A2 --> A3["coerceNumericOrNull"]
  A3 --> A4{"THE CHANGE: read turns_completed"}
  A4 -->|"the count max_turns limits"| A5["turns: 62 / 80, same as CLI"]
  A4 -->|"missing or 0, as in the CLI"| A6["turns: — as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

renderJiraFinalStatusComment and its callers
The Jira final status renderer and the callers that hand it a turn count, Functions view in Architect

The change

  • fanout-task-events.ts: both turns: args (Linear, which also feeds the Linear agent session activity, and Jira) read task.turns_completed. This is the fix.
  • jira-iteration-comment.ts: the fallback status reads metric('turns_completed').
  • orchestration-reconciler.ts: parseTerminalTaskRecord parses turns_completed instead of turns_attempted.
  • shared/numeric.ts: new completedTurnsOrNull, which keeps a count only when it is a finite number above 0. All four sites use it, so they share one rule for "no count" (a recorded 0 is no count, matching the CLI's recorded || ...). Before, three sites used || null, which let a negative through, while the reconciler checked finite and above 0. parseTerminalTaskRecord gets simpler with it, so its complexity record drops from 71 to 70.
  • Tests: the Linear and Jira records carry both counters and assert the comment shows turns_completed and not turns_attempted; the max turns case shows 100 / 100; cases for a missing, 0 or unparseable turns_completed in the three handler test files; the Linear agent session fixture gets turns_completed and a test that the final activity shows 9 / 100; numeric.test.ts covers completedTurnsOrNull for 1, 27, 100 and for undefined, null, 0, -0, -3, NaN and ±Infinity.

What changes for a user

  • Linear and Jira comments, and the Linear agent session's final activity, show the same turn count as bgagent status and the console, and it can no longer exceed max_turns.
  • Cost, duration, the PR link and the frames (✅, ⚠️, ❌) are untouched.
  • turns_attempted stays on the record for debugging; nothing reads it for display anymore.

One question, only because it would change the fallback: the CLI falls back to the newest agent_turn event when the record has no count, but these handlers have no access to the events table, so I took the placeholder — as the default. Every terminal write from the agent sets both counters together, so I think that case is only a task that never ran the agent. Would you like the event lookup here as well, or is the placeholder fine? Happy to change it either way, and any feedback or tips are very welcome.

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

…ents

After aws-samples#1075 the CLI and the web console show turns_completed, the count
max_turns limits, while the completion comments on Linear and Jira still
printed turns_attempted. One task could show two different turn counts,
and the comment could print a number above the limit.

The Linear and Jira completion comments, the turns metric of the Jira
iteration comment and the final status the orchestration reconciler
writes now read turns_completed. As in the CLI, a recorded 0 or a
missing value is no count; these handlers have no agent_turn event to
fall back to, so the comment shows the existing placeholder instead of
turns_attempted.

Refs aws-samples#919
isadeks
isadeks previously approved these changes Nov 25, 2026

@isadeks isadeks 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.

Looks good. I checked the switch against the writers: the agent's terminal write sets turns_attempted and turns_completed together (agent/src/pipeline.py, agent/src/task_state.py), and the cancel path writes neither, so no record can carry the old count without the new one. The four touched test files pass locally (366 tests). Two small non-blocking notes inline.

Comment thread cdk/src/handlers/fanout-task-events.ts Resolved
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@114795e). Learn more about missing BASE report.

Files with missing lines Patch % Lines
cdk/src/handlers/orchestration-reconciler.ts 80.00% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1082   +/-   ##
=======================================
  Coverage        ?   93.83%           
=======================================
  Files           ?      406           
  Lines           ?   117335           
  Branches        ?    13039           
=======================================
  Hits            ?   110098           
  Misses          ?     6635           
  Partials        ?      602           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(observability): turns_attempted can exceed max_turns and the CLI shows a third value — three disagreeing turn counters

4 participants