Skip to content

fix(diff): keep accepted diffs through reentrant bulk closes and other clients - #361

Merged
ThomasK33 merged 12 commits into
mainfrom
model-ownership-reentrancy-issue342
Oct 31, 2026
Merged

ThomasK33 merged 12 commits into
mainfrom
model-ownership-reentrancy-issue342

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Why

The goal of issue #342 is to close two gaps in the verify-diff-lifecycle formal 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.

  1. A ClaudeCodeDiffClosed handler 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.
  2. A second Claude client. Its closeAllDiffTabs, close_tab, or openDiff on 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 calls closeAllDiffTabs at the start of every turn, so a restarted or second session hits this.

What changed

  • close_active_diffs runs its filter again on each entry right before it closes it.
  • close_diff_by_tab_name takes the calling client and keeps a saved diff that belongs to another client. handle_invoke now passes the client to every tool, not only to the blocking ones. A replacing openDiff from another client fails with -32000. Client ids now come from a counter, so a new connection never takes over a disconnected client's id.
  • The model gives each Claude close a calling client and assumes a ClaudeCodeDiffClosed handler in every run. Three new facts read the guards. verify-claudecode-nvim can drive a second client (VCN_CLIENT=b $V connect).

Scope

  • This PR does not touch the opened-handler known finding (bug(diff): a ClaudeCodeDiffOpened handler that accepts or denies the diff leaves openDiff unanswered forever #325).
  • A client's own saved diffs still close on its own closeAllDiffTabs. Another client's pending diffs are still rejected by it. That is unchanged behavior, and it loses no accepted edits.
  • A saved diff whose client is gone now stays open, and no other Claude client can close or replace it. Its edits stay in the proposed buffer, and only the user can save them elsewhere or close the tab. Main already leaves such a diff open after a disconnect. A way to finish it is a follow-up.

Verification

  • Real Neovim 0.12.3, two WebSocket clients: before the fix, every replay path lost the accepted text. After the fix, the diff stays saved with its text. The screenshot shows client a's accepted diff still open after client b's closeAllDiffTabs answered CLOSED_0_DIFF_TABS.
  • New busted specs diff_bulk_close_reentrancy_spec.lua and diff_client_ownership_spec.lua fail before the fix and pass after it. mise run test passes 783 tests.
  • check.sh --mutants catches every fact, and every rule breaks at least once. check.sh passes on this head (main run: 89,205 states).

Client a's accepted diff stays open after client b's closeAllDiffTabs

Fixes #342


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: xhigh

…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`_
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T22:34:13.794936Z af5c123 Manual request
🔒 Security Review ✅ Completed 2026-10-31T22:33:23.486169Z af5c123 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread lua/claudecode/diff.lua Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 76f124ff27

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

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`_
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: af5c12354d

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: af5c12354d

ℹ️ 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".

@ThomasK33

Copy link
Copy Markdown
Member Author

PASS+NOTES
Verified head af5c123 (merges cleanly with main 5903349).

  • Gates pass. mise run check reports 0 warnings and mise run test reports 783 successes. Each new spec fails with only its matching lua/ files taken from main: reentrancy 0/2, ownership 2/5 (the two passing tests guard the unchanged own-client close), client id 0/1.
  • check.sh reports PASS on the head (main 89,205 states, live 5,269). With only the close_active_diffs recheck reverted, it exits 1: CloseRechecksFilter FALSE and main: BROKEN: SavedDiffKept.
  • I drove a live Neovim 0.12.3 session by hand with vcn.sh. Gap 2: client_2 opened two diffs, my ClaudeCodeDiffClosed handler ran :ClaudeCodeDiffAccept on one, and client_2 disconnected. The accepted diff stayed saved and its text stayed in the buffer. Gap 1: client_3 then got CLOSED_0_DIFF_TABS from closeAllDiffTabs, its close_tab left the diff open, and its openDiff on the same tab name got -32000 Diff tab in use. The owner's gap1.sh closeall and gap2.sh disconnect replays print PASS on the head and FAIL with main's lua/, so both bugs are real on main.
  • The single-client paths still work. An openDiff accepted with :w blocked until the user acted, then answered FILE_SAVED exactly once. The owner's closeAllDiffTabs then answered CLOSED_1_DIFF_TABS, and the owner's close_tab on a pending diff answered DIFF_REJECTED once. Client ids are now client_1, client_2, and so on. Every reader treats client.id as an opaque string: server.clients keys, the disconnect cleanup in server/init.lua, and log lines. Lock files never contain the id.
  • Note (follow-up, no tracking issue yet): a saved diff whose owner is gone now stays until Neovim exits. :ClaudeCodeCloseAllDiffs reports No pending Claude diffs to close. On main it also skips saved diffs. :tabclose hides the windows, but the active_diffs entry and the hidden proposed buffer stay. Every later openDiff on that tab name fails. On main, the next client's closeAllDiffTabs removed the entry and lost the edits, so this trade is the right one. The user still needs a way to finish such a diff.
  • Note: close_tab answers TAB_CLOSED even when it keeps another client's diff. This matches the existing not-found answer, and PROTOCOL.md documents it.
  • Note: the recheck also skips a diff that a handler rejected during a bulk close. That diff now stays open, where main closed it. Main already leaves rejected diffs open after a disconnect, and no edits are lost.
  • Guess, not verified: a Claude CLI that reconnects on a new socket gets a new id. Its own saved diffs from the old socket then count as another client's and stay open.

Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

@ThomasK33
ThomasK33 added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit 3cdaba9 Oct 31, 2026
6 checks passed
@ThomasK33
ThomasK33 deleted the model-ownership-reentrancy-issue342 branch October 31, 2026 22:51
@github-actions github-actions Bot mentioned this pull request Oct 31, 2026
ThomasK33 added a commit to joshmedeski/claudecode.nvim that referenced this pull request Nov 10, 2026
…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`_
ThomasK33 added a commit to joshmedeski/claudecode.nvim that referenced this pull request Nov 10, 2026
…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`:

![main: the diff the handler denied stays
open](https://github.com/user-attachments/assets/401cb363-075a-4fad-8154-dbd9dbc0f43b)

This branch:

![this branch: both diffs are
closed](https://github.com/user-attachments/assets/95f07bab-e121-4fa9-865b-d5d67b7abde9)

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`_
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.

skills(verify-diff-lifecycle): model client ownership and ClaudeCodeDiffClosed reentrancy

1 participant