dmChatbotBackend / tests /test_runtime_foundation.py
github-actions
Auto deploy from GitHub
cb505ff
Raw History Blame Contribute Delete
5.8 kB
"""Phase 1 Acceptance Tests for Runtime Foundation."""
from __future__ import annotations
import pytest
from src.runtime.agent_runtime import AgentRuntime
from src.runtime.execution_context import ExecutionContext
from src.runtime.manager_runtime import ManagerRuntime
from src.runtime.worker_runtime import WorkerRuntime
from src.skills.lifecycle import SkillLifecycleState
from src.skills.loader import SkillLoader
from src.skills.models import SkillDefinition, SkillResult
from src.skills.registry import default_skill_registry
def test_manager_can_load_execute_and_flush_skill():
"""Criterion 1: Manager runtime can load, execute, and flush a skill."""
registry = default_skill_registry()
manager = ManagerRuntime(registry=registry)
# 1. Initial state UNLOADED
assert manager.agent_runtime.state == SkillLifecycleState.UNLOADED
assert manager.active_skill is None
# 2. Load Manager skill
skill_def = manager.load_skill("role-routing")
assert skill_def.id == "role-routing"
assert manager.active_skill.id == "role-routing"
assert manager.agent_runtime.state == SkillLifecycleState.ACTIVE
# 3. Execute Manager skill
ctx = ExecutionContext(user_query="I have diabetes type 2")
res = manager.execute(ctx)
assert isinstance(res, SkillResult)
assert res.skill_id == "role-routing"
# 4. Flush Manager skill
manager.flush_skill()
assert manager.active_skill is None
assert manager.agent_runtime.state == SkillLifecycleState.UNLOADED
def test_worker_can_load_execute_and_flush_skill():
"""Criterion 2: Worker runtime can load, execute, flush, and release context."""
registry = default_skill_registry()
worker = WorkerRuntime(registry=registry)
# 1. Load Worker skill
skill_def = worker.load_skill("patient-support")
assert skill_def.id == "patient-support"
assert worker.active_skill.id == "patient-support"
# 2. Execute Worker skill with context
ctx = ExecutionContext(user_query="What should I eat for breakfast?", patient_id="patient-101")
res = worker.execute(ctx)
assert res.skill_id == "patient-support"
assert ctx.worker_result is not None
# 3. Flush and destroy worker
worker.flush_and_destroy()
assert worker.active_skill is None
assert worker.is_destroyed is True
assert worker.agent_runtime.state == SkillLifecycleState.UNLOADED
# Attempting to re-use destroyed worker raises error
with pytest.raises(RuntimeError, match="WorkerRuntime has been destroyed"):
worker.load_skill("patient-support")
def test_skill_switching_without_process_restart():
"""Criterion 3: Skill switching dynamically re-loads skill configuration without process restart."""
registry = default_skill_registry()
agent = AgentRuntime(name="TestSlot", registry=registry)
# First skill
c1 = agent.load_skill("patient-support")
assert c1.definition.id == "patient-support"
assert agent.active_tools == ["knowledge.guideline_search", "knowledge.web_search", "memory.read", "memory.propose_write"]
# Switch directly to second skill (implicitly flushes first)
c2 = agent.load_skill("diabetes-dietary-coach")
assert c2.definition.id == "diabetes-dietary-coach"
assert "knowledge.nutrition_lookup" in agent.active_tools
assert agent.state == SkillLifecycleState.ACTIVE
def test_execution_state_survives_skill_flushing():
"""Criterion 4: Request-scoped ExecutionContext state survives agent/worker flushing."""
registry = default_skill_registry()
worker = WorkerRuntime(registry=registry)
ctx = ExecutionContext(
request_id="req-999",
session_id="ses-888",
patient_id="pat-777",
user_query="Can I take metformin?",
user_role="patient",
)
worker.load_skill("patient-support")
res = worker.execute(ctx)
# Worker flushed
worker.flush_and_destroy()
# Context still maintains session identifiers, query, and metadata
assert ctx.request_id == "req-999"
assert ctx.session_id == "ses-888"
assert ctx.patient_id == "pat-777"
assert ctx.user_query == "Can I take metformin?"
assert len(ctx.skill_trace) > 0
def test_worker_specific_context_released():
"""Criterion 5: Ephemeral worker result context can be released from ExecutionContext after completion."""
ctx = ExecutionContext(user_query="Test query")
ctx.worker_result = {"temp_data": "ephemeral"}
assert ctx.worker_result is not None
ctx.clear_worker_context()
assert ctx.worker_result is None
def test_lifecycle_state_machine_strictness():
"""Criterion 6: Lifecycle manager enforces strict valid transitions."""
registry = default_skill_registry()
agent = AgentRuntime(name="StrictSlot", registry=registry)
# Attempting to execute when UNLOADED raises RuntimeError
ctx = ExecutionContext(user_query="Hello")
with pytest.raises(RuntimeError, match="has no active skill loaded"):
agent.execute(ctx)
def test_worker_destroy_is_idempotent_and_records_runtime_metrics():
worker = WorkerRuntime(default_skill_registry())
worker.flush_and_destroy()
worker.flush_and_destroy()
events = [event["event"] for event in worker.agent_runtime.metrics.events]
assert events.count("runtime.destroyed") == 2
assert worker.is_destroyed is True
def test_execution_metrics_are_attached_to_request_context():
worker = WorkerRuntime(default_skill_registry())
context = ExecutionContext(user_query="metrics", user_role="patient")
worker.load_skill("patient-support")
worker.execute(context)
assert context.metadata["runtime_metrics"]["event_count"] >= 3
assert "skill.executed" in [event["event"] for event in context.metadata["runtime_metrics"]["events"]]