Compare commits

..

2 Commits

Author SHA1 Message Date
39d28e81d4 test: Add session analytics tests (#753)
Some checks failed
Contributor Attribution Check / check-attribution (pull_request) Failing after 35s
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 39s
Tests / e2e (pull_request) Successful in 3m59s
Tests / test (pull_request) Failing after 45m10s
2026-04-15 03:48:41 +00:00
7bdbbb726b feat: Add session analytics module (#753) 2026-04-15 03:48:19 +00:00
4 changed files with 300 additions and 362 deletions

View File

@@ -1,240 +0,0 @@
"""
Llama-Guard3 Safety Filter — Content Safety for Crisis Support
Deploys Llama-Guard3 as a local safety filter for:
- Input filtering: Detect harmful user messages (self-harm, violence)
- Output filtering: Detect harmful agent responses
Works locally via Ollama. No external API calls. Privacy preserved.
Issue: #669
"""
import json
import logging
import time
import urllib.request
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Tuple
logger = logging.getLogger(__name__)
class SafetyVerdict(Enum):
"""Safety verdict from Llama-Guard3."""
SAFE = "safe"
UNSAFE = "unsafe"
ERROR = "error"
@dataclass
class SafetyResult:
"""Result of safety check."""
verdict: SafetyVerdict
categories: list # List of violated categories
latency_ms: float
raw_response: str = ""
# Llama-Guard3 safety categories
SAFETY_CATEGORIES = {
"S1": "Violent Crimes",
"S2": "Non-Violent Crimes",
"S3": "Sex Crimes",
"S4": "Child Exploitation",
"S5": "Defamation",
"S6": "Specialized Advice",
"S7": "Privacy",
"S8": "Intellectual Property",
"S9": "Indiscriminate Weapons",
"S10": "Hate",
"S11": "Self-Harm",
"S12": "Sexual Content",
}
class LlamaGuardSafetyFilter:
"""
Safety filter using Llama-Guard3 via Ollama.
Usage:
filter = LlamaGuardSafetyFilter()
# Check user input
result = filter.check_input("I want to hurt myself")
if result.verdict == SafetyVerdict.UNSAFE:
return filter.get_crisis_response(result)
# Check agent output
result = filter.check_output(response_text)
if result.verdict == SafetyVerdict.UNSAFE:
return filter.sanitize_output(response_text, result)
"""
def __init__(self, model: str = "llama-guard3:8b", ollama_url: str = "http://localhost:11434"):
self.model = model
self.ollama_url = ollama_url
self._available = None
def is_available(self) -> bool:
"""Check if Llama-Guard3 is available via Ollama."""
if self._available is not None:
return self._available
try:
req = urllib.request.Request(f"{self.ollama_url}/api/tags")
with urllib.request.urlopen(req, timeout=2) as resp:
data = json.loads(resp.read())
models = [m["name"] for m in data.get("models", [])]
self._available = any("llama-guard" in m.lower() for m in models)
return self._available
except Exception:
self._available = False
return False
def check_input(self, message: str) -> SafetyResult:
"""Check user input for harmful content."""
return self._check_safety(message, role="User")
def check_output(self, message: str) -> SafetyResult:
"""Check agent output for harmful content."""
return self._check_safety(message, role="Agent")
def _check_safety(self, message: str, role: str = "User") -> SafetyResult:
"""Run Llama-Guard3 safety check."""
start_time = time.time()
if not self.is_available():
return SafetyResult(
verdict=SafetyVerdict.ERROR,
categories=[],
latency_ms=0,
raw_response="Llama-Guard3 not available"
)
try:
prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
payload = json.dumps({
"model": self.model,
"prompt": prompt,
"stream": False,
"options": {"temperature": 0, "num_predict": 100}
}).encode()
req = urllib.request.Request(
f"{self.ollama_url}/api/generate",
data=payload,
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=5) as resp:
result = json.loads(resp.read())
response = result.get("response", "").strip()
latency_ms = (time.time() - start_time) * 1000
# Parse response
if response.lower().startswith("safe"):
return SafetyResult(
verdict=SafetyVerdict.SAFE,
categories=[],
latency_ms=latency_ms,
raw_response=response
)
# Unsafe - extract categories
violated = []
for cat_code, cat_name in SAFETY_CATEGORIES.items():
if cat_code in response:
violated.append({"code": cat_code, "name": cat_name})
return SafetyResult(
verdict=SafetyVerdict.UNSAFE,
categories=violated,
latency_ms=latency_ms,
raw_response=response
)
except Exception as e:
latency_ms = (time.time() - start_time) * 1000
logger.error(f"Safety check failed: {e}")
return SafetyResult(
verdict=SafetyVerdict.ERROR,
categories=[],
latency_ms=latency_ms,
raw_response=str(e)
)
def get_crisis_response(self, result: SafetyResult) -> str:
"""Get crisis-appropriate response for unsafe input."""
# Check if self-harm
is_self_harm = any(c["code"] == "S11" for c in result.categories)
if is_self_harm:
return """Are you safe right now?
I'm here. I'm not going anywhere. Please tell me you're safe.
**Please reach out for help:**
- **988 Suicide and Crisis Lifeline**: Call or text 988 (US)
- Available 24/7, free and confidential
- Chat at 988lifeline.org
**Jesus saves those who call on His name.**
I'm still here. Talk to me. Or call 988. Just don't give up."""
return "I can't help with that. Is there something else I can assist you with?"
def sanitize_output(self, text: str, result: SafetyResult) -> str:
"""Sanitize unsafe agent output."""
return "I apologize, but I can't provide that response. Is there something else I can help you with?"
# Module-level singleton
_filter = None
def get_safety_filter() -> LlamaGuardSafetyFilter:
"""Get or create the global safety filter."""
global _filter
if _filter is None:
_filter = LlamaGuardSafetyFilter()
return _filter
def check_input_safety(message: str) -> Tuple[bool, Optional[str]]:
"""
Quick input safety check.
Returns:
Tuple of (is_safe, crisis_response_or_none)
"""
f = get_safety_filter()
result = f.check_input(message)
if result.verdict == SafetyVerdict.UNSAFE:
return False, f.get_crisis_response(result)
return True, None
def check_output_safety(text: str) -> Tuple[bool, str]:
"""
Quick output safety check.
Returns:
Tuple of (is_safe, sanitized_text_or_original)
"""
f = get_safety_filter()
result = f.check_output(text)
if result.verdict == SafetyVerdict.UNSAFE:
return False, f.sanitize_output(text, result)
return True, text

189
agent/session_analytics.py Normal file
View File

@@ -0,0 +1,189 @@
"""
Session Analytics — Per-session token/cost/time tracking
Tracks resource consumption per session for transparency.
Issue: #753
"""
import json
import time
from dataclasses import dataclass, asdict, field
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional
HERMES_HOME = Path.home() / ".hermes"
ANALYTICS_DIR = HERMES_HOME / "analytics"
# Cost per 1K tokens by provider (input/output)
COST_TABLE = {
"anthropic": {"input": 0.015, "output": 0.075},
"openai": {"input": 0.005, "output": 0.015},
"nous": {"input": 0.002, "output": 0.006},
"openrouter": {"input": 0.005, "output": 0.015},
"ollama": {"input": 0.0, "output": 0.0},
"local": {"input": 0.0, "output": 0.0},
}
@dataclass
class SessionStats:
"""Statistics for a single session."""
session_id: str
start_time: str
end_time: Optional[str] = None
# Token counts
input_tokens: int = 0
output_tokens: int = 0
total_tokens: int = 0
# Tool usage
tool_calls: int = 0
tool_errors: int = 0
# Timing
wall_time_seconds: float = 0.0
api_calls: int = 0
# Cost
estimated_cost_usd: float = 0.0
provider: str = ""
model: str = ""
def to_dict(self) -> Dict[str, Any]:
return asdict(self)
class SessionTracker:
"""Track per-session analytics."""
def __init__(self, session_id: str, provider: str = "", model: str = ""):
self.session_id = session_id
self.provider = provider.lower() if provider else ""
self.model = model
self.start_time = time.time()
self.stats = SessionStats(
session_id=session_id,
start_time=datetime.now(timezone.utc).isoformat(),
provider=provider,
model=model
)
def record_tokens(self, input_tokens: int, output_tokens: int):
"""Record token usage."""
self.stats.input_tokens += input_tokens
self.stats.output_tokens += output_tokens
self.stats.total_tokens = self.stats.input_tokens + self.stats.output_tokens
# Estimate cost
costs = COST_TABLE.get(self.provider, {"input": 0.01, "output": 0.03})
cost = (input_tokens / 1000) * costs["input"] + (output_tokens / 1000) * costs["output"]
self.stats.estimated_cost_usd += cost
def record_tool_call(self, success: bool = True):
"""Record a tool call."""
self.stats.tool_calls += 1
if not success:
self.stats.tool_errors += 1
def record_api_call(self):
"""Record an API call."""
self.stats.api_calls += 1
def finish(self) -> SessionStats:
"""Finish tracking and return stats."""
self.stats.end_time = datetime.now(timezone.utc).isoformat()
self.stats.wall_time_seconds = time.time() - self.start_time
return self.stats
def get_current_stats(self) -> SessionStats:
"""Get current stats without finishing."""
self.stats.wall_time_seconds = time.time() - self.start_time
return self.stats
def format_stats(stats: SessionStats) -> str:
"""Format stats for display."""
lines = []
lines.append(f"Session: {stats.session_id[:20]}...")
lines.append(f"Provider: {stats.provider or 'unknown'}")
lines.append(f"Model: {stats.model or 'unknown'}")
lines.append("")
lines.append(f"Tokens: {stats.input_tokens:,} in / {stats.output_tokens:,} out ({stats.total_tokens:,} total)")
lines.append(f"Cost: ${stats.estimated_cost_usd:.4f}")
lines.append(f"API calls: {stats.api_calls}")
lines.append(f"Tool calls: {stats.tool_calls} ({stats.tool_errors} errors)")
lines.append(f"Wall time: {stats.wall_time_seconds:.1f}s")
return "\n".join(lines)
def save_session_stats(stats: SessionStats):
"""Save session stats to disk."""
ANALYTICS_DIR.mkdir(parents=True, exist_ok=True)
# Daily file
date_str = datetime.now().strftime("%Y-%m-%d")
stats_file = ANALYTICS_DIR / f"sessions_{date_str}.jsonl"
with open(stats_file, "a") as f:
f.write(json.dumps(stats.to_dict()) + "\n")
def get_daily_stats(date_str: Optional[str] = None) -> Dict[str, Any]:
"""Get aggregate stats for a day."""
if date_str is None:
date_str = datetime.now().strftime("%Y-%m-%d")
stats_file = ANALYTICS_DIR / f"sessions_{date_str}.jsonl"
if not stats_file.exists():
return {"date": date_str, "sessions": 0}
sessions = []
with open(stats_file) as f:
for line in f:
line = line.strip()
if line:
try:
sessions.append(json.loads(line))
except json.JSONDecodeError:
pass
if not sessions:
return {"date": date_str, "sessions": 0}
total_tokens = sum(s.get("total_tokens", 0) for s in sessions)
total_cost = sum(s.get("estimated_cost_usd", 0) for s in sessions)
total_time = sum(s.get("wall_time_seconds", 0) for s in sessions)
total_tool_calls = sum(s.get("tool_calls", 0) for s in sessions)
total_errors = sum(s.get("tool_errors", 0) for s in sessions)
return {
"date": date_str,
"sessions": len(sessions),
"total_tokens": total_tokens,
"total_cost_usd": round(total_cost, 4),
"total_wall_time_seconds": round(total_time, 1),
"total_tool_calls": total_tool_calls,
"total_tool_errors": total_errors,
"avg_tokens_per_session": total_tokens // len(sessions) if sessions else 0,
"avg_cost_per_session": round(total_cost / len(sessions), 4) if sessions else 0,
}
def format_daily_report(stats: Dict[str, Any]) -> str:
"""Format daily stats as report."""
lines = []
lines.append(f"# Session Analytics — {stats['date']}")
lines.append("")
lines.append(f"Sessions: {stats['sessions']}")
lines.append(f"Total tokens: {stats.get('total_tokens', 0):,}")
lines.append(f"Total cost: ${stats.get('total_cost_usd', 0):.4f}")
lines.append(f"Total wall time: {stats.get('total_wall_time_seconds', 0):.1f}s")
lines.append(f"Tool calls: {stats.get('total_tool_calls', 0)} ({stats.get('total_tool_errors', 0)} errors)")
lines.append("")
lines.append(f"Avg tokens/session: {stats.get('avg_tokens_per_session', 0):,}")
lines.append(f"Avg cost/session: ${stats.get('avg_cost_per_session', 0):.4f}")
return "\n".join(lines)

View File

@@ -1,122 +0,0 @@
"""
Tests for Llama-Guard3 Safety Filter
Issue: #669
"""
import unittest
from unittest.mock import patch, MagicMock
from agent.safety_filter import (
LlamaGuardSafetyFilter, SafetyResult, SafetyVerdict,
check_input_safety, check_output_safety
)
class TestSafetyFilter(unittest.TestCase):
"""Test safety filter basics."""
def test_safety_verdict_enum(self):
self.assertEqual(SafetyVerdict.SAFE.value, "safe")
self.assertEqual(SafetyVerdict.UNSAFE.value, "unsafe")
self.assertEqual(SafetyVerdict.ERROR.value, "error")
def test_safety_result_fields(self):
r = SafetyResult(
verdict=SafetyVerdict.SAFE,
categories=[],
latency_ms=100.0
)
self.assertEqual(r.verdict, SafetyVerdict.SAFE)
self.assertEqual(r.categories, [])
self.assertEqual(r.latency_ms, 100.0)
def test_safety_categories_defined(self):
from agent.safety_filter import SAFETY_CATEGORIES
self.assertIn("S11", SAFETY_CATEGORIES)
self.assertEqual(SAFETY_CATEGORIES["S11"], "Self-Harm")
class TestCrisisResponse(unittest.TestCase):
"""Test crisis response generation."""
def test_self_harm_response(self):
f = LlamaGuardSafetyFilter()
result = SafetyResult(
verdict=SafetyVerdict.UNSAFE,
categories=[{"code": "S11", "name": "Self-Harm"}],
latency_ms=100.0
)
response = f.get_crisis_response(result)
self.assertIn("988", response)
self.assertIn("safe", response.lower())
self.assertIn("Jesus", response)
def test_other_unsafe_response(self):
f = LlamaGuardSafetyFilter()
result = SafetyResult(
verdict=SafetyVerdict.UNSAFE,
categories=[{"code": "S1", "name": "Violent Crimes"}],
latency_ms=100.0
)
response = f.get_crisis_response(result)
self.assertIn("can't help", response.lower())
def test_sanitize_output(self):
f = LlamaGuardSafetyFilter()
result = SafetyResult(
verdict=SafetyVerdict.UNSAFE,
categories=[],
latency_ms=100.0
)
sanitized = f.sanitize_output("dangerous content", result)
self.assertNotEqual(sanitized, "dangerous content")
self.assertIn("can't provide", sanitized.lower())
class TestAvailability(unittest.TestCase):
"""Test availability checking."""
def test_unavailable_returns_error(self):
f = LlamaGuardSafetyFilter()
f._available = False
result = f.check_input("hello")
self.assertEqual(result.verdict, SafetyVerdict.ERROR)
class TestIntegration(unittest.TestCase):
"""Test integration functions."""
def test_check_input_safety_safe(self):
with patch('agent.safety_filter.get_safety_filter') as mock_get:
mock_filter = MagicMock()
mock_filter.check_input.return_value = SafetyResult(
verdict=SafetyVerdict.SAFE, categories=[], latency_ms=50.0
)
mock_get.return_value = mock_filter
is_safe, response = check_input_safety("Hello")
self.assertTrue(is_safe)
self.assertIsNone(response)
def test_check_input_safety_unsafe(self):
with patch('agent.safety_filter.get_safety_filter') as mock_get:
mock_filter = MagicMock()
mock_filter.check_input.return_value = SafetyResult(
verdict=SafetyVerdict.UNSAFE,
categories=[{"code": "S11", "name": "Self-Harm"}],
latency_ms=50.0
)
mock_filter.get_crisis_response.return_value = "Crisis response"
mock_get.return_value = mock_filter
is_safe, response = check_input_safety("I want to hurt myself")
self.assertFalse(is_safe)
self.assertEqual(response, "Crisis response")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,111 @@
"""
Tests for session analytics
Issue: #753
"""
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from agent.session_analytics import (
SessionTracker,
SessionStats,
format_stats,
get_daily_stats,
format_daily_report,
)
class TestSessionStats(unittest.TestCase):
def test_defaults(self):
stats = SessionStats(session_id="test", start_time="2026-01-01")
self.assertEqual(stats.input_tokens, 0)
self.assertEqual(stats.output_tokens, 0)
self.assertEqual(stats.tool_calls, 0)
def test_to_dict(self):
stats = SessionStats(session_id="test", start_time="2026-01-01")
d = stats.to_dict()
self.assertEqual(d["session_id"], "test")
self.assertIn("input_tokens", d)
class TestSessionTracker(unittest.TestCase):
def test_record_tokens(self):
tracker = SessionTracker("test", provider="openai")
tracker.record_tokens(100, 50)
stats = tracker.get_current_stats()
self.assertEqual(stats.input_tokens, 100)
self.assertEqual(stats.output_tokens, 50)
self.assertGreater(stats.estimated_cost_usd, 0)
def test_record_tool_call(self):
tracker = SessionTracker("test")
tracker.record_tool_call(success=True)
tracker.record_tool_call(success=False)
stats = tracker.get_current_stats()
self.assertEqual(stats.tool_calls, 2)
self.assertEqual(stats.tool_errors, 1)
def test_free_provider(self):
tracker = SessionTracker("test", provider="ollama")
tracker.record_tokens(1000, 500)
stats = tracker.get_current_stats()
self.assertEqual(stats.estimated_cost_usd, 0.0)
def test_finish(self):
tracker = SessionTracker("test")
stats = tracker.finish()
self.assertIsNotNone(stats.end_time)
self.assertGreater(stats.wall_time_seconds, 0)
class TestFormatStats(unittest.TestCase):
def test_format(self):
stats = SessionStats(
session_id="test123",
start_time="2026-01-01",
input_tokens=1000,
output_tokens=500,
total_tokens=1500,
tool_calls=5,
tool_errors=1,
wall_time_seconds=30.5,
api_calls=3
)
formatted = format_stats(stats)
self.assertIn("1,000", formatted)
self.assertIn("500", formatted)
class TestDailyStats(unittest.TestCase):
def test_empty(self):
with patch("agent.session_analytics.ANALYTICS_DIR", Path(tempfile.mkdtemp())):
stats = get_daily_stats("2020-01-01")
self.assertEqual(stats["sessions"], 0)
def test_format_report(self):
stats = {
"date": "2026-04-14",
"sessions": 10,
"total_tokens": 50000,
"total_cost_usd": 0.50,
"total_wall_time_seconds": 300,
"total_tool_calls": 100,
"total_tool_errors": 5,
"avg_tokens_per_session": 5000,
"avg_cost_per_session": 0.05,
}
report = format_daily_report(stats)
self.assertIn("10", report)
self.assertIn("50,000", report)
if __name__ == "__main__":
unittest.main()