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 @@ -21,21 +21,36 @@

不要这样做:只看插件 ID 相同就从另一个源替换,或把新增的终端权限和可执行程序视作旧审批覆盖;也不能以卸载旧插件的方式完成自动升级。

### 手动与后台共享增量下载

市场可在同一索引提交发布独立的补丁索引。旧客户端仍能读取主索引,新客户端先确定完整包,再看是否有与原审批中的旧包摘要匹配的补丁。用 Zstandard(zstd)将旧的原始 ZIP 作为参考,在平台临时目录重建目标 ZIP,并验证旧包、补丁和目标包的 SHA-256。缺少可用基线或重建失败时走完整包,取消会结束下载并清理临时数据。下载完成的 ZIP 可在用户缓存保留,以便后续生成精确基线。

桌面进程共享 ZIP 缓存。写入方从检查容量、淘汰旧归档到发布新归档持有同一把原生锁,避免两个进程按同一份旧容量各自写入。获取锁最多等待一秒。锁不可用时记录失败,已下载的包仍可用于本次安装。

同一进程内使用同一个插件数据根的工作台共享更新轮次。手动安装和后台暂存在提交阶段争用同一个插件安装入口。后台将包与审批一起准备完成后再提交到待激活目录。下次启动 Lithe 进程时在后台验包,读取启动项目和插件语言清单前复核原版本和审批,只有仍然符合的包才进入已安装目录。例如后台暂存 2.0.0 后再禁用 1.0.0,重启时丢弃待更新包。运行中的会话读取的仍是旧目录。

不同进程共享插件数据根。激活、手动安装、后台暂存、禁用和卸载使用同一个原生文件锁。卸载先取消并等待进程内的安装任务,再加锁让 Core 读取安装快照。这样即使另一个窗口刚刚激活新版,卸载计划也会包含它。锁文件保留在可写的插件数据根,进程退出时由操作系统释放锁。

## 考虑过的备选方案

- 默认无条件自动更新:用户没有同意后台下载或插件执行代码更换,拒绝。
- 只比较版本号,不检查来源、权限和程序:会让第三方源或新版程序跨越信任边界,拒绝。
- 在主索引加入可选补丁字段会让当前旧 Core 拒绝未知字段。重新压缩已安装目录也无法保证得到与旧 ZIP 一致的字节。独立索引与有界原包缓存让两条下载路径共享既有校验。
- 立即重启已运行语言服务器和插件后端:影响正在编辑/调试的工作区,也可能丢失临时状态;选择不在后台更新时强制重启。

## 后果

- 收益:用户明确开启后,符合旧审批范围的更新无需反复点击,也不会重复下载安装相同已装版本;托管运行时按现有复用策略避免重复下载。
- 代价:旧版市场插件需要一次手动更新以记录来源;权限/程序变化必须手动审核,后台没有弹窗。后台安装后需重启才能确保旧插件会话切换到新版本。
- 代价:符合补丁格式的原始 ZIP 占用有限的用户缓存。无精确缓存基线时走全包,大包是否发布补丁由实际体积决定。
- 旧版市场插件需要一次手动更新以记录来源;权限/程序变化必须手动审核,后台没有弹窗。后台安装后需重启才能确保旧插件会话切换到新版本。
- 仍需原生跨平台验收:macOS 与 Windows 的窗口无头/真实插件更新、关窗取消与慢网络处理。未完成时功能矩阵保留 pending。

## 验证

- `./scripts/desktop/desktop-go.sh test ./internal/plugintrust ./internal/marketplace ./internal/workbench -run 'TestMayAutoUpdate|TestNewerOffered|TestAutoUpdateCandidates' -count=1`
- `./scripts/desktop/desktop-go.sh test ./internal/marketplace -run TestMarketplaceDelta` 覆盖补丁命中、损坏、取消和已安装包只读。
- `./scripts/desktop/desktop-go.sh test ./internal/marketplace -run 'TestArchiveCacheKeepsBudgetAcrossProcesses|TestMarketplaceDeltaTransport'` 覆盖两个进程同时写入时的归档条数和字节上限,以及缓存锁不可用时继续交付已校验的 ZIP。
- `TestUninstallWaitsForPackagePublicationInAnotherProcess` 用两个进程验证卸载等待发布,并在重试后删除新旧两个版本。测试同时核验发行包内容未改变。
- `node scripts/i18n/generate-i18n.mjs --check`、`./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh`、`./scripts/agent-notes/verify-agent-notes.sh`。
- 在合成插件市场的自动更新场景,观察安装文件、批准记录、运行中的旧进程和重启后新版本;拒绝案例不得提交任何安装。

Expand All @@ -47,3 +62,6 @@
- `desktop/internal/plugintrust/auto_update.go`
- `desktop/internal/plugininstall/plugininstall.go`
- `desktop/internal/marketplace/service.go`
- `desktop/internal/marketplace/delta.go`
- `desktop/internal/marketplace/archive_cache.go`
- `desktop/internal/plugininstall/pending.go`
10 changes: 7 additions & 3 deletions desktop/internal/core/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (o marketplaceOutcome) failure(command string) error {
type MarketplaceSource struct {
IndexURL string `json:"indexURL"`
RecommendationsURL string `json:"recommendationsURL"`
DeltasURL string `json:"deltasURL"`
Repository *string `json:"repository"`
DisplayName string `json:"displayName"`
// Revision is the GitHub repository and ref a GitHub-hosted index follows; nil for any
Expand All @@ -69,6 +70,7 @@ type MarketplacePinned struct {
Commit string `json:"commit"`
IndexURL string `json:"indexURL"`
RecommendationsURL string `json:"recommendationsURL"`
DeltasURL string `json:"deltasURL"`
}

// MarketplaceAvailability is Core's platform and capability decision.
Expand Down Expand Up @@ -110,9 +112,11 @@ type MarketplacePlugin struct {
LatestReleaseDate string `json:"latestReleaseDate"`
// MarketplaceIndexURL identifies the validated source of a fetched listing. It is
// native-only provenance and must never appear in the Core JSON contract.
MarketplaceIndexURL string `json:"-"`
Availability MarketplaceAvailability `json:"availability"`
Versions []MarketplaceVersion `json:"versions"`
MarketplaceIndexURL string `json:"-"`
// MarketplaceDeltasURL comes from the same index revision as Availability.
MarketplaceDeltasURL string `json:"-"`
Availability MarketplaceAvailability `json:"availability"`
Versions []MarketplaceVersion `json:"versions"`
}

// MarketplaceEntryRejection describes one rejected entry without hiding accepted entries.
Expand Down
76 changes: 76 additions & 0 deletions desktop/internal/core/marketplace_delta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package core

import (
"context"
"encoding/json"
)

// MarketplaceDeltaRequest binds a companion index to the approved local base and
// the full archive already selected from the main index.
type MarketplaceDeltaRequest struct {
Index json.RawMessage `json:"index"`
PluginID string `json:"pluginID"`
Platform string `json:"platform"`
Repository string `json:"repository"`
FromVersion string `json:"fromVersion"`
FromSHA256 string `json:"fromSHA256"`
ToVersion string `json:"toVersion"`
ToSHA256 string `json:"toSHA256"`
ArchiveURL string `json:"archiveURL"`
}

// MarketplaceDelta is the optional patch accepted by Core for the exact input.
type MarketplaceDelta struct {
PluginID string `json:"pluginID"`
Platform string `json:"platform"`
FromVersion string `json:"fromVersion"`
FromSHA256 string `json:"fromSHA256"`
ToVersion string `json:"toVersion"`
ToSHA256 string `json:"toSHA256"`
ToSize int64 `json:"toSize"`
Format string `json:"format"`
DownloadURL string `json:"downloadURL"`
SHA256 string `json:"sha256"`
Size int64 `json:"size"`
}

func (c *Client) SelectMarketplaceDelta(ctx context.Context, request MarketplaceDeltaRequest) (*MarketplaceDelta, error) {
const command = "plugins.marketplace.selectDelta"
var result struct {
marketplaceOutcome
Patch *MarketplaceDelta `json:"patch"`
}
if err := c.Execute(ctx, command, request, &result); err != nil {
return nil, err
}
if err := result.failure(command); err != nil {
return nil, err
}
return result.Patch, nil
}

// ApplyMarketplaceDelta writes a verified ZIP to an empty path owned by the caller.
// The caller removes the staging directory on failure or cancellation.
func (c *Client) ApplyMarketplaceDelta(ctx context.Context, patch MarketplaceDelta, basePath, patchPath, outputPath string) error {
const command = "plugins.marketplace.applyDelta"
var result struct {
marketplaceOutcome
Applied bool `json:"applied"`
}
request := struct {
Patch MarketplaceDelta `json:"patch"`
BasePath string `json:"basePath"`
PatchPath string `json:"patchPath"`
OutputPath string `json:"outputPath"`
}{patch, basePath, patchPath, outputPath}
if err := c.Execute(ctx, command, request, &result); err != nil {
return err
}
if err := result.failure(command); err != nil {
return err
}
if !result.Applied {
return newParseError(command, "marketplace returned no reconstructed archive", "")
}
return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package pluginruntime
// Package filelock coordinates access to writable files across processes.
package filelock

import (
"context"
Expand All @@ -8,10 +9,15 @@ import (
"time"
)

// acquire uses an OS lock, not a pid or timestamp lease: process death releases
var (
ErrInvalid = errors.New("lock path must name a regular file")
ErrBusy = errors.New("file lock is busy")
)

// Acquire uses an OS lock, not a pid or timestamp lease: process death releases
// ownership automatically. The file stays in place so another opener cannot
// lock a different inode while a previous waiter still holds the old one.
func acquire(ctx context.Context, name string) (unlock func() error, err error) {
func Acquire(ctx context.Context, name string) (unlock func() error, err error) {
defer func() {
if err != nil && unlock != nil {
err = errors.Join(err, unlock())
Expand Down Expand Up @@ -46,7 +52,7 @@ func acquire(ctx context.Context, name string) (unlock func() error, err error)
return nil, err
}
if !info.Mode().IsRegular() || !os.SameFile(info, opened) {
return nil, ErrArtifact
return nil, ErrInvalid
}
bounded, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
Expand All @@ -56,6 +62,9 @@ func acquire(ctx context.Context, name string) (unlock func() error, err error)
return nil, err
}
if locked {
if err := ctx.Err(); err != nil {
return nil, err
}
owned = true
return file.Close, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build darwin || linux

package pluginruntime
package filelock

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build windows

package pluginruntime
package filelock

import (
"errors"
Expand Down
Loading
Loading