← Back to PRs

#12584: feat(hooks): wire outbound message lifecycle hooks

by vincentkoc open 2026-02-09 10:34 View on GitHub →
docs gateway stale size: M
## Why Message lifecycle hooks were not fully wired across outbound delivery paths, limiting observability and intervention points for message guardrails. This PR completes outbound lifecycle wiring and propagates session context so hook consumers can attribute events reliably. ## Detailed Changes - Wired outbound plugin hooks in delivery pipeline: - `message_sending` - `message_sent` (success/failure/cancel-aware) - Added message hook context fields in outbound flow: - `sessionKey` - `sessionId` - Added `sessionKey` into `message_received` hook context from dispatch flow - Applied session-key normalization touch-up in dispatch store lookup path ## Related Links and Issues - Existing source PR: #9761 - Related issues: - Closes #8807 - Closes #7067 - Closes #9871 - #9387 - #9524 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR completes wiring for outbound message lifecycle plugin hooks by invoking `message_sending` (with cancel/modify support) and `message_sent` (success/failure/cancel-aware) across outbound delivery paths, including text chunking, Signal chunk formatting, and media sends. It also extends hook context with `sessionKey`/`sessionId` and propagates these fields through several call sites (gateway send, cron isolated-agent, heartbeat, auto-reply routing) so hook consumers can better attribute events. Core changes live in `src/infra/outbound/deliver.ts`, which now runs the hooks around per-chunk/per-attachment sends and ensures failures are attributed to the attempted content. Tests were expanded substantially in `src/infra/outbound/deliver.test.ts` to cover chunk rewrite limits, cancel behavior, and accurate failure reporting. <h3>Confidence Score: 4/5</h3> - This PR is close to safe to merge, but there is a concrete session-context propagation bug that undermines the stated observability goal in at least one outbound send path. - Most hook wiring changes in `deliver.ts` are internally consistent and are backed by new regression tests for chunk rewrite/cancel/failure attribution. However, `sendMessage()` appears to pass `sessionKey` from the mirror config rather than from the actual session context, so hook consumers will still miss session attribution in common direct-send flows unless this is corrected. - src/infra/outbound/message.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs