Skip to content

fix(history): session-end write carries the GNU exit history to the host file - #265

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/host-history-provider-contract
Oct 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/host-history-provider-contract

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Root cause

tests/host_contract.rs::history_and_fc_use_the_host_history_provider (the only red in #256's fourth-round CI) panicked at its final assertion (host_contract.rs:89): after a one-shot niu -c "history -c" the host history file still held first command\nthird command\n instead of the empty string the host contract pins.

This is the other half of the #182 bash-it interlock. #250 moved the provider's history -c to the GNU semantics the interlock needs — clear the in-memory list only (builtins/history.def:185), snapshot-and-restore the file bytes so the theme's per-prompt -c && -r cycle can reload them — but did not add bash's other half of -c: the exit write. In bash, exit_shell() (bashhist.c) writes the final history list to $HISTFILE when a session that remembers history ends (overwrite unless histappend), so a list that -c emptied truncates the file at exit. The -c builtin itself never touches the file in bash either. The host contract's "file is empty after -c" outcome was the old truncate-on-clear behavior (assertion from 5ad24ec, before #250) — the test is not stale; the implementation was missing the exit half.

Fix

crates/niubash-runtime/src/history.rs only. The provider now performs the session-end write when it drops (every rc-0 session end — -c, stdin script, script file, REPL exit/Ctrl-D — tears down normally, the same path the pre-existing drop-time sync already relies on):

  • final list empty -> truncate the backing file (the host contract's empty file); a session without history still creates nothing;
  • final list non-empty -> one reedline sync of its unwritten buffer, which after the staleness refresh is exactly the set the file lacks (the -a dedupe). A blind overwrite would roll back lines another concurrent shell appended, and the bash-it -a && -c && -r cycle must stay a fixed point (niubash#182).

The append deliberately routes through reedline's own sync instead of a raw file append: reedline's FileBackedHistory syncs its unwritten buffer again on Drop, so a raw append leaves that accounting stale and the drop-time sync re-appends the same lines (found and avoided during this work).

Failures warn once via log::warn and never reach stderr (the -c one-shot stderr stays byte-stable). Sessions terminating through std::process::exit (nonzero -c statuses, the exit-jump path) skip Drop and keep the pre-clear file, the same as the pre-existing drop-time sync.

Proof

  • Reproduced on master: assertion left == right failed, left: "first command\nthird command\n", right: "" at host_contract.rs:89.
  • After the fix: cargo test --test host_contract -> 43 passed; 0 failed (includes the fixed test); cargo test -p niubash-runtime --lib history:: -> 15 passed; 0 failed.
  • Four new unit tests pin the behavior: cleared-list truncate at session end, no rollback of lines another shell appended after a mid-session -c, dedupe re-append after a transient (read-only) sync failure, and the degraded memory-only session creating no file.
  • Full cargo test --no-fail-fast failure set is identical to pristine master by name: the only local failures are the ConPTY-driven suites (interactive 35, issue182_bashit_history_interlock 1, smoke_1_3_0 1), each timing out at the first-prompt cursor-position handshake (driver.rs:635) on this machine; each binary was re-run on pristine master and fails on exactly the same names. CI (which has a working PTY) is where those run green.

Note on the bridge audit leads (why the scope stays surgical)

The host-bridge audit's leads were checked against the empirical failure state and do not explain this red:

  1. SessionHistory shadows the provider — refuted by the test's own passing steps: -s landed in the file (assert :77-79) and -d rewrote it (asserts :83-85), so the provider owned the list; rubash also only auto-creates a session when no provider is installed (job_builtins.rs:1602).
  2. HISTFILE unset on the session path — session-path only; the -c invocation itself passed assert_success (:88) before the content assert panicked.
  3. Raw-fs fast paths bypassing reedline's fd_lock — a real concurrent-session hardening item, but this assert is single-process sequential; this PR deliberately adds no new raw-fs write. Follow-up material, not mixed in here.
  4. fc -s not writing back to the provider — the test uses fc 2 listing (provider entries, asserts :69-72 pass), not fc -s persistence.
  5. Stale assertion at host_contract.rs:57/89 — matches the blame (assertion predates fix(history): GNU -a/-c/-r semantics end the bash-it theme history interlock #250), but the right resolution is completing the implementation with the GNU exit write (done here) rather than editing the contract: in bash the file genuinely does end up empty after a -c session.

…ost file

The only red in #256's fourth-round CI, tests/host_contract.rs
history_and_fc_use_the_host_history_provider, panicked at its final
assertion: after a one-shot `niu -c "history -c"` the host history
file still held the pre-clear lines, not the empty file the host
contract pins.

#250 (niubash#182) moved the provider's `history -c` to the GNU
semantics the bash-it interlock needs: clear the in-memory list only
(builtins/history.def:185), snapshot-and-restore the file bytes so the
theme's per-prompt `-c && -r` cycle can reload them. The host
contract's "file is empty after -c" outcome was the old
truncate-on-clear behavior (the assertion predates #250; blame
5ad24ec). Both halves are right about their own side; what niu was
missing is bash's OTHER half of `-c`: the exit write. bashhist.c's
exit_shell() writes the final history list to $HISTFILE when a session
that remembers history ends (overwrite unless histappend), so a list
that `-c` emptied truncates the file at exit - the `-c` builtin itself
never touches the file in bash either.

Fix (crates/niubash-runtime/src/history.rs only): the provider now
performs that session-end write when it drops - every rc-0 session end
tears down normally, the same path the pre-existing drop-time sync
already relies on:

- final list empty -> truncate the backing file (the host contract's
  empty file); a session without history still creates nothing;
- final list non-empty -> one reedline sync of its unwritten buffer,
  which after the staleness refresh is exactly the set the file lacks
  (the -a dedupe). A blind overwrite would roll back lines another
  concurrent shell appended, and the bash-it `-a && -c && -r` cycle
  must stay a fixed point.

The append deliberately routes through reedline's own sync instead of a
raw file append: reedline's FileBackedHistory syncs its unwritten buffer
again on Drop, so a raw append leaves that accounting stale and the
drop-time sync re-appends the same lines.

Failures warn once via log::warn and never reach stderr, like every
history-file write. Sessions that terminate through std::process::exit
(nonzero -c statuses, the exit-jump path) skip Drop and keep the
pre-clear file, the same as the pre-existing drop-time sync.

Proven: host_contract 43/43 green including the fixed test; runtime lib
15/15 history unit tests green; four new unit tests pin the cleared-list
truncate, the concurrent-shell no-rollback, the dedupe re-append after a
transient (read-only) sync failure, and the degraded memory-only no-op.
@caomengxuan666
caomengxuan666 merged commit b4ebb1a into master Oct 12, 2026
8 of 9 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 12, 2026
…it funnel

CI root cause for the red `history_and_fc_use_the_host_history_provider`
on this branch's Windows lane — and on master itself since #272: the
one-shot exit funnel (`finish_one_shot`) ends every `niu -c` route through
`std::process::exit`, which skips Drop, so the Drop-based session-end
write (#265 — the only carrier of a GNU `history -c` to the file, the
builtin clearing memory only, niubash#182) never fired on the -c path and
`niu -c 'history -c'` silently kept the stale history file.

Before #272 the funnel hard-exited only on nonzero statuses; rc=0
returned normally through main, the Shell unwound, the Drop flushed, and
the test was green. #272 collapsed both arms into the `-> !` funnel and
removed the rc=0 unwind without noticing it was load-bearing.

Fix: Shell keeps a handle to its RubashHistoryProvider and the funnel
calls `flush_session_history()` explicitly before the hard exit — the GNU
shape anyway (exit_shell -> write_history, bashhist.c). Best-effort like
every history-file write: no stderr, no rc change; interactive sessions
that unwind keep the Drop as a second net.

Evidence: master's own CI (run 38196596891, c3df161) is red with the
identical assertion at tests/host_contract.rs:89; this commit turns the
test green locally on a machine where the Drop path never fires, and the
full-suite failure set drops to exactly the pre-existing
environment-bound set.
@caomengxuan666
caomengxuan666 deleted the fix/host-history-provider-contract branch October 13, 2026 02:06
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…1.4.0 section (#326)

- The [Unreleased] entries (#284/#287, #293-#304, #313) move into
  [1.4.0]; [Unreleased] is empty again.
- Newly recorded (today's wave): Git Bash compatible factory default
  prompt (#292, headline), WT-form shortcuts (#290/#211), the paste
  burst trailing-Enter fix (#308/#291), self-update SHA256 + release
  pinning (#311/#312), managed-block convergence (#324), completion
  tables/NIU_COMPDEFS/NIU_ENV probe (#305-#307), -s on-demand stdin
  (rubash#586 host half), bash/rbash invocation faces (#321/#322).
- Older gaps recorded: closed-stdout pipe contract (#245/#286), history
  session-end write-back (#265), sh POSIX contract (#262), PWD persona
  alignment (#283), one-shot fast path (#270), --cwd-state (#272),
  --encoded-command (#271), REPL-command highlighting (#281), docs
  passes (#255/#225/#226/#227), MSRV (#267), easter-egg prune (#264).

CHANGELOG-only change; no version claims.
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