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,50 @@
# Agent 笔记:保存工具链后刷新构建提供者

状态:已实现

## 先说结论

独立构建提供者(在后台常驻、执行插件构建或测试请求的进程)在启动时绑定工作环境。
通过宿主工具链启动的提供者会在项目 pin 或全局默认值保存后重新启动,独立窗口进程也能读取新值。
已提交的请求由旧进程处理完。初始化期间发现配置已变更会在原请求期限内重新启动。

## 问题

原来进程复用只比较插件设置,不比较宿主保存的工具链。即使设置页已显示新程序,
仍可能由旧进程执行下一次测试任务。

## 决策

启动器先执行 Core 的启动计划。在实际解析宿主工具前,工作台才读取来源文件的身份和内容,并把
这份版本记入会话复用键。项目 pin 只影响读取该项目来源的实例,
全局目录里的工具链配置影响共用该目录的实例。复用前和初始化完成后均核对文件版本。
无变化的保存不改文件。原子替换即使恢复相同的内容,也会因文件身份变化被识别。
直接运行打包程序或打包运行时的提供者沿用插件设置复用键,工具链文件读取失败时仍可运行。
与语言服务器共享的提供者继续走语言服务器的启动与复用机制。

旧进程在后台排空。退役进程和项目关闭时仍在清理的进程继续登记在工作台,直至完成原生销毁。
例如卸载插件时先阻止新启动,等登记中的相关进程退出,再删除包及状态目录。切换项目时
记录所属项目,避免关闭旧项目的任务影响新工作台的会话。

## 考虑过的备选方案

保存时主动停止所有会话会取消正在执行的任务。只递增进程内计数又无法覆盖使用相同配置目录
的独立窗口。测试、模型和依赖查询共用提供者,所以在宿主复用入口检查来源。

## 后果

依赖工具链的会话按请求读取既有的有界配置文件。读取版本本身只访问这两个文件。
来源更新时会重新解析并启动,即使改动的是同一个来源中的另一个工具。连续三次读取或启动
均被来源变动打断时,返回工具链冲突供用户重试。

## 验证

在 `desktop/internal/workbench/build_provider_restart_test.go` 使用 Core 的运行时启动计划验证项目 pin
保存和重复保存、另一个客户端写入、初始化期间保存、已有请求排空,以及卸载前等待退役进程销毁。
初始化重试用例检查两次启动的可执行文件路径。打包提供者用例覆盖项目和全局配置文件无法读取时的
复用和重启。共享提供者选用语言服务器的行为见 `desktop/internal/pluginbackend/pluginbackend_test.go`。
原生窗口的验证动作是先用旧工具执行,再保存新工具并重跑,核对两次实际启动的路径。

## 适用范围

Viva 两端共用的宿主进程管理。Core 的选择规则、插件契约和界面文案不变。
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Core 的第一次 toggle(增减一行选择)只把点击目标加入标记

- 没有批量标记和范围锚点时,Core 先把可见的、非根的当前行纳入,再切换点击行。已有批量继续只切换一行;隐藏旧行不因首次点击被意外加入。
- 文件树与 Git 分支列表有批量状态时只用批量标记呈现选中底色;取消选中的当前行不继续显示为已选。
- 文件树记录已观察的焦点,指针焦点先交给点击处理,键盘焦点仍更新当前行。窗口失焦和恢复不清除批量。
- 文件树记录已观察的焦点,指针焦点先交给点击处理,键盘焦点仍更新当前行。窗口失焦和恢复不清除批量。焦点清空时只记录焦点变化,行匹配要求焦点 ID 非空。目录列表刚加入的行在首次绘制前也使用空 ID,两者相等会把原选中项误移到新行。
- macOS Command 和 Windows Control 由 Viva 已有的主修饰键定义提供,不新增系统按键适配。

## 考虑过的备选方案
Expand All @@ -33,6 +33,7 @@ Core 的第一次 toggle(增减一行选择)只把点击目标加入标记

- Core 共享 row-selection fixture 修改后先红后绿,覆盖首次追加保留、点击当前行取消、隐藏标记、范围选择和根保护。
- `desktop/internal/workbench/filetree_test.go` 的真实驱动点击先复现旧选择丢失,修复后覆盖追加、取消、全部选中底色、普通点击清除及键盘焦点回归。
- `TestProjectTreeKeepsSelectionWhenFocusClearsBeforeNewRowsPaint` 控制焦点清空与目录行首次绘制的顺序,修复前选中新增行,修复后保留原选中行。`TestProjectTreeLocateGivesUpOnAFileThatIsGone` 覆盖删除活跃文件后定位的完整流程,`TestVisualProjectTreeMissingFile` 采集明暗主题的结果。
- `desktop/internal/workbench/branch_delete_test.go` 混合批量从普通点击开始,再用平台主修饰键追加,验证实际删除集合。
- 通过 `scripts/desktop/desktop-go.sh` 执行定向无头测试,`desktop/internal/workbench/branch_delete_visual_test.go` 生成中英文明暗主题的文件树多选截图。Windows 原生交互验证仍待目标平台执行。

Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/desktop-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ jobs:
if-no-files-found: error
retention-days: 14

- name: Capture project tree focus regression
id: project-tree-visual
if: ${{ !cancelled() && steps.rust.outcome == 'success' }}
timeout-minutes: 5
env:
LITHE_VISUAL_OUTPUT_DIR: ${{ github.workspace }}/.artifacts/project-tree-visual
run: |
./scripts/desktop/desktop-go.sh test -tags 'vivahead lithevisual' ./internal/workbench \
-run '^TestVisualProjectTreeMissingFile$' -count=1 -timeout=120s

