Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
4f956f8
feat(core): 插件函数中转的协议第 4 版与 pluginFunctions 清单校验
xiaoyumuxi Nov 4, 2026
45ba3f0
feat(core): relay plugin function calls between plugin backends
xiaoyumuxi Nov 4, 2026
e634b5c
test(core): cover the cross-plugin function relay on real sessions
xiaoyumuxi Nov 4, 2026
6b6d1ec
docs(contract): 插件函数跨插件调用的清单 schema、夹具与 README
xiaoyumuxi Nov 4, 2026
55bd54d
feat(macos): 接入插件函数中转的快照发布与被调方后端启动
xiaoyumuxi Nov 4, 2026
5156b66
docs(contract): 记录 lsp.configureFunctionRelay 命令
xiaoyumuxi Nov 4, 2026
7354aa3
docs(notes): 记录插件函数跨插件中转的第一阶段,并登记功能矩阵
xiaoyumuxi Nov 4, 2026
e4bfd89
fix(core): 中转调用的被调方失败使用独立数值错误码
xiaoyumuxi Nov 4, 2026
8e21f84
Merge branch 'preview' into feat/1495-plugin-function-relay
xiaoyumuxi Nov 4, 2026
970e3dd
Merge remote-tracking branch 'origin/preview' into review/1742
xiaoyumuxi Nov 5, 2026
3488d0d
Merge remote-tracking branch 'origin/preview' into review/1742
xiaoyumuxi Nov 5, 2026
1524e85
fix(toolchain-tests): 测试向量从共享 fixture 读工具 ID,修复 preview 上的宿主语言闸门
xiaoyumuxi Nov 5, 2026
302e3ca
fix(plugin-functions): 中转按会话分道写入,父调用显式关联,上下文失效即结束调用
xiaoyumuxi Nov 5, 2026
58e8faa
Merge remote-tracking branch 'origin/preview' into review/1742
xiaoyumuxi Nov 5, 2026
f9aa344
test(macos): 跨插件调用测试按 #1747 的新目录读取 backend fixtures
xiaoyumuxi Nov 5, 2026
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

插件之间复用运行时、JSON 声明和计算能力属于整个插件平台,不应由某个 Debug 格式顺带定义。本提案从 [Debug #1210](https://github.com/1lck/Lithe-IDEA/issues/1210) 拆出依赖、导出和授权,挂在[公共扩展契约](2026-10-13-public-extension-contract-and-provider-api.md)下;Debug 的 Registry 与第三方 DAP 接入不依赖它。

方向仍是插件好写、共享逻辑尽量复用,但安装闭包(某个插件及其全部间接依赖)、版本冲突和信任必须先有明确规则。以下只是待单独实施的决策边界,不开放 `pluginDependencies`、`exports`、跨包函数或工作流字段,也不更改现有私有函数契约。
方向仍是插件好写、共享逻辑尽量复用,但安装闭包(某个插件及其全部间接依赖)、版本冲突和信任必须先有明确规则。其中**第一阶段已经落地**:两个已安装、已启用的插件之间,可以经 Rust Core 中转调用被明确导出的函数(见提案第 5 节)。其余仍只是待单独实施的决策边界:安装依赖、版本范围解析、依赖闭包、跨包资源租约和工作流字段都没有开放,`pluginDependencies` 也不存在。

## 问题

Expand Down Expand Up @@ -45,16 +45,29 @@

若需要跨包资源引用,正式契约使用显式带标签的类型,例如专门的 backend/lease 变体;不靠普通参数恰好叫 `backendRef` 来识别。宿主签发的租约记录资源所有者、调用链、操作与失效代次,跨包只传受限引用;`${pluginDir}` 永远按声明资源的包解析。用户参数里的同名字符串和对象仍然只是数据。

跨包操作只能由宿主在已授权的导出边界调度到资源所属会话,不能将调用方 pluginID 写入目标会话。后端回调宿主重新进入注册表的禁止规则保留;否则等待中的处理器可能等待自己。如何将新授权模型加入函数路由、如何区分私有与公开调用,需要单独版本化,不能删除旧 `wrongPlugin` 或 `recursiveCall` 检查来实现。
跨包操作只能在已授权的导出边界调度到资源所属会话,不能将调用方 pluginID 写入目标会话。(第一阶段由 Rust Core 在导出边界中转,宿主只负责启动被调方后端;调用方 ID 只出现在 Core 填写的 `caller` 请求字段里。)后端回调宿主重新进入注册表的禁止规则保留;否则等待中的处理器可能等待自己。如何将新授权模型加入函数路由、如何区分私有与公开调用,需要单独版本化,不能删除旧 `wrongPlugin` 或 `recursiveCall` 检查来实现。

### 4. 不在此处偷改已实现决策

[插件函数契约](../../implemented/architecture/2026-10-20-plugin-function-contract.md)仍是当前行为真源:函数私有,领域结果走类型化契约。此提案没有修改它。若以后采用函数传输领域结果或依赖导出,先在实现 PR 中修订对应决策,给出输入/结果、授权、错误优先级与兼容 fixtures,再改 Core 和双端宿主。
[插件函数契约](../../implemented/architecture/2026-10-20-plugin-function-contract.md)仍是当前行为真源:函数默认私有,领域结果走类型化契约。第一阶段在它的决策 8 里增加了“明确导出的函数可经 Core 中转被另一个插件调用”,同一 PR 给出了输入/结果、错误优先级与兼容 fixtures;私有、递归和领域结果走类型化契约的规则没有改动。以后若采用函数传输领域结果,或让导出带上依赖闭包、资源租约,仍须先在实现 PR 中修订对应决策,再改 Core 和双端宿主。

新的字段属于统一 `plugin.json` 的能力演进;基础权限或身份语义变化需要新的基础 API。没有 Debug draftVersion,也不要求现有 Build/VCS/UI/语言插件迁移。现有 `backend: 1` 的全局、网页触发行为不变;新的进程共享方式另行版本化。

通用 workflow 尚无证明必要的真实输入,不在这个依赖提案里重新引入 steps 或取值语言。先通过实际导出契约完成复用,再判断是否有需要由宿主解释的公共流程。

### 5. 第一阶段已落地的部分

对应计划 API-35 的第一阶段,不新增 API 编号,决策正文写在[插件函数契约](../../implemented/architecture/2026-10-20-plugin-function-contract.md)的决策 8,契约写在 `shared/contracts/plugins/v1/README.md` 的 “Cross-plugin calls (protocol version 4)”。它只落地了本提案里不依赖安装解析的那一部分:

- 清单新能力键 `requires.pluginFunctions: 1` 与顶层 `pluginFunctions {exports, calls}`。导出是 `{id, version, context}`,`context` 是封闭集合,第一阶段只有 `"workspace"`,缺失或未知就拒绝整个清单:这兑现了第 2 节“没有定义可接受调用上下文的导出,不允许跨包调用”。调用方只声明 `{id, version}`,版本必须相等,不相等是 `plugin.function.versionMismatch`。
- 协议 `litheFunctions` 增加版本 4,只有协商到 4 的后端能用 `lithe/host/functions/providers` 与 `lithe/host/functions/invoke`。运行时发现被调方,清单里不写被调方的插件 ID。
- 所有检查在 Rust Core:声明、导出、双方启用、版本、调用链(拒绝 A→B→A 和自己调自己)、深度、每个调用方的并发、参数与结果大小、超时、沿链取消,远程工作区拒绝(API-40)。被调方未运行时 Core 只发 `functionProviderStartRequired`,宿主把它当作 I/O 去启动后端,超时即失败。
- 信任不传递:被调方用自己的权限运行,Core 不把调用方的插件 ID 写进被调方的会话身份,只在转发的请求里加一个由 Core 填写的 `caller: {pluginID}` 字段;契约要求导出函数不得接受调用方传来的可执行路径或命令。没有新增权限项,`calls` 声明加 `exports` 选择加入就是双方的同意。
- 安装、更新、卸载、启用、禁用即时更新发现结果,进行中的调用以明确的错误结束;带后端的插件在 macOS 上仍只在启动时注册,新安装或更新的后端插件要重启后才出现在发现结果里(属于 #1515 的后续工作)。
- 旧的 `plugin.function.wrongPlugin` 与 `plugin.function.recursiveCall` 检查一个都没有删除。

**没有落地**:安装时解析依赖、版本范围与冲突报告、依赖闭包和事务恢复、卸载或禁用时的反向依赖提示、启动时发现缺失依赖并标记阻塞、跨包资源租约和带标签的资源引用类型、会启动程序的导出、弱依赖和多版本并存。这些仍按本提案的待办和验收标准单独实施;第一阶段也不引入任何安装期的依赖声明,被调方是否已安装完全由运行时发现。

### 正确做法与反例

正确做法:两个插件依赖同一个运行时,安装器先求兼容版本并展示共同资源的来源与权限,宿主只对获准导出执行调用。
Expand All @@ -73,7 +86,7 @@
- 独立任务/PR 完成安装解析、版本冲突、来源固定、哈希校验、事务恢复及并发安装规则,之后才公布新能力。
- 有共同兼容版本与不兼容菱形两组标准用例,冲突报告带确定排序的两条依赖路径;失败不修改原安装闭包。
- 禁用/卸载、启动时依赖缺失、升级失效及恢复分别验证反向依赖与资源清理;任何失败不误停不相关插件。
- 社区依赖官方、同名包来自不同来源、签名变化和导出撤回均有拒绝输入;旧私有函数的归属和递归拒绝语义不变。
- 社区依赖官方、同名包来自不同来源、签名变化和导出撤回均有拒绝输入;旧私有函数的归属和递归拒绝语义不变。(第一阶段已覆盖导出撤回、禁用与卸载的进行中调用,以及旧 `wrongPlugin`、`recursiveCall` 不变;来源与签名相关的输入属于安装解析,尚未覆盖。)
- 如涉及函数协议,在 `lithe-plugin-docs` 验证 Node/Java SDK;正式契约与 Core 一起落地后再同步副本。
- #1210 的基础 Registry 和第三方 DAP 验收可以完全不安装依赖包而完成。

Expand All @@ -83,12 +96,13 @@

## 验证

本次只运行 `./scripts/agent-notes/verify-agent-notes.sh` 和文档检查,没有依赖解析器或跨包调用实现,不能声称安装或授权用例通过。实施时使用 `write-stable-tests`、`./scripts/architecture/verify-shared-contracts.sh`、`./scripts/rust-core/verify-rust-core.sh`,并分别验证两个宿主和函数 SDK;新增资源时遵守工作树复用与安装目录只读要求。
第一阶段的跨插件调用由 Core 测试(`plugins::plugin_functions`、`lsp::interface::function_relay`、`lsp::interface::engine`)和 macOS 单测(`MacPluginBackendSessionsTests`、`PluginHostServiceDispatchTests`、`PluginPublicHostAPITests`)覆盖,细节见实现 Note 的“验证”。依赖解析器和安装期规则仍没有实现,不能声称安装、版本冲突或依赖授权用例通过。实施时使用 `write-stable-tests`、`./scripts/architecture/verify-shared-contracts.sh`、`./scripts/rust-core/verify-rust-core.sh`,并分别验证两个宿主和函数 SDK;新增资源时遵守工作树复用与安装目录只读要求。

## 适用范围

- `rust/lithe-core/src/plugins/`
- `rust/lithe-core/src/lsp/interface/plugin_function.rs`
- `rust/lithe-core/src/lsp/interface/function_relay.rs`
- `shared/contracts/plugins/v1/`
- `macos/Sources/Lithe/Platform/MacOS/`
- `windows/tauri/src-tauri/src/plugin_host/`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,14 @@ final class PluginHostServiceDispatcher {
Array(Set(uniqueHandlers.map(\.service))).sorted()
}

/// Services Core answers itself. The host has no handler for them, but
/// the plugin is still granted them: a call between two plugins runs
/// inside Core, and the host only starts the callee's backend.
static let coreServedGrants: Set<String> = ["functions"]

func supportedGrants(_ requested: [String]) -> [String] {
requested.filter { grant in
if Self.coreServedGrants.contains(grant) { return true }
if handlers[grant] != nil { return true }
let legacy: [String]
switch grant {
Expand Down
31 changes: 31 additions & 0 deletions macos/Sources/Lithe/Core/Rust/RustCoreBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4289,6 +4289,37 @@ struct RustCoreBridge: Sendable, IncrementalLanguageServerRuntimeCore {
).map { (response: LspHostNotifyResponse) in response.delivered }
}

private struct LspConfigureFunctionRelayRequest: Encodable {
struct Plugin: Encodable {
let pluginID: String
let exports: [ToolingJSONValue]
let calls: [ToolingJSONValue]
}

struct Context: Encodable {
let contextID: String
let generation: Int
let authority: String
}

let plugins: [Plugin]
let contexts: [Context]
}

/// Replaces the enabled plugins and live workspace contexts that Core's
/// relay between plugin backends works from.
func lspConfigureFunctionRelay(_ snapshot: PluginFunctionRelaySnapshot) -> Result<Void, CoreCallError> {
executeVoid(
command: "lsp.configureFunctionRelay",
payload: LspConfigureFunctionRelayRequest(
plugins: snapshot.plugins.map { .init(pluginID: $0.pluginID, exports: $0.exports, calls: $0.calls) },
contexts: snapshot.contexts.map {
.init(contextID: $0.contextID, generation: $0.generation, authority: $0.isRemote ? "remote" : "local")
}
)
)
}

/// Cancels a pending operation. Its completion event still arrives, carrying
/// a cancellation error, so callers never leak a continuation.
func lspCancelOperation(sessionID: String, operationID: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ extension RustCoreBridge: LanguageServerRuntimeCore {
.mapError(Self.runtimeFailure)
}

func configurePluginFunctionRelay(
_ snapshot: PluginFunctionRelaySnapshot
) -> Result<Void, LanguageServerRuntimeFailure> {
lspConfigureFunctionRelay(snapshot).mapError(Self.runtimeFailure)
}

func updateWorkspaceConfiguration(
sessionID: String,
configuration: ToolingJSONValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
private var backgroundQueue: [String] = []
private var backgroundStarts: [String: Task<Void, Never>] = [:]
private var commandStarts: [String: Task<Void, Never>] = [:]
/// Starts a call between plugins waits for, one per plugin.
private var providerStarts: [String: Task<Void, Never>] = [:]
/// The last snapshot Core's relay accepted; an unchanged one is not sent again.
private var publishedRelaySnapshot: PluginFunctionRelaySnapshot?
private var pendingCommands: [String: [ToolingJSONValue]] = [:]
private var packages: [String: Package] = [:]
/// Disabled plugins start again once enabled.
Expand Down Expand Up @@ -193,6 +197,7 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
)
added.insert(id)
}
publishFunctionRelay()
activateForOpenWorkspaces(added)
}

Expand Down Expand Up @@ -475,6 +480,7 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
/// Tells every running backend granted `workspace` that the contexts
/// changed; it asks `listContexts` for them.
private func contextsDidChange(revision: Int) {
publishFunctionRelay()
for (pluginID, entry) in running where entry.hostServices.contains("workspace") {
notify(pluginID: pluginID, method: "lithe/host/workspace/didChangeContexts",
params: .object(["revision": .integer(revision)]))
Expand Down Expand Up @@ -528,16 +534,84 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
))
}

// MARK: Calls between plugins

/// Reports to Core's relay which plugins are enabled and which workspace
/// contexts are live. Core decides from these what a call may do; this
/// side only states the facts, and states them again whenever one of
/// them changes, so calls in flight see the change.
private func publishFunctionRelay() {
let snapshot = functionRelaySnapshot()
guard snapshot != publishedRelaySnapshot else { return }
// A snapshot Core did not accept stays unpublished, so the next
// change sends it again.
guard case .success = core.configurePluginFunctionRelay(snapshot) else { return }
publishedRelaySnapshot = snapshot
}

private func functionRelaySnapshot() -> PluginFunctionRelaySnapshot {
let plugins = packages.compactMap { pluginID, package -> PluginFunctionRelaySnapshot.Plugin? in
guard !disabledPluginIDs.contains(pluginID), !replacedPluginIDs.contains(pluginID), canStart(pluginID),
let declaration = Self.pluginFunctionDeclaration(ofManifest: package.manifest) else { return nil }
return .init(pluginID: pluginID, exports: declaration.exports ?? [], calls: declaration.calls ?? [])
}
let contexts = hostDispatcher.contexts.contexts.map { context -> PluginFunctionRelaySnapshot.Context in
let isRemote: Bool
if case .remote = context.authority { isRemote = true } else { isRemote = false }
return .init(contextID: context.contextID, generation: context.generation, isRemote: isRemote)
}
return PluginFunctionRelaySnapshot(
plugins: plugins.sorted { $0.pluginID < $1.pluginID },
contexts: contexts.sorted { $0.contextID < $1.contextID }
)
}

/// The `pluginFunctions` section of a normalized manifest, left as Core
/// wrote it: Core validated it once and reads it again in the relay.
private struct PluginFunctionDeclaration: Decodable {
let exports: [ToolingJSONValue]?
let calls: [ToolingJSONValue]?
}

private struct PluginFunctionsManifest: Decodable {
let pluginFunctions: PluginFunctionDeclaration?
}

private static func pluginFunctionDeclaration(ofManifest manifest: Data) -> PluginFunctionDeclaration? {
// log-exempt: Core 已校验过清单;解码失败只会让该插件不进中转快照
try? JSONDecoder().decode(PluginFunctionsManifest.self, from: manifest).pluginFunctions
}

/// Starts the backend a call between plugins waits for. This is the
/// host's whole part in the call: Core checks and relays it. The start
/// is bounded like every other one, by the `initialize` time limit, and
/// a failure is reported here while Core ends the waiting call with its
/// own error once its wait runs out.
private func startProviderBackend(_ pluginID: String) {
guard providerStarts[pluginID] == nil else { return }
providerStarts[pluginID] = Task { @MainActor [weak self] in
guard let self else { return }
defer { if !Task.isCancelled { self.providerStarts[pluginID] = nil } }
do {
_ = try await self.session(for: pluginID)
} catch {
if !Task.isCancelled, !(error is CancellationError) { self.onActivationFailure?(pluginID, error) }
}
}
}

/// Stops the backend until `enable` is called.
func disable(_ pluginID: String) {
disabledPluginIDs.insert(pluginID)
stop(pluginID)
publishFunctionRelay()
}

/// The backend may start again; with a workspace open, one that starts
/// on `onWorkspaceOpen` starts now instead of with the next workspace.
func enable(_ pluginID: String) {
disabledPluginIDs.remove(pluginID)
publishFunctionRelay()
activateForOpenWorkspaces([pluginID])
}

Expand All @@ -547,12 +621,14 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
replacedPluginIDs.insert(pluginID)
packages[pluginID] = nil
stop(pluginID)
publishFunctionRelay()
}

private func stop(_ pluginID: String) {
backgroundQueue.removeAll { $0 == pluginID }
backgroundStarts.removeValue(forKey: pluginID)?.cancel()
commandStarts.removeValue(forKey: pluginID)?.cancel()
providerStarts.removeValue(forKey: pluginID)?.cancel()
pendingCommands[pluginID] = nil
hostDispatcher.endSession(pluginID: pluginID)
guard let entry = running.removeValue(forKey: pluginID) else { return }
Expand Down Expand Up @@ -685,6 +761,9 @@ final class MacPluginBackendSessions: PluginBackendRequesting, PluginBackendActi
session.onHostRequestCancelled = { hostRequestID in
hostDispatcher.cancel(hostRequestID: hostRequestID, pluginID: pluginID, sessionID: sessionToken)
}
session.onFunctionProviderStartRequired = { [weak self] providerID in
self?.startProviderBackend(providerID)
}
entry.session = session
try await waitUntilReady(session, rootURL: backend.stateDirectory)
try Task.checkCancellation()
Expand Down
Loading
Loading