Skip to content

fix: exact Postgres timestamps; no panic on invalid command projections - #345

Merged
patrickleet merged 2 commits into
v5from
fix/v5-review-28-incidents
Nov 25, 2026
Merged

patrickleet merged 2 commits into
v5from
fix/v5-review-28-incidents

Conversation

@patrickleet

Copy link
Copy Markdown
Collaborator

Two incidents found during the test-suite work (task 27), fixed as part of task 28.

Postgres timestamps lost precision (incident postgres-outbox-deliver-at-precision)

The Postgres backend wrote timestamps with to_timestamp(<f64 epoch seconds>) and read them with EXTRACT(EPOCH ...)::double precision. At present-day magnitudes an f64 resolves only about 240 ns, so a scheduled outbox message's deliver_at came back about 76 ns off, even for a whole millisecond.

  • Postgres timestamps are now i64 microseconds, which is timestamptz's own resolution.
    • Writes: TIMESTAMPTZ 'epoch' + $n * INTERVAL '1 microsecond'.
    • Reads: (EXTRACT(EPOCH FROM col) * 1000000)::bigint.
    • Both are exact on Postgres 14+; CI uses 16 and 18.
  • Covers every timestamp the event store, snapshots, outbox, outbox claim and command ledger read or write. The ledger lease deadline is now integer microseconds too.
  • SqlxRepoBackend::push_timestamp_cmp takes a SystemTime, so each backend encodes it exactly. SQLite is unchanged.
  • The scheduled-outbox conformance case asserts exact equality again instead of allowing 1 ms.

Typed command canonical_value panicked (incident typed-command-canonical-value-panics)

  • TypedCommandContract::canonical_value and fingerprint_bytes return Result instead of calling .expect on projection validation.
  • CommandSpec::from_contract validates projections, so a bad contract fails registration with the existing "typed command contract cannot compile" message rather than an opaque panic later.
  • Binding a GraphQL service maps the error to GraphqlServiceBindError::InvalidInventory. A failed bind leaves the registered contract unchanged.
  • Regression test: command::tests::invalid_projection_declaration_is_an_error_not_a_panic.

Verification (local)

  • cargo test -p distributed --all-features against Postgres 16: 2,190 passed, 0 failed (88 ignored; these need external services).
  • cargo test -p distributed --features postgres,sqlite: 1,476 passed, 0 failed.
  • Postgres and SQLite scheduled-outbox conformance with the exact assertion: 10 of 10 runs passed.
  • These all pass:
    • cargo check --workspace --all-targets --all-features
    • cargo check -p distributed --no-default-features --tests
    • --features postgres alone and --features sqlite alone
    • scripts/check-wasm32-domains.sh
    • wasm32 checks of tests/celld/worker and examples/game/cells

Not done

  • Sub-microsecond precision is truncated on Postgres; timestamptz cannot store it. SQLite keeps nanoseconds.
  • The third incident from task 27, thin-command-succeeded-fails-with-domain-events, is not reproduced yet and is not in this PR.

Postgres timestamps went through f64 epoch seconds both ways
(`to_timestamp($n)` and `EXTRACT(EPOCH ...)::double precision`). At
present-day magnitudes an f64 resolves only about 240 ns, so a
scheduled outbox message's `deliver_at` read back about 76 ns off even
for a whole millisecond.

The Postgres backend now carries timestamps as i64 microseconds, the
resolution `timestamptz` stores: writes are `TIMESTAMPTZ 'epoch' + $n *
INTERVAL '1 microsecond'` and reads are `(EXTRACT(EPOCH FROM col) *
1000000)::bigint`, both exact on Postgres 14+. `push_timestamp_cmp`
takes a `SystemTime` so each backend encodes it exactly. The scheduled
outbox conformance case asserts equality again instead of a 1 ms
tolerance.

Resolves [[incidents/postgres-outbox-deliver-at-precision]]
Implements [[tasks/v5-review-28]]
`TypedCommandContract::canonical_value` validated the contract's
domain-event projections with `.expect`, so a hand-built or
deserialized contract carrying declaration errors panicked deep inside
fingerprinting.

`canonical_value` and `fingerprint_bytes` now return `Result`.
`CommandSpec::from_contract` validates projections, so registration
fails through its existing "typed command contract cannot compile"
path, and binding a GraphQL service maps the error to
`GraphqlServiceBindError::InvalidInventory`. A direct-projection bind
that fails leaves the registered contract unchanged.

Resolves [[incidents/typed-command-canonical-value-panics]]
Implements [[tasks/v5-review-28]]
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e6994289-8517-43a7-9215-18e2f2aee4cb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

@patrickleet
patrickleet merged commit a79a82e into v5 Nov 25, 2026
27 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