The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
Merge origin/main into ethie/pm-clean
ethernet9a8cdf96bf32
diff --cc tests/hermes_cli/test_update_scoped_reconciliation.pyindex 32764bcb208,41423189af7..1fabeed5f0b--- a/tests/hermes_cli/test_update_scoped_reconciliation.py+++ b/tests/hermes_cli/test_update_scoped_reconciliation.py@@@ -64,30 -81,145 +80,62 @@@ def test_scoped_reconciliation_matrix(massert target.read_bytes() == before- @pytest.mark.parametrize("suffix", ["{", '{"version": 1, "inventory": ', "broken-line"])- def test_malformed_obligation_stays_pending(monkeypatch, suffix):- """An unparseable record is an obligation whose terms are unknown — never a discharged one."""- seed(monkeypatch, {}, "new", [CURRENT])- marker = host_obligation.host_obligation_path()- marker.write_text(marker.read_text(encoding="utf-8") + suffix, encoding="utf-8")- assert fleet._pending_fleet_restart_needed()- assert marker.exists()[email protected]("alive", [True, False, None], ids=["alive", "dead", "unknown"])[email protected]("sha", ["old", "new", None], ids=["different-sha", "same-sha", "no-sha"])-def test_empty_marker_never_inherits_receipt_ownership(monkeypatch, capsys, alive, sha):- old = {"outcome": "success", "plan": {"runtimes": [MANUAL]}, "post_update": {"sha": sha}}- target = seed(monkeypatch, old, "new", [], alive)- before = target.read_bytes()- fleet._warn_pending_fleet_restart_on_startup()- warning = capsys.readouterr().err- assert "hermes gateway restart" in warning- assert ("serve [work] pid 900" in warning) is (alive is not False)- assert host_obligation.host_obligation_path().exists()- assert target.read_bytes() == before--+ DESKTOP = RuntimeRecord(kind="serve", profile="default", pid=901, supervisor="desktop", restart_via="desktop-respawn")+ DESKTOP_RECEIPT = {"outcome": "failed", "plan": {"runtimes": [{"kind": "serve", "profile": "default", "supervisor": "desktop"}]}}++ # (name, receipt, live runtimes, gateway_state per profile, checkout, pending)+ GATEWAYLESS_CASES = [+ # #118742: Desktop app only, no gateway was ever installed.+ ("desktop-only", {}, [DESKTOP], {}, "new", False),+ ("nothing-running", {}, [], {}, "new", False),+ ("gateway-stopped-cleanly", {}, [DESKTOP], {"default": "stopped"}, "new", False),+ ("gateway-startup-failed", {}, [], {"default": "startup_failed"}, "new", False),+ # HEAD carries a local commit on top of the pulled SHA (#119367).+ ("carried-local-commit", {}, [DESKTOP], {}, "hotfix", False),+ # Receipts neither discharge nor block: the old manual row is history, not the live host.+ ("manual-receipt-gatewayless-host", {"outcome": "success", "plan": {"runtimes": [MANUAL]}}, [], {}, "new", False),+ ("desktop-receipt-stopped-gateway", DESKTOP_RECEIPT, [DESKTOP], {"default": "running"}, "new", True),+ ("named-profile-gateway-gone", {}, [DESKTOP], {"work": "running"}, "new", True),+ ("state-record-without-state", {}, [], {"default": None}, "new", True),+ ("unclassified-serve", {}, [RuntimeRecord(kind="serve", profile="default", pid=902, supervisor="manual")], {}, "new", True),+ ("manual-serve-without-identity", {}, [RuntimeRecord(kind="serve", profile="work", pid=903, supervisor="manual-serve", restart_via="respawn-argv")], {}, "new", True),+ ("gateway-runtime-without-fleet-row", {}, [RuntimeRecord(kind="gateway", profile="default", pid=904, supervisor="manual")], {}, "new", True),+ ("checkout-diverged", {}, [DESKTOP], {}, "elsewhere", True),+ ]- def test_marker_reconciliation_collects_one_live_snapshot(monkeypatch):- seed(monkeypatch, {}, "new", [CURRENT])- fleet._write_fleet_restart_pending_marker(expected_sha="new", runtimes=[GATEWAY])- probes = []+ @pytest.mark.parametrize("name,receipt,runtimes,states,checkout,pending", GATEWAYLESS_CASES, ids=[case[0] for case in GATEWAYLESS_CASES])+ def test_gatewayless_host_settles_on_host_evidence(monkeypatch, capsys, name, receipt, runtimes, states, checkout, pending):+ """An inventory-less marker with no live gateway settles on what the host runs now (#118742)."""+ from hermes_cli.profiles import _get_default_hermes_home, _get_profiles_root- def collect(**kwargs):- probes.append(True)- return [CURRENT] if len(probes) == 1 else []+ seed(monkeypatch, receipt, "new", [])+ (get_hermes_home() / "gateway_state.json").unlink()+ for profile, state in states.items():+ write_gateway_state(_get_default_hermes_home() if profile == "default" else _get_profiles_root() / profile, state)+ monkeypatch.setattr(update_inventory, "collect_runtime_inventory", lambda: UpdatePlan(runtimes=list(runtimes)))+ monkeypatch.setattr(fleet, "_current_checkout_sha", lambda: checkout)+ monkeypatch.setattr("hermes_cli.update_cmd._current_checkout_sha", lambda: checkout)+ monkeypatch.setattr("hermes_cli.update_cmd_fleet_checkout.checkout_contains", lambda sha: checkout == "hotfix")++ assert fleet._pending_fleet_restart_needed() is pending+ assert host_obligation.host_obligation_path().exists() is pending+ fleet._warn_pending_fleet_restart_on_startup()+ assert ("hermes gateway restart" in capsys.readouterr().err) is pending- monkeypatch.setattr(update_receipt, "collect_fleet_versions", collect)- assert not fleet._pending_fleet_restart_needed()- assert len(probes) == 1++ def test_gatewayless_probe_failure_keeps_marker(monkeypatch):+ seed(monkeypatch, {}, "new", [])+ (get_hermes_home() / "gateway_state.json").unlink()++ def unavailable():+ raise OSError("process table unreadable")++ monkeypatch.setattr(update_inventory, "collect_runtime_inventory", unavailable)+ assert fleet._pending_fleet_restart_needed()+ assert host_obligation.host_obligation_path().exists()[email protected]("consumer", ["predicate", "startup"])-def test_reconciliation_uses_one_receipt_snapshot(monkeypatch, capsys, consumer):- old = {"outcome": "partial", "plan": {"runtimes": [MANUAL]}, "fleet": []}- seed(monkeypatch, old, None, [])- reads = []-- def read_rotating_receipt():- reads.append(True)- return old if len(reads) == 1 else {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}-- monkeypatch.setattr(update_receipt, "read_latest_receipt", read_rotating_receipt)- if consumer == "predicate":- assert not fleet._pending_fleet_restart_needed()- else:- fleet._warn_pending_fleet_restart_on_startup()- warning = capsys.readouterr().err- assert "hermes gateway restart" not in warning- assert "serve [work] pid 900" in warning- assert len(reads) == 1- assert list((get_hermes_home() / "serve_restart_pending").glob("*.json"))--[email protected]("marker", [None, "new"])[email protected]("blocked_storage", [False, True])-def test_probe_exception_does_not_hide_manual_warning(monkeypatch, capsys, marker, blocked_storage):- old = {"outcome": "partial", "plan": {"runtimes": [GATEWAY, MANUAL]}}- target = seed(monkeypatch, old, marker, [])- if marker is not None:- fleet._write_fleet_restart_pending_marker(expected_sha=marker, runtimes=[GATEWAY])- before = target.read_bytes()- if blocked_storage:- (get_hermes_home() / "serve_restart_pending").write_text("not a directory")-- def unavailable(**kwargs):- raise OSError("gateway probe unavailable")-- monkeypatch.setattr(update_receipt, "collect_fleet_versions", unavailable)- fleet._warn_pending_fleet_restart_on_startup()- warning = capsys.readouterr().err- assert "hermes gateway restart" in warning- assert "serve [work] pid 900" in warning- assert ("could not be saved" in warning) is blocked_storage- assert target.read_bytes() == before--[email protected]("completed_restart", [False, True])-def test_new_marker_cannot_borrow_old_alpha_receipt(monkeypatch, capsys, completed_restart):- """N owns alpha; N+1 owns alpha and beta but dies before writing its receipt."""- old = {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}- if completed_restart:- old.update(post_update={"sha": "new"}, gateway_restart={"incomplete": False})- live = [CURRENT]- target = seed(monkeypatch, old, "new", live)- marker = host_obligation.host_obligation_path()- host_obligation.amend_host_obligation(inventory={"version": 1, "runtimes": [GATEWAY, dict(GATEWAY, profile="beta")]})- receipt_before, marker_before = target.read_bytes(), marker.read_bytes()- fleet._warn_pending_fleet_restart_on_startup()- assert "hermes gateway restart" in capsys.readouterr().err- assert fleet._pending_fleet_restart_needed()- fleet._apply_pending_fleet_restart_catchup(defer=True)- assert "fleet restart deferred" in capsys.readouterr().out- assert marker.read_bytes() == marker_before- assert target.read_bytes() == receipt_before- live.append(dict(CURRENT, profile="beta"))- assert not fleet._pending_fleet_restart_needed()- assert not marker.exists()- assert target.read_bytes() == receipt_before--@pytest.mark.parametrize("completed_restart", [False, True])def test_legacy_marker_discharges_on_live_fleet_evidence_without_receipt(monkeypatch, capsys, completed_restart):"""An inventory-less N+1 marker settles on live-fleet evidence alone (#115638).@@@ -122,3 -254,145 +170,32 @@@ def test_inventory_less_marker_settles_assert ("hermes gateway restart" in capsys.readouterr().err) is pendingassert fleet._pending_fleet_restart_needed() is pendingassert marker.exists() is pending+++ # Explicit `inventory=null` records no obligation either, so it settles on live-fleet+ # evidence like a missing line (#115638), and an explicit empty inventory owes nothing+ # (#115311); the variants below stay fail-closed.[email protected]("inventory", [{}, [], {"version": 2, "runtimes": [GATEWAY]}, {"version": 1, "runtimes": [GATEWAY, dict(MANUAL, detail={})]}, {"version": 1, "runtimes": [None]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": "unknown"}]}, {"version": 1, "runtimes": [{"kind": "gateway", "profile": []}]}])-def test_unverified_marker_inventory_stays_pending(monkeypatch, inventory):- seed(monkeypatch, {"outcome": "success", "plan": {"runtimes": [GATEWAY]}}, "new", [CURRENT])- marker = host_obligation.host_obligation_path()- host_obligation.amend_host_obligation(inventory=inventory)- before = marker.read_bytes()- assert fleet._pending_fleet_restart_needed()- monkeypatch.setattr("hermes_cli.update_cmd._run_pending_fleet_restart", lambda: True)- with pytest.raises(SystemExit) as exc:- fleet._apply_pending_fleet_restart_catchup()- assert exc.value.code == 1- assert marker.read_bytes() == before+++ @pytest.mark.parametrize("suffix", ["{", '{"version": 1, "inventory": ', "broken-line"])+ def test_malformed_obligation_stays_pending(monkeypatch, suffix):+ """An unparseable record is an obligation whose terms are unknown — never a discharged one."""+ seed(monkeypatch, {}, "new", [CURRENT])+ marker = host_obligation.host_obligation_path()+ marker.write_text(marker.read_text(encoding="utf-8") + suffix, encoding="utf-8")+ assert fleet._pending_fleet_restart_needed()+ assert marker.exists()++-def test_pulled_update_marker_owns_pre_update_inventory(monkeypatch):- from types import SimpleNamespace- from hermes_cli import update_cmd- from hermes_cli.update_inventory import RuntimeRecord, UpdatePlan-- old = {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}- target = seed(monkeypatch, old, None, [CURRENT])- before = target.read_bytes()- plan = UpdatePlan(runtimes=[RuntimeRecord(kind="gateway", profile=p) for p in ("alpha", "beta")])- monkeypatch.setattr(update_cmd, "_invalidate_update_cache", lambda: None)- monkeypatch.setattr(update_cmd, "_verify_head_after_pull", lambda *a, **k: "new")-- def interrupt(*args):- raise KeyboardInterrupt-- monkeypatch.setattr(update_cmd, "_sweep_bytecode_after_update", interrupt)- with pytest.raises(KeyboardInterrupt):- update_cmd._apply_pulled_update([], "main", "old", SimpleNamespace(in_place_update=True), None, gateway_mode=False, is_fork=False, desktop_dir=None, had_desktop_app_before_update=False, pre_update_snapshot_id=None, _pre_update_plan=plan, _windows_gateway_resume=None, args=SimpleNamespace())- marker = host_obligation.host_obligation_path()- fields = json.loads(marker.read_text(encoding="utf-8"))- assert fields["expected_sha"] == "new"- assert fields["inventory"] == {"version": 1, "runtimes": plan.to_dict()["runtimes"]}- assert fleet._pending_fleet_restart_needed()- assert target.read_bytes() == before--[email protected]("successor", ["missing", "stale", "unknown", "probe-error", "current"])-def test_catchup_verifies_owned_fleet_after_restart(monkeypatch, successor):- from hermes_cli import update_cmd-- old = {"outcome": "failed", "plan": {"runtimes": [GATEWAY]}}- live = [CURRENT]- target = seed(monkeypatch, old, "new", live)- fleet._write_fleet_restart_pending_marker(expected_sha="new", runtimes=[GATEWAY, dict(GATEWAY, profile="beta")])- marker = host_obligation.host_obligation_path()- receipt_before, marker_before = target.read_bytes(), marker.read_bytes()- restarted = []-- def restart():- restarted.append("alpha")- live[:] = [CURRENT]- if successor not in ("missing", "probe-error"):- live.append(dict(CURRENT, profile="beta", state=successor, code_sha="old" if successor == "stale" else "new"))- return True-- def collect(**kwargs):- if restarted and successor == "probe-error":- raise OSError("fleet unavailable")- return live-- monkeypatch.setattr(update_cmd, "_run_pending_fleet_restart", restart)- monkeypatch.setattr(update_receipt, "collect_fleet_versions", collect)- if successor == "current":- fleet._apply_pending_fleet_restart_catchup()- assert not marker.exists()- else:- with pytest.raises(SystemExit) as exc:- fleet._apply_pending_fleet_restart_catchup()- assert exc.value.code == 1- assert marker.read_bytes() == marker_before- live[:] = [CURRENT, dict(CURRENT, profile="beta")]- monkeypatch.setattr(update_receipt, "collect_fleet_versions", lambda **k: live)- fleet._apply_pending_fleet_restart_catchup()- assert not marker.exists()- assert restarted == ["alpha"]- assert target.read_bytes() == receipt_before--[email protected]("blocked_storage", [False, True])-def test_verified_restart_surviving_marker_preserves_manual_debt(monkeypatch, capsys, blocked_storage):- from hermes_cli import update_cmd-- manual = [MANUAL, dict(MANUAL, pid=901)]- old = {"outcome": "partial", "post_update": {"sha": "new"}, "gateway_restart": {"incomplete": False, "phase_error": ""}, "plan": {"runtimes": [GATEWAY, *manual]}, "fleet": [CURRENT]}- target = seed(monkeypatch, old, "new", [CURRENT])- fleet._write_fleet_restart_pending_marker(expected_sha="new", runtimes=[GATEWAY, *manual])- marker = host_obligation.host_obligation_path()- receipt_before, marker_before = target.read_bytes(), marker.read_bytes()- directory = get_hermes_home() / "serve_restart_pending"- if blocked_storage:- directory.write_text("not a directory")- restarted = []- monkeypatch.setattr(update_cmd, "_run_pending_fleet_restart", lambda: restarted.append(True) or True)- if blocked_storage:- with pytest.raises(SystemExit) as exc:- fleet._apply_pending_fleet_restart_catchup()- assert exc.value.code == 1- assert marker.read_bytes() == marker_before- directory.unlink()- restarted.clear()- fleet._apply_pending_fleet_restart_catchup()- assert not restarted- assert not marker.exists()- assert {json.loads(path.read_text())["pid"] for path in directory.glob("*.json")} == {900, 901}- fleet._warn_pending_fleet_restart_on_startup()- warning = capsys.readouterr().err- assert "hermes gateway restart" not in warning- assert "pid 900" in warning and "pid 901" in warning- assert target.read_bytes() == receipt_before--+ def test_marker_reconciliation_collects_one_live_snapshot(monkeypatch):+ seed(monkeypatch, {}, "new", [CURRENT])+ fleet._write_fleet_restart_pending_marker(expected_sha="new", runtimes=[GATEWAY])+ probes = []++ def collect(**kwargs):+ probes.append(True)+ return [CURRENT] if len(probes) == 1 else []++ monkeypatch.setattr(update_receipt, "collect_fleet_versions", collect)+ assert not fleet._pending_fleet_restart_needed()+ assert len(probes) == 1