AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
perf(backend/copilot): read the three chat-rule keys in one pipelined MGET (#14941)
Reinier van der Leer877ff2b073d4
diff --git a/autogpt_platform/backend/backend/copilot/gate/chat_rules.py b/autogpt_platform/backend/backend/copilot/gate/chat_rules.pyindex 63936af622..85f7574fd8 100644--- a/autogpt_platform/backend/backend/copilot/gate/chat_rules.py+++ b/autogpt_platform/backend/backend/copilot/gate/chat_rules.py@@ -23,6 +23,7 @@ logger = logging.getLogger(__name__)ChatRule = Literal["allow", "judge", "ask"]_RULES: dict[str, ChatRule] = {rule: rule for rule in get_args(ChatRule)}Scope = Literal["chat", "expert", "team"]+_WIDER: tuple[Literal["expert", "team"], ...] = ("expert", "team")# Outlives any chat anyone is still in; dead chats expire out of Redis._TTL_SECONDS = 90 * 24 * 60 * 60@@ -68,7 +69,7 @@ async def set_ask(A rejection also revokes every wider rule that would have run the call."""await set_rule(session_id, rule_key, "ask")- for scope in ("expert", "team"):+ for scope in _WIDER:key = _scoped_key(scope, user_id, expert_id, rule_key)try:redis = await get_redis_async()@@ -145,12 +146,18 @@ async def rule_for(team's. ``unreadable`` asks like ``ask`` but must not claim the user said no."""try:redis = await get_redis_async()- raw = await redis.get(_key(session_id, rule_key))- if raw is not None:+ # The keys sit in different cluster slots, so a plain MGET is refused.+ chat, *wider = await redis.mget_nonatomic(+ [+ _key(session_id, rule_key),+ _scoped_key("expert", user_id, expert_id, rule_key),+ _scoped_key("team", user_id, expert_id, rule_key),+ ]+ )+ if chat is not None:# Rows written before rules had decisions hold "1", which meant ask.- return RuleHit(rule=_RULES.get(_text(raw), "ask"))- for scope in ("expert", "team"):- raw = await redis.get(_scoped_key(scope, user_id, expert_id, rule_key))+ return RuleHit(rule=_RULES.get(_text(chat), "ask"))+ for scope, raw in zip(_WIDER, wider):if raw is not None:rule, _, since = _text(raw).partition(" ")return RuleHit(diff --git a/autogpt_platform/backend/backend/copilot/gate/chat_rules_test.py b/autogpt_platform/backend/backend/copilot/gate/chat_rules_test.pyindex 7d7a7ed1da..9a76e70607 100644--- a/autogpt_platform/backend/backend/copilot/gate/chat_rules_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/chat_rules_test.py@@ -1,3 +1,4 @@+import uuidfrom datetime import datefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, patch@@ -6,6 +7,8 @@ import pytestfrom prisma.enums import ReviewStatusfrom backend.copilot.gate import chat_rules, held+from backend.data import redis_client+from backend.util.testing import is_tcp_port_reachableclass _Redis:@@ -18,6 +21,9 @@ class _Redis:async def get(self, key: str) -> str | None:return self.data.get(key)+ async def mget_nonatomic(self, keys: list[str]) -> list[str | None]:+ return [self.data.get(key) for key in keys]+@pytest.fixturedef redis():@@ -106,6 +112,49 @@ async def test_a_rejection_writes_no_wider_rule_where_there_was_none(redis):assert await _rule("other-chat", "mcp:h/t", "frankie") is None+async def test_every_level_is_read_in_one_call(redis):+ """The lookup sits on every gated call, so it must not pay a round trip per level."""+ await _set("team", "allow")+ reads = AsyncMock(side_effect=redis.mget_nonatomic)+ with (+ patch.object(redis, "mget_nonatomic", reads),+ patch.object(redis, "get", AsyncMock(side_effect=AssertionError)),+ ):+ assert await _rule("s", "mcp:h/t", "frankie") == "allow"+ reads.assert_awaited_once()+++ not is_tcp_port_reachable(redis_client.HOST, redis_client.PORT),+ reason="local redis cluster not reachable",+)+async def test_the_narrowest_rule_decides_on_a_real_cluster():+ """The three keys hash to different slots, which only a real cluster refuses+ to read together."""+ user, chat, key = (f"u-{uuid.uuid4()}", f"s-{uuid.uuid4()}", "mcp:h/t")+ keys = [+ chat_rules._key(chat, key),+ chat_rules._scoped_key("expert", user, "frankie", key),+ chat_rules._scoped_key("team", user, None, key),+ ]+ try:+ await chat_rules.set_scoped_rule("team", user, None, key, "judge")+ hit = await chat_rules.rule_for(chat, key, user, "frankie")+ assert hit is not None and (hit.rule, hit.scope) == ("judge", "team")++ await chat_rules.set_scoped_rule("expert", user, "frankie", key, "allow")+ hit = await chat_rules.rule_for(chat, key, user, "frankie")+ assert hit is not None and (hit.rule, hit.scope) == ("allow", "expert")++ await chat_rules.set_rule(chat, key, "ask")+ hit = await chat_rules.rule_for(chat, key, user, "frankie")+ assert hit is not None and (hit.rule, hit.scope) == ("ask", "chat")+ finally:+ redis = await redis_client.get_redis_async()+ for k in keys:+ await redis.delete(k)++@pytest.mark.parametrize("scope, otto, reason",[diff --git a/autogpt_platform/backend/backend/copilot/tools/gate_mcp_test.py b/autogpt_platform/backend/backend/copilot/tools/gate_mcp_test.pyindex 869e3f1a30..8bf57521a5 100644--- a/autogpt_platform/backend/backend/copilot/tools/gate_mcp_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/gate_mcp_test.py@@ -58,6 +58,9 @@ class _Redis:async def get(self, key: str) -> str | None:return self.data.get(key)+ async def mget_nonatomic(self, keys: list[str]) -> list[str | None]:+ return [self.data.get(key) for key in keys]+@pytest.fixturedef gate():