Skip to content

[PR] fix(win-cmd): double trailing backslashes when quoting for cmd.exe - #6190

Closed
aftabkh4n wants to merge 5 commits into
ranxianglei:masterfrom
aftabkh4n:fix/win-cmd-trailing-backslashes
Closed

aftabkh4n wants to merge 5 commits into
ranxianglei:masterfrom
aftabkh4n:fix/win-cmd-trailing-backslashes

Conversation

@aftabkh4n

Copy link
Copy Markdown
Contributor

Problem

Windows .cmd/.bat shims (claude.cmd, dsh.cmd, …) are launched as comspec /d /s /c "<line>", and quoteWinToken builds that line. It wraps a whitespace-bearing token in quotes but leaves its trailing backslashes as they are. The program the shim starts parses 2n backslashes before a quote as n. That's the MSVCRT / CommandLineToArgvW rule, which splitWindowsCommandLine in claude-native-bootstrap.ts already implements. So:

The live round-trip tests in win-cmd-argv.test.ts only exercise the direct-spawn leg, which is why this went unnoticed on the comspec leg.

Change

  • quoteWinToken in launcher.ts, and its mirror in dsh-channel.ts: when wrapping in quotes, double the trailing backslash run (token.replace(/(\\+)$/, "$1$1")). This is the standard fix, the same one cross-spawn uses. Space-free tokens and non-trailing backslashes are unchanged.
  • winCmdUnsafeToken: drop the odd-trailing-backslash refusal, now unnecessary, and update the docstring.

Tests

  • tests/fix-679-windows-launch.test.ts: quoteWinToken doubles a trailing run and leaves everything else alone.
  • tests/win-cmd-argv.test.ts:
    • Exact comspec lines for planClientSpawn and planDshSpawn with odd and even trailing runs.
    • Odd and even trailing runs moved into the safe set.
    • The two cases that pinned the old refusal removed.

I couldn't install the repo's dev dependencies in my environment, so I didn't run the full suite, tsc or lint. What I did:

  • Ran the new and edited test blocks against src/win-cmd.ts and the exact quoteWinToken / splitWindowsCommandLine sources, using Node's built-in type stripping. They pass with this change, and the new ones fail on master.
  • Confirmed every edited file parses (node --experimental-strip-types --check).
  • Randomized round trip: 200,000 random tokens of letters, spaces, tabs and backslashes, through quoteWinToken and then splitWindowsCommandLine. With this change all 200,000 come back identical with none refused. On master, 11,034 of the guard-approved tokens come back changed and 54,174 are refused.

CI will be the first full run. Happy to adjust anything it flags.

CHANGELOG.md: entry added under [Unreleased] → Fixed.

On the comspec path (.cmd/.bat shims such as claude.cmd, and dsh.cmd),
quoteWinToken wrapped a whitespace-bearing token in quotes but left its
trailing backslashes as they were. The program the shim starts parses
2n backslashes before a quote as n (MSVCRT / CommandLineToArgvW, the
same rule splitWindowsCommandLine implements), so:

- an odd run ("list C:\My Projects\") was refused by winCmdUnsafeToken
  as "an odd number of trailing backslashes";
- an even run passed the ranxianglei#2196 guard as safe but arrived with half its
  backslashes ("C:\My Dir\\" -> "C:\My Dir\").

Doubling the trailing run inside the quotes makes both lossless, so the
odd-run refusal is removed. Round-tripping 200,000 random tokens of
letters, spaces, tabs and backslashes through quoteWinToken and
splitWindowsCommandLine now reproduces every token; before, 11,034 of
the tokens the guard accepted came back changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014TonJ1geSd97oAtSBZofUK
@ranxianglei ranxianglei changed the title fix(win-cmd): double trailing backslashes when quoting for cmd.exe [PR] fix(win-cmd): double trailing backslashes when quoting for cmd.exe Nov 11, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

Conflict resolved and pushed. The merge conflict between PR #6190 and master is cleared.

What I did this run:

  1. Verified live state first: PR head 67fc1ee (on fork aftabkh4n/billion-context), mergeable: False, mergeable_state: dirty — conflict confirmed.
  2. Followed the repo's established pattern for fork-PR conflicts: merge master into the PR branch (not rebase, so the contributor's commits/diff stay intact).
  3. Merged origin/master into the PR branch. Exactly one conflict: CHANGELOG.md — both sides had prepended entries under ### Fixed. Resolved by keeping ALL entries (the PR's Windows fix entry + master's newer entries), removing only the conflict markers. Verified no markers remained and that all PR code changes (src/launcher.ts, src/dsh-channel.ts, src/win-cmd.ts, both test files) were byte-identical post-merge.
  4. Pushed the merge to the fork branch.
  5. Master moved again mid-run, so I re-merged the latest master (clean — only 3 README files) and pushed again.

Final live state (verified against GitHub this run, not from memory):

  • head: b98067d27d
  • mergeable: True, mergeable_state: blocked — no longer dirty; "blocked" = CI re-running on the new head, not a conflict.
  • Check runs for the new head: 22 success, 3 in-progress, 11 queued, 11 skipped, 0 failures.

No red CI to fix (checks are passing/pending). The PR will transition to clean once the in-progress/queued checks finish. No design decision needed; no new issue filed.


八问(中文,供人工 reviewer):

  1. 是什么问题:PR [PR] fix(win-cmd): double trailing backslashes when quoting for cmd.exe #6190 与 master 产生合并冲突(mergeable_state=dirty),无法合并。
  2. 什么原因导致:PR 分支基于较旧的 master,期间 master 前进了多个提交,双方在 CHANGELOG.md 的 ### Fixed 段都新增了条目,产生文本冲突。
  3. 影响范围与严重程度:仅阻塞该 PR 合并;不影响已发布版本或线上行为。
  4. 怎么修复的:按仓库既有惯例(fork PR 冲突 = 把 master merge 进 PR 分支,保留贡献者提交而非 rebase),merge master 进 PR 分支;唯一冲突 CHANGELOG.md 采用「保留双方全部条目」的机械合并,仅删冲突标记;PR 的代码/测试改动逐文件核对未受影响。
  5. 是否有回归问题:无。合并只动 CHANGELOG.md(保留全部条目)+ 引入 master 的 README 变更;PR 的 6 处代码/测试改动与 merge 前逐字节一致,未改变任何既有行为。
  6. 是否破坏缓存:不涉及(无缓存/重试/游标/去重键逻辑改动)。
  7. 是否改了设计方向:不涉及(纯冲突消解,未改任何设计/数据流/边界)。
  8. 是否引入了新的配置:不涉及(无新 env/config/默认值)。

判定:冲突已消解并推送,PR 现为 mergeable(blocked=CI 在新 head 上重跑中,非冲突),无红 CI,无需决策。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

Conflict re-resolved and pushed. Master moved again since my last run, which re-dirtied the PR. I merged the current master tip into the PR branch and cleared the conflict.

What I did this run:

  1. Verified live state first: PR was mergeable: False / mergeable_state: dirty again (head unchanged from last run, but master had advanced).
  2. Root cause: master moved fad056c583 → 259cd87674 (~10 new commits) and prepended 2 new entries to CHANGELOG.md ### Fixed, colliding with the PR's entry at the same location.
  3. Merged origin/master (259cd87) into the PR branch. Single conflict: CHANGELOG.md (both sides prepended to ### Fixed). Resolved by keeping ALL entries (PR's Windows entry + master's 2 new entries + the pre-existing entries), removing only the 3 conflict markers.
  4. Verified the PR's core fix is intact: quoteWinToken trailing-backslash doubling in src/launcher.ts and src/dsh-channel.ts (token.replace(/(\\+)$/, "$1$1")), and the odd-run refusal removed from src/win-cmd.ts.
  5. Pushed to the fork (head 066a9172e0).

Final live state (verified against GitHub this run, not from memory):

  • head: 066a9172e0, base: 259cd87674 (current master tip)
  • mergeable: True, mergeable_state: blocked — no longer dirty; "blocked" = CI re-running on the new head.
  • Check runs: 9 success, 8 skipped, 13 queued, 0 failures.

⚠️ Note: master has been moving fast (advanced 3× across my two runs). The recurring conflict is purely the CHANGELOG "both sides prepend to ### Fixed" pattern — benign, mechanical. The PR is now synced to the current master tip, but if master adds more ### Fixed entries before this PR merges, it will go dirty again. Merging soon (or a re-trigger) will clear it.


八问(中文,供人工 reviewer):

  1. 是什么问题:PR [PR] fix(win-cmd): double trailing backslashes when quoting for cmd.exe #6190 再次与 master 冲突(mergeable_state=dirty)——上次消解后 master 又前进了。
  2. 什么原因导致:master 从 fad056c583 推进到 259cd87674(约 10 个新提交),向 CHANGELOG.md 的 ### Fixed 段顶部新增两条条目,与 PR 在同位置新增的 Windows 条目冲突(「双方都往段顶 prepend」的文本冲突)。
  3. 影响范围与严重程度:仅阻塞该 PR 合并;不影响已发布版本或线上行为。
  4. 怎么修复的:把当前 master 顶端(259cd87674)merge 进 PR 分支;唯一冲突 CHANGELOG.md 采用「保留双方全部条目」的机械合并,仅删 3 行冲突标记;PR 核心修复(quoteWinToken 尾反斜杠加倍、winCmdUnsafeToken 去掉奇数拒绝)逐文件核对未受影响。
  5. 是否有回归问题:无。合并只动 CHANGELOG.md(保留全部条目)+ 引入 master 的新提交;PR 的 6 处代码/测试改动与之前逐字节一致,未改变任何既有行为。
  6. 是否破坏缓存:不涉及(无缓存/重试/游标/去重键逻辑改动)。
  7. 是否改了设计方向:不涉及(纯冲突消解,未改任何设计/数据流/边界)。
  8. 是否引入了新的配置:不涉及(无新 env/config/默认值)。

判定:冲突已再次消解并推送,PR 现为 mergeable(blocked=CI 在新 head 上重跑中,非冲突),无红 CI,无需决策。注意 master 仍在快速前进,若合入前 master 再往 ### Fixed 加条目,PR 会再次变 dirty,需重触发或尽快合入。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

Update — CI is now fully green. (My previous reply above at Floor 15 already covered the conflict resolution; this is a status update since CI has since finished.)

Re-checked live state just now (not from memory):

  • head: 066a9172e0, base: 259cd87674 — master has not moved, so the conflict stays resolved (mergeable: True, not dirty).
  • Check runs on 066a9172e0: 36 success, 12 skipped, 0 failures — CI is complete and fully green (was 13 queued when I last looked).
  • mergeable_state: blocked — with all checks passing, this is now due to branch-protection / required-review gating, not CI and not a conflict.

Bottom line: the conflict is resolved and CI is green. The PR is ready to merge — the remaining blocked is the normal human review/approval gate, which is not mine to clear.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

No action needed — the live state shows no conflict and CI is green. (Re-verified against GitHub this run, not from the forwarded claim.)

Live state just now:

  • mergeable: True, mergeable_state: blocked — not dirty. The forwarded "merge state: dirty" is a stale mirror poll from before my push propagated; the conflict was already resolved in my earlier run (Floor 15).
  • head: 066a9172e0 (unchanged), base: 259cd87674 = current master tip — master has not moved, so nothing re-dirtied the PR.
  • Check runs on 066a9172e0: 36 success, 12 skipped, 0 failures — CI fully green.
  • mergeable_state: blocked with all checks passing = the human review/approval gate (branch protection), not CI and not a conflict. Not mine to clear.

Summary across my runs on this PR:

  1. Resolved the CHANGELOG.md both-prepend conflict by merging master into the PR branch (kept all entries), pushed to the fork — done.
  2. CI went running → fully green — done.
  3. This run: confirmed no conflict, CI green, master stable — nothing left to fix.

The PR is ready to merge; the only remaining gate is human review/approval. I did not re-run the merge because the live state already reflects a clean, green, mergeable PR.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

Fourth re-trigger — the live state is unchanged and there is no conflict to fix. (Re-verified against GitHub this run, not from the forwarded claim.)

Live state just now:

  • mergeable: True, mergeable_state: blocked — not dirty. No merge conflict exists.
  • head 066a9172e0 unchanged, base 259cd87674 = current master tip (master has not moved).
  • CI: 36 success, 12 skipped, 0 failures — fully green.

Why the "dirty" alerts keep firing: the live merge state is blocked, not dirty. blocked here means the PR is gated by required human review/approval (branch protection) — CI is green and there is no merge conflict. The blocker-detection treats any non-clean merge state as a "blocker", but this particular blocker is the human-review gate, which is outside CI scope and which I cannot and should not clear.

What this means: the PR is ready to merge. The only remaining step is a human reviewing/approving and merging it. I have no further CI action to take:

  • the conflict was resolved in my first run (merged master into the PR branch, kept all CHANGELOG.md entries, pushed);
  • CI is fully green;
  • master is stable (nothing re-dirtied the PR).

Repeated "dirty" alerts will keep firing until the PR is merged (or the detection stops treating blocked=review as a CI blocker). I did not re-run the merge because the live state already reflects a clean, green, mergeable PR — re-doing it would be redundant work.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

🔶 需要决策

PR #6190 is ready to merge — the only remaining gate is human review, and the CI-engineer lane keeps getting re-triggered on it. (Live-verified against GitHub this run; 5th check, state unchanged.)

Live state:

  • mergeable: True, mergeable_state: blocked — not dirty; no merge conflict.
  • head 066a9172e0 unchanged, base 259cd87674 = current master tip (master stable).
  • CI: 36 success, 12 skipped, 0 failures — fully green.

Why the CI-engineer lane keeps firing: the blocker-detection treats any non-clean merge state as a "blocker". Here the state is blocked, which means required human review/approval (branch protection) — CI is green and there is no conflict. That gate is outside CI scope and cannot be cleared by the CI engineer. I have already (1) resolved the CHANGELOG.md conflict by merging master into the PR branch and (2) confirmed CI green; there is no further CI work to do.

Options:

  • A — Review and merge the PR (recommended). It is ready: conflict resolved, CI fully green, master stable. Merging clears the blocked gate and stops the repeated CI-engineer triggers.
  • B — Request changes / close the PR, if the Windows cmd.exe trailing-backslash quoting fix is not wanted.

Recommendation: A.
Default if undecided: A — the PR is ready to merge; the natural next step is a human review + merge, which also ends the repeated auto-triggers.

(CI engineer stopping here per owner policy 2026-11-01 — this needs a human decision, not more CI work.)

The PR's exact-line tests pin the bytes quoteWinToken emits, but the
property being fixed — that those bytes decode back to the token under the
receiving program's CommandLineToArgvW rule — was only verified by a manual
200k-token probe. Add a committed round-trip (quoteWinToken ->
splitWindowsCommandLine) so the lossless guarantee is testable in CI and a
regression in either the doubling or the 2n->n halving is caught.

Review of ranxianglei#6190 (small finding fixed on the spot).

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

(ai: vllm-qwen/qwen3.8-27b)

APPROVE — correct, well-scoped, tests have teeth.

Correctness (traced, not assumed). The comspec leg builds comspec /d /s /c "<line>"; the receiving program parses it with the MSVCRT / CommandLineToArgvW rule — 2n backslashes before a quote → n (quote toggles), 2n+1 → n + literal quote. I traced that against splitWindowsCommandLine (src/claude-native-bootstrap.ts:232), which models the rule faithfully. Doubling the trailing run (token.replace(/(\\+)$/, "$1$1")) is exactly the inverse: the doubled run is even, so the receiver halves it back to the original. Verified standalone (no deps): the fixed quoters round-trip 6/6 representative tokens and 175k/175k randomized tokens through splitWindowsCommandLine; the pre-fix (master) quoters fail the odd case (backslash lost + quote swallowed) and the even case (halved). Both mirrors are updated identically (src/launcher.ts:6504, src/dsh-channel.ts:436), and dropping the odd-trailing refusal in src/win-cmd.ts is correct because the doubling makes that case lossless.

Tests. Real, not tautological: the exact-line pins (planClientSpawn/planDshSpawn) fail if the doubling regresses; the safe-set rows fail if the guard re-refuses odd trailing; the unit test pins the doubling itself.

Behavior change (disclosed, intended). Windows comspec leg only: a whitespace-bearing token ending in a backslash run — odd run: was refused → now verbatim; even run: was silently halved → now verbatim. Space-free tokens, interior backslashes, the direct-spawn (.exe) leg, non-win32, and every other #2196 refusal class are byte-identical (the existing #679 regression pin still passes). No cache/retry/cursor surface; no kernel/ changes; CI green.

已顺手修:补一条 comspec 尾随反斜杠 round-trip 测试(PR 的核心性质此前只有手工 200k 探针、未进 CI;现在 quoteWinToken → splitWindowsCommandLine 的无损往返在 CI 可测,加倍或 2n→n 砍半任一回归都会被抓住)+ commit d7f719b

Note (non-blocking): the PR has no linked issue; the body is self-sufficient and the e-work mirror carries the trace, but per house convention it would ideally reference a filed issue.


结构化八问(中文,供人工 reviewer)

  1. 是什么问题:Windows 上 .cmd/.bat shim 走 comspec /d /s /c "<line>",quoteWinToken 给含空格 token 加引号时不加倍尾随反斜杠,而接收端按 MSVCRT/CommandLineToArgvW 把「引号前 2n 个反斜杠」解析成 n 个——奇数尾随被 #2196 守卫拒掉、偶数尾随过了守卫却被砍半,两者都违反「安全 token 逐字通过」的契约。
  2. 什么原因导致的:quoteWinToken(launcher 与 dsh-channel 两处镜像)只加引号、没考虑 MSVCRT 的 2n→n 规则,尾随反斜杠在闭合引号前被砍半(偶)或吞掉引号(奇);#2196 守卫用「拒绝奇数尾随」打补丁,治标(拒绝普通 Windows 路径)且没修偶数砍半。
  3. 影响范围与严重程度:仅 Windows、仅 comspec 车道(claude.cmd/dsh.cmd/codex.cmd 等未升级到 node+entry 的 shim)。触发:含空格的 prompt/参数/profile 以反斜杠结尾(如 C:\My Projects\)。奇数→硬拒绝(启动失败、报可操作错误);偶数→静默损坏(参数砍半)。非全量不可用(.exe 直发与无空格 token 不受影响),但对常见输入形状是真实功能损坏。
  4. 怎么修复的:引号内把尾随反斜杠串加倍(token.replace(/(\\+)$/, "$1$1"),cross-spawn 同款标准修法),接收端 2n→n 砍半后正好还原;同步删掉 winCmdUnsafeToken 里已无必要的奇数尾随拒绝。
  5. 是否有回归问题:逐条过既有行为——无空格 token(原样裸输出、不加倍)✓;非尾随(内部)反斜杠(正则只匹配尾部 /(\\+)$/)✓;#2196 其余拒绝类(空/引号/%/元字符/换行)✓;#679 安全 argv 的 comspec 行字节级不变(既有回归钉测试仍过)✓;.exe 直发与非 win32 路径不动 ✓。高危模式(fail-fast 变吞错、阈值/时序/输出漂移)均无:守卫仍拒绝所有危险类,改动只让「更多」token 变安全(原先被拒/被坏的),从不让更少。行为变更已披露(见上)。
  6. 是否破坏缓存:不涉及——进程 argv 启动路径,无缓存/重试/游标/去重键。
  7. 是否改了设计方向:否——补完既有 #679/#2196 comspec 包裹设计(安全集合契约),把尾随反斜杠从「拒绝/砍半」变为「无损」,无架构/边界/数据流转向。
  8. 是否引入了新的配置:否——无新 env/config/默认值。

判定:可合(APPROVE)。 修法正确、独立验证过 round-trip、测试有牙、无 kernel 改动、CI 全绿。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] PR #6190(fix(win-cmd):cmd.exe 引号内加倍尾随反斜杠)审核完成 → APPROVE。修法正确:独立验证 round-trip(fixed 版本 6/6 代表 token + 175k 随机全过;master 奇数尾随丢反斜杠+吞引号、偶数砍半,均坏),两处 quoteWinToken 镜像同步改、winCmdUnsafeToken 删奇数拒绝正确;测试有牙(exact-line 钉 + safe-set + 单测);无 kernel 改动、无新配置、行为变更已披露;CI 全绿(18 过 / 12 跳过 / 0 红)。已顺手补一条 comspec 尾随反斜杠 round-trip 测试进 CI(PR 核心性质此前只有手工 200k 探针、未进 CI)+ commit d7f719b。mergeable_state=blocked,等人工合并。

@ranxianglei

Copy link
Copy Markdown
Owner

Landed via batch PR #6482 (merged) — closing as superseded.

@ranxianglei ranxianglei reopened this Nov 14, 2026
ranxianglei added a commit to Louie0806/billion-context that referenced this pull request Nov 14, 2026
The PR's exact-line tests pin the bytes quoteWinToken emits, but the
property being fixed — that those bytes decode back to the token under the
receiving program's CommandLineToArgvW rule — was only verified by a manual
200k-token probe. Add a committed round-trip (quoteWinToken ->
splitWindowsCommandLine) so the lossless guarantee is testable in CI and a
regression in either the doubling or the 2n->n halving is caught.

Review of ranxianglei#6190 (small finding fixed on the spot).

(cherry picked from commit d7f719b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants