The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): a refused update-job join is logged and noted in the receipt (m1)
Teknium228217941bab
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.pyindex 41f8cf435db..a9a8af733b5 100644--- a/hermes_cli/update_custody.py+++ b/hermes_cli/update_custody.py@@ -199,7 +199,9 @@ def run_git(git_cmd: Sequence[str], args: Sequence[str], **kwargs) -> subprocess# 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 fails-# the build: the command runs outside the job (the pre-custody behavior) and says so on stderr.+# the build: the command runs outside the job (the pre-custody behavior), says so on stderr and+# writes the notice to the report file (argv[2]) so the updater can log it and note it in the+# update receipt — run_contained captures the child's stderr, so stderr alone is not seen (m1)._CUSTODY_UNAVAILABLE = "hermes: update custody unavailable"_JOIN_JOB = ("import ctypes, subprocess, sys\n"@@ -209,14 +211,43 @@ _JOIN_JOB = ("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" sys.stderr.write('{_CUSTODY_UNAVAILABLE} (could not join the update job: %d); '\n"- " 'this child runs outside it\\n' % ctypes.get_last_error())\n"+ f" note = ('{_CUSTODY_UNAVAILABLE} (could not join the update job: %d); '\n"+ " 'this child runs outside it' % ctypes.get_last_error())\n"+ " sys.stderr.write(note + '\\n')\n"" sys.stderr.flush()\n"+ " try:\n"+ " with open(sys.argv[2], 'w', encoding='utf-8') as report:\n"+ " report.write(note)\n"+ " except OSError:\n"+ " pass\n""k.CloseHandle(h)\n"- "sys.exit(subprocess.call(sys.argv[2:], stdin=subprocess.DEVNULL))\n"+ "sys.exit(subprocess.call(sys.argv[3:], stdin=subprocess.DEVNULL))\n")+def _report_refused_join(report: str, argv: Sequence[str]) -> str | None:+ """Make a refused job join visible: a warning (errors.log) and a receipt step. The notice,+ or None when the child joined (or never ran the launcher)."""+ import os++ try:+ with open(report, encoding="utf-8-sig") as fh:+ note = fh.read().strip()+ except OSError:+ note = ""+ finally:+ with contextlib.suppress(OSError):+ os.unlink(report)+ if not note:+ return None+ logger.warning("%s: %s", note, " ".join(str(arg) for arg in argv[:2]))+ with contextlib.suppress(Exception):+ from hermes_cli.update_receipt import record_step++ record_step("update_custody", False, f"{note}: {' '.join(str(arg) for arg in argv[:2])}")+ return note++def _join_launcher_python() -> str:"""The interpreter for the job-joining launcher: the real one, never a venv redirector.@@ -249,14 +280,21 @@ def contained_command(argv: Sequence[str], *, inherit_lock: bool = True):if handle is None:yield argv, {}return+ import os+ import tempfile++ fd, report = tempfile.mkstemp(prefix="hermes-custody-", suffix=".txt")+ os.close(fd)try:info = subprocess.STARTUPINFO()info.lpAttributeList = {"handle_list": [handle]}- yield [_join_launcher_python(), "-I", "-S", "-c", _JOIN_JOB, str(handle), *argv], {"startupinfo": info}+ yield ([_join_launcher_python(), "-I", "-S", "-c", _JOIN_JOB, str(handle), report, *argv],+ {"startupinfo": info})finally:import ctypesctypes.WinDLL("kernel32").CloseHandle(ctypes.c_void_p(handle))+ _report_refused_join(report, argv)def _inheritable_job_handle() -> int | None:diff --git a/tests/hermes_cli/test_update_custody.py b/tests/hermes_cli/test_update_custody.pyindex 57d49fd3273..5f4803e5d8a 100644--- a/tests/hermes_cli/test_update_custody.py+++ b/tests/hermes_cli/test_update_custody.py@@ -164,3 +164,34 @@ def _state(pid: int) -> str:except OSError:return "Z"return stat[stat.rfind(")") + 2:][:1]+++def test_a_refused_build_join_is_logged_and_noted_in_the_receipt(tmp_path, monkeypatch, caplog):+ """m1: the job-joining launcher's notice used to reach only the build's captured stderr+ (run_contained merges it into the output it drops on success). The report it leaves is now+ a warning (errors.log) and a receipt step; the build's outcome is untouched."""+ import json+ import logging++ import hermes_cli.update_receipt as ur+ from hermes_cli import update_custody++ home = tmp_path / ".hermes"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ report = tmp_path / "report.txt"+ note = f"{update_custody._CUSTODY_UNAVAILABLE} (could not join the update job: 5); this child runs outside it"+ report.write_text(note, encoding="utf-8")+ with ur.update_receipt_scope():+ ur.begin_update_receipt()+ with caplog.at_level(logging.WARNING, logger="hermes_cli.update_custody"):+ assert update_custody._report_refused_join(str(report), ["node", "scripts/build/tui.mjs"]) == note+ payload = json.loads(ur.finalize_update_receipt("success").read_text(encoding="utf-8-sig"))+ assert any(note in rec.getMessage() and "tui.mjs" in rec.getMessage() for rec in caplog.records)+ steps = [s for s in payload["steps"] if s["name"] == "update_custody"]+ assert steps and steps[0]["ok"] is False and note in steps[0]["detail"], payload["steps"]+ assert payload["outcome"] == "success"+ assert not report.exists()+ empty = tmp_path / "joined.txt"+ empty.write_text("", encoding="utf-8")+ assert update_custody._report_refused_join(str(empty), ["node"]) is None # joined: silentdiff --git a/tests/hermes_cli/test_update_lock_windows_live.py b/tests/hermes_cli/test_update_lock_windows_live.pyindex b4205a17c5e..8f0cbcaa912 100644--- a/tests/hermes_cli/test_update_lock_windows_live.py+++ b/tests/hermes_cli/test_update_lock_windows_live.py@@ -185,8 +185,11 @@ def test_a_refused_job_join_still_runs_the_child(tmp_path):child = "import sys; print('built'); sys.exit(3)"launcher = tmp_path / "join_job.py" # a file, not -c: the guard reads argv text as a command linelauncher.write_text(_JOIN_JOB, encoding="utf-8")- out = subprocess.run([sys.executable, "-I", "-S", str(launcher), "0", sys.executable, "-c", child],+ report = tmp_path / "report.txt"+ out = subprocess.run([sys.executable, "-I", "-S", str(launcher), "0", str(report), sys.executable, "-c", child],capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60)assert out.returncode == 3, outassert out.stdout.strip() == "built"assert _CUSTODY_UNAVAILABLE in out.stderr+ # m1: the notice also lands in the report the updater turns into a warning + receipt step+ assert _CUSTODY_UNAVAILABLE in report.read_text(encoding="utf-8")