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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Agent 笔记:关闭标签测试的快捷键变更必须请求刷新

状态:已实现

## 先说结论

无头测试直接修改快捷键时,也要像真实设置页一样通知界面刷新。否则输入仍使用上一帧的绑定,后台任务偶然产生的刷新会掩盖问题。关闭标签测试现在明确请求刷新,并在所有平台验证用户新增的绑定,避免 macOS 默认键位掩盖测试缺口。

## 问题

[#1885](https://github.com/1lck/Lithe-IDEA/issues/1885) 来自 Linux CI:添加 Control-W 后日志页签仍然打开。`6268d92923` 引入的测试只改内存设置,没有真实设置页 `SetKeymap` 的刷新通知;测试驱动的 `Pump` 只在已有唤醒标记时绘制,不保证每次调用都重画。

macOS 原测试直接使用初始注册的 Command-W,因此无法覆盖同一缺陷。在 macOS 显式新增 Control-W 的独立探针及正式回归都复现相同失败,排除了 Java 插件的影响。

## 决策

测试的绑定辅助函数在更新后调用现有刷新接口,下一次输入前由驱动处理刷新;不修改产品逻辑或框架行为。新增跨平台用户绑定回归,先消费已有帧,再修改绑定并立即触发,防止无关后台唤醒提供偶然的刷新。

正确做法是“更新测试设置、明确请求刷新、发送按键”。不要只更新字段后靠下一次后台活动、延长等待或重复运行获取绿灯。

## 考虑过的备选方案

- 每次 Pump 都强制绘制:改变成熟框架的按需绘制语义,掩盖测试与真实设置路径的差异。
- 加 sleep 或重试按键:依赖后台时序,还可能重复关闭其他标签。
- 只跑 macOS 默认键位:遗漏用户自定义绑定及其他平台。

## 后果

测试不再依赖偶然刷新;新增少量测试辅助代码和一项跨平台回归。产品运行时、默认快捷键、资源、性能与插件契约均不改变。

## 验证

- `scripts/desktop/desktop-go.sh test ./internal/workbench -run '^TestCloseTabUserBindingClosesTheSelectedEditorTab$' -count=1`:未请求刷新时失败,日志页签仍为 1;修复后通过。
- `scripts/desktop/desktop-go.sh test ./internal/workbench -run '^TestCloseTab' -count=1` 与相同范围的 `-race`。
- `./scripts/agent-notes/verify-agent-notes.sh` 与测试稳定性静态检查。
- Linux、Windows 的目标平台结果以配套 PR 的 CI 为准,本地 macOS 回归不能代替它们。

## 适用范围

- `desktop/internal/workbench/close_tab_shortcut_test.go`
- `desktop/internal/workbench/settings.go` 的真实设置变更刷新顺序。
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,28 @@ Run 的首次调试复用既有适配器加载入口,完成后检查原调用

真实插件测试在创建工作台后就登记无交互清理,不依赖成功路径的关闭动作。即使断言在断点暂停或文档未保存时中止,也直接关闭测试拥有的项目,等待后台任务、停止剩余运行,并验证 Core 调试会话已移除;不能把需要用户确认的关闭弹窗当作失败清理。

## 项目 JDK 与语言服务器的编译目标

普通源码目录没有 Maven 的目标版本约束时,Viva 曾只把项目选择的 JDK 交给 Run,没有在后端解析语言服务器启动时传入契约已有的可选 `tools`。JDT 因而按随包 Java 21 编译,Run 却使用用户选择的 Java 8,生成的 major 65 class 会被 Java 8 拒绝。只检查 Maven 构建的 class 无法发现这个问题,必须也检查 JDT 的运行前构建。

宿主现在复用 Build/Run 的工具选择,将清单声明工具的 executable、home、version 作为原生安装事实交给后端。可选安装不存在时不覆盖服务器已有配置;读取失败继续返回失败,项目脚本不作为安装事实,环境与凭据不跨这个边界。Java 插件根据这些事实配置 JDT,宿主不解释 Java 版本或 POM。

例如选择 Java 8 的普通源码项目,应在 JDT 编译后仍生成 major 52,并实际用 Java 8 运行、暂停和停止;不能仅让 Maven 的 source/target 掩盖默认 JDK 接线缺失。回归同时覆盖普通目录、外部 JAR、Ant 源码布局和 Eclipse 多源码根;Ant 布局的通过不等于支持执行 Ant target。

冷启动还受到共享握手期限约束。Windows 虚拟机中的真实 JDT 握手实测约 15.4 秒,超过原 Core 默认的 10 秒。Core 的通用默认期限改为 60 秒,显式期限仍优先,取消、超时终止和进程树回收保持原行为;不按 Java 名称分支,也不增加 Core 启动重试。Core 已将声明中的 runtime 归一为工具计划,入口路径也已经展开为绝对路径。Viva 旧桥接只处理旧 runtime 的入口,导致工具计划丢掉 JAR 文件。原生适配器现在把 Core 的入口转换回包内相对路径后逐段验证,拒绝越界和符号链接;再通过 Core 已有的 toolchains.instantiateLaunch 使用定义方的模板。Java 工具的 `-jar` 和入口位置由 Java 插件声明,宿主没有 Java 分支。Gradle 集成同时核验真实设置函数与中转没有超时;不通过延长设置调用期限掩盖进程退出。注入时间的回归验证 15 秒时仍可初始化、61 秒时失败并终止,不让测试真实等待一分钟。目标平台完整工作流必须另行运行,单项 Core 回归不代表 Windows 已验收。

## 冷索引期间的动作恢复

JDT 的 ServiceReady 和项目导入事件并不保证索引任务已经结束。Windows 冷导入中,动作查询在索引结束前用完 6 秒期限,插件返回空动作;索引稍后结束却没有新的模型变化,动作一直为空。将测试等待从 15 秒延长至 120 秒仍失败,证明不能只延长测试等待来解决。

Java 插件现在记录每个活动工作区的一份延迟查询状态,启用 JDT 公开的 `progressReportProvider` 扩展,并监听既有 `language/progressReport` complete 边界;只有发生过瞬态查询失败才在该边界重新注册动作。每个工作区最多跟踪 128 个活动任务标识,等活动任务全部完成才重查,避免零散完成事件在索引结束前耗尽预算。完成计数同时覆盖“工作完成先到、查询超时后到”的竞态。一个失败周期最多自动刷新三次,成功重置预算,工作区和服务器代际变化会隔离旧状态;关闭工作区时删除记录。不新增轮询、计时线程或项目语义模型。

Core 保留标准 LSP 进度的所有权;插件不申请转发宿主保留的方法。JDT 自己选择扩展通道,进度通知会跨现有插件通道转发,但普通进度报告只做常量级检查,不会逐条触发动作查询。延迟、恢复和预算耗尽分别进入 SDK 结构化诊断;这不修改宿主 API。Windows 长 classpath 另由插件复用 manifest JAR 规划,JAR 字节交给宿主临时文件接口,运行结束由宿主删除,不写安装目录。长 module-path 仍不属于已完成范围。

## 考虑过的备选方案

- 放宽 Core 声明解码:掩盖宿主把路由信息当成声明的错误,也允许其他无效字段通过。
- 固定增大等待或对所有进度报告重查:前者已在真实冷导入中证明不能恢复丢失的动作,后者会在正常索引时反复触发昂贵查询。只在失败后的工作完成边界重查。
- 要求用户编辑文件或先打开 Debug 面板:把接线时序变成操作前提,不能修复首次使用。
- 在 Java 插件里按 PID 杀进程:插件不拥有宿主进程树,会重复实现原生清理并误伤外部附加目标。

Expand All @@ -50,5 +69,6 @@ Run 的首次调试复用既有适配器加载入口,完成后检查原调用

- `desktop/internal/pluginservice/hostactions.go`、`hostproviders.go`、`runs.go`
- `desktop/internal/workbench/debug_start.go`、`debug.go`、`pluginworkbench.go`、`pluginbackends.go`
- `desktop/internal/workbench/java_plugin_e2e_test.go`、`java_legacy_e2e_test.go`
- `desktop/internal/workbench/java_plugin_e2e_test.go`、`java_legacy_e2e_test.go`、`java_unmanaged_e2e_test.go`
- `desktop/internal/pluginservice/backendlaunch.go`、`backendlaunch_test.go`;Core 的语言服务器引擎握手期限
- 通用职责边界见 [插件后端会话与宿主服务](../architecture/2026-11-10-viva-plugin-host-services-and-backend-sessions.md)。
15 changes: 15 additions & 0 deletions desktop/internal/core/plugin_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,18 @@ func TestPluginLaunchArchitectureAcceptsOnlyTheContractSpelling(t *testing.T) {
}
}
}

