← Back to PRs

#3749: fix(plugins): invoke before_compaction and after_compaction hooks during compaction

by taronsung open 2026-01-29 04:00 View on GitHub →
agents
## 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