diff --git a/.agents/skills/verify-selection-tracking/SKILL.md b/.agents/skills/verify-selection-tracking/SKILL.md index b0c5c58f..0d203f6e 100644 --- a/.agents/skills/verify-selection-tracking/SKILL.md +++ b/.agents/skills/verify-selection-tracking/SKILL.md @@ -97,6 +97,8 @@ Not covered, so a change there shows nothing: `lua/claudecode/utils.lua` `select | `VisualCheckSparesPluginEdit` | `note_visual_edit` schedules a check with `vim.schedule` that takes the edit back and sets `consumed` only when visual mode is over: a plugin's edit leaves the selection on the screen, an operator ends visual mode | | `FlushIgnoresTick` | neither `on_mode_changed` nor the flush reads a changedtick (comments apart). `FALSE` models the code before #524: the flush returned when the changedtick moved since visual mode started, which took a plugin's edit of another line or a `:write` for an operator | +`DemotionIgnoresTerminal` covers a timer armed before a new Claude terminal opens. The terminal is not registered at `BufEnter`, so the timer can fire there before that buffer's debounce runs. Before #562, this cleared `last_active_visual_selection`, and returning to the file sent its cursor even when it had not moved. The early return keeps the record for `HeldUntilMoved`. + Five guards break no rule alone, because another guard covers for them. `--mutants` removes each with the guard that covers for it, and the rule that breaks is the one `EXPECT` in `check.sh` names. `LeaveNoted` breaks `HeldEndsWhenMoved` alone, but only with 2 files, so `--mutants` runs that rule with 2 files. `ReconcileOnTextChanged` breaks `NoDeletedText` alone in the model, because of `YouEditByPlugin`. On real Neovim that guard and `TextChangeReports` are not the only ones that end the hold after an edit of the current buffer: the listener's `on_lines` fires for that edit too and runs the same check, so the running plugin still ends the hold without either guard (`replays/held_after_plugin_edit.lua` prints `OK` with the call removed from `on_text_changed`). The model lets the listener see only an edit of a buffer that is not current (`YouEditOtherBuffer`). A `BROKEN` trace for one of these two guards is a counterexample of the model, not a failure of the running plugin. `--mutants` still requires them, because the model has no other way to end the hold at that edit. | Removed together | Rule that breaks | The two guards | @@ -203,6 +205,6 @@ An advisor of this skill found a last path that the model could not express, and - A held selection whose buffer is unloaded while you are in the Claude terminal (`:bunload`): `demote_if_selection_text_changed` returns for a buffer that is not loaded, so the selection stays held. The model has no unloaded buffer. A user sees Claude keep the selection of a file that is not loaded until they come back, and then get the cursor (checked on Neovim 0.8.0 and 0.12.3 with `:bunload` in a Claude terminal tab and `:buffer` after it). The text on disk did not change, so Claude holds no text that is gone. Without the return, the check could not find the text in the unloaded buffer, would take it for gone, and would send the cursor of a file that is not loaded (read from the code). The listener is detached by the unload (`on_detach`) and attaches itself again only for a loaded buffer. - Edits while you are in visual mode that the model leaves out: an edit of the selected text itself (the debounce or the listener's check reads the new text). The model runs the check of a plugin's edit in the step of the edit, and the code guarantees that since #540: `note_visual_edit` runs `check_visual_edits` from `vim.schedule` (after the command or callback that made the edit returns) and from a `vim.on_key` listener (before the next key is processed, also a key of a mapping or one that a UI sends while a callback runs), whichever comes first. The flush on Esc comes after the first of them. When the key came first, the schedule usually runs later, after the flush, and finds nothing to do. A `ModeChanged` handler that runs before the plugin's and turns the main loop (`vim.wait`) runs it earlier: after Neovim ended visual mode and before the flush. The key listener has already checked the edit then, so that second check finds nothing to do either way (the `unchecked_edits == 0` guard of `check_visual_edits`, which a unit test and an integration case pin). When the schedule came first, the key listener finds nothing. Before #540 only the schedule ran it, so a key that came first reached the flush with an edit that nobody had checked, and the flush took it for an operator's: Claude got the cursor and not the selection, although its text was untouched. Three ways to get there were reproduced on Neovim 0.8.0 and 0.12.3: a mapping that edits and presses Esc in one typeahead, a key that arrives while a callback runs (`nvim_input`, the way a UI sends a key: during the plugin's own callback, or during another one that was queued before the check), and an autocmd that edits when visual mode starts with the Esc already in the typeahead (a `ModeChanged` handler for `n:v`). The three replays print `OK` now and `BUG` on the code before #540. The model has no step in which a key comes before the check, so removing the key listener or the schedule shows only as `VisualCheckSparesPluginEdit` reading `FALSE` (the fact reads both triggers, and the model then breaks `ReleaseSent`). - A reload or `:edit` while you select (`on_reload` and `on_detach` set `consumed`) counts as an operator, because it replaces the text under the selection. It is not modeled: a user sees Claude get the cursor and not the selection after the file was reloaded under it. -- Coming back from the Claude terminal to the file of a held selection with the cursor where it was: `HeldUntilMoved` applies there, so Claude keeps the selection. The model used to allow either outcome (`holdLoose`), because a demotion that fired in the terminal cleared `last_active_visual_selection` and the return then sent the cursor. A new terminal is not registered at its `BufEnter`, so nothing cancelled that demotion, and with the default 50 ms it fires before the debounce of the terminal does. `DemotionIgnoresTerminal` reads the code that no longer clears it, and a tree where that line is back breaks `HeldUntilMoved`. The replay-style probe is `tests/integration/selection_demotion_in_terminal_spec.lua`. +- `HeldUntilMoved` applies when you return from the Claude terminal to the file at the same cursor position. The model used to allow either outcome (`holdLoose`). Restoring the old clear in the terminal guard breaks `HeldUntilMoved`. The replay-style probe is `tests/integration/selection_demotion_in_terminal_spec.lua`. - A Claude that connects after the last change gets no `selection_changed` until the selection changes: `on_connect` sends none, and `has_selection_changed` stops a resend of an unchanged selection. PROTOCOL.md promises a notification only when the selection changes, and the tools answer on request, so this is not a rule. - A pass covers this model at these bounds, for the facts `check.sh` reads, on the covered files exactly as recorded in `covered.lock`. Any other tree reads STALE. diff --git a/lua/claudecode/README.md b/lua/claudecode/README.md index e8dd9e72..ddf766e9 100644 --- a/lua/claudecode/README.md +++ b/lua/claudecode/README.md @@ -144,7 +144,7 @@ Module map (direct files): - `_format_path_for_at_mention` raises on an empty path. When busted is not loaded, it also raises on a path that is neither readable nor a directory. It returns the absolute path from `fnamemodify(path, ":p")` and cuts no cwd prefix, so with cwd `/a/proj` the path `/a/proj2/x` stays `/a/proj2/x`. It resolves a relative path against the cwd at the call. It puts `./` before a path that starts with `~`, so `~foo` names a file in the cwd, not a home directory. A directory, the cwd included, ends in a path separator (`/` outside Windows). Outside Windows it asserts that the result starts with `/` [init.lua:M._format_path_for_at_mention] - Selection tracking is a no-op unless it is enabled. Updates are debounced by 100 ms, except that leaving visual mode, editing or consuming the held text and moving the cursor away from a held selection report at once [selection.lua:M.flush_visual_selection] [selection.lua:reconcile_held_selection]. The held text can also change while you are in the Claude terminal (a reload, a plugin's edit), where `TextChanged` does not fire for its buffer: a listener from `nvim_buf_attach` on that buffer sends the file's cursor then [selection.lua:watch_selection_buffer]. Entering another file waits for the debounce. Claude terminal buffers are skipped [selection.lua:M.update_selection] - Tracking listens to `CursorMoved`, `CursorMovedI`, `BufEnter`, `ModeChanged`, `TextChanged`, `TextChangedI`, and `TextChangedP` [selection.lua:M._create_autocommands] -- After Visual mode ends, the visual selection stays the latest selection until the cursor leaves the position it had at the exit, the held text changes, or a buffer other than the Claude terminal is entered. The cursor position then replaces it, at once for a cursor move or a text change, and after the 100 ms debounce for another buffer. A move to the Claude terminal keeps the visual selection [selection.lua:reconcile_held_selection] [selection.lua:M.update_selection] +- After Visual mode ends, the visual selection stays the latest selection until the cursor leaves the position it had at the exit, the held text changes, or a buffer other than the Claude terminal is entered. The cursor position then replaces it, at once for a cursor move or a text change, and after the 100 ms debounce for another buffer. A move to the Claude terminal keeps the visual selection. If `handle_selection_demotion` fires in that buffer, it returns before clearing the held selection [selection.lua:reconcile_held_selection] [selection.lua:M.update_selection] [selection.lua:M.handle_selection_demotion] - The automatic paths send `selection_changed` only when the selection differs from the last one sent [selection.lua:M.update_selection] [selection.lua:M.flush_visual_selection]. `send_current_selection` sends with no change check. It runs `update_selection` first, so a changed selection goes out twice [selection.lua:M.send_current_selection] - `has_selection_changed` compares only `filePath`, `text`, and the start and end positions [selection.lua:M.has_selection_changed] - Select mode (`gh`, or `v`, `V` and CTRL-V when `'selectmode'` contains `cmd`) is tracked like Visual mode [selection.lua:is_visual_mode], and a command such as a `ClaudeCodeSend` mapping takes the visual path there [visual_commands.lua:M.create_visual_command_wrapper]. The visual path ends Select mode. For a mapping made with `mode = "v"` (`:vmap`), Neovim queues a key that restores Select mode after the mapping, and the visual path cancels it with `gV` [visual_commands.lua:M.exit_visual_and_schedule] diff --git a/tests/integration/selection_held_spec.lua b/tests/integration/selection_held_spec.lua index f1118463..5233ae79 100644 --- a/tests/integration/selection_held_spec.lua +++ b/tests/integration/selection_held_spec.lua @@ -310,8 +310,8 @@ describe("a held selection with the real plugin", function() assert.is_nil(selection.state.cursor_at_flush) end) - -- The demotion timer is pending when the buffer becomes the terminal. When it fires it only knows the terminal - -- that the provider registered, so it used to take this buffer for an ordinary one and send its cursor. + -- The buffer becomes a Claude terminal before the pending demotion fires. The demotion sees the terminal by name, + -- returns without sending a cursor, and leaves the held selection alone. it("does not send a cursor of the buffer that became the Claude terminal when a demotion is pending", function() start({ provider = "none" }, 1000) open_file("alpha beta gamma")