Skip to content

The agent that grows with you

fix(gateway): a gateway re-entered through a dependency-syncing launcher is a gateway

Teknium33764c64d373
diff --git a/gateway/status_inline_source.py b/gateway/status_inline_source.py
index ad069ffb28a..14d7eb4d37d 100644
--- a/gateway/status_inline_source.py
+++ b/gateway/status_inline_source.py
@@ -84,12 +84,19 @@ _BOOTSTRAPS = (
("path", re.compile(
rf"import sys, runpy; sys\.path\.insert\(.*\brunpy\.run_path\(\s*{_Q}(?P<target>[^'\"]+?){_Q}\s*,\s*run_name\s*=\s*{_MAIN}\s*\)",
re.S)),
+ # hermes_cli.venv_sync.relaunch_command re-entering a ``-c`` launcher: the launcher's source is
+ # exec'd as a string literal, its argv assigned before it; resolved through the rows above/below
+ ("exec", re.compile(r"import sys, runpy; sys\.path\.insert\(.*?;\s*exec\((?P<target>.+)\)", re.S)),
# hermes_cli._launchers._launcher_script (the published POSIX shell / Windows .cmd launcher)
("entry", re.compile(r"import os, re, sys\s.*\bfrom\s+(?P<target>[\w.]+)\s+import\s+(?P<func>\w+)\b.*\bsys\.exit\(\s*(?P=func)\(\)\s*\)", re.S)),
# hermes_cli._launchers._write_cmd_launcher: the launcher script, base64-encoded
("base64", re.compile(rf"import base64; exec\(base64\.b64decode\({_Q}(?P<target>[A-Za-z0-9+/=]+){_Q}\)\)")),
)
_ASSIGNED_ARGV = re.compile(r"\bsys\.argv\s*=\s*\[(.*?)\]\s*;")
+_EXEC_LAUNCHER = re.compile(
+ # no ``\b`` before ``from``/``sys``: an escaped newline normalizes to ``/n`` and abuts them
+ r"import os, re, sys\W.*?from\s+(?P<target>[\w.]+)\s+import\s+(?P<func>\w+)\b.*sys\.exit\(\s*(?P=func)\(\)\s*\)", re.S)
+_EXEC_BASE64 = re.compile(r"import base64; exec\(base64\.b64decode\(\W*(?P<b64>[A-Za-z0-9+/=]+)")
def _bootstrap_entry(source: str, argv: list[str]) -> list[str] | None:
@@ -106,6 +113,17 @@ def _bootstrap_entry(source: str, argv: list[str]) -> list[str] | None:
return _bootstrap_entry(base64.b64decode(target, validate=True).decode("utf-8"), argv)
except (binascii.Error, UnicodeDecodeError):
return None
+ if kind == "exec":
+ # The launcher source is a string literal here, and readers normalize its escapes (``\\n`` ->
+ # ``/n``), so it is matched in place by the launcher row's anchors, never decoded.
+ if assigned := _ASSIGNED_ARGV.search(source):
+ argv = [item.strip().strip("'\"") for item in assigned.group(1).split(",")][1:]
+ if wrapped := _EXEC_BASE64.search(target):
+ return _bootstrap_entry(f"import base64; exec(base64.b64decode('{wrapped['b64']}'))", argv)
+ launcher = _EXEC_LAUNCHER.search(target)
+ if launcher is None:
+ return None
+ kind, target = "entry", launcher["target"]
if kind == "entry": # the launcher script's own ``--run-module <module>`` switch
return ["-m", argv[1], *argv[2:]] if argv[:1] == ["--run-module"] and len(argv) > 1 else ["-m", target, *argv]
if assigned := _ASSIGNED_ARGV.search(source):
diff --git a/tests/gateway/test_gateway_inline_bootstrap_identity.py b/tests/gateway/test_gateway_inline_bootstrap_identity.py
index 7625e409382..4be6775fb2a 100644
--- a/tests/gateway/test_gateway_inline_bootstrap_identity.py
+++ b/tests/gateway/test_gateway_inline_bootstrap_identity.py
@@ -23,6 +23,7 @@ from hermes_cli.update_cmd_windows import _hermes_holder_subcommand
ROOT = Path("/opt/Hermes Agent/hermes-agent")
PY = "/opt/venv/bin/python3"
_SCRIPT = _launchers._launcher_script("hermes", ROOT, None)
+_B64 = f"import base64; exec(base64.b64decode('{base64.b64encode(_SCRIPT.encode()).decode()}'))"
_JOINS = {"space-joined": " ".join, "windows": subprocess.list2cmdline}
@@ -30,10 +31,15 @@ def _forms(argv: list[str]) -> dict[str, list[str]]:
return {
"store-launcher": _launchers.runtime_command(ROOT, argv, python=Path(PY)),
"launcher-script": [PY, "-I", "-c", _SCRIPT, *argv],
- "cmd-launcher": [PY, "-I", "-c", f"import base64; exec(base64.b64decode('{base64.b64encode(_SCRIPT.encode()).decode()}'))", *argv],
+ "cmd-launcher": [PY, "-I", "-c", _B64, *argv],
"venv-reentry": venv_sync.relaunch_command(
Path(PY), ROOT, [str(ROOT / "hermes_cli" / "main.py"), *argv], ["/old/python", "-m", "hermes_cli.main", *argv],
"hermes_cli.main"),
+ # A launch through the published launcher that synced dependencies first re-enters it via exec().
+ "venv-reentry-launcher": venv_sync.relaunch_command(
+ Path(PY), ROOT, ["-c", *argv], ["/old/python", "-I", "-c", _SCRIPT, *argv], None),
+ "venv-reentry-cmd-launcher": venv_sync.relaunch_command(
+ Path(PY), ROOT, ["-c", *argv], ["/old/python", "-I", "-c", _B64, *argv], None),
}