Skip to content

Commit a320d16

Browse files
1lckclaude
andauthored
feat(plugins): 同一语言只由一个已启用的插件提供语言服务器 (#39)
* feat(plugins): 同一语言只由一个已启用的插件提供语言服务器 plugins.installed.lifecycle 的已安装版本可带 languageServers(清单声明的语言服务器 languageID)。启用插件、或确认安装后插件处于启用状态时,其他已启用且最新版本为同一语言 声明语言服务器的插件,各得到一个 displacePlugin 步骤:宿主记为停用、停掉进程并告诉用户。 规则之前已同时启用的两个插件,启用状态按 ID 只保留第一个,选择不改,等用户启用其中一个时 再定下来;停用不会顶替别的插件。 契约 README、开发指南和 rust-core-api 写明该规则,共享 fixture 覆盖启用、安装、更新、 只看最新版本、旧状态和停用。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(plugins): 协议更新记录补 #39 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e131632 commit a320d16

8 files changed

Lines changed: 482 additions & 18 deletions

File tree

‎rust/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/contracts/rust-core-api.md‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4214,17 +4214,23 @@ successfully started session merely because other plugins failed discovery.
42144214
This is metadata selection, not manifest validation, approval or admission.
42154215
Shared cases are in `rust/fixtures/plugins/installed-versions.json`.
42164216

4217-
`plugins.installed.lifecycle` accepts `{ installed: [{ pluginID, version }],
4218-
choices?: [{ pluginID, enabled }], action?: { kind, pluginID } }`, with at most
4219-
4096 installed versions and 4096 choices. `installed` lists every installed
4220-
version and `choices` every enablement choice the user made; an empty ID or
4221-
version, a repeated version or a second choice for one plugin is
4222-
`invalid_request`. `kind` is `confirmInstall`, `enable`, `disable` or
4217+
`plugins.installed.lifecycle` accepts `{ installed: [{ pluginID, version,
4218+
languageServers? }], choices?: [{ pluginID, enabled }], action?: { kind,
4219+
pluginID } }`, with at most 4096 installed versions and 4096 choices.
4220+
`installed` lists every installed version and `choices` every enablement choice
4221+
the user made; an empty ID or version, a repeated version or a second choice for
4222+
one plugin is `invalid_request`. `languageServers` is the `languageID` of every
4223+
language server the version's manifest declares, at most 256 non-empty IDs of at
4224+
most 256 bytes, none listed twice, or `invalid_request`. `kind` is `confirmInstall`, `enable`, `disable` or
42234225
`uninstall`. It returns `{ plugins: [{ pluginID, enabled }], changed, steps,
42244226
rejection? }`. `plugins` lists every installed plugin once, ordered by ID, with
42254227
its enablement once the steps ran; an uninstalled plugin is left out.
42264228
Enablement is per plugin, not per version, and a plugin without a choice is
4227-
disabled. A choice for a plugin that is not installed is kept for a later
4229+
disabled. One enabled plugin serves a language: a plugin whose choice enables it
4230+
is still reported disabled when a plugin before it by ID is enabled and serves
4231+
one of its languages. A plugin serves the languages of its newest installed
4232+
version, compared as `major.minor.patch`; a version that is not one ranks below
4233+
every one that is. A choice for a plugin that is not installed is kept for a later
42284234
installation. `changed` says the steps change the choice for the action's
42294235
plugin. An action on a plugin with no installed version returns
42304236
`rejection: "plugin.lifecycle.notInstalled"`, no steps and the unchanged
@@ -4237,11 +4243,19 @@ backend and returns once they ended or gave up, `{ kind: "removeVersion",
42374243
version }` removes one installed version and then forgets that version's
42384244
approval record (the record stays when the removal failed), and
42394245
`{ kind: "removeState" }` removes the state directory the plugin's programs
4240-
wrote. A failed step does not stop the steps after it; the host reports every
4246+
wrote. `{ kind: "displacePlugin", pluginID, languageIDs }` is the one step on
4247+
another plugin: the host records the choice `enabled: false` for `pluginID`, then
4248+
stops its programs, and tells the user it turned that plugin off because the
4249+
action's plugin serves `languageIDs` too. A failed step does not stop the steps after it; the host reports every
42414250
failure. `confirmInstall` records `enabled: true` only when the plugin has no
42424251
choice, so a plugin disabled before an update stays disabled. `enable` records
42434252
`true` unless already chosen. `disable` records `false` unless already chosen
42444253
and then stops the programs, so nothing starts the plugin again while it stops.
4254+
An `enable` or `confirmInstall` that leaves the plugin enabled ends with one
4255+
`displacePlugin` for every other plugin, in ID order, whose choice enables it
4256+
and which serves one of the same languages, naming the shared languages; this is
4257+
what settles two plugins that choices made before the rule both enable. A
4258+
`disable` displaces nothing, so the other plugin's choice can take over.
42454259
`uninstall` stops the programs, removes every installed version in version text
42464260
order, forgets the choice if there is one and removes the state, so a later
42474261
installation starts as a first one. Shared cases are in

‎rust/crates/plugin-lifecycle/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license = "Apache-2.0"
66
description = "Enablement of installed Lithe plugins and the ordered steps of confirming an install, enabling, disabling and uninstalling one"
77

88
[dependencies]
9+
lithe-plugin-model = { path = "../plugin-model" }
910
lithe-protocol = { path = "../protocol" }
1011
serde = { version = "1.0", features = ["derive"] }
1112
serde_json = "1.0"

‎rust/crates/plugin-lifecycle/src/lib.rs‎

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,34 @@
1414
//! running program; then it removes every installed version, because the plugin page shows one
1515
//! row per plugin and an older version left behind would keep the plugin installed; then it
1616
//! forgets the choice and removes the state the programs wrote, so a later installation starts
17-
//! as a first one. Fixtures live in `rust/fixtures/plugins/installed-lifecycle.json`.
17+
//! as a first one.
18+
//!
19+
//! A language has one language server: two plugins that both serve it would race for every
20+
//! document of it, and neither would know the other answers too. So enabling a plugin, or
21+
//! confirming its installation while it is enabled, turns off every other enabled plugin whose
22+
//! newest installed version serves one of the same languages, and the host records that and
23+
//! stops their programs. Choices made before this rule can still enable two such plugins;
24+
//! enablement then keeps the first by ID and reports the others disabled, without changing the
25+
//! choices, so enabling one of them later is what settles it. Fixtures live in
26+
//! `rust/fixtures/plugins/installed-lifecycle.json`.
1827
1928
use std::collections::{BTreeMap, BTreeSet};
2029

30+
use lithe_plugin_model::PluginVersion;
2131
use lithe_protocol::{CoreError, ErrorCode};
2232
use serde::{Deserialize, Serialize};
2333
use serde_json::Value;
2434

2535
/// Upper bound of installed versions and of choices in one request.
2636
const MAX_ENTRIES: usize = 4096;
2737

38+
/// Upper bound of the languages one installed version serves; a manifest declares at most one
39+
/// language server per language it declares.
40+
const MAX_LANGUAGE_SERVERS: usize = 256;
41+
42+
/// Upper bound of one language ID, in bytes.
43+
const MAX_LANGUAGE_ID_BYTES: usize = 256;
44+
2845
/// Rejection of an action on a plugin that has no installed version: enabling it would
2946
/// silently enable a later installation, and there is nothing to uninstall.
3047
pub const NOT_INSTALLED: &str = "plugin.lifecycle.notInstalled";
@@ -48,6 +65,10 @@ struct InstalledVersion {
4865
#[serde(rename = "pluginID")]
4966
plugin_id: String,
5067
version: String,
68+
/// The `languageID` of every language server this version's manifest declares; only the
69+
/// plugin's newest installed version counts, since it is the one that runs.
70+
#[serde(default, rename = "languageServers")]
71+
language_servers: Vec<String>,
5172
}
5273

5374
/// One plugin and whether it is enabled: a choice in the request, a plugin's enablement in the
@@ -96,6 +117,15 @@ enum Step {
96117
RemoveVersion { version: String },
97118
/// Remove the state directory the plugin's programs wrote.
98119
RemoveState,
120+
/// Turn another plugin off because the action's plugin now serves `language_ids` too:
121+
/// record the choice `enabled: false` for `plugin_id`, then stop its programs. Unlike the
122+
/// other steps it is on `plugin_id`, not on the action's plugin.
123+
DisplacePlugin {
124+
#[serde(rename = "pluginID")]
125+
plugin_id: String,
126+
#[serde(rename = "languageIDs")]
127+
language_ids: Vec<String>,
128+
},
99129
}
100130

101131
#[derive(Debug, Serialize)]
@@ -135,6 +165,7 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
135165
if entry.plugin_id.is_empty() || entry.version.is_empty() {
136166
return Err(invalid("An installed plugin has an empty ID or version"));
137167
}
168+
validate_language_servers(&entry.language_servers)?;
138169
if !versions
139170
.entry(&entry.plugin_id)
140171
.or_default()
@@ -152,9 +183,10 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
152183
return Err(invalid("A plugin has two choices"));
153184
}
154185
}
186+
let served = served_languages(&request.installed);
155187
let Some(action) = &request.action else {
156188
return Ok(encode(Response {
157-
plugins: enablement(&versions, &choices, None),
189+
plugins: enablement(&versions, &choices, &served, None),
158190
changed: false,
159191
steps: Vec::new(),
160192
rejection: None,
@@ -168,7 +200,7 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
168200
let id = action.plugin_id.as_str();
169201
let Some(installed) = versions.get(id) else {
170202
return Ok(encode(Response {
171-
plugins: enablement(&versions, &choices, None),
203+
plugins: enablement(&versions, &choices, &served, None),
172204
changed: false,
173205
steps: Vec::new(),
174206
rejection: Some(NOT_INSTALLED),
@@ -214,30 +246,124 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
214246
if let Some(enabled) = after {
215247
choices.insert(id, enabled);
216248
}
249+
if after == Some(true) {
250+
steps.extend(displace_rivals(id, &served, &mut choices));
251+
}
217252
Ok(encode(Response {
218-
plugins: enablement(&versions, &choices, uninstalled),
253+
plugins: enablement(&versions, &choices, &served, uninstalled),
219254
changed: after != chosen,
220255
steps,
221256
rejection: None,
222257
}))
223258
}
224259

225-
/// Every installed plugin but `left_out`, by ID, enabled only when its choice says so.
260+
/// Every installed plugin but `left_out`, by ID, enabled only when its choice says so and no
261+
/// plugin before it by ID that is enabled already serves one of its languages.
226262
fn enablement(
227263
versions: &BTreeMap<&str, BTreeSet<&str>>,
228264
choices: &BTreeMap<&str, bool>,
265+
served: &BTreeMap<&str, BTreeSet<&str>>,
229266
left_out: Option<&str>,
230267
) -> Vec<Choice> {
268+
let none = BTreeSet::new();
269+
let mut claimed: BTreeSet<&str> = BTreeSet::new();
231270
versions
232271
.keys()
233272
.filter(|id| Some(**id) != left_out)
234-
.map(|id| Choice {
235-
plugin_id: (*id).to_string(),
236-
enabled: choices.get(id).copied().unwrap_or(false),
273+
.map(|id| {
274+
let languages = served.get(id).unwrap_or(&none);
275+
let enabled =
276+
choices.get(id).copied().unwrap_or(false) && languages.is_disjoint(&claimed);
277+
if enabled {
278+
claimed.extend(languages.iter().copied());
279+
}
280+
Choice {
281+
plugin_id: (*id).to_string(),
282+
enabled,
283+
}
284+
})
285+
.collect()
286+
}
287+
288+
/// The languages each installed plugin's newest version serves.
289+
///
290+
/// Versions are compared as `major.minor.patch`; a version that is not one ranks below every
291+
/// one that is, and of two such versions the first listed counts.
292+
fn served_languages(installed: &[InstalledVersion]) -> BTreeMap<&str, BTreeSet<&str>> {
293+
let mut newest: BTreeMap<&str, (Option<PluginVersion>, &InstalledVersion)> = BTreeMap::new();
294+
for entry in installed {
295+
let version = PluginVersion::parse(&entry.version);
296+
let current = newest.entry(&entry.plugin_id).or_insert((version, entry));
297+
if version > current.0 {
298+
*current = (version, entry);
299+
}
300+
}
301+
newest
302+
.into_iter()
303+
.map(|(id, (_, entry))| {
304+
(
305+
id,
306+
entry.language_servers.iter().map(String::as_str).collect(),
307+
)
237308
})
238309
.collect()
239310
}
240311

312+
/// Turns off, in `choices`, every other enabled plugin that serves one of `id`'s languages, and
313+
/// returns the steps that make the host do the same, in plugin ID order.
314+
fn displace_rivals<'a>(
315+
id: &str,
316+
served: &BTreeMap<&'a str, BTreeSet<&'a str>>,
317+
choices: &mut BTreeMap<&'a str, bool>,
318+
) -> Vec<Step> {
319+
let Some(languages) = served.get(id).filter(|languages| !languages.is_empty()) else {
320+
return Vec::new();
321+
};
322+
let mut steps = Vec::new();
323+
for (other, other_languages) in served {
324+
if *other == id || choices.get(other) != Some(&true) {
325+
continue;
326+
}
327+
let shared: Vec<String> = languages
328+
.intersection(other_languages)
329+
.map(|language| (*language).to_string())
330+
.collect();
331+
if shared.is_empty() {
332+
continue;
333+
}
334+
choices.insert(other, false);
335+
steps.push(Step::DisplacePlugin {
336+
plugin_id: (*other).to_string(),
337+
language_ids: shared,
338+
});
339+
}
340+
steps
341+
}
342+
343+
/// Rejects a malformed `languageServers` list: too long, an empty or overlong ID, or an ID
344+
/// listed twice, which a valid manifest never produces.
345+
fn validate_language_servers(languages: &[String]) -> Result<(), CoreError> {
346+
if languages.len() > MAX_LANGUAGE_SERVERS {
347+
return Err(invalid(
348+
"An installed plugin version serves too many languages",
349+
));
350+
}
351+
let mut seen = BTreeSet::new();
352+
for language in languages {
353+
if language.is_empty() || language.len() > MAX_LANGUAGE_ID_BYTES {
354+
return Err(invalid(
355+
"An installed plugin version serves an empty or overlong language ID",
356+
));
357+
}
358+
if !seen.insert(language.as_str()) {
359+
return Err(invalid(
360+
"An installed plugin version serves a language twice",
361+
));
362+
}
363+
}
364+
Ok(())
365+
}
366+
241367
fn invalid(message: &str) -> CoreError {
242368
CoreError::new(ErrorCode::InvalidRequest, message)
243369
}

0 commit comments

Comments
 (0)