Skip to content

fix(executor): inline tput arm — Windows-only whitelist answers byte-equal to GNU ncurses (#800 face A) - #884

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/800
Oct 20, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/800

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因(#800 A 面:Windows 外部进程 spawn 成本)

harvest runner 上每次 $(tput …) spawn 计价 ~300ms(kiddae/dotfiles hermine .bashrc ~32 次 → source_wall 9.7s)。分诊证明成本不在 rubash spawn 路径:同机 32×uname 地板与 master 持平(benchmarks/29 形状);同一个 winuxcmd 4MB 二进制 true ~1ms/次 vs tput bold ~17ms/次——成本在 tput 子进程内部,再乘 harvest runner 环境系数(AV/冷 VM)。有效修法是让准入形状免 spawn 进程内作答,而非继续调 spawn 路径。

变更(Windows-only)

  • src/executor/tput_emulation.rs(新):纯白名单表——TERM ∈ {xterm, xterm-256color, screen, screen-256color, tmux-256color, linux, vt100} × {bold, sgr0, clear, civis, cnorm, el, ed, smcup, rmcup, setaf, setab, cup}。**非准入形状一律回退真二进制:本臂只删 spawn,从不改答案。**缺席能力 → 静默 rc 1(ncurses 契约)。
  • 三处接线(均为该族既有先例位):
    • simple command:external_file_builtins.rs::external_tput(tty/cat/sed 先例;hash 记录 PATH 二进制;child-visible TERM = tempenv 覆盖 > 导出 shell 态,未导出/空回退);
    • pipeline stage:pipeline_exec.rs 的 tput_inline_stage 臂,并发路径 bail(tput 毫秒级,放弃并发无损失),非准入仍 spawn;
    • comsub fast-path:command_substitution_values.rs::run_external_command_substitution 的 tput 臂(tty 臂同位先例)。
  • benchmarks/31-tput-colors-x32.sh:票面形状 perf 探针(16 组 ×(bold+setaf),PERF runs=7 timeout=60)。
  • tests/issue800_tput_inline_arm_regressions.rs:8 条回归,钉死 oracle 字节、TERM 解析顺序、捕获/重定向通道、hash 内省、host-tolerant 回退不变量。

计时(同机背靠背 A/B,2026-10-19 合并 origin/master 41bfdd3 后复测,median,date +%s%N)

探针 master 41bfdd3 本分支(a56ee41d) 加速
31 票面(64×tput,comsub+赋值) 1165ms(runs 1043-1512) 47ms(runs 44-55) ~25x,零 spawn
29 spawn 地板(32×uname) 56ms 58ms 持平(噪声内,通用路径零负担)

(前一批次同形状:master/pin 1.08-1.16s / 分支 13-16ms;本次 master 中位 1165ms 与其吻合。)

Oracle 证据

269 行 TERM×capability×参数 差分矩阵:diff=0——每行字节对照 WSL GNU ncurses tput(Ubuntu 24.04,ncurses 6.4,od 探针 2026-10-19)。表数据全部来自自跑 oracle 探针的输出字节(未接触 ncurses 源码,GPL 红线未触)。矩阵由 tests/issue800_tput_inline_arm_regressions.rs 永久钉死。

验证(全部 --test-threads=1,输出落 target/issue-suites/results/)

  • issue800_tput_inline_arm_regressions:8/8(合并 master 后重编译重跑)
  • 家族回归:issue555 15/15、issue415 22/22(首跑一红系缺 target/debug/rubash.exe——issue415 coproc 探针 spawn debug 二进制的环境项,补建后全绿);tput_emulation 单测 3/3
  • RUSTFLAGS="-D warnings" cargo build --release:零警告
  • cargo fmt --check:本分支零新增(全仓唯一 diff 在 command_prepare.rs:4670,为 mark_quoted_ifs_from_raw simulator is carrier-blind: carrier-laden raw words silently lose per-char IFS marking #868 带入的 master 预存 import 顺序项,已在 pristine 41bfdd3 上核实逐字同款)
  • 全量 --no-fail-fast 双向(分支 vs pristine master 41bfdd3):317 vs 316 套件(多出的 1 个即本票 8 条新测试,全绿);失败集合完全一致(36=36,全为 master 预存主机面)。分支侧全量首跑曾出现 2 条瞬时红(profiler fixture 树按实测 self-time 排序、listing_cache 依赖 NTFS mtime 时序——两者 fixture 均不含 tput,与本臂无关):两侧隔离/整套复跑均全绿(分支 lib 710 = 基线 707 + 本票 3,精确闭合),判定为负载性 flake。

关联

@caomengxuan666

Copy link
Copy Markdown
Member Author

独立代码评审结论:REQUEST-CHANGES(1 BLOCKER)

评审树:自建 detached worktree @ a56ee41(fix/800 分支名被实现树占用,同提交另检);oracle 为自跑 WSL Ubuntu 24.04 ncurses 6.4.20240113(tput -V = 6.4.20240113);base 对照为自建 pristine 41bfdd3 release 二进制。以下全部数据自产,非转抄票面。

BLOCKER

B1:前导零/带符号数值操作数——admitted 形状字节背离 GNU oracle(strtol base-0 八进制语义)

自跑 535 行 TERM×capability×参数 差分矩阵(7 TERM × 9 无参 cap;setaf/setab × 25 数值形 × 7 TERM;cup × 11 形 × 4 TERM;7 控制 TERM × 5 形;20 非准入调用形;simple / 管道 / comsub / tempenv 四形状;stdout+stderr+rc 全对照),除下述家族外 admitted 全部字节吻合。抓到的分歧(simple 形状示例,管道/comsub/tempenv 形状同样分歧):

操作数 GNU ncurses(strtol base 0) 本臂(Rust i32 十进制)
setaf 010(有彩 6 TERM 全分歧;vt100 absent 恰一致) \e[38m(八进制 8) \e[310m
setaf 017 \e[315m \e[317m
setaf 009、setaf 08 \e[30m(strtol 止于非法八进制位,取 "00"→0) \e[39m、\e[38m
setaf +010 \e[38m \e[310m
setaf 000000000010 \e[38m \e[310m
setab 010 \e[48m \e[410m
cup 010 010 \e[9;9H \e[11;11H
cup 008 010 \e[1;9H \e[9;11H

根因:GNU tput 数值操作数走 C strtol(s, NULL, 0)(0NN 八进制、0x.. 十六进制、非法位截断),admitted_color_number/digits_only_i32 按 Rust 十进制解析。tput_emulation.rs:265 注释「+7 and 007 parse the same way GNU's operand parsing answers (both verified → \e[37m)」——007 八进制=十进制=7 是同值巧合,不是语义验证;票面「verified 0, 1, 7, 8, 9, 10, 15…」的 10 是 "10" 而非 "010"。回归钉的 setaf 007 → \e[37m 在两种语义下都通过,故 8/8 全绿掩盖了该分歧。

这违反本臂自设的核心不变量(模块头:「Every admitted (TERM, invocation) pair answers with bytes verified byte-for-byte against the GNU oracle」;rubash#117 纪律:准入必须 provable)。

修法建议(白名单纪律下应收窄而非仿制):admitted_color_number/digits_only_i32 拒绝前导零(除 "0" 本身)与 + 前缀操作数——非准入即回退真二进制,零变更面;不要复刻 strtol base-0 全语义(0x10 在 GNU 是 rc 4 + \e[316m,止于非法位的表面更宽)。修复后请把 010 / 017 / 009 / 08 / +010 / cup 010 010 / cup 008 010 家族补进矩阵与回归钉(现钉的 007 行请保留但注明巧合)。

逐验证点复现结果(全部实做)

  1. 白名单与三处接线:审计通过。7 TERM × cap 表与自跑 oracle 逐字节吻合(B1 家族除外);三处准入判定一致——simple/pipeline 共用 admitted_tput_outcome(child-visible TERM = tempenv > 导出态),comsub &self 臂有结构性守卫:$(TERM=x tput …) 首词非 tput 必落真执行器 simple 臂(实测 PR==BASE);pipeline 并发臂 bail 只损失并发不作错误作答;tput_inline_stage raw-word 过匹配只损失并发('tput' 引号形、函数遮蔽形实测 PR==BASE)。
  2. 字节门:自跑 535 行矩阵(票面 269 行的超集)——除 B1 的 12+ 行外全部吻合,含 absent-cap 静默 rc 1(与 GNU rc 1 对照)、cup 0 0 → 1;1、256 色 8-15 亮色无 ;5; 段、tempenv/重定向/comsub 通道。
  3. 计时:复现。probe31(CREATE_NO_WINDOW 批量,PR-BASE-PR 交错排除漂移):PR median 43-45ms vs base 1447ms(~32x);probe29 spawn 地板 PR 43ms vs base 47ms 持平(通用路径零负担成立)。
  4. TERM 解析边界:tempenv 覆盖 > 导出态;未导出 / 空 / TERM+= 臂让位,PR==BASE;unset TERM 臂让位(winuxcmd 忽略 TERM 面,PR==BASE);nameref 经 tempenv 绑定 target 后臂按 child-visible TERM 作答(declare -n T=TERM; T=xterm-256color tput setaf 9 → \e[91m,与 bash assign_in_env 语义一致——此为 admitted 形状的正确内联,非回归)。
  5. 回归:issue800 8/8、issue555 15/15、issue415 22/22、tput_emulation 单测 3/3 全绿复现(lib 计数 707+3=710 精确闭合)。
  6. fmt:复现——全仓唯一项 command_prepare.rs:4670 import 顺序,该文件不在本 PR 的 8 文件 diff 内,确系 master 预存(mark_quoted_ifs_from_raw simulator is carrier-blind: carrier-laden raw words silently lose per-char IFS marking #868 带入),未越权代改属实。
  7. -D warnings:RUSTFLAGS="-D warnings" cargo build --release 复现零警告。
  8. 全量双向:自跑 --no-fail-fast(--test-threads=1 由 .cargo/config 强制,输出落文件):PR 侧 317 套件 / 5645 过 / 36 失败,BASE 侧 316 套件 / 5634 过 / 36 失败;失败集逐名恒等(36=36,全为 master 预存主机面:tilde/PATH/SIGINT/fd/examples,无 tput 相关);多出的 1 套件即本票 issue800 套件。
  9. GPL 红线:未触。表数据为纯输出字节;本评审以自跑 oracle 独立复现了全表字节(含 B1 分歧行的 oracle 侧),反向证明无 GPL 代码拷贝。
  10. 夹带:净。41bfdd36..a56ee41 仅 2 提交(wip + merge master),8 文件 935+/1-(唯一删除是 tempenv_export_name 可见性放宽),全部在票面范围。
  11. 杀进程白名单:全程仅短命 -c/探针/WSL 进程,无残留监听(复核过 tasklist)。

非阻塞备注

  • N1(设计后果,请确认知情):admitted 形状在 PATH 无 tput 时也作答——PATH=/nonexistent tput bold → PR \e[1m rc 0,master rc 127 command-not-found。与 cat/tty/sed 家族「文件 builtin 派发先于 PATH 查找」契约一致(同形实测),harvest 动机本就要求绕开坏二进制(WinuxCmd#1258);但它是「零行为变更」主张之外的、有意的答案面变化,建议模块头明示一句。
  • N2:PR 基于 41bfdd3,origin/master 已前移至 f570150(现仍 MERGEABLE 无冲突;建议合前 rebase 并重跑字节门 + issue800 套件)。
  • N3:src/executor/mod.rs 中「Windows-only stand-in…」注释被插在 parameter_errors 声明上方(该模块并非 windows-only),位置误导,随 B1 顺手挪到 mod tput_emulation 处。

结论

REQUEST-CHANGES。仅 B1 一项阻塞;其余全部验证点独立复现通过,实现质量高(接线位、TERM 解析、计时、回归卫生都干净)。B1 是窄化准入的一行级修复 + 矩阵补钉;修复后重跑字节门与 issue800 8/8 即可,其余验证不受影响。B 面(read on-EOF)留票正确。不合并。

…arses operands with strtol base-0 (CR #884 B1)

GNU tput reads setaf/setab/cup operands via strtol base-0: '010' is octal 8
(\e[38m), '+010' signed octal, '009'/'08' truncate at the first illegal
octal digit (\e[30m), '000000000010' is octal 10 — every one byte-diverges
from the arm's decimal read (setaf 010 answered \e[310m). The prior
regression pinned setaf 007, where octal and decimal coincide, so 8/8 green
masked the family.

Per the CR's prescribed move the arm narrows instead of replicating strtol:
operands must be canonical decimal (no sign, no leading zero beyond the
literal '0', 0..=255) or the shape falls through to the real binary, whose
base-0 semantics keep answering. Pins added: strtol/ family rows in the
byte-gate matrix (GNU answers recorded as evidence), a host-tolerant
integration test asserting the decimal misread never appears, and unit
rows for every rejected spelling.

Byte gate re-run post-rebase on master 78952f5: 3175 admitted rows
(full 0..255 setaf/setab sweeps, xterm/xterm-256color/screen/
screen-256color/linux/tmux-256color + no-param caps + cup shapes +
tempenv/export/redirect) byte-identical to WSL GNU ncurses tput; 36 strtol
rows recorded, zero decimal misreads. issue800 suite 9/9 + tput unit tests
3/3. Also moves the Windows-only stand-in comment off parameter_errors
onto mod tput_emulation (CR nit N3).
The call sites guard with cfg!(windows) && tput_inline_stage(command),
but cfg! is a compile-time constant — the function must still exist on
non-Windows targets. Provide a stub returning false so Linux/macOS CI
compiles (the arm is Windows-only by design: it exists to dodge the
~300ms Windows spawn floor, and the predicate is always false off
Windows).
@caomengxuan666

Copy link
Copy Markdown
Member Author

CR #884 (复审 @ 52d3785b) — 结论:REQUEST-CHANGES

前轮待办复核(a56ee41d → 52d3785b)

  • B1(strtol base-0 数值操作数分歧):✅ 已解决。tput_emulation.rs:287-291 新增 canonical_decimal,拒绝前导零("0" 本身除外)与 +/- 符号操作数,非规范十进制一律回退外部二进制;issue800_strtol_base0_operands_never_answer_from_the_table 与单测已补钉。
  • N3(src/executor/mod.rs 注释错位):✅ 已挪至 mod tput_emulation 上方。

新发现的阻塞项(52d3785b 深入审查)

B2:run_external_command_substitution(src/executor/command_substitution_values.rs:724-737)绕过函数遮蔽、丢弃 $() 内重定向、且忽略 external_file_builtins_enabled

  1. 同名 shell 函数被劫持:
    在 "$(tput bold)"(command_prepare.rs:1733-1767)及未加引号的 $(tput bold)(expand_word.rs:750-756)路径上,调用链走 &self 的 expand_command_substitution_with_context → expand_command_substitution_inner → run_external_command_substitution(&words)。该路径未检查 self.function_name_for_command_word(first_word).is_some():
    export TERM=xterm
    tput() { echo FAKE; }
    echo "$(tput bold)"
    GNU Bash(execute_cmd.c:4674-4677 find_function)输出 FAKE;本分支在 command_substitution_values.rs:724 直接拦截 "tput" 并返回 \x1b[1m。
  2. $(tput ...) 内的重定向被静默吞掉:
    command_substitution_words_and_stdio(words)(L811-867)会将 <、>、1>、>|、>>、2>、2>> 剥离进 stdio.stdin_path / stdio.stdout_redirect / stdio.stderr_redirect 并置 stdio.had_redirect = true,而 stdio.expanded_words 仅保留 ["tput", "bold"]。L724-737 的 tput 分支未检查 !stdio.had_redirect:
    • x=$(tput bold > out.bin):本分支把 \x1b[1m 返回给 x,不创建也不写入 out.bin(GNU 写入 out.bin 且 x="");
    • x=$(tput bold < /nonexistent/file):本分支忽略输入重定向,返回 \x1b[1m、rc=0(GNU 报错 No such file or directory、rc=1、输出为空)。
  3. 未尊重 self.external_file_builtins_enabled 与 hash -p:
    external_file_builtins.rs:10 受 self.external_file_builtins_enabled 控制,而 command_substitution_values.rs:724 与 pipeline_exec.rs:3678 均未检查 self.external_file_builtins_enabled;此外若用户显式设置了 hash -p /custom/tput tput,内联臂不应覆盖或绕过已登记的自定义哈希路径(GNU findcmd.c:367-380)。

修法建议:在 run_external_command_substitution 的 tput 分支补充守卫:self.external_file_builtins_enabled && !stdio.had_redirect && self.function_name_for_command_word(first_word).is_none() && crate::builtins::hash::hashed_path(&self.shell_state.env_vars, first_word).is_none()(simple / pipeline 分支同样检查 external_file_builtins_enabled 与 hashed_path)。


B3:child_visible_term(src/executor/external_file_builtins.rs:139-194)重复展开 tempenv 赋值 RHS、忽略 readonly TERM、且 nameref 判定顺序颠倒

  1. tempenv RHS 被重复求值(副作用翻倍):
    在顶层简单命令路径中,execute_materialized_command(command_dispatch.rs:72)在进入 execute_external → handle_external_file_builtins → external_tput 之前已经调用了 apply_temporary_assignments(&cmd.assignments, assignment_raws),完成了 cmd.assignments 的 RHS 展开并写入 self.shell_state.env_vars。
    child_visible_term 在 L152 再次调用 self.expand_assignment_value_with_raw(var_name, var_value, raw),会导致 TERM=$(echo xterm; echo side >&2) tput bold 或 TERM=vt100$((i++)) tput bold 的命令替换/算术副作用执行两次(若最终未准入回退到 apply_external_environment,则会执行三次;管道阶段未准入回退时也会执行两次)。
  2. 忽略 READONLY_VARS:
    当 export TERM=xterm; readonly TERM; TERM=xterm-256color tput setaf 9 时,GNU Bash 在 assign_in_env 报错 TERM: readonly variable,非 POSIX 默认模式下继续以原值 TERM=xterm(\x1b[39m)执行 tput;而 child_visible_term 未检查 READONLY_VARS,直接采用 override_term = Some("xterm-256color") 返回 \x1b[91m。
  3. nameref 名字检查在 tempenv_export_name 之前:
    L145-147 先判断 if base_name != "TERM" { continue; },随后才在 L161 调用 self.tempenv_export_name(base_name)。当 declare -n T=TERM; export TERM=xterm; T=xterm-256color tput setaf 9 | cat 在管道阶段执行时(此时 apply_temporary_assignments 尚未预写 env_vars["TERM"]),base_name == "T" 被直接跳过,导致管道阶段按旧的 TERM=xterm(\x1b[39m)作答而非 xterm-256color(\x1b[91m)。

B4:管道分支匹配集与 docs/builtins.md / src/executor/builtin_names.rs 契约同步缺失

  1. pipeline_exec.rs 匹配不一致:
    tput_inline_stage(pipeline_exec.rs:4442)对 Some("tput") | Some("/bin/tput") | Some("/usr/bin/tput") 全部拒绝并发路径,但 execute_pipeline_stage_inner(pipeline_exec.rs:3678)仅匹配 "tput" =>,漏掉了 "/bin/tput" | "/usr/bin/tput"(与 external_file_builtins.rs:95 和 command_substitution_values.rs:724 不一致)。
  2. docs/builtins.md 与 src/executor/builtin_names.rs 未同步:
    按 docs/builtins.md §3.1、§4.1、§5 及 src/executor/builtin_names.rs:260-283(doc_pipeline_fastpath_list_matches_pipeline_arms)维护规则,凡在 execute_pipeline_stage_inner(pipeline_exec.rs match name)新增管线阶段 arm 或在 external_file_builtins.rs 新增模拟命令,必须同步更新 docs/builtins.md(<!-- pipeline-fastpath-list 与 §4.1 列表)及 builtin_names.rs 的 expected 列表。

架构边界说明(对照 AGENTS.md)

@caomengxuan666
caomengxuan666 merged commit 4dcb7ff into master Oct 20, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/800 branch October 21, 2026 08:10
caomengxuan666 added a commit that referenced this pull request Oct 21, 2026
…est platform status, current architecture (#982)

* docs(readme): overhaul — Performance section with pinned numbers, honest platform status, current architecture

- New Performance section, every number pinned to its measuring issue/PR:
  - Android-matrix interpreter triage closed with numbers (#607/#608/#609
    -> #665/#671/#679, #666, #672/#678; net ~1.06x GNU scalar assign)
  - array store O(n^2)->O(1) amortized (#618/#616: assoc 445x->9.9x,
    indexed 736x->3.8x vs GNU at 5000 keys; docs/performance.md ledger)
  - literal fast-path wave with measured deltas: #967 (177->155 ms),
    #966 (1.41x->1.10x), #974 (~11%), #959 (3.65x->3.16x), #958 (-10%),
    #956/#973/#954 family
  - export/unset economics: #916 measured costs stated, write lane landed
    (#917/#918/#920, #943 correctness rework), issue open on residual
  - Windows process economics: #913 one hidden console per session
    (~22 ms/spawn -> <=1 per session; tests/console_window_gate.rs),
    #884 tput inline arm (1165 ms -> 47 ms, 269-row oracle matrix),
    WinuxCmd#1269 twin (~17 ms -> ~0.4 ms in-process), #800 face B open
- Platform status made verifiable: Linux CI lanes + upstream-corpus replay
  job named; 19/24 Linux pass dated and cited (#500) instead of 'and
  climbing'; macOS now runs lib tests in CI, not just compiles
- Architecture tree updated to the real src/ layout (bin/expand/fd/jobs/
  shell added); builtin count 40+ -> 62 engine builtins per the
  test-guarded docs/builtins.md + hidden fast-path builtins
- Testing section: regression suite, console gate, corpus replay commands
- Compatibility baseline line updated to vendored Bash-5.3 patch 20
  (#957) with local+WSL 5.3.20 oracles (#978)
- zh README rewritten to full parity with EN (was still describing the
  pre-dialect path model, missing the identity section, claiming 5.3.0)
- Last-updated stamp refreshed

* docs(readme): date the Linux corpus pass from the archive — 2026-09-27 wt4/linuxrun, 17/24 per its own ledger

The "(2026-09-26, #500)" tag was wrong on both counts: 09-26 is the
earlier "not-yet-usable" judgment (first smoke 8/10, gap-ledger C4) that
the 2026-09-27 wt4/linuxrun lane explicitly declared obsolete, and #500
only carried the figure forward — it is not the measuring record. No
archived record supports 19/24: the lane's own corpus-slice table
(docs/LINUX-RUN-STATUS.md, deleted in #478, archived in #477 section 4
and #581) tallies 17 suites at 0 diff + trap/redir/glob/extglob/new-exp/
read/ifs-posix with diffs = 17/24. README now reads "17/24 byte-identical
(2026-09-27 wt4/linuxrun lane, ledger archived in #477/#581)" in both
the platform-status paragraph and the compatibility table, EN + zh-CN
at parity. Addresses review comment 6319063190 on #982.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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