openclaw - 💡(How to fix) Fix [Feature] Allow subagent.run in agent_end hook (async spawn without request context) [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#51518Fetched 2026-04-08 01:10:06
View on GitHub
Comments
1
Participants
2
Timeline
1
Reactions
0
Participants
Timeline (top)
commented ×1

Error Message

Error: Plugin runtime subagent methods are only available during a gateway request.

Fix Action

Fix / Workaround

Current workaround

Code Example

Error: Plugin runtime subagent methods are only available during a gateway request.
RAW_BUFFERClick to expand / collapse

Problem

Plugin hooks like agent_end fire after the gateway request context has ended. At this point, api.runtime.subagent.run throws:

Error: Plugin runtime subagent methods are only available during a gateway request.

This makes it impossible for plugins to spawn background subagents in response to agent completion — which is the most natural time to do things like:

  • Background reflection/review (à la Hermes agent nudge)
  • Post-turn memory consolidation
  • Async follow-up tasks

Current workaround

Use api.runtime.system.enqueueSystemEvent() to inject a prompt into the main session queue. This works but runs in the main session (not isolated), consuming context and blocking the user.

Proposed solution

Provide an async subagent spawn API that does not require an active gateway request context. Either:

  1. Make subagent.run work in agent_end hooks (deferred execution after hook completes)
  2. Add a separate api.runtime.subagent.runAsync() that queues the subagent spawn independently of the request lifecycle
  3. Expose a fire-and-forget spawn via api.runtime.system.enqueueSubagentRun(params)

Use case: nudge plugin

I built an OpenClaw plugin that counts agent_end events and triggers a background reflection every N turns (inspired by Hermes agent's nudge mechanism). The natural implementation is to spawn a subagent for isolated reflection with deliver: true so results appear in the main session — but the current API restriction prevents this.

Environment

  • OpenClaw 2026.3.13 (5af75b3)
  • Plugin hook: agent_end
  • API attempted: api.runtime.subagent.run

extent analysis

Fix Plan

To address the issue, we will implement a new API method api.runtime.subagent.runAsync() that allows plugins to spawn background subagents without requiring an active gateway request context.

Step-by-Step Solution

  • Introduce a new method runAsync in the subagent module:
// api/runtime/subagent.js
async function runAsync(params) {
  // Queue the subagent spawn independently of the request lifecycle
  const queue = await getSubagentQueue();
  queue.push(params);
}

// Export the new method
module.exports = { ...module.exports, runAsync };
  • Create a separate queue to manage subagent spawns:
// api/runtime/subagentQueue.js
const subagentQueue = [];

async function getSubagentQueue() {
  return subagentQueue;
}

async function processQueue() {
  while (subagentQueue.length > 0) {
    const params = subagentQueue.shift();
    // Spawn the subagent
    await spawnSubagent(params);
  }
}

// Process the queue periodically
setInterval(processQueue, 1000);
  • Update the agent_end hook to use the new runAsync method:
// plugin.js
api.hooks.agent_end.tap('MyPlugin', async (agent) => {
  const params = { /* subagent params */ };
  await api.runtime.subagent.runAsync(params);
});

Verification

To verify the fix, test the agent_end hook with the new runAsync method and ensure that the subagent is spawned successfully in the background.

Extra Tips

  • Make sure to handle errors and edge cases when implementing the new runAsync method.
  • Consider adding a timeout or retry mechanism to the subagent queue processing to ensure reliable execution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING