#13032: feat(hooks): add unified session:before_end hook event
size: S
This PR adds a new `session:before_end` hook event that fires before a session is cleared (e.g., via `/new`, `/reset`, `/compact`, or auto-reset).
This allows plugins to perform cleanup tasks like context summarization or memory flushing before the session transcript is lost.
Relates to Discussion #12164 and PR #12162.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds a new plugin hook event (`session_before_end`) intended to fire right before a session is cleared (manual compaction and session resets/auto-resets). It wires new hook runner entrypoints in `src/plugins/hooks.ts` and attempts to invoke the hook from the compaction command and from session initialization when a reset is detected.
Key issues to address before merge:
- The new shared plugin registry (`getPluginRegistry`) is never initialized (no call to `setPluginRegistry`), so the hook will never run.
- `session_before_end` is documented as sequential but is currently executed in parallel because it uses `runVoidHook`.
- The reset “reason” mapping misclassifies explicit resets (e.g. `/reset`) as `auto`.
- The hook is awaited during `initSessionState`, so a slow plugin can directly delay handling incoming messages (needs an explicit decision on timeout/backgrounding vs blocking semantics).
<h3>Confidence Score: 2/5</h3>
- This PR is not safe to merge until the hook wiring and execution semantics are corrected.
- The `session_before_end` hook currently won’t fire at all due to an uninitialized shared registry, and the implementation contradicts its own sequential-execution contract (it runs handlers in parallel). Additionally, explicit reset reasons are misreported, and the hook can block session initialization with unbounded plugin runtime.
- src/plugins/registry.ts, src/plugins/hooks.ts, src/auto-reply/reply/session.ts
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#9782: feat(hooks): implement session:start and session:end lifecycle events
by kentaro · 2026-02-05
85.5%
#8244: feat(hooks): add session:before_compact and session:after_compact i...
by kephail · 2026-02-03
83.6%
#8431: Hooks: add session-graphiti memory feed
by JorgeAlan · 2026-02-04
78.9%
#12581: feat(hooks): emit session prune lifecycle event
by vincentkoc · 2026-02-09
78.7%
#6853: fix: fire internal hooks on sessions.reset RPC (TUI/webchat /new)
by hamiltonchua · 2026-02-02
78.0%
#3392: fix(hooks): remove debug console.log statements from session-memory...
by WinJayX · 2026-01-28
77.9%
#13861: feat(hooks): add session:compaction hook event
by lailoo · 2026-02-11
76.5%
#7771: Hooks: wire lifecycle events and tests
by rabsef-bicrym · 2026-02-03
76.3%
#3749: fix(plugins): invoke before_compaction and after_compaction hooks d...
by taronsung · 2026-01-29
75.8%
#16788: feat(hooks): emit compaction lifecycle hooks
by vincentkoc · 2026-02-15
75.7%