Skip to content

fix(subagents): subagent results sent straight to the chat fail with "Plugin invocation scope is closed" - #196940

Merged
obviyus merged 2 commits into
mainfrom
fix/plugin-scope-delivery-paths
Nov 11, 2026
Merged

obviyus merged 2 commits into
mainfrom
fix/plugin-scope-delivery-paths

Conversation

@obviyus

@obviyus obviyus commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #196899

Problem

When a subagent finishes and its result has to be sent to the parent chat directly (for example a Telegram DM, where the completion falls back to sending the child's result as text), the send fails with Plugin invocation scope is closed. The announcement is retried and fails the same way every time, so the user never gets the result. The same failure makes the QA smoke scenarios subagent-completion-direct-fallback and subagent-empty-completion-recovery time out on main since #196570 (#196899).

Root cause

The announcement is started from the child's finished turn and runs in that turn's async context, which still carries the child's plugin invocation scope. That scope is released when the turn ends. When the announcement then calls the channel plugin (Telegram's target lookup and send), the plugin call looks up the released scope and throws.

Fix

sendSubagentAnnounceDirectly runs with pluginInvocationContext.exit(...), so the announcement no longer inherits the finished turn's scope and channel calls enter their own plugin owner. This is the same pattern as #196750 and #196700. Closed scopes still throw; no retry, timeout or delivery rule changes.

ChuLaiTheGreat found this path in #171315 and #178125. #178125 carries the same one-line fix inside a Telegram progress feature (with a new SDK callback that needs an owner decision), so this PR lands only the fix. Thanks @ChuLaiTheGreat.

Evidence

A built Gateway on Node 24.22.1 in secretless Docker, with a scripted local stand-in model and a stand-in Telegram Bot API that records every sendMessage. In a Telegram DM the model spawns a subagent; the child answers CHILD_RESULT_42, and the parent's completion turn answers in plain text, so the result is sent directly as text. Two runs each on main ff392dbeb0 and this change 7814ecdb87.

