Skip to content

fix: retain Anthropic requests through cancellation cleanup - #143377

Merged
steipete merged 1 commit into
mainfrom
codex/anthropic-cancellation-tail
Sep 9, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/anthropic-cancellation-tail

Conversation

@steipete

@steipete steipete commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Related: #140674.

What Problem This Solves

Resolves a problem where an aborted Anthropic request can release its prepared runtime while the response body is still cancelling. Cancellation can also run in the aborting caller's context instead of the request's original context.

Why This Change Was Made

Start body cancellation once, in the parser's original continuation, and give that actual promise to the existing host work observer before reporting the abort. Previously the abort listener started cancellation and discarded its promise; the parser's second cancellation was already resolved even though the first cleanup was still running.

The production repair is seven added and two removed lines. Normal non-abort cleanup remains awaited, and aborts preserve their original reason without waiting for cancellation to finish. No public API, dependency, or database policy changes.

User Impact

Aborted Anthropic calls still return promptly, while their runtime remains alive until accepted cancellation work finishes. Cleanup failures do not replace the original abort reason.

Evidence

Original code fails three transport regressions and the actual core ownership case: the parent drains early, cancellation runs in a foreign context, and a new ModelRegistry is built during held cleanup. The candidate passes all 148 transport and core cases. Affected checks, fresh isolated Codex review through P2, and the ordinary build (317.9 seconds) pass.

Compiled proof uses the actual exec reviewer and managed Anthropic transport against a local synthetic HTTP server. Abort reporting took 4.357 and 0.534 milliseconds for resolving and rejecting cancellation. The original owner stayed alive, overlapping preparation reused its generation, and final drainage followed the held cancellation's settlement. Non-abort cleanup and successful completion also passed.

All child processes exited; 12,119 build artifact hashes stayed unchanged and 2,872 resolved files used no application source fallback. No reviewed command was executed, and no real vendor credentials or production Gateway were used. These are individual correctness timings, not a comparative benchmark. The proof establishes ModelRegistry retention; general RUN SQLite physical disposal remains a separate prerequisite chain.

The final rebase combines the newly landed SDK cases with this cancellation regression. Both transport source files remain byte-identical to the reviewed build; all 157 transport/core composition cases and formatting checks pass on the refreshed head.

@clawsweeper

clawsweeper Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added size: M maintainer Maintainer-authored PR labels Sep 9, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 9, 2026
@clawsweeper

clawsweeper Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 9, 2026, 4:46 PM ET / 20:46 UTC (Revision 2).

ClawSweeper review

What this changes

The PR keeps aborted Anthropic requests’ runtime resources alive until response-body cancellation finishes, while preserving prompt abort reporting and adding regression coverage.

Merge readiness

✅ Ready for maintainer review

The fix remains necessary on inspected main, and no blocking defect was found. The verified test merge and reported refreshed validation resolve the previous review’s conflict concern.

Priority: P2
Reviewed head: fd9a80b2d2b14a743e688d9f5b0497c7ba448dc0

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with meaningful regression coverage and sufficient reported production-path observations.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The captured compiled-runtime observations exercise the actual exec-reviewer owner and Anthropic transport over local HTTP, showing prompt abort results, retained generation reuse during delayed cancellation, and final drainage after settlement; refreshed composition validation covers the rebased head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured compiled-runtime observations exercise the actual exec-reviewer owner and Anthropic transport over local HTTP, showing prompt abort results, retained generation reuse during delayed cancellation, and final drainage after settlement; refreshed composition validation covers the rebased head.
Evidence reviewed 8 items Main still contains the defect: Inspected main starts cancellation in the abort listener and discards its promise, then calls cancellation again in parser cleanup. The first cleanup operation is therefore not registered with the request owner.
Single cancellation owner: The introduced production hunk removes cancellation from the abort listener and registers the parser’s actual cancellation promise with the existing host observer. Non-abort cleanup remains awaited.
Existing retention boundary: The host observer passes pending cleanup into trackAsyncWork. Direct plugin completion drains its work scope before releasing the prepared runtime, so registering the real cancellation promise closes the identified lifetime gap without adding an API.
Findings None None.
Security None None.

How this fits together

OpenClaw’s Anthropic transport converts provider response streams into assistant output. Its pending cleanup must remain attached to the request’s runtime owner so resources are released only after that cleanup finishes.

