Repository navigation
fix(history): session-end write carries the GNU exit history to the host file - #265
Merged
Merged
Conversation
…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
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.
This was referenced Oct 12, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-shotniu -c "history -c"the host history file still heldfirst command\nthird command\ninstead 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 -cto 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 && -rcycle 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$HISTFILEwhen a session that remembers history ends (overwrite unlesshistappend), so a list that-cemptied truncates the file at exit. The-cbuiltin 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.rsonly. 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):-adedupe). A blind overwrite would roll back lines another concurrent shell appended, and the bash-it-a && -c && -rcycle must stay a fixed point (niubash#182).The append deliberately routes through reedline's own sync instead of a raw file append: reedline's
FileBackedHistorysyncs its unwritten buffer again onDrop, 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::warnand never reach stderr (the-cone-shot stderr stays byte-stable). Sessions terminating throughstd::process::exit(nonzero-cstatuses, the exit-jump path) skipDropand keep the pre-clear file, the same as the pre-existing drop-time sync.Proof
assertion left == right failed, left: "first command\nthird command\n", right: ""at host_contract.rs:89.cargo test --test host_contract-> 43 passed; 0 failed (includes the fixed test);cargo test -p niubash-runtime --lib history::-> 15 passed; 0 failed.-c, dedupe re-append after a transient (read-only) sync failure, and the degraded memory-only session creating no file.cargo test --no-fail-fastfailure set is identical to pristine master by name: the only local failures are the ConPTY-driven suites (interactive35,issue182_bashit_history_interlock1,smoke_1_3_01), 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:
-slanded in the file (assert :77-79) and-drewrote 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).HISTFILEunset on the session path — session-path only; the-cinvocation itself passedassert_success(:88) before the content assert panicked.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.fc -snot writing back to the provider — the test usesfc 2listing (provider entries, asserts :69-72 pass), notfc -spersistence.-csession.