Case main 1 main 2 fix 1 fix 2
DM subagent result reaches the chat no (4 tries, all Plugin invocation scope is closed) no (same) once once
Control: normal reply once once once once
Control: group subagent completion (agent reply path) once once once once
Control: provider failure (HTTP 500) shows the failure notice once once once once
Control: steered follow-up answered NO_REPLY keeps the earlier answer (#196769) once once once once
Control: session-targeted cron reply (#196750) once once once once

Plugin invocation scope is closed appears 4 times in each main Gateway log (Subagent completion direct announce failed) and never with this change. No message was sent twice.

The QA smoke scenarios from #196899 could not be run here: in the Docker container the QA Telegram transport fails before any scenario starts (Crabline Telegram getMe probe failed with HTTP 500), on main, on this change and for the telegram-status-command control alike. #196899 reports that applying this same change to main makes both scenarios pass locally (2 of 2 each).

Test: subagent-announce-delivery.test.ts adds a case that routes a Telegram completion after the child's plugin scope is released. It fails on main (delivered: false) and passes with this change; the file nets zero test lines (removed four one-line fixture wrappers). plugin-invocation-scope.test.ts passes on both (closed scopes still throw).

@clawsweeper

clawsweeper Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@obviyus

obviyus commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Nov 11, 2026
@clawsweeper

clawsweeper Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@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 11, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. 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. and removed 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 11, 2026
@clawsweeper

clawsweeper Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR detaches subagent announcement routing from the finished child's plugin invocation scope.

Example: A Telegram DM subagent returns CHILD_RESULT_42 and completion delivery falls back to text.

  • Before: Routing throws "Plugin invocation scope is closed", and retries never deliver the result.
  • After: Host-owned routing enters the channel plugin independently and sends the result once.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A minimal, source-supported correction with a discriminating regression and reported Gateway before/after results; behavioral runs use synthetic external services.
Proof confidence 🐚 platinum hermit (4/6) This collaborator-authored PR is outside the ordinary contributor proof gate; its body reports built-Gateway Telegram DM runs on Node 24.22.1 with scripted external services, producing exactly one CHILD_RESULT_42 send after the production correction at 7814ecd, whose production bytes are unchanged at the reviewed head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A completed subagent's result never reaches the parent chat when direct text delivery inherits the child's closed plugin scope.
Reason: The correction restores documented completion delivery with an existing lifecycle mechanism, and the collaborator-authored PR supplies the direction decision.

Regression provenance

Possible regression — suspected (failure trace). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR provides a focused correction for a current-main completion-delivery failure, with no actionable patch defects found.

Priority: P1
Reviewed head: 1adb82191505eaeb3198554b3ade0f5f93378975

Before merge

None.

Findings

None.

Agent review details

How this fits together

The announcement owner receives completed child events, resolves the requester and destination, and either dispatches a requester turn or sends a direct completion through the channel plugin.

flowchart TD
  A[Child completion] --> B[Host announcement routing]
  B --> C[Requester and source checks]
  C --> D[Requester agent turn]
  D --> E[Direct completion fallback]
  E --> F[Channel plugin owner]
  F --> G[Parent chat]
Loading

Technical review

Best possible solution:

Land the focused scope-exit correction while preserving the existing requester, cancellation, plugin-revocation, and delivery checks.

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

The wrapped channel classifier reaches PluginInvocationScope.lookup after the inherited scope is released, explaining the reported failure; the linked issue supplies repeated before/after runs, but no target code was executed during this review.

Is this the best way to solve the issue?

Exiting the finished producer's plugin context at the existing host announcement owner is the smallest complete fix; weakening closed-scope rejection or changing retry deadlines would leave the lifecycle mismatch unresolved.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Requester delivery ownership keeps the original intent (5c78dc3: Consolidate requester authority composition in withSubagentRequesterDelivery while preserving caller guards.)
  • Direct announcement module keeps the original intent (ae6f813: Separate announcement responsibilities into existing delivery owners without changing behavior.)
  • Delivery regression fixtures keeps the original intent (test(agents): consolidate subagent-announce-delivery.test.ts #166029: Consolidate duplicated setup while retaining distinct delivery, lifecycle, and authority cases.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

PR surface

Source +5, Tests 0. Total +5 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 9 4 +5
Tests 1 36 36 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 45 40 +5

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P1: The current-main failure prevents direct subagent results from reaching their parent chat and repeatedly breaks the corresponding smoke scenarios.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P1: The current-main failure prevents direct subagent results from reaching their parent chat and repeatedly breaks the corresponding smoke scenarios.
  • 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.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 10, 2026, 7:51 PM ET / November 11, 2026, 00:51 UTC.

@obviyus
obviyus merged commit afa73f3 into main Nov 11, 2026
139 checks passed
@obviyus
obviyus deleted the fix/plugin-scope-delivery-paths branch November 11, 2026 00:54
@obviyus

obviyus commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

Landed in afa73f3. Thanks @amittell and @ChuLaiTheGreat.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 11, 2026
…"Plugin invocation scope is closed" (openclaw#196940)

fix(subagents): deliver direct subagent announcements outside the finished turn's plugin scope

A finished subagent's announcement started from the child's completed turn and kept that turn's plugin invocation scope, which is released when the turn ends. The channel target lookup and send then failed with "Plugin invocation scope is closed", so the result never reached the chat after its retries. sendSubagentAnnounceDirectly now runs inside pluginInvocationContext.exit, the same pattern as openclaw#196750 and openclaw#196700. ChuLaiTheGreat found this path in openclaw#171315 and openclaw#178125; openclaw#178125 carries the same fix inside a feature.

Evidence: a built Gateway on node:24.22.1 in secretless Docker with a stand-in model and a stand-in Telegram Bot API recording every sendMessage, two runs each. On main a DM subagent result was never delivered and each run logged 4 "Plugin invocation scope is closed" errors. With this change it is delivered once with no scope errors. A normal reply, a group subagent completion, the provider-failure notice, a steered follow-up answered NO_REPLY and a session-targeted cron reply are each delivered once on both, and plugin-invocation-scope tests pass on both, so closed scopes still throw. The two QA Smoke scenarios from openclaw#196899 could not run locally because the QA Telegram transport fails before any scenario starts on main and this branch alike.

Closes openclaw#196899. Thanks @amittell for the report and @ChuLaiTheGreat for finding the path.

Co-authored-by: Ayaan Zaidi <[email protected]>
obviyus added a commit that referenced this pull request Nov 12, 2026
…n already ended (#197658)

fix(gateway): run subagent announce Gateway calls outside the finished turn's plugin scope

When a child agent failed after its parent's turn had already ended, the completion announce's in-process Gateway calls (chat.history, and the related sessions.delete and chat.abort) ran inside the parent turn's closed plugin invocation scope. The chat.history read asks plugins which harness to use, so it failed with "Subagent announce failed: Plugin invocation scope is closed" and the parent never learned the child had failed. These in-process calls now run outside the closed scope, the same pattern as #196940, and go through normal Gateway admission, so nothing is granted or widened. No text changes.

Evidence: a built Gateway in a secretless node:24.22.1 container with a stand-in model, two runs each. With an isolated cron run whose owed child's model call returns HTTP 400, main logged the scope error four times per run and the parent was never woken; with this change there are no scope errors and the parent is woken once with the child's failure. A non-cron failing child behaved the same way. A succeeding child, a fire-and-forget child, a cron run without a spawn and a plain non-cron announce deliver exactly once on both, with no duplicate announce. A closed-scope history read in the existing closed-caller test fails on main, at net 0 test lines. Details: #197658 (comment).

Thanks @dankarization for reporting this bug family in #197335.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 13, 2026
…n already ended (openclaw#197658)

fix(gateway): run subagent announce Gateway calls outside the finished turn's plugin scope

When a child agent failed after its parent's turn had already ended, the completion announce's in-process Gateway calls (chat.history, and the related sessions.delete and chat.abort) ran inside the parent turn's closed plugin invocation scope. The chat.history read asks plugins which harness to use, so it failed with "Subagent announce failed: Plugin invocation scope is closed" and the parent never learned the child had failed. These in-process calls now run outside the closed scope, the same pattern as openclaw#196940, and go through normal Gateway admission, so nothing is granted or widened. No text changes.

Evidence: a built Gateway in a secretless node:24.22.1 container with a stand-in model, two runs each. With an isolated cron run whose owed child's model call returns HTTP 400, main logged the scope error four times per run and the parent was never woken; with this change there are no scope errors and the parent is woken once with the child's failure. A non-cron failing child behaved the same way. A succeeding child, a fire-and-forget child, a cron run without a spawn and a plain non-cron announce deliver exactly once on both, with no duplicate announce. A closed-scope history read in the existing closed-caller test fails on main, at net 0 test lines. Details: openclaw#197658 (comment).

Thanks @dankarization for reporting this bug family in openclaw#197335.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 14, 2026
…#197658)

Exit the completed caller's plugin invocation scope for lifecycle history,
abort and delete dispatch. Retain the captured Gateway owner, least-privilege
method dispatch, request guard and pre/post-effect current-owner checks.

Release adaptation: apply the identical production repair in the release's
server-recovery-runtime-context.ts rather than importing main's renamed
lifecycle runtime. Use the existing hosted Gateway fixture and current
PluginInstance/PluginInvocationScope APIs to prove closed-caller history
reads; no new fixture or runtime architecture. Allocate 31 regression lines.

Companion audit: no later same-path or PR-number follow-ups through
7bccde6. Recovery dispatcher, announcement
history consumer, plugin admission and live owner guards are present.
The related #196940 direct-to-channel repair is a separate sibling path,
not a prerequisite of this lifecycle read; report it for parent disposition.

Proof: 13 hosted lifecycle and recovery-context tests pass (145.94s wrapper,
20ms new regression). Type-aware oxlint, formatting and diff check pass.
Full core compiler and live cron/provider proof remain qualification work.
Baseline negative-control execution was not run.

(cherry picked from commit 4d9f782)

Co-authored-by: Ayaan Zaidi <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
Co-authored-by: vincentkoc <[email protected]>
(cherry picked from commit 9adc76a6976dc17213eb6d221b2b15ee93015cb0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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.

[Bug]: QA smoke subagent completion scenarios time out on main since #196570

1 participant