@@ -289,6 +289,208 @@ void main() {
289289 expect (otherChannelState.transcript, isEmpty);
290290 },
291291 );
292+
293+ test ('expands batch envelopes through ordering and dedupe' , () async {
294+ final ownerKeychain = nostr.Keys .generate ();
295+ final agentKeychain = nostr.Keys .generate ();
296+ final relaySession = _RecordingRelaySession ();
297+ final container = ProviderContainer (
298+ overrides: [
299+ relaySessionProvider.overrideWith (() => relaySession),
300+ relayConfigProvider.overrideWith (
301+ () => _FakeRelayConfigNotifier (nsec: ownerKeychain.nsec),
302+ ),
303+ ],
304+ );
305+ addTearDown (container.dispose);
306+
307+ const channelId = 'test-channel' ;
308+ final key = (channelId: channelId, agentPubkey: agentKeychain.public);
309+ container.read (observerSubscriptionProvider (key));
310+ await Future <void >.delayed (Duration .zero);
311+
312+ final laterFrame = _observerFrameJson (
313+ seq: 2 ,
314+ channelId: channelId,
315+ turnId: 'turn-2' ,
316+ );
317+ final earlierFrame = _observerFrameJson (
318+ seq: 1 ,
319+ channelId: channelId,
320+ turnId: 'turn-1' ,
321+ );
322+ relaySession.emit (
323+ _observerEvent (
324+ ownerKeychain: ownerKeychain,
325+ agentKeychain: agentKeychain,
326+ payload: {
327+ 'seq' : 2 ,
328+ 'timestamp' : '2026-04-30T12:00:02.000Z' ,
329+ 'kind' : 'batch' ,
330+ 'channelId' : channelId,
331+ 'turnId' : 'turn-2' ,
332+ 'payload' : {
333+ 'events' : [laterFrame, earlierFrame, earlierFrame],
334+ },
335+ },
336+ ),
337+ );
338+
339+ final relayState = container.read (observerRelayProvider);
340+ final frames = relayState.framesByAgent[agentKeychain.public];
341+ expect (frames? .map ((frame) => frame.seq), [1 , 2 ]);
342+
343+ final state = container.read (observerSubscriptionProvider (key));
344+ expect (state.connection, ObserverConnectionState .open);
345+ expect (state.transcript, hasLength (2 ));
346+ expect (state.transcript.map ((item) => item.id), [
347+ 'turn:turn-1' ,
348+ 'turn:turn-2' ,
349+ ]);
350+
351+ final otherChannelState = container.read (
352+ observerSubscriptionProvider ((
353+ channelId: 'other-channel' ,
354+ agentPubkey: agentKeychain.public,
355+ )),
356+ );
357+ expect (otherChannelState.transcript, isEmpty);
358+ });
359+
360+ test ('keeps malformed batch envelopes as singleton frames' , () async {
361+ final ownerKeychain = nostr.Keys .generate ();
362+ final agentKeychain = nostr.Keys .generate ();
363+ final relaySession = _RecordingRelaySession ();
364+ final container = ProviderContainer (
365+ overrides: [
366+ relaySessionProvider.overrideWith (() => relaySession),
367+ relayConfigProvider.overrideWith (
368+ () => _FakeRelayConfigNotifier (nsec: ownerKeychain.nsec),
369+ ),
370+ ],
371+ );
372+ addTearDown (container.dispose);
373+
374+ container.read (
375+ observerSubscriptionProvider ((
376+ channelId: 'test-channel' ,
377+ agentPubkey: agentKeychain.public,
378+ )),
379+ );
380+ await Future <void >.delayed (Duration .zero);
381+
382+ relaySession.emit (
383+ _observerEvent (
384+ ownerKeychain: ownerKeychain,
385+ agentKeychain: agentKeychain,
386+ payload: {
387+ 'seq' : 3 ,
388+ 'timestamp' : '2026-04-30T12:00:03.000Z' ,
389+ 'kind' : 'batch' ,
390+ 'channelId' : 'test-channel' ,
391+ 'payload' : < String , dynamic > {},
392+ },
393+ ),
394+ );
395+
396+ final state = container.read (observerRelayProvider);
397+ expect (state.connection, ObserverConnectionState .open);
398+ expect (state.errorMessage, isNull);
399+ expect (state.framesByAgent[agentKeychain.public], hasLength (1 ));
400+ expect (state.framesByAgent[agentKeychain.public]! .single.kind, 'batch' );
401+ });
402+
403+ test (
404+ 'rejects invalid inner batch frames without partial ingestion' ,
405+ () async {
406+ final ownerKeychain = nostr.Keys .generate ();
407+ final agentKeychain = nostr.Keys .generate ();
408+ final relaySession = _RecordingRelaySession ();
409+ final container = ProviderContainer (
410+ overrides: [
411+ relaySessionProvider.overrideWith (() => relaySession),
412+ relayConfigProvider.overrideWith (
413+ () => _FakeRelayConfigNotifier (nsec: ownerKeychain.nsec),
414+ ),
415+ ],
416+ );
417+ addTearDown (container.dispose);
418+
419+ container.read (
420+ observerSubscriptionProvider ((
421+ channelId: 'test-channel' ,
422+ agentPubkey: agentKeychain.public,
423+ )),
424+ );
425+ await Future <void >.delayed (Duration .zero);
426+
427+ relaySession.emit (
428+ _observerEvent (
429+ ownerKeychain: ownerKeychain,
430+ agentKeychain: agentKeychain,
431+ payload: {
432+ 'seq' : 2 ,
433+ 'timestamp' : '2026-04-30T12:00:02.000Z' ,
434+ 'kind' : 'batch' ,
435+ 'channelId' : 'test-channel' ,
436+ 'payload' : {
437+ 'events' : [
438+ _observerFrameJson (
439+ seq: 1 ,
440+ channelId: 'test-channel' ,
441+ turnId: 'turn-1' ,
442+ ),
443+ {'seq' : 'invalid' },
444+ ],
445+ },
446+ },
447+ ),
448+ );
449+
450+ final state = container.read (observerRelayProvider);
451+ expect (state.connection, ObserverConnectionState .error);
452+ expect (state.errorMessage, contains ('Observer event decrypt failed' ));
453+ expect (state.framesByAgent[agentKeychain.public], isNull);
454+ },
455+ );
456+ }
457+
458+ Map <String , dynamic > _observerFrameJson ({
459+ required int seq,
460+ required String channelId,
461+ required String turnId,
462+ }) => {
463+ 'seq' : seq,
464+ 'timestamp' : '2026-04-30T12:00:0$seq .000Z' ,
465+ 'kind' : 'turn_started' ,
466+ 'channelId' : channelId,
467+ 'turnId' : turnId,
468+ 'payload' : {
469+ 'triggeringEventIds' : ['$seq ' ],
470+ },
471+ };
472+
473+ NostrEvent _observerEvent ({
474+ required nostr.Keys ownerKeychain,
475+ required nostr.Keys agentKeychain,
476+ required Map <String , dynamic > payload,
477+ }) {
478+ final conversationKey = getConversationKey (
479+ agentKeychain.secret,
480+ ownerKeychain.public,
481+ );
482+ final event = nostr.Event .from (
483+ kind: EventKind .agentObserverFrame,
484+ content: nip44Encrypt (conversationKey, jsonEncode (payload)),
485+ tags: [
486+ ['p' , ownerKeychain.public],
487+ ['agent' , agentKeychain.public],
488+ ['frame' , 'telemetry' ],
489+ ],
490+ secretKey: agentKeychain.secret,
491+ verify: false ,
492+ );
493+ return NostrEvent .fromJson (event.toMap ());
292494}
293495
294496class _RecordingRelaySession extends RelaySessionNotifier {
0 commit comments