feat: add forked subagent context

This commit is contained in:
Peter Steinberger
2026-04-23 21:28:45 +01:00
parent abedf9c1f4
commit f5042adf27
19 changed files with 582 additions and 20 deletions

View File

@@ -78,13 +78,15 @@ four lifecycle points:
### Subagent lifecycle (optional)
OpenClaw currently calls one subagent lifecycle hook:
OpenClaw calls two optional subagent lifecycle hooks:
- **prepareSubagentSpawn** — prepare shared context state before a child run
starts. The hook receives parent/child session keys, `contextMode`
(`isolated` or `fork`), available transcript ids/files, and optional TTL.
If it returns a rollback handle, OpenClaw calls it when spawn fails after
preparation succeeds.
- **onSubagentEnded** — clean up when a subagent session completes or is swept.
The `prepareSubagentSpawn` hook is part of the interface for future use, but
the runtime does not invoke it yet.
### System prompt addition
The `assemble` method can return a `systemPromptAddition` string. OpenClaw
@@ -191,7 +193,7 @@ Optional members:
| `bootstrap(params)` | Method | Initialize engine state for a session. Called once when the engine first sees a session (e.g., import history). |
| `ingestBatch(params)` | Method | Ingest a completed turn as a batch. Called after a run completes, with all messages from that turn at once. |
| `afterTurn(params)` | Method | Post-run lifecycle work (persist state, trigger background compaction). |
| `prepareSubagentSpawn(params)` | Method | Set up shared state for a child session. |
| `prepareSubagentSpawn(params)` | Method | Set up shared state for a child session before it starts. |
| `onSubagentEnded(params)` | Method | Clean up after a subagent ends. |
| `dispose()` | Method | Release resources. Called during gateway shutdown or plugin reload — not per-session. |

View File

@@ -98,8 +98,9 @@ sub-agents. It supports:
## Spawning sub-agents
`sessions_spawn` creates an isolated session for a background task. It is always
non-blocking -- it returns immediately with a `runId` and `childSessionKey`.
`sessions_spawn` creates an isolated session for a background task by default.
It is always non-blocking -- it returns immediately with a `runId` and
`childSessionKey`.
Key options:
@@ -107,6 +108,8 @@ Key options:
- `model` and `thinking` overrides for the child session.
- `thread: true` to bind the spawn to a chat thread (Discord, Slack, etc.).
- `sandbox: "require"` to enforce sandboxing on the child.
- `context: "fork"` for native sub-agents when the child needs the current
requester transcript; omit it or use `context: "isolated"` for a clean child.
Default leaf sub-agents do not get session tools. When
`maxSpawnDepth >= 2`, depth-1 orchestrator sub-agents additionally receive