Skip to content

[Fix] Delegation continues when the parent's pending tool results cannot be saved - #2189

Open
PierrunoYT wants to merge 1 commit into
Zoo-Code-Org:mainfrom
PierrunoYT:fix/2188-delegation-flush-failure
Open

PierrunoYT wants to merge 1 commit into
Zoo-Code-Org:mainfrom
PierrunoYT:fix/2188-delegation-flush-failure

Conversation

@PierrunoYT

Copy link
Copy Markdown
Contributor

Related GitHub Issue

Closes: #2188

Description

A parent task can have tool results that are not yet in its API history when it starts a subtask. Delegation saves them first. If that save failed and the retry save also failed, delegation continued. The parent was disposed. Later, the parent resumed from disk. Its history ended with a tool_use that had no tool_result. Providers reject this with a 400 error.

This PR makes delegation stop in that case. The behavior is a choice. Please confirm it.

  • Delegation (ClineProvider.delegateParentAndOpenChild): The method flushes the pending results. If the flush fails, it retries the save and flushes once more. Saving the existing history does not add results that never reached it, so the second flush is needed. If the results are still not saved, the method throws before it disposes the parent or creates a child. The parent stays open with its pending results in memory. The user can try the same delegation again.
  • User-visible behavior: new_task returns this error through its normal error handling: "Could not start the subtask: the parent task's pending tool results could not be saved to disk. The parent task stays open and nothing was delegated. Check disk space and permissions, then try again."
  • Flush (Task.flushPendingToolResultsToHistory): A flush can append the results to history and then fail to save them. A second flush used to append the same results again. The task now remembers this state. A repeat flush only saves the history. This makes the retry safe. The state resets when the next API request starts.
  • Not changed: Successful delegation. The send-time normalizer from [Fix] Anthropic tool_use / tool_result pairing errors (400) #2113. A trailing tool_use can still have a result that is pending, so the normalizer must leave it alone.

Review points:

  • The old code treated any flush error as non-fatal. Now every failure to persist the results stops delegation. This includes a flush that throws.
  • The error message is plain English, like the warning it replaces. I did not add translations.

Test Procedure

Unit tests:

  • src: npx vitest run __tests__/ClineProvider.delegation.spec.ts core/task/__tests__/Task.persistence.spec.ts
  • Delegation tests cover: the flush and the retry save both fail; the retry save succeeds but the results are still not in history; the flush throws. Each case rejects, and nothing is disposed, created, or written to history. Other tests cover a repeat flush that succeeds after the retry (delegation continues), and a first flush that succeeds (no retry).
  • Persistence tests cover: a retry saves results that are already in history without a second copy; results that never reached history can still be added by a later flush.
  • The failure cases and the no-duplicate case fail without the source change.

Other checks: check-types passes in src. ESLint passes with no new suppressions. The core/webview, core/task, core/tools, and __tests__ suites pass.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I reviewed my own code with care.
  • Writing Style: The PR title and description use Simplified Technical English (ASD-STE100): short sentences, plain words, active voice, one idea per sentence, no contractions.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Visual Snapshot (UI changes only): Not applicable. This PR has no UI change.
  • Documentation Impact: I checked if my changes need documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required. The new behavior is described above.

🤖 Generated with Claude Code

…sults cannot be saved

A failed flush plus a failed retry save used to be non-fatal: the parent was disposed
and later resumed from disk with a tool_use that had no tool_result, which providers
reject with a 400. Delegation now stops before anything is disposed or created, so the
parent stays open with its pending results and the delegation can be retried.

A flush that already appended the results to history but could not save them now only
saves again, so a retry no longer appends a second copy.

Fixes Zoo-Code-Org#2188

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between ca05b73 and db273fa.

Files selected for processing: 4
  • src/__tests__/ClineProvider.delegation.spec.ts
  • src/core/task/Task.ts
  • src/core/task/__tests__/Task.persistence.spec.ts
  • src/core/webview/ClineProvider.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review status

Thanks for contributing. This comment tracks the review sequence and the next action.

Current step: Required CI passed. Waiting for automated review of the latest commit.

If automated review does not start, a maintainer must restart it.

Review-state labels are managed by this workflow; do not edit them manually. community-approved is managed the same way — do not add or remove it manually. It signals a fresh community code approval for the current head as an advisory priority only; maintainer review is still required.

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/core/task/Task.ts 88.88% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot added coderabbit-review-active Required CI passed; CodeRabbit review is active awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit labels Oct 25, 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

awaiting-coderabbit Waiting for CodeRabbit to approve the latest commit coderabbit-review-active Required CI passed; CodeRabbit review is active

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle delegation when pending tool-result flush and retry save both fail

1 participant