Python · 2458 bytes Raw Blame History
1 """Tests for typed runtime context construction."""
2
3 from __future__ import annotations
4
5 from pathlib import Path
6
7 from loader.agent.loop import Agent, AgentConfig
8 from loader.runtime.context import RuntimeContext
9 from loader.runtime.recovery import RecoveryContext
10 from tests.helpers.runtime_harness import ScriptedBackend
11
12
13 def test_agent_builds_typed_runtime_context(temp_dir: Path) -> None:
14 backend = ScriptedBackend()
15 agent = Agent(
16 backend=backend,
17 config=AgentConfig(auto_context=False),
18 project_root=temp_dir,
19 )
20
21 context = agent._build_runtime_context()
22
23 assert isinstance(context, RuntimeContext)
24 assert context.project_root == temp_dir.resolve()
25 assert context.backend is backend
26 assert context.registry is agent.registry
27 assert context.session is agent.session
28 assert context.config is agent.config
29 assert context.capability_profile == agent.capability_profile
30 assert context.project_context is None
31 assert context.permission_policy is agent.permission_policy
32 assert context.permission_config_status is agent.permission_config_status
33 assert context.workflow_mode == agent.workflow_mode
34 assert context.safeguards is agent.safeguards
35 assert context.messages is agent.session.messages
36 assert context.use_react == agent.use_react
37 assert context.active_permission_mode == agent.active_permission_mode
38 assert context.active_permission_rule_counts == agent.active_permission_rule_counts
39 assert context.reasoning is not None
40 assert context.legacy.message_history() is agent.messages
41
42
43 def test_runtime_context_legacy_services_stay_in_sync(temp_dir: Path) -> None:
44 agent = Agent(
45 backend=ScriptedBackend(),
46 config=AgentConfig(auto_context=False),
47 project_root=temp_dir,
48 )
49
50 context = agent._build_runtime_context()
51 context.legacy.queue_steering_message("Re-check the current task.")
52
53 assert context.legacy.drain_steering_queue() == ["Re-check the current task."]
54
55 context.legacy.set_workflow_mode("clarify")
56 assert agent.workflow_mode == "clarify"
57 assert context.workflow_mode == "clarify"
58
59 recovery = RecoveryContext(original_tool="read", original_args={"file_path": "README.md"})
60 context.recovery_context = recovery
61 assert context.recovery_context is recovery
62
63 context.legacy.refresh_capability_profile()
64 assert context.capability_profile == agent.capability_profile