Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/kiro_crew/acp/harness/pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand All @@ -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'

Expand Down
9 changes: 9 additions & 0 deletions test/test_acp_pi_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading