← Back to PRs

#13861: feat(hooks): add session:compaction hook event

by lailoo open 2026-02-11 03:54 View on GitHub →
docs agents stale
## 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