diff --git a/tests/tools/test_code_execution_modes.py b/tests/tools/test_code_execution_modes.py index 2b05d2d3f..5fdbdec58 100644 --- a/tests/tools/test_code_execution_modes.py +++ b/tests/tools/test_code_execution_modes.py @@ -231,6 +231,58 @@ class TestResolveChildCwd(unittest.TestCase): terminal_tool.register_task_env_overrides(task_id, {"cwd": td}) self.assertEqual(_resolve_child_cwd("project", "/tmp/staging", task_id=task_id), td) + def test_project_prefers_session_cwd_record_over_override(self): + """The session's cwd RECORD (its live `cd` state) outranks the + registration-time workspace override — same ladder as file tools + and the terminal, so a `cd` before execute_code is honored.""" + import tempfile + import tools.terminal_tool as terminal_tool + + with tempfile.TemporaryDirectory() as reg, tempfile.TemporaryDirectory() as cded: + task_id = "session-record-test" + with patch.dict(os.environ, {"TERMINAL_CWD": "/does/not/exist"}): + with patch.object(terminal_tool, "_task_env_overrides", {}, create=False), \ + patch.object(terminal_tool, "_session_cwd", {}, create=False): + terminal_tool.register_task_env_overrides(task_id, {"cwd": reg}) + # Simulate a later `cd`: post-command tracking rewrites the record. + terminal_tool.record_session_cwd(task_id, cded) + self.assertEqual( + _resolve_child_cwd("project", "/tmp/staging", task_id=task_id), cded + ) + + def test_project_uses_session_cwd_record_without_any_override(self): + """A session that only `cd`'d (no session.cwd.set registration) still + resolves to its recorded directory.""" + import tempfile + import tools.terminal_tool as terminal_tool + + with tempfile.TemporaryDirectory() as cded: + task_id = "record-only-test" + with patch.dict(os.environ, {"TERMINAL_CWD": "/does/not/exist"}): + with patch.object(terminal_tool, "_task_env_overrides", {}, create=False), \ + patch.object(terminal_tool, "_session_cwd", {}, create=False): + terminal_tool.record_session_cwd(task_id, cded) + self.assertEqual( + _resolve_child_cwd("project", "/tmp/staging", task_id=task_id), cded + ) + + def test_project_stale_record_falls_through_to_override(self): + """A recorded directory that no longer exists is skipped; the + registered override is the next rung.""" + import tempfile + import tools.terminal_tool as terminal_tool + + with tempfile.TemporaryDirectory() as reg: + task_id = "stale-record-test" + with patch.dict(os.environ, {"TERMINAL_CWD": "/does/not/exist"}): + with patch.object(terminal_tool, "_task_env_overrides", {}, create=False), \ + patch.object(terminal_tool, "_session_cwd", {}, create=False): + terminal_tool.register_task_env_overrides(task_id, {"cwd": reg}) + terminal_tool.record_session_cwd(task_id, "/deleted/dir/gone") + self.assertEqual( + _resolve_child_cwd("project", "/tmp/staging", task_id=task_id), reg + ) + # --------------------------------------------------------------------------- # Schema description