Skip to content

fix(history): gate history -r multi-line regrouping on read-time HISTTIMEFORMAT (Refs #3393) - #3537

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3393-history-read-multiline-gate
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3393-history-read-multiline-gate

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

修了什么

history -r(以及交互启动时的 HISTFILE 载入)读带时间戳的历史文件时,rubash 无条件把无戳行并入上一条目(多行重组)并保留空行。GNU 只在 history_multiline_entries 已武装时这么做,而一次读只在「文件首行是 #<digit> 戳行」且「读时 HISTTIMEFORMAT 存在」时才武装它;该计数此后永不复位(history -c、unset HISTTIMEFORMAT 都不清)。

  • SessionHistory 新增 multiline_entries(粘性),load_file_with_arming 以 has_timestamps && timestamps_armed(调用方传入的就是 HISTTIMEFORMAT 是否存在)累积它,重组与 default_skipblanks 都改读它。
  • 交互启动载入(script_driver.rs 的 load_history 面)原来硬编码 armed=true,改为与 history -r 相同的读时 HISTTIMEFORMAT 状态。

删了什么

has_multiline = has_timestamps 这一「有戳即重组」的假设。

GNU 依据

  • lib/readline/histfile.c:384 read_history_range:history_multiline_entries += has_timestamps && history_write_timestamps;
  • histfile.c:387 default_skipblanks = history_multiline_entries == 0;;histfile.c:436-437 并入上一条目的分支以 history_multiline_entries 为条件。
  • histfile.c:126 是该变量唯一的其它出现(初始化),无复位点 → 粘性。
  • variables.c:6177 sv_histtimefmt:history_write_timestamps = (v != 0)(设为空串也武装)。

WSL GNU 5.3.20 实测(脚本文件):格式后设 → 3 条目;无格式 → 3;反斜杠行 → 3;两孤行 → 4;空行无格式 → 跳过;格式先行 → 2(重组,空行保留);先武装再 unset + history -c 后再读 → 仍重组。

验证

  • 新钉 tests/issue3393_history_read_multiline_gate.rs(8 例,期望取自 GNU 5.3.20):未修树 2 passed / 6 failed(两例对照组「格式先行」「粘性」本就重组),修后 8/8。
  • 8 格探针脚本 rubash vs GNU 字节一致(读时戳年份归一后)。
  • issue1669_history_timestamp_display_regressions:原钉在同一进程里先做武装读再做「未武装读」,只在旧的非粘性模型下成立;GNU 5.3.20 同一脚本实测 plain.txt 会被重组且带读时戳(注释符武装同样粘性,见 history: comment-char arming is sticky in GNU — reads after unset HISTTIMEFORMAT are still armed #3535)。把两段未武装相拆到独立进程(GNU 独立进程实测正是原期望 ?? 形),期望值未改。修后 2/2。
  • 同族 history 钉:issue1071 / 1289 / 1627 / 1628 / 1629 / 1633 / 1660 / 1668 / 1669 / 2733 / 2824 / 3393 / 646×2 / 756 / script_driver_history_in 全绿。issue1016…/1830/1852/2006_2007/3329/660/661 的失败(common::issue2197_wait_bounded_* 两例、interactive_histfile_null_rc_and_append_diag_match_gnu、issue1852_session_path_read_failures_are_silent、history_s_empty_entry_is_written_by_w)在未修树上同样失败(本机环境:交互 pty / 相对路径按进程 cwd 解析 bind -f / history 文件面的相对文件名不锚定会话 cwd(进程 cwd 解析,平名复现) #3090),与本改动无关。
  • tests/d11_pipeline_squote_carrier_regressions.rs 仅 rustfmt(master 上 fix(executor): restore DATA_SQUOTE carrier on pipeline child-argv boundaries #3511 遗留的格式漂移,fmt hook 需要)。

Owed

Fixes #3393

…TIMEFORMAT (Refs #3393)

GNU lib/readline/histfile.c:384 read_history_range arms
history_multiline_entries only when the file starts with a timestamp
line AND history_write_timestamps is set (variables.c:6177
sv_histtimefmt: HISTTIMEFORMAT exists). The counter is never reset.
rubash regrouped whenever the file had timestamps. Model the sticky
counter on SessionHistory and feed it the read-time variable state;
the interactive startup load takes the same state instead of a
hard-wired armed read.

The issue1669 pin chained unarmed reads after an armed one in one
process, which only matched the old non-sticky model; GNU 5.3.20
regroups there. Split the unarmed phases into fresh processes.

Also rustfmt the d11 stem (master drift from #3511) so the fmt hook
passes.

Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 04:55
@caomengxuan666
caomengxuan666 marked this pull request as draft November 14, 2026 05:09
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】

PR #3537 第1轮 — 建议:PASS
head: e59a71a base(merge-base): 8b48c16 文件: 5 个
关联 issue: #3393(状态:open,我核过的时间:2026-11-15 本地);测试同时覆盖 #1669 回归面

机制核查:

  • SessionHistory.multiline_entries 新增粘滞位:|= has_timestamps && timestamps_armed,此后所有读的门禁改用它——对应 lib/readline/histfile.c:384 history_multiline_entries += has_timestamps && history_write_timestamps、:387 default_skipblanks、:436-437 append 分支;variables.c:6177 sv_histtimefmt 使 set-but-empty 的 HISTTIMEFORMAT 也 arm。该计数 GNU 从不复位(histfile.c:126 仅声明),本实现同样不被 history -c / unset 复位。
  • script_driver.rs 启动装载改用 load_file_with_arming(..., write_ts)——对应 bashhist.c load_history 走同一 read_history 路径。
  • issue1669 测试重构:ARMED/UNARMED 阶段分进程跑,因为 arm 是粘滞的(unset 不回退)——该重构本身是 GNU 语义的正确反映。
  • 可疑/无关改动:d11 仅 rustfmt(正文已声明的惯例)。无桥接/黑名单修法。

腿:

  • 钉自跑:issue3393_history_read_multiline_gate 8/8;issue1669_history_timestamp_display_regressions 2/2(均为 head)。
  • fail-first:base 8b48c16 上 issue3393 = 2 passed / 6 failed,红名 = {read_without_format_splits_physical_lines, format_set_after_read_does_not_regroup, format_set_after_read_shows_file_stamps_per_split_entry, backslash_line_still_splits_without_format, two_orphan_lines_are_two_entries_without_format, blank_line_is_skipped_without_format};issue1669 改后版在 base 上 2/0——其 unarmed 形状用的文件无前导时间戳,base 与 head 同输出,属重构保覆盖,非废钉(鉴别力由 issue3393 承担)。
  • lib 双边:head {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同 / 差集 ∅。
  • -D warnings:1 error:tests/issue523_compgen_quoted_tilde_regressions.rs unused import std::io::Write——不在本 PR diff,基线存量。
  • 卫生:rustfmt --check 五个改动文件全过。
  • CI:3 个红 job(GNU goldens、Rust tests、Rust tests macOS)红名单各只有 issue2875_input_procsub_child_runs_as_rubash_shell,全为基线。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release,探针 target/audit-w2/probe3393.sh(set -o history 后 5 阶段)。逐行一致:无 format 读入按物理行分裂;读后设 format 不重组;读前 HISTTIMEFORMAT=(空值也 arm)→ echo a\nb 合成一条;history -c+unset 后仍重组(粘滞);armed 状态下空行保留在条目内。

F 点:无

未跑/VOID 的腿:无(lib base 集复用同 merge-base 实跑结果)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:04
@caomengxuan666
caomengxuan666 merged commit 1cfba96 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.

history -r 无条件重组多行条目(GNU 仅当 HISTTIMEFORMAT 在读之前已设才重组)

1 participant