Skip to content

fix(cdk): settle a failed child's issue only once per task - #1083

Closed
abhiramshaji-cyber wants to merge 3 commits into
aws-samples:mainfrom
abhiramshaji-cyber:fix/1073-late-write-reverts-failed-child
Closed

abhiramshaji-cyber wants to merge 3 commits into
aws-samples:mainfrom
abhiramshaji-cyber:fix/1073-late-write-reverts-failed-child

Conversation

@abhiramshaji-cyber

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

Copy link
Copy Markdown
Contributor

When a subtask fails, its issue is now moved back and given ❌ exactly once, so neither a later write on its task record nor a retry of the same record undoes a move a person made.

Hey @krokoko, hope you're doing well! Your write up made this really easy to follow, thank you, especially the second delivery trick in the build gate test, which reproduced it straight away.

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 #1073

Changes

What this code is for

reconcileTerminalChild in cdk/src/handlers/orchestration-reconciler.ts runs for every stream record of a finished task that belongs to an orchestration. It writes the verdict onto the child row, and for a child that did not succeed (step 1b) it calls revertState and replaceIssueReaction(..., 'failed') on the subtask's issue.

Before this PR

A subtask opens a pull request whose build is red, and a person fixes it and moves the Linear issue to "In Review".

  1. The first record (COMPLETED, build_passed = false) arrives. Step 1 writes child_status = failed under child_task_id = :tid AND (child_status = :running OR child_status = :s).
  2. Step 1b reverts the issue and sets ❌. Correct.
  3. The person moves the issue to "In Review".
  4. A later push stamps a preview screenshot on the same task record, so a new stream record arrives with the same terminal status.
  5. Step 1 succeeds again, because the row already holds failed (the child_status = :s half of the condition), so nothing marks the record as out of date.
  6. Step 1b runs again: revertIssueToNotStarted moves the issue back to Todo and the reaction goes back to ❌.

After this PR

Same subtask, same person, same later push.

  1. Step 1 writes child_status = failed as before.
  2. Before any tracker call, claimSettle writes settled_task_id = TA under child_task_id = :tid AND child_status = :failed AND (attribute_not_exists(settled_task_id) OR settled_task_id <> :tid). It succeeds, so step 1b reverts the issue and sets ❌.
  3. The person moves the issue to "In Review".
  4. The later write arrives. Step 1 succeeds again, but claimSettle fails its condition because settled_task_id is already TA.
  5. Step 1b is skipped and the issue stays in "In Review". Step 2 onward runs as before.

If DynamoDB throws on the claimSettle write (a throttle), the record is reported for retry before the issue is touched, and the retry settles it once. If the record fails after the claim, or the Lambda times out during the tracker calls, the retry finds the claim and does not settle again.

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: every later write reverts the issue"]
direction TB
  B1["later write on task TA"] --> B2["reconcileTerminalChild"]
  B2 --> B3["step 1: child_status = failed"]
  B3 --> B4{"row already failed for TA"}
  B4 -->|"condition passes"| B5["step 1b: issue back to Todo, reaction ❌"]
  B4 -->|"row moved on"| B6["out of date, issue left alone"]
end

subgraph AFTER["AFTER: settled once per task"]
direction TB
  A1["later write on task TA"] --> A2["reconcileTerminalChild"]
  A2 --> A3["step 1: child_status = failed"]
  A3 --> A4{"THE CHANGE: claimSettle before tracker calls"}
  A4 -->|"settled_task_id already TA"| A5["step 1b skipped, issue stays In Review"]
  A4 -->|"first claim for TA"| A6["step 1b runs once"]
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

reconcileTerminalChild and what it calls
A terminal task record from handler through reconcileTerminalChild to the settle, Functions view in Architect

The change

  • The fix: claimSettle (L538 to L552) claims settled_task_id with a conditional write, and step 1b runs only when that claim wins (L669). The claim also requires child_task_id = :tid and child_status = failed, so a row released to a new task or repaired between the verdict and the claim keeps its issue as it is.
  • A DynamoDB error on the claim is rethrown, the same as the status write above it: nothing has touched the issue yet, so the retry is safe.
  • settled_task_id is a new optional attribute on the child row (DynamoDB schema change, ask first under cdk/AGENTS.md). It is in OrchestrationChildRow and in a new "Orchestration table" section of docs/design/ORCHESTRATOR.md.
  • I went this way instead of the window after completed_at you suggested, because the stream is NEW_IMAGE only (task-table.ts L142) and a window still reverts a hand move that lands inside it. Would you rather have the window anyway, or this marker? Happy to switch.
  • Tests in cdk/test/handlers/orchestration-reconciler.test.ts: a repeated later write, a settle whose tracker call failed, a marker left by an earlier task, a throttled claim, a failure after the claim, and a row released or repaired between the verdict and the claim.

What changes for a user

  • A subtask that failed is still moved back and given ❌, once.
  • A later write on that task record (screenshot of a later push, the hourly pull request read), or a retry of the same record, no longer touches the issue.
  • A retry of the subtask is a new task id, so its own failure settles the issue again as before.
  • Repairs through a comment and successful children behave exactly as before.
  • Taken on purpose: if the Lambda dies after the claim but before the tracker calls finish, that failure is not settled. The other side would be undoing a move a person made.
  • Left for a decision in review: whether a retryable tracker failure should fail the record so the stream retries it (the adapters return false today rather than throwing), and whether a failed iteration should move its issue back.

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.

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

Thanks, the marker approach reads cleanly and the PR's own tests pass locally (112 in orchestration-reconciler.test.ts). I checked the behaviour on this head with a few throwaway tests against the same store harness. Two things I'd change before merge, one trade-off to confirm with the maintainer, and one open question. Details inline.

  1. A DynamoDB error on the marker write now fails the whole record, and the retry reverts the issue again.
  2. That same gap means a hand move can still be undone when the first delivery dies between the tracker calls and the marker write.
  3. Marking after a failed tracker call is a deliberate choice here, but it means a short tracker outage leaves a failed sub-task showing ✅ for good.
  4. Question on a failed iteration under the same task id (not verified).

On the question in the description: the marker is a new stored attribute on the child row, and cdk/AGENTS.md lists DynamoDB schema changes under ask-first, so it is good that you raised it. If it stays, it should be added to the row type and the orchestrator design doc.

// what a later run did).
const settleChannel = channelForMeta(snapshot.meta);
if (settleChannel && !plan.terminalSucceeded && !stale && !isIntegrationNode(subIssueId)) {
if (settleChannel && !plan.terminalSucceeded && !stale && !settled && !isIntegrationNode(subIssueId)) {

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.

Two points on the skip.

Trade-off to confirm. The description says the marker is written even when the tracker call failed, and there is a test for it, so this is intended. The cost: with the tracker down for the first delivery and back for the next two, I saw the marker set and the issue never reverted or given ❌ (0 successful reverts, 0 ❌ reactions). Before this PR a later write healed it. A middle ground would be to let a thrown tracker error fail the record so the stream retries it within its normal retry window, and still mark once that window is over. Worth a decision from the maintainer either way.

Question, not verified. A reply on a failed sub-task starts an iteration while the row keeps the same child_task_id. If that iteration fails, cascadeRestack returns at "source task not successful" without settling the issue. Can the issue be left in a running state there? If so, a later write on the original task used to revert it and now will not. I did not trace the iteration path, so this may be a non-issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for testing the outage case and for the iteration question, both were really worth digging into.

On the trade off, one thing I found while tracing it: the real adapters never throw here. revertIssueToNotStarted returns false on any failure, swapReaction returns false when a request fails, Slack's reaction swap does the same, and Jira has neither call. So a tracker outage shows up as false, and for the revert false also means "left alone on purpose" (the issue isn't in a started state because a person already moved it). Letting a failed tracker call fail the record would mean having the adapters report a retryable failure separately. graphqlRequest already works out retryable for 5xx and 429, so it's doable. The stream retries 3 times with bisect, so the retry window is short. For now I kept it as it was: settle once, even if the tracker call didn't land. @krokoko, would you rather have the retry on a retryable tracker failure? Happy to add it either way.

On the question: I traced it, and yes, it can happen. An iteration task carries linear_issue_id = the child issue. The agent moves it to In Progress when it starts (react_task_started, not read only) and leaves it there on failure (react_task_finished moves it only on success). replyToIterationComment sets ❌ on the comment but, by design, leaves the state ("On failure, leave the state … Never demote"). Before this PR the issue only came back to Todo if the original task record happened to get another write, which is the same write this PR stops. If it would help, I could revert the issue in the failure branch of replyToIterationComment, behind the existing once per iteration ack_replied_at claim, so a late write can't repeat it. Since that changes a documented choice, I left it out for now. Would you like it here, or in a separate PR?

replyToIterationComment and what it calls
What replyToIterationComment calls, Functions view in Architect

…aws-samples#1073)

A DynamoDB error on the marker write failed the record after the issue
was already reverted, and the retry reverted it again. The marker is now
claimed with a conditional write before the tracker calls, so a failed
claim leaves the issue untouched and a retry after the claim never
settles twice. Adds settled_task_id to the row type and the design doc.
@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor Author

Hey @isadeks, hope you're doing well! Thanks a lot for such a careful review, and for running the throttle and outage cases, that made both problems really clear.

I pushed a fix for both blockers. The marker is now claimed with a conditional write before the tracker calls, so a DynamoDB error fails the record before the issue is touched, and a retry after the claim never reverts it again. settled_task_id is now in OrchestrationChildRow and in a new "Orchestration table" section of docs/design/ORCHESTRATOR.md, as you suggested. The full cdk suite, typecheck and lint all pass.

On the two open points, I kept the current behaviour and wrote up what I found in the thread so @krokoko can decide: whether to retry a tracker call that fails, and whether a failed iteration should move its issue back. Happy to change either one.

@krokoko

krokoko commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Hi @abhiramshaji-cyber , thank you for this ! It was closed by #1084 , if you want to assign an issue to yourself please let us know on a ticket to avoid duplicated work on our end!

@krokoko krokoko closed this Nov 26, 2026
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(orchestration): a late write on a failed sub-task's record puts its issue back to not started

3 participants