Skip to content

fix(desktop): stream live context usage - #39370

Open
OmarB97 wants to merge 2 commits into
NousResearch:mainfrom
OmarB97:codex/desktop-context-meter-live-usage-upstream-20260604
Open

fix(desktop): stream live context usage#39370
OmarB97 wants to merge 2 commits into
NousResearch:mainfrom
OmarB97:codex/desktop-context-meter-live-usage-upstream-20260604

Conversation

@OmarB97

@OmarB97 OmarB97 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Summary

This fixes the desktop footer context meter staying at 0/context while an agent turn is already running. The backend now emits a structured token_usage status update immediately after the high-accuracy preflight token estimate, the gateway forwards it as token.usage, and the desktop/TUI clients merge it into live usage state before message.complete.

The status bar keeps the main text grey and applies semantic capacity color only to the small bracket meter: muted at 0%, green while comfortably under capacity, yellow/orange as it fills, and red near max.

Verification

  • /Users/obaradei/.hermes/hermes-agent/venv/bin/python -m pytest tests/run_agent/test_run_agent.py::TestRunConversation::test_preflight_token_usage_emits_before_api_response tests/test_tui_gateway_server.py::test_status_update_emits_structured_token_usage -q
  • npm --prefix apps/desktop exec vitest run src/lib/usage-events.test.ts src/lib/statusbar.test.ts -- --environment jsdom
  • npm --prefix ui-tui exec vitest run src/__tests__/createGatewayEventHandler.test.ts
  • npm --prefix apps/desktop run type-check
  • npx prettier --check apps/desktop/src/lib/usage-events.ts apps/desktop/src/lib/usage-events.test.ts apps/desktop/src/lib/statusbar.ts apps/desktop/src/lib/statusbar.test.ts apps/desktop/src/app/session/hooks/use-message-stream.ts apps/desktop/src/app/shell/hooks/use-statusbar-items.tsx ui-tui/src/app/createGatewayEventHandler.ts ui-tui/src/__tests__/createGatewayEventHandler.test.ts ui-tui/src/gatewayTypes.ts
  • git diff --check HEAD^..HEAD

Live install checked locally: packaged /Applications/Hermes.app was rebuilt and swapped to commit 7ed069c16b9b6c2d6317814658eb920d76896a00; install stamp reports that commit and the desktop backend is running on 127.0.0.1:9120.

Note: npm --prefix ui-tui run type-check still fails on existing unrelated packages/hermes-ink/src/utils/execFileNoThrow.ts readonly stdio / child-process typing debt.

@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/tui Terminal UI (ui-tui/ + tui_gateway/) comp/gateway Gateway runner, session dispatch, delivery labels Jun 4, 2026
@OmarB97
OmarB97 force-pushed the codex/desktop-context-meter-live-usage-upstream-20260604 branch from cd42bc0 to c0f51aa Compare June 9, 2026 23:35
@OmarB97
OmarB97 force-pushed the codex/desktop-context-meter-live-usage-upstream-20260604 branch from c0f51aa to cbcd8e7 Compare June 10, 2026 00:13

OmarB97 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Maintainer-ready after refresh.

I merged current upstream/main into this live context-usage PR and pushed head 2051f0cce638f7e560280c08d7235b30d2ac638f. I also added the required scripts/release.py AUTHOR_MAP entry for omar@kostudios.io so check-attribution passes.

Verification:

  • python3 -m py_compile agent/conversation_loop.py run_agent.py tui_gateway/server.py tests/run_agent/test_run_agent.py tests/test_tui_gateway_server.py scripts/release.py
  • python3 -m pytest tests/test_tui_gateway_server.py::test_status_update_emits_structured_token_usage tests/run_agent/test_run_agent.py::TestRunConversation::test_preflight_token_usage_emits_before_api_response -q (2 passed, 1 warning)
  • npm --prefix apps/desktop run test:ui -- src/lib/statusbar.test.ts src/lib/usage-events.test.ts (3 passed)
  • npm --prefix apps/desktop run typecheck
  • npm --prefix ui-tui run typecheck
  • npm exec eslint -- on changed desktop/ui-tui files
  • git diff --check upstream/main...HEAD

Note: a broader local run of tests/run_agent/test_run_agent.py tests/test_tui_gateway_server.py produced 635 passed plus one unrelated browser.manage launch-hint failure outside this PR diff, with macOS fork-safety crash noise. The focused tests for this PR pass, and GitHub checks are green.

MeshBoard merge dry-run passes with green checks and no stale-overlap risk. I attempted the actual merge through meshctl pr merge, but GitHub denied MergePullRequest for my account, so this is ready for a maintainer to merge.

@OmarB97
OmarB97 force-pushed the codex/desktop-context-meter-live-usage-upstream-20260604 branch from 2051f0c to ad5d0a3 Compare June 12, 2026 14:14
@alt-glitch alt-glitch added the comp/desktop Electron desktop app (apps/desktop/*) label Jun 26, 2026
@OmarB97
OmarB97 force-pushed the codex/desktop-context-meter-live-usage-upstream-20260604 branch 2 times, most recently from fceb571 to 3b094b7 Compare July 6, 2026 01:29

@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 tracing the usage flow through the agent, gateway, Desktop, and TUI. The live-update premise is still valid on current main: tui_gateway/server.py:9164-9174 supplies usage only in message.complete, while the clients merge that final payload.

Problems

  • agent/conversation_loop.py:174-176 on the PR head turns last_prompt_tokens=-1 into 0 and overwrites it with a rough estimate. Current main explicitly preserves that sentinel in agent/turn_context.py:401-405; tests/agent/test_context_compressor.py:2990-3018 documents that clobbering it can re-trigger compression from schema-inflated estimates.
  • The PR labels estimate_request_tokens_rough() as context_tokens, but current main defines context_used as real current-window occupancy and leaves it unknown otherwise (tui_gateway/server.py:3219-3243). The estimator intentionally overestimates schema-heavy requests (agent/context_compressor.py:1239-1245).

Suggested changes

  • Emit a distinct preflight estimate without mutating last_prompt_tokens; retain the sentinel/defer guards.
  • Add regressions for the -1 sentinel and deferred post-compression preflight before salvage.

Automated hermes-sweeper review.

return

try:
previous = getattr(compressor, "last_prompt_tokens", 0) 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.

last_prompt_tokens = -1 is a post-compression sentinel. or 0 converts it to a writable value, so line 176 overwrites it with a rough estimate and reintroduces the repeat-compaction regression guarded by current agent/turn_context.py:401-405. Do not mutate this field here; emit a distinct estimate or preserve the sentinel/defer logic.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit labels Jul 14, 2026
@teknium1 teknium1 added area/streaming Streaming responses: gateway delivery, provider wire area/usage-cost Token accounting, usage reporting, billing, cost tracking labels Jul 19, 2026
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@OmarB97
OmarB97 force-pushed the codex/desktop-context-meter-live-usage-upstream-20260604 branch from 467bab7 to f1e530a Compare July 29, 2026 14:44
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/streaming Streaming responses: gateway delivery, provider wire 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/desktop Electron desktop app (apps/desktop/*) comp/gateway Gateway runner, session dispatch, delivery comp/tui Terminal UI (ui-tui/ + tui_gateway/) P3 Low — cosmetic, nice to have sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants