Skip to content

The agent that grows with you

fix(raft): scope RAFT_PROFILE resolution to the active multiplex profile

nftpoetrist504dd1f24f6a
diff --git a/plugins/platforms/raft/adapter.py b/plugins/platforms/raft/adapter.py
index d31ee4601a4..49f9224675c 100644
--- a/plugins/platforms/raft/adapter.py
+++ b/plugins/platforms/raft/adapter.py
@@ -97,6 +97,52 @@ _RAFT_TURN_IDS: set[str] = set()
_RAFT_PROMPT_TURN_IDS: set[str] = set()
+def _profile_scoped() -> bool:
+ """True when running inside a multiplexed secondary profile's scope.
+
+ Secondary-profile adapters are constructed, connected, and reloaded
+ inside ``_profile_runtime_scope`` (secret scope installed + multiplex
+ active) — the same discriminator the Buzz/SimpleX adapters use for this
+ bug class (#98738). The DEFAULT profile under multiplexing runs
+ unscoped: ``os.environ`` holds its own bridge output there and keeps its
+ legacy precedence.
+ """
+ try:
+ from agent.secret_scope import current_secret_scope, is_multiplex_active
+
+ return bool(is_multiplex_active() and current_secret_scope() is not None)
+ except Exception:
+ return False
+
+
+def _resolve_raft_profile() -> str:
+ """Scope-aware resolution of the ``RAFT_PROFILE`` slug.
+
+ Raft has no ``config.yaml`` equivalent for this value (env-only), so a
+ secondary multiplex profile's only way to configure Raft is via its own
+ ``.env`` file — which the installed secret scope (built from that
+ profile's ``.env`` by ``_profile_runtime_scope``) already carries.
+ Reading raw ``os.environ.get("RAFT_PROFILE")`` here would instead return
+ the DEFAULT profile's bridged value, misdirecting the bridge subprocess
+ or CLI hint at another profile's external Raft workspace/agent identity.
+
+ ``get_secret()`` is only called when ``_profile_scoped()`` is True — the
+ callers of this helper (``connect()``/``register()``) run inside
+ ``_profile_runtime_scope`` for secondary profiles, but the DEFAULT
+ profile's own startup path never installs a scope, where ``get_secret()``
+ would raise ``UnscopedSecretError``; the guard keeps that path on the
+ unchanged ``os.environ`` read.
+ """
+ if _profile_scoped():
+ try:
+ from agent.secret_scope import get_secret
+
+ return (get_secret("RAFT_PROFILE") or "").strip()
+ except Exception:
+ return ""
+ return os.environ.get("RAFT_PROFILE", "").strip()
+
+
def check_raft_requirements() -> bool:
"""Check if Raft channel dependencies are available.
@@ -533,7 +579,7 @@ class RaftAdapter(BasePlatformAdapter):
logger.warning("[raft] raft CLI not found in PATH; bridge not spawned — wake-only polling mode")
return
- profile = os.environ.get("RAFT_PROFILE", "")
+ profile = _resolve_raft_profile()
if not profile:
logger.warning("[raft] RAFT_PROFILE not set; bridge not spawned")
return
@@ -777,8 +823,12 @@ def _env_enablement() -> Optional[dict]:
"""Seed PlatformConfig.extra from env vars during gateway config load.
Auto-enables when RAFT_PROFILE is set (the adapter needs it anyway).
+ Scope-aware: consults the active profile's own RAFT_PROFILE (env, or a
+ secondary profile's own .env via the secret scope) instead of the
+ default profile's bridged env value (mirrors the Buzz/SimpleX fix for
+ #98738) — see ``_resolve_raft_profile``.
"""
- if not os.getenv("RAFT_PROFILE"):
+ if not _resolve_raft_profile():
return None
return {"enabled": True}
@@ -839,12 +889,18 @@ def register(ctx) -> None:
setup_fn=interactive_setup,
env_enablement_fn=_env_enablement,
emoji="🔔",
+ # Scope-aware (mirrors _resolve_raft_profile's docstring): register()
+ # runs inside _profile_runtime_scope for a secondary multiplex
+ # profile (via discover_plugins() in
+ # gateway/run.py::_start_one_profile_adapters), so this resolves
+ # that profile's own RAFT_PROFILE instead of the default profile's
+ # bridged env value baked into a shared registry entry.
platform_hint=(
"You are connected to Raft via an external-agent channel. "
"Run `raft --profile {profile} profile show` to confirm which agent profile is active. "
"Run `raft --profile {profile} manual get raft-cli-overview` to learn available Raft commands. "
"Always pass `--profile {profile}` to every raft CLI call."
- ).format(profile=os.environ.get("RAFT_PROFILE", "your-agent-profile")),
+ ).format(profile=_resolve_raft_profile() or "your-agent-profile"),
)
ctx.register_hook("on_session_start", _on_session_start)
ctx.register_hook("pre_llm_call", _on_pre_llm_call)
diff --git a/tests/gateway/test_raft_adapter.py b/tests/gateway/test_raft_adapter.py
index 34a739f6e2f..552c0da2911 100644
--- a/tests/gateway/test_raft_adapter.py
+++ b/tests/gateway/test_raft_adapter.py
@@ -3,6 +3,7 @@
import asyncio
import json
import os
+from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
@@ -218,3 +219,104 @@ class TestRaftConfig:
assert os.environ["RAFT_PROFILE"] == "existing"
assert "Keeping RAFT_PROFILE=existing" in capsys.readouterr().out
+
+# ---------------------------------------------------------------------------
+# Multiplex secondary-profile scope (RAFT_PROFILE resolution)
+# ---------------------------------------------------------------------------
+#
+# _spawn_bridge, _env_enablement, and register()'s platform_hint all
+# previously read RAFT_PROFILE via raw os.environ.get unconditionally. Under
+# a multiplexed secondary profile, os.environ holds the DEFAULT profile's
+# YAML-to-env bridge output — a secondary profile with its own RAFT_PROFILE
+# (set only in its own .env, resolved via the installed secret scope) would
+# silently connect the bridge subprocess / CLI hint to the default profile's
+# external Raft workspace/agent identity instead of its own. Mirrors the
+# Buzz/SimpleX fix for #98738.
+
+def multiplex_scope():
+ """Install multiplex + a secondary-profile secret scope; restore after."""
+ tokens = []
+
+ def install(scope=None):
+ from agent.secret_scope import set_multiplex_active, set_secret_scope
+
+ set_multiplex_active(True)
+ tokens.append(set_secret_scope(scope or {}))
+ return tokens[-1]
+
+ yield install
+
+ from agent.secret_scope import reset_secret_scope, set_multiplex_active
+
+ for token in reversed(tokens):
+ reset_secret_scope(token)
+ set_multiplex_active(False)
+
+
+def default_profile_env(monkeypatch):
+ """The default profile's YAML-to-env bridge output in os.environ."""
+ monkeypatch.setenv("RAFT_PROFILE", "default-profile-slug")
+
+
+class _FakeCtx:
+ """Minimal ``ctx`` capturing ``register_platform``'s kwargs."""
+
+ def __init__(self):
+ self.platform_kwargs = None
+
+ def register_platform(self, **kwargs):
+ self.platform_kwargs = kwargs
+
+ def register_hook(self, *args, **kwargs):
+ pass
+
+
+class TestMultiplexProfileScope:
+
+ def test_secondary_profile_uses_its_own_slug_and_never_borrows_default(
+ self, multiplex_scope, default_profile_env, monkeypatch
+ ):
+ """Bridge spawn, env-enablement and the register() hint all resolve
+ the secondary profile's own RAFT_PROFILE; with none of its own the
+ profile fails closed (bridge not spawned, not auto-enabled)."""
+ import plugins.platforms.raft.adapter as raft_mod
+
+ monkeypatch.setattr(raft_mod.shutil, "which", lambda name: "/usr/bin/raft")
+ spawned = []
+ monkeypatch.setattr(
+ raft_mod.subprocess, "Popen",
+ lambda cmd, **kwargs: spawned.append(cmd) or SimpleNamespace(pid=1),
+ )
+
+ multiplex_scope({"RAFT_PROFILE": "secondary-profile-slug"})
+ _make_adapter()._spawn_bridge(9999)
+ assert spawned[-1][:3] == ["/usr/bin/raft", "--profile", "secondary-profile-slug"]
+ assert _env_enablement() == {"enabled": True}
+ ctx = _FakeCtx()
+ register(ctx)
+ assert "--profile secondary-profile-slug" in ctx.platform_kwargs["platform_hint"]
+ assert "default-profile-slug" not in ctx.platform_kwargs["platform_hint"]
+
+ spawned.clear()
+ multiplex_scope({})
+ _make_adapter()._spawn_bridge(9999)
+ assert spawned == []
+ assert _env_enablement() is None
+
+ def test_default_profile_unscoped_keeps_env_precedence(
+ self, monkeypatch, default_profile_env
+ ):
+ """Multiplex ON but no scope (the DEFAULT profile constructs
+ unscoped): env is its own bridge output and still wins."""
+ from agent.secret_scope import set_multiplex_active
+
+ set_multiplex_active(True)
+ try:
+ assert _env_enablement() == {"enabled": True}
+ ctx = _FakeCtx()
+ register(ctx)
+ assert "--profile default-profile-slug" in ctx.platform_kwargs["platform_hint"]
+ finally:
+ set_multiplex_active(False)