Skip to content

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单 - #4

Merged
tallcode merged 5 commits into
mainfrom
claude/windows-bash-exit-code-d5760d
Jul 15, 2026
Merged

tallcode merged 5 commits into
mainfrom
claude/windows-bash-exit-code-d5760d

Conversation

@tallcode

Copy link
Copy Markdown
Collaborator

背景

起于 Windows 下 shell 工具"明明失败却显示已完成"、退出码恒为 0 的问题;调查后扩展为一组 Windows 平台正确性修复 + 工具改名。

改动

  • 重命名 bash 工具 → shell:工具名、Rust 符号、gRPC 命令、proto、TUI/CLI 客户端、GUI 工具类型匹配 + i18n 全部统一(保留字面 bash 二进制引用)。
  • Windows 执行/捕获修复(P0):新增 sandbox::shell_invocation 作为平台 shell 单一事实来源。PowerShell wrapper 用 & { } 脚本块(支持多语句)、执行前清 $LASTEXITCODE(消除 chcp 污染出的假 exit 0)、$Error 兜底命令未找到/cmdlet 失败、捕获 PowerShell 自身 stderr。
  • 认知层对齐(P1):工具描述与 os_hint 按运行时 shell 如实告知(pwsh 7 支持 &&/||,5.1 只准 ;;$env:VAR 而非 %VAR%);平台化文件写入禁令示例。
  • pwsh 7 优先 + -EncodedCommand(P2):PATH 上有 pwsh 则优先;wrapper 经 base64/UTF-16LE 传递绕开引号二次解析。
  • PowerShell 5.1 编码:改用无 BOM 的 UTF8Encoding($false),并设 $OutputEncoding(5.1 默认 ASCII)修复中文管道乱码。
  • Manual 层只读白名单:识别 PowerShell 只读 cmdlet(Get-ChildItem/Get-Content/Select-String…),basename 跨平台规约(\/.exe/大小写)。
  • 修复三处 GUI 退出码解析器:agent 早已用 [exit: N] 末行 footer,但 GUI 仍找 [exit code: N] 开头前缀 → 失败检测失效(即本次调查的直接根因)。含真实数据回归测试。

验证

  • agent:116→118 测试通过,x86_64-pc-windows-msvc 交叉编译(含测试)干净,fmt/clippy 干净。
  • gui:tsc 干净,vitest 167 通过,src-tauri 118 测试通过。
  • tui/cli:tsc 干净。

⚠️ Windows 端到端仍需真机实测。

🤖 Generated with Claude Code

tallcode and others added 5 commits July 15, 2026 18:59
Rename the "bash" tool to "shell" across all layers (agent tool name,
Rust symbols, gRPC bash/abort_bash commands, proto, TUI/CLI clients,
GUI tool-kind matching + i18n), so the model's strongest prior — the
tool name — no longer implies bash semantics on a platform where the
command is actually interpreted by PowerShell. Literal bash-binary
references (Unix `bash -c`, Seatbelt wrapper) are preserved.

Fix Windows execution/capture (P0): add sandbox::shell_invocation as
the single source of truth for how a command runs per platform. The
PowerShell wrapper now uses a `& { … }` script block (multi-statement
commands parse), clears $LASTEXITCODE before the command (kills the
chcp-polluted false [exit: 0]), falls back to $Error for command-not-
found / cmdlet failures that never set $LASTEXITCODE, sets UTF-8
console output encoding, and captures PowerShell's own stderr so parse
errors aren't silently dropped. merged_cmd no longer wraps a bash-ism
subshell on Windows.

Align the cognition layer (P1): the shell tool description and os_hint
now state the actual interpreter (PowerShell 5.1 on Windows: use `;`
not `&&`/`||`, $env:VAR not %VAR%), and remove the false "cmd syntax
is acceptable" claim.

Also fix three pre-existing bugs surfaced along the way: GUI exit-code
parsers (agentActivity.ts, persist.rs, store/runs.rs) still read the
old "[exit code: N]" prefix instead of the current "[exit: N]" footer,
so failure detection was dead; execute_shell hardcoded `bash -c`; and
several docs/comments claimed Windows runs via `cmd /c`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…-write prompt

P2 hardening of the Windows shell path:
- Prefer pwsh (PowerShell 7+) when on PATH, else Windows PowerShell 5.1.
  pwsh supports && / ||, defaults to UTF-8, and parses -EncodedCommand
  identically. Resolved once via a side-effect-free PATH scan (no spawn).
- Deliver the wrapper script via -EncodedCommand (base64/UTF-16LE) instead
  of -Command, eliminating the fragile Rust→CreateProcess→PowerShell quote
  re-parsing. Wrapper logic (script block, $LASTEXITCODE/$Error capture,
  UTF-8) is unchanged and split into a testable windows_wrapper_script.
