mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
- `is_diagnostic_notice()` replaces three drifting copies: the gateway muted every
`credits.*` notice, the TUI and CLI only `warn`/`error`, so `credits.restored` was hidden
on Telegram and shown in the TUI for the same config. Every credit-service notice is an
automatic diagnostic (a "restored" line after a hidden depletion notice is orphan noise).
- `effective_user_config()` is the single fail-open effective-config read; the two extra
`deepcopy`s per foreground turn go (the loader already returns a fresh copy and the
snapshot is read-only).
- `diagnostic_metadata(event)` replaces the repeated
`{"notification_category": "diagnostic"} if event.internal and ... else {}` literal in
gateway/run_turn.py; the gateway-side imports of the resolver are module-level (no cycle:
it imports only gateway.display_config).
- `display.suppress_warning_notifications` is listed with its sibling display keys in
cli-config.yaml.example and the configuration reference; the messaging guide states that a
muted diagnostic wake still runs (and bills) its agent turn.
59 lines
2.9 KiB
Python
59 lines
2.9 KiB
Python
"""Actual response-header capture -> credit latches -> CLI/TUI presentation."""
|
|
import json
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from run_agent import AIAgent
|
|
from tui_gateway import server
|
|
from hermes_cli.cli_stream_mixin import CLIStreamMixin
|
|
from tests.agent.test_credits_tracker import HEALTHY_HEADERS, DEPLETED_HEADERS
|
|
|
|
|
|
class CLI(CLIStreamMixin):
|
|
def _invalidate(self):
|
|
pass
|
|
|
|
|
|
@pytest.mark.parametrize("setting", [None, False, True])
|
|
@pytest.mark.parametrize("surface", ["cli", "tui"])
|
|
def test_actual_credit_capture_depletion_and_recovery(tmp_path, monkeypatch, setting, surface):
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
monkeypatch.setenv("HERMES_MANAGED_DIR", str(tmp_path / "managed"))
|
|
cfg = {} if setting is None else {"display": {"suppress_warning_notifications": setting}}
|
|
(tmp_path / "config.yaml").write_text(json.dumps(cfg))
|
|
agent = object.__new__(AIAgent)
|
|
agent.provider = "nous"
|
|
agent.model = "paid-model"
|
|
agent.base_url = ""
|
|
agent._credits_state = agent._credits_session_start_micros = None
|
|
agent._credits_notices_enabled_cache = True
|
|
agent._notification_config = cfg
|
|
observed, clears, frames = [], [], []
|
|
cli = CLI()
|
|
cli.agent = agent
|
|
monkeypatch.setattr(server, "write_json", lambda frame: frames.append(frame) or True)
|
|
monkeypatch.setitem(server._sessions, "credit", {"agent": agent, "profile_home": str(tmp_path)})
|
|
callback = cli._on_notice if surface == "cli" else server._agent_cbs("credit")["notice_callback"]
|
|
def observe(notice):
|
|
observed.append(notice)
|
|
callback(notice)
|
|
agent.notice_callback = observe
|
|
agent.notice_clear_callback = clears.append
|
|
for headers in (HEALTHY_HEADERS, DEPLETED_HEADERS, DEPLETED_HEADERS, HEALTHY_HEADERS):
|
|
current = dict(headers)
|
|
current['x-nous-credits-as-of-ms'] = str(int(time.time() * 1000))
|
|
agent._capture_credits(SimpleNamespace(headers=current))
|
|
assert sum(n.key == "credits.depleted" for n in observed) == 1
|
|
assert "credits.depleted" in clears
|
|
assert agent.get_credits_state().paid_access is True
|
|
assert agent.get_credits_state().remaining_micros == int(HEALTHY_HEADERS['x-nous-credits-remaining-micros'])
|
|
projected = getattr(cli, "_pending_credit_notices", []) if surface == "cli" else [
|
|
f for f in frames if f.get("params", {}).get("type") == "notification.show"]
|
|
levels = [row[0] for row in projected] if surface == "cli" else [
|
|
frame["params"]["payload"]["level"] for frame in projected]
|
|
assert ("error" in levels) is not (setting is True)
|
|
# Every credit-service notice is an automatic diagnostic, recovery included: a "restored"
|
|
# line after a hidden depletion notice would be orphan noise (same rule as the gateway).
|
|
assert ("success" in levels) is not (setting is True)
|
|
assert sum(n.key == "credits.restored" for n in observed) == 1
|