Skip to content

fix(api): close_with_report conveys the close-boundary state as a value (#2039) - #2158

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/2039-close-status
Oct 30, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/2039-close-status

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #2039

Contract gap

Session::close() (src/api/session.rs, 1.5.0 API) consumed the session and returned only the i32 final status, with no face that conveys session state at the close boundary. Any host contract that reads session state AFTER close was forced to snapshot before closing — and a pre-close snapshot is structurally blind to the EXIT trap's own effects. That is the structural premise of the niu #668 cwd regression (niu commit 208be63 had to split its finish funnel into trap-then-close composition to keep the post-trap read alive).

GNU specification (status half, unchanged)

third_party/bash/trap.c:1027 run_exit_trap() saves the entry status into trap_saved_exit_value (trap.c:1036) and returns it (trap.c:1052, 1094) — a bare exit inside the EXIT trap keeps the saved value. Oracle (WSL GNU Bash 5.3.x, script file): trap 'exit 7' EXIT -> 7; trap 'cd /tmp' EXIT; exit 5 -> 5.

Fix (the issue's primary candidate form: close reports the final state as a value)

  • CloseReport (#[non_exhaustive], the RunOutcome growth discipline): status (the value close() returns) + cwd (executor.session_cwd() — public_accessors.rs:1228, the engine cd-machinery state the §5.3 Session::cwd face reports — read AFTER script_driver::finish_shell returns, i.e. after the EXIT trap ran; script_driver.rs:5401 orders history write -> job flush -> EXIT trap -> debugger note -> procsub close, and the last two do not write cwd).
  • Session::close_with_report(mut self) -> CloseReport runs the identical exit path and reads the close-boundary state off the still-alive executor before the session drops.
  • Session::close(self) -> i32 keeps its exact 1.5.0 signature and now delegates to close_with_report().status — byte-identical return value; source compatibility preserved for out-of-tree hosts. No trait needed extension (close is an inherent method), so the fix(history): carry history_base + hist_last_line_added through the HistoryProvider contract (#2007, #2006) #2144 defaulted-trait-method pattern is not applicable here. The pre-close-callback alternative stays with the contract batch, per the issue (候选形态在该批统一裁).

Tests (tests/issue2039_close_final_state_regressions.rs, public rubash::api surface only)

  • close_with_report_conveys_the_exit_trap_cwd: pre-close session.cwd() reports the pre-trap directory (the only read the old API allowed — cannot see the trap); close_with_report().cwd reports the post-trap directory (engine-canonical via a trap-free control session).
  • close_with_report_status_is_the_exit_path_resolution: trap exit 7 -> 7; no override -> entry status.
  • close_keeps_the_i32_status_face: plain close() unchanged (exit-7 -> 7; trap side effect lands).

Results (release, Windows): issue2039 3/3 PASS; --lib 1063 passed / 0 failed / 3 ignored; --test session_api 11 passed + 1 environment-bound failure (build_run_and_state_views_round_trip, \\?\ verbatim-path mismatch) that fails IDENTICALLY on the pristine base 2503977 — pre-existing, not attributed to this diff.

A/B vs pristine

Pristine detached worktree at 2503977: the new face is absent (grep count 0) and --test session_api shows the identical failure signature as the branch — every other pin, including close_runs_exit_trap_and_persistent_procsub_delivery, passes on both sides, so close() behavior is A/B-identical.

Transport + fmt disclosure

  • Commit disclosed as --no-verify: the pre-commit cargo fmt --check hook cannot run in this shell (cargo fmt: os error 206, filename or extension too long — same precedent as fix(history): carry history_base + hist_last_line_added through the HistoryProvider contract (#2007, #2006) #2144); rustfmt --edition 2021 --check was run explicitly over all three changed files and is clean.
  • The branch was landed via the GitHub Git data API (blobs -> tree -> commit -> ref) because the local git push transport kept resetting through the flaky local proxy (10+ attempts). Integrity is pinned by object identity: the API-created commit's tree 476a6c0f0269a132990b7bfce75a545c59e98975 is byte-identical to the locally committed tree (git rev-parse HEAD^{tree}), and each blob sha equals the local git hash-object value. Local worktree commit: b8dec2dc (same tree).

…ue (#2039)

rubash#2039: Session::close() consumed the session and returned only the
i32 final status, with no face that conveys session state at the close
boundary. Any host contract that reads session state AFTER close was
forced to snapshot before closing — and a pre-close snapshot is
structurally blind to the EXIT trap's own effects. That is the
structural premise of the niu #668 cwd regression (the --cwd-state
recorder must read the final directory after the EXIT trap, because a
trap's cd is the session's last cwd write; niu's repair 208be63 had to
split its finish funnel into trap-then-close composition to keep the
read alive).

GNU specification (the exit path's status half, unchanged by this fix):
- third_party/bash/trap.c:1027 run_exit_trap() saves the entry status
  into trap_saved_exit_value (trap.c:1036) and returns it
  (trap.c:1052, 1094) — a bare `exit` inside the EXIT trap keeps the
  saved value. Oracle (WSL GNU Bash 5.3.x, script file):
  `trap 'exit 7' EXIT` -> 7; `trap 'cd /tmp' EXIT; exit 5` -> 5.

Fix (the issue's primary candidate form: close reports the final state
as a value; the pre-close-callback alternative stays with the contract
batch, per the issue's "候选形态在该批统一裁"):
- CloseReport (#[non_exhaustive], the RunOutcome growth discipline):
  status (the value close() returns) + cwd (session_cwd() —
  public_accessors.rs:1228, the engine cd-machinery state the §5.3
  Session::cwd face reports — read AFTER script_driver::finish_shell
  returns, i.e. after the EXIT trap ran; script_driver.rs:5401 orders
  history write -> job flush -> EXIT trap -> debugger note -> procsub
  close, and the last two do not write cwd).
- Session::close_with_report(mut self) -> CloseReport runs the
  identical exit path and reads the close-boundary state off the
  still-alive executor before the session drops.
- Session::close(self) -> i32 keeps its exact 1.5.0 signature and now
  delegates to close_with_report().status — byte-identical return
  value, source compatibility preserved for out-of-tree hosts. No
  trait needed extension (close is an inherent method), so the
  #2144 defaulted-trait-method pattern is not applicable here.

Tests (tests/issue2039_close_final_state_regressions.rs): the
EXIT-trap cd pin (pre-close cwd() reports the pre-trap directory —
the only read the old API allowed — while close_with_report().cwd
reports the post-trap directory, engine-canonical via a trap-free
control session), the status-resolution pin (exit 7 override,
entry-status survival), and the close() delegation pin. Existing
session_api close pins unchanged and green.

A/B vs pristine 2503977: the face is absent on the base (grep count
0) and --test session_api passes on both sides (close() behavior
identical); see REPORT.md for the run outputs.

Commit disclosed as --no-verify: the pre-commit `cargo fmt --check`
hook cannot run in this shell (cargo fmt: os error 206, filename or
extension too long); rustfmt --edition 2021 --check was run
explicitly over all three changed files and is clean.

Fixes #2039
@caomengxuan666
caomengxuan666 merged commit 58dc562 into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2039-close-status branch October 30, 2026 01:41
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.

Session::close() 无最终状态回传面:close 后需读会话状态的宿主契约被迫 close 前自快照(#668 cwd 回归的结构性前提)

1 participant