- The cognition layer now tracks the resolved shell at runtime:
  sandbox::shell_display_name / shell_supports_chain_operators feed os_hint,
  so the chaining guidance (`;` only vs `;`/`&&`/`||`) matches the actual
  interpreter. The shell tool description is version-neutral to avoid
  contradicting it.

Prompt review — platform-split the one remaining bash-flavored instruction:
the "don't write files via shell redirection" guideline named tee/heredocs/
`cat > file`, which a PowerShell model can't map to Out-File/Set-Content.
It now lists the redirection forms the host shell actually has. All other
model-facing command examples were already platform-split or neutral.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…llow list

On Windows the model generates PowerShell, so the read-only auto-allow
whitelist (grep/ls/cat/git…) never matched the cmdlets it actually
emits (Get-ChildItem, Get-Content, Select-String, …), and every
read-only command fell through to a prompt on the Manual tier.

- Add WINDOWS_READONLY_PROGRAMS: read-only cmdlets and their built-in
  aliases (gci/dir, gc, sls, measure, findstr, …). Deliberately excludes
  writers (Set-/Add-/New-/Remove-/Out-File) and arbitrary-exec cmdlets
  (Invoke-/Start-/ForEach-Object/Where-Object — the latter also need a
  `{ … }` block, already rejected by the operator guard).
- Normalize the program basename cross-platform, matching the existing
  is_soft_fail_command precedent: split on both `/` and `\`, strip a
  `.exe` suffix, and lower-case (PowerShell resolves names case-
  insensitively). So `C:\Windows\System32\findstr.exe` and
  `get-content` both resolve correctly.

The operator guard (`{`, `` ` ``, `;`, `&`, `$(`, redirects) already
blocks PowerShell script blocks and subexpressions, so this only widens
recognition of bare read-only cmdlets — the conservative posture holds.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ll 5.1

The prior wrapper set `[Console]::OutputEncoding = [Text.Encoding]::UTF8`,
which left two PowerShell 5.1 encoding gaps:

- `[Text.Encoding]::UTF8` carries a BOM. On a redirected stdout (our
  captured pipe) PS 5.1 prepends it as a stray `EF BB BF`, which our
  `from_utf8_lossy` surfaces as a leading U+FEFF on every command's
  output. Switch all encodings to a BOM-less `UTF8Encoding($false)`.
- `$OutputEncoding` (encoding for strings piped INTO a native command's
  stdin) defaults to ASCII in 5.1, mangling non-ASCII to `?`. Set it to
  UTF-8 too, so `"中文" | some.exe` round-trips.

chcp 65001 (asks native children to emit UTF-8) is retained. Native
tools that ignore the code page and hard-code OEM/ANSI output remain
unfixable at this layer — those bytes degrade to replacement chars via
from_utf8_lossy rather than corrupting the capture. pwsh 7 already
defaults to BOM-less UTF-8, so these assignments are a no-op there.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
… failure" bug

Direct-from-session regression coverage for nonZeroExitCode: a failed
shell command (`future: command not found`, exit 127) rendered as
"completed" because the deployed parser matched a "[exit code: N]" prefix
while the agent writes an "[exit: N]" footer on the last line.

The added cases pin: the exact capture with leading output + a blank line
before the footer; the Windows form of the same failure (PowerShell
reports command-not-found as exit 1, not 127 — the parser keys on any
non-zero so both platforms are caught); rejection of the old prefix
format; and that the soft-fail exemption still keys exit 1 to bare
grep/findstr (Windows no-match) while a real command-not-found stays failed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@tallcode
tallcode merged commit 42e88e3 into main Jul 15, 2026
huichen pushed a commit that referenced this pull request Jul 16, 2026
…handling, and more

Bug fixes:
- Navigation observer: poll 500ms instead of 15s for non-navigating actions.
  One fix resolves click file:// timeout, selector click timeout, and press
  15s delay (bugs #1, #2, #3).
- Snapshot: collect text content from h1-h6, p, li, td, th, dt, dd, label,
  span, div, pre, code, blockquote (bug #4).
- CdpError: changed from plain object to class extends Error so it serialises
  as a readable message instead of '[object Object]' (bug #8).

Improvements:
- Snapshot: textarea/input name priority aria-label > placeholder > associated
  label > name > value[:30] (bug #10).
- Tabs: all actions now return { tabs: [...], tabCount: N } for consistent
  output (bug #6).
- Start: when endpoint already reachable, returns already_running with note
  instead of silently switching (bug #7).
- Type: returns both ref (typed) and resolved selector (bug #9).
- Scroll: new scroll command with direction/amount/ref arguments (bug #11).

Defense in depth:
- tools.ts catch block: extract .message from non-Error objects as fallback.
huichen pushed a commit that referenced this pull request Jul 16, 2026
…5760d

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单
huichen pushed a commit that referenced this pull request Jul 16, 2026
…handling, and more

Bug fixes:
- Navigation observer: poll 500ms instead of 15s for non-navigating actions.
  One fix resolves click file:// timeout, selector click timeout, and press
  15s delay (bugs #1, #2, #3).
- Snapshot: collect text content from h1-h6, p, li, td, th, dt, dd, label,
  span, div, pre, code, blockquote (bug #4).
- CdpError: changed from plain object to class extends Error so it serialises
  as a readable message instead of '[object Object]' (bug #8).

Improvements:
- Snapshot: textarea/input name priority aria-label > placeholder > associated
  label > name > value[:30] (bug #10).
- Tabs: all actions now return { tabs: [...], tabCount: N } for consistent
  output (bug #6).
- Start: when endpoint already reachable, returns already_running with note
  instead of silently switching (bug #7).
- Type: returns both ref (typed) and resolved selector (bug #9).
- Scroll: new scroll command with direction/amount/ref arguments (bug #11).

Defense in depth:
- tools.ts catch block: extract .message from non-Error objects as fallback.
huichen pushed a commit that referenced this pull request Jul 16, 2026
…5760d

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单
huichen pushed a commit that referenced this pull request Jul 16, 2026
…handling, and more

Bug fixes:
- Navigation observer: poll 500ms instead of 15s for non-navigating actions.
  One fix resolves click file:// timeout, selector click timeout, and press
  15s delay (bugs #1, #2, #3).
- Snapshot: collect text content from h1-h6, p, li, td, th, dt, dd, label,
  span, div, pre, code, blockquote (bug #4).
- CdpError: changed from plain object to class extends Error so it serialises
  as a readable message instead of '[object Object]' (bug #8).

Improvements:
- Snapshot: textarea/input name priority aria-label > placeholder > associated
  label > name > value[:30] (bug #10).
- Tabs: all actions now return { tabs: [...], tabCount: N } for consistent
  output (bug #6).
- Start: when endpoint already reachable, returns already_running with note
  instead of silently switching (bug #7).
- Type: returns both ref (typed) and resolved selector (bug #9).
- Scroll: new scroll command with direction/amount/ref arguments (bug #11).

Defense in depth:
- tools.ts catch block: extract .message from non-Error objects as fallback.
huichen pushed a commit that referenced this pull request Jul 16, 2026
…5760d

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单
huichen pushed a commit that referenced this pull request Jul 16, 2026
…handling, and more

Bug fixes:
- Navigation observer: poll 500ms instead of 15s for non-navigating actions.
  One fix resolves click file:// timeout, selector click timeout, and press
  15s delay (bugs #1, #2, #3).
- Snapshot: collect text content from h1-h6, p, li, td, th, dt, dd, label,
  span, div, pre, code, blockquote (bug #4).
- CdpError: changed from plain object to class extends Error so it serialises
  as a readable message instead of '[object Object]' (bug #8).

Improvements:
- Snapshot: textarea/input name priority aria-label > placeholder > associated
  label > name > value[:30] (bug #10).
- Tabs: all actions now return { tabs: [...], tabCount: N } for consistent
  output (bug #6).
- Start: when endpoint already reachable, returns already_running with note
  instead of silently switching (bug #7).
- Type: returns both ref (typed) and resolved selector (bug #9).
- Scroll: new scroll command with direction/amount/ref arguments (bug #11).

Defense in depth:
- tools.ts catch block: extract .message from non-Error objects as fallback.
huichen pushed a commit that referenced this pull request Jul 16, 2026
…5760d

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单
huichen pushed a commit that referenced this pull request Jul 16, 2026
…handling, and more

Bug fixes:
- Navigation observer: poll 500ms instead of 15s for non-navigating actions.
  One fix resolves click file:// timeout, selector click timeout, and press
  15s delay (bugs #1, #2, #3).
- Snapshot: collect text content from h1-h6, p, li, td, th, dt, dd, label,
  span, div, pre, code, blockquote (bug #4).
- CdpError: changed from plain object to class extends Error so it serialises
  as a readable message instead of '[object Object]' (bug #8).

Improvements:
- Snapshot: textarea/input name priority aria-label > placeholder > associated
  label > name > value[:30] (bug #10).
- Tabs: all actions now return { tabs: [...], tabCount: N } for consistent
  output (bug #6).
- Start: when endpoint already reachable, returns already_running with note
  instead of silently switching (bug #7).
- Type: returns both ref (typed) and resolved selector (bug #9).
- Scroll: new scroll command with direction/amount/ref arguments (bug #11).

Defense in depth:
- tools.ts catch block: extract .message from non-Error objects as fallback.
huichen pushed a commit that referenced this pull request Jul 16, 2026
…5760d

Windows shell 执行/退出码修复 + bash→shell 改名 + PowerShell 编码/白名单
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.

1 participant