#8244: feat(hooks): add session:before_compact and session:after_compact internal hook events
agents
stale
Cluster:
Compaction Hooks Enhancement
Adds internal hook events that fire before and after session compaction, allowing hooks like memory-extract to capture session content before it's summarized.
## Events
- `session:before_compact`: Fires before `session.compact()` with context: `sessionFile`, `sessionId`, `cfg`
- `session:after_compact`: Fires after compaction with context: `sessionFile`, `sessionId`, `tokensBefore`, `tokensAfter`, `cfg`
## Behavior
- **before_compact** is awaited to ensure hooks can capture session content synchronously before compaction modifies/summarizes it
- **after_compact** is fire-and-forget to not block the compaction return
## Use Case
Memory extraction hooks can now listen for `session:before_compact` to capture conversation facts before context is compacted, preventing information loss during auto-compaction.
Example hook handler:
```typescript
if (
(event.type === 'command' && event.action === 'new') ||
(event.type === 'session' && event.action === 'before_compact')
) {
const content = fs.readFileSync(event.context.sessionFile, 'utf-8');
// Extract facts from content before compaction...
}
```
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adds two new internal hook events around session compaction in the embedded runner (`session:before_compact` awaited; `session:after_compact` fire-and-forget) and documents them in the bundled hooks README. The events include session identifiers and compaction token stats so bundled/custom hooks (e.g., memory extraction) can capture session state before summarization alters it.
<h3>Confidence Score: 4/5</h3>
- This PR is generally safe to merge; it’s a small, localized behavior change adding hook emissions around compaction.
- Changes are confined to emitting internal hook events and updating documentation. Main risk is semantic/contract mismatch for hook consumers (e.g., after_compact not firing on failures), but there are no obvious runtime/type issues introduced.
- src/agents/pi-embedded-runner/compact.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#13861: feat(hooks): add session:compaction hook event
by lailoo · 2026-02-11
89.1%
#3749: fix(plugins): invoke before_compaction and after_compaction hooks d...
by taronsung · 2026-01-29
85.6%
#16788: feat(hooks): emit compaction lifecycle hooks
by vincentkoc · 2026-02-15
83.8%
#13032: feat(hooks): add unified session:before_end hook event
by TGambit65 · 2026-02-10
83.6%
#16915: fix: await compaction hooks with timeout to prevent cross-session d...
by maximalmargin · 2026-02-15
80.0%
#20184: feat: memory plugin compaction control
by solstead · 2026-02-18
79.3%
#9782: feat(hooks): implement session:start and session:end lifecycle events
by kentaro · 2026-02-05
79.3%
#10591: feat(hooks): add session-start-memory bundled hook
by morningstar-daemon · 2026-02-06
78.6%
#8431: Hooks: add session-graphiti memory feed
by JorgeAlan · 2026-02-04
78.6%
#3392: fix(hooks): remove debug console.log statements from session-memory...
by WinJayX · 2026-01-28
78.2%