Repository navigation
feat(tools): add notebook_edit for structured .ipynb cell edits - #337
Open
raymondginger2018-sudo wants to merge 1 commit into
Open
raymondginger2018-sudo wants to merge 1 commit into
raymondginger2018-sudo wants to merge 1 commit into
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A Jupyter notebook is JSON, and a cell's source is stored as a list of lines. Editing one through
editorwriteis how a notebook gets corrupted — and the model has no structured way to add, remove, or retarget a cell.notebook_editdoes 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 indexdelete— remove the target cellIt writes valid nbformat JSON back, keeps code-cell
outputs/execution_countconsistent 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 exportNotebookEditToolindefault_coding_tools.core/harness/permissions.py—notebook_editis a file edit, so it follows the same accept-edits rules aswrite/edit/apply_patch.core/harness/approval_scope.py—notebook_pathis 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
write/edit; no new network or process path._FILE_EDIT_TOOL_NAMES, so it is subject to the same confirmation / accept-edits decisions and cannot bypass them by having a different name.notebook_pathjoins the session-scope path keys, so scope keys cannot silently miss this tool; the tool itself resolves through the same_resolve/_withinhelpers as the file tools.Tests
tests/test_notebook_edit.py5 passed; the contract and permission tests pass. Two pre-existing failures on this Windows checkout (test_native_file_tools.pyread-window cases, andtest_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
securitylabel from a fork (no write access) — requesting @Zongwei9888's review in a comment, as the contributing guide asks.