: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
fix(aio): continue summarization coordinator as new less often (#111002)
posthog[bot]298fb7764a6e
diff --git a/posthog/temporal/ai_observability/trace_summarization/README.md b/posthog/temporal/ai_observability/trace_summarization/README.mdindex 41db47555ca2..0e3c764cc633 100644--- a/posthog/temporal/ai_observability/trace_summarization/README.md+++ b/posthog/temporal/ai_observability/trace_summarization/README.md@@ -65,6 +65,8 @@ It starts the next team when any running team finishes, so one slow team holds oThe coordinator fixes one window from the time Temporal started the run, and passes it to every per-team workflow as `window_start` and `window_end`.So each team covers the same hour however late in the run it starts, and consecutive runs cover consecutive hours.Continue-as-new carries the remaining teams and the window into the next run, so it loses nothing.+Before it continues as new, the coordinator waits for the running teams to finish, and the free slots stay idle during that wait.+So it continues as new only at its own history limits (the `continue_as_new_history_length` and `continue_as_new_history_size_bytes` inputs), not at the lower Temporal suggestion.A run that is skipped, or that reaches its timeout before it reaches a team, still loses that hour for the teams it did not reach.**Inputs** (`BatchTraceSummarizationCoordinatorInputs`): `max_traces`, `batch_size`, `mode`, `window_minutes`, `model` - all optional with sensible defaults.diff --git a/posthog/temporal/ai_observability/trace_summarization/constants.py b/posthog/temporal/ai_observability/trace_summarization/constants.pyindex 2ffe1b3a22b0..de7e8e13ea64 100644--- a/posthog/temporal/ai_observability/trace_summarization/constants.py+++ b/posthog/temporal/ai_observability/trace_summarization/constants.py@@ -68,6 +68,14 @@ DEFAULT_MAX_CONCURRENT_TEAMS = 20 # Max teams to process in parallel# The patch id keeps coordinator executions that started with fixed batches deterministic on replay.SLIDING_WINDOW_PATCH_ID = "llma-summarization-sliding-window-2026-09"+# Continue-as-new waits for the running children to finish, so each continuation leaves slots idle.+# These defaults are above the Temporal suggestion, so a run continues as new less often.+# A run starts to wait at a limit, and the children that still run add their own events.+# So a run closes above its limit, and the limits leave room below the Temporal warning levels (10,240 events, 10 MiB).+CONTINUE_AS_NEW_HISTORY_LENGTH = 10_000+CONTINUE_AS_NEW_HISTORY_SIZE_BYTES = 8 * 1024 * 1024+FEWER_CONTINUATIONS_PATCH_ID = "llma-summarization-fewer-continuations-2026-10"+# Timeout configuration (in seconds)SAMPLE_TIMEOUT_SECONDS = 900 # 15 minutes for sampling query (buffer above QUERY_ASYNC 600s ClickHouse timeout)diff --git a/posthog/temporal/ai_observability/trace_summarization/coordinator.py b/posthog/temporal/ai_observability/trace_summarization/coordinator.pyindex 451ea1d906d7..535f5e657580 100644--- a/posthog/temporal/ai_observability/trace_summarization/coordinator.py+++ b/posthog/temporal/ai_observability/trace_summarization/coordinator.py@@ -4,8 +4,10 @@ Coordinator workflow for batch trace summarization.This workflow discovers teams dynamically via the team discovery activityand spawns child workflows to process traces for each team.-Uses continue_as_new between teams, when Temporal suggests it, to keep the-workflow history bounded (Temporal has a 50K event limit per execution).+Uses continue_as_new between teams to keep the workflow history bounded+(Temporal has a 50K event limit per execution). The sliding window continues+at its own history limits, above the Temporal suggestion, because each+continuation first waits for the running children to finish.Per-team child workflows handle the case where a team has no tracesgracefully (returning empty results).@@ -29,6 +31,8 @@ from temporalio.workflow import ChildWorkflowHandlefrom posthog.temporal.ai_observability.trace_summarization import constantsfrom posthog.temporal.ai_observability.trace_summarization.constants import (CHILD_WORKFLOW_ID_PREFIX,+ CONTINUE_AS_NEW_HISTORY_LENGTH,+ CONTINUE_AS_NEW_HISTORY_SIZE_BYTES,COORDINATOR_WORKFLOW_NAME,DEFAULT_BATCH_SIZE,DEFAULT_MAX_CONCURRENT_TEAMS,@@ -37,6 +41,7 @@ from posthog.temporal.ai_observability.trace_summarization.constants import (DEFAULT_MODEL,DEFAULT_WINDOW_MINUTES,DEFAULT_WINDOW_OFFSET_MINUTES,+ FEWER_CONTINUATIONS_PATCH_ID,GENERATION_CHILD_WORKFLOW_ID_PREFIX,SLIDING_WINDOW_PATCH_ID,WORKFLOW_EXECUTION_TIMEOUT_MINUTES,@@ -100,6 +105,10 @@ class BatchTraceSummarizationCoordinatorInputs:window_minutes: int = DEFAULT_WINDOW_MINUTESmodel: str = DEFAULT_MODELmax_concurrent_teams: int = DEFAULT_MAX_CONCURRENT_TEAMS+ # The limits are inputs so that Temporal records them when a run starts.+ # A replay then uses the recorded limits, and a later change to the defaults does not break a running execution.+ continue_as_new_history_length: int = CONTINUE_AS_NEW_HISTORY_LENGTH+ continue_as_new_history_size_bytes: int = CONTINUE_AS_NEW_HISTORY_SIZE_BYTES# Fields used by continue_as_new to carry state across continuations.# When remaining_team_ids is set, team discovery is skipped.remaining_team_ids: list[int] | None = None@@ -290,8 +299,19 @@ class BatchTraceSummarizationCoordinatorWorkflow(PostHogWorkflow):def is_drained() -> bool:return in_flight == 0+ fewer_continuations = temporalio.workflow.patched(FEWER_CONTINUATIONS_PATCH_ID)++ def should_continue_as_new() -> bool:+ info = temporalio.workflow.info()+ if not fewer_continuations:+ return info.is_continue_as_new_suggested()+ return (+ info.get_current_history_length() >= inputs.continue_as_new_history_length+ or info.get_current_history_size() >= inputs.continue_as_new_history_size_bytes+ )+for index, team_id in enumerate(team_ids):- if index > 0 and temporalio.workflow.info().is_continue_as_new_suggested():+ if index > 0 and should_continue_as_new():# Children close with this run, so let the running ones finish first.await temporalio.workflow.wait_condition(is_drained)logger.info(diff --git a/posthog/temporal/ai_observability/trace_summarization/tests/test_coordinator.py b/posthog/temporal/ai_observability/trace_summarization/tests/test_coordinator.pyindex 04f1a3db3614..ba85a058aa49 100644--- a/posthog/temporal/ai_observability/trace_summarization/tests/test_coordinator.py+++ b/posthog/temporal/ai_observability/trace_summarization/tests/test_coordinator.py@@ -54,10 +54,13 @@ class FakeTeamSummarizationWorkflow:started = workflow.now()await workflow.sleep(timedelta(minutes=10 if inputs.team_id == SLOW_TEAM_ID else 1))if not workflow.unsafe.is_replaying():+ parent = workflow.info().parent+ assert parent is not Nonechild_runs.append({"team_id": inputs.team_id,"window": (inputs.window_start, inputs.window_end),+ "coordinator_run_id": parent.run_id,"started": started,"finished": workflow.now(),}@@ -283,6 +286,32 @@ class TestBatchTraceSummarizationCoordinatorWorkflow:assert len(windows) == 1assert None not in next(iter(windows))+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "inputs",+ [+ pytest.param(+ BatchTraceSummarizationCoordinatorInputs(max_concurrent_teams=2, continue_as_new_history_length=20),+ id="history_length",+ ),+ pytest.param(+ BatchTraceSummarizationCoordinatorInputs(max_concurrent_teams=2, continue_as_new_history_size_bytes=1),+ id="history_size",+ ),+ ],+ )+ async def test_continue_as_new_carries_remaining_teams_results_and_window(self, inputs):+ child_runs.clear()+ result = await _run_coordinator(inputs)++ assert len({run["coordinator_run_id"] for run in child_runs}) > 1+ assert sorted(run["team_id"] for run in child_runs) == sorted(DISCOVERED_TEAM_IDS)+ assert result.teams_processed == len(DISCOVERED_TEAM_IDS)+ assert result.total_summaries == len(DISCOVERED_TEAM_IDS)+ windows = {run["window"] for run in child_runs}+ assert len(windows) == 1+ assert None not in next(iter(windows))+@pytest.mark.asyncio@pytest.mark.parametrize("max_concurrent_teams", [0, -1])async def test_non_positive_concurrency_fails_instead_of_hanging(self, max_concurrent_teams):