Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions envs/jupyter_env/server/jupyter_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gradio UI checks wrong cell status after edit

Low Severity

Replacing the last code cell in place leaves trailing shell cells at the end of self._state.cells. In envs/jupyter_env/server/gradio_ui.py, on_edit_run still checks cells[-1]["success"] to report whether the edit succeeded, so after a trailing shell command the status bar reflects the shell cell's status instead of the edited code cell's result (unlike last_cell_success).

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 650069c. Configure here.

self._state.last_cell_success = result.success
return _format_for_llm(result)

Expand Down
18 changes: 11 additions & 7 deletions envs/jupyter_env/server/notebook_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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]:
Expand Down
19 changes: 19 additions & 0 deletions tests/envs/test_jupyter_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down