feat(hooks): add session:before_compact and session:after_compact internal hook events#8244
feat(hooks): add session:before_compact and session:after_compact internal hook events#8244kephail wants to merge 1 commit into
Conversation
…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.
| // Trigger before_compact hook (allows hooks to capture session content) | ||
| const sessionKey = params.sessionKey?.trim() || params.sessionId; | ||
| const beforeCompactEvent = createInternalHookEvent( | ||
| "session", |
There was a problem hiding this 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.
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.bfc1ccb to
f92900f
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
This comment was marked as spam.
This comment was marked as spam.
|
|
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 beforesession.compact()with context:sessionFile,sessionId,cfgsession:after_compact: Fires after compaction with context:sessionFile,sessionId,tokensBefore,tokensAfter,cfgBehavior
Use Case
Memory extraction hooks can now listen for
session:before_compactto capture conversation facts before context is compacted, preventing information loss during auto-compaction.Example hook handler:
Greptile Overview
Greptile Summary
This PR adds two new internal hook events around session compaction in the embedded runner (
session:before_compactawaited;session:after_compactfire-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
(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!