Skip to content

feat(tools): add notebook_edit for structured .ipynb cell edits - #337

Open
raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/notebook-edit
Open

raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/notebook-edit

Conversation

@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor

Summary

A Jupyter notebook is JSON, and a cell's source is stored as a list of lines. Editing one through edit or write is how a notebook gets corrupted — and the model has no structured way to add, remove, or retarget a cell.

notebook_edit does one cell at a time, by id or 0-based index:

  • replace — rewrite the target cell's source (and optionally its type)
  • insert — add a cell at an index
  • delete — remove the target cell

It writes valid nbformat JSON back, keeps code-cell outputs/execution_count consistent when the type changes, and only emits cell ids on nbformat 4.5+ (older files reject the field).

Changes

  • core/harness/tools/notebook.py (new) — the tool, including the workspace fence and an optional per-turn checkpoint capture hook.
  • core/harness/tools/__init__.py — register and export NotebookEditTool in default_coding_tools.
  • core/harness/permissions.py — notebook_edit is a file edit, so it follows the same accept-edits rules as write / edit / apply_patch.
  • core/harness/approval_scope.py — notebook_path is a path argument, so session-scope grants cover a notebook like any other edited file.
  • core/agent_setup.py — name the tool in the core-tool list and say why notebooks need it.
  • tests/test_notebook_edit.py (new, 5 cases) — the three modes, id/index targeting, nbformat 4.4 ids, validation/fence, permission scope.
  • tests/contract/test_default_capability_compatibility.py, tests/test_native_file_tools.py — the default-tool contract includes the new tool.

Security Considerations

  • Attack surface changed: one new file-writing tool, fenced to the workspace exactly like write / edit; no new network or process path.
  • Permission boundary: it is added to _FILE_EDIT_TOOL_NAMES, so it is subject to the same confirmation / accept-edits decisions and cannot bypass them by having a different name.
  • Path handling: notebook_path joins the session-scope path keys, so scope keys cannot silently miss this tool; the tool itself resolves through the same _resolve / _within helpers as the file tools.
  • Fail-safe: invalid JSON, a missing file, a wrong extension, or a missing target cell is rejected with a message; nothing is written unless the edit is valid.

Tests

tests/test_notebook_edit.py 5 passed; the contract and permission tests pass. Two pre-existing failures on this Windows checkout (test_native_file_tools.py read-window cases, and test_harness_permissions.py::test_dot_dot_after_a_linked_directory_is_followed) reproduce on the base commit. pre-commit (ruff 0.15.21 + ruff-format) clean.

I could not add the security label from a fork (no write access) — requesting @Zongwei9888's review in a comment, as the contributing guide asks.

A notebook is JSON and a cell's source is a list of lines, so editing one
through `edit` or `write` can corrupt the document. `notebook_edit` replaces,
inserts, or deletes one cell by id or 0-based index and writes valid nbformat
JSON back (cell ids only on nbformat 4.5+), under the same workspace fence as
the other file tools.

It is registered in `default_coding_tools` and treated as a file edit by the
permission engine (and by session-scope keys via `notebook_path`), so it cannot
slip past the accept-edits boundary that write / edit / apply_patch respect.

Tests: tests/test_notebook_edit.py — replace by index and id, insert/delete,
the nbformat 4.4 id rule, validation and the workspace fence, and the
file-edit permission scope.
@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor Author

@Zongwei9888 requesting security review: this adds a file-writing tool and marks it as a file edit in the permission engine. Security Considerations are in the description; happy to narrow the surface or split the permission change if you prefer.

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