Skip to content

fix(send): 收紧参数预检,阻止位置参数静默吞正文 - #2774

Merged
deepcoldy merged 14 commits into
deepcoldy:masterfrom
BanboLee:fix/send-arg-parsing
Nov 4, 2026
Merged

deepcoldy merged 14 commits into
deepcoldy:masterfrom
BanboLee:fix/send-arg-parsing

Conversation

@BanboLee

@BanboLee BanboLee commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

基线:0502c3c03(origin/master)

改了什么

botmux send 的正文/附件解析长期存在一类静默错误:多余位置参数会被当成正文,且位置正文优先于 stdin/heredoc;--files/--images 每次只吃 1 个值;- 不是 stdin 别名,会原样变成正文 -(Markdown 渲染成一个点)。本次把解析收敛到最早入口的共享 preflight,并补上确定性校验:

  • 新增 src/cli/send-invocation.ts:角色无关 parse + client/host 两个 validator + 完整 arity。
  • src/cli.ts:在 Riff/sandbox 分流、session 解析、marker/ledger 变更和任何 transport 之前 preflight;cmdSend/sendDryRun/relaySend 消费同一份 plan;stdin 在 preflight 读取并缓存,避免拒绝正文前先推进 queued turn。
  • 裸 - 仅作为唯一位置参数时读 stdin;空白 stdin 直接 exit 2。
  • 附件 flag 后多写的路径直接 exit 2 并提示重复写 --files/--images:额外 token 全部 stat 为真实普通文件,或判为 path-like(无空白/CJK/URL,且命中显式前缀 / ./ ../ ~/、盘符、单/双前导反斜杠,或以常见附件扩展名结尾)。显式前缀但带空白/CJK 的 token,仅在同时以常见扩展名结尾时按路径处理。附件-only 发送仍允许空正文。
  • 未知 flag、任意 occurrence 缺值、exact --、client 手写 riff-upload: 占位符都在最早入口拒绝。
  • image-mode / response-kind 的非法值在 stdin 读取前校验,参数错误不被“正文为空”覆盖。
  • Riff client 在上传/rewrite 前校验;main/dry-run/relay 共享同一个 ClientSendPlan,Riff client 从同一份缓存 stdinContent 重建 plan(防御性重校验,stdin 不二读);Riff host 只做 placeholder 结构 + uploads 双射,不 stat、不做位置文件启发式;host strict 不 trim。
  • src/services/riff-upload-placeholder.ts:^riff-upload:(0|[1-9]\d*)$ 单一来源。

为什么

线上出现三类用户可见事故:文件只发 path、只发一个点、只发一张图没有正文。根因都是参数解析把真实正文替换成了另一个路径/-,且不报错。本次把确定性类别改成响亮失败,避免 bot 在不知情时继续错发。

影响面

真实 send、--dry-run、sandbox relay、Riff client/host、remote-runner 合成 argv、scheduled turn、plugin card 均接入同一 preflight。CLI 行为变化;无 DB/持久状态迁移。

不包含(后续 PR):-- 分隔符完整支持、--attention/--urgent 空格形式报错、docs/i18n/dry-run 文案补齐。

验证

在独立 checkout(/tmp/botmux-verify,deps 完整;未使用 canonical node_modules)执行:

  • bunx tsc -p tsconfig.json --noEmit:exit 0
  • bun run build:通过(typecheck:scripts / test-mocks / dashboard bundle / asset audits)
  • 定向 send/riff 套件(15 files):388 tests passed
    bunx vitest run --project unit test/send-invocation.test.ts test/cli-send-dry-run.test.ts test/riff-send-preflight.test.ts test/send-preflight-transport.test.ts test/send-preflight-marker.test.ts test/send-policy.test.ts test/cli-send-dispatch.test.ts test/cli-send-image-mode.test.ts test/cli-send-diff.test.ts test/cli-send-custom-final.test.ts test/cli-send-reply-card.test.ts test/riff-host-tools.test.ts test/remote-runner-outbound-send.test.ts test/cli-send-link-ref.test.ts test/cli-root-help.test.ts
  • relay/sandbox 相关(4 files):24 tests passed
    bunx vitest run --project unit test/cli-send-deferred-topic-isolated.test.ts test/sandbox-send-route.test.ts test/send-attachment-message-ids-wiring.test.ts test/cli-send-topic-policy.test.ts

六条路径的覆盖:

路径 验证方式
实发 cmdSend test/cli-send-reply-card.test.ts + test/send-preflight-transport.test.ts(fixture fake HTTP,断言解析失败零 transport/零状态变化)
--dry-run test/cli-send-dry-run.test.ts(lone -、多附件、exact --、参数错误优先于空正文)
relay test/cli-send-deferred-topic-isolated.test.ts、test/cli-send-topic-policy.test.ts
Riff client test/riff-send-preflight.test.ts(上传/rewrite 前拒绝;缓存 stdin 只读一次)
Riff host test/riff-host-tools.test.ts(placeholder 严格匹配、uploads 双射、不 stat、不写入 upload 临时目录)
sandbox test/sandbox-send-route.test.ts、test/send-attachment-message-ids-wiring.test.ts
remote-runner test/remote-runner-outbound-send.test.ts

新增副作用回归:test/send-preflight-transport.test.ts 表驱动覆盖 blank lone - 与 blank 隐式 stdin,同时断言 marker 字节不变、ledger 文件名/字节不变、CAPTURE_HTTP 为空、sandbox relay 目录无 .req.json。test/send-preflight-marker.test.ts 另外单独锁 marker。Riff 侧新增计数测试:传入缓存 stdinContent 时底层 readStdin 调用 0 次,无缓存时调用 1 次。

全量 unit suite 实况:2195 passed / 35 failed files / 103 failed tests,失败集中在 workspace-recycle / traex 的临时路径期望;本 PR 不修改这些模块,CI 会再跑。

  • 最新 head 60867ad 复验:9 个 send/riff/relay 文件 146 passed / 0 failed;tsc --noEmit 0 错;bun run build 通过;real CLI dry-run 矩阵 11 放行 / 7 拒绝;三条变异各自咬红。

错误优先级:body/arity preflight 先于 transport/sandbox gate 报错(有意行为;gate 仍然是最终的发送拒绝者)。

已知边界

  • 「附件 flag 后的 positional 是误写路径还是正文」是启发式:无扩展名、无路径语法的裸 token(如 missing)仍按正文;含空格/CJK 且无常见扩展名的缺失路径也可能漏判。
  • 目录/断链/FIFO/socket/device 不走 allFiles(stat)分支,但命中显式路径前缀或常见附件扩展名时仍会拒绝。
  • 以显式路径前缀开头、且以常见扩展名结尾的正文会被 fail-closed 拒绝(如 ./build.sh 会生成 report.sql),这是与带空格文件名不可区分时的取舍。
  • Riff host 不做 stat,只负责 placeholder 结构与 uploads 双射;路径误写由 client preflight 与 spawned CLI 的同一份校验拦住。
  • exact -- 仍 blanket ban。

交叉评审后追加的提交(append 在作者原提交之后,未改写原提交)

  • 修复:botmux send --as independent|suggestion 单独发送(空 stdin,正文由 cmdSend 用 crossPrincipalAsKeyword 合成、嵌入 [botmux-as:v1:…])此前被新预检误判为「正文为空」(exit 2),现恢复可用;非法 --as 值在读取 stdin 之前报参数用法错误(非法值配合不闭合的非 TTY stdin 不再阻塞在 stdin 读取)。
  • docs/riff.md:Riff 工具通道的 send 正文只走 heredoc/管道 stdin,裸 - 位置参数会被判 riff_host_flag_unsupported:-;裸 - 作 stdin 同义仅普通本地 CLI 成立;card stream write --content-file - 仍保留。
  • client 拼装 riff 占位符改用统一常量 RIFF_UPLOAD_PREFIX,不再散落字面量。

有意行为变化(在上方清单基础上补充)

  • --as independent|suggestion 单独发送(无正文/无附件)恢复为合法输入,正文在下游合成;这是恢复 cmdSend 本就存在的能力(合法输入集相对本 PR 收紧阶段略放大,最终相对收紧前 master 为「恢复原有行为」)。
  • send --content-file - 在 client 预检直接拒绝(Riff 与本地一致),改用 heredoc/管道;card stream write --content-file - 不变。
  • exact -- 在 send 全域拒绝。

Banbo added 2 commits November 3, 2026 22:36
在 Riff/sandbox 分流和任何会话/relay/marker 变更之前统一解析 send argv:裸 - 作为 stdin,附件 flag 后多写的真实文件路径直接报错,未知 flag、任意 occurrence 缺值、exact -- 和手写 riff-upload 占位符一律提前拒绝。Riff host 只做 placeholder 结构与 uploads 双射校验,不做 stat 或位置文件启发式。

补充真实 send/dry-run/Riff 通道的回归测试,并让 fixture 可按需记录全部 HTTP 以便断言解析失败零 transport。
Banbo and others added 8 commits November 4, 2026 15:18
交叉复审发现:preflightSendInvocation 的空正文判定只放行了 --diff,
但 --as independent|suggestion 的正文本来就由 cmdSend 后续合成
(crossPrincipalAsKeyword),skill 也明确写了可单独 `botmux send --as
independent` 不带正文。收紧预检后这条用法被误杀为 exit 2「正文为空」。

- --as 与 --diff 并列作为「正文由后续阶段合成」的来源,空 stdin 放行
- 非法 --as 值在空正文判定之前报 as_usage,参数错误不被「正文为空」覆盖
- 补端到端回归:空 stdin 的 --as independent/suggestion 实发并断言合成
  token;非法值断言 as_usage 且零 CAPTURE
- 补预检顺序取舍注释与「virtual 会话 + 非空 stdin 仍被 transport 闸拒绝」
  的构建版行为测试
- RIFF_UPLOAD_PREFIX 交给 client 占位符拼装处使用,不再是无引用导出
- docs/riff.md:send 的 --content-file - 已在客户端预检拒绝,heredoc/
  管道裸 stdin 替代;card stream write 仍保留

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner

交叉复审后追加了一个修复提交 fbed88afe(fast-forward,未改动原有提交)。

修了什么

复审发现一个 P2:新的 send 预检把 --as independent|suggestion 的「不带正文单独发送」误判成「正文为空」(exit 2),而这个写法 skill 里明确支持(src/skills/definitions.ts),正文本来就由 cmdSend 后续用 crossPrincipalAsKeyword 合成(--as 的合成语义早已存在,与 --diff 渲染卡片同属「正文由后续阶段合成」)。

preflightSendInvocation 现在:

  • 把合法的 --as independent|suggestion 与 --diff 并列,空 stdin 放行,正文仍在下游合成 [botmux-as:v1:...];
  • 非法 --as 值在空正文判定之前报 --as 仅支持 independent|suggestion,参数错误不再被「正文为空」覆盖(下游 cmdSend 的校验保留作第二道)。

顺带处理了复审的几个非阻断项:

  • docs/riff.md:send --content-file - 现在会在客户端预检拒绝,改成 heredoc/管道走裸 stdin 的写法;card stream write 的 --content-file - 仍保留,文档分开说明;
  • RIFF_UPLOAD_PREFIX 交给 client 占位符拼装处使用,不再是无引用导出;
  • 更新 test/cross-principal-choice.test.ts 里把 -- "<prose>" 当既定写法的过时注释;
  • 在预检 stdin 读取处补了顺序取舍注释(该读取先于 workflow/transport/capability 闸)。

测试

  • 新增端到端回归(cli-send-reply-card.test.ts,真实子进程 + 假 HTTP 夹具 + 播种会话):空 stdin 的 send --as independent/suggestion --no-mention 实发 exit 0 且卡片正文含合成 token;非法 --as bogus exit 2、报 as_usage、零 HTTP;
  • 新增构建版行为测试(api-only-cli-gate.behavior.test.ts):virtual 会话 + 非空 stdin 仍被 transport 闸拒绝(钉住预检读 stdin 提前后,闸仍然有效);
  • send/riff 相关 8 个文件 189 测试在 vitest 与 bun:test 双腿全绿;tsc、build 通过;
  • 变异复验:撤掉 --as 空正文豁免 → 新增 2 个用例双腿红;撤掉非法 --as 提前报错 → 对应用例双腿红;恢复后双腿全绿。

这是自动评审流程追加的修复,最终以维护者审阅为准。

DeepCold review and others added 4 commits November 4, 2026 23:44
第二轮交叉复审的两个非阻断小修:

- --as 值是纯 argv 推导(与 --image-mode/--response-kind 同类),把校验
  从 readStdin() 之后挪到之前:非法 --as 配永不 EOF 的非 TTY stdin 时
  直接报 as_usage(exit 2),不再无意义地阻塞在 stdin 读取上。合法
  --as 空 stdin 的放行不变。补一条「stdin 为持开 socket、--as bogus
  必须自行 exit 2」的子进程测试;把校验挪回 readStdin 之后该用例会
  挂到超时(变异已验)。
- docs/riff.md:Riff 工具通道里裸 `-` 位置参数会被判为未知 flag
  (riff_host_flag_unsupported:-),只有 heredoc/管道走 stdin 字段可用;
  裸 `-` 作 stdin 同义只在普通本地 CLI 成立,文档据此更正。

Co-Authored-By: Claude Code <[email protected]>
同步主干以解决 src/cli.ts 与 deepcoldy#2770 beginFeedbackEffect 的签名/调用点
机械冲突:cmdSend(rest, beginFeedbackEffect?, preflight?),send 调用点
同时传 lease 回调与 sendPreflight,两条路径时序各自保持原样。
tree c4aa98b,已经双审背书。
同步到 e013b27;src/cli.ts 与 deepcoldy#2770/deepcoldy#2772 的 send/workflow 改动非相邻,
git 自动合并。落地树 c47ef79 已双审背书。
同步 deepcoldy#2784(并发上限可配置);与本 PR send/riff 零交集,git 自动合并。
落地树 5518442 已双审背书。
@deepcoldy
deepcoldy merged commit c3eba0b into deepcoldy:master Nov 4, 2026
11 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

已合并,感谢 @BanboLee 的贡献 🎉

这个 PR 把 botmux send 长期存在的几类「静默吞正文/附件」错误(多写的附件路径被当正文、多余位置参数盖掉真正正文、裸 - 发出一个点、写错旗标不报错)收敛成最早入口的统一预检,并覆盖实发 / --dry-run / sandbox relay / Riff client 与 host 全路径,方向和测试都很扎实。

交叉评审中一起打磨的点(均已包含在合并提交中):

  • 恢复 botmux send --as independent|suggestion 不带正文单独发送(正文由下游合成),非法 --as 在读取 stdin 之前就报参数错误;
  • Riff 侧 send 正文以 heredoc/管道 stdin 为准,文档中关于裸 - 与 --content-file - 的描述已按实际行为更正(card stream write --content-file - 仍保留)。

没有阻断性遗留项。欢迎继续贡献!

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

🚀 Released in v3.57.1

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.

2 participants