Skip to content

Chapter 9 — Alerts

9.1 — Chapter summary

⚙ Process: the alert engine (alerts/engine.py::execute_alert_query + _build_ch_ctes) — runs on a schedule or on demand, evaluates metric rules against the PIPE_ tables, and writes pipe_alerts. Configured through the metric ontology (config_alert_metric_ontology — 62 metrics) + the synonym map (config_alert_synonyms — 558 entries). ClickHouse REQUIRED — the engine raises RuntimeError when CH is disabled (no PG fallback).

All PIPE_ tables (forecast, netting, MEIO, supply, backtest, allocation…) → Metric ontology 62 metrics + 558 synonyms → alert engine rule evaluation per metric (threshold, direction, window) → pipe_alerts (PG, per run) → Alerts panel + feedback loop

The one-sentence model: alerts are the planner's standing watch — rules over the planning metrics ("fill rate below 85%", "netting consumption above 50%", "stock below safety"), evaluated against the ClickHouse outputs, triaged in the panel, and the feedback loop feeds alert quality back.

9.2 — The metric ontology & the synonym map (live data)

Alert component Role and evidence
config_alert_metric_ontology Count: 62 metrics.
What it holds: canonical metric name, description, CH source table/columns, default threshold, and aggregation direction.
Example: meio_fill_rate means achieved fill rate from PIPE_meio_results, alerting when below target.
config_alert_synonyms Count: 558 entries.
What it holds: the natural-language keyword map from planner phrasings to metrics.
Example: “service level” → meio_fill_rate; “stockout” → shortage_count.
pipe_alerts Count: 0 on the demo tenant.
What it holds: alert results, one row per (alert rule, item/site, evaluation), with metric value and breach status.
Example: empty until an alert scenario runs.

ClickHouse REQUIRED: all PIPE_* alert inputs are CH-primary. The engine raises RuntimeError when CH is disabled/unreachable — never a silent zero.

9.3 — Alert rules configuration & the feedback loop (vs exceptions)

⚙ Process: the alert engine — a QUERY surface, not a pipeline step and not a scenario type. Run: the Alerts panel's 🔍 Query, or the batch refresh (every enabled rule). Configure: Settings → AI → Ontology — edit the metric rules, teach the synonyms (the API equivalents are in 9.D).

There is no "alert scenario". The rule set is TENANT-WIDE master data: config_alert_metric_ontology (62 rules — field, operator, threshold, severity, enabled) + config_alert_synonyms (558 planner phrasings → canonical metric). What IS per-pipeline is the query CONTEXT: execute_alert_query requires a pipeline_id and resolves that pipeline's per-type scenario ids + the latest consumable archives exactly like every other PIPE_ reader (_build_ch_ctes — the same scenario-guard + archive-pinning chain). The same rules evaluated on different pipelines see different data; the rules themselves never vary per pipeline.

Alert lifecycle stage What happens
1 · Parse The NL phrase ("too much stock and bad forecast") → the LLM parser (Ollama mistral, env-tunable, low timeout) with the TF-IDF cosine-similarity fallback → conditions [(field, operator, threshold)]; known-field ontology hits get synthetic confidence
2 · Evaluate execute_alert_query — CH REQUIRED (RuntimeError when disabled: the PG pipe_* mirrors are dead DDL); per-type scenario ids + latest-archive pinned CTEs; rows enriched with explanation ("High Stock: 142 days (threshold: 90)") and sorted severity-first
3 · Triage The Alerts panel: severity badges, metric pills (value vs threshold), the explanation line, the drill link to the series page
4 · Feedback Approve ("action needed") / Reject ("false positive") → pipe_alerts.status + an interact_alert_feedback row (action, comment, user) + the auto-approval audit trail (recommendation_type='alert_action')

The feedback loop — what is real today. The learning channel that exists is the synonym map: taught terms (learned=TRUE) merge into the TF-IDF index at engine build, so the planner's vocabulary widens what the rules catch. The approve/reject signal is RECORDED per alert, per user, with comments — but no engine reader consumes it yet for priority/filtering (honest gap flag: the recorded signal is the raw material, the consumer is a code-change candidate). Note also: save_alerts_to_db is a no-op since the PG alert store was removed — the panel's Saved tab reads pipe_alerts, which has no writer today, so alerts are transient query results unless CH-persisted via save_alerts_to_ch (exc_type='alert' rows in PIPE_series_exceptions — defined, currently uncalled).

Different from exceptions? Yes — a different system on a different layer.

Exceptions (ch. 8) Alerts (ch. 9)
What it is Engine-DETECTED plan violations, written by the pipeline's exceptions step every run Planner-ASKED queries over the planning metrics — a standing watch, evaluated on demand
Configuration An exception scenario per pipeline (thresholds, snoozes, review log) Tenant-wide ontology + synonyms; the pipeline enters only as the query context
Output PIPE_supply_exceptions / PIPE_series_exceptions rows, scenario-tagged Transient result lists (the CH persistence path exists but is uncalled)
Feeds planning? No — a read-only observer No — feedback tunes the QUERY surface, never the plan

9.D — For developers — the alert APIs

The UI paths above cover the planner's loop; the equivalents for scripts and add-ons:

Call Purpose
POST /api/alerts/query · POST /api/alerts/batch The 🔍 Query and the batch refresh (every enabled rule).
PUT /api/alerts/ontology/{metric} · DELETE /api/alerts/ontology/{metric} · POST /api/alerts/ontology/seed Edit / remove / re-seed the metric rules.
POST /api/alerts/synonyms Teach the parser new planner phrasings.
GET /api/alerts · POST /api/alerts/{id}/approve · POST /api/alerts/{id}/reject The saved list + the feedback actions.

9.T — Step-by-step test walkthrough

The alert query surface and its feedback loop.

# Where to look / what to click You should see
1 Open the Alerts panel the query box + saved views
2 Type a question (e.g. too much stock and bad forecast) → Query results with severity badges, metric pills (value vs threshold), explanation lines, drill links
3 Approve one alert (action needed) status recorded + the feedback row (action, comment, user)
4 Reject another (false positive) with a comment the reject signal is recorded per alert, per user
5 Settings → AI → Ontology the metric rules: field, operator, threshold, severity, enabled — edit a threshold (auto-save)
6 Teach a synonym (a new phrasing) the term joins the parse vocabulary for future queries

Alert rules

The alert rule (metric ontology) configuration surface.