func TestRuntimeToolPlanKeepsAnEntryForNativeInstantiation(t *testing.T) {
manifest, err := os.ReadFile(contractfixture.Path("fixtures/run/tool-reference-launch.plugin.json"))
if err != nil {
t.Fatal(err)
}
root := t.TempDir()
plan, err := NewClient().PlanPluginLanguageServer(testContext(t), manifest, "script", PluginBackendDirectories{PluginDirectory: root, StateDirectory: t.TempDir(), Architecture: PluginArchitecture()})
if err != nil {
t.Fatal(err)
}
if plan.Launch.Kind != "tool" || plan.Launch.Entry == "" {
t.Fatalf("runtime lost its entry: %+v", plan.Launch)
}
}
39 changes: 35 additions & 4 deletions desktop/internal/pluginbackend/pluginbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ type Runner struct {
lookup func(name string) (string, error)
// tools resolves a plan's {kind: "tool"} launch to an executable. Nil refuses
// such a launch rather than guessing a program.
tools ToolResolver
tools ToolResolver
toolEntries ToolEntryResolver
// initializeTimeout is the startup-handshake deadline applied to a request
// that does not carry its own. Zero keeps Core's default.
initializeTimeout time.Duration
Expand All @@ -174,8 +175,23 @@ func (r *Runner) SetInitializeTimeout(timeout time.Duration) { r.initializeTimeo
type ToolExecutable struct {
Path string
Environment map[string]string
// Arguments are the complete vector Core instantiated for a package entry.
Arguments []string
}

// ToolEntryLaunch contains verified native paths and the opaque inputs to
// Core's tool template. No host code interprets a tool's entry grammar.
type ToolEntryLaunch struct {
Tool, Executable, Entry, MinimumVersion string
RuntimeArguments, Arguments []string
}

// ToolEntryResolver applies a Core-owned tool template to verified native facts.
type ToolEntryResolver func(context.Context, string, string, ToolEntryLaunch) (ToolExecutable, error)

// SetToolEntryResolver installs the entry planner before any backend starts.
func (r *Runner) SetToolEntryResolver(resolver ToolEntryResolver) { r.toolEntries = resolver }

// ToolResolver resolves the tool definition a plugin launch names to the
// executable the host runs, using the host's toolchain layers for the workspace
// the process serves (workingDirectory, empty for a process with no workspace).
Expand Down Expand Up @@ -278,9 +294,24 @@ func (r *Runner) Start(ctx context.Context, request StartRequest) (Session, erro
return Session{}, err
}
}
// A tool launch runs the installation the toolchain layers select; the plan's
// own environment wins over what the selection declares.
arguments := Arguments(plan, resolved.Entry)
// A tool entry is instantiated by Core using its declaring plugin's
// template after native path verification. This also covers packaged tools.
var toolEnvironment map[string]string
if plan.Launch.Kind == pluginhost.LaunchTool && resolved.Entry != "" {
if r.toolEntries == nil {
return Session{}, fmt.Errorf("%w: this host cannot instantiate tool entries", ErrRequest)
}
tool, err := r.toolEntries(ctx, request.PluginID, request.WorkingDirectory, ToolEntryLaunch{
Tool: plan.Launch.Tool, Executable: executable, Entry: resolved.Entry,
MinimumVersion: plan.Launch.MinimumVersion, RuntimeArguments: plan.Launch.RuntimeArguments, Arguments: plan.Arguments,
})
if err != nil {
return Session{}, err
}
executable, toolEnvironment, arguments = tool.Path, tool.Environment, tool.Arguments
}
// A bare tool launch uses the selected installation and the opaque args.
if executable == "" && plan.Launch.Kind == pluginhost.LaunchTool {
if r.tools == nil {
return Session{}, fmt.Errorf("%w: this host cannot resolve tool launches", ErrRequest)
Expand All @@ -303,7 +334,7 @@ func (r *Runner) Start(ctx context.Context, request StartRequest) (Session, erro
PluginID: request.PluginID,
Role: role,
ExecutablePath: executable,
Arguments: Arguments(plan, resolved.Entry),
Arguments: arguments,
Environment: mergeEnvironment(mergeEnvironment(toolEnvironment, plan.Environment), request.Environment),
SessionPolicy: plan.SessionPolicy,
RootURI: request.RootURI,
Expand Down
50 changes: 50 additions & 0 deletions desktop/internal/pluginbackend/pluginbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
"sort"
"sync"
"testing"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/contractfixture"
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginhost"
"github.com/1lck/Lithe-IDEA/desktop/internal/toolchain"
)

// fakeKernel records what the runner asked Core to do.
Expand Down Expand Up @@ -635,3 +638,50 @@ func TestStartResolvesAToolThroughTheHostToolchain(t *testing.T) {
t.Fatalf("environment = %v", env)
}
}

func TestToolEntryUsesCoreInstantiationAfterNativeVerification(t *testing.T) {
root := backendPackage(t)
entry := filepath.Join(root, "入口 with 空格.script")
if err := os.WriteFile(entry, []byte("fixture"), 0600); err != nil {
t.Fatal(err)
}
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
plan := core.PluginBackendPlan{}
plan.Launch.Kind, plan.Launch.Tool = pluginhost.LaunchTool, "interpreter"
plan.Launch.Entry = entry
plan.Launch.RuntimeArguments = []string{"--runtime-option"}
plan.Arguments = []string{"--stdio"}
kernel := &fakeKernel{plan: plan, started: core.PluginBackendStarted{SessionID: "entry-session"}}
runner := New(kernel, echoServices(t))
if _, err := runner.Start(context.Background(), startRequest(root)); !errors.Is(err, ErrRequest) {
t.Fatalf("missing template resolver: %v", err)
}
manifest, err := os.ReadFile(contractfixture.Path("fixtures/run/tool-reference-launch.plugin.json"))
if err != nil {
t.Fatal(err)
}
registry, err := toolchain.RegistryInput([]json.RawMessage{manifest})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
runner.SetToolEntryResolver(func(_ context.Context, _, _ string, input ToolEntryLaunch) (ToolExecutable, error) {
if input.Entry != entry || input.Tool != "interpreter" || len(input.RuntimeArguments) != 1 || input.RuntimeArguments[0] != "--runtime-option" || len(input.Arguments) != 1 || input.Arguments[0] != "--stdio" {
t.Fatalf("wrong verified facts for Core: %+v", input)
}
// Use the linked Core and its shared non-Java contract fixture. Native
// adapters only supply facts; Core owns the tool's argument grammar.
launch, err := core.NewClient().InstantiateToolchainLaunch(ctx, registry, input.Tool, executable, "", input.Entry, input.RuntimeArguments, input.Arguments)
return ToolExecutable{Path: launch.Executable, Arguments: launch.Arguments}, err
})
if _, err := runner.Start(context.Background(), startRequest(root)); err != nil {
t.Fatal(err)
}
if got := kernel.started_.Arguments; len(got) != 3 || got[0] != "--runtime-option" || got[1] != plan.Launch.Entry {
t.Fatalf("Core arguments lost: %v", got)
}
}
27 changes: 24 additions & 3 deletions desktop/internal/pluginhost/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pluginhost

import (
"fmt"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -115,15 +116,21 @@ func VerifyLaunch(packageDirectory string, launch Launch) (Resolved, error) {
}
return Resolved{Required: required}, nil
case LaunchTool:
// The tool is resolved by the host's toolchain registry, not from the package.
// Current Core plans represent runtime entries as tools. Apply the same
// native path verification as a legacy runtime before Core instantiates
// the tool's entry template; a bare tool has no entry to verify.
if launch.Tool == "" {
return Resolved{}, fmt.Errorf("%w: a tool launch names no tool", ErrPackagePath)
}
required, err := ResolvePackagePaths(packageDirectory, launch.Required)
entry, err := plannedPackagePath(packageDirectory, launch.Entry)
if err != nil {
return Resolved{}, err
}
return Resolved{Required: required}, nil
runtime, err := plannedPackagePath(packageDirectory, launch.PackagedRuntime)
if err != nil {
return Resolved{}, err
}
return VerifyLaunch(packageDirectory, Launch{Kind: LaunchRuntime, Tool: launch.Tool, Entry: entry, PackagedRuntime: runtime, Required: launch.Required})
default:
return Resolved{}, fmt.Errorf("%w: unknown launch kind %q", ErrPackagePath, launch.Kind)
}
Expand Down Expand Up @@ -179,3 +186,17 @@ func GrantServices(requested []string, services *Services) []string {
}
return granted
}

// Core expands tool entry paths to absolute native paths. Relativizing them
// preserves ResolvePackagePath's segment-by-segment no-symlink verification;
// traversal, other volumes and files outside the package remain refusals.
func plannedPackagePath(root, path string) (string, error) {
if !filepath.IsAbs(path) {
return path, nil
}
relative, err := filepath.Rel(root, path)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrPackagePath, err)
}
return filepath.ToSlash(relative), nil
}
11 changes: 11 additions & 0 deletions desktop/internal/pluginhost/launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,14 @@ func TestVerifyLaunchAcceptsHostRuntimeWithPackagedEntry(t *testing.T) {
t.Fatalf("resolved = %+v", resolved)
}
}

func TestToolEntryIsVerifiedInsideThePackage(t *testing.T) {
root := launchPackage(t)
resolved, err := VerifyLaunch(root, Launch{Kind: LaunchTool, Tool: "interpreter", Entry: filepath.Join(root, "lib/main.js")})
if err != nil || resolved.Entry != filepath.Join(root, "lib/main.js") {
t.Fatalf("tool entry was not verified: %+v %v", resolved, err)
}
if _, err := VerifyLaunch(root, Launch{Kind: LaunchTool, Tool: "interpreter", Entry: "../outside.js"}); err == nil {
t.Fatal("tool entry escaped the installed package")
}
}
48 changes: 47 additions & 1 deletion desktop/internal/pluginservice/backendlaunch.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func (s *Service) resolveBackendLaunch(ctx context.Context, pluginID, version st
}
// The function's params are the plan's resolveParams themselves, as the
// contract says: serverID, platform, the four directories and the settings.
params := plan.ResolveParams
params, err := s.languageLaunchParams(ctx, workspace.Path, manifest, plan.ResolveParams)
if err != nil {
return fail(err)
}
reference, err := json.Marshal(workspace.ContextRef)
if err != nil {
return fail(err)
Expand All @@ -99,3 +102,46 @@ func (s *Service) resolveBackendLaunch(ctx context.Context, pluginID, version st
}
return backendLaunch{plan: &decoded, directories: directories}, nil
}

