Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): publish mcp buffer teams from the tick and mask intents (#126918)

posthog[bot]6dd83c6836b9
diff --git a/products/signals/backend/activity_summary/config.py b/products/signals/backend/activity_summary/config.py
index 6ee0cab8e34f..561bc0b34b73 100644
--- a/products/signals/backend/activity_summary/config.py
+++ b/products/signals/backend/activity_summary/config.py
@@ -101,8 +101,11 @@ class ActivitySummaryConfig:
# The largest share of a tick that rebuilds of stale summaries can take. Other updates get the rest.
rebuild_budget_share: float = 0.3
mcp_buffer_enabled: bool = False
- # Limits the buffer to the enrolled teams of these organizations. Empty means every enrolled team.
+ # The buffer keeps the enrolled teams of these organizations and of the `mcp_buffer_percentage` rollout.
+ # Use `mcp_buffer_percentage: 100` for every enrolled team.
mcp_buffer_organization_ids: tuple[str, ...] = ()
+ # Uses the rollout buckets of `self_driving_organization_percentage`, so a higher percentage keeps the lower ones.
+ mcp_buffer_percentage: float = 0.0
# The buffer keeps the latest calls of each user, team and day. A separate counter keeps the full count.
mcp_calls_per_day: int = 50
# Must cover the longest gap between two updates of one summary.
@@ -246,6 +249,7 @@ def parse_config(payload: Any) -> ActivitySummaryConfig:
rebuild_budget_share=_fraction(payload.get("rebuild_budget_share"), defaults.rebuild_budget_share),
mcp_buffer_enabled=_bool(payload.get("mcp_buffer_enabled"), defaults.mcp_buffer_enabled),
mcp_buffer_organization_ids=_organization_ids(payload.get("mcp_buffer_organization_ids")),
+ mcp_buffer_percentage=_percentage(payload.get("mcp_buffer_percentage"), defaults.mcp_buffer_percentage),
mcp_calls_per_day=min(
_positive_int(payload.get("mcp_calls_per_day"), defaults.mcp_calls_per_day), MAX_MCP_CALLS_PER_DAY
),
diff --git a/products/signals/backend/activity_summary/mcp_buffer.py b/products/signals/backend/activity_summary/mcp_buffer.py
index 95d0311277fa..277159c45738 100644
--- a/products/signals/backend/activity_summary/mcp_buffer.py
+++ b/products/signals/backend/activity_summary/mcp_buffer.py
@@ -9,12 +9,21 @@ So the request thread does no network I/O: it only appends to a bounded in-proce
thread writes the queue to Redis about once a second, with short timeouts and a circuit breaker, and
refreshes the enrolled teams in the background.
+The hourly tick publishes the buffer's teams to `as:mcp:teams`, so web processes do not read the
+enrollment from Postgres. An enrollment change, such as a revoked AI data processing approval, reaches
+the buffer within one tick plus one refresh. `mcp_buffer_enabled: false` takes effect within one refresh.
+A missing key means no teams, so the buffer stops when the tick stops publishing.
+
+The intent is masked with `clean_text` before it goes to Redis.
+
Redis keys, each with a TTL of `mcp_buffer_days`:
- `as:mcp:{team_id}:{user_id}:{yyyymmdd}`: a list of the latest `mcp_calls_per_day` records.
- `as:mcp:n:{team_id}:{user_id}:{yyyymmdd}`: the count of all records of the day, trimmed or not.
- `as:mcp:dirty`: a set of `{team_id}:{user_id}` members that have new records.
+`as:mcp:teams` holds the comma-separated team ids, with a TTL of `TEAMS_TTL_SECONDS`.
+
A record is `ts|session_or_conversation_id|route_name|intent`, with `ts` in Unix seconds.
`inputs.user_mcp_records` reads the buffer through the key and record helpers of this module.
"""
@@ -37,7 +46,7 @@ from prometheus_client import Counter, Histogram
from posthog.dataclasses import frozen
from posthog.redis import get_client
-from products.signals.backend.activity_summary.render import MCP_INTENT_CHARS, McpItem
+from products.signals.backend.activity_summary.render import MCP_INTENT_CHARS, McpItem, clean_text
logger = structlog.get_logger(__name__)
@@ -50,7 +59,12 @@ CIRCUIT_OPEN_SECONDS = 30.0
SETTINGS_REFRESH_SECONDS = 300.0
# The default client timeouts can stall the flush thread for seconds.
REDIS_TIMEOUT_SECONDS = 0.05
+# The team list is read off the request thread, and it can hold many ids.
+TEAMS_REDIS_TIMEOUT_SECONDS = 1.0
DIRTY_KEY = "as:mcp:dirty"
+TEAMS_KEY = "as:mcp:teams"
+# Three hourly ticks, so one failed tick does not stop the buffer.
+TEAMS_TTL_SECONDS = 3 * 3600
RECORDS = Counter(
"activity_summary_mcp_intent_records",
@@ -62,6 +76,11 @@ FLUSH_SECONDS = Histogram(
"Wall-clock time of one Redis flush of the MCP intent buffer.",
buckets=(0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1),
)
+TEAMS_PUBLISHES = Counter(
+ "activity_summary_mcp_teams_publishes",
+ "Tick writes of the MCP intent buffer's team list by outcome: published, deleted or failed.",
+ ["outcome"],
+)
def records_key(team_id: int, user_id: int, day: str) -> str:
@@ -72,6 +91,22 @@ def count_key(team_id: int, user_id: int, day: str) -> str:
return f"as:mcp:n:{team_id}:{user_id}:{day}"
+def format_team_ids(team_ids: list[int]) -> str:
+ return ",".join(str(team_id) for team_id in sorted(team_ids))
+
+
+def parse_team_ids(value: bytes | str | None) -> frozenset[int]:
+ """The ids that `format_team_ids` wrote. A missing key or an invalid id gives no team."""
+ text = value.decode() if isinstance(value, bytes) else value
+ team_ids: set[int] = set()
+ for part in (text or "").split(","):
+ try:
+ team_ids.add(int(part))
+ except ValueError:
+ continue
+ return frozenset(team_ids)
+
+
def record_day(at: datetime.datetime) -> str:
"""The UTC day of a record, as its keys name it."""
return at.astimezone(datetime.UTC).strftime("%Y%m%d")
@@ -118,6 +153,11 @@ def _field(value: str | None, max_chars: int) -> str:
return " ".join((value or "").replace("|", " ").split())[:max_chars]
+def _intent(value: str) -> str:
+ # The header is bounded, and `clean_text` reads a bounded prefix, so this stays cheap on the request thread.
+ return clean_text(value.replace("|", " "), INTENT_MAX_CHARS)
+
+
def build_record(
*, team_id: int, user_id: int, session_id: str | None, route_name: str | None, intent: str, at: datetime.datetime
) -> McpIntentRecord:
@@ -126,35 +166,53 @@ def build_record(
str(int(at.timestamp())),
_field(session_id, _FIELD_MAX_CHARS),
_field(route_name, _FIELD_MAX_CHARS),
- _field(intent, INTENT_MAX_CHARS),
+ _intent(intent),
)
)
return McpIntentRecord(team_id=team_id, user_id=user_id, day=record_day(at), value=value)
-def load_settings() -> BufferSettings:
- """The enrolled teams and the buffer limits, from the `activity-summary` flag payload and Postgres."""
+def load_settings(redis_client: Callable[[], redis.Redis]) -> BufferSettings:
+ """The buffer's teams that the tick published, and the limits of the `activity-summary` flag payload.
+
+ Raises on a Redis error, so the caller keeps its previous settings."""
from products.signals.backend.activity_summary.config import ( # noqa: PLC0415 — keeps heavy deps off the middleware import path
ACTIVITY_SUMMARY_FLAG,
ACTIVITY_SUMMARY_FLAG_DISTINCT_ID,
parse_config,
)
- from products.signals.backend.activity_summary.queue import ( # noqa: PLC0415 — keeps heavy deps off the middleware import path
- mcp_buffer_team_ids,
- )
from products.signals.backend.scout_harness.team_limits import ( # noqa: PLC0415 — keeps heavy deps off the middleware import path
read_flag_payload,
)
# Not `read_config`, which logs on every read: each web process refreshes every few minutes.
config = parse_config(read_flag_payload(ACTIVITY_SUMMARY_FLAG, ACTIVITY_SUMMARY_FLAG_DISTINCT_ID))
+ team_ids = parse_team_ids(redis_client().get(TEAMS_KEY)) if config.mcp_buffer_enabled else frozenset()
return BufferSettings(
- team_ids=frozenset(mcp_buffer_team_ids(config)),
+ team_ids=team_ids,
calls_per_day=config.mcp_calls_per_day,
buffer_days=config.mcp_buffer_days,
)
+def publish_team_ids(team_ids: list[int] | None) -> None:
+ """Write the buffer's teams for the web processes, or delete the key when `team_ids` is None.
+
+ The tick calls this, so a failure logs and never raises."""
+ try:
+ client = _teams_redis_client()
+ if team_ids is None:
+ client.delete(TEAMS_KEY)
+ TEAMS_PUBLISHES.labels(outcome="deleted").inc()
+ return
+ client.set(TEAMS_KEY, format_team_ids(team_ids), ex=TEAMS_TTL_SECONDS)
+ except Exception:
+ TEAMS_PUBLISHES.labels(outcome="failed").inc()
+ logger.warning("activity_summary.mcp_buffer_teams_publish_failed", exc_info=True)
+ return
+ TEAMS_PUBLISHES.labels(outcome="published").inc()
+
+
def _redis_client() -> redis.Redis:
return get_client(
settings.ACTIVITY_SUMMARY_MCP_BUFFER_REDIS_URL or None,
@@ -163,17 +221,27 @@ def _redis_client() -> redis.Redis:
)
+def _teams_redis_client() -> redis.Redis:
+ return get_client(
+ settings.ACTIVITY_SUMMARY_MCP_BUFFER_REDIS_URL or None,
+ socket_timeout=TEAMS_REDIS_TIMEOUT_SECONDS,
+ socket_connect_timeout=TEAMS_REDIS_TIMEOUT_SECONDS,
+ )
+
+
class McpIntentBuffer:
def __init__(
self,
*,
queue_size: int = QUEUE_SIZE,
redis_client: Callable[[], redis.Redis] = _redis_client,
+ teams_redis_client: Callable[[], redis.Redis] = _teams_redis_client,
clock: Callable[[], float] = monotonic,
start_thread: bool = True,
) -> None:
self._queue_size = queue_size
self._redis_client = redis_client
+ self._teams_redis_client = teams_redis_client
self._clock = clock
self._start_thread = start_thread
self._lock = threading.Lock()
@@ -243,7 +311,7 @@ class McpIntentBuffer:
def refresh_settings(self) -> None:
try:
- self.buffer_settings = load_settings()
+ self.buffer_settings = load_settings(self._teams_redis_client)
except Exception:
logger.warning("activity_summary.mcp_buffer_settings_failed", exc_info=True)
diff --git a/products/signals/backend/activity_summary/queue.py b/products/signals/backend/activity_summary/queue.py
index 8af0b986c720..e1f1261a8c2b 100644
--- a/products/signals/backend/activity_summary/queue.py
+++ b/products/signals/backend/activity_summary/queue.py
@@ -209,9 +209,8 @@ def organization_rollout_bucket(organization_id: uuid.UUID) -> int:
return int.from_bytes(digest[:8], "big") % _ROLLOUT_BUCKETS
-def _in_rollout(config: ActivitySummaryConfig, organization_id: uuid.UUID) -> bool:
- threshold = config.self_driving_organization_percentage * _ROLLOUT_BUCKETS / 100
- return organization_rollout_bucket(organization_id) < threshold
+def _in_rollout(percentage: float, organization_id: uuid.UUID) -> bool:
+ return organization_rollout_bucket(organization_id) < percentage * _ROLLOUT_BUCKETS / 100
@frozen
@@ -220,6 +219,8 @@ class Enrollment:
organization_ids: list[uuid.UUID]
# The organizations that a 100 percent rollout enrolls, with or without manual enrollment.
qualifying_organization_ids: list[uuid.UUID]
+ # The organization of each enrolled team.
+ team_organization_ids: dict[int, uuid.UUID] = dataclasses.field(default_factory=dict)
def enrollment(config: ActivitySummaryConfig) -> Enrollment:
@@ -248,12 +249,13 @@ def enrollment(config: ActivitySummaryConfig) -> Enrollment:
enrolled |= {
team_id: organization_id
for team_id, organization_id in with_source.items()
- if _in_rollout(config, organization_id)
+ if _in_rollout(config.self_driving_organization_percentage, organization_id)
}
return Enrollment(
team_ids=sorted(enrolled),
organization_ids=sorted(set(enrolled.values())),
qualifying_organization_ids=sorted(set(with_source.values())),
+ team_organization_ids=enrolled,
)
@@ -261,17 +263,16 @@ def enrolled_team_ids(config: ActivitySummaryConfig) -> list[int]:
return enrollment(config).team_ids
-def mcp_buffer_team_ids(config: ActivitySummaryConfig) -> list[int]:
- """The enrolled teams whose MCP intents the buffer keeps."""
+def mcp_buffer_team_ids(config: ActivitySummaryConfig, enrolled: Enrollment) -> list[int]:
+ """The enrolled teams whose MCP intents the buffer keeps. The percentage never adds a team that is not enrolled."""
if not config.mcp_buffer_enabled:
return []
- if not config.mcp_buffer_organization_ids:
- return enrolled_team_ids(config)
- # Every web process refreshes this list, so check only the listed organizations' teams, not the full enrollment.
- teams = Team.objects.filter(
- organization_id__in=_parse_organization_ids(config.mcp_buffer_organization_ids)
- ).select_related("organization")
- return sorted(team.id for team in teams if is_team_enrolled_for_generation(config, team))
+ listed = _parse_organization_ids(config.mcp_buffer_organization_ids)
+ return sorted(
+ team_id
+ for team_id, organization_id in enrolled.team_organization_ids.items()
+ if organization_id in listed or _in_rollout(config.mcp_buffer_percentage, organization_id)
+ )
def is_team_enrolled_for_generation(config: ActivitySummaryConfig, team: Team) -> bool:
@@ -288,7 +289,7 @@ def is_team_enrolled_for_generation(config: ActivitySummaryConfig, team: Team) -
return False
if config.enroll_all_approved or team.organization_id in _parse_organization_ids(config.organization_ids):
return True
- if not _in_rollout(config, team.organization_id):
+ if not _in_rollout(config.self_driving_organization_percentage, team.organization_id):
return False
from products.signals.backend.facade.api import (
has_enabled_source, # noqa: PLC0415 — the facade imports this module through `read`
diff --git a/products/signals/backend/activity_summary/tick.py b/products/signals/backend/activity_summary/tick.py
index 33406c607cb8..af5d85b4c792 100644
--- a/products/signals/backend/activity_summary/tick.py
+++ b/products/signals/backend/activity_summary/tick.py
@@ -39,6 +39,7 @@ from products.signals.backend.activity_summary.logic import (
record_update_exception,
update_activity_summary,
)
+from products.signals.backend.activity_summary.mcp_buffer import publish_team_ids
from products.signals.backend.activity_summary.prompt import PROMPT_VERSION
from products.signals.backend.activity_summary.queue import (
TRIGGERS,
@@ -50,6 +51,7 @@ from products.signals.backend.activity_summary.queue import (
feed_queue,
is_team_enrolled_for_generation,
max_update_cost_usd,
+ mcp_buffer_team_ids,
select_pairs,
sync_pair_state,
)
@@ -97,6 +99,7 @@ class EnrollmentCounts:
qualifying_organizations: int = 0
enrolled_organizations: int = 0
enrolled_teams: int = 0
+ mcp_buffer_teams: int = 0
@frozen
@@ -184,12 +187,15 @@ def plan_tick(limit: int | None, now: datetime.datetime) -> ActivitySummaryTickP
enabled = config.enabled and not gateway_missing
# One enrollment read serves the feed, the selection and the counts.
enrolled = enrollment(config)
+ buffer_team_ids = mcp_buffer_team_ids(config, enrolled)
counts = EnrollmentCounts(
qualifying_organizations=len(enrolled.qualifying_organization_ids),
enrolled_organizations=len(enrolled.organization_ids),
enrolled_teams=len(enrolled.team_ids),
+ mcp_buffer_teams=len(buffer_team_ids),
)
logger.info("activity_summary_enrollment_read", **dataclasses.asdict(counts))
+ publish_team_ids(buffer_team_ids if config.enabled and config.mcp_buffer_enabled else None)
if config.enabled:
# The cursors move even when a cap stops the tick, so new rows do not pile up behind them.
feed_queue(config, now, enrolled.team_ids)
diff --git a/products/signals/backend/test/test_activity_summary_mcp_buffer.py b/products/signals/backend/test/test_activity_summary_mcp_buffer.py
index fae9f68ee2f8..556c08e220f5 100644
--- a/products/signals/backend/test/test_activity_summary_mcp_buffer.py
+++ b/products/signals/backend/test/test_activity_summary_mcp_buffer.py
@@ -22,11 +22,16 @@ from products.signals.backend.activity_summary.inputs import McpRead, user_mcp_r
from products.signals.backend.activity_summary.mcp_buffer import (
CIRCUIT_OPEN_SECONDS,
DIRTY_KEY,
+ INTENT_MAX_CHARS,
+ TEAMS_KEY,
BufferSettings,
McpIntentBuffer,
+ build_record,
count_key,
+ load_settings,
records_key,
)
+from products.signals.backend.activity_summary.render import clean_text
NOW = datetime.datetime(2026, 10, 20, 12, tzinfo=datetime.UTC)
DAY = "20261020"
@@ -103,6 +108,72 @@ class TestMcpIntentBuffer(SimpleTestCase):
"written"
]
+ def test_a_record_holds_the_masked_intent(self) -> None:
+ intent = (
+ "Debug login for [email protected] with key phx_a1b2c3d4e5f6g7h8i9j0k1l2 | at https://example.com/x?y=1"
+ )
+
+ value = build_record(
+ team_id=TEAM_ID, user_id=USER_ID, session_id="s1", route_name="insight-list", intent=intent, at=NOW
+ ).value
+
+ stored = value.split("|", 3)[3]
+ assert "jane.doe" not in value
+ assert "a1b2c3d4" not in value
+ assert "y=1" not in value
+ assert value.count("|") == 3
+ assert clean_text(stored, INTENT_MAX_CHARS) == stored
+
+
+class TestLoadSettings(SimpleTestCase):
+ def setUp(self) -> None:
+ self.redis = fakeredis.FakeRedis()
+
+ def _load(self, payload: dict, redis_client: MagicMock) -> BufferSettings:
+ with patch(
+ "products.signals.backend.scout_harness.team_limits.read_flag_payload",
+ return_value={"enabled": True, "mcp_calls_per_day": 20, **payload},
+ ):
+ return load_settings(redis_client)
+
+ @parameterized.expand(
+ [
+ ("the published teams", b"3,1,not-a-team", frozenset({1, 3})),
+ ("no teams when the key is missing", None, frozenset()),
+ ]
+ )
+ def test_reads_the_teams_that_the_tick_published(
+ self, _name: str, stored: bytes | None, expected: frozenset[int]
+ ) -> None:
+ if stored is not None:
+ self.redis.set(TEAMS_KEY, stored)
+
+ settings = self._load({"mcp_buffer_enabled": True}, MagicMock(return_value=self.redis))
+
+ assert settings == BufferSettings(team_ids=expected, calls_per_day=20, buffer_days=10)
+
+ def test_a_disabled_buffer_has_no_teams_without_a_redis_read(self) -> None:
+ client = MagicMock(return_value=self.redis)
+ self.redis.set(TEAMS_KEY, b"1")
+
+ assert self._load({"mcp_buffer_enabled": False}, client).team_ids == frozenset()
+ client.assert_not_called()
+
+ def test_a_redis_error_keeps_the_previous_settings(self) -> None:
+ previous = BufferSettings(team_ids=frozenset({TEAM_ID}))
+ buffer = McpIntentBuffer(
+ start_thread=False, teams_redis_client=MagicMock(side_effect=redis.TimeoutError("timed out"))
+ )
+ buffer.buffer_settings = previous
+
+ with patch(
+ "products.signals.backend.scout_harness.team_limits.read_flag_payload",
+ return_value={"enabled": True, "mcp_buffer_enabled": True},
+ ):
+ buffer.refresh_settings()
+
+ assert buffer.buffer_settings is previous
+
class TestUserMcpRecords(SimpleTestCase):
def setUp(self) -> None:
diff --git a/products/signals/backend/test/test_activity_summary_tick.py b/products/signals/backend/test/test_activity_summary_tick.py
index b44c85cd7412..17431f73cd15 100644
--- a/products/signals/backend/test/test_activity_summary_tick.py
+++ b/products/signals/backend/test/test_activity_summary_tick.py
@@ -12,6 +12,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
from django.test import override_settings
+import redis
from parameterized import parameterized
from temporalio import activity
from temporalio.client import Client, ScheduleState
@@ -24,6 +25,7 @@ from posthog.models.file_system.file_system_representation import FileSystemRepr
from posthog.models.file_system.file_system_view_log import log_file_system_view
from products.signals.backend.activity_summary import (
+ mcp_buffer,
schedule as tick_schedule,
tick,
)
@@ -134,6 +136,7 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = [
"mcp_render_enabled": 1,
"mcp_calls_per_day": 0,
"mcp_buffer_days": 10_000,
+ "mcp_buffer_percentage": 101,
},
ActivitySummaryConfig(enabled=True, organization_ids=("org-a",), skip_team_ids=(3,), mcp_buffer_days=30),
),
@@ -164,6 +167,7 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = [
"cold_start_min_active_days": 5,
"mcp_buffer_enabled": True,
"mcp_buffer_organization_ids": ["org-a"],
+ "mcp_buffer_percentage": 5,
"mcp_calls_per_day": 20,
"mcp_buffer_days": 14,
"mcp_render_enabled": True,
@@ -188,6 +192,7 @@ _CONFIG_CASES: list[tuple[str, Any, ActivitySummaryConfig]] = [
cold_start_min_active_days=5,
mcp_buffer_enabled=True,
mcp_buffer_organization_ids=("org-a",),
+ mcp_buffer_percentage=5.0,
mcp_calls_per_day=20,
mcp_buffer_days=14,
mcp_render_enabled=True,
@@ -424,27 +429,102 @@ class TestActivitySummaryQueue(BaseTest):
@parameterized.expand(
[
- ("every_enrolled_team", "none", True, {"own", "other"}),
- ("listed_organizations", "own", True, {"own"}),
- ("listed_but_not_enrolled", "own", False, set()),
- ("invalid_listed_id", "invalid", True, set()),
+ ("nothing_at_zero_without_a_list", (), 0.0, {}, set()),
+ ("a_listed_organization_at_zero", ("own",), 0.0, {}, {"own"}),
+ ("an_invalid_listed_id", ("invalid",), 0.0, {}, set()),
+ ("every_enrolled_team_at_100", (), 100.0, {}, {"own", "other"}),
+ ("a_denied_organization_at_100", (), 100.0, {"deny": "other"}, {"own"}),
+ ("a_listed_organization_without_approval", ("own",), 100.0, {"unapproved": "own"}, {"other"}),
]
)
- def test_mcp_buffer_team_ids(self, _name: str, listed: str, approved: bool, expected: set[str]) -> None:
+ def test_mcp_buffer_team_ids(
+ self, _name: str, listed: tuple[str, ...], percentage: float, overrides: dict[str, str], expected: set[str]
+ ) -> None:
other = Team.objects.create(organization=Organization.objects.create(name="Other"), name="Other")
- self.organization.is_ai_data_processing_approved = approved
- self.organization.save()
- listed_ids = {"none": (), "own": (str(self.organization.id),), "invalid": ("not-a-uuid",)}[listed]
+ teams = {"own": self.team, "other": other}
+ if "unapproved" in overrides:
+ organization = teams[overrides["unapproved"]].organization
+ organization.is_ai_data_processing_approved = False
+ organization.save()
+ organization_ids = {name: str(team.organization_id) for name, team in teams.items()} | {"invalid": "not-a-uuid"}
config = ActivitySummaryConfig(
enabled=True,
enroll_all_approved=True,
+ deny_organization_ids=tuple(organization_ids[name] for name in [overrides.get("deny")] if name),
mcp_buffer_enabled=True,
- mcp_buffer_organization_ids=listed_ids,
+ mcp_buffer_organization_ids=tuple(organization_ids[name] for name in listed),
+ mcp_buffer_percentage=percentage,
)
- team_ids = {"own": self.team.id, "other": other.id}
+ enrolled = enrollment(config)
+
+ assert mcp_buffer_team_ids(dataclasses.replace(config, mcp_buffer_enabled=False), enrolled) == []
+ assert set(mcp_buffer_team_ids(config, enrolled)) == {teams[name].id for name in expected}
+
+ def test_a_higher_mcp_buffer_percentage_keeps_the_teams_of_a_lower_one(self) -> None:
+ # These ids have rollout buckets on both sides of every percentage below.
+ teams = [
+ Team.objects.create(
+ organization=Organization.objects.create(id=uuid.UUID(int=index), name=f"Org {index}"),
+ name=f"Team {index}",
+ )
+ for index in range(1, 9)
+ ]
+ lowest = min(teams, key=lambda team: organization_rollout_bucket(team.organization_id))
+ config = ActivitySummaryConfig(enabled=True, enroll_all_approved=True, mcp_buffer_enabled=True)
+ enrolled = enrollment(config)
+ lowest_percentage = (organization_rollout_bucket(lowest.organization_id) + 1) / 100
+
+ selected = [
+ set(mcp_buffer_team_ids(dataclasses.replace(config, mcp_buffer_percentage=percentage), enrolled))
+ for percentage in (lowest_percentage, 25.0, 50.0, 75.0)
+ ]
+
+ assert lowest.id in selected[0]
+ assert all(lower <= higher for lower, higher in zip(selected, selected[1:]))
+ assert len(selected[-1]) < len(enrolled.team_ids)
+
+ @parameterized.expand(
+ [
+ ("published", {"mcp_buffer_enabled": True, "mcp_buffer_percentage": 100.0}, "own"),
+ ("deleted_when_the_buffer_is_off", {"mcp_buffer_percentage": 100.0}, None),
+ ("deleted_when_activity_summary_is_off", {"enabled": False, "mcp_buffer_enabled": True}, None),
+ ]
+ )
+ def test_plan_publishes_the_mcp_buffer_teams(
+ self, _name: str, overrides: dict[str, Any], published: str | None
+ ) -> None:
+ not_enrolled = Team.objects.create(organization=Organization.objects.create(name="Other"), name="Other")
+ client = mcp_buffer._teams_redis_client()
+ client.set(mcp_buffer.TEAMS_KEY, f"{not_enrolled.id}")
+
+ with (
+ patch.object(tick, "read_config", return_value=dataclasses.replace(self._config(), **overrides)),
+ patch("products.signals.backend.activity_summary.queue.list_user_task_times", return_value=[]),
+ ):
+ plan = plan_tick(None, NOW)
+
+ stored = client.get(mcp_buffer.TEAMS_KEY)
+ if published:
+ assert mcp_buffer.parse_team_ids(stored) == {self.team.id}
+ assert 0 < client.ttl(mcp_buffer.TEAMS_KEY) <= mcp_buffer.TEAMS_TTL_SECONDS
+ assert plan.enrollment.mcp_buffer_teams == 1
+ else:
+ assert stored is None
+
+ def test_a_failed_publish_does_not_fail_the_plan(self) -> None:
+ self._changes(self._user("[email protected]"), 3)
+ self._seed()
+ broken = MagicMock(side_effect=redis.ConnectionError("unreachable"))
+
+ with (
+ patch.object(tick, "read_config", return_value=self._config(mcp_buffer_enabled=True)),
+ patch.object(mcp_buffer, "_teams_redis_client", broken),
+ patch("products.signals.backend.activity_summary.queue.list_user_task_times", return_value=[]),
+ ):
+ plan = plan_tick(None, NOW)
- assert mcp_buffer_team_ids(dataclasses.replace(config, mcp_buffer_enabled=False)) == []
- assert set(mcp_buffer_team_ids(config)) == {team_ids[name] for name in expected}
+ broken.assert_called_once()
+ assert plan.candidates == 1
def test_selects_nothing_without_ai_data_processing_approval(self) -> None:
self.organization.is_ai_data_processing_approved = False