Skip to content

Coding agents: Cursor's StrReplace, ApplyPatch and EditNotebook are decided as the edits they are - #767

Merged
LoopGlitch26 merged 6 commits into
mainfrom
agent/cursor-edit-tools
Nov 13, 2026
Merged

LoopGlitch26 merged 6 commits into
mainfrom
agent/cursor-edit-tools

Conversation

@LoopGlitch26

@LoopGlitch26 LoopGlitch26 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What and why

Purv-Kabaria found, in #755, the names Cursor 3.29.15 gives its edit tools: StrReplace (path, old_string,
new_string, replace_all), Write (path, contents), Delete (path), EditNotebook and ApplyPatch (Codex
models only), and in an older prompt version search_replace, write, delete_file and edit_notebook.

Tracekit's Cursor mapping (CURSOR_TOOLS) had none of the edit names. A payload that named them was decided as an
unknown tool, which the dev and coding packs only flag. So a StrReplace of ~/.cursor/hooks.json was allowed where
a Write of it is held (TK-F009), and a StrReplace of .env was allowed where an Edit of it is denied
(TK-D005), on the v1 and the v2 hook.

What Cursor's own preToolUse hook sends, read from the code of Cursor 3.26.13 (installed) and 3.29.15 (current
stable, downloaded from cursor.com): Cursor fires the hook where a call runs, not with the model's tool name. An edit
by StrReplace, ApplyPatch or EditNotebook reaches the hook as a Read of the file and then a Write with
file_path and the whole new content, both under the model's call id; a delete as Delete with file_path. Cursor
also translates a Claude Code hook's Edit matcher to its Write. So the live hook already held these edits as
Writes. The model's names reach Tracekit where a payload carries them: Cursor's transcripts, which tracekit audit
replays through this mapping (#755), and any Cursor build that sends them.

Changes

  • StrReplace and search_replace → Edit; write → Write; delete_file → Edit, as Delete is;
    EditNotebook and edit_notebook → NotebookEdit, decided on target_notebook (now in CURSOR_PATH_ARGS). Each
    is mapped only when the argument its tool reads its file from (path, file_path, file_path, target_file,
    target_notebook, from Cursor 3.29.15's schemas) holds a string; otherwise the name passes through unmapped, an
    unknown tool, exactly as on main. Cursor refuses such a call.
  • ApplyPatch → Edit, with paths set to every file a patch header names, in whichever argument holds it, as
    Factory Droid's is read. It is read the way Cursor reads it: a JSON {"patch": ...} is unwrapped (tried only when the
    trimmed text starts with {; text that isn't JSON is read as the patch it is), \r and \r\n are read as \n,
    and each line is trimmed as JavaScript trims it. A path argument sent beside the patch (path, file_path,
    paths, …) doesn't replace its files and is decided with them, as is one beside an EditNotebook's
    target_notebook. A patch Tracekit can't read, such as one with no file header (none, empty, {}, {"patch": 5}),
    passes through unmapped and is decided as an unknown tool, as on main.
  • TK-F009 (an agent's edit of its harness's settings, held) and TK-F016 (ANTHROPIC_BASE_URL in a call's
    arguments, flagged) now also cover NotebookEdit. They were the two file-write rules in the packs without it, so an
    EditNotebook of ~/.cursor/hooks.json would still have run. The coding pack's version is 2026.11-v2-83:
    v2-82 is taken by Coding agents: hooks for CodeBuddy Code (#757) #761, which is still open. strict.yaml itself is unchanged; it gets the change through
    extends, as with earlier coding-pack changes. The eval results get the packs' new policy hashes; no result changed.
  • JavaScript's trim() is now str.strip over JavaScript's whitespace set (JS_SPACE) instead of a regex that took
    quadratic time on a long run of spaces (4.65 s for 40,000). That regex was used for OpenCode's and Cline's patches and
    Cline's here-docs too, so all of them now use JS_SPACE. Python's \s also stripped \x1c-\x1f and \x85,
    which JavaScript keeps.
  • Docs: Cursor's tool names in docs/coding-agents.md; a docs/limits.md row on what Cursor's hook sends and the names
    it sends that stay unmapped (Fetch, List, ReadLints, WriteShellStdin, which carries the typed text's length
    but not the text, FetchMcpResource, …); NotebookEdit in the TK-F009 and TK-F016 rows.

How I ran it as a user

tracekit init --dev --v2 --agent cursor in a temporary HOME, then the preToolUse and postToolUse commands it
wrote, run through a shell as Cursor runs them, with Cursor 3.29.15's payload shapes:

-- what Cursor 3.29.15's hook sends for one StrReplace of its own hooks file (Read, then Write, one call id)
preToolUse   Read           $HOME/.cursor/hooks.json   exit=0 {"permission": "allow"}
postToolUse  Read           $HOME/.cursor/hooks.json   exit=0 {}
preToolUse   Write          $HOME/.cursor/hooks.json   exit=2 {"permission": "deny", ...} [tracekit] HELD, nothing ran: apr-851e… (TK-F009)
-- the same edit under the names Cursor's transcripts give its tools
preToolUse   StrReplace     $HOME/.cursor/hooks.json   exit=2 {"permission": "deny", ...} [tracekit] HELD, nothing ran: apr-a926… (TK-F009)
preToolUse   ApplyPatch     (patch)                    exit=2 {"permission": "deny", ...} [tracekit] HELD, nothing ran: apr-0531… (TK-F009)
-- after the review: a patch with \r line ends, and edits naming no file (unknown tools: flagged on dev, as on main)
preToolUse   ApplyPatch     (\r patch)                 exit=2 {"permission": "deny", ...} [tracekit] HELD, nothing ran: apr-0eff… (TK-F009)
preToolUse   ApplyPatch     (no header)                exit=0 {"permission": "allow"}
preToolUse   StrReplace     (no path)                  exit=0 {"permission": "allow"}
-- an ordinary edit of a project file
preToolUse   StrReplace     $PROJECT/src/app.py        exit=0 {"permission": "allow"}
postToolUse  StrReplace     $PROJECT/src/app.py        exit=0 {}

The dev signer that run started loaded the packs from the main checkout, so EditNotebook of the hooks file went
through there (NotebookEdit wasn't in TK-F009 yet); test_cursor_edit_tools_are_decided_as_the_edits_they_are
in tests/test_harness_hooks_v2.py runs the same payloads through a signer with this branch's packs, where it is held.

Tests

Each fails on main:

  • tests/test_signer_policy.py CodingPack.test_cursor_edit_tools_are_decided_as_the_edits_they_are: every Cursor
    name, on ~/.cursor/hooks.json, .env and a project file, is decided as its Claude Code form (same verdict and
    rule ids) on the dev, coding, server and strict packs, on both regex engines; the hooks file is held as a Write of
    it is (TK-F009), .env denied (TK-D005), the project file never TK-UNKNOWN-TOOL.
  • tests/test_harness_hooks_v2.py OnTheDevPolicy.test_cursor_edit_tools_are_decided_as_the_edits_they_are: the v2
    hook against a real signer, starting with the Read-then-Write Cursor sends; the signed decisions name Edit,
    Write and NotebookEdit.
  • tests/test_agent_hooks.py: Harnesses.test_cursor_edit_tools_are_decided_as_edits (the v1 hook against a real
    daemon) and CursorTools.test_each_is_decided_on_the_file_its_tool_reads (v1 and v2 policy, with decoy path keys).

Added after the first review, each failing on the first head (dc03b4d):

  • A CR-only patch of the hooks file, raw or JSON-wrapped, is held, and one of .env is denied: in the every-pack test,
    the v2 hook test and the v1 policy test.
  • CodingPack.test_a_cursor_edit_naming_no_file_stays_an_unknown_tool: on each pack and engine, a header-less, empty,
    {}, {"patch": 5} or blank-header patch, and a StrReplace, search_replace, write, delete_file,
    EditNotebook or edit_notebook without a string in its file argument, keeps its own name, gets TK-UNKNOWN-TOOL,
    and is never allowed. The v2 hook test checks the signed decision says the same, and
    CursorTools.test_one_naming_no_file_is_left_to_the_v1_policy_as_before checks the v1 side.
  • CodingPack.test_a_notebook_edit_naming_the_model_endpoint_is_flagged: TK-F016 flags a NotebookEdit, and a
    Cursor EditNotebook, that sets ANTHROPIC_BASE_URL.
  • CursorTools.test_a_long_run_of_spaces_takes_linear_time: a Cursor patch, an OpenCode patch, a Cline patch and a
    Cline here-doc, each with a run of 40,000 spaces inside a line, take under 1 s of CPU time together
    (time.process_time); the removed regex took about 4 s on one such line.

The reviewer's probes on this head: every unreadable shape decides exactly as on main on all four packs, v1 included;
each CR-only shape is held through the real v2 hook and signer.

Added after the second review, each failing on 85d6fd9:

  • In the every-pack test: a patch with a "{" argument beside it, and a patch whose first line is {, are decided on
    their files (held on the hooks file, denied on .env) instead of as unknown tools.
  • CodingPack.test_every_path_argument_of_a_cursor_edit_is_decided: an EditNotebook, edit_notebook or
    ApplyPatch with /w/.env in another path argument (file_path, path, target_file, paths) is denied
    (TK-D005) on each pack and engine.
  • test_a_cursor_edit_naming_no_file_stays_an_unknown_tool decides at run time which JSON this Python's json
    module reads: Python 3.12 and later read nesting 5,000 deep, where 3.11 doesn't, and a patch it reads is held as an
    edit of the hooks file. On 85d6fd9 it failed on 3.13 (8 subtests), and 3.12's json reads the same input.

The gate in the worktree (make test-ts lint build test) on this head, on two Pythons. 3.11: TypeScript 162 tests, 152 passed, 10 skipped, 0 failed; pyflakes clean; the wheel and sdist built and passed twine check; pytest 5001 passed, 394 skipped, 10 xfailed, 64875 subtests passed. 3.13 (a stdlib venv, as CI makes): TypeScript the same; pyflakes clean; build and twine check passed; pytest 5111 passed, 284 skipped, 10 xfailed, 64945 subtests passed. The branch merged main up to #736; it doesn't take main's later commits, so that the merge train with the other policy PRs settles the pack version and the eval policy hashes once.

Checklist

  • make check passes (lint, tests, build); make test-ts too if sdk/typescript/ changed
  • a regression test covers the change
  • user-visible changes are described above (release notes are written at release time; don't edit CHANGELOG.md)
  • docs updated if behaviour or a security claim changed (docs/threat-model-laptop.md,
    docs/threat-model-server.md)
  • every commit is signed off (git commit -s, see CONTRIBUTING.md)

Security invariants

  • the agent still never holds a signing key or assigns sequence numbers
  • nothing an agent-controlled process supplies (isolation level, fail mode, gap/tamper records,
    approval identity) is newly trusted by the signer or verifier
  • gaps are still signed, never silent; a check that cannot run warns instead of passing
  • no verification code is shipped inside a bundle

Evidence format

  • this change does not touch the frozen formats: v1 (core.sig_message, ledger.make_record, the v1 schema, how
    the verifier treats existing bundles) or the published v2 format
  • or, if it does: the issue explicitly asks for it, existing bundles (including docs/sample/ and
    tests/fixtures/) still verify, and the change is called out above

…ecided as the edits they are

Cursor 3.29.15 names its edit tools StrReplace, Write, Delete, EditNotebook
and ApplyPatch (an older prompt: search_replace, write, delete_file,
edit_notebook), as Purv-Kabaria found in #755. The Cursor mapping had none of
the edit names, so a payload naming them was decided as an unknown tool, a
flag: a StrReplace of ~/.cursor/hooks.json ran where a Write of it is held
(TK-F009), and one of .env where an Edit of it is denied (TK-D005).

StrReplace and search_replace map to Edit, write to Write, delete_file to Edit
(as Delete), EditNotebook and edit_notebook to NotebookEdit on their
target_notebook. ApplyPatch maps to Edit on every file a patch header names,
in any argument, as Cursor reads it (a JSON {"patch": ...} unwrapped, each
line trimmed as JavaScript trims). TK-F009 now covers NotebookEdit, the only
file-write rule without it. Evals: the packs' new policy hash (no result
changed).

Cursor's own preToolUse hook (read in 3.26.13 and 3.29.15) sends an edit by
any of these tools as a Read and then a Write of the whole file, which was
already held; docs/limits.md says so and lists the names it sends that stay
unmapped.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…inear trim; TK-F016 reads NotebookEdit

From the review of #767:

- A Cursor edit Tracekit can't read a file from (an ApplyPatch with no file
  header, an empty or non-string patch, a StrReplace, search_replace, write,
  delete_file, EditNotebook or edit_notebook without a string in the argument
  its tool reads its file from) was mapped to an Edit of no file and allowed.
  It now keeps its own name, an unknown tool, decided exactly as on main.
- ApplyPatch reads \r and \r\n as line ends, as Cursor does: a CR-only patch
  of ~/.cursor/hooks.json was decided on no file. It is held.
- JavaScript's trim() is str.strip over JavaScript's whitespace (JS_SPACE),
  not a regex that took 4.65 s on a line of 40,000 spaces; OpenCode's and
  Cline's patch and here-doc readers used the same regex. JSON is tried only
  on text starting with {.
- TK-F016 also reads NotebookEdit, as TK-F009 now does. Coding pack
  2026.11-v2-83 (v2-82 is #761's); eval results get the new policy hashes,
  no result changed.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…ine's readers in the timing test

An ApplyPatch argument that starts with { and that Python's json module
can't read (nesting past its recursion limit, an integer past its digit
limit) now leaves the whole patch unread: the call keeps its own name, an
unknown tool, decided as on main, whatever headers its other arguments hold.

The CPU-time test also runs Cline's apply_patch and run_commands here-doc
readers, on a line with a run of spaces inside it.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…t of a patch or notebook edit is decided

From the review of #767:

- An ApplyPatch argument starting with { that isn't JSON (json.JSONDecodeError)
  no longer leaves the patch unread: Cursor's JSON.parse fails on it too and
  Cursor reads the text as the patch, so Tracekit does as well. A patch with a
  "{" note beside it, or a first line of "{", is decided on its files.
- ApplyPatch and EditNotebook decide every path argument the input names
  (file_path, path, target_file, filePath, target_notebook, paths) beside the
  file the tool reads, as Cursor's other tools already did.
- The unknown-tool test decides at runtime which JSON this Python reads: 3.12
  and later read nesting 5,000 deep, and the patch is then held as an Edit of
  the hooks file.
- docs/coding-agents.md says so.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
@LoopGlitch26

Copy link
Copy Markdown
Member Author

This lands in #799 (a merge train of the policy PRs), which renumbers rule ids and re-records the evals once; your commits stay as they are and this PR will show as merged.

@LoopGlitch26
LoopGlitch26 merged commit ae0a13d into main Nov 13, 2026
32 of 34 checks passed
@LoopGlitch26
LoopGlitch26 deleted the agent/cursor-edit-tools branch November 13, 2026 08:50
kausthubhk pushed a commit to kausthubhk/Tracekit that referenced this pull request Nov 14, 2026
Carries Cygnux-Labs#731 onto main (5d88184): Cygnux-Labs#732 took v2-81 and the open Cygnux-Labs#761 and
Cygnux-Labs#767 claim the next numbers, so this takes v2-84; the final number is
settled at merge. E17 is 853/853 with 1 false alarm in 901; E21's
held-out pool is 259/420 on default and 286/420 on strict, and strict
holds 47 of E17's 901 benign calls. The blind pools don't move. The
held-out table in docs/evaluation.md quotes the new rows; the machine,
engine-version and latency fields stay main's.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
kausthubhk pushed a commit to kausthubhk/Tracekit that referenced this pull request Nov 14, 2026
Cygnux-Labs#761 also took v2-82, and the open Cygnux-Labs#767 and Cygnux-Labs#731 claim the next numbers,
so this takes v2-85; the final number is settled at merge. Only the
policy hashes move: E17 is 846/846 with 1 false alarm in 904, E21's
held-out pool is 255/420 on default and 285/420 on strict, and
amandeavor's blind pool is 37 and 40 of 45. The machine, engine-version
and latency fields stay main's.

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
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