"""Tests for the shared tool-call dispatch (scheduler/tools/dispatch.py). Session 2 dedup (docs/refactor-handoffs/02-dedup.md Task 2): both worker.py's OpenAI-compatible loop and backends/copilot.py's Copilot SDK loop now call dispatch_tool_call for their shared, non-terminal tool calls. These tests pin the dispatch contract directly, including a real behavior fix found while unifying it: worker.py's run_command previously did not cap its requested timeout against config.max_shell_timeout at all (copilot.py already did) -- both now cap consistently. """ from pathlib import Path from afk_developer.config import Config from afk_developer.scheduler.tools.dispatch import dispatch_tool_call def test_dispatch_routes_read_file_to_the_real_tool(tmp_path: Path) -> None: (tmp_path / "hello.py").write_text("print('hi')\n", encoding="utf-8") config = Config(projects_dir=tmp_path / "state") output = dispatch_tool_call( "read_file", {"path": "hello.py"}, project_dir=str(tmp_path), evidence_roots=[], config=config, ) assert "print('hi')" in output def test_dispatch_routes_write_file_and_creates_the_file(tmp_path: Path) -> None: config = Config(projects_dir=tmp_path / "state") dispatch_tool_call( "write_file", {"path": "new.py", "content": "value = 1\n"}, project_dir=str(tmp_path), evidence_roots=[], config=config, ) assert (tmp_path / "new.py").read_text(encoding="utf-8") == "value = 1\n" def test_dispatch_caps_run_command_timeout_at_the_configured_shell_limit( tmp_path: Path, monkeypatch ) -> None: """Real behavior fix found while unifying dispatch: worker.py's run_command previously passed the model-requested timeout straight through with no cap at all, unlike copilot.py (which already capped it). Both now share this same capping logic.""" captured: dict = {} def fake_run_command(command, project_dir, timeout): captured["timeout"] = timeout return "ok" monkeypatch.setattr("afk_developer.scheduler.tools.dispatch.run_command", fake_run_command) config = Config(projects_dir=tmp_path / "state", max_shell_timeout=30) dispatch_tool_call( "run_command", {"command": "echo hi", "timeout": 9999}, project_dir=str(tmp_path), evidence_roots=[], config=config, ) assert captured["timeout"] == 30 def test_dispatch_unknown_tool_returns_a_readable_error() -> None: config = Config(projects_dir=Path(".")) output = dispatch_tool_call( "not_a_real_tool", {}, project_dir=".", evidence_roots=[], config=config ) assert output == "Unknown tool: not_a_real_tool" def test_dispatch_applies_task_pytest_scope_policy(tmp_path: Path, monkeypatch) -> None: calls = [] def fake_run_command(*args, **kwargs): calls.append((args, kwargs)) return "TASK_COMMAND_POLICY_REJECTED: scoped task" monkeypatch.setattr("afk_developer.scheduler.tools.dispatch.run_command", fake_run_command) config = Config(projects_dir=tmp_path / "state") output = dispatch_tool_call( "run_command", {"command": "python -m pytest -q"}, project_dir=str(tmp_path), evidence_roots=[], config=config, disallow_unscoped_pytest=True, ) assert output.startswith("TASK_COMMAND_POLICY_REJECTED:") assert calls[0][1]["disallow_unscoped_pytest"] is True def test_dispatch_routes_normalize_line_endings(tmp_path: Path) -> None: """worker.py's schema now offers normalize_line_endings (shared schemas module) -- confirm its dispatch is also wired, not just copilot.py's.""" (tmp_path / "file.txt").write_bytes(b"a\r\nb\r\n") config = Config(projects_dir=tmp_path / "state") dispatch_tool_call( "normalize_line_endings", {"path": "file.txt", "style": "lf"}, project_dir=str(tmp_path), evidence_roots=[], config=config, ) assert (tmp_path / "file.txt").read_bytes() == b"a\nb\n" def test_dispatch_rejects_mutation_outside_discovery_checkpoint_outputs(tmp_path: Path) -> None: config = Config(projects_dir=tmp_path / "state") output = dispatch_tool_call( "write_file", {"path": "scratch.py", "content": "value = 1\n"}, project_dir=str(tmp_path), evidence_roots=[], config=config, allowed_output_paths=["tests/test_witness.py"], ) assert output.startswith("TASK_MUTATION_SCOPE_REJECTED:") assert not (tmp_path / "scratch.py").exists()