Skip to content

Commit b91ac88

Browse files
npub1anev4pmmnnkaq8qqrs4wz7vd9je27pmvkr8gycrszdgwzutag6cshucm90tellaho
andcommitted
fix(desktop): make team agent_pubkeys Option on the wire — omission must not wipe membership
kind:30176 events serialized agent_pubkeys with skip_serializing_if = Vec::is_empty, making 'old client omitted the field' and 'new client explicitly emptied the team' byte-identical. apply_inbound_team blindly overwrote, so an old client merely renaming a team wiped its agent membership on every newer device. - TeamEventContent.agent_pubkeys is now Option<Vec<String>>. The Option is PERMANENT wire semantics, not a transitional shim (doc comment says so): None = publisher predates the field, Some(vec![]) = explicitly emptied. New clients always publish Some, even when empty. - apply_inbound_team overwrites local membership only on Some; None preserves local. Fresh inserts unwrap_or_default. - persona_ids deliberately left unwrapped, with a doc comment explaining why: it predates agent_pubkeys, every shipped client emits it, so omitted genuinely means empty — wrapping it would freeze stale members when an old client legitimately empties a team. - Tests: parse test flips to assert None (not empty); new tests cover always-publish-Some, None-preserves-local reconcile, and Some(vec![])-clears reconcile. Fixes the pre-merge blocker from the tho/agents-first-remove-personas review (Chuckie/Angelica/Tommy consensus). Zero events with the ambiguous shape existed in the wild before this fix. Co-authored-by: Taylor Ho <[email protected]> Signed-off-by: Taylor Ho <[email protected]>
1 parent 1e707d6 commit b91ac88

3 files changed

Lines changed: 81 additions & 13 deletions

File tree

‎desktop/src-tauri/src/commands/personas/inbound_tests.rs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn team_content(name: &str) -> TeamEventContent {
336336
name: name.to_string(),
337337
description: Some("remote desc".to_string()),
338338
persona_ids: vec!["p-remote-1".to_string(), "p-remote-2".to_string()],
339-
agent_pubkeys: vec!["remote-agent-pk".to_string()],
339+
agent_pubkeys: Some(vec!["remote-agent-pk".to_string()]),
340340
}
341341
}
342342

@@ -370,6 +370,40 @@ fn inbound_team_match_patches_shared_preserves_local() {
370370
assert_eq!(t.created_at, "2025-01-01T00:00:00Z");
371371
}
372372

373+
#[test]
374+
fn inbound_team_none_agent_pubkeys_preserves_local_membership() {
375+
// An event from a client that predates `agent_pubkeys` parses to `None`.
376+
// The reconcile must NOT wipe local agent membership — the old client is
377+
// ignorant of the field, not asserting emptiness.
378+
let mut teams = vec![local_team()];
379+
let mut content = team_content("Renamed By Old Client");
380+
content.agent_pubkeys = None;
381+
apply_inbound_team(&mut teams, TEAM_ID.to_string(), content);
382+
383+
let t = &teams[0];
384+
assert_eq!(t.name, "Renamed By Old Client", "shared fields still apply");
385+
assert_eq!(
386+
t.agent_pubkeys,
387+
vec!["local-agent-pk".to_string()],
388+
"None must preserve local agent membership, not wipe it"
389+
);
390+
}
391+
392+
#[test]
393+
fn inbound_team_explicit_empty_agent_pubkeys_clears_local_membership() {
394+
// `Some(vec![])` is a new client explicitly emptying the team — that DOES
395+
// overwrite, unlike `None`.
396+
let mut teams = vec![local_team()];
397+
let mut content = team_content("Emptied Team");
398+
content.agent_pubkeys = Some(vec![]);
399+
apply_inbound_team(&mut teams, TEAM_ID.to_string(), content);
400+
401+
assert!(
402+
teams[0].agent_pubkeys.is_empty(),
403+
"explicit Some(vec![]) must clear local agent membership"
404+
);
405+
}
406+
373407
#[test]
374408
fn inbound_team_no_match_inserts_idempotently() {
375409
let mut teams = vec![local_team()];

‎desktop/src-tauri/src/commands/personas/mod.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,22 @@ fn apply_inbound_team(teams: &mut Vec<TeamRecord>, d_tag: String, inbound: TeamE
676676
local.name = inbound.name;
677677
local.description = inbound.description;
678678
local.persona_ids = inbound.persona_ids;
679-
local.agent_pubkeys = inbound.agent_pubkeys;
679+
// `None` means the event came from a client that predates
680+
// `agent_pubkeys` — its true membership is unknown, so preserve
681+
// local. Only `Some` (including `Some(vec![])` = explicitly
682+
// emptied) overwrites. See `TeamEventContent` for the wire rules.
683+
if let Some(agent_pubkeys) = inbound.agent_pubkeys {
684+
local.agent_pubkeys = agent_pubkeys;
685+
}
680686
}
681687
None => teams.push(TeamRecord {
682688
id: d_tag,
683689
name: inbound.name,
684690
description: inbound.description,
685691
persona_ids: inbound.persona_ids,
686-
agent_pubkeys: inbound.agent_pubkeys,
692+
// Fresh insert has no local membership to preserve; `None` from a
693+
// pre-field client simply means no known agent members.
694+
agent_pubkeys: inbound.agent_pubkeys.unwrap_or_default(),
687695
is_builtin: false,
688696
source_dir: None,
689697
is_symlink: false,

‎desktop/src-tauri/src/managed_agents/team_events.rs‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ pub struct TeamEventContent {
2424
pub name: String,
2525
#[serde(default, skip_serializing_if = "Option::is_none")]
2626
pub description: Option<String>,
27+
/// Pack persona members. Deliberately NOT `Option`-wrapped: this field
28+
/// predates `agent_pubkeys`, so every shipped client understands it and
29+
/// omits it only when the list is genuinely empty — omitted = empty.
30+
/// Wrapping it would make an old client's legitimate empty-out parse as
31+
/// `None` and freeze stale members on newer devices.
2732
#[serde(default, skip_serializing_if = "Vec::is_empty")]
2833
pub persona_ids: Vec<String>,
29-
/// Managed-agent members by pubkey. Additive field — events published by
30-
/// older clients simply omit it and parse to an empty list.
31-
#[serde(default, skip_serializing_if = "Vec::is_empty")]
32-
pub agent_pubkeys: Vec<String>,
34+
/// Managed-agent members by pubkey. `Option` is PERMANENT wire semantics,
35+
/// not a transitional shim: `None` = publisher predates this field (its
36+
/// true membership is unknown — reconcile must preserve local), while
37+
/// `Some(vec![])` = explicitly emptied. New clients always publish
38+
/// `Some(...)`. "Cleaning up" the Option later reintroduces the bug where
39+
/// an old client's event silently wipes team agent membership.
40+
#[serde(default, skip_serializing_if = "Option::is_none")]
41+
pub agent_pubkeys: Option<Vec<String>>,
3342
}
3443

3544
/// Project a `TeamRecord` onto the content fields published in team events.
@@ -40,7 +49,9 @@ pub fn team_event_content(record: &TeamRecord) -> TeamEventContent {
4049
name: record.name.clone(),
4150
description: record.description.clone(),
4251
persona_ids: record.persona_ids.clone(),
43-
agent_pubkeys: record.agent_pubkeys.clone(),
52+
// Always `Some`, even when empty — `None` is reserved for events from
53+
// clients that predate the field (see the struct doc comment).
54+
agent_pubkeys: Some(record.agent_pubkeys.clone()),
4455
}
4556
}
4657

@@ -152,18 +163,33 @@ mod tests {
152163
assert!(json.contains("\"agent_pubkeys\""));
153164
let restored: TeamEventContent = serde_json::from_str(&json).unwrap();
154165
assert_eq!(restored, event_content);
155-
assert_eq!(restored.agent_pubkeys, vec!["a".repeat(64)]);
166+
assert_eq!(restored.agent_pubkeys, Some(vec!["a".repeat(64)]));
156167
}
157168

158169
#[test]
159-
fn content_from_old_clients_without_agent_pubkeys_parses_empty() {
160-
// agent_pubkeys is additive: events published before the field existed
161-
// must still parse, with membership defaulting to no agent members.
170+
fn content_from_old_clients_without_agent_pubkeys_parses_none() {
171+
// Events published before `agent_pubkeys` existed must still parse.
172+
// The field reads back `None` — NOT an empty list — so the reconcile
173+
// can distinguish "publisher predates the field" from "explicitly
174+
// emptied" and preserve local membership (see apply_inbound_team).
162175
let legacy = r#"{"name":"Old Team","persona_ids":["p1"]}"#;
163176
let restored: TeamEventContent = serde_json::from_str(legacy).unwrap();
164177
assert_eq!(restored.name, "Old Team");
165178
assert_eq!(restored.persona_ids, vec!["p1"]);
166-
assert!(restored.agent_pubkeys.is_empty());
179+
assert_eq!(restored.agent_pubkeys, None);
180+
}
181+
182+
#[test]
183+
fn content_publishes_some_even_when_agent_pubkeys_empty() {
184+
// New clients must always publish `Some`, even for an empty list —
185+
// `Some(vec![])` is the explicit "no agent members" signal that old
186+
// clients can never produce.
187+
let mut team = sample_team();
188+
team.agent_pubkeys = vec![];
189+
let event_content = team_event_content(&team);
190+
assert_eq!(event_content.agent_pubkeys, Some(vec![]));
191+
let json = serde_json::to_string(&event_content).unwrap();
192+
assert!(json.contains("\"agent_pubkeys\":[]"));
167193
}
168194

169195
#[test]

0 commit comments

Comments
 (0)