Skip to content

The agent that grows with you

fix(bot-mode): a Windows relaunch never adds a second delivery outcome

kshitijk4poor9979e56cc138
diff --git a/hermes_bootstrap.py b/hermes_bootstrap.py
index cda4bcfa3ec..9cb74b77e33 100644
--- a/hermes_bootstrap.py
+++ b/hermes_bootstrap.py
@@ -518,6 +518,12 @@ if _legacy_post_swap is not None:
raise SystemExit(_continue_legacy_post_swap(_handoff_path, argv_tail=_argv_tail))
+class RelaunchExit(SystemExit):
+ """Exit carrying a relaunched child's status: that child already produced this run's output,
+ so callers that report their own boot failures (the Bot Chat delivery runner) must not."""
+ relaunched = True
+
+
from pm.environments import activate_dependencies, install_state_permission_message
from hermes_cli._early_recovery import recover_if_needed
@@ -539,7 +545,7 @@ if not _pm_repair:
if os.name == "nt":
import subprocess
- raise SystemExit(subprocess.call(_command))
+ raise RelaunchExit(subprocess.call(_command))
os.execv(str(_launch_python), _command)
except Exception as exc:
if isinstance(exc, PermissionError) and (message := install_state_permission_message(_root, exc)):
diff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.py
index a394dd8ef6b..03da9170000 100644
--- a/tools/bot_mode_dm.py
+++ b/tools/bot_mode_dm.py
@@ -547,12 +547,20 @@ def _live_outcome_unknown(dm_file: str, cause: object) -> str:
"evidence_file": dm_file})
-def _runner_dm_file(args: list[str]) -> Optional[str]:
- """The DM file named by ``--run-delivery [--author <json>] <mode> <dm_file> …`` argv."""
- rest = args[1:]
+def _runner_argv(args: list[str]) -> tuple[Optional[str], str, str, list[str]] | None:
+ """Split ``--run-delivery [--author <json>] <mode> <dm_file> <argv…>`` into
+ ``(author_json, mode, dm_file, argv)``; None when malformed. Stdlib only: the boot-failure
+ report runs it when no Hermes import is available."""
+ if args[:1] != ["--run-delivery"]:
+ return None
+ rest, author = args[1:], None
if rest[:1] == ["--author"]:
- rest = rest[2:]
- return rest[1] if len(rest) >= 2 and rest[0] in ("stdin", "query-file") else None
+ if len(rest) < 2:
+ return None
+ author, rest = rest[1], rest[2:]
+ if len(rest) < 2 or rest[0] not in ("stdin", "query-file"):
+ return None
+ return author, rest[0], rest[1], rest[2:]
def _run_delivery(argv: list[str], dm_file: str, *, stdin_file: bool,
@@ -788,23 +796,22 @@ def _delivery_main(args: list[str]) -> int:
Malformed argv exits 2 without touching the DM file."""
if args[:1] == ["--wait-reply"]:
return _wait_reply_main(*args[1:]) if len(args) == 4 else 2
- if not args or args[0] != "--run-delivery":
+ parsed = _runner_argv(args)
+ if parsed is None:
return 2
- rest, author = args[1:], None
- if rest[:1] == ["--author"]:
+ author_json, mode, dm_file, argv = parsed
+ author = None
+ if author_json is not None:
from agent.turn_author import parse_turn_author
- author = parse_turn_author(rest[1]) if len(rest) > 1 else None
+ author = parse_turn_author(author_json)
if author is None:
return 2
- rest = rest[2:]
- if len(rest) < 2 or rest[0] not in ("stdin", "query-file"):
- return 2
try:
- argv, profile_home = rest[2:], None
+ profile_home = None
if len(argv) >= 2 and argv[0] == "--profile-home":
profile_home, argv = Path(argv[1]), argv[2:]
- return _run_delivery(argv, rest[1], stdin_file=rest[0] == "stdin", profile_home=profile_home, author=author)
+ return _run_delivery(argv, dm_file, stdin_file=mode == "stdin", profile_home=profile_home, author=author)
except Exception as exc:
# Every refusal ships a typed reason on stdout so the completion notification carries it
# back to the sender (#93091): 'target_busy' from the queue's bounded wait, otherwise the
@@ -844,18 +851,19 @@ if __name__ == "__main__": # pragma: no cover - exercised as a background proce
# entry point before the lazy Hermes imports. Run as a path, sys.path[0] is tools/.
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
# Only the delivery lane boots: the reply waiter is stdlib only and its stdout is the
- # sender's wake-up, so an activation failure must never exit it before it can print.
+ # sender's wake-up, and any other argv exits 2 before a Hermes import.
if sys.argv[1:2] == ["--run-delivery"]:
try:
import hermes_bootstrap # noqa: F401
except (Exception, SystemExit) as exc:
# A pinned live intent means the sender may already have admitted this DM and was
# told not to resend; a bare repair hint would read as "NOT delivered". Without an
- # intent nothing was handed over, so the plain failure is the truth.
- dm_file = _runner_dm_file(sys.argv[1:])
- failed = not isinstance(exc, SystemExit) or exc.code not in (None, 0)
- if failed and dm_file and os.path.exists(dm_file + ".live.json"):
- print(_live_outcome_unknown(dm_file, "the delivery runner could not activate "
- "Hermes dependencies (see stderr)"))
+ # intent nothing was handed over, so the plain failure is the truth. A relaunched
+ # child (Windows) already printed its own outcome.
+ booted = getattr(exc, "relaunched", False) or (isinstance(exc, SystemExit) and not exc.code)
+ parsed = _runner_argv(sys.argv[1:])
+ if not booted and parsed and os.path.exists(parsed[2] + ".live.json"):
+ print(_live_outcome_unknown(parsed[2], "the delivery runner could not activate "
+ "Hermes dependencies (see stderr)"))
raise
raise SystemExit(_delivery_main(sys.argv[1:]))