The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(e2e): upgrading a pre-runtime Windows install over its running gateway, elevated and not
Teknium457b49d8b1cc
diff --git a/tests/e2e/core/windows_update/_machine.py b/tests/e2e/core/windows_update/_machine.pyindex 973168ce11c..1b7b9e6be04 100644--- a/tests/e2e/core/windows_update/_machine.py+++ b/tests/e2e/core/windows_update/_machine.py@@ -148,6 +148,9 @@ class Machine:# Git for Windows on PATH (a typical developer box). False: a clean machine, where the# installer must stage its own pinned Git.system_git: bool = False+ # Install an older commit (``base``) instead of this checkout, and update to this checkout's+ # HEAD instead of a synthetic NEXT: a user upgrading a real prior release.+ base: str = ""path_prepend: list[str] = field(default_factory=list)head: str = ""next: str = ""@@ -192,7 +195,7 @@ class Machine:# -- environment ----------------------------------------------------------- def env(self, extra: dict[str, str] | None = None) -> dict[str, str]:+ def env(self, extra: dict[str, str] | None = None, *, as_user: bool = False) -> dict[str, str]:env = {k: v for k, v in os.environ.items()if k in _PASSTHROUGH_ENV and not k.upper().endswith(_SECRET_SUFFIXES)}roaming = self.profile / "AppData" / "Roaming"@@ -211,6 +214,11 @@ class Machine:# state.db lives under tmp; under a pytest ancestor the live-DB guard would refuse it."HERMES_STATE_DB_GUARD_BYPASS": "1",})+ if as_user:+ # The runner's own %TEMP% belongs to the elevated session; a user's is in their profile.+ temp = self.local / "Temp"+ temp.mkdir(parents=True, exist_ok=True)+ env.update({"TEMP": str(temp), "TMP": str(temp)})env.update(extra or {})return env@@ -219,13 +227,13 @@ class Machine:def stage(self) -> None:"""serve.git at HEAD (+ NEXT in its object store), the URL redirect, a seeded home."""self.root.mkdir(parents=True, exist_ok=True)- self.head = harness_git("-C", str(REPO_ROOT), "rev-parse", "HEAD")+ self.head = harness_git("-C", str(REPO_ROOT), "rev-parse", f"{self.base}^{{commit}}" if self.base else "HEAD")harness_git("clone", "--bare", "--quiet", str(REPO_ROOT), str(self.serve))harness_git("-C", str(self.serve), "update-ref", "refs/heads/main", self.head)harness_git("-C", str(self.serve), "symbolic-ref", "HEAD", "refs/heads/main")for key in ("uploadpack.allowAnySHA1InWant", "uploadpack.allowFilter"):harness_git("-C", str(self.serve), "config", key, "true")- self.next = self._mint_next()+ self.next = (harness_git("-C", str(REPO_ROOT), "rev-parse", "HEAD") if self.base else self._mint_next())file_url = "file:///" + str(self.serve).replace("\\", "/")rewrites = "".join(f"\tinsteadOf = {url}\n" for url in CANONICAL_URLS)(self.root / "e2e-gitconfig").write_text(f'[url "{file_url}"]\n{rewrites}', encoding="utf-8")@@ -320,11 +328,30 @@ class Machine:# -- running --------------------------------------------------------------def _run_logged(self, argv: list[str], label: str, *, timeout: float, cwd: Path | None = None,- env_extra: dict[str, str] | None = None) -> Run:- """Run to completion with the full transcript on disk (survives a timeout)."""+ env_extra: dict[str, str] | None = None, as_user: bool = False) -> Run:+ """Run to completion with the full transcript on disk (survives a timeout).++ ``as_user``: under a standard-user token (``_tokens.StandardUserProcess``), as a+ non-elevated console would; the child writes the transcript itself (no timestamps)."""self._seq += 1- log = self.logs / f"{self._seq:02d}-{label}.log"+ log = self.logs / f"{self._seq:02d}-{label}{'-as-user' if as_user else ''}.log"started = time.monotonic()+ if as_user:+ from tests.e2e.core.windows_update._tokens import StandardUserProcess++ proc = StandardUserProcess(argv, cwd=cwd or self.profile, env=self.env(env_extra, as_user=True),+ log=log, creationflags=subprocess.CREATE_NO_WINDOW)+ self._spawned.append(proc) # type: ignore[arg-type]+ try:+ code = proc.wait(timeout=timeout)+ self.timings.append((label, round(time.monotonic() - started, 1)))+ except subprocess.TimeoutExpired:+ subprocess.run(["taskkill", "/PID", str(proc.pid), "/T", "/F"], capture_output=True, timeout=60,+ check=False)+ raise AssertionError(+ f"{label} (standard user) did not finish within {timeout:.0f}s\n{self._tail(log)}\n"+ f"{self.evidence()}") from None+ return Run(code, _decode(log.read_bytes()), "")chunks: list[bytes] = []proc = subprocess.Popen(argv, cwd=cwd or self.profile, env=self.env(env_extra),stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)@@ -361,33 +388,42 @@ class Machine:return f"<no {path.name}>"return f"--- {path.name} (last {n} chars) ---\n{text[-n:]}"- def install(self) -> Run:- """``powershell -File install.ps1 -NonInteractive`` from a directory that is no project."""+ def install(self, *, as_user: bool = False) -> Run:+ """``powershell -File install.ps1 -NonInteractive`` from a directory that is no project.++ With ``base`` set it is that commit's install.ps1, the script that user downloaded then."""script = self.root / "install.ps1"- shutil.copyfile(REPO_ROOT / "scripts" / "install.ps1", script)+ if self.base:+ raw = subprocess.run([REAL_GIT, "-C", str(self.serve), "show", f"{self.head}:scripts/install.ps1"],+ env={k: v for k, v in os.environ.items() if not k.startswith("GIT_")},+ capture_output=True, timeout=120, check=True).stdout+ script.write_bytes(raw)+ else:+ shutil.copyfile(REPO_ROOT / "scripts" / "install.ps1", script)cwd = self.root / "install-cwd"cwd.mkdir(parents=True, exist_ok=True)run = self._run_logged(["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(script), "-NonInteractive"],"install", timeout=INSTALL_TIMEOUT, cwd=cwd,# serve.git carries this checkout's release tags; install main, not the latest release.- env_extra={"HERMES_INSTALL_CHANNEL": "main"})+ env_extra={"HERMES_INSTALL_CHANNEL": "main"}, as_user=as_user)if run.returncode == 0 and self.hermes_exe.is_file():# advance() lands NEXT on main and publishes no release: follow main, not the stable# default an official-origin checkout gets (channel resolution has its own suites).- pin = self.hermes("update", "--set-channel", "main", label="pin-main-channel")+ pin = self.hermes("update", "--set-channel", "main", label="pin-main-channel", as_user=as_user)assert pin.returncode == 0, f"could not pin the main channel\n{self.evidence()}"return rundef hermes(self, *args: str, label: str | None = None, timeout: float = CMD_TIMEOUT,- env_extra: dict[str, str] | None = None) -> Run:+ env_extra: dict[str, str] | None = None, as_user: bool = False) -> Run:assert self.hermes_exe.is_file(), f"installer published no {self.hermes_exe}\n{self.evidence()}"name = label or "hermes-" + "-".join(a.strip("-") for a in args[:2] if a)- return self._run_logged([str(self.hermes_exe), *args], name, timeout=timeout, env_extra=env_extra)+ return self._run_logged([str(self.hermes_exe), *args], name, timeout=timeout, env_extra=env_extra,+ as_user=as_user)- def update(self, *extra: str, label: str = "update") -> Run:+ def update(self, *extra: str, label: str = "update", as_user: bool = False) -> Run:with self.gateway_phase():- return self.hermes("update", "--yes", *extra, label=label, timeout=UPDATE_TIMEOUT)+ return self.hermes("update", "--yes", *extra, label=label, timeout=UPDATE_TIMEOUT, as_user=as_user)@contextlib.contextmanagerdef gateway_phase(self):@@ -444,12 +480,21 @@ class Machine:except (OSError, ValueError):return {}- def spawn_gateway(self, env_extra: dict[str, str] | None = None) -> subprocess.Popen:+ def spawn_gateway(self, env_extra: dict[str, str] | None = None, *, as_user: bool = False) -> subprocess.Popen:"""``hermes gateway run`` through the published launcher, windowless and detached from- this console: the shape the Desktop / login item uses to host a gateway."""+ this console: the shape the Desktop / login item uses to host a gateway.++ ``as_user``: under a standard-user token, as a non-elevated login item / Desktop starts it."""self._seq += 1- log = self.logs / f"{self._seq:02d}-gateway-run.log"+ log = self.logs / f"{self._seq:02d}-gateway-run{'-as-user' if as_user else ''}.log"flags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NO_WINDOW+ if as_user:+ from tests.e2e.core.windows_update._tokens import StandardUserProcess++ proc = StandardUserProcess([str(self.hermes_exe), "gateway", "run"], cwd=self.profile,+ env=self.env(env_extra, as_user=True), log=log, creationflags=flags)+ self._spawned.append(proc) # type: ignore[arg-type]+ return proc # type: ignore[return-value]with log.open("wb") as fh:proc = subprocess.Popen([str(self.hermes_exe), "gateway", "run"], cwd=self.profile,env=self.env(env_extra), stdin=subprocess.DEVNULL, stdout=fh,@@ -544,7 +589,7 @@ class Machine:def new_machine(tmp_root: Path, base_url: str, *, label: str, person: str = "",- system_git: bool = False) -> Machine:+ system_git: bool = False, base: str = "") -> Machine:"""A staged machine. Its work dir (serve.git, transcripts) lives under``HERMES_E2E_MACHINE_ROOT`` (else ``tmp_root``); its user profile under``HERMES_E2E_PROFILES_ROOT`` (CI: ``C:\\Users``), named ``[<person> ]hermes-e2e-<id>``."""@@ -553,7 +598,7 @@ def new_machine(tmp_root: Path, base_url: str, *, label: str, person: str = "",profiles = os.environ.get("HERMES_E2E_PROFILES_ROOT")name = f"{person} hermes-e2e-{sfx}" if person else f"hermes-e2e-{sfx}"machine = Machine(root=base / f"{label}-{sfx}", profile_name=name, base_url=base_url,- profiles_root=Path(profiles) if profiles else None, system_git=system_git)+ profiles_root=Path(profiles) if profiles else None, system_git=system_git, base=base)machine.stage()return machine@@ -620,7 +665,7 @@ class Turn:return self.run.returncode == 0 and self.reply_id in self.run.stdout and self.reached_wire-def one_shot_turn(machine: Machine, srv: Any, label: str) -> Turn:+def one_shot_turn(machine: Machine, srv: Any, label: str, *, as_user: bool = False) -> Turn:"""``hermes chat -q ... -Q`` through the published launcher against the loopback provider."""from tests.e2e.core.windows._helpers import last_userfrom tests.fakes.fake_llm_provider import Text@@ -631,7 +676,7 @@ def one_shot_turn(machine: Machine, srv: Any, label: str) -> Turn:srv.drop_unconsumed()before = len(srv.main_requests())srv.push(Text(f"The answer is {reply_id}."))- res = machine.hermes("chat", "-q", f"Say the code {prompt_id}", "-Q", label=label)+ res = machine.hermes("chat", "-q", f"Say the code {prompt_id}", "-Q", label=label, as_user=as_user)wired = any(prompt_id in last_user(body) for body in srv.main_requests()[before:])return Turn(res, reply_id, wired)diff --git a/tests/e2e/core/windows_update/_tokens.py b/tests/e2e/core/windows_update/_tokens.pynew file mode 100644index 00000000000..e53db5ff34f--- /dev/null+++ b/tests/e2e/core/windows_update/_tokens.py@@ -0,0 +1,387 @@+"""Standard-user tokens for the Windows install/update journeys (the runner session is elevated).++Two uses:++* :func:`standard_user_access` asks the kernel (AccessCheck) whether a standard-user token may+ open a path with a given access mask, without starting anything.+* :class:`StandardUserProcess` starts a real process under that token, so a journey can run+ ``hermes gateway run``, ``hermes gateway status`` or a turn exactly as a non-elevated user's+ console, login item or Desktop would.++The token is the runner's own token passed through ``CreateRestrictedToken`` with+BUILTIN\\Administrators deny-only, every privilege but SeChangeNotify dropped (``LUA_TOKEN``) and+Medium integrity: the shape of UAC's filtered token. It never depends on the host's SAFER policy+(Blacksmith's image hands back a SAFER token with Administrators still enabled).+"""++from __future__ import annotations++import ctypes+import os+import subprocess+from ctypes import wintypes+from pathlib import Path++_ADMINISTRATORS = "S-1-5-32-544"+_SYSTEM = "S-1-5-18"+_MEDIUM_IL = "S-1-16-8192"+FILE_READ_EXECUTE = 0x1200A9 # FILE_GENERIC_READ | FILE_GENERIC_EXECUTE+FILE_READ_WRITE = 0x12019F # FILE_GENERIC_READ | FILE_GENERIC_WRITE+FILE_ALL_ACCESS = 0x1F01FF++_handle = wintypes.HANDLE+++class _SidAndAttributes(ctypes.Structure):+ _fields_ = [("Sid", ctypes.c_void_p), ("Attributes", wintypes.DWORD)]+++class _GenericMapping(ctypes.Structure):+ _fields_ = [("GenericRead", wintypes.DWORD), ("GenericWrite", wintypes.DWORD),+ ("GenericExecute", wintypes.DWORD), ("GenericAll", wintypes.DWORD)]+++def _dlls():+ advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)+ kernel32.GetCurrentProcess.restype = _handle+ kernel32.CloseHandle.argtypes = [_handle]+ kernel32.LocalFree.argtypes = [ctypes.c_void_p]+ advapi32.OpenProcessToken.argtypes = [_handle, wintypes.DWORD, ctypes.POINTER(_handle)]+ advapi32.ConvertStringSidToSidW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_void_p)]+ advapi32.GetLengthSid.argtypes = [ctypes.c_void_p]+ advapi32.GetLengthSid.restype = wintypes.DWORD+ advapi32.CreateRestrictedToken.argtypes = [+ _handle, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(_SidAndAttributes), wintypes.DWORD,+ ctypes.c_void_p, wintypes.DWORD, ctypes.c_void_p, ctypes.POINTER(_handle)]+ advapi32.SetTokenInformation.argtypes = [_handle, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD]+ advapi32.GetTokenInformation.argtypes = [_handle, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD,+ ctypes.POINTER(wintypes.DWORD)]+ advapi32.DuplicateToken.argtypes = [_handle, ctypes.c_int, ctypes.POINTER(_handle)]+ advapi32.GetFileSecurityW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, ctypes.c_void_p,+ wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]+ advapi32.AccessCheck.argtypes = [ctypes.c_void_p, _handle, wintypes.DWORD, ctypes.POINTER(_GenericMapping),+ ctypes.c_void_p, ctypes.POINTER(wintypes.DWORD),+ ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.BOOL)]+ advapi32.ConvertSidToStringSidW.argtypes = [ctypes.c_void_p, ctypes.POINTER(wintypes.LPWSTR)]+ advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [+ wintypes.LPCWSTR, wintypes.DWORD, ctypes.POINTER(ctypes.c_void_p), ctypes.c_void_p]+ advapi32.GetSecurityDescriptorDacl.argtypes = [ctypes.c_void_p, ctypes.POINTER(wintypes.BOOL),+ ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(wintypes.BOOL)]+ return advapi32, kernel32+++def _check(ok: int, what: str) -> None:+ if not ok:+ err = ctypes.get_last_error()+ raise OSError(err, f"{what}: WinError {err} {ctypes.FormatError(err).strip()}")+++class _Sid:+ """A SID from its string form, freed on exit."""++ def __init__(self, advapi32, kernel32, text: str) -> None:+ self._kernel32 = kernel32+ self.ptr = ctypes.c_void_p()+ _check(advapi32.ConvertStringSidToSidW(text, ctypes.byref(self.ptr)), f"ConvertStringSidToSidW({text})")++ def __enter__(self) -> _Sid:+ return self++ def __exit__(self, *exc) -> None:+ self._kernel32.LocalFree(self.ptr)+++def current_user_sid() -> str:+ """String SID of this process's token user (the account a standard-user token keeps)."""+ advapi32, kernel32 = _dlls()+ token = _handle()+ _check(advapi32.OpenProcessToken(kernel32.GetCurrentProcess(), 0x0008, ctypes.byref(token)), "OpenProcessToken")+ try:+ needed = wintypes.DWORD()+ advapi32.GetTokenInformation(token, 1, None, 0, ctypes.byref(needed)) # TokenUser+ buf = ctypes.create_string_buffer(needed.value)+ _check(advapi32.GetTokenInformation(token, 1, buf, needed, ctypes.byref(needed)), "GetTokenInformation")+ sid = ctypes.cast(buf, ctypes.POINTER(_SidAndAttributes)).contents.Sid+ text = wintypes.LPWSTR()+ _check(advapi32.ConvertSidToStringSidW(sid, ctypes.byref(text)), "ConvertSidToStringSidW")+ try:+ return str(text.value)+ finally:+ kernel32.LocalFree(text)+ finally:+ kernel32.CloseHandle(token)+++def standard_user_token() -> _handle:+ """A primary token for this user with the UAC filtered-token shape. The caller closes it."""+ advapi32, kernel32 = _dlls()+ own = _handle()+ # TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ADJUST_DEFAULT+ _check(advapi32.OpenProcessToken(kernel32.GetCurrentProcess(), 0x0001 | 0x0002 | 0x0008 | 0x0080,+ ctypes.byref(own)), "OpenProcessToken")+ out = _handle()+ try:+ with _Sid(advapi32, kernel32, _ADMINISTRATORS) as admins:+ disable = (_SidAndAttributes * 1)(_SidAndAttributes(admins.ptr, 0))+ # DISABLE_MAX_PRIVILEGE (0x1) | LUA_TOKEN (0x4)+ _check(advapi32.CreateRestrictedToken(own, 0x1 | 0x4, 1, disable, 0, None, 0, None, ctypes.byref(out)),+ "CreateRestrictedToken")+ finally:+ kernel32.CloseHandle(own)+ try:+ with _Sid(advapi32, kernel32, _MEDIUM_IL) as medium:+ label = _SidAndAttributes(medium.ptr, 0x20) # SE_GROUP_INTEGRITY+ size = ctypes.sizeof(label) + advapi32.GetLengthSid(medium.ptr)+ _check(advapi32.SetTokenInformation(out, 25, ctypes.byref(label), size), # TokenIntegrityLevel+ "SetTokenInformation(TokenIntegrityLevel)")+ # An elevated admin token's default owner is Administrators, now deny-only: new objects must+ # be owned by the user, as a filtered token's are.+ user_sid = current_user_sid()+ with _Sid(advapi32, kernel32, user_sid) as user:+ owner = ctypes.c_void_p(user.ptr.value)+ _check(advapi32.SetTokenInformation(out, 4, ctypes.byref(owner), ctypes.sizeof(owner)), # TokenOwner+ "SetTokenInformation(TokenOwner)")+ # Likewise its default DACL (Administrators + SYSTEM): the processes, threads and unnamed+ # objects this token creates would be unopenable by the user's other processes. A filtered+ # token's default DACL grants the user and SYSTEM.+ sd = ctypes.c_void_p()+ _check(advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW(+ f"D:(A;;GA;;;{user_sid})(A;;GA;;;SY)", 1, ctypes.byref(sd), None),+ "ConvertStringSecurityDescriptorToSecurityDescriptorW")+ try:+ present, dacl, defaulted = wintypes.BOOL(), ctypes.c_void_p(), wintypes.BOOL()+ _check(advapi32.GetSecurityDescriptorDacl(sd, ctypes.byref(present), ctypes.byref(dacl),+ ctypes.byref(defaulted)), "GetSecurityDescriptorDacl")+ _check(advapi32.SetTokenInformation(out, 6, ctypes.byref(dacl), ctypes.sizeof(dacl)), # TokenDefaultDacl+ "SetTokenInformation(TokenDefaultDacl)")+ finally:+ kernel32.LocalFree(sd)+ except BaseException:+ kernel32.CloseHandle(out)+ raise+ return out+++def standard_user_access(paths: list[Path], access: int = FILE_READ_EXECUTE) -> dict[Path, str]:+ """``{path: ""}`` when a standard-user token is granted ``access`` on ``path``, else why not.++ The kernel's own decision (AccessCheck) against :func:`standard_user_token`.+ """+ advapi32, kernel32 = _dlls()+ primary = standard_user_token()+ imp = _handle()+ try:+ _check(advapi32.DuplicateToken(primary, 2, ctypes.byref(imp)), "DuplicateToken") # SecurityImpersonation+ mapping = _GenericMapping(0x120089, 0x120116, 0x1200A0, 0x1F01FF)+ verdicts: dict[Path, str] = {}+ for path in paths:+ needed = wintypes.DWORD()+ info = 0x1 | 0x2 | 0x4 | 0x10 # OWNER | GROUP | DACL | LABEL+ advapi32.GetFileSecurityW(str(path), info, None, 0, ctypes.byref(needed))+ sd = ctypes.create_string_buffer(max(needed.value, 1))+ try:+ _check(advapi32.GetFileSecurityW(str(path), info, sd, needed, ctypes.byref(needed)),+ "GetFileSecurityW")+ privs, privs_len = ctypes.create_string_buffer(1024), wintypes.DWORD(1024)+ granted, status = wintypes.DWORD(), wintypes.BOOL()+ _check(advapi32.AccessCheck(sd, imp, access, ctypes.byref(mapping), privs,+ ctypes.byref(privs_len), ctypes.byref(granted), ctypes.byref(status)),+ "AccessCheck")+ verdicts[path] = "" if status.value else f"access 0x{access:x} denied to a standard-user token"+ except OSError as exc:+ verdicts[path] = str(exc)+ return verdicts+ finally:+ for h in (imp, primary):+ if h:+ kernel32.CloseHandle(h)+++def harness_control(scratch: Path) -> str:+ """The token reads System32 but not an Administrators+SYSTEM-only file: it really is restricted."""+ admin_only = scratch / "admin-only.txt"+ admin_only.write_text("x", encoding="utf-8")+ subprocess.run(["icacls", str(admin_only), "/inheritance:r", "/grant:r", f"*{_ADMINISTRATORS}:F",+ f"*{_SYSTEM}:F"], capture_output=True, check=True, timeout=60)+ cmd = Path(r"C:\Windows\System32\cmd.exe")+ verdicts = standard_user_access([cmd, admin_only])+ assert verdicts[cmd] == "", f"harness: the standard-user token cannot even run cmd.exe: {verdicts[cmd]}"+ assert verdicts[admin_only], "harness: the standard-user token still reads an Administrators-only file"+ return "ok"+++def grant_user_profile_acl(profile: Path) -> None:+ """Give a fake profile the DACL Windows gives a real one: the user, SYSTEM and Administrators+ full control, inherited by everything below.++ Without it a profile the elevated harness made under ``C:\\Users`` inherits only+ ``Users:(RX)`` plus CREATOR OWNER (= Administrators for an elevated creator), so no+ non-elevated process of the same user could write its own home: a harness artefact, never a+ user's machine."""+ profile.mkdir(parents=True, exist_ok=True)+ aces = [f"*{sid}:(OI)(CI)F" for sid in (current_user_sid(), _SYSTEM, _ADMINISTRATORS)]+ subprocess.run(["icacls", str(profile), "/inheritance:r", "/grant:r", *aces, "/Q"],+ capture_output=True, check=True, timeout=300)+ # What the harness already staged below (the seeded home) inherits it, as a real profile's does.+ if any(profile.iterdir()):+ subprocess.run(["icacls", str(profile / "*"), "/reset", "/T", "/C", "/Q"],+ capture_output=True, check=True, timeout=300)+++def hand_to_user(path: Path) -> None:+ """Make a harness dir (serve.git, the gitconfig, install.ps1) the user's own, as a user's clone+ or download would be: owner = the user (git refuses a repo it judges another owner's) plus an+ inherited full-control grant."""+ sid = current_user_sid()+ for args in (["/setowner", f"*{sid}"], ["/grant", f"*{sid}:(OI)(CI)F"]):+ subprocess.run(["icacls", str(path), *args, "/T", "/C", "/Q"], capture_output=True, check=True, timeout=300)+++# -- starting a process under the token -------------------------------------------------------+++class _StartupInfo(ctypes.Structure):+ _fields_ = [("cb", wintypes.DWORD), ("lpReserved", wintypes.LPWSTR), ("lpDesktop", wintypes.LPWSTR),+ ("lpTitle", wintypes.LPWSTR), ("dwX", wintypes.DWORD), ("dwY", wintypes.DWORD),+ ("dwXSize", wintypes.DWORD), ("dwYSize", wintypes.DWORD), ("dwXCountChars", wintypes.DWORD),+ ("dwYCountChars", wintypes.DWORD), ("dwFillAttribute", wintypes.DWORD),+ ("dwFlags", wintypes.DWORD), ("wShowWindow", wintypes.WORD), ("cbReserved2", wintypes.WORD),+ ("lpReserved2", ctypes.c_void_p), ("hStdInput", _handle), ("hStdOutput", _handle),+ ("hStdError", _handle)]+++class _StartupInfoEx(ctypes.Structure):+ _fields_ = [("StartupInfo", _StartupInfo), ("lpAttributeList", ctypes.c_void_p)]+++class _ProcessInformation(ctypes.Structure):+ _fields_ = [("hProcess", _handle), ("hThread", _handle), ("dwProcessId", wintypes.DWORD),+ ("dwThreadId", wintypes.DWORD)]+++_EXTENDED_STARTUPINFO_PRESENT = 0x00080000+_CREATE_UNICODE_ENVIRONMENT = 0x00000400+_STARTF_USESTDHANDLES = 0x00000100+_PROC_THREAD_ATTRIBUTE_HANDLE_LIST = 0x00020002+_INFINITE = 0xFFFFFFFF+_STILL_ACTIVE = 259+++class StandardUserProcess:+ """A ``subprocess.Popen``-shaped handle on a process started under :func:`standard_user_token`.++ stdout and stderr go to ``log`` (an inheritable handle the elevated parent opened, so the+ child needs no access to the transcript dir); stdin is NUL. Only those two handles are+ inherited (PROC_THREAD_ATTRIBUTE_HANDLE_LIST), never the pytest process's pipes.+ """++ def __init__(self, argv: list[str], *, cwd: Path, env: dict[str, str], log: Path,+ creationflags: int = 0, append: bool = False) -> None:+ import msvcrt++ advapi32, kernel32 = _dlls()+ self._kernel32 = kernel32+ kernel32.InitializeProcThreadAttributeList.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD,+ ctypes.POINTER(ctypes.c_size_t)]+ kernel32.UpdateProcThreadAttribute.argtypes = [ctypes.c_void_p, wintypes.DWORD, ctypes.c_size_t,+ ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p,+ ctypes.c_void_p]+ kernel32.DeleteProcThreadAttributeList.argtypes = [ctypes.c_void_p]+ advapi32.CreateProcessAsUserW.argtypes = [+ _handle, wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p, wintypes.BOOL,+ wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR, ctypes.c_void_p,+ ctypes.POINTER(_ProcessInformation)]+ kernel32.WaitForSingleObject.argtypes = [_handle, wintypes.DWORD]+ kernel32.WaitForSingleObject.restype = wintypes.DWORD+ kernel32.GetExitCodeProcess.argtypes = [_handle, ctypes.POINTER(wintypes.DWORD)]++ self.args = argv+ self.returncode: int | None = None+ out = open(log, "ab" if append else "wb")+ nul = open(os.devnull, "rb")+ token = standard_user_token()+ attrs = None+ try:+ out_h = msvcrt.get_osfhandle(out.fileno())+ in_h = msvcrt.get_osfhandle(nul.fileno())+ os.set_handle_inheritable(out_h, True)+ os.set_handle_inheritable(in_h, True)+ handles = (_handle * 2)(out_h, in_h)+ size = ctypes.c_size_t()+ kernel32.InitializeProcThreadAttributeList(None, 1, 0, ctypes.byref(size))+ attrs = ctypes.create_string_buffer(size.value)+ _check(kernel32.InitializeProcThreadAttributeList(attrs, 1, 0, ctypes.byref(size)),+ "InitializeProcThreadAttributeList")+ _check(kernel32.UpdateProcThreadAttribute(attrs, 0, _PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handles,+ ctypes.sizeof(handles), None, None),+ "UpdateProcThreadAttribute")+ si = _StartupInfoEx()+ si.StartupInfo.cb = ctypes.sizeof(_StartupInfoEx)+ si.StartupInfo.dwFlags = _STARTF_USESTDHANDLES+ si.StartupInfo.hStdInput = in_h+ si.StartupInfo.hStdOutput = out_h+ si.StartupInfo.hStdError = out_h+ si.lpAttributeList = ctypes.cast(attrs, ctypes.c_void_p)+ block = "".join(f"{k}={v}\0" for k, v in sorted(env.items(), key=lambda kv: kv[0].upper())) + "\0"+ env_buf = ctypes.create_unicode_buffer(block, len(block))+ cmdline = ctypes.create_unicode_buffer(subprocess.list2cmdline(argv))+ pi = _ProcessInformation()+ flags = creationflags | _EXTENDED_STARTUPINFO_PRESENT | _CREATE_UNICODE_ENVIRONMENT+ _check(advapi32.CreateProcessAsUserW(token, None, cmdline, None, None, True, flags, env_buf,+ str(cwd), ctypes.byref(si), ctypes.byref(pi)),+ f"CreateProcessAsUserW({argv[0]})")+ kernel32.CloseHandle(pi.hThread)+ self._process = pi.hProcess+ self.pid = int(pi.dwProcessId)+ finally:+ if attrs is not None:+ kernel32.DeleteProcThreadAttributeList(attrs)+ kernel32.CloseHandle(token)+ out.close()+ nul.close()++ def poll(self) -> int | None:+ if self.returncode is None and self._kernel32.WaitForSingleObject(self._process, 0) == 0:+ code = wintypes.DWORD()+ self._kernel32.GetExitCodeProcess(self._process, ctypes.byref(code))+ self.returncode = int(code.value)+ return self.returncode++ def wait(self, timeout: float | None = None) -> int:+ ms = _INFINITE if timeout is None else max(0, int(timeout * 1000))+ if self._kernel32.WaitForSingleObject(self._process, ms) != 0:+ raise subprocess.TimeoutExpired(self.args, timeout or 0)+ return self.poll() # type: ignore[return-value]++ def __del__(self) -> None:+ try:+ self._kernel32.CloseHandle(self._process)+ except Exception:+ pass+++def probe_token_elevation(cwd: Path, env: dict[str, str], log: Path) -> str:+ """What a process under the token says about itself (``whoami /groups``), as+ ``elevated=<bool> admin=<bool>``: elevated = High/System integrity, admin = Administrators+ enabled (not deny-only).++ The harness control for every non-elevated cell: a child that is still elevated (or still+ holds an enabled Administrators group) would make the cell prove nothing."""+ import csv+ import io++ whoami = Path(os.environ.get("SystemRoot", r"C:\Windows")) / "System32" / "whoami.exe"+ proc = StandardUserProcess([str(whoami), "/groups", "/fo", "csv", "/nh"], cwd=cwd, env=env, log=log)+ rc = proc.wait(120)+ raw = log.read_bytes()+ text = raw.decode("utf-8", errors="replace")+ assert rc == 0, f"harness: whoami under the standard-user token exited {rc}: {text}"+ rows = list(csv.reader(io.StringIO(text)))+ sids = {row[2]: row[3] for row in rows if len(row) >= 4}+ elevated = "S-1-16-12288" in sids or "S-1-16-16384" in sids+ admin = _ADMINISTRATORS in sids and "deny only" not in sids[_ADMINISTRATORS].lower()+ verdict = f"elevated={elevated} admin={admin}"+ assert "S-1-16-8192" in sids, f"harness: the standard-user token is not Medium integrity: {text}"+ assert verdict == "elevated=False admin=False", f"harness: the standard-user token is not standard: {text}"+ return verdictdiff --git a/tests/e2e/core/windows_update/test_upgrade_over_old_gateway.py b/tests/e2e/core/windows_update/test_upgrade_over_old_gateway.pynew file mode 100644index 00000000000..8f8dd7ecb73--- /dev/null+++ b/tests/e2e/core/windows_update/test_upgrade_over_old_gateway.py@@ -0,0 +1,340 @@+"""Upgrading a real pre-#106742 install over its running gateway, elevated and not (#134889 W4).++Failure class: the release that introduced the unified gateway runtime (#106742, eb75627faf5)+changed who owns ``gateway.lock`` and how it is opened. A user upgrading from the release before+it has that OLD gateway running and holding ``gateway.lock`` when ``hermes update`` starts. The+update must stop it, take the checkout to the new release, start the NEW gateway on that same+lock, and leave every file the gateway needs usable by the same user without elevation:++* ``base``: the release before the merge (``d04056d609b``, its first parent), installed with+ ITS OWN ``install.ps1`` from the local origin, exactly what that user ran then.+* ``hermes update`` to this checkout's HEAD (current main plus nothing but tests).++Two journeys, one per file-level fixture, each on its own fresh profile:++* ``elevated``: an administrator console (the runner's session) installs, hosts the old gateway+ and updates. Afterwards the same user WITHOUT elevation (a UAC filtered-shape token, see+ ``_tokens.py``) must be able to read and write ``gateway.lock`` and the gateway's state, and,+ once the elevated gateway is stopped, start their own gateway on that lock, see it in+ ``hermes gateway status`` and get a reply.+* ``standard user``: the whole journey under the filtered token: install, old gateway, update,+ new gateway, status, turn.+"""++from __future__ import annotations++import os+import re+from pathlib import Path++import pytest++from tests.e2e.core.windows._helpers import wait_until+from tests.e2e.core.windows_update._machine import (+ REQUIRES_OPT_IN,+ Journey,+ Machine,+ fail_with,+ new_machine,+ one_shot_turn,+)+from tests.fakes.fake_llm_provider import FakeLLMServer++pytestmark = [pytest.mark.platforms("windows"), pytest.mark.integration,+ pytest.mark.live_system_guard_bypass, REQUIRES_OPT_IN]++# The first parent of the #106742 merge (eb75627faf5): the last main before the unified runtime.+PRE_RUNTIME_RELEASE = os.environ.get("HERMES_E2E_W4_BASE", "d04056d609bb53530843a6c630493243b2ebdc24")+_STATUS_LINE = re.compile(r"^.*[✓✗].*[Gg]ateway.*$", re.MULTILINE)+_RESTART_FAILURE = re.compile(+ r"^.*(recovery failed|restart could not be verified|restart incomplete|not verified alive).*$", re.MULTILINE)+++def _status_line(text: str) -> str:+ found = _STATUS_LINE.findall(text)+ return found[0].strip() if found else "<no status line>"+++def _alive(pid: int) -> bool:+ import psutil++ try:+ return psutil.Process(pid).is_running()+ except psutil.Error:+ return False+++def _gateway_files(m: Machine) -> list[Path]:+ """What a gateway opens for writing at start: its home, lock, pid and state records, logs, the+ per-user host lock dir, and every top-level entry the elevated run left in the home."""+ home = m.hermes_home+ paths = [home, home / "gateway.lock", home / "gateway.pid", home / "gateway_state.json", home / "logs"]+ lock_dir = m.profile / ".local" / "state" / "hermes" / "gateway-locks"+ if lock_dir.is_dir():+ paths += [lock_dir, *sorted(lock_dir.iterdir())]+ skip = {"hermes-agent", "tools", "bin"} # code and toolchain: read+execute (test_paths_and_acls.py)+ paths += [p for p in sorted(home.iterdir()) if p.name not in skip]+ seen, out = set(), []+ for p in paths:+ key = os.path.normcase(str(p))+ if key not in seen and p.exists():+ seen.add(key)+ out.append(p)+ return out+++def _user_write_access(m: Machine) -> dict[Path, str]:+ from tests.e2e.core.windows_update._tokens import FILE_READ_WRITE, standard_user_access++ return standard_user_access(_gateway_files(m), FILE_READ_WRITE)+++def _lock_held(m: Machine) -> bool:+ """True while some process holds ``gateway.lock`` (a byte-range lock or a sharing deny)."""+ import msvcrt++ path = m.hermes_home / "gateway.lock"+ try:+ fh = open(path, "r+b")+ except PermissionError:+ return True+ except OSError:+ return False+ with fh:+ fh.seek(1024 * 1024) # gateway/status.py _WINDOWS_LOCK_OFFSET, the same in both releases+ try:+ msvcrt.locking(fh.fileno(), msvcrt.LK_NBLCK, 1)+ except OSError:+ return True+ fh.seek(1024 * 1024)+ msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)+ return False+++def _machine(tmp_path_factory, srv: FakeLLMServer, label: str) -> Machine:+ from tests.e2e.core.windows_update._tokens import grant_user_profile_acl, hand_to_user++ machine = new_machine(tmp_path_factory.mktemp(label), srv.base_url, label=label, system_git=True,+ base=PRE_RUNTIME_RELEASE)+ # A real profile's DACL (the user owns their home), and the origin + gitconfig the user can+ # read: harness facts, so a standard-user verdict is about Hermes and nothing else.+ grant_user_profile_acl(machine.profile)+ hand_to_user(machine.root)+ return machine+++def _token_control(m: Machine) -> str:+ from tests.e2e.core.windows_update._tokens import harness_control, probe_token_elevation++ harness_control(m.root)+ return probe_token_elevation(m.profile, m.env(as_user=True), m.logs / "00-token-probe.log")+++def _wait_new_gateway(m: Machine, old_pid: int | None) -> dict:+ return m.wait_gateway_running(not_pid=old_pid)+++def _pid(state) -> int:+ return int(state.get("pid") or 0) if isinstance(state, dict) else 0+++def _stopped(pid: int) -> bool:+ try:+ wait_until(lambda: not _alive(pid), 90, f"gateway pid {pid} to exit", interval=0.5)+ except AssertionError:+ return False+ return True++[email protected](scope="module")+def elevated(tmp_path_factory):+ with FakeLLMServer() as srv:+ machine = _machine(tmp_path_factory, srv, "w4e")+ j = Journey(machine)+ try:+ j.step("control", lambda: _token_control(machine))+ install = j.step("install", machine.install)+ if j.ok("install"):+ j.step("install_ok", lambda: j.require("install", install.returncode == 0,+ f"install.ps1 ({PRE_RUNTIME_RELEASE[:11]}) exited "+ f"{install.returncode}", install))+ if j.ok("install_ok"):+ with machine.gateway_phase():+ j.step("installed_base", machine.installed_head)+ j.step("spawn_old", machine.spawn_gateway)+ old = j.step("old_state", machine.wait_gateway_running)+ j.step("old_lock_held", lambda: _lock_held(machine))+ machine.advance() # the new release lands on main: an update is available+ j.step("update", machine.update)+ j.step("new_state", lambda: _wait_new_gateway(machine, _pid(old) or None))+ j.step("status_admin", lambda: machine.hermes("gateway", "status", label="status-admin"))+ j.step("status_user", lambda: machine.hermes("gateway", "status", label="status-user",+ as_user=True))+ j.step("turn_admin", lambda: one_shot_turn(machine, srv, "turn-admin"))+ j.step("user_access", lambda: _user_write_access(machine))+ # The elevated gateway goes; the same user, unelevated, takes the lock next.+ new_pid = _pid(j.results.get("new_state"))+ j.step("stop_admin", lambda: machine.hermes("gateway", "stop", label="stop-admin"))+ j.step("admin_stopped", lambda: _stopped(new_pid))+ j.step("spawn_user", lambda: machine.spawn_gateway(as_user=True))+ j.step("user_state", lambda: _wait_new_gateway(machine, new_pid or None))+ j.step("user_status", lambda: machine.hermes("gateway", "status", label="status-user-gw",+ as_user=True))+ j.step("user_turn", lambda: one_shot_turn(machine, srv, "turn-user", as_user=True))+ user_pid = _pid(j.results.get("user_state"))+ j.step("stop_user", lambda: machine.hermes("gateway", "stop", label="stop-user", as_user=True))+ j.step("user_stopped", lambda: _stopped(user_pid))+ machine.kill_owned()+ yield j+ finally:+ machine.teardown()++[email protected](scope="module")+def standard_user(tmp_path_factory):+ with FakeLLMServer() as srv:+ machine = _machine(tmp_path_factory, srv, "w4u")+ j = Journey(machine)+ try:+ j.step("control", lambda: _token_control(machine))+ install = j.step("install", lambda: machine.install(as_user=True))+ if j.ok("install"):+ j.step("install_ok", lambda: j.require("install", install.returncode == 0,+ f"install.ps1 ({PRE_RUNTIME_RELEASE[:11]}) as a standard "+ f"user exited {install.returncode}", install))+ if j.ok("install_ok"):+ with machine.gateway_phase():+ j.step("installed_base", machine.installed_head)+ j.step("spawn_old", lambda: machine.spawn_gateway(as_user=True))+ old = j.step("old_state", machine.wait_gateway_running)+ j.step("old_lock_held", lambda: _lock_held(machine))+ machine.advance()+ j.step("update", lambda: machine.update(as_user=True))+ j.step("new_state", lambda: _wait_new_gateway(machine, _pid(old) or None))+ j.step("status_user", lambda: machine.hermes("gateway", "status", label="status-user",+ as_user=True))+ j.step("user_turn", lambda: one_shot_turn(machine, srv, "turn-user", as_user=True))+ new_pid = _pid(j.results.get("new_state"))+ j.step("stop_user", lambda: machine.hermes("gateway", "stop", label="stop-user", as_user=True))+ j.step("user_stopped", lambda: _stopped(new_pid))+ machine.kill_owned()+ yield j+ finally:+ machine.teardown()+++# -- shared verdicts ---------------------------------------------------------------------------+++def _assert_old_gateway_held_lock(j: Journey) -> None:+ m, state = j.machine, j["old_state"]+ assert j["installed_base"] == m.head, fail_with(+ m, f"the install is not the pre-runtime release: checkout {j['installed_base']}, expected {m.head}")+ assert str(state.get("code_sha") or "")[:7] == m.head[:7] or not state.get("code_sha"), fail_with(+ m, f"the gateway before the update serves {state.get('code_sha')!r}, expected the base {m.head}")+ assert j["old_lock_held"], fail_with(+ m, f"the old gateway (pid {state.get('pid')}) is running but nothing holds gateway.lock")+++def _assert_update_replaced_gateway(j: Journey) -> None:+ m, run = j.machine, j["update"]+ failure = _RESTART_FAILURE.search(run.stdout)+ assert run.returncode == 0 and failure is None and m.installed_head() == m.next, fail_with(+ m, f"hermes update over the running pre-runtime gateway failed (rc={run.returncode}, checkout "+ f"{m.installed_head()}, expected {m.next}): "+ f"{failure.group(0).strip() if failure else '<no restart message>'}", run)+ state = j["new_state"]+ sha = str(state.get("code_sha") or "")+ assert len(sha) >= 7 and m.next.startswith(sha), fail_with(+ m, f"after the update the gateway (pid {state.get('pid')}) serves code_sha={sha!r}, expected {m.next}")+ assert _pid(state) != _pid(j["old_state"]), fail_with(m, "the old gateway was never replaced")+++def _assert_status_sees(j: Journey, status_key: str, state_key: str, who: str) -> None:+ m, status, state = j.machine, j[status_key], j[state_key]+ line = _status_line(status.stdout)+ assert status.returncode == 0 and line.startswith("✓") and str(_pid(state)) in line, fail_with(+ m, f"`hermes gateway status` ({who}) does not see the running gateway (pid {_pid(state)}): {line!r}",+ status)+++def _assert_turn(j: Journey, key: str, who: str) -> None:+ m, turn = j.machine, j[key]+ assert turn.ok, fail_with(+ m, f"a turn ({who}) after the upgrade got no reply (reply printed={turn.reply_id in turn.run.stdout}, "+ f"prompt reached provider={turn.reached_wire})", turn.run)+++# -- elevated journey ----------------------------------------------------------------------------+++def test_elevated_harness_token_is_standard(elevated: Journey) -> None:+ assert elevated["control"] == "elevated=False admin=False"+++def test_elevated_old_gateway_held_lock_before_update(elevated: Journey) -> None:+ _assert_old_gateway_held_lock(elevated)+++def test_elevated_update_replaces_old_gateway(elevated: Journey) -> None:+ _assert_update_replaced_gateway(elevated)+++def test_elevated_status_sees_new_gateway(elevated: Journey) -> None:+ _assert_status_sees(elevated, "status_admin", "new_state", "administrator console")+ _assert_status_sees(elevated, "status_user", "new_state", "same user, not elevated")+++def test_elevated_turn_replies_after_update(elevated: Journey) -> None:+ _assert_turn(elevated, "turn_admin", "administrator console")+++def test_elevated_upgrade_leaves_gateway_files_usable_unelevated(elevated: Journey) -> None:+ m, verdicts = elevated.machine, elevated["user_access"]+ elevated["update"]+ assert any(p.name == "gateway.lock" for p in verdicts), fail_with(+ m, f"no gateway.lock after the upgrade under {m.hermes_home}")+ broken = [f"{p}: {why}" for p, why in verdicts.items() if why]+ assert not broken, fail_with(+ m, f"after an elevated upgrade the same user, not elevated, cannot write: {'; '.join(broken)}")+++def test_elevated_upgrade_then_user_gateway_takes_lock(elevated: Journey) -> None:+ m = elevated.machine+ stop = elevated["stop_admin"]+ assert stop.returncode == 0 and elevated["admin_stopped"], fail_with(+ m, f"`hermes gateway stop` left the elevated gateway (pid {_pid(elevated['new_state'])}) running", stop)+ elevated["spawn_user"]+ state = elevated["user_state"]+ sha = str(state.get("code_sha") or "")+ assert m.next.startswith(sha) and len(sha) >= 7, fail_with(+ m, f"the unelevated gateway serves {sha!r}, expected {m.next}")+ _assert_status_sees(elevated, "user_status", "user_state", "same user, not elevated")+ _assert_turn(elevated, "user_turn", "same user, not elevated")+ assert elevated["stop_user"].returncode == 0 and elevated["user_stopped"], fail_with(+ m, f"the unelevated `hermes gateway stop` left its gateway (pid {_pid(state)}) running",+ elevated["stop_user"])+++# -- standard-user journey -----------------------------------------------------------------------+++def test_standard_user_harness_token_is_standard(standard_user: Journey) -> None:+ assert standard_user["control"] == "elevated=False admin=False"+++def test_standard_user_old_gateway_held_lock_before_update(standard_user: Journey) -> None:+ _assert_old_gateway_held_lock(standard_user)+++def test_standard_user_update_replaces_old_gateway(standard_user: Journey) -> None:+ _assert_update_replaced_gateway(standard_user)+++def test_standard_user_status_and_turn_after_update(standard_user: Journey) -> None:+ _assert_status_sees(standard_user, "status_user", "new_state", "standard user")+ _assert_turn(standard_user, "user_turn", "standard user")+ m = standard_user.machine+ assert standard_user["stop_user"].returncode == 0 and standard_user["user_stopped"], fail_with(+ m, f"`hermes gateway stop` left the gateway (pid {_pid(standard_user['new_state'])}) running",+ standard_user["stop_user"])