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
Expand Up @@ -2,6 +2,8 @@

状态:已实现

> 2026-11-18 更新:用户层改存 `.lithe/run/configurations.yaml`、`local.yaml` 和按插件拆分的 `.lithe/run/plugin/<插件 ID>.toml`、`<插件 ID>.local.toml`;Core 不再写这些文件,编辑命令返回文件改动,由宿主按哈希检查后应用。内置的脚本与程序运行不经过生成层。决策见[脚本与程序运行由 Core 内置,运行配置存 YAML 和 TOML](2026-11-18-builtin-process-runs-and-run-file-formats.md)。
>
> 2026-11-16 更新:Core 删除了内置的运行发现探测器、项目内容指纹和工具链需求([Lithe-core-rust #77](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/77),#1856 第 4 项)。生成层现在只由构建插件声明的 `buildSystems[].run` 组成。原来的“跨会话缓存语言服务入口”和“识别前也能选 JDK”两节随之删除;分类与重名规则从已归档的 `.agents/notes/archived/feature/2026-09-20-run-configuration-discovery-quality.md` 并入本文,指纹相关的旧做法见已归档的 `.agents/notes/archived/bug-fix/2026-09-22-run-panel-fingerprint-loading.md`。

## 先说结论
Expand Down Expand Up @@ -63,7 +65,7 @@ Core 按规范化后的工作区路径保存最近一次 `runConfig.generate`

### 发布用户文档的保护

宿主先构造两层文档,用 Core 预检,然后比较所有输入(包括未改写的一层)和偏好状态,才使用带源版本检查的原子替换。写入、回读、取消或冲突失败时,只回滚自身仍未被改动的输出;不覆盖用户或另一进程的新数据。生成任务的租约(可撤销的写入资格)在取消、工作区切换、模型变化或后续生成时失效。
Core 的编辑命令只返回文件改动(路径、改动前的哈希和新内容),宿主在 `applyRunConfigChanges` 里确认每个文件仍是 Core 读到的字节后才一次性应用;任何一个不符就整体放弃并报冲突。写入或回读失败时,只回滚自身仍未被改动的输出,不覆盖用户或另一进程的新数据。生成任务的租约(可撤销的写入资格)在取消、工作区切换、模型变化或后续生成时失效。

生成层不是持久配置,不参与这套发布。

Expand Down Expand Up @@ -97,9 +99,11 @@ Core 按规范化后的工作区路径保存最近一次 `runConfig.generate`

这是[构建系统 Provider 提案](../../proposed/architecture/2026-10-17-build-system-provider-api.md)原来的迁移计划。没有采用:Core 仍然维护生态知识,只是换了格式;插件已经能声明同样的内容。

### 没有生成层时 `resolve` 按空生成层处理
### 没有生成层时 `resolve` 按空生成层处理,并照常报孤立覆盖

对只有稀疏覆盖的文档,这可以少一次“未生成”的判断。但团队层里覆盖自动条目的配置会全部变成“孤立覆盖”,用户会看到一批误导性的诊断。保留 `missing` 并让宿主自动生成更直接。
对只有稀疏覆盖的文档,这可以少一次“未生成”的判断。但团队层里覆盖自动条目的配置会全部变成“孤立覆盖”,用户会看到一批误导性的诊断。

2026-11-18 起采用了折中做法:没有生成层时,`resolve` 返回已保存的配置(一个都没有时返回空列表),让内置的脚本与程序运行不必等构建模型;这时不报孤立覆盖。生成仍由 `inspect` 的 `requiresGeneration` 触发。

## 后果

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Agent 笔记:脚本与程序运行由 Core 内置,运行配置存 YAML(内置)和 TOML(插件)

状态:已实现

## 先说结论

Shell 脚本、PowerShell 脚本、cmd 批处理(只在 Windows)和可执行程序四种运行由 Core 自己提供,叫“内置运行”(provider 为 `process.*`)。它们的表单、校验和启动计划都在 Core,宿主只负责画表单、写文件和起进程,不经过任何构建插件,所以 Java 等插件缺失或出错时这四种运行照常可用。

用户保存的运行配置换成两种格式:内置运行存 `.lithe/run/configurations.yaml`(团队共享)和 `.lithe/run/local.yaml`(本机);插件提供的运行按插件拆成 `.lithe/run/plugin/<插件 ID>.toml` 和 `<插件 ID>.local.toml`,文件头记录插件的作者、仓库和版本要求。Core 只计算文件改动,不写文件,宿主按哈希检查后一次性应用。

## 问题

- 运行平台自带的脚本和程序本来就不需要生态知识,却要装插件才能出现在 Run 列表里;插件坏了,连最基本的“跑一下脚本”都做不到(Issue #2100)。
- 旧的 `.lithe/run/configurations.json` 把所有提供者的配置混在一份 JSON 里。用户想把运行配置提交进仓库时,JSON 不便手改、合并冲突难读;插件的配置也看不出依赖哪个插件、哪个版本。
- 同名插件可能来自不同作者。配置只记插件 ID 时,换了来源的插件会悄悄接手旧配置。
- 以后表单上一定会多出按钮和参数框。如果字段写死在宿主里,每加一个字段都要同时改 Core、macOS 和 Windows。

## 决策

### 四种内置运行属于 Core

`process.shell`、`process.powershell`、`process.batch`、`process.executable` 的运行类型列表(`runConfig.configurationTypes`)、表单(`runConfig.editorForm`)、保存校验(`runConfig.saveConfiguration`)和启动计划(`runConfig.createLaunchPlan`)都在 Core 的 `execution/src/configuration/process.rs` 与 `form.rs`。默认解释器由 Core 决定:Shell 取脚本首行的 `#!`,没有时用 `/bin/sh`;PowerShell 用 `pwsh`(Windows 上 `powershell`)并默认带 `-ExecutionPolicy Bypass`,用户可以取消。

Core 只在 Windows 上列出 `process.batch`。其他平台上读到已有的 batch 条目时丢弃,并给出 `unsupportedPlatform` 诊断,界面上不出现 cmd。

宿主(`desktop/internal/workbench/runconfig_process.go`)拿到启动计划后只做三件事:在 PATH 里找可执行文件,把“脚本文本”写到系统临时目录里的 `lithe-run-script-*` 目录、进程退出后删除,然后用 `buildprocess` 起进程,输出进 Run 窗口。宿主不判断脚本类型,也不拼解释器参数。

只内置平台本身就有的东西。Python、Node、Make 这类需要识别生态或工具链的运行,交给插件声明(见[运行配置的生成层只来自插件声明](2026-10-29-run-configuration-detection-in-memory.md))。

### 内置运行不依赖插件和构建模型

列出、新建、编辑和启动内置运行时,不需要构建模型或生成层,也不需要插件宿主。还没加载构建模型的项目,`resolve` 照样返回内置运行;插件宿主不可用时,标题栏和构建窗口仍然列出并运行它们。插件配置文件写坏时,只把那一份文件标为“未读取”(诊断带 `path`,标题栏下拉列出该文件,点一下在编辑器中打开),其余配置照常读取。

### 两种文件格式各管一层

| 文件 | 谁写 | 内容 |
| --- | --- | --- |
| `.lithe/run/configurations.yaml` | 用户勾选“存为项目文件” | 团队共享的内置配置 |
| `.lithe/run/local.yaml` | 默认 | 本机的内置配置、选项和工具偏好 |
| `.lithe/run/plugin/<插件 ID>.toml` | 插件运行存为项目文件 | 团队共享的插件配置 |
| `.lithe/run/plugin/<插件 ID>.local.toml` | 插件运行默认 | 本机的插件配置 |

插件 TOML 的 `[plugin]` 头记录 `id`、`name`、`author`、`repository`、`version`、`versionRequirement`(默认“`>=` 保存时的版本”)、`pluginAPIVersion` 和 `capabilities`。作者和仓库取自插件清单新增的可选字段 `repository`(`https://github.com/<作者>/<仓库>`)。插件没装、仓库不一致或版本不满足时,这些配置标为不可用并说明原因,不删除也不改写。

旧 JSON 只在对应的 YAML 不存在时读取。第一次编辑某一层时,Core 返回“写 YAML、删 JSON”两项改动。

### Core 计算改动,宿主应用改动

所有编辑命令都返回 `changes: [{path, baseSha256, content}]`,Core 自己不写文件。宿主的 `applyRunConfigChanges`(`desktop/internal/workbench/runconfig_changes.go`)只接受 `.lithe/` 下的路径。它先确认每个文件仍是 Core 计算时的字节,有一个不符就整体放弃并报冲突;写入先于删除,所以新 YAML 落盘后才删旧 JSON。中途失败时,它只还原自己改过、并且之后没被别人改动的文件。

### 表单字段由 Core 描述,宿主按类型画

`runConfig.editorForm` 返回字段列表(类型、是否必填、位置、自动值、可选项、`visibleWhen` 条件、说明)和当前值,`formVersion` 为 1。宿主(`runconfig_form.go`、`runconfig_form_view.go`)只画同时满足两个条件的字段:类型是它认识的,`shared/i18n` 里有这个字段 ID 的标题。其余字段跳过不画,值原样回传。Core 返回的 `formVersion` 比宿主新时,宿主不打开表单,提示升级。

### 从界面按下运行时打开 Run 窗口

从编辑对话框的“运行”、构建窗口的配置列表(按钮或双击)和 Run 窗口的再次运行启动时,都走 `runConfiguration`(`runconfig.go`)。它启动后打开 Run 窗口,并选中这次运行的控制台;构建插件提供的运行从列表启动时也一样。经 IDE API(`startRunConfiguration`)启动的运行不改变窗口布局,因为调用方不一定是用户。

### 正确做法

- 给 Shell 表单加一个参数框:在 Core 的 `form.rs` 描述字段、在 `process.rs` 解释它的值,再在 `shared/i18n/` 加标题词条。宿主已认识这种类型时,不用改任何宿主代码。
- 新增一种全新的字段类型:先在宿主实现它的绘制,再让 Core 开始返回它;旧宿主跳过新字段,配置照样能保存。
- 插件运行需要新的元数据:加进插件 TOML 的 `[plugin]` 头,由 Core 读写,宿主不解析 TOML。

### 不要这样做

- 不要在宿主里按扩展名判断脚本类型、补 `-ExecutionPolicy` 或拼 `cmd /c`。这些规则只在 Core,宿主重复一份就会和 Windows 端分叉。
- 不要在宿主或 Core 里直接写 `.lithe/run/` 下的文件,绕过 `changes` 和哈希检查会覆盖别人刚做的修改。
- 不要让内置运行等构建模型加载完、或等插件宿主就绪再出现,那正是这次要去掉的依赖。
- 不要把脚本文本写进项目目录或 app bundle。它是一次性的运行产物,只放系统临时目录。

## 考虑过的备选方案

### 把脚本和程序运行做成一个官方插件

和其他运行走同一条路,Core 不多一类提供者。没有采用:用户要求插件全坏时这几种运行也能用,放进插件就又依赖插件宿主、插件安装和插件版本。

### 全部配置都用 YAML,或都用 TOML

一种格式更简单。没有采用:内置配置只有通用字段,YAML 适合手写和代码评审;插件配置要带一个“依赖哪个插件、哪个版本”的头,TOML 的 `[plugin]` 表把元数据和配置分得清楚。按插件拆文件后,卸载插件或插件出错只影响一份文件。

### 插件配置只记插件 ID

字段少,也不用改插件清单。没有采用:插件 ID 防不了不同作者用同一个名字。作者和仓库让 Core 能发现“已安装的插件不是保存配置时那一个”,并把配置标为不可用,而不是交给另一个插件运行。

### 表单字段写死在宿主

宿主可以完全控制布局。没有采用:每加一个字段都要同时改 Core 和两个桌面端,还要在宿主里重复 Core 的校验规则;以后 Windows 端还得再写一遍。

### 迁移时立即把所有旧 JSON 转成 YAML

一次转完,代码里不用再读 JSON。没有采用:打开项目就改写用户文件,会在版本控制里产生用户没做过的改动。现在只在用户真正编辑某一层时才转换。代价是旧客户端读不到被转换的那一层。

## 后果

- 收益:不装插件也能运行脚本和程序;运行配置可以按团队和本机分开提交;插件配置能说明自己依赖哪个插件;以后加字段时,通常只需改 Core 和词条。
- 代价:多了一种文件格式和一套插件元数据要维护;脚本文本运行要在临时目录写文件,进程崩溃时留下的目录由系统清理;旧 macOS 客户端读不到 Viva 已转换的那一层配置。
- Windows 上裸写的 `bash` 可能解析到 WSL 的启动器,需要 Git Bash 时要在解释器路径里填完整路径。批处理的 `commandLine` 只按 cmd 的规则加引号,不转义 `%`。
- 需要重新评估:Windows 或 Linux 实机验收发现的解释器或引号问题;插件需要在 TOML 里放自定义字段时;`formVersion` 需要升到 2 时。

## 验证

- `scripts/desktop/desktop-go.sh test ./internal/core/... ./internal/buildprocess/... ./internal/workbench/...`:`runconfig_process_test.go` 用系统 shell 真实运行脚本,并断言 Run 窗口打开、输出进入控制台;`runconfig_browser_test.go`、`title_run_test.go`、`runconfig_edit_test.go` 覆盖列表、标题栏和保存。
- `scripts/desktop/desktop-go.sh test -tags 'vivahead lithevisual' ./internal/workbench -run '^TestVisualBuiltInProcessRuns$'`:设置 `LITHE_VISUAL_OUTPUT_DIR` 后生成 `docs/assets/builtin-process-runs/` 的截图。
- Core 仓库的 `cargo test -p lithe-execution -p lithe-core`:覆盖表单、校验、启动计划、YAML/TOML 读写、旧 JSON 和插件可用性。
- `./scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:确认运行不写发行目录。

## 适用范围

- `desktop/internal/workbench/runconfig_process.go`
- `desktop/internal/workbench/runconfig_changes.go`
- `desktop/internal/workbench/runconfig_form.go`
- `desktop/internal/workbench/runconfig_form_view.go`
- `desktop/internal/workbench/runconfig.go`
- `desktop/internal/core/run_process.go`
- `desktop/internal/buildprocess/`
- Core 仓库的内置运行与文件格式:[process.rs](https://github.com/Lithe-IDEA/Lithe-core-rust/blob/main/rust/crates/execution/src/configuration/process.rs)、[storage.rs](https://github.com/Lithe-IDEA/Lithe-core-rust/blob/main/rust/crates/execution/src/configuration/storage.rs)、[plugin_document.rs](https://github.com/Lithe-IDEA/Lithe-core-rust/blob/main/rust/crates/execution/src/configuration/plugin_document.rs)
46 changes: 46 additions & 0 deletions desktop/internal/buildprocess/batch_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,49 @@ func TestBatchFileUnderASpacedDirectoryReceivesItsArguments(t *testing.T) {
t.Fatal("an argument ran a command")
}
}

// A command line Core planned reaches the process unchanged, Executable included, instead of
// one quoted from the arguments: cmd.exe runs a batch script the way Core wrote its line.
func TestCommandLineReachesTheProcessUnchanged(t *testing.T) {
directory := filepath.Join(t.TempDir(), "spaced dir")
if err := os.MkdirAll(directory, 0o755); err != nil {
t.Fatal(err)
}
script := filepath.Join(directory, "run me.cmd")
body := "@echo off\r\n\"" + os.Args[0] + "\" \"-test.run=^TestBatchArgumentsHelper$\" -- %*\r\nexit /b %ERRORLEVEL%\r\n"
if err := os.WriteFile(script, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cmd := filepath.Join(os.Getenv("SystemRoot"), "System32", "cmd.exe")
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
var output strings.Builder
var mu sync.Mutex
process, err := Start(ctx, Spec{
Executable: cmd,
WorkingDirectory: directory,
Environment: map[string]string{batchArgumentsMode: "1"},
CommandLine: `cmd /d /s /c ""` + script + `" "a b" c"`,
}, func(_ Stream, text string) {
mu.Lock()
output.WriteString(text)
mu.Unlock()
})
if err != nil {
t.Fatal(err)
}
waitErr := process.Wait()
mu.Lock()
text := output.String()
mu.Unlock()
if waitErr != nil {
t.Fatalf("script failed: %v\n%s", waitErr, text)
}
var got []string
if err := json.Unmarshal([]byte(strings.TrimSpace(text)), &got); err != nil {
t.Fatalf("output is not the helper's JSON: %v\n%s", err, text)
}
if want := []string{"a b", "c"}; !slices.Equal(got, want) {
t.Fatalf("arguments = %q, want %q", got, want)
}
}
7 changes: 6 additions & 1 deletion desktop/internal/buildprocess/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type Spec struct {
// adapter) can write frames after the process starts while cancellation
// still ends the whole tree.
Stdin io.Reader
// CommandLine, when set, is the exact Windows command line the process gets,
// Executable included, instead of one quoted from Arguments. Core plans it for
// a batch script run by cmd.exe, whose quoting rules differ from a program's.
// Start refuses it on other platforms, which have no command line.
CommandLine string
}

// Process is one running host-owned process. Wait must be called once to
Expand Down Expand Up @@ -108,7 +113,7 @@ func Start(ctx context.Context, spec Spec, observe func(Stream, string)) (*Proce
command.Stdout = stdoutWrite
command.Stderr = stderrWrite
platform := &processPlatform{}
err = platform.configure(command)
err = platform.configure(command, spec.CommandLine)
if err == nil {
err = command.Start()
}
Expand Down
12 changes: 10 additions & 2 deletions desktop/internal/buildprocess/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package buildprocess

import (
"errors"
"os"
"os/exec"
"sync"
Expand All @@ -20,12 +21,19 @@ type processPlatform struct {
}

// configure puts the process in its own group so Stop can end the whole build
// tree, including wrappers that start child compilers.
func (*processPlatform) configure(command *exec.Cmd) error {
// tree, including wrappers that start child compilers. A process here gets its
// arguments as a vector, so a Windows command line is refused.
func (*processPlatform) configure(command *exec.Cmd, commandLine string) error {
if commandLine != "" {
return errCommandLineUnsupported
}
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return nil
}

// errCommandLineUnsupported refuses a Spec.CommandLine outside Windows.
var errCommandLineUnsupported = errors.New("buildprocess: a command line is passed only on Windows")

func (p *processPlatform) adopt(process *os.Process) error {
if process == nil {
return nil
Expand Down
17 changes: 17 additions & 0 deletions desktop/internal/buildprocess/process_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package buildprocess

import (
"context"
"errors"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -110,3 +111,19 @@ func TestProcessKillEndsATreeInItsGrace(t *testing.T) {
t.Fatal("the killed program reported success")
}
}

// A Windows command line is refused here, where a process gets its arguments as a vector, so
// a batch plan never starts with arguments it did not mean.
func TestStartRefusesACommandLineOutsideWindows(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
process, err := Start(ctx, Spec{Executable: "/bin/sh", CommandLine: `cmd /d /s /c "x.cmd"`}, func(Stream, string) {})
if err == nil {
process.Kill()
_ = process.Wait()
t.Fatal("Start ran a command line outside Windows")
}
if !errors.Is(err, errCommandLineUnsupported) {
t.Fatalf("err = %v, want errCommandLineUnsupported", err)
}
}
8 changes: 7 additions & 1 deletion desktop/internal/buildprocess/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ type processPlatform struct {
active bool
}

func (p *processPlatform) configure(command *exec.Cmd) error {
// configure creates the process suspended in a group of its own. commandLine, when set, is
// passed as the process's whole command line unchanged (Spec.CommandLine).
func (p *processPlatform) configure(command *exec.Cmd, commandLine string) error {
command.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: windows.CREATE_NO_WINDOW | windows.CREATE_SUSPENDED | windows.CREATE_NEW_PROCESS_GROUP,
}
if commandLine != "" {
command.SysProcAttr.CmdLine = commandLine
return nil
}
// A program that was not found keeps its lookup error for Start to report.
if command.Err != nil || !isBatchFile(command.Path) {
return nil
Expand Down
Loading
Loading