feat(jarvis): 本地沙箱改多目录白名单 + 让它知道自己能操作这台电脑

单目录沙箱做不了"操作我电脑"——只能在一个文件夹里打转,跨目录整理直接没戏。
且模型压根不知道 shell 除了跑脚本还能开 App、控 App、触发快捷指令。

沙箱:单根 → 多根白名单
- 用户授权多个目录(设置里每行一个,可一键填入桌面/下载/文档),其余一律拒
- 路径改绝对路径(多根之下相对路径没有唯一含义),支持 ~ 展开,相对路径兜底按首个根解释
- local_list_dir 留空 path = 返回授权目录清单 → 模型据此自己发现"我能访问哪儿"
- local_exec 可指定 cwd(须在授权目录内)
- 防逃逸不变:软链解析后必须落在某个根内,越界即拒(单测覆盖 ../ 与软链逃逸)

告诉模型它能干什么
- local_exec 描述展开:文件整理(mv/cp/find)、mdfind 全盘搜、open 开应用/文件/网址、
  osascript 控制 Mac App、shortcuts run/list 触发快捷指令、系统信息
- 语音系统提示词把 JARVIS 定位成"这台电脑的操作者",并要求先想清用哪个工具再动手

live 验证(两个授权目录):
① "你能访问哪些目录,里面有什么" → 自主先查授权清单、再逐个列举,答全对
② "把下载里的图片挪到桌面" → 自主 mv,文件真的跨目录移动了

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-25 15:42:58 +08:00
parent 149c4dc89a
commit a16229573b
10 changed files with 255 additions and 96 deletions
+17 -5
View File
@@ -131,7 +131,7 @@ func askApproval(title, detail string) bool {
}
// execWrite 写文件(沙箱内 + 审批)。
func execWrite(root string, req *runnerReq) *runnerResp {
func execWrite(roots []string, req *runnerReq) *runnerResp {
fail := func(m string) *runnerResp { return &runnerResp{ID: req.ID, OK: false, Error: m} }
if !execAllowed() {
return fail("用户未开启「允许执行命令与写文件」。请如实告知用户需要在 JARVIS 设置里打开。")
@@ -141,7 +141,7 @@ func execWrite(root string, req *runnerReq) *runnerResp {
if strings.TrimSpace(rel) == "" {
return fail("缺少文件路径")
}
p, err := resolveInRoot(root, rel)
p, err := resolveAllowed(roots, rel)
if err != nil {
return fail(err.Error())
}
@@ -162,7 +162,7 @@ func execWrite(root string, req *runnerReq) *runnerResp {
}
// execCommand 在沙箱工作目录里跑一条命令(黑名单 + 审批 + 超时 + 输出截断)。
func execCommand(root string, req *runnerReq) *runnerResp {
func execCommand(roots []string, req *runnerReq) *runnerResp {
fail := func(m string) *runnerResp { return &runnerResp{ID: req.ID, OK: false, Error: m} }
if !execAllowed() {
return fail("用户未开启「允许执行命令与写文件」。请如实告知用户需要在 JARVIS 设置里打开。")
@@ -175,14 +175,26 @@ func execCommand(root string, req *runnerReq) *runnerResp {
if reason := checkDenied(cmdStr); reason != "" {
return fail(reason) // 硬拒:连审批框都不弹
}
if !askApproval("JARVIS 想在你的电脑上执行命令", "工作目录:"+root+"\n\n命令:\n"+cmdStr) {
cwd := ""
if len(roots) > 0 {
cwd = roots[0]
}
// 模型可指定在哪个授权目录里跑(不给就用第一个);越界目录直接拒。
if d, _ := req.Args["cwd"].(string); strings.TrimSpace(d) != "" {
resolved, rerr := resolveAllowed(roots, d)
if rerr != nil {
return fail(rerr.Error())
}
cwd = resolved
}
if !askApproval("JARVIS 想在你的电脑上执行命令", "工作目录:"+cwd+"\n\n命令:\n"+cmdStr) {
return fail("用户拒绝了这次执行。")
}
ctx, cancel := context.WithTimeout(context.Background(), execTimeout)
defer cancel()
c := exec.CommandContext(ctx, "/bin/sh", "-c", cmdStr)
c.Dir = root // 工作目录锁在沙箱根
c.Dir = cwd // 工作目录锁在授权目录内
out, err := c.CombinedOutput()
text := string(out)
if len(text) > execMaxOutput {