From 3c9b6f0e22ebbf7bf64947df7e3811a7ae7c4260 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:47:52 +1100 Subject: [PATCH 01/15] Move memory-lifetime rules into the spec; delete the finished leak plans; drop timing asserts from .NET endpoint tests The leak plans' checklists were complete. Their durable rules and IDs now live in lspkit-spec.md [MEM-LIFETIME] (MEM-FIX/MEM-AUDIT/MEM-060 citations renamed). The engine endpoint test proves the pre-connect refusal by its message instead of elapsed < 5s. --- AGENTS.md | 2 +- .../lsp-profiler-dotnet/tests/dotnet_e2e.rs | 28 +- .../examples/diagnostic-reference/analysis.rs | 2 +- .../examples/diagnostic-reference/ingress.rs | 2 +- .../session/admit_retention.rs | 2 +- .../diagnostic-reference/session/handlers.rs | 4 +- .../session/memory_retention.rs | 2 +- .../session/outbox_retention.rs | 2 +- .../session/task_retention.rs | 2 +- .../diagnostic-reference/session/tasks.rs | 2 +- .../examples/diagnostic-reference/wire.rs | 2 +- crates/lspkit-server/src/diagnostics.rs | 2 +- crates/lspkit-server/src/dispatch.rs | 2 +- .../src/dispatch/cancellation.rs | 2 +- crates/lspkit-server/src/dispatch/queued.rs | 2 +- crates/lspkit-server/src/live/scheduler.rs | 2 +- crates/lspkit-server/tests/diagnostics.rs | 2 +- crates/lspkit-server/tests/dispatch.rs | 2 +- .../lspkit-server/tests/dispatch_heap_leak.rs | 2 +- .../tests/dispatch_memory_retention.rs | 6 +- .../lspkit-server/tests/jsonrpc_frame_heap.rs | 2 +- crates/lspkit-server/tests/live_scheduler.rs | 2 +- .../lspkit-server/tests/outbound_heap_leak.rs | 2 +- .../lspkit-server/tests/pull_forget_heap.rs | 2 +- crates/lspkit/Cargo.toml | 2 +- crates/lspkit/src/sidecar/correlator.rs | 2 +- crates/lspkit/src/sidecar/inbound.rs | 4 +- crates/lspkit/src/treesitter/cache.rs | 2 +- crates/lspkit/tests/sidecar_correlator.rs | 2 +- crates/lspkit/tests/sidecar_heap_leak.rs | 2 +- .../lspkit/tests/sidecar_memory_retention.rs | 2 +- .../tests/sidecar_notification_bound.rs | 2 +- crates/lspkit/tests/treesitter_cache.rs | 2 +- docs/plans/memory-leak-audit-evidence.md | 291 -------------- docs/plans/memory-leak-audit.md | 95 ----- docs/plans/memory-leak-fix-evidence.md | 363 ------------------ docs/plans/memory-leak-fixes-0.6.0.md | 50 --- docs/plans/memory-leak-fixes.md | 53 --- docs/specs/LSP-PROFILER-SPEC.md | 2 +- docs/specs/lspkit-spec.md | 19 +- .../lsp-profiler-ui/src/bounded-ledger.ts | 2 +- test-support/state.rs | 2 +- 42 files changed, 65 insertions(+), 912 deletions(-) delete mode 100644 docs/plans/memory-leak-audit-evidence.md delete mode 100644 docs/plans/memory-leak-audit.md delete mode 100644 docs/plans/memory-leak-fix-evidence.md delete mode 100644 docs/plans/memory-leak-fixes-0.6.0.md delete mode 100644 docs/plans/memory-leak-fixes.md diff --git a/AGENTS.md b/AGENTS.md index 0875549..bb861df 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,7 +34,7 @@ Reusable Rust components for building language servers, plus the shared CPU and ## Memory lifetimes ๐Ÿ›‘ -Language servers run for days, so a retained allocation is a production bug. These rules come from the leaks fixed under `[MEM-FIX-SCOPE]` ([plan](docs/plans/memory-leak-fixes.md)); review every change against them. +Language servers run for days, so a retained allocation is a production bug. These rules come from the leaks fixed under `[MEM-LIFETIME]` ([spec](docs/specs/lspkit-spec.md#mem-lifetime-memory-lifetimes)); review every change against them. - **No ownership cycles.** A handler, callback or spawned task never captures an owner of the registry that stores it (`Server`, `Dispatcher`, correlator). Capture an `Arc` of only the data it needs; back-references are `Weak` and get upgraded per call. - **Every registration has an RAII owner.** Pending-response entries, cancellation controls, watcher tasks and subscriptions are removed by a guard's `Drop`. That includes error, rejection, cancellation and abandoned-future paths. Never rely on a later sweep or count call. diff --git a/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs b/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs index 8b32719..c2efd51 100644 --- a/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs +++ b/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs @@ -219,6 +219,11 @@ async fn memory_profiling_sees_the_leak_its_root_and_the_collection() { service.shutdown().await; } +/// The engine's refusal when the target has no diagnostics endpoint. Only the +/// pre-connect check reports it; a failed connection reports the runtime's own +/// error ([PROF-DOTNET-ENGINE-ENDPOINT]). +const NO_ENDPOINT: &str = "has no .NET diagnostics endpoint"; + async fn error_code(service: &ProfilerService, method: &str, params: Value) -> i64 { match service.handle(method, params).await { Ok(value) => panic!("{method} must be refused, got {value}"), @@ -250,9 +255,8 @@ async fn a_pid_that_names_no_process_is_process_not_found_everywhere() { } /// A live process no runtime recognises (this test binary) is `UNSUPPORTED_RUNTIME` -/// for every PID-taking command, answered well under the engine's connect timeout: -/// the engine decides "no .NET diagnostics endpoint" up front ([PROF-ERRORS], -/// [PROF-DOTNET-ENGINE-ENDPOINT]). Kill refuses it โ€” kill can never reach a +/// for every PID-taking command ([PROF-ERRORS]); the engine-level test below proves +/// the refusal comes before any connection ([PROF-DOTNET-ENGINE-ENDPOINT]). Kill refuses it โ€” kill can never reach a /// process outside the profiler's reach. #[tokio::test(flavor = "multi_thread")] async fn a_live_process_no_runtime_recognises_is_unsupported_and_never_killed() { @@ -267,28 +271,20 @@ async fn a_live_process_no_runtime_recognises_is_unsupported_and_never_killed() error_code(&service, methods::MEMORY_START, own.clone()).await, -32002 ); - let started = tokio::time::Instant::now(); assert_eq!(error_code(&service, methods::CPU_START, own).await, -32002); - assert!( - started.elapsed() < Duration::from_secs(5), - "the engine answers without waiting for a connect timeout: {:?}", - started.elapsed() - ); service.shutdown().await; } /// The engine itself refuses a live process with no .NET diagnostics endpoint as -/// `UnsupportedRuntime` immediately, before any connection attempt +/// `UnsupportedRuntime` before any connection attempt /// ([PROF-DOTNET-ENGINE-ENDPOINT]). Drives the engine binary directly so the /// endpoint decision is proven at the engine boundary, not through the service. #[tokio::test(flavor = "multi_thread")] -async fn the_engine_refuses_a_process_without_a_dotnet_endpoint_immediately() { - let started = std::time::Instant::now(); +async fn the_engine_refuses_a_process_without_a_dotnet_endpoint_before_connecting() { let output = std::process::Command::new(engine()) .args(["cpu", "--pid", &std::process::id().to_string()]) .output() .expect("run the engine"); - let elapsed = started.elapsed(); let stdout = String::from_utf8_lossy(&output.stdout); let last: Value = serde_json::from_str(stdout.lines().last().unwrap_or_default()) .unwrap_or_else(|error| panic!("one protocol line expected ({error}): {stdout}")); @@ -296,8 +292,10 @@ async fn the_engine_refuses_a_process_without_a_dotnet_endpoint_immediately() { assert_eq!(last["kind"], "failure", "{last}"); assert_eq!(last["error_kind"], "UnsupportedRuntime", "{last}"); assert!( - elapsed < Duration::from_secs(5), - "the engine answers without waiting for a connect timeout: {elapsed:?}" + last["message"] + .as_str() + .is_some_and(|m| m.contains(NO_ENDPOINT)), + "decided before any connection attempt: {last}" ); } diff --git a/crates/lspkit-server/examples/diagnostic-reference/analysis.rs b/crates/lspkit-server/examples/diagnostic-reference/analysis.rs index 4a5f5ab..09647e3 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/analysis.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/analysis.rs @@ -76,7 +76,7 @@ fn document_text(sources: &Sources, uri: &str) -> Result } /// Whether the session still tracks `uri`: an open document, or a JSON file -/// in the watched root, as the workspace scan lists them. [MEM-060-PULL-ADMIT] +/// in the watched root, as the workspace scan lists them. [MEM-LIFETIME-PULL-ADMIT] pub(super) fn tracked(sources: &Sources, uri: &str) -> bool { let open = sources .documents diff --git a/crates/lspkit-server/examples/diagnostic-reference/ingress.rs b/crates/lspkit-server/examples/diagnostic-reference/ingress.rs index cc092b2..b2e3c6e 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/ingress.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/ingress.rs @@ -6,7 +6,7 @@ use serde_json::Value; /// Register before preparation; the prepared future adopts this queued /// control. Hold the guard until then: dropping it releases a request that -/// was never prepared. [MEM-060-QUEUED-REQUEST] +/// was never prepared. [MEM-LIFETIME-QUEUED-REQUEST] pub(super) fn register( dispatcher: &Dispatcher, id: RequestId, diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs index cd3b637..100dd07 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs @@ -1,4 +1,4 @@ -//! [MEM-060-PULL-ADMIT]: a pull admits a Salsa input and memo for its URI. +//! [MEM-LIFETIME-PULL-ADMIT]: a pull admits a Salsa input and memo for its URI. //! Once the URI is neither open nor a JSON file in the watched root, nothing //! will close it, so the session must release it itself. diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs index 7569ede..857cf37 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs @@ -56,7 +56,7 @@ impl Drop for HandlerState { } } -/// Register handlers without retaining their own registry. [MEM-FIX-SCOPE] +/// Register handlers without retaining their own registry. [MEM-LIFETIME] pub(super) fn register_handlers(server: &Server) { let state = Arc::new(HandlerState { sources: server.sources.clone(), @@ -90,7 +90,7 @@ fn register_metadata(server: &Server, state: Arc) { } /// Release `uri`'s Salsa input and memo unless the session still tracks it: -/// nothing else would ever close it. [MEM-060-PULL-ADMIT] +/// nothing else would ever close it. [MEM-LIFETIME-PULL-ADMIT] pub(crate) fn release_untracked(sources: &Sources, pull: &PullStore, uri: &str) { if analysis::tracked(sources, uri) { return; diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs index e988eac..5939902 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs @@ -1,4 +1,4 @@ -//! [MEM-AUDIT-SCOPE]: isolate actual reference-server ownership without process-exit masking. +//! [MEM-LIFETIME]: isolate actual reference-server ownership without process-exit masking. use super::{handle_notification, register_handlers, Outbox, Server}; use lspkit_server::Message; diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs index 62cb5d1..cfa91c1 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs @@ -1,4 +1,4 @@ -//! [MEM-060-EXAMPLE-OUTBOX]: while the editor is not reading stdout, every +//! [MEM-LIFETIME-EXAMPLE-OUTBOX]: while the editor is not reading stdout, every //! workspace change must not queue another notification behind the pump. use crate::wire::Outbox; diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs index e80185a..5aa9f5f 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs @@ -1,4 +1,4 @@ -//! [MEM-060-EXAMPLE-TASKS]: a request still in flight when the session ends +//! [MEM-LIFETIME-EXAMPLE-TASKS]: a request still in flight when the session ends //! must not keep the session's documents alive. use std::sync::Arc; diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs b/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs index cdc27e8..3b3ba8b 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs @@ -1,4 +1,4 @@ -//! The session's request tasks. [MEM-060-EXAMPLE-TASKS] +//! The session's request tasks. [MEM-LIFETIME-EXAMPLE-TASKS] //! //! Every request task is owned: finished tasks are reaped as new ones start, //! and dropping the owner at session end aborts any still in flight, so a diff --git a/crates/lspkit-server/examples/diagnostic-reference/wire.rs b/crates/lspkit-server/examples/diagnostic-reference/wire.rs index 176575f..949e4ca 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/wire.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/wire.rs @@ -56,7 +56,7 @@ impl Outbox { /// Tell the editor the workspace changed. One queued notification covers /// every change until the pump dequeues it, so a stalled editor never - /// accumulates them. [MEM-060-EXAMPLE-OUTBOX] + /// accumulates them. [MEM-LIFETIME-EXAMPLE-OUTBOX] pub(super) fn send_workspace_changed(&self) -> Result<(), EgressClosed> { if self.change_queued.swap(true, Ordering::AcqRel) { return Ok(()); diff --git a/crates/lspkit-server/src/diagnostics.rs b/crates/lspkit-server/src/diagnostics.rs index fc93460..2fa7a0b 100644 --- a/crates/lspkit-server/src/diagnostics.rs +++ b/crates/lspkit-server/src/diagnostics.rs @@ -131,7 +131,7 @@ impl DiagnosticsBus { /// Attach a sink for as long as the returned guard lives: dropping it /// detaches the sink, so a per-connection consumer never accumulates on - /// a long-lived bus. [MEM-060-DIAGNOSTICS-SINK] + /// a long-lived bus. [MEM-LIFETIME-DIAGNOSTICS-SINK] pub fn attach_owned(&self, sink: Arc) -> SinkRegistration { let registered = Arc::downgrade(&sink); let _count = self.attach(sink); diff --git a/crates/lspkit-server/src/dispatch.rs b/crates/lspkit-server/src/dispatch.rs index 4418c5b..d63a133 100644 --- a/crates/lspkit-server/src/dispatch.rs +++ b/crates/lspkit-server/src/dispatch.rs @@ -183,7 +183,7 @@ impl Dispatcher { /// Release ingress-only state for a rejected request. A control still held by /// a prepared request with the same id stays registered and cancellable. - /// [LSP-PULL-CANCELLATION] [MEM-FIX-SCOPE] + /// [LSP-PULL-CANCELLATION] [MEM-LIFETIME] /// /// # Errors /// Returns [`DispatchError::RegistrationPoisoned`] if the registry lock is poisoned. diff --git a/crates/lspkit-server/src/dispatch/cancellation.rs b/crates/lspkit-server/src/dispatch/cancellation.rs index 5bebab7..a542747 100644 --- a/crates/lspkit-server/src/dispatch/cancellation.rs +++ b/crates/lspkit-server/src/dispatch/cancellation.rs @@ -46,7 +46,7 @@ impl CancellationHandle { /// [`register`](Self::register) `id` and own the queued control: dropping /// the guard releases it unless a prepared request adopted it. - /// [MEM-060-QUEUED-REQUEST] + /// [MEM-LIFETIME-QUEUED-REQUEST] /// /// # Errors /// Returns a registration error if the registry is unavailable. diff --git a/crates/lspkit-server/src/dispatch/queued.rs b/crates/lspkit-server/src/dispatch/queued.rs index ad6197e..978a771 100644 --- a/crates/lspkit-server/src/dispatch/queued.rs +++ b/crates/lspkit-server/src/dispatch/queued.rs @@ -1,4 +1,4 @@ -//! Owned ingress registration. [MEM-060-QUEUED-REQUEST] +//! Owned ingress registration. [MEM-LIFETIME-QUEUED-REQUEST] use std::sync::{Arc, Weak}; diff --git a/crates/lspkit-server/src/live/scheduler.rs b/crates/lspkit-server/src/live/scheduler.rs index 41eb160..bc3b569 100644 --- a/crates/lspkit-server/src/live/scheduler.rs +++ b/crates/lspkit-server/src/live/scheduler.rs @@ -111,7 +111,7 @@ impl Burst { } /// Coalesce events until the source ends or the consumer drops the triggers; -/// the task never outlives its consumer. [MEM-060-SCHEDULER-TASK] +/// the task never outlives its consumer. [MEM-LIFETIME-SCHEDULER-TASK] async fn run( mut events_rx: mpsc::UnboundedReceiver, tx: mpsc::UnboundedSender, diff --git a/crates/lspkit-server/tests/diagnostics.rs b/crates/lspkit-server/tests/diagnostics.rs index 7acce58..9061191 100644 --- a/crates/lspkit-server/tests/diagnostics.rs +++ b/crates/lspkit-server/tests/diagnostics.rs @@ -185,7 +185,7 @@ fn batch_new_preserves_fields() { assert_eq!(b.diagnostics.len(), 2); } -/// [MEM-060-DIAGNOSTICS-SINK] A consumer that attaches per connection must not +/// [MEM-LIFETIME-DIAGNOSTICS-SINK] A consumer that attaches per connection must not /// stay attached, with everything it owns, after its connection ends. #[tokio::test] async fn owned_sinks_detach_when_their_registration_drops() { diff --git a/crates/lspkit-server/tests/dispatch.rs b/crates/lspkit-server/tests/dispatch.rs index 715cb99..63ca1ce 100644 --- a/crates/lspkit-server/tests/dispatch.rs +++ b/crates/lspkit-server/tests/dispatch.rs @@ -200,7 +200,7 @@ async fn dispatcher_clone_shares_handlers() { ); } -/// [LSP-PULL-CANCELLATION] [MEM-FIX-SCOPE]: rejecting an unknown method that reuses an +/// [LSP-PULL-CANCELLATION] [MEM-LIFETIME]: rejecting an unknown method that reuses an /// in-flight id releases only orphaned ingress state, never the live request's control. #[tokio::test] async fn unknown_method_with_inflight_duplicate_id_keeps_inflight_cancellable() { diff --git a/crates/lspkit-server/tests/dispatch_heap_leak.rs b/crates/lspkit-server/tests/dispatch_heap_leak.rs index 87a608e..55510d9 100644 --- a/crates/lspkit-server/tests/dispatch_heap_leak.rs +++ b/crates/lspkit-server/tests/dispatch_heap_leak.rs @@ -1,4 +1,4 @@ -//! [LSP-PULL-CANCELLATION] [MEM-FIX-SCOPE]: terminal errors release actual heap allocations. +//! [LSP-PULL-CANCELLATION] [MEM-LIFETIME]: terminal errors release actual heap allocations. use lspkit_server::{DispatchError, Dispatcher, HandlerResult, RequestId, RequestInterest}; use serde_json::Value; diff --git a/crates/lspkit-server/tests/dispatch_memory_retention.rs b/crates/lspkit-server/tests/dispatch_memory_retention.rs index 928c892..436b332 100644 --- a/crates/lspkit-server/tests/dispatch_memory_retention.rs +++ b/crates/lspkit-server/tests/dispatch_memory_retention.rs @@ -1,4 +1,4 @@ -//! [LSP-PULL-CANCELLATION] and [MEM-AUDIT-SCOPE]: terminal request ownership. +//! [LSP-PULL-CANCELLATION] and [MEM-LIFETIME]: terminal request ownership. //! Each abandoned ingress control owns a 64 KiB input string. The failed //! assertion observes retained registry entries, not transient RSS or errors. @@ -99,7 +99,7 @@ fn queue(handle: &CancellationHandle) -> Result, DispatchErro .collect() } -/// [MEM-060-QUEUED-REQUEST] A consumer that rejects a queued request itself. +/// [MEM-LIFETIME-QUEUED-REQUEST] A consumer that rejects a queued request itself. #[test] fn queued_requests_abandoned_before_prepare_release_their_interest() { let dispatcher = known_dispatcher(); @@ -110,7 +110,7 @@ fn queued_requests_abandoned_before_prepare_release_their_interest() { assert_released(&dispatcher, &known_dispatcher()); } -/// [MEM-060-QUEUED-REQUEST] A held guard keeps a queued cancellation for the +/// [MEM-LIFETIME-QUEUED-REQUEST] A held guard keeps a queued cancellation for the /// prepared request, which then releases it on completion. #[tokio::test] async fn a_held_queue_guard_keeps_queued_cancellation_until_prepared() { diff --git a/crates/lspkit-server/tests/jsonrpc_frame_heap.rs b/crates/lspkit-server/tests/jsonrpc_frame_heap.rs index f370b60..8b934e8 100644 --- a/crates/lspkit-server/tests/jsonrpc_frame_heap.rs +++ b/crates/lspkit-server/tests/jsonrpc_frame_heap.rs @@ -1,4 +1,4 @@ -//! [JSONRPC-FRAME-LIMIT] [MEM-FIX-SCOPE] A body shorter than its claimed length +//! [JSONRPC-FRAME-LIMIT] [MEM-LIFETIME] A body shorter than its claimed length //! costs only the bytes that arrived. Single-test binary: the instrumented //! allocator sees no sibling tests. diff --git a/crates/lspkit-server/tests/live_scheduler.rs b/crates/lspkit-server/tests/live_scheduler.rs index 1dec658..b7467cc 100644 --- a/crates/lspkit-server/tests/live_scheduler.rs +++ b/crates/lspkit-server/tests/live_scheduler.rs @@ -197,7 +197,7 @@ async fn second_burst_after_first_fire_yields_second_trigger() { assert!(CAP > QUIET); } -/// [MEM-060-SCHEDULER-TASK] The scheduler task belongs to its consumer: once +/// [MEM-LIFETIME-SCHEDULER-TASK] The scheduler task belongs to its consumer: once /// the trigger receiver is dropped it must exit, releasing the event /// receiver, even while no event ever arrives. #[tokio::test(flavor = "current_thread", start_paused = true)] diff --git a/crates/lspkit-server/tests/outbound_heap_leak.rs b/crates/lspkit-server/tests/outbound_heap_leak.rs index 3e0776c..170848e 100644 --- a/crates/lspkit-server/tests/outbound_heap_leak.rs +++ b/crates/lspkit-server/tests/outbound_heap_leak.rs @@ -1,4 +1,4 @@ -//! [LSP-OUTBOUND] [MEM-FIX-SCOPE]: every server-request outcome frees its heap. +//! [LSP-OUTBOUND] [MEM-LIFETIME]: every server-request outcome frees its heap. //! Single-test binary: the instrumented allocator sees no sibling tests. use lspkit_server::{JsonRpcError, Message, Outbound, OutboundError, RequestId}; diff --git a/crates/lspkit-server/tests/pull_forget_heap.rs b/crates/lspkit-server/tests/pull_forget_heap.rs index 95b8c2a..3e04d2b 100644 --- a/crates/lspkit-server/tests/pull_forget_heap.rs +++ b/crates/lspkit-server/tests/pull_forget_heap.rs @@ -1,4 +1,4 @@ -//! [LSP-PULL-FORGET] [MEM-FIX-SCOPE] Open/forget cycles of distinct URIs leave +//! [LSP-PULL-FORGET] [MEM-LIFETIME] Open/forget cycles of distinct URIs leave //! no heap behind. Single-test binary: the instrumented allocator sees no //! sibling tests. #![cfg(feature = "pull")] diff --git a/crates/lspkit/Cargo.toml b/crates/lspkit/Cargo.toml index e58670a..74635fb 100644 --- a/crates/lspkit/Cargo.toml +++ b/crates/lspkit/Cargo.toml @@ -29,7 +29,7 @@ thiserror.workspace = true tokio.workspace = true tokio-util.workspace = true toml.workspace = true -# Dropped sidecar notifications are logged. [MEM-060-SIDECAR-NOTIFICATIONS] +# Dropped sidecar notifications are logged. [MEM-LIFETIME-SIDECAR-NOTIFICATIONS] tracing.workspace = true # Optional: tree-sitter compiles a C runtime, so LSP-only consumers opt in. diff --git a/crates/lspkit/src/sidecar/correlator.rs b/crates/lspkit/src/sidecar/correlator.rs index 407069f..be6e496 100644 --- a/crates/lspkit/src/sidecar/correlator.rs +++ b/crates/lspkit/src/sidecar/correlator.rs @@ -48,7 +48,7 @@ pub enum CorrelationError { type Pending = Mutex>>; /// A response future that releases its registration immediately when abandoned. -/// [MEM-FIX-SCOPE] +/// [MEM-LIFETIME] #[derive(Debug)] pub struct PendingResponse { /// The registration released by this receiver. diff --git a/crates/lspkit/src/sidecar/inbound.rs b/crates/lspkit/src/sidecar/inbound.rs index 27f0f4d..84014c4 100644 --- a/crates/lspkit/src/sidecar/inbound.rs +++ b/crates/lspkit/src/sidecar/inbound.rs @@ -13,7 +13,7 @@ use tokio::task::JoinHandle; use crate::sidecar::transport::read_frame; /// Notifications buffered for a consumer that is not reading them; later ones -/// are dropped, counted and logged. [MEM-060-SIDECAR-NOTIFICATIONS] +/// are dropped, counted and logged. [MEM-LIFETIME-SIDECAR-NOTIFICATIONS] pub const NOTIFICATION_CAPACITY: usize = 1024; /// A single reader task with distinct notification and response channels. @@ -125,7 +125,7 @@ impl Channels { /// a consumer that waits only for responses would deadlock. A full buffer /// drops the notification, counts it, and warns each time the total /// reaches a power of two, so the log stays bounded under a flood. - /// [MEM-060-SIDECAR-NOTIFICATIONS] + /// [MEM-LIFETIME-SIDECAR-NOTIFICATIONS] fn notify(&self, envelope: E) { if let Err(mpsc::error::TrySendError::Full(_dropped)) = self.notifications.try_send(envelope) diff --git a/crates/lspkit/src/treesitter/cache.rs b/crates/lspkit/src/treesitter/cache.rs index 9b18d9a..9aedec6 100644 --- a/crates/lspkit/src/treesitter/cache.rs +++ b/crates/lspkit/src/treesitter/cache.rs @@ -3,7 +3,7 @@ //! Parse trees are cached by `(language_name, content_hash)`. Repeated parses //! of identical source bytes return the cached tree directly. The cache keeps //! at most [`MAX_TREES`] trees and evicts the oldest first, so editing a -//! document never leaves one tree per version behind. [MEM-060-PARSE-CACHE] +//! document never leaves one tree per version behind. [MEM-LIFETIME-PARSE-CACHE] use std::collections::hash_map::DefaultHasher; use std::collections::VecDeque; diff --git a/crates/lspkit/tests/sidecar_correlator.rs b/crates/lspkit/tests/sidecar_correlator.rs index 5dc3f0e..9d21327 100644 --- a/crates/lspkit/tests/sidecar_correlator.rs +++ b/crates/lspkit/tests/sidecar_correlator.rs @@ -193,7 +193,7 @@ impl RequestIdTestExt for RequestId { } } -/// Callers storing in-flight responses name the future through the sidecar facade. [MEM-FIX-SCOPE] +/// Callers storing in-flight responses name the future through the sidecar facade. [MEM-LIFETIME] #[tokio::test] async fn pending_response_is_nameable_from_the_sidecar_facade() { let correlator: Correlator = Correlator::new(); diff --git a/crates/lspkit/tests/sidecar_heap_leak.rs b/crates/lspkit/tests/sidecar_heap_leak.rs index 925baf1..12bef32 100644 --- a/crates/lspkit/tests/sidecar_heap_leak.rs +++ b/crates/lspkit/tests/sidecar_heap_leak.rs @@ -1,4 +1,4 @@ -//! [MEM-FIX-SCOPE]: actual allocator bytes, before any count accessor can sweep entries. +//! [MEM-LIFETIME]: actual allocator bytes, before any count accessor can sweep entries. use lspkit::sidecar::{Correlator, RequestId}; diff --git a/crates/lspkit/tests/sidecar_memory_retention.rs b/crates/lspkit/tests/sidecar_memory_retention.rs index 3d9eddb..00f0843 100644 --- a/crates/lspkit/tests/sidecar_memory_retention.rs +++ b/crates/lspkit/tests/sidecar_memory_retention.rs @@ -1,4 +1,4 @@ -//! Allocation-lifetime proofs for [MEM-AUDIT-SCOPE] in docs/plans/memory-leak-audit.md. +//! Allocation-lifetime proofs for [MEM-LIFETIME] (docs/specs/lspkit-spec.md). //! Isolated public-API tests are necessary to observe ownership without RSS noise. use lspkit::sidecar::Correlator; diff --git a/crates/lspkit/tests/sidecar_notification_bound.rs b/crates/lspkit/tests/sidecar_notification_bound.rs index d827852..f8dd97d 100644 --- a/crates/lspkit/tests/sidecar_notification_bound.rs +++ b/crates/lspkit/tests/sidecar_notification_bound.rs @@ -1,4 +1,4 @@ -//! [MEM-060-SIDECAR-NOTIFICATIONS]: a consumer that only reads responses keeps +//! [MEM-LIFETIME-SIDECAR-NOTIFICATIONS]: a consumer that only reads responses keeps //! the notification receiver alive inside `Inbound`. Unsolicited backend //! events must not queue up behind it for the life of the server, and every //! drop is counted and logged, never silent. diff --git a/crates/lspkit/tests/treesitter_cache.rs b/crates/lspkit/tests/treesitter_cache.rs index 3433d84..179cfa9 100644 --- a/crates/lspkit/tests/treesitter_cache.rs +++ b/crates/lspkit/tests/treesitter_cache.rs @@ -172,7 +172,7 @@ fn content_hash_is_hashable() { /// Edits a document this many times; every edit is new content. const EDITED_VERSIONS: usize = 512; -/// [MEM-060-PARSE-CACHE] A long session parses every edited version of every +/// [MEM-LIFETIME-PARSE-CACHE] A long session parses every edited version of every /// document. Old versions must leave the cache, or it keeps one tree per /// keystroke for the life of the server. #[test] diff --git a/docs/plans/memory-leak-audit-evidence.md b/docs/plans/memory-leak-audit-evidence.md deleted file mode 100644 index 0a3adf5..0000000 --- a/docs/plans/memory-leak-audit-evidence.md +++ /dev/null @@ -1,291 +0,0 @@ -# Memory leak audit โ€” executed evidence - -[Audit and checklist](memory-leak-audit.md) ยท 2026-11-16 - -## [MEM-AUDIT-EVIDENCE] Reproduction - -These are real test executions against unchanged production behavior. Every command exited 101 because the ownership assertion failed. The reference executable has only a `cfg(test)` module added; no retention fixes are applied. - -### Sidecar allocation ownership - -```sh -cargo test -p lspkit --test sidecar_memory_retention --locked -- --nocapture -``` - -```text -warning: unused workspace dependency `lsp-profiler-dotnet` - --> Cargo.toml:162:1 - | -162 | lsp-profiler-dotnet = { version = "0.0.0-dev", path = "crates/lsp-profiler-dotnet" } - | ^^^^^^^^^^^^^^^^^^^ - | - = note: `cargo::unused_workspace_dependencies` is set to `warn` by default -help: consider removing the workspace dependency `lsp-profiler-dotnet` -warning: unused workspace dependency `lspkit-server` - --> Cargo.toml:160:1 - | -160 | lspkit-server = { version = "0.0.0-dev", path = "crates/lspkit-server" } - | ^^^^^^^^^^^^^ - | -help: consider removing the workspace dependency `lspkit-server` -warning: workspace (manifest) generated 2 warnings - Compiling proc-macro2 v1.0.106 - Compiling quote v1.0.45 - Compiling unicode-ident v1.0.24 - Compiling libc v0.2.186 - Compiling serde_core v1.0.229 - Compiling autocfg v1.5.0 - Compiling pin-project-lite v0.2.17 - Compiling once_cell v1.21.4 - Compiling cfg-if v1.0.4 - Compiling serde v1.0.229 - Compiling tree-sitter-language v0.1.9 - Compiling parking_lot_core v0.9.12 - Compiling smallvec v1.15.1 - Compiling shlex v2.0.1 - Compiling find-msvc-tools v0.1.15 - Compiling crossbeam-utils v0.8.21 - Compiling thiserror v2.0.21 - Compiling winnow v1.0.3 - Compiling futures-core v0.3.34 - Compiling cc v1.9.0 - Compiling rustix v1.1.4 - Compiling getrandom v0.3.4 - Compiling num-traits v0.2.19 - Compiling scopeguard v1.2.0 - Compiling bytes v1.11.1 - Compiling zmij v1.0.21 - Compiling toml_parser v1.1.5+spec-1.1.0 - Compiling lock_api v0.4.14 - Compiling tracing-core v0.1.36 - Compiling str_indices v0.4.4 - Compiling toml_writer v1.1.3+spec-1.1.0 - Compiling futures-sink v0.3.34 - Compiling bitflags v2.11.1 - Compiling syn v3.0.9 - Compiling syn v2.0.117 - Compiling hashbrown v0.14.5 - Compiling serde_json v1.0.154 - Compiling ropey v1.6.1 - Compiling memchr v2.8.0 - Compiling itoa v1.0.18 - Compiling slab v0.4.12 - Compiling errno v0.3.14 - Compiling socket2 v0.6.3 - Compiling mio v1.2.0 - Compiling signal-hook-registry v1.4.8 - Compiling tree-sitter-rust v0.24.2 - Compiling futures-task v0.3.34 - Compiling fastrand v2.4.1 - Compiling rmp v0.8.15 - Compiling dashmap v6.2.1 - Compiling tempfile v3.27.0 - Compiling toml_datetime v1.1.2+spec-1.1.0 - Compiling serde_spanned v1.1.2 - Compiling toml v1.1.8+spec-1.1.0 - Compiling serde_derive v1.0.229 - Compiling thiserror-impl v2.0.21 - Compiling async-trait v0.1.92 - Compiling futures-macro v0.3.34 - Compiling futures-util v0.3.34 - Compiling tokio-macros v2.7.0 - Compiling tracing-attributes v0.1.31 - Compiling tokio v1.54.1 - Compiling tracing v0.1.44 - Compiling rmp-serde v1.3.1 - Compiling tokio-util v0.7.20 - Compiling lspkit v0.0.0-dev (/Users/christianfindlay/Documents/Code/lspkit/crates/lspkit) - Finished `test` profile [unoptimized + debuginfo] target(s) in 29.83s - Running tests/sidecar_memory_retention.rs (target/debug/build/lspkit/9697ec9472675c16/out/sidecar_memory_retention-9697ec9472675c16) - -running 3 tests - -thread 'abandoned_requests_release_their_channel_allocations' (65018485) panicked at crates/lspkit/tests/sidecar_memory_retention.rs:47:5: -assertion `left == right` failed: 32 abandoned oneshot senders still own at least 2097152 bytes of heap response slots; no receivers or request work remain - left: 32 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace - -thread 'dropped_inbound_readers_release_their_decoder_allocations' (65018486) panicked at crates/lspkit/tests/sidecar_memory_retention.rs:118:5: -assertion `left == right` failed: 32 dropped inbound readers retain 65536-byte decoder allocations after the executor becomes idle - left: 32 - right: 0 -test dropped_inbound_readers_release_their_decoder_allocations ... FAILED -test abandoned_requests_release_their_channel_allocations ... FAILED -test resolved_requests_release_their_channel_allocations ... ok - -failures: - -failures: - abandoned_requests_release_their_channel_allocations - dropped_inbound_readers_release_their_decoder_allocations - -test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.32s - -error: test failed, to rerun pass `-p lspkit --test sidecar_memory_retention` -``` - -### Dispatcher ingress ownership - -```sh -cargo test -p lspkit-server --test dispatch_memory_retention --locked -- --nocapture -``` - -```text -warning: unused workspace dependency `lsp-profiler-dotnet` - --> Cargo.toml:162:1 - | -162 | lsp-profiler-dotnet = { version = "0.0.0-dev", path = "crates/lsp-profiler-dotnet" } - | ^^^^^^^^^^^^^^^^^^^ - | - = note: `cargo::unused_workspace_dependencies` is set to `warn` by default -help: consider removing the workspace dependency `lsp-profiler-dotnet` -warning: unused workspace dependency `lspkit-server` - --> Cargo.toml:160:1 - | -160 | lspkit-server = { version = "0.0.0-dev", path = "crates/lspkit-server" } - | ^^^^^^^^^^^^^ - | -help: consider removing the workspace dependency `lspkit-server` -warning: workspace (manifest) generated 2 warnings - Compiling lspkit-server v0.0.0-dev (/Users/christianfindlay/Documents/Code/lspkit/crates/lspkit-server) - Finished `test` profile [unoptimized + debuginfo] target(s) in 0.96s - Running tests/dispatch_memory_retention.rs (target/debug/build/lspkit-server/e783b3c44f3a9847/out/dispatch_memory_retention-e783b3c44f3a9847) - -running 3 tests - -thread 'unknown_method_releases_pre_registered_interest' (64917771) panicked at crates/lspkit-server/tests/dispatch_memory_retention.rs:44:5: -assertion `left == right` failed: terminal requests retained registry entries, each owning a 65536-byte input allocation - left: "Dispatcher { handlers: 0, in_flight: 32 }" - right: "Dispatcher { handlers: 0, in_flight: 0 }" -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test unknown_method_releases_pre_registered_interest ... FAILED -test dropped_futures_release_pre_registered_interest ... ok -test completed_requests_release_pre_registered_interest ... ok - -failures: - -failures: - unknown_method_releases_pre_registered_interest - -test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s - -error: test failed, to rerun pass `-p lspkit-server --test dispatch_memory_retention` -``` - -### Actual reference server ownership - -```sh -cargo test -p lspkit-server --example diagnostic-reference --features pull --locked memory_retention -- --nocapture -``` - -```text -warning: unused workspace dependency `lsp-profiler-dotnet` - --> Cargo.toml:162:1 - | -162 | lsp-profiler-dotnet = { version = "0.0.0-dev", path = "crates/lsp-profiler-dotnet" } - | ^^^^^^^^^^^^^^^^^^^ - | - = note: `cargo::unused_workspace_dependencies` is set to `warn` by default -help: consider removing the workspace dependency `lsp-profiler-dotnet` -warning: unused workspace dependency `lspkit-server` - --> Cargo.toml:160:1 - | -160 | lspkit-server = { version = "0.0.0-dev", path = "crates/lspkit-server" } - | ^^^^^^^^^^^^^ - | -help: consider removing the workspace dependency `lspkit-server` -warning: workspace (manifest) generated 2 warnings - Blocking waiting for file lock on build directory - Compiling smallvec v1.15.1 - Compiling crossbeam-epoch v0.9.21 - Compiling tracing-core v0.1.36 - Compiling crossbeam-deque v0.8.8 - Compiling foldhash v0.2.0 - Compiling syn v3.0.9 - Compiling allocator-api2 v0.2.21 - Compiling rayon-core v1.13.0 - Compiling equivalent v1.0.2 - Compiling log v0.4.29 - Compiling portable-atomic v1.15.0 - Compiling typeid v1.0.3 - Compiling tokio v1.54.1 - Compiling objc2-core-foundation v0.3.2 - Compiling same-file v1.0.6 - Compiling bitflags v1.3.2 - Compiling regex-syntax v0.8.10 - Compiling either v1.19.0 - Compiling fluent-uri v0.1.4 - Compiling walkdir v2.5.0 - Compiling parking_lot_core v0.9.12 - Compiling ropey v1.6.1 - Compiling serde_repr v0.1.20 - Compiling crossbeam-queue v0.3.14 - Compiling dashmap v6.2.1 - Compiling hashbrown v0.17.1 - Compiling parking_lot v0.12.5 - Compiling notify-types v2.1.0 - Compiling tracing v0.1.44 - Compiling intrusive-collections v0.10.3 - Compiling thin-vec v0.2.21 - Compiling boxcar v0.2.14 - Compiling rustc-hash v2.1.3 - Compiling lazy_static v1.5.0 - Compiling inventory v0.3.25 - Compiling salsa-macro-rules v0.28.5 - Compiling sharded-slab v0.1.7 - Compiling tracing-log v0.2.0 - Compiling thread_local v1.1.9 - Compiling nu-ansi-term v0.50.3 - Compiling objc2-core-services v0.3.2 - Compiling notify v8.2.1 - Compiling hashlink v0.12.2 - Compiling indexmap v2.14.0 - Compiling rayon v1.12.0 - Compiling regex-automata v0.4.14 - Compiling matchers v0.2.0 - Compiling serde_derive v1.0.229 - Compiling thiserror-impl v2.0.21 - Compiling async-trait v0.1.92 - Compiling futures-macro v0.3.34 - Compiling salsa-macros v0.28.5 - Compiling futures-util v0.3.34 - Compiling thiserror v2.0.21 - Compiling tokio-util v0.7.20 - Compiling salsa v0.28.5 - Compiling serde v1.0.229 - Compiling rmp-serde v1.3.1 - Compiling lsp-types v0.97.0 - Compiling tracing-serde v0.2.0 - Compiling tracing-subscriber v0.3.23 - Compiling lspkit v0.0.0-dev (/Users/christianfindlay/Documents/Code/lspkit/crates/lspkit) - Compiling lspkit-server v0.0.0-dev (/Users/christianfindlay/Documents/Code/lspkit/crates/lspkit-server) - Finished `test` profile [unoptimized + debuginfo] target(s) in 55.31s - Running unittests examples/diagnostic-reference/main.rs (target/debug/build/lspkit-server/daef989524df31ce/out/diagnostic_reference-daef989524df31ce) - -running 3 tests - -thread 'session::memory_retention::registered_session_releases_its_source_allocations' (65026738) panicked at crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs:49:5: -assertion `left == right` failed: handler closures retain the source buffer after every external Server owner is dropped - left: 4 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test session::memory_retention::unregistered_session_releases_its_source_allocations ... ok - -thread 'session::memory_retention::closed_documents_release_their_source_buffers' (65026737) panicked at crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs:70:5: -assertion `left == right` failed: didClose retained complete document text buffers after all 32 documents closed - left: (32, 2097152) - right: (0, 0) -test session::memory_retention::registered_session_releases_its_source_allocations ... FAILED -test session::memory_retention::closed_documents_release_their_source_buffers ... FAILED - -failures: - -failures: - session::memory_retention::closed_documents_release_their_source_buffers - session::memory_retention::registered_session_releases_its_source_allocations - -test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s - -error: test failed, to rerun pass `-p lspkit-server --example diagnostic-reference` -``` diff --git a/docs/plans/memory-leak-audit.md b/docs/plans/memory-leak-audit.md deleted file mode 100644 index 4a9d2a0..0000000 --- a/docs/plans/memory-leak-audit.md +++ /dev/null @@ -1,95 +0,0 @@ -# Memory leak audit โ€” 2026-11-16 - -## [MEM-AUDIT-SCOPE] Outcome and proof standard - -**Five confirmed ownership leaks: three in shared library APIs, two in the actual reference LSP implementation.** Nine new tests execute successfully as binaries: five fail on retained allocations and four controls pass. [Complete failure outputs and reproduction commands](memory-leak-audit-evidence.md). - -This is the requested audit and failing-test deliverable. Production behavior is unchanged; the only addition to an existing implementation file is a `cfg(test)` module declaration. The regression tests deliberately remain red, with no ignores, expected-panic attributes, weakened assertions, or changed gates. The [fix-bug workflow](../../.agents/skills/fix-bug/SKILL.md) has its failing-test prerequisite ready for each repair. - -A leak claim requires allocations still owned after the relevant operation ends. Tests use live registry entries owning concrete heap storage, `Weak::strong_count`, and retained `String` lengths. None uses RSS growth, arbitrary sleeps, compilation failures, or fabricated protocol responses as proof. Small isolation tests are necessary here: exiting an LSP child would let the OS reclaim its memory and hide the very ownership defect under examination. - -## [MEM-AUDIT-CHECKLIST] Mandatory checks - -- [x] Read repository rules, relevant specifications, test-first workflow, and PiNotes model guidance. -- [x] Select installed current pi models and run bounded Rust, profiler/.NET, UI, and consumer-trace reviews. -- [x] Independently inspect candidates, lifecycle implementations, and existing tests. -- [x] Execute deterministic failing tests for every confirmed leak before changing production behavior. -- [x] Record exact failures, ownership chains, controls, and unproven/rejected candidates. -- [x] Run static analysis, existing regressions, and Deslop without weakening gates. -- [x] Record verified model results in PiNotes. -- [x] Remove audit temporary files and unnecessary owned build output, stop owned processes, and check disk space. - -## [MEM-AUDIT-CORRELATOR] Abandoned requests retain channel allocations - -**Shared library:** [sidecar/correlator.rs](../../crates/lspkit/src/sidecar/correlator.rs), `Correlator::register`, `pending`, and `resolve`. - -`Correlator.pending โ†’ oneshot::Sender โ†’ Arc> โ†’ inline Option` remains owned after the receiver is dropped. Only a response removes the entry; there is no abandonment or disconnection cleanup. Timeouts/cancellation followed by a backend that never replies accumulate entries for the correlator's lifetime. - -[Proof](../../crates/lspkit/tests/sidecar_memory_retention.rs): `abandoned_requests_release_their_channel_allocations` registers and drops 32 receivers with a 65,536-byte inline response type. It fails with **32 retained channel allocations, at least 2,097,152 bytes of response storage**, expected zero. The byte figure is the response slots alone, excluding channel/map overhead; it does not imply ordinary callers always use this payload size. The completed-response control runs 32 exchanges and returns to zero each time. - -An initial waker-based hypothesis was rejected: Tokio released the waker allocations, while the channel slots remained. The final proof measures the retained slots, not that disproved hypothesis. - -## [MEM-AUDIT-INBOUND] Dropped readers leave detached tasks owning decoder state - -**Shared library:** [sidecar/inbound.rs](../../crates/lspkit/src/sidecar/inbound.rs), `Inbound.task` and `read_loop`; violates [LSP-PULL-TRANSPORT](../specs/LSP-PULL-SPEC.md#lsp-pull-transport-idle-compiler-events). - -Dropping `Inbound` drops a Tokio `JoinHandle`, which detaches its task. The pending read still owns its reader, channels, decoder, and notification classifier. A peer that stays open and silent keeps that state indefinitely. Failed sends on notification-only traffic are also ignored. - -[Proof](../../crates/lspkit/tests/sidecar_memory_retention.rs): `dropped_inbound_readers_release_their_decoder_allocations` drives 32 real framed exchanges through Tokio streams. Each decoder exclusively owns a 64 KiB `Arc>`; the test retains only weak observers and peer writers. After dropping all `Inbound` owners, the current-thread runtime's `on_thread_park` event proves the ready queue has drained. **All 32 decoder allocations remain alive (2 MiB)**, expected zero. Destroying the runtime releases every probe, positively identifying the task as the retaining owner. No wall-clock delay or guessed yield count is used. - -## [MEM-AUDIT-DISPATCH] Terminal preparation errors retain ingress registrations - -**Shared library:** [dispatch.rs](../../crates/lspkit-server/src/dispatch.rs), `Dispatcher::prepare`, and [dispatch/cancellation.rs](../../crates/lspkit-server/src/dispatch/cancellation.rs), `CancellationHandle::register`; violates [LSP-PULL-CANCELLATION](../specs/LSP-PULL-SPEC.md#lsp-pull-cancellation-request-lifetime). - -Ingress registration stores `RequestControl` and its owned interest strings before dispatch. An unknown-method error returns before creating `RegistrationGuard`. Neither that error nor `cancel_all()` removes the terminal registration. Plain `prepare` without ingress registration does not have this defect. - -[Proof](../../crates/lspkit-server/tests/dispatch_memory_retention.rs): `unknown_method_releases_pre_registered_interest` creates 32 ingress controls, each with its own 64 KiB input string, observes all 32 terminal errors, and cancels everything. The registry still reports **`in_flight: 32` instead of zero**, retaining at least 2 MiB of input strings. Separate controls pre-register the same 32 requests and release all entries after completion or after dropping unpolled prepared futures. - -## [MEM-AUDIT-CLOSED-DOCUMENTS] Reference LSP ignores document closure - -**Reference implementation, not a generic VFS defect:** [diagnostic-reference/session.rs](../../crates/lspkit-server/examples/diagnostic-reference/session.rs), `handle_notification` and `store_document`. - -The notification handler inserts full document text for `didOpen`/`didChange` but never handles `textDocument/didClose`. The source map therefore owns every closed document's last text for the session lifetime. - -[Proof](../../crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs): `closed_documents_release_their_source_buffers` invokes the actual handler with 32 open/close sequences, each containing 64 KiB of text. After all close notifications, the actual source map retains **32 documents and 2,097,152 text bytes**, expected `(0, 0)`. Handler registration is unnecessary to reproduce this defect, so it is independent of the cycle below. - -## [MEM-AUDIT-HANDLER-CYCLE] Reference LSP handlers retain their own dispatcher - -**Reference implementation:** [diagnostic-reference/session.rs](../../crates/lspkit-server/examples/diagnostic-reference/session.rs), `register_handlers`. - -`Dispatcher.handlers โ†’ handler closure โ†’ cloned Server โ†’ Dispatcher.handlers` forms a strong `Arc` cycle. Those captured servers also retain source buffers, the pull database, gate map, and output queue. Dropping every external server owner cannot break it. - -[Proof](../../crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs): `registered_session_releases_its_source_allocations` opens a 64 KiB document, calls the actual registration function, takes a weak observer of the source registry, and drops the server. **Four strong owners remain**, expected zero. The otherwise identical unregistered-session control releases the registry completely. This is a genuine unreachable cycle, not a live cache. - -## [MEM-AUDIT-CONSUMERS] Consumer impact and remaining candidates - -These tests establish defects in this checkout. **They do not establish that these five defects caused the previously observed memory spikes in Osprey, SharpLsp, or Deslop.** The bounded consumer trace confirmed SharpLsp's use of shared `PullStore` and ingress `CancellationHandle::register` in `src/sharplsp/src/pull_diagnostics/requests.rs` and `src/sharplsp/src/inbound.rs`; it did not reproduce a consumer memory incident. No consumer repository was modified. - -| Candidate | Evidence and limit | -| --- | --- | -| `PullStore` retains URI inputs and Salsa diagnostic memos without a document-removal API | High-priority unbounded-retention risk, especially on solution/workspace churn. SharpLsp uses this store. Memoization is explicitly part of the spec; no executed lifecycle test yet establishes unintended retention after a supported eviction boundary. Not counted as a proven leak. | -| Refused ingress requests never adopted by `prepare_call` | Shared registry has no explicit discard operation. SharpLsp preregisters before foreground dispatch; rejected-after-shutdown traffic is a candidate trigger. The test above proves the terminal unknown-method path, not SharpLsp's shutdown path. | -| UI `memory-profiler.ts` keeps `boundStore` after disposal; host logger/palette bindings also retain their last host | Static ownership candidates, not GC-tested. A functional accessor returning old state alone is insufficient proof of retained allocations. | -| Concurrent `profile-server.ts::ensureServer` calls can each create a loopback server before the singleton is adopted | Independently observed race candidate; only the last server is stored for disposal. Requires an executed real-host test of surviving server handles before being called a confirmed leak. | -| Memory-manager eviction removes only its map entry | .NET backend has a separate session map and heap-graph directory. The evicted ID becomes unavailable to normal stop routing. Not reproduced through the real engine; file retention is not automatically a RAM-leak proof. | -| Profiler shutdown only calls CPU `stop_all` | Memory backend cleanup is a follow-up; the large heap graphs identified by pi live on disk, not in the Rust session map. | -| Hung .NET CPU forwarder | Its detached task can wait indefinitely on engine reads/process completion even after the consumer's bounded stop. Conditional candidate; no hung real-engine proof executed. | -| .NET capture exception cleanup | Candidate temporary-file/handle retention requires an actual exception reproduction. Not counted as a memory leak. | -| Reference output pump owns an `Outbox` sender to its own receiver | Additional task-lifetime candidate at runtime scope; not independently tested and not included in the five confirmed findings. | - -Reviewed cleanup paths with no confirmed leak: VFS `close` removes documents; explicit parse-cache `clear` releases its caller-owned cache; combined watchers abort their forwarding task; normal dispatcher futures have a release guard; sampler drop signals stop; memory timelines are bounded and growth streaks prune absent sites; cooperative/helper process shutdown has explicit stop paths. CPU recordings retained until explicit stop are documented archival state. Diagnostics sink registration and parse-cache growth need caller-lifetime analysis, not a blanket leak label. - -The parked Python runtime received a targeted lifecycle inspection, not an exhaustive executable audit. UI/.NET candidates remain unproven. This scan is not a guarantee that the repository or its consumers contain no further leaks. - -## [MEM-AUDIT-VALIDATION] Executed validation - -- **New proofs:** 5 ownership assertions fail, 4 release controls pass; exact commands and complete output are in [the evidence file](memory-leak-audit-evidence.md). -- **Existing nearby suites:** 9 correlator tests and 12 dispatcher tests pass, demonstrating why existing green checks missed these cases. -- **`make lint`: exit 0.** Workspace release Clippy with warnings denied, spec-citation checks, skill-link checks, TypeScript 7/oxlint, and .NET builds pass. Cargo separately emits existing unused-manifest-dependency warnings; .NET reports zero warnings/errors. -- **Deslop:** no connected Deslop MCP tools were available, so the documented [CLI fallback](https://deslop.live/docs/for-ai/) was used. Read the existing `0.0.0-dev` report before writing; then ran the installed bundled CLI and inspected the JSON. Final report has no cluster touching any new `memory_retention` test. Global duplication is 7.006%; thresholds/configuration unchanged. The repeated setup introduced by the pi draft was consolidated. -- **No full-suite green claim:** the requested leak regressions intentionally fail. No production repairs were made, so the fix skill's post-repair full test run is not represented as completed. -- **Pi verification:** Rust, profiler/.NET, UI, and consumer reviews used `zai/glm-5.3`. The test draft used `zai/glm-5.3-flash`; its first control incorrectly left 31 deliberately queued requests and violated several lints. It was corrected before acceptance, and all final assertions were independently rerun. `kimi-coding/k3.1-pro-highspeed` returned a quota error and contributed no review. Missing APIs/compile failures suggested by reviewers were rejected as leak proofs. Details: `PiNotes/2026-11-16-lspkit-memory-audit.md`. - -## [MEM-AUDIT-CLEANUP] Cleanup - -Removed generated Cargo build output, the task-installed npm dependencies, the .NET Release build directories, and audit temporary files after preserving the evidence. All pi jobs ended; .NET build-server shutdown completed through its own CLI. Final disk check reports 163 GiB available (other workspace activity also changed disk usage, so this is not a claimed amount freed by the audit). No git commands, commits, pushes, issue changes, cloud jobs, or VS Code process termination were performed. diff --git a/docs/plans/memory-leak-fix-evidence.md b/docs/plans/memory-leak-fix-evidence.md deleted file mode 100644 index a11bbfb..0000000 --- a/docs/plans/memory-leak-fix-evidence.md +++ /dev/null @@ -1,363 +0,0 @@ -# Memory leak repair evidence โ€” 2026-11-16 - -[MEM-FIX-SCOPE](memory-leak-fixes.md#mem-fix-scope-scope). Executed test output before and after repairs; compiler progress omitted. Each red result is an allocation-retention assertion, not a build failure. - -## sidecar-red - -Command: `cargo test -p lspkit --test sidecar_memory_retention --locked -- --nocapture` - -```text -running 3 tests - -thread 'abandoned_requests_release_their_channel_allocations' (65454926) panicked at crates/lspkit/tests/sidecar_memory_retention.rs:47:5: -assertion `left == right` failed: 32 abandoned oneshot senders still own at least 2097152 bytes of heap response slots; no receivers or request work remain - left: 32 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test abandoned_requests_release_their_channel_allocations ... FAILED - -thread 'dropped_inbound_readers_release_their_decoder_allocations' (65454927) panicked at crates/lspkit/tests/sidecar_memory_retention.rs:118:5: -assertion `left == right` failed: 32 dropped inbound readers retain 65536-byte decoder allocations after the executor becomes idle - left: 32 - right: 0 -test dropped_inbound_readers_release_their_decoder_allocations ... FAILED -test resolved_requests_release_their_channel_allocations ... ok - -failures: - -failures: - abandoned_requests_release_their_channel_allocations - dropped_inbound_readers_release_their_decoder_allocations - -test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.71s - -error: test failed, to rerun pass `-p lspkit --test sidecar_memory_retention` -``` - -## dispatch-red - -Command: `cargo test -p lspkit-server --test dispatch_memory_retention --locked -- --nocapture` - -```text -running 3 tests - -thread 'unknown_method_releases_pre_registered_interest' (65476304) panicked at crates/lspkit-server/tests/dispatch_memory_retention.rs:44:5: -assertion `left == right` failed: terminal requests retained registry entries, each owning a 65536-byte input allocation - left: "Dispatcher { handlers: 0, in_flight: 32 }" - right: "Dispatcher { handlers: 0, in_flight: 0 }" -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test unknown_method_releases_pre_registered_interest ... FAILED -test completed_requests_release_pre_registered_interest ... ok -test dropped_futures_release_pre_registered_interest ... ok - -failures: - -failures: - unknown_method_releases_pre_registered_interest - -test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - -error: test failed, to rerun pass `-p lspkit-server --test dispatch_memory_retention` -``` - -## reference-red - -Command: `cargo test -p lspkit-server --example diagnostic-reference --features pull --locked memory_retention -- --nocapture` - -```text -running 3 tests - -thread 'session::memory_retention::registered_session_releases_its_source_allocations' (65513362) panicked at crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs:49:5: -assertion `left == right` failed: handler closures retain the source buffer after every external Server owner is dropped - left: 4 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test session::memory_retention::unregistered_session_releases_its_source_allocations ... ok -test session::memory_retention::registered_session_releases_its_source_allocations ... FAILED - -thread 'session::memory_retention::closed_documents_release_their_source_buffers' (65513361) panicked at crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs:70:5: -assertion `left == right` failed: didClose retained complete document text buffers after all 32 documents closed - left: (32, 2097152) - right: (0, 0) -test session::memory_retention::closed_documents_release_their_source_buffers ... FAILED - -failures: - -failures: - session::memory_retention::closed_documents_release_their_source_buffers - session::memory_retention::registered_session_releases_its_source_allocations - -test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s - -error: test failed, to rerun pass `-p lspkit-server --example diagnostic-reference` -``` - -## sidecar-heap-red - -Command: `cargo test -p lspkit --test sidecar_heap_leak -- --nocapture` - -```text -running 1 test - -thread 'abandoned_receivers_free_heap_without_a_later_correlator_call' (65514815) panicked at crates/lspkit/tests/../../../test-support/state.rs:16:5: -assertion `left == right` failed: completed work still retains heap bytes: Stats { allocations: 32, deallocations: 0, reallocations: 0, bytes_allocated: 2099200, bytes_deallocated: 0, bytes_reallocated: 0 } - left: 2099200 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test abandoned_receivers_free_heap_without_a_later_correlator_call ... FAILED - -failures: - -failures: - abandoned_receivers_free_heap_without_a_later_correlator_call - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.11s - -error: test failed, to rerun pass `-p lspkit --test sidecar_heap_leak` -``` - -## dispatch-heap-red - -Command: `cargo test -p lspkit-server --test dispatch_heap_leak --locked -- --nocapture` - -```text -running 1 test - -thread 'terminal_lookup_errors_free_heap_before_shutdown_or_registry_inspection' (65526701) panicked at crates/lspkit-server/tests/../../../test-support/state.rs:16:5: -assertion `left == right` failed: completed work still retains heap bytes: Stats { allocations: 192, deallocations: 32, reallocations: 0, bytes_allocated: 2105792, bytes_deallocated: 224, bytes_reallocated: 0 } - left: 2105792 - right: 224 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test terminal_lookup_errors_free_heap_before_shutdown_or_registry_inspection ... FAILED - -failures: - -failures: - terminal_lookup_errors_free_heap_before_shutdown_or_registry_inspection - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - -error: test failed, to rerun pass `-p lspkit-server --test dispatch_heap_leak` -``` - -## watcher-red - -Command: `cargo test -p lspkit-server --example diagnostic-reference --features pull --locked initialized_watcher_releases_session_source_allocations -- --nocapture` - -```text -running 1 test - -thread 'session::watch_memory_retention::initialized_watcher_releases_session_source_allocations' (65759049) panicked at crates/lspkit-server/examples/diagnostic-reference/session/watch_memory_retention.rs:36:5: -assertion `left == right` failed: initialized watcher retains the real source buffer after session teardown - left: 1 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test session::watch_memory_retention::initialized_watcher_releases_session_source_allocations ... FAILED - -failures: - -failures: - session::watch_memory_retention::initialized_watcher_releases_session_source_allocations - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.01s - -error: test failed, to rerun pass `-p lspkit-server --example diagnostic-reference` -``` - -## sidecar-green - -Command: `cargo test -p lspkit --test sidecar_memory_retention --test sidecar_heap_leak --test sidecar_correlator --locked -- --nocapture` - -```text -running 9 tests -test pending_count_reflects_registrations ... ok -test debug_rendering_shows_pending_count ... ok -test default_constructor_is_usable ... ok -test request_id_traits_are_derived ... ok -test error_display_strings_are_stable ... ok -test allocated_ids_are_monotonic_from_one ... ok -test out_of_order_responses_match_correct_receivers ... ok -test resolving_unknown_id_errors ... ok -test resolve_against_dropped_receiver_errors ... ok - -test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - - Running tests/sidecar_heap_leak.rs (target/debug/build/lspkit/3462b822b4c0ff8d/out/sidecar_heap_leak-3462b822b4c0ff8d) - -running 1 test -test abandoned_receivers_free_heap_without_a_later_correlator_call ... ok - -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s - - Running tests/sidecar_memory_retention.rs (target/debug/build/lspkit/b0ac403ea636af54/out/sidecar_memory_retention-b0ac403ea636af54) - -running 3 tests -test abandoned_requests_release_their_channel_allocations ... ok -test dropped_inbound_readers_release_their_decoder_allocations ... ok -test resolved_requests_release_their_channel_allocations ... ok - -test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.07s - -``` - -## dispatch-green - -Command: `cargo test -p lspkit-server --test dispatch_memory_retention --test dispatch_heap_leak --test dispatch --locked -- --nocapture` - -```text -running 12 tests -test cancel_trips_token_for_inflight_request ... ok -test default_dispatcher_matches_new ... ok -test cancel_unknown_id_is_noop ... ok -test debug_shows_handler_count ... ok -test unknown_method_returns_error ... ok -test registered_handler_receives_params_and_returns_ok ... ok -test handler_result_debug_works ... ok -test dispatcher_clone_shares_handlers ... ok -test registered_handler_can_return_jsonrpc_error ... ok -test dispatch_removes_cancellation_on_completion ... ok -test re_registering_replaces_handler ... ok -test dispatch_error_display_includes_method ... ok - -test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s - - Running tests/dispatch_heap_leak.rs (target/debug/build/lspkit-server/aa8702b73f2d6345/out/dispatch_heap_leak-aa8702b73f2d6345) - -running 1 test -test terminal_lookup_errors_free_heap_before_shutdown_or_registry_inspection ... ok - -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - - Running tests/dispatch_memory_retention.rs (target/debug/build/lspkit-server/4a4addbb18ef7a80/out/dispatch_memory_retention-4a4addbb18ef7a80) - -running 3 tests -test dropped_futures_release_pre_registered_interest ... ok -test unknown_method_releases_pre_registered_interest ... ok -test completed_requests_release_pre_registered_interest ... ok - -test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s - -``` - -## watcher-green - -Command: `cargo test -p lspkit-server --example diagnostic-reference --features pull --locked memory_retention -- --nocapture` - -```text -running 4 tests -test session::memory_retention::unregistered_session_releases_its_source_allocations ... ok -test session::memory_retention::registered_session_releases_its_source_allocations ... ok -test session::memory_retention::closed_documents_release_their_source_buffers ... ok -test session::watch_memory_retention::initialized_watcher_releases_session_source_allocations ... ok - -test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.03s - -``` - -## Original proof integrity - -- `crates/lspkit/tests/sidecar_memory_retention.rs`: `fd4a381a50be75e542fbd6131b9fa06ee0b1b259f3b2cc157fe9c427e84b0237` -- `crates/lspkit-server/tests/dispatch_memory_retention.rs`: `79d9fbb9cfe4a36bbf7a32309662f077296c7acdc004c94d752c3810665f8bde` -- `crates/lspkit-server/examples/diagnostic-reference/session/memory_retention.rs`: `b24215fcadfbc2c2ad64d78f79647cb8751a6cfa061d1e5fafc7758d89f41364` - -## Full validation - -`make ci` exited 0 on 2026-11-16, including analysis, tests, coverage, and release build. `make fmt CHECK=1` exited 0. Original proof hashes above were rechecked after the full run and match. - -```text -Summary [ 38.669s] 496 tests run: 496 passed, 0 skipped -OK: lsp-profiler 95.54% >= 94% -OK: lsp-profiler-dotnet 92.76% >= 92% -OK: lspkit 99.17% >= 99% -OK: lspkit-server 97.12% >= 97% -OK: lspkit-server::live 94.66% >= 94% -OK: lspkit-server::mcp 100.00% >= 100% -OK: lspkit::config 100.00% >= 100% -OK: lspkit::sidecar 99.25% >= 99% -OK: lspkit::treesitter 95.00% >= 95% -OK: lspkit::vfs 100.00% >= 100% -Statements : 99.44% ( 1424/1432 ) -Branches : 96.48% ( 247/256 ) -Functions : 99.12% ( 113/114 ) -Lines : 99.44% ( 1424/1432 ) -``` - -## Closed pull inputs โ€” additional red/green proof - -`cargo test -p lspkit-server --example diagnostic-reference --features pull --locked closing_documents_releases_their_salsa_diagnostic_allocations -- --nocapture` drives 32 real open โ†’ JSON analysis โ†’ close sequences while keeping the session alive. Before the repair: - -```text -running 1 test - -thread 'session::pull_memory_retention::closing_documents_releases_their_salsa_diagnostic_allocations' (66406473) panicked at crates/lspkit-server/examples/diagnostic-reference/session/pull_memory_retention.rs:28:5: -assertion `left == right` failed: closed documents retain real Salsa diagnostic allocations while the session remains alive - left: 32 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test session::pull_memory_retention::closing_documents_releases_their_salsa_diagnostic_allocations ... FAILED - -failures: - -failures: - session::pull_memory_retention::closing_documents_releases_their_salsa_diagnostic_allocations - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 4 filtered out; finished in 0.00s - -error: test failed, to rerun pass `-p lspkit-server --example diagnostic-reference` -``` - -After the repair, the same allocation assertion passes: - -```text -running 1 test -test session::pull_memory_retention::closing_documents_releases_their_salsa_diagnostic_allocations ... ok - -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 4 filtered out; finished in 0.00s - -``` - -`PullStore::clear` replaces the database without locking compiler execution. This releases Salsa input slots, URI strings, and diagnostic memos; removing only map keys would leave Salsa-owned allocations alive. Active old queries finish as superseded and release their old storage. A barrier-driven test proves clearing returns with compiler work blocked, permits a new query, preserves memoization until the next clear, and releases allocations through any store clone. An isolated queued-query test proves old storage cannot publish under a replacement identity. - -## Poisoned source registry close โ€” red/green proof - -A caught panic deterministically poisons the real source registry after inserting 64 KiB of text. The real close notification must still free it. Isolation is necessary to force this error path without crashing the process. - -red: - -```text -running 1 test - -thread 'session::pull_memory_retention::closing_after_a_source_lock_panic_releases_retained_text' (66470111) panicked at crates/lspkit-server/examples/diagnostic-reference/session/pull_memory_retention.rs:25:5: -assertion `left == right` failed: poisoned close retains a 64 KiB source allocation - left: 65536 - right: 0 -note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace -test session::pull_memory_retention::closing_after_a_source_lock_panic_releases_retained_text ... FAILED - -failures: - -failures: - session::pull_memory_retention::closing_after_a_source_lock_panic_releases_retained_text - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 5 filtered out; finished in 0.00s - -error: test failed, to rerun pass `-p lspkit-server --example diagnostic-reference` -``` - -green: - -```text -running 1 test -test session::pull_memory_retention::closing_after_a_source_lock_panic_releases_retained_text ... ok - -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 5 filtered out; finished in 0.00s - -``` - -The repair recovers the poisoned registry with a structured warning, removes the closed buffer, then clears Salsa. No assertion changed. - -## Final release-candidate validation - -`make ci` exited 0 on `51287e1` before merge: 502 Rust tests passed, zero skipped; .NET and TypeScript tests, static analysis, coverage gates and release build passed. Final server coverage is 97.15% against 97%; tree-sitter coverage is 95.45% against 95%; the other eight floors remain green. Final Deslop review found no new high-similarity production duplication. The three original proof hashes still match the recorded values. - -All required PR checks passed on the same candidate. PR #46 merged as `a451a9b`, tagged `v0.5.0`. Release run [43858247154](https://github.com/Nimblesite/lspkit/actions/runs/43858247154) passed all publication and Pages jobs. All four crates.io 0.5.0 versions were verified non-yanked. npm metadata, latest tag, downloadable tarball SRI and byte equality with the signed GitHub release asset were verified after npm processing completed. SHA256SUMS matched all six package/SBOM assets. Release announcements were sent directly to all 29 other registered TMC agents and broadcast with API migration details. diff --git a/docs/plans/memory-leak-fixes-0.6.0.md b/docs/plans/memory-leak-fixes-0.6.0.md deleted file mode 100644 index fcd0af3..0000000 --- a/docs/plans/memory-leak-fixes-0.6.0.md +++ /dev/null @@ -1,50 +0,0 @@ -# Memory leak repairs after v0.6.0 - -## [MEM-060-SCOPE] Scope - -Fix every leak in the v0.6.0 static review (four pi agents on GLM 5.3 plus a manual pass), following [fix-bug](../../.agents/skills/fix-bug/SKILL.md). Each fix has a regression test that failed because of the leak before the fix: a `Weak`/`strong_count` check, a bounded count of retained entries, or a released file handle. None uses RSS, timing or `sleep`. The rules are the ones in [AGENTS.md](../../AGENTS.md#memory-lifetimes-). - -## [MEM-060-CHECKLIST] Fixes - -| ID | Finding | Fix | Regression test | -| --- | --- | --- | --- | -| [MEM-060-PARSE-CACHE] | H1 `ParseCache` kept one tree per edit | FIFO bound of `MAX_TREES` (128) | `lspkit/tests/treesitter_cache.rs` | -| [PROF-CPU-SESSIONS] | H2 ended CPU sessions stayed forever | A closed sampler counts as ended; all but 8 ended sessions are evicted on register | `lsp-profiler/src/session_lifetime_tests.rs` | -| [PROF-UI-LIFETIME] | H3 DAP output, stop-state and pending resumes kept per ended session | Evicted on `onDidTerminateDebugSession` | `lsp-profiler-ui/src/test/unit/dap-output-eviction.test.ts` | -| [PROF-CPU-PROGRESS] | M1 progress task held the whole service | Weak back-reference, upgraded per tick | `lsp-profiler/src/service/lifetime_tests.rs` | -| [PROF-MEMORY-SESSION] | M2 evicted and shut-down memory sessions kept backend state (.NET heap graphs) | Eviction and `stop_all` (called by `shutdown`) run the backend's `stop` | `lsp-profiler/src/memory/session_lifetime_tests.rs` | -| [MEM-060-EXAMPLE-TASKS] | M3 example detached request tasks pinned a `Server` | `Tasks` owns a `JoinSet`, reaped per spawn and aborted at session end | `examples/diagnostic-reference/session/task_retention.rs` | -| [MEM-060-PULL-ADMIT] | M4 pulls for URIs outside the session admitted Salsa inputs nothing forgot | Forget a URI that is neither open nor a JSON file in the watched root, after pulls, scans and watched-file changes | `examples/diagnostic-reference/session/admit_retention.rs` | -| [MEM-060-QUEUED-REQUEST] | M5 a request registered but never prepared had no owner | `CancellationHandle::queue` returns a `QueuedRequest` guard; the example holds it until `prepare` | `lspkit-server/tests/dispatch_memory_retention.rs` | -| [MEM-060-SIDECAR-NOTIFICATIONS] | M6 unread sidecar notifications queued without bound | Bounded at `NOTIFICATION_CAPACITY`, overflow counted | `lspkit/tests/sidecar_notification_bound.rs` | -| [MEM-060-SCHEDULER-TASK] | M6 the live scheduler outlived a consumer that dropped its triggers | The loop selects on `closed()` | `lspkit-server/tests/live_scheduler.rs` | -| [MEM-060-EXAMPLE-OUTBOX] | M6 workspace-change notifications queued behind a stalled editor | One queued notification covers every change until the pump dequeues it | `examples/diagnostic-reference/session/outbox_retention.rs` | -| [PROF-UI-LIFETIME] | M7 `ProfilerUi.dispose()` left the processes provider and tree view alive | `registerProcessesView` returns both among its disposables | `lsp-profiler-ui/src/test/teardown/ui-teardown.test.ts` (own editor instance) | -| [PROF-UI-LIFETIME] | M8 capture timeline and autopilot ledger grew per capture | `appendBounded`, 120 entries, matching the core timeline | `lsp-profiler-ui/src/test/unit/memory-timeline-bound.test.ts` | -| [MEM-060-DIAGNOSTICS-SINK] | LOW `DiagnosticsBus::attach` had no detach | `attach_owned` returns a `SinkRegistration` guard | `lspkit-server/tests/diagnostics.rs` | -| [PROF-EXPORT] | LOW profiler artifacts on disk were never pruned | Keep the newest 32 per extension after each write | `lsp-profiler/src/export/artifact_retention_tests.rs` | -| [PROF-UI-LIFETIME] | LOW `FileLogSink` had no dispose and no size cap | `dispose()` closes it; two alternating capped files (8 MiB each) | `lsp-profiler-ui/src/test/unit/file-log-sink.test.ts` | -| [PROF-DOTNET-CPU] | LOW `cpu.rs` spawned its forwarder with `drop(tokio::spawn(..))` | `SamplerHandle::own_producer` keeps the task's abort handle; dropping the handle aborts it | `lsp-profiler/src/sampler_lifetime_tests.rs`, plus the forwarder pin in `lsp-profiler-dotnet/src/cpu.rs` | -| [PROF-UI-LIFETIME] | LOW decoration-type maps keyed by colour grew on every palette change: the palette is re-read per render | `pruneToPalette` disposes types whose colour the current palette no longer paints | `lsp-profiler-ui/src/test/suite/decoration-palette.test.ts` (9 types after 8 changes before the fix) | -| [PROF-CPU-COOPERATIVE] | LOW cooperative tail thread: a header that arrived after `tail` gave up left the in-process sampler writing into a file nobody read | The thread writes the stop sentinel, allows the drain grace, then removes both files | `cooperative.rs::a_header_after_attach_gave_up_stops_the_writer_and_removes_the_file` | -| [PROF-MEMORY-STOP] | LOW Python `stop_tracemalloc` left `gc.garbage`, the baseline snapshot, the `atexit` hook and the signal handlers in the debuggee | The stop script (now `src/memory/scripts/stop_tracemalloc.py`) releases all four | `lsp-profiler-python/tests/python/test_stop_tracemalloc.py`, run by `make test` in a real interpreter | -| [PROF-DOTNET-MEMORY] | Follow-up review on 6957145: every .NET `snapshot` and `diff` kept a new numbered heap graph (about 16 MB each) until `stop`, though only the latest is read | `capture` deletes the graph it supersedes | `lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs` (two graphs after one diff before the fix) | - -## [MEM-060-NOT-LEAKS] Checked and left as they are - -- **M9 `Symbolizer` `_ids`:** each CPU session runs its own engine process, and the interner is that recording's frame dictionary, mirrored by the Rust relay. It goes when the session's engine exits. -- **Other unowned handles (`live/scheduler.rs`, `live/watcher.rs`):** each ends with its consumer. The scheduler task selects on its consumer's `closed()` (tested); the watcher thread (pinned by `dropping_the_native_watcher_ends_the_forwarder_thread`) exits when its receiver or native watcher drops. -- **`forgotten` close queue:** a `HashSet` of URIs, bounded by distinct closed documents and drained by the query that holds the state once it finishes (`pull_forget.rs::forget_returns_while_a_compiler_query_runs_and_the_next_pull_applies_it`). -- **Sidecar responses and the example's response frames:** bounded by the requests in flight. Dropping a response would break the protocol. -- **One analysis per trigger:** `Session::run` keeps its tested contract (`run_advances_generation_on_each_success`). The scheduler adds no memory: each event lands in exactly one trigger. - -## [MEM-060-PARKED] The parked Python crate - -`lsp-profiler-python` is excluded from the workspace and does not build: it depends on `basilisk-common`, which is not in this repository. Its stop script is plain Python, so it is tested directly: `make _python_scripts_test` runs it against real interpreter state, and every assertion failed on the old script. The crate's two remaining unowned handles end with their owners. The `pyspy.rs` sampler thread exits when the handle's `Drop` sets the stop flag or its receiver closes, and the helper's stop listener ends with the helper process. Neither is a leak, and no Rust test can compile against them until the port lands, so they are unchanged. - -## [MEM-060-CONTRACT] API changes - -- `lspkit::sidecar::inbound::Inbound::notifications` is a bounded `mpsc::Receiver`. `Inbound` gains `dropped_notifications`. -- `lspkit-server` adds `CancellationHandle::queue`, `QueuedRequest`, `DiagnosticsBus::attach_owned` and `SinkRegistration`. -- `lsp-profiler` adds `SamplerHandle::own_producer`, `MemorySessionManager::stop_all`, `export::MAX_ARTIFACTS_PER_KIND` and `export::ARTIFACT_PREFIX`. `ProfilerService::shutdown` now also stops memory sessions. -- `@nimblesite/lsp-profiler-ui`: `FileLogSink` takes an optional byte cap and gains `dispose(): Promise`. diff --git a/docs/plans/memory-leak-fixes.md b/docs/plans/memory-leak-fixes.md deleted file mode 100644 index 6e1705b..0000000 --- a/docs/plans/memory-leak-fixes.md +++ /dev/null @@ -1,53 +0,0 @@ -# Memory leak repairs โ€” 2026-11-16 - -## [MEM-FIX-SCOPE] Scope - -Repair the five allocation-lifetime defects proved by the [audit](memory-leak-audit.md), plus the initialized watcher leak discovered during validation, following [fix-bug](../../.agents/skills/fix-bug/SKILL.md). Preserve all original leak proofs unchanged. Add direct allocator measurements for the correlator and dispatcher before repairing them; a count-only sweep must not substitute for freeing memory when work ends. - -## [MEM-FIX-CHECKLIST] Required steps - -- [x] Read the skill, source, specifications, and original evidence. -- [x] Reconfirm all five original failures and record direct heap-retention failures. -- [x] Repair abandoned correlator request ownership; prove release. -- [x] Repair inbound reader shutdown; prove decoder release. -- [x] Release ingress registrations on terminal preparation errors; prove release. -- [x] Release closed reference-server document buffers; prove release. -- [x] Break the reference-server handler cycle; prove release. -- [x] Prove and repair initialized watcher retention after session teardown. -- [x] Prove and repair closed-document Salsa input and diagnostic retention. -- [x] Run `make test`, `make lint`, formatting, and Deslop without weakening gates. -- [x] Record pi review results and exact validation evidence. -- [x] Clean owned temporary/build files and processes; check disk space. -- [x] Open and merge a green PR, publish v0.5.0, verify registries and notify consumer agents. - -## [MEM-FIX-RESULTS] Validation - -The original eight leak tests pass after failing for actual retained allocations before the fixes. Full `make ci` passes: 502 Rust tests, all ten coverage floors, TypeScript tests and coverage, static analysis, and the release build. `make fmt CHECK=1` passes. All three original proof files remain byte-identical. Exact output: [memory-leak-fix-evidence.md](memory-leak-fix-evidence.md). - -| Ownership defect | Executed failure before repair | Repair | -| --- | --- | --- | -| Abandoned correlator responses | 32 response channels retain 2,099,200 heap bytes; zero deallocations | `PendingResponse` drop removes the sender immediately, with a weak registry reference | -| Detached inbound readers | 32 decoder allocations remain live after every receiver owner drops and the runtime parks | The reader selects channel closure alongside incoming frames; both destinations must close | -| Rejected preregistered requests | 32 terminal errors retain 2,105,568 heap bytes | Unknown-method preparation adopts and drops the ingress registration guard | -| Closed reference documents | 32 closed documents retain 2,097,152 source bytes | Handle `didClose`, remove the source buffer and invalidate diagnostics | -| Reference handler cycle | Source allocation retains four strong owners after the server drops | Handler captures exclude their owning dispatcher | -| Closed pull inputs and memos | 32 real diagnostic allocations remain alive after open โ†’ analyze โ†’ close | Replace Salsa storage on close without waiting behind compiler work | -| Initialized reference watcher | Real source allocation retains one strong owner after initialized session drops | Session owns and aborts the watcher task; callback holds only weak session ownership | - -The allocator tests warm map capacity, then compare actual allocated and freed bytes before registry inspection, shutdown, or owner teardown. Weak probes establish whether specific real allocations remain alive; these tests do not infer leaks from RSS, elapsed time, or generic errors. Isolation is needed because exiting an LSP process would mask all retained memory. - -## [MEM-FIX-CONTRACT] API and consumer impact - -The only public signature change is `Correlator::register` returning `PendingResponse` instead of a bare Tokio receiver. Await and `try_recv` remain supported. Under [SIDECAR-REQUEST-LIFETIME](../specs/lspkit-spec.md#cross-cutting-helpers--required-behavior), an abandoned ID returns `UnknownId`; preserving the former `AlreadyResolved` outcome after deterministic abandonment would require retaining an unbounded history. The old API assertion is updated to the explicit new contract and additionally asserts immediate registry release. Every original memory-leak proof remains unchanged. `AlreadyResolved` still reports the concurrent drop/send race. - -Inbound and dispatcher signatures are unchanged. Three fixes are in the reference server example, not consumers' own document stores or handler registrations. `PullStore::clear` adds an explicit close/scope eviction boundary; the reference server invokes it on `didClose`. Salsa input slots cannot be deleted individually, so closing a document clears the whole memo database and advances the workspace identity. No watcher payload feature is part of this patch. These tests do not establish the causes of previous consumer incidents. The consumer agents have received scope and API details through TMC. The user authorized a PR and release; v0.5.0 is the target because the correlator return type changes. - -The first full run exposed a separate existing harness failure: Cargo placed tests below `build///out`, so inferring the producer path from two parent directories failed before real sessions started. `scripts/rust-coverage.sh` now obtains the executable from Cargo's parsed artifact metadata, overriding stale caller settings with the just-built instrumented producer. No test assertion or coverage gate changed. - -Coverage gaps are covered by deterministic receiver-owner teardown, closed-channel and poisoned-registry tests, native watcher observation loss and consumer shutdown, absent native parse results, and real .NET capability/error-path negotiation. Native failure paths require isolation because safe public handles cannot deterministically force OS overflow or a missing tree. Existing coverage thresholds and reporting semantics remain unchanged. The pi coverage review incorrectly attributed the gaps to stale profiles; a clean rebuild reproduced them, so that recommendation was rejected and actual missing paths were tested. - -## [MEM-FIX-RELEASE] Published result - -[PR #46](https://github.com/Nimblesite/lspkit/pull/46) merged after every required check passed. [v0.5.0](https://github.com/Nimblesite/lspkit/releases/tag/v0.5.0) ships merged commit `a451a9b782c47bcc40debbd49b8bc92f39d78565`; release run `43858247154` completed successfully, including Pages. All four crates were checked on crates.io as non-yanked 0.5.0 releases. npm reports `@nimblesite/lsp-profiler-ui@0.5.0` and `latest=0.5.0`; its downloadable tarball matches its SRI integrity and the signed release asset byte for byte. All six package/SBOM assets match SHA256SUMS. Every registered TMC agent received a direct release announcement, plus shared migration notices. - -Generated build output and Deslop caches were moved to Trash; no owned compiler or pi task remains running. Disk space was checked (139 GiB available). Original leak proof hashes remain unchanged. diff --git a/docs/specs/LSP-PROFILER-SPEC.md b/docs/specs/LSP-PROFILER-SPEC.md index 7f37d5c..b88c0db 100644 --- a/docs/specs/LSP-PROFILER-SPEC.md +++ b/docs/specs/LSP-PROFILER-SPEC.md @@ -548,7 +548,7 @@ The same code then runs in any VS Code-family editor and under test fakes. ### [PROF-UI-LIFETIME] Lifetimes -An editor session runs for days, so nothing the library keeps grows without a bound or outlives its owner ([plan](../plans/memory-leak-fixes-0.6.0.md)): +An editor session runs for days, so nothing the library keeps grows without a bound or outlives its owner ([MEM-LIFETIME](lspkit-spec.md#mem-lifetime-memory-lifetimes)): - `ProfilerUi.dispose()` disposes everything `registerProfilerUi` created, the processes tree view and its provider (with the provider's poll and effects) included. - A debug session's output buffer, stop-state and pending resume requests are dropped when the session terminates. diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index 25f9dce..c311f43 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -91,12 +91,6 @@ pub trait EngineApi: Send + Sync + 'static { ## Cross-cutting helpers โ€” required behavior - **[SIDECAR-REQUEST-LIFETIME]** Dropping a correlator response future immediately unregisters and frees its response channel without another correlator call or a runtime. Abandoned IDs are no longer pending, so later responses return `UnknownId`; no cancellation tombstones accumulate. `register` returns `PendingResponse`, supporting `Future` and `try_recv`. A receiver holds only a weak registry reference, so it cannot prolong the correlator's lifetime. Inbound readers terminate when both destination receivers close, even without EOF or another frame. -- **[LIFETIME-BOUNDS-060]** Long-lived structures that grow with use are bounded or owned ([plan](../plans/memory-leak-fixes-0.6.0.md)): - - `treesitter::ParseCache` keeps at most `MAX_TREES` (128) trees and evicts the oldest first, so an edited document never keeps one tree per keystroke. - - `sidecar::inbound` buffers at most `NOTIFICATION_CAPACITY` (1024) unread notifications; later ones are dropped and counted in `Inbound::dropped_notifications`, so a consumer that only reads responses never accumulates them and the reader never blocks. - - `CancellationHandle::queue` registers a request like `register` and returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control. `register` stays dispatcher-owned until preparation. - - `DiagnosticsBus::attach_owned` returns a `SinkRegistration`; dropping it detaches the sink. `attach` stays for sinks that live as long as the bus. - - The live scheduler's task ends when its consumer drops the trigger receiver, not only when the events end. - **[JSONRPC-FRAME-LIMIT]** `read_message` accepts a body of at most `MAX_FRAME_BYTES` (64 MiB); `read_message_within` takes a server-chosen cap. A larger `Content-Length` is `FramingError::FrameTooLarge`: its body is discarded through a fixed-size buffer, never allocated, so the connection stays usable and the next read starts at the following frame. A length beyond `u64` is `BadHeader`, and the body is read incrementally, so memory follows the bytes that arrive, never the claimed length. A body shorter than its claim is `Io(UnexpectedEof)`. The header block, every line and terminator included, is read through one `MAX_HEADER_BYTES` (8 KiB) budget, so one endless line and endless short lines both end in `HeaderTooLarge` with memory bounded by the cap; no frame boundary is known after it, so the connection cannot continue. Peer input never panics. - **[LSP-OUTBOUND]** Serverโ†’client requests (`lspkit-server::outbound::Outbound`): `request` allocates a numeric JSON-RPC id from the sidecar correlator and returns the request frame plus an `OutboundResponse` future; once ids pass `i64::MAX` it returns `IdsExhausted` instead of reusing a number. `null` params are omitted from the frame. `resolve` routes an inbound response to its waiter as the editor's `result` (absent or `null` is `Value::Null`) or its `JsonRpcError`. Requests and notifications are `NotAResponse`; ids never allocated, already answered or abandoned are `UnknownId`. Dropping a wait unregisters it ([SIDECAR-REQUEST-LIFETIME]). `sidecar::RequestId` rebuilds from its raw wire number with `From`, and `JsonRpcError` implements `Display` and `std::error::Error`. - **Position encoding** (`lspkit::vfs::PositionEncoding`): negotiates encoding at `initialize` per LSP 3.17 client capabilities; converts on every read; round-trips byte/char offsets correctly across multi-byte scalars and surrogate pairs. @@ -112,6 +106,19 @@ pub trait EngineApi: Send + Sync + 'static { - **Diagnostics bus** (`lspkit-server::diagnostics`): fan-out from one source to LSP `publishDiagnostics` AND MCP resource-updated notifications, atomically per generation. - **Workspace folders** (`lspkit-server::workspace`): multi-root aware; `Workspace` exposes folder enumeration and per-folder configuration scope. +## [MEM-LIFETIME] Memory lifetimes + +Language servers run for days, so an allocation retained after the work that needed it ends is a production bug. The rules are in [AGENTS.md](../../AGENTS.md#memory-lifetimes-). Every fix ships a regression that failed on the leak: exact allocator bytes through `test-support/state.rs` in a single-test binary, or a `Weak::strong_count() == 0` check after teardown. RSS, timing and `sleep` never prove or disprove a leak. + +- **[MEM-LIFETIME-PARSE-CACHE]** `treesitter::ParseCache` keeps at most `MAX_TREES` (128) trees and evicts the oldest first, so an edited document never keeps one tree per keystroke. +- **[MEM-LIFETIME-SIDECAR-NOTIFICATIONS]** `sidecar::inbound` buffers at most `NOTIFICATION_CAPACITY` (1024) unread notifications; later ones are dropped, counted in `Inbound::dropped_notifications` and logged, so a consumer that only reads responses never accumulates them and the reader never blocks. +- **[MEM-LIFETIME-QUEUED-REQUEST]** `CancellationHandle::queue` registers a request like `register` and returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control. A terminal preparation error (an unknown method) releases its ingress registration. `register` stays dispatcher-owned until preparation. +- **[MEM-LIFETIME-DIAGNOSTICS-SINK]** `DiagnosticsBus::attach_owned` returns a `SinkRegistration`; dropping it detaches the sink. `attach` stays for sinks that live as long as the bus. +- **[MEM-LIFETIME-SCHEDULER-TASK]** The live scheduler's task ends when its consumer drops the trigger receiver, not only when the events end. +- **[MEM-LIFETIME-EXAMPLE-TASKS]** The reference server's `Tasks` owns its request tasks in a `JoinSet`, reaped per spawn and aborted when the session ends. Its handlers never capture the dispatcher that stores them, and its initialized watcher is owned and aborted by the session. +- **[MEM-LIFETIME-PULL-ADMIT]** The reference server forgets a pulled URI that is neither open nor a JSON file in the watched root, after pulls, scans and watched-file changes, and `didClose` releases a document's source buffer and its pull state ([LSP-PULL-FORGET]). +- **[MEM-LIFETIME-EXAMPLE-OUTBOX]** One queued workspace-change notification covers every change until the output pump dequeues it, so a stalled editor never accumulates them. + ## Tech stack (locked) - `tokio` 1.x, `tracing` + `tracing-subscriber`, `serde` + `serde_derive`, `async-trait`. diff --git a/packages/lsp-profiler-ui/src/bounded-ledger.ts b/packages/lsp-profiler-ui/src/bounded-ledger.ts index 6c5626b..4364b58 100644 --- a/packages/lsp-profiler-ui/src/bounded-ledger.ts +++ b/packages/lsp-profiler-ui/src/bounded-ledger.ts @@ -1,4 +1,4 @@ -// Implements [PROF-UI-LIFETIME]. See docs/plans/memory-leak-fixes-0.6.0.md +// Implements [PROF-UI-LIFETIME] (docs/specs/LSP-PROFILER-SPEC.md). /** * Append-only per-session records with an eviction boundary: a tracking * session can run for days, so a list that grows once per capture keeps only diff --git a/test-support/state.rs b/test-support/state.rs index ae4b95a..09325ff 100644 --- a/test-support/state.rs +++ b/test-support/state.rs @@ -1,4 +1,4 @@ -//! Direct heap accounting shared by isolated, single-test executables. [MEM-FIX-SCOPE] +//! Direct heap accounting shared by isolated, single-test executables. [MEM-LIFETIME] use stats_alloc::{Region, Stats, StatsAlloc, INSTRUMENTED_SYSTEM}; use std::alloc::System; From 8165bcb44704455f4e298b01779d42cbd5a134a8 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:50:28 +1100 Subject: [PATCH 02/15] Profile loopback server: concurrent first registrations share one server Each concurrent first call created its own http.Server and the last one won, so the others listened until the extension host exited and dispose never closed them. One pending start is shared; a start superseded by dispose closes its server. [PROF-UI-LIFETIME] --- .../lsp-profiler-ui/src/profile-server.ts | 53 ++++++++++++------- .../src/test/suite/profile-server.test.ts | 41 ++++++++++++++ 2 files changed, 74 insertions(+), 20 deletions(-) diff --git a/packages/lsp-profiler-ui/src/profile-server.ts b/packages/lsp-profiler-ui/src/profile-server.ts index d54bc33..875550d 100644 --- a/packages/lsp-profiler-ui/src/profile-server.ts +++ b/packages/lsp-profiler-ui/src/profile-server.ts @@ -40,11 +40,14 @@ interface Registration { } interface LoopbackServerState { + /** The one start in flight or done; concurrent callers share it, so no server is orphaned. */ + starting: Promise | undefined; server: http.Server | undefined; - serverPort: number | undefined; + /** Bumped by dispose, so a start still binding when it ran closes its server. */ + epoch: number; } -const serverState: LoopbackServerState = { server: undefined, serverPort: undefined }; +const serverState: LoopbackServerState = { starting: undefined, server: undefined, epoch: 0 }; const registrations = new Map(); /** @@ -70,36 +73,46 @@ export async function serveProfileForBrowser( export function disposeProfileServer(): void { serverState.server?.close(); serverState.server = undefined; - serverState.serverPort = undefined; + serverState.starting = undefined; + serverState.epoch += 1; registrations.clear(); } -/** Start the loopback server if needed and resolve its port. */ +/** Start the loopback server once and resolve its port; concurrent callers share the start. */ async function ensureServer(): Promise { - if (serverState.server !== undefined && serverState.serverPort !== undefined) { - return serverState.serverPort; + serverState.starting ??= startServer(serverState.epoch); + return serverState.starting; +} + +/** Bind a new loopback server and adopt it; a failed start may be retried. */ +async function startServer(epoch: number): Promise { + try { + const created = http.createServer((req, res) => { + void handleRequest(req, res); + }); + created.on("error", (error: Error) => { + Logger.warn(`Profile loopback server error: ${error.message}`); + }); + // Port 0 = ephemeral; loopback only โ€” never reachable off-machine. + created.listen(0, "127.0.0.1"); + await once(created, "listening"); + return adoptListening(created, epoch); + } catch (error) { + if (epoch === serverState.epoch) { + serverState.starting = undefined; + } + throw error; } - const created = http.createServer((req, res) => { - void handleRequest(req, res); - }); - created.on("error", (error: Error) => { - Logger.warn(`Profile loopback server error: ${error.message}`); - }); - // Port 0 = ephemeral; loopback only โ€” never reachable off-machine. - created.listen(0, "127.0.0.1"); - await once(created, "listening"); - return adoptListening(created); } /** Keep the server once it is listening on a port; one bound without a port is closed. */ -function adoptListening(created: http.Server): number { +function adoptListening(created: http.Server, epoch: number): number { const address = created.address(); - if (address === null || typeof address === "string") { + if (address === null || typeof address === "string" || epoch !== serverState.epoch) { created.close(); - throw new Error("profile loopback server bound without a port"); + throw new Error("profile loopback server bound without a port, or disposed while starting"); } serverState.server = created; - serverState.serverPort = address.port; return address.port; } diff --git a/packages/lsp-profiler-ui/src/test/suite/profile-server.test.ts b/packages/lsp-profiler-ui/src/test/suite/profile-server.test.ts index cfefe4f..61deb35 100644 --- a/packages/lsp-profiler-ui/src/test/suite/profile-server.test.ts +++ b/packages/lsp-profiler-ui/src/test/suite/profile-server.test.ts @@ -16,6 +16,9 @@ import path from "node:path"; import { disposeProfileServer, serveProfileForBrowser } from "../../profile-server"; import { removeTestDir } from './test-helpers'; +/** First registrations racing to start the loopback server. */ +const CONCURRENT_REGISTRATIONS = 4; + /** GET a URL and resolve status, headers, and body. */ async function fetchUrl( url: string, @@ -82,6 +85,44 @@ suite("Profile loopback server โ€” speedscope deep links load automatically", () assert.strictEqual(response.status, 404, "expired registrations must 404, not serve stale data"); }); + test("concurrent first registrations share one server, and dispose closes it ([PROF-UI-LIFETIME])", async () => { + disposeProfileServer(); + const urls = await Promise.all( + Array.from({ length: CONCURRENT_REGISTRATIONS }, async () => serveProfileForBrowser(profilePath)), + ); + const ports = new Set(urls.map((url) => new URL(url).port)); + assert.strictEqual( + ports.size, + 1, + `every concurrent caller must get the one server; ports ${[...ports].join(", ")} mean ` + + "the extra servers were orphaned and never closed", + ); + const responses = await Promise.all(urls.map(async (url) => fetchUrl(url))); + for (const [index, response] of responses.entries()) { + assert.strictEqual(response.status, 200, `each concurrent registration must be served: ${urls[index] ?? ""}`); + assert.strictEqual(response.body, profileJson, "each must serve the exact exported JSON"); + } + disposeProfileServer(); + await Promise.all( + urls.map(async (url) => + assert.rejects(fetchUrl(url), `after dispose no server may still listen behind ${url}`), + ), + ); + }); + + test("a dispose while the server is still binding closes it; the next registration starts afresh", async () => { + disposeProfileServer(); + const pending = serveProfileForBrowser(profilePath); + disposeProfileServer(); + await assert.rejects(pending, /disposed while starting/u, "a start superseded by dispose must not be adopted"); + const url = await serveProfileForBrowser(profilePath); + const response = await fetchUrl(url); + assert.strictEqual(response.status, 200, "a registration after dispose must start a working server"); + assert.strictEqual(response.body, profileJson); + disposeProfileServer(); + await assert.rejects(fetchUrl(url), "dispose must close the fresh server too"); + }); + test("dispose tears the server down โ€” nothing stays reachable after deactivate", async () => { const url = await serveProfileForBrowser(profilePath); disposeProfileServer(); From 335f32a8a665d7869b911870680a90cd54c8d0ba Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:50:41 +1100 Subject: [PATCH 03/15] Makefile: profiler-ui targets run under Windows PowerShell (no && chaining) (#23) --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7cd82df..773a63d 100644 --- a/Makefile +++ b/Makefile @@ -214,11 +214,12 @@ doc: ## that `cargo build` generates). profiler-ui: cargo build -p lsp-profiler --locked - cd packages/lsp-profiler-ui && npm ci && npm run build + npm ci --prefix packages/lsp-profiler-ui + npm run build --prefix packages/lsp-profiler-ui ## profiler-ui-test: Test the UI library against a mock language server. profiler-ui-test: profiler-ui - cd packages/lsp-profiler-ui && npm test + npm test --prefix packages/lsp-profiler-ui # [WEB-CONTENT] Share dependency setup; npm owns the build and dev commands. _website_setup: From ac1751c4cf09d9564f1a8f866ca025b0509484ee Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:51:26 +1100 Subject: [PATCH 04/15] live::watcher docs: drop the exclusion rules it never had --- crates/lspkit-server/src/live/watcher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/lspkit-server/src/live/watcher.rs b/crates/lspkit-server/src/live/watcher.rs index a8d303c..37b0e41 100644 --- a/crates/lspkit-server/src/live/watcher.rs +++ b/crates/lspkit-server/src/live/watcher.rs @@ -1,8 +1,8 @@ //! Filesystem watcher that emits coarse change events. //! //! Wraps [`notify`] and forwards events to a tokio channel so consumers can -//! await them with the usual async machinery. Exclusion rules let callers -//! ignore noisy directories (`target/`, `.git/`, vendor caches, etc.). +//! await them with the usual async machinery. Every change under the watched +//! root is reported; callers filter the paths they do not care about. use std::path::{Path, PathBuf}; use std::sync::mpsc as std_mpsc; From 4dd0da2dd0440b41412576b603a8353a0ef1c62e Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:53:44 +1100 Subject: [PATCH 05/15] Live scheduler: one waiting trigger per busy consumer, bounded backlog The trigger channel was unbounded: a slow analyzer under sustained churn queued one Trigger (with every event path) per quiet window, without limit. Now at most one trigger waits; later bursts merge into it, and past MAX_PENDING_EVENTS it becomes one Rescan. spawn_scheduler returns a bounded mpsc::Receiver. [MEM-LIFETIME-SCHEDULER-BACKLOG] --- crates/lspkit-server/src/live.rs | 4 +- crates/lspkit-server/src/live/scheduler.rs | 45 +++++++++--- crates/lspkit-server/src/live/session.rs | 2 +- crates/lspkit-server/src/live/watcher.rs | 2 +- crates/lspkit-server/tests/live_scheduler.rs | 73 +++++++++++++++++++- crates/lspkit-server/tests/live_session.rs | 10 +-- docs/specs/lspkit-spec.md | 1 + 7 files changed, 116 insertions(+), 21 deletions(-) diff --git a/crates/lspkit-server/src/live.rs b/crates/lspkit-server/src/live.rs index 9994fe0..e8677b9 100644 --- a/crates/lspkit-server/src/live.rs +++ b/crates/lspkit-server/src/live.rs @@ -9,6 +9,8 @@ pub mod scheduler; pub mod session; pub mod watcher; -pub use crate::live::scheduler::{spawn as spawn_scheduler, ScheduleConfig, Trigger}; +pub use crate::live::scheduler::{ + spawn as spawn_scheduler, ScheduleConfig, Trigger, MAX_PENDING_EVENTS, +}; pub use crate::live::session::{Analyzer, AnalyzerError, AnalyzerResult, Session}; pub use crate::live::watcher::{ChangeEvent, FileWatcher, WatcherError}; diff --git a/crates/lspkit-server/src/live/scheduler.rs b/crates/lspkit-server/src/live/scheduler.rs index bc3b569..6e656dc 100644 --- a/crates/lspkit-server/src/live/scheduler.rs +++ b/crates/lspkit-server/src/live/scheduler.rs @@ -13,6 +13,11 @@ use tokio::time; use crate::live::watcher::ChangeEvent; +/// Most events one waiting trigger holds while its consumer is still busy; past +/// this the trigger becomes one [`ChangeEvent::Rescan`]. +/// [MEM-LIFETIME-SCHEDULER-BACKLOG] +pub const MAX_PENDING_EVENTS: usize = 1024; + /// Scheduler configuration. #[non_exhaustive] #[derive(Debug, Clone, Copy)] @@ -43,14 +48,16 @@ pub struct Trigger { /// Run a debouncing single-flight scheduler. /// /// Reads change events from `events_rx`, coalesces them per `config`, and -/// pushes one [`Trigger`] per quiet window to the returned receiver. The task -/// ends when the events end or the receiver is dropped. +/// pushes one [`Trigger`] per quiet window to the returned receiver. At most +/// one trigger waits for a busy consumer: later bursts merge into the next +/// trigger, bounded by [`MAX_PENDING_EVENTS`]. The task ends when the events +/// end or the receiver is dropped. #[must_use] pub fn spawn( events_rx: mpsc::UnboundedReceiver, config: ScheduleConfig, -) -> mpsc::UnboundedReceiver { - let (tx, rx) = mpsc::unbounded_channel(); +) -> mpsc::Receiver { + let (tx, rx) = mpsc::channel(1); let _scheduler = tokio::spawn(run(events_rx, tx, config)); rx } @@ -72,16 +79,18 @@ struct Burst { events: Vec, /// When the burst's first event arrived. first_event_at: Option, + /// The window elapsed; the burst waits only for the consumer. + ready: bool, } impl Burst { - /// Wait for the next event, or for the window of a buffered burst. + /// Wait for the next event, or for the window of a buffered, unready burst. async fn next( &self, events_rx: &mut mpsc::UnboundedReceiver, config: ScheduleConfig, ) -> Step { - let event = if self.events.is_empty() { + let event = if self.events.is_empty() || self.ready { events_rx.recv().await } else { let deadline = compute_deadline(self.first_event_at, config); @@ -93,17 +102,25 @@ impl Burst { event.map_or(Step::Ended, Step::Event) } - /// Buffer one event, starting the burst's cap window on the first. + /// Buffer one event, starting the burst's cap window on the first. A ready + /// burst past [`MAX_PENDING_EVENTS`] is one `Rescan`, which covers every change. fn push(&mut self, event: ChangeEvent) { if self.events.is_empty() { self.first_event_at = Some(time::Instant::now()); } - self.events.push(event); + match self.events.as_slice() { + [ChangeEvent::Rescan] if self.ready => {} + events if self.ready && events.len() >= MAX_PENDING_EVENTS => { + self.events = vec![ChangeEvent::Rescan]; + } + _ => self.events.push(event), + } } /// End the burst as one trigger. fn take(&mut self) -> Trigger { self.first_event_at = None; + self.ready = false; Trigger { events: std::mem::take(&mut self.events), } @@ -112,9 +129,11 @@ impl Burst { /// Coalesce events until the source ends or the consumer drops the triggers; /// the task never outlives its consumer. [MEM-LIFETIME-SCHEDULER-TASK] +/// A ready burst waits for the consumer's free slot while it keeps absorbing +/// events. [MEM-LIFETIME-SCHEDULER-BACKLOG] async fn run( mut events_rx: mpsc::UnboundedReceiver, - tx: mpsc::UnboundedSender, + tx: mpsc::Sender, config: ScheduleConfig, ) { let mut burst = Burst::default(); @@ -122,12 +141,16 @@ async fn run( let step = tokio::select! { biased; () = tx.closed() => return, + permit = tx.reserve(), if burst.ready => match permit { + Ok(permit) => { permit.send(burst.take()); continue; } + Err(_) => return, + }, step = burst.next(&mut events_rx, config) => step, }; match step { Step::Event(event) => burst.push(event), - Step::Quiet if tx.send(burst.take()).is_ok() => {} - Step::Quiet | Step::Ended => return, + Step::Quiet => burst.ready = true, + Step::Ended => return, } } } diff --git a/crates/lspkit-server/src/live/session.rs b/crates/lspkit-server/src/live/session.rs index 878b8ba..c42c6ed 100644 --- a/crates/lspkit-server/src/live/session.rs +++ b/crates/lspkit-server/src/live/session.rs @@ -79,7 +79,7 @@ impl Session { /// on success. Returns when `triggers` closes. pub async fn run( self, - mut triggers: tokio::sync::mpsc::UnboundedReceiver, + mut triggers: tokio::sync::mpsc::Receiver, analyzer: A, ) { while let Some(trigger) = triggers.recv().await { diff --git a/crates/lspkit-server/src/live/watcher.rs b/crates/lspkit-server/src/live/watcher.rs index 37b0e41..f73bdec 100644 --- a/crates/lspkit-server/src/live/watcher.rs +++ b/crates/lspkit-server/src/live/watcher.rs @@ -48,7 +48,7 @@ pub struct FileWatcher { #[derive(Debug)] pub struct CoalescedWatcher { /// Yields batches after the shared scheduler's quiet or capped window. - triggers: mpsc::UnboundedReceiver, + triggers: mpsc::Receiver, /// Owns the async pump so dropping this value stops reading native events. reader: tokio::task::JoinHandle<()>, } diff --git a/crates/lspkit-server/tests/live_scheduler.rs b/crates/lspkit-server/tests/live_scheduler.rs index b7467cc..de5fea7 100644 --- a/crates/lspkit-server/tests/live_scheduler.rs +++ b/crates/lspkit-server/tests/live_scheduler.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use std::time::Duration; -use lspkit_server::live::{spawn_scheduler, ChangeEvent, ScheduleConfig, Trigger}; +use lspkit_server::live::{spawn_scheduler, ChangeEvent, ScheduleConfig, Trigger, MAX_PENDING_EVENTS}; use tokio::sync::mpsc; use tokio::time; @@ -18,7 +18,7 @@ const CAP: Duration = Duration::from_millis(500); /// Build a fresh scheduler with default config. fn build() -> ( mpsc::UnboundedSender, - mpsc::UnboundedReceiver, + mpsc::Receiver, ) { let (tx, rx) = mpsc::unbounded_channel(); let triggers = spawn_scheduler(rx, ScheduleConfig::default()); @@ -212,3 +212,72 @@ async fn the_scheduler_exits_when_its_consumer_drops_the_triggers() { "the scheduler task outlived its consumer and still holds the event receiver" ); } + +/// Paths of every path-carrying event in `trigger`, in order. +fn paths_of(trigger: &Trigger) -> Vec { + trigger + .events + .iter() + .flat_map(|event| match event { + ChangeEvent::Touched(paths) | ChangeEvent::Removed(paths) => paths.clone(), + _ => Vec::new(), + }) + .collect() +} + +/// [MEM-LIFETIME-SCHEDULER-BACKLOG] While the consumer is still analysing, later +/// bursts merge into one waiting trigger instead of queueing one trigger each, +/// and the waiting trigger is delivered as soon as the consumer reads again. +#[tokio::test(flavor = "current_thread", start_paused = true)] +async fn bursts_behind_a_busy_consumer_merge_into_one_waiting_trigger() { + let (tx, mut triggers) = build(); + for name in ["a.rs", "b.rs", "c.rs", "d.rs"] { + send_and_quiet(&tx, touched(name)).await.expect("send"); + } + let first = triggers.recv().await.expect("first trigger"); + assert_eq!(paths_of(&first), vec![PathBuf::from("a.rs")]); + settle().await; + let merged = triggers.recv().await.expect("merged trigger"); + assert_eq!( + paths_of(&merged), + ["b.rs", "c.rs", "d.rs"].map(PathBuf::from).to_vec(), + "every change made while the consumer was busy arrives, in order, in one trigger" + ); + settle().await; + assert!( + triggers.try_recv().is_err(), + "a busy consumer must never have more than one trigger waiting" + ); + send_and_quiet(&tx, removed("e.rs")).await.expect("send"); + let after = triggers.recv().await.expect("trigger after catching up"); + assert_eq!(paths_of(&after), vec![PathBuf::from("e.rs")]); + assert!(matches!(after.events.as_slice(), [ChangeEvent::Removed(_)])); +} + +/// [MEM-LIFETIME-SCHEDULER-BACKLOG] A consumer that stays busy through endless churn +/// holds at most `MAX_PENDING_EVENTS` waiting events: past that the waiting +/// trigger becomes one `Rescan`, which covers every change it replaced. +#[tokio::test(flavor = "current_thread", start_paused = true)] +async fn a_backlog_past_the_bound_collapses_to_one_rescan() { + let (tx, mut triggers) = build(); + send_and_quiet(&tx, touched("first.rs")).await.expect("send"); + send_and_quiet(&tx, touched("waiting.rs")).await.expect("send"); + for index in 0..MAX_PENDING_EVENTS + 10 { + tx.send(touched(&format!("churn{index}.rs"))).expect("send"); + } + settle().await; + let first = triggers.recv().await.expect("first trigger"); + assert_eq!(paths_of(&first), vec![PathBuf::from("first.rs")]); + settle().await; + let collapsed = triggers.recv().await.expect("collapsed trigger"); + assert!( + matches!(collapsed.events.as_slice(), [ChangeEvent::Rescan]), + "an overflowing backlog is exactly one Rescan, got {} events", + collapsed.events.len() + ); + settle().await; + assert!(triggers.try_recv().is_err(), "nothing else may be queued"); + send_and_quiet(&tx, touched("fresh.rs")).await.expect("send"); + let fresh = triggers.recv().await.expect("trigger after the rescan"); + assert_eq!(paths_of(&fresh), vec![PathBuf::from("fresh.rs")]); +} diff --git a/crates/lspkit-server/tests/live_session.rs b/crates/lspkit-server/tests/live_session.rs index 7967582..2a2b4d3 100644 --- a/crates/lspkit-server/tests/live_session.rs +++ b/crates/lspkit-server/tests/live_session.rs @@ -119,9 +119,9 @@ async fn run_advances_generation_on_each_success() { let analyzer = OkAnalyzer { calls: Arc::clone(&calls), }; - let (tx, rx) = mpsc::unbounded_channel(); + let (tx, rx) = mpsc::channel(triggers.len()); for t in triggers { - tx.send(t).expect("send"); + tx.try_send(t).expect("send"); } drop(tx); @@ -137,9 +137,9 @@ async fn run_does_not_advance_on_analyzer_failure() { let triggers = produce_triggers(2).await.expect("two real triggers"); let session = Session::new(); - let (tx, rx) = mpsc::unbounded_channel(); + let (tx, rx) = mpsc::channel(triggers.len()); for t in triggers { - tx.send(t).expect("send"); + tx.try_send(t).expect("send"); } drop(tx); @@ -152,7 +152,7 @@ async fn run_does_not_advance_on_analyzer_failure() { #[tokio::test] async fn run_handles_empty_trigger_stream() { let session = Session::new(); - let (tx, rx) = mpsc::unbounded_channel::(); + let (tx, rx) = mpsc::channel::(1); drop(tx); let observer = session.clone(); session diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index c311f43..cf084ca 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -115,6 +115,7 @@ Language servers run for days, so an allocation retained after the work that nee - **[MEM-LIFETIME-QUEUED-REQUEST]** `CancellationHandle::queue` registers a request like `register` and returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control. A terminal preparation error (an unknown method) releases its ingress registration. `register` stays dispatcher-owned until preparation. - **[MEM-LIFETIME-DIAGNOSTICS-SINK]** `DiagnosticsBus::attach_owned` returns a `SinkRegistration`; dropping it detaches the sink. `attach` stays for sinks that live as long as the bus. - **[MEM-LIFETIME-SCHEDULER-TASK]** The live scheduler's task ends when its consumer drops the trigger receiver, not only when the events end. +- **[MEM-LIFETIME-SCHEDULER-BACKLOG]** At most one trigger waits for a busy consumer (`spawn_scheduler` returns a bounded `mpsc::Receiver`). Bursts that complete while it waits merge, in order, into the next trigger, which holds at most `MAX_PENDING_EVENTS` (1024) events; past that it is one `ChangeEvent::Rescan`. A consumer that keeps up sees one trigger per burst, as before. - **[MEM-LIFETIME-EXAMPLE-TASKS]** The reference server's `Tasks` owns its request tasks in a `JoinSet`, reaped per spawn and aborted when the session ends. Its handlers never capture the dispatcher that stores them, and its initialized watcher is owned and aborted by the session. - **[MEM-LIFETIME-PULL-ADMIT]** The reference server forgets a pulled URI that is neither open nor a JSON file in the watched root, after pulls, scans and watched-file changes, and `didClose` releases a document's source buffer and its pull state ([LSP-PULL-FORGET]). - **[MEM-LIFETIME-EXAMPLE-OUTBOX]** One queued workspace-change notification covers every change until the output pump dequeues it, so a stalled editor never accumulates them. From be3b82e48e34611813348243fcf3002df129cc0c Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:56:49 +1100 Subject: [PATCH 06/15] CPU sessions: an ended recording no longer blocks its PID; atomic PID check ensure_free counted ended recordings (kept only so stop can return them), so a timed-out session refused every new session on its process until eviction. The check also ran under a separate lock from the insert, so two concurrent starts on one PID could both succeed. Now only recording sessions hold a PID, and the check and insert share one lock. [PROF-CPU-SESSIONS] --- crates/lsp-profiler/src/session.rs | 38 +++++++++++++------ .../src/session_lifetime_tests.rs | 25 ++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/crates/lsp-profiler/src/session.rs b/crates/lsp-profiler/src/session.rs index 3168793..7001378 100644 --- a/crates/lsp-profiler/src/session.rs +++ b/crates/lsp-profiler/src/session.rs @@ -215,20 +215,13 @@ impl std::fmt::Debug for CpuSessionManager { } impl CpuSessionManager { - /// Fail when `pid` already has a session. + /// Fail when `pid` already has a session that is still recording. /// /// # Errors /// /// Returns [`ProfilerError::AlreadyProfiling`]. pub async fn ensure_free(&self, pid: u32) -> Result<(), ProfilerError> { - let sessions = self.sessions.lock().await; - match sessions.iter().find(|(_, entry)| entry.pid == pid) { - Some((session_id, _)) => Err(ProfilerError::AlreadyProfiling { - pid, - session_id: session_id.clone(), - }), - None => Ok(()), - } + refuse_recorded(&*self.sessions.lock().await, pid) } /// Register a freshly attached sampler as a session. @@ -243,16 +236,17 @@ impl CpuSessionManager { sample_rate: u64, limit: Option, ) -> Result { - self.ensure_free(sampler.pid).await?; let session = CpuSession::new(runtime, sampler, sample_rate, limit); let started = session.started(); - self.register(session).await; + self.register(session).await?; info!(session_id = %started.session_id, pid = started.pid, runtime = %started.runtime, sample_rate, "profiling session started"); Ok(started) } /// Insert the stable identity without holding the registry across recording work. - async fn register(&self, session: CpuSession) { + /// The PID check and the insert share one lock, so concurrent starts on one + /// process cannot both succeed. + async fn register(&self, session: CpuSession) -> Result<(), ProfilerError> { let session_id = session.session_id.clone(); let entry = Arc::new(SessionEntry { pid: session.sampler.pid, @@ -260,8 +254,10 @@ impl CpuSessionManager { state: Mutex::new(Some(session)), }); let mut sessions = self.sessions.lock().await; + refuse_recorded(&sessions, entry.pid)?; let _ = sessions.insert(session_id, entry); evict_ended(&mut sessions); + Ok(()) } /// Stop a session, wait for its producer's last batch, and return the recording. @@ -385,6 +381,24 @@ fn evict_ended(sessions: &mut HashMap>) { } } +/// Refuse `pid` while a session still records it. An ended recording, kept +/// only so `stop` can return it, does not hold its process. [PROF-CPU-SESSIONS] +fn refuse_recorded( + sessions: &HashMap>, + pid: u32, +) -> Result<(), ProfilerError> { + match sessions + .iter() + .find(|(_, entry)| entry.pid == pid && ended_at(entry).is_none()) + { + Some((session_id, _)) => Err(ProfilerError::AlreadyProfiling { + pid, + session_id: session_id.clone(), + }), + None => Ok(()), + } +} + /// When an idle, ended session started; `None` while it records or is busy. fn ended_at(entry: &SessionEntry) -> Option { let state = entry.state.try_lock().ok()?; diff --git a/crates/lsp-profiler/src/session_lifetime_tests.rs b/crates/lsp-profiler/src/session_lifetime_tests.rs index 6600ec0..82dba26 100644 --- a/crates/lsp-profiler/src/session_lifetime_tests.rs +++ b/crates/lsp-profiler/src/session_lifetime_tests.rs @@ -144,3 +144,28 @@ async fn the_newest_ended_recording_is_still_returned_by_stop() -> Result<(), St } Ok(()) } + +/// [PROF-CPU-SESSIONS] A recording that ended (its time limit passed or its +/// target exited) is kept only so `stop` can still return it; it must not +/// block a new session on the same process. +#[tokio::test] +async fn an_ended_recording_does_not_block_a_new_session_on_its_pid() -> Result<(), String> { + const PID: u32 = 7; + let manager = CpuSessionManager::default(); + let runtime: Arc = Arc::new(Recorded); + let first = finished_session(&manager, &runtime, PID, Some(Duration::ZERO)).await?; + let second = finished_session(&manager, &runtime, PID, None).await?; + if first == second { + return Err("the second session reused the first session's id".to_owned()); + } + for id in [first, second] { + let recorded = manager.stop(&id).await.map_err(|error| error.to_string())?; + if recorded.data.total_samples != 1 { + return Err(format!( + "{id} returned {} samples", + recorded.data.total_samples + )); + } + } + Ok(()) +} From ad77f7d166272236aec4cd843ad8369d7c0ccd90 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:01:10 +1100 Subject: [PATCH 07/15] Consumer kit: ship dotnet/Directory.Build.props so its fixtures build alone (#42) The v0.10.1 kit's HotLeakApp failed to build outside the repo (CS0246: List<>) because ImplicitUsings and the rest of the shared settings live in dotnet/Directory.Build.props, which the kit omitted. CI's kit check only listed files; scripts/consumer-kit-check.sh now also builds every fixture from a fresh extraction of the kit. [REL-CONSUMER-ASSETS] --- .github/workflows/ci.yml | 10 +-------- docs/specs/lspkit-spec.md | 9 +++++++- scripts/consumer-kit-check.sh | 42 +++++++++++++++++++++++++++++++++++ scripts/release-assets.sh | 4 +++- 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 scripts/consumer-kit-check.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dd64cd9..5effe09 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,15 +112,7 @@ jobs: # here keeps a broken script from surfacing only at release time. # [REL-CONSUMER-ASSETS] - name: Release consumer assets (linux-x64) - run: | - set -eu - sh scripts/release-assets.sh ci "${RUNNER_TEMP}/assets" linux-x64 - tar -tzf "${RUNNER_TEMP}/assets/lsp-profiler-dotnet-engine-ci-linux-x64.tar.gz" | grep -qx './lsp-profiler-dotnet-engine' - kit=$(tar -tzf "${RUNNER_TEMP}/assets/lspkit-consumer-kit-ci.tar.gz") - for want in dotnet/fixtures/HotLeakApp/Program.cs docs/specs/LSP-PROFILER-SPEC.md docs/specs/LSP-PULL-SPEC.md test-support/state.rs scripts/typediagram/run-cli.mjs; do - printf '%s\n' "$kit" | grep -qx "lspkit-consumer-kit-ci/$want" - done - if printf '%s\n' "$kit" | grep -q '/bin/\|/obj/'; then echo "kit ships build output"; exit 1; fi + run: sh scripts/consumer-kit-check.sh "${RUNNER_TEMP}/assets" linux-x64 - name: Upload build artifacts uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index cf084ca..45a1900 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -94,6 +94,13 @@ pub trait EngineApi: Send + Sync + 'static { - **[JSONRPC-FRAME-LIMIT]** `read_message` accepts a body of at most `MAX_FRAME_BYTES` (64 MiB); `read_message_within` takes a server-chosen cap. A larger `Content-Length` is `FramingError::FrameTooLarge`: its body is discarded through a fixed-size buffer, never allocated, so the connection stays usable and the next read starts at the following frame. A length beyond `u64` is `BadHeader`, and the body is read incrementally, so memory follows the bytes that arrive, never the claimed length. A body shorter than its claim is `Io(UnexpectedEof)`. The header block, every line and terminator included, is read through one `MAX_HEADER_BYTES` (8 KiB) budget, so one endless line and endless short lines both end in `HeaderTooLarge` with memory bounded by the cap; no frame boundary is known after it, so the connection cannot continue. Peer input never panics. - **[LSP-OUTBOUND]** Serverโ†’client requests (`lspkit-server::outbound::Outbound`): `request` allocates a numeric JSON-RPC id from the sidecar correlator and returns the request frame plus an `OutboundResponse` future; once ids pass `i64::MAX` it returns `IdsExhausted` instead of reusing a number. `null` params are omitted from the frame. `resolve` routes an inbound response to its waiter as the editor's `result` (absent or `null` is `Value::Null`) or its `JsonRpcError`. Requests and notifications are `NotAResponse`; ids never allocated, already answered or abandoned are `UnknownId`. Dropping a wait unregisters it ([SIDECAR-REQUEST-LIFETIME]). `sidecar::RequestId` rebuilds from its raw wire number with `From`, and `JsonRpcError` implements `Display` and `std::error::Error`. - **Position encoding** (`lspkit::vfs::PositionEncoding`): negotiates encoding at `initialize` per LSP 3.17 client capabilities; converts on every read; round-trips byte/char offsets correctly across multi-byte scalars and surrogate pairs. +- **Position-aware text primitives** (`lspkit::vfs::text`): generic word and column helpers over plain text plus the negotiated `PositionEncoding`. Classification is per Unicode scalar value โ€” no regexes, no language knowledge. + - **[VFS-TEXT-IDENTIFIER]** An identifier character is a Unicode `XID_Continue` scalar (via the `unicode-ident` crate), which covers letters, digits, `_` and combining marks; an identifier is a non-empty run of identifier characters. Decomposed text forms one run: `e` + `U+0301` is the identifier `e\u{0301}`. Emoji and other symbols are never identifier characters. + - **[VFS-TEXT-WORD]** `word_at(text, position, encoding)` returns the identifier run under the cursor as text plus a `Range` (line and encoded columns), or `None`. A line beyond the document or a column beyond the line's encoded width is `None`; so is a cursor with no run at or immediately behind it. A column inside a multi-unit character resolves to that character. A cursor on an identifier character selects its run; a cursor on any other character, or at end of line, selects the run ending exactly at that boundary โ€” never crossing a non-identifier character. + - **[VFS-TEXT-OCCURRENCES]** `occurrences(text, name, encoding)` scans identifier runs line by line (`\n` line breaks) and returns one `Range` per run equal to `name`, so `foo` never matches inside `foobar`, `_foo` or `foo2`. A `name` that is not a valid identifier yields no occurrences; matching is case-sensitive and never spans lines. + - **[VFS-TEXT-MEASURE]** `char_width(ch, encoding)` is the position units one scalar occupies โ€” UTF-8 bytes, UTF-16 code units (2 for surrogate pairs), one UTF-32 unit โ€” and `measure(text, encoding)` is its saturating sum over a text. + - **[VFS-TEXT-PREFIX]** `prefix_to(line, character, encoding)` returns the line's UTF-8 prefix up to an encoded column boundary; `None` when the column splits a multi-unit character or lies beyond the line. +- **[LSP-HOVER-MARKDOWN]** `lspkit_server::hover_markdown(Option)` builds the `textDocument/hover` result `{ "contents": { "kind": "markdown", "value": โ€ฆ } }` as a plain `serde_json::Value`; `None` is JSON `null` (no hover). No protocol type appears in its signature. - **URI โ†” `PathBuf`** (`lspkit-server::uri`): handles `file://`, percent-decoding, Windows drive letters, UNC paths, and non-UTF-8 paths. - **[URI-AUTHORITY]** A URI's authority is its host, never a path segment. An empty or `localhost` host names a local path; any other host names a UNC path (`file://server/share/a.rs` โ†” `\\server\share\a.rs`) on Windows and is an error elsewhere. `uri_to_path` never returns a relative path. `path_to_uri` removes the extended-length prefixes `\\?\` and `\\?\UNC\` that `std::fs::canonicalize` adds. - **[URI-WINDOWS]** All path โ†” URI handling in the lspkit tree lives in `lspkit-server::uri`; nothing else parses or builds a file path from a URI. On Windows one file has one URI, and every URI decodes to one exact, absolute path: @@ -162,7 +169,7 @@ lspkit runs a **release-only** regime modelled on [Shipwright](https://github.co - **[REL-GITHUB]** The release attaches every crate tarball, every npm tarball, a CycloneDX SBOM and one `SHA256SUMS` over them all, cosign keyless-signed (`SHA256SUMS.sigstore.json`). Build provenance and the SBOM are attested against `SHA256SUMS`, so consumers can run `gh attestation verify --repo Nimblesite/lspkit`. `[SWR-SEC-CHECKSUM]` `[SWR-SEC-PROVENANCE]` `[SWR-SEC-SBOM]` - **[REL-CONSUMER-ASSETS]** Nothing a consumer needs requires a source checkout. `scripts/release-assets.sh` attaches two more kinds of asset to the release, which `SHA256SUMS`, cosign and the attestations cover like every other asset: - `lsp-profiler-dotnet-engine--.tar.gz` for `linux-x64`, `linux-arm64`, `osx-x64`, `osx-arm64`, `win-x64` and `win-arm64`: the framework-dependent .NET engine that `lsp-profiler-dotnet` launches. Point `LSP_PROFILER_DOTNET_ENGINE` at the extracted `lsp-profiler-dotnet-engine` executable, or place it beside the language server ([PROF-DOTNET-ENGINE-LOCATE]). - - `lspkit-consumer-kit-.tar.gz`: the profiler fixtures (`dotnet/fixtures`, as source), `LSP-PROFILER-SPEC.md`, `LSP-PULL-SPEC.md`, `test-support/` and `scripts/typediagram/run-cli.mjs`. + - `lspkit-consumer-kit-.tar.gz`: the profiler fixtures (`dotnet/fixtures`, as source) with the `dotnet/Directory.Build.props` they build with, `LSP-PROFILER-SPEC.md`, `LSP-PULL-SPEC.md`, `test-support/` and `scripts/typediagram/run-cli.mjs`. CI runs `scripts/consumer-kit-check.sh`, which builds every fixture from a fresh extraction of the kit outside the repository. The `lsp-profiler-dotnet` e2e suites honour `LSP_PROFILER_DOTNET_ENGINE`, so they run unchanged against a released engine. - **[REL-OIDC]** crates.io and npm both publish through OIDC Trusted Publishing from `release.yml` in the protected `release` environment. The repo stores no `CARGO_REGISTRY_TOKEN` or `NPM_TOKEN`. npm packages also publish with `--provenance`. `[SWR-SEC-OIDC-PUBLISH]` diff --git a/scripts/consumer-kit-check.sh b/scripts/consumer-kit-check.sh new file mode 100644 index 0000000..793a4af --- /dev/null +++ b/scripts/consumer-kit-check.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# [REL-CONSUMER-ASSETS] Check the release assets a consumer downloads instead +# of checking out the source (#42): build them for one runtime id, then build +# every fixture from the extracted consumer kit alone, outside this repository. +# +# Usage: scripts/consumer-kit-check.sh +set -eu + +if [ "$#" -ne 2 ]; then + echo "usage: $0 " >&2 + exit 2 +fi +dist=$1 +rid=$2 +root=$(cd "$(dirname "$0")/.." && pwd) + +sh "$root/scripts/release-assets.sh" ci "$dist" "$rid" +tar -tzf "$dist/lsp-profiler-dotnet-engine-ci-$rid.tar.gz" | grep -qx './lsp-profiler-dotnet-engine' + +kit=$(tar -tzf "$dist/lspkit-consumer-kit-ci.tar.gz") +for want in dotnet/Directory.Build.props dotnet/fixtures/HotLeakApp/Program.cs \ + dotnet/fixtures/HotLeakAppFs/Program.fs docs/specs/LSP-PROFILER-SPEC.md \ + docs/specs/LSP-PULL-SPEC.md test-support/state.rs scripts/typediagram/run-cli.mjs; do + if ! printf '%s\n' "$kit" | grep -qx "lspkit-consumer-kit-ci/$want"; then + echo "consumer kit lacks $want" >&2 + exit 1 + fi +done +if printf '%s\n' "$kit" | grep -q '/bin/\|/obj/'; then + echo "consumer kit ships build output" >&2 + exit 1 +fi + +# A consumer has only the kit: build each fixture from a fresh extraction. +outside=$(mktemp -d) +trap 'rm -rf "$outside"' EXIT +tar -xzf "$dist/lspkit-consumer-kit-ci.tar.gz" -C "$outside" +for fixture in HotLeakApp HotLeakAppFs; do + dotnet build "$outside/lspkit-consumer-kit-ci/dotnet/fixtures/$fixture" \ + --configuration Release --nologo +done +echo "consumer kit: engine, files and standalone fixture builds OK" diff --git a/scripts/release-assets.sh b/scripts/release-assets.sh index 910ef02..74c975c 100644 --- a/scripts/release-assets.sh +++ b/scripts/release-assets.sh @@ -8,7 +8,8 @@ # LSP_PROFILER_DOTNET_ENGINE at the extracted executable, or place it # beside the language server. # lspkit-consumer-kit-.tar.gz -# The profiler fixtures (dotnet/fixtures), the specs consumers cite +# The profiler fixtures (dotnet/fixtures) with the dotnet/Directory.Build.props +# they build with, so they build from the kit alone; the specs consumers cite # (LSP-PROFILER-SPEC.md, LSP-PULL-SPEC.md), test-support/ and # scripts/typediagram/run-cli.mjs. # @@ -53,6 +54,7 @@ kit="$work/lspkit-consumer-kit-$version" mkdir -p "$kit/dotnet" "$kit/docs/specs" "$kit/scripts/typediagram" # Fixtures ship as source; their build output never does. tar -cf - -C "$root/dotnet" --exclude=bin --exclude=obj fixtures | tar -xf - -C "$kit/dotnet" +cp "$root/dotnet/Directory.Build.props" "$kit/dotnet/" cp "$root/docs/specs/LSP-PROFILER-SPEC.md" "$root/docs/specs/LSP-PULL-SPEC.md" "$kit/docs/specs/" cp -R "$root/test-support" "$kit/test-support" cp "$root/scripts/typediagram/run-cli.mjs" "$kit/scripts/typediagram/" From 9c3908661ca11b4d431fafe8f5fb6ad84f8176e7 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:09:50 +1100 Subject: [PATCH 08/15] Dispatch: remove the unguarded CancellationHandle::register A request registered with register() and never prepared (rejected by the consumer, peer gone, loop stuck) kept its control and interest strings until the Dispatcher dropped: 32 abandoned 64 KiB registrations stayed in_flight. queue() and its QueuedRequest guard are now the only pre-dispatch registration. [MEM-LIFETIME-QUEUED-REQUEST] [LSP-PULL-CANCELLATION] --- .../examples/diagnostic-reference/lifetime.rs | 11 +++-- .../src/dispatch/cancellation.rs | 31 +++----------- crates/lspkit-server/tests/dispatch.rs | 5 ++- .../lspkit-server/tests/dispatch_heap_leak.rs | 40 ++++++++++++------- .../tests/dispatch_memory_retention.rs | 23 +++-------- crates/lspkit-server/tests/live_scheduler.rs | 21 ++++++---- docs/specs/LSP-PULL-SPEC.md | 6 ++- docs/specs/lspkit-spec.md | 2 +- 8 files changed, 65 insertions(+), 74 deletions(-) diff --git a/crates/lspkit-server/examples/diagnostic-reference/lifetime.rs b/crates/lspkit-server/examples/diagnostic-reference/lifetime.rs index b3a0644..6cab89e 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/lifetime.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/lifetime.rs @@ -96,19 +96,22 @@ fn render(result: HandlerResult) -> Outcome { /// A queued request's second registration must adopt its earlier client cancellation. async fn interrupted(dispatcher: &Dispatcher, ingress: &CancellationHandle) -> Outcome { let id = RequestId::Number(3); - ingress.register(id.clone(), None)?; + let first = ingress.queue(id.clone(), None)?; ingress.cancel(&id); - ingress.register(id.clone(), None)?; - render(dispatcher.dispatch(&id, METHOD, Value::Null).await?) + let second = ingress.queue(id.clone(), None)?; + let outcome = render(dispatcher.dispatch(&id, METHOD, Value::Null).await?); + drop((first, second)); + outcome } /// Reader-side controls can arrive after their foreground owner has naturally exited. fn late_inputs(ingress: &CancellationHandle) -> Outcome<()> { let id = RequestId::Number(4); - ingress.register(id.clone(), None)?; + let queued = ingress.queue(id.clone(), None)?; ingress.cancel(&id); ingress.supersede(METHOD, ""); ingress.supersede_all(); + drop(queued); Ok(()) } diff --git a/crates/lspkit-server/src/dispatch/cancellation.rs b/crates/lspkit-server/src/dispatch/cancellation.rs index a542747..3b4f235 100644 --- a/crates/lspkit-server/src/dispatch/cancellation.rs +++ b/crates/lspkit-server/src/dispatch/cancellation.rs @@ -22,31 +22,12 @@ impl CancellationHandle { } } - /// Register protocol interest before forwarding to a possibly occupied foreground loop. - /// `Dispatcher::prepare_call` adopts this same control rather than replacing it. - /// Completed controls are released by the prepared future; queued controls live with - /// the dispatcher. An exited owner is harmless. [LSP-PULL-CANCELLATION] - /// - /// # Errors - /// Returns a registration error if the registry is unavailable. - pub fn register( - &self, - id: RequestId, - interest: Option, - ) -> Result<(), DispatchError> { - let Some(registry) = self.registry.upgrade() else { - return Ok(()); - }; - let Ok(mut guard) = registry.write() else { - return Err(DispatchError::RegistrationPoisoned(id)); - }; - register(&mut guard, id, interest); - Ok(()) - } - - /// [`register`](Self::register) `id` and own the queued control: dropping - /// the guard releases it unless a prepared request adopted it. - /// [MEM-LIFETIME-QUEUED-REQUEST] + /// Register protocol interest before forwarding to a possibly occupied foreground + /// loop, and own the queued control. `Dispatcher::prepare_call` adopts this same + /// control rather than replacing it, and the prepared future releases it. Dropping + /// the guard first releases it, so a request rejected or abandoned before + /// preparation leaves nothing behind. An exited owner is harmless. + /// [LSP-PULL-CANCELLATION] [MEM-LIFETIME-QUEUED-REQUEST] /// /// # Errors /// Returns a registration error if the registry is unavailable. diff --git a/crates/lspkit-server/tests/dispatch.rs b/crates/lspkit-server/tests/dispatch.rs index 63ca1ce..3942d9b 100644 --- a/crates/lspkit-server/tests/dispatch.rs +++ b/crates/lspkit-server/tests/dispatch.rs @@ -230,12 +230,13 @@ async fn unknown_method_with_inflight_duplicate_id_keeps_inflight_cancellable() ); assert!(format!("{dispatcher:?}").contains("in_flight: 0")); let ingress_only = RequestId::Number(8); - dispatcher + let queued = dispatcher .cancellation_handle() - .register(ingress_only.clone(), None) + .queue(ingress_only.clone(), None) .expect("ingress registration"); assert!(format!("{dispatcher:?}").contains("in_flight: 1")); let rejected = dispatcher.prepare(ingress_only, "missing", Value::Null); assert!(matches!(rejected, Err(DispatchError::UnknownMethod(_)))); assert!(format!("{dispatcher:?}").contains("in_flight: 0")); + drop(queued); } diff --git a/crates/lspkit-server/tests/dispatch_heap_leak.rs b/crates/lspkit-server/tests/dispatch_heap_leak.rs index 55510d9..7eb43e7 100644 --- a/crates/lspkit-server/tests/dispatch_heap_leak.rs +++ b/crates/lspkit-server/tests/dispatch_heap_leak.rs @@ -1,6 +1,8 @@ //! [LSP-PULL-CANCELLATION] [MEM-LIFETIME]: terminal errors release actual heap allocations. -use lspkit_server::{DispatchError, Dispatcher, HandlerResult, RequestId, RequestInterest}; +use lspkit_server::{ + DispatchError, Dispatcher, HandlerResult, QueuedRequest, RequestId, RequestInterest, +}; use serde_json::Value; #[path = "../../../test-support/state.rs"] @@ -14,28 +16,31 @@ const INPUT_BYTES: usize = 64 * 1024; const METHOD: &str = "memory/probe"; /// Populate the registry without retaining a second copy of the input allocations. -fn register_inputs(dispatcher: &Dispatcher) -> Result<(), DispatchError> { - for id in 0..REQUESTS { - dispatcher.cancellation_handle().register( - RequestId::Number(id), - Some(RequestInterest { - method: METHOD.to_owned(), - resource: id.to_string(), - input: Some("x".repeat(INPUT_BYTES)), - }), - )?; - } - Ok(()) +fn register_inputs(dispatcher: &Dispatcher) -> Result, DispatchError> { + let handle = dispatcher.cancellation_handle(); + (0..REQUESTS) + .map(|id| { + handle.queue( + RequestId::Number(id), + Some(RequestInterest { + method: METHOD.to_owned(), + resource: id.to_string(), + input: Some("x".repeat(INPUT_BYTES)), + }), + ) + }) + .collect() } /// Warm map capacity with successful prepared/dropped lifetimes before measuring errors. fn warmed_dispatcher() -> Result { let dispatcher = Dispatcher::new(); dispatcher.register(METHOD, |_, _| async { HandlerResult::Ok(Value::Null) }); - register_inputs(&dispatcher)?; + let queued = register_inputs(&dispatcher)?; for id in 0..REQUESTS { drop(dispatcher.prepare(RequestId::Number(id), METHOD, Value::Null)?); } + drop(queued); Ok(dispatcher) } @@ -43,10 +48,15 @@ fn warmed_dispatcher() -> Result { fn terminal_lookup_errors_free_heap_before_shutdown_or_registry_inspection() { let dispatcher = warmed_dispatcher().expect("warm registry capacity"); state::assert_releases_heap(|| { - register_inputs(&dispatcher).expect("register real input allocations"); + let queued = register_inputs(&dispatcher).expect("register real input allocations"); for id in 0..REQUESTS { let result = dispatcher.prepare(RequestId::Number(id), "unknown", Value::Null); assert!(matches!(result, Err(DispatchError::UnknownMethod(_)))); } + assert!( + format!("{dispatcher:?}").contains("in_flight: 0"), + "the terminal errors released every entry before any guard dropped" + ); + drop(queued); }); } diff --git a/crates/lspkit-server/tests/dispatch_memory_retention.rs b/crates/lspkit-server/tests/dispatch_memory_retention.rs index 436b332..8ab83f1 100644 --- a/crates/lspkit-server/tests/dispatch_memory_retention.rs +++ b/crates/lspkit-server/tests/dispatch_memory_retention.rs @@ -17,20 +17,6 @@ const UNKNOWN_METHOD: &str = "textDocument/neverRegistered"; /// Real handler used by both successful lifetime controls. const KNOWN_METHOD: &str = "textDocument/known"; -/// Register owned heap strings through the public protocol handle. -fn pre_register(handle: &CancellationHandle) { - for id in 0..REQUESTS { - let interest = RequestInterest { - method: UNKNOWN_METHOD.to_owned(), - resource: format!("file:///memory-audit/{id}"), - input: Some("x".repeat(INPUT_BYTES)), - }; - assert!(handle - .register(RequestId::Number(id), Some(interest)) - .is_ok()); - } -} - /// A real registered handler without any captured dispatcher or test allocations. fn known_dispatcher() -> Dispatcher { let dispatcher = Dispatcher::new(); @@ -49,19 +35,20 @@ fn assert_released(actual: &Dispatcher, empty: &Dispatcher) { #[test] fn unknown_method_releases_pre_registered_interest() { let dispatcher = Dispatcher::new(); - pre_register(&dispatcher.cancellation_handle()); + let queued = queue(&dispatcher.cancellation_handle()).expect("queue the requests"); for id in 0..REQUESTS { let result = dispatcher.prepare(RequestId::Number(id), UNKNOWN_METHOD, Value::Null); assert!(matches!(result, Err(DispatchError::UnknownMethod(_)))); } dispatcher.cancel_all(); assert_released(&dispatcher, &Dispatcher::new()); + drop(queued); } #[tokio::test] async fn completed_requests_release_pre_registered_interest() { let dispatcher = known_dispatcher(); - pre_register(&dispatcher.cancellation_handle()); + let queued = queue(&dispatcher.cancellation_handle()).expect("queue the requests"); for id in 0..REQUESTS { let result = dispatcher .prepare(RequestId::Number(id), KNOWN_METHOD, Value::Null) @@ -70,12 +57,13 @@ async fn completed_requests_release_pre_registered_interest() { assert!(matches!(result, HandlerResult::Ok(Value::Null))); } assert_released(&dispatcher, &known_dispatcher()); + drop(queued); } #[test] fn dropped_futures_release_pre_registered_interest() { let dispatcher = known_dispatcher(); - pre_register(&dispatcher.cancellation_handle()); + let queued = queue(&dispatcher.cancellation_handle()).expect("queue the requests"); for id in 0..REQUESTS { let future = dispatcher .prepare(RequestId::Number(id), KNOWN_METHOD, Value::Null) @@ -83,6 +71,7 @@ fn dropped_futures_release_pre_registered_interest() { drop(future); } assert_released(&dispatcher, &known_dispatcher()); + drop(queued); } /// Queue owned heap strings through the public protocol handle. diff --git a/crates/lspkit-server/tests/live_scheduler.rs b/crates/lspkit-server/tests/live_scheduler.rs index de5fea7..07c72b8 100644 --- a/crates/lspkit-server/tests/live_scheduler.rs +++ b/crates/lspkit-server/tests/live_scheduler.rs @@ -8,7 +8,9 @@ use std::path::PathBuf; use std::time::Duration; -use lspkit_server::live::{spawn_scheduler, ChangeEvent, ScheduleConfig, Trigger, MAX_PENDING_EVENTS}; +use lspkit_server::live::{ + spawn_scheduler, ChangeEvent, ScheduleConfig, Trigger, MAX_PENDING_EVENTS, +}; use tokio::sync::mpsc; use tokio::time; @@ -16,10 +18,7 @@ const QUIET: Duration = Duration::from_millis(100); const CAP: Duration = Duration::from_millis(500); /// Build a fresh scheduler with default config. -fn build() -> ( - mpsc::UnboundedSender, - mpsc::Receiver, -) { +fn build() -> (mpsc::UnboundedSender, mpsc::Receiver) { let (tx, rx) = mpsc::unbounded_channel(); let triggers = spawn_scheduler(rx, ScheduleConfig::default()); (tx, triggers) @@ -260,8 +259,12 @@ async fn bursts_behind_a_busy_consumer_merge_into_one_waiting_trigger() { #[tokio::test(flavor = "current_thread", start_paused = true)] async fn a_backlog_past_the_bound_collapses_to_one_rescan() { let (tx, mut triggers) = build(); - send_and_quiet(&tx, touched("first.rs")).await.expect("send"); - send_and_quiet(&tx, touched("waiting.rs")).await.expect("send"); + send_and_quiet(&tx, touched("first.rs")) + .await + .expect("send"); + send_and_quiet(&tx, touched("waiting.rs")) + .await + .expect("send"); for index in 0..MAX_PENDING_EVENTS + 10 { tx.send(touched(&format!("churn{index}.rs"))).expect("send"); } @@ -277,7 +280,9 @@ async fn a_backlog_past_the_bound_collapses_to_one_rescan() { ); settle().await; assert!(triggers.try_recv().is_err(), "nothing else may be queued"); - send_and_quiet(&tx, touched("fresh.rs")).await.expect("send"); + send_and_quiet(&tx, touched("fresh.rs")) + .await + .expect("send"); let fresh = triggers.recv().await.expect("trigger after the rescan"); assert_eq!(paths_of(&fresh), vec![PathBuf::from("fresh.rs")]); } diff --git a/docs/specs/LSP-PULL-SPEC.md b/docs/specs/LSP-PULL-SPEC.md index 7024042..80bb218 100644 --- a/docs/specs/LSP-PULL-SPEC.md +++ b/docs/specs/LSP-PULL-SPEC.md @@ -66,8 +66,10 @@ Completion, cancellation and dropped work release that registration. Shutdown cancels all outstanding requests. A workspace scan checks cancellation and its snapshot revision between documents and before emitting partial or final results. -An ingress reader can register replaceable request interest before forwarding a -request to the foreground loop. Preparation adopts that same registration, so +An ingress reader can queue replaceable request interest before forwarding a +request to the foreground loop (`CancellationHandle::queue`, which returns a +`QueuedRequest` guard; dropping it before preparation releases the registration, +[MEM-LIFETIME-QUEUED-REQUEST]). Preparation adopts that same registration, so cancellation received while queued remains effective. A changed input identity supersedes older work only for the same method and canonical resource. The first interruption wins: client cancellation remains `-32800`; supersession returns diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index 45a1900..d096ec4 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -119,7 +119,7 @@ Language servers run for days, so an allocation retained after the work that nee - **[MEM-LIFETIME-PARSE-CACHE]** `treesitter::ParseCache` keeps at most `MAX_TREES` (128) trees and evicts the oldest first, so an edited document never keeps one tree per keystroke. - **[MEM-LIFETIME-SIDECAR-NOTIFICATIONS]** `sidecar::inbound` buffers at most `NOTIFICATION_CAPACITY` (1024) unread notifications; later ones are dropped, counted in `Inbound::dropped_notifications` and logged, so a consumer that only reads responses never accumulates them and the reader never blocks. -- **[MEM-LIFETIME-QUEUED-REQUEST]** `CancellationHandle::queue` registers a request like `register` and returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control. A terminal preparation error (an unknown method) releases its ingress registration. `register` stays dispatcher-owned until preparation. +- **[MEM-LIFETIME-QUEUED-REQUEST]** `CancellationHandle::queue` is the only way to register a request before dispatch. It returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control, so a request rejected or abandoned before preparation leaves nothing behind. Once prepared, the request owns the control and releases it on completion, cancellation or drop. A terminal preparation error (an unknown method) releases its registration. There is no unguarded registration. - **[MEM-LIFETIME-DIAGNOSTICS-SINK]** `DiagnosticsBus::attach_owned` returns a `SinkRegistration`; dropping it detaches the sink. `attach` stays for sinks that live as long as the bus. - **[MEM-LIFETIME-SCHEDULER-TASK]** The live scheduler's task ends when its consumer drops the trigger receiver, not only when the events end. - **[MEM-LIFETIME-SCHEDULER-BACKLOG]** At most one trigger waits for a busy consumer (`spawn_scheduler` returns a bounded `mpsc::Receiver`). Bursts that complete while it waits merge, in order, into the next trigger, which holds at most `MAX_PENDING_EVENTS` (1024) events; past that it is one `ChangeEvent::Rescan`. A consumer that keeps up sees one trigger per burst, as before. From 07eca7b2e9dfbb40438f7b099f9ac0425add293e Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:12:47 +1100 Subject: [PATCH 09/15] lspkit::vfs text primitives and lspkit_server::hover_markdown (#2) word_at (identifier run under a cursor), occurrences (whole-word matches), char_width / measure / prefix_to, all in the negotiated PositionEncoding. Identifier characters are Unicode XID_Continue (unicode-ident), so combining marks stay inside their word. hover_markdown builds the markdown hover result or null. Table tests permute UTF-8/16/32 across ASCII, CJK, emoji and combining marks. [VFS-TEXT] [LSP-HOVER-MARKDOWN] --- Cargo.lock | 1 + Cargo.toml | 1 + crates/lspkit-server/src/hover.rs | 17 + crates/lspkit-server/src/lib.rs | 2 + crates/lspkit-server/tests/hover.rs | 48 +++ crates/lspkit/Cargo.toml | 2 + crates/lspkit/src/vfs.rs | 2 + crates/lspkit/src/vfs/edit.rs | 4 +- crates/lspkit/src/vfs/text.rs | 241 +++++++++++++ crates/lspkit/tests/vfs_text.rs | 376 +++++++++++++++++++++ crates/lspkit/tests/vfs_text_common/mod.rs | 10 + crates/lspkit/tests/vfs_text_scan.rs | 282 ++++++++++++++++ 12 files changed, 985 insertions(+), 1 deletion(-) create mode 100644 crates/lspkit-server/src/hover.rs create mode 100644 crates/lspkit-server/tests/hover.rs create mode 100644 crates/lspkit/src/vfs/text.rs create mode 100644 crates/lspkit/tests/vfs_text.rs create mode 100644 crates/lspkit/tests/vfs_text_common/mod.rs create mode 100644 crates/lspkit/tests/vfs_text_scan.rs diff --git a/Cargo.lock b/Cargo.lock index edf08bc..87a3e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -601,6 +601,7 @@ dependencies = [ "tracing-subscriber", "tree-sitter", "tree-sitter-rust", + "unicode-ident", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 04d6de8..78ac6e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -133,6 +133,7 @@ lsp-types = "0.97" # Document / state dashmap = "6" ropey = "1" +unicode-ident = "1.0.24" # Filesystem notify = "8" diff --git a/crates/lspkit-server/src/hover.rs b/crates/lspkit-server/src/hover.rs new file mode 100644 index 0000000..8ac2520 --- /dev/null +++ b/crates/lspkit-server/src/hover.rs @@ -0,0 +1,17 @@ +//! Hover reply helpers. [LSP-HOVER-MARKDOWN] + +use serde_json::{json, Value}; + +/// Build a `textDocument/hover` result carrying markdown contents. +/// +/// `Some(value)` becomes `{ "contents": { "kind": "markdown", "value" } }`; +/// `None` becomes JSON `null`, the protocol's "no hover" result. The reply is +/// a plain [`Value`] so no protocol type leaks into a public signature. +/// [LSP-HOVER-MARKDOWN] +#[must_use] +pub fn hover_markdown(contents: Option) -> Value { + match contents { + Some(value) => json!({ "contents": { "kind": "markdown", "value": value } }), + None => Value::Null, + } +} diff --git a/crates/lspkit-server/src/lib.rs b/crates/lspkit-server/src/lib.rs index fde6b1a..a47e8c5 100644 --- a/crates/lspkit-server/src/lib.rs +++ b/crates/lspkit-server/src/lib.rs @@ -19,6 +19,7 @@ pub mod capabilities; pub mod diagnostics; pub mod dispatch; +pub mod hover; pub mod jsonrpc; pub mod live; pub mod mcp; @@ -37,6 +38,7 @@ pub use crate::diagnostics::{ pub use crate::dispatch::{ CancellationHandle, DispatchError, Dispatcher, HandlerResult, QueuedRequest, RequestInterest, }; +pub use crate::hover::hover_markdown; pub use crate::jsonrpc::{FramingError, JsonRpcError, Message, MessageWriter, RequestId}; pub use crate::outbound::{Outbound, OutboundError, OutboundResponse, OutboundResult}; pub use crate::progress::{NoopReporter, ProgressReporter, ProgressSink, ProgressUpdate}; diff --git a/crates/lspkit-server/tests/hover.rs b/crates/lspkit-server/tests/hover.rs new file mode 100644 index 0000000..350aea1 --- /dev/null +++ b/crates/lspkit-server/tests/hover.rs @@ -0,0 +1,48 @@ +//! End-to-end tests for [`lspkit_server::hover_markdown`]. [LSP-HOVER-MARKDOWN] + +use lspkit_server::hover_markdown; +use serde_json::json; + +#[test] +fn some_wraps_markdown_in_contents_object() { + let value = hover_markdown(Some("```rust\nlet x = 1;\n```".to_owned())); + assert_eq!( + value, + json!({ + "contents": { + "kind": "markdown", + "value": "```rust\nlet x = 1;\n```" + } + }) + ); + let kind = value + .get("contents") + .and_then(|contents| contents.get("kind")) + .and_then(|kind| kind.as_str()); + assert_eq!(kind, Some("markdown")); +} + +#[test] +fn some_preserves_multiline_markdown_verbatim() { + let markdown = "# Title\n\n- a\n- b\n\nemoji ๐Ÿ˜€ and CJK ๆ•ฐ้‡\n"; + let value = hover_markdown(Some(markdown.to_owned())); + let stored = value + .get("contents") + .and_then(|contents| contents.get("value")) + .and_then(|value| value.as_str()); + assert_eq!(stored, Some(markdown)); +} + +#[test] +fn empty_string_still_emits_markdown_contents() { + let value = hover_markdown(Some(String::new())); + assert_eq!( + value, + json!({ "contents": { "kind": "markdown", "value": "" } }) + ); +} + +#[test] +fn none_is_json_null() { + assert_eq!(hover_markdown(None), serde_json::Value::Null); +} diff --git a/crates/lspkit/Cargo.toml b/crates/lspkit/Cargo.toml index 74635fb..2ad29e4 100644 --- a/crates/lspkit/Cargo.toml +++ b/crates/lspkit/Cargo.toml @@ -29,6 +29,8 @@ thiserror.workspace = true tokio.workspace = true tokio-util.workspace = true toml.workspace = true +# Identifier classification for position-aware text primitives. [VFS-TEXT-IDENTIFIER] +unicode-ident.workspace = true # Dropped sidecar notifications are logged. [MEM-LIFETIME-SIDECAR-NOTIFICATIONS] tracing.workspace = true diff --git a/crates/lspkit/src/vfs.rs b/crates/lspkit/src/vfs.rs index da1b815..215f923 100644 --- a/crates/lspkit/src/vfs.rs +++ b/crates/lspkit/src/vfs.rs @@ -8,7 +8,9 @@ pub mod document; pub mod edit; pub mod encoding; +pub mod text; pub use crate::vfs::document::{DocumentUri, DocumentVersion, Vfs, VfsError}; pub use crate::vfs::edit::{EditError, Position, Range, TextEdit}; pub use crate::vfs::encoding::{negotiate, PositionEncoding, UnknownEncoding}; +pub use crate::vfs::text::{char_width, measure, occurrences, prefix_to, word_at, WordSpan}; diff --git a/crates/lspkit/src/vfs/edit.rs b/crates/lspkit/src/vfs/edit.rs index 73f1baf..6208387 100644 --- a/crates/lspkit/src/vfs/edit.rs +++ b/crates/lspkit/src/vfs/edit.rs @@ -145,7 +145,9 @@ fn utf16_to_utf8_offset(line: &str, utf16_units: usize) -> Result Result { +/// +/// Shared with [`crate::vfs::text`] for char-index to byte-offset conversion. +pub(crate) fn utf32_to_utf8_offset(line: &str, code_points: usize) -> Result { line.char_indices().nth(code_points).map_or_else( || { if line.chars().count() == code_points { diff --git a/crates/lspkit/src/vfs/text.rs b/crates/lspkit/src/vfs/text.rs new file mode 100644 index 0000000..af537fc --- /dev/null +++ b/crates/lspkit/src/vfs/text.rs @@ -0,0 +1,241 @@ +//! Position-aware text primitives. [VFS-TEXT] +//! +//! Generic, language-agnostic helpers that operate on plain text plus the +//! negotiated [`PositionEncoding`]: the identifier run under a cursor, every +//! whole-word occurrence of a name, and encoding-aware column measurement. +//! Character classification follows Unicode `XID_Continue` โ€” no regexes. + +use crate::vfs::edit::utf32_to_utf8_offset; +use crate::vfs::edit::{Position, Range}; +use crate::vfs::encoding::PositionEncoding; + +/// An identifier run under a cursor plus its location. [VFS-TEXT-WORD] +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WordSpan { + /// The matched identifier text. + text: String, + /// Half-open `[start, end)` range in the negotiated encoding. + range: Range, +} + +impl WordSpan { + /// The identifier text of this span. + #[must_use] + pub fn text(&self) -> &str { + &self.text + } + + /// The span's location in the document. + #[must_use] + pub fn range(&self) -> Range { + self.range + } +} + +/// An identifier character: Unicode `XID_Continue`, which covers letters, +/// digits, `_` and combining marks. [VFS-TEXT-IDENTIFIER] +fn is_identifier_char(ch: char) -> bool { + unicode_ident::is_xid_continue(ch) +} + +/// A non-empty run of identifier characters. [VFS-TEXT-IDENTIFIER] +fn is_identifier(name: &str) -> bool { + !name.is_empty() && name.chars().all(is_identifier_char) +} + +/// Largest scalar UTF-8 encodes in one byte. +const UTF8_ONE_BYTE_MAX: u32 = 0x7F; +/// Largest scalar UTF-8 encodes in two bytes. +const UTF8_TWO_BYTE_MAX: u32 = 0x7FF; +/// Largest Basic Multilingual Plane scalar: three UTF-8 bytes, one UTF-16 unit. +const BMP_MAX: u32 = 0xFFFF; + +/// Position units one scalar occupies in `encoding`. [VFS-TEXT-MEASURE] +#[must_use] +pub fn char_width(ch: char, encoding: PositionEncoding) -> u32 { + let scalar = u32::from(ch); + match encoding { + PositionEncoding::Utf8 => utf8_width(scalar), + PositionEncoding::Utf16 if scalar > BMP_MAX => 2, + PositionEncoding::Utf16 | PositionEncoding::Utf32 => 1, + } +} + +/// UTF-8 bytes of a scalar value. +const fn utf8_width(scalar: u32) -> u32 { + match scalar { + s if s <= UTF8_ONE_BYTE_MAX => 1, + s if s <= UTF8_TWO_BYTE_MAX => 2, + s if s <= BMP_MAX => 3, + _ => 4, + } +} + +/// Encoded width of `text`: the saturating sum of its char widths. +/// [VFS-TEXT-MEASURE] +#[must_use] +pub fn measure(text: &str, encoding: PositionEncoding) -> u32 { + text.chars().fold(0u32, |total, ch| { + total.saturating_add(char_width(ch, encoding)) + }) +} + +/// The `number`-th line of `text`, split on `\n`. `None` when out of range. +fn line_at(text: &str, line: u32) -> Option<&str> { + let index = usize::try_from(line).ok()?; + text.split('\n').nth(index) +} + +/// One maximal identifier run: `(start char, end char, start units, end units)`. +type Run = (usize, usize, u32, u32); + +/// Each char with its index and encoded span start and width. +fn encoded_chars( + line: &str, + encoding: PositionEncoding, +) -> impl Iterator + '_ { + let mut index = 0usize; + let mut units = 0u32; + line.chars().map(move |ch| { + let width = char_width(ch, encoding); + let entry = (index, units, width, ch); + index += 1; + units = units.saturating_add(width); + entry + }) +} + +/// Every maximal identifier run on a line, with encoded unit spans. +/// [VFS-TEXT-IDENTIFIER] +fn identifier_runs(line: &str, encoding: PositionEncoding) -> Vec { + let mut runs = Vec::new(); + let mut open: Option = None; + for (index, start_units, width, ch) in encoded_chars(line, encoding) { + let end_units = start_units.saturating_add(width); + open = match (open, is_identifier_char(ch)) { + (None, true) => Some((index, index + 1, start_units, end_units)), + (Some((start, _, run_start, _)), true) => { + Some((start, index + 1, run_start, end_units)) + } + (Some(run), false) => { + runs.push(run); + None + } + (None, false) => None, + }; + } + runs.extend(open); + runs +} + +/// The UTF-8 text of one run, or `None` on an impossible index. +fn run_text<'a>(line: &'a str, run: &Run) -> Option<&'a str> { + let start = utf32_to_utf8_offset(line, run.0).ok()?; + let end = utf32_to_utf8_offset(line, run.1).ok()?; + line.get(start..end) +} + +/// The identifier run at a position. [VFS-TEXT-WORD] +/// +/// A cursor on an identifier character selects its run; a cursor on any other +/// character, or at end of line, selects the run ending exactly at that +/// boundary. `None` when the column is beyond the line's encoded width or no +/// run qualifies. A column inside a multi-unit character resolves to that +/// character. +#[must_use] +pub fn word_at(text: &str, position: Position, encoding: PositionEncoding) -> Option { + let line = line_at(text, position.line)?; + let cursor = char_index_at(line, position.character, encoding)?; + let run = run_at_cursor(&identifier_runs(line, encoding), cursor)?; + let range = Range::new( + Position::new(position.line, run.2), + Position::new(position.line, run.3), + ); + Some(WordSpan { + text: run_text(line, &run)?.to_owned(), + range, + }) +} + +/// The identifier run selected by a cursor already resolved to a char index. +/// [VFS-TEXT-WORD] +fn run_at_cursor(runs: &[Run], cursor: usize) -> Option { + runs.iter() + .copied() + .find(|&(start, end, _, _)| start <= cursor && cursor < end) + .or_else(|| { + runs.iter() + .copied() + .rev() + .find(|&(_, end, _, _)| end == cursor) + }) +} + +/// Resolve an encoded column to a char index on `line`. +/// +/// A column inside a multi-unit character resolves to that character; the +/// end-of-line boundary resolves to the char count. `None` beyond the line. +fn char_index_at(line: &str, character: u32, encoding: PositionEncoding) -> Option { + encoded_chars(line, encoding) + .find(|&(_, start, width, _)| start.saturating_add(width) > character) + .map(|(index, ..)| index) + .or_else(|| (measure(line, encoding) == character).then(|| line.chars().count())) +} + +/// Every whole-word occurrence of `name`, in document order. [VFS-TEXT-OCCURRENCES] +/// +/// Only whole identifier runs match, so `foo` never matches inside `foobar`, +/// `_foo` or `foo2`. A `name` that is not a valid identifier yields no +/// occurrences. Matching is case-sensitive and never spans lines. +#[must_use] +pub fn occurrences(text: &str, name: &str, encoding: PositionEncoding) -> Vec { + if !is_identifier(name) { + return Vec::new(); + } + (0_u32..) + .zip(text.split('\n')) + .flat_map(|(line_number, line)| line_occurrences(line, name, encoding, line_number)) + .collect() +} + +/// Ranges of whole-word matches of `name` on one line. [VFS-TEXT-OCCURRENCES] +fn line_occurrences( + line: &str, + name: &str, + encoding: PositionEncoding, + line_number: u32, +) -> Vec { + identifier_runs(line, encoding) + .into_iter() + .filter(|run| run_text(line, run).is_some_and(|text| text == name)) + .map(|(_, _, start, end)| { + Range::new( + Position::new(line_number, start), + Position::new(line_number, end), + ) + }) + .collect() +} + +/// The line's prefix up to an encoded column boundary. [VFS-TEXT-PREFIX] +/// +/// `None` when `character` splits a multi-unit character or lies beyond the +/// line's encoded width. +#[must_use] +pub fn prefix_to(line: &str, character: u32, encoding: PositionEncoding) -> Option<&str> { + line.get(..prefix_byte_at(line, character, encoding)?) +} + +/// UTF-8 byte offset of an exact encoded-column boundary. [VFS-TEXT-PREFIX] +fn prefix_byte_at(line: &str, character: u32, encoding: PositionEncoding) -> Option { + encoded_chars(line, encoding) + .find(|&(_, start_units, _, _)| start_units == character) + .and_then(|(index, _, _, _)| utf32_to_utf8_offset(line, index).ok()) + .or_else(|| whole_line_boundary(line, character, encoding)) +} + +/// The whole-line boundary when `character` equals the line's encoded width. +fn whole_line_boundary(line: &str, character: u32, encoding: PositionEncoding) -> Option { + (measure(line, encoding) == character).then_some(line.len()) +} diff --git a/crates/lspkit/tests/vfs_text.rs b/crates/lspkit/tests/vfs_text.rs new file mode 100644 index 0000000..9996ff4 --- /dev/null +++ b/crates/lspkit/tests/vfs_text.rs @@ -0,0 +1,376 @@ +//! Integration tests for `lspkit::vfs::text::word_at`. [VFS-TEXT-WORD] +//! +//! Tables permute `PositionEncoding` ร— script (ASCII, emoji, CJK, combining +//! marks) ร— cursor placement: start/middle/end of word, between words, empty +//! lines, out-of-range line and character, underscores and digits. + +use lspkit::vfs::{word_at, Position, PositionEncoding, Range}; + +#[path = "vfs_text_common/mod.rs"] +mod vfs_text_common; + +use vfs_text_common::ALL_ENCODINGS; + +/// Expected word result: (word, line, start character, end character). +type Word = (&'static str, u32, u32, u32); + +/// One `word_at` table row. +#[derive(Clone)] +struct WordCase { + /// Label used in failure messages. + label: &'static str, + /// Whole document text. + text: &'static str, + /// Cursor (line, character) in `encoding` units. + cursor: (u32, u32), + /// Encoding of the cursor and reported span. + encoding: PositionEncoding, + /// The identifier under the cursor and its span, or `None`. + expected: Option, +} + +/// Multi-line ASCII document: `"let alpha = beta2;\ngamma3 delta"`. +const ASCII_DOC: &str = "let alpha = beta2;\ngamma3 delta"; + +/// Underscore/digit document: runs `foo_bar foo1 _baz ab12cd`. +const SYMBOL_DOC: &str = "foo_bar foo1 _baz ab12cd"; + +/// CJK document: `"ๆ•ฐ้‡ = value"`. +const CJK_DOC: &str = "ๆ•ฐ้‡ = value"; + +/// Emoji document: `"x๐Ÿ˜€ foo"`. +const EMOJI_DOC: &str = "x๐Ÿ˜€ foo"; + +/// Combining-mark document: `"e\u{0301} foo"` (decomposed รฉ). +const COMBINING_DOC: &str = "e\u{0301} foo"; + +/// Mixed-script identifier document: `"ๆ—ฅๆœฌ่ชž123 is_one"`. +const MIXED_DOC: &str = "ๆ—ฅๆœฌ่ชž123 is_one"; + +/// Cursor placements over pure-ASCII lines, permuted across encodings. +fn placement_cases() -> Vec { + let mut cases = Vec::new(); + for encoding in ALL_ENCODINGS { + cases.extend_from_slice(&[ + WordCase { + label: "keyword start", + text: ASCII_DOC, + cursor: (0, 0), + encoding, + expected: Some(("let", 0, 0, 3)), + }, + WordCase { + label: "word start boundary", + text: ASCII_DOC, + cursor: (0, 4), + encoding, + expected: Some(("alpha", 0, 4, 9)), + }, + WordCase { + label: "word middle", + text: ASCII_DOC, + cursor: (0, 6), + encoding, + expected: Some(("alpha", 0, 4, 9)), + }, + WordCase { + label: "boundary after word falls back to run", + text: ASCII_DOC, + cursor: (0, 9), + encoding, + expected: Some(("alpha", 0, 4, 9)), + }, + WordCase { + label: "cursor on terminator after identifier", + text: ASCII_DOC, + cursor: (0, 17), + encoding, + expected: Some(("beta2", 0, 12, 17)), + }, + WordCase { + label: "end of line past terminator has no run", + text: ASCII_DOC, + cursor: (0, 18), + encoding, + expected: None, + }, + WordCase { + label: "digit-suffixed identifier", + text: ASCII_DOC, + cursor: (0, 15), + encoding, + expected: Some(("beta2", 0, 12, 17)), + }, + WordCase { + label: "second line between words", + text: ASCII_DOC, + cursor: (1, 6), + encoding, + expected: Some(("gamma3", 1, 0, 6)), + }, + WordCase { + label: "second line word start", + text: ASCII_DOC, + cursor: (1, 7), + encoding, + expected: Some(("delta", 1, 7, 12)), + }, + WordCase { + label: "end of last line", + text: ASCII_DOC, + cursor: (1, 12), + encoding, + expected: Some(("delta", 1, 7, 12)), + }, + WordCase { + label: "out-of-range character", + text: ASCII_DOC, + cursor: (0, 19), + encoding, + expected: None, + }, + ]); + } + cases +} + +/// Document shapes: underscores, digits, empty lines, permuted across +/// encodings. +fn structure_cases() -> Vec { + let mut cases = Vec::new(); + for encoding in ALL_ENCODINGS { + cases.extend_from_slice(&[ + WordCase { + label: "out-of-range line", + text: ASCII_DOC, + cursor: (2, 0), + encoding, + expected: None, + }, + WordCase { + label: "far out-of-range line and character", + text: ASCII_DOC, + cursor: (7, 40), + encoding, + expected: None, + }, + WordCase { + label: "underscore inside identifier", + text: SYMBOL_DOC, + cursor: (0, 4), + encoding, + expected: Some(("foo_bar", 0, 0, 7)), + }, + WordCase { + label: "identifier with trailing digits", + text: SYMBOL_DOC, + cursor: (0, 10), + encoding, + expected: Some(("foo1", 0, 8, 12)), + }, + WordCase { + label: "leading underscore identifier", + text: SYMBOL_DOC, + cursor: (0, 13), + encoding, + expected: Some(("_baz", 0, 13, 17)), + }, + WordCase { + label: "digits inside identifier", + text: SYMBOL_DOC, + cursor: (0, 21), + encoding, + expected: Some(("ab12cd", 0, 18, 24)), + }, + WordCase { + label: "empty line between words", + text: "foo\n\nbar", + cursor: (1, 0), + encoding, + expected: None, + }, + WordCase { + label: "trailing newline empty line", + text: "foo\nbar\n", + cursor: (2, 0), + encoding, + expected: None, + }, + WordCase { + label: "empty document", + text: "", + cursor: (0, 0), + encoding, + expected: None, + }, + WordCase { + label: "carriage return is not an identifier char", + text: "foo\r\nbar", + cursor: (0, 3), + encoding, + expected: Some(("foo", 0, 0, 3)), + }, + ]); + } + cases +} + +/// CJK and mixed-script rows whose spans differ per encoding. +fn script_cases() -> Vec { + vec![ + WordCase { + label: "cjk identifier start", + text: CJK_DOC, + cursor: (0, 0), + encoding: PositionEncoding::Utf8, + expected: Some(("ๆ•ฐ้‡", 0, 0, 6)), + }, + WordCase { + label: "cjk identifier start", + text: CJK_DOC, + cursor: (0, 0), + encoding: PositionEncoding::Utf16, + expected: Some(("ๆ•ฐ้‡", 0, 0, 2)), + }, + WordCase { + label: "cjk identifier start", + text: CJK_DOC, + cursor: (0, 0), + encoding: PositionEncoding::Utf32, + expected: Some(("ๆ•ฐ้‡", 0, 0, 2)), + }, + WordCase { + label: "word after cjk prefix", + text: CJK_DOC, + cursor: (0, 9), + encoding: PositionEncoding::Utf8, + expected: Some(("value", 0, 9, 14)), + }, + WordCase { + label: "word after cjk prefix", + text: CJK_DOC, + cursor: (0, 5), + encoding: PositionEncoding::Utf16, + expected: Some(("value", 0, 5, 10)), + }, + WordCase { + label: "cursor inside word after cjk prefix", + text: CJK_DOC, + cursor: (0, 7), + encoding: PositionEncoding::Utf32, + expected: Some(("value", 0, 5, 10)), + }, + WordCase { + label: "cjk with digits one run utf-8", + text: MIXED_DOC, + cursor: (0, 0), + encoding: PositionEncoding::Utf8, + expected: Some(("ๆ—ฅๆœฌ่ชž123", 0, 0, 12)), + }, + WordCase { + label: "cjk with digits one run utf-16", + text: MIXED_DOC, + cursor: (0, 3), + encoding: PositionEncoding::Utf16, + expected: Some(("ๆ—ฅๆœฌ่ชž123", 0, 0, 6)), + }, + ] +} + +/// Emoji and combining-mark rows whose cursor resolution differs per +/// encoding. +fn mark_cases() -> Vec { + vec![ + WordCase { + label: "cursor inside surrogate pair resolves to prior word", + text: EMOJI_DOC, + cursor: (0, 2), + encoding: PositionEncoding::Utf16, + expected: Some(("x", 0, 0, 1)), + }, + WordCase { + label: "cursor inside emoji utf-8 bytes resolves to prior word", + text: EMOJI_DOC, + cursor: (0, 3), + encoding: PositionEncoding::Utf8, + expected: Some(("x", 0, 0, 1)), + }, + WordCase { + label: "utf-32 cursor after emoji lands on space", + text: EMOJI_DOC, + cursor: (0, 2), + encoding: PositionEncoding::Utf32, + expected: None, + }, + WordCase { + label: "word after emoji", + text: EMOJI_DOC, + cursor: (0, 6), + encoding: PositionEncoding::Utf8, + expected: Some(("foo", 0, 6, 9)), + }, + WordCase { + label: "word after emoji", + text: EMOJI_DOC, + cursor: (0, 4), + encoding: PositionEncoding::Utf16, + expected: Some(("foo", 0, 4, 7)), + }, + WordCase { + label: "word after emoji", + text: EMOJI_DOC, + cursor: (0, 3), + encoding: PositionEncoding::Utf32, + expected: Some(("foo", 0, 3, 6)), + }, + WordCase { + label: "combining mark continues an identifier utf-16", + text: COMBINING_DOC, + cursor: (0, 1), + encoding: PositionEncoding::Utf16, + expected: Some(("e\u{0301}", 0, 0, 2)), + }, + WordCase { + label: "combining mark mid-scalar snaps back to run utf-8", + text: COMBINING_DOC, + cursor: (0, 2), + encoding: PositionEncoding::Utf8, + expected: Some(("e\u{0301}", 0, 0, 3)), + }, + WordCase { + label: "combining mark continues an identifier utf-32", + text: COMBINING_DOC, + cursor: (0, 1), + encoding: PositionEncoding::Utf32, + expected: Some(("e\u{0301}", 0, 0, 2)), + }, + ] +} + +#[test] +fn word_at_matches_table() { + let mut cases = placement_cases(); + cases.extend(structure_cases()); + cases.extend(script_cases()); + cases.extend(mark_cases()); + for case in cases { + let found = word_at( + case.text, + Position::new(case.cursor.0, case.cursor.1), + case.encoding, + ); + let actual = found.as_ref().map(|word| (word.text(), word.range())); + let expected = case.expected.map(|(text, line, start, end)| { + ( + text, + Range::new(Position::new(line, start), Position::new(line, end)), + ) + }); + let label = format!( + "{label} ({encoding})", + label = case.label, + encoding = case.encoding + ); + assert_eq!(actual, expected, "word_at for {label}"); + } +} diff --git a/crates/lspkit/tests/vfs_text_common/mod.rs b/crates/lspkit/tests/vfs_text_common/mod.rs new file mode 100644 index 0000000..b110d14 --- /dev/null +++ b/crates/lspkit/tests/vfs_text_common/mod.rs @@ -0,0 +1,10 @@ +//! Shared fixtures for the vfs text-primitive test binaries. [VFS-TEXT] + +use lspkit::vfs::PositionEncoding; + +/// The three LSP position encodings, for table-driven permutations. [VFS-TEXT] +pub const ALL_ENCODINGS: [PositionEncoding; 3] = [ + PositionEncoding::Utf8, + PositionEncoding::Utf16, + PositionEncoding::Utf32, +]; diff --git a/crates/lspkit/tests/vfs_text_scan.rs b/crates/lspkit/tests/vfs_text_scan.rs new file mode 100644 index 0000000..7940951 --- /dev/null +++ b/crates/lspkit/tests/vfs_text_scan.rs @@ -0,0 +1,282 @@ +//! Integration tests for `lspkit::vfs::text` occurrence scanning and +//! encoding-aware measurement. [VFS-TEXT-OCCURRENCES] [VFS-TEXT-MEASURE] +//! [VFS-TEXT-PREFIX] + +use lspkit::vfs::{char_width, measure, occurrences, prefix_to, Position, PositionEncoding, Range}; + +#[path = "vfs_text_common/mod.rs"] +mod vfs_text_common; + +use vfs_text_common::ALL_ENCODINGS; + +/// Expected range of one occurrence: (line, start character, end character). +type Span = (u32, u32, u32); + +/// One `occurrences` table row. +#[derive(Clone)] +struct ScanCase { + /// Label used in failure messages. + label: &'static str, + /// Whole document text. + text: &'static str, + /// Name to scan for. + name: &'static str, + /// Encoding of the reported spans. + encoding: PositionEncoding, + /// Every whole-word occurrence, in document order. + expected: &'static [Span], +} + +/// ASCII rows whose columns match in every encoding, permuted across all +/// three. +fn ascii_scan_cases() -> Vec { + let ascii_doc = "let foo = 1;\nlet foobar = foo + _foo;\nfoo"; + let substring_doc = "foobar _foo foo2 foo.bar foo,foo!"; + let mut cases = Vec::new(); + for encoding in ALL_ENCODINGS { + cases.extend_from_slice(&[ + ScanCase { + label: "whole word only across lines", + text: ascii_doc, + name: "foo", + encoding, + expected: &[(0, 4, 7), (1, 13, 16), (2, 0, 3)], + }, + ScanCase { + label: "leading underscore name matches its run", + text: ascii_doc, + name: "_foo", + encoding, + expected: &[(1, 19, 23)], + }, + ScanCase { + label: "name is case sensitive", + text: ascii_doc, + name: "FOO", + encoding, + expected: &[], + }, + ScanCase { + label: "empty name matches nothing", + text: ascii_doc, + name: "", + encoding, + expected: &[], + }, + ScanCase { + label: "name with non-identifier characters matches nothing", + text: ascii_doc, + name: "foo +", + encoding, + expected: &[], + }, + ScanCase { + label: "substring must not match longer runs", + text: substring_doc, + name: "foo", + encoding, + expected: &[(0, 17, 20), (0, 25, 28), (0, 29, 32)], + }, + ScanCase { + label: "empty text matches nothing", + text: "", + name: "foo", + encoding, + expected: &[], + }, + ]); + } + cases +} + +/// Rows whose spans differ per encoding: CJK, emoji, combining marks. +fn script_scan_cases() -> Vec { + vec![ + ScanCase { + label: "cjk name utf-8 spans", + text: "ๆ•ฐ้‡ = 1;\nlet x = ๆ•ฐ้‡;\nๆ•ฐ้‡ๆ•ฐ้‡", + name: "ๆ•ฐ้‡", + encoding: PositionEncoding::Utf8, + expected: &[(0, 0, 6), (1, 8, 14)], + }, + ScanCase { + label: "cjk name utf-16 spans", + text: "ๆ•ฐ้‡ = 1;\nlet x = ๆ•ฐ้‡;\nๆ•ฐ้‡ๆ•ฐ้‡", + name: "ๆ•ฐ้‡", + encoding: PositionEncoding::Utf16, + expected: &[(0, 0, 2), (1, 8, 10)], + }, + ScanCase { + label: "cjk name utf-32 spans", + text: "ๆ•ฐ้‡ = 1;\nlet x = ๆ•ฐ้‡;\nๆ•ฐ้‡ๆ•ฐ้‡", + name: "ๆ•ฐ้‡", + encoding: PositionEncoding::Utf32, + expected: &[(0, 0, 2), (1, 8, 10)], + }, + ScanCase { + label: "emoji separates runs utf-8", + text: "foo๐Ÿ˜€foo foo", + name: "foo", + encoding: PositionEncoding::Utf8, + expected: &[(0, 0, 3), (0, 7, 10), (0, 11, 14)], + }, + ScanCase { + label: "emoji separates runs utf-16", + text: "foo๐Ÿ˜€foo foo", + name: "foo", + encoding: PositionEncoding::Utf16, + expected: &[(0, 0, 3), (0, 5, 8), (0, 9, 12)], + }, + ScanCase { + label: "emoji separates runs utf-32", + text: "foo๐Ÿ˜€foo foo", + name: "foo", + encoding: PositionEncoding::Utf32, + expected: &[(0, 0, 3), (0, 4, 7), (0, 8, 11)], + }, + ScanCase { + label: "combining-mark name matches only equal runs", + text: "e\u{0301} xe\u{0301}", + name: "e\u{0301}", + encoding: PositionEncoding::Utf16, + expected: &[(0, 0, 2)], + }, + ] +} + +#[test] +fn occurrences_match_table() { + let mut cases = ascii_scan_cases(); + cases.extend(script_scan_cases()); + for case in cases { + let found = occurrences(case.text, case.name, case.encoding); + let expected: Vec = case + .expected + .iter() + .map(|&(line, start, end)| { + Range::new(Position::new(line, start), Position::new(line, end)) + }) + .collect(); + let label = format!( + "{label} (name {name}, {encoding})", + label = case.label, + name = case.name, + encoding = case.encoding + ); + assert_eq!(found, expected, "spans for {label}"); + } +} + +#[test] +fn char_width_matches_table() { + let table: &[(char, PositionEncoding, u32)] = &[ + ('a', PositionEncoding::Utf8, 1), + ('a', PositionEncoding::Utf16, 1), + ('a', PositionEncoding::Utf32, 1), + ('_', PositionEncoding::Utf8, 1), + ('_', PositionEncoding::Utf16, 1), + ('_', PositionEncoding::Utf32, 1), + ('รฉ', PositionEncoding::Utf8, 2), + ('รฉ', PositionEncoding::Utf16, 1), + ('รฉ', PositionEncoding::Utf32, 1), + ('\u{0301}', PositionEncoding::Utf8, 2), + ('\u{0301}', PositionEncoding::Utf16, 1), + ('\u{0301}', PositionEncoding::Utf32, 1), + ('ไฝ ', PositionEncoding::Utf8, 3), + ('ไฝ ', PositionEncoding::Utf16, 1), + ('ไฝ ', PositionEncoding::Utf32, 1), + ('๐Ÿ˜€', PositionEncoding::Utf8, 4), + ('๐Ÿ˜€', PositionEncoding::Utf16, 2), + ('๐Ÿ˜€', PositionEncoding::Utf32, 1), + ('ูฃ', PositionEncoding::Utf8, 2), + ('ูฃ', PositionEncoding::Utf16, 1), + ('ูฃ', PositionEncoding::Utf32, 1), + ]; + for &(ch, encoding, expected) in table { + assert_eq!(char_width(ch, encoding), expected, "width of {ch:?}"); + } +} + +#[test] +fn measure_matches_table() { + let table: &[(&str, PositionEncoding, u32)] = &[ + ("", PositionEncoding::Utf8, 0), + ("", PositionEncoding::Utf16, 0), + ("", PositionEncoding::Utf32, 0), + ("let x", PositionEncoding::Utf8, 5), + ("let x", PositionEncoding::Utf16, 5), + ("let x", PositionEncoding::Utf32, 5), + ("๐Ÿ˜€", PositionEncoding::Utf8, 4), + ("๐Ÿ˜€", PositionEncoding::Utf16, 2), + ("๐Ÿ˜€", PositionEncoding::Utf32, 1), + ("a๐Ÿ˜€ไฝ ", PositionEncoding::Utf8, 8), + ("a๐Ÿ˜€ไฝ ", PositionEncoding::Utf16, 4), + ("a๐Ÿ˜€ไฝ ", PositionEncoding::Utf32, 3), + ("e\u{0301}", PositionEncoding::Utf8, 3), + ("e\u{0301}", PositionEncoding::Utf16, 2), + ("e\u{0301}", PositionEncoding::Utf32, 2), + ]; + for &(text, encoding, expected) in table { + assert_eq!(measure(text, encoding), expected, "measure of {text:?}"); + } +} + +#[test] +fn prefix_to_matches_table() { + let line = "a๐Ÿ˜€ไฝ e"; + let table: &[(&str, u32, PositionEncoding, Option<&str>)] = &[ + (line, 0, PositionEncoding::Utf8, Some("")), + (line, 0, PositionEncoding::Utf16, Some("")), + (line, 0, PositionEncoding::Utf32, Some("")), + (line, 1, PositionEncoding::Utf8, Some("a")), + (line, 1, PositionEncoding::Utf16, Some("a")), + (line, 1, PositionEncoding::Utf32, Some("a")), + (line, 2, PositionEncoding::Utf8, None), + (line, 2, PositionEncoding::Utf16, None), + (line, 2, PositionEncoding::Utf32, Some("a๐Ÿ˜€")), + (line, 3, PositionEncoding::Utf8, None), + (line, 3, PositionEncoding::Utf16, Some("a๐Ÿ˜€")), + (line, 3, PositionEncoding::Utf32, Some("a๐Ÿ˜€ไฝ ")), + (line, 4, PositionEncoding::Utf8, None), + (line, 4, PositionEncoding::Utf16, Some("a๐Ÿ˜€ไฝ ")), + (line, 4, PositionEncoding::Utf32, Some("a๐Ÿ˜€ไฝ e")), + (line, 5, PositionEncoding::Utf8, Some("a๐Ÿ˜€")), + (line, 5, PositionEncoding::Utf16, Some("a๐Ÿ˜€ไฝ e")), + (line, 5, PositionEncoding::Utf32, None), + (line, 7, PositionEncoding::Utf8, None), + (line, 8, PositionEncoding::Utf8, Some("a๐Ÿ˜€ไฝ ")), + (line, 9, PositionEncoding::Utf8, Some("a๐Ÿ˜€ไฝ e")), + (line, 10, PositionEncoding::Utf8, None), + (line, 6, PositionEncoding::Utf32, None), + ]; + for &(text, character, encoding, expected) in table { + assert_eq!( + prefix_to(text, character, encoding), + expected, + "prefix_to {text:?} at {character} ({encoding})" + ); + } +} + +#[test] +fn prefix_to_handles_combining_marks() { + let line = "e\u{0301}x"; + let table: &[(u32, PositionEncoding, Option<&str>)] = &[ + (1, PositionEncoding::Utf8, Some("e")), + (2, PositionEncoding::Utf8, None), + (3, PositionEncoding::Utf8, Some("e\u{0301}")), + (4, PositionEncoding::Utf8, Some(line)), + (5, PositionEncoding::Utf8, None), + (2, PositionEncoding::Utf16, Some("e\u{0301}")), + (3, PositionEncoding::Utf16, Some(line)), + (4, PositionEncoding::Utf16, None), + (2, PositionEncoding::Utf32, Some("e\u{0301}")), + ]; + for &(character, encoding, expected) in table { + assert_eq!( + prefix_to(line, character, encoding), + expected, + "prefix_to at {character} ({encoding})" + ); + } +} From cd4e731c36bc514a2770bdc9daad845720d32d26 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:13:58 +1100 Subject: [PATCH 10/15] memory/start: a failed start stops and releases the session it opened memory_start opened the session before backend.start; when the start (or its first step) failed, the session stayed in the manager and the backend's state was never stopped, while the client never received the id to stop it. The failed start now runs backend.stop and closes the session. Regression drives the real service handle with a refusing backend and checks the runtime's strong count while the service lives. [PROF-MEMORY-SESSION] --- crates/lsp-profiler/src/service.rs | 2 + .../service/memory_start_lifetime_tests.rs | 262 ++++++++++++++++++ .../lsp-profiler/src/service/memory_steps.rs | 71 ++++- 3 files changed, 324 insertions(+), 11 deletions(-) create mode 100644 crates/lsp-profiler/src/service/memory_start_lifetime_tests.rs diff --git a/crates/lsp-profiler/src/service.rs b/crates/lsp-profiler/src/service.rs index f26a95f..d217c81 100644 --- a/crates/lsp-profiler/src/service.rs +++ b/crates/lsp-profiler/src/service.rs @@ -34,6 +34,8 @@ use crate::{cooperative, process}; #[cfg(test)] mod lifetime_tests; +#[cfg(test)] +mod memory_start_lifetime_tests; mod memory_steps; mod progress; diff --git a/crates/lsp-profiler/src/service/memory_start_lifetime_tests.rs b/crates/lsp-profiler/src/service/memory_start_lifetime_tests.rs new file mode 100644 index 0000000..c808d0b --- /dev/null +++ b/crates/lsp-profiler/src/service/memory_start_lifetime_tests.rs @@ -0,0 +1,262 @@ +//! Lifetime regressions for a failed `memory/start`. [PROF-MEMORY-START] +//! +//! `memory/start` opens the session before it runs the backend's first step. +//! When that step fails the client never receives the session id, so nothing +//! outside the manager can stop it: the failed start must release the session +//! itself, the way `memory/stop` and shutdown do. + +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use async_trait::async_trait; +use serde_json::{json, Value}; + +use super::{ProfilerConfig, ProfilerEvents, ProfilerService}; +use crate::error::ProfilerError; +use crate::memory::{MemoryAction, MemoryError, MemoryPayload}; +use crate::runtime::{ + Classification, CpuBackend, FrameFilter, KeepAllFrames, MemoryBackend, MemoryContext, + ProcessCandidate, Runtime, TypeQuery, +}; +use crate::wire::{methods, MemoryCapabilities, MemoryMode}; + +/// Runtime identity. +const RUNTIME_ID: &str = "refusing"; +/// Failed starts per test: below the manager's session capacity, so eviction +/// can never hide a leaked session. +const ATTEMPTS: usize = 8; +/// Why the backend refuses every start. +const REFUSED: &str = "tracking refused"; +/// Where exports from the test service go. +const OUTPUT_DIR: &str = "lsp-profiler-memstart-lifetime"; +/// How long the spawned child outlives the test's tracking attempts. +const CHILD_LIFE: &str = "600"; + +/// A backend that refuses every start and records what it was asked to do. +#[derive(Debug, Default)] +struct Refusing { + /// Sessions whose start was attempted. + started: Mutex>, + /// Sessions whose stop ran. + stopped: Mutex>, +} + +impl Refusing { + /// Sessions whose start was attempted. + fn starts(&self) -> Vec { + lock(&self.started).clone() + } + + /// Sessions whose stop ran. + fn stops(&self) -> Vec { + lock(&self.stopped).clone() + } + + /// Record `session_id` in `list`. + fn push(list: &Mutex>, session_id: &str) { + lock(list).push(session_id.to_owned()); + } +} + +/// `list` under its lock, poison-tolerant like the other lifetime tests. +fn lock(list: &Mutex>) -> MutexGuard<'_, Vec> { + list.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// The answer to every successful step. +const fn ack() -> MemoryAction { + MemoryAction::Payload(MemoryPayload::Ack) +} + +#[async_trait] +impl MemoryBackend for Refusing { + fn capabilities(&self) -> MemoryCapabilities { + MemoryCapabilities { + mode: MemoryMode::Direct, + allocation_sites: false, + references: false, + objects_by_type: false, + gc_collect: false, + } + } + async fn start(&self, context: &MemoryContext) -> Result { + Self::push(&self.started, &context.session_id); + Err(MemoryError::Backend(REFUSED.to_owned())) + } + async fn snapshot(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn diff(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn references( + &self, + _context: &MemoryContext, + _query: &TypeQuery, + ) -> Result { + Ok(ack()) + } + async fn objects_by_type( + &self, + _context: &MemoryContext, + _query: &TypeQuery, + ) -> Result { + Ok(ack()) + } + async fn gc_collect(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn stop(&self, context: &MemoryContext) -> Result { + Self::push(&self.stopped, &context.session_id); + Ok(ack()) + } + async fn parse( + &self, + _context: &MemoryContext, + _output: &str, + ) -> Result { + Ok(MemoryPayload::Ack) + } +} + +/// A runtime exposing only the [`Refusing`] backend. +#[derive(Debug)] +struct RefusingRuntime { + /// The process this runtime claims, so `memory/start` resolves to it. + pid: u32, + /// The memory backend. + backend: Refusing, +} + +impl Runtime for RefusingRuntime { + fn id(&self) -> &'static str { + RUNTIME_ID + } + fn display_name(&self) -> &'static str { + RUNTIME_ID + } + fn classify(&self, candidates: &[ProcessCandidate]) -> Vec<(usize, Classification)> { + candidates + .iter() + .position(|candidate| candidate.pid == self.pid) + .map(|index| (index, Classification::default())) + .into_iter() + .collect() + } + fn frame_filter(&self) -> &dyn FrameFilter { + &KeepAllFrames + } + fn cpu(&self) -> Option<&dyn CpuBackend> { + None + } + fn memory(&self) -> Option<&dyn MemoryBackend> { + Some(&self.backend) + } +} + +/// Events sink that discards notifications. +#[derive(Debug)] +struct Discard; + +#[async_trait] +impl ProfilerEvents for Discard { + async fn notify(&self, _method: &'static str, _params: Value) {} +} + +/// A spawned `sleep`, killed when the test ends. The process table excludes +/// the test binary itself, so the runtime needs another live process to claim. +struct Child(std::process::Child); + +impl Drop for Child { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// A live process the refusing runtime can claim. +fn child() -> Result { + std::process::Command::new("sleep") + .arg(CHILD_LIFE) + .spawn() + .map(Child) + .map_err(|error| format!("cannot spawn a child process to track: {error}")) +} + +/// A `memory/start` request for `pid` on the refusing runtime. +fn start_params(pid: u32) -> Value { + json!({"pid": i64::from(pid), "runtime": RUNTIME_ID}) +} + +/// A service whose only runtime is `runtime`. +fn service(runtime: Arc) -> ProfilerService { + let plugin: Arc = runtime; + let events: Arc = Arc::new(Discard); + let config = ProfilerConfig { + output_dir: std::env::temp_dir().join(OUTPUT_DIR), + ..ProfilerConfig::default() + }; + ProfilerService::new(vec![plugin], config, events) +} + +#[tokio::test] +async fn a_failed_memory_start_releases_the_session_it_opened() -> Result<(), String> { + let tracked = child()?; + let pid = tracked.0.id(); + let runtime = Arc::new(RefusingRuntime { + pid, + backend: Refusing::default(), + }); + let service = service(Arc::clone(&runtime)); + let baseline = Arc::strong_count(&runtime); + for _ in 0..ATTEMPTS { + refuse_once(&service, pid).await?; + } + check_backend(&runtime)?; + check_manager(&runtime, baseline) +} + +/// One refused `memory/start` through the real dispatch path. +async fn refuse_once(service: &ProfilerService, pid: u32) -> Result<(), String> { + let error = service + .handle(methods::MEMORY_START, start_params(pid)) + .await + .err() + .ok_or("a refused backend start still answered")?; + match error { + ProfilerError::Memory(MemoryError::Backend(message)) if message == REFUSED => Ok(()), + other => Err(format!("unexpected error for a refused start: {other}")), + } +} + +/// Every attempted start must have reached the backend's `stop`. +fn check_backend(runtime: &Arc) -> Result<(), String> { + let started = runtime.backend.starts(); + let stopped = runtime.backend.stops(); + if started.len() != ATTEMPTS { + return Err(format!( + "{} of {ATTEMPTS} starts reached the backend", + started.len() + )); + } + if stopped != started { + return Err(format!( + "a failed start never stopped its session: {} stops for {} starts", + stopped.len(), + started.len() + )); + } + Ok(()) +} + +/// Every open session holds a clone of its runtime, so while the service is +/// alive the runtime's strong count is back at its pre-start baseline only if +/// the manager kept no session from the failed starts. +fn check_manager(runtime: &Arc, baseline: usize) -> Result<(), String> { + let retained = Arc::strong_count(runtime).saturating_sub(baseline); + if retained != 0 { + return Err(format!( + "{retained} of {ATTEMPTS} failed starts left their sessions in the manager" + )); + } + Ok(()) +} diff --git a/crates/lsp-profiler/src/service/memory_steps.rs b/crates/lsp-profiler/src/service/memory_steps.rs index 8d6e303..bd56fd2 100644 --- a/crates/lsp-profiler/src/service/memory_steps.rs +++ b/crates/lsp-profiler/src/service/memory_steps.rs @@ -14,6 +14,7 @@ use crate::wire::{ methods, MemoryIngestParams, MemoryResult, MemorySessionParams, MemoryStartParams, MemoryStartResult, MemoryStep, MemoryTypeParams, }; +use tracing::warn; /// Default retention-graph node budget. const DEFAULT_GRAPH_LIMIT: u32 = 200; @@ -36,19 +37,52 @@ impl ProfilerService { let _owner = self.recognise(pid).await?; let runtime = self.memory_runtime(request.runtime.as_deref(), pid).await?; let backend = memory_backend(runtime.as_ref())?; - let depth = request.traceback_depth.and_then(|d| u32::try_from(d).ok()); let context = self - .inner - .memory - .open(Arc::clone(&runtime), pid, depth) + .open_session(&runtime, pid, request.traceback_depth) .await; - let action = backend.start(&context).await?; - Ok(MemoryStartResult { - memory_session_id: context.session_id.clone(), - runtime: runtime.id().to_owned(), - mode: backend.capabilities().mode, - step: self.to_step(&context.session_id, action).await?, - }) + let answer = self.start_step(backend, &context).await; + if answer.is_err() { + self.abandon(backend, &context).await; + } + answer.map(|step| start_result(&runtime, backend, &context, step)) + } + + /// Open the session this start will run its first step on. + async fn open_session( + &self, + runtime: &Arc, + pid: u32, + traceback_depth: Option, + ) -> MemoryContext { + let depth = traceback_depth.and_then(|d| u32::try_from(d).ok()); + self.inner + .memory + .open(Arc::clone(runtime), pid, depth) + .await + } + + /// Run the backend's first step on a just-opened session. + async fn start_step( + &self, + backend: &dyn MemoryBackend, + context: &MemoryContext, + ) -> Result { + let action = backend.start(context).await?; + self.to_step(&context.session_id, action).await + } + + /// Release a session whose start failed, the way `memory/stop` does: the + /// backend may hold state from the failed start, and the client never + /// received the id, so nothing else can stop it. + async fn abandon(&self, backend: &dyn MemoryBackend, context: &MemoryContext) { + if let Err(error) = backend.stop(context).await { + warn!( + session_id = %context.session_id, + %error, + "cannot stop a memory session whose start failed" + ); + } + self.inner.memory.close(&context.session_id).await; } /// Dispatch a parameter-free memory operation and normalize its next step. @@ -189,3 +223,18 @@ fn memory_backend(runtime: &dyn Runtime) -> Result<&dyn MemoryBackend, ProfilerE MemoryError::Unsupported(format!("memory profiling of {}", runtime.display_name())).into() }) } + +/// The wire answer to a start whose first step produced `step`. +fn start_result( + runtime: &Arc, + backend: &dyn MemoryBackend, + context: &MemoryContext, + step: MemoryStep, +) -> MemoryStartResult { + MemoryStartResult { + memory_session_id: context.session_id.clone(), + runtime: runtime.id().to_owned(), + mode: backend.capabilities().mode, + step, + } +} From e807e0af019b0eeb87297a908058e369aa5ded12 Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:16:11 +1100 Subject: [PATCH 11/15] Retention graph: sharp on Retina, labels never overlap (#4) The retention canvas sized its backing store in CSS pixels, so it blurred at devicePixelRatio 2, and long type names overlapped. One devicePixelRatio helper (canvas-hidpi.ts) now sizes the retention graph, memory timeline and flame graph; the layout is a pure, unit-tested function that fits each label to its node's share of the row. Contract regressions: backing store 2x at dpr 2, and no two drawn label boxes overlap. [PROF-UI-REFGRAPH] --- docs/specs/LSP-PROFILER-SPEC.md | 4 +- packages/lsp-profiler-ui/src/canvas-hidpi.ts | 51 +++ packages/lsp-profiler-ui/src/flame-script.ts | 10 +- .../src/memory-dashboard-html.ts | 6 +- .../lsp-profiler-ui/src/memory-ref-graph.ts | 98 +++--- .../lsp-profiler-ui/src/ref-graph-layout.ts | 305 ++++++++++++++++++ .../src/test/unit/ref-graph-layout.test.ts | 140 ++++++++ .../src/testing/webview-contract-memory.ts | 151 ++++++++- 8 files changed, 701 insertions(+), 64 deletions(-) create mode 100644 packages/lsp-profiler-ui/src/canvas-hidpi.ts create mode 100644 packages/lsp-profiler-ui/src/ref-graph-layout.ts create mode 100644 packages/lsp-profiler-ui/src/test/unit/ref-graph-layout.test.ts diff --git a/docs/specs/LSP-PROFILER-SPEC.md b/docs/specs/LSP-PROFILER-SPEC.md index b88c0db..d8f1cc9 100644 --- a/docs/specs/LSP-PROFILER-SPEC.md +++ b/docs/specs/LSP-PROFILER-SPEC.md @@ -634,7 +634,9 @@ The retention graph is a layered layout of a `RefGraph`: a node's row is its `de - targets, roots and cycles are distinct; - a root is named by its label above its type, so a static field or an F# module value reads as written (`static sessions`); - edge labels name the field; -- a row too tight to label each node names its type once, with a count (`40 ร— App.Session`). +- a row too tight to label each node names its type once, with a count (`40 ร— App.Session`); +- labels are fitted to their node's share of the row, ending in an ellipsis, so no two labels overlap; +- the canvas backing store is scaled by `devicePixelRatio` (one helper shared with the memory timeline and the flame graph), so it is sharp on Retina screens. The type picker offers the server's ranked type names (the latest snapshot's top types) plus free text, so it is language-neutral. It never offers names taken from source text or from one language's builtins. diff --git a/packages/lsp-profiler-ui/src/canvas-hidpi.ts b/packages/lsp-profiler-ui/src/canvas-hidpi.ts new file mode 100644 index 0000000..345185c --- /dev/null +++ b/packages/lsp-profiler-ui/src/canvas-hidpi.ts @@ -0,0 +1,51 @@ +// Implements [PROF-UI-WEBVIEW]. See docs/specs/LSP-PROFILER-SPEC.md +/** + * One device-pixel-ratio scaler for every profiler canvas webview: the memory + * dashboard timeline, the flame graph and the retention graph. A Retina screen + * renders two (or three) device pixels per CSS pixel, so a canvas that sizes + * its backing store in CSS pixels draws blurred. + * + * The webview scripts embed this function's COMPILED SOURCE via + * Function.prototype.toString, so the exact code every webview executes is + * type-checked and unit-tested here. The rules of flame-script-helpers apply, + * plus one the CommonJS output forces: no module-level constants (an exported + * const compiles to `exports.X`, which does not exist in the webview). + */ + +/** The canvas transform matrix, as the 2D context's setTransform takes it. */ +export type TransformMatrix = readonly [number, number, number, number, number, number]; + +/** The canvas surface the scaler writes: CSS size and backing store. */ +export interface DevicePixelCanvas { + style: { width: string; height: string }; + width: number; + height: number; +} + +/** A 2D context whose transform the scaler sets. */ +export interface ScaledCanvasContext { + setTransform: (...matrix: TransformMatrix) => void; +} + +/** What the scaler sizes: the canvas, its context, its CSS size and the screen's ratio. */ +export interface HiDpiRequest { + canvas: DevicePixelCanvas; + ctx: ScaledCanvasContext; + cssWidth: number; + cssHeight: number; + devicePixelRatio: number; +} + +/** + * Size a canvas for its screen: the CSS size in logical pixels, the backing + * store multiplied by `devicePixelRatio`, and the context scaled by the same + * ratio so all later drawing keeps using CSS-pixel coordinates. + */ +export function sizeCanvasToDevicePixels(request: HiDpiRequest): void { + const ratio = request.devicePixelRatio > 0 ? request.devicePixelRatio : 1; + request.canvas.style.width = `${request.cssWidth}px`; + request.canvas.style.height = `${request.cssHeight}px`; + request.canvas.width = Math.round(request.cssWidth * ratio); + request.canvas.height = Math.round(request.cssHeight * ratio); + request.ctx.setTransform(ratio, 0, 0, ratio, 0, 0); +} diff --git a/packages/lsp-profiler-ui/src/flame-script.ts b/packages/lsp-profiler-ui/src/flame-script.ts index 3ad8cde..37c156c 100644 --- a/packages/lsp-profiler-ui/src/flame-script.ts +++ b/packages/lsp-profiler-ui/src/flame-script.ts @@ -9,12 +9,13 @@ // function sources (type-checked + unit-tested TS, embedded verbatim via // toString); the rest is one literal with no backticks or dollar-brace sequences. +import { sizeCanvasToDevicePixels } from "./canvas-hidpi"; import { byFrameKeyThenIndex, framePaletteColor, frameRanks, hasSourceFile, inkFor, paletteFrames } from "./palette"; import { clampViewRange, coveredSpan, fitLabelText, searchMatches } from "./flame-script-helpers"; /** The type-checked, unit-tested functions the webview runs, as source. */ const EMBEDDED = [hasSourceFile, byFrameKeyThenIndex, frameRanks, framePaletteColor, paletteFrames, inkFor, - fitLabelText, searchMatches, coveredSpan, clampViewRange].map((fn) => fn.toString()).join("\n"); + fitLabelText, searchMatches, coveredSpan, clampViewRange, sizeCanvasToDevicePixels].map((fn) => fn.toString()).join("\n"); export const FLAME_SCRIPT = `(function () { "use strict"; @@ -73,14 +74,9 @@ export const FLAME_SCRIPT = `(function () { } function resize() { - var dpr = window.devicePixelRatio || 1; var width = canvas.parentElement.clientWidth; var height = depthCount() * ROW_H + 2; - canvas.style.width = width + "px"; - canvas.style.height = height + "px"; - canvas.width = Math.round(width * dpr); - canvas.height = Math.round(height * dpr); - ctx.setTransform(dpr, 0, 0, dpr, 0, 0); + sizeCanvasToDevicePixels({ canvas: canvas, ctx: ctx, cssWidth: width, cssHeight: height, devicePixelRatio: window.devicePixelRatio || 1 }); render(); } diff --git a/packages/lsp-profiler-ui/src/memory-dashboard-html.ts b/packages/lsp-profiler-ui/src/memory-dashboard-html.ts index 5cd7337..e9d6e0b 100644 --- a/packages/lsp-profiler-ui/src/memory-dashboard-html.ts +++ b/packages/lsp-profiler-ui/src/memory-dashboard-html.ts @@ -1,4 +1,5 @@ // Implements [PROF-UI-MEMORY]. Dashboard document rendering. +import { sizeCanvasToDevicePixels } from "./canvas-hidpi"; import { brand } from "./ids"; import type { MemoryDashboardSnapshot, MemoryDiffData } from "./memory-dashboard"; import { buildWebviewDocument, embedJson } from "./profiler-webview"; @@ -145,9 +146,7 @@ const TIMELINE_SETUP_SCRIPT = ` const TIMELINE_DRAW_SCRIPT = ` function renderTimeline() { const ctr=tlCanvas.parentElement, w=ctr.clientWidth-24, h=200; - tlCanvas.width=w*devicePixelRatio; tlCanvas.height=h*devicePixelRatio; - tlCanvas.style.width=w+'px'; tlCanvas.style.height=h+'px'; - tlCtx.setTransform(devicePixelRatio,0,0,devicePixelRatio,0,0); + sizeCanvasToDevicePixels({canvas:tlCanvas,ctx:tlCtx,cssWidth:w,cssHeight:h,devicePixelRatio:devicePixelRatio||1}); if(timeline.length<2){ tlCtx.fillStyle=cssVar('--prof-text-secondary');tlCtx.font='13px "Space Grotesk",-apple-system,sans-serif'; tlCtx.textAlign='center';tlCtx.textBaseline='middle'; @@ -325,6 +324,7 @@ function buildDashboardScriptTag( const vscode = globalThis.profilerVsCodeApi || (globalThis.profilerVsCodeApi = acquireVsCodeApi()); ${dashboardDataScript(snapshot, diff)} ${PROFILER_JS_UTILS} + ${sizeCanvasToDevicePixels.toString()} ${SUMMARY_SCRIPT} ${TIMELINE_SCRIPT} ${TOGGLE_SCRIPT} diff --git a/packages/lsp-profiler-ui/src/memory-ref-graph.ts b/packages/lsp-profiler-ui/src/memory-ref-graph.ts index bdb09df..7749119 100644 --- a/packages/lsp-profiler-ui/src/memory-ref-graph.ts +++ b/packages/lsp-profiler-ui/src/memory-ref-graph.ts @@ -13,6 +13,32 @@ */ import { cmd } from "./ids"; +import { sizeCanvasToDevicePixels } from "./canvas-hidpi"; +import { fitLabelText } from "./flame-script-helpers"; +import { + REF_MIN_WIDTH, + belowLabels, + collectLayer, + crowdedLayerLabel, + deepestDepth, + fittedLabel, + groupByDepth, + layoutLayer, + nodeLabels, + nodeRadius, + placeNode, + refBottomMargin, + refCrowdedGap, + refGraphLayout, + refLabelColor, + refLabelFont, + refLabelPadding, + refLayerHeight, + refMinHeight, + refTopMargin, + rootLabel, + rowGeometry, +} from "./ref-graph-layout"; import { PROFILER_CSS_RESET, PROFILER_CSS_VARS, @@ -177,7 +203,7 @@ function refGraphDataScript(nodesJson: string, edgesJson: string, cyclesJson: st `; } -/** An honest empty state, or each node laid out in a layer by its distance from the targets. */ +/** The graph canvas: sized for its screen, then laid out by pure, tested code. */ const REF_GRAPH_LAYOUT_SCRIPT = ` if (nodes.length === 0) { document.getElementById('graph').style.display = 'none'; @@ -189,27 +215,12 @@ const REF_GRAPH_LAYOUT_SCRIPT = ` const canvas = document.getElementById('graph'); const ctx = canvas.getContext('2d'); // Fill the panel width so the graph is not letterboxed into 800px. - const W = canvas.width = Math.max(800, canvas.parentElement ? canvas.clientWidth : 800); - // Layers by distance from the targets: roots on top, targets along the bottom. - const LAYER = 120; - const top = Math.max(...nodes.map((n) => n.depth)); - const H = canvas.height = Math.max(360, (top + 1) * LAYER + 40); - const cycleNodeIds = new Set(cycles.flat()); - const layers = new Map(); - nodes.forEach((n) => layers.set(n.depth, [...(layers.get(n.depth) || []), n])); - const nodeMap = new Map(); - for (const [depth, members] of layers) { - const gap = W / (members.length + 1); - // A row too tight to label each node names its type once, with a count. - const crowded = gap < 160; - members.forEach((n, i) => nodeMap.set(n.id, { - ...n, - x: gap * (i + 1), - y: 60 + (top - depth) * LAYER, - crowded, - radius: Math.min(crowded ? gap / 2.5 : 30, Math.max(8, Math.log2(Math.max(n.size, 1)) * 2)), - })); - }`; + const cssWidth = Math.max(${REF_MIN_WIDTH}, canvas.parentElement ? canvas.clientWidth : ${REF_MIN_WIDTH}); + const sized = nodes.map((n) => ({ ...n, sizeLabel: formatBytes(n.size) })); + const layout = refGraphLayout(sized, cssWidth, fitLabelText); + sizeCanvasToDevicePixels({ canvas: canvas, ctx: ctx, cssWidth: cssWidth, cssHeight: layout.height, devicePixelRatio: window.devicePixelRatio || 1 }); + const nodeMap = layout.nodes; + const cycleNodeIds = new Set(cycles.flat());`; /** Edges, a cycle's in the leak colour. */ const REF_GRAPH_EDGES_SCRIPT = ` @@ -231,7 +242,7 @@ const REF_GRAPH_EDGES_SCRIPT = ` } }`; -/** Nodes: targets, roots and cycles in the legend's colours; a root named by its label above its type. */ +/** Nodes: targets, roots and cycles in the legend's colours; every label fitted by the layout. */ const REF_GRAPH_NODES_SCRIPT = ` for (const n of nodeMap.values()) { const color = cssVar(n.isTarget ? '--prof-mem-critical' : n.isRoot ? '--prof-info' : cycleNodeIds.has(n.id) ? '--prof-mem-leak' : '--prof-text-secondary'); @@ -245,30 +256,20 @@ const REF_GRAPH_NODES_SCRIPT = ` ctx.lineWidth = n.isTarget || n.isRoot ? 3 : 1.5; ctx.stroke(); ctx.textAlign = 'center'; - if (n.isRoot) { - ctx.fillStyle = color; - ctx.font = 'bold 13px monospace'; - ctx.fillText(n.repr, n.x, n.y - n.radius - 10); + for (const label of n.labels) { + ctx.font = refLabelFont(label.kind); + ctx.fillStyle = cssVar(refLabelColor(label.kind)); + ctx.fillText(label.text, label.x, label.y); } - if (n.crowded) continue; - ctx.fillStyle = cssVar('--prof-text'); - ctx.font = '10px monospace'; - ctx.fillText(n.type, n.x, n.y + n.radius + 14); - ctx.fillStyle = cssVar('--prof-text-secondary'); - ctx.font = '9px monospace'; - ctx.fillText(formatBytes(n.size), n.x, n.y + n.radius + 26); }`; -/** A crowded layer names its type once, with a count, and the drawing branch closes. */ +/** A crowded layer names its type once, with a count; the drawing branch closes. */ const REF_GRAPH_LAYER_LABELS_SCRIPT = ` - for (const [depth, members] of layers) { - const first = nodeMap.get(members[0].id); - if (!first.crowded) continue; - const types = new Set(members.map((n) => n.type)); - ctx.fillStyle = cssVar('--prof-text'); - ctx.font = '11px monospace'; + for (const label of layout.layerLabels) { + ctx.font = refLabelFont(label.kind); + ctx.fillStyle = cssVar(refLabelColor(label.kind)); ctx.textAlign = 'center'; - ctx.fillText(members.length + ' ร— ' + (types.size === 1 ? members[0].type : 'objects'), W / 2, first.y + first.radius + 18); + ctx.fillText(label.text, label.x, label.y); } }`; @@ -298,10 +299,23 @@ export function buildRefGraphHtml(result: ReferenceGraphResult): string { /** * The drawing script. Node types/reprs and edge labels are profiled-program * data, embedded so they can never close the inline