#19833: feat: contextScripts pre-spawn hook for sub-agent sessions (rebased on v2026.2.17)
docs
agents
size: L
Cluster:
Agent Messaging Enhancements
## Summary
Rebased version of #13965, updated for the v2026.2.17 refactor that extracted spawn logic into `subagent-spawn.ts` (`spawnSubagentDirect`).
### What changed from #13965
- **Schema**: already merged upstream — no schema changes in this PR
- **Implementation**: `context-scripts.ts` unchanged
- **Wiring**: hook now lives in `subagent-spawn.ts` instead of the old inline `sessions-spawn-tool.ts`
- **Allowlist**: checks both original requested ID and any script-overridden ID
### Files
| File | Change |
|------|--------|
| `src/agents/context-scripts.ts` | New — execution engine (resolve, dedupe, execute) |
| `src/agents/subagent-spawn.ts` | Modified — hook after targetAgentId resolution, before allowlist |
| `docs/concepts/context-scripts.md` | New — concept documentation |
### How it works
1. Before the allowlist check, resolves context scripts from both default and agent-specific config
2. Executes scripts with spawn variables (targetAgentId, task, label, etc.)
3. Scripts can prepend/append to the task and override the target agent ID
4. Allowlist validates both the original and overridden agent IDs
Supersedes #13965.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds a pre-spawn hook system ("context scripts") for sub-agent sessions, allowing scripts to inject context (identity, charters, instructions) into the task message and optionally override the target agent ID before session creation.
- **New file `context-scripts.ts`**: Implements the full context script lifecycle — resolution (merge defaults + per-agent, dedupe, sort by priority), execution (local scripts via `execFile`/`spawn`, HTTP via `fetch`), and output parsing (JSON response pipeline with error detection, content extraction, agent ID override).
- **Modified `subagent-spawn.ts`**: Wires the context scripts hook into `spawnSubagentDirect`, executing between target agent resolution and allowlist validation. The allowlist now checks both the original requested ID and any script-overridden ID.
- **New docs `context-scripts.md`**: Comprehensive concept documentation covering configuration, variable passing, output parsing, and override resolution.
- The HTTP script execution path in `context-scripts.ts` lacks a timeout (`fetch` without `AbortSignal`), which could block spawns indefinitely on unresponsive endpoints — the local script paths correctly enforce a 10-second timeout.
<h3>Confidence Score: 3/5</h3>
- Mostly safe to merge; the missing HTTP timeout is the main concern that should be addressed before merging.
- The core logic is sound — script resolution, deduplication, priority sorting, and the spawn integration are all well-implemented. The allowlist correctly validates both original and overridden agent IDs. However, the missing timeout on HTTP fetch calls in context scripts is a real issue that could cause indefinite hangs during spawn. The outer try/catch in subagent-spawn.ts would not help since the await would never resolve. No tests are included for the new context-scripts module.
- `src/agents/context-scripts.ts` — missing HTTP fetch timeout; `src/agents/subagent-spawn.ts` — minor unreachable code in allowlist error message
<sub>Last reviewed commit: 10359fe</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#20418: feat(hooks): add session:pre-spawn and agent:pre-run hook events
by NOVA-Openclaw · 2026-02-18
78.1%
#17921: feat: add spawnableBy bidirectional sub-agent spawn authorization
by jacobot01 · 2026-02-16
77.4%
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
77.0%
#16247: feat(agents): declarative agent definitions for sessions_spawn
by zerone0x · 2026-02-14
76.8%
#10748: feat: Add sessions.spawn gateway method for direct subagent spawning
by fox-openclaw · 2026-02-06
76.4%
#23166: fix(agents): restore subagent announce chain from #22223
by tyler6204 · 2026-02-22
75.3%
#21556: fix(agents): graceful fallback when spawned model is not in allowlist
by irchelper · 2026-02-20
75.2%
#8471: fix(subagent): add defensive checks for undefined string fields
by adam-smeth · 2026-02-04
74.6%
#11788: feat: inter-agent communication via CLI scripts
by jingkang0822 · 2026-02-08
74.4%
#19329: feat: add per-agent compaction and context pruning overrides
by curtismercier · 2026-02-17
74.2%