H3: Phpactor UX: fast tick while indexing, progress, messages, php -l

- Session tick: 1 ms while Phpactor reports work-done progress and in a
  session's first 120 s, otherwise 25 ms as before. Fast intervals use
  thread::sleep, because recv_timeout wakes at most ~64/s on Windows.
  First useful completion on 2,000 classes: 71.6 s -> 20.7-26.9 s.
- $/progress -> lspProgress {active, title?, message?, percentage?},
  progress in lsp.status, a status-bar item (LspProgressItem).
- window/showMessage -> notification.lsp.message (repeats within 30 s and
  the known Windows lint-provider complaint only logged); showMessage,
  logMessage and stderr in a 1,000-line log, new command lsp.log.
- Lutra's own php -l on Windows (lsp/php_lint.rs): on open and save, one
  worker thread, lutra_proc::spawn, stdin, 10 s timeout, output caps;
  merged into diagnosticsChanged with source "php -l".
- editor.completion.delayMs stays 0 (measured: 100 ms only delays the
  list; Phpactor marks every list incomplete).
- Fake server: scripted notifications after initialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-25 17:33:26 +01:00
parent 8701f50d37
commit e128400246
21 changed files with 1752 additions and 48 deletions

View file

@ -416,7 +416,7 @@ fn no_server_without_a_project_or_toolchain() {
}
assert_eq!(
engine.execute("lsp.status", Value::Null).unwrap().value,
json!({"state": "stopped"})
json!({"state": "stopped", "progress": {"active": false}})
);
let err = engine
.execute("diagnostics.list", json!({"bogus": 1}))

View file

@ -0,0 +1,526 @@
//! Phpactor UX (H3): `window/showMessage` as notifications,
//! `$/progress` as `lspProgress`, the fast tick while the server indexes,
//! and Lutra's own `php -l` on save.
//!
//! The fake server (`lutra-lsp-fake --script`) sends the messages; the
//! `php -l` tests need PHP on `PATH` (or `LUTRA_PHP`) and skip with a
//! message without it. The ignored `real_phpactor_first_completion`
//! measures the time to the first useful completion on a generated
//! project.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use lutra_engine::lsp::Launch;
use lutra_engine::{Engine, EngineError};
use lutra_lsp::ServerOptions;
use serde_json::{Value, json};
use tempfile::TempDir;
/// The fake server binary, built on demand when this test runs without
/// the rest of the workspace (`cargo test -p lutra-engine`).
fn fake_server() -> PathBuf {
let exe = format!("lutra-lsp-fake{}", std::env::consts::EXE_SUFFIX);
let deps = std::env::current_exe().unwrap();
let path = deps.parent().unwrap().parent().unwrap().join(exe);
if !path.exists() {
let cargo = std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
let status = std::process::Command::new(cargo)
.args(["build", "-p", "lutra-lsp", "--bin", "lutra-lsp-fake"])
.status()
.unwrap();
assert!(status.success(), "building lutra-lsp-fake failed");
}
path
}
/// A temp project with `files`, opened in an engine; with `script`, the
/// fake server runs with it (see the fake server's docs), else none.
fn project(files: &[(&str, &str)], script: Option<Value>) -> (TempDir, TempDir, Engine) {
let dir = tempfile::tempdir().unwrap();
let scripts = tempfile::tempdir().unwrap();
for (name, text) in files {
fs::write(dir.path().join(name), text).unwrap();
}
let mut engine = Engine::new().unwrap();
engine.set_php_lint(None);
if let Some(script) = script {
let file = scripts.path().join("script.json");
fs::write(&file, script.to_string()).unwrap();
engine.set_language_server(Some(Launch {
command: fake_server().into(),
args: vec![OsString::from("--script"), file.into_os_string()],
options: ServerOptions::default(),
tick: None,
}));
}
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
(dir, scripts, engine)
}
/// Pumps until an event satisfies `done` (at most 10 s); returns every
/// event seen.
fn pump_until(engine: &mut Engine, done: impl Fn(&Value) -> bool) -> Vec<Value> {
let started = Instant::now();
let mut seen = Vec::new();
loop {
for event in pump(engine) {
let hit = done(&event);
seen.push(event);
if hit {
return seen;
}
}
assert!(
started.elapsed() < Duration::from_secs(10),
"timed out; events: {seen:#?}"
);
std::thread::sleep(Duration::from_millis(5));
}
}
/// The version of buffer `id`.
fn version(engine: &mut Engine, id: u64) -> Value {
engine
.execute("buffer.get", json!({"id": id}))
.unwrap()
.value["version"]
.clone()
}
/// A `$/progress` notification.
fn progress(token: &str, value: Value) -> Value {
json!({"method": "$/progress", "params": {"token": token, "value": value}})
}
#[test]
fn messages_become_notifications_and_progress_reaches_the_status() {
let show = |kind: u8, message: &str| json!({"method": "window/showMessage", "params": {"type": kind, "message": message}});
let script = json!({"notifications": [
show(2, "Index is stale"),
show(2, "Diagnostic provider \"php\" errored with \"CreateProcess failed\", removing from pool"),
show(2, "Index is stale"),
show(1, "File watcher died"),
{"method": "window/logMessage", "params": {"type": 4, "message": "quiet"}},
progress("idx", json!({"kind": "begin", "title": "Indexing workspace", "message": "3 PHP files", "percentage": 0})),
progress("idx", json!({"kind": "report", "message": "2/3", "percentage": 67})),
progress("other", json!({"kind": "report", "percentage": 5})),
progress("idx", json!({"kind": "end", "message": "done"})),
progress("refs", json!({"kind": "begin", "title": "Finding references", "percentage": 250})),
show(3, "last")
]});
let (_dir, _scripts, mut engine) = project(&[("a.php", "<?php\n")], Some(script));
let events = pump_until(&mut engine, |e| e["params"]["message"] == "last");
let notes: Vec<(&str, &str)> = events
.iter()
.filter(|e| e["messageKey"] == "notification.lsp.message")
.map(|e| {
(
e["level"].as_str().unwrap(),
e["params"]["message"].as_str().unwrap(),
)
})
.collect();
assert_eq!(
notes,
[
("warning", "Index is stale"),
("error", "File watcher died"),
("info", "last")
]
);
let progress: Vec<&Value> = events
.iter()
.filter(|e| e["kind"] == "lspProgress")
.collect();
assert_eq!(
progress,
[
&json!({"kind": "lspProgress", "active": true, "title": "Indexing workspace", "message": "3 PHP files", "percentage": 0}),
&json!({"kind": "lspProgress", "active": true, "title": "Indexing workspace", "message": "2/3", "percentage": 67}),
&json!({"kind": "lspProgress", "active": false}),
&json!({"kind": "lspProgress", "active": true, "title": "Finding references", "percentage": 100}),
]
);
let status = engine.execute("lsp.status", json!({})).unwrap().value;
assert_eq!(status["progress"]["title"], "Finding references");
let log = engine.execute("lsp.log", json!({})).unwrap().value;
let lines: Vec<&str> = log["lines"]
.as_array()
.unwrap()
.iter()
.map(|l| l.as_str().unwrap())
.collect();
assert!(lines.contains(&"[info] hello"), "{lines:?}");
assert!(lines.contains(&"[log] quiet"), "{lines:?}");
assert!(
lines.iter().any(|l| l.contains("CreateProcess failed")),
"{lines:?}"
);
let after = events_of(&mut engine, "lsp.restart");
assert!(
after.contains(&json!({"kind": "lspProgress", "active": false})),
"{after:?}"
);
}
/// PHP for the `php -l` tests: `LUTRA_PHP` or `php` on `PATH`; `None`
/// (the test skips) when it does not run.
fn php() -> Option<PathBuf> {
let php = PathBuf::from(std::env::var("LUTRA_PHP").unwrap_or_else(|_| "php".into()));
let runs = std::process::Command::new(&php)
.arg("-v")
.output()
.is_ok_and(|o| o.status.success());
if !runs {
eprintln!("skipped: no PHP (set LUTRA_PHP or put php on PATH)");
}
runs.then_some(php)
}
/// The `php -l` items of a `diagnosticsChanged` event.
fn lint_items(event: &Value) -> Vec<&Value> {
event["items"]
.as_array()
.map(|items| items.iter().filter(|i| i["source"] == "php -l").collect())
.unwrap_or_default()
}
#[test]
fn php_lint_reports_syntax_errors_on_open_and_save() {
let Some(php) = php() else {
return;
};
let broken = "<?php\n\nfunction f() {\n retur 1;\n}\n";
let (dir, _scripts, mut engine) = project(&[("a.php", broken), ("b.txt", "x")], None);
engine.set_php_lint(Some(php));
let id = open(&mut engine, "a.php");
open(&mut engine, "b.txt");
let events = pump_until(&mut engine, |e| e["kind"] == "diagnosticsChanged");
let changed = events.last().unwrap();
assert_eq!(changed["path"], "a.php");
let items = lint_items(changed);
assert_eq!(items.len(), 1, "{changed}");
assert_eq!(items[0]["severity"], "error");
assert_eq!(items[0]["range"]["start"], json!({"line": 3, "column": 0}));
assert_eq!(items[0]["range"]["end"], json!({"line": 3, "column": 12}));
assert!(
items[0]["message"]
.as_str()
.unwrap()
.contains("syntax error"),
"{changed}"
);
let listed = engine.execute("diagnostics.list", json!({})).unwrap().value;
assert_eq!(listed["files"][0]["path"], "a.php");
let base = version(&mut engine, id);
engine
.execute(
"buffer.edit",
json!({"id": id, "baseVersion": base, "edits": [{"from": 26, "to": 31, "insert": "return"}]}),
)
.unwrap();
for _ in 0..20 {
let events = pump(&mut engine);
assert!(
!events.iter().any(|e| e["kind"] == "diagnosticsChanged"),
"a dirty buffer is not linted: {events:?}"
);
std::thread::sleep(Duration::from_millis(5));
}
engine.execute("file.save", json!({"id": id})).unwrap();
let events = pump_until(&mut engine, |e| e["kind"] == "diagnosticsChanged");
assert_eq!(
events.last().unwrap(),
&json!({"kind": "diagnosticsChanged", "path": "a.php", "items": []})
);
assert!(
fs::read_to_string(dir.path().join("a.php"))
.unwrap()
.contains("return 1;")
);
let base = version(&mut engine, id);
engine
.execute(
"buffer.edit",
json!({"id": id, "baseVersion": base, "edits": [{"from": 38, "to": 38, "insert": "}"}]}),
)
.unwrap();
engine.execute("file.save", json!({"id": id})).unwrap();
let events = pump_until(&mut engine, |e| e["kind"] == "diagnosticsChanged");
assert_eq!(lint_items(events.last().unwrap()).len(), 1);
let mut events = events_of(&mut engine, "project.close");
events.extend(pump(&mut engine));
let cleared = events
.iter()
.find(|e| e["kind"] == "diagnosticsChanged")
.unwrap();
assert_eq!(cleared["items"], json!([]));
}
#[test]
fn php_lint_merges_with_the_servers_diagnostics() {
let Some(php) = php() else {
return;
};
let (_dir, _scripts, mut engine) = project(&[("a.php", "<?php\nif (\n")], Some(json!({})));
engine.set_php_lint(Some(php));
open(&mut engine, "a.php");
let events = pump_until(&mut engine, |e| {
e["kind"] == "diagnosticsChanged" && e["items"].as_array().is_some_and(|i| i.len() == 2)
});
let items = events.last().unwrap()["items"].as_array().unwrap();
assert_eq!(items[0]["source"], "fake");
assert_eq!(items[1]["source"], "php -l");
let events = events_of(&mut engine, "lsp.restart");
let changed = events
.iter()
.find(|e| e["kind"] == "diagnosticsChanged")
.unwrap();
let items = changed["items"].as_array().unwrap();
assert_eq!(items.len(), 1, "the server's diagnostic goes, php -l stays");
assert_eq!(items[0]["source"], "php -l");
}
#[test]
fn php_lint_is_off_without_php() {
let (_dir, _scripts, mut engine) = project(&[("a.php", "<?php\nif (\n")], None);
open(&mut engine, "a.php");
for _ in 0..40 {
let events = pump(&mut engine);
assert!(
!events.iter().any(|e| e["kind"] == "diagnosticsChanged"),
"{events:?}"
);
std::thread::sleep(Duration::from_millis(5));
}
let listed = engine.execute("diagnostics.list", json!({})).unwrap().value;
assert_eq!(listed["files"], json!([]));
}
/// Opens `path`; returns the buffer id.
fn open(engine: &mut Engine, path: &str) -> u64 {
engine
.execute("file.open", json!({"path": path}))
.unwrap()
.value["id"]
.as_u64()
.unwrap()
}
/// Runs `command` and waits for its pending result, as a host does.
fn ask(engine: &mut Engine, command: &str, params: Value) -> Result<Value, EngineError> {
let mut outcome = engine.execute(command, params)?;
match outcome.pending.take() {
Some(pending) => Ok(engine.resolve(pending.wait())?.value),
None => Ok(outcome.value),
}
}
/// Runs `command` and returns the events it queued, as JSON.
fn events_of(engine: &mut Engine, command: &str) -> Vec<Value> {
engine
.execute(command, json!({}))
.unwrap()
.events
.iter()
.map(|e| serde_json::to_value(e).unwrap())
.collect()
}
/// Pumps once and returns the events as JSON.
fn pump(engine: &mut Engine) -> Vec<Value> {
engine
.pump_vfs()
.unwrap()
.iter()
.map(|e| serde_json::to_value(e).unwrap())
.collect()
}
/// A shop-like project: `classes` classes in `src/` plus the file the
/// test completes in.
fn shop(classes: usize) -> Vec<(String, String)> {
let mut files = Vec::new();
for i in 0..classes {
files.push((
format!("src/Product{i}.php"),
format!(
"<?php\n\nnamespace Shop;\n\n/**\n * A product of the shop, number {i}.\n */\nfinal class Product{i}\n{{\n public function __construct(private string $name, private int $price) {{}}\n\n /** The display name. */\n public function name(): string\n {{\n return $this->name;\n }}\n\n /** The price in cents, times `$count`. */\n public function price(int $count = 1, bool $gross = true): int\n {{\n return $this->price * $count;\n }}\n}}\n"
),
));
}
files.push((
"src/Cart.php".into(),
"<?php\n\nnamespace Shop;\n\nfinal class Cart\n{\n public function add(Product1999 $product): void\n {\n $product->\n }\n}\n".into(),
));
files
}
/// Time from opening a generated project to the first completion list
/// that has the method of a class Phpactor must have indexed
/// (`Product1999`, or the last class of a smaller project), with the
/// `lspProgress` events on the way:
///
/// `LUTRA_PHPACTOR=<phar> [LUTRA_PHP=php] [LUTRA_SHOP_CLASSES=2000] cargo test -p lutra-engine --test lsp_ux real_phpactor_first_completion -- --ignored --nocapture`
#[test]
#[ignore = "needs PHP and Phpactor (LUTRA_PHPACTOR)"]
fn real_phpactor_first_completion() {
let phar = std::env::var("LUTRA_PHPACTOR").expect("LUTRA_PHPACTOR");
let php = std::env::var("LUTRA_PHP").unwrap_or_else(|_| "php".into());
let classes: usize = std::env::var("LUTRA_SHOP_CLASSES")
.ok()
.and_then(|n| n.parse().ok())
.unwrap_or(2000);
let dir = tempfile::tempdir().unwrap();
for (name, text) in shop(classes) {
let text = text.replace("Product1999", &format!("Product{}", classes - 1));
let path = dir.path().join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
let mut engine = Engine::new().unwrap();
engine.set_language_server(Some(lutra_engine::lsp::phpactor_launch(
&php,
&phar,
cfg!(windows),
)));
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let started = Instant::now();
let id = open(&mut engine, "src/Cart.php");
let at_arrow =
json!({"id": id, "position": {"line": 8, "column": 18}, "triggerCharacter": ">"});
let mut ready = None;
let mut attempts = 0;
loop {
for event in pump(&mut engine) {
match event["kind"].as_str() {
Some("lspStatus") if event["state"] == "ready" && ready.is_none() => {
ready = Some(started.elapsed());
println!("ready after {:?}", started.elapsed());
}
Some("lspProgress") => println!("{:?} {event}", started.elapsed()),
Some("notification") => println!("{:?} {event}", started.elapsed()),
_ => {}
}
}
if ready.is_some() {
attempts += 1;
let out = ask(&mut engine, "lsp.completion", at_arrow.clone()).unwrap();
let found = out["items"]
.as_array()
.unwrap()
.iter()
.any(|i| i["label"] == "price");
if found {
break;
}
}
assert!(
started.elapsed() < Duration::from_secs(180),
"no useful completion within 180 s"
);
std::thread::sleep(Duration::from_millis(250));
}
println!(
"FIRST USEFUL COMPLETION {:?} after project.open ({classes} classes, {attempts} attempts)",
started.elapsed()
);
std::thread::sleep(Duration::from_secs(2));
for event in pump(&mut engine) {
if event["kind"] == "notification" {
println!("{event}");
}
}
let log = engine.execute("lsp.log", json!({})).unwrap().value;
for line in log["lines"].as_array().unwrap() {
if line.as_str().unwrap().starts_with("[show") {
println!("log: {line}");
}
}
let requests: usize = std::env::var("LUTRA_UX_REQUESTS")
.ok()
.and_then(|n| n.parse().ok())
.unwrap_or(0);
if requests > 0 {
std::thread::sleep(Duration::from_secs(5));
let epoch = || {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
};
let cases = [
("member", " $product->pri", None::<&str>),
("class", " $x = new Produc", None),
];
for (name, line, trigger) in cases {
let source = fs::read_to_string(dir.path().join("src/Cart.php")).unwrap();
let text: String = source
.split('\n')
.enumerate()
.map(|(i, l)| if i == 8 { line } else { l })
.collect::<Vec<_>>()
.join("\n");
let current = engine
.execute("buffer.get", json!({"id": id}))
.unwrap()
.value;
engine
.execute(
"buffer.edit",
json!({"id": id, "baseVersion": current["version"], "edits": [{"from": 0, "to": current["text"].as_str().unwrap().encode_utf16().count(), "insert": text}]}),
)
.unwrap();
let at = json!({"id": id, "position": {"line": 8, "column": line.len()}, "triggerCharacter": trigger});
let _ = ask(&mut engine, "lsp.completion", at.clone());
println!("PHASE {name} start {}", epoch());
let mut ms = Vec::new();
let mut last = Value::Null;
for _ in 0..requests {
let t = Instant::now();
last = ask(&mut engine, "lsp.completion", at.clone()).unwrap();
ms.push(t.elapsed().as_secs_f64() * 1000.0);
}
println!("PHASE {name} end {}", epoch());
ms.sort_by(f64::total_cmp);
println!(
"{name}: {requests} requests, median {:.0} ms, max {:.0} ms, items {}, incomplete {}",
ms[ms.len() / 2],
ms[ms.len() - 1],
last["items"].as_array().unwrap().len(),
last["incomplete"]
);
}
}
let idle: u64 = std::env::var("LUTRA_UX_IDLE_SECS")
.ok()
.and_then(|n| n.parse().ok())
.unwrap_or(0);
if idle > 0 {
let epoch = || {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
};
println!("PHASE idle start {} at {:?}", epoch(), started.elapsed());
std::thread::sleep(Duration::from_secs(idle));
println!("PHASE idle end {} at {:?}", epoch(), started.elapsed());
}
}