#3749: fix(plugins): invoke before_compaction and after_compaction hooks during compaction
agents
Cluster:
Compaction Hooks Enhancement
## Summary
Fixes #3728
The `before_compaction` and `after_compaction` plugin hooks were defined in `plugins/hooks.ts` but never invoked during the actual compaction pipeline. This PR adds the missing hook invocations in `compact.ts`.
## Changes
- Added import for `getGlobalHookRunner` from `hook-runner-global.js`
- Call `runBeforeCompaction` before `session.compact()` with:
- `messageCount`: Number of messages before compaction
- `tokenCount`: Estimated token count before compaction
- Call `runAfterCompaction` after `session.compact()` with:
- `messageCount`: Number of messages after compaction
- `tokenCount`: Tokens before compaction (from SDK result)
- `compactedCount`: Number of messages removed
## Use Cases Enabled
Plugins can now:
- Flush external context stores before compaction
- Inject recovery instructions after compaction
- Coordinate with external memory systems (e.g., GPT-OSS style local memory co-processors)
## Testing
- [x] Linter passes (0 errors, 0 warnings)
- [x] TypeScript compiles without errors
- [x] Existing compaction tests pass (7/7)
- [x] Overflow compaction tests pass (4/4)
## AI-Assisted
- [x] Mark as AI-assisted in the PR title or description
- [x] Note the degree of testing: **lightly tested** (lint + existing tests)
- [x] Confirm I understand what the code does
This PR was generated with Claude assistance. The fix follows the existing pattern for hook invocation used elsewhere in the codebase (e.g., `runBeforeAgentStart` in `run/attempt.ts`).
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR wires the previously-defined plugin compaction hooks into the embedded Pi session compaction flow. It imports the global hook runner and invokes `before_compaction` immediately before `session.compact()` and `after_compaction` immediately after, passing message counts and token estimates/SDK token counts plus a computed `compactedCount`.
Overall this is a small, localized change in `src/agents/pi-embedded-runner/compact.ts` that completes the hook lifecycle in the compaction pipeline, consistent with other hook invocations (e.g., `before_agent_start`).
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, with minor behavioral/expectation considerations for plugin authors.
- Changes are confined to adding two hook invocations around an existing compaction call, with errors caught and logged. Main risk is semantic (hooks firing even on no-op compactions) and minor extra work from token estimation loops.
- src/agents/pi-embedded-runner/compact.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#13861: feat(hooks): add session:compaction hook event
by lailoo · 2026-02-11
87.1%
#16915: fix: await compaction hooks with timeout to prevent cross-session d...
by maximalmargin · 2026-02-15
86.1%
#8244: feat(hooks): add session:before_compact and session:after_compact i...
by kephail · 2026-02-03
85.6%
#16788: feat(hooks): emit compaction lifecycle hooks
by vincentkoc · 2026-02-15
84.5%
#20184: feat: memory plugin compaction control
by solstead · 2026-02-18
80.0%
#14887: feat(compaction): configurable auto-compaction notifications with o...
by seilk · 2026-02-12
79.1%
#4042: agents: add proactive compaction before request
by freedomzt · 2026-01-29
78.6%
#8961: feat: smarter compaction tool truncation + token count in system pr...
by SocialNerd42069 · 2026-02-04
78.0%
#17445: fix(pi-embedded): add aggregate timeout to compaction retry + harde...
by joeykrug · 2026-02-15
78.0%
#14021: feat(compaction): optional memory flush before manual /compact
by phenomenoner · 2026-02-11
77.8%