Agent notifications are system notifications, never in-app toasts

An agent's notify call now always posts an OS notification when permitted,
whatever app is in front (only skipped while that chat is open in the
focused page); no toast or chime. Without permission the tab title counts
it. The browser's permission prompt is shown on the operator's first click.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 16:32:29 +00:00
parent e05b2f07dc
commit 355a5767fb
4 changed files with 71 additions and 73 deletions
+16
View File
@@ -6,6 +6,7 @@ import 'dart:async';
import '../features/runs/domain/run_tab_counts.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/material.dart';
@@ -15,6 +16,7 @@ import '../core/transport/pipelines_client.dart';
import '../shell/adaptive_shell.dart';
import '../shell/destination.dart';
import '../features/attention/notify/notification_controller.dart';
import '../features/attention/notify/notification_models.dart';
import '../features/attention/notify/chat_question_notices.dart';
import '../features/attention/notify/agent_notices.dart';
import '../features/attention/notify/page_signals.dart';
@@ -456,6 +458,11 @@ class _PipelinesAppState extends State<PipelinesApp>
);
};
unawaited(_notifications.load());
// Browsers only show the permission prompt from a user gesture: ask on
// the operator's first click anywhere, once, while undecided, so agent
// notifications can be real system notifications without a trip to
// Settings.
GestureBinding.instance.pointerRouter.addGlobalRoute(_askOnFirstPress);
bindAttentionAckSync(_session.gateway, _ackSync);
unawaited(_ackSync.load());
unawaited(_attention.load());
@@ -592,8 +599,17 @@ class _PipelinesAppState extends State<PipelinesApp>
}
}
void _askOnFirstPress(PointerEvent event) {
if (event is! PointerDownEvent || !_notifications.loaded) return;
GestureBinding.instance.pointerRouter.removeGlobalRoute(_askOnFirstPress);
if (_notifications.permission == NotificationPermissionState.notRequested) {
unawaited(_notifications.requestPermission());
}
}
@override
void dispose() {
GestureBinding.instance.pointerRouter.removeGlobalRoute(_askOnFirstPress);
_tabLocations.dispose();
WidgetsBinding.instance.removeObserver(this);
_deliveryHandoff?.dispose();
@@ -115,14 +115,11 @@ class AgentNoticeDelivery {
/// Decides how [notice] is delivered.
///
/// A focused page shows a toast, or nothing when the notice's chat is already
/// in view. A page the operator is not looking at gets a system notification
/// when [canNotify] (permission granted) and a count in the tab title; a low
/// priority notice only raises a system notification when the page is hidden,
/// and otherwise waits as a toast. Without system notifications (denied, not
/// asked, or an insecure http page) the toast, the title count and, for
/// default and high priority, a short chime carry it. A [replay] (missed while
/// disconnected) never chimes.
/// An agent's notification is a system (OS) notification whenever the
/// browser or OS allows one, whatever app is in front; the only exception is
/// the operator already looking at that chat in a focused page. There is no
/// in-app toast or sound. Without permission (not asked yet, denied, or an
/// insecure http page) only the tab title counts it until the page is focused.
AgentNoticeDelivery agentNoticeDelivery({
required AgentNotice notice,
required PagePresence presence,
@@ -130,14 +127,8 @@ AgentNoticeDelivery agentNoticeDelivery({
required bool canNotify,
bool replay = false,
}) {
if (presence == PagePresence.focused) {
return AgentNoticeDelivery(toast: !chatInView);
}
final system = canNotify && (!notice.low || presence == PagePresence.hidden);
return AgentNoticeDelivery(
system: system,
toast: !system,
badge: true,
chime: !system && !notice.low && !replay,
);
final looking = presence == PagePresence.focused && chatInView;
if (looking) return const AgentNoticeDelivery();
if (canNotify) return const AgentNoticeDelivery(system: true);
return AgentNoticeDelivery(badge: presence != PagePresence.focused);
}
@@ -162,14 +162,15 @@ class AgentNotices {
);
final headline = notice.headline(chat);
void open() => openChat(notice.sessionId);
var toast = delivery.toast;
if (delivery.system && !_showSystem(notice, headline, open)) toast = true;
if (toast) {
var badge = delivery.badge;
// A system notification the OS refused is counted in the tab title.
if (delivery.system && !_showSystem(notice, headline, open)) badge = true;
if (delivery.toast) {
showToast(
AgentToast(headline: headline, message: notice.message, open: open),
);
}
if (delivery.badge && page.presence != PagePresence.focused) {
if (badge && page.presence != PagePresence.focused) {
_badge++;
page.setBadge(_badge);
}
@@ -126,16 +126,17 @@ void main() {
await gateway.controller.close();
});
test('a focused page shows a toast that opens the chat', () async {
test('a focused page on another chat still posts a system notification '
'that opens the chat; nothing shows in the app', () async {
await setUpWith();
gateway.controller.add(_push(_notice('a')));
await pump();
expect(bridge.shown, isEmpty);
expect(toasts.single.headline, 'Release chat: Build done');
expect(toasts.single.message, 'All tests pass');
expect(bridge.shown.single.title, 'Release chat: Build done');
expect(bridge.shown.single.body, 'All tests pass');
expect(toasts, isEmpty);
expect(page.badges, isEmpty);
toasts.single.open();
bridge.taps.first!();
expect(opened, ['ses_1']);
});
@@ -152,8 +153,8 @@ void main() {
);
});
test('an unfocused page posts a system notification and badges the title; '
'focus clears the badge', () async {
test('an unfocused page posts a system notification, even for the chat '
'in view', () async {
await setUpWith();
page.current = PagePresence.hidden;
active = 'ses_1'; // In view but not looked at: still notified.
@@ -168,60 +169,49 @@ void main() {
expect(bridge.shown.first.tag, 'chat:ses_1:notify:a');
expect(bridge.shown.first.body, 'All tests pass');
expect(toasts, isEmpty);
expect(page.badges, [1, 2]);
expect(notices.badge, 2);
expect(page.badges, isEmpty);
expect(page.chimes, 0);
bridge.taps.first!();
expect(opened, ['ses_1']);
});
test('a low priority notice is a system notification too', () async {
await setUpWith();
page.current = PagePresence.visible;
gateway.controller.add(_push(_notice('low', priority: 2)));
await pump();
expect(bridge.shown, hasLength(1));
expect(toasts, isEmpty);
});
test('without notification permission only the tab title counts it, and '
'focus clears the count', () async {
await setUpWith(permission: 'default', secure: false);
page.current = PagePresence.hidden;
gateway.controller.add(_push(_notice('a', priority: 4)));
await pump();
expect(notifications.canNotify, isFalse);
expect(bridge.shown, isEmpty);
expect(toasts, isEmpty);
expect(page.badges, [1]);
expect(page.chimes, 0);
page.current = PagePresence.focused;
page.focus.add(null);
await pump();
expect(page.badges.last, 0);
expect(notices.badge, 0);
});
test('a low priority notice waits as a toast on a visible page and posts '
'only when the page is hidden', () async {
await setUpWith();
page.current = PagePresence.visible;
gateway.controller.add(_push(_notice('low', priority: 2)));
await pump();
expect(bridge.shown, isEmpty);
expect(toasts, hasLength(1));
expect(page.badges, [1]);
expect(page.chimes, 0);
page.current = PagePresence.hidden;
gateway.controller.add(_push(_notice('low2', priority: 2)));
await pump();
expect(bridge.shown, hasLength(1));
});
test(
'an insecure page falls back to a toast, a title count and a chime',
() async {
await setUpWith(permission: 'default', secure: false);
page.current = PagePresence.hidden;
gateway.controller.add(_push(_notice('a', priority: 4)));
await pump();
expect(notifications.canNotify, isFalse);
expect(bridge.shown, isEmpty);
expect(toasts.single.headline, 'Release chat: Build done');
expect(page.badges, [1]);
expect(page.chimes, 1);
},
);
test('a refused system notification falls back to the toast', () async {
test('a refused system notification is counted in the tab title', () async {
await setUpWith();
bridge.accepts = false;
page.current = PagePresence.hidden;
gateway.controller.add(_push(_notice('a')));
await pump();
expect(bridge.shown, hasLength(1));
expect(toasts, hasLength(1));
expect(toasts, isEmpty);
expect(page.badges, [1]);
});
test('other notification kinds are left to their own surfaces', () async {
@@ -243,7 +233,7 @@ void main() {
gateway.backlog = [_notice('a')];
await notices.connected();
await pump();
expect(toasts, hasLength(1));
expect(bridge.shown, hasLength(1));
},
);
@@ -259,7 +249,7 @@ void main() {
await notices.connected();
await pump();
expect(gateway.calls, ['subscribe_notifications']);
expect(toasts.map((t) => t.headline), ['Release chat: Build done']);
expect(bridge.shown.map((m) => m.title), ['Release chat: Build done']);
// The ledger persisted both chat_notify ids, so a reload shows neither.
final stored = decodeNotificationLedger(store.stored);
@@ -274,11 +264,11 @@ void main() {
bridge: bridge,
);
await notifications.load();
toasts.clear();
bridge.shown.clear();
notices = build();
await notices.connected();
await pump();
expect(toasts, isEmpty);
expect(bridge.shown, isEmpty);
},
);
@@ -310,7 +300,7 @@ void main() {
'notifications': [_notice('missed', age: const Duration(minutes: 5))],
});
await pump();
expect(toasts.single.headline, 'Release chat: Build done');
expect(bridge.shown.single.title, 'Release chat: Build done');
});
test('a replay shows at most the newest few and never chimes', () async {
@@ -322,7 +312,7 @@ void main() {
];
await notices.connected();
await pump();
expect(toasts, hasLength(agentNoticeReplayLimit));
expect(toasts, isEmpty);
expect(page.chimes, 0);
expect(page.badges.last, agentNoticeReplayLimit);
});