diff --git a/envs/jupyter_env/server/jupyter_environment.py b/envs/jupyter_env/server/jupyter_environment.py index ef27e6134..ce6a03335 100644 --- a/envs/jupyter_env/server/jupyter_environment.py +++ b/envs/jupyter_env/server/jupyter_environment.py @@ -109,10 +109,11 @@ def edit_and_execute_current_cell(code: str) -> str: if not self._sandbox: return "Error: environment not reset. Call reset() first." result = self._sandbox.run_code(code) - cell = self._tracker.update_last_code_cell(code, result) - if self._state.cells: - self._state.cells.pop() - self._state.cells.append(self._NotebookCell(**_cell_to_model_kwargs(cell))) + self._tracker.update_last_code_cell(code, result) + self._state.cells = [ + self._NotebookCell(**_cell_to_model_kwargs(cell)) + for cell in self._tracker.cells + ] self._state.last_cell_success = result.success return _format_for_llm(result) diff --git a/envs/jupyter_env/server/notebook_tracker.py b/envs/jupyter_env/server/notebook_tracker.py index e918c92ac..e9486f934 100644 --- a/envs/jupyter_env/server/notebook_tracker.py +++ b/envs/jupyter_env/server/notebook_tracker.py @@ -17,7 +17,7 @@ class NotebookTracker: In-memory cell history for one episode. Cells are appended on every ``add_*_cell`` call. ``update_last_code_cell`` - replaces the most recent cell, implementing the edit-and-re-execute pattern + replaces the most recent code cell, implementing the edit-and-re-execute pattern used by agents to fix errors without cluttering the notebook with failed attempts. """ @@ -39,15 +39,19 @@ def add_code_cell(self, code: str, result: CellResult) -> Dict[str, Any]: def update_last_code_cell(self, code: str, result: CellResult) -> Dict[str, Any]: """ - Replace the last cell with a new code + result pair. + Replace the last code cell with a new code + result pair. This implements the ``edit_and_execute_current_cell`` semantics — - the agent fixes the last cell rather than adding a new failed one. + the agent fixes the last code cell rather than adding a new failed one. + Shell cells are left alone. Without a code cell, a new one is added. """ - if self.cells: - self.cells.pop() - # Reuse the same exec count (it's a replacement, not a new cell) - self._exec_count = max(0, self._exec_count - 1) + for i in reversed(range(len(self.cells))): + if self.cells[i]["cell_type"] == "code": + cell = self._make_cell("code", code, result) + # A replacement keeps the cell's place and execution count + cell["execution_count"] = self.cells[i]["execution_count"] + self.cells[i] = cell + return cell return self.add_code_cell(code, result) def add_shell_cell(self, command: str, result: CellResult) -> Dict[str, Any]: diff --git a/tests/envs/test_jupyter_environment.py b/tests/envs/test_jupyter_environment.py index 1bc136c8d..a8a06f20d 100644 --- a/tests/envs/test_jupyter_environment.py +++ b/tests/envs/test_jupyter_environment.py @@ -153,6 +153,25 @@ def test_shell_tool_updates_notebook_state(): assert env.state.cells[0].cell_type == "shell" +def test_edit_replaces_the_last_code_cell_not_a_shell_cell(): + env = JupyterEnvironment() + env._sandbox = FakeSandbox() + env._state = JupyterState(episode_id="episode-1", sandbox_id="fake-sandbox") + + def call(tool_name, **arguments): + env.step(CallToolAction(tool_name=tool_name, arguments=arguments)) + + call("add_and_execute_code_cell", code="x = 1") + call("execute_shell_command", command="pwd") + call("edit_and_execute_current_cell", code="x = 2") + + assert [(c.cell_type, c.code) for c in env.state.cells] == [ + ("code", "x = 2"), + ("shell", "$ pwd"), + ] + assert [c["code"] for c in env._tracker.cells] == ["x = 2", "$ pwd"] + + def test_reset_runs_setup_and_stores_verify_commands(monkeypatch): monkeypatch.setenv("E2B_API_KEY", "fake-key") env = JupyterEnvironment()