Skip to content

fix(terminal): bash 在数组 PROMPT_COMMAND 和回显 BASH_COMMAND 的 DEBUG trap 下仍只上报一次命令开始 - #2055

Merged
1lck merged 1 commit into
mainfrom
fix/bash-command-start-reports
Nov 16, 2026
Merged

1lck merged 1 commit into
mainfrom
fix/bash-command-start-reports

Conversation

@1lck

@1lck 1lck commented Nov 16, 2026

Copy link
Copy Markdown
Owner

关联 Issue / Linked issue

PR 类型 / Type

  • feat:新功能或用户可见的改进
  • fix:缺陷修复
  • docs:文档、Agent Note、Skill 等规则文件
  • release:稳定版发布准备

改动说明 / What changes

背景 / Why

从 #2010(终端接通本地 Shell 集成)开始,main 上每一轮 Desktop CI 的 “Vet and test on Linux” 都有两个测试失败:

  • TestIntegratedBashDebugTrapReportsEachCommandLineOnce:启动时命令开始标记报了 0 次。
  • TestIntegratedSystemBashReportsEachCommandLineOnce:启动时命令开始标记报了 2 次。

原因是 runner 已换成 Ubuntu 26.04。它自带 systemd 257 起提供的 /etc/profile.d/80-systemd-osc-context.sh,这个脚本:

  • 把 PROMPT_COMMAND 改成数组;
  • 在 PS0 前加 $(__systemd_osc_context_ps0)。

集成脚本会先读 /etc/profile,所以用户的机器只要是 Ubuntu 26.04、Fedora 42、Arch 等装了新版 systemd 的发行版,也会遇到同样的问题。CI 只是最先暴露出来。

改动 / Changes

desktop/internal/localshell/integration_scripts.go 的 bash 集成脚本:

  • PS0 改用提示符转义写 C 标记:原来写的是 PS0=$'\e]133;C…\a'${PS0-},现在改为 PS0='\e]133;C…\a'${PS0-}。
    • $'…' 在赋值语句的文本里就展开成了 ESC 和 BEL 字节。
    • 用户的 DEBUG trap 回显 BASH_COMMAND 时,会把一个带本会话令牌的 C 标记原样打到终端,终端就多记了一次“命令开始”。CI 上的 2 次就是这样来的。
    • PS0 显示时和 PS1 一样会解码 \e、\a,所以实际输出的标记不变。
  • DEBUG trap 路径同样处理数组形式的 PROMPT_COMMAND:
    • 原来遇到数组就整段跳过,一次命令开始都不报,CI 上的 0 次就是这样来的。
    • 现在和 PS0 路径一致,在数组首尾加入 __motty_disarm 和 __motty_arm。
    • 字符串形式的处理不变。Bash 5.1 之前只执行数组的第 0 项,这种组合下仍不上报,和原来一样。

desktop/internal/localshell/integration_unix_test.go:

  • 新增 commandStartProfiles,包含两种用户 profile:
    • 原有的字符串形式 PROMPT_COMMAND;
    • 照 systemd 脚本写法设置的数组形式 PROMPT_COMMAND,外加 PS0 命令替换。
    • 数组这种写在测试自己的 .bash_profile 里,不依赖宿主的 /etc/profile,所以在 macOS 上也能复现。
  • 新增 TestIntegratedBashReportsEachCommandLineOnce,用 PATH 中 4.4 以上的 bash 覆盖 PS0 路径。macOS 的 /bin/bash 是 3.2,原来的测试一直走不到 PS0 路径。
  • TestIntegratedBashDebugTrapReportsEachCommandLineOnce 改为对两种 profile 各跑一个子测试。
  • 新增 bashAtLeast 读取 bash 版本,带 10 秒超时:
    • 数组 profile 需要 Bash 5.1 以上,版本不够时跳过并写明原因;
    • 走 PS0 路径的测试需要 Bash 4.4 以上,同样跳过并写明原因。
  • checkCommandStartReports 额外断言 profile 里的 PROMPT_COMMAND 项和 PS0 内容照常运行。这两项文本由 printf 拼出来,避免被 DEBUG trap 回显的源码误命中。

shared/platform-feature-matrix/features/terminal-shell-integration.json:验证说明里加上这种情形和三个测试名,notes 里记一条。

.agents/notes/implemented/architecture/2026-11-16-local-shell-integration-shim.md:记录 bash 集成脚本和 Motty 不同的这两处、改动原因和对应的回归测试,避免以后同步 Motty 时被改回去。

不在本 PR 范围内 / Out of scope


类型专属:fix

复现与根因

  • 复现步骤、实际结果、期望结果:
    • 在 Ubuntu 26.04 上,用 Shell 集成启动 bash,第一个提示符出现时:
      • 走 DEBUG trap 路径时,实际没有命令开始标记,期望有 1 次;
      • 走 PS0 路径、且用户的 DEBUG trap 回显 BASH_COMMAND 时,实际有 2 次,期望有 1 次。
    • 在 macOS 上,往测试 home 的 .bash_profile 加入 systemd 的写法,用 Homebrew bash 5.3 可以稳定复现同样的 0 次和 2 次。
  • 根因:都在 feat(desktop): 终端接通本地 Shell 集成,提示符处调整大小只留一份提示符、点击命令行移动光标(Motty 203857c9、b2357d69) #2010(11eb12f9e)从 Motty 引入的 bash 集成脚本里。
    • DEBUG trap 路径默认 PROMPT_COMMAND 是字符串,遇到数组直接跳过。原注释写的是“数组只出现在有 PS0 的 5.1+”,但系统 profile 也会把它改成数组。
    • PS0 的赋值用了 $'…',标记字节直接出现在命令文本里。
  • 影响平台:macOS 与 Linux 的本地 bash,以及 WSL 里的 bash(它们用同一段脚本)。CI 上只有 Linux 失败,原因有两个:
    • macOS runner 没有 systemd;
    • macOS 的 /bin/bash 是 3.2,走不到 PS0 路径。

检查项

  • 已先写回归测试,确认它在修复前失败、修复后通过;测试路径与修复前的失败输出:desktop/internal/localshell/integration_unix_test.go。在 macOS 上把修复暂存起来、只保留新测试运行:
    --- FAIL: TestIntegratedBashDebugTrapReportsEachCommandLineOnce/array_prompt_command
        integration_unix_test.go:414: the shell did not announce command-start reporting once: 0
    --- FAIL: TestIntegratedBashReportsEachCommandLineOnce/string_prompt_command
        integration_unix_test.go:439: the shell did not announce command-start reporting once: 2
    --- FAIL: TestIntegratedBashReportsEachCommandLineOnce/array_prompt_command
        integration_unix_test.go:439: the shell did not announce command-start reporting once: 2
    
    恢复修复后,这几项全部通过。
  • [N/A] 无法自动化时,已写明手动复现与验证步骤:已经有自动化测试
  • 已搜索被修改函数、类型、契约字段的全部调用方(含另一平台、插件、SDK、脚本和按字符串分发的调用):bashIntegration 只被 startScript 使用。startScript 的调用方有两个:本地的 integrationScriptWith(macOS/Linux)和 wslIntegrationScript(Windows 上的 WSL),两者都用上了修复,WSL 脚本测试 TestWSLScriptStartsTheLoginShellInTheStartFolder 也通过。zsh 部分没有 DEBUG trap 和 PS0,不受影响。
  • 已检查另一平台是否有同类缺陷(有则本 PR 一并修复,或已提交 Issue):Windows 的 PowerShell 和 cmd 用另一套脚本(integration_windows.go),不涉及 PS0 和 PROMPT_COMMAND;WSL 的 bash 用同一段脚本,本 PR 一并修复。
  • 只改了修复所需的代码,没有夹带重构;修复过程中发现的其他问题已提交 Issue:[Bug] Linux 上关闭集成 bash 终端后,后台任务偶尔没有收到挂断而残留 #2054
  • 修复改变了行为约束或容易复发,已在 .agents/notes/implemented/bug-fix/ 记录:约束写在这块代码所属的 Shell 集成 Note(architecture/2026-11-16-local-shell-integration-shim.md)里,和 Motty 的其他差异放在一起,同步时只需要看一处

范围检查 / Scope checks

功能矩阵 / Feature matrix

  • 已更新 shared/platform-feature-matrix/features/<id>.json:两端的实现状态和验证状态、证据路径、owner、验证动作都如实填写;没有运行验证的保持 pending(terminal-shell-integration 的验证说明和 notes,状态不变)
  • 已运行 ./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh;改了平台实现路径的,还运行了 ./scripts/platform-feature-matrix/verify-platform-feature-matrix-change.sh origin/main HEAD
  • 没有提交 .artifacts/ 下生成的矩阵视图
  • [N/A] 没有更新矩阵的,理由:已更新

插件归属 / Plugin ownership

  • 已在仓库根目录运行 develop-lithe 第三节“插件归属与宿主零依赖”的自查命令,结果为空
  • 新增或修改的测试只使用测试内联的合成插件、Core 契约 fixture 或插件市场索引快照;没有读取具体插件的插件包、仓库检出、构建产物或清单,没有使用真实插件 ID,没有“缺少插件时跳过”的测试(不涉及插件)
  • 没有为具体插件提供的能力新建或更新宿主功能矩阵条目;宿主条目的证据和验证动作不引用插件的测试、插件包或插件仓库
  • 插件文案没有写进 shared/i18n/;插件的构建步骤、资源路径和补丁没有写进 docs/ci-builds.md、Agent Note 或 Skill
  • [N/A] 修改了通用扩展点、可能影响已发布插件的,已在受影响插件的仓库运行它的测试:没有改扩展点

UI 验证 / UI evidence

N/A:没有界面变化,只改了 Shell 启动脚本输出的终端控制序列。

统一日志系统 / Structured logging

N/A:没有新增或改动日志路径。

界面文案 / i18n

N/A:没有用户可见文案。