flowchart TD
  A[Prepared model request] --> B[Anthropic response stream]
  B --> C[Response parser]
  D[Caller abort] --> C
  C --> E[Prompt abort result]
  C --> F[Tracked body cancellation]
  F --> G[Runtime owner drains]
  G --> H[Release request resources]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +5, Tests +139. Total +144 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 7 2 +5
Tests 2 196 57 +139
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 203 59 +144

Review metrics

None.

Technical review

Best possible solution:

Keep response cancellation in its original request context and retain the existing runtime owner until the actual cleanup promise settles.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: abort a stalled Anthropic response from another async context while body cancellation remains pending; main discards the first cleanup promise. This review did not execute the reproduction.

Is this the best way to solve the issue?

Yes. Moving cancellation to the parser and using the existing work observer repairs the lifecycle owner directly without introducing a parallel API or changing normal cleanup.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against b1aa4805a6c4.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The captured compiled-runtime observations exercise the actual exec-reviewer owner and Anthropic transport over local HTTP, showing prompt abort results, retained generation reuse during delayed cancellation, and final drainage after settlement; refreshed composition validation covers the rebased head.

Label justifications:

  • P2: This repairs a bounded Anthropic cancellation lifetime defect while preserving existing completion and abort behavior.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The captured compiled-runtime observations exercise the actual exec-reviewer owner and Anthropic transport over local HTTP, showing prompt abort results, retained generation reuse during delayed cancellation, and final drainage after settlement; refreshed composition validation covers the rebased head.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured compiled-runtime observations exercise the actual exec-reviewer owner and Anthropic transport over local HTTP, showing prompt abort results, retained generation reuse during delayed cancellation, and final drainage after settlement; refreshed composition validation covers the rebased head.

Evidence

What I checked:

  • Main still contains the defect: Inspected main starts cancellation in the abort listener and discards its promise, then calls cancellation again in parser cleanup. The first cleanup operation is therefore not registered with the request owner. (packages/ai/src/transports/anthropic-transport-stream.ts:253, b1aa4805a6c4)
  • Single cancellation owner: The introduced production hunk removes cancellation from the abort listener and registers the parser’s actual cancellation promise with the existing host observer. Non-abort cleanup remains awaited. (packages/ai/src/transports/anthropic-transport-stream.ts:350, fd9a80b2d2b1)
  • Existing retention boundary: The host observer passes pending cleanup into trackAsyncWork. Direct plugin completion drains its work scope before releasing the prepared runtime, so registering the real cancellation promise closes the identified lifetime gap without adding an API. (src/llm/ai-transport-host.ts:34, fd9a80b2d2b1)
  • Regression coverage: Transport cases cover resolving, rejecting, and synchronously failing cancellation from a foreign async context; the core case uses real HTTP fetch and checks retained generation reuse, delayed parent drainage, and subsequent release. Existing non-abort cleanup coverage is strengthened. Tests were inspected, not executed in this review. (src/plugins/runtime/runtime-llm.prepared-owner.test.ts:667, fd9a80b2d2b1)
  • Compiled runtime observations: The captured PR body reports the actual compiled exec reviewer and managed Anthropic transport against a synthetic HTTP server: abort results in 4.357 and 0.534 milliseconds, retained original owner, generation reuse during held cleanup, and drainage after cancellation settles. Normal cleanup and successful completion also passed. It explicitly limits the claim to ModelRegistry retention rather than general SQLite physical disposal. The final rebase reports unchanged transport source and 157 passing composition cases. Source snapshot identity: 7f294b461203f587fd6dfea7eceb48a655f3a7ae0b172e40f97a973d9627d340. (fd9a80b2d2b1)
  • Prior conflict concern resolved: The raw test-merge parents are the pinned main revision followed by the exact reviewed head. Its comparison against main contains only the three introduced paths, preserving unrelated main changes. The previous review had no code findings and requested refreshed composition validation, which the captured body supplies. (9211dfb68d81)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-09T20:37:05.847Z sha 0e3774cdd715bedebf65606ce546032e5f23328c :: blocked before merge. :: none

@steipete
steipete force-pushed the codex/anthropic-cancellation-tail branch from 0e3774c to fd9a80b Compare September 9, 2026 20:39
@steipete
steipete marked this pull request as ready for review September 9, 2026 20:40
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Sep 9, 2026
@steipete
steipete merged commit f30b1e9 into main Sep 9, 2026
213 of 214 checks passed
@steipete
steipete deleted the codex/anthropic-cancellation-tail branch September 9, 2026 21:00
@steipete

steipete commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant