Part of #327. Test script for session analysis, template creation, and warm session bootstrapping.
219 lines
6.6 KiB
Python
219 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for warm session provisioning.
|
|
|
|
This script demonstrates the warm session provisioning system
|
|
by analyzing sessions, creating templates, and testing warm sessions.
|
|
|
|
Issue: #327
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add the tools directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
def test_session_analysis():
|
|
"""Test session analysis functionality."""
|
|
print("=== Testing Session Analysis ===\n")
|
|
|
|
try:
|
|
from tools.warm_session import SessionProfiler
|
|
from hermes_state import SessionDB
|
|
|
|
session_db = SessionDB()
|
|
profiler = SessionProfiler(session_db)
|
|
|
|
# Get a session to analyze
|
|
sessions = session_db.get_messages.__self__.execute_write(
|
|
"SELECT id FROM sessions ORDER BY started_at DESC LIMIT 1"
|
|
)
|
|
|
|
if not sessions:
|
|
print("No sessions found in database.")
|
|
return False
|
|
|
|
session_id = sessions[0][0]
|
|
print(f"Analyzing session: {session_id}\n")
|
|
|
|
analysis = profiler.analyze_session(session_id)
|
|
|
|
if "error" in analysis:
|
|
print(f"Analysis error: {analysis['error']}")
|
|
return False
|
|
|
|
print(f"Message count: {analysis['message_count']}")
|
|
print(f"Proficiency score: {analysis['proficiency_score']:.2f}")
|
|
|
|
print("\nTool call analysis:")
|
|
for tool, stats in analysis.get("tool_calls", {}).items():
|
|
print(f" {tool}: {stats.get('success_rate', 0):.0%} success")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"Test failed: {e}")
|
|
return False
|
|
|
|
|
|
def test_template_creation():
|
|
"""Test template creation from session."""
|
|
print("\n=== Testing Template Creation ===\n")
|
|
|
|
try:
|
|
from tools.warm_session import create_template_from_session, TemplateManager
|
|
from hermes_state import SessionDB
|
|
|
|
session_db = SessionDB()
|
|
|
|
# Get a session
|
|
sessions = session_db.get_messages.__self__.execute_write(
|
|
"SELECT id FROM sessions ORDER BY started_at DESC LIMIT 1"
|
|
)
|
|
|
|
if not sessions:
|
|
print("No sessions found.")
|
|
return False
|
|
|
|
session_id = sessions[0][0]
|
|
print(f"Creating template from session: {session_id}\n")
|
|
|
|
template = create_template_from_session(
|
|
session_id,
|
|
session_db,
|
|
name="Test Template"
|
|
)
|
|
|
|
if not template:
|
|
print("Failed to create template.")
|
|
return False
|
|
|
|
print(f"Template ID: {template.template_id}")
|
|
print(f"Name: {template.name}")
|
|
print(f"Tool patterns: {len(template.tool_patterns)}")
|
|
|
|
# Save the template
|
|
manager = TemplateManager()
|
|
path = manager.save_template(template)
|
|
print(f"Saved to: {path}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"Test failed: {e}")
|
|
return False
|
|
|
|
|
|
def test_warm_session():
|
|
"""Test warm session creation."""
|
|
print("\n=== Testing Warm Session Creation ===\n")
|
|
|
|
help:from tools.warm_session import TemplateManager, WarmSessionBootstrapper, SessionTemplate, ToolCallPattern, UserPattern
|
|
|
|
# Create a mock template for testing
|
|
template = SessionTemplate(
|
|
template_id="test_warm_001",
|
|
name="Test Warm Session",
|
|
description="Test template for warm session provisioning",
|
|
tool_patterns=[
|
|
ToolCallPattern(
|
|
tool_name="terminal",
|
|
success_rate=0.95,
|
|
common_args={"command": "ls -la"},
|
|
context_requirements=[]
|
|
),
|
|
ToolCallPattern(
|
|
tool_name="file_operations",
|
|
success_rate=0.90,
|
|
common_args={"operation": "read", "path": "README.md"},
|
|
context_requirements=[]
|
|
)
|
|
],
|
|
user_patterns=UserPattern(
|
|
request_style="direct",
|
|
feedback_style="terse",
|
|
common_domains=["coding", "file_management"],
|
|
preferred_tools=["terminal", "file_operations"]
|
|
),
|
|
recovery_patterns=[],
|
|
context_markers=["README.md", "src/", "tests/"],
|
|
created_at="2026-04-13T00:00:00",
|
|
source_session_id=None
|
|
)
|
|
|
|
# Create bootstrapper
|
|
manager = TemplateManager()
|
|
bootstrapper = WarmSessionBootstrapper(manager)
|
|
|
|
# Test message
|
|
test_message = "Help me understand the project structure"
|
|
|
|
print(f"Creating warm session with message: {test_message}\n")
|
|
|
|
messages = bootstrapper.create_warm_session_messages(template, test_message)
|
|
|
|
print(f"Generated {len(messages)} messages:")
|
|
for i, msg in enumerate(messages):
|
|
role = msg.get("role", "unknown")
|
|
content = msg.get("content", "")
|
|
|
|
if role == "system":
|
|
print(f"\n[System Context] ({len(content)} chars)")
|
|
print(content[:100] + "..." if len(content) > 100 else content)
|
|
elif role == "user":
|
|
print(f"\n[User]: {content}")
|
|
elif role == "assistant":
|
|
print(f"\n[Assistant]: {content}")
|
|
if msg.get("tool_calls"):
|
|
for tc in msg["tool_calls"]:
|
|
func = tc.get("function", {})
|
|
print(f" Tool Call: {func.get('name')}({func.get('arguments')})")
|
|
elif role == "tool":
|
|
print(f" [Tool Result]: {content[:50]}...")
|
|
|
|
return True
|
|
|
|
|
|
def main():
|
|
"""Run all tests."""
|
|
print("Warm Session Provisioning Test Suite")
|
|
print("=" * 50)
|
|
|
|
tests = [
|
|
("Session Analysis", test_session_analysis),
|
|
("Template Creation", test_template_creation),
|
|
("Warm Session Creation", test_warm_session)
|
|
]
|
|
|
|
results = []
|
|
|
|
for name, test_func in tests:
|
|
print(f"\nRunning: {name}")
|
|
try:
|
|
result = test_func()
|
|
results.append((name, result))
|
|
print(f"Result: {'PASS' if result else 'FAIL'}")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
results.append((name, False))
|
|
|
|
print("\n" + "=" * 50)
|
|
print("Test Results:")
|
|
|
|
passed = sum(1 for _, result in results if result)
|
|
total = len(results)
|
|
|
|
for name, result in results:
|
|
status = "✓ PASS" if result else "✗ FAIL"
|
|
print(f" {status}: {name}")
|
|
|
|
print(f"\nPassed: {passed}/{total}")
|
|
|
|
return 0 if passed == total else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|