#13861: feat(hooks): add session:compaction hook event
docs
agents
stale
Cluster:
Compaction Hooks Enhancement
## Summary
Fixes #13754
## Problem
No hook event fires after session compaction completes. Agents with persistent memory systems (daily logs, project files) need to re-bootstrap context after compaction, but there is no mechanism to trigger a context re-read.
The `agent:bootstrap` hook only fires on session creation, not after compaction. The planned `session:start` / `session:end` events do not cover this either.
## Fix
Add a `session:compaction` hook event that fires after successful compaction in `compactEmbeddedPiSessionDirect`. The event includes `sessionKey`, `trigger` ("manual" or "auto"), `tokensBefore`, and `tokensAfter` in its context.
### Changes:
- **`src/agents/pi-embedded-runner/compact.ts`**: Add `trigger` param to `CompactEmbeddedPiSessionParams`, fire `session:compaction` hook after successful compaction (best-effort, failures do not break compaction)
- **`src/auto-reply/reply/commands-compact.ts`**: Pass `trigger: "manual"` for `/compact` command
- **`src/agents/pi-embedded-runner/run.ts`**: Pass `trigger: "auto"` for context overflow auto-compaction
- **`docs/automation/hooks.md`**: Document the new Session Events section with `session:compaction` event, context fields, and example handler
- **`src/hooks/internal-hooks.test.ts`**: Add 3 regression tests for the new event
- **`CHANGELOG.md`**: Add entry
## Effect on User Experience
**Before fix:**
After compaction, agents lose awareness of detailed context stored in workspace files (daily logs, project notes). No way to trigger automated re-read. Users rely on AGENTS.md instructions which is unreliable.
**After fix:**
Hooks can listen for `session:compaction` and inject system events (e.g. "Read today's daily log") to recover context after compaction. Both manual `/compact` and auto-compaction on context overflow trigger the hook.
Example hook registration:
```
metadata: { "openclaw": { "events": ["session:compaction"] } }
```
## Testing
- ✅ 21 tests pass (pnpm vitest run src/hooks/internal-hooks.test.ts)
- ✅ Lint passes (oxlint + oxfmt)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds a new internal hook event `session:compaction` that fires after successful session compaction, with context fields (`sessionKey`, `trigger`, `tokensBefore`, `tokensAfter`). The compaction code (`src/agents/pi-embedded-runner/compact.ts`) now triggers the hook best-effort, and callers set `trigger` to distinguish manual `/compact` vs auto-compaction on context overflow. Documentation and internal hook tests were updated accordingly, and the changelog notes the new event.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Changes are localized and consistent: a new hook event is emitted best-effort after successful compaction, with call sites explicitly setting the trigger. Tests cover event delivery to both general and specific handlers and message mutation.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#8244: feat(hooks): add session:before_compact and session:after_compact i...
by kephail · 2026-02-03
89.1%
#3749: fix(plugins): invoke before_compaction and after_compaction hooks d...
by taronsung · 2026-01-29
87.1%
#16915: fix: await compaction hooks with timeout to prevent cross-session d...
by maximalmargin · 2026-02-15
84.0%
#16788: feat(hooks): emit compaction lifecycle hooks
by vincentkoc · 2026-02-15
82.4%
#19565: feat: add agent lifecycle hook events (session, message, error)
by tag-assistant · 2026-02-17
80.0%
#8313: feat: auto-compaction support for spawned subagent sessions
by vishaltandale00 · 2026-02-03
78.0%
#9782: feat(hooks): implement session:start and session:end lifecycle events
by kentaro · 2026-02-05
77.5%
#20184: feat: memory plugin compaction control
by solstead · 2026-02-18
76.6%
#13032: feat(hooks): add unified session:before_end hook event
by TGambit65 · 2026-02-10
76.5%
#4042: agents: add proactive compaction before request
by freedomzt · 2026-01-29
76.4%