from __future__ import annotations from pathlib import Path import subprocess import tempfile import unittest from agent_harness.live_agent_experiment import ( AgentWorkspace, swe_agent_style_tool_definitions, tool_definitions, ) from agent_harness.specs import load_harnesses, load_tasks ROOT = Path(__file__).resolve().parents[1] class LiveAgentExperimentTests(unittest.TestCase): def test_workspace_applies_edit_and_reconstructs_base_relative_diff(self) -> None: task = load_tasks(ROOT)["TASK_CR_001"] with tempfile.TemporaryDirectory() as temporary: tree = Path(temporary) / "tree" tree.mkdir() source = tree / "source.go" source.write_text("package demo\n\nfunc value() int { return 1 }\n", encoding="utf-8") workspace = AgentWorkspace(tree, ("source.go",), task, max_test_runs=2) result = workspace.apply_patch( "--- a/source.go\n" "+++ b/source.go\n" "@@ -1,3 +1,3 @@\n" " package demo\n" " \n" "-func value() int { return 1 }\n" "+func value() int { return 2 }\n" ) self.assertTrue(result["accepted"]) final = workspace.final_patch() self.assertIn("--- a/source.go", final) self.assertIn("+func value() int { return 2 }", final) check = subprocess.run( ["git", "apply", "--check", "-"], cwd=tree, input=final, text=True, capture_output=True, check=False, ) # It should no longer apply to the edited tree, demonstrating that # the reconstructed patch is relative to the frozen base. self.assertNotEqual(check.returncode, 0) def test_workspace_rejects_test_edits(self) -> None: task = load_tasks(ROOT)["TASK_CR_001"] with tempfile.TemporaryDirectory() as temporary: tree = Path(temporary) (tree / "source_test.go").write_text("package demo\n", encoding="utf-8") workspace = AgentWorkspace(tree, ("source_test.go",), task, max_test_runs=2) with self.assertRaises(ValueError): workspace.apply_patch( "--- a/source_test.go\n+++ b/source_test.go\n@@ -1 +1 @@\n-package demo\n+package changed\n" ) def test_tools_separate_unified_specialized_and_control_interfaces(self) -> None: harnesses = load_harnesses(ROOT) task = load_tasks(ROOT)["TASK_CR_001"] names = lambda harness_id: { item["function"]["name"] for item in tool_definitions(harnesses[harness_id], task) } self.assertIn("search_code", names("H007")) self.assertNotIn("search_dense", names("H007")) self.assertIn("search_dense", names("H011")) self.assertIn("search_graph", names("H011")) self.assertFalse(any(item.startswith("search_") for item in names("H016"))) self.assertFalse(any(item.startswith("search_") for item in names("H018"))) def test_swe_agent_style_interface_is_exact_search_only(self) -> None: task = load_tasks(ROOT)["TASK_CR_001"] names = { item["function"]["name"] for item in swe_agent_style_tool_definitions(task) } self.assertIn("find_files", names) self.assertIn("search_text", names) self.assertNotIn("search_dense", names) self.assertNotIn("search_graph", names) def test_python_workspace_rejects_test_edits(self) -> None: task = load_tasks(ROOT)["TASK_S2_R003_003"] with tempfile.TemporaryDirectory() as temporary: tree = Path(temporary) test_path = tree / "tests" / "unit" / "test_example.py" test_path.parent.mkdir(parents=True) test_path.write_text("def test_example():\n assert True\n") workspace = AgentWorkspace( tree, ("tests/unit/test_example.py",), task, max_test_runs=2 ) with self.assertRaises(ValueError): workspace.apply_patch( "--- a/tests/unit/test_example.py\n" "+++ b/tests/unit/test_example.py\n" "@@ -1,2 +1,2 @@\n" " def test_example():\n" "- assert True\n" "+ assert False\n" ) if __name__ == "__main__": unittest.main()