← Back to PRs

#21874: feat: add agent_end hook with message injection support

by ava-saint open 2026-02-20 13:47 View on GitHub →
size: L
## Summary Implements message injection for `agent_end` hooks, enabling anti-rationalization gates and auto-continue patterns. ## Changes ### ✅ Completed **1. Type Updates (src/plugins/types.ts)** - Add `lastAssistantMessage?: string` to `PluginHookAgentEndEvent` - Add `PluginHookAgentEndResult { continue?: boolean; message?: string }` - Update handler map to return `PluginHookAgentEndResult | void` **2. Hook Runner (src/plugins/hooks.ts)** - Change `runAgentEnd` from fire-and-forget to result aggregation - Collect results from all hooks in parallel - Return first result with `continue: true` - Maintains backwards compatibility (void-returning hooks work) ### 🚧 In Progress **3. Message Extraction** (src/agents/pi-embedded-runner/run/attempt.ts) - Extract last assistant message text from `messagesSnapshot` - Pass to hook event as `lastAssistantMessage` **4. Message Injection** (src/agents/pi-embedded-runner/run/attempt.ts) - Await hook result instead of fire-and-forget - If `result.continue === true`: - Inject user message with `result.message` - Trigger another agent run **5. Tests** - Unit test for hook return value aggregation - E2E test with mock plugin forcing continuation - Anti-rationalization example plugin ## Use Case Enables plugins like anti-rationalization gates: ```typescript api.on('agent_end', async (event, ctx) => { const judgment = await reviewForRationalization(event.lastAssistantMessage); if (judgment.incomplete) { return { continue: true, message: "You are rationalizing incomplete work. Finish the task." }; } }); ``` ## Prior Art - **pi-mono**: Already has `agent_end` event (OpenClaw's upstream) - **Claude Code**: Has `Stop` hook with prompt-based judgment - **Trail of Bits**: Uses this pattern for anti-rationalization gates ## Status **Draft / WIP** - Types and hook runner done, message injection needs completion. Pausing for review before implementing message injection logic.

Most Similar PRs