Skip to content

fix(sandbox): scratch 预检本机能力,启动失败时说清原因和修法 - #2545

Merged
deepcoldy merged 8 commits into
masterfrom
fix/scratch-capability-precheck
Oct 30, 2026
Merged

deepcoldy merged 8 commits into
masterfrom
fix/scratch-capability-precheck

Conversation

@deepcoldy

@deepcoldy deepcoldy commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

改了什么

起因是用户反馈:scratch 文件沙盒在非 root 的 daemon 上用默认的 tmpfs 存储时必然失败,Dashboard 不预先提示,会话失败时只给一句笼统的「start a new session」。维护者定的方向(决策看板 #84,方案 A):非 root 下 tmpfs 仍然失败、不自动降级,但要提前告诉用户「这台机器只能用 disk」,失败时说清楚原因和修法。

  1. 只读能力探测(新文件 src/adapters/backend/scratch-capability.ts)。daemon 侧检查 uid、PATH 里的 bwrap / fuse-overlayfs / fusermount3、/dev/fuse 能否以读写打开(打开后立刻关)、/proc/filesystems 里有没有 overlay、user namespace 相关 sysctl(user.max_user_namespaces、kernel.unprivileged_userns_clone、AppArmor 的 apparmor_restrict_unprivileged_userns)、bwrap 的 setuid 位、/proc/self/status 的 NoNewPrivs。不挂载、不起 bwrap、不安装任何东西。给出 tmpfs、disk 各自可用 / 不可用 / 无法确定,以及缺的包和安装命令。读不出结论的项只标「无法确定」,不按失败算。

    • NoNewPrivs 这一项是验证时发现的:daemon 带着 NoNewPrivileges(比如 systemd 的 NoNewPrivileges=yes)时,setuid 的 fusermount3 拿不到权限,非 root 的 disk 存储也起不来。实测报错是 fusermount3: mount failed: Operation not permitted。
  2. Dashboard 提示:Bot 配置 → 权限与安全 → 文件沙盒,选中 scratch 时按探测结果显示:

    • 「这台机器不能用 scratch:<原因>」
    • 「这台机器只能用磁盘存储:<原因>」;当前选的是内存存储时,再补一句「新会话会启动失败,请改成磁盘」
    • 缺依赖时给出安装命令;daemon 非 root 时说明「根文件系统以外的挂载盘在沙盒里只读」。

    只提示,不阻止保存,默认值仍是 tmpfs。/api/bots 加了 scratchCapability 字段;老 daemon 不回这个字段,页面就不显示提示。

  3. 失败原因:新增 prepareScratchSandboxDetailed / prepareMacScratchSandboxDetailed,失败时返回原因枚举(18 种,SCRATCH_FAILURE_REASONS)和宿主细节(stderr 首行、路径、缺的包)。原来的 prepareScratchSandbox / prepareMacScratchSandbox 签名不变,仍是失败返回 null,供探针脚本和老测试用。worker 改调 Detailed 版,用 scratchFailureNotice 把原因翻成中文提示加修法发到话题里;只有「tmpfs 在机器重启后丢失」才建议新开会话,其余都给宿主或配置上的修法。原来十几处 console.error 照旧保留在日志里。

  4. 文档 docs/file-sandbox.md:修正「非 root 自动走 fuse-overlayfs,storage 强制为 disk 语义」这句,代码里从来没有强制。补了两张表:前置条件表(root / 非 root × tmpfs / disk,含 /dev/fuse、bwrap、fuse-overlayfs、fusermount3、NoNewPrivileges),以及每条 [scratch-sandbox] 日志对应的原因和修法。

影响面

  • 没开 scratch 的 bot:行为完全不变。探测只在生成 /api/bots 时执行,都是只读的文件和 sysctl 读取,外加打开一次 /dev/fuse。
  • 开了 scratch、本来能用的(root daemon):行为不变,Dashboard 不显示提示。
  • 开了 scratch、本来会失败的:照旧失败,只是话题里的错误文案从固定英文换成原因 + 修法。默认值没改,也没有自动降级。
  • 涉及模块:scratch-sandbox.ts(内部函数返回值从 boolean 改成失败原因,挂载、卸载、回滚逻辑没动;失败日志里的 stderr 改为取第一行,mount 会在真实错误后面追加一句 dmesg 提示)、scratch-sandbox-darwin.ts(同样只改返回值)、worker.ts 的 scratch 分支(只改失败时的文案)、dashboard-ipc-server.ts(新增一个只读字段)、Dashboard 文件沙盒区块。oncall 沙盒、off 模式、其它 CLI、tmux / pty 后端都不经过这些改动。
  • 和别的沙盒任务的交界:⑥(Dashboard 文案)如果也改文件沙盒区块或 docs/file-sandbox.md 的 scratch 一节,后合的一方同步 master 再改。

验证

  • bun run build 通过(含 tsc、scripts / test-mocks 类型检查、dashboard bundle)。
  • 相关测试:npx vitest run test/scratch-capability.test.ts test/scratch-capability-note.test.ts test/dashboard-i18n.test.ts test/dashboard-i18n-c5.test.ts test/dashboard-bot-payload.test.ts test/scratch-sandbox-unit.test.ts test/scratch-sandbox-darwin-profile.test.ts test/sandbox-mode.test.ts test/scratch-submount-plan.test.ts test/test-hygiene.test.ts:8 个文件、568 例通过(加上新增的两个文件,共 23 例)。bun test 跑新增的三个文件:26 pass。
  • 真挂载的用例(test/scratch-prepare-failure.test.ts)在 isolate-run --timeout 5m 里跑,每个用例再套私有挂载 + PID 命名空间(test/helpers/private-mount-ns.ts),挂载出不到宿主:
    • tmpfs 会话机器重启后再起,返回 tmpfs-upper-lost;
    • 已建立的 disk 会话改成 tmpfs,返回 storage-conflict 并带上原来的存储;
    • 以 nobody(uid 65534)身份运行:探测判 tmpfs 不可用,真去准备 tmpfs 也返回 tmpfs-needs-root;
    • 起真实 worker:话题里收到的错误正是 scratchFailureNotice 生成的原因 + 修法,不再是固定文案。
  • 端到端对照(isolate-run 内私有命名空间):
    • root:探测为 all,tmpfs 和 disk 真准备都成功;
    • nobody:探测为 none(tmpfs-needs-root;disk 因 NoNewPrivs 失败),真准备的结果是 tmpfs 报 must be superuser to use mount、disk 报 fusermount3: mount failed: Operation not permitted,与探测一致。
  • 变异 13 处,全部变红:mountSlot 不按 uid 区分、tmpfs 丢失报成别的原因、存储冲突不带原存储、worker 退回固定文案、非 root 不判 tmpfs 不可用、不看 NoNewPrivs、不查 fusermount、/dev/fuse 缺失不算问题、setuid bwrap 绕过 userns 的判断失效、存储冲突也建议新开会话、选 tmpfs 时不提示改磁盘、disk-only 一律只算警告、内核无 overlay 时 tmpfs 照样判可用。
  • 全量单测(isolate-run):本机以 root 跑测试,master 自身就有约 70 个文件因环境原因失败。本分支与同基线的 master 对比,多出的 11 个文件单独重跑,430 例全部通过(负载下偶发),没有本次改动引入的失败;全量以 CI 为准。
  • 测试结束后核对宿主:挂载表里没有本次夹具的挂载,也没有残留的 fuse-overlayfs 进程。

第二轮(首审 4 条 P3,已在本 PR 修)

  1. 只缺 fusermount3 时,安装命令补上 fuse3 包。
  2. 非 root 下本用户打不开 /dev/fuse(比如旧式 0660 root:root 的设备节点),但 setuid 的 fusermount3 可用、且没有 NoNewPrivs 时,改判为「无法确定」,不再直接判「不能用」。原因是 fusermount3 以 root 身份打开设备。没有 fusermount3 或有 NoNewPrivs 时仍判不可用。
  3. 结论为「只能用磁盘存储」或「都能用」时,如果还有无法确定的检查项(比如 AppArmor 限制),也显示「第一个会话会实际验证」那一行。
  4. bot-payload.ts 透传 scratchCapability 补了测试:正常透传;老 daemon 不回这个字段时为 null;非对象值丢弃。

变异 4 处(去掉 fuse3 安装提示、去掉「无法确定」降级、不显示待验证项、payload 不透传),全部变红。文档前置条件表同步改了 /dev/fuse 那一格。

截图(合成数据,隔离 daemon + Dashboard)

daemon 以 root 运行,所以截图时把 /api/bots 里的 scratchCapability 换成了真实 evaluateScratchCapability 对非 root 主机算出的结果。

非 root,当前选的是内存存储(提示改成磁盘):

非 root + 内存存储

非 root,当前选的是磁盘存储:

非 root + 磁盘存储

没有 /dev/fuse、也没装 fuse-overlayfs(不能用 scratch):

不能用 scratch

root 且依赖齐全时不显示提示。

🤖 Generated with Claude Code

DeepCold review and others added 3 commits October 30, 2026 02:52
- 新增只读能力探测(uid、bwrap / fuse-overlayfs / fusermount3、/dev/fuse、
  内核 overlay、user namespace sysctl、NoNewPrivs),不挂载、不起沙盒;
  Dashboard 选 scratch 时提示「这台机器不能用 scratch」或「只能用磁盘存储」,
  只提示、不阻止保存,默认值不变
- prepareScratchSandbox / prepareMacScratchSandbox 增加 Detailed 版本返回失败
  原因枚举,worker 翻成中文原因和修法;只有 tmpfs 在机器重启后丢失才建议新开会话
- docs/file-sandbox.md 修正非 root 的说法,补前置条件表与日志对照修法表

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

@deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 89a9cea…22d1c8d0b(20 个提交),同批合入的还有 #2546 #2556 #2589 #2591 #2538 #2595 #2596 #2597 #2581 #2539 #2598 #2515 #2536 #2575 #2366 #2606 #2544 #2593 #2603,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Kimi basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, the middle column displays a welcome message "欢迎加入群组" (Welcome to the group) and system notifications about group creation and settings, rather than a list o…
Gemini basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, the main content area displays a topic conversation. I can see messages from "Gemini".
Codex DM chat mode replies inline and keeps one session feishu-core-dm-modes-codex failed Feishu Messenger chat list did not load after 4 attempts (URL: https://acn2e4mfbjx2.feishu.cn/next/messenger/, title: 消息 - 飞书). A login redirect means the saved storage state expired; a skeleton list…

deepcoldy pushed a commit that referenced this pull request Oct 31, 2026
- 首审 P2:bytedcli-home / vc-meeting-daemon-auth / oauth-pending / cli-identity
  的固定遮挡不再受「数据目录存在」门控(自定义 SESSION_DATA_DIR 时
  ~/.botmux/data 可能在会话中途才由首次登录创建);补单测与真容器变体
- 首审 P3:docs-site 中英 sandbox.md 写清 scratch 不隔离凭据;顺带把已失效的
  scratch 启动失败报错说明更新为 #2545 之后的原因 + 修法

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@deepcoldy
deepcoldy deleted the fix/scratch-capability-precheck branch November 1, 2026 00:47
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

🚀 Released in v3.56.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant