22
33from __future__ import annotations
44
5+ import inspect
56import json
67import re
78from dataclasses import dataclass
@@ -76,7 +77,7 @@ def _status_payload(stdout: str) -> tuple[dict[str, Any] | None, str]:
7677 return payload , _safe_diagnostic (safe_stdout )
7778
7879
79- def cursor_auth_status () -> CursorAuthStatus :
80+ def cursor_auth_status (* , process_registry : proc . ProcessRegistry | None = None ) -> CursorAuthStatus :
8081 """Return a bounded, prompt-free diagnosis for the headless Cursor CLI."""
8182 if proc .which ("cursor-agent" ) is None :
8283 return CursorAuthStatus (
@@ -86,10 +87,11 @@ def cursor_auth_status() -> CursorAuthStatus:
8687 "" ,
8788 127 ,
8889 )
89- result = proc .run (
90- ["cursor-agent" , "status" , "--format" , "json" ],
91- timeout = CURSOR_AUTH_TIMEOUT_SECONDS ,
92- )
90+ argv = ["cursor-agent" , "status" , "--format" , "json" ]
91+ if process_registry is None :
92+ result = proc .run (argv , timeout = CURSOR_AUTH_TIMEOUT_SECONDS )
93+ else :
94+ result = proc .run (argv , timeout = CURSOR_AUTH_TIMEOUT_SECONDS , process_registry = process_registry )
9395 payload , stdout = _status_payload (result .stdout )
9496 stderr = _safe_diagnostic (result .stderr )
9597 diagnostic = _diagnostic_line (stdout , stderr )
@@ -164,8 +166,11 @@ def build_argv(
164166 return argv
165167
166168
167- def installed_version () -> tuple [str | None , str ]:
168- result = proc .run (["acpx" , "--version" ], timeout = 10.0 )
169+ def installed_version (* , process_registry : proc .ProcessRegistry | None = None ) -> tuple [str | None , str ]:
170+ if process_registry is None :
171+ result = proc .run (["acpx" , "--version" ], timeout = 10.0 )
172+ else :
173+ result = proc .run (["acpx" , "--version" ], timeout = 10.0 , process_registry = process_registry )
169174 if result .code != 0 :
170175 return None , result .stderr .strip () or result .stdout .strip () or f"exit { result .code } "
171176 match = re .search (r"\b(\d+\.\d+\.\d+)\b" , result .stdout )
@@ -174,6 +179,17 @@ def installed_version() -> tuple[str | None, str]:
174179 return match .group (1 ), ""
175180
176181
182+ def _call_with_process_registry (function , * , process_registry : proc .ProcessRegistry | None ):
183+ parameters = inspect .signature (function ).parameters .values ()
184+ accepts_registry = any (
185+ parameter .name == "process_registry" or parameter .kind is inspect .Parameter .VAR_KEYWORD
186+ for parameter in parameters
187+ )
188+ if accepts_registry :
189+ return function (process_registry = process_registry )
190+ return function ()
191+
192+
177193def _permission_prompt_diagnostic (error : object ) -> dict [str , object ] | None :
178194 if not isinstance (error , dict ):
179195 return None
@@ -349,6 +365,7 @@ def run_cursor(
349365 version : str ,
350366 read_only : bool ,
351367 writable_worktree : bool = False ,
368+ process_registry : proc .ProcessRegistry | None = None ,
352369) -> AgentResult :
353370 if version != SUPPORTED_VERSION :
354371 return AgentResult (
@@ -377,7 +394,10 @@ def run_cursor(
377394 failure_kind = "missing-executable" ,
378395 transport = "acpx" ,
379396 )
380- installed , version_error = installed_version ()
397+ installed , version_error = _call_with_process_registry (
398+ installed_version ,
399+ process_registry = process_registry ,
400+ )
381401 if installed != version :
382402 found = installed or version_error
383403 return AgentResult (
@@ -389,7 +409,10 @@ def run_cursor(
389409 transport = "acpx" ,
390410 acpx_version = installed ,
391411 )
392- auth = cursor_auth_status ()
412+ auth = _call_with_process_registry (
413+ cursor_auth_status ,
414+ process_registry = process_registry ,
415+ )
393416 auth_event : dict [str , object ] = {
394417 "type" : "provider_auth" ,
395418 "status" : auth .state ,
@@ -436,7 +459,12 @@ def run_cursor(
436459 acpx_version = installed ,
437460 safe_events = (auth_event ,),
438461 )
439- result = proc .run (argv , timeout = timeout + 5.0 , cwd = cwd )
462+ result = proc .run (
463+ argv ,
464+ timeout = timeout + 5.0 ,
465+ cwd = cwd ,
466+ process_registry = process_registry ,
467+ )
440468 if result .decode_failed :
441469 detail = (result .stderr .strip () or result .decode_failure_detail )[:200 ]
442470 return AgentResult (
0 commit comments