Skip to content

The agent that grows with you

fix(gateway-windows): preserve localized task arguments

joaomarcose333cc009930
diff --git a/hermes_cli/gateway_windows.py b/hermes_cli/gateway_windows.py
index 1989a19a27c..504274447c5 100644
--- a/hermes_cli/gateway_windows.py
+++ b/hermes_cli/gateway_windows.py
@@ -37,10 +37,17 @@ logger = logging.getLogger(__name__)
_SCHTASKS_TIMEOUT_S = 15
# Patterns in schtasks stderr that mean "fall back to the Startup folder".
_FALLBACK_PATTERNS = re.compile(
- r"(access is denied|acceso denegado|přístup byl odepřen|schtasks timed out|schtasks produced no output)",
+ r"(access is denied|acceso denegado|přístup byl odepřen|"
+ r"错误\s*:\s*拒绝访问|拒绝访问|アクセスが拒否されました|"
+ r"액세스가 거부되었습니다|schtasks timed out|schtasks produced no output)",
+ re.IGNORECASE,
+)
+_ACCESS_DENIED_PATTERN = re.compile(
+ r"(access is denied|acceso denegado|přístup byl odepřen|"
+ r"错误\s*:\s*拒绝访问|拒绝访问|アクセスが拒否されました|"
+ r"액세스가 거부되었습니다)",
re.IGNORECASE,
)
-_ACCESS_DENIED_PATTERN = re.compile(r"(access is denied|acceso denegado)", re.IGNORECASE)
# Set by _spawn_detached() when the breakaway spawn failed and it retried WITHOUT
# CREATE_BREAKAWAY_FROM_JOB — the child stays in the parent's Job Object and may be killed when this
@@ -66,6 +73,29 @@ def _schtasks_encoding() -> str:
return "utf-8"
+def _windows_ansi_encoding() -> str:
+ """Return the Windows ANSI code page without Python UTF-8 Mode influencing it."""
+ try:
+ code_page = int(ctypes.windll.kernel32.GetACP())
+ if code_page <= 0:
+ raise ValueError("Windows returned an invalid ANSI code page")
+ return f"cp{code_page}"
+ except (AttributeError, OSError, ValueError):
+ return _schtasks_encoding()
+
+
+def _decode_schtasks_output(data: bytes | str) -> str:
+ """Decode captured ``schtasks.exe`` bytes while preserving localized paths and messages."""
+ if isinstance(data, str):
+ return data
+ for encoding in (_windows_ansi_encoding(), "utf-8"):
+ try:
+ return data.decode(encoding)
+ except (LookupError, UnicodeDecodeError):
+ continue
+ return data.decode("utf-8", errors="replace")
+
+
def _assert_windows() -> None:
if sys.platform != "win32":
raise RuntimeError("gateway_windows is Windows-only")
@@ -155,10 +185,14 @@ def _exec_schtasks(args: list[str]) -> tuple[int, str, str]:
# Locale encoding + replace: a non-UTF-8 status line must never surface a UnicodeDecodeError
# from subprocess' reader threads. CREATE_NO_WINDOW: no flashing console under a TUI.
proc = subprocess.run(
- [schtasks, *args], capture_output=True, text=True, encoding=_schtasks_encoding(), errors="replace",
+ [schtasks, *args], capture_output=True, text=False,
timeout=_SCHTASKS_TIMEOUT_S, creationflags=windows_hide_flags(),
)
- return (proc.returncode, proc.stdout or "", proc.stderr or "")
+ return (
+ proc.returncode,
+ _decode_schtasks_output(proc.stdout or b""),
+ _decode_schtasks_output(proc.stderr or b""),
+ )
except subprocess.TimeoutExpired:
return (124, "", f"schtasks timed out after {_SCHTASKS_TIMEOUT_S}s")
except OSError as e:
diff --git a/tests/hermes_cli/test_gateway_windows.py b/tests/hermes_cli/test_gateway_windows.py
index 1789d0946de..2c8e1cdc30a 100644
--- a/tests/hermes_cli/test_gateway_windows.py
+++ b/tests/hermes_cli/test_gateway_windows.py
@@ -15,6 +15,20 @@ import hermes_cli.setup as setup
_BREAKAWAY_MARKER = "_HERMES_GATEWAY_BREAKAWAY"
+def test_decode_schtasks_output_preserves_non_ascii_windows_path(monkeypatch):
+ """Scheduled Task XML keeps an ANSI-encoded account path intact under UTF-8 mode."""
+ monkeypatch.setattr(gateway_windows, "_windows_ansi_encoding", lambda: "gbk")
+ raw = r"<Arguments>C:\\Users\\方舟\\AppData\\Local\\hermes\\gateway.vbs</Arguments>".encode("gbk")
+
+ assert "方舟" in gateway_windows._decode_schtasks_output(raw)
+
+
+def test_localized_access_denied_uses_existing_fallback_paths():
+ """Localized schtasks denial still reaches elevation/startup fallback handling."""
+ detail = "错误: 拒绝访问。"
+
+ assert gateway_windows._should_fall_back(1, detail)
+ assert gateway_windows._is_access_denied(detail)
def test_schtasks_encoding_falls_back_to_utf8(monkeypatch):