TODO section 28, lane C (the file-size rule in CLAUDE.md).
server.rs (2 559 production + 6 293 test lines) becomes server/{mod,
dispatch, client, index, notification, progress, diagnostics, batch,
formatting, large_file, semantic_tokens}.rs with the tests in
server/tests/ (16 files, one per former banner section, 15 shared
helpers hoisted into the harness); completion.rs (1 716 + 2 463)
becomes completion/{mod, member, module, tiers, word, site,
harness}.rs with each provider's tests beside it. answer_request (393)
and handle_notification (197) stay whole as the dispatch tables they
are; perf.rs stays whole because its one sentence has no and. Every
import elsewhere in the crate resolves through pub(crate) re-exports,
so main.rs, inline.rs, instantiate.rs, hover.rs, nav.rs and state.rs
are byte-identical. Proof: per-binary counts identical (1 435 / 85 / 3;
workspace 3 215 / 48), e2e and the vsix smoke green, a live LSP session
against opentitan answering byte-identically from this build and the
base, and a line-multiset + signature census (360/360, 188/188) done
by the dev and redone from scratch by QA.
docs/features/decomposition-server/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
389 lines
15 KiB
Rust
389 lines
15 KiB
Rust
//! `$/cancelRequest` (`docs/features/cancel-request/README.md`).
|
|
|
|
use super::*;
|
|
|
|
// ------------------------------------------------------------------
|
|
// $/cancelRequest — docs/features/cancel-request/README.md
|
|
// ------------------------------------------------------------------
|
|
|
|
/// A `textDocument/documentSymbol` request, queued. Any request would do;
|
|
/// this one is cheap and its answer names what the buffer holds, which is
|
|
/// what the ordering test needs.
|
|
fn ask(id: i32, uri: &Uri) -> Message {
|
|
Message::Request(Request::new(
|
|
RequestId::from(id),
|
|
DocumentSymbolRequest::METHOD.to_string(),
|
|
serde_json::json!({ "textDocument": { "uri": uri } }),
|
|
))
|
|
}
|
|
|
|
/// A `$/cancelRequest` naming `id`, exactly as a client sends it — the id
|
|
/// is passed as raw JSON so both protocol forms can be tested.
|
|
fn cancel(id: Value) -> Message {
|
|
Message::Notification(Notification::new(
|
|
Cancel::METHOD.to_string(),
|
|
serde_json::json!({ "id": id }),
|
|
))
|
|
}
|
|
|
|
/// The protocol's `RequestCancelled`.
|
|
const CANCELLED: i32 = -32800;
|
|
|
|
/// Every response since the last drain, as (id, error code or `None`).
|
|
fn outcomes(session: &Session) -> Vec<(RequestId, Option<i32>)> {
|
|
session
|
|
.answers()
|
|
.into_iter()
|
|
.map(|response| {
|
|
let code = response.response_result.err().map(|error| error.code);
|
|
(response.id, code)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The top-level symbol names in a `documentSymbol` answer.
|
|
fn symbol_names(result: &Value) -> Vec<String> {
|
|
result
|
|
.as_array()
|
|
.expect("an array of symbols")
|
|
.iter()
|
|
.map(|symbol| symbol["name"].as_str().expect("a name").to_string())
|
|
.collect()
|
|
}
|
|
|
|
/// A session with one open buffer, drained of the startup traffic.
|
|
fn cancelling_session(name: &str, text: &str) -> (Tree, Session, Uri) {
|
|
let tree = Tree::new(name);
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), text);
|
|
let _ = session.drain();
|
|
(tree, session, uri)
|
|
}
|
|
|
|
const ONE_MODULE: &str = "module m;\nendmodule\n";
|
|
|
|
#[test]
|
|
fn a_request_cancelled_in_the_same_burst_is_answered_cancelled() {
|
|
// The point of the whole feature: the client asked and changed its
|
|
// mind while the server was busy elsewhere, so the answer costs
|
|
// nothing to produce.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelsame", ONE_MODULE);
|
|
let shutdown = session.batch(vec![ask(1, &uri), cancel(serde_json::json!(1))]);
|
|
|
|
assert!(!shutdown, "a cancel is not a shutdown");
|
|
assert_eq!(outcomes(&session), vec![(RequestId::from(1), Some(CANCELLED))]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_dequeued_before_its_request_still_cancels_it() {
|
|
// The batch is scanned as a whole before any of it runs, so a cancel
|
|
// the transport happened to deliver first is not a cancel that
|
|
// arrived too early.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelfirst", ONE_MODULE);
|
|
session.batch(vec![cancel(serde_json::json!(1)), ask(1, &uri)]);
|
|
|
|
assert_eq!(outcomes(&session), vec![(RequestId::from(1), Some(CANCELLED))]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_names_one_request_and_leaves_the_rest_of_the_burst_alone() {
|
|
let (_tree, mut session, uri) = cancelling_session("cancelone", ONE_MODULE);
|
|
session.batch(vec![ask(1, &uri), ask(2, &uri), cancel(serde_json::json!(1))]);
|
|
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(1), Some(CANCELLED)), (RequestId::from(2), None)],
|
|
"id 2 was answered normally, and in its own place in the queue"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_for_a_request_that_is_not_in_the_burst_is_dropped_on_the_spot() {
|
|
// A cancel that arrives in a later burst than its request is too late
|
|
// — that request was answered already — and remembering it would be a
|
|
// set that grows for the life of the session. The identical id in the
|
|
// burst after proves nothing was kept: real clients never reuse an
|
|
// id, so only a leak could cancel this one.
|
|
let (_tree, mut session, uri) = cancelling_session("cancellate", ONE_MODULE);
|
|
let shutdown = session.batch(vec![cancel(serde_json::json!(1))]);
|
|
|
|
assert!(!shutdown);
|
|
assert_eq!(outcomes(&session), vec![], "a lone cancel is answered with nothing at all");
|
|
|
|
session.batch(vec![ask(1, &uri)]);
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(1), None)],
|
|
"the dropped cancel did not survive into the next burst"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_notification_queued_ahead_of_a_request_is_applied_before_it_is_answered() {
|
|
// The invariant the burst must not break: order. A `didChange` the
|
|
// client sent before its request has to be in effect when that
|
|
// request is answered, exactly as it would be one message at a time.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelorder", ONE_MODULE);
|
|
session.batch(vec![changed(&uri, 2, "module renamed;\nendmodule\n"), ask(1, &uri)]);
|
|
|
|
let answers = session.answers();
|
|
assert_eq!(answers.len(), 1);
|
|
let result = answers[0].response_result.clone().expect("an answer");
|
|
assert_eq!(symbol_names(&result), vec!["renamed".to_string()]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_never_cancels_a_notification() {
|
|
// Notifications carry no id and get no response, so nothing in a
|
|
// burst can suppress one — whatever id the cancel names.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelnotify", ONE_MODULE);
|
|
session.batch(vec![
|
|
changed(&uri, 2, "module renamed;\nendmodule\n"),
|
|
cancel(serde_json::json!(1)),
|
|
cancel(serde_json::json!("textDocument/didChange")),
|
|
]);
|
|
|
|
assert_eq!(buffer(&session, &uri), "module renamed;\nendmodule\n");
|
|
assert_eq!(outcomes(&session), vec![], "no ids were cancelled, so no responses");
|
|
}
|
|
|
|
#[test]
|
|
fn a_shutdown_is_honoured_however_it_is_cancelled() {
|
|
// The spec lets a server answer a cancelled request normally, and for
|
|
// `shutdown` that is the only safe reading: a client that cannot shut
|
|
// the server down leaks a process. `handle_shutdown` runs before the
|
|
// cancelled set is consulted, so the cancel never gets a say.
|
|
let (_tree, mut session, _uri) = cancelling_session("cancelshutdown", ONE_MODULE);
|
|
// `handle_shutdown` waits for the `exit` that follows, and reads it off
|
|
// the *connection* — so the forwarder has to have stopped first, or it
|
|
// takes the `exit` and the handshake finds nothing. It stops at the
|
|
// first `shutdown` it forwards, so one is sent through it and waited
|
|
// for: once that message is off the queue the forwarder has already
|
|
// broken out of its loop and will never read the connection again.
|
|
// (Before 2026-08-28 the forwarder did not exist until `run`, which
|
|
// this test never calls, so the `exit` simply sat there.)
|
|
session
|
|
.client
|
|
.sender
|
|
.send(Message::Request(Request::new(
|
|
RequestId::from(99),
|
|
Shutdown::METHOD.to_string(),
|
|
Value::Null,
|
|
)))
|
|
.expect("queue a shutdown for the forwarder to stop at");
|
|
session
|
|
.server
|
|
.queue
|
|
.recv_timeout(std::time::Duration::from_secs(5))
|
|
.expect("the forwarder stops at it");
|
|
session
|
|
.client
|
|
.sender
|
|
.send(Message::Notification(Notification::new("exit".to_string(), Value::Null)))
|
|
.expect("queue exit");
|
|
|
|
let request = Message::Request(Request::new(
|
|
RequestId::from(9),
|
|
Shutdown::METHOD.to_string(),
|
|
Value::Null,
|
|
));
|
|
let shutdown = session.batch(vec![request, cancel(serde_json::json!(9))]);
|
|
|
|
assert!(shutdown, "the loop exits");
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(9), None)],
|
|
"shutdown was answered, and answered successfully"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn cancelling_one_request_twice_answers_it_once() {
|
|
let (_tree, mut session, uri) = cancelling_session("canceltwice", ONE_MODULE);
|
|
session.batch(vec![
|
|
ask(1, &uri),
|
|
cancel(serde_json::json!(1)),
|
|
cancel(serde_json::json!(1)),
|
|
ask(2, &uri),
|
|
]);
|
|
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(1), Some(CANCELLED)), (RequestId::from(2), None)],
|
|
"the second cancel changed nothing — id 2 is not collateral damage"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_id_may_be_a_number_or_a_string() {
|
|
// The protocol allows both, and a client that numbers its requests
|
|
// with strings is not exotic.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelstring", ONE_MODULE);
|
|
let request = Message::Request(Request::new(
|
|
RequestId::from("abc".to_string()),
|
|
DocumentSymbolRequest::METHOD.to_string(),
|
|
serde_json::json!({ "textDocument": { "uri": &uri } }),
|
|
));
|
|
session.batch(vec![request, cancel(serde_json::json!("abc"))]);
|
|
assert_eq!(outcomes(&session), vec![(RequestId::from("abc".to_string()), Some(CANCELLED))]);
|
|
|
|
session.batch(vec![ask(7, &uri), cancel(serde_json::json!(7))]);
|
|
assert_eq!(outcomes(&session), vec![(RequestId::from(7), Some(CANCELLED))]);
|
|
|
|
// A string that spells a number is not the number: `RequestId` keeps
|
|
// the two apart, and so must the cancel.
|
|
session.batch(vec![ask(8, &uri), cancel(serde_json::json!("8"))]);
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(8), None)],
|
|
"the string \"8\" does not cancel the number 8"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_malformed_cancel_is_dropped_rather_than_guessed_at() {
|
|
let (_tree, mut session, uri) = cancelling_session("cancelmalformed", ONE_MODULE);
|
|
let broken = Message::Notification(Notification::new(
|
|
Cancel::METHOD.to_string(),
|
|
serde_json::json!({ "id": { "nested": true } }),
|
|
));
|
|
session.batch(vec![ask(1, &uri), broken]);
|
|
|
|
assert_eq!(outcomes(&session), vec![(RequestId::from(1), None)]);
|
|
}
|
|
|
|
#[test]
|
|
fn is_shutdown_matches_only_the_shutdown_request_never_a_notification_of_the_same_name() {
|
|
// The forwarder stops forwarding the moment it sees this return
|
|
// `true`. A malformed or adversarial client sending a *notification*
|
|
// named "shutdown" must not trip it — that would silently stall the
|
|
// whole backlog, with `handle_shutdown` never having run to explain
|
|
// why.
|
|
let request = Message::Request(Request::new(
|
|
RequestId::from(1),
|
|
Shutdown::METHOD.to_string(),
|
|
Value::Null,
|
|
));
|
|
let notification =
|
|
Message::Notification(Notification::new(Shutdown::METHOD.to_string(), Value::Null));
|
|
let other_request = Message::Request(Request::new(
|
|
RequestId::from(2),
|
|
DocumentSymbolRequest::METHOD.to_string(),
|
|
Value::Null,
|
|
));
|
|
|
|
assert!(is_shutdown(&request));
|
|
assert!(
|
|
!is_shutdown(¬ification),
|
|
"a notification named shutdown is not the shutdown request"
|
|
);
|
|
assert!(!is_shutdown(&other_request));
|
|
}
|
|
|
|
#[test]
|
|
fn burst_collects_everything_already_queued_in_arrival_order() {
|
|
// `burst` is what turns "one blocking receive" into "the whole
|
|
// backlog" — the only place FIFO order could be broken. Nothing here
|
|
// touches `Server`, so the check is on the free function alone.
|
|
let (sender, receiver) = std::sync::mpsc::channel();
|
|
sender
|
|
.send(Message::Notification(Notification::new("second".to_string(), Value::Null)))
|
|
.expect("send");
|
|
sender
|
|
.send(Message::Notification(Notification::new("third".to_string(), Value::Null)))
|
|
.expect("send");
|
|
|
|
let first = Message::Notification(Notification::new("first".to_string(), Value::Null));
|
|
let batch = burst(&receiver, first);
|
|
|
|
let methods: Vec<&str> = batch
|
|
.iter()
|
|
.map(|message| match message {
|
|
Message::Notification(notification) => notification.method.as_str(),
|
|
_ => "?",
|
|
})
|
|
.collect();
|
|
assert_eq!(
|
|
methods,
|
|
vec!["first", "second", "third"],
|
|
"the blocked-for message first, then the backlog, in order"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_forwarder_stops_at_shutdown_and_leaves_what_follows_for_handle_shutdown() {
|
|
// The handoff this whole feature depends on: once the forwarder has
|
|
// pushed a `shutdown` request onto the queue, it must not call `recv`
|
|
// on `connection.receiver` again — that channel is where
|
|
// `handle_shutdown` reads the `exit` that follows. An eager forwarder
|
|
// would steal it and strand the handshake for thirty seconds.
|
|
let (_tree, session, _uri) = cancelling_session("forwarderstop", ONE_MODULE);
|
|
let shutdown_id = RequestId::from(9);
|
|
session
|
|
.client
|
|
.sender
|
|
.send(Message::Request(Request::new(
|
|
shutdown_id.clone(),
|
|
Shutdown::METHOD.to_string(),
|
|
Value::Null,
|
|
)))
|
|
.expect("queue shutdown");
|
|
session
|
|
.client
|
|
.sender
|
|
.send(Message::Notification(Notification::new("exit".to_string(), Value::Null)))
|
|
.expect("queue exit");
|
|
|
|
// The server's own queue — the one the forwarder started at
|
|
// construction is filling — rather than a second one: two forwarders
|
|
// over one connection would each take half the messages.
|
|
let queue = &session.server.queue;
|
|
let forwarded =
|
|
queue.recv_timeout(std::time::Duration::from_secs(1)).expect("shutdown is forwarded");
|
|
assert!(matches!(&forwarded, Message::Request(request) if request.id == shutdown_id));
|
|
|
|
// Give the forwarder thread every chance to (wrongly) keep reading
|
|
// before asserting that it did not.
|
|
std::thread::sleep(std::time::Duration::from_millis(50));
|
|
assert!(
|
|
queue.try_recv().is_err(),
|
|
"the forwarder must not forward anything after the shutdown request"
|
|
);
|
|
|
|
// The `exit` is still sitting on the real connection, unread — right
|
|
// where `handle_shutdown` looks for it.
|
|
let direct = session
|
|
.server
|
|
.connection
|
|
.receiver
|
|
.recv_timeout(std::time::Duration::from_secs(1))
|
|
.expect("exit is still there for handle_shutdown to read");
|
|
assert!(
|
|
matches!(&direct, Message::Notification(notification) if notification.method == "exit")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_cancel_naming_a_server_initiated_id_does_not_touch_response_routing() {
|
|
// `lucisv-0`, `lucisv-1`, … are ids the server hands out for requests
|
|
// it sends *to* the client (`applyEdit`, watcher registration,
|
|
// progress `create`) — they come back as a `Message::Response`, a
|
|
// different match arm entirely from the `Message::Request`s a burst
|
|
// scans for cancellable ids. A client cancelling one by name has
|
|
// nothing to reach.
|
|
let (_tree, mut session, uri) = cancelling_session("cancelserverid", ONE_MODULE);
|
|
let server_id = session.server.send_request("workspace/applyEdit", serde_json::json!({}));
|
|
assert_eq!(server_id, RequestId::from("lucisv-0".to_string()));
|
|
|
|
session.batch(vec![ask(1, &uri), cancel(serde_json::json!("lucisv-0"))]);
|
|
assert_eq!(
|
|
outcomes(&session),
|
|
vec![(RequestId::from(1), None)],
|
|
"the client's own request was untouched by a cancel naming a server-initiated id"
|
|
);
|
|
|
|
// The server-initiated request can still be answered normally — the
|
|
// cancel did not intercept it.
|
|
session.answer(server_id, Value::Null);
|
|
}
|