F3: suspended Phpactor start, ConPTY shell in a job, cancel superseded completions, syntax proptest

- lutra_proc::spawn_suspended: CREATE_SUSPENDED, join the kill-on-close
  job, resume via a Tool Help thread snapshot. Opt-in for long-lived
  children (Phpactor); git, php -l and the probe keep spawn because the
  snapshot costs ~60 ms per start.
- lutra_proc::adopt + Guard (Windows): the terminal's ConPTY shell joins a
  job, so processes it started end with the tab or with Lutra.
- lsp.completion cancels the previous pending completion on the same
  server ($/cancelRequest); the cancelled command resolves empty.
- Syntax proptests compare leaf kinds, outline and folding instead of the
  S-expression (tree-sitter-php 0.25.1 keeps a union_type wrapper after
  an undo); Malte's seed added.
- docs/development.md: data dirs, target dir inside the repo, parallel
  builds, deleting ui/node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-27 17:28:54 +01:00
parent 7ad3005217
commit ebbc89832c
17 changed files with 468 additions and 36 deletions

View file

@ -264,7 +264,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
`crates/syntax` (L1, depends on `lutra-text` only) parses PHP with tree-sitter (`tree-sitter` 0.26, `tree-sitter-php` 0.25, both MIT; the grammar variant that accepts inline HTML around `<?php` tags). It has no engine types; M2.7 wires it to buffers and M2.5 builds the index on it.
- **API:** `Parser::new()?`, `parser.parse(&str) -> Tree` (never fails), `parser.reparse(&Tree, &[lutra_text::Edit]) -> Result<Tree, SyntaxError>`, `outline(&Tree) -> Vec<OutlineItem>`, `folding(&Tree) -> Vec<Range>`. `Tree` carries the text it was parsed from (a `lutra_text::Buffer`, cheap to clone) plus `truncated()` and `has_errors()`; that is why `outline` takes no separate text argument (the contract sketch had `outline(tree, text)`): a tree and a mismatching text can't be combined.
- **Reparse:** the edit batch has `Buffer::apply` semantics (ranges against the old text, any order). It is mapped to one tree-sitter `InputEdit` covering the smallest start to the largest end, with points (row, byte column, rows by `\n` as tree-sitter counts them) computed from the old and new text. Proptests check that a reparse gives the same tree, outline and folding as a fresh parse whenever the edited text is error-free, and that an undo round trip ends at the original tree. On broken code tree-sitter's error recovery may legitimately differ between an incremental and a fresh parse; the engine can reparse from scratch when it wants a canonical tree (for example on save).
- **Reparse:** the edit batch has `Buffer::apply` semantics (ranges against the old text, any order). It is mapped to one tree-sitter `InputEdit` covering the smallest start to the largest end, with points (row, byte column, rows by `\n` as tree-sitter counts them) computed from the old and new text. Proptests check that a reparse gives the same leaf node kinds, outline and folding as a fresh parse whenever the edited text is error-free, and that an undo round trip ends at the original's. They compare leaves, not the whole S-expression (F3): an incremental reparse may keep a wrapper node a fresh parse does not build (tree-sitter-php 0.25.1, the newest release, keeps `(union_type (primitive_type))` for `array &$cache` after an undo where a fresh parse gives `(primitive_type)`), and nothing Lutra builds on the tree reads those wrappers. On broken code tree-sitter's error recovery may legitimately differ between an incremental and a fresh parse; the engine can reparse from scratch when it wants a canonical tree (for example on save).
- **Outline:** `OutlineItem {kind, name, detail, range, selection_range, children}`; `OutlineKind::as_str()` gives the contract names (`namespace`, `class`, …, `enumCase`). Braced and unbraced namespaces (an unbraced one owns everything up to the next namespace statement), promoted constructor parameters as properties after the constructor, enum cases with backing values, multi-element `const`/property declarations as one item each, attributes inside the declaration range. Anonymous classes are classes named `class@anonymous` (`OutlineItem::is_anonymous`, for the index to skip). Function, method and closure bodies are not searched; statements at file level are (`if (!function_exists(…)) { function … }`). Error nodes are transparent and nameless declarations are dropped. The search descends at most 96 levels.
- **Folding:** multi-line brace blocks (class-like bodies, function and control bodies, `switch`/`match`), arrays, comments, heredoc/nowdoc, `use A\{…}` groups and runs of two or more `use` statements; ranges include the delimiters. The walk is iterative and skips the inside of single-line nodes.
- **Positions:** 0-based line and UTF-16 column through `Buffer::offset_to_position` (about 1 µs per conversion on a 1 MB rope; the parse dominates).
@ -343,9 +343,11 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
- **Windows:** one job object per child with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` (`windows-sys`). Lutra holds the only handle, so dropping the `Child` or the end of Lutra's process (the kernel closes its handles) kills the child and everything it started. Jobs nest (Windows 8+), so this works when Lutra itself runs in a job (terminal, `cargo`, CI). `crates/proc/src/windows.rs` is the only module that allows `unsafe` (FFI).
- **Unix:** the child leads a new process group; a `/bin/sh` watchdog in its own group blocks on a pipe only Lutra holds and sends `SIGKILL` to the child's group when the pipe closes (drop, kill, quit, or a `SIGKILL` of Lutra). Left behind after a `SIGKILL` of Lutra: processes that left the group themselves (`setsid`/`setpgid`, daemons), and the child if Lutra dies between starting it and starting its watchdog. Compiled and tested by CI only (no Unix machine here).
- `Child` dereferences to `std::process::Child`. `Child::kill` replaces the standard `kill` and takes the whole group; `kill_and_wait` also reaps; `Drop` kills the group and reaps.
- A child runs for a moment before it joins its job (Windows) or before its watchdog exists (Unix); a grandchild started in those microseconds can escape.
- Tests: `crates/proc/tests/process_tree.rs` starts `lutra-proc-helper`, which starts a sleeping grandchild holding the test's pipe. A hard kill of the helper (`TerminateProcess` / `SIGKILL`, with and without the helper itself in a group), `Child::kill` and drop each end the tree; the pipe reaches end of file. Without `KILL_ON_JOB_CLOSE` the hard-kill tests fail.
- The integrated terminal (portable-pty) does not use it yet. On Unix portable-pty makes the shell a session leader, and closing the PTY master on Lutra's exit hangs it up. On Windows the ConPTY host ends its clients when its pipes break; joining the shell to a job needs an adopt-by-handle API (`portable_pty::Child::as_raw_handle`), a follow-up.
- A child of `spawn` runs for a moment before it joins its job (Windows) or before its watchdog exists (Unix); a grandchild started in those microseconds can escape. Short-lived tools (git, `php -l`, the `php -v` probe) keep this path: git starts hooks long after it is in its job.
- `lutra_proc::spawn_suspended` (F3) closes the window on Windows for long-lived children (Phpactor): `CREATE_SUSPENDED | CREATE_NO_WINDOW` (replacing the command's creation flags), assign to the job, then resume the primary thread. std keeps the thread handle to itself, so the thread is found with a Tool Help snapshot (`CreateToolhelp32Snapshot` + `Thread32First/Next`, `OpenThread` + `ResumeThread`). The snapshot covers every thread on the machine. Measured 2026-10-27, debug, 30× `cmd /c exit`, other builds running: std 35–38 ms, std + `CREATE_NO_WINDOW` 54–55 ms, `spawn` 55–65 ms (unchanged from H1), `spawn_suspended` 130–187 ms. That is why git does not use it. On Unix it is `spawn`.
- `lutra_proc::adopt(RawHandle) -> io::Result<Guard>` (Windows, F3) puts a process someone else started into a new kill-on-close job; dropping the `Guard` or `Guard::kill` ends the process and everything it started since.
- Tests: `crates/proc/tests/process_tree.rs` starts `lutra-proc-helper`, which starts a sleeping grandchild holding the test's pipe. A hard kill of the helper (`TerminateProcess` / `SIGKILL`, with and without the helper itself in a group), `Child::kill` and drop each end the tree; the pipe reaches end of file. Without `KILL_ON_JOB_CLOSE` the hard-kill tests fail. The same runs for `spawn_suspended`, and `adopt` is tested with a helper that starts its grandchild only after a line on stdin, i.e. after the adoption (9 tests).
- The integrated terminal (portable-pty): on Windows the ConPTY shell is adopted into a job right after it starts (`portable_pty::Child::as_raw_handle`, F3). `Session::kill` and drop kill the job, so processes the shell started (a dev server, `start /b`) end with the terminal tab or with Lutra, even after the shell itself exited. A process the shell starts in the microseconds before the adoption is not in the job; the ConPTY host still ends its console clients when its pipes break. On Unix portable-pty makes the shell a session leader, and closing the PTY master on Lutra's exit hangs it up.
### Terminal (M3.6)
@ -422,7 +424,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
- After an edit: 1.8 s and 2.7–2.9 s, including Phpactor's 1 s debounce.
- Without the adjustments: no diagnostics within 180 s.
- **Known gaps:**
- ~~A Phpactor process outlives Lutra when Lutra is killed.~~ Fixed by H1: Phpactor starts through `lutra_proc::spawn` (see Child processes).
- ~~A Phpactor process outlives Lutra when Lutra is killed.~~ Fixed by H1: Phpactor starts through `lutra_proc::spawn` (since F3 `spawn_suspended`; see Child processes).
- ~~`window/showMessage` and `logMessage` are dropped, and server-initiated progress is not shown.~~ Fixed by H3.
- **Tests:**
- `crates/engine/tests/lsp.rs` has 10 tests against `lutra-lsp-fake`: sync incl. UTF-16 and CRLF edits, diagnostics list/clear, reload resync, requests after edits, project close, restart, quit, missing server, crash and give-up, no server without a project, and a proptest of random edits. The ignored `real_phpactor_reports_diagnostics` runs with `LUTRA_PHPACTOR=<phar>`.
@ -466,6 +468,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
- Under the lock the command validates, runs the LSP pump once (so a buffer opened a moment ago is opened on the server first) and queues a closure with `lsp::submit`. That runs after every queued `didChange`.
- The closure sends the request with `request_with` (timeout 10 s). The reply callback converts the answer on the client thread, so a big list is never converted under the lock.
- It sends the result into the `Pending`'s channel. The host waits without the lock (wait 11 s + 1 s grace) and resolves.
- **Superseded completions (F3):** each `lsp.completion` cancels the previous completion request still pending on the same server (`Server::cancel`, which sends `$/cancelRequest`) before it sends its own; CodeMirror has dropped that answer anyway. The cancelled command resolves with the empty result, not an error. The latest request is one process-wide slot keyed by the server's address. Hover and signature help are not cancelled. Test: `a_new_completion_cancels_the_one_it_supersedes` (the fake server holds a completion while the text contains `SLOW`).
- The UI calls these commands with `runWaiting` (`engine_execute_waiting`, off the main thread).
- **Editor:**
- **Before a request,** the UI awaits the buffer sync's `idle()` and drops the request if the document changed meanwhile. Positions are lines and UTF-16 columns, which CodeMirror counts the same way within a line, so CRLF files need no conversion.
@ -649,6 +652,7 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
| Date | Change | Summary |
|---|---|---|
| 2026-10-27 | minor | F3: `lutra_proc::spawn_suspended` (Phpactor) and `adopt` (ConPTY shell in a job), cancel superseded completions, syntax proptests compare leaf kinds |
| 2026-10-26 | minor | M4.5: branches (status-bar item, popup, `vcs.branches`, `vcs.newBranch`, `vcs.checkout`) |
| 2026-10-26 | minor | M4.3: diff viewer (`vcs.showDiff`, `vcs.nextChange`, `vcs.previousChange`, `@codemirror/merge` loaded lazily, the project tree's context menu) |
| 2026-10-25 | minor | M4.4: `git.lineChanges` (index cache, `similar` line diff off the lock), gutter change markers, Git status colours in the tree and tabs |