pi-web-ui 0.104.2 → 0.104.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/dist/server/agent-service.js +32 -14
- package/dist/server/attachment-store.js +10 -1
- package/dist/server/dsh/dsh-agent-service.js +12 -3
- package/dist/server/goal-review-gate.js +2 -0
- package/dist/server/mcp-agent-tool.js +4 -0
- package/dist/server/mcp-hot-reload.js +6 -1
- package/dist/server/mcp-manager.js +9 -0
- package/dist/server/plugin-facilities.js +9 -0
- package/dist/server/plugins.js +48 -0
- package/dist/server/sandbox-service.js +149 -9
- package/dist/server/sandbox-tool.js +52 -6
- package/package.json +1 -1
- package/plugin-sdk/index.d.ts +13 -0
- package/web/dist/assets/{TerminalPanel-LQLL17DU.js → TerminalPanel-VjlOOAig.js} +1 -1
- package/web/dist/assets/index-5SITmQd_.js +680 -0
- package/web/dist/assets/index-D9wTa3O-.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-BEuJeIJv.js +0 -680
- package/web/dist/assets/index-RqIdFOsh.css +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,36 @@
|
|
|
12
12
|
|
|
13
13
|
暂无未发布内容。
|
|
14
14
|
|
|
15
|
+
## [0.104.3] — 2026-10-12
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- **面包屑与帮助提示统一使用 SVG 图标并支持无延迟 TipButton** —— 全站统一采用基于 SVG 的提示图标与面包屑组件,新增支持零等待延迟的 `TipButton`,提升界面的视觉一致性与响应交互体验。
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
|
|
23
|
+
- **沙盒硬围栏强化与启发式命令拦截** —— 完善 bash 沙盒围栏规则,深度拦截越界 `cd`、重定向、系统特权命令以及越界写删行为;补齐 `[path#TAG]` / `[path]` hashline 段头解析,避免 patch 围栏空转;`sandbox` 工具的 `action=destroy` 接入强制审批保护,销毁后安全回退当前目录;完善沙盒重置容错与写回删除同步机制。
|
|
24
|
+
- **IM 通道插件群聊触发收敛与稳定性修复**:
|
|
25
|
+
- **Telegram / Slack**:收敛群聊触发面,Telegram 仅响应针对机器人自身的精确 @mention 实体,Slack 频道内仅响应包含 `<@botId>` 的明确提及,彻底避免被动误触发与刷屏。
|
|
26
|
+
- **企业微信 (WeCom)**:AI 回复切块逻辑严格按 UTF-8 字节数(4096 字节限制)处理,消除纯中文超限被拒;回复链路补齐 `errcode` 校验,防止消息静默丢失。
|
|
27
|
+
- **飞书 (Feishu)**:移除空轮询假降级,WS 断开时如实报错并反馈真实原因,杜绝面板虚假“已连接”。
|
|
28
|
+
- **QQ 开放平台**:同一消息的多段分块回复补齐 `msg_seq` 自增序号,修复长回答分块被平台丢弃;新增 Access Token 惰性提前刷新机制,解决 Token 过期后 401 永久重试问题。
|
|
29
|
+
- **Matrix**:持久化 sync 游标到 `host.storage`,重启或热重载后不再重放历史消息;主动过滤 `m.notice` 类型机器人消息,避免死循环互聊。
|
|
30
|
+
- **iMessage**:非 macOS 环境不再返回假成功,如实反馈 unsupported 状态。
|
|
31
|
+
- **MCP 服务重名保护与热重载口径对齐** —— `install` 操作对同名已有服务默认拒绝,提供显式 `spec.overwrite` 选项;放宽热重载 legacy 回退逻辑,确保与服务端启动阶段加载行为完全一致。
|
|
32
|
+
- **项目列表多端派生视图缓存隔离** —— 静态 `projectsCache` 仅用于派生视图广播,不再跨客户端写回静态内存,杜绝客户端工作区视图相互污染;引入 TTL 后台续期机制,兼顾磁盘发现能力与切换性能。
|
|
33
|
+
- **目标审查写闸门补拦 CodeMode** —— 目标审查回路严格禁止写操作,写工具拦截名单补齐 `codemode`,防止通过内嵌 QuickJS 沙箱绕过只读限制;附件存储遇到 EPERM 锁定故障时安全回退 Data URL,防止写入失效的死链记录。
|
|
34
|
+
- **插件宿主控制面兼容 API 补齐** —— 扩展并兼容插件宿主常用控制面调用接口,提升外部插件运行与交互兼容性。
|
|
35
|
+
|
|
36
|
+
<!-- auto-i18n:start -->
|
|
37
|
+
|
|
38
|
+
### i18n
|
|
39
|
+
|
|
40
|
+
- 前端中文变更(1):`sandboxDiffDeletedNote`
|
|
41
|
+
- 前端英文变更(1):`sandboxDiffDeletedNote`
|
|
42
|
+
|
|
43
|
+
<!-- auto-i18n:end -->
|
|
44
|
+
|
|
15
45
|
## [0.104.2] — 2026-10-11
|
|
16
46
|
|
|
17
47
|
### Added
|
|
@@ -1709,6 +1739,7 @@ when?, children?}`,也收 `topbar` / `settings` 这类简写别名);宿主
|
|
|
1709
1739
|
[0.64.4]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.64.4
|
|
1710
1740
|
[0.64.3]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.64.3
|
|
1711
1741
|
[0.64.2]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.64.2
|
|
1742
|
+
[0.104.3]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.104.3
|
|
1712
1743
|
[0.104.2]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.104.2
|
|
1713
1744
|
[0.64.1]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.64.1
|
|
1714
1745
|
[0.64.0]: https://github.com/xing-shuyin/pi-web-ui/releases/tag/v0.64.0
|
|
@@ -10839,18 +10839,30 @@ export class ClientSession {
|
|
|
10839
10839
|
const cached = ClientSession.projectsCache;
|
|
10840
10840
|
// TTL 命中(或已建立过项目缓存):直接把当前 cwd 合并重排并续期,
|
|
10841
10841
|
// 避免切换工作区时因为刚好跨过 60s TTL 而触发全盘 30 个会话子目录重扫。
|
|
10842
|
+
// 已建立过缓存:emit 本客户端的派生视图即可,**绝不把派生结果写回静态缓存**
|
|
10843
|
+
// ——withCurrentCwd 注入了本客户端的 cwd/lastUsed/removedKeys 过滤与沙盒
|
|
10844
|
+
// 元数据,写回会把 A 客户端的视图固化给 B(静态缓存跨客户端共享);
|
|
10845
|
+
// 底层超过 TTL 后后台重扫一次,磁盘上新工作区不会被永久挡在列表外。
|
|
10842
10846
|
if (cached) {
|
|
10843
|
-
|
|
10844
|
-
|
|
10845
|
-
|
|
10847
|
+
this.emit({ type: "projects", projects: this.withCurrentCwd(cached.projects, now) });
|
|
10848
|
+
if (now - cached.at >= ClientSession.PROJECTS_CACHE_TTL)
|
|
10849
|
+
void this.ensureProjectsCache(false);
|
|
10846
10850
|
return;
|
|
10847
10851
|
}
|
|
10848
|
-
|
|
10852
|
+
await this.ensureProjectsCache(true);
|
|
10853
|
+
}
|
|
10854
|
+
/**
|
|
10855
|
+
* 确保静态缓存有基底(单飞:已有扫描在跑则搭车,不并发扫两遍盘)。
|
|
10856
|
+
* `emitResult`:首发扫描把结果 emit 给客户端;后台续期扫描只更新缓存——
|
|
10857
|
+
* 当前视图已由命中分支 emit 过,续期完成后客户端下次 pushProjects 自然拿到新基底。
|
|
10858
|
+
*/
|
|
10859
|
+
async ensureProjectsCache(emitResult) {
|
|
10849
10860
|
if (ClientSession.projectsInFlight) {
|
|
10850
10861
|
try {
|
|
10851
10862
|
const projects = await ClientSession.projectsInFlight;
|
|
10852
|
-
if (projects)
|
|
10863
|
+
if (projects && emitResult) {
|
|
10853
10864
|
this.emit({ type: "projects", projects: this.withCurrentCwd(projects, Date.now()) });
|
|
10865
|
+
}
|
|
10854
10866
|
}
|
|
10855
10867
|
catch {
|
|
10856
10868
|
/* 首发扫描已自行 emit 错误结果,这里不再补 */
|
|
@@ -10868,13 +10880,16 @@ export class ClientSession {
|
|
|
10868
10880
|
this.stateStore.mergeDiscoveredProjects(discovered);
|
|
10869
10881
|
const merged = await this.stateStore.getRecentProjects(this.clientId);
|
|
10870
10882
|
ClientSession.projectsCache = { at: Date.now(), projects: merged };
|
|
10871
|
-
|
|
10872
|
-
|
|
10883
|
+
if (emitResult) {
|
|
10884
|
+
this.emit({ type: "projects", projects: this.withCurrentCwd(merged, Date.now()) });
|
|
10885
|
+
}
|
|
10873
10886
|
return merged;
|
|
10874
10887
|
}
|
|
10875
10888
|
catch {
|
|
10876
10889
|
const fallback = this.withCurrentCwd(await this.stateStore.getRecentProjects(this.clientId), Date.now());
|
|
10877
|
-
|
|
10890
|
+
if (emitResult) {
|
|
10891
|
+
this.emit({ type: "projects", projects: fallback });
|
|
10892
|
+
}
|
|
10878
10893
|
return fallback;
|
|
10879
10894
|
}
|
|
10880
10895
|
finally {
|
|
@@ -11467,16 +11482,18 @@ export class ClientSession {
|
|
|
11467
11482
|
});
|
|
11468
11483
|
}
|
|
11469
11484
|
}
|
|
11470
|
-
/** 将导入型沙盒的改动写回源项目(保留源项目 .git
|
|
11485
|
+
/** 将导入型沙盒的改动写回源项目(保留源项目 .git 与依赖缓存,并清理已删除文件)。 */
|
|
11471
11486
|
async applySandboxToSource(path) {
|
|
11472
11487
|
const target = path?.trim() || this.cwd;
|
|
11473
11488
|
try {
|
|
11474
|
-
const { sandbox, sourcePath, copiedFiles } = await this.sandboxes.applyToSource(target);
|
|
11489
|
+
const { sandbox, sourcePath, copiedFiles, deletedFiles } = await this.sandboxes.applyToSource(target);
|
|
11490
|
+
const delSuffixZh = deletedFiles ? `,清理 ${deletedFiles} 个已删除文件` : "";
|
|
11491
|
+
const delSuffixEn = deletedFiles ? `, pruned ${deletedFiles} deleted files` : "";
|
|
11475
11492
|
this.emit({
|
|
11476
11493
|
type: "notice",
|
|
11477
11494
|
level: "info",
|
|
11478
|
-
text: `✅ 已将沙盒「${sandbox.name}
|
|
11479
|
-
textEn: `✅ Applied
|
|
11495
|
+
text: `✅ 已将沙盒「${sandbox.name}」中的改动同步回源项目:${sourcePath}(同步 ${copiedFiles} 个文件${delSuffixZh})`,
|
|
11496
|
+
textEn: `✅ Applied changes from sandbox "${sandbox.name}" back to source project: ${sourcePath} (${copiedFiles} files synced${delSuffixEn})`,
|
|
11480
11497
|
});
|
|
11481
11498
|
}
|
|
11482
11499
|
catch (err) {
|
|
@@ -11513,9 +11530,10 @@ export class ClientSession {
|
|
|
11513
11530
|
}
|
|
11514
11531
|
await this.setCwd(fallbackCwd);
|
|
11515
11532
|
}
|
|
11516
|
-
//
|
|
11533
|
+
// 清理仍挂在该沙盒下的后台会话(cwd 在沙盒**子目录**的也算——
|
|
11534
|
+
// 精确相等比较会让 <sandbox>/src 里的子代理/后台对话销毁后悬空)
|
|
11517
11535
|
for (const [cid, c] of this.convs) {
|
|
11518
|
-
if (cid !== this.activeId &&
|
|
11536
|
+
if (cid !== this.activeId && isSameOrSubpath(c.cwd, sbx.path)) {
|
|
11519
11537
|
this.removeConversation(cid);
|
|
11520
11538
|
}
|
|
11521
11539
|
}
|
|
@@ -103,8 +103,17 @@ export function saveAttachmentSync(buffer, mimeType = "application/octet-stream"
|
|
|
103
103
|
}
|
|
104
104
|
catch { }
|
|
105
105
|
const code = err.code;
|
|
106
|
-
if (code
|
|
106
|
+
if (code === "EEXIST") {
|
|
107
|
+
// 并发写同哈希:目标已在,按「已存在」成功路径返回
|
|
108
|
+
}
|
|
109
|
+
else if (code === "EPERM" && existsSync(filePath)) {
|
|
110
|
+
// Windows 杀软/OneDrive 锁住的是 tmp 源而目标已由并发方写好:等同已存在
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// EPERM 且目标不存在 / 其他错误:照常抛出。吞掉会返回指向缺失文件的
|
|
114
|
+
// 坏记录,消息里图片 404;抛出让 serialize 回退 dataUrl(转录里 base64 仍在)
|
|
107
115
|
throw err;
|
|
116
|
+
}
|
|
108
117
|
}
|
|
109
118
|
}
|
|
110
119
|
return {
|
|
@@ -2582,12 +2582,14 @@ export class DshClientSession {
|
|
|
2582
2582
|
async applySandboxToSource(path) {
|
|
2583
2583
|
const target = path?.trim() || this.cwd;
|
|
2584
2584
|
try {
|
|
2585
|
-
const { sandbox, sourcePath, copiedFiles } = await this.sandboxes.applyToSource(target);
|
|
2585
|
+
const { sandbox, sourcePath, copiedFiles, deletedFiles } = await this.sandboxes.applyToSource(target);
|
|
2586
|
+
const delSuffixZh = deletedFiles ? `,清理 ${deletedFiles} 个已删除文件` : "";
|
|
2587
|
+
const delSuffixEn = deletedFiles ? `, pruned ${deletedFiles} deleted files` : "";
|
|
2586
2588
|
this.emit({
|
|
2587
2589
|
type: "notice",
|
|
2588
2590
|
level: "info",
|
|
2589
|
-
text: `✅ 已将沙盒「${sandbox.name}
|
|
2590
|
-
textEn: `✅ Applied
|
|
2591
|
+
text: `✅ 已将沙盒「${sandbox.name}」中的改动同步回源项目:${sourcePath}(同步 ${copiedFiles} 个文件${delSuffixZh})`,
|
|
2592
|
+
textEn: `✅ Applied changes from sandbox "${sandbox.name}" back to source project: ${sourcePath} (${copiedFiles} files synced${delSuffixEn})`,
|
|
2591
2593
|
});
|
|
2592
2594
|
}
|
|
2593
2595
|
catch (err) {
|
|
@@ -2621,6 +2623,13 @@ export class DshClientSession {
|
|
|
2621
2623
|
}
|
|
2622
2624
|
await this.setCwd(fallbackCwd);
|
|
2623
2625
|
}
|
|
2626
|
+
// 清理仍挂在该沙盒下的后台会话(含子目录 cwd)——ClientSession 版有此步,
|
|
2627
|
+
// DSH 版此前完全没有:销毁后这些对话的 runtime/终端指向已删目录
|
|
2628
|
+
for (const [id, conv] of this.convs) {
|
|
2629
|
+
if (id !== this.activeId && isSameOrSubpath(conv.cwd, sbx.path)) {
|
|
2630
|
+
this.removeConversation(id);
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2624
2633
|
await this.sandboxes.destroy(sbx.id);
|
|
2625
2634
|
this.stateStore.removeProject(this.clientId, sbx.path);
|
|
2626
2635
|
await this.pushProjects();
|
|
@@ -31,6 +31,7 @@ export function makeMcpManageTool(host, lang) {
|
|
|
31
31
|
promptSnippet: "check whether an MCP server is installed or install/configure one",
|
|
32
32
|
promptGuidelines: [
|
|
33
33
|
"Run mcp_manage with action='check' before installing an MCP server to avoid duplicate entries.",
|
|
34
|
+
"install refuses to overwrite an existing server with the same name unless overwrite=true (it would replace command/args/env).",
|
|
34
35
|
"Prefer package shorthand (e.g. package='@modelcontextprotocol/server-filesystem') for npm-based stdio MCP servers.",
|
|
35
36
|
],
|
|
36
37
|
parameters: Type.Object({
|
|
@@ -76,6 +77,9 @@ export function makeMcpManageTool(host, lang) {
|
|
|
76
77
|
scope: Type.Optional(Type.Union([Type.Literal("project"), Type.Literal("global")], {
|
|
77
78
|
description: "install/remove: config scope ('project' = .pi/mcp.json, 'global' = ~/.pi/agent/mcp.json).",
|
|
78
79
|
})),
|
|
80
|
+
overwrite: Type.Optional(Type.Boolean({
|
|
81
|
+
description: "install: set true to replace an existing server with the same name (its command/args/env get overwritten).",
|
|
82
|
+
})),
|
|
79
83
|
description: Type.Optional(Type.String({
|
|
80
84
|
description: "install: human-readable server note.",
|
|
81
85
|
})),
|
|
@@ -55,7 +55,12 @@ export function createMcpHotReload(deps) {
|
|
|
55
55
|
const g = readOneFile(globalFile);
|
|
56
56
|
let globalServers = g.servers;
|
|
57
57
|
let gFp = g.fp;
|
|
58
|
-
|
|
58
|
+
// 与 mcp-bridge 的启动加载同口径:全局文件**不存在或存在但 0 个服务器**都
|
|
59
|
+
// 回退 dataDir legacy 配置。只判「文件不存在」时,「全局空配置 + legacy 有
|
|
60
|
+
// 服务」的部署里启动用 legacy 起服务、热加载却跳过 legacy——改 legacy 的
|
|
61
|
+
// 增删在重启前永不生效。
|
|
62
|
+
const globalHasNoServers = g.servers !== null && Object.keys(g.servers).length === 0;
|
|
63
|
+
if ((g.fp === "empty" || globalHasNoServers) && legacyFile !== globalFile) {
|
|
59
64
|
const leg = readOneFile(legacyFile);
|
|
60
65
|
if (leg.servers) {
|
|
61
66
|
globalServers = leg.servers;
|
|
@@ -343,6 +343,15 @@ export async function installMcpServer(opts) {
|
|
|
343
343
|
};
|
|
344
344
|
}
|
|
345
345
|
const scope = spec.scope === "global" ? "global" : "project";
|
|
346
|
+
// 重名保护:install 默认拒绝覆盖——promptGuidelines 里的"先 check"只是给模型
|
|
347
|
+
// 的建议,不拦运行时;静默覆盖会吃掉用户手工配置的同名条目(含私有 token env)
|
|
348
|
+
const existing = listAllMcpServers({ agentDir: opts.agentDir, cwd: opts.cwd, dataDir: opts.dataDir }).servers.find((s) => s.name === rawName && s.scope === scope);
|
|
349
|
+
if (existing && spec.overwrite !== true) {
|
|
350
|
+
return {
|
|
351
|
+
ok: false,
|
|
352
|
+
error: `已存在同名服务器「${rawName}」(${scope})——确认要覆盖请传 overwrite=true(将替换其命令/参数/环境变量)`,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
346
355
|
const uiServer = {
|
|
347
356
|
name: rawName,
|
|
348
357
|
scope,
|
|
@@ -282,6 +282,15 @@ export class PluginSecrets {
|
|
|
282
282
|
list() {
|
|
283
283
|
return Object.keys(this.load().items);
|
|
284
284
|
}
|
|
285
|
+
getAll() {
|
|
286
|
+
const out = {};
|
|
287
|
+
for (const name of this.list()) {
|
|
288
|
+
const val = this.get(name);
|
|
289
|
+
if (val !== undefined)
|
|
290
|
+
out[name] = val;
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
285
294
|
}
|
|
286
295
|
// ---------------------------------------------------------------------------
|
|
287
296
|
// deps(宿主代插件自动补装运行时依赖)
|
package/dist/server/plugins.js
CHANGED
|
@@ -2917,6 +2917,29 @@ export class PluginManager {
|
|
|
2917
2917
|
handlers.add(h);
|
|
2918
2918
|
return () => handlers.delete(h);
|
|
2919
2919
|
},
|
|
2920
|
+
onClientMessage: (h) => {
|
|
2921
|
+
return host.onMessage((payload, from) => {
|
|
2922
|
+
const reply = (res) => {
|
|
2923
|
+
if (from) {
|
|
2924
|
+
host.sendTo(from, res);
|
|
2925
|
+
}
|
|
2926
|
+
else {
|
|
2927
|
+
host.broadcast(res);
|
|
2928
|
+
}
|
|
2929
|
+
};
|
|
2930
|
+
try {
|
|
2931
|
+
const ret = h(payload, reply);
|
|
2932
|
+
if (ret instanceof Promise) {
|
|
2933
|
+
ret.catch((err) => {
|
|
2934
|
+
console.error(`[plugin:${info.id}] onClientMessage async handler failed:`, err);
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
catch (err) {
|
|
2939
|
+
console.error(`[plugin:${info.id}] onClientMessage handler failed:`, err);
|
|
2940
|
+
}
|
|
2941
|
+
});
|
|
2942
|
+
},
|
|
2920
2943
|
onToolEvent: (h) => {
|
|
2921
2944
|
toolHandlers.add(h);
|
|
2922
2945
|
return () => toolHandlers.delete(h);
|
|
@@ -3189,6 +3212,31 @@ export class PluginManager {
|
|
|
3189
3212
|
const off = this.registerAgentTool(info.id, tool);
|
|
3190
3213
|
return effects.add(`agentTool:${String(tool?.name ?? "?")}`, off);
|
|
3191
3214
|
},
|
|
3215
|
+
registerTool: (tool) => {
|
|
3216
|
+
if (!can("tools"))
|
|
3217
|
+
return () => { };
|
|
3218
|
+
if (!tool || typeof tool !== "object")
|
|
3219
|
+
return () => { };
|
|
3220
|
+
const exec = tool.execute;
|
|
3221
|
+
const adaptedTool = {
|
|
3222
|
+
name: tool.name,
|
|
3223
|
+
description: tool.description,
|
|
3224
|
+
label: tool.label,
|
|
3225
|
+
promptSnippet: tool.promptSnippet,
|
|
3226
|
+
promptGuidelines: tool.promptGuidelines,
|
|
3227
|
+
parameters: tool.parameters,
|
|
3228
|
+
execute: async (toolCallId, params, signal, onUpdate) => {
|
|
3229
|
+
if (typeof exec !== "function")
|
|
3230
|
+
return "";
|
|
3231
|
+
if (exec.length <= 1) {
|
|
3232
|
+
return await exec(params);
|
|
3233
|
+
}
|
|
3234
|
+
const multiFn = exec;
|
|
3235
|
+
return await multiFn(toolCallId, params, signal, onUpdate);
|
|
3236
|
+
},
|
|
3237
|
+
};
|
|
3238
|
+
return host.registerAgentTool(adaptedTool);
|
|
3239
|
+
},
|
|
3192
3240
|
dir,
|
|
3193
3241
|
dataDir: this.dataDir,
|
|
3194
3242
|
get cwd() {
|
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
* - 支持「临时沙盒(用完即删)」与「永久沙盒(持久化保留,随时一键转正)」;
|
|
7
7
|
* - 支持一键重置(还原到初始空白或重新从源项目同步)、另存为正式项目(promote)、将修改写回源项目(applyToSource)、彻底销毁(destroy)。
|
|
8
8
|
*
|
|
9
|
-
* 2.
|
|
9
|
+
* 2. 沙盒安全围栏(Sandbox Jail Gate):
|
|
10
10
|
* - 当会话处于受管沙盒目录内时,所有文件写入/编辑/补丁(write/edit/edit_soft/patch)必须严格落在
|
|
11
11
|
* `sandbox.path` 内部(含祖先目录 realpath 校验,防软链逃逸),越界直接硬拒绝(不走审批放行);
|
|
12
|
-
* - 终端命令(bash
|
|
12
|
+
* - 终端命令(bash)采用启发式拦截(禁止直接引用源项目路径 `sourcePath`、拦截越界 cd、输出重定向、越界写删命令与系统特权命令等);
|
|
13
|
+
* 请注意:沙盒基于工作区副本与工具调用层校验,未采用内核命名空间/容器级物理隔离,bash 终端拦截为尽力而为的启发式防护,非绝对安全边界;
|
|
13
14
|
* - 外部副作用工具(sftp 远端写/删/执行、mail 发信/删信)在沙盒内默认拦截。
|
|
14
15
|
*/
|
|
15
16
|
import { existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -137,6 +138,14 @@ export function extractPatchTargetPaths(patchText) {
|
|
|
137
138
|
const mUnified = line.match(/^(?:---|\+\+\+)\s+(?:[ab]\/)?([^\t\r\n]+)$/);
|
|
138
139
|
if (mUnified?.[1] && mUnified[1] !== "/dev/null") {
|
|
139
140
|
paths.push(mUnified[1].trim());
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
// hashline 格式段头(本仓 patch 工具实际消费的唯一格式):[path#TAG] / [path]。
|
|
144
|
+
// 不识别的话 hashline 补丁提取出 0 个目标 → 围栏检查整体空转,patch 路径
|
|
145
|
+
// 只剩 hashline-engine 的词法 cwd 限制(无 realpath,沙盒内软链可逃逸)
|
|
146
|
+
const mHashline = rawLine.match(/^\s*\[([^#\]]+)(?:#[0-9a-fA-F]{4})?\]\s*$/);
|
|
147
|
+
if (mHashline?.[1]?.trim()) {
|
|
148
|
+
paths.push(mHashline[1].trim());
|
|
140
149
|
}
|
|
141
150
|
}
|
|
142
151
|
return paths;
|
|
@@ -201,15 +210,83 @@ export function evaluateSandboxJail(sandbox, toolName, params, cwd) {
|
|
|
201
210
|
};
|
|
202
211
|
}
|
|
203
212
|
}
|
|
204
|
-
// 3b.
|
|
205
|
-
|
|
206
|
-
|
|
213
|
+
// 3b. 拦截通过 cd 跳出沙盒边界(裸 cd 跳 $HOME、cd ~、cd ..、或 cd 到沙盒外绝对路径,支持括号子 shell 与带引号/变量路径)
|
|
214
|
+
for (const m of cmd.matchAll(/(?:^|[;&|\n(`]\s*)cd(?:\s+([^;&|\n)`]+))?(?=$|[;&|\n)`])/g)) {
|
|
215
|
+
const rawArg = (m[1] ?? "").trim().replace(/^["']|["']$/g, "");
|
|
216
|
+
if (!rawArg ||
|
|
217
|
+
rawArg === "~" ||
|
|
218
|
+
rawArg.startsWith("~/") ||
|
|
219
|
+
rawArg.startsWith("~\\") ||
|
|
220
|
+
/^\$(?:HOME|USERPROFILE)\b|^\$\{(?:HOME|USERPROFILE)\}|^%USERPROFILE%/i.test(rawArg) ||
|
|
221
|
+
/\$(?:HOME|USERPROFILE)|\$\{(?:HOME|USERPROFILE)\}/i.test(rawArg)) {
|
|
222
|
+
return {
|
|
223
|
+
reason: `【沙盒安全隔离拦截】沙盒内禁止执行跳回用户主目录的 "cd ${rawArg || "~"}" 命令,请保持在沙盒目录(${sbxPath})内操作。`,
|
|
224
|
+
reasonEn: `[Sandbox Isolation Blocked] Escaping to the user home directory via "cd ${rawArg || "~"}" is forbidden in sandbox mode; stay within "${sbxPath}".`,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
if (rawArg !== "-" && !rawArg.startsWith("$") && !isStrictlyInsideSandbox(rawArg, cwd, sbxPath)) {
|
|
228
|
+
return {
|
|
229
|
+
reason: `【沙盒安全隔离拦截】禁止通过 "cd ${rawArg}" 跳出沙盒根目录(${sbxPath})。`,
|
|
230
|
+
reasonEn: `[Sandbox Isolation Blocked] Escaping the sandbox root (${sbxPath}) via "cd ${rawArg}" is forbidden.`,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// 3c. 拦截重定向(> / >>)写入沙盒外路径(/dev/null 除外)
|
|
235
|
+
for (const m of cmd.matchAll(/(?:^|[^0-9>&])>>?\s*([^\s;&|>'"]+|"[^"]+"|'[^']+')/g)) {
|
|
236
|
+
const target = (m[1] ?? "").trim().replace(/^["']|["']$/g, "");
|
|
237
|
+
if (!target || target === "/dev/null" || target === "NUL" || target.startsWith("&"))
|
|
238
|
+
continue;
|
|
239
|
+
if (!isStrictlyInsideSandbox(target, cwd, sbxPath)) {
|
|
207
240
|
return {
|
|
208
|
-
reason:
|
|
209
|
-
reasonEn: `[Sandbox Isolation Blocked]
|
|
241
|
+
reason: `【沙盒安全隔离拦截】禁止将输出重定向写入沙盒目录(${sbxPath})以外的路径:"${target}"。`,
|
|
242
|
+
reasonEn: `[Sandbox Isolation Blocked] Redirecting output to a path outside the sandbox directory (${sbxPath}) is forbidden: "${target}".`,
|
|
210
243
|
};
|
|
211
244
|
}
|
|
212
245
|
}
|
|
246
|
+
// 3d. 拦截系统特权命令与全局包安装污染宿主机
|
|
247
|
+
if (/(?:^|[;&|\n]\s*)(?:sudo|su|mkfs|format|fdisk|diskpart|reg\s+(?:add|delete)|schtasks|systemctl|launchctl|sc\s+(?:create|delete|stop))\b/i.test(cmd) ||
|
|
248
|
+
/\b(?:npm\s+(?:i|install|add|un|uninstall)\s+(?:[^;&|\n]*\s)?(?:-g|--global)\b|pnpm\s+(?:add|remove)\s+(?:[^;&|\n]*\s)?(?:-g|--global)\b|yarn\s+global\b)/i.test(cmd)) {
|
|
249
|
+
return {
|
|
250
|
+
reason: `【沙盒安全隔离拦截】沙盒内禁止执行宿主机系统级管理命令或全局包安装(-g/--global),以保护宿主系统环境。`,
|
|
251
|
+
reasonEn: `[Sandbox Isolation Blocked] System administration commands and global package installations (-g/--global) are forbidden in sandbox mode to protect the host environment.`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
// 3e. 拦截文件修改/删除命令(rm/del/mv/cp/chmod/touch/mkdir/sed -i/tee 等)指向 ..、~、$HOME 或沙盒外绝对路径
|
|
255
|
+
const mutatingCmdRe = /(?:^|[;&|\n(`]\s*)(?:rm|del|erase|rmdir|rd|mv|move|cp|copy|xcopy|robocopy|ln|mklink|chmod|chown|touch|mkdir|tee|dd|Remove-Item|Move-Item|Copy-Item|Set-Content|Add-Content|Out-File|sed\s+-[a-zA-Z]*i)\b([^;&|\n)]*)/gi;
|
|
256
|
+
for (const m of cmd.matchAll(mutatingCmdRe)) {
|
|
257
|
+
const argsTail = (m[1] ?? "").trim();
|
|
258
|
+
const tokens = argsTail.match(/(?:"[^"]*"|'[^']*'|\S+)/g) ?? [];
|
|
259
|
+
for (const rawTok of tokens) {
|
|
260
|
+
const tok = rawTok.replace(/^["']|["']$/g, "");
|
|
261
|
+
if (!tok || tok.startsWith("-") || tok === "/dev/null")
|
|
262
|
+
continue;
|
|
263
|
+
if (tok === "~" ||
|
|
264
|
+
tok.startsWith("~/") ||
|
|
265
|
+
tok.startsWith("~\\") ||
|
|
266
|
+
/^\$(?:HOME|USERPROFILE)\b|^%USERPROFILE%/i.test(tok) ||
|
|
267
|
+
tok === ".." ||
|
|
268
|
+
tok.startsWith("../") ||
|
|
269
|
+
tok.startsWith("..\\") ||
|
|
270
|
+
tok.includes("/../") ||
|
|
271
|
+
tok.includes("\\..\\")) {
|
|
272
|
+
if (!isStrictlyInsideSandbox(tok, cwd, sbxPath)) {
|
|
273
|
+
return {
|
|
274
|
+
reason: `【沙盒安全隔离拦截】检测到命令尝试操作沙盒外部或宿主系统路径("${tok}"),已被硬围栏阻断。`,
|
|
275
|
+
reasonEn: `[Sandbox Isolation Blocked] Command attempted to mutate a path outside the sandbox or on the host system ("${tok}") and was blocked.`,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// 绝对路径(POSIX /... 或 Windows C:\...)且不在沙盒根内
|
|
280
|
+
if ((tok.startsWith("/") && !tok.startsWith("//")) || /^[a-zA-Z]:[\\/]/.test(tok)) {
|
|
281
|
+
if (!isStrictlyInsideSandbox(tok, cwd, sbxPath)) {
|
|
282
|
+
return {
|
|
283
|
+
reason: `【沙盒安全隔离拦截】检测到命令尝试修改沙盒目录(${sbxPath})以外的系统/外部路径("${tok}"),已被硬围栏阻断。`,
|
|
284
|
+
reasonEn: `[Sandbox Isolation Blocked] Command attempted to modify an external/system path outside the sandbox ("${tok}") and was blocked.`,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
213
290
|
return null;
|
|
214
291
|
}
|
|
215
292
|
// 4. 外部副作用工具:sftp / mail
|
|
@@ -446,6 +523,10 @@ export class SandboxManager {
|
|
|
446
523
|
* 创建一个新沙盒工作区(空白或从 `sourcePath` 导入隔离副本)。
|
|
447
524
|
*/
|
|
448
525
|
async create(opts) {
|
|
526
|
+
// 先同步共享注册表:每个 ClientSession/DshClientSession 各持一个管理器实例、
|
|
527
|
+
// 共享同一 registry.json——不 ensureFresh 的话并发创建会用陈旧内存覆盖写回,
|
|
528
|
+
// 丢掉其他客户端刚建的沙盒条目(getByCwd 找不到 → jail 围栏静默失效)
|
|
529
|
+
this.ensureFresh();
|
|
449
530
|
const rawSource = opts.sourcePath?.trim();
|
|
450
531
|
let resolvedSource;
|
|
451
532
|
if (rawSource) {
|
|
@@ -464,6 +545,11 @@ export class SandboxManager {
|
|
|
464
545
|
if (isSameOrSubpath(this.sandboxesRoot, abs)) {
|
|
465
546
|
throw new Error(`Refusing to import a parent directory of sandboxes root: ${abs}`);
|
|
466
547
|
}
|
|
548
|
+
// 沙盒根**内部**的目录也不允许当源:会造出嵌套沙盒(源被另一沙盒的
|
|
549
|
+
// destroy/reset 抽掉、apply_to_source 把改动写进另一个沙盒)
|
|
550
|
+
if (isSameOrSubpath(abs, this.sandboxesRoot)) {
|
|
551
|
+
throw new Error(`Refusing to import a directory inside the sandboxes root: ${abs}`);
|
|
552
|
+
}
|
|
467
553
|
// 若选中的 source 本身已经是某个沙盒,而且该沙盒记录了原始 sourcePath,保留最外层真实路径或直接复制该沙盒内容
|
|
468
554
|
resolvedSource = toWirePath(abs);
|
|
469
555
|
}
|
|
@@ -498,8 +584,23 @@ export class SandboxManager {
|
|
|
498
584
|
}
|
|
499
585
|
await mkdir(info.path, { recursive: true });
|
|
500
586
|
const entries = await readdir(info.path);
|
|
587
|
+
// 逐条目容错:Windows 下沙盒内进程未停(npm run dev 的 node.exe 持有句柄)
|
|
588
|
+
// 会让单个条目 EBUSY/EPERM——整体抛错会留下半清空状态;能删的先删,
|
|
589
|
+
// 全部失败才抛
|
|
590
|
+
const failures = [];
|
|
501
591
|
for (const entry of entries) {
|
|
502
|
-
|
|
592
|
+
try {
|
|
593
|
+
await rm(join(info.path, entry), { recursive: true, force: true, maxRetries: 3 });
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
failures.push(entry);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (failures.length === entries.length && entries.length > 0) {
|
|
600
|
+
throw new Error(`Failed to clear sandbox contents (all ${failures.length} entries busy): ${failures.slice(0, 5).join(", ")}`);
|
|
601
|
+
}
|
|
602
|
+
if (failures.length > 0) {
|
|
603
|
+
console.warn(`[sandbox] reset: ${failures.length}/${entries.length} entries could not be removed (busy): ${failures.slice(0, 5).join(", ")}`);
|
|
503
604
|
}
|
|
504
605
|
let copyStats = { copiedFiles: 0, skippedFiles: 0 };
|
|
505
606
|
if (info.sourcePath && existsSync(info.sourcePath)) {
|
|
@@ -565,7 +666,7 @@ export class SandboxManager {
|
|
|
565
666
|
};
|
|
566
667
|
}
|
|
567
668
|
/**
|
|
568
|
-
* 将导入型沙盒中的修改写回源项目(保留源项目 `.git`
|
|
669
|
+
* 将导入型沙盒中的修改写回源项目(保留源项目 `.git` 与重型依赖目录,同步工作区文件变更并清理已删除文件)。
|
|
569
670
|
*/
|
|
570
671
|
async applyToSource(idOrPath) {
|
|
571
672
|
const info = this.get(idOrPath) ?? this.getByCwd(idOrPath);
|
|
@@ -575,11 +676,50 @@ export class SandboxManager {
|
|
|
575
676
|
if (!info.sourcePath || !existsSync(info.sourcePath)) {
|
|
576
677
|
throw new Error("This sandbox has no valid source project to apply changes back to");
|
|
577
678
|
}
|
|
679
|
+
// 1. 先通过 diff 计算出被删除的文件列表,以便同步清理源项目中的对应文件
|
|
680
|
+
const diff = await this.diffWithSource(info.id);
|
|
681
|
+
let deletedFiles = 0;
|
|
682
|
+
for (const rel of diff.deleted) {
|
|
683
|
+
// 安全校验:严格限制在 sourcePath 内,且避开 .git 与重型忽略目录
|
|
684
|
+
if (!rel ||
|
|
685
|
+
rel === ".git" ||
|
|
686
|
+
rel.startsWith(".git/") ||
|
|
687
|
+
rel.startsWith(".git\\") ||
|
|
688
|
+
!isStrictlyInsideSandbox(rel, info.sourcePath, info.sourcePath)) {
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
const targetFile = join(info.sourcePath, rel);
|
|
692
|
+
if (existsSync(targetFile)) {
|
|
693
|
+
try {
|
|
694
|
+
await rm(targetFile, { force: true });
|
|
695
|
+
deletedFiles++;
|
|
696
|
+
// 尝试顺带清理变空的父目录(直到 sourcePath,不删 sourcePath 自身)
|
|
697
|
+
let parent = dirname(targetFile);
|
|
698
|
+
while (parent &&
|
|
699
|
+
isSameOrSubpath(parent, info.sourcePath) &&
|
|
700
|
+
normalizePathKey(parent) !== normalizePathKey(info.sourcePath)) {
|
|
701
|
+
const remaining = await readdir(parent).catch(() => ["non-empty"]);
|
|
702
|
+
if (remaining.length === 0) {
|
|
703
|
+
await rm(parent, { recursive: true, force: true }).catch(() => { });
|
|
704
|
+
parent = dirname(parent);
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
catch {
|
|
712
|
+
// 忽略单个文件清理失败
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
// 2. 将沙盒工作区的文件复制覆盖回源项目
|
|
578
717
|
const stats = await copyWorkspaceTree(info.path, info.sourcePath, { skipGitDir: true });
|
|
579
718
|
return {
|
|
580
719
|
sandbox: info,
|
|
581
720
|
sourcePath: info.sourcePath,
|
|
582
721
|
copiedFiles: stats.copiedFiles,
|
|
722
|
+
deletedFiles,
|
|
583
723
|
};
|
|
584
724
|
}
|
|
585
725
|
/**
|