feat(workflow): start(first, key=) tells a resent event apart from a clash on another unique column

Merged
MA
8d ago
+554-22

Requested by Masen · project thread

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update (included)

What

Workflow.start inserts with ON CONFLICT DO NOTHING and no target, so a clash on any unique column returns False. A webhook receiver (the Workflows-in-the-Builder trigger route) has to answer "duplicate" for a resend and something else for a real conflict, and today it can't tell them apart.

  • await row.start(first, key=Model.column) inserts with ON CONFLICT (column) DO NOTHING. False then means exactly "a run with this key already exists".
  • A clash on any other unique constraint raises the new RunConflict, whose .constraint is the name the driver reported (psycopg diag, or the chained asyncpg error), or None.
  • key= must name a column unique on its own: a single-column primary key, unique=True, a single-column UniqueConstraint, or a non-partial unique index. Anything else raises TypeError/ValueError before the insert, since Postgres would reject the ON CONFLICT target anyway (or silently need a WHERE for a partial index).
  • Without key=, behaviour is unchanged. fan_out children are untouched.

Notes for review

  • The ticket asked for SQLite tests too, but the engine is Postgres-only (pg_insert), so the start tests run under REFLEX_TEST_POSTGRES like test_claim.py/test_runner.py. The key_column refusal tests need no database and run everywhere.
  • Fixes a bug from the prototype: a partial unique index made key_column raise TypeError: Boolean value of this clause is not defined instead of refusing it.
  • Docs: a "Key a run on an outside event" section in docs/workflows/runs.md, and start(key=) plus RunConflict in the reference.

Tests

  • uv run pytest tests/units/reflex_workflow against Postgres 16: 1069 passed.
  • ruff, ruff format, pyright, pre-commit clean on the touched files.

Linear: ENG-13935


Generated by Claude Code