Skip to content

fix(plugin-sdk): retain prepared models through host cleanup - #143270

Merged
steipete merged 2 commits into
mainfrom
codex/sdk-prepared-completion-resources
Sep 9, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/sdk-prepared-completion-resources

Conversation

@steipete

@steipete steipete commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Related: #140674. Internal caller migrations landed in #143302, #143307, and #143308.

What Problem This Solves

Resolves a problem where plugins receive an executable prepared model after its preparation lease has already been released. Completion callbacks and nested preparation can then outlive the model's owner, particularly during cancellation or host shutdown.

Why This Change Was Made

Keep the existing bare-result SDK API under its SDK host. Capture the host before lazy preparation, retain the successful model, and track actual completion and callback work through host closure. Re-enter that host for nested preparation without replacing Gateway request authority.

With all three internal callers using acquisition, delete the obsolete core bare adapter. Shared parameter and result types preserve the public fingerprint, optional selection fields, and deprecated parameter; the SDK runtime import remains lazy. Core preparation tests now release acquired results. The distinct direct preparation API remains unchanged.

User Impact

Plugins keep their existing prepared-model API and gain reliable host-owned cleanup. Host closure joins pending work and refuses new requests afterward. Provider selection, authentication checks, and public result shapes are preserved.

Evidence

The original native SDK regressions and a separately reproduced nested-callback host-context failure pass after repair. Final validation passed 166 focused tests, the unchanged export-collision guard, all selected changed-file gates, fresh isolated Codex review through P2, and a full build in 248.17 seconds.

A plain Node run of the built public SDK used actual ModelRegistry instances, a synthetic provider, and loopback HTTP. It exercised nested preparation in a real response callback, prompt cancellation while that callback remained held, host drainage, independent request stores, base-catalog reuse, post-close refusal, and fresh preparation after the last host closed. It also verified that the built core exposes acquisition and no longer exports the obsolete bare adapter.

That strict, joined run completed in 2.495 seconds, observing two base catalogs, four request forks, and three HTTP calls. All 21,093 build artifacts remained unchanged; 2,880 resolved modules had no application TypeScript/source fallback. Preparation samples were 145.059 / 3.162 / 5.768 ms for initial, retained, and fresh-after-closure preparations; these are local observations, not a comparative speedup benchmark.

After rebasing onto the three landed caller migrations, all 14 source files match the reviewed proof. The collision guard and 42 SDK composition tests passed again. This does not activate general run-registration physical disposal or claim a paid-provider or operator-Gateway test.

@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 docs Improvements or additions to documentation agents Agent runtime and tooling plugin: file-transfer size: M maintainer Maintainer-authored PR labels Sep 9, 2026
@steipete
steipete marked this pull request as ready for review September 9, 2026 16:50
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. 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 9, 2026
@clawsweeper

clawsweeper Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 9, 2026, 2:51 PM ET / 18:51 UTC (Revision 2).

ClawSweeper review

What this changes

Retains plugin-prepared models under their original host through completion and cleanup, preserves the public SDK shape, and removes the obsolete internal preparation adapter.

Merge readiness

⛔ Blocked before merge - 1 item remains

This remains a useful, focused repair: current main still releases the SDK preparation lease before returning the executable model. No actionable introduced defect was found, and the reported compiled-runtime proof covers the changed lifecycle.

Priority: P2
Reviewed head: 731e23f098ce5e402ed4da2d5db4ab6917054a95

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with convincing runtime observations and meaningful lifecycle coverage; no blocking code finding remains.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The captured body reports the built public SDK exercising real model registries and loopback HTTP through nested preparation, held callbacks, cancellation, host drainage, post-close refusal, and fresh-host recovery. These observations cover the changed production ownership boundary; focused tests supplement them.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The captured body reports the built public SDK exercising real model registries and loopback HTTP through nested preparation, held callbacks, cancellation, host drainage, post-close refusal, and fresh-host recovery. These observations cover the changed production ownership boundary; focused tests supplement them.
Evidence reviewed 9 items Current main still needs the repair: The current-main adapter calls acquireSimpleCompletionModelForAgent, removes release from the result, invokes release immediately, and returns the prepared model.
SDK lifecycle ownership: The SDK captures its host before lazy loading, checks closure after acquisition, retains the lease, and binds completion to that host. Failed adoption releases the claim.
Execution fencing and cleanup: Completion composes host and caller checks. The existing stream facade invokes the check after asynchronous transport initialization; host closure drains tracked work before releasing claims. Gateway request authority remains in its separate context carrier.
Findings None None.
Security None None.

How this fits together

Plugins use the completion SDK to turn agent configuration into prepared models and provider responses. The Gateway or CLI host now owns those models until accepted completion, callback, and cancellation work finishes.

flowchart TD
  A[Plugin preparation request] --> B[Capture original host]
  B --> C[Acquire model resources]
  C --> D[Prepared model]
  D --> E{Host and caller current?}
  E -->|Yes| F[Provider completion and callbacks]
  E -->|No| G[Reject execution]
  F --> H[Drain work and release at shutdown]
Loading

Before merge

  • Resolve merge risk (P1) - Plugins that retain a prepared model across its original host's closure will now receive an error and must prepare again under an open host; this intentional SDK lifecycle change is documented and covered by replacement-host proof.
Agent review details

Security

None.

PR surface

Source +86, Tests +272, Docs +13. Total +371 across 14 files.

View PR surface stats
Area Files Added Removed Net
Source 7 161 75 +86
Tests 6 448 176 +272
Docs 1 13 0 +13
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 14 622 251 +371

Review metrics

Metric Value Why it matters
Lifecycle implementation versus coverage Production net +86 lines; tests net +272 lines Production growth supplies host retention and execution ownership; most added coverage exercises lifecycle races through the existing transport boundary.

Merge-risk options

Maintainer options:

  1. Retain the documented host boundary (recommended)
    Accept post-close rejection with the existing compatibility documentation and demonstrated ability to prepare under a fresh host.

Technical review

Best possible solution:

Preserve the public SDK contract with explicit host-owned retention, documented post-close rejection, and the demonstrated fresh-host recovery path.

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

Yes, source establishes the premature release: current main releases the acquired lease before returning the prepared model. The PR supplies focused failing-before/passing-after evidence; this review did not execute tests.

Is this the best way to solve the issue?

Yes. Reusing the existing acquisition, host, and asynchronous work owners repairs the lifetime boundary while preserving public types and leaving provider selection and request authority intact.

AGENTS.md: found and applied where relevant.

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

Labels

Label justifications:

  • P2: This repairs a bounded plugin SDK resource-lifetime defect without evidence of an urgent widespread outage.
  • merge-risk: 🚨 compatibility: The existing preparation API now rejects reuse after its original host closes, requiring affected plugins to prepare again.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster 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): The captured body reports the built public SDK exercising real model registries and loopback HTTP through nested preparation, held callbacks, cancellation, host drainage, post-close refusal, and fresh-host recovery. These observations cover the changed production ownership boundary; focused tests supplement them.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured body reports the built public SDK exercising real model registries and loopback HTTP through nested preparation, held callbacks, cancellation, host drainage, post-close refusal, and fresh-host recovery. These observations cover the changed production ownership boundary; focused tests supplement them.

Evidence

What I checked:

  • Current main still needs the repair: The current-main adapter calls acquireSimpleCompletionModelForAgent, removes release from the result, invokes release immediately, and returns the prepared model. (src/agents/simple-completion-runtime.ts:553, 6d37d60fc8de)
  • SDK lifecycle ownership: The SDK captures its host before lazy loading, checks closure after acquisition, retains the lease, and binds completion to that host. Failed adoption releases the claim. (src/plugin-sdk/simple-completion-runtime.ts:17, 731e23f098ce)
  • Execution fencing and cleanup: Completion composes host and caller checks. The existing stream facade invokes the check after asynchronous transport initialization; host closure drains tracked work before releasing claims. Gateway request authority remains in its separate context carrier. (src/agents/simple-completion-execution.ts:50, 731e23f098ce)
  • Actual callback and cancellation ownership: The repository-owned AI transport reports pending callback and cancellation promises to OpenClaw's work tracker, connecting actual transport cleanup to the new host scope. This path uses packages/ai, not the Codex harness. (packages/ai/src/transports/transport-stream-shared.ts:319, 731e23f098ce)
  • Boundary regression coverage: Inspected coverage exercises overlapping hosts, changed configuration and credentials, late preparation, nested callbacks, held callback/cancellation work, and fresh preparation after closure. Closed-host and caller-check cases assert zero provider requests. Tests were inspected, not executed during this read-only review. (src/plugins/runtime/runtime-llm.prepared-owner.test.ts:394, 731e23f098ce)
  • Reported compiled-runtime proof: The complete supplied PR body reports a plain Node run through the built public SDK using actual ModelRegistry instances and loopback HTTP: nested preparation, cancellation with a held callback, host drainage, isolated request stores, post-close refusal, and fresh preparation passed. It records three HTTP calls, four request forks, two catalogs, and completion in 2.495 seconds, with unchanged source after rebasing. Captured context sourceRevision: f215b2c2312495311b0d00fa58dbafa9a9a072d1d679f9a202c955042dce4f2d. (731e23f098ce)

Likely related people:

  • Peter Steinberger: Raw commit b3260af adds src/plugins/legacy-sdk-resource-host.ts:92 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: b3260afd30a8; files: src/plugins/legacy-sdk-resource-host.ts)

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-09T16:56:51.553Z sha aab1860 :: blocked before merge. :: none

@steipete
steipete force-pushed the codex/sdk-prepared-completion-resources branch from aab1860 to 731e23f Compare September 9, 2026 18:45
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L and removed size: M labels Sep 9, 2026
@steipete

steipete commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Maintainer decision: retain the documented host boundary. A prepared model owned by a closed Gateway or CLI host must reject new execution; callers prepare again under the current open host. Keeping the old model usable would retain retired resources or silently change its model/authentication ownership.

The public preparation/result types remain unchanged, and standalone callers retain process-lifetime ownership. The host-lifetime behavior is documented in docs/plugins/sdk-runtime/models.md; the compiled public-SDK proof covers zero requests after closure and successful preparation under a fresh host. This accepts the compatibility tradeoff identified in the review; no compatibility shim or code change is requested.

@steipete
steipete merged commit 51b311b into main Sep 9, 2026
183 of 185 checks passed
@steipete
steipete deleted the codex/sdk-prepared-completion-resources branch September 9, 2026 19:55
@steipete

steipete commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

steipete added a commit that referenced this pull request Sep 9, 2026
Keep source-authored model overrides while filling omitted capabilities
from the same prepared catalog identity. Clear inherited capabilities when
the configured API or endpoint changes, and reuse the prepared policy
instead of rebuilding its configured catalog.

Compose admitted-runtime checks with the public SDK host lifetime and keep
internal configuration and authority fields out of prepared results.
Retain acquired models through HTTP completion in the affected test flows.

Related: #130706, #134496, #143270
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 10, 2026
…w#143270)

* fix(plugin-sdk): retain prepared models through host cleanup

* refactor: remove obsolete core completion adapter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. plugin: file-transfer proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L 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