heimdall-consent: live answers need text; ties on received_at break by bytewise event_id

Rulings: Heimdall PR #6 comment 759. Golden vectors for empty live text, empty revoked,
and received_at ties in both input orders.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-06 19:42:24 +00:00
commit 5ab6a6181f
3 changed files with 99 additions and 3 deletions

View file

@ -108,7 +108,8 @@ pub struct Answer {
/// One-way: set by redaction, never cleared.
#[cultcache(key = 6, default)]
pub exited: bool,
/// `origin_server_ts` of the reply, unix milliseconds UTC.
/// Assigned by the collector on receipt, unix milliseconds UTC (the reply's `origin_server_ts`
/// in practice); ties are broken by `supersedes`.
#[cultcache(key = 7)]
pub received_at: i64,
#[cultcache(key = 8)]
@ -141,6 +142,9 @@ impl Answer {
if self.exited && self.text.is_some() {
bail!("an exited answer holds no text");
}
if !self.exited && self.text.as_deref().is_none_or(str::is_empty) {
bail!("a live answer needs non-empty text; empty text is valid only on a revoked answer");
}
if let Some(hash) = &self.text_hash {
if !self.exited || hash.len() != 32 {
bail!("text_hash is 32 bytes and only on an exited answer");
@ -149,6 +153,13 @@ impl Answer {
Ok(())
}
/// Replacement rule for two answers at one key: the later reply is the greater `(received_at,
/// event_id)`. `received_at` is assigned by the collector; a tie breaks by `event_id` compared
/// bytewise, the greater winning (Heimdall PR #6 comment 759). Insertion order never decides.
pub fn supersedes(&self, earlier: &Answer) -> bool {
(self.received_at, self.event_id.as_bytes()) > (earlier.received_at, earlier.event_id.as_bytes())
}
/// Redaction is exit: text erased, `exited` set, the tombstone hash kept. The hash comes from the
/// member-side service, which holds the salt; this crate never computes it. A reply the collector
/// never collected has no text and gets no tombstone, so an answer without `text` is refused.

View file

@ -31,6 +31,15 @@ fn put_answer(cache: &mut CultCache, a: &Answer) {
cache.put(Answer::cache_key(&a.experiment_id, &a.sample_id, &a.member_id).unwrap(), a).unwrap();
}
/// The collector's replace rule: an incoming answer replaces the stored one only if it is the later reply.
fn put_if_later(cache: &mut CultCache, a: &Answer) {
let k = Answer::cache_key(&a.experiment_id, &a.sample_id, &a.member_id).unwrap();
if cache.get::<Answer>(&k).unwrap().is_some_and(|old| !a.supersedes(&old)) {
return;
}
cache.put(k, a).unwrap();
}
fn training_events(cache: &CultCache) -> Vec<String> {
let mut ids: Vec<String> = cache
.get_all::<Answer>()
@ -59,7 +68,7 @@ fn golden_vectors() {
_ => cases.push((f[0], vec![f])),
}
}
assert_eq!(cases.len(), 5, "fixture shrank");
assert_eq!(cases.len(), 9, "fixture shrank");
for (name, rows) in cases {
let dir = tempfile::tempdir().unwrap();
let mut cache = new_cache(&dir);
@ -100,7 +109,42 @@ fn golden_vectors() {
text_hash: None,
};
answer.validate().unwrap();
put_answer(&mut cache, &answer);
put_if_later(&mut cache, &answer);
}
"reply_rejected" => {
let answer = Answer {
sample_id: sample.into(),
experiment_id: room.into(),
member_id: member.into(),
notice_version: nv,
scope: scope(sc),
text: Some(payload.into()),
exited: false,
received_at: ts,
event_id: event.into(),
text_hash: None,
};
assert!(answer.validate().is_err(), "{ctx}: must be refused");
}
"answer_revoked" => {
let answer = Answer {
sample_id: sample.into(),
experiment_id: room.into(),
member_id: member.into(),
notice_version: nv,
scope: scope(sc),
text: None,
exited: true,
received_at: ts,
event_id: event.into(),
text_hash: if payload.is_empty() { None } else { Some(hex(payload)) },
};
answer.validate().unwrap();
put_if_later(&mut cache, &answer);
}
"expect_winner" => {
let a = cache.get_required::<Answer>(&Answer::cache_key(room, sample, member).unwrap()).unwrap();
assert_eq!((a.event_id.as_str(), a.text.as_deref()), (event, Some(payload)), "{ctx}");
}
"redact" => {
let k = Answer::cache_key(room, sample, member).unwrap();
@ -269,6 +313,8 @@ fn validate_enforces_the_representation_rules() {
("version zero", Box::new(|a| a.notice_version = Some(0))),
("version without scope", Box::new(|a| a.scope = None)),
("scope without version", Box::new(|a| a.notice_version = None)),
("live without text", Box::new(|a| a.text = None)),
("live with empty text", Box::new(|a| a.text = Some(String::new()))),
("exited with text", Box::new(|a| a.exited = true)),
("hash on a live answer", Box::new(|a| a.text_hash = Some(vec![0; 32]))),
("short hash", Box::new(|a| {
@ -282,6 +328,10 @@ fn validate_enforces_the_representation_rules() {
break_it(&mut a);
assert!(a.validate().is_err(), "{name}");
}
let mut revoked = answer();
revoked.text = None;
revoked.exited = true;
assert!(revoked.validate().is_ok(), "empty text is valid on a revoked answer");
let mut unscoped = answer();
unscoped.notice_version = None;
unscoped.scope = None;
@ -297,3 +347,13 @@ fn validate_enforces_the_representation_rules() {
assert!(acc.validate().is_err());
assert!(MemberTerms { member_id: "m".into(), terms_version: String::new(), terms_accepted_at: 1 }.validate().is_err());
}
#[test]
fn later_reply_is_greater_received_at_then_bytewise_event_id() {
let at = |t, e: &str| Answer { received_at: t, event_id: e.into(), ..answer() };
assert!(at(2, "a").supersedes(&at(1, "z")));
assert!(!at(1, "z").supersedes(&at(2, "a")));
assert!(at(1, "a").supersedes(&at(1, "B")), "bytewise: 0x61 > 0x42");
assert!(at(1, "z").supersedes(&at(1, "aa")), "bytewise, not by length");
assert!(!at(1, "e").supersedes(&at(1, "e")));
}

View file

@ -3,6 +3,8 @@
# reply: first reply under a (room, member, notice_version) writes an acceptance, then the answer replaces any earlier one at (room, sample_id, member); payload is the text.
# answer_raw: stores the answer with no acceptance written. redact: replaces the answer whose event_id matches with its tombstone; payload is the 32-byte tombstone hash as 64 hex chars (the member-side service's output; the crate never computes it).
# A reply with no established notice version or scope is stored with both absent (doc L82-89, Answer record), writes no acceptance, and is excluded. answer_raw with a version but no acceptance models a lost or never-written acceptance: still excluded (doc L120).
# reply_rejected: validate must refuse the answer (nothing stored). answer_revoked: stores an exited answer with no text; payload is the tombstone hash (empty means none). expect_winner: the stored answer's event_id and text must equal event and payload.
# Replacement at one key: the later reply is the greater (received_at, event_id), event_id compared bytewise; input order never decides (doc L78-80 and Heimdall PR #6 comment 759).
# terms: member field terms_version (payload) and terms_accepted_at (ts). expect_*: payload is what must hold.
# Expected values are written by hand from docs/answer-collector-consent.md (Heimdall PR #6, doc commit 2364979), not computed by the crate.
versioned-key,reply,m1,room1,1,train,s1@1,first,100,e1
@ -39,3 +41,26 @@ version-change,expect_train,,,,,,e14,,
version-change,reply,m10,room1,2,train,s1@3,first reply under v2,300,e16
version-change,expect_counts,,,,,,answers=3;acceptances=2,,
version-change,expect_train,,,,,,e14;e16,,
empty-live-rejected,reply_rejected,m11,room1,,,s1@1,,100,e17
empty-live-rejected,expect_counts,,,,,,answers=0;acceptances=0,,
empty-live-rejected,reply,m11,room1,,,s1@1,now filled,110,e18
empty-live-rejected,expect_winner,m11,room1,,,s1@1,now filled,,e18
empty-revoked,answer_revoked,m12,room1,1,train,s1@1,,100,e19
empty-revoked,answer_revoked,m13,room1,1,train,s1@1,a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90,100,e20
empty-revoked,expect_counts,,,,,,answers=2;acceptances=0,,
empty-revoked,expect_train,,,,,,,,
tie-greater-first,reply,m14,room1,,,s1@1,upper-B,500,B
tie-greater-first,reply,m14,room1,,,s1@1,lower-a,500,a
tie-greater-first,reply,m15,room1,,,s1@1,short-z,500,z
tie-greater-first,reply,m15,room1,,,s1@1,long-aa,500,aa
tie-greater-first,reply,m16,room1,,,s1@1,newer-ts-first,600,a
tie-greater-first,reply,m16,room1,,,s1@1,older-ts-last,500,z
tie-greater-first,expect_winner,m14,room1,,,s1@1,lower-a,,a
tie-greater-first,expect_winner,m15,room1,,,s1@1,short-z,,z
tie-greater-first,expect_winner,m16,room1,,,s1@1,newer-ts-first,,a
tie-greater-last,reply,m14,room1,,,s1@1,lower-a,500,a
tie-greater-last,reply,m14,room1,,,s1@1,upper-B,500,B
tie-greater-last,reply,m15,room1,,,s1@1,long-aa,500,aa
tie-greater-last,reply,m15,room1,,,s1@1,short-z,500,z
tie-greater-last,expect_winner,m14,room1,,,s1@1,lower-a,,a
tie-greater-last,expect_winner,m15,room1,,,s1@1,short-z,,z

1 # case,op,member,experiment,notice_version,scope,sample_id,payload,ts,event_id
3 # reply: first reply under a (room, member, notice_version) writes an acceptance, then the answer replaces any earlier one at (room, sample_id, member); payload is the text.
4 # answer_raw: stores the answer with no acceptance written. redact: replaces the answer whose event_id matches with its tombstone; payload is the 32-byte tombstone hash as 64 hex chars (the member-side service's output; the crate never computes it).
5 # A reply with no established notice version or scope is stored with both absent (doc L82-89, Answer record), writes no acceptance, and is excluded. answer_raw with a version but no acceptance models a lost or never-written acceptance: still excluded (doc L120).
6 # reply_rejected: validate must refuse the answer (nothing stored). answer_revoked: stores an exited answer with no text; payload is the tombstone hash (empty means none). expect_winner: the stored answer's event_id and text must equal event and payload.
7 # Replacement at one key: the later reply is the greater (received_at, event_id), event_id compared bytewise; input order never decides (doc L78-80 and Heimdall PR #6 comment 759).
8 # terms: member field terms_version (payload) and terms_accepted_at (ts). expect_*: payload is what must hold.
9 # Expected values are written by hand from docs/answer-collector-consent.md (Heimdall PR #6, doc commit 2364979), not computed by the crate.
10 versioned-key,reply,m1,room1,1,train,s1@1,first,100,e1
41 version-change,reply,m10,room1,2,train,s1@3,first reply under v2,300,e16
42 version-change,expect_counts,,,,,,answers=3;acceptances=2,,
43 version-change,expect_train,,,,,,e14;e16,,
44 empty-live-rejected,reply_rejected,m11,room1,,,s1@1,,100,e17
45 empty-live-rejected,expect_counts,,,,,,answers=0;acceptances=0,,
46 empty-live-rejected,reply,m11,room1,,,s1@1,now filled,110,e18
47 empty-live-rejected,expect_winner,m11,room1,,,s1@1,now filled,,e18
48 empty-revoked,answer_revoked,m12,room1,1,train,s1@1,,100,e19
49 empty-revoked,answer_revoked,m13,room1,1,train,s1@1,a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90,100,e20
50 empty-revoked,expect_counts,,,,,,answers=2;acceptances=0,,
51 empty-revoked,expect_train,,,,,,,,
52 tie-greater-first,reply,m14,room1,,,s1@1,upper-B,500,B
53 tie-greater-first,reply,m14,room1,,,s1@1,lower-a,500,a
54 tie-greater-first,reply,m15,room1,,,s1@1,short-z,500,z
55 tie-greater-first,reply,m15,room1,,,s1@1,long-aa,500,aa
56 tie-greater-first,reply,m16,room1,,,s1@1,newer-ts-first,600,a
57 tie-greater-first,reply,m16,room1,,,s1@1,older-ts-last,500,z
58 tie-greater-first,expect_winner,m14,room1,,,s1@1,lower-a,,a
59 tie-greater-first,expect_winner,m15,room1,,,s1@1,short-z,,z
60 tie-greater-first,expect_winner,m16,room1,,,s1@1,newer-ts-first,,a
61 tie-greater-last,reply,m14,room1,,,s1@1,lower-a,500,a
62 tie-greater-last,reply,m14,room1,,,s1@1,upper-B,500,B
63 tie-greater-last,reply,m15,room1,,,s1@1,long-aa,500,aa
64 tie-greater-last,reply,m15,room1,,,s1@1,short-z,500,z
65 tie-greater-last,expect_winner,m14,room1,,,s1@1,lower-a,,a
66 tie-greater-last,expect_winner,m15,room1,,,s1@1,short-z,,z