Skip to content

fix(runtime): preserve transactions on idempotency conflicts - #52

Merged
sskyy merged 2 commits into
mainfrom
codex/fix-dispatch-idempotency-conflict
Oct 3, 2026
Merged

sskyy merged 2 commits into
mainfrom
codex/fix-dispatch-idempotency-conflict

Conversation

@sskyy

@sskyy sskyy commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On PostgreSQL, when two dispatches carrying the same idempotency key arrive concurrently for the first time, the second dispatch's _DispatchIdempotency_ insert fails with a unique violation after the first one commits. A unique violation aborts the PostgreSQL transaction, and claimDispatchIdempotencyRow then reads the conflicting row inside that transaction, so the duplicate dispatch returned SQLSTATE 25P02 (current transaction is aborted) instead of the documented idempotency result (replayed, or the typed IdempotencyError).

The claim now uses INSERT ... ON CONFLICT DO NOTHING RETURNING on PostgreSQL, PGLite and SQLite, so a conflict is not an error, the transaction stays usable, and the stored state maps to the existing typed errors through one shared helper. MySQL, which has no dispatch transactions, keeps the previous unique-error path.

The defect was found by a downstream application (Mesh) while integrating its GenerationOutcome feature (issue #884 in that project's tracker). The application will consume the published interaqt fix instead of adding a local workaround.

Scope of the defect

Isolation level Before After
READ COMMITTED (default) duplicate dispatch returns 25P02 duplicate dispatch returns replayed / typed IdempotencyError
SERIALIZABLE 40001, retried by runWithTransactionRetry unchanged

Under SERIALIZABLE the framework's own flow already produced a retryable 40001 before this change: the winner's finish() reads the key after inserting it and the loser read it through load(), so PostgreSQL's serializable conflict detection reports a serialization failure for both INSERT forms. The fix therefore changes only the default isolation level. The SERIALIZABLE test is a pinned neighbouring cell, not a reproduction of the defect.

Review follow-up

  • The conflict-safe path is selected with getSchemaDialect(db).name, like every other dialect decision in MonoSystem. A driver that declares no schemaDialect is treated as postgres; with the raw db.schemaDialect?.name read it silently kept the defect.
  • New cells: a PGLite driver with no declared dialect (fails with the raw read), and a real-PostgreSQL SERIALIZABLE cell (passes before and after, labelled as such).
  • The dimension registry row now names the isolation and dialect-resolution axes, and the undefined label in its last column is replaced by a description of the escape.

Validation

  • Pre-fix check: with the pre-fix MonoSystem.ts, the READ COMMITTED real-PostgreSQL race and the PGLite claim-conflict cell fail (25P02); with the fix they pass.
  • npm run check — passed.
  • npm run test:postgres — 11 files, 53 tests passed; includes the two-Controller race for winner commit and winner rollback, and the SERIALIZABLE cell.
  • npm test — 236 files passed, 15 skipped; 2,804 tests passed, 71 skipped.

🤖 Generated with Claude Code

sskyy and others added 2 commits October 3, 2026 07:52
…pin neighbouring cells

The conflict-safe idempotency claim chose its path from the raw
`db.schemaDialect?.name`, while every other dialect decision in MonoSystem uses
`getSchemaDialect(db).name`, which treats a driver without a declared dialect as
postgres. A PostgreSQL-compatible driver that declares no dialect therefore kept
the aborted-transaction defect. The claim now resolves the dialect the same way,
and returns the typed rejection explicitly instead of relying on fall-through.

Tests: add the no-declared-dialect driver cell (fails with the raw read), and a
real-PostgreSQL SERIALIZABLE cell. The SERIALIZABLE cell behaves the same before
and after the change (the winner's finish() reads the key, so PostgreSQL reports
40001 for both INSERT forms); it is registered as a pinned neighbouring cell,
not as a reproduction of the defect. Registry row now names the isolation and
dialect-resolution axes and drops the undefined label.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sskyy
sskyy merged commit 9ffbcd7 into main Oct 3, 2026
2 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.

1 participant