Notifications: agent notify reaches every device and clears everywhere

- Server: chat ntfy messages carry a sequence id derived from their app id;
  clearing a chat app notification also clears it in ntfy (PUT
  /<topic>/<seq>/clear, verified on ntfy v2.38.1).
- Server: a turn that sent an agent notify does not also send chat_reply.
- notify tool and MCP instructions: Pipelines already notifies at reply end
  and for questions/approvals; notify is for mid-turn or background news.
- Shown notify items carry channels (apps/phone); the transcript row says
  where it went.
- App: NotificationBridge.close(tag); web closes tracked Notifications and
  tells other tabs over a BroadcastChannel; native bridge cancels by tag and
  now supports macOS. AgentNotices closes on notifications_cleared and on
  resubscribe cleared ids, and a tap sends clear_notifications.
- Docs: skill, chat reference, ntfy/notifications docs, app note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 19:43:55 +00:00
parent c441400f36
commit b91ef04ab0
32 changed files with 743 additions and 73 deletions
+33
View File
@@ -23,6 +23,10 @@ const chatReplyFallbackBody = "The agent finished its reply."
// (chat_reply_finished). Callers must hold h.mu.
func (h *chatHub) noteReplyAttentionLocked(prior *protocol.ChatStep, frame protocol.ChatEventFrame) {
step := frame.Step
if frame.Kind == protocol.ChatEventStep && step != nil && step.Status == protocol.ChatStepStarted && !step.Intermediate {
// A new turn has not notified yet.
delete(h.notifiedTurns, frame.SessionID)
}
if frame.Kind != protocol.ChatEventStep || step == nil || step.Status != protocol.ChatStepEnded || step.Intermediate || frame.SessionID == "" {
return
}
@@ -35,6 +39,9 @@ func (h *chatHub) noteReplyAttentionLocked(prior *protocol.ChatStep, frame proto
if step.TurnID != "" && !h.claimChatAttentionLocked("reply\x00"+frame.SessionID+"\x00"+step.TurnID) {
return
}
if h.takeTurnNotifiedLocked(frame.SessionID) {
return
}
body := h.finalReplyTextLocked(frame.SessionID)
if body == "" {
body = chatReplyFallbackBody
@@ -46,6 +53,32 @@ func (h *chatHub) noteReplyAttentionLocked(prior *protocol.ChatStep, frame proto
h.startChatAttention(chatAttention{sessionID: frame.SessionID, reply: true, body: body, id: chatNotificationID(frame.SessionID, "reply", turnID)})
}
// markTurnNotified records that sessionID's turn in progress sent an agent
// notify, so its end is not announced as a reply too. A notify outside a turn
// the hub is following (background work after the turn) marks nothing.
func (h *chatHub) markTurnNotified(sessionID string) {
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil || turn.step == nil || (turn.step.Status == protocol.ChatStepEnded && !turn.step.Intermediate) {
return
}
if h.notifiedTurns == nil {
h.notifiedTurns = map[string]struct{}{}
}
h.notifiedTurns[sessionID] = struct{}{}
}
// takeTurnNotifiedLocked reports and forgets whether sessionID's ending turn
// already sent an agent notify. Callers must hold h.mu.
func (h *chatHub) takeTurnNotifiedLocked(sessionID string) bool {
if _, ok := h.notifiedTurns[sessionID]; !ok {
return false
}
delete(h.notifiedTurns, sessionID)
return true
}
// finalReplyTextLocked returns a bounded excerpt, paragraph breaks kept, of the latest
// assistant message's text in the current turn: messages after the last user
// message, scanned newest first. Callers must hold h.mu.