Skip to content

The agent that grows with you

fix: a post-commit follow-up is owed once, through one owe_followup helper

Teknium0b5b01ba7a92
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 6d092950862..c45e99ffc96 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -748,12 +748,10 @@ def _resume_paused_gateways_at_exit(token: dict | None, request: dict | None) ->
try:
_m()._resume_windows_gateways_after_update(token)
except Exception as exc: # health: allow BLE001 -- a restart failure is owed, not the update's status
- from hermes_cli.update_receipt import _current, amend_terminal_followup, record_followup
+ from hermes_cli.update_receipt import owe_followup
- reason = f"Windows gateway recovery failed: {exc}"
- record_followup("windows_resume", reason)
- if _current.get() is None and request: # the run already finalized its receipt
- amend_terminal_followup(request["receipt"]["update_id"], "windows_resume", reason)
+ owe_followup(request["receipt"]["update_id"] if request else None, "windows_resume",
+ f"Windows gateway recovery failed: {exc}")
def _complete_source_update(request: dict | None) -> None:
@@ -812,11 +810,9 @@ def _complete_source_update(request: dict | None) -> None:
try:
_m()._resume_windows_gateways_after_update(token)
except Exception as exc: # health: allow BLE001 -- the code is committed (C3)
- from hermes_cli.update_receipt import amend_terminal_followup, record_followup
+ from hermes_cli.update_receipt import owe_followup
- reason = f"Windows gateway recovery failed: {exc}"
- record_followup("windows_resume", reason)
- amend_terminal_followup(request["receipt"]["update_id"], "windows_resume", reason)
+ owe_followup(request["receipt"]["update_id"], "windows_resume", f"Windows gateway recovery failed: {exc}")
_settle_windows_resume(request)
if result["exit_code"]:
raise SystemExit(result["exit_code"])
@@ -826,11 +822,10 @@ def _complete_source_update(request: dict | None) -> None:
if adopt_retired_channel(request):
print(f"→ Source subscription moved to {request['channel_retirement']['destination']}")
except Exception as exc: # health: allow BLE001 -- the code is committed (C3); the next update re-adopts
- from hermes_cli.update_receipt import amend_terminal_followup, record_followup
+ from hermes_cli.update_receipt import owe_followup
- reason = str(exc) or type(exc).__name__
- record_followup("channel_adoption", reason, retry="the next `hermes update` adopts it again")
- amend_terminal_followup(request["receipt"]["update_id"], "channel_adoption", reason)
+ owe_followup(request["receipt"]["update_id"], "channel_adoption", str(exc) or type(exc).__name__,
+ retry="the next `hermes update` adopts it again")
def _reconcile_diverged_checkout(git_cmd, branch: str, pre_pull_sha, *, target_ref=None) -> None:
diff --git a/hermes_cli/update_completion.py b/hermes_cli/update_completion.py
index c209e851a8e..b4631bdad01 100644
--- a/hermes_cli/update_completion.py
+++ b/hermes_cli/update_completion.py
@@ -462,10 +462,8 @@ def _finish(request: dict, result_path: Path) -> int:
from hermes_cli.update_cmd import _resume_windows_gateways_after_update
_resume_windows_gateways_after_update(request["windows_resume"])
except Exception as exc:
- step_reason = f"Windows gateway recovery failed: {exc}"
- update_receipt.record_followup("windows_resume", step_reason)
- if update_receipt._current.get() is None: # verification already finalized this run
- update_receipt.amend_terminal_followup(request["receipt"]["update_id"], "windows_resume", step_reason)
+ update_receipt.owe_followup(request["receipt"]["update_id"], "windows_resume",
+ f"Windows gateway recovery failed: {exc}")
update_receipt.finalize_pending_update_receipt(code, reason)
terminal_receipt = _read_terminal_receipt(request)
if terminal_receipt and terminal_receipt.get("outcome") == "success":
diff --git a/hermes_cli/update_finish.py b/hermes_cli/update_finish.py
index beb386eb45d..2014b0a1b6c 100644
--- a/hermes_cli/update_finish.py
+++ b/hermes_cli/update_finish.py
@@ -63,10 +63,7 @@ def finish_update(*, root, assume_yes, gateway_mode, pre_update_snapshot_id,
_verify_fleet_after_update(restarted, _pre_update_plan=plan, _windows_gateway_resume=windows_resume,
update_complete=bool(runtime_safe) and not tail_owed)
except (Exception, SystemExit) as exc: # health: allow BLE001 -- the code is committed (C3): the fleet obligation stays armed
- reason = str(exc) or type(exc).__name__
- record_followup("gateway_restart", reason)
- if update_receipt._current.get() is None and update_id: # verification already finalized the run
- update_receipt.amend_terminal_followup(update_id, "gateway_restart", reason)
+ update_receipt.owe_followup(update_id, "gateway_restart", str(exc) or type(exc).__name__)
def _restore_plan(data):
@@ -187,10 +184,7 @@ def main(context: Path, result: Path) -> int:
resume(token)
except Exception as exc: # noqa: BLE001 — a restart failure is owed, never the exit status (C3)
attempted = True
- reason = f"Windows gateway recovery failed: {exc}"
- update_receipt.record_followup("windows_resume", reason)
- if update_receipt._current.get() is None: # the run already finalized its receipt
- update_receipt.amend_terminal_followup(request["update_id"], "windows_resume", reason)
+ update_receipt.owe_followup(request["update_id"], "windows_resume", f"Windows gateway recovery failed: {exc}")
handled = cli_started or (begun and update_receipt._current.get() is None)
written = update_receipt.finalize_pending_update_receipt(code, "historical takeover completion")
result.write_text(json.dumps({
diff --git a/hermes_cli/update_receipt.py b/hermes_cli/update_receipt.py
index d30ec60bfbd..f107a8ff8a2 100644
--- a/hermes_cli/update_receipt.py
+++ b/hermes_cli/update_receipt.py
@@ -443,6 +443,15 @@ def amend_terminal_followup(update_id: str, step: str, reason: str) -> None:
_write_latest(payload)
+def owe_followup(update_id: Optional[str], step: str, reason: str, **retry: str) -> None:
+ """A post-commit step failed: record it on the run while its receipt is open, else amend the
+ run's closed receipt. Amending an OPEN run would append a second copy beside the one
+ ``record_followup`` just persisted. Never raises."""
+ record_followup(step, reason, **retry)
+ if _current.get() is None and update_id:
+ amend_terminal_followup(update_id, step, reason)
+
+
def _record(method: str, what: str, *args: Any, **kwargs: Any) -> None:
"""Invoke ``method`` on the active receipt; no-op when none, never raises.
diff --git a/tests/hermes_cli/test_update_receipt_owe_followup.py b/tests/hermes_cli/test_update_receipt_owe_followup.py
new file mode 100644
index 00000000000..381e4bb8f66
--- /dev/null
+++ b/tests/hermes_cli/test_update_receipt_owe_followup.py
@@ -0,0 +1,36 @@
+"""A post-commit follow-up lands on the run exactly once, whether its receipt is still open or closed."""
+from __future__ import annotations
+
+import json
+
+from hermes_cli import update_receipt
+
+
+def _steps(path):
+ return [f["step"] for f in json.loads(path.read_text(encoding="utf-8")).get("followups") or []]
+
+
+def test_follow_up_on_an_open_run_is_recorded_once_not_amended_again(_isolate_hermes_home):
+ update_receipt.begin_update_receipt()
+ run = update_receipt._current.get().data
+ archive = update_receipt._run_file(update_receipt._receipt_dir(), run)
+
+ update_receipt.owe_followup(run["update_id"], "channel_adoption", "remote refused",
+ retry="the next `hermes update` adopts it again")
+
+ # The durable running record (what an interrupted run leaves behind) owes it once.
+ assert _steps(archive) == ["channel_adoption"]
+ update_receipt.finalize_update_receipt("success")
+ assert _steps(archive) == ["channel_adoption"]
+
+
+def test_follow_up_after_the_run_closed_amends_its_terminal_receipt(_isolate_hermes_home):
+ update_receipt.begin_update_receipt()
+ run = dict(update_receipt._current.get().data)
+ update_receipt.finalize_update_receipt("success")
+
+ update_receipt.owe_followup(run["update_id"], "windows_resume", "Windows gateway recovery failed: boom")
+
+ assert _steps(update_receipt._run_file(update_receipt._receipt_dir(), run)) == ["windows_resume"]
+ assert [f["step"] for f in update_receipt.read_latest_receipt()["followups"]] == ["windows_resume"]
+ assert update_receipt.read_latest_receipt()["outcome"] == "success"