@@ -24,10 +24,27 @@ pub struct TeamEventContent {
2424 pub name : String ,
2525 #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
2626 pub description : Option < String > ,
27+ /// Runtime-layered instructions. `Option` is PERMANENT wire semantics
28+ /// (not a transitional shim): absent = publisher predates always-publish
29+ /// (true value unknown — reconcile must preserve local), `null` =
30+ /// explicitly cleared, a string = set. New clients always publish this
31+ /// field (outer always `Some`), using `null` for "no instructions" so
32+ /// that state round-trips instead of being read back as "unknown".
33+ #[ serde(
34+ default ,
35+ deserialize_with = "crate::util::double_option" ,
36+ skip_serializing_if = "Option::is_none"
37+ ) ]
38+ pub instructions : Option < Option < String > > ,
39+ /// Pack persona members. `Option` is PERMANENT wire semantics, not a
40+ /// transitional shim: `None` = publisher predates always-publish (its
41+ /// true membership is unknown — reconcile must preserve local), while
42+ /// `Some(vec![])` = explicitly emptied. New clients always publish
43+ /// `Some(...)`, even when empty. "Cleaning up" the Option later
44+ /// reintroduces the bug where an old client's event silently wipes team
45+ /// membership (see the Sietch Tabr incident).
2746 #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
28- pub instructions : Option < String > ,
29- #[ serde( default , skip_serializing_if = "Vec::is_empty" ) ]
30- pub persona_ids : Vec < String > ,
47+ pub persona_ids : Option < Vec < String > > ,
3148}
3249
3350/// Project a `TeamRecord` onto the content fields published in team events.
@@ -37,8 +54,11 @@ pub fn team_event_content(record: &TeamRecord) -> TeamEventContent {
3754 TeamEventContent {
3855 name : record. name . clone ( ) ,
3956 description : record. description . clone ( ) ,
40- instructions : record. instructions . clone ( ) ,
41- persona_ids : record. persona_ids . clone ( ) ,
57+ // Always `Some`, even when the inner value is absent — `None` is
58+ // reserved for events from clients that predate always-publish (see
59+ // the struct doc comments).
60+ instructions : Some ( record. instructions . clone ( ) ) ,
61+ persona_ids : Some ( record. persona_ids . clone ( ) ) ,
4262 }
4363}
4464
@@ -152,6 +172,75 @@ mod tests {
152172 assert_eq ! ( restored, event_content) ;
153173 }
154174
175+ // ── persona_ids wire semantics (omission must not wipe membership) ────
176+
177+ #[ test]
178+ fn content_from_old_clients_without_persona_ids_parses_none ( ) {
179+ // Events published before always-publish must still parse. The
180+ // field reads back `None` — NOT an empty list — so the reconcile
181+ // can distinguish "publisher predates always-publish" from
182+ // "explicitly emptied" and preserve local membership (see
183+ // apply_inbound_team). This is the exact shape of the Sietch Tabr
184+ // wipe event.
185+ let legacy = r#"{"name":"Old Team"}"# ;
186+ let restored: TeamEventContent = serde_json:: from_str ( legacy) . unwrap ( ) ;
187+ assert_eq ! ( restored. name, "Old Team" ) ;
188+ assert_eq ! ( restored. persona_ids, None ) ;
189+ }
190+
191+ #[ test]
192+ fn content_publishes_some_even_when_persona_ids_empty ( ) {
193+ // New clients must always publish `Some`, even for an empty list —
194+ // `Some(vec![])` is the explicit "no persona members" signal that a
195+ // pre-fix client can never produce.
196+ let mut team = sample_team ( ) ;
197+ team. persona_ids = vec ! [ ] ;
198+ let event_content = team_event_content ( & team) ;
199+ assert_eq ! ( event_content. persona_ids, Some ( vec![ ] ) ) ;
200+ let json = serde_json:: to_string ( & event_content) . unwrap ( ) ;
201+ assert ! ( json. contains( "\" persona_ids\" :[]" ) ) ;
202+ }
203+
204+ // ── instructions wire semantics (tri-state: absent/null/value) ────────
205+ //
206+ // Deserialized from raw JSON strings, not constructed enum states
207+ // directly — the bug is specifically in how serde maps JSON shapes onto
208+ // the tri-state, so the test must exercise that mapping (mirrors
209+ // `double_option_tristate` in `util.rs`).
210+
211+ #[ test]
212+ fn content_from_old_clients_without_instructions_parses_none ( ) {
213+ let legacy = r#"{"name":"Old Team","persona_ids":["p1"]}"# ;
214+ let restored: TeamEventContent = serde_json:: from_str ( legacy) . unwrap ( ) ;
215+ assert_eq ! ( restored. instructions, None ) ;
216+ }
217+
218+ #[ test]
219+ fn content_with_explicit_null_instructions_parses_some_none ( ) {
220+ let json = r#"{"name":"Team","persona_ids":["p1"],"instructions":null}"# ;
221+ let restored: TeamEventContent = serde_json:: from_str ( json) . unwrap ( ) ;
222+ assert_eq ! ( restored. instructions, Some ( None ) ) ;
223+ }
224+
225+ #[ test]
226+ fn content_with_instructions_value_parses_some_some ( ) {
227+ let json = r#"{"name":"Team","persona_ids":["p1"],"instructions":"Coordinate."}"# ;
228+ let restored: TeamEventContent = serde_json:: from_str ( json) . unwrap ( ) ;
229+ assert_eq ! ( restored. instructions, Some ( Some ( "Coordinate." . to_string( ) ) ) ) ;
230+ }
231+
232+ #[ test]
233+ fn content_publishes_some_none_when_instructions_absent_locally ( ) {
234+ // New clients must always publish the field — `null` is the explicit
235+ // "no instructions" signal that a pre-fix client can never produce.
236+ let mut team = sample_team ( ) ;
237+ team. instructions = None ;
238+ let event_content = team_event_content ( & team) ;
239+ assert_eq ! ( event_content. instructions, Some ( None ) ) ;
240+ let json = serde_json:: to_string ( & event_content) . unwrap ( ) ;
241+ assert ! ( json. contains( "\" instructions\" :null" ) ) ;
242+ }
243+
155244 #[ test]
156245 fn build_team_delete_has_single_a_tag_no_e_tag ( ) {
157246 const OWNER : & str = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798" ;
0 commit comments