: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(signals): fleet-wide default_team_config for scout tick caps (#64821)
Andrew Maguire8bd3aeab750d
diff --git a/products/signals/backend/temporal/agentic/scout_coordinator.py b/products/signals/backend/temporal/agentic/scout_coordinator.pyindex c1eef966393c..379032b81513 100644--- a/products/signals/backend/temporal/agentic/scout_coordinator.py+++ b/products/signals/backend/temporal/agentic/scout_coordinator.py@@ -50,15 +50,20 @@ MAX_RUNS_PER_TICK = 1000# per day: cap × ticks/day), so a team registering many scouts degrades its own cadence,# not everyone else's. Sized well above the canonical fleet (~16 scouts) so a fully-enrolled# team is never trimmed; round-robin allocation still keeps any one team from starving the-# others even when this is close to the global cap. This is the DEFAULT — a per-team override-# takes precedence when set in the `signals-scout` flag payload under `team_configs` (see-# `_team_configs`), to give an important dogfooder more headroom or hold a noisy one lower,-# no deploy.+# others even when this is close to the global cap.+#+# This is the LAST-RESORT default. Effective per-team cap resolves through three layers,+# most specific first (see `_resolve_max_runs_per_tick`): a team's own `team_configs` entry →+# the fleet-wide `default_team_config` → this code constant. The two flag layers are read+# fresh each tick, so the launch posture (e.g. cap every enrolled team at 1 run/tick via+# `default_team_config`, then hand a close partner more headroom via `team_configs`) is+# tunable in the flag UI with no deploy.MAX_RUNS_PER_TEAM_PER_TICK = 50-# Key inside a team's `team_configs` entry that overrides `MAX_RUNS_PER_TEAM_PER_TICK` for that-# team. `team_configs` is a forward-looking per-team override bag — add more keys here as other-# settings become per-team-tunable; each consumer reads + validates the key it cares about.+# Key inside a `team_configs` entry (or the `default_team_config` blob) that overrides+# `MAX_RUNS_PER_TEAM_PER_TICK`. Both blobs share the same inner shape — a forward-looking+# override bag — so add more per-team-tunable settings here and each consumer reads + validates+# the key it cares about.TEAM_CONFIG_MAX_RUNS_PER_TICK = "max_runs_per_tick"# Coordinator tick cadence. Per-scout schedules are enforced via the due-check, so this is@@ -129,8 +134,9 @@ async def fetch_enabled_signals_scout_runs_activity(payload = await asyncio.to_thread(_read_flag_payload)enrolled_team_ids = _enrolled_team_ids(payload)team_configs = _team_configs(payload)+ default_team_config = _default_team_config(payload)planned = await database_sync_to_async(_collect_planned_runs, thread_sensitive=False)(- enrolled_team_ids, team_configs+ enrolled_team_ids, team_configs, default_team_config)logger.info("signals_scout coordinator: planned runs", count=len(planned))return FetchEnabledRunsOutput(planned_runs=planned)@@ -172,11 +178,15 @@ class _DueRun:skill_name: str-def _collect_planned_runs(enrolled_team_ids: set[int], team_configs: dict[int, dict] | None = None) -> list[PlannedRun]:+def _collect_planned_runs(+ enrolled_team_ids: set[int],+ team_configs: dict[int, dict] | None = None,+ default_team_config: dict | None = None,+) -> list[PlannedRun]:"""Sync DB scan. Runs in a worker thread via Django's per-thread connection mgmt.- Takes the already-resolved enrolled team ids (and optional per-team config overrides) so- the flag reads stay off this DB pool.+ Takes the already-resolved enrolled team ids, the optional per-team config overrides, and+ the fleet-wide default config so the flag reads stay off this DB pool."""now = timezone.now()team_configs = _canonicalize_team_config_keys(team_configs or {})@@ -208,32 +218,32 @@ def _collect_planned_runs(enrolled_team_ids: set[int], team_configs: dict[int, dif not due:return []- selected = _allocate_tick_budget(due, team_configs)+ selected = _allocate_tick_budget(due, team_configs, default_team_config)planned = [PlannedRun(team_id=d.team_id, skill_name=d.skill_name) for d in selected]# Stable order for predictable child-workflow ids within the tick.planned.sort(key=lambda p: (p.team_id, p.skill_name))return planned-def _allocate_tick_budget(due: list[_DueRun], team_configs: dict[int, dict] | None = None) -> list[_DueRun]:+def _allocate_tick_budget(+ due: list[_DueRun],+ team_configs: dict[int, dict] | None = None,+ default_team_config: dict | None = None,+) -> list[_DueRun]:"""Apply the per-team and global tick caps fairly. Deterministic — no sampling.- Each team's due runs are ordered most-overdue-first and trimmed to its per-team cap — the- `max_runs_per_tick` from its `team_configs` flag override if set, else- `MAX_RUNS_PER_TEAM_PER_TICK`; the global `MAX_RUNS_PER_TICK` budget is then filled- round-robin across teams (one run per team per round) so a single team with many due scouts- can't monopolize the tick. Deferred runs stay unstamped, so they're the most overdue next- tick — a poor-man's queue, same catch-up semantics as before.+ Each team's due runs are ordered most-overdue-first and trimmed to its per-team cap,+ resolved per `_resolve_max_runs_per_tick` (team override → fleet default → code constant);+ the global `MAX_RUNS_PER_TICK` budget is then filled round-robin across teams (one run per+ team per round) so a single team with many due scouts can't monopolize the tick. Deferred+ runs stay unstamped, so they're the most overdue next tick — a poor-man's queue, same+ catch-up semantics as before."""team_configs = team_configs or {}+ default_team_config = default_team_config or {}def _team_cap(team_id: int) -> int:- # Per-team override takes precedence; validate the value here (the config blob is- # arbitrary flag JSON) and fall back to the global default if absent or invalid.- override = (team_configs.get(team_id) or {}).get(TEAM_CONFIG_MAX_RUNS_PER_TICK)- if isinstance(override, int) and not isinstance(override, bool) and override > 0:- return override- return MAX_RUNS_PER_TEAM_PER_TICK+ return _resolve_max_runs_per_tick(team_id, team_configs, default_team_config)by_team: dict[int, list[_DueRun]] = {}for d in due:@@ -279,6 +289,21 @@ def _allocate_tick_budget(due: list[_DueRun], team_configs: dict[int, dict] | Noreturn selected+def _resolve_max_runs_per_tick(team_id: int, team_configs: dict[int, dict], default_team_config: dict) -> int:+ """Effective per-tick cap for a team, most-specific layer first.++ `team_configs[team_id]` (per-team override) → `default_team_config` (fleet-wide default) →+ `MAX_RUNS_PER_TEAM_PER_TICK` (code constant). Both flag blobs are arbitrary JSON, so the+ `max_runs_per_tick` value is validated at each layer (positive int, not bool); an absent or+ malformed value falls through to the next layer rather than failing the tick.+ """+ for source in ((team_configs.get(team_id) or {}), default_team_config):+ override = source.get(TEAM_CONFIG_MAX_RUNS_PER_TICK)+ if isinstance(override, int) and not isinstance(override, bool) and override > 0:+ return override+ return MAX_RUNS_PER_TEAM_PER_TICK++def _participating_teams(enrolled: set[int]) -> list[Team]:"""Resolve enrolled team ids to canonical `Team`s to run scouts on.@@ -408,6 +433,23 @@ def _team_configs(payload: dict | None) -> dict[int, dict]:return configs+def _default_team_config(payload: dict | None) -> dict:+ """Fleet-wide default config applied to every enrolled team, parsed from the `signals-scout`+ flag payload key `default_team_config`.++ Same inner shape as a `team_configs` entry (today: `max_runs_per_tick`). It sits between a+ per-team `team_configs` override and the code constant in `_resolve_max_runs_per_tick`, so a+ single fleet-wide cost guardrail (e.g. cap every enrolled team at 1 run/tick for launch) can+ be set in the flag UI with no deploy, while specific teams still get more headroom via+ `team_configs`. Absent/malformed (`None` payload included) → `{}` (everyone falls back to the+ code constants — unchanged behaviour).+ """+ if payload is None:+ return {}+ raw = payload.get("default_team_config", {})+ return raw if isinstance(raw, dict) else {}++def _overdue_seconds(config: SignalScoutConfig, now: datetime) -> float | None:"""Seconds past due (down to `-DUE_GRACE_SECONDS`), or None if not yet due. Never-run rows are maximally overdue."""if config.last_run_at is None:diff --git a/products/signals/backend/test/test_scout_coordinator.py b/products/signals/backend/test/test_scout_coordinator.pyindex c4935f7d487d..3beed289bc3e 100644--- a/products/signals/backend/test/test_scout_coordinator.py+++ b/products/signals/backend/test/test_scout_coordinator.py@@ -30,6 +30,7 @@ from products.signals.backend.temporal.agentic.scout_coordinator import (PlannedRun,SignalsScoutCoordinatorWorkflow,StampDispatchedRunsInput,+ _default_team_config,_enrolled_team_ids,_read_flag_payload,_team_configs,@@ -544,6 +545,64 @@ async def test_per_team_config_override_keyed_by_child_env_applies_to_parent(ateawait sync_to_async(child.delete)()+# ── Fleet-wide default config via the flag payload (default_team_config) ──────────+++ "payload,expected",+ [+ ({"default_team_config": {"max_runs_per_tick": 1}}, {"max_runs_per_tick": 1}),+ ('{"default_team_config": {"max_runs_per_tick": 2}}', {"max_runs_per_tick": 2}), # JSON string payload+ ({"default_team_config": "nope"}, {}), # wrong type → empty+ ({}, {}), # absent key → no fleet default+ (None, {}), # no payload → no fleet default+ ],+)+def test_default_team_config_parses_payload(payload, expected):+ with patch(_PAYLOAD_PATH, return_value=payload):+ assert _default_team_config(_read_flag_payload()) == expected+++ "team_override_cap,expected_names",+ [+ # No per-team entry → the fleet-wide default_team_config (cap 1) binds.+ (None, ["signals-scout-most"]),+ # A valid per-team override beats the fleet default, so all three dispatch.+ (3, ["signals-scout-least", "signals-scout-mid", "signals-scout-most"]),+ # An invalid per-team override falls through to the fleet default (cap 1).+ ("nope", ["signals-scout-most"]),+ ],+)+async def test_default_team_config_resolution(ateam, team_override_cap, expected_names):+ # A fleet-wide `default_team_config` caps every enrolled team at 1/tick; a per-team+ # `team_configs` override takes precedence when present and valid, else the fleet default+ # still binds. Three scouts due (10h/5h/2h overdue) make the resolved cap observable.+ now = timezone.now()+ for name, hours in [("signals-scout-most", 10), ("signals-scout-mid", 5), ("signals-scout-least", 2)]:+ await database_sync_to_async(_create_skill)(ateam, name)+ await database_sync_to_async(_create_config)(+ ateam, name, enabled=True, run_interval_minutes=60, last_run_at=now - timedelta(hours=hours)+ )++ def _payload(*_a, **_k):+ payload: dict[str, Any] = {+ "guaranteed_team_ids": [int(t) for t in _FLAGGED_TEAM_IDS],+ "default_team_config": {"max_runs_per_tick": 1},+ }+ if team_override_cap is not None:+ payload["team_configs"] = {str(ateam.id): {"max_runs_per_tick": team_override_cap}}+ return payload++ with patch(_PAYLOAD_PATH, side_effect=_payload):+ planned = await _run_activity()++ assert sorted(p.skill_name for p in planned) == sorted(expected_names)++@pytest.mark.asyncio@pytest.mark.django_dbasync def test_auto_register_past_enabled_cap_creates_disabled_config(ateam):