Skip to content

feat(durable): key_scope: lets runs of different workflows share a key - #53

Merged
kasvith merged 5 commits into
mainfrom
feat/issue-52
Nov 25, 2026
Merged

kasvith merged 5 commits into
mainfrom
feat/issue-52

Conversation

@kasvith

@kasvith kasvith commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #52.

A workflow that names key_scope: "account_index" shares its keys with every workflow naming the same scope. One run per scope and key holds it, so an index update and a rebuild for one account never run at the same time. Workflows without key_scope: keep their own keys, even two in one module, so nothing changes for existing users.

workflow "update_index", key_scope: "account_index", on_duplicate: :run_again do ... end
workflow "rebuild_index", key_scope: "account_index" do ... end

Four commits: the feature, its follow-ups, credo for the dashboard, and ex_slop for both packages.

Rules

  • A start that finds another workflow's run holding the key, or waiting on it, joins the end of the line whatever its on_duplicate: says. It never gets that run's id back, cancels it, sends it an event or gives it its input.
  • on_duplicate: and debounce: meet only the start's own workflow's runs. Each workflow has at most one run waiting in the line.
  • The line runs in start order, also after a run in the middle of it is cancelled.
  • A scoped run being rolled back keeps its key until its undo has run. A start of any workflow in the scope, its own included, waits behind it, in any mode.
  • A scoped workflow must use reuse: :after_close; anything else fails the compile. key_scope: passed to Durable.start/3 raises, as reuse: does.

What changed

Where Change
Migration 20261125000000 Nullable key_scope column and a unique index on (key_scope, key) WHERE holds_key AND key_scope IS NOT NULL. No row is rewritten; the per-workflow holder index stays for nodes not yet upgraded.
Keys.lock_name/1 A scoped start takes the scope's advisory lock, not the workflow's.
Claim SQL (rule 30) A run still finishing a step it was told to stop holds up runs of any workflow in its scope.
Keys Holder, waiter and line lookups match the scope. A run of another workflow, or one being undone (undoing?/2), sends the start to the line (behind_others/4). A run that has to wait again waits behind the newest run started before it (ahead/2).
Executor.without_key/2 Keeps a scoped run's key through its rollback (as reuse: :never already did).
Query.list_keys/1, count_keys/1 Each workflow's row gets the scope's holder, plus holder_workflow_name and key_scope; the holder_status: filter agrees with it. get_execution/2 returns key_scope.
Dashboard Keys page: the scope's holder, with its workflow name when it's another's. A run's Key view lists the runs of every workflow in the scope, and its subtitle names the scope. DESIGN.md updated first.
Docs guides/workflow_keys.md key_scope: section; DSL option docs; ARCHITECTURE.md keys paragraph, rules 3, 16 and 30.

Tests

  • key_scope_test.exs (31 tests). Starts in each direction wait for each other. The line runs in start order, including after a cancel. Each on_duplicate: mode waits its turn. A rollback keeps the key:

    • while compensating, an update asked for meanwhile waits;
    • a start of its own workflow waits;
    • cancel(rollback: true).

    Also the scope-aware listings, a scope change, unscoped workflows staying separate, and the DSL refusals.

  • integration/issue52_test.exs, on live pollers:

    • update, rebuild, update never overlap and run in start order;
    • 20 concurrent starts form one line;
    • a compensation held open while an update is started: they never overlap.
  • Dashboard keys_live_test.exs: the Key view and the Keys page for a shared key.

  • Every new test was written first and failed on the code before its fix. I also removed each piece on its own and checked that a test fails:

    Piece removed Tests that fail
    The scope in the claim SQL 1
    The scoped lock name the integration race, 3 of 3 runs
    without_key/2 keeping the key 3 unit, plus the integration overlap test
    undoing?/2 2

Tooling (separate commits)

  • Credo for the dashboard. It gets durable's .credo.exs, the dev/test dep, credo --strict in mix precommit and a Credo step in CI. Its 28 findings are fixed.
  • ex_slop in both packages. ExSlop.recommended_checks() (28 checks) is the tail of each enabled: list, so it runs under credo --strict locally and in CI. LengthComparison skips test/: those assertions check the exact size of lists the test built. All other findings are fixed.

Verified (final code)

Suite Result
Durable fast 1421 passed. One run had a failure that passed when rerun, on its own and in a full rerun.
Integration 460 passed
Fuzz 116 passed (seed 809522)
Dashboard 399 passed
React Doctor no issues
Credo --strict + ex_slop clean, both packages
compile --warnings-as-errors clean
Dialyzer same warnings as before this PR
CI all green, including the new dashboard Credo step

Not changed

  • A scoped run that failed and halted has already passed its key on. If it's rolled back later (Durable.rollback/2, or on_failure: [rollback_after: ms]), its undo runs beside whichever run holds the key then. I checked this with a run; the guide says so. Fixing it would mean an undo that waits in line for the key.
  • Durable.Stats.keys/1 still counts per workflow.

A workflow that names a `key_scope:` shares its keys with every workflow
that names the same scope: one run per scope and key holds it, so an
index update and an index rebuild for one account never run at once.

A start that finds another workflow's run holding the key, or waiting
on it, joins the end of the line whatever its `on_duplicate:` says.
`on_duplicate:` and `debounce:` only meet the start's own workflow's
runs. A scoped workflow must use `reuse: :after_close`.

The start's key lock, the holder unique index and the kept-lock check
in the claim query all cover the scope. Migration 20261125000000 adds a
nullable `key_scope` column and a unique index; no row is rewritten and
the per-workflow index stays for nodes not yet upgraded.

Closes #52.
…rder

A scoped run being rolled back keeps its key until its undo has run,
and a start of any workflow in the scope, its own included, waits
behind it: the compensations write what the next run works on.

A run of the line that comes for the key after the run it waited behind
was cancelled out of the line waits behind the newest run started
before it (`Keys.ahead/2`), so the line stays in start order.

`Query.list_keys/1` and `count_keys/1` give each workflow's row the
scope's holder (`holder_workflow_name`, `key_scope`), so the
`holder_status:` filter agrees with it, and `get_execution/2` returns
`key_scope`. The dashboard's Keys page and a run's Key view show the
scope's holder and runs.
The dashboard gets durable's .credo.exs, credo in its dev/test deps, a
Credo step in CI and `credo --strict` in `mix precommit`. Its 28
findings are fixed: alias order, nested module aliases, string sigils,
one filter instead of three, case over a one-clause cond, and the
nesting and complexity ones split into helpers (status text and tones
are lookup tables now).
Both .credo.exs files add ExSlop.recommended_checks() to the enabled
list, so `credo --strict` (CI and `mix precommit`) runs them.
LengthComparison skips test/: a test asserts the exact size of a list
it built, and the check is for code that runs.

Fixed what they found: Map.new over a reduce of Map.put, a length/1
guard and comparisons made pattern matches (Enum.count_until/2 for the
100-run threshold), an identity Enum.map, two docs that restated their
names, and atom-or-string key reads of maps that are always read back
from JSONB with string keys. Durable's rollback_interrupted activity
reads its error as stored, since storable_error/1 can hand atom keys.
The check was that the new deadline sat 0..30 ms from when the test read
it, so a retry that took longer than 30 ms on a loaded machine failed it.
It's now between the clock read just before the retry and just after,
plus the lane's 30 ms, which doesn't depend on how long the retry took.
A retry that gave the lane twice its time still fails it.
@kasvith
kasvith merged commit da7193f into main Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keys: let runs of different workflows share a key, so they never run at the same time

1 participant