From 4f09a52f7c644f027266a032697ee8ef762627e7 Mon Sep 17 00:00:00 2001 From: bolichen Date: Sat, 7 Nov 2026 05:48:47 +0000 Subject: [PATCH] fix: escape % in the pi gate launcher's cmd paths on windows --- src/kiro_crew/acp/harness/pi.py | 22 +++++++++++++++++++++- test/test_acp_pi_backend.py | 9 +++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/kiro_crew/acp/harness/pi.py b/src/kiro_crew/acp/harness/pi.py index 0f47095355fe..47320e78e019 100644 --- a/src/kiro_crew/acp/harness/pi.py +++ b/src/kiro_crew/acp/harness/pi.py @@ -546,6 +546,25 @@ def _publish_gate_artifact( return target +def _pi_cmd_literal(path: str) -> str: + """*path* as literal text inside a quoted ``.cmd`` token. + + ``cmd.exe`` expands ``%NAME%`` in a batch file even inside quotes, so ``%`` + doubles. A double quote or a newline cannot be expressed there at all and is + refused (the rule of ``pod/windows.py`` ``_cmd_literal``). + + shortcut: the launcher's own path is not covered -- ``cmd.exe /c`` expands a + defined ``%NAME%`` in it before the batch runs; upgrade if a data home needs one. + """ + if '"' in path or "\r" in path or "\n" in path: + raise AcpError( + f"the pi gate launcher cannot name {path!r}: it holds a double quote or a " + "newline, which cmd.exe cannot express in a batch file. Point " + "KIROCREW_HOME and the pi install at paths without them." + ) + return path.replace("%", "%%") + + def _pi_gate_launcher_body(pi_bin: str, extension_path: str, program: tuple[str, ...] = ()) -> str: """The launcher pi-acp is told to run in place of ``pi``. @@ -557,7 +576,8 @@ def _pi_gate_launcher_body(pi_bin: str, extension_path: str, program: tuple[str, pi's floor (:func:`~kiro_crew.acp.harness.pi_node.choose_pi_node`, POSIX only). """ if platform_compat.IS_WINDOWS: - return f'@echo off\r\n"{pi_bin}" %* {_PI_EXTENSION_FLAG} "{extension_path}"\r\n' + pi_lit, ext_lit = _pi_cmd_literal(pi_bin), _pi_cmd_literal(extension_path) + return f'@echo off\r\n"{pi_lit}" %* {_PI_EXTENSION_FLAG} "{ext_lit}"\r\n' run = " ".join(shlex.quote(part) for part in (program or (pi_bin,))) return f'#!/bin/sh\nexec {run} "$@" {_PI_EXTENSION_FLAG} {shlex.quote(extension_path)}\n' diff --git a/test/test_acp_pi_backend.py b/test/test_acp_pi_backend.py index eb788762fcc3..43940c56cf74 100644 --- a/test/test_acp_pi_backend.py +++ b/test/test_acp_pi_backend.py @@ -337,6 +337,15 @@ def test_the_windows_body_is_a_cmd_that_forwards_arguments(self, monkeypatch): assert "%*" in body assert '"C:\\tools\\pi.cmd"' in body and '"C:\\site\\gate.ts"' in body + def test_the_windows_body_keeps_a_percent_in_either_path_literal(self, monkeypatch): + """cmd.exe expands ``%OS%`` even inside quotes, so pi would not be found.""" + monkeypatch.setattr(acp_client.platform_compat, "IS_WINDOWS", True) + body = _pi_gate_launcher_body(r"C:\a%OS%b\pi.cmd", r"C:\x%y\g.ts") + assert body == '@echo off\r\n"C:\\a%%OS%%b\\pi.cmd" %* --extension "C:\\x%%y\\g.ts"\r\n' + quoted = 'C:\\q"\\pi.cmd' + with pytest.raises(AcpError, match=re.escape(repr(quoted))): + _pi_gate_launcher_body(quoted, r"C:\x\g.ts") + def test_the_launcher_is_written_once_and_lives_in_the_run_dir(self, monkeypatch, tmp_path): run_dir = tmp_path / "run" run_dir.mkdir()