← Back to PRs

#20418: feat(hooks): add session:pre-spawn and agent:pre-run hook events

by NOVA-Openclaw open 2026-02-18 22:09 View on GitHub →
gateway agents size: M
New internal hook events for the subagent and agent run lifecycle: - `session:pre-spawn` — fires before a subagent session is created in `spawnSubagentDirect()`, with mutable context including agentId, model, thinking, and task - `agent:pre-run` — fires before an agent run starts in the gateway agent handler, with mutable context including agentId, sessionKey, model, and thinking Both events support context mutation, allowing hook handlers to modify parameters before they take effect. Follows the same pattern as `agent:bootstrap` and `message:received`/`message:sent` events. Example use cases: - Override model/thinking per agent from an external config source - Log or audit spawn/run events - Enforce model policies <!-- greptile_comment --> <h3>Greptile Summary</h3> Adds two new internal hook events (`session:pre-spawn` and `agent:pre-run`) that fire before subagent spawning and agent run execution, respectively. Both support context mutation for overriding model and thinking parameters. Type definitions, type guards, and tests are included. - **Critical bug in `subagent-spawn.ts`**: The call to `resolveSubagentSpawnModelSelection` passes `modelOverrideFromHook` as a shorthand property instead of `modelOverride: modelOverrideFromHook`, meaning model overrides (both from callers and hooks) are silently dropped. - **`agent:pre-run` model mutation is a no-op**: `request.model` is always `undefined` (not in the RPC schema), and the mutated `modelFromHook` is never passed to `agentCommand`. Only `thinking` mutation is effective. - **Test nesting issue**: New test `describe` blocks are placed outside the top-level `describe("hooks", ...)` suite, so they don't benefit from the shared `beforeEach`/`afterEach` cleanup that calls `clearInternalHooks()`. <h3>Confidence Score: 1/5</h3> - This PR has critical bugs that silently break model override functionality in both hook integration points - Two of the three integration bugs are logic errors that silently discard values: the property name mismatch in subagent-spawn.ts breaks existing model override behavior (regression), and the agent:pre-run model mutation path is non-functional. The test nesting issue means tests may not catch state leaks. - Pay close attention to `src/agents/subagent-spawn.ts` (property name mismatch is a regression) and `src/gateway/server-methods/agent.ts` (model override wiring is incomplete) <sub>Last reviewed commit: 4c391bd</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs