Skip to content

fix(plugins): finish retained cleanup after caller release - #191126

Merged
vincentkoc merged 1 commit into
mainfrom
fix/plugin-consumer-cleanup-scope-20261102
Nov 2, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
fix/plugin-consumer-cleanup-scope-20261102

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #191123

What Problem This Solves

Fixes retained plugin cleanup being rejected with Plugin invocation scope is closed when teardown starts inside a caller that has already finished. This can surface as a best-effort context-engine completion warning.

User Impact

Host-owned cleanup can finish after its original caller closes. Ordinary callbacks captured by that caller remain revoked, as do closed consumer callbacks and calls to unrelated retiring plugins. No provider, account, configuration, schema, or public API changes.

The live warning did not identify its exact phase. This repair does not establish that the warning caused missing replies, offline periods, or a failed onSubagentEnded hook.

Why This Change Was Made

consumer.close() owns the transition from operation callbacks to a separate teardown token. It revoked callbacks correctly, but entered teardown while retaining the ambient caller scope; the closed caller rejected entry before cleanup could install its own bindings. Exit that inherited invocation scope at the existing consumer owner, matching physical plugin/module disposal. Registry provenance, failure collection, and final release ordering stay with the same owner.

This completes the lifetime distinction already used in #163818 and preserves the ordinary callback guard involved in #158369. It adds no second cleanup path or retry.

Evidence

  • Candidate: 2ac8f5ac1e989fb12072d1ce27c5d9cc762e399c. Signed commit, normal content/format hooks, targeted formatting, whitespace/privacy checks, and staged test-line ratchet passed. Production file remains within its line cap.
  • Independent autoreview: exit 0, scoped-clean through P2, no actionable findings. Reviewed source bytes match the commit.
  • Direct regression exercises asynchronous retained-consumer cleanup inside a released caller, verifies the retained registry, and keeps closed caller/consumer and unrelated-instance refusals.
  • Integration regression registers a managed context engine through the plugin API, then uses real engine resolution, completion notification, and disposal under a closed caller. It asserts the ordered creation/notification/disposal events, no warning, and no retained work.
  • Node's AsyncLocalStorage.exit contract was inspected directly, along with the existing physical-disposal implementation and fixture ownership contracts.
  • Exact-head Bun CI passed all 52 focused tests: plugin-instance.test.ts 35 and plugin-invocation-scope.test.ts 7 in job125303920703, plus context-engine cleanup 10 in job125303920771. Both new closed-caller regressions passed. The enclosing plugin group passed 830 tests across 50 files; both selected wrappers exited 0.
  • Full CI41453152265 completed successfully: 47 successful jobs, 29 skipped, zero failures. The managed local checkout has no qualified dependency install. No red-baseline replay, Node execution of these tests, deployed repair, or live Telegram recovery proof is claimed.
  • Production delta: +2 lines; tests: +82 lines. The added production wrapper enforces the existing host teardown lifetime boundary. No new helper, fallback, or guard weakening.

Focused proof command:

node scripts/run-vitest.mjs src/plugins/plugin-invocation-scope.test.ts src/agents/subagents/registry/subagent-registry-context-cleanup.test.ts src/plugins/plugin-instance.test.ts --maxWorkers=1

@vincentkoc vincentkoc self-assigned this Nov 2, 2026
@clawsweeper

