# apply-edit ## Goal Make the three port commands actually edit something in a stock editor. `workspace/executeCommand` used to answer with the `WorkspaceEdit` that `ports::execute` computed, as the request's *result*. The protocol defines no such thing: an `executeCommand` result is `any | null`, and a client applies what a server **pushes** at it with `workspace/applyEdit`. `vscode-languageclient` (which `editors/vscode/extension.js` uses unmodified) drops the result on the floor, and so does Neovim's built-in client — so `lucisv.addPort`, `lucisv.removePort` and `lucisv.propagatePorts` invoked *as commands* did nothing at all in every stock client. (The code-action path was never affected: a code action carries its edit inline.) This round pushes the edit, answers `null`, and reads the one client response the server can no longer ignore. ## Assumptions - **`workspace.applyEdit` is the gate.** A client that advertised it gets the push and a `null` result. A client that did not gets exactly the old behavior — the edit as the result — because for that one it is the only thing that could ever work, and a silent no-op is worse than a non-standard answer that some caller may yet read. Both VS Code and Neovim advertise it; `editors/vscode/smoke.js` sends `capabilities: {}` and so takes the fallback, which is why it still passes untouched. - **The push goes out before the command's own response.** The loop sends a request's response when the handler returns, and the handler sends the `applyEdit` request during it. The protocol allows a server to make a request at any time, and a client that waited for the command result before applying the edit would deadlock against any server that does this — VS Code and Neovim both apply immediately. Stated here because it is the ordering a reader would otherwise wonder about. - **A refusal is worth a popup.** `ApplyWorkspaceEditResponse.applied: false` means the user ran a command, watched nothing happen, and has no other way to find out why. It is shown as an ERROR naming the command, plus the client's own `failureReason` when it sent one. Success says nothing. - **A response that does not parse is a refusal.** Nothing was seen to be applied, so the honest thing is to say so rather than assume the best. - **Watcher (un)registration answers stay ignored.** Their ids are never in the pending map, so they fall straight through the new handler — including an error response, which is what a client that refuses to watch sends. The behavior `docs/features/file-watching/README.md` documents is unchanged. - **No retry, no timeout.** A client that never answers leaves one entry in the pending map for the life of the session; three port commands per session is not a leak worth a timer, and the server has no timers by design. - **The `report_scope(warnings)` behavior is unchanged** — the engine's warnings are still surfaced exactly as before, whichever delivery path the edit takes. ## Handshakes ### `lucisv_server::server` - `ClientAbilities` gains `apply_edit: bool` and derives `Default`, so a test can name only the ability it cares about. `main.rs` fills it from `InitializeParams.capabilities.workspace.apply_edit`. - `Server` gains `applying: HashMap` — the outstanding `workspace/applyEdit` requests, each remembering the command that asked. - `Server::send_request` now **returns** the `RequestId` it sent on. The watcher registration ignores it, exactly as it ignores the answer. - `run`'s `Message::Response(_) => {}` becomes `self.response(response)`. - `ExecuteCommand` goes through the new `Server::deliver`. ### `ports` `ports::execute`'s signature is unchanged; only the doc comment's claim that "the server does not push `workspace/applyEdit`" is now false, and is rewritten. No engine behavior changed. ### `editors/vscode` **No extension change.** `vscode-languageclient` applies `workspace/applyEdit` requests itself; `extension.js` has no custom `executeCommand` handling and needs none. Verified by reading, not assumed. ## Status / TODO - [x] `workspace.applyEdit` read into `ClientAbilities` at initialize - [x] `Server::deliver`: push the edit with a `label`, answer `null` - [x] `Server::response`: match answers to outstanding requests, report a refusal as an ERROR naming the command - [x] Return-the-edit fallback kept for a client that cannot apply - [x] Tests: pushed-and-null, fallback-returns-the-edit, refusal reported, success silent, duplicate/unknown answer ignored, watcher answer still ignored - [ ] Not done, deliberately: no `$/progress` or timeout on an unanswered `applyEdit` — see Assumptions. - [ ] Not done, deliberately: `WorkspaceEdit` still carries plain `changes` (no `documentChanges`, no snippet edits). That is TODO §9c's item, and it would improve `addPort` cursor placement. ## Work log ### 2026-08-26 — dev agent, §9a - **`deliver` is a method on `Server`, not a free function**, because it has to both send a request and remember its id; splitting it would have meant returning a request for the caller to send, which is a shape with one user. - **`send_request` returning the id** was the smallest change that let the applyEdit path record it. The two watcher call sites ignore the value; a `RequestId` is not `#[must_use]`, so this stays silent. - **The pending map is keyed by `RequestId`, not by command**, so two commands in flight cannot be confused and a duplicate answer to a spent id does nothing. Pinned by `an_edit_the_editor_applied_is_said_nothing_about`'s second half. - **The `label`** is `lucisv: ` — it is what an editor shows on its undo stack, so it names the thing the user would want to undo. - Files touched: `crates/lucisv_server/src/server.rs`, `crates/lucisv_server/src/main.rs`, `crates/lucisv_server/tests/e2e.rs`, and `ports.rs`'s stale doc comment (it claimed the server does not push `workspace/applyEdit`). `editors/vscode/extension.js` was read and deliberately left alone. **Verification.** `cargo fmt`, `cargo clippy --all-targets` (zero warnings), `cargo test --workspace`, `node editors/vscode/smoke.js` — all clean. 5 new unit tests in `server.rs` (plus the watcher-answer guard), 1 in `main.rs`, and one e2e session (`a_command_pushes_its_edit_to_a_client_that_can_apply_one`) that pushes the edit at a real binary and answers it as an editor would. ## QA log ### 2026-08-26 — QA agent, §9a **Re-ran the dev's verification, clean.** `cargo fmt --all -- --check`, `cargo clippy --all-targets --workspace` (zero warnings, forced a rebuild by touching the changed files first so the check was not answered from a stale cache), `cargo test --workspace` (3 full runs, no failures), `node editors/vscode/smoke.js target/debug/lucisv-server` (exit 0) — all reproduced independently. **Read the diff against the README's claims,** file:line references against the working tree as it stood before this QA pass: - `server.rs`'s `ExecuteCommand` handler (around what is now line 304) calls `self.report_scope(warnings)` unconditionally in the `Ok` arm, before `self.deliver(...)` branches on the ability — confirmed **both** the push and the fallback path surface engine warnings identically. - **RequestId collision, checked and ruled out.** `applying` and the watcher (un)registration ids are both minted by the same `send_request`, whose counter (`self.request_ids`) is a single monotonically increasing `u32` shared across every call site — two ids can never collide, so a watcher answer can never land in `applying` and vice versa. This is structural, not incidental: there is exactly one counter to break for a collision to become possible. - `ports.rs`'s doc comment rewrite matches the new behavior; no engine behavior changed there, confirmed by reading `execute`'s body (untouched except the comment). **Attacked the response-routing path** (`server.rs::response`, the `ApplyWorkspaceEditResponse` deserialization). The existing tests covered `Ok(applied: true)`, `Ok(applied: false, failureReason)`, a duplicate answer, and a watcher answer — but not the two cases the doc comment explicitly claims to handle: a response payload that is `Ok` JSON but does not deserialize as `ApplyWorkspaceEditResponse` at all, and a JSON-RPC-level `Err` response (a client that rejects the request outright rather than answering it). Added both as tests in `server.rs`: - `a_malformed_apply_edit_answer_is_reported_as_a_refusal` — answers with `{"notApplied": "whatever"}` (no `applied` field, which is required with no default in `lsp_types::ApplyWorkspaceEditResponse`); asserts one ERROR naming the command, not a panic and not silence. - `a_response_with_a_jsonrpc_error_is_reported_too` — uses `Response::new_err` directly; asserts the `error.message` reaches the shown text. Both pass. Confirms the "a response that does not parse is a refusal" assumption is real code, not just a comment. **Found and fixed one self-inflicted test bug while adding the above:** the first draft of `a_malformed_apply_edit_answer_is_reported_as_a_refusal` named its tree `propagating("malformed", ...)`, which collides with the *existing* `Tree::new("malformed")` in `a_malformed_watched_files_notification_is_dropped` — both resolve to the same `std::env::temp_dir()` path (keyed only by pid + name), so running the suite in parallel raced the two tests' filesystem setup and made `ports::execute` fail to find `sub.sv` (`server.rs`, originally around line 1417 in this session's numbering). Not a server bug — a QA-test-authoring bug. Renamed to `"malformedapplyedit"`; reran the full `lucisv_server` suite three times back to back with no failures. Worth a general note for future dev/QA agents: `Tree::new`'s temp path is name-keyed, not test-keyed, so a tree name must be checked for uniqueness across the whole crate, not just the tests being added. **Capability reading:** `apply_edit_support` reads `params.capabilities.workspace.as_ref().and_then(|w| w.apply_edit)` — verified this cannot panic on `InitializeParams::default()` or any partial capability object; already covered by `apply_edit_support_is_read_from_the_clients_own_capabilities`, re-ran it directly. **CLAUDE.md compliance:** grepped the new/changed lines in `server.rs`, `main.rs`, `ports.rs` for `.unwrap()`, `.expect()`, `panic!` outside `#[cfg(test)]` — none found. Public items added (`ClientAbilities::apply_edit`) carry doc comments. README has all seven required sections; TODO.md's checkbox is checked with the item text unchanged and a `→` pointer added. **Verdict: no blocking findings.** The two new attack tests and the tree-name-collision fix are the only changes made to this feature's code. ## Review ### 2026-08-26 — lead Reviewed the diff, the work log and the QA log; re-ran fmt/clippy/the full workspace suite/smoke.js myself, all clean. The design is right: the gate is the client's own `workspace.apply_edit` capability, the fallback is exactly the old behavior for exactly the clients the old behavior could serve, and the pending-map keyed by the one shared id counter makes response routing collision-free by construction (QA verified the same). The `deliver` doc comment's deadlock note is a good catch worth keeping. The no-timeout decision is accepted: three commands per session cannot leak meaningfully, and the server has no timers by design. `ports.rs`'s previously-false doc comment is now true. Accepted as shipped.