Skip to content

fix(llm): retain prepared runtimes through transport cleanup - #142197

Merged
steipete merged 1 commit into
mainfrom
codex/split-completion-producer-drain
Sep 8, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/split-completion-producer-drain

Conversation

@steipete

@steipete steipete commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

A direct plugin completion can return after cancellation while its response callback or response-body cancellation is still running. Releasing the prepared runtime at that point drops its last lease, so an overlapping request rebuilds the same generation while accepted work still uses the previous one. Parent shutdown can also finish without joining that work.

Why This Change Was Made

Keep the existing lease inside the existing AsyncWorkScope until accepted transport callbacks and cancellation promises settle. A narrow transport-host observer records the actual promises before they can be lost behind a cancellation race. The caller receives its result independently; parent cleanup retains and drains the operation before releasing the lease.

This six-file slice follows #141938 and replaces part of #140674. It reuses the existing work-scope implementation and preserves callback deferral, synchronous cancellation invocation, and the original completion error. Physical registration disposal remains a separate change.

User Impact

Cancelled calls still return before a held cleanup operation finishes. Overlapping calls reuse prepared facts while keeping independent mutable request stores. Once accepted work and the overlapping call finish, the generation can retire normally.

Evidence

The two new native cases fail on the original code: parent drainage finishes early and the overlapping request creates a second generation. Both pass with the fix, along with all nine existing native ownership cases. The selected native, package transport, and runtime suites pass 124 tests. Full canonical changed checks, a complete build, and independent Codex review pass.

Separate compiled baseline and candidate processes call createPluginRuntime().llm.complete through a registered synthetic provider and real loopback HTTP. Both callback-after-abort and native cancellation cases demonstrate:

Observation Baseline Candidate
Generation builds while cancelled work remains pending 2 1
Independent request-store forks 2 2
Caller result arrives before the held work settles Yes Yes
Later request rebuilds after drainage Yes Yes

The cancellation probe uses the public guarded-fetch and transport-notification helpers with a native Web Streams cancellation operation. It verifies emitted host/transport ownership, not the private cancellation internals of a particular provider SDK. Source and compiled artifacts remain unchanged through the probes; all owned processes, sockets, and synchronization operations are joined. These are structural reuse observations, not a general inference-latency claim.

@clawsweeper

clawsweeper Bot commented Sep 8, 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 8, 2026
@steipete
steipete marked this pull request as ready for review September 8, 2026 13:51
@clawsweeper clawsweeper Bot added 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. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 8, 2026
@clawsweeper

clawsweeper Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 8, 2026, 9:57 AM ET / 13:57 UTC.

ClawSweeper review

What this changes

The PR keeps prepared model runtimes alive until response callbacks and cancellation cleanup finish, while allowing cancelled callers to return promptly.

Merge readiness

✅ Ready for maintainer review

This remains a useful follow-up to the merged completion-lease repair: current main still releases ownership before pending transport cleanup settles. No blocking correctness or security defect was found.

Priority: P2
Reviewed head: 185194f66d6995dfbcc78d9820972c17d29febd4

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused ownership repair with relevant production-path observations and meaningful regression coverage; no blocking finding was identified.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.
Evidence reviewed 9 items Reviewed change identity: The complete host-provided introduction delta is pinned from 2bc6e41 to the reviewed head. The verified test merge contains the same six changed paths; unrelated base changes are excluded from this review.
Completion owns drainage: The direct-provider branch registers its owner before acquisition, resolves the caller independently, drains its nested work scope, and then releases the prepared lease. Acquisition failures still reject the caller.
Actual transport promises are retained: The shared transport observes the deferred callback promise before racing cancellation and observes the cancellation promise after synchronous invocation. The core host forwards these promises to the existing asynchronous work scope without replacing the lifecycle error.
Findings None None.
Security None None.

How this fits together

Plugins request model completions through OpenClaw’s host runtime, which prepares provider resources and invokes the transport. This change connects pending transport work to the completion’s resource lifetime and parent cleanup.

flowchart TD
  A[Plugin completion request] --> B[Acquire prepared runtime]
  B --> C[Provider transport]
  C --> D[Caller result or cancellation]
  C --> E[Pending callbacks and cleanup]
  E --> F[Drain owned work]
  F --> G[Release prepared runtime]
  F --> H[Parent cleanup completes]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +27, Tests +234. Total +261 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 4 88 61 +27
Tests 2 236 2 +234
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 324 63 +261

Review metrics

None.

Technical review

Best possible solution:

Keep caller completion independent while retaining the existing prepared lease through all accepted transport work and parent drainage.

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

Yes, from source: hold a response callback or cancellation promise, let the caller finish, and start an overlapping completion. Current main releases the lease before that held work settles; the reported baseline probes exercise this sequence.

Is this the best way to solve the issue?

Yes. Tracking actual transport promises through the existing work scope repairs the resource owner without delaying caller cancellation or changing the public completion API.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This repairs a bounded completion-lifecycle defect affecting runtime reuse and cleanup after cancellation.
  • add proof: sufficient: Contributor real behavior proof is sufficient. Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.

Label justifications:

  • P2: This repairs a bounded completion-lifecycle defect affecting runtime reuse and cleanup after cancellation.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.
  • proof: sufficient: Contributor real behavior proof is sufficient. Reported compiled loopback runs exercise the production completion owner with held response callbacks and native cancellation, showing prompt caller return, retained generation reuse, and retirement after drainage.

Evidence

What I checked:

  • Reviewed change identity: The complete host-provided introduction delta is pinned from 2bc6e41 to the reviewed head. The verified test merge contains the same six changed paths; unrelated base changes are excluded from this review. (185194f66d69)
  • Completion owns drainage: The direct-provider branch registers its owner before acquisition, resolves the caller independently, drains its nested work scope, and then releases the prepared lease. Acquisition failures still reject the caller. (src/plugins/runtime/runtime-llm.runtime.ts:584, 185194f66d69)
  • Actual transport promises are retained: The shared transport observes the deferred callback promise before racing cancellation and observes the cancellation promise after synchronous invocation. The core host forwards these promises to the existing asynchronous work scope without replacing the lifecycle error. (packages/ai/src/transports/transport-stream-shared.ts:256, 185194f66d69)
  • Existing ownership primitive: AsyncWorkScope registers operations before invocation and drains cooperating descendants with allSettled. Its captured tracker carries work ownership rather than authorization; this patch does not introduce a competing lifecycle abstraction. (src/shared/async-work-scope.ts:25, 185194f66d69)
  • Current main still needs the repair: At the fetched main revision, direct completion releases prepared resources immediately in finally, and transport callback/cancellation promises have no pending-work observer. The previously merged repair therefore does not cover this cleanup tail. (src/plugins/runtime/runtime-llm.runtime.ts:645, a39239faf938)
  • Related merged work and routing: GitHub confirms fix(llm): retain prepared runtimes through direct completions #141938 merged as this commit. Current-main path history also identifies Peter Steinberger’s recent completion and work-scope changes. Exact historical line attribution could not be established from the available older blobs, so no introduction claim is made. (src/plugins/runtime/runtime-llm.runtime.ts, e4ee9d681e14)

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.

@steipete
steipete merged commit 868ac24 into main Sep 8, 2026
247 checks passed
@steipete
steipete deleted the codex/split-completion-producer-drain branch September 8, 2026 14:24
@steipete

steipete commented Sep 8, 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