From a3c0de1a361ce541a3a2657ecc4507668cbb57ef Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Thu, 16 Jul 2026 01:17:50 -0700 Subject: [PATCH] test(execute-code): cover session cwd record precedence in project mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up for salvaged PRs #56055 + #56803, adapted to the per-session cwd record store (PR #65213): the record (live cd state) is rung 1, the registered session.cwd.set override rung 2, TERMINAL_CWD rung 3 — the same ladder file tools and the terminal resolve against. Covers record-over-override, record-only, and stale-record fall-through. --- tests/tools/test_code_execution_modes.py | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) 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