delete-guard-agent 0.0.0-stage → 0.5.0
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 +207 -2
- package/SKILL.md +143 -0
- package/hooks/pretooluse.py +147 -0
- package/package.json +33 -4
- package/pyrightconfig.json +6 -0
- package/scripts/delete_guard/__init__.py +62 -0
- package/scripts/delete_guard/cli.py +88 -0
- package/scripts/delete_guard/core.py +924 -0
- package/scripts/delete_guard/paths.py +329 -0
- package/scripts/delete_guard/platform.py +66 -0
- package/scripts/delete_guard/rules/__init__.py +43 -0
- package/scripts/delete_guard/rules/common.py +60 -0
- package/scripts/delete_guard/rules/linux.py +23 -0
- package/scripts/delete_guard/rules/macos.py +15 -0
- package/scripts/delete_guard/rules/windows.py +32 -0
- package/scripts/guard.py +71 -0
- package/src/extension.ts +136 -0
package/src/extension.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* blast-radius-guard pi extension(原 windows-delete-guard)
|
|
3
|
+
*
|
|
4
|
+
* 在 bash / powershell 工具调用执行之前,把命令交给同仓库的判定器
|
|
5
|
+
* (scripts/guard.py -> delete_guard 包,Windows / Linux / macOS 通用):
|
|
6
|
+
* verdict=block -> { block: true, reason: 带事故编号的理由 + 改写建议 }
|
|
7
|
+
* verdict=warn -> 放行(不弹窗、不打断;理由由模型侧的 skill 负责)
|
|
8
|
+
* verdict=allow -> 放行
|
|
9
|
+
*
|
|
10
|
+
* 设计取向:
|
|
11
|
+
* - fail-open:guard.py 缺失/超时/非零异常时放行,只在 reason 缺位时报一次。
|
|
12
|
+
* 安全工具被误拦逼到禁用,比偶尔漏拦更糟。
|
|
13
|
+
* - 只拦不问:不打断用户(不调用 ctx.ui),block 的 reason 会回到模型,
|
|
14
|
+
* 由模型按 reason 里的建议改写命令后重试。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { execFile } from "node:child_process";
|
|
18
|
+
import { existsSync } from "node:fs";
|
|
19
|
+
import { dirname, join } from "node:path";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
22
|
+
|
|
23
|
+
const GUARD_TIMEOUT_MS = 5_000;
|
|
24
|
+
|
|
25
|
+
function guardScript(): string | null {
|
|
26
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
27
|
+
const p = join(here, "..", "scripts", "guard.py");
|
|
28
|
+
return existsSync(p) ? p : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Windows 原生通常只有 `python`(`python3` 常是 Microsoft Store 假别名桩),
|
|
33
|
+
* WSL/Linux/macOS 通常是 `python3`。按序探测第一个可用的;
|
|
34
|
+
* 环境变量 WDG_PYTHON 可覆盖。
|
|
35
|
+
*/
|
|
36
|
+
let pythonResolved: string[] | null | undefined;
|
|
37
|
+
async function resolvePython(): Promise<string[] | null> {
|
|
38
|
+
if (process.env.WDG_PYTHON) return [process.env.WDG_PYTHON];
|
|
39
|
+
if (pythonResolved !== undefined) return pythonResolved;
|
|
40
|
+
pythonResolved = null;
|
|
41
|
+
for (const cmd of ["python3", "python", "py"]) {
|
|
42
|
+
const candidate = cmd === "py" ? [cmd, "-3"] : [cmd];
|
|
43
|
+
if (await probe(candidate)) {
|
|
44
|
+
pythonResolved = candidate;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return pythonResolved;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function probe(cmd: string[]): Promise<boolean> {
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
execFile(cmd[0], cmd.slice(1).concat(["-c", "pass"]), { timeout: 3_000 }, (err) => {
|
|
54
|
+
resolve(!err);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function runGuard(
|
|
60
|
+
python: string[],
|
|
61
|
+
script: string,
|
|
62
|
+
command: string,
|
|
63
|
+
): Promise<{ verdict: string; text: string } | null> {
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
execFile(
|
|
66
|
+
python[0],
|
|
67
|
+
python.slice(1).concat([script, "command", command, "--json"]),
|
|
68
|
+
{ timeout: GUARD_TIMEOUT_MS, maxBuffer: 1024 * 1024 },
|
|
69
|
+
(err, stdout) => {
|
|
70
|
+
if (err && !stdout) {
|
|
71
|
+
resolve(null); // fail-open
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const parsed = JSON.parse(stdout.toString());
|
|
76
|
+
resolve({ verdict: parsed.verdict ?? "allow", text: stdout.toString() });
|
|
77
|
+
} catch {
|
|
78
|
+
resolve(null);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface InputLike {
|
|
86
|
+
command?: unknown;
|
|
87
|
+
[key: string]: unknown;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default function (pi: ExtensionAPI) {
|
|
91
|
+
const script = guardScript();
|
|
92
|
+
|
|
93
|
+
pi.on("tool_call", async (event) => {
|
|
94
|
+
if (!script) return undefined;
|
|
95
|
+
if (event.toolName !== "bash" && event.toolName !== "powershell") {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const input = event.input as InputLike | undefined;
|
|
99
|
+
const command = typeof input?.command === "string" ? input.command : undefined;
|
|
100
|
+
if (!command || !command.trim()) return undefined;
|
|
101
|
+
|
|
102
|
+
// 快速预筛:不含删除/移动动词时不启动子进程(省掉 python 探测和 ~50ms)
|
|
103
|
+
if (!/\b(rm|rmdir|del|erase|rd|unlink|shred|trash|remove-item|ri|move-item|move|mv)\b/i.test(command)) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const python = await resolvePython();
|
|
108
|
+
if (!python) return undefined; // fail-open:环境里没有可用 Python
|
|
109
|
+
|
|
110
|
+
const result = await runGuard(python, script, command);
|
|
111
|
+
if (!result) return undefined;
|
|
112
|
+
|
|
113
|
+
if (result.verdict === "block") {
|
|
114
|
+
const reasons = (() => {
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(result.text);
|
|
117
|
+
return (parsed.hazards ?? [])
|
|
118
|
+
.map((h: { code: string; evidence: string }) => `${h.code}(依据: ${h.evidence})`)
|
|
119
|
+
.join("; ");
|
|
120
|
+
} catch {
|
|
121
|
+
return result.text.slice(0, 400);
|
|
122
|
+
}
|
|
123
|
+
})();
|
|
124
|
+
return {
|
|
125
|
+
block: true,
|
|
126
|
+
reason:
|
|
127
|
+
`blast-radius-guard 拒绝执行该删除命令:${reasons}。` +
|
|
128
|
+
"先改写成单层 shell、绝对路径的目标(Windows 用 -LiteralPath);" +
|
|
129
|
+
"或先 rename 进隔离目录(Windows: %USERPROFILE%\\.delete-quarantine\\<date>;" +
|
|
130
|
+
"POSIX: ~/.delete-quarantine/<date>)观察后再删;" +
|
|
131
|
+
"不要把算好的路径管道进 cmd /c 或嵌套引号链。完整判定输出已写入扩展日志。",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
});
|
|
136
|
+
}
|