|
97 | 97 | _RAFT_PROMPT_TURN_IDS: set[str] = set() |
98 | 98 |
|
99 | 99 |
|
| 100 | +def _profile_scoped() -> bool: |
| 101 | + """True when running inside a multiplexed secondary profile's scope. |
| 102 | +
|
| 103 | + Secondary-profile adapters are constructed, connected, and reloaded |
| 104 | + inside ``_profile_runtime_scope`` (secret scope installed + multiplex |
| 105 | + active) — the same discriminator the Buzz/SimpleX adapters use for this |
| 106 | + bug class (#98738). The DEFAULT profile under multiplexing runs |
| 107 | + unscoped: ``os.environ`` holds its own bridge output there and keeps its |
| 108 | + legacy precedence. |
| 109 | + """ |
| 110 | + try: |
| 111 | + from agent.secret_scope import current_secret_scope, is_multiplex_active |
| 112 | + |
| 113 | + return bool(is_multiplex_active() and current_secret_scope() is not None) |
| 114 | + except Exception: |
| 115 | + return False |
| 116 | + |
| 117 | + |
| 118 | +def _resolve_raft_profile() -> str: |
| 119 | + """Scope-aware resolution of the ``RAFT_PROFILE`` slug. |
| 120 | +
|
| 121 | + Raft has no ``config.yaml`` equivalent for this value (env-only), so a |
| 122 | + secondary multiplex profile's only way to configure Raft is via its own |
| 123 | + ``.env`` file — which the installed secret scope (built from that |
| 124 | + profile's ``.env`` by ``_profile_runtime_scope``) already carries. |
| 125 | + Reading raw ``os.environ.get("RAFT_PROFILE")`` here would instead return |
| 126 | + the DEFAULT profile's bridged value, misdirecting the bridge subprocess |
| 127 | + or CLI hint at another profile's external Raft workspace/agent identity. |
| 128 | +
|
| 129 | + ``get_secret()`` is only called when ``_profile_scoped()`` is True — the |
| 130 | + callers of this helper (``connect()``/``register()``) run inside |
| 131 | + ``_profile_runtime_scope`` for secondary profiles, but the DEFAULT |
| 132 | + profile's own startup path never installs a scope, where ``get_secret()`` |
| 133 | + would raise ``UnscopedSecretError``; the guard keeps that path on the |
| 134 | + unchanged ``os.environ`` read. |
| 135 | + """ |
| 136 | + if _profile_scoped(): |
| 137 | + try: |
| 138 | + from agent.secret_scope import get_secret |
| 139 | + |
| 140 | + return (get_secret("RAFT_PROFILE") or "").strip() |
| 141 | + except Exception: |
| 142 | + return "" |
| 143 | + return os.environ.get("RAFT_PROFILE", "").strip() |
| 144 | + |
| 145 | + |
100 | 146 | def check_raft_requirements() -> bool: |
101 | 147 | """Check if Raft channel dependencies are available. |
102 | 148 |
|
@@ -533,7 +579,7 @@ def _spawn_bridge(self, port: int) -> None: |
533 | 579 | logger.warning("[raft] raft CLI not found in PATH; bridge not spawned — wake-only polling mode") |
534 | 580 | return |
535 | 581 |
|
536 | | - profile = os.environ.get("RAFT_PROFILE", "") |
| 582 | + profile = _resolve_raft_profile() |
537 | 583 | if not profile: |
538 | 584 | logger.warning("[raft] RAFT_PROFILE not set; bridge not spawned") |
539 | 585 | return |
@@ -777,8 +823,12 @@ def _env_enablement() -> Optional[dict]: |
777 | 823 | """Seed PlatformConfig.extra from env vars during gateway config load. |
778 | 824 |
|
779 | 825 | Auto-enables when RAFT_PROFILE is set (the adapter needs it anyway). |
| 826 | + Scope-aware: consults the active profile's own RAFT_PROFILE (env, or a |
| 827 | + secondary profile's own .env via the secret scope) instead of the |
| 828 | + default profile's bridged env value (mirrors the Buzz/SimpleX fix for |
| 829 | + #98738) — see ``_resolve_raft_profile``. |
780 | 830 | """ |
781 | | - if not os.getenv("RAFT_PROFILE"): |
| 831 | + if not _resolve_raft_profile(): |
782 | 832 | return None |
783 | 833 |
|
784 | 834 | return {"enabled": True} |
@@ -839,12 +889,18 @@ def register(ctx) -> None: |
839 | 889 | setup_fn=interactive_setup, |
840 | 890 | env_enablement_fn=_env_enablement, |
841 | 891 | emoji="🔔", |
| 892 | + # Scope-aware (mirrors _resolve_raft_profile's docstring): register() |
| 893 | + # runs inside _profile_runtime_scope for a secondary multiplex |
| 894 | + # profile (via discover_plugins() in |
| 895 | + # gateway/run.py::_start_one_profile_adapters), so this resolves |
| 896 | + # that profile's own RAFT_PROFILE instead of the default profile's |
| 897 | + # bridged env value baked into a shared registry entry. |
842 | 898 | platform_hint=( |
843 | 899 | "You are connected to Raft via an external-agent channel. " |
844 | 900 | "Run `raft --profile {profile} profile show` to confirm which agent profile is active. " |
845 | 901 | "Run `raft --profile {profile} manual get raft-cli-overview` to learn available Raft commands. " |
846 | 902 | "Always pass `--profile {profile}` to every raft CLI call." |
847 | | - ).format(profile=os.environ.get("RAFT_PROFILE", "your-agent-profile")), |
| 903 | + ).format(profile=_resolve_raft_profile() or "your-agent-profile"), |
848 | 904 | ) |
849 | 905 | ctx.register_hook("on_session_start", _on_session_start) |
850 | 906 | ctx.register_hook("pre_llm_call", _on_pre_llm_call) |
|
0 commit comments