2026-10-03 10:06:12 -07:00
|
|
|
"""The single commit point of a source ``hermes update``: everything the tree move needs armed first.
|
|
|
|
|
|
|
|
|
|
Before git (or the ZIP swap) writes the first file of the new code, three things are durable:
|
|
|
|
|
|
|
|
|
|
* the interrupted-pull marker (``.git/hermes-update-pull``) naming the commit git moves to, for EVERY
|
|
|
|
|
tree move (branch switch, upstream fork pull, the pull itself) so a launch after any exit that left
|
|
|
|
|
a torn tree puts the old one back (``_early_recovery.restore_interrupted_pull``);
|
|
|
|
|
* the source-completion tail (``venv_sync.arm_completion``) and the host fleet-restart obligation,
|
|
|
|
|
so a kill after the move but before the completion child starts still leaves the tail owed to the
|
|
|
|
|
next launch (``venv_sync.prepare_launch``) and the restart owed to the next ``hermes update``.
|
|
|
|
|
|
|
|
|
|
Failures before the move are no-ops: ``disarm_commit_obligations`` puts both obligations back the way
|
|
|
|
|
this run found them once the tree is verified at its pre-update commit again.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
import logging
|
2026-10-03 10:06:12 -07:00
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
2026-10-04 13:18:18 -07:00
|
|
|
from contextlib import suppress
|
2026-10-03 10:06:12 -07:00
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
2026-10-04 13:18:18 -07:00
|
|
|
from hermes_cli._early_recovery import (
|
|
|
|
|
RECOVERY_CLOSURE,
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
RECOVERY_CLOSURE_INIT,
|
|
|
|
|
RECOVERY_CLOSURE_MANIFEST,
|
|
|
|
|
blob_id,
|
2026-10-05 02:49:51 -07:00
|
|
|
_lock_identity,
|
2026-10-04 13:18:18 -07:00
|
|
|
interrupted_pull_marker,
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
is_object_id,
|
2026-10-04 13:18:18 -07:00
|
|
|
recovery_closure_dir,
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
recovery_closure_verified,
|
2026-10-04 13:18:18 -07:00
|
|
|
restore_interrupted_pull,
|
2026-10-05 01:48:38 -07:00
|
|
|
write_durable_text,
|
2026-10-04 13:18:18 -07:00
|
|
|
)
|
2026-10-07 00:19:33 -07:00
|
|
|
from hermes_cli.update_cmd_common import _record_stop
|
2026-10-04 02:28:55 -07:00
|
|
|
from hermes_cli.update_custody import run_git
|
2026-10-03 10:06:12 -07:00
|
|
|
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
logger = logging.getLogger("hermes_cli.update_cmd")
|
2026-10-03 10:06:12 -07:00
|
|
|
# What this run found before it armed anything: {path: bytes or None}. None = nothing armed yet.
|
|
|
|
|
_armed_snapshot: Optional[dict[Path, Optional[bytes]]] = None
|
2026-10-04 23:26:03 -07:00
|
|
|
# What this run's latest arm left at each path: disarm hands back only records still exactly these.
|
|
|
|
|
_armed_bytes: dict[Path, Optional[bytes]] = {}
|
2026-10-03 13:17:21 -07:00
|
|
|
# (git_cmd, (HEAD, branch)) when this run reached its checkout phase, and the checkout it names.
|
2026-10-03 10:06:12 -07:00
|
|
|
_run_start: Optional[tuple[list, tuple[str, str]]] = None
|
2026-10-03 13:17:21 -07:00
|
|
|
_obligation_root: Optional[Path] = None
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
# This run's stake in the shared host record (``owners``): disarm removes only this one.
|
|
|
|
|
_owner: str = ""
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _owns_live_checkout(root: Path) -> bool:
|
|
|
|
|
from hermes_cli.update_cmd import _m
|
|
|
|
|
|
|
|
|
|
return _m()._pytest_owns_live_checkout(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _obligation_paths(root: Path) -> list[Path]:
|
|
|
|
|
from hermes_cli.update_cmd_fleet import _fleet_restart_pending_marker_path
|
|
|
|
|
from hermes_cli.update_host_obligation import host_obligation_path
|
|
|
|
|
from hermes_cli.venv_sync import completion_pending_path
|
|
|
|
|
|
2026-10-04 23:26:03 -07:00
|
|
|
return [completion_pending_path(root), _fleet_restart_pending_marker_path(), host_obligation_path()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _read_or_none(path: Path) -> Optional[bytes]:
|
|
|
|
|
"""The record's bytes, None when absent. Any other read error raises: custody is never guessed."""
|
|
|
|
|
try:
|
|
|
|
|
return path.read_bytes()
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
return None
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def arm_commit_obligations(root: Path, expected_sha: str) -> None:
|
|
|
|
|
"""Owe the completion tail and the fleet restart for ``expected_sha`` BEFORE the tree moves.
|
|
|
|
|
|
|
|
|
|
Idempotent within a run (the first call snapshots what to restore on a no-op failure). An
|
2026-10-04 23:26:03 -07:00
|
|
|
unwritable install state, an unreadable record or a fleet restart neither store accepted
|
|
|
|
|
raises: nothing has moved yet, and moving without the obligation is exactly the
|
|
|
|
|
tail-never-runs state this exists to prevent.
|
2026-10-03 10:06:12 -07:00
|
|
|
"""
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
global _armed_snapshot, _owner
|
|
|
|
|
import uuid
|
|
|
|
|
|
2026-10-03 10:06:12 -07:00
|
|
|
from hermes_cli.update_cmd_fleet import _write_fleet_restart_pending_marker
|
|
|
|
|
from hermes_cli.venv_sync import arm_completion
|
|
|
|
|
|
|
|
|
|
root = Path(root)
|
|
|
|
|
if _owns_live_checkout(root):
|
|
|
|
|
return
|
|
|
|
|
if _armed_snapshot is None:
|
2026-10-04 23:26:03 -07:00
|
|
|
_armed_snapshot = {path: _read_or_none(path) for path in _obligation_paths(root)}
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
_owner = f"{root}|{os.getpid()}|{uuid.uuid4().hex}"
|
2026-10-04 23:26:03 -07:00
|
|
|
try:
|
|
|
|
|
arm_completion(root)
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
owed = _write_fleet_restart_pending_marker(expected_sha=expected_sha or "", owner=_owner)
|
2026-10-04 23:26:03 -07:00
|
|
|
finally: # even a half-done arm: disarm must still recognise what this run wrote
|
|
|
|
|
_armed_bytes.update({path: _read_or_none(path) for path in _armed_snapshot})
|
|
|
|
|
if not owed:
|
|
|
|
|
raise OSError("could not record the pending gateway-restart obligation")
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def disarm_commit_obligations() -> None:
|
2026-10-03 13:17:21 -07:00
|
|
|
"""Restore both obligations to what this run found: the tree never left its pre-update commit.
|
|
|
|
|
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
Off the commit this run STARTED from nothing is handed back: a failed later move (CP1 refused
|
|
|
|
|
or failed after the CP0 branch switch, the upstream fork ff after the origin pull) settles on a
|
|
|
|
|
commit that is already new code, so the obligations are owed for THAT head instead of the
|
|
|
|
|
failed move's target, which the checkout does not contain and no restart could discharge.
|
2026-10-03 13:17:21 -07:00
|
|
|
"""
|
2026-10-03 10:06:12 -07:00
|
|
|
global _armed_snapshot
|
2026-10-03 13:17:21 -07:00
|
|
|
if _run_start is not None:
|
|
|
|
|
git_cmd, (start_head, _branch) = _run_start
|
|
|
|
|
root = Path(_obligation_root) if _obligation_root is not None else None
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
head = head_and_branch(git_cmd, root)[0] if root is not None else ""
|
|
|
|
|
if root is None or not start_head or head != start_head:
|
|
|
|
|
if root is not None:
|
|
|
|
|
_owe_for(root, head)
|
2026-10-03 13:17:21 -07:00
|
|
|
return
|
2026-10-05 06:02:29 -07:00
|
|
|
from hermes_cli.update_host_obligation import host_obligation_path, release_host_obligation, replace_bytes
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
|
2026-10-03 10:06:12 -07:00
|
|
|
snapshot, _armed_snapshot = _armed_snapshot, None
|
2026-10-04 23:26:03 -07:00
|
|
|
armed = dict(_armed_bytes)
|
|
|
|
|
_armed_bytes.clear()
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
host = host_obligation_path()
|
2026-10-03 10:06:12 -07:00
|
|
|
for path, data in (snapshot or {}).items():
|
|
|
|
|
try:
|
fix(update): one install's failed run hands back only its own stake in the host restart record
What: the host obligation record carries `owners` (one commit-point token per arming run:
install root, pid, uuid) and `found` (base64 of what its first owner found there).
write_host_obligation(owner=) adds the run to a same-SHA record instead of keeping it
byte-identical; disarm_commit_obligations releases the host record through
release_host_obligation, which removes only this run's token and, when no owner is left,
puts back what the first owner found (absent -> unlinked). A record whose owners do not name
the run (rewritten by another install's newer pull, or unparseable) stays untouched. The
non-host restores in disarm now fsync before os.replace (review C12, partial).
Why: the F22 disarm compared bytes, but a same-SHA arm kept the record unchanged, so install
A and install B arming the same SHA shared byte-identical bytes and A's failed run deleted the
debt B still owed through (review C2, probe review/commit/w2/probe_cas.py).
Test (RED at 5dfc384669e: record deleted): two installs arm the same SHA; A's disarm keeps the
record, the last owner's disarm puts back what the first owner found (nothing).
2026-10-05 01:46:48 -07:00
|
|
|
if path == host:
|
|
|
|
|
# Shared by every install of the OS user: same-SHA arms join one record, so bytes
|
|
|
|
|
# cannot tell whose it is; the ``owners`` stake can (review C2).
|
|
|
|
|
release_host_obligation(_owner)
|
|
|
|
|
continue
|
2026-10-04 23:26:03 -07:00
|
|
|
if path not in armed or _read_or_none(path) != armed[path]:
|
|
|
|
|
continue # rewritten since this run armed it (another install's update): theirs now
|
2026-10-03 10:06:12 -07:00
|
|
|
if data is None:
|
|
|
|
|
path.unlink(missing_ok=True)
|
|
|
|
|
else:
|
2026-10-05 06:02:29 -07:00
|
|
|
# An unpredictable exclusive temp, never a fixed name: nothing at any guessable
|
|
|
|
|
# sibling is written through or deleted first (reviews N05, O1).
|
|
|
|
|
replace_bytes(path, data)
|
2026-10-03 10:06:12 -07:00
|
|
|
except OSError:
|
|
|
|
|
pass # an owed tail/restart left armed is a retry, never a lost obligation
|
|
|
|
|
|
|
|
|
|
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
def owe_restore_of(root: Path, pre: str | None) -> None:
|
|
|
|
|
"""A failed move left a torn tree under its marker: the next launch's restore puts ``pre`` back,
|
|
|
|
|
so the obligations name ``pre``, never the target the checkout will not hold."""
|
|
|
|
|
_owe_for(Path(root), pre or "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _owe_for(root: Path, sha: str) -> None:
|
|
|
|
|
"""Re-arm what this run armed for ``sha``, the commit the checkout is (or is restored) on."""
|
|
|
|
|
if _armed_snapshot is None or not is_object_id(sha):
|
|
|
|
|
return # nothing armed by this run, or nothing names the code: keep what stands
|
|
|
|
|
try:
|
|
|
|
|
arm_commit_obligations(root, sha)
|
|
|
|
|
except OSError as exc:
|
|
|
|
|
logger.warning("Could not re-arm the update obligations for %s: %s", sha[:10], exc)
|
|
|
|
|
print(f" ⚠ Could not record the restart this update still owes for {sha[:10]} ({exc}); "
|
|
|
|
|
"run `hermes gateway restart` once the update is done.", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
fix(update): the fork sync judges its target before moving and owes a commit every landing holds (review G1, O4, F23)
What: _sync_with_upstream_if_needed compiles the upstream commit's startup inventory before its
fast-forward (the origin target's preflight, applied to the second move) and raises
UpstreamTargetBroken without moving; both callers roll the update back the way the post-pull
guard did (the late one checks the branch first, so an unexpected branch's ref is never reset).
A later move of the run arms its obligations for its pre commit (debt_sha_for_move): every
landing of a fast-forward contains it and the fleet discharge holds the gateways to the actual
HEAD, so a failed merge needs no retarget write. The merge verifies HEAD is the counted commit
before reporting success.
Why: the fork ff dropped its marker on git's exit 0 and only the caller's later guard judged the
code, so a kill in between left a broken commit with no recovery record (G1); a failed merge's
retarget to the pulled commit could itself fail and leave the debt naming the unmerged commit
with HEAD on the pulled one (O4, the residual of F23).
Changed test: test_update_syntax_failure_restores_pre_update_head pinned the post-pull guard
catching the upstream target ("Pulled code has a syntax error") and the broken commit landing on
the unexpected branch; the target is now refused before its move, so it asserts the refusal
text and that the broken commit never landed. Outcomes (exit 1, HEAD back at base, stash) hold.
Tests (RED at fd5c37f17bf): a refused retarget leaves the debt on the pulled commit; a broken
upstream target is refused with HEAD unmoved and no marker.
2026-10-05 06:14:30 -07:00
|
|
|
def debt_sha_for_move(pre: str, target: str) -> str:
|
|
|
|
|
"""The commit a fast-forward ``pre`` -> ``target`` arms the obligations for.
|
|
|
|
|
|
|
|
|
|
A later move of this run (the upstream sync after the origin pull) owes ``pre``: the commit the
|
|
|
|
|
earlier move already owes, which every landing of a fast-forward contains, and the fleet
|
|
|
|
|
discharge holds the gateways to the checkout's actual HEAD whenever it contains the debt. Its
|
|
|
|
|
failure then needs no retarget write, which could itself fail and leave the debt naming a commit
|
|
|
|
|
the checkout never reached (review O4). The run's first move owes its ``target``: its failure
|
|
|
|
|
hands the obligations back whole (``disarm_commit_obligations``).
|
|
|
|
|
"""
|
|
|
|
|
start = _run_start[1][0] if _run_start is not None else ""
|
|
|
|
|
return pre if start and pre != start else target
|
|
|
|
|
|
|
|
|
|
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
def arm_commit_point(git_cmd, root: Path, expected_sha: str, **move) -> str | None:
|
|
|
|
|
"""Arm the obligations, then the tree-move marker (``arm_tree_move(**move)``): no marker, no move.
|
|
|
|
|
|
|
|
|
|
``None`` when both are durable; otherwise why not, with the obligations handed back
|
|
|
|
|
(``disarm_commit_obligations``), and the caller must stop before git writes a file.
|
2026-10-06 22:06:01 -07:00
|
|
|
Linux/macOS gateways are paused first: this is every route's first checkout move.
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
"""
|
2026-10-06 22:06:01 -07:00
|
|
|
from hermes_cli.update_cmd_posix_pause import pause_at_commit_point
|
|
|
|
|
if refused := pause_at_commit_point():
|
|
|
|
|
return refused
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
try:
|
|
|
|
|
arm_commit_obligations(root, expected_sha)
|
|
|
|
|
arm_tree_move(git_cmd, root, **move)
|
|
|
|
|
except OSError as exc:
|
|
|
|
|
disarm_commit_obligations()
|
fix(update): a failed later tree move owes the restart for the HEAD it settled on
What: disarm_commit_obligations, refused off the run's start commit, now re-arms the
obligations for the HEAD the checkout is actually on instead of leaving them on the failed
move's target; owe_restore_of arms them for the move's pre when the tree is torn under its
marker (the commit the next launch's restore lands on). Every failed-move site settles through
these: _pull_updates (any settle, not only one back at the baseline), the CP0 switch (exception,
torn and settled paths, and an attempt-1 arm left behind when attempt 2 is unresolvable), the
syntax rollback's failure path, and the upstream fork sync, whose merge catch now also takes
OSError/SubprocessError (CustodyRefused is an OSError) instead of letting it escape armed.
arm_commit_point no longer says "the checkout was not changed" when an earlier step of the run
(CP0) already moved it. The fork sync refuses a HEAD that is not an object id instead of
arming pre=, and a failed re-arm is logged and printed, not suppressed.
Why: a second move in one run re-armed the host obligation for its own target; when it failed
it settled back to a commit that is not the run start, disarm refused (head != start_head) and
nothing re-armed for the actual HEAD, so the record named a commit the checkout never holds
(undischargeable on a gateway-less host, wrong for the catch-up restart).
Review C1, C11. Tests (RED at 5dfc384669e): CP1 refused after CP0 owes A, not B, and the refusal
names the moved HEAD; a CustodyRefused upstream merge after the pull owes c1.
2026-10-05 01:43:38 -07:00
|
|
|
head = head_and_branch(git_cmd, root)[0] if _run_start is not None else ""
|
|
|
|
|
if head and head != _run_start[1][0]:
|
|
|
|
|
# A later move (CP1 after the CP0 switch): THIS step moved nothing, an earlier one did.
|
|
|
|
|
return (f"could not arm the update ({exc}); this step changed nothing, but an earlier step "
|
|
|
|
|
f"of this update already moved the checkout to {head[:10]}")
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
return f"could not arm the update ({exc}); the checkout was not changed"
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-10-03 13:17:21 -07:00
|
|
|
def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: str | None,
|
|
|
|
|
rollback: str | None = None) -> Path:
|
|
|
|
|
"""Write the interrupted-pull marker for one git tree move (pre -> target).
|
|
|
|
|
|
fix(update): a syntax rollback resets HEAD and the index before any file
The syntax rollback's first step was `reset --soft` (or `update-ref` for a
parked/detached checkout), which leaves the index on the broken commit. A
kill during the following `reset --hard`, after it had written a good file,
left that file staged as the broken version, so the next launch's restore
landed on the pre-update commit with a dirty index (`MM`).
The first step is now a mixed reset (`reset -q <pre>`), after `update-ref
--no-deref HEAD` for the detach mode. HEAD and the index are back on the
pre-update commit before any file is written. The marker's `rollback=` field
is `branch`/`detach`, and recovery redoes the same steps when the kill came
before them. A failed rollback step now goes to the in-process restore every
time. Before this, a failure in the first step dropped the marker, even
after `update-ref` had already moved HEAD.
Also: the old branch-switch hostile cell triggers on `checkout [--no-guess]
main`, since CP0 now passes `--no-guess`. The fork cell makes origin read as
a fork through the git shim, because the sandbox otherwise reports the
official URL and the upstream sync never runs.
(cherry picked from commit 23f9658868b9c1f639ed972e59251fe154372929)
(cherry picked from commit 5fd6de2e62152076d6703e5ff70250ec1ece71b9)
2026-10-03 13:49:00 -07:00
|
|
|
``rollback`` (``branch``/``detach``): a syntax rollback that moves HEAD and the index back to
|
|
|
|
|
``pre`` before any file; a kill before that step finds HEAD still on ``target`` and the restore
|
2026-10-05 00:53:20 -07:00
|
|
|
redoes it first, only while HEAD still names the ``ref`` recorded here (empty: detached).
|
2026-10-03 13:17:21 -07:00
|
|
|
"""
|
2026-10-03 10:06:12 -07:00
|
|
|
marker = interrupted_pull_marker(root)
|
2026-10-05 00:53:20 -07:00
|
|
|
ref = ""
|
|
|
|
|
if rollback:
|
|
|
|
|
named = run_git(git_cmd, ["symbolic-ref", "-q", "HEAD"], cwd=str(root), capture_output=True, text=True,
|
|
|
|
|
encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, timeout=60)
|
|
|
|
|
ref = named.stdout.strip() if named.returncode == 0 else ""
|
2026-10-04 13:18:18 -07:00
|
|
|
if pre:
|
|
|
|
|
with suppress(OSError, subprocess.SubprocessError): # no closure: the in-tree repair still runs
|
|
|
|
|
publish_recovery_closure(git_cmd, root, pre)
|
2026-10-05 01:48:38 -07:00
|
|
|
# The restore's only record, durable before git writes: temp + fsync + rename, never in place.
|
2026-10-05 02:49:51 -07:00
|
|
|
# ``index_lock``: the lock generation already there before this move's git ran (empty: none). Only
|
|
|
|
|
# THAT one is another git's for sure; a lock that appears later can be our own killed git's.
|
2026-10-05 01:48:38 -07:00
|
|
|
write_durable_text(marker, f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
|
2026-10-05 02:49:51 -07:00
|
|
|
+ f"index_lock={_lock_identity(marker.parent / 'index.lock')}\n"
|
2026-10-05 01:48:38 -07:00
|
|
|
+ (f"rollback={rollback}\nref={ref}\n" if rollback else "")
|
|
|
|
|
+ (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""))
|
2026-10-03 10:06:12 -07:00
|
|
|
return marker
|
|
|
|
|
|
|
|
|
|
|
2026-10-04 13:18:18 -07:00
|
|
|
def publish_recovery_closure(git_cmd, root: Path, pre: str) -> Path:
|
|
|
|
|
"""Copy the launch repair's modules, as committed at ``pre``, beside the marker (outside the tree).
|
|
|
|
|
|
|
|
|
|
The bytes come from the commit the repair restores, never from the working tree: a rollback move
|
|
|
|
|
starts from a broken tree, and a second move in one run starts from code this process never ran.
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
A manifest names each file's blob id; the launcher runs the closure only when every file hashes
|
|
|
|
|
to it (``recovery_closure_verified``) and rebuilds it from git's objects otherwise. Files and
|
|
|
|
|
directory are fsynced before the atomic rename, so a power loss during the move that follows
|
|
|
|
|
finds the whole closure or none. An older run's closure (another ``pre``) is dropped.
|
2026-10-04 13:18:18 -07:00
|
|
|
"""
|
|
|
|
|
import shutil
|
|
|
|
|
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
if not is_object_id(pre):
|
|
|
|
|
raise OSError(f"not a commit id: {pre!r}")
|
2026-10-04 13:18:18 -07:00
|
|
|
dest = recovery_closure_dir(Path(root), pre)
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
if recovery_closure_verified(dest, pre):
|
2026-10-04 13:18:18 -07:00
|
|
|
return dest
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
git_cmd = [*([git_cmd] if isinstance(git_cmd, str) else git_cmd), "--no-replace-objects"]
|
|
|
|
|
listed = run_git(git_cmd, ["ls-tree", "-z", "--full-tree", pre, "--", *RECOVERY_CLOSURE], cwd=str(root),
|
|
|
|
|
capture_output=True, stdin=subprocess.DEVNULL, timeout=120)
|
|
|
|
|
ids = {}
|
|
|
|
|
for entry in (listed.stdout or b"").split(b"\0") if listed.returncode == 0 else ():
|
|
|
|
|
meta, _, rel = entry.decode("utf-8", "replace").partition("\t")
|
|
|
|
|
if meta.split()[1:2] == ["blob"]:
|
|
|
|
|
ids[rel] = meta.split()[2]
|
|
|
|
|
blobs = {RECOVERY_CLOSURE_INIT: b""}
|
2026-10-05 23:37:20 -07:00
|
|
|
shown = read_target_files(git_cmd, root, pre, RECOVERY_CLOSURE) # one spawn, not a cat-file per file
|
2026-10-04 13:18:18 -07:00
|
|
|
for rel in RECOVERY_CLOSURE:
|
2026-10-05 23:37:20 -07:00
|
|
|
if rel not in ids or shown[rel] is None or blob_id(shown[rel], pre) != ids[rel]:
|
2026-10-04 13:18:18 -07:00
|
|
|
raise OSError(f"{rel} is not in {pre[:10]}")
|
2026-10-05 23:37:20 -07:00
|
|
|
blobs[rel] = shown[rel]
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
manifest = "".join(f"{blob_id(data, pre)} {rel}\n" for rel, data in blobs.items()).encode("utf-8")
|
2026-10-04 13:18:18 -07:00
|
|
|
staging = dest.with_name(f".{pre}.{os.getpid()}.staging")
|
|
|
|
|
shutil.rmtree(staging, ignore_errors=True)
|
|
|
|
|
try:
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
for rel, data in {**blobs, RECOVERY_CLOSURE_MANIFEST: manifest}.items():
|
2026-10-04 13:18:18 -07:00
|
|
|
(staging / rel).parent.mkdir(parents=True, exist_ok=True)
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
with open(staging / rel, "wb") as handle:
|
|
|
|
|
handle.write(data)
|
|
|
|
|
handle.flush()
|
|
|
|
|
os.fsync(handle.fileno())
|
|
|
|
|
for directory in (staging / "hermes_cli", staging):
|
|
|
|
|
_fsync_dir(directory)
|
2026-10-04 13:18:18 -07:00
|
|
|
shutil.rmtree(dest, ignore_errors=True)
|
|
|
|
|
os.replace(staging, dest)
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
_fsync_dir(dest.parent)
|
2026-10-04 13:18:18 -07:00
|
|
|
finally:
|
|
|
|
|
shutil.rmtree(staging, ignore_errors=True)
|
|
|
|
|
for stale in dest.parent.iterdir():
|
|
|
|
|
if stale != dest:
|
|
|
|
|
shutil.rmtree(stale, ignore_errors=True)
|
|
|
|
|
return dest
|
|
|
|
|
|
|
|
|
|
|
fix(update): the launch repair runs a published closure only when it hashes to pre's blobs (M1, m3, m4)
A published recovery closure was trusted on presence alone: a power loss after the rename
could leave a torn _early_recovery.py that failed every launch forever, and foreign bytes
ran in place of pre's code.
- publish_recovery_closure writes a MANIFEST of blob ids (ls-tree at pre, each cat-file
output re-hashed against its id), fsyncs files and directories before the rename,
re-verifies an existing closure instead of trusting it, and refuses a pre that is not a
full commit id (no HEAD, no path under the git dir).
- The minted launcher hashes every file against the manifest before importing; a missing,
torn or foreign closure is discarded and rebuilt from git's objects (--no-replace-objects)
and verified again.
- Its git is the recorded absolute one or an absolute PATH entry, never a bare 'git' that
CreateProcess (or an empty POSIX PATH entry) resolves from the current directory (m4).
- A repair refused by a live writer exits 1 with the repair's own reason: no "repairing it"
banner, no import traceback (m3).
(cherry picked from commit 94e436325b40b5b99962c3c646d3760ecad9dc4f)
2026-10-04 15:32:58 -07:00
|
|
|
def _fsync_dir(directory: Path) -> None:
|
|
|
|
|
"""POSIX: make a directory's entries durable. Windows opens no directory handle; NTFS journals
|
|
|
|
|
the rename itself."""
|
|
|
|
|
if os.name == "nt":
|
|
|
|
|
return
|
|
|
|
|
fd = os.open(directory, os.O_RDONLY)
|
|
|
|
|
try:
|
|
|
|
|
os.fsync(fd)
|
|
|
|
|
finally:
|
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
|
|
|
2026-10-04 04:58:59 -07:00
|
|
|
def _absolute_git(git_cmd) -> str:
|
|
|
|
|
"""The git this run moves the tree with, as an absolute path for the next launch's repair.
|
|
|
|
|
|
|
|
|
|
A Windows install's git is often PM's store copy, on PATH only inside the updater
|
|
|
|
|
(``expose_pm_git``); the repair cannot look it up through ``pm`` when the killed move tore a
|
|
|
|
|
module ``pm`` imports (``hermes_constants``)."""
|
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
|
head = (list(git_cmd) or ["git"])[0] if not isinstance(git_cmd, str) else git_cmd
|
|
|
|
|
found = shutil.which(head)
|
|
|
|
|
return os.path.abspath(found) if found else ""
|
|
|
|
|
|
|
|
|
|
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
def files_added_by(git_cmd, root: Path, pre: str, target: str | None) -> list[str]:
|
|
|
|
|
"""Paths ``target`` adds over ``pre``. A rollback's mixed reset un-tracks them and ``reset --hard``
|
|
|
|
|
never touches untracked files, so ``drop_added_files`` removes them by name afterwards."""
|
|
|
|
|
if not target or target == pre:
|
|
|
|
|
return []
|
2026-10-04 02:28:55 -07:00
|
|
|
cp = run_git(git_cmd, ["diff", "--name-only", "-z", "--no-renames", "--diff-filter=A", pre, target],
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace",
|
|
|
|
|
stdin=subprocess.DEVNULL, timeout=120)
|
|
|
|
|
return [p for p in cp.stdout.split("\0") if p] if cp.returncode == 0 else []
|
|
|
|
|
|
|
|
|
|
|
fix(update): a killed syntax rollback keeps its recovery record until the tree is whole
R1. The launch-time replay of a syntax rollback ran `git reset -q pre` before any
dead-lock reclaim and ignored its result; HEAD still on the broken release then
read as "git finished" and the marker was deleted, leaving the broken tree and a
stale index.lock with nothing left to restore it. In-process, the same path made
settle_failed_tree_move report the rollback complete.
Now: the killed step's index.lock is reclaimed first and only once its git is
proven gone (/proc fds + live git with cwd in the checkout on Linux, lsof on
macOS/BSD, the unlink itself on Windows; unknowable = keep); update-ref, reset
and the HEAD readback are each checked; a rollback marker is removed only when
HEAD == pre, no index.lock and no tracked change. The in-process rollback checks
drop_added_files and the same whole-tree oracle before removing the marker, and
says the next launch finishes it when it could not.
Was (reviewer probe, real git SIGKILLed at index.lock): restore returned False,
HEAD broken, lock left, marker gone. Now: restore returns True, HEAD pre, clean,
lock and marker gone; a live lock holder keeps the marker and the lock.
(cherry picked from commit 087ae72d38be89892a70e97a935db532a9e8284f)
(cherry picked from commit 5984c98726cb29b1123552f875f99d5020f01b71)
2026-10-03 22:16:29 -07:00
|
|
|
def drop_added_files(root: Path, added: list[str]) -> bool:
|
|
|
|
|
"""Every one is the move's own file: git refuses to move a checkout over an untracked file.
|
|
|
|
|
|
|
|
|
|
False when one of them is still there (the caller keeps the marker; the restore retries)."""
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
root = Path(root)
|
fix(update): a killed syntax rollback keeps its recovery record until the tree is whole
R1. The launch-time replay of a syntax rollback ran `git reset -q pre` before any
dead-lock reclaim and ignored its result; HEAD still on the broken release then
read as "git finished" and the marker was deleted, leaving the broken tree and a
stale index.lock with nothing left to restore it. In-process, the same path made
settle_failed_tree_move report the rollback complete.
Now: the killed step's index.lock is reclaimed first and only once its git is
proven gone (/proc fds + live git with cwd in the checkout on Linux, lsof on
macOS/BSD, the unlink itself on Windows; unknowable = keep); update-ref, reset
and the HEAD readback are each checked; a rollback marker is removed only when
HEAD == pre, no index.lock and no tracked change. The in-process rollback checks
drop_added_files and the same whole-tree oracle before removing the marker, and
says the next launch finishes it when it could not.
Was (reviewer probe, real git SIGKILLed at index.lock): restore returned False,
HEAD broken, lock left, marker gone. Now: restore returns True, HEAD pre, clean,
lock and marker gone; a live lock holder keeps the marker and the lock.
(cherry picked from commit 087ae72d38be89892a70e97a935db532a9e8284f)
(cherry picked from commit 5984c98726cb29b1123552f875f99d5020f01b71)
2026-10-03 22:16:29 -07:00
|
|
|
gone = True
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
for rel in added:
|
fix(update): a killed syntax rollback keeps its recovery record until the tree is whole
R1. The launch-time replay of a syntax rollback ran `git reset -q pre` before any
dead-lock reclaim and ignored its result; HEAD still on the broken release then
read as "git finished" and the marker was deleted, leaving the broken tree and a
stale index.lock with nothing left to restore it. In-process, the same path made
settle_failed_tree_move report the rollback complete.
Now: the killed step's index.lock is reclaimed first and only once its git is
proven gone (/proc fds + live git with cwd in the checkout on Linux, lsof on
macOS/BSD, the unlink itself on Windows; unknowable = keep); update-ref, reset
and the HEAD readback are each checked; a rollback marker is removed only when
HEAD == pre, no index.lock and no tracked change. The in-process rollback checks
drop_added_files and the same whole-tree oracle before removing the marker, and
says the next launch finishes it when it could not.
Was (reviewer probe, real git SIGKILLed at index.lock): restore returned False,
HEAD broken, lock left, marker gone. Now: restore returns True, HEAD pre, clean,
lock and marker gone; a live lock holder keeps the marker and the lock.
(cherry picked from commit 087ae72d38be89892a70e97a935db532a9e8284f)
(cherry picked from commit 5984c98726cb29b1123552f875f99d5020f01b71)
2026-10-03 22:16:29 -07:00
|
|
|
try:
|
|
|
|
|
(root / rel).unlink(missing_ok=True)
|
|
|
|
|
except OSError:
|
|
|
|
|
gone = False
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
for parent in sorted({p for rel in added for p in Path(rel).parents if str(p) != "."},
|
|
|
|
|
key=lambda p: len(p.parts), reverse=True):
|
|
|
|
|
try:
|
|
|
|
|
(root / parent).rmdir() # only when empty: anything else inside keeps it
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
fix(update): a killed syntax rollback keeps its recovery record until the tree is whole
R1. The launch-time replay of a syntax rollback ran `git reset -q pre` before any
dead-lock reclaim and ignored its result; HEAD still on the broken release then
read as "git finished" and the marker was deleted, leaving the broken tree and a
stale index.lock with nothing left to restore it. In-process, the same path made
settle_failed_tree_move report the rollback complete.
Now: the killed step's index.lock is reclaimed first and only once its git is
proven gone (/proc fds + live git with cwd in the checkout on Linux, lsof on
macOS/BSD, the unlink itself on Windows; unknowable = keep); update-ref, reset
and the HEAD readback are each checked; a rollback marker is removed only when
HEAD == pre, no index.lock and no tracked change. The in-process rollback checks
drop_added_files and the same whole-tree oracle before removing the marker, and
says the next launch finishes it when it could not.
Was (reviewer probe, real git SIGKILLed at index.lock): restore returned False,
HEAD broken, lock left, marker gone. Now: restore returns True, HEAD pre, clean,
lock and marker gone; a live lock holder keeps the marker and the lock.
(cherry picked from commit 087ae72d38be89892a70e97a935db532a9e8284f)
(cherry picked from commit 5984c98726cb29b1123552f875f99d5020f01b71)
2026-10-03 22:16:29 -07:00
|
|
|
return gone and not any(os.path.lexists(root / rel) for rel in added)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tree_whole_at(git_cmd, root: Path, sha: str) -> bool:
|
|
|
|
|
"""HEAD is ``sha``, no ``index.lock`` is left and no tracked file differs from it."""
|
|
|
|
|
def run(*args: str) -> subprocess.CompletedProcess:
|
2026-10-04 02:28:55 -07:00
|
|
|
return run_git(git_cmd, [*args], cwd=str(root), capture_output=True, text=True, encoding="utf-8",
|
fix(update): a killed syntax rollback keeps its recovery record until the tree is whole
R1. The launch-time replay of a syntax rollback ran `git reset -q pre` before any
dead-lock reclaim and ignored its result; HEAD still on the broken release then
read as "git finished" and the marker was deleted, leaving the broken tree and a
stale index.lock with nothing left to restore it. In-process, the same path made
settle_failed_tree_move report the rollback complete.
Now: the killed step's index.lock is reclaimed first and only once its git is
proven gone (/proc fds + live git with cwd in the checkout on Linux, lsof on
macOS/BSD, the unlink itself on Windows; unknowable = keep); update-ref, reset
and the HEAD readback are each checked; a rollback marker is removed only when
HEAD == pre, no index.lock and no tracked change. The in-process rollback checks
drop_added_files and the same whole-tree oracle before removing the marker, and
says the next launch finishes it when it could not.
Was (reviewer probe, real git SIGKILLed at index.lock): restore returned False,
HEAD broken, lock left, marker gone. Now: restore returns True, HEAD pre, clean,
lock and marker gone; a live lock holder keeps the marker and the lock.
(cherry picked from commit 087ae72d38be89892a70e97a935db532a9e8284f)
(cherry picked from commit 5984c98726cb29b1123552f875f99d5020f01b71)
2026-10-03 22:16:29 -07:00
|
|
|
errors="replace", stdin=subprocess.DEVNULL, timeout=120)
|
|
|
|
|
|
|
|
|
|
head = run("rev-parse", "-q", "--verify", "HEAD")
|
|
|
|
|
if head.returncode != 0 or head.stdout.strip() != sha or (interrupted_pull_marker(Path(root)).parent / "index.lock").exists():
|
|
|
|
|
return False
|
|
|
|
|
status = run("status", "--porcelain", "-z", "--untracked-files=no")
|
|
|
|
|
return status.returncode == 0 and not status.stdout.strip("\0")
|
fix(update): a historical updater's hand-off no longer dies on a missing ruamel
The takeover's new fleet-obligation arm imported `update_cmd_fleet`'s writer,
which imports `update_cmd` -> `hermes_cli.config` -> ruamel. `prepare()` runs
in the HISTORICAL interpreter before PM syncs the new dependencies, so every
update from a release whose venv has no ruamel (the installer-script and
e2e-upgrade core/handoff/pm legs) exited 1 with
`Update preparation failed: No module named 'ruamel'`. The arm now writes the
host obligation through the stdlib-only `update_host_obligation`, with the same
per-home breadcrumb fallback.
Also:
- The syntax rollback's mixed reset un-tracked the files the broken commit
ADDED, and `reset --hard` never touches untracked files, so they stayed in
the tree (`?? hermes_cli/`). They are now removed by name after the reset.
- A git that EXITED on another git's `index.lock` no longer has that lock
deleted by the in-process settle: the lock is foreign by construction, the
scan only reads, and a tree with writes to put back keeps its marker.
- `_early_recovery`'s git runs carry `cwd` as well as `-C`.
- `_early_recovery._git_executable` gets a reasoned resolution-allowlist row
(stdlib-only launch repair; cannot import `hermes_platform`).
Tests rewritten to the new contract:
- test_source_release_channels zip fallback: the download stub takes the
target SHA the ZIP swap now journals, and asserts it is passed through.
- test_update_target_identity: the fault injection keys the upstream sync by
its remote (`upstream`/`upstream/main`), since the sync now merges the
fetched ref instead of a second `pull`; the origin-target syntax case
expects the pre-move preflight refusal.
- test_hostile_commit ZIP cell kills right after a tracked file landed, not
at the N-th swap (listing order made it land on unchanged entries only).
(cherry picked from commit b6adf96ca6988262fe7c408ea652d3b390c7be8d)
(cherry picked from commit 028cb933271626d21003c255bb397deb8998b7da)
2026-10-03 14:37:38 -07:00
|
|
|
|
|
|
|
|
|
2026-10-03 10:06:12 -07:00
|
|
|
def settle_failed_tree_move(root: Path) -> bool:
|
|
|
|
|
"""Git exited without finishing a tree move: put back what it wrote, keep the marker if we can't.
|
|
|
|
|
|
|
|
|
|
True when the tree is verified whole again (at the pre-move commit, or at the target when git got
|
|
|
|
|
there anyway); False leaves the marker for the next launch's ``restore_interrupted_pull``.
|
|
|
|
|
"""
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
marker = interrupted_pull_marker(Path(root))
|
|
|
|
|
if not marker.is_file():
|
|
|
|
|
return False # no marker names the move: its absence proves nothing about the tree
|
2026-10-03 10:06:12 -07:00
|
|
|
restore_interrupted_pull(Path(root), after_failure=True)
|
fix(update): no tree move starts without its recovery marker and a resolved target (review F17, F22, F23, F24, F25, F29)
What: arm_commit_point arms the obligations then the interrupted-pull marker as one hard
precondition (failure hands the obligations back and the caller stops before git): the origin
pull exits, CP0 returns a failed result (the caller restores the autostash), the upstream ff
skips. The pull resolves its target with a checked rev-parse --verify and refuses a non-OID.
The pull's refusals run inside the try whose finally names the autostash. The syntax rollback
(cannot refuse: the tree is broken) rolls back unmarked with a warning, and
settle_failed_tree_move no longer counts a missing marker as proof the tree is whole, so only a
tree verified at pre reports "Rollback complete".
Why: _best_effort/suppress(OSError) let git move with no marker (a kill left a torn tree no
launch could identify), an unresolved target armed 'target=' and still merged, and an arm
failure after the autostash exited with no stash guidance.
Red (91270e84a26 update_cmd.py/update_cmd_git.py/update_cmd_commit.py): IsADirectoryError escapes after the unmarked move (pull, CP0, upstream, rollback); unresolved target not refused; stdout '' (no stash ref).
Green: SystemExit/failed result/False with HEAD and tree unchanged; stash ref printed; rollback prints the manual recipe.
2026-10-04 23:30:06 -07:00
|
|
|
return not marker.is_file()
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
def _requires_python_spec(pyproject: bytes | str | None) -> str | None:
|
|
|
|
|
"""The target's ``requires-python`` specifier, None when absent or unreadable."""
|
|
|
|
|
if not pyproject:
|
|
|
|
|
return None
|
|
|
|
|
import tomllib
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
text = pyproject.decode("utf-8") if isinstance(pyproject, bytes) else pyproject
|
|
|
|
|
spec = tomllib.loads(text)["project"]["requires-python"]
|
|
|
|
|
except (KeyError, TypeError, ValueError): # TOMLDecodeError is a ValueError
|
|
|
|
|
return None
|
|
|
|
|
return spec if isinstance(spec, str) else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _spec_admits(spec: str, version: str) -> bool:
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
|
|
|
|
except ImportError: # the lower bound is the part a Python bump moves
|
|
|
|
|
floor = re.search(r">=\s*(\d+)\.(\d+)", spec)
|
|
|
|
|
found = re.match(r"(\d+)\.(\d+)", version)
|
|
|
|
|
return not floor or bool(found) and (int(found[1]), int(found[2])) >= (int(floor[1]), int(floor[2]))
|
|
|
|
|
try:
|
|
|
|
|
return SpecifierSet(spec).contains(version, prereleases=True)
|
|
|
|
|
except InvalidSpecifier:
|
|
|
|
|
return True # unknown: keep judging with this interpreter, never wave a file through
|
|
|
|
|
|
|
|
|
|
|
2026-10-03 13:17:21 -07:00
|
|
|
def requires_other_python(pyproject: bytes | str | None) -> bool:
|
|
|
|
|
"""True when a target's ``requires-python`` excludes the running interpreter.
|
|
|
|
|
|
|
|
|
|
Its startup modules may then use a newer Python's syntax that this interpreter's ``compile()``
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
cannot judge: ``startup_syntax_error`` asks an installed interpreter the target admits instead.
|
|
|
|
|
Unknown -> False: keep checking, so a real syntax error is never waved through on a guess.
|
2026-10-03 13:17:21 -07:00
|
|
|
"""
|
|
|
|
|
import platform
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
|
|
|
|
|
spec = _requires_python_spec(pyproject)
|
|
|
|
|
return spec is not None and not _spec_admits(spec, platform.python_version())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_PYTHON_VERSION = "import platform; print(platform.python_version())"
|
|
|
|
|
# Run by the target's interpreter: compile every startup file sent on stdin, print the first error.
|
|
|
|
|
_COMPILE_ALL = (
|
|
|
|
|
"import base64, json, sys\n"
|
|
|
|
|
"verdict = None\n"
|
|
|
|
|
"for rel, data in json.load(sys.stdin).items():\n"
|
|
|
|
|
" try:\n"
|
|
|
|
|
" compile(base64.b64decode(data), rel, 'exec', dont_inherit=True)\n"
|
|
|
|
|
" except (SyntaxError, ValueError) as exc:\n"
|
|
|
|
|
" verdict = [rel, f'{type(exc).__name__}: {exc}']\n"
|
|
|
|
|
" break\n"
|
|
|
|
|
"print(json.dumps(verdict))\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _run_python(argv: list[str], *, stdin: str = "", timeout: float = 60) -> str | None:
|
|
|
|
|
"""Stdout of a read-only interpreter child (it never writes the checkout), None when it failed."""
|
|
|
|
|
from hermes_cli._subprocess_compat import windows_hide_flags
|
2026-10-03 13:17:21 -07:00
|
|
|
|
|
|
|
|
try:
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
done = subprocess.run(argv, input=stdin, capture_output=True, text=True, encoding="utf-8",
|
2026-10-09 01:25:59 +02:00
|
|
|
errors="replace", timeout=timeout, creationflags=windows_hide_flags(), check=False)
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
except (OSError, subprocess.SubprocessError):
|
|
|
|
|
return None
|
|
|
|
|
return done.stdout if done.returncode == 0 else None
|
2026-10-03 13:17:21 -07:00
|
|
|
|
|
|
|
|
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
def _admitted_python(spec: str) -> str | None:
|
2026-10-05 07:22:26 -07:00
|
|
|
"""An installed ``python3.N`` the target's ``requires-python`` admits (newest first), or None
|
|
|
|
|
when this machine has none. No uv tier: Hermes never resolves the user's uv
|
|
|
|
|
(tests/test_managed_runtime_resolution.py), and a missing interpreter only means this one judges."""
|
|
|
|
|
from hermes_platform.resolver import locate_command
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
|
2026-10-05 07:22:26 -07:00
|
|
|
candidates = [res.command[0] for minor in range(40, sys.version_info[1], -1)
|
|
|
|
|
if (res := locate_command(f"python3.{minor}")).command][:3]
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
for python in candidates:
|
|
|
|
|
version = (_run_python([python, "-I", "-c", _PYTHON_VERSION], timeout=30) or "").strip()
|
|
|
|
|
if version and _spec_admits(spec, version):
|
|
|
|
|
return python
|
2026-10-05 00:53:20 -07:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
def _compile_here(sources: dict[str, bytes]) -> tuple[str, str] | None:
|
|
|
|
|
for rel, data in sources.items():
|
|
|
|
|
try:
|
|
|
|
|
compile(data, rel, "exec", dont_inherit=True)
|
|
|
|
|
except (SyntaxError, ValueError) as exc:
|
|
|
|
|
return rel, f"{type(exc).__name__}: {exc}"
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def startup_syntax_error(sources: dict[str, bytes | None], pyproject: bytes | None) -> tuple[str, str] | None:
|
|
|
|
|
"""``(path, error)`` for the first startup file that does not compile, else None.
|
|
|
|
|
|
|
|
|
|
Judged by the interpreter that will run the target: this one when the target's
|
|
|
|
|
``requires-python`` admits it, else an installed interpreter it admits (review N15: a
|
|
|
|
|
conflict-marker scan is not a syntax check). With none installed, this interpreter still
|
|
|
|
|
judges: code it parses is code a newer Python parses, and a refusal names the Python to install
|
|
|
|
|
so the release can be judged by its own interpreter. ``None`` values (absent files) are skipped.
|
|
|
|
|
"""
|
|
|
|
|
import base64
|
|
|
|
|
import json
|
|
|
|
|
import platform
|
|
|
|
|
|
|
|
|
|
present = {rel: data for rel, data in sources.items() if data is not None}
|
|
|
|
|
spec = _requires_python_spec(pyproject)
|
|
|
|
|
if spec is None or _spec_admits(spec, platform.python_version()):
|
|
|
|
|
return _compile_here(present)
|
|
|
|
|
if python := _admitted_python(spec):
|
|
|
|
|
payload = json.dumps({rel: base64.b64encode(data).decode("ascii") for rel, data in present.items()})
|
|
|
|
|
out = _run_python([python, "-I", "-c", _COMPILE_ALL], stdin=payload, timeout=120)
|
|
|
|
|
try:
|
|
|
|
|
verdict = json.loads((out or "").strip().splitlines()[-1])
|
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
|
verdict = False # the target's interpreter gave no verdict: judge here (below)
|
|
|
|
|
if verdict is None:
|
|
|
|
|
return None
|
|
|
|
|
if isinstance(verdict, list) and len(verdict) == 2:
|
|
|
|
|
return str(verdict[0]), f"{verdict[1]} (judged by {python})"
|
|
|
|
|
broken = _compile_here(present)
|
|
|
|
|
if broken is None:
|
|
|
|
|
return None
|
|
|
|
|
return broken[0], (f"{broken[1]}\nJudged by Python {platform.python_version()}: the target requires Python "
|
|
|
|
|
f"{spec} and no installed interpreter satisfies it. Install one (`uv python install "
|
|
|
|
|
f"'{spec}'`) and re-run `hermes update` so the release is judged by its own Python.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_target_files(git_cmd, root: Path, target_ref: str, relpaths) -> dict[str, bytes | None]:
|
|
|
|
|
"""``{rel: bytes}`` of each path as committed at ``target_ref`` (None: absent there or unreadable),
|
|
|
|
|
in ONE ``git cat-file --batch`` instead of one ``git show`` per file."""
|
|
|
|
|
names = list(dict.fromkeys(relpaths))
|
|
|
|
|
found: dict[str, bytes | None] = dict.fromkeys(names)
|
|
|
|
|
request = "".join(f"{target_ref}:{rel}\n" for rel in names).encode("utf-8")
|
2026-10-09 05:35:25 -07:00
|
|
|
cp = run_git(git_cmd, ["cat-file", "--batch"], cwd=str(root), input=request, capture_output=True, timeout=120)
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
out, pos = (cp.stdout or b"") if cp.returncode == 0 else b"", 0
|
|
|
|
|
for rel in names:
|
|
|
|
|
end = out.find(b"\n", pos)
|
|
|
|
|
if end < 0:
|
|
|
|
|
break
|
|
|
|
|
header, pos = out[pos:end].split(), end + 1
|
|
|
|
|
if len(header) == 3 and header[2].isdigit(): # "<oid> <type> <size>", then the bytes and "\n"
|
|
|
|
|
size = int(header[2])
|
|
|
|
|
if header[1] == b"blob":
|
|
|
|
|
found[rel] = out[pos:pos + size]
|
|
|
|
|
pos += size + 1
|
|
|
|
|
return found
|
|
|
|
|
|
|
|
|
|
|
2026-10-03 10:06:12 -07:00
|
|
|
def target_syntax_error(git_cmd, root: Path, target_ref: str, relpaths) -> tuple[str, str] | None:
|
|
|
|
|
"""``(path, error)`` for the first startup-critical file that does not compile at ``target_ref``.
|
|
|
|
|
|
|
|
|
|
Read from the object store, never written to the tree: this runs BEFORE HEAD moves, so a broken
|
|
|
|
|
release is refused with the install untouched (the post-pull rollback stays as the backstop).
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
``startup_syntax_error`` judges it under the interpreter the target admits. A file absent at the
|
|
|
|
|
target (or unreadable) is skipped: the post-pull guard has the last word.
|
2026-10-03 10:06:12 -07:00
|
|
|
"""
|
2026-10-09 05:35:25 -07:00
|
|
|
try:
|
|
|
|
|
files = read_target_files(git_cmd, root, target_ref, ["pyproject.toml", *relpaths])
|
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
|
# A blobless install (install.sh clones --filter=blob:none) lazily fetches each changed blob
|
|
|
|
|
# in its own round trip; on a slow link that outlasts the limit. Unread, like an absent file.
|
|
|
|
|
print(" ⚠ Syntax preflight skipped (slow object fetch); the post-update check still runs.")
|
|
|
|
|
return None
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
pyproject = files.pop("pyproject.toml", None)
|
|
|
|
|
return startup_syntax_error(files, pyproject)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tree_syntax_error(root: Path, relpaths) -> tuple[str, str] | None:
|
|
|
|
|
"""``startup_syntax_error`` for the files in the worktree at ``root`` (the post-move backstop)."""
|
|
|
|
|
root = Path(root)
|
|
|
|
|
sources: dict[str, bytes | None] = {}
|
2026-10-03 10:06:12 -07:00
|
|
|
for rel in relpaths:
|
|
|
|
|
try:
|
fix(update): startup syntax is judged by the target's Python over the whole entry closure (review G3, N15)
What: the critical inventory now holds every module the launcher and hermes_bootstrap import
before application code (hermes_bootstrap.py, _parser, _launchers, the recovery closure,
update_handoff, venv_sync, pm/environments), pinned by a test that derives that set from the
real launcher text and bootstrap imports. One startup_syntax_error() serves the preflight, the
post-pull guard and the ZIP gate: a target whose requires-python excludes this interpreter is
compiled by an installed interpreter it admits (uv python find, then python3.N on PATH); with
none installed this interpreter still compiles it, and a refusal names the Python to install.
The preflight reads pyproject + the inventory in one `git cat-file --batch`.
Why: a broken bootstrap passed both guards and failed at the first import (G3); a Python bump
reduced the check to a conflict-marker scan, so `def broken(:` landed (N15, the class the
00f0e40b6ce marker scan left open); preflight cost one git spawn per file (kshitijk4poor).
Changed test: test_syntax_guards_skip_a_target_that_requires_a_newer_python pinned the N15 bug
(`def newer_syntax(:` admitted under >=3.99); it is replaced by the no-interpreter refusal and
the admitted-interpreter verdict tests.
ZIP gate (update_cmd_zip.py, czip's file): its compile loop is replaced by the shared helper.
2026-10-05 06:05:22 -07:00
|
|
|
sources[rel] = (root / rel).read_bytes() if (root / rel).is_file() else None
|
|
|
|
|
except OSError as exc:
|
|
|
|
|
return str(rel), f"could not read: {exc}"
|
|
|
|
|
pyproject = root / "pyproject.toml"
|
|
|
|
|
return startup_syntax_error(sources, pyproject.read_bytes() if pyproject.is_file() else None)
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def head_and_branch(git_cmd, root: Path) -> tuple[str, str]:
|
|
|
|
|
def out(*args: str) -> str:
|
2026-10-04 02:28:55 -07:00
|
|
|
cp = run_git(git_cmd, [*args], cwd=str(root), capture_output=True, text=True, encoding="utf-8", errors="replace",
|
2026-10-03 10:06:12 -07:00
|
|
|
stdin=subprocess.DEVNULL, timeout=60)
|
|
|
|
|
return cp.stdout.strip() if cp.returncode == 0 else ""
|
|
|
|
|
|
|
|
|
|
return out("rev-parse", "HEAD"), out("rev-parse", "--abbrev-ref", "HEAD")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkout_untouched(git_cmd, root: Path, start: tuple[str, str] | None) -> bool:
|
|
|
|
|
"""True when the checkout is exactly where this run found it: same HEAD, same branch, no
|
|
|
|
|
autostash taken, no tree move armed. Only then may a failed git update fall back to ZIP."""
|
|
|
|
|
from hermes_cli.update_cmd_stash import _unrestored_autostash_notice
|
|
|
|
|
|
|
|
|
|
if start is None or not start[0]:
|
|
|
|
|
return False
|
|
|
|
|
if _unrestored_autostash_notice() is not None or interrupted_pull_marker(root).is_file():
|
|
|
|
|
return False
|
|
|
|
|
return head_and_branch(git_cmd, root) == start
|
|
|
|
|
|
|
|
|
|
|
fix(update): a retarget or a later update keeps the host restart debt that stood before it
The host restart record's `found` baseline is what an owner's release puts back. A second arm by
the same owner (CP0 branch, then the pull, then the upstream ff) saw a non-empty `owners` list,
inferred "nothing was here" and wrote `found: null` on the different-SHA path; the disarm after a
rollback to the start commit then unlinked the restart debt that predated the run. A new owner
arming another SHA over another run's record did the same to that run's debt.
`_baseline_for` keeps one rule: a new owner saves the bytes there now (other owners' stakes
included); an owner already in the record carries the baseline it saved, or, when it shared the
record, the shared record minus its own stake. So release always restores exactly the state before
this owner's first stake (review R1).
The snapshot, owner and run start were process-global and set only once, so a second
`hermes update` in one interpreter reused the first update's snapshot and owner and handed back
the first update's committed tail and restart as its own undo when its move was refused.
`begin_update_attempt()` starts each attempt with no state; `_cmd_update_impl` calls it first
(one additive line in update_cmd.py) (review O5).
Tests: same-run B->C retarget with a real git rollback check, a later owner's failed retarget, a
shared record left for another target, and a second in-process update entered through
`_cmd_update_impl`; all four fail on fd5c37f17bf.
2026-10-05 06:01:49 -07:00
|
|
|
def begin_update_attempt() -> None:
|
|
|
|
|
"""A new ``hermes update`` attempt owns nothing yet: no snapshot, no stake, no run start.
|
|
|
|
|
|
|
|
|
|
The state above is per attempt, not per process: a second update in one interpreter (a
|
|
|
|
|
long-lived caller) that inherited the first one's snapshot and owner would hand back the
|
|
|
|
|
first update's committed debt as its own undo when its move is refused (review O5).
|
|
|
|
|
"""
|
|
|
|
|
global _armed_snapshot, _run_start, _obligation_root, _owner
|
|
|
|
|
_owner = ""
|
|
|
|
|
_armed_snapshot = None
|
|
|
|
|
_armed_bytes.clear()
|
|
|
|
|
_run_start = None
|
|
|
|
|
_obligation_root = None
|
|
|
|
|
|
|
|
|
|
|
2026-10-03 10:06:12 -07:00
|
|
|
def record_run_start(git_cmd, root: Path) -> None:
|
2026-10-03 13:17:21 -07:00
|
|
|
global _run_start, _obligation_root
|
2026-10-03 10:06:12 -07:00
|
|
|
_run_start = (list(git_cmd), head_and_branch(git_cmd, root))
|
2026-10-03 13:17:21 -07:00
|
|
|
_obligation_root = Path(root)
|
2026-10-03 10:06:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_checkout_untouched(root: Path) -> bool:
|
|
|
|
|
"""``checkout_untouched`` for this run; True before the checkout phase (nothing can have moved)."""
|
|
|
|
|
if _run_start is None:
|
|
|
|
|
return True
|
|
|
|
|
return checkout_untouched(_run_start[0], Path(root), _run_start[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def preflight_refusal(git_cmd, root: Path, target_ref: str, critical_files) -> str | None:
|
|
|
|
|
"""Why this update must not start, checked BEFORE the first tree move; ``None`` to proceed.
|
|
|
|
|
|
|
|
|
|
* a venv owned by another OS user (the completion child used to refuse only after the swap);
|
|
|
|
|
* a target whose startup-critical modules do not compile (rollback used to be the only guard).
|
|
|
|
|
"""
|
|
|
|
|
root = Path(root)
|
|
|
|
|
if not _owns_live_checkout(root):
|
|
|
|
|
try:
|
|
|
|
|
from hermes_cli.venv_sync import refuse_foreign_owned_venv
|
|
|
|
|
|
|
|
|
|
refuse_foreign_owned_venv(root)
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
2026-10-05 00:22:57 -07:00
|
|
|
except Exception as exc: # health: allow BLE001 -- fail closed: any probe error refuses before the first move
|
2026-10-07 00:19:33 -07:00
|
|
|
_record_stop("venv_foreign_owner")
|
2026-10-05 00:22:57 -07:00
|
|
|
return f"✗ {exc}" # pm's refusal carries its own remediation text
|
2026-10-03 10:06:12 -07:00
|
|
|
broken = target_syntax_error(git_cmd, root, target_ref, critical_files)
|
|
|
|
|
if broken is not None:
|
|
|
|
|
path, error = broken
|
2026-10-07 00:19:33 -07:00
|
|
|
_record_stop("target_syntax_error")
|
2026-10-03 10:06:12 -07:00
|
|
|
return (f"✗ The update target has a syntax error in a critical file:\n {path}\n "
|
|
|
|
|
+ "\n ".join(error.splitlines()[:6]))
|
|
|
|
|
return None
|