逻辑归属 / Logic placement

  • 可共享的逻辑(解析、校验、默认值和“自动”值的解析、状态规则、错误码等)在 Rust Core,并且共享契约和 fixture 先于平台接入;平台层只做原生 I/O 和呈现(Shell 启动脚本是平台进程启动的一部分,原本就在 localshell)
  • [N/A] 修改平台层已有的可共享逻辑时已迁入 Core:没有可共享逻辑
  • 工具或运行时专属的知识放在插件或成熟上游,宿主和 Core 没有按工具名加分支(bash/zsh 集成本来就由宿主终端负责,本 PR 没有新增分支)

性能与内存 / Performance and memory

N/A:Shell 启动多做一次 declare -p 判断;没有新增进程、缓存、定时器或依赖。

设置 / Settings

N/A:不涉及设置。

插件契约 / Plugin contract

N/A:不涉及插件契约。

发行包与资源 / Bundle and resources

N/A:启动文件仍写在 Lithe 新建的私有临时目录里,没有新增资源路径。

无用代码与重构 / Dead code and refactoring

  • 本次改动导致变成无用的代码已在本 PR 删除(DEBUG trap 路径里“数组就跳过”的空分支已删除)
  • [N/A] 本次之前就存在的无用代码或可重构之处,已提交带 refactor 标签的 Issue:没有发现

验证 / Verification

命令或操作 平台 结果
修复前只运行新测试(修复暂存) macOS arm64,Homebrew bash 5.3.9、/bin/bash 3.2.57 失败 3 项,分别是 0 次、2 次、2 次(见上)
go test -run 'TestIntegratedBash.*ReportsEachCommandLineOnce|TestIntegratedSystemBashReportsEachCommandLineOnce' ./internal/localshell macOS arm64 全部通过
./.agents/skills/write-stable-tests/scripts/verify-test-stability.sh --base origin/main macOS passed
run-go-tests-with-timing.mjs(与 test-stability-macos.sh 的参数相同,范围是整个 ./internal/localshell) macOS arm64 100 项全部通过;改动的测试耗时 210–490 ms,都低于 1 s 的告警线;报告在 .artifacts/test-stability/index.html
./scripts/desktop/desktop-go.sh test -count=1 ./internal/localshell/... ./internal/term/... ./internal/termview/... macOS arm64 全部 ok
./scripts/desktop/desktop-go.sh test -race -run ReportsEachCommandLineOnce ./internal/localshell macOS arm64 ok
go vet ./internal/localshell,GOOS=linux go vet ./internal/localshell macOS 通过
./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh、verify-platform-feature-matrix-change.sh origin/main HEAD macOS 通过
./scripts/agent-notes/verify-agent-notes.sh macOS ok: 162
插件零依赖自查 rg macOS 结果为空

未能执行的检查和原因:

  • 本机无法启动 Linux 环境:OrbStack 的数据目录属主是 root,需要 sudo 才能修复,这次没有绕过。Linux 上的验证以本 PR 的 CI(ubuntu-26.04)为准。
  • test-stability-macos.sh 会先扫描整个工作区,被本地另一个会话留下、未跟踪的 extension-host/node_modules 拦住了。所以这次单独运行了基于 --base origin/main 的静态检查,再用同样的参数直接运行计时脚本。
  • Windows 没有执行:本 PR 改的 bash 脚本在 Windows 上只给 WSL 用,相关测试 wsl_unix_test.go 只在 Unix 上编译,已经在 macOS 上通过。

提交前确认 / Final checks

  • 分支从最新的 main 创建;目标分支:feat、fix、docs 为 main,release 以发布流程要求为准
  • 以 Draft 提交,本地验证通过后才标记 ready
  • 没有无关改动,没有提交 .build/、target/、dist/、.artifacts/ 等产物,没有凭据、个人路径和未脱敏的日志或截图

🤖 Generated with Claude Code

…下仍只上报一次命令开始

Ubuntu 26.04(systemd 257 起的 /etc/profile.d/80-systemd-osc-context.sh)把
PROMPT_COMMAND 改成数组,并在 PS0 前放命令替换。集成先读 /etc/profile,于是:

- DEBUG trap 路径遇到数组整段跳过,一次命令开始都不报。现在与 PS0 路径一样
  在数组首尾加入 __motty_disarm / __motty_arm。
- PS0 路径用 $'...' 写标记,赋值语句文本里就带 ESC/BEL,用户 DEBUG trap
  回显 BASH_COMMAND 时伪造一次命令开始。改用 PS0 自己会解码的提示符转义。

回归测试在测试自己的 .bash_profile 里照 systemd 的写法设置,不依赖宿主
/etc/profile;新增 PS0 路径的 TestIntegratedBashReportsEachCommandLineOnce。
同步更新功能矩阵 terminal-shell-integration 与 Shell 集成 Agent Note。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ghfind-review ghfind-review Bot added the review: high ghfind author score; see https://ghfind.com label Nov 16, 2026
@1lck
1lck marked this pull request as ready for review November 16, 2026 14:18
@1lck
1lck merged commit aa276d6 into main Nov 16, 2026
6 of 16 checks passed
@1lck
1lck deleted the fix/bash-command-start-reports branch November 16, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: high ghfind author score; see https://ghfind.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant