PM: merge F3 hardening: suspended Phpactor start, ConPTY in a job, completion cancel, syntax proptest
Some checks are pending
ci / ci (macos-15) (push) Waiting to run
ci / ci (ubuntu-24.04) (push) Waiting to run
ci / ci (windows-2025) (push) Waiting to run

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

View file

@ -19,6 +19,14 @@
//! runs these commands through the `engine_execute_waiting` IPC, on a
//! worker thread, so the window never waits either.
//!
//! **Superseded completions:** CodeMirror asks again as the user types and
//! drops the older answer. Each new `lsp.completion` therefore cancels the
//! previous one still pending on the same server (`$/cancelRequest`, via
//! [`Server::cancel`]), so Phpactor stops computing a list nobody reads.
//! The cancelled command resolves with the empty result, not an error.
//! Hover and signature help are not cancelled: they are asked once per
//! pause, not per keystroke.
//!
//! Without a session (no project, toolchain not ready, server failed) the
//! commands fail with `failed`; a server that does not offer the feature,
//! and a buffer that is not PHP, give the empty result. Server answers are
@ -26,11 +34,12 @@
//! empty result.
use std::path::Path;
use std::sync::{Mutex, PoisonError};
use std::time::Duration;
use lutra_lsp::types::position_to_json;
use lutra_lsp::uri::path_to_uri;
use lutra_lsp::{LspError, Range, Server};
use lutra_lsp::{LspError, Range, RequestId, Server};
use lutra_text::{ColumnUnit, Position};
use serde::Deserialize;
use serde_json::{Map, Value, json};
@ -65,6 +74,13 @@ pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
/// for the queue in front of it ([`Pending::wait`] adds one more second).
const WAIT_TIMEOUT: Duration = Duration::from_secs(11);
/// The latest `textDocument/completion` request: the address of the
/// [`Server`] it went to and its id. One slot for the process is enough:
/// there is one PHP session per engine, and a slot taken over by another
/// engine's server only means one older request runs to its end. The
/// address keeps a request from being cancelled on another server.
static LATEST_COMPLETION: Mutex<Option<(usize, RequestId)>> = Mutex::new(None);
/// Registers the commands of this module.
///
/// # Errors
@ -146,6 +162,7 @@ impl Command for Completion {
None => json!({"triggerKind": 1}),
}),
convert: completion_result,
supersede: true,
},
)
}
@ -183,6 +200,7 @@ impl Command for Hover {
empty: json!({ "contents": "" }),
context: None,
convert: hover_result,
supersede: false,
},
)
}
@ -220,6 +238,7 @@ impl Command for SignatureHelp {
empty: json!({ "signatures": [], "activeSignature": 0, "activeParameter": 0 }),
context: None,
convert: signature_result,
supersede: false,
},
)
}
@ -243,6 +262,9 @@ struct Ask {
context: Option<Value>,
/// Converts the server's answer into the command's result.
convert: fn(&Value) -> Value,
/// Whether this request cancels the previous one of its kind still
/// pending (completion; see the module docs).
supersede: bool,
}
/// Validates the request, queues it on the session (see the module docs)
@ -262,6 +284,7 @@ fn ask(state: &mut State, ask: Ask) -> Result<Outcome, EngineError> {
empty,
context,
convert,
supersede,
} = ask;
let open = state
.buffers
@ -300,17 +323,28 @@ fn ask(state: &mut State, ask: Ask) -> Result<Outcome, EngineError> {
let _ = sender.send(Ok(empty));
return;
}
let empty = empty.clone();
if supersede {
cancel_latest(server);
}
let reply = sender.clone();
let sent = server.request_with(
method,
request,
REQUEST_TIMEOUT,
Box::new(move |result: Result<Value, LspError>| {
let _ = reply.send(result.map(|value| convert(&value)));
let _ = reply.send(match result {
Err(LspError::Cancelled) => Ok(empty),
other => other.map(|value| convert(&value)),
});
}),
);
if let Err(e) = sent {
let _ = sender.send(Err(e));
match sent {
Ok(request) if supersede => remember_latest(server, request),
Ok(_) => {}
Err(e) => {
let _ = sender.send(Err(e));
}
}
});
if !queued {
@ -325,6 +359,33 @@ fn ask(state: &mut State, ask: Ask) -> Result<Outcome, EngineError> {
)))
}
/// The address that identifies `server` in [`LATEST_COMPLETION`].
fn server_key(server: &Server) -> usize {
std::ptr::from_ref(server).addr()
}
/// Cancels the latest completion request if it went to `server` and is
/// still pending; its command then resolves with the empty result. A
/// poisoned slot is used as it is: it only holds plain data.
fn cancel_latest(server: &Server) {
let latest = LATEST_COMPLETION
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take();
if let Some((key, id)) = latest
&& key == server_key(server)
{
server.cancel(id);
}
}
/// Records `id` as the latest completion request, sent to `server`.
fn remember_latest(server: &Server, id: RequestId) {
*LATEST_COMPLETION
.lock()
.unwrap_or_else(PoisonError::into_inner) = Some((server_key(server), id));
}
/// The command's value from the converted answer.
///
/// # Errors

View file

@ -147,6 +147,39 @@ fn completion_sees_the_edit_before_it_and_is_capped() {
assert_eq!(items[0]["label"], "item000");
}
#[test]
fn a_new_completion_cancels_the_one_it_supersedes() {
let (_dir, mut engine) = project(&[("a.php", "<?php\nSLOW")], true);
let id = open(&mut engine, "a.php");
let mut slow = engine
.execute(
"lsp.completion",
json!({"id": id, "position": {"line": 1, "column": 4}}),
)
.unwrap();
let slow = slow.pending.take().unwrap();
engine
.execute(
"buffer.edit",
json!({"id": id, "baseVersion": 1, "edits": [{"from": 6, "to": 10, "insert": ""}]}),
)
.unwrap();
let fresh = ask(
&mut engine,
"lsp.completion",
json!({"id": id, "position": {"line": 1, "column": 0}}),
)
.unwrap();
assert_eq!(fresh["items"].as_array().unwrap().len(), 4);
let started = std::time::Instant::now();
let superseded = engine.resolve(slow.wait()).unwrap().value;
assert!(
started.elapsed() < std::time::Duration::from_secs(5),
"the superseded request was not cancelled"
);
assert_eq!(superseded, json!({"items": [], "incomplete": false}));
}
#[test]
fn hover_and_signature_help() {
let (_dir, mut engine) = project(&[("a.php", "<?php\r\npair(1, 2);\r\n")], true);