Skip to content

fix(cosh-ng): keep debug trap on bash 4.2 prompts - #2757

Merged
kongche-jbw merged 1 commit into
alibaba:mainfrom
jfeng18:fix/cosh-ng/bash42-debug-trap
Aug 22, 2026
Merged

fix(cosh-ng): keep debug trap on bash 4.2 prompts#2757
kongche-jbw merged 1 commit into
alibaba:mainfrom
jfeng18:fix/cosh-ng/bash42-debug-trap

Conversation

@jfeng18

@jfeng18 jfeng18 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Why

On bash 4.2.x (e.g. Alibaba Group Enterprise Linux 7 line) the bash marker script permanently loses its DEBUG trap after the first prompt whenever PROMPT_COMMAND is set, so no preexec OSC markers are emitted from the second command on — silently losing the command audit trail. Closes #2736.

Mechanism: bash 4.x suspends the DEBUG trap while PROMPT_COMMAND runs, so trap -p DEBUG prints nothing inside the hook. The snapshot block in _cosh_prompt_command read that empty output and cleared _COSH_ACTIVE_DEBUG_TRAP; the subsequent eval "$active_debug_trap" in the preexec handler then re-installed nothing, and the freshly restored trap was permanently dropped.

What changed

  • cosh-shell/src/shell_host/marker/bash.rs (_cosh_prompt_command): the trap-snapshot block is now gated on (( BASH_VERSINFO[0] >= 5 )). On bash < 5 the snapshot is skipped entirely — suspension guarantees the trap cannot change while the hook runs, so the snapshot is meaningless there and its empty read is actively harmful. The bash 5.x execution path is byte-identical (the gate only short-circuits the 4.x branch); the snapshot block body itself is unchanged.

Related issue

closes #2736

User / Agent impact

On bash 4.2 hosts with PROMPT_COMMAND set (e.g. the AGL7 audit-hook default), command-level OSC markers, command blocks, and exit codes are captured again from the very first command instead of silently disappearing after the first prompt. bash >= 5 hosts see no change.

Risk and compatibility

  • Public CLI, API, configuration, or documented behavior changed
  • Privileged or security-sensitive behavior changed
  • Cross-component contract changed
  • Migration or rollback guidance is needed

Low risk: the change is a version gate in front of one snapshot block; on bash >= 5 the gate is always true. One trade-off: on bash < 5, a user hook that itself installs a new DEBUG trap would no longer be re-snapshotted — but bash < 5 suspends the trap during the hook anyway, so any trap swap could only take effect after the hook returns, which is exactly the window the preexec re-arm path already owns.

Validation

  • Discriminating bidirectional evidence on the failing test (local machine, bash 4.2.46 — the unique repro environment):
    • pre-fix on a clean main baseline: cargo test -p cosh-shell --test shell_host shell_host_runs_bash_pty_and_emits_command_events FAILED (assertion failed: ledger.blocks.iter().any(...) at marker.rs:73 — no command block, matching fix(cosh-ng): bash marker permanently loses DEBUG trap after first prompt when PROMPT_COMMAND is set (bash 4.2) #2736)
    • post-fix (this PR): the same test passes
    • full marker module on the same bash 4.2 box: baseline 43 passed / 15 failed → fixed 53 passed / 5 failed; the remaining 5 failures are a strict subset of the baseline 15 (pre-existing bash 4.2 issues out of scope here), so the fix strictly reduces failures with zero regressions
  • ECS (Linux, bash 5.2.15, RUSTUP_TOOLCHAIN=1.89.0): cargo test -p cosh-shell --lib — 1347 passed / 0 failed; cargo test -p cosh-shell --test shell_host (full suite) — 160 passed / 0 failed / 1 ignored; cargo fmt --all -- --check — clean; cargo clippy -p cosh-shell --all-targets -- -D warnings — exit 0
  • Known limitation, stated plainly: the GitHub Actions runners run bash 5.x, so CI cannot exercise the bash 4.2 code path; the local bash 4.2.46 fail→pass evidence above is the substitute for that coverage.

Documentation and rollback

None. Revert the single commit to restore the previous behavior on bash 4.2 (bash 5.x behavior is identical either way).

bash 4.x suspends the DEBUG trap while PROMPT_COMMAND runs; the
snapshot logic misread the suspended empty output as a user-cleared
trap and permanently dropped it after the first command; skip the
snapshot on bash < 5 where suspension guarantees the trap is
untouched; bash 5.x path unchanged.

Closes alibaba#2736
Fixes: 0ad6b4b

Assisted-by: Qoder
Signed-off-by: Jiangtian Feng <jiangtianf97@163.com>
@jfeng18
jfeng18 requested a review from SunnyQjm as a code owner August 21, 2026 15:50
@github-actions github-actions Bot added the component:cosh-ng src/cosh-ng label Aug 21, 2026

@qoderai qoderai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • bash 4.2 行为假设合理,但建议在注释中更精确地限定适用版本,减少未来兼容扩展时的误用风险。
  • 版本门逻辑本身清晰,未发现功能或安全层面的回归风险。
  • 当前修改范围有限,未在审查范围内发现其它需要调整的问题。

🤖 Generated by QoderView workflow run

Comment thread src/cosh-ng/crates/cosh-shell/src/shell_host/marker/bash.rs
@kongche-jbw
kongche-jbw self-requested a review August 22, 2026 09:06

@kongche-jbw kongche-jbw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review baseline: a4cb23ea27c9645d7294b7afe65309262e6a0e4b...732502d2f4405431f7aca57a8733b70730683f66

未发现阻塞问题。

Remaining risks:

  • 精确的 bash 4.2.46 前后对照仍依赖 PR 中报告的本地验证。

Verification scope:

  • 复查了完整 diff、相邻 DEBUG/PROMPT_COMMAND 状态机、PTY 测试及既有 review thread。
  • 使用源码构建的 bash 4.2.0 与系统 bash 5.2.21 分别运行两项相关 PTY 测试,均通过。
  • 当前 cosh-ng CI、release build、fast checks、PR lint 与 CLA 均通过。

@kongche-jbw
kongche-jbw merged commit 271bd99 into alibaba:main Aug 22, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cosh-ng): bash marker permanently loses DEBUG trap after first prompt when PROMPT_COMMAND is set (bash 4.2)

2 participants