package pipeline import ( "context" "database/sql" "encoding/json" "errors" "strings" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // AppNotificationRetention bounds how long an app notification is kept and // replayed to a reconnecting client. const AppNotificationRetention = 7 * 24 * time.Hour // maxReplayedClearedIDs bounds the cleared ids a subscribe reply carries. const maxReplayedClearedIDs = 500 // AppNotificationRecord is one app notification plus the run or chat it // belongs to, which the clearing paths match on. type AppNotificationRecord struct { protocol.AppNotification RunID int64 SessionID string } // AppNotificationFilter selects active notifications to clear: a run or a // chat session, optionally narrowed to kinds or one exact id. An empty filter // matches nothing. type AppNotificationFilter struct { RunID int64 SessionID string Kinds []string ID string } // initAppNotifications creates the app notification outbox. seq is the // replay cursor order; id is the stable, client-visible identity. func (s *Store) initAppNotifications() error { _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS app_notifications ( seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL UNIQUE, kind TEXT NOT NULL, title TEXT NOT NULL, message TEXT NOT NULL, click TEXT NOT NULL, priority INTEGER NOT NULL, tags TEXT NOT NULL, grp TEXT NOT NULL, run_id INTEGER NOT NULL DEFAULT 0, session_id TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, cleared_at INTEGER NOT NULL DEFAULT 0); CREATE INDEX IF NOT EXISTS app_notifications_run ON app_notifications(run_id, cleared_at); CREATE INDEX IF NOT EXISTS app_notifications_session ON app_notifications(session_id, cleared_at); CREATE INDEX IF NOT EXISTS app_notifications_created ON app_notifications(created_at);`) return err } // RecordAppNotification stores n once (by id) and reports whether it is new // and active, returning it with its created_at. A repeated id (a retried // publication) is ignored. A run_waiting notification for a run that is no // longer waiting is stored already cleared, so a delayed publication cannot // resurrect a stale decision. Rows older than AppNotificationRetention are // pruned. func (s *Store) RecordAppNotification(ctx context.Context, n AppNotificationRecord, now time.Time) (protocol.AppNotification, bool, error) { if n.Tags == nil { n.Tags = []string{} } if n.Priority == 0 { n.Priority = protocol.DefaultNotificationPriority } n.CreatedAt = now.UTC().Format(time.RFC3339) tags, err := json.Marshal(n.Tags) if err != nil { return n.AppNotification, false, err } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return n.AppNotification, false, err } defer tx.Rollback() nowMillis := now.UnixMilli() if _, err := tx.ExecContext(ctx, `DELETE FROM app_notifications WHERE created_at < ?`, now.Add(-AppNotificationRetention).UnixMilli()); err != nil { return n.AppNotification, false, err } var cleared int64 if n.Kind == protocol.NotificationRunWaiting && n.RunID > 0 { var status string switch err := tx.QueryRowContext(ctx, `SELECT status FROM pipeline_runs WHERE id=?`, n.RunID).Scan(&status); { case errors.Is(err, sql.ErrNoRows): case err != nil: return n.AppNotification, false, err case status != protocol.RunStatusWaiting: cleared = nowMillis } } result, err := tx.ExecContext(ctx, `INSERT INTO app_notifications(id,kind,title,message,click,priority,tags,grp,run_id,session_id,created_at,cleared_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO NOTHING`, n.ID, n.Kind, n.Title, n.Message, n.Click, n.Priority, string(tags), n.Group, n.RunID, n.SessionID, nowMillis, cleared) if err != nil { return n.AppNotification, false, err } inserted, _ := result.RowsAffected() if err := tx.Commit(); err != nil { return n.AppNotification, false, err } return n.AppNotification, inserted > 0 && cleared == 0, nil } // ActiveAppNotifications returns the active (not cleared) notifications newer // than the notification named after, oldest first, bounded to the newest // protocol.MaxReplayedNotifications within the retention window. An empty or // unknown cursor replays the whole bounded set. cleared lists notifications // cleared within the window (newest first, bounded). func (s *Store) ActiveAppNotifications(ctx context.Context, after string, now time.Time) (active []protocol.AppNotification, cleared []string, err error) { cutoff := now.Add(-AppNotificationRetention).UnixMilli() var afterSeq int64 if after != "" { if err := s.db.QueryRowContext(ctx, `SELECT seq FROM app_notifications WHERE id=?`, after).Scan(&afterSeq); err != nil && !errors.Is(err, sql.ErrNoRows) { return nil, nil, err } } rows, err := s.db.QueryContext(ctx, `SELECT id,kind,title,message,click,priority,tags,grp,created_at FROM app_notifications WHERE cleared_at=0 AND seq>? AND created_at>=? ORDER BY seq DESC LIMIT ?`, afterSeq, cutoff, protocol.MaxReplayedNotifications) if err != nil { return nil, nil, err } active = []protocol.AppNotification{} for rows.Next() { var n protocol.AppNotification var tags string var created int64 if err := rows.Scan(&n.ID, &n.Kind, &n.Title, &n.Message, &n.Click, &n.Priority, &tags, &n.Group, &created); err != nil { rows.Close() return nil, nil, err } if json.Unmarshal([]byte(tags), &n.Tags) != nil || n.Tags == nil { n.Tags = []string{} } n.CreatedAt = time.UnixMilli(created).UTC().Format(time.RFC3339) active = append(active, n) } rows.Close() if err := rows.Err(); err != nil { return nil, nil, err } for i, j := 0, len(active)-1; i < j; i, j = i+1, j-1 { active[i], active[j] = active[j], active[i] } rows, err = s.db.QueryContext(ctx, `SELECT id FROM app_notifications WHERE cleared_at>0 AND created_at>=? ORDER BY cleared_at DESC, seq DESC LIMIT ?`, cutoff, maxReplayedClearedIDs) if err != nil { return nil, nil, err } defer rows.Close() cleared = []string{} for rows.Next() { var id string if err := rows.Scan(&id); err != nil { return nil, nil, err } cleared = append(cleared, id) } return active, cleared, rows.Err() } // ClearAppNotifications clears the named active notifications and returns the // ones it changed, in request order. Unknown or already cleared ids are // skipped. func (s *Store) ClearAppNotifications(ctx context.Context, ids []string, now time.Time) ([]AppNotificationRecord, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() changed := []AppNotificationRecord{} for _, id := range ids { var n AppNotificationRecord err := tx.QueryRowContext(ctx, `UPDATE app_notifications SET cleared_at=? WHERE id=? AND cleared_at=0 RETURNING id,kind,grp,run_id,session_id`, now.UnixMilli(), id).Scan(&n.ID, &n.Kind, &n.Group, &n.RunID, &n.SessionID) if errors.Is(err, sql.ErrNoRows) { continue } if err != nil { return nil, err } changed = append(changed, n) } return changed, tx.Commit() } // ClearAppNotificationsFor clears every active notification matching filter // and returns the ids it changed, oldest first. func (s *Store) ClearAppNotificationsFor(ctx context.Context, filter AppNotificationFilter, now time.Time) ([]string, error) { var where []string var args []any switch { case filter.RunID > 0: where, args = append(where, "run_id=?"), append(args, filter.RunID) case filter.SessionID != "": where, args = append(where, "session_id=?"), append(args, filter.SessionID) default: return []string{}, nil } if len(filter.Kinds) > 0 { where = append(where, "kind IN (?"+strings.Repeat(",?", len(filter.Kinds)-1)+")") for _, kind := range filter.Kinds { args = append(args, kind) } } if filter.ID != "" { where, args = append(where, "id=?"), append(args, filter.ID) } rows, err := s.db.QueryContext(ctx, `UPDATE app_notifications SET cleared_at=? WHERE cleared_at=0 AND `+strings.Join(where, " AND ")+` RETURNING seq,id`, append([]any{now.UnixMilli()}, args...)...) if err != nil { return nil, err } defer rows.Close() type cleared struct { seq int64 id string } var found []cleared for rows.Next() { var c cleared if err := rows.Scan(&c.seq, &c.id); err != nil { return nil, err } found = append(found, c) } if err := rows.Err(); err != nil { return nil, err } // RETURNING order is unspecified; report oldest first. for i := 1; i < len(found); i++ { for j := i; j > 0 && found[j].seq < found[j-1].seq; j-- { found[j], found[j-1] = found[j-1], found[j] } } ids := make([]string, 0, len(found)) for _, c := range found { ids = append(ids, c.id) } return ids, nil }