Skip to content

fix(history): GNU -a/-c/-r semantics end the bash-it theme history interlock - #250

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/bashit-history-interlock
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/bashit-history-interlock

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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:

  1. history -a re-appended the WHOLE in-memory list to $HISTFILE. GNU appends only what the file lacks (bashhist.c appends history_lines_this_session); the grown file was then re-read by history -r — self-amplifying growth.
  2. 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 on every cycle.
  3. 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 file is written by -a/-w explicitly.

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): new LiveFileBackedHistory::clear_memory snapshots and restores the file bytes around the in-memory clear, refreshing the file signature so refresh_if_stale does not reload what the theme just cleared.
  • replace (history -d, interactive recording veto): deliberately keeps the truncating rewrite semantics.

Tests

  • 5 new unit tests in history.rs covering each GNU semantic and the fixed-point of repeated -a/-c/-r cycles (18 history unit tests green).
  • New ConPTY regression 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.
  • No regression: full niubash-runtime lib suite (400+), tests/interactive.rs (40 passed), issue191 suite.

…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
caomengxuan666 merged commit cce8ad4 into master Oct 10, 2026
3 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/bashit-history-interlock branch October 10, 2026 05:51
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.
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.

[P0][hang] bash-it 主题(codeword/gitline)交互 source 后卡死:每提示符 history -a/-c/-r 循环与引擎 history 持久化互锁

1 participant