: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(slack-app): seed dedup state before signals (#73547)
Raúl Negrón-Otero7bff726e7bc4
diff --git a/posthog/temporal/ai/slack_app/slack_app_mention.py b/posthog/temporal/ai/slack_app/slack_app_mention.pyindex 7aaca3ea49c0..1f1d474cf19d 100644--- a/posthog/temporal/ai/slack_app/slack_app_mention.py+++ b/posthog/temporal/ai/slack_app/slack_app_mention.py@@ -24,6 +24,14 @@ SLACK_APP_MENTION_IDLE_TIMEOUT_SECONDS = 30# stays small; old keys only matter for Slack retries, which arrive within# minutes of the original event.SLACK_APP_MENTION_MAX_PROCESSED_KEYS = 200+# Temporal runs buffered signal handlers before run() starts, so dedup state seeded in+# run() arrived too late: a redelivered Slack event signalled into the first workflow task+# missed the dedup keys and started a duplicate child workflow. Seeding moved to __init__,+# which runs first. That changes which child workflows an execution starts, so executions+# that already recorded the old order keep replaying it through this patch. The patch can+# be deleted once no pre-patch executions remain, which takes minutes because these+# workflows complete after a 30 second idle timeout.+SLACK_APP_MENTION_SEED_IN_INIT_PATCH = "slack-app-mention-seed-in-init"def derive_slack_app_mention_workflow_id(inputs: PostHogCodeSlackMentionWorkflowInputs) -> str | None:@@ -68,13 +76,23 @@ class SlackAppMentionWorkflow(PostHogWorkflow):deduped and gets processed again."""- def __init__(self) -> None:- self._queue: list[PostHogCodeSlackMentionWorkflowInputs] = []+ @workflow.init+ def __init__(self, inputs: SlackAppMentionWorkflowInputs) -> None:self._processing = False# Dedup for Slack event redeliveries: signals have no server-side# dedup the way per-message workflow IDs did. A dict, not a set, so# iteration order stays deterministic for the continue_as_new carry-over.self._seen_keys: dict[str, None] = {}+ self._queue: list[PostHogCodeSlackMentionWorkflowInputs] = []+ if workflow.patched(SLACK_APP_MENTION_SEED_IN_INIT_PATCH):+ self._seed(inputs)++ def _seed(self, inputs: SlackAppMentionWorkflowInputs) -> None:+ for key in inputs.processed_event_keys:+ self._seen_keys[key] = None+ for message in inputs.pending_messages:+ self._seen_keys.setdefault(derive_mention_workflow_id(message), None)+ self._queue.append(message)@workflow.signalasync def new_message(self, message: PostHogCodeSlackMentionWorkflowInputs) -> None:@@ -158,11 +176,9 @@ class SlackAppMentionWorkflow(PostHogWorkflow):@workflow.runasync def run(self, inputs: SlackAppMentionWorkflowInputs) -> None:- for key in inputs.processed_event_keys:- self._seen_keys[key] = None- for message in inputs.pending_messages:- self._seen_keys.setdefault(derive_mention_workflow_id(message), None)- self._queue.append(message)+ # Executions that predate the patch recorded their seeding here, so they keep it.+ if not workflow.patched(SLACK_APP_MENTION_SEED_IN_INIT_PATCH):+ self._seed(inputs)while True:try:diff --git a/posthog/temporal/tests/ai/slack_app/test_slack_app_mention_workflow.py b/posthog/temporal/tests/ai/slack_app/test_slack_app_mention_workflow.pyindex 185c0d6283aa..994fbce2fc8f 100644--- a/posthog/temporal/tests/ai/slack_app/test_slack_app_mention_workflow.py+++ b/posthog/temporal/tests/ai/slack_app/test_slack_app_mention_workflow.py@@ -247,9 +247,11 @@ class _Harness:signals in real time — no sleeps, no timer races."""- def __init__(self, rec: _Recorder) -> None:+ def __init__(self, rec: _Recorder, *, start_worker: bool = True) -> None:self.rec = recself.task_queue = str(uuid.uuid4())+ self._auto_start_worker = start_worker+ self._worker_cm: Worker | None = Noneasync def __aenter__(self):# Escape hatch for networks where the SDK's temporal.download fetch is@@ -260,6 +262,14 @@ class _Harness:test_server_existing_path=os.environ.get("TEMPORAL_TEST_SERVER_PATH"))self.env = await self._env_cm.__aenter__()+ if self._auto_start_worker:+ await self.start_worker()+ return self++ async def start_worker(self) -> None:+ # Deferred worker start lets a test land the start and its signals in+ # history before any worker polls, forcing them into the first workflow+ # task (the buffered-signal case that races state seeded in run()).self._worker_cm = Worker(self.env.client,task_queue=self.task_queue,@@ -268,10 +278,10 @@ class _Harness:workflow_runner=UnsandboxedWorkflowRunner(),)await self._worker_cm.__aenter__()- return selfasync def __aexit__(self, *exc_info):- await self._worker_cm.__aexit__(*exc_info)+ if self._worker_cm is not None:+ await self._worker_cm.__aexit__(*exc_info)await self._env_cm.__aexit__(*exc_info)@@ -403,9 +413,12 @@ async def test_continue_as_new_carry_over_processes_pending_and_dedups_seen():pending = _message("1.1", event_id="Ev-pending")already_seen = _message("1.2", event_id="Ev-seen")- async with _Harness(rec) as h:- # Start with post-continue_as_new-shaped inputs: one carried pending- # message and one already-processed key.+ async with _Harness(rec, start_worker=False) as h:+ # Post-continue_as_new-shaped inputs: one carried pending message and+ # one already-processed key. Land the start and the duplicate signal in+ # history before the worker polls, so both hit the first workflow task —+ # the case where seeding dedup state in run() rather than __init__ let+ # the already-seen event slip past dedup and get queued and processed.handle = await h.env.client.start_workflow(SlackAppMentionWorkflow.run,SlackAppMentionWorkflowInputs(@@ -416,6 +429,7 @@ async def test_continue_as_new_carry_over_processes_pending_and_dedups_seen():task_queue=h.task_queue,)await handle.signal(SlackAppMentionWorkflow.new_message, already_seen)+ await h.start_worker()await asyncio.wait_for(handle.result(), timeout=30)assert rec.created == [("1.1", "org/auto-repo")]