|
| 1 | +# Alerts Standards |
| 2 | + |
| 3 | +## 1. The `createAlert()` Contract |
| 4 | + |
| 5 | +Every new feature that may fail, produce notable events, or require user attention **MUST** emit alerts using the canonical `createAlert()` function: |
| 6 | + |
| 7 | +```typescript |
| 8 | +import { createAlert } from "@alerts"; |
| 9 | + |
| 10 | +await createAlert(env, { |
| 11 | + type: "deployment", // AlertType: health | webhook | security | deployment | agent | info |
| 12 | + severity: "error", // AlertSeverity: info | warning | error | critical |
| 13 | + title: "Deploy failed", |
| 14 | + description: "Wrangler exited with code 1. Check logs.", |
| 15 | + link_url: "/webhooks", // Optional deep-link (relative path) |
| 16 | + process_origin: "DeployWorkflow", // Human-readable origin |
| 17 | +}); |
| 18 | +``` |
| 19 | + |
| 20 | +`createAlert()` is fire-and-forget: it never throws, never blocks the main thread, and auto-gates based on `ALERTS_CONFIG` in KV. |
| 21 | + |
| 22 | +## 2. When to Emit Alerts |
| 23 | + |
| 24 | +| Scenario | Type | Severity | |
| 25 | +| ----------------------------------- | ------------ | --------------------- | |
| 26 | +| Health check failure | `health` | `error` or `critical` | |
| 27 | +| Webhook delivery failure (repeated) | `webhook` | `warning` | |
| 28 | +| Detected secret leak | `security` | `critical` | |
| 29 | +| Worker deploy failure | `deployment` | `error` | |
| 30 | +| Agent task failure after retries | `agent` | `error` | |
| 31 | +| Informational system event | `info` | `info` | |
| 32 | + |
| 33 | +Do **NOT** alert on: |
| 34 | + |
| 35 | +- Expected/transient 4xx responses |
| 36 | +- Individual tool-call retries (only alert on final failure) |
| 37 | +- Debug or verbose logs (use `console.log` / `@logging` for those) |
| 38 | + |
| 39 | +## 3. Config-Gated Alerts |
| 40 | + |
| 41 | +`createAlert()` reads `ALERTS_CONFIG` from `KV_CONFIGS` at emit time. If a type is disabled or the master switch is off, the alert is dropped silently. You do not need to check the config yourself — the service handles it. |
| 42 | + |
| 43 | +## 4. Path Alias |
| 44 | + |
| 45 | +Always use `@alerts` — never use a relative import: |
| 46 | + |
| 47 | +```typescript |
| 48 | +// ✅ Correct |
| 49 | +import { createAlert } from "@alerts"; |
| 50 | + |
| 51 | +// ❌ Wrong |
| 52 | +import { createAlert } from "../../alerts"; |
| 53 | +``` |
| 54 | + |
| 55 | +## 5. New Alert Types |
| 56 | + |
| 57 | +If you need a new alert type (beyond the 6 built-in), you must: |
| 58 | + |
| 59 | +1. Add it to `ALERT_TYPES` in `backend/src/db/schemas/app/alerts.ts` |
| 60 | +2. Add it to `AlertTypeFlags` in `backend/src/alerts/config.ts` |
| 61 | +3. Add its `TYPE_META` entry in `AlertTray.tsx` and `Alerts.tsx` |
| 62 | +4. Run `pnpm drizzle-kit generate` if schema changed |
0 commit comments