feat(diagnostics): persistent alert history with a status-bar bell (Foundation QoL)

Toasts fade in 1-4s, so anything that scrolled past was gone. Notifications now retains
every pushed alert in a capped (100) ring buffer with a wall-clock epoch (AlertRecord) —
separate from the 5-item live-toast deque — plus a monotonic total_pushed_ counter.

A bell in the status-bar right cluster opens an upward popup listing recent alerts
newest-first: severity icon + colour (reusing the toast palette), the message, and a
relative age (formatTimeAgoShort), with a Clear-all action. An unread dot on the bell,
coloured by the most-severe unseen alert, marks alerts that arrived since the panel was
last opened — driven by totalPushed() deltas so it survives capping/clearing.

Thread note: every push is on the UI thread (RPC results run as main-thread MainCb
callbacks), matching this class's existing lock-free model; documented as a
no-raw-worker-thread invariant.

New i18n keys (alerts_*). Build-clean; ctest 1/1 (adds testNotificationHistory: retention,
order, cap, monotonic counter, clear). Closes the QoL bundle and the Foundation tier.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 20:57:02 -05:00
co-authored by Claude Opus 4.8
parent c7c3440a7b
commit 4b3f0fa92b
6 changed files with 239 additions and 7 deletions
+40 -6
View File
@@ -9,6 +9,8 @@
#include <chrono>
#include <functional>
#include <cstdio>
#include <cstdint>
#include <ctime>
#include "../util/logger.h"
#include "schema/ui_schema.h"
@@ -22,6 +24,15 @@ enum class NotificationType {
Error
};
// A retained alert for the persistent history panel. Unlike a live Notification (which fades and is
// erased within seconds), this keeps a wall-clock epoch so its age can be shown as "3m ago" long
// after the toast is gone. See App::renderAlertHistoryPanel.
struct AlertRecord {
std::string message;
NotificationType type;
std::int64_t epoch; // std::time(nullptr) at push — wall-clock, for relative-age display
};
struct Notification {
std::string message;
NotificationType type;
@@ -84,20 +95,30 @@ public:
void push(const std::string& message, NotificationType type, float duration = 5.0f) {
notifications_.emplace_back(message, type, duration);
// Retain a copy in the persistent history (the toast above will fade in seconds; this
// survives so the user can review what happened). Thread note: every push is on the UI
// thread (RPC results run as main-thread MainCb callbacks), so this container needs no lock,
// consistent with the rest of this class. Do NOT push from a raw worker thread.
history_.push_back(AlertRecord{message, type, static_cast<std::int64_t>(std::time(nullptr))});
++total_pushed_;
while (history_.size() > kMaxHistory) {
history_.pop_front();
}
// Log errors and warnings (debug-only output)
if (type == NotificationType::Error) {
DEBUG_LOGF("[ERROR] Notification: %s\n", message.c_str());
} else if (type == NotificationType::Warning) {
DEBUG_LOGF("[WARN] Notification: %s\n", message.c_str());
}
// Forward errors and warnings to console callback
if (console_callback_ && (type == NotificationType::Error || type == NotificationType::Warning)) {
const char* prefix = (type == NotificationType::Error) ? "[ERROR] " : "[WARN] ";
console_callback_(prefix + message, type == NotificationType::Error);
}
// Limit max notifications
while (notifications_.size() > max_notifications_) {
notifications_.pop_front();
@@ -122,21 +143,34 @@ public:
void clear() {
notifications_.clear();
}
void setMaxNotifications(size_t max) {
max_notifications_ = max;
}
// ── Persistent alert history (for the status-bar bell panel) ──
/// Retained alerts, oldest first (capped at kMaxHistory; the toast deque is separate).
const std::deque<AlertRecord>& history() const { return history_; }
bool hasHistory() const { return !history_.empty(); }
void clearHistory() { history_.clear(); }
/// Monotonic count of every alert ever pushed this session — survives capping/clearing, so it is
/// the correct basis for an "unseen since last opened" count (deque size is not).
std::uint64_t totalPushed() const { return total_pushed_; }
private:
Notifications() = default;
~Notifications() = default;
Notifications(const Notifications&) = delete;
Notifications& operator=(const Notifications&) = delete;
std::deque<Notification> notifications_;
size_t max_notifications_ = 5;
std::function<void(const std::string&, bool)> console_callback_;
std::deque<AlertRecord> history_;
std::uint64_t total_pushed_ = 0;
static constexpr size_t kMaxHistory = 100;
static float schemaDuration(const char* key, float fallback) {
float v = schema::UI().drawElement("components.notifications", key).size;
return v > 0.0f ? v : fallback;