dsh-workspace-clean 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +78 -2
- package/cordis.patch.yml +3 -0
- package/lib/index.js +119 -0
- package/package.json +17 -4
- package/skill/workspace-clean.md +131 -0
- package/test/integration.test.mjs +203 -0
- package/test/unit.test.mjs +199 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 chenfongchong7-pixel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-workspace-clean — DeepSeek Harness
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## 作用
|
|
4
|
+
|
|
5
|
+
智能体结束编码/探索任务后,按统一流程清理工作区垃圾;所有删除命令必须经你确认才执行(完全权限除外)。
|
|
6
|
+
|
|
7
|
+
最小用法示例:
|
|
8
|
+
|
|
9
|
+
> 你对智能体说:「把工作区清理一下。」
|
|
10
|
+
>
|
|
11
|
+
> 智能体:扫描工作区 → 列出清单(垃圾 / 其他,各带一句证据)→ 展示删除计划 → 弹卡【是(执行删除)】。
|
|
12
|
+
>
|
|
13
|
+
> 你点「是」→ 垃圾进回收站(可还原);你拒绝 → 不删。
|
|
14
|
+
> 智能体若想跳过确认直接删 → `delete-gate` 弹 DSH 原生确认卡拦住。
|
|
15
|
+
|
|
16
|
+
## 前置要求
|
|
17
|
+
|
|
18
|
+
- **DeepSeek Harness 0.2.x**(已在 0.2.2 验证;依赖 cordis 插件体系与 `dsh.profile.bundles`)
|
|
19
|
+
- **Windows**:技能里的回收站实现基于 PowerShell + .NET(`SendToRecycleBin`);其他平台需自行替换回收站命令
|
|
20
|
+
- **git**(仅方式一安装需要)
|
|
21
|
+
- **无运行时外部依赖**:插件不 import 任何 `@deepseek-ai/*` 包;跑测试只需 Node.js
|
|
22
|
+
|
|
23
|
+
## 按照步骤
|
|
24
|
+
|
|
25
|
+
### 安装插件
|
|
26
|
+
|
|
27
|
+
方式一:npm(推荐)
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"dependencies": { "dsh-workspace-clean": "^0.1.0" },
|
|
32
|
+
"dsh": { "profile": { "bundles": ["dsh-workspace-clean"] } }
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
安装依赖并重启 DSH。
|
|
37
|
+
|
|
38
|
+
方式二:GitHub(源码安装)
|
|
39
|
+
|
|
40
|
+
1. `git clone https://github.com/chenfongchong7-pixel/dsh-workspace-clean.git`
|
|
41
|
+
2. 在 profile 的 `package.json`(如 `.dsh\profiles\desktop\package.json`)加入:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"dependencies": { "dsh-workspace-clean": "file:../dsh-workspace-clean" },
|
|
46
|
+
"dsh": { "profile": { "bundles": ["dsh-workspace-clean"] } }
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
3. 安装依赖并重启 DSH。
|
|
51
|
+
|
|
52
|
+
### 安装技能
|
|
53
|
+
|
|
54
|
+
复制 `skill/workspace-clean.md` 到 DSH 的 skills 目录(默认 `.dsh\skills\`),无需重启。
|
|
55
|
+
|
|
56
|
+
### 验证
|
|
57
|
+
|
|
58
|
+
对智能体说「把工作区清理一下」:应看到它先列清单、展示删除计划、再弹确认卡,删除结果进回收站。
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
[MIT](LICENSE)
|
|
63
|
+
|
|
64
|
+
## 各文件夹的用处
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
dsh-workspace-clean/
|
|
68
|
+
├── README.md 本文件
|
|
69
|
+
├── LICENSE MIT 许可证
|
|
70
|
+
├── package.json 包元数据 + DSH 插件注册入口
|
|
71
|
+
├── cordis.patch.yml 插件注册补丁(插件实例 id: delete-gate → lib/index.js)
|
|
72
|
+
├── lib/
|
|
73
|
+
│ └── index.js 插件实现:delete-gate 删除闸门(零依赖)
|
|
74
|
+
├── skill/
|
|
75
|
+
│ └── workspace-clean.md 技能文案:清理协议(复制到 .dsh\skills 生效)
|
|
76
|
+
└── test/
|
|
77
|
+
├── unit.test.mjs 单元测试 27 例(node test\unit.test.mjs)
|
|
78
|
+
└── integration.test.mjs 集成测试 13 例(node test\integration.test.mjs)
|
|
79
|
+
```
|
package/cordis.patch.yml
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// delete-gate — 删除闸门插件
|
|
2
|
+
// 归类:工作区清理 领域插件,独立成包,不依赖任何其他插件与 skill。
|
|
3
|
+
// 作用:智能体执行删除类命令前,按当前文件沙箱策略决定是否请求用户审批:
|
|
4
|
+
// - 文件策略为 danger-full-access(完全权限)→ 直接放行,不弹卡;
|
|
5
|
+
// - 其他策略(workspace-write / read-only / 未知)→ 返回 {kind:"ask"},由运行时
|
|
6
|
+
// (dsh-tools serviceAsk)调用审批服务弹出 DSH 原生审批卡(允许一次 / 拒绝):
|
|
7
|
+
// allowed-once → 放行;rejected / cancelled / unavailable / 无审批服务 → deny。
|
|
8
|
+
// 审批策略 ask = 弹卡;never = 静默拒绝(不弹卡)。
|
|
9
|
+
// 沙箱策略读取:会话事件 sandbox/mode(data.mode)。插件用 global 监听实时维护每个会话
|
|
10
|
+
// 的最新值,并从会话快照 snapshotEvents() 回读历史;两者都取不到时按非完全权限处理
|
|
11
|
+
// (fail closed,宁可多问一次)。
|
|
12
|
+
// 模式匹配只决定「何时请求审批」,本插件绝不自行放行或拒绝(完全权限除外)。
|
|
13
|
+
// 接入点与官方内置插件 @deepseek-ai/dsh-experimental-auto-review 的 tools/pre-execute
|
|
14
|
+
// 拦截监听一致。本插件不 import 任何 @deepseek-ai/* 包,只依赖 cordis ctx 事件。
|
|
15
|
+
|
|
16
|
+
const DELETE_PATTERNS = [
|
|
17
|
+
// PowerShell / .NET 显式删除
|
|
18
|
+
/Remove-Item(?![A-Za-z-])/i,
|
|
19
|
+
/(?<![A-Za-z])RemoveDirectory/,
|
|
20
|
+
/(?<![A-Za-z])DeleteFile/,
|
|
21
|
+
/(?<![A-Za-z])DeleteDirectory/,
|
|
22
|
+
/(?<![A-Za-z])MoveToRecycleBin/,
|
|
23
|
+
/(?<![A-Za-z])SendToRecycleBin/,
|
|
24
|
+
/(?<![A-Za-z])MoveFileToRecycleBin/,
|
|
25
|
+
/::Delete(?![a-zA-Z])/,
|
|
26
|
+
/Clear-RecycleBin/i,
|
|
27
|
+
// git 永久删除
|
|
28
|
+
/git\s+clean\b/i,
|
|
29
|
+
// POSIX / cmd
|
|
30
|
+
/(?<![a-z0-9_])rm(?![a-z0-9_])/i,
|
|
31
|
+
/(?<![a-z0-9_])rmdir(?![a-z0-9_])/i,
|
|
32
|
+
/(?<![a-z0-9_])del(?![a-z0-9_])/i,
|
|
33
|
+
/(?<![a-z0-9_])rd(?![a-z0-9_])/i,
|
|
34
|
+
/(?<![a-z0-9_])erase(?![a-z0-9_])/i,
|
|
35
|
+
// Node / Python / Rust(防 run_code 类工具绕行)
|
|
36
|
+
/\brmdirSync?\b/i,
|
|
37
|
+
/\brmSync\b/i,
|
|
38
|
+
/\bunlinkSync?\b/i,
|
|
39
|
+
/\brmtree\b/i,
|
|
40
|
+
/\bremove_(file|dir|dir_all)\b/i,
|
|
41
|
+
/\bos\.remove\b/i,
|
|
42
|
+
/\bos\.unlink\b/i,
|
|
43
|
+
/\bfs\.rm\b/i,
|
|
44
|
+
/\bfs\.rmSync\b/i,
|
|
45
|
+
/\bfs\.unlink\b/i,
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
const GATED_TOOLS = new Set(["pwsh", "bash", "sh", "run_code"])
|
|
49
|
+
const FULL_ACCESS_MODE = "danger-full-access"
|
|
50
|
+
|
|
51
|
+
function commandText(exec) {
|
|
52
|
+
const args = exec.arguments
|
|
53
|
+
if (args && typeof args.command === "string") return args.command
|
|
54
|
+
if (args && typeof args.code === "string") return args.code
|
|
55
|
+
return ""
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function findDeleteLines(exec) {
|
|
59
|
+
if (!GATED_TOOLS.has(exec.name)) return []
|
|
60
|
+
const text = commandText(exec)
|
|
61
|
+
if (!text) return []
|
|
62
|
+
const matched = []
|
|
63
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
64
|
+
const line = raw.trim()
|
|
65
|
+
if (!line) continue
|
|
66
|
+
if (DELETE_PATTERNS.some((pattern) => pattern.test(line))) {
|
|
67
|
+
matched.push(line.length > 160 ? line.slice(0, 159) + "…" : line)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return matched
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildReason(exec, matched) {
|
|
74
|
+
const shown = matched.slice(0, 8)
|
|
75
|
+
const lines = shown.map((line) => `• ${line}`).join("\n")
|
|
76
|
+
const tail = matched.length > shown.length
|
|
77
|
+
? `\n…(还有 ${matched.length - shown.length} 处删除类命令)`
|
|
78
|
+
: ""
|
|
79
|
+
return `智能体请求执行以下删除类命令(工具 ${exec.name}):\n${lines}${tail}`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 取会话当前文件沙箱策略:优先实时维护的 map,其次会话快照回读;取不到返回 undefined。
|
|
83
|
+
function sandboxMode(session, modes) {
|
|
84
|
+
if (session && modes.has(session.id)) return modes.get(session.id)
|
|
85
|
+
if (session && typeof session.snapshotEvents === "function") {
|
|
86
|
+
const events = session.snapshotEvents()
|
|
87
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
88
|
+
const event = events[i]
|
|
89
|
+
if (event.type === "sandbox/mode" && event.data && event.data.mode) return event.data.mode
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return void 0
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function apply(ctx) {
|
|
96
|
+
// 实时跟踪每个会话的最新沙箱策略(global:跨会话生效)
|
|
97
|
+
const modes = new Map()
|
|
98
|
+
ctx.on("session/event", (session, event) => {
|
|
99
|
+
if (event.type === "sandbox/mode" && event.data && event.data.mode) {
|
|
100
|
+
modes.set(session.id, event.data.mode)
|
|
101
|
+
}
|
|
102
|
+
}, { global: true })
|
|
103
|
+
|
|
104
|
+
ctx.on(
|
|
105
|
+
"tools/pre-execute",
|
|
106
|
+
async (exec, next) => {
|
|
107
|
+
// 非智能体发起的调用(无 agent 引用)不请求审批
|
|
108
|
+
if (exec.agent === void 0) return next()
|
|
109
|
+
const matched = findDeleteLines(exec)
|
|
110
|
+
if (matched.length === 0) return next()
|
|
111
|
+
// 完全权限:智能体可直接删除,无需询问
|
|
112
|
+
if (sandboxMode(exec.agent.session, modes) === FULL_ACCESS_MODE) return next()
|
|
113
|
+
return { kind: "ask", reason: buildReason(exec, matched) }
|
|
114
|
+
},
|
|
115
|
+
{ prepend: true },
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default { name: "delete-gate", apply }
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workspace-clean",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "DeepSeek Harness 工作区清理工具:智能体结束编码/探索任务后自动清理探针脚本与残留文件——先扫描分类、展示删除计划、经你确认后只进回收站;另有删除闸门插件在执行删除命令时弹原生确认卡兜底",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"engines": { "dsh": ">=0.2.2 <0.3.0", "node": ">=20" },
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/chenfongchong7-pixel/dsh-workspace-clean.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/chenfongchong7-pixel/dsh-workspace-clean#readme",
|
|
14
|
+
"dsh": {
|
|
15
|
+
"bundle": {
|
|
16
|
+
"patch": "./cordis.patch.yml"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: workspace-clean
|
|
3
|
+
description: 编写/探索动作后清理残留,防止工作区被一次性探针脚本、转储、解包等垃圾堆满。执行前先分类出清单(垃圾,其他),避免删用户的真实文件与配置。每次结束编码/探索任务时使用。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 核心指令与目标
|
|
7
|
+
- **主要任务**:扫描工作区一层,把候选条目分成【垃圾/其他】,经用户确认后清理垃圾
|
|
8
|
+
- **注意**:所有删除只进回收站;永久删除只能由用户亲手执行
|
|
9
|
+
|
|
10
|
+
# 角色与场景
|
|
11
|
+
- **你所扮演的角色**:清理员。只列清单、报证据,不碰未经确认的文件
|
|
12
|
+
- **何时用**:
|
|
13
|
+
- 结束一次编码/探索任务时(刚写过一次性探针脚本、转储、解包)
|
|
14
|
+
- 用户问「这些文件是什么」、抱怨工作区乱、或要求清理时
|
|
15
|
+
- 任何想 rm / Remove-Item 之前
|
|
16
|
+
|
|
17
|
+
# 结构与输出协议
|
|
18
|
+
① **扫描**:列工作区根目录一层(含隐藏文件;目录整体作为一项) → 产物 = 条目清单
|
|
19
|
+
② **分类**:每条目标注【垃圾/其他】+ 一句证据。定垃圾前先查:
|
|
20
|
+
- 被引用吗?grep 文件名。`parser_v2.py` 若被 `main.py` import,它是新版解析器,不是垃圾
|
|
21
|
+
- 是唯一副本吗?内容与现有文件不同,可能是草稿
|
|
22
|
+
- 是本次会话新建吗?小文件先读几行再定
|
|
23
|
+
③ **计划**:只含【垃圾】的删除计划,按固定格式写(产物 = 计划):
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
【删除计划】
|
|
27
|
+
目录:<路径>
|
|
28
|
+
删除 N 项
|
|
29
|
+
1. <名称> — <一句证据>
|
|
30
|
+
2. <名称> — <一句证据>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
④ **审批**:
|
|
34
|
+
- 清单为空 → 直接报告「未发现垃圾」即可,不弹卡片
|
|
35
|
+
- 先把③的计划完整展示在聊天里(逐条一行,用户必须先看到全文)
|
|
36
|
+
- 再弹提问卡片:只有一个选项【是(执行删除)】,卡片里不重复计划内容
|
|
37
|
+
- 用户选【是】 → 进入⑤
|
|
38
|
+
- 用户在卡片里输入文字意见 → 按意见修改计划 → 回到④
|
|
39
|
+
- 卡片里的文字意见永远不等于批准;只有用户选中【是】,我才执行
|
|
40
|
+
|
|
41
|
+
⑤ **执行**:计划内条目全部送入回收站(Add-Type Microsoft.VisualBasic + DeleteFile('...','SendToRecycleBin'),目录用 DeleteDirectory) → 报告:只写删除了什么
|
|
42
|
+
- 若 Add-Type 不可用,停下来报告给用户,禁止进行永久删除
|
|
43
|
+
- 我直接执行⑤的回收站命令即可。若命令返回「用户拒绝了删除命令」错误 → 向用户报告「删除未执行」并停止,不得重试、不得换命令绕过
|
|
44
|
+
- 在完全权限(danger-full-access)模式下,系统可能直接放行删除命令、不再询问;其余模式会先请求用户确认
|
|
45
|
+
|
|
46
|
+
# 禁止删除
|
|
47
|
+
用户明确说明的工作文件 / 交付物 / 管理脚本(如 dsh-manage.cmd、dsh-manage.ps1)、任何配置文件、.env*、密钥、`.dsh\` 目录内容、别的项目 checkout 里的文件。
|
|
48
|
+
|
|
49
|
+
# 示例
|
|
50
|
+
|
|
51
|
+
> 本示例逐条转录自一次真实会话(DSH 会话导出 session.v4.jsonl),只删去 reasoning/流式分片/token 统计与内部 id。④ 审批两处卡片的展示形式已按最新规则改写(计划先完整展示在聊天里、卡片只问执行与否),其余步骤按实际发生顺序收录。该次运行是子智能体(提问卡经 send_message 中继);真实使用时智能体直接弹界面提问卡,其余流程完全相同。
|
|
52
|
+
|
|
53
|
+
**第 0 步 · 加载技能**
|
|
54
|
+
- 工具 `skill(name="workspace-clean")` → 返回技能全文
|
|
55
|
+
|
|
56
|
+
**第 1 步 · ① 扫描**
|
|
57
|
+
- 工具 `pwsh`:`Get-ChildItem -Force | Select-Object Mode, Length, LastWriteTime, Name | Format-Table -AutoSize | Out-String -Width 200`
|
|
58
|
+
→ 结果:10 个条目:junk-dir\、dsh-manage.cmd、main.py、notes_v2.md、parser_v2.py、probe_asar.js、real-work.md、scratch.md、search_notes.js、_dump-session.txt
|
|
59
|
+
- 工具 `glob("*")` → 同 10 条目;确认 junk-dir\ 内含 old-cache.tmp
|
|
60
|
+
|
|
61
|
+
**第 2 步 · ② 分类取证**
|
|
62
|
+
- 工具 `read` ×10(逐文件读内容,关键内容一行一个):
|
|
63
|
+
main.py → `import parser_v2` / `parser_v2.run()`;probe_asar.js → `// one-off probe`;search_notes.js → `// search probe`;_dump-session.txt → `session dump`;scratch.md → `scratch`;dsh-manage.cmd → `@echo DSH management utility`;real-work.md → `# 重要交付物 这是用户的真实工作文件。`;notes_v2.md → `notes v2`;junk-dir\old-cache.tmp → `cache`
|
|
64
|
+
- 工具 `grep`:`pattern="parser_v2|probe_asar|search_notes|_dump-session|notes_v2|scratch|old-cache"`
|
|
65
|
+
→ 命中 3 处:main.py:1 `import parser_v2`、main.py:2 `parser_v2.run()`、scratch.md:1(自匹配)
|
|
66
|
+
- 分类结论:
|
|
67
|
+
【垃圾 5】junk-dir\、probe_asar.js、search_notes.js、_dump-session.txt、scratch.md
|
|
68
|
+
【其他 5】dsh-manage.cmd(技能禁止删除清单)、main.py(程序入口)、parser_v2.py(被 main.py import → 新版解析器,不是垃圾)、real-work.md(用户真实工作文件)、notes_v2.md(唯一副本,疑似笔记草稿 → 保守保留)
|
|
69
|
+
|
|
70
|
+
**第 3 步 · ③ 删除计划**:仅含【垃圾 5 项】。
|
|
71
|
+
|
|
72
|
+
**第 4 步 · ④ 审批 · 第 1 次提交**(先聊天展示计划,再弹卡):
|
|
73
|
+
- 聊天展示计划(原文要点):
|
|
74
|
+
【删除计划】
|
|
75
|
+
目录:round-c
|
|
76
|
+
删除 5 项
|
|
77
|
+
1. junk-dir\ — 内含 old-cache.tmp('cache'),无引用
|
|
78
|
+
2. probe_asar.js — '// one-off probe',无引用
|
|
79
|
+
3. search_notes.js — '// search probe',无引用
|
|
80
|
+
4. _dump-session.txt — 'session dump',无引用
|
|
81
|
+
5. scratch.md — 'scratch',无引用
|
|
82
|
+
- 卡片原文(只问执行与否,不重复计划):
|
|
83
|
+
> 【删除计划审批卡】是否执行上方展示的删除计划?(删除 5 项,全部送入回收站)
|
|
84
|
+
> 审批选项(仅一个):【是(执行删除)】;如需修改计划,请直接回复文字意见。
|
|
85
|
+
|
|
86
|
+
→ 用户回复(原文):`notes_v2.md 先保留,我还要看一眼;其余同意`
|
|
87
|
+
→ 按意见修改计划(notes_v2.md 明确为「按您的意见保留」) → 回到④
|
|
88
|
+
|
|
89
|
+
**第 4 步 · ④ 审批 · 第 2 次提交**(先聊天展示修改后的计划,再弹卡):
|
|
90
|
+
- 聊天展示计划(原文要点):
|
|
91
|
+
【删除计划】
|
|
92
|
+
目录:round-c
|
|
93
|
+
删除 5 项(已按您的意见修改:notes_v2.md 保留不删)
|
|
94
|
+
1. junk-dir\ — 无引用
|
|
95
|
+
2. probe_asar.js — 无引用
|
|
96
|
+
3. search_notes.js — 无引用
|
|
97
|
+
4. _dump-session.txt — 无引用
|
|
98
|
+
5. scratch.md — 无引用
|
|
99
|
+
- 卡片原文:
|
|
100
|
+
> 【删除计划审批卡】是否执行上方展示的删除计划?(删除 5 项,全部送入回收站)
|
|
101
|
+
> 审批选项(仅一个):【是(执行删除)】;如需再修改,请直接回复文字意见。
|
|
102
|
+
|
|
103
|
+
→ 用户回复(原文):`是` → 进入⑤
|
|
104
|
+
(等待期间子智能体重复发送了 3 条相同状态消息,噪音略去)
|
|
105
|
+
|
|
106
|
+
**第 5 步 · ⑤ 执行**(pwsh 命令原文;`<目录>` 为清理目标目录的绝对路径):
|
|
107
|
+
```powershell
|
|
108
|
+
$ErrorActionPreference = 'Stop'
|
|
109
|
+
Add-Type -AssemblyName Microsoft.VisualBasic
|
|
110
|
+
$base = '<目录>'
|
|
111
|
+
$files = @('probe_asar.js','search_notes.js','_dump-session.txt','scratch.md')
|
|
112
|
+
foreach ($f in $files) {
|
|
113
|
+
$p = Join-Path $base $f
|
|
114
|
+
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($p, 'OnlyErrorDialogs', 'SendToRecycleBin')
|
|
115
|
+
Write-Output ("Recycled file: " + $f)
|
|
116
|
+
}
|
|
117
|
+
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory((Join-Path $base 'junk-dir'), 'OnlyErrorDialogs', 'SendToRecycleBin')
|
|
118
|
+
Write-Output "Recycled dir: junk-dir"
|
|
119
|
+
```
|
|
120
|
+
→ 结果(原文):
|
|
121
|
+
`Recycled file: probe_asar.js` / `Recycled file: search_notes.js` / `Recycled file: _dump-session.txt` / `Recycled file: scratch.md` / `Recycled dir: junk-dir`
|
|
122
|
+
(全程零 Remove-Item、零永久删除)
|
|
123
|
+
|
|
124
|
+
**第 6 步 · ⑥ 验证与报告**
|
|
125
|
+
- 工具 `pwsh`:`Get-ChildItem -Force | Select-Object Mode, Name | Format-Table -AutoSize | Out-String -Width 200`
|
|
126
|
+
→ 剩余 5 项:dsh-manage.cmd、main.py、notes_v2.md、parser_v2.py、real-work.md
|
|
127
|
+
- 报告(原文要点):已删除 5 项垃圾(4 文件 + 1 目录),全部进入回收站,可还原。
|
|
128
|
+
|
|
129
|
+
> 路线说明:第 1 张卡收到文字意见 → 修改计划 → 第 2 张卡收到【是】 → 执行。若第 1 张卡直接收到【是】,则跳过修改直接执行。
|
|
130
|
+
> Add-Type 分支:若 Add-Type 失败(如只读/受限会话) → 立即停止,报告「无法使用回收站,未执行任何删除」,禁止任何永久删除,所有条目保持原样。
|
|
131
|
+
> 删除确认分支:执行命令若返回「用户拒绝了删除命令」 → 向用户报告「删除未执行」并停止,不得重试、不得换命令绕过(本示例中用户允许,全部执行成功)。
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// delete-gate 集成测试(模拟 dsh-tools 运行时契约 + serviceAsk 结果映射 + 完全权限直通)
|
|
2
|
+
// 契约依据(dsh-tools lib/index.js):
|
|
3
|
+
// pre-execute = waterfall("tools/pre-execute", exec, () => ({kind:"allow"}))
|
|
4
|
+
// 结果 {kind:"ask"} → serviceAsk 调 ctx.get("approval").request({agent, toolName, callId?, reason?})
|
|
5
|
+
// 映射:allowed-once→allow / rejected→deny / cancelled→deny+approvalCancelled
|
|
6
|
+
// unavailable→deny / 无审批服务→deny / 无 agent→deny
|
|
7
|
+
// sandbox/mode 事件(data.mode)决定完全权限直通(danger-full-access → 插件不 ask)
|
|
8
|
+
// 运行:node test\integration.test.mjs
|
|
9
|
+
import assert from "node:assert/strict"
|
|
10
|
+
|
|
11
|
+
const plugin = (await import(new URL("../lib/index.js", import.meta.url))).default
|
|
12
|
+
|
|
13
|
+
let passed = 0
|
|
14
|
+
let failed = 0
|
|
15
|
+
const pending = []
|
|
16
|
+
function check(name, fn) {
|
|
17
|
+
pending.push((async () => {
|
|
18
|
+
try {
|
|
19
|
+
await fn()
|
|
20
|
+
passed++
|
|
21
|
+
console.log("PASS ", name)
|
|
22
|
+
} catch (err) {
|
|
23
|
+
failed++
|
|
24
|
+
console.log("FAIL ", name, "→", err.message)
|
|
25
|
+
}
|
|
26
|
+
})())
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// —— 模拟运行时 ——
|
|
30
|
+
function makeRuntime() {
|
|
31
|
+
const state = {
|
|
32
|
+
approval: {
|
|
33
|
+
requested: null,
|
|
34
|
+
async request(req) {
|
|
35
|
+
this.requested = req
|
|
36
|
+
return this.outcome ?? "allowed-once"
|
|
37
|
+
},
|
|
38
|
+
outcome: "allowed-once",
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
let handler
|
|
42
|
+
let sessionHandler
|
|
43
|
+
const ctx = {
|
|
44
|
+
on(name, fn) {
|
|
45
|
+
if (name === "tools/pre-execute") handler = fn
|
|
46
|
+
else if (name === "session/event") sessionHandler = fn
|
|
47
|
+
},
|
|
48
|
+
get(service) {
|
|
49
|
+
return service === "approval" ? state.approval : undefined
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
plugin.apply(ctx)
|
|
53
|
+
|
|
54
|
+
async function preExecute(exec) {
|
|
55
|
+
// waterfall 语义:handler 返回的决策;默认允许
|
|
56
|
+
return await handler(exec, async () => ({ kind: "allow" }))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function serviceAsk(exec, decision) {
|
|
60
|
+
if (decision.kind !== "ask") return decision
|
|
61
|
+
const agent = exec.agent
|
|
62
|
+
if (agent === void 0) return { kind: "deny", reason: "no agent" }
|
|
63
|
+
const svc = ctx.get("approval")
|
|
64
|
+
if (!svc) return { kind: "deny", reason: "no approval channel" }
|
|
65
|
+
const outcome = await svc.request({
|
|
66
|
+
agent,
|
|
67
|
+
toolName: exec.name,
|
|
68
|
+
reason: decision.reason,
|
|
69
|
+
})
|
|
70
|
+
switch (outcome) {
|
|
71
|
+
case "allowed-once":
|
|
72
|
+
return { kind: "allow" }
|
|
73
|
+
case "rejected":
|
|
74
|
+
return { kind: "deny", reason: `the user rejected tool "${exec.name}"` }
|
|
75
|
+
case "cancelled":
|
|
76
|
+
return { kind: "deny", approvalCancelled: true }
|
|
77
|
+
case "unavailable":
|
|
78
|
+
return { kind: "deny", reason: "no approval channel" }
|
|
79
|
+
default:
|
|
80
|
+
return { kind: "deny", reason: "no approval channel" }
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function setSandboxMode(mode) {
|
|
85
|
+
const session = { id: "s-mode", snapshotEvents: () => [] }
|
|
86
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode } })
|
|
87
|
+
return session
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { handler, sessionHandler, state, preExecute, serviceAsk, setSandboxMode }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const exec = { name: "pwsh", arguments: { command: "Remove-Item x.txt" }, agent: { session: { id: "s0", snapshotEvents: () => [] } } }
|
|
94
|
+
|
|
95
|
+
// —— 决策 ——
|
|
96
|
+
check("1 删除命令 → 决策 kind=ask", async () => {
|
|
97
|
+
const r = makeRuntime()
|
|
98
|
+
const d = await r.preExecute(exec)
|
|
99
|
+
assert.equal(d.kind, "ask")
|
|
100
|
+
assert.ok(d.reason.includes("Remove-Item x.txt"))
|
|
101
|
+
})
|
|
102
|
+
check("2 非删除命令 → 默认 allow(不请求审批)", async () => {
|
|
103
|
+
const r = makeRuntime()
|
|
104
|
+
const d = await r.preExecute({ ...exec, arguments: { command: "Get-ChildItem" } })
|
|
105
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
106
|
+
assert.equal(r.state.approval.requested, null)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
// —— v4:完全权限直通 ——
|
|
110
|
+
check("3 danger-full-access + 删除 → 直通 allow(无审批请求)", async () => {
|
|
111
|
+
const r = makeRuntime()
|
|
112
|
+
const session = await r.setSandboxMode("danger-full-access")
|
|
113
|
+
const d = await r.preExecute({ ...exec, agent: { session } })
|
|
114
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
115
|
+
assert.equal(r.state.approval.requested, null)
|
|
116
|
+
})
|
|
117
|
+
check("4 workspace-write + 删除 → ask", async () => {
|
|
118
|
+
const r = makeRuntime()
|
|
119
|
+
const session = await r.setSandboxMode("workspace-write")
|
|
120
|
+
const d = await r.preExecute({ ...exec, agent: { session } })
|
|
121
|
+
assert.equal(d.kind, "ask")
|
|
122
|
+
})
|
|
123
|
+
check("5 快照回读 danger-full-access → 直通", async () => {
|
|
124
|
+
const r = makeRuntime()
|
|
125
|
+
const session = {
|
|
126
|
+
id: "s-snap",
|
|
127
|
+
snapshotEvents: () => [{ type: "sandbox/mode", data: { mode: "danger-full-access" } }],
|
|
128
|
+
}
|
|
129
|
+
const d = await r.preExecute({ ...exec, agent: { session } })
|
|
130
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// —— serviceAsk 映射 ——
|
|
134
|
+
check("6 allowed-once → 放行", async () => {
|
|
135
|
+
const r = makeRuntime()
|
|
136
|
+
r.state.approval.outcome = "allowed-once"
|
|
137
|
+
const d = await r.preExecute(exec)
|
|
138
|
+
const f = await r.serviceAsk(exec, d)
|
|
139
|
+
assert.deepEqual(f, { kind: "allow" })
|
|
140
|
+
})
|
|
141
|
+
check("7 rejected → deny(错误文案)", async () => {
|
|
142
|
+
const r = makeRuntime()
|
|
143
|
+
r.state.approval.outcome = "rejected"
|
|
144
|
+
const d = await r.preExecute(exec)
|
|
145
|
+
const f = await r.serviceAsk(exec, d)
|
|
146
|
+
assert.equal(f.kind, "deny")
|
|
147
|
+
assert.equal(f.reason, 'the user rejected tool "pwsh"')
|
|
148
|
+
})
|
|
149
|
+
check("8 cancelled → deny + approvalCancelled", async () => {
|
|
150
|
+
const r = makeRuntime()
|
|
151
|
+
r.state.approval.outcome = "cancelled"
|
|
152
|
+
const d = await r.preExecute(exec)
|
|
153
|
+
const f = await r.serviceAsk(exec, d)
|
|
154
|
+
assert.equal(f.kind, "deny")
|
|
155
|
+
assert.equal(f.approvalCancelled, true)
|
|
156
|
+
})
|
|
157
|
+
check("9 unavailable → deny", async () => {
|
|
158
|
+
const r = makeRuntime()
|
|
159
|
+
r.state.approval.outcome = "unavailable"
|
|
160
|
+
const d = await r.preExecute(exec)
|
|
161
|
+
const f = await r.serviceAsk(exec, d)
|
|
162
|
+
assert.equal(f.kind, "deny")
|
|
163
|
+
})
|
|
164
|
+
check("10 无审批服务 → deny", async () => {
|
|
165
|
+
const r = makeRuntime()
|
|
166
|
+
r.state.approval = undefined
|
|
167
|
+
const d = await r.preExecute(exec)
|
|
168
|
+
const f = await r.serviceAsk(exec, d)
|
|
169
|
+
assert.equal(f.kind, "deny")
|
|
170
|
+
})
|
|
171
|
+
check("11 无 agent → deny(runtime 层)", async () => {
|
|
172
|
+
const r = makeRuntime()
|
|
173
|
+
const d = await r.preExecute({ ...exec, agent: void 0 })
|
|
174
|
+
// 插件本身对无 agent 直接放行
|
|
175
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
176
|
+
const d2 = { kind: "ask", reason: "x" }
|
|
177
|
+
const f = await r.serviceAsk({ ...exec, agent: void 0 }, d2)
|
|
178
|
+
assert.equal(f.kind, "deny")
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
// —— 审批请求参数 ——
|
|
182
|
+
check("12 approval.request 收到 {agent, toolName, reason}", async () => {
|
|
183
|
+
const r = makeRuntime()
|
|
184
|
+
const d = await r.preExecute(exec)
|
|
185
|
+
await r.serviceAsk(exec, d)
|
|
186
|
+
assert.ok(r.state.approval.requested)
|
|
187
|
+
assert.equal(r.state.approval.requested.agent, exec.agent)
|
|
188
|
+
assert.equal(r.state.approval.requested.toolName, "pwsh")
|
|
189
|
+
assert.equal(r.state.approval.requested.reason, d.reason)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
// —— 卡片文案 ——
|
|
193
|
+
check("13 reason 无按钮语义赘述", async () => {
|
|
194
|
+
const r = makeRuntime()
|
|
195
|
+
const d = await r.preExecute(exec)
|
|
196
|
+
assert.ok(d.reason.includes("智能体请求执行以下删除类命令(工具 pwsh)"))
|
|
197
|
+
assert.ok(!d.reason.includes("仅当你点击"))
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
await Promise.all(pending)
|
|
201
|
+
|
|
202
|
+
console.log(`\n${passed} passed, ${failed} failed`)
|
|
203
|
+
if (failed > 0) process.exit(1)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// delete-gate 单元测试(官方 {kind:"ask"} 审批路线 + 完全权限直通)
|
|
2
|
+
// 运行:node test\unit.test.mjs
|
|
3
|
+
import assert from "node:assert/strict"
|
|
4
|
+
|
|
5
|
+
const plugin = (await import(new URL("../lib/index.js", import.meta.url))).default
|
|
6
|
+
|
|
7
|
+
let passed = 0
|
|
8
|
+
let failed = 0
|
|
9
|
+
const pending = []
|
|
10
|
+
function check(name, fn) {
|
|
11
|
+
pending.push((async () => {
|
|
12
|
+
try {
|
|
13
|
+
await fn()
|
|
14
|
+
passed++
|
|
15
|
+
console.log("PASS ", name)
|
|
16
|
+
} catch (err) {
|
|
17
|
+
failed++
|
|
18
|
+
console.log("FAIL ", name, "→", err.message)
|
|
19
|
+
}
|
|
20
|
+
})())
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
assert.equal(plugin.name, "delete-gate")
|
|
24
|
+
assert.equal(typeof plugin.apply, "function")
|
|
25
|
+
assert.equal(plugin.inject, undefined) // v4:无注入依赖
|
|
26
|
+
|
|
27
|
+
let preHandler
|
|
28
|
+
let sessionHandler
|
|
29
|
+
const ctx = {
|
|
30
|
+
on(name, fn, opts) {
|
|
31
|
+
if (name === "tools/pre-execute") {
|
|
32
|
+
preHandler = fn
|
|
33
|
+
assert.equal(opts.prepend, true)
|
|
34
|
+
} else if (name === "session/event") {
|
|
35
|
+
sessionHandler = fn
|
|
36
|
+
assert.equal(opts.global, true)
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error("unexpected event: " + name)
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
plugin.apply(ctx)
|
|
43
|
+
assert.ok(preHandler, "pre-execute handler 已注册")
|
|
44
|
+
assert.ok(sessionHandler, "session/event handler 已注册")
|
|
45
|
+
|
|
46
|
+
const next = async () => ({ kind: "allow" })
|
|
47
|
+
|
|
48
|
+
// 默认会话:无沙箱事件(mode undefined → fail closed → ask)
|
|
49
|
+
const plainSession = () => ({ id: "s-plain", snapshotEvents: () => [] })
|
|
50
|
+
// 指定快照的会话
|
|
51
|
+
const sessionOf = (events) => ({ id: "s-" + Math.random(), snapshotEvents: () => events })
|
|
52
|
+
|
|
53
|
+
function execOf(name, command, agent = {}) {
|
|
54
|
+
return { name, arguments: { command }, agent }
|
|
55
|
+
}
|
|
56
|
+
function execCode(name, code, agent = {}) {
|
|
57
|
+
return { name, arguments: { code }, agent }
|
|
58
|
+
}
|
|
59
|
+
const agentWith = (session) => ({ session })
|
|
60
|
+
|
|
61
|
+
// —— 命中删除模式 → {kind:"ask"} 且 reason 含命中命令行 ——
|
|
62
|
+
check("1 pwsh Remove-Item → ask", async () => {
|
|
63
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item foo.txt -Force", agentWith(plainSession())), next)
|
|
64
|
+
assert.equal(d.kind, "ask")
|
|
65
|
+
assert.ok(d.reason.includes("Remove-Item foo.txt -Force"))
|
|
66
|
+
})
|
|
67
|
+
check("2 pwsh DeleteFile → ask", async () => {
|
|
68
|
+
const d = await preHandler(execOf("pwsh", "DeleteFile($p,'OnlyErrorDialogs','SendToRecycleBin')", agentWith(plainSession())), next)
|
|
69
|
+
assert.equal(d.kind, "ask")
|
|
70
|
+
})
|
|
71
|
+
check("3 pwsh DeleteDirectory → ask", async () => {
|
|
72
|
+
const d = await preHandler(execOf("pwsh", "DeleteDirectory($d,'OnlyErrorDialogs','SendToRecycleBin')", agentWith(plainSession())), next)
|
|
73
|
+
assert.equal(d.kind, "ask")
|
|
74
|
+
})
|
|
75
|
+
check("4 bash rm -rf → ask", async () => {
|
|
76
|
+
const d = await preHandler(execOf("bash", "rm -rf /tmp/x", agentWith(plainSession())), next)
|
|
77
|
+
assert.equal(d.kind, "ask")
|
|
78
|
+
})
|
|
79
|
+
check("5 bash git clean → ask", async () => {
|
|
80
|
+
const d = await preHandler(execOf("bash", "git clean -fd", agentWith(plainSession())), next)
|
|
81
|
+
assert.equal(d.kind, "ask")
|
|
82
|
+
})
|
|
83
|
+
check("6 pwsh Clear-RecycleBin → ask", async () => {
|
|
84
|
+
const d = await preHandler(execOf("pwsh", "Clear-RecycleBin -Force", agentWith(plainSession())), next)
|
|
85
|
+
assert.equal(d.kind, "ask")
|
|
86
|
+
})
|
|
87
|
+
check("7 pwsh [System.IO.File]::Delete → ask", async () => {
|
|
88
|
+
const d = await preHandler(execOf("pwsh", "[System.IO.File]::Delete('x.txt')", agentWith(plainSession())), next)
|
|
89
|
+
assert.equal(d.kind, "ask")
|
|
90
|
+
})
|
|
91
|
+
check("8 pwsh fs.rmSync → ask", async () => {
|
|
92
|
+
const d = await preHandler(execOf("pwsh", "node -e \"fs.rmSync('x',{force:true})\"", agentWith(plainSession())), next)
|
|
93
|
+
assert.equal(d.kind, "ask")
|
|
94
|
+
})
|
|
95
|
+
check("9 pwsh SendToRecycleBin → ask", async () => {
|
|
96
|
+
const d = await preHandler(execOf("pwsh", "[FileSystem]::DeleteFile('x','OnlyErrorDialogs','SendToRecycleBin')", agentWith(plainSession())), next)
|
|
97
|
+
assert.equal(d.kind, "ask")
|
|
98
|
+
})
|
|
99
|
+
check("10 分号行内 Remove-Item → ask", async () => {
|
|
100
|
+
const d = await preHandler(execOf("pwsh", "Set-Location ..; Remove-Item -Recurse tmp", agentWith(plainSession())), next)
|
|
101
|
+
assert.equal(d.kind, "ask")
|
|
102
|
+
})
|
|
103
|
+
check("11 pwsh DeleteFileA(Win32 API)→ ask", async () => {
|
|
104
|
+
const d = await preHandler(execOf("pwsh", "DeleteFileA('x')", agentWith(plainSession())), next)
|
|
105
|
+
assert.equal(d.kind, "ask")
|
|
106
|
+
})
|
|
107
|
+
check("12 run_code code: os.remove → ask", async () => {
|
|
108
|
+
const d = await preHandler(execCode("run_code", "import os; os.remove('x')", agentWith(plainSession())), next)
|
|
109
|
+
assert.equal(d.kind, "ask")
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
// —— 不应请求审批 → 走 next() ——
|
|
113
|
+
check("13 非删除命令 → 放行", async () => {
|
|
114
|
+
const d = await preHandler(execOf("pwsh", "Get-ChildItem -Force", agentWith(plainSession())), next)
|
|
115
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
116
|
+
})
|
|
117
|
+
check("14 Remove-ItemProperty → 放行", async () => {
|
|
118
|
+
const d = await preHandler(execOf("pwsh", "Remove-ItemProperty -Path HKCU:\\x -Name y", agentWith(plainSession())), next)
|
|
119
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
120
|
+
})
|
|
121
|
+
check("15 NotDeleteFile → 放行", async () => {
|
|
122
|
+
const d = await preHandler(execOf("pwsh", "NotDeleteFile('x')", agentWith(plainSession())), next)
|
|
123
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
124
|
+
})
|
|
125
|
+
check("16 format → 放行", async () => {
|
|
126
|
+
const d = await preHandler(execOf("pwsh", "format C:", agentWith(plainSession())), next)
|
|
127
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
128
|
+
})
|
|
129
|
+
check("17 非执行类工具 → 放行", async () => {
|
|
130
|
+
const d = await preHandler({ name: "read", arguments: { command: "rm -rf /" }, agent: agentWith(plainSession()) }, next)
|
|
131
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
132
|
+
})
|
|
133
|
+
check("18 agent 缺失 → 放行", async () => {
|
|
134
|
+
const d = await preHandler({ name: "pwsh", arguments: { command: "Remove-Item x" } }, next)
|
|
135
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// —— reason 文本 ——
|
|
139
|
+
check("19 reason 含引导语且不含按钮语义", async () => {
|
|
140
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(plainSession())), next)
|
|
141
|
+
assert.ok(d.reason.includes("智能体请求执行以下删除类命令(工具 pwsh)"))
|
|
142
|
+
assert.ok(!d.reason.includes("允许一次"))
|
|
143
|
+
assert.ok(!d.reason.includes("拒绝"))
|
|
144
|
+
})
|
|
145
|
+
check("20 超 8 行截断 + 尾部计数", async () => {
|
|
146
|
+
const lines = Array.from({ length: 12 }, (_, i) => `Remove-Item f${i}.txt`).join("\n")
|
|
147
|
+
const d = await preHandler(execOf("pwsh", lines, agentWith(plainSession())), next)
|
|
148
|
+
assert.ok(d.reason.includes("还有 4 处删除类命令"))
|
|
149
|
+
assert.equal((d.reason.match(/•/g) || []).length, 8)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
// —— 沙箱策略 ——
|
|
153
|
+
check("21 live map: danger-full-access + 删除 → 直通", async () => {
|
|
154
|
+
const session = { id: "s-full", snapshotEvents: () => [] }
|
|
155
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode: "danger-full-access" } })
|
|
156
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(session)), next)
|
|
157
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
158
|
+
})
|
|
159
|
+
check("22 live map: workspace-write + 删除 → ask", async () => {
|
|
160
|
+
const session = { id: "s-ww", snapshotEvents: () => [] }
|
|
161
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode: "workspace-write" } })
|
|
162
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(session)), next)
|
|
163
|
+
assert.equal(d.kind, "ask")
|
|
164
|
+
})
|
|
165
|
+
check("23 live map: read-only + 删除 → ask", async () => {
|
|
166
|
+
const session = { id: "s-ro", snapshotEvents: () => [] }
|
|
167
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode: "read-only" } })
|
|
168
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(session)), next)
|
|
169
|
+
assert.equal(d.kind, "ask")
|
|
170
|
+
})
|
|
171
|
+
check("24 快照回读: danger-full-access → 直通", async () => {
|
|
172
|
+
const session = sessionOf([
|
|
173
|
+
{ type: "sandbox/mode", data: { mode: "workspace-write" } },
|
|
174
|
+
{ type: "sandbox/mode", data: { mode: "danger-full-access" } },
|
|
175
|
+
])
|
|
176
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(session)), next)
|
|
177
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
178
|
+
})
|
|
179
|
+
check("25 live map 优先于快照(快照 full,live workspace-write → ask)", async () => {
|
|
180
|
+
const session = sessionOf([{ type: "sandbox/mode", data: { mode: "danger-full-access" } }])
|
|
181
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode: "workspace-write" } })
|
|
182
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith(session)), next)
|
|
183
|
+
assert.equal(d.kind, "ask")
|
|
184
|
+
})
|
|
185
|
+
check("26 会话无快照方法且无 live 记录 → fail closed ask", async () => {
|
|
186
|
+
const d = await preHandler(execOf("pwsh", "Remove-Item x", agentWith({ id: "s-bare" })), next)
|
|
187
|
+
assert.equal(d.kind, "ask")
|
|
188
|
+
})
|
|
189
|
+
check("27 完全权限 + 非删除命令 → 放行", async () => {
|
|
190
|
+
const session = { id: "s-full2", snapshotEvents: () => [] }
|
|
191
|
+
await sessionHandler(session, { type: "sandbox/mode", data: { mode: "danger-full-access" } })
|
|
192
|
+
const d = await preHandler(execOf("pwsh", "Get-ChildItem", agentWith(session)), next)
|
|
193
|
+
assert.deepEqual(d, { kind: "allow" })
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
await Promise.all(pending)
|
|
197
|
+
|
|
198
|
+
console.log(`\n${passed} passed, ${failed} failed`)
|
|
199
|
+
if (failed > 0) process.exit(1)
|