feat(api): expose session context usage and compaction events - #86926
Open
fangliquanflq wants to merge 3 commits into
Open
feat(api): expose session context usage and compaction events#86926fangliquanflq wants to merge 3 commits into
fangliquanflq wants to merge 3 commits into
Conversation
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
API clients can now read the same per-session context occupancy information that Hermes surfaces in its TUI. The authenticated session API exposes a durable context snapshot, completed chat turns include that snapshot in usage metadata, and streaming clients receive automatic compaction lifecycle updates as structured SSE events.
Motivation
Clients integrating through the Hermes API could not determine how full a session context was, how close it was to automatic compression, or when automatic compaction occurred. This left editor integrations unable to present context pressure or react reliably to compression.
Behavior
GET /api/sessions/{session_id}/contextreturns the latest normalized context window, occupancy, compression threshold and progress, remaining tokens, compression count, enabled state, compaction state, and update timestamp.context.compactionSSE events.Implementation
Context-engine readings are normalized in a shared helper and persisted in the existing session database. The API reuses existing authenticated session routes and SSE transport rather than adding a second event subsystem. A structural marker preserves automatic compaction event classification even when context engines customize status text.
Related Issue
Closes #86885
Type of Change
Changes Made
agent/context_usage.py- normalize context occupancy and compression metrics across context engines.agent/context_engine.py- structurally identify automatic compaction status messages.gateway/platforms/api_server.py- expose the context route, turn usage snapshot, capability metadata, persistence, and SSE events.hermes_state.pyandhermes_state_common.py- persist per-session context snapshots.tests/gateway/test_session_api.pyandtests/gateway/test_multiplex_api_server_routing.py- cover authentication, routing, persistence, rotation, normalized usage, and stream events.How to Test
Checklist
Code
fix(scope):,feat(scope):, etc.)Documentation & Housekeeping
cli-config.yaml.exampleupdates are N/A because no configuration keys changedCONTRIBUTING.mdandAGENTS.mdupdates are N/A because no development workflow changedScreenshots / Logs
Focused verification: 281 passed, 2 skipped.