Skip to content

verify(core): prove kind classification and replaceable precedence against the SQL rule they were ported from #1245

Backlog

Description

@chappie-daemon

Part of the formal-verification plan in docs/infra/nostream-formal-verification.md (workspace).

Why

These two decisions decide whether a write replaces the stored event, is dropped, or is
stored alongside it — and they are ported from the TypeScript capture's SQL predicate
(event-repository.ts:404-410), so a disagreement between the Rust and the SQL form is a silent
data difference after a migration, not a crash.

Kind::is_replaceable / is_ephemeral / is_parameterized_replaceable
(crates/nostream-core/src/event.rs:223-257) are range checks with hard-coded edge kinds
(0, 3, 41) beside four numeric ranges. replaceable.rs::supersedes is a one-line comparison whose
tie-break (same created_at, lexicographically smaller id) is exactly the kind of rule that gets
inverted in a refactor.

Scope

A new crates/nostream-kinds (no dependencies) holding:

  • the kind classification over i32/u64, with nostream-core::Kind delegating to it;
  • supersedes(candidate_created_at: i64, candidate_id: &[u8; 32], stored_created_at: i64, stored_id: &[u8; 32]) -> bool, with replaceable::supersedes delegating to it.

Delegation keeps one implementation: nostream-core depends on the crate, as it does for any other
internal dependency.

Properties to prove

Function Property
is_replaceable, is_ephemeral, is_parameterized_replaceable the ranges are disjoint (no kind is in two) and match the documented bounds exactly: 0, 3, 41; 10 000..=19 999; 20 000..=29 999; 30 000..=39 999; DVM 5 000..=5 999
supersedes irreflexive; antisymmetric on distinct ids (not both directions); total enough that exactly one of supersedes(a,b), supersedes(b,a) holds when a != b; and it agrees with the SQL predicate restated over the same pair
cross-property an ephemeral kind is never replaceable; a parameterized replaceable kind always takes the parameterized storage path

Acceptance

  • cargo kani -p nostream-kinds verifies every harness
  • nostream-core delegates to the crate; the tests in replaceable.rs keep passing unchanged
  • each harness names its failing variant (e.g. a supersedes with <= on the tie-break)
  • the CI step added for the key-codec crate covers this one too

Notes

Cheapest of the three: the logic is already pure and total, so the extraction is mostly a move. Do
it while the key-codec crate's CI runner is fresh in mind.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions