Skip to content

feat(agent): show post-compaction context usage - #6239

Closed
BongSuCHOI wants to merge 2 commits into
NousResearch:mainfrom
BongSuCHOI:feat/compaction-usage-display
Closed

feat(agent): show post-compaction context usage#6239
BongSuCHOI wants to merge 2 commits into
NousResearch:mainfrom
BongSuCHOI:feat/compaction-usage-display

Conversation

@BongSuCHOI

Copy link
Copy Markdown
Contributor

Summary

After context compaction, display before→after token/message counts with a progress bar so users can verify compression actually freed context space.

Currently Hermes shows a pre-compaction warning bar (e.g. "85% to compaction") but gives no feedback after compression completes. This is confusing — users have no way to know if compression worked or how much space was freed.

Changes

  • _compress_context() now returns a 3-tuple (messages, system_prompt, stats) instead of 2-tuple
  • New _emit_compaction_result() method — follows the same CLI/gateway split as the existing _emit_context_pressure()
  • agent/display.py — two new formatters:
    • format_compaction_result() — ANSI for CLI
    • format_compaction_result_gateway() — plain text for Telegram/Discord/etc.
  • All 5 call sites updated (3 in run_agent.py, 2 in gateway/run.py)
  • Error/compression-retry paths silently skip the display (stats = None guard)

Example output

CLI:

  ✓ context compacted  ▰▰▱▱▱▱▱▱▱▱ 25% of window
  messages 142 → 23  ·  tokens 85k → 22k (saved 74%)

Telegram/Discord:

✅ Context compacted: ▰▰▱▱▱▱▱▱▱▱ 25%
85k → 22k tokens (saved 74%)  ·  messages 142 → 23

Testing

  • Manual: triggered compaction via long session, verified CLI and gateway output
  • Verified existing tests pass: pytest tests/ -v
  • No new dependencies

Notes

  • Backward-compatible: _compress_context() return value only unpacked at existing call sites
  • The stats dict may contain None/0 for before_tokens when compression is triggered by error (413/overflow) without a prior token estimate — display is skipped gracefully in these cases

After context compaction, display before→after token/message counts
with a progress bar so users can verify the compression actually
freed context space.

- _compress_context() now returns a 3-tuple with compression stats
- New _emit_compaction_result() follows same CLI/gateway split as
  _emit_context_pressure()
- Displayed in CLI with ANSI, in gateways (Telegram/Discord) as
  plain text
- All 5 call sites updated; error paths silently skip display
_compress_context now returns (messages, system_prompt, stats) instead of
(messages, system_prompt). Update all test mocks and ACP adapter to unpack
3 values.
@BongSuCHOI

Copy link
Copy Markdown
Contributor Author

CI Failure Analysis

Verified that all 10 test failures are pre-existing on main and unrelated to this PR. The changes here only touch run_agent.py, gateway/run.py, and agent/display.py — none of the failing test files were modified.

Test failures (all pre-existing):

  • test_quick_commands.py (5) — print mock issue from a recent CLI interface change
  • test_auxiliary_named_custom_providers.py (1) — custom: prefix normalization
  • test_api_key_providers.py (1) — HF model MiniMaxAI/MiniMax-M2.5 not yet added to DEFAULT_CONTEXT_LENGTHS
  • test_docker_environment.py (2) — Docker env var handling in CI
  • test_vision_tools.py (1) — codex auth check

9270 tests passed. Safe to merge.

@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard labels Apr 30, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for targeting a real usability gap: current main still prints only compacting context… in the normal automatic path (agent/conversation_loop.py:4769-4777).

Problems

  • The PR's _before_tokens = approx_tokens or 0 (run_agent.py:5844 on PR head) produces a truthy stats payload even when the input estimate is unknown. The formatter then displays an invalid 0 → N result rather than skipping it.
  • The active loop was extracted by 053025238 into agent/conversation_loop.py; the PR's run_agent.py call-site changes no longer cover current automatic compaction paths. Current preflight compaction is also in agent/turn_context.py:436.

Suggested changes

  • Salvage the feature onto the current loop/turn-context call sites and preserve unknown token counts instead of coercing them to zero.
  • Reuse the existing manual-feedback semantics in agent/manual_compression_feedback.py, and distinguish approximate post-compression estimates from provider-reported usage (agent/conversation_compression.py:952-963).
  • Add focused tests for automatic success, no-op/unknown estimates, and gateway status delivery.

Automated hermes-sweeper review.

Comment thread run_agent.py
before_messages, after_messages (or None if compression didn't run).
"""
_pre_msg_count = len(messages)
_before_tokens = approx_tokens or 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approx_tokens can be absent/zero when compaction was triggered from a rough fallback, but this turns it into a displayed 0 while _stats remains truthy. Preserve an unknown value and skip the token-result display, or compute a real before estimate before emitting feedback.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit labels Jul 12, 2026
lincoln-mackay pushed a commit to lincoln-mackay/hermes-agent that referenced this pull request Jul 19, 2026
- chat_completion_helpers.py: add provider-level retry for silent failures
  - module-level constants: HERMES_PROVIDER_RETRY_* (max_attempts=3, base_delay=1s, max_delay=30s, backoff=2.0, jitter=0.2)
  - _is_silent_provider_failure() detects empty responses (no content, tool_calls, reasoning)
  - _provider_retry_backoff() exponential backoff with ±20% jitter
  - interruptible_api_call() wrapped in while True with per-attempt state isolation
  - silent failure check BEFORE error raise with interrupt-safe 100ms backoff wait
  - continues retry on silent failure, returns on success, raises on exhausted retries

- agent_init.py: snapshot primary model/provider before first fallback swap
  enables UI to show 'Fallback: X (primary: Y)'

Follows PR NousResearch#6239 architecture: provider-call level retry (not whole-turn),
emission guard, cancellation-safe, no double-emit, NousResearch#4729 flush preserved.

All 38 streaming tests pass including new silent retry tests.
@teknium1 teknium1 added the area/usage-cost Token accounting, usage reporting, billing, cost tracking label Jul 19, 2026
@teknium1

Copy link
Copy Markdown
Contributor

The visibility this PR targeted has landed via PR #70457: with compression.progress_notices: true, users now see compaction start AND the completion stat (the N→M shape you built here) on chat platforms — the design gate was settled in issue #52995 as opt-in/default-off. Thanks @BongSuCHOI for being the earliest PR to push on post-compaction feedback; the completion-stat idea is credited in the gate discussion.

This branch predates the run_agent.py modularization and the status-template system (#69550), so the mechanism couldn't be cherry-picked — the completion notice now rides the existing COMPACTION_DONE_STATUS lifecycle edge instead of new emit sites. Closing with credit.

@teknium1 teknium1 closed this Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages 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.

3 participants