Skip to content

The agent that grows with you

fix(matrix): scope MATRIX_RECOVERY_KEY_OUTPUT_FILE under multiplex profiles

EloquentBrush0x3ef1c4200bdb
diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py
index ee3faaf72dc..69225c8f50d 100644
--- a/plugins/platforms/matrix/adapter.py
+++ b/plugins/platforms/matrix/adapter.py
@@ -482,7 +482,9 @@ def _extra_csv_set(config, key: str, env_name: str) -> Set[str]:
def _recovery_key_output_path() -> Optional[Path]:
- output_file = os.getenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", "").strip()
+ """MATRIX_RECOVERY_KEY_OUTPUT_FILE via the profile-scoped reader: a bare os.getenv under
+ multiplex resolves the default profile's path, writing/finding the wrong profile's file."""
+ output_file = _get_scoped_secret("MATRIX_RECOVERY_KEY_OUTPUT_FILE", "").strip()
return Path(output_file).expanduser() if output_file else None
diff --git a/tests/gateway/test_matrix_recovery_key_scope.py b/tests/gateway/test_matrix_recovery_key_scope.py
index 13478f0ef05..3e7f08f0f97 100644
--- a/tests/gateway/test_matrix_recovery_key_scope.py
+++ b/tests/gateway/test_matrix_recovery_key_scope.py
@@ -7,11 +7,17 @@ The fix routes the recovery-key read through ``_scoped_recovery_key()``,
which uses :func:`agent.secret_scope.get_secret` (scope-aware) and only falls
back to ``os.getenv`` for an *unscoped* read under multiplex — mirroring the
established Slack app-token pattern (#59739).
+
+``MATRIX_RECOVERY_KEY_OUTPUT_FILE`` is the sibling of the recovery key itself
+(read by ``_recovery_key_output_path()``) and was missed by the #69090 fix:
+it still used a bare ``os.getenv``, so a secondary profile's freshly
+bootstrapped recovery key would either not be written at all, or be written
+to the default profile's configured path.
"""
import pytest
from agent import secret_scope as ss
-from plugins.platforms.matrix.adapter import _scoped_recovery_key
+from plugins.platforms.matrix.adapter import _recovery_key_output_path, _scoped_recovery_key
@pytest.fixture(autouse=True)
@@ -77,3 +83,53 @@ class TestScopedRecoveryKey:
def test_unset_returns_empty(self, monkeypatch):
monkeypatch.delenv("MATRIX_RECOVERY_KEY", raising=False)
assert _scoped_recovery_key() == ""
+
+
+class TestScopedRecoveryKeyOutputPath:
+ def test_multiplex_inactive_reads_environ(self, monkeypatch, tmp_path):
+ default_path = tmp_path / "default-profile-key.txt"
+ monkeypatch.setenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", str(default_path))
+ assert _recovery_key_output_path() == default_path
+
+ def test_multiplex_active_scoped_uses_scope_not_environ(self, monkeypatch, tmp_path):
+ """Secondary profile under multiplex must resolve its own output path.
+
+ A bare ``os.getenv`` would have returned the default profile's path
+ (from os.environ), writing the secondary profile's freshly bootstrapped
+ recovery key to the wrong profile's file.
+ """
+ default_path = tmp_path / "default-profile-key.txt"
+ secondary_path = tmp_path / "secondary-profile-key.txt"
+ monkeypatch.setenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", str(default_path))
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope(
+ {"MATRIX_RECOVERY_KEY_OUTPUT_FILE": str(secondary_path)}
+ )
+ try:
+ assert _recovery_key_output_path() == secondary_path
+ finally:
+ ss.reset_secret_scope(token)
+
+ def test_multiplex_active_unscoped_falls_back_to_environ(self, monkeypatch, tmp_path):
+ """Default-profile startup loop under multiplex: unscoped read is fine."""
+ default_path = tmp_path / "default-profile-key.txt"
+ monkeypatch.setenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", str(default_path))
+ ss.set_multiplex_active(True)
+ assert _recovery_key_output_path() == default_path
+
+ def test_multiplex_active_scoped_missing_key_is_none(self, monkeypatch, tmp_path):
+ """A scope without the setting must NOT fall through to another
+ profile's env — the secondary profile's key silently goes unwritten
+ instead of landing in the default profile's file."""
+ default_path = tmp_path / "default-profile-key.txt"
+ monkeypatch.setenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", str(default_path))
+ ss.set_multiplex_active(True)
+ token = ss.set_secret_scope({"SOME_OTHER_KEY": "x"})
+ try:
+ assert _recovery_key_output_path() is None
+ finally:
+ ss.reset_secret_scope(token)
+
+ def test_unset_returns_none(self, monkeypatch):
+ monkeypatch.delenv("MATRIX_RECOVERY_KEY_OUTPUT_FILE", raising=False)
+ assert _recovery_key_output_path() is None