/** * 永久删除守卫:把「绕过回收站的删除」从 shell 里挡回去,逼模型改用可恢复的 Trash。 * * ⚠️ 可靠性边界(与 src/irreversible/rules.ts 同一标准,别高估它): * 这是**防模型误删**的护栏,不是对抗性沙盒。判据是对命令文本做正则匹配, * 因此下列写法一律漏过,且**有意不追**(越追越像军备竞赛,误伤率先爆): * · 绝对路径调用 /bin/rm -rf x * · 间接执行 xargs rm / find -exec rm / echo $(rm x) * · 换语言 python -c "import shutil; shutil.rmtree('x')" * · 跨 shell 转发 cmd 里 powershell -c "Remove-Item x" * 真正兜底的是「模型没有恶意」+ Trash 提供了更顺手的替代路径,不是这张正则表。 * * ⚠️ `shell` 必须是**实际执行命令的那个 shell**,不是调用它的工具名。 * Bash 工具在 win32 找不到 Git Bash 时会回落 cmd.exe(见 tools/bash/bash.ts 的 * WIN_BASH 探测,且它的 description 会主动告诉模型「这里是 cmd」),那时模型写的是 * cmd 语法(del / rd),若仍按 'bash' 匹配 —— 三层防护全部落空、文件直接永久丢失。 */ /** POSIX shell:删除动词在命令位置(串首或 ; & | 换行之后)。 */ const BASH_DELETE = [ /(?:^|[;&|\n])\s*(?:sudo\s+)?(?:rm|unlink|rmdir)\b/i, /(?:^|[;&|\n])\s*find\b[^\n;&|]*\s-delete\b/i, ]; /** * cmd.exe:del / erase 删文件,rd / rmdir 删目录,一律不进回收站。 * 分隔符**必须含 `&`** —— cmd 用 `&` / `&&` 串命令,这正是 POWERSHELL_DELETE * 的分隔符类 `[;|\n]` 不能直接复用的原因。 * `rm` 一并收进来:Git 的 usr\bin 可能在 PATH 上(此时 rm.exe 可用),而 cmd 本身没有 rm。 */ const CMD_DELETE = [/(?:^|[;&|\n])\s*(?:del|erase|rd|rmdir|rm)\b/i]; const GIT_CLEAN = /(?:^|[;&|\n])\s*git\b[^\n;&|]*\bclean\b([^\n;&|]*)/i; const POWERSHELL_DELETE = [ /(?:^|[;|\n])\s*(?:Remove-Item|Clear-RecycleBin)\b/i, /(?:^|[;|\n])\s*(?:del|erase|rd|rmdir|rm|ri)\b/i, /\.Delete\s*\(/i, ]; export type ShellKind = 'bash' | 'cmd' | 'powershell'; const RULES: Readonly> = { bash: BASH_DELETE, cmd: CMD_DELETE, powershell: POWERSHELL_DELETE, }; export function permanentDeleteReason( command: string, shell: ShellKind, ): string | undefined { let hit = RULES[shell].some((pattern) => pattern.test(command)); // `git clean -f` 三种 shell 里都能跑、都永久删除,所以不按 shell 分支。 // `-n` / `--dry-run` 是只读预演,必须放行(否则模型连"先看看会删什么"都做不到)。 if (!hit) { const gitClean = GIT_CLEAN.exec(command); const options = gitClean?.[1] ?? ''; const dryRun = /--dry-run\b/.test(options) || /(?:^|\s)-[a-z]*n[a-z]*(?:\s|$)/i.test(options); hit = Boolean( gitClean && !dryRun && /(?:^|\s)-[a-z]*f[a-z]*(?:\s|$)|--force\b/i.test(options), ); } return hit ? '该命令会绕过系统回收站永久删除文件。请改用 Trash;清理 Git 未跟踪文件请用 Trash(action="git-clean")。' : undefined; }