Репозитории /
AiRouter /
tests/test_session.py
test_session.py
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import asyncio |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from airouter.config import Config |
| 7 | from airouter.session import Session |
| 8 | |
| 9 | from .fake import FakeProvider, turn_text, turn_with_tool |
| 10 | |
| 11 | |
| 12 | def make_config(**kwargs) -> Config: |
| 13 | config = Config() |
| 14 | config.orchestrator_model = kwargs.pop("orchestrator_model", "main-fake") |
| 15 | config.worker_model = kwargs.pop("worker_model", "worker-fake") |
| 16 | config.limits.max_steps = kwargs.pop("max_steps", 10) |
| 17 | config.limits.max_worker_steps = kwargs.pop("max_worker_steps", 8) |
| 18 | config.limits.wait_timeout = kwargs.pop("wait_timeout", 10) |
| 19 | for key, value in kwargs.items(): |
| 20 | setattr(config.limits, key, value) |
| 21 | return config |
| 22 | |
| 23 | |
| 24 | def test_followup_reuses_worker_context(tmp_path: Path): |
| 25 | provider = FakeProvider( |
| 26 | { |
| 27 | "worker-fake": [ |
| 28 | turn_with_tool("write_file", {"path": "out.txt", "content": "one"}), |
| 29 | turn_text("Status: done\nSummary: wrote one"), |
| 30 | turn_text("Status: done\nSummary: follow-up applied"), |
| 31 | ] |
| 32 | } |
| 33 | ) |
| 34 | config = make_config() |
| 35 | |
| 36 | async def scenario(): |
| 37 | session = Session.create( |
| 38 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 39 | config=config, |
| 40 | provider=provider, |
| 41 | cwd=tmp_path, |
| 42 | ) |
| 43 | worker = session.spawn_worker(task="Create out.txt", workspace=".") |
| 44 | await session.wait_for_workers([worker.id], mode="all", timeout=10) |
| 45 | assert worker.status == "done" |
| 46 | reply = await session._tool_message_worker(worker.id, "Also write second.txt") |
| 47 | assert '"running"' in reply |
| 48 | result = await session.wait_for_workers([worker.id], mode="all", timeout=10) |
| 49 | assert result["completed"][0]["followups"] == 1 |
| 50 | assert worker.followups == 1 |
| 51 | assert worker.status == "done" |
| 52 | assert any("Also write second.txt" in str(m.get("content", "")) for m in worker.messages) |
| 53 | assert len(session.workers) == 1 |
| 54 | await session.close() |
| 55 | |
| 56 | asyncio.run(scenario()) |
| 57 | |
| 58 | |
| 59 | def test_cancel_worker_wakes_waiters(tmp_path: Path): |
| 60 | async def responder(messages, count, session_id): |
| 61 | await asyncio.sleep(30) |
| 62 | return turn_text("never") |
| 63 | |
| 64 | provider = FakeProvider({"worker-fake": responder}) |
| 65 | config = make_config() |
| 66 | |
| 67 | async def scenario(): |
| 68 | session = Session.create( |
| 69 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 70 | config=config, |
| 71 | provider=provider, |
| 72 | cwd=tmp_path, |
| 73 | ) |
| 74 | worker = session.spawn_worker(task="long task", workspace=".") |
| 75 | await asyncio.sleep(0.05) |
| 76 | result = await session._tool_cancel_worker(worker.id, "test") |
| 77 | assert "cancelled" in result |
| 78 | assert worker.status == "cancelled" |
| 79 | snapshot = session.check_workers([worker.id]) |
| 80 | assert snapshot["workers"][0]["status"] == "cancelled" |
| 81 | await session.close() |
| 82 | |
| 83 | asyncio.run(scenario()) |
| 84 | |
| 85 | |
| 86 | def test_role_model_routing(tmp_path: Path): |
| 87 | provider = FakeProvider({"review-model": [turn_text("Status: done\nSummary: reviewed")]}) |
| 88 | config = make_config() |
| 89 | config.roles["reviewer"].model = "review-model" |
| 90 | |
| 91 | async def scenario(): |
| 92 | session = Session.create( |
| 93 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 94 | config=config, |
| 95 | provider=provider, |
| 96 | cwd=tmp_path, |
| 97 | ) |
| 98 | worker = session.spawn_worker(task="Review the code", role="reviewer", workspace=".") |
| 99 | assert worker.model == "review-model" |
| 100 | await session.wait_for_workers([worker.id], mode="all", timeout=10) |
| 101 | await session.close() |
| 102 | |
| 103 | asyncio.run(scenario()) |
| 104 | |
| 105 | |
| 106 | def test_worker_runs_and_persists(tmp_path: Path): |
| 107 | provider = FakeProvider( |
| 108 | { |
| 109 | "worker-fake": [ |
| 110 | turn_with_tool("bash", {"command": "printf hello > out.txt"}), |
| 111 | turn_text("Status: done\nSummary: wrote out.txt\nFiles changed: out.txt"), |
| 112 | ] |
| 113 | } |
| 114 | ) |
| 115 | config = make_config() |
| 116 | |
| 117 | async def scenario(): |
| 118 | session = Session.create( |
| 119 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 120 | config=config, |
| 121 | provider=provider, |
| 122 | cwd=tmp_path, |
| 123 | ) |
| 124 | events = [] |
| 125 | session.listener = events.append |
| 126 | worker = session.spawn_worker(task="Create out.txt with hello", workspace=".") |
| 127 | result = await session.wait_for_workers([worker.id], mode="all", timeout=10) |
| 128 | assert result["completed"][0]["status"] == "done" |
| 129 | assert worker.status == "done" |
| 130 | assert (tmp_path / "out.txt").read_text() == "hello" |
| 131 | assert "out.txt" in worker.files_changed |
| 132 | assert worker.report_path and Path(worker.report_path).is_file() |
| 133 | types = [event.type for event in events] |
| 134 | assert "main_sleep" in types |
| 135 | assert types.index("main_sleep") < types.index("worker_started") < types.index("worker_done") |
| 136 | assert types.index("worker_done") < types.index("main_wake") |
| 137 | await session.close() |
| 138 | |
| 139 | asyncio.run(scenario()) |
| 140 | assert provider.closed |
| 141 | |
| 142 | |
| 143 | def test_orchestrator_delegates_and_waits(tmp_path: Path): |
| 144 | provider = FakeProvider( |
| 145 | { |
| 146 | "main-fake": [ |
| 147 | turn_with_tool( |
| 148 | "spawn_worker", |
| 149 | {"task": "Create out.txt with hello and verify it", "workspace": "."}, |
| 150 | "call_spawn", |
| 151 | ), |
| 152 | turn_with_tool("wait_for_workers", {"mode": "all"}, "call_wait"), |
| 153 | turn_text("Готово: out.txt создан воркером."), |
| 154 | ], |
| 155 | "worker-fake": [ |
| 156 | turn_with_tool("write_file", {"path": "out.txt", "content": "hello"}), |
| 157 | turn_text("Status: done\nSummary: wrote out.txt\nFiles changed: out.txt"), |
| 158 | ], |
| 159 | } |
| 160 | ) |
| 161 | config = make_config() |
| 162 | |
| 163 | async def scenario(): |
| 164 | session = Session.create( |
| 165 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 166 | config=config, |
| 167 | provider=provider, |
| 168 | cwd=tmp_path, |
| 169 | ) |
| 170 | events = [] |
| 171 | session.listener = events.append |
| 172 | final = await session.run_turn("Создай out.txt с hello") |
| 173 | assert "out.txt" in final |
| 174 | assert (tmp_path / "out.txt").read_text() == "hello" |
| 175 | assert session.workers["w1"].status == "done" |
| 176 | assert session.workers["w1"].reported is True |
| 177 | main_calls = [call for call in provider.calls if call["model"] == "main-fake"] |
| 178 | assert len(main_calls) == 3 # spawn, wait, final; no polling |
| 179 | types = [event.type for event in events] |
| 180 | assert "main_sleep" in types |
| 181 | assert types.index("main_sleep") < types.index("main_wake") |
| 182 | # The worker report must be in the lead's context as a tool result. |
| 183 | tool_contents = [m.get("content", "") for m in session.main_messages if m.get("role") == "tool"] |
| 184 | assert any("out.txt" in content for content in tool_contents) |
| 185 | await session.close() |
| 186 | |
| 187 | asyncio.run(scenario()) |
| 188 | |
| 189 | |
| 190 | def test_auto_wait_when_lead_tries_to_finish(tmp_path: Path): |
| 191 | provider = FakeProvider( |
| 192 | { |
| 193 | "main-fake": [ |
| 194 | turn_with_tool( |
| 195 | "spawn_worker", |
| 196 | {"task": "Create out.txt with hello", "workspace": "."}, |
| 197 | "call_spawn", |
| 198 | ), |
| 199 | turn_text("Сейчас отвечу без ожидания."), |
| 200 | turn_text("Готово, учёл отчёт воркера."), |
| 201 | ], |
| 202 | "worker-fake": [ |
| 203 | turn_with_tool("write_file", {"path": "out.txt", "content": "hello"}), |
| 204 | turn_text("Status: done\nSummary: wrote out.txt"), |
| 205 | ], |
| 206 | } |
| 207 | ) |
| 208 | config = make_config() |
| 209 | |
| 210 | async def scenario(): |
| 211 | session = Session.create( |
| 212 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 213 | config=config, |
| 214 | provider=provider, |
| 215 | cwd=tmp_path, |
| 216 | ) |
| 217 | events = [] |
| 218 | session.listener = events.append |
| 219 | final = await session.run_turn("Создай out.txt") |
| 220 | assert "учёл" in final |
| 221 | assert any(event.type == "notice" for event in events) |
| 222 | assert (tmp_path / "out.txt").read_text() == "hello" |
| 223 | await session.close() |
| 224 | |
| 225 | asyncio.run(scenario()) |
| 226 | |
| 227 | |
| 228 | def test_wait_any_then_all(tmp_path: Path): |
| 229 | async def responder(messages, count, session_id): |
| 230 | if "_w1" in session_id: |
| 231 | await asyncio.sleep(0.2) |
| 232 | else: |
| 233 | await asyncio.sleep(0.05) |
| 234 | return turn_text("Status: done\nSummary: ok") |
| 235 | |
| 236 | provider = FakeProvider({"worker-fake": responder}) |
| 237 | config = make_config() |
| 238 | |
| 239 | async def scenario(): |
| 240 | session = Session.create( |
| 241 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 242 | config=config, |
| 243 | provider=provider, |
| 244 | cwd=tmp_path, |
| 245 | ) |
| 246 | w1 = session.spawn_worker(task="slow", workspace="one") |
| 247 | w2 = session.spawn_worker(task="fast", workspace="two") |
| 248 | first = await session.wait_for_workers([w1.id, w2.id], mode="any", timeout=10) |
| 249 | assert [item["id"] for item in first["completed"]] == [w2.id] |
| 250 | assert first["still_running"] == [w1.id] |
| 251 | second = await session.wait_for_workers([w1.id, w2.id], mode="all", timeout=10) |
| 252 | assert [item["id"] for item in second["completed"]] == [w1.id] |
| 253 | assert not second["still_running"] |
| 254 | await session.close() |
| 255 | |
| 256 | asyncio.run(scenario()) |
| 257 | |
| 258 | |
| 259 | def test_session_resume(tmp_path: Path): |
| 260 | provider = FakeProvider( |
| 261 | { |
| 262 | "worker-fake": [ |
| 263 | turn_with_tool("write_file", {"path": "out.txt", "content": "hello"}), |
| 264 | turn_text("Status: done\nSummary: wrote out.txt"), |
| 265 | ], |
| 266 | "main-fake": [turn_text("unused")], |
| 267 | } |
| 268 | ) |
| 269 | config = make_config() |
| 270 | root = tmp_path / ".airouter" / "sessions" |
| 271 | |
| 272 | async def scenario(): |
| 273 | session = Session.create( |
| 274 | sessions_root=root, config=config, provider=provider, cwd=tmp_path |
| 275 | ) |
| 276 | worker = session.spawn_worker(task="Create out.txt", workspace=".") |
| 277 | await session.wait_for_workers([worker.id], mode="all", timeout=10) |
| 278 | session.main_messages.append({"role": "user", "content": "после перезапуска"}) |
| 279 | session.save_all() |
| 280 | session_id = session.id |
| 281 | await session.close() |
| 282 | |
| 283 | loaded = Session.load( |
| 284 | sessions_root=root, |
| 285 | session_id=session_id, |
| 286 | config=config, |
| 287 | provider=FakeProvider({}), |
| 288 | ) |
| 289 | assert loaded.id == session_id |
| 290 | assert loaded.workers[worker.id].status == "done" |
| 291 | assert loaded.workers[worker.id].messages |
| 292 | assert loaded.main_messages[-1]["content"] == "после перезапуска" |
| 293 | await loaded.close() |
| 294 | |
| 295 | asyncio.run(scenario()) |
| 296 | |
| 297 | |
| 298 | def test_intelligence_and_explicit_model_routing(tmp_path: Path): |
| 299 | provider = FakeProvider( |
| 300 | { |
| 301 | "high-model": [turn_text("Status: done\nSummary: high")], |
| 302 | "explicit-model": [turn_text("Status: done\nSummary: explicit")], |
| 303 | } |
| 304 | ) |
| 305 | config = make_config() |
| 306 | config.intelligence["high"] = "high-model" |
| 307 | config.roles["coder"].model = "role-model" |
| 308 | |
| 309 | async def scenario(): |
| 310 | session = Session.create( |
| 311 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 312 | config=config, |
| 313 | provider=provider, |
| 314 | cwd=tmp_path, |
| 315 | ) |
| 316 | fixed_role = session.spawn_worker( |
| 317 | task="hard task", workspace="fixed", intelligence="high" |
| 318 | ) |
| 319 | high = session.spawn_worker( |
| 320 | task="hard task", role="reviewer", workspace="high", intelligence="high" |
| 321 | ) |
| 322 | explicit = session.spawn_worker( |
| 323 | task="explicit task", workspace="explicit", model="explicit-model" |
| 324 | ) |
| 325 | assert fixed_role.model == "role-model" |
| 326 | assert high.model == "high-model" |
| 327 | assert high.intelligence == "high" |
| 328 | assert explicit.model == "explicit-model" |
| 329 | await session.wait_for_workers( |
| 330 | [fixed_role.id, high.id, explicit.id], mode="all", timeout=10 |
| 331 | ) |
| 332 | await session.close() |
| 333 | |
| 334 | asyncio.run(scenario()) |
| 335 | |
| 336 | |
| 337 | def test_plan_update_and_settings_persist(tmp_path: Path): |
| 338 | provider = FakeProvider({"main-fake": [turn_text("ok")]}) |
| 339 | config = make_config() |
| 340 | root = tmp_path / ".airouter" / "sessions" |
| 341 | |
| 342 | async def scenario(): |
| 343 | session = Session.create( |
| 344 | sessions_root=root, config=config, provider=provider, cwd=tmp_path |
| 345 | ) |
| 346 | result = session.set_plan( |
| 347 | [ |
| 348 | { |
| 349 | "id": "build", |
| 350 | "title": "Build feature", |
| 351 | "status": "in_progress", |
| 352 | "worker_id": "w1", |
| 353 | "subtasks": [ |
| 354 | {"title": "write code", "status": "done"}, |
| 355 | {"title": "test it", "status": "pending"}, |
| 356 | ], |
| 357 | } |
| 358 | ] |
| 359 | ) |
| 360 | assert result["tasks"] == 1 |
| 361 | assert session.todos[0]["status"] == "in_progress" |
| 362 | assert session.todos[0]["subtasks"][1]["status"] == "pending" |
| 363 | session.apply_settings( |
| 364 | max_workers=3, |
| 365 | orchestrator_model="main-fake", |
| 366 | worker_model="worker-fake", |
| 367 | intelligence={"low": "l", "medium": "m", "high": "h"}, |
| 368 | ) |
| 369 | session_id = session.id |
| 370 | session.save_all() |
| 371 | await session.close() |
| 372 | |
| 373 | config2 = make_config() |
| 374 | loaded = Session.load( |
| 375 | sessions_root=root, |
| 376 | session_id=session_id, |
| 377 | config=config2, |
| 378 | provider=FakeProvider({}), |
| 379 | ) |
| 380 | assert loaded.todos == session.todos |
| 381 | assert loaded.config.limits.max_workers == 3 |
| 382 | assert loaded.config.intelligence == {"low": "l", "medium": "m", "high": "h"} |
| 383 | await loaded.close() |
| 384 | |
| 385 | asyncio.run(scenario()) |