Repository navigation
fix(history): GNU -a/-c/-r semantics end the bash-it theme history interlock - #250
Merged
Merged
Conversation
…terlock (closes #182) bash-it themes (codeword/gitline) run `history -a && history -c && history -r` from PROMPT_COMMAND on every prompt. Three provider-level defects turned that cycle into a self-amplifying loop that doubled the history file each prompt and wedged the interactive session: - `history -a` re-appended the WHOLE in-memory list to HISTFILE (GNU appends only what the file lacks, bashhist.c); the grown file was then re-read by `history -r` — O(2^n) growth. - `history -r` re-fed the file through per-entry saves; reedline's sync appends its unwritten buffer verbatim (no dedupe), so the file re-appended itself. It now rebuilds the reader from disk (histfile.c read_history_range only ever appends to the in-memory list), leaving the file byte-identical. - `history -c` routed through reedline's clear, which also deletes the backing file (GNU clears the in-memory list only, builtins/history.def:185). The provider now snapshots and restores the file bytes; `replace` (history -d, the interactive veto) keeps the truncating rewrite. The ConPTY interlock regression drives 12 prompt cycles of the exact theme pattern with a deadline and asserts the history file holds each command exactly once.
caomengxuan666
added a commit
that referenced
this pull request
Oct 10, 2026
…notes draft (#254) - Version 1.3.4 -> 1.4.0 (workspace root + crates/niubash-runtime; Cargo.lock synced; internal niubash-runtime dep requirement aligned 1.2.5 -> 1.4.0). The rubash engine requirement stays 1.3.4: it is semver-open (^1.3.4 admits the engine's 1.4.0), and the committed [patch] resolves it to the local ../rubash worktree, whose Cargo.toml still says 1.3.4 until the engine cuts its own release. - CHANGELOG: the Unreleased section becomes [1.4.0] - 2026-10-10. Also repairs the committed merge damage from #243 (stray conflict marker and the #241 doctor-advice entry glued onto the carapace entry) and folds in the merged cycle: #235 #239 #246 #247 #250 #251 #252 #253 plus a compact rubash 1.4.0 engine summary. #240's P2-batch content stays under [1.3.4] where the file already records it (no dup). - docs/release-notes-1.4.0-draft.md: zh+en draft release notes (Added / Fixed / Performance), winuxcmd #1151/#1154-#1157 marked as in-flight for the coordinator, plus the local verification log: release build PASS (niu.exe 10.6 MB), MSIX packed unsigned with a placeholder identity (5.1 MB), wpm-source main carries all four preinstall packages, and cargo publish --dry-run chain findings (rubash -> niubash-runtime -> niubash, blocked on the engine's crates.io release). No tag, no release workflow trigger, no in-flight PRs merged.
caomengxuan666
added a commit
that referenced
this pull request
Oct 12, 2026
…ion end - the missing exit half of #250's -c semantics (#265) 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.
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.
Closes #182.
Problem
bash-it themes (codeword/gitline) run
_save-and-reload-history(history -a && history -c && history -r) from PROMPT_COMMAND on every prompt. Three host-history defects turned that cycle into a self-amplifying loop that doubled the history file each prompt and wedged the interactive ConPTY session:history -are-appended the WHOLE in-memory list to $HISTFILE. GNU appends only what the file lacks (bashhist.c appendshistory_lines_this_session); the grown file was then re-read byhistory -r— self-amplifying growth.history -rre-fed the file through per-entry saves; reedline'ssyncappends its unwritten buffer verbatim (no dedupe), so the file re-appended itself on every cycle.history -crouted through reedline'sclear, which also deletes the backing file. GNU clears the in-memory list only (builtins/history.def:185); the file is written by-a/-wexplicitly.Fix (crates/niubash-runtime/src/history.rs)
append_history(-a): dedupe against the current file contents; no-op when nothing is new.read_history(-r) /read_new_history(-n): fast path rebuilds the reedline reader from disk (histfile.c read_history_range only ever appends to the in-memory list), leaving the file byte-identical; per-entry fallback kept for foreign paths / degraded memory-only sessions.clear(-c): newLiveFileBackedHistory::clear_memorysnapshots and restores the file bytes around the in-memory clear, refreshing the file signature sorefresh_if_staledoes not reload what the theme just cleared.replace(history -d, interactive recording veto): deliberately keeps the truncating rewrite semantics.Tests
history.rscovering each GNU semantic and the fixed-point of repeated-a/-c/-rcycles (18 history unit tests green).tests/issue182_bashit_history_interlock.rs: drives 12 prompt cycles of the exact theme pattern with per-cycle deadlines, asserts the session stays interactive and the history file holds each command exactly once (bounded, no per-cycle growth). Stable across repeated runs.niubash-runtimelib suite (400+),tests/interactive.rs(40 passed), issue191 suite.