| 1 | # 0005 · The alert center |
| 2 | |
| 3 | - **Status**: Built · five kinds live (`source.sync_failed`, `llm.unreachable`, |
| 4 | `llm.rate_limited` #160, `llm.out_of_credit` #182, `data_source.schema_sync_failed`); |
| 5 | the panel has search and per-group paging; decisions 1 and 2 were overturned during |
| 6 | implementation; `document.no_text_layer` still unwired. |
| 7 | - **Written**: 2026-08-29 · condensed into English 2026-09-03 |
| 8 | - **Related**: adjacent to the Review queue |
| 9 | ([0001](0001-ontology-import-and-governance.md) P4); |
| 10 | [0004](0004-language-and-localization.md) is why titles are assembled on the client. |
| 11 | |
| 12 | ## The problem |
| 13 | |
| 14 | Failure state was scattered over six places with no single view: `jobs.status='failed'` + |
| 15 | `jobs.last_error` (no UI at all), `documents.status='failed'`, |
| 16 | `documents.graph_status='failed'`, `sources.last_sync_status='failed'`, |
| 17 | `source_sync_runs.status='failed'`, and the log. It had been patched once, by adding |
| 18 | `graph_error` to `documents`: one column per failure class. With reasoning, execution, OCR |
| 19 | and lakehouse connections still to come, that road ends with a dozen error columns and no |
| 20 | answer to "what is wrong right now". |
| 21 | |
| 22 | The real harm is silent failure. Drop in 100 PDFs, 12 of them scans, and the UI shows 100 |
| 23 | green rows; the user finds out when an answer lacks that contract, and never suspects |
| 24 | ingestion. |
| 25 | |
| 26 | **Boundary with Review.** Review holds what needs a decision (merge these two entities?); |
| 27 | the alert center holds what a person needs to know (this document did not get in). Review |
| 28 | guards the correctness of knowledge; alerts guard the health of the system. |
| 29 | `extraction_drops` (11 drop reasons, shown per document on the library row) is a third |
| 30 | channel: something in this document did not land. None of it goes to Review. |
| 31 | |
| 32 | ## Decisions |
| 33 | |
| 34 | 1. **Aggregation belongs to the view.** 100 PDFs with 12 scans must read as one line, "12 |
| 35 | documents have no text layer", or the alert center is ignored within two weeks. But a |
| 36 | stored aggregate is live and must be cleared when things recover, or it lies. So: one |
| 37 | row per failure, never updated; the read side folds adjacent rows of the same `(kb, |
| 38 | kind)` in SQL (gaps and islands, two `row_number()` subtractions), because paging is by |
| 39 | group and a client-side fold would split a run at a page boundary. Counts are computed |
| 40 | and never stale; a different failure in between is a different episode. |
| 41 | 2. **"Is it fixed?" is a question the alert center does not answer.** Every failure is a |
| 42 | new row. Once the fault is fixed there are no new rows; read ones sink and the badge |
| 43 | goes dark; a recurrence two months later is a new alert. No success signal, no probe, |
| 44 | no time constant. The cost is rows (a broken hourly source writes 24 a day), purged |
| 45 | after `alerting::RETAIN_DAYS` (30). Whether something is still broken is on the source |
| 46 | page and in document status; an alert's job is to make someone look. |
| 47 | 3. **Read state is per person.** `alert_reads` is a two-column table; unread means |
| 48 | "visible to me and not clicked by me". Reading says whether I have seen it, nothing |
| 49 | about whether it is over. Nobody can read an alert away for someone else, as with |
| 50 | GitHub notifications and Slack unread. |
| 51 | 4. **Visibility reuses the KB role chain.** `kb_id IS NULL` marks a system-level alert |
| 52 | (the three LLM kinds), visible to `users.is_admin` only; a KB-level alert is visible to |
| 53 | roles ≥ `min_role` in that KB, and admins see everything, as in `access::kb_role()`. |
| 54 | `min_role` lives on the row: configuration alerts go to admins, content alerts (parse, |
| 55 | extraction, source sync) to editors and above, because whoever uploaded the 12 scans |
| 56 | needs to know more than the admin does. |
| 57 | 5. **A row names one subject and carries no display text.** `subject_id` is a single |
| 58 | column, and `detail` keeps the subject's name so the alert renders after the subject is |
| 59 | deleted. Titles are assembled on the client by kind |
| 60 | ([0004](0004-language-and-localization.md)), so search matches KB name, `detail` and |
| 61 | the kind code, never the title. |
| 62 | 6. **Push is one global stream with no data and no permission check.** The per-KB SSE |
| 63 | route cannot carry a cross-KB badge, so `/alerts/events` rides the existing `AppEvent` |
| 64 | broadcast (kind `alert`) and only tells clients to refetch; the list query decides |
| 65 | visibility once. Someone without permission is woken for nothing, and the push path |
| 66 | holds no permission logic. |
| 67 | 7. **Classification is a pure function on error types.** `alert_for` decides the kind from |
| 68 | the error's type, never its text, and has unit tests; job failures reach it through |
| 69 | `observe_job_failure` only once retries are exhausted. `llm.unreachable` means "no |
| 70 | parseable answer from the endpoint": connection refused, or a proxy answering HTML. A |
| 71 | clean 4xx is the API saying the key or quota is wrong, another person's problem |
| 72 | (`rate_limited`, `out_of_credit`). |
| 73 | 8. **A bell with a popover**, because a page pulls people away from their work and then |
| 74 | nobody goes. The badge is a red dot ("something unseen" is binary; a count jumps with |
| 75 | every retry); a click marks read, hovering does not. |
| 76 | |
| 77 | ## Dead ends |
| 78 | |
| 79 | - **Stored aggregation**: `(kb_id, kind)` unique among unresolved rows, repeats appended |
| 80 | to `subject_ids UUID[]`. Built first; three bugs shared one root: the row was hollowed |
| 81 | out as things self-healed (empty `subject_ids` and `detail`) and produced titles like "0 |
| 82 | sources failed to sync". Its `WHERE resolved_at IS NULL` partial unique index would also |
| 83 | have failed silently for `kb_id IS NULL`, since NULL never collides; the same trap is |
| 84 | why `mark_group_read` compares `kb_id` with `IS NOT DISTINCT FROM`. |
| 85 | - **Self-healing** (`resolved_at` cleared by the producer). Every new kind must implement |
| 86 | its own "what counts as fixed": `source.sync_failed` gets it free from `finish_sync`; |
| 87 | `llm.unreachable` needed a background probe hitting the endpoint every minute, guarded |
| 88 | by "is the alert still lit?". A missing clear is invisible at compile time and shows as |
| 89 | an alert that never goes out. Recorded because the idea is tempting enough to be |
| 90 | proposed again. |
| 91 | - **Dedup by `(kb, kind, subject)` with a bumped timestamp**: bounded rows, no retention, |
| 92 | rejected on paper. Recurrence and "never fixed" become indistinguishable, so either |
| 93 | read-once-forever (a recurrence is silent) or every bump re-unreads (a known fault |
| 94 | lights up hourly). Missed alerts cost more than rows. |
| 95 | - **Shared read state**: any admin opening an alert marks it read for all. A glances in |
| 96 | the morning and puts it off; B and C never learn it happened, and no trace shows the |
| 97 | gap. |
| 98 | - **Reusing `audit_events`**: the ledger records what people did; alerts record what the |
| 99 | system did, with different retention (30 days versus never), visibility and readers. |
| 100 | - **A UNION over the six status columns, no table**: zero migrations, but no per-user |
| 101 | reads and no history; once the source recovers, "why did last Wednesday's sync fail" is |
| 102 | gone. |
| 103 | - **An empty framework first**: migrations are the hardest thing to take back, and |
| 104 | aggregation, self-healing and per-user reads can only be validated with data flowing. |
| 105 | Two real sources went first (`source.sync_failed`, and `llm.unreachable` for `kb_id IS |
| 106 | NULL`), and two of the three original decisions failed on them before any tag was cut. |
| 107 | |
| 108 | ## Revisions |
| 109 | |
| 110 | - The "resolved for everyone at once" half of decision 3 went with decision 2; the |
| 111 | per-person half stands. |
| 112 | - 2026-09-02: "no new permission logic" did not hold. The list filters in one SQL |
| 113 | statement through `access::visible_kb_roles()`, a `VISIBLE` CASE and a `rank()` in |
| 114 | `alerts.rs` that must stay in the same order as `Role`'s `PartialOrd`: three places to |
| 115 | edit when roles change, exactly the hidden rule this repository fears. |
| 116 | - 2026-09-02: three more kinds were wired from existing error classes with no new |
| 117 | detection logic. `data_source.schema_sync_failed` is raised from a state left behind |
| 118 | (source attached, schema never ingested), so "any execution path reports on failure" was |
| 119 | too narrow. |
| 120 | - `llm.unreachable` first matched only transport failures, so the commonest fault (a wrong |
| 121 | URL, a proxy answering HTML) produced no alert at all. |
| 122 | |
| 123 | ## Open questions |
| 124 | |
| 125 | - **`document.no_text_layer`** waits for detection (an empty parse result) and an OCR |
| 126 | endpoint, so the message can say "no text layer; configure OCR and reprocess". When |
| 127 | wired, do not ask when it is fixed: one row per scan, the panel folds adjacent rows, |
| 128 | reprocessing needs no cleanup. |