@@ -0,0 +1,115 @@ |
| 1 | +"""Tests for shared runtime bootstrap and context synchronization.""" |
| 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.bootstrap import build_runtime_context, sync_runtime_context |
| 9 | +from loader.runtime.conversation import ConversationRuntime |
| 10 | +from loader.runtime.explore import ExploreRuntime |
| 11 | +from tests.helpers.runtime_harness import ScriptedBackend |
| 12 | + |
| 13 | + |
| 14 | +def test_build_runtime_context_uses_shared_bootstrap_contract( |
| 15 | + temp_dir: Path, |
| 16 | +) -> None: |
| 17 | + backend = ScriptedBackend() |
| 18 | + agent = Agent( |
| 19 | + backend=backend, |
| 20 | + config=AgentConfig(auto_context=False), |
| 21 | + project_root=temp_dir, |
| 22 | + ) |
| 23 | + |
| 24 | + context = build_runtime_context(agent) |
| 25 | + |
| 26 | + assert context.project_root == temp_dir.resolve() |
| 27 | + assert context.backend is agent.backend |
| 28 | + assert context.registry is agent.registry |
| 29 | + assert context.session is agent.session |
| 30 | + assert context.config is agent.config |
| 31 | + assert context.capability_profile == agent.capability_profile |
| 32 | + assert context.permission_policy is agent.permission_policy |
| 33 | + assert context.permission_config_status is agent.permission_config_status |
| 34 | + assert context.workflow_mode == agent.workflow_mode |
| 35 | + assert context.prompt_format == agent.prompt_format |
| 36 | + assert context.prompt_sections == agent.prompt_sections |
| 37 | + |
| 38 | + |
| 39 | +def test_sync_runtime_context_refreshes_prompt_and_capability_state( |
| 40 | + temp_dir: Path, |
| 41 | +) -> None: |
| 42 | + backend = ScriptedBackend(supports_native_tools=True) |
| 43 | + agent = Agent( |
| 44 | + backend=backend, |
| 45 | + config=AgentConfig(auto_context=False), |
| 46 | + project_root=temp_dir, |
| 47 | + ) |
| 48 | + context = build_runtime_context(agent) |
| 49 | + |
| 50 | + agent.prompt_format = "native" |
| 51 | + agent.prompt_sections = ["Workflow Context", "Runtime Config"] |
| 52 | + agent.set_workflow_mode("clarify") |
| 53 | + backend._supports_native_tools = False # type: ignore[attr-defined] |
| 54 | + agent.refresh_capability_profile() |
| 55 | + |
| 56 | + sync_runtime_context(context, agent) |
| 57 | + |
| 58 | + assert context.workflow_mode == "clarify" |
| 59 | + assert context.prompt_format == "native" |
| 60 | + assert context.prompt_sections == ["Workflow Context", "Runtime Config"] |
| 61 | + assert context.capability_profile.supports_native_tools is False |
| 62 | + |
| 63 | + |
| 64 | +def test_conversation_runtime_uses_shared_bootstrap_factory( |
| 65 | + temp_dir: Path, |
| 66 | + monkeypatch, |
| 67 | +) -> None: |
| 68 | + agent = Agent( |
| 69 | + backend=ScriptedBackend(), |
| 70 | + config=AgentConfig(auto_context=False, stream=False), |
| 71 | + project_root=temp_dir, |
| 72 | + ) |
| 73 | + calls: list[str] = [] |
| 74 | + real_build_runtime_context = build_runtime_context |
| 75 | + |
| 76 | + def fake_build_runtime_context(source) -> object: |
| 77 | + calls.append("conversation") |
| 78 | + return real_build_runtime_context(source) |
| 79 | + |
| 80 | + monkeypatch.setattr( |
| 81 | + "loader.runtime.conversation.build_runtime_context", |
| 82 | + fake_build_runtime_context, |
| 83 | + ) |
| 84 | + |
| 85 | + runtime = ConversationRuntime(agent) |
| 86 | + |
| 87 | + assert calls == ["conversation"] |
| 88 | + assert runtime.context.project_root == temp_dir.resolve() |
| 89 | + |
| 90 | + |
| 91 | +def test_explore_runtime_uses_shared_bootstrap_factory( |
| 92 | + temp_dir: Path, |
| 93 | + monkeypatch, |
| 94 | +) -> None: |
| 95 | + agent = Agent( |
| 96 | + backend=ScriptedBackend(), |
| 97 | + config=AgentConfig(auto_context=False, stream=False), |
| 98 | + project_root=temp_dir, |
| 99 | + ) |
| 100 | + calls: list[str] = [] |
| 101 | + real_build_runtime_context = build_runtime_context |
| 102 | + |
| 103 | + def fake_build_runtime_context(source) -> object: |
| 104 | + calls.append("explore") |
| 105 | + return real_build_runtime_context(source) |
| 106 | + |
| 107 | + monkeypatch.setattr( |
| 108 | + "loader.runtime.explore.build_runtime_context", |
| 109 | + fake_build_runtime_context, |
| 110 | + ) |
| 111 | + |
| 112 | + runtime = ExploreRuntime(agent) |
| 113 | + |
| 114 | + assert calls == ["explore"] |
| 115 | + assert runtime.context.project_root == temp_dir.resolve() |