Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pm-stack/aggregator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ alerts-intake-token`, resolved by name at runtime
within 24 hours. A class under 10% acted-on over the rolling
four-week window renders as a default-off _proposal_ — display
only; the page never changes a preference or a default.
- **Preferences**: `/alerts/preferences` (#3318) is the
per-class subscription page: toggles for every class in
`NOTIFICATION_CLASS_DEFAULTS`, quiet-hours editing, this
browser's subscription state, and a send-test button per class
(`POST /api/push/test-class`). Saving writes through the same
subscription upsert; the engine reads the stored classes/quiet
on its next tick, and a per-class test honours the toggle (a
class that is off receives no test). Test landings follow the
design §3 deep-link contract (`notificationTestLanding`): a real
candidate's own URL when one is supplied, otherwise the class's
own view — never the origin root; the digest test is the only
list-shaped landing (attention filtered to its refs). The JSON
twin is `/api/alerts/preferences.json`. `/notifications` stays
the read-only #3319 measurement page.
- **Ack surface**: `/alerts` lists open incidents with Ack/Close.
Ack is exactly-once: only the open→acked transition writes an
event (and stamps the sends' `acted_on_at`); a repeat ack returns
Expand Down
80 changes: 80 additions & 0 deletions pm-stack/aggregator/src/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import { sendPush, type PushOutcome, type PushTarget } from "./push.ts";
import { getSupabaseClient, sqlInt, sqlJson, sqlStr, type SupabaseClient } from "./supabase.ts";
import {
buildNotificationCandidates,
classTestObjectRef,
decideNotification,
nextNotificationDigestAt,
notificationTestLanding,
NOTIFICATION_CLASS_DEFAULTS,
type NotificationHistoryEntry,
type NotificationSnapshot,
Expand Down Expand Up @@ -598,6 +600,84 @@ export class AlertsStore {
}
}

/** Per-class test sends (#3318): the preferences page's send-test
* button per class. The payload carries the class under test and
* lands per the design §3 deep-link contract
* (notificationTestLanding): a real candidate's own triage-capable
* URL when one is supplied, otherwise the test item's class view —
* never the origin root, and the digest test is the only
* list-shaped landing (attention filtered to its refs). */
export function buildClassTestPayload(
cls: string,
origin: string,
candidate?: { objectRef: string; title: string; url: string } | null,
): PushPayload {
return {
class: cls,
objectRef: candidate?.objectRef ?? classTestObjectRef(cls),
url: notificationTestLanding(cls, origin, candidate),
title: `Test ${cls} — PM Stack`,
body: candidate
? `Test ${cls} notification for ${candidate.title}. If you can read this, ${cls} delivery and its deep link work on this device.`
: `Test ${cls} notification (test item ${classTestObjectRef(cls)}). If you can read this, ${cls} delivery and its deep link work on this device.`,
};
}

export interface ClassTestPushSummary extends TestPushSummary {
cls: string;
/** Subscriptions the class toggle let through (all of them for
* the digest test, which is a delivery shape, not a class). */
eligible: number;
/** The deep-link landing the test notification opens. */
url: string;
}

/**
* Send the per-class test push. Unlike the generic test, this one
* honours the stored preference: a subscription whose class toggle
* is off receives nothing — the same classEnabled check the engine
* applies, so the test proves the toggle's effect. One sends-ledger
* row per attempted subscription, under the tested class with the
* test object ref. The sender is injectable, like sendTestPush.
*/
export async function sendClassTestPush(
store: Pick<AlertsStore, "listSubscriptions" | "logSend" | "noteSubscriptionGone" | "noteSubscriptionAlive">,
origin: string,
cls: string,
send: typeof sendPush = sendPush,
candidate?: { objectRef: string; title: string; url: string } | null,
): Promise<ClassTestPushSummary> {
const payload = buildClassTestPayload(cls, origin, candidate);
const subs = await store.listSubscriptions();
const targets = cls === "digest" ? subs : subs.filter((sub) => classEnabled(sub, cls));
const summary: ClassTestPushSummary = {
subscriptions: subs.length,
eligible: targets.length,
sent: 0,
gone: 0,
errors: 0,
cls,
url: payload.url,
};
for (const sub of targets) {
const target: PushTarget = { endpoint: sub.endpoint, p256dh: sub.p256dh, auth: sub.auth };
const outcome = await send(target, payload);
if (outcome.ok) {
summary.sent += 1;
await store.logSend({ class: cls, objectRef: payload.objectRef, outcome: "sent" });
await store.noteSubscriptionAlive(sub.endpoint);
} else if (outcome.gone) {
summary.gone += 1;
await store.logSend({ class: cls, objectRef: payload.objectRef, outcome: "gone" });
await store.noteSubscriptionGone(sub.endpoint);
} else {
summary.errors += 1;
await store.logSend({ class: cls, objectRef: payload.objectRef, outcome: "error" });
}
}
return summary;
}

/** The test alert (#3676 follow-up, Drew 2026-11-01): the /alerts
* page's "Send a test alert" button. Clearly a test in title + body;
* objectRef is a stable non-incident ref so the sends ledger keeps
Expand Down
95 changes: 95 additions & 0 deletions pm-stack/aggregator/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,101 @@ export const NOTIFICATION_CLASS_DEFAULTS: Record<string, boolean> = {
"blocked-age": true,
};

/** Notification preference classes (#3318, slice (d) of the #1254
* design): the classes a subscription can toggle on the preferences
* page (/alerts/preferences). Ids are exactly the keys of
* NOTIFICATION_CLASS_DEFAULTS — the stored subscription `classes`
* map and the engine's classEnabled check read the same keys, so a
* toggle on the page is the next engine decision. */
export interface NotificationClassMeta {
id: string;
label: string;
blurb: string;
}

export const NOTIFICATION_CLASSES: NotificationClassMeta[] = [
{
id: "incident",
label: "Incident alerts",
blurb: "A watchdog reports something down. Always immediate — quiet hours never hold an incident.",
},
{
id: "needs-drew",
label: "Needs Drew",
blurb: "An item enters the needs-Drew tier on the attention page.",
},
{
id: "quota-wall",
label: "Quota wall",
blurb: "A pool hits its limit well before reset, or a sole-door pool walls at all.",
},
{
id: "quota-near-wall",
label: "Quota near wall",
blurb: "A sole-door pool crosses 85% of a window.",
},
{
id: "stack-health",
label: "Stack health",
blurb: "A monitored stack service starts failing its probe.",
},
{
id: "blocked-age",
label: "Blocked-age reminders",
blurb: "A needs-Drew item stays unacted for 24/48 hours, then weekly (digest-only except the 48-hour breakthrough).",
},
];

/** Classes a per-class test send accepts: every preference class,
* plus "digest" — the digest is not a stored preference (it is the
* delivery shape quiet/digest-only candidates take), but its landing
* is part of the deep-link acceptance, so it gets a test too. */
export const NOTIFICATION_TEST_CLASSES: string[] = [...NOTIFICATION_CLASSES.map((c) => c.id), "digest"];

export function isNotificationTestClass(value: unknown): value is string {
return typeof value === "string" && NOTIFICATION_TEST_CLASSES.includes(value);
}

/** The test item's stable object ref for a class test send. The
* `test:` prefix keeps test traffic distinguishable in the sends
* ledger from real candidates of the same class. */
export const classTestObjectRef = (cls: string): string => `test:${cls}`;

/** Deep-link landing for a per-class test notification (design §3,
* #3318 acceptance). With a real candidate in hand, the landing is
* the candidate's own URL — the same URL its dashboard card links
* to. Without one, the test item lands on the view the engine itself
* assigns that class: incident → its anchored Alerts card, quota
* classes → /quota, stack-health → /health, and the attention
* classes → /attention anchored to the test item (mirroring
* tierItemCandidate's fallback for items with no session/GitHub
* ref). Never the origin root; the only list-shaped landing is the
* digest case, filtered to the digest's refs. */
export function notificationTestLanding(
cls: string,
origin: string,
candidate?: Pick<NotificationCandidate, "url"> | null,
): string {
if (candidate?.url) return /^https?:\/\//.test(candidate.url) ? candidate.url : `${origin}${candidate.url}`;
switch (cls) {
case "incident":
return `${origin}/alerts#incident-test`;
case "quota-wall":
case "quota-near-wall":
return `${origin}/quota`;
case "stack-health":
return `${origin}/health`;
case "digest":
return `${origin}/attention?refs=${encodeURIComponent("test:needs-drew,test:blocked-age")}`;
case "needs-drew":
return `${origin}/attention#test-needs-drew`;
case "blocked-age":
return `${origin}/attention#test-blocked-age`;
default:
return `${origin}/alerts`;
}
}

export const NOTIFICATION_COOLDOWN_MS = 6 * 60 * 60_000;
export const BLOCKED_AGE_REMINDER_MS = 24 * 60 * 60_000;
export const BLOCKED_AGE_BREAKTHROUGH_MS = 48 * 60 * 60_000;
Expand Down
3 changes: 3 additions & 0 deletions pm-stack/aggregator/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ export const SUBROUTE_PARENTS: { name: string; prefix: string; parent: string }[
{ name: "discuss", prefix: "/discuss/", parent: "/" },
{ name: "program-project", prefix: "/program/", parent: "/program" },
{ name: "service-detail", prefix: "/quota/service/", parent: "/quota" },
// #3318: the notification preferences page is reached from /alerts
// (and linked from the /notifications measurement page).
{ name: "alerts-preferences", prefix: "/alerts/preferences", parent: "/alerts" },
// #1497: Projects merged into Program — /projects redirects to the
// pool section of /program, which is its manifest parent.
{ name: "projects-merge", prefix: "/projects", parent: "/program" },
Expand Down
Loading
Loading