Skip to content

feat(hooks): add session:before_compact and session:after_compact internal hook events#8244

Closed
kephail wants to merge 1 commit into
openclaw:mainfrom
kephail:feature/compaction-internal-hooks
Closed

feat(hooks): add session:before_compact and session:after_compact internal hook events#8244
kephail wants to merge 1 commit into
openclaw:mainfrom
kephail:feature/compaction-internal-hooks

Conversation

@kephail

@kephail kephail commented Feb 3, 2026

Copy link
Copy Markdown

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:

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 Overview

Greptile Summary

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.

Confidence Score: 4/5

  • 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

(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!

…ernal hook events

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 sessionFile, sessionId, cfg
- session:after_compact: Fires after compaction with tokensBefore, tokensAfter, etc.

The before_compact hook is awaited to ensure hooks can capture content synchronously.
The after_compact hook is fire-and-forget to not block the compaction return.
@openclaw-barnacle openclaw-barnacle Bot added the agents Agent runtime and tooling label Feb 3, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +436 to +439
// Trigger before_compact hook (allows hooks to capture session content)
const sessionKey = params.sessionKey?.trim() || params.sessionId;
const beforeCompactEvent = createInternalHookEvent(
"session",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] session:after_compact is fired even if session.compact() throws, so consumers can’t reliably interpret it as “compaction completed”.

Because the after-hook is only triggered on the success path (after const result = await session.compact(...)), any error during compaction will skip this event entirely. If hooks use after_compact for cleanup/metrics, they’ll miss failures. Consider either (a) emitting a separate failure event (e.g. session:compact_error) or (b) moving the after_compact trigger into a finally with an ok/error field in context, depending on intended semantics.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/compact.ts
Line: 436:439

Comment:
[P1] `session:after_compact` is fired even if `session.compact()` throws, so consumers can’t reliably interpret it as “compaction completed”.

Because the after-hook is only triggered on the success path (after `const result = await session.compact(...)`), any error during compaction will skip this event entirely. If hooks use `after_compact` for cleanup/metrics, they’ll miss failures. Consider either (a) emitting a separate failure event (e.g. `session:compact_error`) or (b) moving the `after_compact` trigger into a `finally` with an `ok`/`error` field in context, depending on intended semantics.

How can I resolve this? If you propose a fix, please make it concise.

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added stale Marked as stale due to inactivity and removed stale Marked as stale due to inactivity labels Feb 21, 2026
@mudrii

This comment was marked as spam.

@jalehman

jalehman commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

before_compaction and after_compaction hooks have since been added upstream, superseding this PR. Thank you though!

@jalehman jalehman closed this Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants