Skip to content

feat(api): expose session context usage and compaction events - #86926

Open
fangliquanflq wants to merge 3 commits into
NousResearch:mainfrom
fangliquanflq:feat/api-session-context-metrics
Open

feat(api): expose session context usage and compaction events#86926
fangliquanflq wants to merge 3 commits into
NousResearch:mainfrom
fangliquanflq:feat/api-session-context-metrics

Conversation

@fangliquanflq

Copy link
Copy Markdown
Contributor

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}/context returns the latest normalized context window, occupancy, compression threshold and progress, remaining tokens, compression count, enabled state, compaction state, and update timestamp.
  • Completed session chat responses include the same normalized snapshot under turn usage.
  • Session chat streams emit automatic compaction lifecycle updates as context.compaction SSE events.
  • Context snapshots persist with the session and follow durable compression continuation IDs after session rotation.
  • This change does not add historical compaction-event replay or alter the existing TUI gauge.

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

  • New feature (non-breaking change that adds functionality)

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.py and hermes_state_common.py - persist per-session context snapshots.
  • tests/gateway/test_session_api.py and tests/gateway/test_multiplex_api_server_routing.py - cover authentication, routing, persistence, rotation, normalized usage, and stream events.

How to Test

  1. Run the focused API, state, and context-engine suites:
scripts/run_tests.sh tests/gateway/test_session_api.py tests/gateway/test_multiplex_api_server_routing.py tests/test_hermes_state.py tests/agent/test_context_engine.py tests/agent/test_context_engine_host_contract.py
  1. Confirm the focused suites report 281 passed and 2 skipped.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this feature (no unrelated commits)
  • I've run the repository test entry on the relevant suites and all tests pass
  • I've added tests for my changes
  • I've tested on my platform: Windows 11

Documentation & Housekeeping

  • Relevant documentation updates are N/A because the capability is advertised through the API capability contract
  • cli-config.yaml.example updates are N/A because no configuration keys changed
  • CONTRIBUTING.md and AGENTS.md updates are N/A because no development workflow changed
  • I've considered cross-platform impact; the implementation uses platform-independent Python, SQLite, HTTP, and SSE paths
  • Tool description and schema updates are N/A because no model tool changed

Screenshots / Logs

Focused verification: 281 passed, 2 skipped.

@alt-glitch alt-glitch added type/feature New feature or request comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have area/sessions Session lifecycle, resume, persistence, history area/compression Context compression and continuation sessions area/usage-cost Token accounting, usage reporting, billing, cost tracking sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Aug 15, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Related to #84602 and #15618: this expands the same context-telemetry family with a persisted session endpoint and compaction events, while #84602 focuses on completed-run context usage.

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

Labels

area/compression Context compression and continuation sessions area/sessions Session lifecycle, resume, persistence, history area/usage-cost Token accounting, usage reporting, billing, cost tracking comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exposing the TUIs context guage information

2 participants