google / google/adk-python

Feature: behavioral drift monitoring hooks for context compaction events

Open
#5,050 1 comment 0 reactions 2 assignees Claimed by @GWeale View on GitHub
core needs review
Dominant language
Python
Stars
21.5k
Forks
4k
Avg merge
1d 14h
Merged PRs (30d)
37

Description

## Summary

ADK's context compaction feature (sliding window + summarization) solves the token budget problem but creates a measurement blind spot: it's currently not possible to observe whether agent behavior changed after compaction. I'd like to propose lightweight lifecycle hooks to close that gap.

## The problem

When `ContextCompaction` runs, it summarizes and discards older events. This is correct behavior for token management. But for production agents running long sessions, you often want to know:

- Did the agent's tool-use pattern shift after compaction?
- Did topic focus drift (e.g., started on debugging, now answering unrelated questions)?
- Is the agent's response style or vocabulary measurably different post-compaction?

Right now there's no first-class place to hook a behavioral snapshot before and after the compaction boundary. Instrumenting this requires monkey-patching the compaction runner, which is fragile.

## Proposed hooks

Two new optional callbacks on the session or runner, analogous to existing tool hooks:

```python
# Before compaction triggers
def on_pre_compaction(session: Session, context_size: int) -> None:
... # snapshot behavioral fingerprint

# After compaction completes
def on_post_compaction(session: Session, summary: str, dropped_events: int) -> None:
... # compare to pre-compaction snapshot; alert if drift exceeds threshold
```

Or alternatively, a `CompactionEvent` that gets emitted into the event stream with metadata (pre-compaction event count, summary size, timestamp), allowing downstream monitoring tools to subscribe without modifying the runner.

## Why it matters

Context compaction is increasingly common in production agents. Silent behavioral change after compaction is a real failure mode that isn't caught by per-turn evaluation because the boundary event itself isn't observable.

The practical use case: a persistent agent that runs 8+ hour sessions will compact multiple times. Knowing that the agent's behavior is consistent across those boundaries is operationally important — same as knowing a microservice's behavior is stable across restarts.

## Reference implementation

I built a toolkit for exactly this measurement problem: [compression-monitor](https://github.com/agent-morrow/compression-monitor). It detects ghost lexicon decay, behavioral footprint shift, and semantic topic drift across session boundaries. It works today via filesystem inspection of LangChain/DeepAgents compaction markers, but it would be much cleaner with first-class hooks from the framework.

Happy to prototype a reference implementation of the hooks against ADK if that would help move this forward.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.