- name: Upload project tree screenshots
if: ${{ !cancelled() && steps.project-tree-visual.outcome == 'success' }}
uses: actions/upload-artifact@v7
with:
name: project-tree-visual-${{ runner.os }}-${{ runner.arch }}
path: .artifacts/project-tree-visual/*.png
if-no-files-found: error
retention-days: 14

- name: Upload native verification reports
if: always()
uses: actions/upload-artifact@v7
Expand Down
48 changes: 37 additions & 11 deletions desktop/internal/buildprovider/buildprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ type Client struct {
done chan struct{}
doneOnce sync.Once
closeOnce sync.Once
dispose chan struct{}
disposed bool
// closeErr records why the session ended, for requests that arrive later.
closeErr error

Expand All @@ -160,19 +162,10 @@ type Client struct {
// provider. The caller must run this away from the window thread. Close owns
// the process and must be called when the workspace closes.
func Start(ctx context.Context, runner *pluginbackend.Runner, request pluginbackend.StartRequest) (*Client, error) {
if runner == nil {
return nil, errors.New("buildprovider: no runner")
}
if request.Role == "" {
request.Role = "build"
}
session, err := runner.Start(ctx, request)
client, err := Launch(ctx, runner, request)
if err != nil {
return nil, err
}
client := newClient(runner, session)
client.ctx, client.cancel = context.WithCancel(context.Background())
go client.serve()
if err := client.WaitReady(ctx); err != nil {
// The session may have started but never negotiated the protocol. Stop
// it instead of leaving an unusable process behind.
Expand All @@ -186,6 +179,25 @@ func Start(ctx context.Context, runner *pluginbackend.Runner, request pluginback
return client, nil
}

// Launch gives the caller ownership immediately. The caller must wait for
// readiness and dispose the session even if initialization fails.
func Launch(ctx context.Context, runner *pluginbackend.Runner, request pluginbackend.StartRequest) (*Client, error) {
if runner == nil {
return nil, errors.New("buildprovider: no runner")
}
if request.Role == "" {
request.Role = "build"
}
session, err := runner.Start(ctx, request)
if err != nil {
return nil, err
}
client := newClient(runner, session)
client.ctx, client.cancel = context.WithCancel(context.Background())
go client.serve()
return client, nil
}

// Attach returns a client over a session another owner started, such as a
// language server of the same plugin. The owner must call Observe with every
// event that session produces. Close detaches this client; it never stops the
Expand All @@ -211,6 +223,7 @@ func newClient(runner *pluginbackend.Runner, session pluginbackend.Session) *Cli
session: session,
ready: make(chan struct{}),
done: make(chan struct{}),
dispose: make(chan struct{}, 1),
pending: make(map[string]*Operation),
}
}
Expand Down Expand Up @@ -361,6 +374,19 @@ func (c *Client) Close(ctx context.Context) error {
if c == nil {
return nil
}
if !c.shared {
// Retirement, project close and uninstall can all request the same stop.
// Claim native disposal before waking the requests that retirement awaits.
select {
case c.dispose <- struct{}{}:
defer func() { <-c.dispose }()
case <-ctx.Done():
return ctx.Err()
}
if c.disposed {
return nil
}
}
c.closeOnce.Do(func() {
c.mu.Lock()
c.closed = true
Expand All @@ -380,7 +406,7 @@ func (c *Client) Close(ctx context.Context) error {
}
var err error
if c.runner != nil {
err = c.runner.Stop(ctx, c.session)
c.disposed, err = c.runner.StopAndDispose(ctx, c.session)
}
c.end(ErrClosed)
return err
Expand Down
69 changes: 44 additions & 25 deletions desktop/internal/configfile/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"errors"
"io"
"os"
Expand All @@ -32,19 +33,37 @@ type Snapshot struct {
name string
maximum int64
rootInfo os.FileInfo
fileInfo os.FileInfo
content []byte
exists bool
}

// Path is a display-only source path. I/O always uses os.Root-relative methods.
func (s Snapshot) Path() string { return filepath.Join(s.root, s.name) }
func (s Snapshot) Exists() bool { return s.exists }
func (s Snapshot) Exists() bool { return s.fileInfo != nil }
func (s Snapshot) Bytes() []byte { return append([]byte(nil), s.content...) }

// Revision identifies the bounded bytes and the native file that supplied them.
// Atomic replacements can restore the same bytes while a consumer is starting.
type Revision struct {
file os.FileInfo
hash [sha256.Size]byte
}

func (s Snapshot) Revision() Revision {
return Revision{file: s.fileInfo, hash: sha256.Sum256(s.content)}
}

func (r Revision) Equal(other Revision) bool {
if r.file == nil || other.file == nil {
return r.file == nil && other.file == nil
}
return r.hash == other.hash && r.file.ModTime().Equal(other.file.ModTime()) && os.SameFile(r.file, other.file)
}

// Text rejects undecodable bytes before JSON string transport can replace them.
// A nil string means a missing file; an empty existing file remains explicit.
func (s Snapshot) Text() (*string, error) {
if !s.exists {
if !s.Exists() {
return nil, nil
}
if !utf8.Valid(s.content) {
Expand Down Expand Up @@ -76,66 +95,66 @@ func Capture(ctx context.Context, rootDirectory, relativeName string, maximumByt
if err != nil {
return snapshot, err
}
snapshot.content, snapshot.exists, err = read(ctx, root, snapshot.name, maximumBytes, false)
snapshot.content, snapshot.fileInfo, err = read(ctx, root, snapshot.name, maximumBytes)
return snapshot, err
}

func read(ctx context.Context, root *os.Root, name string, maximum int64, writable bool) (data []byte, exists bool, err error) {
func read(ctx context.Context, root *os.Root, name string, maximum int64) (data []byte, info os.FileInfo, err error) {
if err := ctx.Err(); err != nil {
return nil, false, err
return nil, nil, err
}
info, err := root.Lstat(name)
info, err = root.Lstat(name)
// log-exempt: 预期的缺失:缺失的配置文件是未覆盖的空来源。
if errors.Is(err, os.ErrNotExist) {
return nil, false, nil
return nil, nil, nil
}
if err != nil {
return nil, false, err
return nil, nil, err
}
if !info.Mode().IsRegular() {
return nil, false, ErrTarget
return nil, nil, ErrTarget
}
file, err := openSource(root, name, writable)
file, err := openSource(root, name, false)
if err != nil {
return nil, false, err
return nil, nil, err
}
defer func() { err = errors.Join(err, file.Close()) }()
openedInfo, err := file.Stat()
if err != nil {
return nil, false, err
return nil, nil, err
}
if !os.SameFile(info, openedInfo) {
return nil, false, ErrConflict
return nil, nil, ErrConflict
}
info = openedInfo
if !info.Mode().IsRegular() {
return nil, false, ErrTarget
return nil, nil, ErrTarget
}
if info.Size() > maximum {
return nil, true, ErrTooLarge
return nil, nil, ErrTooLarge
}
data, err = io.ReadAll(io.LimitReader(file, maximum+1))
if err != nil {
return nil, true, err
return nil, nil, err
}
if int64(len(data)) > maximum {
return nil, true, ErrTooLarge
return nil, nil, ErrTooLarge
}
if err := ctx.Err(); err != nil {
return nil, true, err
return nil, nil, err
}
return data, true, nil
return data, info, nil
}

func (s Snapshot) check(ctx context.Context, root *os.Root) error {
data, exists, err := read(ctx, root, s.name, s.maximum, false)
data, info, err := read(ctx, root, s.name, s.maximum)
if errors.Is(err, ErrTooLarge) {
return ErrConflict
}
if err != nil {
return err
}
if exists != s.exists || !bytes.Equal(data, s.content) {
if (info != nil) != s.Exists() || !bytes.Equal(data, s.content) {
return ErrConflict
}
return nil
Expand Down Expand Up @@ -255,16 +274,16 @@ func (s Snapshot) publish(ctx context.Context, content []byte, remove bool, chec
return false, err
}
}
if remove && !s.exists {
if remove && !s.Exists() {
return false, nil
}
if !remove && s.exists && bytes.Equal(content, s.content) {
if !remove && s.Exists() && bytes.Equal(content, s.content) {
return false, nil
}

// Prove native write access, and retain the original metadata by handle.
var source *os.File
if s.exists {
if s.Exists() {
source, err = openSource(root, s.name, true)
if err != nil {
return false, err
Expand Down
13 changes: 13 additions & 0 deletions desktop/internal/pluginbackend/pluginbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ type StartRequest struct {
// ManagedTools are per-launch installations verified by the managed runtime resolver.
// They are never stored as global defaults or used by a different plugin/session.
ManagedTools map[string]ToolExecutable
// BeforeResolveTool lets a launch owner capture the sources that determine
// the selected installation. It runs only before host toolchain resolution.
BeforeResolveTool func(context.Context) error
// Preparation starts a private resolver without workspace services or the
// cross-plugin function relay. Only direct host invocations may reach it.
Preparation bool
Expand Down Expand Up @@ -382,6 +385,11 @@ func (r *Runner) Start(ctx context.Context, request StartRequest) (Session, erro
if r.toolEntries == nil {
return Session{}, fmt.Errorf("%w: this host cannot instantiate tool entries", ErrRequest)
}
if executable == "" && request.BeforeResolveTool != nil {
if err := request.BeforeResolveTool(ctx); err != nil {
return Session{}, err
}
}
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,
Expand All @@ -398,6 +406,11 @@ func (r *Runner) Start(ctx context.Context, request StartRequest) (Session, erro
if r.tools == nil {
return Session{}, fmt.Errorf("%w: this host cannot resolve tool launches", ErrRequest)
}
if request.BeforeResolveTool != nil {
if err := request.BeforeResolveTool(ctx); err != nil {
return Session{}, err
}
}
var err error
tool, err = r.tools(ctx, request.PluginID, request.WorkingDirectory, plan.Launch.Tool)
if err != nil {
Expand Down
Loading
Loading