Repository navigation
fix(server): join fragmented messages, check UTF-8 per message, refuse binary - #395
Conversation
RFC 6455 requires a server to accept a text message split into fragments, and only the whole message must be valid UTF-8. The plugin checks each frame: a valid message whose fragments split a code point gets 1007, and the first fragment of any other is dispatched alone before the next one closes the connection with 1003. A binary frame skips the UTF-8 check and runs as JSON-RPC. The new message_spec drives process_data. Nine of its cases fail on main. The parse_frame text-frame UTF-8 cases move there as message cases, because the check belongs to the message. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…e binary process_data now keeps the fragments of a text message on the client and calls on_message once with the whole text. Control frames can arrive between fragments. The UTF-8 check moves from parse_frame to the joined message, so a fragment may end inside a code point and an ill-formed continuation frame closes with 1007. A binary message closes with 1003 (Unsupported data) and nothing runs it: JSON-RPC is text, and Claude Code sends only text frames. A continuation with no message, or a text frame inside a fragmented message, closes with 1002. A message larger than frame.MAX_PAYLOAD_LENGTH (100 MiB, the limit one frame already had) closes with 1009. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Only server/client.lua and server/frame.lua changed, with no covered block or call site. A joined message reaches the diffs as one tools/call, the same as an unfragmented one, and a refused frame closes the connection through the same on_disconnect path as any other close. The model already has both, and check.sh passes. Part of #393 _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: 3f15865705
ℹ️ 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. |
Each fragment took one table entry, and the 100 MiB limit counts only payload bytes, so empty continuation frames are free against it. With 100000 empty and 100000 one-byte fragments, memory grows by 2052 KiB for a 100 KiB text. The new case allows 512 KiB. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
add_data_frame now joins each piece onto the one below it while that one is not longer, as a binary counter carries. The client then holds O(log n) strings for n fragments, and an empty fragment adds nothing. In the new case it holds 7 strings and grows by 126 KiB, where it held 200001 entries and grew by 2063 KiB. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Under luacov, the 200000-fragment loop made mise run test take 72 s instead of 15 s. 20000 empty and 20000 one-byte fragments still tell the cases apart: without the join, memory grows by 519 KiB, over the 256 KiB the case allows. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Only server/client.lua changed again, inside add_data_frame, which the model does not cover. check.sh passes. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
🛡️ 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. |
Takes main's covered.lock; the next commit re-records it. _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
main's record (#394) now covers diff.lua. Only server/client.lua and server/frame.lua differ from it, as in the earlier records on this branch, and check.sh passes. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
|
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". |
🛡️ 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. |
# Conflicts: # .agents/skills/verify-diff-lifecycle/covered.lock
Besides server/client.lua and server/frame.lua, the record takes in the selection.lua, tools/open_file.lua and visual_commands.lua changes from #396 and #397, which main had not recorded. None of them reaches diff state, an openDiff answer, a disconnect or a diff command: the only diff word in them is open_file's check of a window's 'diff' option. Part of #393 _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
|
@codex review |
|
@codex security review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 41964a6a62
ℹ️ 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. |
… and #400 Against main's record, only the put_accepted_text_in_buffer block and the diff.lua file hash change. All 21 facts read TRUE and the model is unchanged. _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
The next claudecode.nvim release. Merging this PR creates the release
tag and GitHub Release, then starts Communique GitHub Release note
generation.
---
## [0.4.0] - 2026-11-09
### Added
- `:ClaudeCodeSendText {text}` command and
`require("claudecode.terminal").send_to_terminal(text, opts)` to send
arbitrary text to the open Claude terminal as if typed at the prompt
(submitting by default; `:ClaudeCodeSendText!` inserts without
submitting). Multi-line text is sent via bracketed paste. Works with the
in-editor `native`/`snacks` providers only.
([coder#197](coder#197),
[coder#272](coder#272))
- `:checkhealth claudecode` health check that verifies Neovim version,
`setup()`, the Claude CLI, terminal provider, WebSocket server, lock
file, and live connection state.
([coder#275](coder#275))
- `:ClaudeCodeCloseAllDiffs` command to close pending Claude diffs at
once. A diff accepted with `:w` closes once the file holds the accepted
text; otherwise the accepted text is put into the buffer as unsaved
changes for `:w`/`:e!` so edits are never discarded.
([coder#248](coder#248),
[coder#261](coder#261))
- `diff_opts.layout = "unified"`: a VS Code-style unified diff rendered
in a single buffer with interleaved deleted/added lines, as a compact
alternative to `"vertical"`/`"horizontal"`. Documented to require Neovim
>= 0.9.0. Customizable via the
`ClaudeCodeInlineDiffAdd`/`Delete`/`AddSign`/`DeleteSign` highlight
groups. ([coder#195](coder#195),
[coder#295](coder#295),
[coder#297](coder#297))
- `User ClaudeCodeSendComplete` autocmd, fired once per file when a send
is accepted while Claude is connected, with `data = { file_path,
start_line, end_line, context }` (lines 0-indexed). Enables post-send
logic such as focusing a Claude session running outside Neovim.
([coder#228](coder#228),
[coder#265](coder#265))
- `User ClaudeCodeDiffOpened` / `ClaudeCodeDiffClosed` autocmds carrying
diff metadata, plus `diff_opts.auto_resize_terminal` (default `true`) to
opt out of plugin-managed terminal width during diffs and
`terminal.diff_split_width_percentage` to set a distinct terminal width
while a diff is open.
([coder#270](coder#270))
- `terminal.auto_insert` option (default `true`): set to `false` to stay
in Normal mode when refocusing the Claude terminal and preserve your
scroll position instead of jumping to the prompt.
([coder#233](coder#233))
- `ClaudeCodeTreeAdd` / `<leader>as` support in snacks.nvim pickers
(`snacks_picker_list`), covering `Snacks.explorer()` and modal
`Snacks.picker.files()`/`grep()`.
([coder#192](coder#192),
[coder#269](coder#269))
- netrw file selection for `<leader>as` / `:ClaudeCodeTreeAdd`,
supporting marked files and the file under the cursor.
([coder#62](coder#62))
### Changed
- Model picker labels are now version-free evergreen aliases (`Claude
Opus (Latest)`, `Claude Sonnet (Latest)`, `Claude Haiku (Latest)`), with
new 1M-context variants (`opus[1m]`, `sonnet[1m]`) and a `Default
(account recommended)` entry.
([coder#146](coder#146),
[coder#256](coder#256))
- `:checkhealth claudecode` and the MCP `serverInfo.version` now report
the real plugin version from the `VERSION` file instead of a hard-coded
value. ([coder#437](coder#437))
### Fixed
- The Snacks provider no longer fails to launch Claude when a model
alias contains shell-glob characters (e.g. `opus[1m]`); the command is
now passed as an argv list with no shell interpretation.
([coder#256](coder#256))
- The native terminal now adopts only terminal buffers it started
(marked with `b:claudecode_native_terminal`), instead of any visible
terminal whose name contains `claude`. Upgrade note: a terminal that an
older plugin version started (for example after `:Lazy reload` following
an upgrade), or that a session file restored, carries no mark and is no
longer adopted. The next open starts a second Claude session, and the
old terminal keeps running until you close its buffer or quit Claude in
it. ([coder#349](coder#349),
[coder#352](coder#352),
[coder#364](coder#364))
- `closeAllDiffTabs` is now scoped to claudecode's own tracked diffs and
no longer wipes out an open diffview.nvim, fugitive, or native
`:diffsplit` review; `openFile`/`openDiff` also never reuse a window
that is in diff mode.
([coder#277](coder#277),
[coder#290](coder#290))
- `close_tab` now closes only the diff with that tab name and never
deletes a user buffer.
([coder#373](coder#373))
- Closing the native terminal, for example with `:ClaudeCodeClose`, now
stops the Claude job instead of leaving it running.
([coder#336](coder#336))
- The Claude terminal now adds loopback hosts to `no_proxy`/`NO_PROXY`
so a configured proxy no longer tunnels Claude's `ws://127.0.0.1` IDE
connection and times out queued @ mentions.
([coder#70](coder#70),
[coder#268](coder#268))
- `focus_after_send = true` with `provider = "none"`/`"external"` now
emits a one-time warning pointing to the `User ClaudeCodeSendComplete`
autocmd instead of failing silently.
([coder#228](coder#228),
[coder#265](coder#265))
- Rejecting a Claude diff with `:q` / `:close` / `<C-w>c` (or closing
the tab) now resolves it as rejected via a `WinClosed` autocmd.
([coder#266](coder#266))
- Quickly-made and single-line visual selections are now pushed to
Claude reliably; selections are flushed synchronously on visual-mode
exit and persist until the cursor moves, including in the external
`/ide` flow and Select mode.
([coder#246](coder#246),
[coder#267](coder#267),
[coder#367](coder#367),
[coder#381](coder#381),
[coder#388](coder#388))
- Pending diffs are now automatically closed when the client that opened
them disconnects or the integration stops, so orphaned proposals no
longer linger.
([coder#248](coder#248),
[coder#261](coder#261))
- `openDiff` now works when the Claude terminal is the only window,
creating a split to host the diff instead of failing with "No suitable
editor window found".
([coder#260](coder#260))
- Fixed the "climbing cursor" in the Snacks terminal, where the cursor
sat one row above Claude's prompt after hide and show. On Neovim >= 0.10
a floating terminal is now hidden instead of closed. A split is still
closed on hide and created again on show, which keeps the cursor in
place. Floating terminals on Neovim < 0.10 keep the old behavior.
([coder#240](coder#240),
[coder#183](coder#183),
[coder#271](coder#271))
- Worked around a Neovim core bug (< 0.12.2) that fragmented large
bracketed pastes into the terminal; controlled by
`terminal.fix_streamed_paste` (`"auto"` by default).
([coder#252](coder#252))
- `openFile` now selects exactly the requested line range or matched
text, keeps the user's Visual mode, unsaved changes, focus and preview
window intact, never prompts W11/W12, and never picks the preview window
as the main editor window.
([coder#353](coder#353),
[coder#365](coder#365),
[coder#370](coder#370),
[coder#375](coder#375),
[coder#382](coder#382),
[coder#384](coder#384),
[coder#387](coder#387),
[coder#396](coder#396),
[coder#408](coder#408))
- `getDiagnostics` now handles plain file paths without a `file://` URI
scheme, and IDE diagnostics return grouped URI-based payloads with
editor-native ranges and severity names.
([coder#163](coder#163),
[coder#274](coder#274))
- `:ClaudeCodeAdd` and `openFile` now handle `$` in file paths.
([coder#286](coder#286))
- Prevented a segfault when accepting a new-file diff with
`render-markdown.nvim` installed, by turning off diff mode before the
post-write redraw.
([coder#224](coder#224))
- The auth token is now generated from a CSPRNG and the lock file is
written atomically with mode `0600` in a `0700` directory.
([coder#259](coder#259))
- Detect system sleep/wake and reset client keepalive timestamps to
prevent false disconnections.
([coder#141](coder#141))
- The WebSocket server now retries port binding on `EADDRINUSE`
(parallel instances), closes connections on malformed/oversized or
binary frames with the correct status, joins fragmented messages,
validates UTF-8 per message, and never repeats a client id within a
process. ([coder#258](coder#258),
[coder#283](coder#283),
[coder#378](coder#378),
[coder#383](coder#383),
[coder#390](coder#390),
[coder#395](coder#395),
[coder#405](coder#405),
[coder#422](coder#422),
[coder#431](coder#431))
- Legacy `diff_opts` (e.g. `vertical_split`, `open_in_current_tab`) now
take effect again after the config merge-order regression.
([coder#142](coder#142))
- `:q` on a diff window no longer hangs; floating diff terminals keep
focus; empty buffers are wiped when the terminal provider is `none`; and
accepted diffs are handed over in the buffer's own line-ending format.
([coder#178](coder#178),
[coder#223](coder#223),
[coder#266](coder#266),
[coder#392](coder#392))
### Compatibility
- Integration tests now run on Neovim v0.8.0, and a new-tab diff no
longer fails there because Neovim 0.8 has no `statuscolumn`. The unified
layout is documented to require Neovim >= 0.9.0, and the v0.8.0
integration tests skip it.
([coder#369](coder#369),
[coder#394](coder#394))
---
Maintained by the release-please workflow. CHANGELOG.md and the release
PR body are regenerated by Communique on every push to main, so manual
edits to this PR are overwritten.
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Thomas Kosiewski <tk@coder.com>
…-ordered tests (coder#400) ## Why The operator goal is to clear the open follow-up issues in this repository (coder#393 now) with small reviewed PRs. This PR takes coder#393 item 7. Item 6 is in coder#395. 1. **New dirs above `VERIFY_OUT` were refused under a default ACL.** Since coder#386, `vcn.sh launch` created the evidence path with `mkdir -p -m 700`, which sets only the last dir's mode. Under a parent with a default ACL, each new dir above it got the ACL's mode (`770`), and the check after `mkdir` refused the run. This hit a `VERIFY_OUT` more than one level below such a dir. It also hit a new default parent dir (`verify-claudecode-nvim`) under such an `XUM_SCRATCH_DIR`. I fixed it instead of documenting it, because the fix is one loop. 2. **Nothing stopped a spec from registering tests in `pairs()` order.** LuaJIT seeds its string hash in each process, so `pairs()` visits string keys in a different order in each run. A spec that registers tests from such a loop runs them in a different order each time, and the seed that `mise run test` prints does not repeat that order. coder#386 fixed three such loops. A fourth one came in with coder#388 (`visual_commands_select_mode_spec.lua`): in 12 runs, its three tests came in three different orders. ## What changed - `vcn.sh`: launch runs `mkdir -m 700` on each missing dir of the evidence path, shallowest first. This replaces the `mkdir -p -m 700` and its `shellcheck disable`. Two other changes come with it: - If another launch makes a missing parent dir first, launch uses that dir, and the check after `mkdir` decides whether it is safe. Before, `mkdir -p` accepted such a dir. - Launch now keeps a list of only the dirs it made itself. On a failure or a refusal, it removes those dirs and never one that another process made. - `proof-launch-refusals.sh`: - `out-made` puts `VERIFY_OUT` two levels below the default-ACL dir and also checks the middle dir's mode. - `parent-made-unsafe` expected the old refusal of a new default parent dir, so I removed it. That dir now goes through the same code as `out-made`'s middle dir. - `race` (new) puts a `mkdir` shim on `PATH`. The shim plays another launch that makes the dir above `VERIFY_OUT` first, group-writable. Launch must refuse that dir in the check after `mkdir` and leave it in place. - `SKILL.md`: the creation mode and the ACL cases. - `tests/helpers/test_order.lua` (new) is a busted helper, and the new `.busted` file loads it for every busted run, `mise run test` included. When a spec registers a test (`describe`, `it` or `pending`), the helper walks the stack. In each frame of a file in this repository's `tests/` dir, it looks for a generic `for` loop whose iterator is `next`, which is what `pairs()` returns. If it finds one, the registration fails with `<file>:<line> registers "<name>" inside a pairs() loop; loop over an array with ipairs`. Because it checks at run time, it does not depend on formatting, comments, one-line loops or strings. - `tests/unit/test_order_helper_spec.lua` (new) tests the helper. It covers a loop in the spec, a loop in a calling frame, a loop in code outside the `tests/` dir, an `ipairs` loop, and a registration after a loop. - `tests/unit/visual_commands_select_mode_spec.lua`: the three cases come from an array now. - `DEVELOPMENT.md`, `AGENTS.md` and `README.md` (System Verification): the rule and the guard. Codex round 1 found two holes in my first version, a text scan (`scripts/check_test_order.sh`). Round 2 found two more. So I replaced the scan with the run-time check, and the script is gone. Round 3 found the remaining two issues: the helper counted any path with a `tests/` part, and launch aborted when it lost the race for a parent dir. Both are fixed. ## Scope coder#393 item 7. No plugin code changes. ## Blast Radius - **People and agents who run `vcn.sh launch` under a dir with a default ACL.** A deeper `VERIFY_OUT`, or a new default parent dir, now works and starts private, where launch refused it before. - **Contributors.** A spec that registers tests inside a `pairs()` loop now errors in `mise run test`, and in any `busted` run from the repo root. The error names the file and line. - **Run time.** The helper adds about 1 s to the suite under coverage. In two pairs of runs it took 15.8 s against 14.6 s, and 16.9 s against 15.7 s. ## Verification | Run | Code | Result | | ------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `VERIFY_OUT=<0700 dir with default ACL g::rwx>/mid/deeper/out vcn.sh launch` | main (c05a629) | exit 1: `…/mid/deeper, above the evidence dir, is writable by others and not sticky`. The dirs it made are removed | | Same | this PR | launch works. After cleanup, `mid`, `mid/deeper` and `out` are `700`, and every evidence file is `0600` | | `XUM_SCRATCH_DIR=<0700 dir with that ACL> vcn.sh launch` (new default parent dir) | main | exit 1: `…/verify-claudecode-nvim is group- or world-writable` | | Same | this PR | launch works, and `verify-claudecode-nvim` is `700` | | `VERIFY_OUT=<dir>/a/<300-character name>` | this PR | `cannot create …`. The proof dir is empty afterwards, so the `a` that launch made is removed again | | `proof-launch-refusals.sh` (red) | 9575ee0, the new `out-made` on main's `vcn.sh` | `FAIL: out-made: launch failed: vcn: refusing evidence dir: …/acl/mid, above the evidence dir, is writable by others and not sticky` | | `race` case alone (red, Codex round 3 finding) | 3392cd6, the case on b5c7ec4's `vcn.sh` | exit 1: `cannot create …/race/mid/out` | | `race` case alone | this PR | exit 1: `…/race/mid, above the evidence dir, is writable by others and not sticky`. `mid` stays, and `mid/out` is gone. With a shim that makes `mid` `0700`, launch starts the run | | `proof-launch-refusals.sh` (green) | this PR (882da0f) | all 18 cases `PASS` in 78 s on a busy host (56 s before the `race` case, which starts no run), with an empty `TMPDIR` afterwards | | The whole suite with the helper | the tree of bf80d11 (main before coder#386) | 3 errors: `native_terminal_toggle_spec.lua:523` and `:969` and `diff_bulk_close_reentrancy_spec.lua:71`, the three loops coder#386 fixed | | `busted tests/unit/visual_commands_select_mode_spec.lua` with the helper | main's spec (c05a629) | error: `visual_commands_select_mode_spec.lua:24 registers "runs the visual handler for a charwise Select-mode selection" inside a pairs() loop; loop over an array with ipairs` | | Same | this PR | 3 successes | | 12 runs of that spec, test order | main / this PR | 3 different orders / 1 order | | `tests/unit/test_order_helper_spec.lua` with each of two helper mutants (never match `next`, never match a `tests/` frame) | this PR | each: 1 success, 2 failures | | Checkout and rocks copied under `<scratch>/tests/claudecode.nvim`, `busted tests/unit/config_spec.lua` (Codex round 3 finding) | d49b31e / this PR | 1 error / 16 successes | | `mise run check`, `mise run test` | this PR (5df456a, with main up to coder#396 merged) | pass. 967 successes, 0 failures | Gates on 5df456a: `stylua --check`, `prettier --check`, `shellcheck`, `shfmt -d` and `treefmt --ci` pass. This PR changes no file under `lua/` or `plugin/`. On 5df456a, `verify-diff-lifecycle/check.sh` reads STALE only for main's own unrecorded `selection.lua`, `tools/open_file.lua` and `visual_commands.lua` changes (coder#396, coder#397). coder#395 re-records them. Part of coder#393 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…der#398) Part of coder#393 (item 3). ## Why The goal is to close the open follow-up issues in this repo, which list bugs found while verifying earlier fixes. This PR covers item 3 of issue 393: the messages and line endings of the accepted-diff hand-over from coder#392. When an accepted diff's Claude client has exited, `:ClaudeCodeCloseAllDiffs` puts the accepted text into the file's buffer and tells the user how to write it. 1. A swap file from another Neovim kept the diff with "E325: ATTENTION", with no next step. 2. A read-only buffer got the hint `:w`, which fails with `E45: 'readonly' option is set`. 3. A proposal with both CRLF and LF line breaks, put into a `dos` buffer, was written with `\r\r\n` for its CRLF lines. The README sentence "`:w` writes one CR per line break" did not hold for it. 4. The diff-lifecycle model does not cover a `ClaudeCodeDiffClosed` handler that opens or replaces a diff while a bulk close runs. ## What changed | Sub-item | Decision | Change | | --- | --- | --- | | Swap file (E325) | Fix | The kept-diff reason adds: open it with `:split <file>` (answer the swap file prompt if Neovim shows one), then run the command again. After the accept the user is in the diff's own window. A bare `:e` there reloads the proposal, and `:e <file>` fails with E37 in the unified layout, whose modified buffer cannot be abandoned. `:split <file>` works in every layout. | | Read-only buffer | Fix | The hint is `:checktime, then :w!` when the buffer has `'readonly'`. The `!` skips Neovim's changed-since-reading check, so `:checktime` shows a change on disk first. A read-only buffer in a missing folder keeps the `!` (`:w! ++p`, or `... \| w!` on Neovim 0.8). | | Mixed CRLF/LF text | Fix the code | In a `dos` buffer, `accepted_lines` drops the CR before every line break, not only when all of them are CRLF. `:w` then writes CRLF once per line break, which makes the README sentence true. | | Model gap | Document | `.agents/skills/verify-diff-lifecycle/SKILL.md` now says how this happens, what the code does, and why the model leaves it out. See Scope. | - The vim mock (`tests/mocks/vim.lua`) now leaves a buffer that `bufadd()` created unloaded until `bufload()`, as Neovim does. Without that, the mock never reached the failed hand-over's cleanup. - README.md and `lua/claudecode/README.md` name `:w!` and the swap step. `covered.lock` is re-recorded, because the covered block `put_accepted_text_in_buffer` changed. No fact reads that block. It is recorded on the tree merged with main 0b41634 (after coder#395 and coder#400). Against main's record, only the `sync` line, the `put_in_buffer` block and the `diff.lua` file hash change. ## Scope - The model gap is documented, not modeled. A `ClaudeCodeDiffClosed` handler can open or replace a diff only by running the event loop (for example `vim.wait`), so that a queued openDiff from Claude runs inside the loop. `close_active_diffs` reads each entry again and reruns its filter. So a new entry under a listed `tab_name` closes only when it matches, and a rejected entry closes only when it is the listed one (`tests/unit/diff_bulk_close_reentrancy_spec.lua` checks a replaced entry). Modeling it needs the per-call `reqTab` and `reqClient` inside the record the bulk-close operators pass along, which changes every action in the model. - The swap step is a hint only. The command still never answers a swap prompt for the user. - Sub-items 1 and 2 of coder#393 are in coder#394 and in a separate window-choice PR. ## Blast Radius - Only `:ClaudeCodeCloseAllDiffs` messages, and the hand-over lines in a `dos` buffer, change. - In a `dos` buffer, a text line that itself ends in a CR before its line break loses that CR. Before, the same line was written with `\r\r\n`. Lines with no CR and the last line without a final line break are unchanged. - `unix` buffers are unchanged: a CRLF proposal keeps its CRs there, and `:w` writes the accepted bytes. ## Verification Red first (commit c1380f9): the unit test for the swap message fails, and the real-Neovim spec fails 2 of 9 (mixed endings, read-only hint) on Neovim 0.8.3 and 0.12.3. Codex round 2 findings (named file, `!` kept, `:checktime`) are red first in 7e907fd. The `:split` hint is red first in a138763: the spec runs the hint's own command from the diff window in each layout, and the unified case failed with E37 on 0.9.5 and 0.12.3. With the fixes, `tests/unit/diff_client_ownership_spec.lua` passes 18 of 18, and `tests/integration/diff_hand_over_spec.lua` passes 12 of 12 on 0.9.5 and 0.12.3, and 11 of 11 on 0.8.3 (no unified layout there). Live runs through the `verify-claudecode-nvim` harness (Neovim 0.12.3): Claude opens a diff, the user accepts it, Claude exits, the user runs `:ClaudeCodeCloseAllDiffs`, then does what the message says. | Case | Before | After | | --- | --- | --- | | Swap file from another Neovim | "Kept ... E325: ATTENTION", no step. With the `:e <file>` hint of an earlier head, typing it in the unified diff window gives `E37: No write since last change` | adds the `:split <file>` step. Typed in the diff window, it works in the vertical, new-tab and unified layouts on 0.12.3 (`W325: Ignoring swapfile ...`), and in the vertical and new-tab layouts on 0.8.3 (real swap prompt, answered `(E)dit anyway`). The second run hands the text over | | Read-only file (mode 444) | hint `:w`, which fails with `E45`, file unchanged | hint `:checktime, then :w!`, which writes the accepted text, mode stays 444 | | Dos file, proposal `alpha\r\nBETA accepted\ngamma\r\n` | disk `alpha\r\r\n...gamma\r\r\n` | disk `alpha\r\nBETA accepted\r\ngamma\r\n` | Swap file, before:  Swap file, after:  Read-only file, before (the hint `:w` fails):  Gates on the head: `mise run check` (0 warnings), `mise run test` (980 passed, shuffled), `./scripts/run_integration_tests_individually.sh` (50 passed, on the tree merged with main 0b41634), stylua and prettier clean. `.agents/skills/verify-diff-lifecycle/check.sh` passes. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…linear time, keep client errors off the screen (coder#405) ## Why The operator goal is to clear the open follow-up issues in this repository (coder#401 now) with small reviewed PRs. This PR takes coder#401 item 4, the WebSocket follow-ups from coder#395. Item 6 gets its own PR. 1. **A binary frame inside an open text message closed with 1003.** RFC 6455 (section 5.4) makes a new data frame inside a fragmented message a protocol error, so the status must be 1002. 2. **Closes with 1007 and 1009 sent the reason text "Protocol error".** The client could not tell from the reason which rule its data broke. 3. **`frame.validate_frame` still checked UTF-8 per frame.** Nothing called it, and the server README listed it. 4. **The server copied the rest of its input buffer after each frame.** Many small frames in one read cost quadratic time and memory. 4000 frames in one read allocated 65 MiB (66,408 KiB). 5. **A client's error could stop Neovim at "Press ENTER".** A client's read error or protocol error went to `server.on_error`, which `init.lua` logs at ERROR. The logger shows every line it writes, and that line starts with about 77 characters of prefix. In an 80-column Neovim, one bad frame raised "Press ENTER". Until the user pressed Enter, no other client got an answer. ## What changed - `lua/claudecode/server/client.lua`: - `add_data_frame` checks for an open message first, so a TEXT or BINARY frame inside one closes with 1002. - A `CLOSE_REASONS` table gives each Close frame the RFC 6455 name of its status (section 7.4.1): `Protocol error` (1002), `Unsupported data` (1003), `Invalid frame payload data` (1007) and `Message too big` (1009). - `process_data` parses one read from an offset and keeps the unparsed rest once, at the end. - `lua/claudecode/server/frame.lua`: `parse_frame(data, start?)` parses the frame that starts at byte `start`. I removed `validate_frame`. `is_control_frame` now has no caller, and I kept it. - `lua/claudecode/server/tcp.lua`: a client's read error and protocol error reach the server only as the reason of its `on_disconnect`, which `init.lua` logs at debug level. `on_error` keeps the server's own failures: listen, accept, and creating a client handle or the ping timer. - `lua/claudecode/server/README.md`: these contracts. - Tests: - `tests/unit/server/message_spec.lua` gets a 1002 case, five reason-text cases and a memory case. The memory case pauses the JIT and the coverage line hook while it counts, because both allocate too. - `tests/unit/server/tcp_spec.lua` now expects `on_error` to stay silent for a client's read error and protocol error. - `.agents/skills/verify-diff-lifecycle/covered.lock`: re-recorded. Only the three file hashes changed. A client error still reaches the diffs through `on_disconnect`. ## Scope coder#401 item 4. The outbound side does not change. ## Blast Radius - **Clients that break the protocol.** They get the right status and reason text in the Close frame. - **Users.** A client's read error or protocol error no longer shows as `[ClaudeCode] [server] [ERROR] WebSocket server error: …`. One such error used to block Neovim at "Press ENTER" in a narrow terminal. With `log_level = "debug"`, the reason still shows in the `WebSocket client disconnected: client_N (code: 1006, reason: Client error: …)` line. This also covers `Client read error: ECONNRESET` from a Claude that crashed. - **`frame.validate_frame`** is gone. Nothing in the repository called it. ## Verification | Run | Code | Result | | -------------------------------------------------------------------------------------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `busted tests/unit/server/message_spec.lua tests/unit/server/tcp_spec.lua` (red) | ea2d537 and 98b62a7, the tests on main | 7 failures: 1003 instead of 1002, three reasons `Protocol error`, `allocated 66424 KiB`, and `on_error` called for a read error and for a protocol error | | Same (green) | this PR | all pass. `mise run test`: 987 successes, 0 failures | | N frames in one read through `process_data`, LuaJIT with the JIT off, bytes allocated (GC stopped) | main / this PR | 1000 frames: 4.8 / 0.9 MiB. 4000: 65 / 3.7 MiB. 16000: 992 / 15 MiB, and CPU 0.525 / 0.026 s | | Same with the JIT on, as in Neovim | main / this PR | 1000 frames: 8.0 / 9.6 MiB, CPU 0.010 / 0.014 s. 4000: 128 / 67 MiB, 0.105 / 0.070 s. 16000: 1278 / 300 MiB, 0.797 / 0.300 s. JIT trace data adds 9 to 18 KiB per frame, so the 1000-frame case shows no gain | | Raw WebSocket client in real Neovim 0.12.3 (`verify-claudecode-nvim` harness) | main (182ee38) / this PR | see the table below | | `verify-diff-lifecycle/check.sh` | this PR (4ba97e1) | `PASS`. `main: pass (89205 distinct states found)`, `live: pass (5269 distinct states found)` | Results of the raw-client run: | Frames sent | main | this PR | | -------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | | A binary frame inside an open text message | Close 1003 `Unsupported data` | Close 1002 `Protocol error` | | A text message `FF` | Close 1007 `Protocol error` | Close 1007 `Invalid frame payload data` | | A binary frame header with a 64-bit length of 200 MiB | Close 1009 `Protocol error` | Close 1009 `Message too big` | | A close frame whose reason is `FF` | Close 1007 `Protocol error` | Close 1007 `Invalid frame payload data` | | 20000 / 40000 empty pings in one write, time to the last pong | 670 / 1316 ms | 525 / 1051 ms | | `:set columns=80`. Client A sends `FF` as a text message, then client B sends a valid `tools/call` | B gets no answer in 3 s. The screen shows `[ClaudeCode] [server] [ERROR] WebSocket server error: Client client_8 error: Invalid UTF-8 in text message` and `Press ENTER or type command to continue`. Nvim answers no RPC until Enter | B gets the plugin's reply, the JSON-RPC error `-32001 File not open` for its made-up path. Nvim stays in Normal mode with no message | The ping times improve less than the unit numbers. TCP hands the server a large write in several reads, so each read holds fewer frames, and the 20000 pong writes take most of the time. ## Follow-ups - The server's own `on_error` messages (listen, accept, handle and ping-timer failures) still go to ERROR. A long one can still raise "Press ENTER" in a narrow window. These failures are rare and the user can act on them. - Handshake and authentication failures log at WARN in `client.lua`. A long one has the same risk. - A large frame that arrives in many reads still grows `client.buffer` by concatenation, which is quadratic in that frame's size up to the 100 MiB limit. - The memory case in `message_spec.lua` checks a bound (8 MiB for 4000 frames), not linearity, although its name says "linear". Part of coder#401 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
…e memory tests stop failing at random (coder#418) ## Why This PR closes part of the open follow-up issues in coder/claudecode.nvim: item 1 of coder#416, a memory test that makes CI fail at random. `tests/unit/server/message_spec.lua` "handles many small frames in one read with memory linear in their number" failed in about 1 run in 20. It counted up to 9048 KiB against its limit of 8192 KiB, while the parser allocates about 3800 KiB. The test calls `jit.off()` before it counts. That stops new traces, but traces that earlier tests compiled still run. In some runs they allocated up to 5 MiB more. The amount changed from process to process, even with the same `--seed`. The fix calls `jit.flush()` after `jit.off()`, so only the interpreter runs while the test counts. ## What changed - `tests/unit/server/message_spec.lua`: `jit.flush()` after `jit.off()` in the "many small frames" test, and in the "large frame in many reads" test, which counts the same way. ## Scope - Test code only. No plugin code changes. - The limits stay as they were: 8192 KiB and 32768 KiB. ## Blast Radius - `jit.flush()` drops every compiled trace once per test. Later tests compile them again. The suite takes the same time. ## Verification Allocations that the "many small frames" test counted, one process per run, `busted --shuffle --seed=N tests/unit/server/message_spec.lua`: | Run | Before (main 335232b) | After | | --- | --- | --- | | 60 runs, seeds 1 to 60 (three tries before, one after) | 3782 to 9048 KiB, in steps of 750 KiB | 3782 to 3800 KiB | | 100 runs, seeds 1 to 100 | 5 failed (8282 and 8298 KiB) | 0 failed | | Seed 9, 12 processes | 3798 to 9048 KiB | 3798 KiB in all 12 | The whole suite, one process per run, seeds 1 to N. The coverage runs each had their own `TMPDIR`, because `tests/lockfile_test.lua` writes to `$TMPDIR/claude_test` and parallel loops shared it: | Run | Before (main) | After | | --- | --- | --- | | 100 runs, no coverage | 5 failed, all in this test (9032 KiB) | 0 failed | | `--coverage`, as `mise run test` and CI run it | 0 of 50 failed | 0 of 100 failed | With coverage the old test did not fail here, so the failures show up mostly in runs without coverage, such as `busted` on one spec file. I did not check why. The large-frame test counted 9298 to 9300 KiB in 60 runs both before and after. It gets `jit.flush()` because it counts the same way. The test still fails on the client code that cut the buffer after each frame. With `client.lua` from before coder#405 (fb2ee96) it counts 66392 KiB, and from before coder#395 (0fa2832) 65955 KiB. Both fail against the 8192 KiB limit. With the current client, it passes. A standalone LuaJIT script that runs the same 4000 frames through `process_data`, outside busted, counts 3799 KiB in each of 15 processes. The extra allocations come only from traces that the other tests compiled. Gates on the head: `mise run check` (0 warnings), `mise run test` (1036 successes, 0 failures), `stylua --check`. Part of coder#416 --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `xhigh`_
Why
The operator goal is to clear the open follow-up issues in this repository (#393 now) with small reviewed PRs. This PR takes #393 item 6.
RFC 6455 (section 5.4) says that servers "MUST support receiving both fragmented and unfragmented messages". A fragmented text message is a TEXT frame with FIN clear, then CONTINUATION frames up to one with FIN set, and control frames can come between them. Section 5.6 says that one frame may hold a partial UTF-8 sequence, and that only the whole message must be valid UTF-8.
The plugin checked UTF-8 per frame and did not join fragments:
With this PR, the plugin joins the fragments and checks UTF-8 once, on the whole text. It refuses binary messages with 1003, as the RFC's own example for that status suggests.
What changed
lua/claudecode/server/client.lua: a newadd_data_framekeeps the fragments of the open text message on the client (client.fragments). It returns the whole text once the last fragment arrives, andprocess_datathen callson_messageonce. Pings and pongs between fragments are answered as before. The status codes:The
on_errortexts are short. A longer one wrapped in a 160-column terminal and left Neovim at "Press ENTER", which blocked the next scheduled message in my first run.lua/claudecode/server/frame.lua:parse_frameno longer checks the UTF-8 of a text frame. It still checks the close reason, because control frames are never fragmented. The 100 MiB limit is nowframe.MAX_PAYLOAD_LENGTH, and the message check uses it too.lua/claudecode/server/README.md: the fragmentation invariant (it said "Fragmentation is not supported"), theparse_frameinvariant and the new constant.tests/unit/server/message_spec.lua(new) drivesprocess_data. Theparse_frametext-frame UTF-8 cases from fix(server): reject ill-formed UTF-8 in inbound frames with 1007 #390 move there as message cases.frame_spec.luakeeps the close-reason cases, and a new case checks thatparse_framereturns a text frame that ends inside a code point..agents/skills/verify-diff-lifecycle/covered.lock: re-recorded. Only the file hashes ofclient.luaandframe.luachanged, and no covered block or call site changed. A joined message reaches the diffs as onetools/call, and a refused frame closes the connection through the usualon_disconnectpath. The model already has both.Scope
#393 item 6. Outbound frames do not change: the server still sends each message as one text frame.
Blast Radius
ws.sendwith a JSON string. The MCP TypeScript SDK sends text frames too. I did not find a client that sends binary.Verification
busted tests/unit/server/(red)mise run teston 41964a6: 974 successes, 0 failuresverify-claudecode-nvimharness), eight casesclaude mcp add-json -s user nvim '{"type":"ws",…}', thenclaude mcp get nvimStatus: ✔ Connected, so the CLI'sinitializewent through. The CLI then closed with 1000. This makes no model request, andANTHROPIC_API_KEYwas unsetjourney-opendiff-accept.shPASS opendiff-acceptverify-diff-lifecycle/check.shPASS.main: pass (89205 distinct states found),live: pass (5269 distinct states found)Results of the raw-client run:
tools/callin two fragments, split inside 😀/x😀hi, then the answerED A0 80(a surrogate) in the continuation frametools/callholdingED A0 80File must be open … /x���After both runs, the plugin still had its launch client, and Neovim was in Normal mode.
Part of #393
Generated with
xum• Model:anthropic:claude-opus-5-5• Thinking:xhigh