Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ or removing a command fails until the table changes in the same commit.
| `processLifecycle.inventory` | Group a process snapshot of one Lithe instance into terminals, Agent tasks and background services |
| `processLifecycle.settle` | Turn confirmation answers into what ends, what is kept and the updated settings |
| `processMemory.summarize` | Sum a process table's memory, compressed memory, graphics memory and processor and graphics use by part of one Lithe instance: the host, language servers, plugin services, system helpers and other child processes |
| `process.listeningPorts` | Filter, normalize, deduplicate, and format listening TCP ports for one run process tree |
| `markdown.render` | Renders and sanitizes shared Markdown |
| `markdown.outline` | Outlines the headings of shared Markdown as a document-symbol tree |
| `markdown.resolveReference` | Decides what a link or an image of a rendered Markdown document points at |
Expand Down Expand Up @@ -6295,6 +6296,32 @@ A table without the host or with a repeated process ID, or a `logicalProcessors`
of 0, is `invalid_request`. Fixtures live in
`rust/fixtures/process-lifecycle/memory-summary-v1.json`.

`process.listeningPorts` accepts `{ rootProcessId, processes, sockets,
excludePorts?, limit? }` and returns `{ schemaVersion, ports, truncated }` for
one running session's process tree:

- `rootProcessId` must be a positive integer. `processes` is a list of
`{ processId, parentProcessId, startTime? }`; parent links where
`parent.startTime > child.startTime` are ignored as PID reuse, and
`rootProcessId` itself is always included even when `processes` is empty.
- `sockets` is a list of `{ processId, address, port }`. Addresses are trimmed,
unwrapped from `[...]`, stripped of IPv6 `%zone` suffixes, mapped from `""` /
`"*"` to `"0.0.0.0"`, and unwrapped from IPv4-mapped IPv6 `::ffff:a.b.c.d` to
IPv4; non-IP strings return `invalid_request`.
- Sockets with `port == 0`, ports listed in `excludePorts` (such as an
attached debugger port), or `processId` outside the tree are filtered out.
- Multiple sockets sharing a port (for example dual-stack `0.0.0.0` + `::`)
collapse deterministically to one entry, preferring loopback/unspecified binds,
then IPv4 over IPv6, then loopback over unspecified, then lowest `processId`.
- Loopback and unspecified binds (`127.0.0.0/8`, `0.0.0.0`, `::1`, `::`) format
`label` as `:<port>` and `url` as `http://localhost:<port>`; specific
interface binds format `label` as `<ip>:<port>` / `[<ip>]:<port>` and `url`
as `http://<ip>:<port>` / `http://[<ip>]:<port>`.
- Results are ordered ascending by `port`; `limit` defaults to 8 and is clamped
to `1..=32`, with `truncated: true` when more matching ports existed.

Fixtures live in `rust/fixtures/process-lifecycle/listening-ports-v1.json`.

## Local IDE capability broker

`ideHost.control` accepts `{action, arguments}` and delegates to the native
Expand Down
8 changes: 8 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ pub enum CoreCommand {
ProcessLifecycleSettle,
/// Sums a process table's memory by part of the instance (`processMemory.summarize`).
ProcessMemorySummarize,
/// Filters, normalizes, and formats listening TCP ports of one process tree
/// (`process.listeningPorts`).
ProcessListeningPorts,
/// Renders and sanitizes shared Markdown (`markdown.render`).
MarkdownRender,
/// Outlines the headings of shared Markdown as document symbols (`markdown.outline`).
Expand Down Expand Up @@ -1336,6 +1339,7 @@ impl CoreCommand {
"processLifecycle.inventory" => Some(Self::ProcessLifecycleInventory),
"processLifecycle.settle" => Some(Self::ProcessLifecycleSettle),
"processMemory.summarize" => Some(Self::ProcessMemorySummarize),
"process.listeningPorts" => Some(Self::ProcessListeningPorts),
"markdown.render" => Some(Self::MarkdownRender),
"markdown.outline" => Some(Self::MarkdownOutline),
"markdown.resolveReference" => Some(Self::MarkdownResolveReference),
Expand Down Expand Up @@ -1820,6 +1824,10 @@ mod tests {
CoreCommand::parse("processMemory.summarize"),
Some(CoreCommand::ProcessMemorySummarize)
));
assert!(matches!(
CoreCommand::parse("process.listeningPorts"),
Some(CoreCommand::ProcessListeningPorts)
));
}

#[test]
Expand Down
19 changes: 19 additions & 0 deletions rust/crates/core/src/runtime/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@ fn execute_parsed(mut parsed: CoreRequest, document: Option<&str>) -> CoreRespon
),
}
}
CoreCommand::ProcessListeningPorts => {
match serde_json::from_value::<lithe_process::ports::ListeningPortsRequest>(
parsed.payload,
)
.map_err(|error| error.to_string())
.and_then(|request| {
lithe_process::ports::resolve(&request).map_err(|error| error.to_string())
}) {
Ok(ports) => CoreResponse::success(
id,
serde_json::to_value(ports).expect("listening ports response should encode"),
),
Err(details) => CoreResponse::failure(
id,
CoreError::new(ErrorCode::InvalidRequest, "Invalid listening ports request")
.with_details(details),
),
}
}
CoreCommand::Editor(command) => editor::dispatch(command, id, parsed.payload, document),
CoreCommand::WorkbenchChromeLayoutReduce => {
rules_response(id, "workbench.chromeLayout.reduce", parsed.payload)
Expand Down
5 changes: 4 additions & 1 deletion rust/crates/core/src/runtime/request_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ pub(crate) fn is_hot(method: &str) -> bool {
method,
"workspace.filterChanges" | "lsp.workspaceFilesChanged" | "git.classifyMetadataChanges"
)
// The status bar sums the instance's memory every few seconds while it is shown.
// The status bar sums the instance's memory every few seconds while it is shown,
// and active runs probe their process tree's listening ports periodically.
|| method == "processMemory.summarize"
|| method == "process.listeningPorts"
}

/// Explicit host provenance wins; Git provenance covers hosts that send only that.
Expand Down Expand Up @@ -275,6 +277,7 @@ mod tests {
"git.classifyMetadataChanges",
"debug.transport.handle",
"processMemory.summarize",
"process.listeningPorts",
] {
assert!(is_hot(method), "{method}");
}
Expand Down
1 change: 1 addition & 0 deletions rust/crates/core/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod plugin_settings_documents;
mod plugin_source_settings;
mod plugin_views;
mod process_memory;
mod process_ports;
mod project;
mod project_generators;
mod project_tree;
Expand Down
33 changes: 33 additions & 0 deletions rust/crates/core/src/tests/process_ports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! `process.listeningPorts` against the shared listening-ports fixture.

use serde_json::{json, Value};

fn run(request: &Value) -> Value {
serde_json::from_str(&crate::execute_json(
&json!({"command": "process.listeningPorts", "payload": request}).to_string(),
))
.unwrap()
}

#[test]
fn shared_listening_ports_fixture_runs_every_case() {
let fixture: Value = serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../fixtures/process-lifecycle/listening-ports-v1.json"
)))
.unwrap();
for case in fixture["cases"].as_array().unwrap() {
let response = run(&case["request"]);
assert_eq!(response["ok"], true, "case {}: {response}", case["name"]);
assert_eq!(response["data"], case["expected"], "case {}", case["name"]);
}
for case in fixture["invalid"].as_array().unwrap() {
let response = run(&case["request"]);
assert_eq!(response["ok"], false, "case {}: {response}", case["name"]);
assert_eq!(
response["error"]["code"], "invalid_request",
"case {}",
case["name"]
);
}
}
1 change: 1 addition & 0 deletions rust/crates/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
pub mod inventory;
pub mod lifecycle;
pub mod memory;
pub mod ports;
Loading
Loading