Skip to content

fix(executor): re-derive POSIX mode on POSIXLY_CORRECT bind (Refs #3661) - #3669

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3661-posixly-correct-runtime-hook
Nov 15, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3661-posixly-correct-runtime-hook

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix for rubash#3661: POSIXLY_CORRECT assigned at runtime now re-derives POSIX mode, like GNU. The branch carries the original fail-first nails (fe879b3), the mechanism fix (608c4ec), and oracle-verified nail corrections (9950cf9).

Root cause

GNU derives POSIX mode from the variable on every bind: variables.c:5820 registers sv_strict_posix for POSIXLY_CORRECT, and variables.c:6257-6266 recomputes posixly_correct = var && var_isset (var), then runs posix_initialize() (general.c:103-128) + set_shellopts(). rubash only flipped the mode from set -o posix (builtins/set.def:406-411 path) and from the startup environment scan, so a runtime assignment was invisible to the mode's consumers — observably, export -p kept printing the declare -x form instead of the POSIX form (builtins/setattr.def:504-510), and the modernish isset -x gate in the #2902 close-out chain died on it.

Fix (mechanism, by bind-funnel category)

  • src/builtins/set/options.rs: apply_posix_mode_transition is split into the set -o path (bind/unbind the variable, then walk) and a new sync_posix_mode_from_variable that only re-derives — it refreshes the __RUBASH_SETOPT_posix flag + SHELLOPTS and runs the same posix_initialize walk, never writing the variable itself (an empty value stays set-but-empty, as on GNU).
  • Hooks at every funnel that can bind or unbind the variable: plain/append/element statement assignments, temporary-prefix restore (restore_temporary_assignments), function-local frame pop, unset, declare/local/export/readonly operand tails, arithmetic stores, and printf -v.
  • Written-text keying where GNU keys on the written text: the arithmetic hook fires only for a scalar lvalue spelled exactly POSIXLY_CORRECT (expr.c:352 passes the lhs verbatim to stupidly_hack_special_variables), and printf -v only for the bare operand (builtins/printf.def:112-113) — element spellings match no special variable and do not flip. Statement element binds and declare/local element operands do flip: those routes reach the hook with the bare variable name (variables.c:5314, builtins/declare.def:1105).
  • Temporary prefixes on special builtins keep GNU's promotion (variables.c:4485-4525 push_posix_temp_var/merge_temporary_env); the restore hook re-derives from the promoted state.

Nail corrections (4 cells, all re-probed on the GNU 5.3.20 oracle, env -i)

  • set_plus_o_posix_unbinds: GNU prints declare: POSIXLY_CORRECT: not found on stderr with status 1 after set +o posix, not a DECLARE line on stdout with status 0.
  • Temp-prefix restore cell now uses command export -p (regular builtin): a prefix on the special builtin export is promoted by GNU instead of restored; the promotion is nailed separately in the new cell temp_prefix_special_builtin_persists (21 cells total).
  • arith_element_assign / printf_v_element_assign expect DECLARE, not POSIX (written-text keying above).

Legs (captain re-verification welcome; all run locally under the single build slot, -j 1)

  • Nail fail-first on the unfixed head fe879b3: 17 failed / 3 passed (the 3 green are vacuous or the pre-existing set -o path).
  • Nail on head 9950cf9: 21 passed / 0 failed.
  • GNU oracle differential matrix (25 bind shapes × export -p form + SHELLOPTS + set -o report, incl. declare/export/readonly/read/arith/printf element spellings, temp prefixes, comsub isolation, controls): 25/25 SAME, 0 DIFF.
  • Neighbors: 20 stems, 55 tests, all test result: ok (posix/shopt, assignment chains, temp-env, local/declare, readonly, arith cache, printf -v, unset, export filters); the 4 arith/printf stems re-run after the written-text narrowing, all ok.
  • Lib bilateral (bin built first on both sides): base 2e02e90 = 1249 passed / 0 failed / 4 ignored; head = 1249 / 0 / 4; failed-test name sets empty on both sides.
  • RUSTFLAGS="-D warnings" cargo check: rc=0, zero warnings.
  • scripts/check-utf8-boundary-hygiene.sh: clean. All touched files rustfmt-clean (two pre-existing base drifts — src/main.rs, tests/issue3353_... — were left untouched).

本 PR 删除了什么

No product code or files are deleted. The fix commit is +194/−3 across 12 source files (the −3 is the refactored transition body in options.rs). The nail-correction commit rewrites the expectations of 4 existing nail cells and the file header's probe summary, per the oracle evidence above; no nail cell was removed (one was added: temp_prefix_special_builtin_persists).

Known accepted gaps (documented in the lane report)

  • Arithmetic parse-cache fast paths (++/op= on an already-typed variable) do not fire the hook; they cannot create the variable from unset (an unbound name takes the slow path), and an existing value stays isset, so the mode cannot diverge there.
  • mapfile POSIXLY_CORRECT does not flip on GNU either (oracle-verified) — no hook, matching GNU.
  • A valueless local POSIXLY_CORRECT creation edge is not hooked.

Refs #3661

UNVERIFIED-BY-CAPTAIN

@caomengxuan666 caomengxuan666 changed the title test: nail POSIXLY_CORRECT runtime-assignment hook (Refs #3661) fix(executor): re-derive POSIX mode on POSIXLY_CORRECT bind (Refs #3661) Nov 15, 2026
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核:PASS(本地腿全齐;CI 按名待新代 run 落定,合入前补齐)

复核对象:新头 9950cf9f(追加提交 608c4ec 修法 + 9950cf9 钉修正,未改写历史),基底 2e02e906。Refs #3661。

机制(队长亲读 diff):GNU sv_strict_posix(variables.c:5820 注册、6257-6266 每次 bind 按 isset-ness 重导模式)在我方拆为两条方向——apply_posix_mode_transition 保留 set -o 侧的变量绑定语义(set.def:413-416),新增变量驱动的 sync_posix_mode_from_variable 只重导模式(刷新 __RUBASH_SETOPT_posix + SHELLOPTS + posix_initialize walk),永不写变量本身,空值保持 set-but-empty。挂钩覆盖全部 bind/unbind 漏斗:语句赋值漏斗、command_prepare 两处元素站点、tempenv 恢复、函数 local 帧弹出、unset(整名)、declare/local/export/readonly 尾、算术 set_lvalue、printf -v。书面文本定键队长认可:GNU 绑定后钩按书面 lhs/操作数文本查表(expr.c:352、printf.def:112-113),算术元素与 printf 元素拼写不触发、语句元素与 declare/local 元素操作数触发(variables.c:5314、declare.def:1105)——实现已按此收窄,oracle 矩阵为证。

钉修正 4 格(非原处方 2 格)队长认可:set +o 的 stderr/rc 格、临时前缀特建提升格(新增第 21 格 temp_prefix_special_builtin_persists)、arith 元素格、printf -v 元素格——四格皆有 GNU 5.3.20 oracle 复探证据,期望写反的是钉不是产品。

腿(队长抽核 lib 日志原文 + REPORT 逐 stem 表):

待补:新代 CI(run 43716437689,头 9950cf9)在跑——Rust tests job 已显 failure,run 落定后队长按名补核;若出现 issue2875 基线 cell 之外的新红,本 PASS 失效待复议。

合入轮:update-branch 到现尖 68328a3d + 合并形复跑钉 21 格后队长合入;合入后队长手关 #3661。#2902 收官链的 §2 门即此单。

@caomengxuan666
caomengxuan666 force-pushed the fix/3661-posixly-correct-runtime-hook branch 3 times, most recently from e1d1b39 to 346a95a Compare November 15, 2026 14:07
Fail-first regression nails for rubash#3661: assigning POSIXLY_CORRECT
at runtime must flip POSIX mode (GNU variables.c sv_strict_posix),
so export -p prints the POSIX form. Covers the plain assignment,
empty value (value preserved), unset, set -o/+o posix, temp-prefix
restore, export/declare/readonly/local, compound/element/append,
arithmetic, printf -v, read, comsub isolation and the modernish
isset -x alias-handler shape. All expectations probed on the GNU
5.3.20 oracle with a clean environment before this commit.
GNU keeps POSIX mode derived from the variable: variables.c:5820
registers sv_strict_posix for POSIXLY_CORRECT, and
variables.c:6257-6266 (sv_strict_posix) recomputes
posixly_correct = var && var_isset(var) on every bind, then runs
posix_initialize (general.c:103-128) + set_shellopts. rubash only
flipped the mode from set -o posix (builtins/set.def:406-411
set_posix_mode) and from the startup environment scan, so a runtime
assignment was invisible to the mode's consumers — observably,
export -p kept printing the declare form instead of the POSIX form
(builtins/setattr.def:504-510).

Mechanism fix, by bind-funnel category rather than per-shape:
- builtins/set/options.rs: split apply_posix_mode_transition into
  the set -o path (bind/unbind the variable, then walk) and a new
  sync_posix_mode_from_variable that only re-derives: it refreshes
  the __RUBASH_SETOPT_posix flag + SHELLOPTS and runs the same
  posix_initialize walk, never writing the variable itself (an
  empty value stays set-but-empty, as on GNU).
- Hooks at every funnel that can bind or unbind the variable:
  plain/append/element statement assignments (temporary_assignments
  funnel + both command_prepare element sites), temporary-prefix
  restore (restore_temporary_assignments, BASH_COMPAT-shaped),
  function-local frame pop (restore_function_locals), unset,
  declare/local/export/readonly operand tails, arithmetic stores,
  and printf -v.
- Written-text keying where GNU keys on the written text: the
  arithmetic hook fires only for a scalar lvalue spelled exactly
  POSIXLY_CORRECT (expr.c:352 expr_bind_variable passes the lhs
  verbatim to stupidly_hack_special_variables), and printf -v only
  for the bare operand (builtins/printf.def:112-113) — element
  spellings match no special variable and do not flip the mode.
  Statement element binds and declare/local element operands do
  flip: those routes reach the hook with the bare variable name
  (variables.c:5314, builtins/declare.def:1105).
- Temporary prefixes on special builtins keep GNU's promotion
  (variables.c:4485-4525 push_posix_temp_var/merge_temporary_env):
  the tempenv path already promotes; the restore hook re-derives
  from the promoted state.

Refs #3661
All four corrections re-probed on the GNU bash 5.3.20 oracle with a
clean environment:

- set_plus_o_posix_unbinds: `declare -p POSIXLY_CORRECT` after
  set +o posix prints `declare: POSIXLY_CORRECT: not found` on
  stderr with status 1, not a DECLARE line on stdout with status 0.
- temp-prefix restore cell: a prefix on the special builtin export
  is promoted by GNU (variables.c:4485-4525 merge_temporary_env), so
  the restore shape now uses `command export -p` (regular builtin);
  the promotion itself is nailed in the new cell
  temp_prefix_special_builtin_persists (21 cells total).
- arith_element_assign / printf_v_element_assign: expected DECLARE,
  not POSIX — GNU keys the post-bind hook on the written lhs/operand
  text (expr.c:352, builtins/printf.def:112-113), so an element
  spelling matches no special variable.

Refs #3661
@caomengxuan666
caomengxuan666 force-pushed the fix/3661-posixly-correct-runtime-hook branch from 346a95a to 5e816ac Compare November 15, 2026 14:17
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 14:41
@caomengxuan666
caomengxuan666 merged commit 16e641c into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
…after unset HISTTIMEFORMAT stay armed

Refs #3535

## 根因

GNU 的 `history_comment_char` 武装是粘性的:`variables.c:6168-6177 sv_histtimefmt` 在 HISTTIMEFORMAT 存在时把 `history_comment_char` 置 `'#'`(仅当其为 0),unset 时只清 `history_write_timestamps`,从不复位注释符。rubash 把武装建模为「HISTTIMEFORMAT 当前存在」(各 history 读取面采样 `get_env("HISTTIMEFORMAT").is_some()` 同时充当 write-timestamps 与 comment-char 武装),于是置过又撤掉之后,`history -r` 把 `#<epoch>` 行当普通条目读入、条目带空戳,列表渲染为 `??` 三行,与 GNU 的两行带戳不符。

已合入的 #3537(#3393)只建模了同样粘性的 `history_multiline_entries`,未动注释符武装——本 PR 补这一面。

## 修法(机制,非个案)

- `ShellState` 新增粘性闩 `history_comment_char_armed`(readline `history_comment_char` 进程全局的移植,随 Clone fork 边界传递);`Executor::new` 以进程环境导入的 HISTTIMEFORMAT 播种(GNU 对环境导入变量同样跑 sv_histtimefmt)。
- 赋值面闩定:`execute_command` 每命令边界在 HISTTIMEFORMAT 存在时闩定;临时(前缀)绑定在 `apply_temporary_assignments` 内闩定(其恢复先于命令边界,GNU 探针证实 `HISTTIMEFORMAT='[%Y] ' true` 同样粘性武装)。
- `SessionHistory` 读取面把两个 GNU 状态拆开传参:`comment_char_armed`(粘性,管 `#<digit>` 识别与读时戳,histfile.c:375-383 + history.c hist_inittime)与 `timestamps_armed`(现值 = history_write_timestamps,仅继续管 multiline 重组门,histfile.c:384——粘性不放宽该门,钉 multigate 格守此界)。

## GNU 锚点

- `variables.c:6168-6177` sv_histtimefmt(武装粘性本体)
- `lib/readline/histfile.c:375-383` + `:466`(文件首行戳仅借用本读、读毕复位——不闩定)
- `lib/readline/history.c:322-333` hist_inittime(条目戳首字 = 当前 comment char)
- `lib/readline/histfile.c:384`(multiline 门 = history_write_timestamps 现值)

期望值全部以 GNU bash 5.3.20 脚本文件实测(repro / 从未武装对照 / 前缀绑定 / 空值绑定 / `history -n` 面 / multiline 门,共 6 形)。

## 回归钉

`tests/issue3535_history_comment_char_sticky.rs`(6 格):武装后撤除的 `-r` 形、从未武装对照组、前缀绑定、空值绑定、`-n` 面、multiline 门不放宽。

## 本 PR 删除了什么

无删除。纯增性改动:新增一个 ShellState 布尔闩与其播种/闩定点、读取面签名增加一个参数(仓内 5 个调用点同步更新)、新增钉文件。`src/script_driver.rs`、`src/executor/init.rs` 的改动仅为上述闩的播种与调用点传参。

## Captain verification (merge round R5, head 84a58c5, base 78060e4)

- Pin `issue3535_history_comment_char_sticky`: 6/6 green on the merged shape (fail-first banked at delivery: 4 red / 2 green on base).
- Neighbors: issue3393 8/8, issue1669 2/2, both aligned with base.
- `RUSTFLAGS="-D warnings" cargo check --locked --lib --bins`: rc=0, zero warnings. `rustfmt --check` on all 9 changed files: rc=0.
- Full lib bilateral (head vs base 78060e4, bin-first): 1249/0/4 both sides, failure name-set diff empty.
- CI on the R5 head (run 43729056059, head == 84a58c5): 3 failing jobs (Rust tests, Rust tests (macOS), GNU regression goldens and canaries), each failing ONLY on the issue2875 baseline cell (`api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell`); niu-canary success. PASS comment 6894417642 stands.
- Intersection (merge-base eaa357f..78060e4 vs the PR's 9 files): exactly 2 files, both released by the captain after personally reading both sides' hunks — `src/executor/command_execute.rs` (tip #3686 @@ -468 and #3690 @@ -600, both in `execute_command_with_procsub_scope`, vs PR @@ -30 in `execute_command`; ~438 / ~570 lines apart, different functions, no text overlap) and `src/executor/temporary_assignments.rs` (tip #3669 @@ -1482 in `apply_shell_assignment` vs PR @@ -186 in `apply_temporary_assignments`; ~1296 lines apart, different functions, no text overlap). Rebase onto 78060e4 was clean, zero conflicts; the rebased diff is byte-identical to the R4 shape except the two base-blob index lines. Tip unmoved after all legs.

Refs #3535 (the issue stays open until the captain hand-closes it on this merge).
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