feat(F423): ::getcontext Inline-Command — Context-Auslastung anzeigen
@implements: [F423] Neuer Inline-Command ::getcontext zeigt die aktuelle Context-Auslastung an (Weg B, von Rainer evaluiert). Liest Token-Daten über GatewayRunner aus dem Agent-Objekt im Gateway-Speicher: - agent.session_input_tokens — aktuelle Context-Größe - compressor.context_length — maximales Context-Fenster - agent.max_tokens — reserviertes Output-Budget (~ markiert) - compressor.threshold_tokens — Compression-Schwelle - compressor.threshold_percent — Compression-Schwelle in Prozent Auflösung: laufender Agent (mid-turn) → gecachter Agent (zwischen Turns) via runner._resident_agent_for(session_key). Graceful degradation: ohne Runner/Agent/Context-Fenster → 'Keine aktive Session gefunden'. Reine Textanzeige, kein Button. Registriert in _register_inline_commands (Kategorie 'Hilfe'), erscheint automatisch in ::hilfe. 7 Unit-Tests.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
"""Tests für F423 — ::getcontext zeigt die aktuelle Context-Auslastung an.
|
||||
|
||||
Der Handler ``_cmd_getcontext`` liest die Token-Daten über den GatewayRunner
|
||||
aus dem Agent-Objekt (Weg B). Diese Tests decken die Formatierung und die
|
||||
Graceful-Degradation-Pfade ab — ohne echten Gateway-Runner, damit sie
|
||||
standalone laufen.
|
||||
|
||||
Abgedeckte Szenarien:
|
||||
- Vollständige Daten (used/context/threshold/max_tokens) → 4-Zeilen-Format
|
||||
- Kein gateway_runner → "Keine aktive Session gefunden"
|
||||
- Kein Agent (running/cached) → "Keine aktive Session gefunden"
|
||||
- Kein context_length → reduzierte Anzeige
|
||||
- used >= threshold → "Schwelle bereits erreicht"
|
||||
- Output-Budget mit '~' markiert
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from plugin.adapter import RocketChatAdapter
|
||||
import plugin.adapter as adapter_mod
|
||||
|
||||
|
||||
def _make_adapter():
|
||||
"""Erzeuge eine Adapter-Instanz OHNE echten __init__ (braucht PlatformConfig)."""
|
||||
return RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
|
||||
|
||||
class _FakeSessionSource:
|
||||
"""Ersetzt gateway.session.SessionSource im Standalone-Test (Import-Fallback = object)."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _patch_gateway_types(monkeypatch):
|
||||
"""Simuliert den Gateway-Kontext: SessionSource + Platform sind echte Klassen."""
|
||||
monkeypatch.setattr(adapter_mod, "SessionSource", _FakeSessionSource)
|
||||
monkeypatch.setattr(adapter_mod, "Platform", lambda name: name)
|
||||
|
||||
|
||||
class _FakeCompressor:
|
||||
def __init__(self, context_length=0, threshold=0, threshold_pct=0.0, last_prompt=0):
|
||||
self.context_length = context_length
|
||||
self.threshold_tokens = threshold
|
||||
self.threshold_percent = threshold_pct
|
||||
self.last_prompt_tokens = last_prompt
|
||||
|
||||
|
||||
class _FakeAgent:
|
||||
def __init__(self, input_tokens=0, max_tokens=0, compressor=None):
|
||||
self.session_input_tokens = input_tokens
|
||||
self.max_tokens = max_tokens
|
||||
self.context_compressor = compressor
|
||||
|
||||
|
||||
def _make_runner(agent=None):
|
||||
"""Baut einen Fake-GatewayRunner mit _session_key_for_source + _resident_agent_for."""
|
||||
runner = MagicMock()
|
||||
runner._session_key_for_source.return_value = "rocketchat:user:testuser"
|
||||
runner._resident_agent_for.return_value = agent
|
||||
return runner
|
||||
|
||||
|
||||
def _run(adapter, runner):
|
||||
adapter.gateway_runner = runner
|
||||
adapter._last_inline_username = "testuser"
|
||||
adapter._last_inline_room_name = "Testraum"
|
||||
return asyncio.run(adapter._cmd_getcontext("room_id", ""))
|
||||
|
||||
|
||||
def test_full_data_four_line_format():
|
||||
adapter = _make_adapter()
|
||||
ctx = _FakeCompressor(context_length=262000, threshold=170000, threshold_pct=0.65)
|
||||
agent = _FakeAgent(input_tokens=120000, max_tokens=32000, compressor=ctx)
|
||||
runner = _make_runner(agent)
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "📊 Context: 120K / 262K (46%)" in out
|
||||
assert "Compression-Threshold: 170K (65%)" in out
|
||||
assert "Output-Budget: ~32K (max_tokens)" in out
|
||||
assert "Verfuegbar: ~50K bis zur Compression" in out
|
||||
|
||||
|
||||
def test_output_budget_marked_with_tilde():
|
||||
adapter = _make_adapter()
|
||||
ctx = _FakeCompressor(context_length=262000, threshold=170000, threshold_pct=0.65)
|
||||
agent = _FakeAgent(input_tokens=120000, max_tokens=32000, compressor=ctx)
|
||||
runner = _make_runner(agent)
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "Output-Budget: ~32K" in out
|
||||
assert "Output-Budget: 32K" not in out
|
||||
|
||||
|
||||
def test_no_runner_returns_no_session():
|
||||
adapter = _make_adapter()
|
||||
adapter.gateway_runner = None
|
||||
adapter._last_inline_username = "testuser"
|
||||
adapter._last_inline_room_name = "Testraum"
|
||||
|
||||
out = asyncio.run(adapter._cmd_getcontext("room_id", ""))
|
||||
|
||||
assert "Keine aktive Session gefunden" in out
|
||||
|
||||
|
||||
def test_no_agent_returns_no_session():
|
||||
adapter = _make_adapter()
|
||||
runner = _make_runner(None) # _resident_agent_for → None
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "Keine aktive Session gefunden" in out
|
||||
|
||||
|
||||
def test_no_context_length_reduced_display():
|
||||
adapter = _make_adapter()
|
||||
agent = _FakeAgent(input_tokens=5000, max_tokens=32000, compressor=None)
|
||||
runner = _make_runner(agent)
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "Context:** 5K Tokens" in out
|
||||
assert "Output-Budget:** ~32K" in out
|
||||
|
||||
|
||||
def test_threshold_already_reached():
|
||||
adapter = _make_adapter()
|
||||
ctx = _FakeCompressor(context_length=262000, threshold=170000, threshold_pct=0.65)
|
||||
agent = _FakeAgent(input_tokens=180000, max_tokens=32000, compressor=ctx)
|
||||
runner = _make_runner(agent)
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "Schwelle bereits erreicht" in out
|
||||
|
||||
|
||||
def test_fallback_to_last_prompt_tokens_when_input_zero():
|
||||
adapter = _make_adapter()
|
||||
ctx = _FakeCompressor(
|
||||
context_length=262000, threshold=170000, threshold_pct=0.65, last_prompt=90000
|
||||
)
|
||||
agent = _FakeAgent(input_tokens=0, max_tokens=32000, compressor=ctx)
|
||||
runner = _make_runner(agent)
|
||||
|
||||
out = _run(adapter, runner)
|
||||
|
||||
assert "90K / 262K" in out
|
||||
Reference in New Issue
Block a user