fix: persist and retry run alerts and honor notification modes

This commit is contained in:
2026-10-20 15:42:21 +01:00
parent e4f48ecbb1
commit 016fcb552a
20 changed files with 697 additions and 41 deletions
+20 -6
View File
@@ -1,6 +1,5 @@
// Pure, DOM-free notification model for failed/waiting runs (L5). It mirrors
// android/.../notify/RunAlerts.kt: the same two qualifying kinds, the same
// headlines, the same run line and the same one-notification-per-run+kind rule.
// Pure, DOM-free notification model. Auto retains failed/waiting alerts;
// per-run on also alerts terminal success and off suppresses every tray alert.
//
// Everything here is pure: no DOM, no clock, no storage access. The page owns
// the Notification constructor, the permission request gesture and localStorage;
@@ -13,14 +12,16 @@ import { AttentionKind, attentionKey, attentionKindOf } from "./attention.js";
// Kinds, headlines and the run line
// ---------------------------------------------------------------------------
/** The two run events that raise a notification (RunAlerts.kt RunAlertKind). */
/** Notification kinds; success does not create a Needs you entry. */
export const RunAlertKind = Object.freeze({
Decision: AttentionKind.Decision,
Failed: AttentionKind.Failed,
Succeeded: Object.freeze({ name: "Succeeded" }),
});
/** `alertKindOf`: an attention kind is already exactly the notification kind. */
export function alertKindOf(kind) {
if (kind?.name === "Succeeded") return RunAlertKind.Succeeded;
if (kind === AttentionKind.Decision || kind?.name === AttentionKind.Decision.name) {
return AttentionKind.Decision;
}
@@ -37,6 +38,8 @@ export function alertTitle(kind) {
return "A run needs your decision";
case "Failed":
return "A run failed";
case "Succeeded":
return "A run succeeded";
default:
return "";
}
@@ -90,15 +93,18 @@ export function alertBody(run) {
*/
export function createAlertLedger() {
const lastStatus = new Map();
const modes = new Map();
const notified = new Set();
let seeding = false;
function seed(runs) {
const next = new Map();
modes.clear();
for (const run of runs ?? []) {
const runId = runIdOf(run?.id ?? run?.run_id);
if (runId == null) continue;
next.set(runId, normalizeStatus(run?.status));
modes.set(runId, normalizeStatus(run?.notify_mode) || "auto");
}
lastStatus.clear();
for (const [runId, status] of next) lastStatus.set(runId, status);
@@ -113,16 +119,23 @@ export function createAlertLedger() {
const previous = lastStatus.get(runId);
lastStatus.set(runId, status);
const kind = attentionKindOf(status);
if (frame?.notify_mode != null) modes.set(runId, normalizeStatus(frame.notify_mode));
const mode = modes.get(runId) || "auto";
const kind = status === "succeeded" && mode === "on" ? RunAlertKind.Succeeded : attentionKindOf(status);
if (!kind) {
forget(runId, AttentionKind.Decision);
forget(runId, AttentionKind.Failed);
forget(runId, RunAlertKind.Succeeded);
return null;
}
// A run can only be in one qualifying state; forget the other kind's key so
// a later transition back into it is a new event.
forget(runId, kind === AttentionKind.Decision ? AttentionKind.Failed : AttentionKind.Decision);
for (const other of Object.values(RunAlertKind)) {
if (other !== kind) forget(runId, other);
}
if (seeding) return null;
if (mode === "off") return null;
if (previous === undefined || previous === status) return null;
const key = alertKey(runId, kind);
@@ -143,6 +156,7 @@ export function createAlertLedger() {
function reset() {
lastStatus.clear();
modes.clear();
seeding = false;
}
+4
View File
@@ -172,6 +172,7 @@ function sameResourceWait(a, b) {
/** The run-state fields a frame advances, shared by the summary row and the detail. */
function runStateFields(current) {
return {
notify_mode: current.notify_mode ?? "auto",
resource_wait: current.resource_wait ?? null,
status: current.status ?? "",
outcome: current.outcome ?? "",
@@ -196,6 +197,7 @@ function applyRunStateFields(current, frame) {
? "queued"
: advanceRunStatus(before.status, frame.status);
const merged = {
notify_mode: blank(frame.notify_mode) ? before.notify_mode : frame.notify_mode,
resource_wait: isTerminalRunStatus(status)
? null
: frame.resource_wait_set === true
@@ -210,6 +212,7 @@ function applyRunStateFields(current, frame) {
executing: executingFor(status, before.executing),
};
const changed =
merged.notify_mode !== before.notify_mode ||
!sameResourceWait(merged.resource_wait, before.resource_wait) ||
merged.status !== before.status ||
merged.outcome !== before.outcome ||
@@ -236,6 +239,7 @@ export function newRunSummaryFromFrame(frame) {
id: Number(frame.run_id),
pipeline_id: pipelineId,
name,
notify_mode: frame.notify_mode ?? "auto",
title: blank(frame.title) ? null : frame.title,
version: Number(frame.version) || 0,
status: frame.status ?? "",