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.

feat(signals): prefer richer mcp sessions in activity summary inputs (#127412)

posthog[bot]713894204b0a
diff --git a/products/signals/backend/activity_summary/logic.py b/products/signals/backend/activity_summary/logic.py
index 4a4be9488a38..b81df1805439 100644
--- a/products/signals/backend/activity_summary/logic.py
+++ b/products/signals/backend/activity_summary/logic.py
@@ -213,6 +213,9 @@ def _inputs_used(rendered: RenderedInput, limits: RenderLimits, mcp: McpRead | N
used.update(
{
"mcp_sessions": rendered.mcp_pool,
+ "mcp_burst_cap_drops": rendered.mcp_burst_cap_drops,
+ "mcp_sample_cap_drops": rendered.mcp_sample_cap_drops,
+ "mcp_budget_drops": rendered.mcp_budget_drops,
"mcp_lines": rendered.n_mcp_lines,
"mcp_chars": rendered.mcp_chars,
"mcp_records": len(mcp.records),
diff --git a/products/signals/backend/activity_summary/render.py b/products/signals/backend/activity_summary/render.py
index 5b0908785680..6f77d236dd14 100644
--- a/products/signals/backend/activity_summary/render.py
+++ b/products/signals/backend/activity_summary/render.py
@@ -25,7 +25,7 @@ from posthog.dataclasses import frozen
# Bump on any code change to the filters, the default caps, the sampling or the rendered layout, so summaries
# stay comparable. Limits from the flag payload go into `inputs_used` instead.
-RENDER_VERSION = 11
+RENDER_VERSION = 12
WINDOW_DAYS = 28
RECENCY_HALF_LIFE_DAYS = 7
@@ -67,6 +67,11 @@ MCP_INTENT_CHARS = 200
MIN_MCP_INTENT_CHARS = 60
# An agent can keep one MCP session open all day. A longer pause between two calls starts a new line.
MCP_SESSION_IDLE_GAP = datetime.timedelta(minutes=30)
+# A log scale lets a long investigation win a slot over a one-call lookup, but not every slot. Calls can
+# repeat one step, so their share is smaller and stops below the share of one more distinct intent.
+MCP_WEIGHT_PER_INTENT_DOUBLING = 1.0
+MCP_WEIGHT_PER_CALL_DOUBLING = 0.25
+MCP_MAX_CALL_WEIGHT = 0.5
# Intents that open with the same text keep it on the first intent only, if the shared text is at least this long.
MIN_SHARED_OPENING_CHARS = 20
SECTIONS_MAX_CHARS = 8000
@@ -402,6 +407,7 @@ class Unit:
# Characters of agent intent on the line.
intent_chars: int = 0
intent_keys: frozenset[str] = frozenset()
+ weight: float = 1.0
@frozen
@@ -444,8 +450,12 @@ class RenderedInput:
intent_chars: int = 0
n_mcp_lines: int = 0
mcp_chars: int = 0
- # MCP sessions that passed the named filters, before the caps.
+ # MCP sessions that passed the named filters and the per-day cap, before the sample.
mcp_pool: int = 0
+ # MCP lines that the per-day cap, the sample and the sections budget dropped. `drops` mixes the sources.
+ mcp_burst_cap_drops: int = 0
+ mcp_sample_cap_drops: int = 0
+ mcp_budget_drops: int = 0
# Products whose pages the person opened. They render in the computed block, not here, but count as units.
n_page_units: int = 0
# The refs of the task and MCP lines in `text`, in the order of the lines.
@@ -1194,6 +1204,12 @@ def _mcp_line(session: _McpSession, limits: RenderLimits) -> str:
return cap_line(head + MCP_CHAIN_SEPARATOR.join(cap_line(intent, room) for intent in chain), limits.mcp_line_chars)
+def _session_weight(session: _McpSession) -> float:
+ """1.0 for a one-call session with one intent, and more for each doubling of its distinct intents or calls."""
+ calls = min(MCP_WEIGHT_PER_CALL_DOUBLING * math.log2(max(session.calls, 1)), MCP_MAX_CALL_WEIGHT)
+ return 1.0 + MCP_WEIGHT_PER_INTENT_DOUBLING * math.log2(max(len(session.chain), 1)) + calls
+
+
def mcp_units(
records: Iterable[McpItem],
activity_lines: Iterable[Unit],
@@ -1212,7 +1228,7 @@ def mcp_units(
if chains[tuple(_goal_key(intent) for intent in session.chain)] >= MIN_REPEATED_MCP_CHAINS:
drops[Drop.MCP_REPEATED_CHAIN] += 1
continue
- units.append(Unit(at=session.at, text=_mcp_line(session, limits)))
+ units.append(Unit(at=session.at, text=_mcp_line(session, limits), weight=_session_weight(session)))
return sorted(units, key=lambda unit: (unit.at, unit.text))
@@ -1228,10 +1244,10 @@ def dedupe_latest(units: Iterable[Unit], drops: Counter[str]) -> list[Unit]:
def burst_cap(units: Sequence[Unit], drops: Counter[str], max_per_day: int = MAX_UNITS_PER_DAY) -> list[Unit]:
- """At most `max_per_day` units per UTC day, newest first, in chronological order."""
+ """At most `max_per_day` units per UTC day, the heaviest and then the newest, in chronological order."""
kept: list[Unit] = []
per_day: Counter[datetime.date] = Counter()
- for unit in sorted(units, key=lambda unit: (unit.at, unit.text), reverse=True):
+ for unit in sorted(units, key=lambda unit: (unit.weight, unit.at, unit.text), reverse=True):
if per_day[_day(unit.at)] >= max_per_day:
drops[Drop.BURST_CAP] += 1
continue
@@ -1247,17 +1263,18 @@ def sampling_seed(seed_key: str, team_id: int, user_id: int) -> int:
def sample_units(units: Sequence[Unit], *, limit: int, as_of: datetime.datetime, seed: int) -> list[Unit]:
- """Up to `limit` units without replacement, weighted towards recent ones, in chronological order.
+ """Up to `limit` units without replacement, weighted towards recent and heavy ones, in chronological order.
Weighted reservoir keys (Efraimidis and Spirakis): each unit draws `u ** (1 / weight)` and the
- largest keys win, so one busy afternoon cannot fill every slot."""
+ largest keys win, so one busy afternoon cannot fill every slot. The weight is the recency
+ weight times `unit.weight`."""
if len(units) <= limit:
return sorted(units, key=lambda unit: (unit.at, unit.text))
rng = random.Random(seed)
keyed: list[tuple[float, Unit]] = []
for unit in sorted(units, key=lambda unit: (unit.at, unit.text)):
age_days = max((as_of - unit.at).total_seconds(), 0.0) / 86400
- weight = 0.5 ** (age_days / RECENCY_HALF_LIFE_DAYS)
+ weight = 0.5 ** (age_days / RECENCY_HALF_LIFE_DAYS) * unit.weight
keyed.append((rng.random() ** (1 / weight), unit))
chosen = sorted(keyed, key=lambda pair: pair[0], reverse=True)[:limit]
return sorted((unit for _, unit in chosen), key=lambda unit: (unit.at, unit.text))
@@ -1335,9 +1352,8 @@ def build_input(
activity_pool = len(activity_lines) + drops[Drop.SAMPLE_CAP] + drops[Drop.BURST_CAP] - capped_before
view_lines = view_units(views, activity, drops, limits)
pages = page_products(views, drops)
- mcp_pool = burst_cap(
- dedupe_latest(mcp_units(mcp, activity_lines, drops, limits), drops), drops, limits.max_units_per_day
- )
+ mcp_sessions = dedupe_latest(mcp_units(mcp, activity_lines, drops, limits), drops)
+ mcp_pool = burst_cap(mcp_sessions, drops, limits.max_units_per_day)
if not task_pool and not activity_lines and not view_lines and not mcp_pool and not pages:
return None
chosen_tasks = sample_units(task_pool, limit=limits.task_lines, as_of=as_of, seed=seed)
@@ -1375,6 +1391,9 @@ def build_input(
n_mcp_lines=len(sections[2][1]),
mcp_chars=len(rendered[2]),
mcp_pool=len(mcp_pool),
+ mcp_burst_cap_drops=len(mcp_sessions) - len(mcp_pool),
+ mcp_sample_cap_drops=len(mcp_pool) - len(chosen_mcp),
+ mcp_budget_drops=len(chosen_mcp) - len(sections[2][1]),
n_page_units=len(pages),
task_refs=_line_refs(sections[0][1], "t"),
mcp_refs=_line_refs(sections[2][1], "m"),
diff --git a/products/signals/backend/test/test_activity_summary_render.py b/products/signals/backend/test/test_activity_summary_render.py
index d1900a72ab8e..538f11e6ebc8 100644
--- a/products/signals/backend/test/test_activity_summary_render.py
+++ b/products/signals/backend/test/test_activity_summary_render.py
@@ -539,13 +539,34 @@ def test_without_products_drops_inbox_rows_and_views_and_counts_them() -> None:
assert drops == {Drop.EXCLUDED_PRODUCT: 2}
-def test_burst_cap_keeps_the_newest_units_of_a_day() -> None:
+ [
+ (
+ "equal weights keep the newest",
+ [Unit(at=_at(1, minutes=minute), text=f"unit {minute}") for minute in range(MAX_UNITS_PER_DAY + 2)],
+ [f"unit {minute}" for minute in range(2, MAX_UNITS_PER_DAY + 2)],
+ ),
+ (
+ "a heavy unit keeps its slot over newer light ones",
+ [
+ Unit(at=_at(1), text="investigation", weight=3.0),
+ *(
+ Unit(at=_at(1, minutes=minute), text=f"lookup {minute}")
+ for minute in range(1, MAX_UNITS_PER_DAY + 2)
+ ),
+ ],
+ ["investigation", *(f"lookup {minute}" for minute in range(3, MAX_UNITS_PER_DAY + 2))],
+ ),
+ ]
+)
+def test_burst_cap_keeps_the_heaviest_then_newest_units_of_a_day(
+ _name: str, units: list[Unit], expected: list[str]
+) -> None:
drops: Counter[str] = Counter()
- units = [Unit(at=_at(1, minutes=minute), text=f"unit {minute}") for minute in range(MAX_UNITS_PER_DAY + 2)]
kept = burst_cap(units, drops)
- assert [unit.text for unit in kept] == [f"unit {minute}" for minute in range(2, MAX_UNITS_PER_DAY + 2)]
+ assert [unit.text for unit in kept] == expected
assert drops[Drop.BURST_CAP] == 2
@@ -612,6 +633,14 @@ def test_sampling_is_deterministic_and_favors_recent_units() -> None:
]
assert sum(1 for age in picked_ages if age < 7) > sum(1 for age in picked_ages if age >= 21)
+ weighted = [Unit(at=_at(1), text=f"unit {n}", weight=4.0 if n % 2 else 1.0) for n in range(20)]
+ picked_weights = [
+ unit.weight
+ for partition in range(50)
+ for unit in sample_units(weighted, limit=5, as_of=AS_OF, seed=sampling_seed(str(partition), 2, 7))
+ ]
+ assert picked_weights.count(4.0) > 2 * picked_weights.count(1.0) > 0
+
def test_build_input_renders_the_four_sections_in_order() -> None:
rendered = build_input(
@@ -701,6 +730,43 @@ def test_mcp_units_assemble_each_session_in_order_and_cap_a_long_chain() -> None
assert len(long_chain) <= DEFAULT_LIMITS.mcp_line_chars
+INVESTIGATION = [
+ _mcp("investigation", "find the failed export", minutes=0),
+ _mcp("investigation", "read the export logs", minutes=1),
+ _mcp("investigation", "query the rows it skipped", minutes=2),
+]
+POLL_LOOP = [_mcp("poll", "poll the export status", minutes=10 + n) for n in range(64)]
+LOOKUPS = [_mcp(f"lookup {n}", f"count signups {n}", minutes=100 + 10 * n) for n in range(3)]
+
+
+ [
+ ("default limits", DEFAULT_LIMITS, (3, 2, 0, 0), 3),
+ ("a sample cap of one line", RenderLimits(mcp_lines=1), (3, 2, 2, 0), 1),
+ ("a budget below the section overhead", RenderLimits(sections_max_chars=1), (3, 2, 0, 3), 0),
+ ]
+)
+def test_build_input_keeps_the_richest_mcp_sessions_of_a_day_and_counts_their_drops(
+ _name: str, limits: RenderLimits, expected_counts: tuple[int, int, int, int], expected_lines: int
+) -> None:
+ rendered = build_input([], [], as_of=AS_OF, seed=1, limits=limits, mcp=[*INVESTIGATION, *POLL_LOOP, *LOOKUPS])
+
+ assert rendered is not None
+ assert (
+ rendered.mcp_pool,
+ rendered.mcp_burst_cap_drops,
+ rendered.mcp_sample_cap_drops,
+ rendered.mcp_budget_drops,
+ ) == expected_counts
+ assert rendered.n_mcp_lines == expected_lines
+ if limits == DEFAULT_LIMITS:
+ assert rendered.text.split("\n")[1:] == [
+ "- m1 2026-10-18, 3 calls: find the failed export --> read the export logs --> query the rows it skipped",
+ "- m2 2026-10-18, 64 calls: poll the export status",
+ "- m3 2026-10-18, 1 call: count signups 2",
+ ]
+
+
KEEPER = _mcp("k", "why signups dropped", days_ago=3)
diff --git a/products/signals/backend/test/test_activity_summary_update.py b/products/signals/backend/test/test_activity_summary_update.py
index de11a1b2c03b..fed378e173ec 100644
--- a/products/signals/backend/test/test_activity_summary_update.py
+++ b/products/signals/backend/test/test_activity_summary_update.py
@@ -217,6 +217,9 @@ class TestUpdateActivitySummary(BaseTest):
assert on.inputs_used is not None
assert {key: value for key, value in on.inputs_used.items() if key.startswith("mcp_")} == {
"mcp_sessions": 1,
+ "mcp_burst_cap_drops": 0,
+ "mcp_sample_cap_drops": 0,
+ "mcp_budget_drops": 0,
"mcp_lines": 1,
"mcp_chars": len(section),
"mcp_records": 2,