#11872: Add Pre-Reset Hook
gateway
stale
size: S
The agent isn't very vigilant about committing things to memory, and it's unnatural to force them to write to memory while they are still working. It also leads to a journal littered with exploration dead ends.
A more productive way would be to ask them to summarize notable things after the session breaks. It's possible to do this for implicit breaks using heartbeats, but for explicit `/reset` initiated by user, the context is immediately wiped.
A pre-reset hook allows the agent to be prompted to commit important bits they have learned, and/or unfinished businesses, to memory, in order that they will make future sessions more relevant. This is an optional setting which has no effect if unset.
Fully tested, has been running in my local instance for almost a week.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds an optional `session.preReset` config (types + zod schema) and wires a new `runPreResetHook()` into both chat-triggered resets (`initSessionState` on `/reset`-style triggers) and the gateway `sessions.reset` endpoint. The hook performs a best-effort agent turn (non-delivered) with a configurable prompt and timeout, intended to let the agent write wrap-up notes to memory before the session is cleared.
<h3>Confidence Score: 4/5</h3>
- Mostly safe to merge, but there is at least one correctness issue in the gateway reset hook wiring that can cause the hook to be skipped unexpectedly.
- The feature is isolated and guarded by config, but the gateway `sessions.reset` path appears to load the session entry using the raw request key rather than the resolved canonical key, which can yield an undefined entry and cause `skipIfEmpty` to skip incorrectly. Aside from that, the hook is best-effort and failures are contained/logged.
- src/gateway/server-methods/sessions.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</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
#12615: Add configurable resetPrompt option to config, customizing prompt a...
by htahboub · 2026-02-09
75.5%
#13032: feat(hooks): add unified session:before_end hook event
by TGambit65 · 2026-02-10
75.2%
#10591: feat(hooks): add session-start-memory bundled hook
by morningstar-daemon · 2026-02-06
73.3%
#8919: Pr/memory flush improvements
by shortbus · 2026-02-04
73.2%
#14243: fix: fire session-memory hook on auto-resets + topic-aware memory p...
by TheDude135 · 2026-02-11
73.1%
#21155: reply: add cancellable before_reset guard for /new and /reset
by LiShengYang-yiyi · 2026-02-19
72.9%
#7771: Hooks: wire lifecycle events and tests
by rabsef-bicrym · 2026-02-03
71.7%
#7091: feat: add pre-answer hooks system for automatic context injection
by dizhaky · 2026-02-02
71.6%
#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]
by tsukhani · 2026-02-02
71.5%
#6853: fix: fire internal hooks on sessions.reset RPC (TUI/webchat /new)
by hamiltonchua · 2026-02-02
71.3%