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.

chore(temporal): require patches for workflow branches on new data (#124608)

Charles Vienaf65f1118364
diff --git a/.claude/rules/temporal-workflow-versioning.md b/.claude/rules/temporal-workflow-versioning.md
index 4acb36ffe2a3..0a71b541f629 100644
--- a/.claude/rules/temporal-workflow-versioning.md
+++ b/.claude/rules/temporal-workflow-versioning.md
@@ -4,29 +4,38 @@ paths:
- 'products/*/backend/temporal/**'
---
-**Editing a `@workflow.defn` body (adding/removing/reordering `execute_activity`, child-workflow
-starts, or timers) breaks in-flight executions with "Non Deterministic Error"** — on deploy every
-running execution replays its recorded history against the new code, and any new unconditional
-command at a point an execution already passed fails the replay. Activity implementations and
-activity _input_ dataclasses are safe to edit; workflow command sequences are not.
+**Changing which commands a `@workflow.defn` body emits breaks in-flight executions with "Non Deterministic Error".**
+Commands are `execute_activity` calls, child-workflow starts, timers (`workflow.sleep`) and patch markers.
+On deploy, every running execution replays its recorded history against the new code.
+The replay fails when the new code emits a command that the history does not have.
+It also fails when the new code skips a command that the history has.
+Temporal retries the failed workflow task forever, so the execution hangs.
+Activity implementations and activity _input_ dataclasses are safe to edit; workflow command sequences are not.
-When adding a new activity or child-workflow start to an existing workflow, gate it one of two ways:
+Gate every change to the command sequence with `if workflow.patched("my-change-2026-07"): ...`.
+Old histories have no marker, so they keep the old commands.
+New executions record the marker and run the new code.
+Precedent: `posthog/temporal/ai_observability/run_evaluation.py`.
+Removing or reordering existing commands keeps the old path in the `else` branch.
+Once no pre-patch executions remain (days, for short-lived workflows), you can swap to `workflow.deprecate_patch(...)` + unconditional code.
-1. `if workflow.patched("my-change-2026-07"): ...` — the default. Old histories skip the block,
- new executions record a marker and run it. Precedent: `posthog/temporal/ai_observability/run_evaluation.py`.
- Once no pre-patch executions remain (days, for short-lived workflows), optionally swap to
- `workflow.deprecate_patch(...)` + unconditional code.
-2. Gate on a **new field of an existing activity's output dataclass that defaults to the skip
- value** (e.g. `enrichment_needed: bool = False` in `create_job_model.py`) — old histories decode
- the missing field to the default, so the new command never fires during replay. Only valid when
- the decision comes from recorded history and skipping is acceptable for in-flight runs.
- **Not safe on its own for skipping or removing an existing command.** In a rolling deploy, old
- and new pods share the task queue: a new activity can return "skip" into a history that old
- workflow code wrote with the command still in it. The new code then replays that answer, skips
- the command, and every replay of that execution fails. Put `workflow.patched(...)` in front of
- the field (`if workflow.patched("x") and result.skip_it: ...`) so a history without the marker
- keeps the old command sequence.
+**New data does not replace the patch.**
+A condition on data that old code ignores changes the command sequence just as new code does.
+This covers a new field on an activity's output dataclass that defaults to the old behavior.
+It also covers a new key in a dict that an activity reads from the database, such as run state that the same change starts to write.
+In a rolling deploy, old workflow code can receive the new data.
+A new pod on the same task queue can return it.
+A new web pod can also write it before the workers roll.
+Old workflow code ignores the data and records the old commands.
+The new code then replays that history and acts on the data, so every replay of that execution fails.
+Put the patch in front of the data check (`if workflow.patched("x") and result.skip_it: ...`), so a history without the marker keeps the old commands.
-Never gate workflow commands on values computed inside the workflow body (feature flags, settings,
-clock, DB reads) — that is itself non-deterministic. Removing/reordering existing commands needs
-`workflow.patched()` with the old path kept in the `else` branch.
+**A new condition in front of an existing `workflow.patched(...)` call needs its own patch.**
+When the condition skips the call, Temporal records no marker.
+Histories that recorded the marker then fail to replay.
+
+Where the decision can live in an activity, put it there instead.
+The activity returns early, the workflow keeps the same commands and no patch is needed.
+
+Never gate workflow commands on values computed inside the workflow body (feature flags, settings, clock, DB reads).
+That is itself non-deterministic.
diff --git a/products/autoresearch/backend/temporal/AGENTS.md b/products/autoresearch/backend/temporal/AGENTS.md
index 6e84b246df3a..8c8b8cb2e682 100644
--- a/products/autoresearch/backend/temporal/AGENTS.md
+++ b/products/autoresearch/backend/temporal/AGENTS.md
@@ -83,7 +83,7 @@ Temporal activity payloads are capped at roughly 2 MiB, and autoresearch moves g
## When editing this flow
- **Editing a `@workflow.defn` body breaks in-flight executions.** Adding, removing, or reordering `execute_activity` calls, child-workflow starts, or timers fails replay with a non-determinism error on every running execution.
- Gate new commands behind `workflow.patched("...")`, or on a new field of an existing activity's output dataclass that defaults to the skip value. Activity _implementations_ and activity _input_ dataclasses are safe to edit; command sequences are not.
+ Gate new commands behind `workflow.patched("...")`. A new field on an activity's output dataclass does not replace the patch, because old workflow code can receive that field during a rolling deploy. Activity _implementations_ and activity _input_ dataclasses are safe to edit; command sequences are not.
- **Never gate a workflow command on something computed inside the workflow body** — a feature flag, a setting, the clock, a database read. That is itself non-deterministic.
- Keep scoring, validation and training logic out of the activities. It belongs in the package the activity calls, or the management commands will quietly behave differently from production. Only sweep-specific decisions (what is due, who may launch) stay here.
- New workflows and activities must be added to `WORKFLOWS` / `ACTIVITIES` in `__init__.py` or they are never registered — this fails silently, since nothing errors when a workflow simply never runs.