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
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
2. **结果取决于真实操作系统的行为**(进程树、job object、权限位、batch 启动、可执行文件查找等):放进 [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform) 里对应主题的模块。需要被启动的程序时,给 `lithe-platform-fixture` 增加一种模式。不要把这类测试写在被测 crate 里,也不要在其他 crate 里加 `#[cfg(windows)]`、`#[cfg(target_os = "macos")]` 之类的测试。
3. **只借用 Unix 才有的夹具**(符号链接、权限位)来验证本 crate 自己的逻辑,并且 Linux 上会运行的 `#[cfg(unix)]` 测试:可以留在本 crate。如果 Windows 上应有另一种结果,那个结果按第 2 条写进 `lithe-platform-tests`。
4. **测试里的 `cfg!`** 只能把夹具改成本机格式,例如 Windows 绝对路径要带盘符;不能用它为不同系统选择不同的预期结果。预期随系统变化的断言属于第 2 条。
5. **例外**:crate 的集成测试必须启动本包的可执行文件时,因为 `CARGO_BIN_EXE_*` 只对同一个包可见,测试无法搬走。这种 crate 在自己的 `Cargo.toml` 声明主机。目前只有 [`lithe-remote`](rust/crates/remote):它的服务端运行在 Linux 和 macOS 主机上。新增例外要在 PR 里说明为什么不能放进 `lithe-platform-tests`。
5. **例外**:crate 的集成测试必须启动本包的可执行文件时,因为 `CARGO_BIN_EXE_*` 只对同一个包可见,测试无法搬走。这种 crate 在自己的 `Cargo.toml` 声明主机。例如 [`lithe-remote`](rust/crates/remote) 启动远端服务端,[`lithe-ide-host`](rust/crates/ide-host) 启动 `lithe-mcp` 验证 stdio 协议。新增例外要在 PR 里说明为什么不能放进 `lithe-platform-tests`。

[`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) 检查的是:有没有测试在所属 crate 的每台 CI 主机上都被 `cfg` 编译掉,或者被 `cfg_attr(…, ignore)` 忽略。这样的测试在 CI 里永远不会运行,检查会失败。CI 的 `Select crates` 作业每次都运行这项检查。加 `--list` 会列出所有只在部分主机上运行的测试。第 3、4 条要看测试的意图,脚本判断不了,由评审把关。

Expand All @@ -28,7 +28,7 @@ hosts = ["macos", "windows"] # 除 Linux 外,还要在这些主机上运行
test-features = ["server"] # 测试需要的 feature,在每台主机上都启用
```

`hosts` 只接受 `macos` 和 `windows`;每个 crate 都会在 Linux 上测试,不必声明。目前声明主机的 crate 有 `lithe-platform`、`lithe-platform-tests` 和 `lithe-remote`。声明主机后,每个影响到该 crate 的 PR 都会多占用 macOS 或 Windows runner,所以只能用于上一节允许的情况。
`hosts` 只接受 `macos` 和 `windows`;每个 crate 都会在 Linux 上测试,不必声明。声明主机后,每个影响到该 crate 的 PR 都会多占用 macOS 或 Windows runner,所以只能用于上一节允许的情况。

## CI 怎样只运行受影响的 crate

Expand Down Expand Up @@ -85,7 +85,7 @@ Take the first rule that applies:
2. **A result that depends on the real operating system** (process trees, job objects, permission bits, batch launches, executable lookup and the like): add it to the module for that subject under [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform). When a test needs a program to start, add a mode to `lithe-platform-fixture`. Do not write such tests in the crate under test, and do not add `#[cfg(windows)]`, `#[cfg(target_os = "macos")]` or similar tests to any other crate.
3. **A `#[cfg(unix)]` test that only borrows a Unix fixture** (a symbolic link, permission bits) to check the crate's own logic, and that runs on Linux: it may stay in its crate. If Windows should behave differently, that result goes into `lithe-platform-tests` under rule 2.
4. **`cfg!` in a test** may only convert a fixture to the host's form, for example give a Windows absolute path its drive. It must not choose a different expected result per system; such assertions fall under rule 2.
5. **Exception**: when a crate's integration tests must start the crate's own executable, they cannot move, because `CARGO_BIN_EXE_*` is visible only within the package. Such a crate declares its hosts in its own `Cargo.toml`. Today only [`lithe-remote`](rust/crates/remote) does, because its server runs on Linux and macOS hosts. A new exception explains in its pull request why the tests cannot go into `lithe-platform-tests`.
5. **Exception**: when a crate's integration tests must start the crate's own executable, they cannot move, because `CARGO_BIN_EXE_*` is visible only within the package. Such a crate declares its hosts in its own `Cargo.toml`. For example, [`lithe-remote`](rust/crates/remote) starts the remote server and [`lithe-ide-host`](rust/crates/ide-host) starts `lithe-mcp` to exercise its stdio protocol. A new exception explains in its pull request why the tests cannot go into `lithe-platform-tests`.

[`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) fails when a test is compiled out by `cfg`, or ignored by `cfg_attr(…, ignore)`, on every CI host of its crate, because CI would never run it. The `Select crates` job runs it on every change. `--list` also prints every test that runs on only some hosts. Rules 3 and 4 are about a test's intent, which the script cannot judge; review enforces them.

Expand All @@ -97,7 +97,7 @@ hosts = ["macos", "windows"] # hosts that also run the crate's tests, besides
test-features = ["server"] # features the tests need, enabled on every host
```

`hosts` accepts `macos` and `windows`; every crate is tested on Linux without declaring it. Today `lithe-platform`, `lithe-platform-tests` and `lithe-remote` declare hosts. A declaration makes every pull request that affects the crate use a macOS or Windows runner, so it is only for the cases the previous section allows.
`hosts` accepts `macos` and `windows`; every crate is tested on Linux without declaring it. A declaration makes every pull request that affects the crate use a macOS or Windows runner, so it is only for the cases the previous section allows.

## How CI runs only the affected crates

Expand Down
33 changes: 33 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions rust/contracts/ide-api/v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ the project dispatcher answered a tool call.

MCP `tools/list` includes `ttlMs: 0` and `cacheScope: "private"`, including for
`2026-07-28` discovery. Protocol negotiation and envelopes remain SDK-owned;
legacy and modern clients are verified in separate stdio sessions using
`rust/fixtures/ide-api/mcp-protocol.json`.
the [stdio integration test](../../crates/ide-host/tests/mcp_protocol.rs) starts
a fresh helper for legacy initialization, modern discovery without initialization,
and modern discovery after initialization. It reads the protocol expectations
from [mcp-protocol.json](../../fixtures/ide-api/mcp-protocol.json) and checks
the listed names against the connection's tool exposure.

## Capabilities

Expand Down Expand Up @@ -207,7 +210,7 @@ surface; it does not introduce a new plugin marketplace, sandbox or UI extension

- `cargo test --manifest-path rust/Cargo.toml -p lithe-ide-host`
- `cargo build --manifest-path rust/Cargo.toml -p lithe-ide-host --features mcp --bin lithe-mcp`
- `node --test scripts/ide-mcp/test-ide-mcp.mjs`
- `cargo test --manifest-path rust/Cargo.toml -p lithe-ide-host --features mcp --test mcp_protocol`
- Windows: the host-api and run frontend tests, plus a native packaged run using
the generated client configuration.
- macOS: `IdeCapabilitiesTests`, followed by a packaged native run.
Expand Down
10 changes: 10 additions & 0 deletions rust/crates/ide-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ publish = false
[features]
mcp = ["dep:rmcp", "dep:reqwest"]

[package.metadata.lithe-ci]
hosts = ["windows"]
test-features = ["mcp"]

[dependencies]
axum = "0.8"
fs2 = "0.4"
Expand All @@ -26,5 +30,11 @@ name = "lithe-mcp"
path = "src/bin/lithe-mcp.rs"
required-features = ["mcp"]

[[test]]
name = "mcp_protocol"
required-features = ["mcp"]

[dev-dependencies]
reqwest = { version = "0.12", default-features = false, features = ["json"] }
tempfile = "3"
tokio = { version = "1", features = ["io-util", "process"] }
166 changes: 166 additions & 0 deletions rust/crates/ide-host/tests/mcp_protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//! Exercises MCP discovery over the same stdio interface used by external clients.

use serde_json::{json, Value};
use std::{process::Stdio, time::Duration};
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines},
process::{ChildStdin, ChildStdout, Command},
time::timeout,
};

struct Client {
input: ChildStdin,
output: Lines<BufReader<ChildStdout>>,
id: u64,
}

impl Client {
async fn send(&mut self, message: Value) {
self.input
.write_all(format!("{message}\n").as_bytes())
.await
.expect("write MCP message");
}

async fn request(&mut self, method: &str, params: Value) -> Value {
self.id += 1;
self.send(json!({"jsonrpc": "2.0", "id": self.id, "method": method, "params": params}))
.await;
let line = self.output.next_line().await.unwrap().expect("MCP reply");
let reply: Value = serde_json::from_str(&line).unwrap();
assert_eq!(reply["id"], self.id, "{method}: {reply}");
reply
.get("result")
.unwrap_or_else(|| panic!("{method}: {reply}"))
.clone()
}
}

