commit 17ccc2f114a9ed19255d4bcd28b722c2c993b45e
parent 123b545d6b0b29a9cb3854f69789afe00c650cec
Author: triesap <tyson@radroots.org>
Date: Mon, 12 Oct 2026 21:11:29 +0000
replica_store: Correct off-profile relay membership
- Select the complete complement with a parameterized NOT EXISTS query.
- Retain actual SQLite regressions for membership, uniqueness and delete effects.
- Validate Rust 1.99.0 formatting, all targets, 76 tests and strict Clippy.
- Close DEV-032 with actual evidence and preserve pending consumer adoption.
Diffstat:
4 files changed, 191 insertions(+), 1 deletion(-)
diff --git a/contracts/architecture/deviations.toml b/contracts/architecture/deviations.toml
@@ -2,6 +2,35 @@ schema_version = 1
architecture_id = "radroots.crates.release.v1"
[[deviation]]
+id = "RCRV1-DEV-032"
+date = "2026-10-12"
+status = "closed"
+approval = "The user approved the successor refactor, scoped execution/recovery amendments and all remaining local verified checkpoints; publication and supported consumer qualification remain separate."
+affected_steps = ["010", "070", "130"]
+spec_anchors = ["contracts/crates/release_v1/radroots_crates_release_v1.toml#repository_policy"]
+source_evidence = [
+ "Relay off-profile selection filters joined rows by a different public key; the profile-side off-relay query instead uses a complete NOT EXISTS complement.",
+ "The full actual-SQLite suite only asserts that relay relation calls succeed, without checking returned membership.",
+ "The current owner and freshly fetched published integration branch retain the identical relay query; no newer published correction was observed.",
+ "All three retained actual-SQLite regressions failed against unchanged source: unassigned omission/shared inclusion, missing-profile duplicate rows and incomplete single/delete membership effects.",
+]
+replacement_action = "Bring forward a contained relation-query correction during source reevaluation. Prove membership through disposable actual SQLite, then select relays having no edge to the requested profile using a bound NOT EXISTS predicate shared by existing many/single/delete paths. Preserve public API shapes, on-profile behavior and source/feature ownership."
+verification = [
+ "The prospective standalone architecture check passed before the production edit; the retained public regression then failed all three cases against unchanged source.",
+ "Fresh in-memory SQLite with two synthetic profiles, four non-contacted fixture relay URLs and four edges now proves the complete membership complement, missing-profile uniqueness and single/delete effects while preserving on-profile membership.",
+ "Exact Rust 1.99.0 package formatting, all-feature/all-target checking, the full replica-store tests (76 passed, zero ignored) and warnings-as-errors Clippy passed after the bound NOT EXISTS correction; actual command outputs are retained in the parent checkpoint evidence.",
+]
+unresolved_risk = "Local relation qualification is complete. Bounded typed admission, complete transaction/cleanup outcomes, missing relation sources, browser backend, protected restore/caller qualification and supported consumer adoption remain separate; no deployed incident or full G1 is claimed."
+normative_architecture_change = false
+adr_required = false
+closure_evidence = [
+ "All three retained public relation tests failed against unchanged production source, after the prospective architecture check passed.",
+ "The shared parameterized NOT EXISTS query now selects the complete off-profile complement, including unassigned relays, excluding shared memberships and returning each eligible relay once; actual single/delete effects preserve assigned relays.",
+ "Exact Rust 1.99.0 scoped formatting, all-feature/all-target checking, the full package tests (76 passed, zero ignored) and warnings-as-errors Clippy passed on local macOS arm64.",
+ "The first closure validation rejected this record because closure_evidence was absent; that failed attempt is retained, and only this evidence field was added before repeating the architecture check.",
+]
+
+[[deviation]]
id = "RCRV1-DEV-031"
date = "2026-10-12"
status = "closed"
diff --git a/crates/replica_store/README b/crates/replica_store/README
@@ -11,6 +11,12 @@ facades and shaped query helpers for the `radroots` core libraries.
* shaped query helpers for common trade, farm, and event freshness lookups;
* feature-gated web and SQLx native backend support through `radroots_sql_core`.
+Off-profile relay reads and deletes select the complete complement of a
+profile's relay memberships. This includes unassigned relays and relays assigned
+only to other profiles; it excludes every relay assigned to the requested
+profile, including relays shared with other profiles. A missing profile has all
+relays in its complement, with each relay returned once.
+
## Copyright
Except as otherwise noted, all files in the `radroots_replica_store` distribution
diff --git a/crates/replica_store/src/models/nostr_relay.rs b/crates/replica_store/src/models/nostr_relay.rs
@@ -84,7 +84,7 @@ fn rel_query(rel: &NostrRelayFindManyRel) -> (&'static str, Vec<Value>) {
vec![Value::from(args.public_key.clone())],
),
NostrRelayFindManyRel::OffProfile(args) => (
- "SELECT rl.* FROM nostr_relay rl LEFT JOIN nostr_profile_relay pr_rl ON rl.id = pr_rl.tb_rl LEFT JOIN nostr_profile pr ON pr.id = pr_rl.tb_pr WHERE pr.public_key <> ?",
+ "SELECT rl.* FROM nostr_relay rl WHERE NOT EXISTS (SELECT 1 FROM nostr_profile_relay pr_rl JOIN nostr_profile pr ON pr.id = pr_rl.tb_pr WHERE pr_rl.tb_rl = rl.id AND pr.public_key = ?)",
vec![Value::from(args.public_key.clone())],
),
}
diff --git a/crates/replica_store/tests/relay_complement.rs b/crates/replica_store/tests/relay_complement.rs
@@ -0,0 +1,155 @@
+use std::collections::BTreeSet;
+
+use radroots_replica_schema::nostr_profile::{INostrProfileCreate, NostrProfileQueryBindValues};
+use radroots_replica_schema::nostr_profile_relay::INostrProfileRelayRelation;
+use radroots_replica_schema::nostr_relay::{
+ INostrRelayCreate, INostrRelayDelete, INostrRelayFindMany, INostrRelayFindOne,
+ INostrRelayFindOneRelArgs, NostrRelayFindManyRel, NostrRelayProfileArgs,
+ NostrRelayQueryBindValues,
+};
+use radroots_replica_store::ReplicaSql;
+use radroots_sql_core::{SqlError, SqlxSqliteExecutor};
+use serde_json::json;
+
+type Database = ReplicaSql<SqlxSqliteExecutor>;
+
+const UNASSIGNED: &str = "wss://unassigned.example.test";
+const TARGET_ONLY: &str = "wss://target.example.test";
+const OTHER_ONLY: &str = "wss://other.example.test";
+const SHARED: &str = "wss://shared.example.test";
+
+fn target_key() -> String {
+ "a".repeat(64)
+}
+
+fn fixture() -> Database {
+ let db = ReplicaSql::new(SqlxSqliteExecutor::open_memory().unwrap());
+ db.migrate_up().unwrap();
+ let profiles: Vec<_> = [target_key(), "b".repeat(64)]
+ .into_iter()
+ .map(|public_key| {
+ let input: INostrProfileCreate = serde_json::from_value(json!({
+ "public_key": public_key,
+ "profile_type": "person",
+ "name": "synthetic relation fixture"
+ }))
+ .unwrap();
+ db.nostr_profile_create(&input).unwrap().result.id
+ })
+ .collect();
+ let relays: Vec<_> = [UNASSIGNED, TARGET_ONLY, OTHER_ONLY, SHARED]
+ .into_iter()
+ .map(|url| {
+ let input: INostrRelayCreate = serde_json::from_value(json!({ "url": url })).unwrap();
+ db.nostr_relay_create(&input).unwrap().result.id
+ })
+ .collect();
+ for (profile, relay) in [(0, 1), (1, 2), (0, 3), (1, 3)] {
+ db.nostr_profile_relay_set(&INostrProfileRelayRelation {
+ nostr_profile: NostrProfileQueryBindValues::Id {
+ id: profiles[profile].clone(),
+ },
+ nostr_relay: NostrRelayQueryBindValues::Id {
+ id: relays[relay].clone(),
+ },
+ })
+ .unwrap();
+ }
+ db
+}
+
+fn off_profile(public_key: String) -> NostrRelayFindManyRel {
+ NostrRelayFindManyRel::OffProfile(NostrRelayProfileArgs { public_key })
+}
+
+fn many(db: &Database, rel: NostrRelayFindManyRel) -> BTreeSet<String> {
+ let rows = db
+ .nostr_relay_find_many(&INostrRelayFindMany::Rel { rel })
+ .unwrap()
+ .results;
+ let count = rows.len();
+ let urls: BTreeSet<_> = rows.into_iter().map(|row| row.url).collect();
+ assert_eq!(
+ urls.len(),
+ count,
+ "relation reads must not duplicate relays"
+ );
+ urls
+}
+
+fn urls(expected: &[&str]) -> BTreeSet<String> {
+ expected.iter().map(|url| (*url).to_owned()).collect()
+}
+
+#[test]
+fn off_profile_is_the_complete_membership_complement() {
+ let db = fixture();
+ assert_eq!(
+ many(
+ &db,
+ NostrRelayFindManyRel::OnProfile(NostrRelayProfileArgs {
+ public_key: target_key(),
+ })
+ ),
+ urls(&[TARGET_ONLY, SHARED])
+ );
+ assert_eq!(
+ many(&db, off_profile(target_key())),
+ urls(&[UNASSIGNED, OTHER_ONLY])
+ );
+}
+
+#[test]
+fn absent_profile_has_no_members_and_all_relays_in_the_complement() {
+ let db = fixture();
+ assert!(
+ many(
+ &db,
+ NostrRelayFindManyRel::OnProfile(NostrRelayProfileArgs {
+ public_key: "c".repeat(64),
+ })
+ )
+ .is_empty()
+ );
+ assert_eq!(
+ many(&db, off_profile("c".repeat(64))),
+ urls(&[UNASSIGNED, TARGET_ONLY, OTHER_ONLY, SHARED])
+ );
+}
+
+#[test]
+fn single_lookup_and_delete_preserve_assigned_relays() {
+ let db = fixture();
+ let mut remaining = urls(&[UNASSIGNED, OTHER_ONLY]);
+ while !remaining.is_empty() {
+ let selected = db
+ .nostr_relay_find_one(&INostrRelayFindOne::Rel(INostrRelayFindOneRelArgs {
+ rel: off_profile(target_key()),
+ }))
+ .unwrap()
+ .result
+ .expect("the complement contains a relay");
+ assert!(remaining.contains(&selected.url));
+ db.nostr_relay_delete(&INostrRelayDelete::Rel(INostrRelayFindOneRelArgs {
+ rel: off_profile(target_key()),
+ }))
+ .unwrap();
+ let after = many(&db, off_profile(target_key()));
+ assert_eq!(after.len(), remaining.len() - 1);
+ assert!(after.is_subset(&remaining));
+ remaining = after;
+ }
+ assert_eq!(
+ many(
+ &db,
+ NostrRelayFindManyRel::OnProfile(NostrRelayProfileArgs {
+ public_key: target_key(),
+ })
+ ),
+ urls(&[TARGET_ONLY, SHARED])
+ );
+ let missing = db.nostr_relay_delete(&INostrRelayDelete::Rel(INostrRelayFindOneRelArgs {
+ rel: off_profile(target_key()),
+ }));
+ assert!(matches!(missing, Err(error) if matches!(error.error, SqlError::NotFound(_))));
+}