Skip to content

feat(hooks): emit session prune lifecycle event#12581

Open
vincentkoc wants to merge 11 commits intoopenclaw:mainfrom
vincentkoc:vincentkoc-code/hooks-pruning-event
Open

feat(hooks): emit session prune lifecycle event#12581
vincentkoc wants to merge 11 commits intoopenclaw:mainfrom
vincentkoc:vincentkoc-code/hooks-pruning-event

Conversation

@vincentkoc
Copy link
Contributor

@vincentkoc vincentkoc commented Feb 9, 2026

Why

Context pruning changes model input quality and cost, but there was no dedicated internal lifecycle signal for pruning actions. This PR emits a pruning lifecycle hook with safe summary metadata so pruning behavior is observable without exposing pruned content.

Closes #8606

Detailed Changes

  • Added internal lifecycle event:
    • session:prune
  • Pruning hook context includes summary metadata:
    • softTrimmedCount
    • hardClearedCount
    • toolNames
    • sessionId (when available)
  • Updated context-pruning extension/pruner tests for async path + hook emission behavior

Related Links and Issues

Greptile Overview

Greptile Summary

This PR adds a new internal hook lifecycle event, session:prune, emitted by the context pruning path. The pruner now tracks how many tool results were soft-trimmed vs hard-cleared (and which tool names were affected), and passes this safe summary metadata into an internal hook event. The context-pruning extension’s context handler was updated to be async to await pruning, and tests were updated to cover the async path and hook emission behavior.

Within the codebase, this integrates into the existing internal hook system (src/hooks/internal-hooks.ts) by creating and triggering an InternalHookEvent of type session and action prune, keyed by a derived session key.

Confidence Score: 4/5

  • This PR is mostly safe to merge, but there is one correctness issue around the emitted hook event timestamp semantics that should be clarified/fixed.
  • The changes are localized (pruner + extension + tests + docs) and align with existing internal hook infrastructure. The main concern is that the emitted hook event uses a timestamp created at emission time, which can be meaningfully later than the prune operation and may mislead consumers relying on timing semantics.
  • src/agents/pi-extensions/context-pruning/pruner.ts

@openclaw-barnacle openclaw-barnacle bot added the agents Agent runtime and tooling label Feb 9, 2026
@vincentkoc vincentkoc marked this pull request as ready for review February 9, 2026 10:41
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 9, 2026

Additional Comments (1)

src/agents/pi-extensions/context-pruning/pruner.ts
Missing prune hook emission
When prunableToolChars < settings.minPrunableToolChars, the function returns outputAfterSoftTrim without calling emitPruneHook(). In that scenario, earlier soft-trims may have happened (and counts/tool names are tracked), but the new session:prune lifecycle event will never be emitted. If the intent is “emit whenever any pruning occurred”, this early return breaks observability.

This affects the minPrunableToolChars guard path specifically (pruning happens, but total prunable chars is below the threshold).

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-extensions/context-pruning/pruner.ts
Line: 345:355

Comment:
**Missing prune hook emission**
When `prunableToolChars < settings.minPrunableToolChars`, the function returns `outputAfterSoftTrim` without calling `emitPruneHook()`. In that scenario, earlier soft-trims may have happened (and counts/tool names are tracked), but the new `session:prune` lifecycle event will never be emitted. If the intent is “emit whenever any pruning occurred”, this early return breaks observability.

This affects the `minPrunableToolChars` guard path specifically (pruning happens, but total prunable chars is below the threshold).

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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

@vincentkoc vincentkoc marked this pull request as draft February 9, 2026 16:44
@openclaw-barnacle openclaw-barnacle bot added the docs Improvements or additions to documentation label Feb 9, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 9, 2026

Additional Comments (1)

src/agents/pi-extensions/context-pruning.test.ts, line 24
Mock path likely wrong

This test mocks "../../hooks/internal-hooks.js", but pruner.ts imports the hook module via "../../../hooks/internal-hooks.js" (from src/agents/pi-extensions/context-pruning/pruner.ts). If Vitest resolves these as different module IDs, the mock won’t apply and triggerInternalHook assertions will be calling the real implementation (or not be observed at all). Safer to mock the same specifier the code under test uses (or mock by absolute/alias path used across the repo) so the hook spy is guaranteed to intercept the import.

@vincentkoc vincentkoc marked this pull request as ready for review February 9, 2026 19:32
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

@vincentkoc vincentkoc marked this pull request as draft February 9, 2026 20:00
@vincentkoc vincentkoc marked this pull request as ready for review February 9, 2026 21:44
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Hook event for context pruning

1 participant

Comments