// languageLaunchParams supplies native installation facts, without interpreting
// any tool's version or configuration. Core already validated requiresTools;
// the resolver uses the same user choices and discovery as Build and Run.
func (s *Service) languageLaunchParams(ctx context.Context, root string, manifest, params json.RawMessage) (json.RawMessage, error) {
if s.LanguageTool == nil {
return params, nil
}
var declaration struct {
RequiresTools []string `json:"requiresTools"`
}
if err := json.Unmarshal(manifest, &declaration); err != nil {
return nil, err
}
type installation struct {
Executable string `json:"executable"`
Home *string `json:"home,omitempty"`
Version string `json:"version,omitempty"`
}
tools := make(map[string]installation)
for _, tool := range declaration.RequiresTools {
selected, err := s.LanguageTool(ctx, root, tool)
if err != nil {
return nil, err
}
if selected != nil && !selected.ProjectCode {
tools[tool] = installation{selected.Executable, selected.Home, selected.Version}
}
}
if len(tools) == 0 {
return params, nil
}
var value map[string]json.RawMessage
if err := json.Unmarshal(params, &value); err != nil {
return nil, err
}
encoded, err := json.Marshal(tools)
if err != nil {
return nil, err
}
value["tools"] = encoded
return json.Marshal(value)
}
Loading
Loading