fix(llm): sync CodeBuddy models when the CLI path has a space on Windows

On Windows, fetch_codebuddy_models ran the CLI as
["cmd.exe", "/d", "/s", "/c", list2cmdline(cli_args)]. The inner
list2cmdline quotes a path with a space, and the outer one (asyncio
joining the argv) escapes those quotes as \", which cmd.exe does not
understand. With the npm shim under C:\Users\John Smith\AppData\Roaming
the sync failed with "is not recognized as an internal or external
command".

Run the shim directly, as on POSIX; Windows starts a .cmd through
cmd.exe itself. The creation flags are kept.
This commit is contained in:
RRFZ
2026-10-31 21:46:01 -04:00
parent f7aed0744c
commit f50d2ff4b9
2 changed files with 55 additions and 2 deletions

View File

@@ -596,7 +596,10 @@ async def fetch_codebuddy_models(api_key: str | None = None) -> list[str]:
]
process_kwargs: dict[str, Any] = {}
if os.name == "nt":
command = ["cmd.exe", "/d", "/s", "/c", subprocess.list2cmdline(cli_args)]
# Windows launches a .cmd shim through cmd.exe on its own. Wrapping the
# line in "cmd.exe /d /s /c" quoted it twice, so a CLI path with a space
# (C:\Users\John Smith\AppData\Roaming\npm\codebuddy.cmd) reached cmd.exe
# as \"...\" and was "not recognized as an internal or external command".
# Windows-only constants; the stubs omit them off-Windows, and mypy
# cannot narrow on os.name the way it narrows on sys.platform.
process_kwargs["creationflags"] = (
@@ -604,8 +607,8 @@ async def fetch_codebuddy_models(api_key: str | None = None) -> list[str]:
| subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore[attr-defined]
)
else:
command = cli_args
process_kwargs["start_new_session"] = True
command = cli_args
env = os.environ.copy()
if api_key:

View File

@@ -525,6 +525,56 @@ async def test_fetch_codebuddy_models_uses_account_catalog(monkeypatch) -> None:
assert captured["env"]["CODEBUDDY_API_KEY"] == "secret"
@pytest.mark.asyncio
async def test_fetch_codebuddy_models_runs_the_windows_shim_directly(monkeypatch) -> None:
# "cmd.exe /d /s /c <list2cmdline(...)>" quoted the line twice, so a CLI
# path with a space reached cmd.exe as \"...\" and was not found.
import subprocess
import deeptutor.services.llm.provider_core.codebuddy_provider as module
captured: dict[str, object] = {}
class FakeProcess:
async def communicate(self):
return (b"supported models for your account:\n - hy3\n", b"")
async def fake_subprocess(*args, **kwargs):
captured["args"] = args
captured["kwargs"] = kwargs
return FakeProcess()
shim = r"C:\Users\John Smith\AppData\Roaming\npm\codebuddy.cmd"
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_subprocess)
monkeypatch.setattr(shutil, "which", lambda name: shim if name == "codebuddy" else None)
monkeypatch.setitem(sys.modules, "codebuddy_agent_sdk", SimpleNamespace(query=lambda: None))
monkeypatch.setattr(subprocess, "CREATE_NO_WINDOW", 0x08000000, raising=False)
monkeypatch.setattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0x00000200, raising=False)
monkeypatch.setattr(module.os, "name", "nt")
assert await fetch_codebuddy_models() == ["hy3"]
assert captured["args"][0] == shim
assert captured["kwargs"]["creationflags"] == 0x08000000 | 0x00000200
@pytest.mark.asyncio
@pytest.mark.skipif(os.name != "nt", reason="launches a real Windows .cmd shim")
async def test_fetch_codebuddy_models_finds_a_shim_under_a_path_with_spaces(
tmp_path, monkeypatch
) -> None:
shim_dir = tmp_path / "John Smith & Co" / "npm"
shim_dir.mkdir(parents=True)
shim = shim_dir / "codebuddy.cmd"
shim.write_text(
"@echo off\r\necho supported models for your account:\r\necho - hy3\r\n",
encoding="ascii",
)
monkeypatch.setattr(shutil, "which", lambda name: str(shim) if name == "codebuddy" else None)
monkeypatch.setitem(sys.modules, "codebuddy_agent_sdk", SimpleNamespace(query=lambda: None))
assert await fetch_codebuddy_models() == ["hy3"]
def test_codebuddy_registry_aliases_and_factory(monkeypatch) -> None:
spec = find_by_name("workbuddy")