Repository navigation
fix(diff): keep accepted diffs through reentrant bulk closes and other clients - #361
Conversation
…r accepts close_active_diffs snapshots the tab names, then closes each one. Each close fires ClaudeCodeDiffClosed synchronously. A handler that accepts another diff from the snapshot turns it into a saved diff, and the next loop step then tears it down with the user's accepted edits. This test fails until the loop rechecks its filter before each close. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
close_active_diffs closed every tab name in its snapshot, even when a ClaudeCodeDiffClosed handler fired by an earlier close had accepted that diff in the meantime. :ClaudeCodeCloseAllDiffs, server stop, and the disconnect cleanup then tore down a diff that held the user's accepted edits. After a disconnect, nothing else held them. The loop now runs the same filter on the entry as it is right before each close. The formal check models the handler as an assumption (ClosedHandlerResolvesOther) in every run, and the new fact CloseRechecksFilter reads the recheck. Without it, SavedDiffKept breaks. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
VCN_CLIENT=<name> $V connect starts one more ws-client.mjs with its own message files, and VCN_CLIENT makes rpc, tool, and await act as that client. $V disconnect kills a client by its recorded PID. This lets a replay show what one Claude session does to another session's diffs. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
When the user accepts a diff with :w, the accepted edits live only in the proposed buffer until the Claude client that opened it writes the file. Another client's closeAllDiffTabs, close_tab, or openDiff on the same tab_name tears that diff down today. If the owner died before it wrote the file, the edits are lost. These tests fail until the plugin keeps another client's saved diff. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The real server invokes close_tab and closeAllDiffTabs through handle_invoke, which dropped the calling client for tools that do not need a coroutine. A replay on Neovim showed it: calling the tool handlers directly passed, while the real path still lost the edits. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
When the user accepts a diff with :w, the edits live only in the proposed buffer until the client that opened it writes the file. Another client's closeAllDiffTabs, close_tab, or openDiff on the same tab_name tore that diff down. If the owner had exited before it wrote the file, the edits were lost. The Claude CLI calls closeAllDiffTabs at the start of every turn, so a second session or a restarted one hit this. close_diff_by_tab_name now takes the calling client and keeps a saved diff that belongs to another client. handle_invoke passes the client to every tool, close_tab and closeAllDiffTabs pass its id on, and a replacing openDiff from another client fails with -32000. The formal check now gives each Claude close a calling client. SavedDiffKept counts a saved diff as lost when it is torn down while its owner is disconnected, and the facts ReplaceKeepsOthersSaved and CloseKeepsOthersSaved read the new guards. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…p fixes check.sh --mutants catches every fact, and check.sh passes. The record also covers the main commits since the last sync. Those change only terminal code, the temp-file helpers in diff.lua, and a command description in init.lua, so none of them reaches diff state, an openDiff answer, a disconnect, or a diff command. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The three new facts get a table of their own under the main one. The main fact table stays byte-identical to main, so a parallel change to it does not collide with these rows. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 76f124ff27
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
A diff the user accepted keeps the id of the client that opened it after that client disconnects. The id comes from the TCP handle's address, which libuv can give to a later connection. That connection then passes as the owner. This test fails until ids are unique for the life of the Neovim process. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
A diff the user accepted keeps the id of the client that opened it after that client disconnects. The id came from the TCP handle's address, which libuv can hand to a later connection. That connection then passed as the owner, and its closeAllDiffTabs, close_tab, or openDiff tore the accepted diff down. create_client now builds each id from a counter that lives as long as the Neovim process. The formal check reads it as ClientIdsUnique. Without it, ClientIdReused lets a new connection take a disconnected client's id, and SavedDiffKept breaks. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…rancy-issue342 # Conflicts: # .agents/skills/verify-claudecode-nvim/scripts/vcn.sh # .agents/skills/verify-diff-lifecycle/SKILL.md # .agents/skills/verify-diff-lifecycle/covered.lock # .agents/skills/verify-diff-lifecycle/model/DiffLifecycle.tla
The merge brings in #357 (SetupEnds, SetupAnswerReturned) and keeps this branch's facts on top of it. check.sh --mutants catches every fact, and check.sh passes with 89,205 states. The record also covers #355, which changes only debug log text in server/init.lua, close_tab, and get_diagnostics, so it reaches no diff state or answer. Refs #342 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
PASS+NOTES
Generated with |
…r#383) ## Why The operator goal is to clear the open follow-up issues in this repository (coder#362, coder#363 and coder#374 now) with small reviewed PRs. This PR takes coder#363 items 2 and 3. 1. **Item 3. A reload repeated client ids.** A diff that the user accepted with `:w` stays tied to the id of the client that opened it, and no other client may close it. Since coder#361 the ids come from a counter, but the counter was a local of `claudecode.server.client`. A reload of `claudecode.server.*` restarted it at 1, while `claudecode.diff` kept the old ids. After a server restart, the next Claude got the id of a Claude that had exited. It then passed as the owner of that Claude's accepted diff, and its `closeAllDiffTabs` dropped the accepted edits before anything wrote them to disk. 2. **Item 2. A Claude CLI that reconnects gets a new id.** I read the Claude Code 2.1.312 bundle to find anything that ties a new connection to the old one. Nothing does, and the CLI never refers to an older diff over a new connection (details below). The only per-process value is a pid, which is not safe to match on. So this PR documents the behavior and does not change it. ## What changed - `lua/claudecode/server/client.lua`: `create_client` counts in `_G.claudecode_clients_created`, which a module reload keeps. Ids keep the form `client_<n>`. - `PROTOCOL.md`, section "4. Claude Connects": each connection is a client, a reconnect makes a new client, and what Claude Code 2.1.312 sends and does on a reconnect. - `lua/claudecode/server/README.md`: the id invariant and the new global. - `verify-diff-lifecycle`: the `ClientIdsUnique` fact now reads the global counter. It also requires that nothing else in `lua/` or `plugin/` assigns that global, with or without spaces, through `_G["..."]`, or with `rawset`. `covered.lock` is re-recorded. - `tests/unit/server/client_id_spec.lua`: a test that reloads the module three times and asserts that no id comes back. <details> <summary>What Claude Code 2.1.312 sends and does on a reconnect (read from <code>~/.local/share/claude/versions/2.1.312</code>)</summary> - The `ws-ide` transport opens the socket with subprotocol `mcp` and two headers, `User-Agent` and `X-Claude-Code-Ide-Authorization`. The token comes from the lock file, so every Claude of this Neovim sends the same one. - `initialize` carries `clientInfo` `{ name: "claude-code", title: "Claude Code", version: "2.1.312", description, websiteUrl }` and `capabilities`, the same for every process and with no session id. - After the connection succeeds, it sends the notification `ide_connected` with `{ pid: process.pid }`. The plugin has no handler for it. A pid is not unique across pid namespaces (two Claude CLIs in two containers can both report the same pid), and the OS reuses it. Matching on it could hand one live Claude's accepted diff to another, which is the harm coder#361 fixed. - Each `openDiff` gets the `tab_name` `✻ [Claude Code] <basename> (<first 6 chars of a random UUID>) ⧉`, so a new connection does not reuse an old name. - The CLI keeps the IDE client object from the moment it calls `openDiff` and sends that diff's `close_tab` through it. After a reconnect, that `close_tab` fails on the old connection. The new connection never names the old diff. So matching a reconnect to its old id would change nothing for this CLI. The leftover diff is the same as the accepted diff of a Claude that exited (coder#363 item 1). coder#376 lets `:ClaudeCodeCloseAllDiffs` finish such a diff. </details> ## Scope coder#363 items 2 and 3 only. Item 2 changes only docs. This PR does not touch `diff.lua`, which coder#376 changes. ## Blast Radius - Users who reload `claudecode.server.*` in a running Neovim and then restart the server. A new Claude can no longer take over an accepted diff of a Claude that exited. Nobody else sees a change: ids keep their form and still count up from 1 in a fresh Neovim. - One new Lua global, `_G.claudecode_clients_created`. - `covered.lock` will conflict with coder#376, because both PRs re-record it. Whichever PR merges second must re-run `check.sh --record` after the merge. ## Verification | Run | Code | Result | | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `busted tests/unit/server/client_id_spec.lua` (red) | 3919af6, the test on main | `id client_1 came back after a reload` | | Same (green) | this PR | 2 successes. `mise run test`: 839 successes, 0 failures | | Real Neovim 0.12.3 through `verify-claudecode-nvim` (below) | main (96a4774) | the new Claude gets `client_1`, `closeAllDiffTabs` answers `CLOSED_1_DIFF_TABS`, and the accepted text is gone | | Same | this PR (4aa133b) | the new Claude gets `client_2`, `closeAllDiffTabs` answers `CLOSED_0_DIFF_TABS`, and the accepted diff stays | | `verify-diff-lifecycle/check.sh` with the fix and main's fact regex | the fix on 96a4774 | `ClientIdsUnique FALSE`, `main: BROKEN: SavedDiffKept` with the trace `ClaudeOpensDiff, UserHandlerResolves, SetupEnds, ClientDisconnects, ClientIdReused, ClaudeOpensDiff` | | `check.sh` | this PR (7fb8f7b, again on 9ec4014) | `PASS`. `main: pass (89205 distinct states found)`, `live: pass (5269 distinct states found)` | | `check.sh --mutants` | this PR | every guard is caught. `ClientIdsUnique=FALSE caught by: SavedDiffKept` | | `check.sh` with one extra assignment of the counter: `_G.claudecode_clients_created=0`, `_G["claudecode_clients_created"] = 0`, or `rawset(_G, "claudecode_clients_created", 0)` | 9ec4014 | each: `ClientIdsUnique FALSE`, `main: BROKEN: SavedDiffKept` | The real-Neovim script: Claude calls `openDiff`, the user saves it with `:w`, and Claude gets `FILE_SAVED`. Claude exits before it writes the file. The user types `:lua` to clear `claudecode.server.*` from `package.loaded`, then `:ClaudeCodeStop` and `:ClaudeCodeStart` on the same port. A second Claude connects and calls `closeAllDiffTabs`. <details> <summary>Output on main</summary> ``` 1. launch client ids: ["client_1"] 2. user :w -> openDiff answer: ["FILE_SAVED","alpha\nBETA accepted\ngamma\n"] 3. Claude exited before writing the file. diffs (owner status): {"✻ [Claude Code] hello.txt (vcn) ⧉":"client_1 saved"} proposed buffer exists: 1 4. reloaded claudecode.server.* and restarted on port 39032 5. new client b ids: ["client_1"] 6. b closeAllDiffTabs answer: "CLOSED_1_DIFF_TABS" 7. diffs (owner status): [] proposed buffer exists: 0 8. hello.txt on disk: alpha|beta|gamma| ``` </details> <details> <summary>Output on this PR</summary> ``` 1. launch client ids: ["client_1"] 2. user :w -> openDiff answer: ["FILE_SAVED","alpha\nBETA accepted\ngamma\n"] 3. Claude exited before writing the file. diffs (owner status): {"✻ [Claude Code] hello.txt (vcn) ⧉":"client_1 saved"} proposed buffer exists: 1 4. reloaded claudecode.server.* and restarted on port 44871 5. new client b ids: ["client_2"] 6. b closeAllDiffTabs answer: "CLOSED_0_DIFF_TABS" 7. diffs (owner status): {"✻ [Claude Code] hello.txt (vcn) ⧉":"client_1 saved"} proposed buffer exists: 1 8. hello.txt on disk: alpha|beta|gamma| ``` </details> `covered.lock` on main was already stale for `selection.lua`, `tools/open_file.lua`, `utils.lua` and the `send_message` part of `server/client.lua` (coder#367, coder#375, coder#378). None of those changes can reach diff state, an openDiff answer, a disconnect or a diff command, so the new record covers them too. Part of coder#363 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…id bulk close (coder#389) ## Why This PR is one of the small, reviewed fixes that close the open follow-up issues in claudecode.nvim (coder#362, coder#363). It covers coder#363 item 6. A bulk close (`:ClaudeCodeCloseAllDiffs`, a client disconnect, a server stop) first lists its diffs, then closes each one, and each close fires `ClaudeCodeDiffClosed`. Since coder#361 the loop checks each entry against its filter again right before it closes it, so a diff that a handler accepted mid-loop stays open with the user's edits. That same check also skipped a diff that a handler *rejected* mid-loop, because its status was no longer `pending`. The rejected diff stayed open, with its windows and its record. Before coder#361 the loop closed it. No edits were lost, but the user had to close the leftover diff by hand. ## What changed - `close_active_diffs` now also closes a listed entry whose status is `rejected`, when `active_diffs` still holds the same entry it listed. Such a diff holds no edits and has already had its one answer (`DIFF_REJECTED`), so the close only cleans up. A diff that a handler accepted still stays open. - The diff-lifecycle model gets the fact `CloseTakesRejected`, a ghost flag, and the rule `NoRejectedLeftover`: a bulk close leaves open no diff from its list that a `ClaudeCodeDiffClosed` handler denied while the loop ran. `check.sh` reads the fact from the new recheck line, and its `CloseRechecksFilter` regex accepts both the old and the new shape of the line. SKILL.md lists the new rule and fact. `covered.lock` is re-recorded. - `lua/claudecode/README.md` states the recheck. ## Scope - Only diffs on the bulk close's own list are affected. A diff that a handler rejects but that was never on the list (for example, another client's diff during a disconnect cleanup) is not closed, which matches main before coder#361. - coder#361's guarantees are unchanged: a diff that a handler accepted mid-loop stays open, and another client's accepted diff still stays open through `close_tab` and `closeAllDiffTabs`. The facts `CloseRechecksFilter`, `CloseKeepsOthersSaved`, and `ReplaceKeepsOthersSaved` stay TRUE. - A diff that a handler opens under a listed `tab_name` is a new entry. The bulk close treats it like any other entry and does not take it for the listed one (review round 1). The model does not model a handler that opens diffs. The unit tests cover it. - Commits: `91d6618` and `551c584` (the failing cases, then a fix to how they count answers), `11e30f0` (fix, model, docs), `f5235d0` (`covered.lock`). Review round 1: `755ea54` (failing case) and `b57dd32` (fix), `a88aef8` (`covered.lock`). ## Blast Radius Only the loop in `close_active_diffs` changes, and only for a listed diff whose status became `rejected` during the loop. Such a diff now closes, and the bulk close counts it. For example, `:ClaudeCodeCloseAllDiffs` reports `Closed 2 pending Claude diff(s)` instead of `Closed 1`. No call gets a second answer. ## Verification Real Neovim 0.12.3 with `vcn.sh`: Claude opens two diffs (`hello.txt` and `other.txt`). The user's `ClaudeCodeDiffClosed` handler, loaded with `:luafile`, runs `:ClaudeCodeDiffDeny` in the other diff's window on the first close. Then the user runs `:ClaudeCodeCloseAllDiffs`. | | main (`17f53b3`) | this branch | | --- | --- | --- | | message | `Closed 1 pending Claude diff(s)` | `Closed 2 pending Claude diff(s)` | | diffs left | 1 (`other.txt`, status `rejected`), its tab still open | 0, one tab | | answers | `DIFF_REJECTED` once for each call | `DIFF_REJECTED` once for each call | Main, after `:ClaudeCodeCloseAllDiffs`:  This branch:  With a handler that accepts the other diff instead, the pending pass keeps it, and `:ClaudeCodeCloseAllDiffs` then hands its text to the file's buffer (coder#376). No edits are lost. Tests and model checks: - `tests/unit/diff_bulk_close_reentrancy_spec.lua`: the new cases for `close_pending_diffs` and `close_diffs_for_client` fail on main ("the rejected diff must close too") and pass here. They also count every call to the response sender, so a second answer would fail them. The cases where the handler accepts the other diff still pass. A further case has a handler replace a listed diff with one for another client and reject it. It fails on `f5235d0` and passes here. - `mise run check` passes. `mise run test` gives 902 passing and 1 pending. `scripts/run_integration_tests_individually.sh` passes. stylua and prettier pass. - `check.sh` on the code before the fix reads `CloseTakesRejected` as FALSE and breaks `NoRejectedLeftover`. The trace matches the real run: two diffs open, then `UserClosesAllDiffs`. On this branch, `check.sh --mutants` catches every guard (`CloseTakesRejected=FALSE` breaks `NoRejectedLeftover`, and `CloseRechecksFilter=FALSE` still breaks `SavedDiffKept`), and `check.sh` passes after `--record` (89,205 states, liveness 5,269 states). Part of coder#363 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Why
The goal of issue #342 is to close two gaps in the
verify-diff-lifecycleformal check. Modeling them showed two real bugs. In both, the plugin tore down a diff the user had accepted with:w, while the accepted edits lived only in the proposed buffer.ClaudeCodeDiffClosedhandler that accepts another diff during a bulk close.:ClaudeCodeCloseAllDiffs, server stop, and the disconnect cleanup then closed that diff anyway. After a disconnect, no copy of the edits was left.closeAllDiffTabs,close_tab, oropenDiffon the same tab name tore down a diff that another client owned. If that client had exited before it wrote the file, the edits were lost. The Claude CLI callscloseAllDiffTabsat the start of every turn, so a restarted or second session hits this.What changed
close_active_diffsruns its filter again on each entry right before it closes it.close_diff_by_tab_nametakes the calling client and keeps a saved diff that belongs to another client.handle_invokenow passes the client to every tool, not only to the blocking ones. A replacingopenDifffrom another client fails with-32000. Client ids now come from a counter, so a new connection never takes over a disconnected client's id.ClaudeCodeDiffClosedhandler in every run. Three new facts read the guards.verify-claudecode-nvimcan drive a second client (VCN_CLIENT=b $V connect).Scope
opened-handlerknown finding (bug(diff): a ClaudeCodeDiffOpened handler that accepts or denies the diff leaves openDiff unanswered forever #325).closeAllDiffTabs. Another client's pending diffs are still rejected by it. That is unchanged behavior, and it loses no accepted edits.Verification
closeAllDiffTabsansweredCLOSED_0_DIFF_TABS.diff_bulk_close_reentrancy_spec.luaanddiff_client_ownership_spec.luafail before the fix and pass after it.mise run testpasses 783 tests.check.sh --mutantscatches every fact, and every rule breaks at least once.check.shpasses on this head (main run: 89,205 states).Fixes #342
Generated with
xum• Model:anthropic:claude-opus-5-5• Thinking:xhigh