fix(gateway): a reload under a bound owner scope is provisional

An edit to the launch profile's config first seen from a routed profile's
task reloaded under the launch home's bound scope, which skips the YAML env
bridge: the snapshot rebuilt the pre-edit lists, yet the edited version was
recorded, so the owner's own next event never reloaded. A reload under a
bound scope now updates the gates without acknowledging the version.

Adds a test for the owner-scope bind's key path (owner edit first seen
under a routed scope whose home has conflicting YAML and .env lists).

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
This commit is contained in:
Teknium
2026-11-13 03:46:03 -08:00
parent 9fb6aef518
commit 7af249ff94
2 changed files with 63 additions and 7 deletions

View File

@@ -372,13 +372,13 @@ def _owner_scope(home: str):
from hermes_constants import get_hermes_home
if str(get_hermes_home()) == home:
yield
yield True
return
from pathlib import Path
from gateway.run import _profile_runtime_scope
with _profile_runtime_scope(Path(home)):
yield
yield False
def refresh_live_gate_extra(adapter: Any, keys: Iterable[str], on_reload: Optional[Callable[[], None]] = None) -> bool:
@@ -400,12 +400,12 @@ def refresh_live_gate_extra(adapter: Any, keys: Iterable[str], on_reload: Option
with _LIVE_GATE_LOCK:
if version == adapter._live_gate_version:
return False
with _owner_scope(home):
return _reload_live_gate_extra(adapter, keys, version, on_reload)
with _owner_scope(home) as own_scope:
return _reload_live_gate_extra(adapter, keys, version, on_reload, acknowledge=own_scope)
def _reload_live_gate_extra(adapter: Any, keys: Iterable[str], version: tuple,
on_reload: Optional[Callable[[], None]]) -> bool:
on_reload: Optional[Callable[[], None]], acknowledge: bool = True) -> bool:
from gateway.config import PlatformConfig, load_gateway_config
try: # ONE checked load: the fallback env-only config would drop the YAML-held lists
loaded = load_gateway_config(strict=True)
@@ -424,8 +424,11 @@ def _reload_live_gate_extra(adapter: Any, keys: Iterable[str], version: tuple,
extra.pop(key, None)
if on_reload is not None:
on_reload()
# Retry this same edit after a transient read failure, without requiring a second write.
adapter._live_gate_version = version
# Retry this same edit after a transient read failure, without requiring a second write. A reload under
# a bound scope is provisional: the launch profile's bound scope skips its YAML env bridge, so the
# snapshot can still hold the pre-edit values; the owner's own next event redoes it.
if acknowledge:
adapter._live_gate_version = version
return True

View File

@@ -403,3 +403,56 @@ def test_a_resolved_username_survives_a_second_on_ready_and_a_later_edit(_home):
_write(_home, {"allow_from": ["alice"], "allowed_channels": ["555"]}) # an unrelated edit
assert adapter._is_allowed_user("111", alice, guild=guild, is_dm=False) is True
assert adapter.resolved_allowlist_user_ids() == {"111"}
def test_an_owner_edit_first_seen_under_a_routed_scope_applies_the_owners_lists(tmp_path):
"""The owner's edit can reach the gate first from a routed profile's task (a /voice listen loop).
The reload and the env re-snapshot must both run as the owner: the routed home's lists and .env
never become this bot's gate."""
from agent.secret_scope import reset_multiplex_context, set_multiplex_context
from gateway.run import _profile_runtime_scope
owner, routed = tmp_path / "a", tmp_path / "b"
owner.mkdir()
routed.mkdir()
(routed / ".env").write_text("DISCORD_ALLOWED_USERS=9999\n", encoding="utf-8")
_write(owner, {"allowed_roles": ["2"]})
_write(routed, {"allowed_roles": ["7"]})
token = set_multiplex_context(True)
try:
with _profile_runtime_scope(owner):
adapter, guild, member = _connected_discord(roles=(5,))
_write(owner, {"allowed_roles": ["2", "5"]})
stranger = SimpleNamespace(id=9999, roles=[SimpleNamespace(id=7)], guild=guild)
with _profile_runtime_scope(routed):
assert _allowed(adapter, guild, member) is True
assert adapter._is_allowed_user("9999", stranger, guild=guild, is_dm=False) is False
with _profile_runtime_scope(owner):
assert adapter._is_allowed_user("9999", stranger, guild=guild, is_dm=False) is False
finally:
reset_multiplex_context(token)
def test_a_launch_profile_edit_first_seen_under_a_routed_scope_still_applies(tmp_path, monkeypatch):
"""The launch profile's YAML lists live in the process env (the single-profile bridge). Its bound
scope skips that bridge, so a reload there can only be provisional: the owner's next own event
must still apply the edit instead of finding it already acknowledged."""
from agent.secret_scope import reset_multiplex_context, set_multiplex_context
from gateway.run import _profile_runtime_scope
default, routed = tmp_path / "default", tmp_path / "b"
default.mkdir()
routed.mkdir()
monkeypatch.setenv("HERMES_HOME", str(default))
_write(default, {"allowed_roles": ["2"]})
_write(routed, {"allowed_roles": ["7"]})
token = set_multiplex_context(True)
try:
adapter, guild, member = _connected_discord(roles=(9,))
assert _allowed(adapter, guild, member) is False
_write(default, {"allowed_roles": ["9"]})
with _profile_runtime_scope(routed):
_allowed(adapter, guild, member)
assert _allowed(adapter, guild, member) is True
finally:
reset_multiplex_context(token)