clawsweeper Bot commented Nov 2, 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 agents Agent runtime and tooling size: S labels Nov 2, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review November 2, 2026 13:27
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 2, 2026
@vincentkoc
vincentkoc merged commit 5a160d1 into main Nov 2, 2026
227 checks passed
@vincentkoc
vincentkoc deleted the fix/plugin-consumer-cleanup-scope-20261102 branch November 2, 2026 14:02
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 3, 2026
obviyus added a commit that referenced this pull request Nov 10, 2026
…closed plugin scope (#196700)

fix(gateway): run the automatic activity recap outside the finished run's plugin scope

When a Codex run ended, the automatic Activity recap never sent its request and the recap stayed at "updating". The Codex runtime emits the run-end event inside the run's plugin invocation scope, and the recap service scheduled the recap from that event without leaving the scope (since #169622). Since #184243, new prepared-model scopes take the caller's scope as their parent, so when the recap checked utility model auth after the run had finished, it hit "Plugin invocation scope is closed", and its own error handling hit the same closed scope. The run-end recap is now scheduled inside pluginInvocationContext.exit(...), the same pattern #191126 used for #191123. The RPC and archive recap paths are unchanged.

Evidence: a built Gateway in Docker with the real Codex app-server and a stand-in endpoint. On main, two Codex runs sent no automatic recap request and stayed at "updating"; with debug logging the Gateway logged "Activity recap background work failed {"error":"Plugin invocation scope is closed"}". With this change two Codex runs each sent one recap request and the summary became current. The RPC-requested recap works on both, plain OpenAI runs recap on both, and plugin-invocation-scope.test.ts passes on both, so genuinely closed scopes still throw.

Co-authored-by: Ayaan Zaidi <[email protected]>
obviyus added a commit that referenced this pull request Nov 10, 2026
…with "Plugin invocation scope is closed" (#196750)

fix(reply): deliver session-targeted cron and system-event replies outside a closed plugin scope

Replies of cron jobs targeted at a conversation (sessionTarget "session:<key>") and replies to system-event wakes were never delivered. Delivery ran inside the triggering run's plugin invocation scope, which had already closed, so the channel send failed with "Plugin invocation scope is closed" and the run recorded a delivery error. Session-event delivery now runs outside that scope with pluginInvocationContext.exit, the same pattern as #196700 and #191126.

Evidence: a built Gateway on node 24.22.1 in secretless Docker with a scripted local model and a stand-in Telegram Bot API recording every sendMessage, two runs each. On main a session-targeted cron run (openclaw cron run) and an openclaw system event --mode now wake delivered nothing, and the run recorded "Failed to route reply to telegram: Plugin invocation scope is closed". With this change both replies are delivered once. A normal chat turn and an isolated announce cron job deliver on both, and plugin-invocation-scope tests pass on both, so closed scopes still throw. Details: #196750 (comment).

Closes #196749. Thanks @masatohoshino.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 11, 2026
…closed plugin scope (openclaw#196700)

fix(gateway): run the automatic activity recap outside the finished run's plugin scope

When a Codex run ended, the automatic Activity recap never sent its request and the recap stayed at "updating". The Codex runtime emits the run-end event inside the run's plugin invocation scope, and the recap service scheduled the recap from that event without leaving the scope (since openclaw#169622). Since openclaw#184243, new prepared-model scopes take the caller's scope as their parent, so when the recap checked utility model auth after the run had finished, it hit "Plugin invocation scope is closed", and its own error handling hit the same closed scope. The run-end recap is now scheduled inside pluginInvocationContext.exit(...), the same pattern openclaw#191126 used for openclaw#191123. The RPC and archive recap paths are unchanged.

Evidence: a built Gateway in Docker with the real Codex app-server and a stand-in endpoint. On main, two Codex runs sent no automatic recap request and stayed at "updating"; with debug logging the Gateway logged "Activity recap background work failed {"error":"Plugin invocation scope is closed"}". With this change two Codex runs each sent one recap request and the summary became current. The RPC-requested recap works on both, plain OpenAI runs recap on both, and plugin-invocation-scope.test.ts passes on both, so genuinely closed scopes still throw.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 11, 2026
…with "Plugin invocation scope is closed" (openclaw#196750)

fix(reply): deliver session-targeted cron and system-event replies outside a closed plugin scope

Replies of cron jobs targeted at a conversation (sessionTarget "session:<key>") and replies to system-event wakes were never delivered. Delivery ran inside the triggering run's plugin invocation scope, which had already closed, so the channel send failed with "Plugin invocation scope is closed" and the run recorded a delivery error. Session-event delivery now runs outside that scope with pluginInvocationContext.exit, the same pattern as openclaw#196700 and openclaw#191126.

Evidence: a built Gateway on node 24.22.1 in secretless Docker with a scripted local model and a stand-in Telegram Bot API recording every sendMessage, two runs each. On main a session-targeted cron run (openclaw cron run) and an openclaw system event --mode now wake delivered nothing, and the run recorded "Failed to route reply to telegram: Plugin invocation scope is closed". With this change both replies are delivered once. A normal chat turn and an isolated announce cron job deliver on both, and plugin-invocation-scope tests pass on both, so closed scopes still throw. Details: openclaw#196750 (comment).

Closes openclaw#196749. Thanks @masatohoshino.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to clawhaven/openclaw that referenced this pull request Nov 23, 2026
…nclaw#191126)

(cherry picked from commit 5a160d1)

Release adaptation: Cleanup exits finished invocation context while retaining active cleanup lease; later recap and event dispatch changes are different consumers.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Retained plugin cleanup inherits a closed caller scope

1 participant