Summary
reset_workflow_threads (introduced in #1129) deletes a workflow conversation's LangGraph checkpoint threads before each fire so a run starts clean. It skips threads that have a write in flight, but that check is a snapshot: a second fire of the same workflow that starts writing between the snapshot and the delete has its checkpoints removed from under it. Two fires of one workflow can overlap today because manual and scheduled fires are dispatched under different ARQ job ids and only the scheduled path goes through a claim gate.
Raised by CodeRabbit on #1129 (apps/api/app/services/workflow/thread_reset.py, review thread on line 79). Deferred out of that PR because it is a scheduler-level concern shared by every trigger, not a playbook one.
Where
apps/api/app/services/workflow/thread_reset.py—reset_workflow_threads(conversation_id):SELECT DISTINCT thread_id FROM checkpoints WHERE …(the conversation's thread, the executor's, each subagent's)SELECT DISTINCT w.thread_id FROM checkpoint_writes w WHERE … checkpoint_id = max(...)→inflightfor thread_id in deletable: await checkpointer.adelete_thread(thread_id)Steps 2 and 3 are not atomic. A run that starts between them is not ininflightand loses its threads.
- Called once per fire from
apps/api/app/workers/tasks/workflow_tasks.py(execute_workflow_by_id,await reset_workflow_threads(conversation_id)), before the run. - Why two fires can overlap:
- Scheduled fires:
WorkflowScheduler._build_job_argsarms an ARQ job keyed by(workflow_id, scheduled_for)andclaim_scheduled_for_executionrefuses a fire whose stamp no longer matchestrigger_config.next_run. - Manual fires:
WorkflowQueueService.queue_workflow_executionenqueuesexecute_workflow_by_idunder a different deterministic job id (sha256 of workflow + user + context) and does not go through the claim. Integration-triggered fires likewise. - So "run now" landing while a scheduled fire is in progress (or vice versa) runs concurrently. The per-conversation executor busy lock (
executor:busy:<conv>) serialises the executor part of both runs, but the reset happens before that lock is taken.
- Scheduled fires:
Reproduction (shape)
- A workflow with a scheduled fire in progress whose executor is mid-run (checkpoints being written to
executor_<conv>). - Trigger
POST /api/v1/workflows/{id}/executeat the same moment. - The manual fire's
reset_workflow_threadssnapshotsinflightjust before the scheduled run's first write lands on a new thread (e.g. the first handoff subagent's<ns>_executor_<conv>), then deletes it. - The scheduled run's next checkpoint write fails or the subagent resumes from nothing; the run degrades or repeats a side effect.
The window is small (tens of milliseconds) and needs two fires within the same second, which is why it has not been observed live, but nothing prevents it.
Proposed fix
Serialise the reset with the run it belongs to, using the lock primitives that already exist rather than a new mechanism:
- Take a per-conversation lock around snapshot + delete + run start.
app/agents/core/background/executor_queue.pyalready hastry_acquire_lock/release_lock_if_owned/build_lock_valueon Redis for the executor busy lock; a sibling key such asworkflow:reset:<conversation_id>with a short TTL, acquired inexecute_workflow_by_idbeforereset_workflow_threadsand released after the run has taken the executor busy lock, closes the window. - Alternatively (bigger, arguably cleaner): route manual and integration fires through the same claim gate as scheduled fires (
claim_scheduled_for_executionor a generalclaim_for_execution), so at most one fire of a workflow is ever in flight and the reset needs no lock at all. This also answers the "execution idempotency across overlapping fires" point raised on #1129'srecord_run_outcomethread.
Whichever is chosen, a fire that loses the race should be queued or recorded as queued, never silently dropped and never run concurrently. The existing WorkflowFireQueued path (execution_service.py) is the right shape for "this fire did not run".
Acceptance criteria
- Two fires of one workflow dispatched within the same second never both run
reset_workflow_threadswhile the other is between snapshot and delete; the second either waits or is recorded as queued. - A checkpoint write that lands after the first fire's snapshot is never deleted by that fire.
- Nothing changes for the single-fire case (the reset still runs, still skips in-flight threads, still never raises).
threads_reset/threads_skipped_inflightin the wide event stay accurate; add the lock outcome (acquired / waited / queued) as a field.
Tests
tests/stress/(in-process fakes, no sleeps): two concurrentexecute_workflow_by_idfor one workflow with a fake checkpointer that records deletes and writes with a logical clock; assert no delete targets a thread written after that fire's snapshot.tests/unit/workers/: the second fire takes the queued path when the reset lock is held.tests/integration/real/: the existing thread-reset suite (tests/unit/services/workflow/test_thread_reset.pyplus the real-Postgres cases) keeps passing unchanged.
Out of scope
The nightly prune uses the same "write parked on the head checkpoint" condition; it is not affected because it never races a fire of the same conversation by design.
Activity
0No activity yet.