fn tool_names(tools: &Value) -> Vec<&str> {
let mut names: Vec<_> = tools
.as_array()
.unwrap()
.iter()
.map(|tool| tool["name"].as_str().unwrap())
.collect();
names.sort_unstable();
names
}

async fn check_session(mode: &'static str) {
let fixture: Value =
serde_json::from_str(include_str!("../../../fixtures/ide-api/mcp-protocol.json")).unwrap();
assert!(fixture["modes"].as_array().unwrap().contains(&json!(mode)));
let hosts: Value = serde_json::from_str(include_str!(
"../../../fixtures/ide-api/host-tool-exposure.json"
))
.unwrap();
let directory = tempfile::tempdir().unwrap();
let descriptor = directory.path().join("connection.json");
let descriptor_path = descriptor.clone();
let write_descriptor =
move |exposure: &Value| {
std::fs::write(&descriptor_path, json!({
"endpoint": "http://127.0.0.1:1/call", "token": "fixture", "toolExposure": exposure
}).to_string()).unwrap();
};
write_descriptor(&hosts["macos"]);
let mut child = Command::new(env!("CARGO_BIN_EXE_lithe-mcp"))
.arg("--connection")
.arg(&descriptor)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.kill_on_drop(true)
.spawn()
.expect("start lithe-mcp");
let mut client = Client {
input: child.stdin.take().unwrap(),
output: BufReader::new(child.stdout.take().unwrap()).lines(),
id: 0,
};

// Keep ownership of the child outside the client task so even a failed
// protocol assertion closes both pipes and reaps the helper.
let exchange = tokio::spawn(timeout(Duration::from_secs(10), async move {
let modern = &fixture["modernProtocolVersion"];
if mode != "modern" {
let version = if mode == "legacy" {
&fixture["legacyProtocolVersion"]
} else {
modern
};
let initialized = client
.request(
"initialize",
json!({
"protocolVersion": version, "capabilities": {},
"clientInfo": {"name": "fixture", "version": "1"}
}),
)
.await;
assert_eq!(initialized["serverInfo"]["name"], "lithe");
if mode == "legacy" {
assert_eq!(&initialized["protocolVersion"], version);
}
client
.send(json!({"jsonrpc": "2.0", "method": "notifications/initialized"}))
.await;
}
let mut params = json!({});
if mode != "legacy" {
params = json!({"_meta": {
"io.modelcontextprotocol/protocolVersion": modern,
"io.modelcontextprotocol/clientCapabilities": {}
}});
let discovery = client.request("server/discover", params.clone()).await;
assert!(discovery["supportedVersions"]
.as_array()
.unwrap()
.contains(modern));
}
for platform in ["macos", "windows"] {
let exposure = &hosts[platform];
write_descriptor(exposure);
let list = client.request("tools/list", params.clone()).await;
assert_eq!(list["ttlMs"], fixture["cachePolicy"]["ttlMs"]);
assert_eq!(list["cacheScope"], fixture["cachePolicy"]["cacheScope"]);
let names = tool_names(&list["tools"]);
assert_eq!(names, tool_names(exposure), "{platform} tool exposure");
for provider in fixture["providerTools"].as_array().unwrap() {
assert!(
names.contains(&provider.as_str().unwrap()),
"missing {provider}"
);
}
}
// Both platforms currently expose the full catalog. A restricted
// descriptor makes a cached or unfiltered tools/list observable.
let restricted = json!([hosts["windows"][0]]);
write_descriptor(&restricted);
let list = client.request("tools/list", params).await;
assert_eq!(tool_names(&list["tools"]), tool_names(&restricted));
}))
.await;
timeout(Duration::from_secs(5), child.kill())
.await
.expect("reap lithe-mcp before deadline")
.unwrap();
exchange
.expect("MCP client assertions")
.expect("MCP session deadline");
}

#[tokio::test]
async fn legacy() {
check_session("legacy").await;
}

#[tokio::test]
async fn modern() {
check_session("modern").await;
}

#[tokio::test]
async fn modern_after_initialize() {
check_session("modern-after-initialize").await;
}
1 change: 0 additions & 1 deletion rust/fixtures/ide-api/mcp-protocol.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"ttlMs": 0,
"cacheScope": "private"
},
"toolCount": 17,
"modes": [
"legacy",
"modern",
Expand Down
Loading