Skip to content

fix(startup): #422+#430 nits — posix $ENV exit skips the login greeting tail; rbash×loader pin; typeahead comment truth; clippy cast - #432

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/422-430-nits
Oct 19, 2026

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #422, closes #430. Four small items bundled, no behavior beyond the one early return.

#422-1 — posix $ENV exit now early-returns before the login tail (shell.rs)

The login posix branch ran update_completion_state / finish_startup / greeting hooks after execute_env_file("ENV") had already ended the invocation with a top-level exit — inconsistent with the sibling profile-exit and rc-exit paths, which return the jump status immediately. Under sh -l -i the ENV file's exit could still emit greeting output before the session ended.

GNU oracle (WSL GNU Bash 5.3.0, scratch HOME, profile sets PS1):

  • sh -l -i with $ENV = echo ENV-RAN; exit 7 → prints ENV-RAN, exits 7, the first prompt never renders.
  • Control (ENV without the exit) → prompt renders, EOF logout.
  • sh -l -i -c with the same ENV → marker, exit 7, command line never runs.

Fix: return Some(status) straight out of the ENV gate, before the login tail (same shape as the profile/rc exit paths).

Double proof on the local side:

  • lib pin (login_posix_env_exit_skips_the_greeting_startup_tail): a greeting hook that would export GREETING_RAN must NOT run after the jump; plus a no-exit control that must still run it. Mutation-checked: reverting the early return fails the pin.
  • ConPTY interactive probes (tests/interactive.rs, sh.exe face + -l, REPL dispatch): ENV exit ends the session with status 7 and zero P1> renders (PS1 staged inside the ENV file so the no-prompt assertion is meaningful on a login face); the control reaches the prompt and answers a command line.

#422-2 — rbash × generated-loader combo pin (tests/rc_readonly_loader.rs, now 14 tests)

rbash_named_session_loads_the_generated_loader_then_restricts (NIU_INVOKED_AS=rbash, -i -c): loader body runs before the rc and before restrictions arm (a HISTFILE= assignment first in the loader body fails readonly if the ordering ever moves), body sees loaded|rc| + restricted $-, the five maybe_make_restricted variables (PATH/SHELL/ENV/BASH_ENV/HISTFILE) each fail readonly in subshells, and startup stays zero-write (config inventory, rc identity, loader bytes).

#430-1 — typeahead reinjection comments say tail, not head

WriteConsoleInputW appends behind the pending events (MSDN); the old wording ("exactly as if they had waited in the queue the whole time", "BACK at the head of the input stream") misled on ordering. Corrected in typeahead_guard.rs (module doc + disarm_and_reinject) and the repl.rs requeue docs (unread field, Unread alias, enter_opens_chunk, the #420 test doc, with_scripted_queue): the difference is benign — canonical wedge has an empty queue (tail = head); a multi-line burst migrates the requeued empty-line CR to the burst's end while non-empty lines stay strictly ordered. The scripted test queue's push_front is now documented as modeling the head semantics — slightly stronger fidelity than production's tail append.

#430-2 — repl.rs 0x0D as u16 cast removed

clippy::unnecessary_cast; the literal infers u16. (Clippy is not a CI gate — noted in the issue — and master carries pre-existing clippy findings elsewhere; no new lints are introduced by this diff.)

Verification

  • cargo test -p niubash-runtime --lib --locked -- --test-threads=1 → 554 passed / 0 failed (552 + 2 new pins; one unrelated flaky failure in an earlier run, two subsequent full runs green)
  • cargo test --test interactive --locked -- --test-threads=1 → 83 passed / 0 failed (81 + 2 new probes)
  • cargo test --test startup_files --locked -- --test-threads=1 → 29 passed / 0 failed; rc_readonly_loader → 14 / 0
  • cargo fmt --all --check clean; RUSTFLAGS="-D warnings" cargo build + cargo test --no-run clean

…loader pin; typeahead tail-append comment truth; drop a clippy cast (closes #422, closes #430)
@caomengxuan666

Copy link
Copy Markdown
Member Author

Independent review: APPROVE-WITH-NITS

Verified independently on d5b5748 (fresh detached worktree; base 7b4ee73 = merge-base with master, single commit, diff = exactly the 5 declared files — no smuggled changes, no pin/version churn, no process-kill or license red-line content).

#422-1 — posix $ENV exit skips the login tail: correct

  • Shape congruence holds. All three startup-file jumps now return Some(status) before the login tail: profile (shell.rs:1157-1164), rc (1234-1245), and ENV (1179-1190). NIU_REPL_STARTUP is already consumed before ENV runs (unset at 1166 on the profile-success path), so the ENV exit leaves no marker behind — the inline unsets the other two paths do are not needed here. Both dispatchers (run_startup_rc shell.rs:998, run_interactive_startup_rc :1079) run finish_startup() unconditionally after run_startup_files, so the rbash/-r activation write can never be lost to the early return; a session taking the jump is terminating anyway.
  • WSL oracle re-run by me (GNU bash 5.3.0 invoked as sh via symlink, scratch HOME, $ENV = path to file): sh -l -i + exit 7 in the ENV file → rc 7, only ENV-RAN printed, prompt never rendered, no logout; the no-exit control renders the prompt and exits via EOF. Exactly the behavior this fix implements.
  • Mutation re-run by me: reverting the early return turns login_posix_env_exit_skips_the_greeting_startup_tail RED on the greeting assertion (control stays green). The lib pin has teeth.
  • Nit: the two new ConPTY probes are mutation-inert for the tail-skip — I reverted the fix and issue422_sh_login_repl_env_exit_ends_session_before_startup_tail still passes (rc 7 + no prompt are pinned, and both held under the old code too; only greeting-after-ENV differs, which the probe's expects don't observe). The end-to-end jump contract is still worth pinning, but the regression teeth for the tail live in the lib pin.
  • Nit: the comments describe the oracle as $ENV = "echo ENV-RAN; exit 7" — literally that would source a file named echo ENV-RAN; exit 7 (GNU execute_env_file takes a filename, FEVAL_ENOENTOK). The tests themselves correctly use file paths, so this is shorthand imprecision in prose only.

#422-2 — rbash × generated loader: real-face pin, correct

  • NIU_INVOKED_AS=rbash is the shim-forwarded argv[0] channel (rubash#598), consumed by Shell::new (shell.rs:231-235) into the same invoked_as_rbash mark armed at finish_startup (981-983) — the genuine invocation-name face, not the -r flag face; the fix(startup): a shell invoked as rbash starts restricted (#400) #413 argv0-face concern is satisfied. The interactive side goes further and spawns a true argv0 copy (sh.exe copied from niu.exe) for the ENV probes.
  • The ordering pin is well-constructed: HISTFILE= first inside the loader doc proves the loader body ran while the restricted gates were still off; NIU_LOADER_MARK|NIU_RC_MARK proves loader-before-rc; the five readonly assignments each assert status 1. Zero-write assertions cover all three claimed surfaces (recursive ~/.niubash inventory, rc bytes+len+mtime, loader bytes verbatim). 14/14 pass.

#430 — both nits correct

  • MSDN checked: "WriteConsoleInput places input records into the input buffer behind any pending events in the buffer" — the tail-append reading in typeahead_guard.rs and the repl.rs unread docs is accurate, and the scripted queue's push_front is honestly labeled as modeling head semantics with slightly stronger fidelity than production.
  • Cast 0x0D as u16 removed; base→head clippy diff under current stable shows the unnecessary_cast warning on repl.rs eliminated.

Regression (all self-run, serial per the repo's pinned RUST_TEST_THREADS=1)

  • niubash-runtime --lib: 554/0. --test interactive: 83/0 (5 ignored; the two new probes individually confirmed). --test startup_files: 29/0. --test rc_readonly_loader: 14/0. cargo fmt --all -- --check: clean.
  • Full workspace, bidirectional: base 7b4ee73 1026 passed / 0 failed vs head d5b5748 1031 passed / 0 failed — delta is exactly the 5 new tests, zero failures either side (the ~37-44 local-failure baseline from earlier rounds did not reproduce here; this machine is green on both commits).

Nits (non-blocking)

  1. Under current stable clippy (1.99) the PR adds two new warnings in its own new lib tests — shell.rs:6375/6416 &env_file.to_string_lossy().into_owned(): into_owned is redundant (&Cow<str> derefs to &str); drop it for &env_file.to_string_lossy(). CI has no clippy gate and the repo's rust-version is 1.95, so toolchain-relative — fine to leave or fix in a follow-up.
  2. The $ENV = "echo ENV-RAN; exit 7" comment shorthand (see above) — one clarifying word ("file containing") would prevent a future reader from "simplifying" a test into the broken form.
  3. Consider a follow-up that gives the ConPTY probe teeth for the tail (e.g., stage a greeting hook that emits a sentinel and assert it does not appear after ENV-RAN).

Conclusion: APPROVE-WITH-NITS. The #422-1 semantics match the GNU oracle, the pins are real-face and the load-bearing one is mutation-verified, and both commits-in-one is fully green against base.

@caomengxuan666
caomengxuan666 merged commit 76f148b into master Oct 19, 2026
11 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 19, 2026
Conflict resolution (crates/niubash-runtime/src/repl.rs, two hunks):

1. Unread type doc — both sides kept: #432's MSDN tail-append framing is
   the primitive's truth (WriteConsoleInput APPENDS, it cannot prepend);
   the merged text states that #417's face wraps that primitive with a
   drain-and-rewrite (requeue_console_input) so the re-queued events land
   at the head slot, the position the probe took them from.

2. win_unread record construction — the #417 general synthesis kept
   (fields.*); #432's clippy-cast deletion survives inside it (the
   '0x0D as u16' cast no longer exists anywhere — UnicodeChar comes from
   a u16 field; the necessary char casts live in key_press_records).

Stale #432 derivations of the old tail-append behavior updated to the
compensated face (comments still cite the MSDN append as the primitive):
the unread field doc (tail-write claim -> drain-and-rewrite, and why the
#417 writeback needs the head slot: a plain append would shred a burst
into 'ead...'), with_scripted_queue (head slot is now a production
guarantee, not a test-only fidelity upgrade), enter_opens_chunk (the
requeued partner CR is the next submit nothing has to drain past), and
typeahead_guard::requeue_console_input's doc (same primitive as the
guard's reinjection; the guard can settle for the bare append — its
queue is empty at reinjection — this face buys the head slot).

Verification (this host, Win10 19045, --test-threads=1) on the merge
result: runtime lib 560/0; interactive 85/0 (5 ignored, unix-only legs;
includes #432's two new tests and both #417 echo-proof regressions);
issue373 3/0; cargo fmt --check clean; RUSTFLAGS='-D warnings' cargo
check clean. niubash#434 (control-chord synthesis arm) intentionally NOT
touched here — separate ticket.
@caomengxuan666
caomengxuan666 deleted the fix/422-430-nits branch October 21, 2026 08:14
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