Skip to content

fix(history): carry history_base + hist_last_line_added through the HistoryProvider contract (#2007, #2006) - #2144

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2007-2006-history-numbering
Oct 29, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2007-2006-history-numbering

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Two history-face bugs from the niubash#670 PTY audit, same root: the HistoryProvider contract (the niu REPL bridge) carried less state than the engine's own session history, so the provider path could not reproduce GNU faces the session path already matches (PR #2021 pins).

Base: d5c172ae (origin/master tip at branch time).

Commit 1 — rubash#2007: HISTSIZE trimming renumbers history from 1

PTY evidence p3-gnu-04 vs p3-hist-04: GNU prints 399/400/401 after HISTSIZE=3; niu renumbered 1/2/3.

  • GNU spec: lib/readline/history.c:113 (history_base = 1); history.def display + history.def:203-216,243-260 (history -d positive operands offset by history_base); stifling advances the base (history.c:389-404 stifled add_history, history.c:675-698 stifle_history) — global numbering never restarts.
  • Fix (issue candidate solution A): HistoryProvider::history_base() with the unstifled GNU initial value as default; provider display numbering and history -d operand offset consume it instead of hard-wired 1.

Commit 2 — rubash#2006: fc -l lists the fc command itself and the whole history

PTY evidence p3-gnu-03 vs p3-hist-03: GNU's fc -l excludes the fc line; niu listed everything including 6 fc -l.

  • GNU spec: builtins/fc.def:328-335 last_hist = i - rh - hist_last_line_added ("the current command line is already part of the history list... we check hist_last_line_added"); bashhist.c:174,957-964 (hist_last_line_added set by really_add_history); fc.def:403-421 edit-form bash_delete_last_history (bashhist.c:392-417).
  • Fix: HistoryProvider::last_line_added() (default = the C zero initializer); the fc provider call site stops hard-coding history_base=1, last_line_added=false, remember_on_history=false — the bridge is the interactive recording path (shell.c:1830-1842 init_interactive arms remember_on_history), and the edit-form drop now reaches the host store via the existing history_provider_drop_last.

Both defaults mirror GNU's C initializers, so out-of-tree providers keep compiling and behaving as before; hosts following the save-at-accept contract override the channels.

Tests

tests/issue2006_2007_history_faces.rs:

  • provider_path_history_numbering_survives_stifle — un-ignored (the acceptance step the issues adjudicated; the fixture adopts the channels explicitly since a stifled store's base is host-private state), plus new provider_path_history_delete_offset_uses_base (commit 1).
  • provider_path_fc_list_excludes_itself — un-ignored, plus a base-aware fc -l range pin (commit 2).
  • Session-path pins unchanged and green.

A/B on base d5c172ae: both provider pins fail pre-fix with exactly the issue outputs (3\t fc -l listed; 1/2/3 renumbering); post-fix all pins pass. Pre-existing environment-bound failure issue1830 interactive_histfile_null (Windows path-mangled diagnostic) fails identically on the pristine base — not attributed to this PR.

Disclosure: commits are --no-verify — the cargo fmt --check hook cannot run in this shell (cargo fmt: os error 206, filename too long); rustfmt --edition 2021 --check was run explicitly over every changed file and is clean.

Fixes #2007
Fixes #2006

rubash#2007: after HISTSIZE trimming, the provider (niu REPL) path
renumbered `history` output from 1; GNU keeps the global continuous
numbering (PTY evidence p3-gnu-04 vs p3-hist-04: 399/400/401, not
1/2/3). The session path already matches GNU (PR #2021 pins); the
provider path could not, because the HistoryProvider contract carried
no display base.

GNU specification:
- lib/readline/history.c:113 `int history_base = 1` — the list number
  of entries[0]; builtins/history.def display_history numbers entries
  from it, and history.def:203-216,243-260 offset positive `history -d`
  operands by history_base.
- history.c:389-404 (stifled add_history): dropping the oldest entry
  bumps history_base; history.c:675-698 (stifle_history) sets it to the
  dropped count. The global numbering survives trimming; it never
  restarts at 1.

Fix (the issue's candidate solution A):
- HistoryProvider gains `history_base()` with the unstifled GNU initial
  value (1) as the default impl; hosts that stifle their store override
  it with the advanced base (src/history.rs).
- The provider branch of the history builtin fetches the channel once
  and uses it for both the display numbering
  (builtins/history.rs execute_with_history gains the parameter; the
  session path keeps its own shell.base) and the positive `history -d`
  operand offset (executor/job_builtins.rs), replacing the hard-wired
  base=1.

Tests (tests/issue2006_2007_history_faces.rs): the provider-path
stifle pin is un-ignored and passes (store base 4 -> display 4,5,6);
new pin covers provider `history -d 5` against base 4. The #[ignore]
removal is the acceptance step the issue adjudicated; the fixture
adopts the channel explicitly because a stifled store's base is
host-private state no default can synthesize.

A/B: the pre-fix failure ("    1  echo h3..." vs "    4 ...") and the
post-fix pass were both produced from this branch base d5c172a
(origin/master tip). One environment-bound pre-existing failure
(issue1830 interactive_histfile_null, Windows path-mangled diagnostic)
fails identically on the pristine base and is not attributed here.

Commit disclosed as --no-verify: the pre-commit `cargo fmt --check`
hook cannot run in this shell (cargo fmt: os error 206, filename or
extension too long); rustfmt --edition 2021 --check was run explicitly
over all four changed files and is clean.

Fixes #2007
…contract

rubash#2006: on the provider (niu REPL) path, `fc -l` listed the fc
command itself and the whole history; GNU excludes the fc line (PTY
evidence p3-gnu-03 vs p3-hist-03: GNU's list ends at `echo G2`, niu
printed `6  fc -l` too).

GNU specification:
- builtins/fc.def:328-335: "the current command line ... is already
  part of the history list by the time we get to this point ... we
  check hist_last_line_added" — `rh = remember_on_history || ...;
  last_hist = i - rh - hist_last_line_added`.
- bashhist.c:174 (`int hist_last_line_added;`, zero-initialized),
  cleared per line by maybe_add_history (bashhist.c:757-764), set when
  the line was actually recorded by really_add_history (957-964).
- rh is ON for the interactive shapes the formula targets: shell.c
  init_interactive arms remember_on_history (shell.c:1830-1842).
- The not-listing edit form deletes the fc line from THE history list
  first (fc.def:403-421 -> bash_delete_last_history, bashhist.c:392-417).

The provider call site hard-coded all three inputs (history_base=1,
last_line_added=false, remember_on_history=false), so last_hist
pointed at the fc line and the default listing swept the whole store.
Fix: HistoryProvider gains `last_line_added()` (commit 1 added the
base channel); the fc provider branch passes the two channels plus
rh=true — the bridge replaces the engine's interactive recording, so
the interactive default applies — and the edit-form drop reaches the
host store via the existing history_provider_drop_last when no engine
session owns the list. Both trait defaults mirror the GNU C
initializers, so providers predating the contract keep compiling and
behave exactly as before.

Tests (tests/issue2006_2007_history_faces.rs): the provider-path fc
pin is un-ignored and passes (store [alpha, beta, fc -l], last_added
-> output "1 alpha / 2 beta", no fc line); new pin covers a stifled
store (base 5): default `fc -l` numbers 5,6,7 and excludes the fc
line, `fc -l 6 8` resolves through the base.

A/B: pre-fix failure ("3\t fc -l" listed; whole history) reproduced on
this branch's base; post-fix all pins pass. Environment-bound
pre-existing failures (issue1830/issue1852 session-path diagnostics,
Windows path-mangled text) fail identically on the pristine base and
are not attributed here.

Commit disclosed as --no-verify: the pre-commit `cargo fmt --check`
hook cannot run in this shell (cargo fmt: os error 206, filename or
extension too long); rustfmt --edition 2021 --check was run explicitly
over the three changed files and is clean.

Fixes #2006
@caomengxuan666
caomengxuan666 merged commit 6641b50 into master Oct 29, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2007-2006-history-numbering branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant