Skip to content

refactor(database): write the safe event order once, so the audit chain can't drift from consumers - #306

Merged
PiquelChips merged 2 commits into
mainfrom
refactor/single-safe-event-order
Nov 2, 2026
Merged

PiquelChips merged 2 commits into
mainfrom
refactor/single-safe-event-order

Conversation

@PiquelChips

Copy link
Copy Markdown
Member

#30 said the safe consumer order for domain_events would be written once, in EventRepository::read_after. In practice the audit sealer repeated the rule in its own query, AuditRepository::unsealed. That rule is: read in (txid, sequence) order, and only from transactions older than pg_snapshot_xmin. If the two copies drifted apart, the hash chain could silently skip events or put them in the wrong order.

The rule now lives in one in_safe_order! query macro in repositories/mod.rs. It wraps sqlx::query_as!, so queries are still checked at compile time, and adds the snapshot filter and ORDER BY e.txid, e.sequence. read_after and unsealed both build on it. The sealer still adds LIMIT $1 FOR UPDATE after it, so its row locks, chain positions and the runtime role's grants don't change. read_after stays because the alerts work (#83) will consume it. .sqlx/ is regenerated: only the query text changed, and the result types sqlx recorded are identical.

Proof: just db test passes for the events, audit and runtime-role tests, including the interleaved-transaction test read_after_never_skips_an_event_committed_out_of_order, the sealer's events_are_sealed_in_safe_order_and_checkpointed and runtime_role_has_only_row_privileges. The .sqlx check, clippy -D warnings and fmt --check also pass.

Reviewed with review-loop: 1 round with 2× gpt-6.1-sol and claude-opus-5-5. Verdict: approved

Note

Written by Claude Opus 5.5 in Claude Code and reviewed by GPT-6.1 Sol and Claude Opus 5.5 (via T3 Code)

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ae33e8e6-ab8c-4847-8f84-aeff53295279
Commits

Reviewing files that changed from the base of the PR and between 80f31f9 and d9d490c.

Files selected for processing: 6
  • crates/database/.sqlx/query-be1810aec08903cffacb404daeb5b9b833c5edc65fd2d531a661f8dc07e7dde8.json
  • crates/database/.sqlx/query-fa66a4fb55d05dc901397703382fd3835f1dca45eb8a5fd8910db60e09736432.json
  • crates/database/src/repositories/audit/mod.rs
  • crates/database/src/repositories/events/mod.rs
  • crates/database/src/repositories/mod.rs
  • docs/architecture/database-crate.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

PiquelChips and others added 2 commits November 2, 2026 15:38
…nd the sealer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PiquelChips
PiquelChips force-pushed the refactor/single-safe-event-order branch from 01c01a8 to d9d490c Compare November 2, 2026 14:38
@PiquelChips PiquelChips self-assigned this Nov 2, 2026
@PiquelChips PiquelChips added the refactor No behviour changes label Nov 2, 2026
@PiquelChips PiquelChips added this to the 0.1 milestone Nov 2, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in sourceorigin.dev Nov 2, 2026
@PiquelChips PiquelChips moved this from Needs Triage to Ready in sourceorigin.dev Nov 2, 2026
@PiquelChips
PiquelChips merged commit 7aa0c68 into main Nov 2, 2026
9 checks passed
@PiquelChips
PiquelChips deleted the refactor/single-safe-event-order branch November 2, 2026 15:01
@github-project-automation github-project-automation Bot moved this from Ready to Done in sourceorigin.dev Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor No behviour changes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant