fix(gateway): rebase launchd --replace removal onto main
Rebuilt branch from upstream/main f751a8c546 and re-applied the PR
changes. Resolved one conflict in tests/gateway/test_platform_base.py:
main had added TestDockerProfileSandboxMediaTranslation in the same
region — kept both main's new tests and the PR's
TestPlatformLockTakeoverGovernance regression suite.
Local: tests/gateway/test_platform_base.py +
tests/hermes_cli/test_gateway_service.py — 185 passed, 2 skipped;
ruff clean.
Refs: #79096
This commit is contained in:
@@ -4881,10 +4881,22 @@ def _timestamped_stderr_gateway_command(
|
||||
``hermes update`` sees the flag on the live grandchild argv and hands
|
||||
the process back to launchd instead of starting a detached watcher
|
||||
(#86893 / #87005). The detached nohup fallback stays unmarked.
|
||||
|
||||
Supervised starts also drop ``--replace`` (issue #79048): a launchd
|
||||
service is respawned by KeepAlive, so takeover authority would be
|
||||
re-armed on every respawn — two profiles legitimately sharing one
|
||||
platform token would each terminate the sibling, and launchd would
|
||||
revive the victim forever. Bounded replacement is the lifecycle
|
||||
commands' job (``launchctl kickstart -k``, drain in
|
||||
``launchd_restart()``, bootout+bootstrap in install/refresh), which
|
||||
run before supervision resumes. Mirrors ``generate_systemd_unit``,
|
||||
whose ExecStart also runs ``gateway run`` without ``--replace``.
|
||||
"""
|
||||
inner = _gateway_run_command()
|
||||
if external_supervisor and "--external-supervisor" not in inner:
|
||||
inner = [*inner, "--external-supervisor"]
|
||||
if external_supervisor and "--replace" in inner:
|
||||
inner = [part for part in inner if part != "--replace"]
|
||||
return [
|
||||
get_python_path(),
|
||||
"-m",
|
||||
|
||||
@@ -10,6 +10,7 @@ from gateway.platforms.base import (
|
||||
BasePlatformAdapter,
|
||||
GATEWAY_SECRET_CAPTURE_UNSUPPORTED_MESSAGE,
|
||||
MessageEvent,
|
||||
SendResult,
|
||||
cache_audio_from_bytes,
|
||||
cache_image_from_bytes,
|
||||
cache_video_from_bytes,
|
||||
@@ -1254,6 +1255,7 @@ class TestMediaFallbackDoesNotLeakHostPath:
|
||||
assert self.SENSITIVE_PATH not in sent_text
|
||||
|
||||
|
||||
|
||||
class TestDockerProfileSandboxMediaTranslation:
|
||||
"""MEDIA from persistent Docker sandboxes must resolve to the host
|
||||
directory the profile's container actually bind-mounts (#93950).
|
||||
@@ -1386,3 +1388,101 @@ class TestDockerProfileSandboxMediaTranslation:
|
||||
"did not resolve" in r.message and f"session_key={self.SESSION_KEY}" in r.message
|
||||
for r in caplog.records
|
||||
)
|
||||
|
||||
class _LockGovernanceProbeAdapter(BasePlatformAdapter):
|
||||
"""Minimal concrete adapter for platform-lock takeover governance tests."""
|
||||
|
||||
async def connect(self, *, is_reconnect: bool = False) -> bool:
|
||||
return True
|
||||
|
||||
async def disconnect(self) -> None:
|
||||
pass
|
||||
|
||||
async def send(self, *args, **kwargs) -> SendResult:
|
||||
return SendResult(success=True)
|
||||
|
||||
async def get_chat_info(self, chat_id: str) -> dict:
|
||||
return {"name": "probe", "type": "dm"}
|
||||
|
||||
|
||||
class TestPlatformLockTakeoverGovernance:
|
||||
"""A supervised (non-``--replace``) gateway must never evict a live holder.
|
||||
|
||||
Regression for #79048: launchd services with ``KeepAlive=true`` used to be
|
||||
generated with ``--replace``, re-arming takeover authority on every
|
||||
respawn. Two profiles sharing one platform token (e.g. the same Discord
|
||||
bot) would then terminate each other in an endless mutual-eviction loop.
|
||||
The runtime guard is the adapter's ``_platform_lock_takeover_allowed``
|
||||
bit — only an explicit ``gateway run --replace`` startup arms it. Without
|
||||
that authority a live cross-home holder must be left alone and reported
|
||||
as a retryable failure, never terminated.
|
||||
"""
|
||||
|
||||
def _make_adapter(self, *, takeover_allowed: bool):
|
||||
from gateway.config import Platform, PlatformConfig
|
||||
|
||||
adapter = _LockGovernanceProbeAdapter(
|
||||
config=PlatformConfig(), platform=Platform.DISCORD
|
||||
)
|
||||
adapter._platform_lock_takeover_allowed = takeover_allowed
|
||||
return adapter
|
||||
|
||||
def test_no_takeover_without_replace_authority(self, tmp_path, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
existing = {
|
||||
"pid": 4242,
|
||||
"start_time": 123,
|
||||
"home": str(tmp_path / "other-profile-home"),
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
status,
|
||||
"acquire_scoped_lock",
|
||||
lambda scope, identity, metadata=None: (False, existing),
|
||||
)
|
||||
takeover_calls = []
|
||||
monkeypatch.setattr(
|
||||
status,
|
||||
"take_over_scoped_lock_holder",
|
||||
lambda existing: takeover_calls.append(existing) or 4242,
|
||||
)
|
||||
monkeypatch.setattr(status, "write_runtime_status", lambda **kw: None)
|
||||
|
||||
adapter = self._make_adapter(takeover_allowed=False)
|
||||
# Ordinary (supervised) start: the live cross-home holder must be
|
||||
# left untouched — the gateway fails retryably instead of killing it.
|
||||
assert adapter._acquire_platform_lock("discord-token", "tok", "Discord") is False
|
||||
assert takeover_calls == []
|
||||
assert adapter.fatal_error_retryable is True
|
||||
assert adapter._fatal_error_code == "discord-token_lock"
|
||||
|
||||
def test_takeover_authority_consumed_once(self, tmp_path, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
existing = {
|
||||
"pid": 4242,
|
||||
"start_time": 123,
|
||||
"home": str(tmp_path / "other-profile-home"),
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
status,
|
||||
"acquire_scoped_lock",
|
||||
lambda scope, identity, metadata=None: (False, existing),
|
||||
)
|
||||
takeover_calls = []
|
||||
monkeypatch.setattr(
|
||||
status,
|
||||
"take_over_scoped_lock_holder",
|
||||
lambda existing: takeover_calls.append(existing) or 4242,
|
||||
)
|
||||
monkeypatch.setattr(status, "write_runtime_status", lambda **kw: None)
|
||||
|
||||
adapter = self._make_adapter(takeover_allowed=True)
|
||||
# An explicit --replace start may attempt exactly one takeover...
|
||||
assert adapter._acquire_platform_lock("discord-token", "tok", "Discord") is False
|
||||
assert len(takeover_calls) == 1
|
||||
# ...but the authority is consumed, so a reconnect can never evict a
|
||||
# healthy holder (this is what stops the supervised respawn loop).
|
||||
assert adapter._acquire_platform_lock("discord-token", "tok", "Discord") is False
|
||||
assert len(takeover_calls) == 1
|
||||
assert adapter._platform_lock_takeover_attempted is True
|
||||
|
||||
@@ -1490,7 +1490,6 @@ class TestProfileArg:
|
||||
"mybot",
|
||||
"gateway",
|
||||
"run",
|
||||
"--replace",
|
||||
"--external-supervisor",
|
||||
]
|
||||
|
||||
@@ -2149,6 +2148,44 @@ class TestServiceWorkingDirIsStable:
|
||||
assert "/.worktrees/" not in m.group(1)
|
||||
|
||||
|
||||
class TestServiceTakeoverGovernance:
|
||||
"""Supervised service definitions must never arm ``--replace`` takeover.
|
||||
|
||||
Regression for #79048: two launchd-supervised profile gateways sharing
|
||||
one platform token (e.g. the same Discord bot) entered an endless
|
||||
mutual-eviction loop because the generated plist armed ``--replace`` on
|
||||
every KeepAlive respawn: each revived process was authorized to terminate
|
||||
the sibling holding the shared token, and launchd immediately revived the
|
||||
victim. The systemd unit already runs ``gateway run`` without
|
||||
``--replace``; the launchd plist must match so a supervised restart can
|
||||
never evict a legitimate cross-profile lock holder. Bounded replacement
|
||||
stays the lifecycle commands' job (kickstart -k / drain / bootout).
|
||||
"""
|
||||
|
||||
def test_launchd_plist_does_not_arm_takeover(self, tmp_path, monkeypatch):
|
||||
home = tmp_path / ".hermes"
|
||||
home.mkdir()
|
||||
monkeypatch.setattr(gateway_cli, "get_hermes_home", lambda: home)
|
||||
plist = gateway_cli.generate_launchd_plist()
|
||||
# The whole bug class: no --replace anywhere in the supervised argv.
|
||||
assert "--replace" not in plist
|
||||
# It still runs the plain gateway command under KeepAlive.
|
||||
assert "<string>gateway</string>" in plist
|
||||
assert "<string>run</string>" in plist
|
||||
assert "<key>KeepAlive</key>" in plist
|
||||
assert "<true/>" in plist
|
||||
|
||||
def test_systemd_unit_does_not_arm_takeover(self, tmp_path, monkeypatch):
|
||||
home = tmp_path / ".hermes"
|
||||
home.mkdir()
|
||||
monkeypatch.setattr(gateway_cli, "get_hermes_home", lambda: home)
|
||||
unit = gateway_cli.generate_systemd_unit(system=False)
|
||||
exec_starts = [l for l in unit.splitlines() if l.startswith("ExecStart=")]
|
||||
assert exec_starts, "unit has no ExecStart line"
|
||||
# The safe service posture the launchd plist now mirrors.
|
||||
assert "--replace" not in exec_starts[0]
|
||||
|
||||
|
||||
class TestLaunchctlBootstrapEioRetry:
|
||||
"""`_launchctl_bootstrap` must recover from a stale already-loaded label.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user