Under gateway.multiplex_profiles, os.environ holds the DEFAULT profile's .env; a
secondary profile's values exist only in the per-turn secret scope. Every reader
below still read os.environ/os.getenv at call time, so a secondary profile's turn
silently used the default profile's value.
Credentials (F6): FIRECRAWL_API_KEY (read_file hosted OCR), OPENVIKING_API_KEY,
mem0-OSS OPENAI_API_KEY, MODAL_TOKEN_ID/SECRET and BROWSER_USE_API_KEY presence
gates, and the xAI video plugin's os.getenv("XAI_API_KEY") fallback AFTER the
scoped resolver had already missed — the exact fallback-after-miss shape
gateway/AGENTS.md forbids. Deleted, not re-scoped: the resolver is the scope.
Identity / tenant (F7): MEM0_USER_ID/AGENT_ID/HOST/MODE, SUPERMEMORY_CONTAINER_TAG,
RETAINDB_PROJECT, OPENVIKING_ACCOUNT/USER/AGENT (and the whole layered() env
read), HINDSIGHT_BANK_ID/MODE/retain shaping, HERMES_HONCHO_HOST. A raw read
put a secondary profile's memories into the default profile's account/bank/
project/tenant and recalled them back into the default's turns. Each now uses
get_secret with the provider's own per-profile default on a miss.
Endpoints (F8): OPENAI_BASE_URL (aux custom runtime + direct-alias expansion),
XAI_BASE_URL/HERMES_XAI_BASE_URL (aux OAuth), NOUS_INFERENCE_BASE_URL (#65941,
both the aux builder and hermes_cli.auth_nous._nous_inference_env_override),
GATEWAY_PROXY_URL (same UnscopedSecretError-only fallback shape as
GATEWAY_PROXY_KEY three lines below), FIRECRAWL_API_URL, BROWSERBASE_BASE_URL,
SUPERMEMORY/RETAINDB/HONCHO/HINDSIGHT URLs. The keys beside them were already
scoped, so a secondary's key was sent to the default profile's proxy or host.
Targets / display (F11): WEIXIN_HOME_CHANNEL (message posted into the default's
chat), HERMES_LANGUAGE, and agent/i18n's process-wide lru_cache of
display.language — now keyed by HERMES_HOME.
Outbound webhooks: hooks.outbound[].secret_env resolved from os.environ while
the gateway registers each profile's targets inside that profile's scope, so a
secondary's deliveries were signed with the default's secret or left unsigned.
Agent-cache eviction: _spawn_release_thread started a bare threading.Thread, so
commit_memory_session -> provider on_session_end ran with an EMPTY context. The
thread now runs copy_context() and, for the unscoped housekeeping sweep, enters
the owning profile's _profile_runtime_scope resolved from the session key
(agent:<profile>:...). The pressure batch does the same per key.
session_search (#82903): agent/inline_tool_executors.py::_session_search
forwarded every schema argument except `profile`, so a gateway agent could
never select a named profile's store. Forwarded; the ownership-scoping design
in #87779/#87847 is a separate design call and is not attempted here.
Live repro (/tmp/mux_audit/fix-tool-memory-reads/repro.py): 28 FAIL on
origin/main -> 0 FAIL with this change; 10 new invariant tests red on base.
Fixes #82903
Fixes #65941
Fixes #99121
Addresses #87779
Co-authored-by: webtecnica <75556242+webtecnica@users.noreply.github.com>
Co-authored-by: Michael Versluis (Berry) <michael@wve.nl>
66 lines
3.2 KiB
Python
66 lines
3.2 KiB
Python
"""Multiplex invariant: a scoped API key is never paired with the DEFAULT profile's base URL / proxy.
|
|
|
|
`agent/auxiliary_client.py` already scopes provider keys via ``_scoped_key_env``; the base URLs beside
|
|
them (OPENAI_BASE_URL, XAI_BASE_URL, NOUS_INFERENCE_BASE_URL) and the gateway proxy URL / browser
|
|
provider endpoints must follow the same rule, or a secondary's key is sent to another profile's host.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from agent import secret_scope
|
|
|
|
|
|
@pytest.fixture
|
|
def secondary_scope(monkeypatch):
|
|
for name in ("OPENAI_BASE_URL", "XAI_BASE_URL", "HERMES_XAI_BASE_URL", "NOUS_INFERENCE_BASE_URL",
|
|
"GATEWAY_PROXY_URL", "FIRECRAWL_API_URL", "BROWSERBASE_BASE_URL", "XAI_API_KEY"):
|
|
monkeypatch.setenv(name, f"https://{name.lower()}.default.example/v1")
|
|
secret_scope.set_multiplex_active(True)
|
|
token = secret_scope.set_secret_scope({"BROWSERBASE_API_KEY": "bb-b", "BROWSERBASE_PROJECT_ID": "pid-b"})
|
|
try:
|
|
yield
|
|
finally:
|
|
secret_scope.reset_secret_scope(token)
|
|
secret_scope.set_multiplex_active(False)
|
|
|
|
|
|
def test_base_urls_follow_the_scoped_key_not_default_environ(monkeypatch, secondary_scope, tmp_path):
|
|
import agent.auxiliary_client as aux
|
|
import plugins.browser.browserbase.provider as browserbase
|
|
import plugins.browser.firecrawl.provider as firecrawl
|
|
import plugins.video_gen.xai as xai_video
|
|
from gateway.run_turn import GatewayTurnMixin
|
|
from hermes_cli import auth_nous
|
|
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
monkeypatch.setattr(aux, "_get_named_custom_provider", lambda name: None, raising=False)
|
|
|
|
_, base = aux._expand_direct_api_alias("openai", None)
|
|
assert "default.example" not in (base or "")
|
|
assert aux._scoped_key_env("OPENAI_BASE_URL") == ""
|
|
assert auth_nous._nous_inference_env_override() is None
|
|
monkeypatch.setattr("gateway.run._load_gateway_config", lambda: {})
|
|
assert GatewayTurnMixin._get_proxy_url(GatewayTurnMixin.__new__(GatewayTurnMixin)) is None
|
|
assert "default.example" not in browserbase.BrowserbaseBrowserProvider()._get_config_or_none()["base_url"]
|
|
assert "default.example" not in firecrawl.FirecrawlBrowserProvider()._api_url()
|
|
# xAI video: a scoped miss stays a miss — no environ fallback-after-miss for the key or the URL.
|
|
api_key, base_url = xai_video._resolve_xai_credentials()
|
|
assert api_key == "" and "default.example" not in base_url
|
|
|
|
|
|
def test_unscoped_single_profile_reads_keep_environ(monkeypatch):
|
|
"""Multiplex OFF (CLI / single gateway): environ IS the profile's own value — behaviour unchanged."""
|
|
from gateway.run_turn import GatewayTurnMixin
|
|
from hermes_cli import auth_nous
|
|
|
|
secret_scope.set_multiplex_active(False)
|
|
token = secret_scope.set_secret_scope(None)
|
|
try:
|
|
monkeypatch.setenv("GATEWAY_PROXY_URL", "https://proxy.mine.example/")
|
|
monkeypatch.setenv("NOUS_INFERENCE_BASE_URL", "https://nous.mine.example/v1/")
|
|
assert GatewayTurnMixin._get_proxy_url(GatewayTurnMixin.__new__(GatewayTurnMixin)) == "https://proxy.mine.example"
|
|
assert auth_nous._nous_inference_env_override() == "https://nous.mine.example/v1"
|
|
finally:
|
|
secret_scope.reset_secret_scope(token)
|