Skip to content

fix(jupyter_env): edit the last code cell, not a trailing shell cell - #1741

Open
Aafimalek wants to merge 1 commit into
openenv:mainfrom
Aafimalek:fix/jupyter-edit-last-code-cell
Open

Aafimalek wants to merge 1 commit into
openenv:mainfrom
Aafimalek:fix/jupyter-edit-last-code-cell

Conversation

@Aafimalek

@Aafimalek Aafimalek commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

edit_and_execute_current_cell is documented as replacing the last code cell, but the tracker popped the last cell of any type. After running code, then execute_shell_command "pip install foo", an edit deleted the shell cell and appended the new code, leaving [code:x=1, code:x=2].

update_last_code_cell now replaces the last code cell in place and keeps its execution count. Without a code cell, it adds a new one. The env rebuilds state.cells from the tracker, so the two stay in sync.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • New environment
  • Refactoring

Alignment Checklist

Before submitting, verify:

  • I have read .claude/docs/PRINCIPLES.md and this PR aligns with our principles
  • I have checked .claude/docs/INVARIANTS.md and no invariants are violated
  • I have run /pre-submit-pr (or bash .claude/hooks/lint.sh and tests) and addressed all issues

RFC Status

  • Not required (bug fix, docs, minor refactoring)
  • RFC exists: #___
  • RFC needed (will create before merge)

Test Plan

  • New test_edit_replaces_the_last_code_cell_not_a_shell_cell in tests/envs/test_jupyter_environment.py, using the existing FakeSandbox. On main: 1 failed, 63 passed. With the fix: 64 passed (WSL, Python 3.12).
  • bash .claude/hooks/lint.sh passes.
  • Not run: the full test suite, and a live E2B sandbox.

Claude Code Review

Alignment review by Opus 5.5, in the format of .claude/skills/alignment-review/SKILL.md.

Automated Checks

  • Lint: PASS, bash .claude/hooks/lint.sh
  • Debug code: CLEAN

Tier 1: Fixes Required

None.

Tier 2: Alignment Discussion

None.


Note

Low Risk
Scoped bug fix in notebook edit semantics with a new unit test; no auth, security, or infra impact.

Overview
Fixes edit_and_execute_current_cell so it targets the most recent code cell instead of whatever cell is last in the notebook—fixing a bug where a shell step after code (e.g. pip install) caused edits to remove the shell cell and leave duplicate code cells.

NotebookTracker.update_last_code_cell now walks backward, replaces the last code cell in place, and keeps that cell’s execution count; shell cells are unchanged, and with no code cell it falls back to appending one.

JupyterEnvironment rebuilds state.cells from the tracker after an edit so agent-visible state matches tracker history. A regression test covers code → shell → edit.

Reviewed by Cursor Bugbot for commit 650069c. Bugbot is set up for automated code reviews on this repo. Configure here.

edit_and_execute_current_cell removed the last cell even when it was a
shell cell. Replace the last code cell in place instead.

@cursor cursor Bot left a comment

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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

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

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant