fix(tasks): close TaskRun startup race causing TaskNotFoundError
Problem
A small number of Tasks agent runs fail at startup with a non-retryable TaskNotFoundError, aborting the run before any work begins.
Task.create_and_run starts the process-task Temporal workflow fire-and-forget immediately after TaskRun.objects.create(...). When that path runs inside an enclosing transaction.atomic() block (e.g. the signals auto-start lock), the row isn't committed yet — so the workflow's first activity, get_task_processing_context, can read the TaskRun before it's visible and raise a fatal TaskNotFoundError. Because the error is non-retryable, the workflow never recovers once the row commits a moment later.
Changes
- Close the race at the source: defer the workflow start in
Task.create_and_runtotransaction.on_commit(...), so the run is committed before the activity reads it. In autocommit modeon_commitruns the callback immediately, so non-atomic callers are unchanged. - Defense in depth: treat a missing
TaskRuninget_task_processing_contextas a retryableTaskRunNotReadyErrorinstead of the fatalTaskNotFoundError, letting the activity's existing retry policy recover from the transient visibility window. The new error intentionally does not capture to error tracking, since it's an expected transient condition the retry absorbs rather than a fault — this also removes the duplicate error-tracking noise.
The later, genuinely-fatal TaskNotFoundError usages (a Task disappearing mid-workflow) are left as-is.
How did you test this code?
I'm an agent. The local environment had no Python dependencies installed and no network to fetch them, so I could not execute the test suite here. I verified the changes with ruff check/ruff format (clean) and py_compile, and traced every production and test call path to create_and_run to confirm impact.
Test changes:
- Updated
test_get_task_processing_context_task_not_found_is_retryableto assert the missing-run path now raises a retryableTaskRunNotReadyError(non_retryable is False) — catches a regression to the fatal behavior that caused the bug. - Wrapped the 9
TestTask.test_create_and_run_*tests that assert the workflow mock fired inself.captureOnCommitCallbacks(execute=True), since the start is now deferred to commit. Other call paths (/run/,/start/, posthog_ai routing, slack followup) go through the synchronous_trigger_task_processing_workflowor passstart_workflow=False, so they're unaffected.
🤖 Agent context
Autonomy: Fully autonomous
Authored by PostHog Code (Claude) from an inbox report flagging two duplicate TaskRun.DoesNotExist error-tracking issues. No human drove the work, so the PR is left unassigned for the owning team to triage.
Approach: the fix has two layers — the on_commit deferral eliminates the race for callers that create the run inside a transaction, and the retryable error is a belt-and-suspenders recovery for any residual transient window. I considered keeping the transient error capturing (as siblings do) but chose not to, since capturing on every retry of an expected-transient condition is exactly the error-tracking noise the report called out.
Created with PostHog Code from an inbox report.