Skip to content

The agent that grows with you

fix(update): a build command that would start outside the update job never runs (review F54)

Teknium24137c7d270d
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index 975f83f84f2..26a2aee0e82 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -361,9 +361,9 @@ def _prefetch_for_move(git_cmd: list[str], args: list[str], kwargs: dict) -> Non
# A stdlib launcher for children whose Popen the updater never sees (``run_contained``): it
-# joins the job named by an inherited handle, drops that handle (only the owner's handle may
-# keep the job open) and runs the real command with the same stdio. A refused join never runs
-# the command (D2): the launcher says so on stderr, writes the notice to the report file
+# joins the job named by an inherited handle, starts the real command suspended with the same
+# stdio, runs it only once it is in the job, and drops that handle (only the owner's handle may
+# keep the job open). A refused join, or a command outside the job, never runs (D2): the launcher says so on stderr, writes the notice to the report file
# (argv[2]) — run_contained captures the child's stderr, so stderr alone is not seen (m1) — and
# exits; the updater logs the notice, notes it in the receipt and raises CustodyRefused.
_CUSTODY_UNAVAILABLE = "hermes: update custody unavailable"
@@ -375,9 +375,8 @@ _JOIN_JOB = (
"k.GetCurrentProcess.restype = ctypes.c_void_p\n"
"k.CloseHandle.argtypes = [ctypes.c_void_p]\n"
"h = ctypes.c_void_p(int(sys.argv[1]))\n"
- "if not k.AssignProcessToJobObject(h, k.GetCurrentProcess()):\n"
- f" note = ('{_CUSTODY_UNAVAILABLE} (could not join the update job: %d); '\n"
- " 'the command was not run' % ctypes.get_last_error())\n"
+ "def refuse(why):\n"
+ f" note = '{_CUSTODY_UNAVAILABLE} (%s); the command was not run' % why\n"
" sys.stderr.write(note + '\\n')\n"
" sys.stderr.flush()\n"
" try:\n"
@@ -386,8 +385,24 @@ _JOIN_JOB = (
" except OSError:\n"
" pass\n"
f" sys.exit({_REFUSED_EXIT})\n"
+ "if not k.AssignProcessToJobObject(h, k.GetCurrentProcess()):\n"
+ " refuse('could not join the update job: %d' % ctypes.get_last_error())\n"
+ # Verify, never assume the topology: a redirecting interpreter (a Store Python alias) can
+ # join while what it starts lands outside the job. Start the command suspended and run it
+ # only once Windows says it is in the job (F54).
+ "k.IsProcessInJob.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]\n"
+ "p = subprocess.Popen(sys.argv[3:], stdin=subprocess.DEVNULL, creationflags=0x4)\n"
+ "inside = ctypes.c_int(0)\n"
+ "if not k.IsProcessInJob(ctypes.c_void_p(int(p._handle)), h, ctypes.byref(inside)) or not inside.value:\n"
+ " p.kill()\n"
+ " p.wait()\n"
+ " refuse('the command would start outside the update job')\n"
"k.CloseHandle(h)\n"
- "sys.exit(subprocess.call(sys.argv[3:], stdin=subprocess.DEVNULL))\n"
+ "nt = ctypes.WinDLL('ntdll')\n"
+ "nt.NtResumeProcess.argtypes = [ctypes.c_void_p]\n"
+ "if nt.NtResumeProcess(ctypes.c_void_p(int(p._handle))) != 0:\n"
+ " p.kill()\n"
+ "sys.exit(p.wait())\n"
)
diff --git a/tests/hermes_cli/test_update_lock_windows_live.py b/tests/hermes_cli/test_update_lock_windows_live.py
index 53135d325f1..0958c02e986 100644
--- a/tests/hermes_cli/test_update_lock_windows_live.py
+++ b/tests/hermes_cli/test_update_lock_windows_live.py
@@ -288,6 +288,38 @@ def test_a_refused_job_join_never_runs_the_command(tmp_path):
+def test_a_command_that_would_start_outside_the_job_never_runs(tmp_path):
+ """F54: a launcher interpreter that joins while what it starts escapes the job (a Store
+ Python alias) must not run the command. Control: a job with SILENT_BREAKAWAY_OK, which the
+ launcher joins but whose children start outside it."""
+ import ctypes
+
+ from hermes_cli.update_custody import _CUSTODY_UNAVAILABLE, _JOIN_JOB, _REFUSED_EXIT
+
+ class _Basic(ctypes.Structure): # JOBOBJECT_BASIC_LIMIT_INFORMATION
+ _fields_ = [("user_limits", ctypes.c_int64 * 2), ("LimitFlags", ctypes.c_uint32),
+ ("working_set", ctypes.c_size_t * 2), ("ActiveProcessLimit", ctypes.c_uint32),
+ ("Affinity", ctypes.c_size_t), ("classes", ctypes.c_uint32 * 2)]
+
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
+ kernel32.CreateJobObjectW.restype = ctypes.c_void_p
+ kernel32.SetInformationJobObject.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_ulong]
+ job = kernel32.CreateJobObjectW(None, None)
+ limits = _Basic(LimitFlags=0x1000) # JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
+ assert kernel32.SetInformationJobObject(job, 2, ctypes.byref(limits), ctypes.sizeof(limits))
+ os.set_handle_inheritable(job, True)
+ launcher = tmp_path / "join_job.py"
+ launcher.write_text(_JOIN_JOB, encoding="utf-8")
+ info = subprocess.STARTUPINFO()
+ info.lpAttributeList = {"handle_list": [job]}
+ out = subprocess.run([sys.executable, "-I", "-S", str(launcher), str(job), str(tmp_path / "report.txt"),
+ sys.executable, "-c", "print('built')"], startupinfo=info, capture_output=True,
+ text=True, encoding="utf-8", errors="replace", timeout=60)
+ assert out.returncode == _REFUSED_EXIT, out
+ assert "built" not in out.stdout and _CUSTODY_UNAVAILABLE in out.stderr, out
+
+
+
# --- R8 m1: the completion child is bound before it runs; a refused bind reaches the receipt ---
_COMPLETION_CHILD = (