Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(web): state the group that existed before the board on it

0xken15f80e252184ae
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 99a2c9200..e3db6287b 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -29,6 +29,7 @@ import '../viewer/group_sync.dart'
GroupRoster,
GroupSyncOutcome,
adoptBoard,
+ boardNews,
adoptHandedRoster,
admitGroupMember,
handoffRoster,
@@ -5710,6 +5711,14 @@ class AppNotifier extends ChangeNotifier {
if (board.entries case final entries?) {
news = true;
await _afterGroupSync(await adoptBoard(keys, entries));
+ // And what this device knows that the board lacks in its words — a group from before the
+ // board, or one grown by the relay swap alone. The board takes 32 at a time.
+ final mine = await boardNews(keys, entries);
+ for (var i = 0; i < mine.length; i += 32) {
+ await _vouch(
+ mine.sublist(i, i + 32 > mine.length ? mine.length : i + 32),
+ );
+ }
}
}
final online = [
@@ -7745,8 +7754,11 @@ class AppNotifier extends ChangeNotifier {
systemNotifications.withdraw(machineId, agentId);
agentAlerts.dismiss(
AgentAlert(
- machineId: machineId, agentId: agentId,
- title: '', kind: AlertKind.done, at: DateTime.now(),
+ machineId: machineId,
+ agentId: agentId,
+ title: '',
+ kind: AlertKind.done,
+ at: DateTime.now(),
),
);
_announceAgentSeen(machineId, agentId, readToken);
@@ -7754,7 +7766,11 @@ class AppNotifier extends ChangeNotifier {
/// Reading is not answering. A device receipt clears only the exact message
/// it displayed; the pending question and all pane/focus state remain intact.
- void readAgentNotification(String machineId, String agentId, {String? readToken}) {
+ void readAgentNotification(
+ String machineId,
+ String agentId, {
+ String? readToken,
+ }) {
if (readToken != null &&
agentUnread.readTokenFor(machineId, agentId) != readToken) {
return;
@@ -7766,7 +7782,9 @@ class AppNotifier extends ChangeNotifier {
..remove(key)
..[key] = question.requestId;
while (_readQuestionNotifications.length > AgentUnread.capacity) {
- _readQuestionNotifications.remove(_readQuestionNotifications.keys.first);
+ _readQuestionNotifications.remove(
+ _readQuestionNotifications.keys.first,
+ );
}
}
_forgetUnread(machineId, agentId);
@@ -7801,7 +7819,8 @@ class AppNotifier extends ChangeNotifier {
unawaited(
_conn(machineId)
.sendTerminalFrame('agent_seen', {
- 'agentId': agentId, 'readToken': ?readToken,
+ 'agentId': agentId,
+ 'readToken': ?readToken,
})
.catchError((_) => false),
);
@@ -13271,8 +13290,10 @@ class AppNotifier extends ChangeNotifier {
final readId = payload['agentId'];
final readMachine = payload['machineId'];
final readToken = payload['readToken'];
- if (readId is String && readMachine is String &&
- readToken is String && readToken.isNotEmpty) {
+ if (readId is String &&
+ readMachine is String &&
+ readToken is String &&
+ readToken.isNotEmpty) {
readAgentNotification(readMachine, readId, readToken: readToken);
}
break;
@@ -13389,7 +13410,9 @@ class AppNotifier extends ChangeNotifier {
final goneId = _eventAgentId(machine, event, payload);
if (goneId != null) {
agentUnread.forget(machineId, goneId);
- _readQuestionNotifications.remove(AgentUnread.keyFor(machineId, goneId));
+ _readQuestionNotifications.remove(
+ AgentUnread.keyFor(machineId, goneId),
+ );
}
final agentId = _eventAgentId(machine, event, payload);
if (agentId != null) {
@@ -13436,7 +13459,9 @@ class AppNotifier extends ChangeNotifier {
// reconnect and when attaching to a turn that was already mid-dialog, and a window
// that beeped at those would sound an alarm every time the network hiccuped.
if (!repeat && !questionNotificationRead(machineId, agentId)) {
- _readQuestionNotifications.remove(AgentUnread.keyFor(machineId, agentId));
+ _readQuestionNotifications.remove(
+ AgentUnread.keyFor(machineId, agentId),
+ );
_raiseAlert(machine, agentId, AlertKind.needsYou);
}
}
@@ -13452,7 +13477,9 @@ class AppNotifier extends ChangeNotifier {
final open = machine.blockedAgents[agentId];
if (open != null && open.requestId == requestId) {
machine.blockedAgents.remove(agentId);
- _readQuestionNotifications.remove(AgentUnread.keyFor(machineId, agentId));
+ _readQuestionNotifications.remove(
+ AgentUnread.keyFor(machineId, agentId),
+ );
// An old question close cannot erase a newer completed result.
if (agentUnread.kindFor(machineId, agentId) == AlertKind.needsYou) {
_forgetUnread(machineId, agentId);
diff --git a/desktop/lib/viewer/group_sync.dart b/desktop/lib/viewer/group_sync.dart
index 96939d62a..92571b5c4 100644
--- a/desktop/lib/viewer/group_sync.dart
+++ b/desktop/lib/viewer/group_sync.dart
@@ -608,3 +608,66 @@ Future<GroupSyncOutcome> adoptBoard(
});
return _apply(keys, merged);
}
+
+/// What this device knows that the board does not yet carry in its own words: each member of its
+/// roster (itself aside) that no vouch it signed states at that stamp or later, and each removal of a
+/// key the board still vouches for. Posting it is how a group formed before the board existed — or
+/// grown by the relay swap alone — reaches devices that share no machine with it.
+///
+/// [entries] is the board as read. Labels are cut to the board's 60 characters; a machine with no id
+/// and a stamp the board would refuse are left out.
+Future<List<Map<String, Object>>> boardNews(
+ ViewerKeyStore keys,
+ Object? entries, {
+ DateTime? now,
+}) async {
+ final selfPub = b64e((await keys.identity()).pub);
+ final roster = await _seeded(keys, selfPub);
+ final stated = <String, int>{};
+ final mentioned = <String, int>{};
+ if (entries is List) {
+ for (final e in entries) {
+ if (e is! Map || e['subject'] is! Map) continue;
+ final subject = e['subject'] as Map;
+ final pub = subject['pub'], at = subject['at'];
+ if (pub is! String || at is! int) continue;
+ if (subject['removed'] != true) {
+ mentioned[pub] = at > (mentioned[pub] ?? -1) ? at : mentioned[pub]!;
+ }
+ if (e['signer'] == selfPub) {
+ stated[pub] = at > (stated[pub] ?? -1) ? at : stated[pub]!;
+ }
+ }
+ }
+ final latest = (now ?? DateTime.now())
+ .add(_maxClockSkew)
+ .millisecondsSinceEpoch;
+ final removedAt = {for (final t in roster.removed) t.pub: t.at};
+ final news = <Map<String, Object>>[];
+ for (final m in roster.members) {
+ if (m.pub == selfPub || (m.isMachine && m.machineId == null)) continue;
+ if (m.at <= 0 || m.at > latest) continue;
+ if ((removedAt[m.pub] ?? -1) >= m.at) continue;
+ if ((stated[m.pub] ?? -1) >= m.at) continue;
+ news.add(
+ memberSubject(
+ GroupMember(
+ pub: m.pub,
+ kind: m.kind,
+ label: m.label.length > 60 ? m.label.substring(0, 60) : m.label,
+ at: m.at,
+ machineId: m.machineId,
+ ),
+ ),
+ );
+ }
+ for (final t in roster.removed) {
+ if (t.pub == selfPub || t.at <= 0 || t.at > latest) continue;
+ // Only a key someone still vouches for needs taking back.
+ final vouched = mentioned[t.pub];
+ if (vouched == null || vouched > t.at) continue;
+ if ((stated[t.pub] ?? -1) >= t.at) continue;
+ news.add({'pub': t.pub, 'at': t.at, 'removed': true});
+ }
+ return news;
+}
diff --git a/desktop/test/viewer/group_sync_test.dart b/desktop/test/viewer/group_sync_test.dart
index f8d149b23..75abbbaed 100644
--- a/desktop/test/viewer/group_sync_test.dart
+++ b/desktop/test/viewer/group_sync_test.dart
@@ -355,6 +355,32 @@ void main() {
);
});
+ test(
+ 'a group from before the board reaches it: the phone states what it already knew',
+ () async {
+ // The phone approved this browser with no machine, then the Mac — all before the board.
+ final phoneKeys = ViewerKeyStore(storage: _Memory());
+ final web = ViewerKeyStore(storage: _Memory());
+ final phone = await phoneKeys.identity();
+ final mac = await E2eeIdentity.generate();
+ await phoneKeys.pin('d' * 32, mac.pub, label: 'MacbookPro.local');
+ await admitGroupMember(
+ web,
+ GroupMember(pub: b64e(phone.pub), kind: 'viewer', label: 'phone', at: 5),
+ );
+ expect(await web.peer('d' * 32), isNull);
+
+ final news = await boardNews(phoneKeys, const []);
+ expect(news.map((s) => s['machineId']), ['d' * 32]);
+ final board = [for (final s in news) await signVouch(phone, s)];
+ final outcome = await adoptBoard(web, board);
+ expect(outcome.pinned, ['d' * 32]);
+ expect((await web.peer('d' * 32))!.pub, mac.pub);
+ // Stated once: the next read of the same board has nothing new to say.
+ expect(await boardNews(phoneKeys, board), isEmpty);
+ },
+ );
+
test(
'a browser the phone approved learns a machine the phone approved later',
() async {