mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 07:02:29 +01:00
* fix(orangecat): the wall went silent, and every link on it pointed at a login
Three failures on one seam, each invisible from inside Loki because the
symptom is on somebody else's page.
THE WALL STOPPED. OrangeCat rotated this account's refresh token at
2026-09-17 07:42:44 and issued a replacement that is still valid and was
never used; Loki kept presenting the one that rotation REVOKED at that
instant. Every refresh since answered 400, getOrangeCatLink returned null,
and every promote was skipped as "feature unavailable" — across every
project, for four days. The last entry to reach any OrangeCat wall was
posted eight minutes before that access token expired. The only trace was
66 console.warn lines on the box.
Verified by hashing the stored token and finding its row in OrangeCat's
oauth_refresh_tokens: revoked_at = 2026-09-17 07:42:44.
Three rules follow, none of them a repair: one refresh at a time per
account (the backfill cron fires up to 50 promotes at once, so a burst was
guaranteed to look like a broken link); a spent refresh token is never
written back (`?? account.refresh_token` reads as defensive and stores a
credential that can only ever fail); and a dead link raises an alert,
because re-linking needs a human click and nobody clicks what nobody sees.
EVERY LINK POINTED AT A LOGIN. Wall entries carried a hardcoded
`${LOKI_PUBLIC_ORIGIN}/projects` — the private dashboard. A stranger
following "via Loki" from Heidi's OrangeCat page got a sign-in form; the
owner got all 36 projects instead of the one the entry was about. Twelve
such links on that page alone. The right page has existed the whole time:
/fleet/<slug>, now derived exactly as the register derives it.
THAT PAGE WAS UNREADABLE ON A PHONE. Measured at 390px: 737px of roadmap
content in a 390px column, clipped by .ui-public-surface's own
overflow-x:hidden — 47% of every milestone line unreachable, no scrollbar,
no ellipsis. Cause: unbreakable GitHub URLs in prose. Fixed on the
ui-public-* prose tokens in globals.css, so it holds for every public page.
Also on that page: three of six "changelog" entries were internal dispatch
bookkeeping (two announcing a FAILED dispatch, stored truncated mid-word),
a `Source: <url>` provenance pointer rendered as an untickable milestone,
"0% recorded progress" under every item, and a raw ISO timestamp.
NO GATE COULD HAVE CAUGHT IT. audit-covers-every-page skipped every route
containing "[", so /fleet/[slug] — the template behind all 36 public
profiles — had never been measured at any width. Dynamic routes now take
the same decision as static ones: a concrete sample (/fleet/heidi,
/u/cato, one essay) or a line in NOT_MEASURED saying what it would take.
The overflow detector could not have seen it either — it reads
documentElement.scrollWidth, which the clip holds at the viewport width —
so there is a new one asking the reader's question: can I get to the rest
of this sentence? A listed route that 404s now fails, because a rotted
sample looks exactly like a covered route.
Two gates lied and are fixed with it: the audit's list parser ended an
array at the first "]" in a comment, and alert-registry read
`grant_type: "refresh_token"` as an unregistered alert type.
AND THE FEED IS NOW A CHOICE. Publishing did two things under one press:
it created the public page and enrolled the project in a live feed of
everything its agents did, with no switch — "stop telling everyone what my
agents are doing" cost you the funding page. user_projects.orangecat_autopost
separates them, three-state like auto_ship (null = nobody has chosen, and
an unanswered question is not consent). Reversible either way, without
touching the page. Already-published projects are backfilled to true:
introducing consent must not silently stop a feed someone relies on.
Left on, it is the strongest thing a public project page can carry —
dated evidence of work, which is the half of a transparency claim that
better copy cannot manufacture.
/fleet/[slug] also now reads CONSENT rather than ownership (it resolved any
slug in the owner's map, which the catalogue beside it has never done), and
a slug with no public profile gets an invitation instead of a dead end.
verify: tsc, lint (0 errors, 2 pre-existing warnings), test:unit 231/231.
The page itself is measured against prod after deploy — it cannot be
measured before.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(smoke): the pre-push gate blocked a push over another app's dev server
`/api/health` answering with a `"commit"` field was the whole identity test,
and every app in the fleet serves that route from the same template. On
2026-09-22 a Heidi dev server held :3000 first; Loki's 48-route auth smoke ran
against it, reported 23 routes returning 404, and failed the push — a verdict
about somebody else's app, delivered as a verdict about this one.
A name cannot be mistaken for another name: health now says which app answered,
and the hook asks for "loki" instead of for a shape. Additive to the payload,
so the deploy assertion, the uptime sweep and check-deploy-ref are unaffected.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
198 lines
8.6 KiB
TypeScript
198 lines
8.6 KiB
TypeScript
/**
|
|
* Inline self-test: an alert type must not outlive the code that raises it.
|
|
*
|
|
* THE FAULT THIS LOCKS DOWN
|
|
* -------------------------
|
|
* Measured in production 2026-08-26: ten alerts open, and **four were zombies**
|
|
* — `bill_due`, `stale_relationship`, `overdue_commitment`, `stalled_goal` —
|
|
* raised between 2026-05-10 and 2026-06-23 by features that no longer exist.
|
|
* Nothing could refresh them, nothing could auto-resolve them, and the `alerts`
|
|
* table records no "last confirmed" timestamp, so a live alarm and a 108-day-old
|
|
* fossil are indistinguishable from the row alone.
|
|
*
|
|
* Forty per cent of the surface was permanent noise. That matters because the
|
|
* rational response to a list that is mostly wrong is to stop reading it — and
|
|
* queued underneath those fossils were "a telemetry sensor has been dead for 77
|
|
* days" and "a machine is running a runner we replaced". A channel degrades to
|
|
* the reliability of its worst entry.
|
|
*
|
|
* BOTH DIRECTIONS, AND THE SECOND ONE IS THE POINT
|
|
* -----------------------------------------------
|
|
* producer ⊆ registry — nothing raises an alert this codebase cannot name.
|
|
* registry ⊆ producer — every registered type still has code that raises it.
|
|
*
|
|
* The second is what makes retiring a feature loud: delete the producer and CI
|
|
* goes red until the alert type is retired with it, at which point
|
|
* `sweep-orphan-alerts` clears the rows it left behind. Without it the registry
|
|
* becomes another hand-maintained list that rots exactly like the thing it
|
|
* documents.
|
|
*
|
|
* Run: npx tsx scripts/test/alert-registry.ts
|
|
*/
|
|
import { readFileSync, existsSync, readdirSync, statSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, resolve as resolvePath, join } from "node:path";
|
|
import { ALERT_TYPES, ALERT_TYPE_IDS, isRegisteredAlertType } from "@/config/alert-types";
|
|
|
|
function assert(condition: boolean, message: string): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const repoRoot = resolvePath(here, "../..");
|
|
|
|
/** Source with comments removed — a rule must never be satisfied by the prose
|
|
* that explains it. (Learned twice today: a CI gate matched `fetch-depth: 0`
|
|
* inside its own justification, and a husky check matched a counter-example
|
|
* it quoted.) */
|
|
function codeOf(absPath: string): string {
|
|
return readFileSync(absPath, "utf8")
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.split("\n")
|
|
.map((l) => l.replace(/\/\/.*$/, ""))
|
|
.join("\n");
|
|
}
|
|
|
|
function walk(dir: string, out: string[] = []): string[] {
|
|
for (const entry of readdirSync(dir)) {
|
|
if (entry === "node_modules" || entry === ".next") continue;
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) walk(full, out);
|
|
else if (full.endsWith(".ts") || full.endsWith(".tsx")) out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// ── The zombies must stay unregistered ─────────────────────────────────────
|
|
|
|
for (const dead of ["bill_due", "stale_relationship", "overdue_commitment", "stalled_goal"]) {
|
|
assert(
|
|
!isRegisteredAlertType(dead),
|
|
`"${dead}" is registered again, but nothing raises it — it was one of the four ` +
|
|
`types found open in production with no producer. Registering it would exempt ` +
|
|
`those rows from the sweep and put the fossils back on the surface.`,
|
|
);
|
|
}
|
|
|
|
// ── registry ⊆ producer: a registered type must still be raised somewhere ───
|
|
|
|
for (const [id, spec] of Object.entries(ALERT_TYPES)) {
|
|
const producer = resolvePath(repoRoot, spec.producer);
|
|
assert(
|
|
existsSync(producer),
|
|
`alert type "${id}" names producer ${spec.producer}, which does not exist. ` +
|
|
`If that feature was retired, remove the type from ALERT_TYPES — ` +
|
|
`sweep-orphan-alerts then clears the rows it left behind. Leaving the entry ` +
|
|
`is how bill_due sat open for 108 days.`,
|
|
);
|
|
assert(
|
|
codeOf(producer).includes(`"${id}"`),
|
|
`alert type "${id}" is registered to ${spec.producer}, but that file no ` +
|
|
`longer contains the literal — so nothing raises it and any open rows are ` +
|
|
`unrefreshable fossils. Retire the type, or point it at its real producer. ` +
|
|
`(Comments are stripped before this check, so documenting the id does not ` +
|
|
`count as raising it.)`,
|
|
);
|
|
}
|
|
|
|
// ── producer ⊆ registry: nothing raises a type the codebase cannot name ─────
|
|
|
|
const RAISERS = /insertActiveAlertOnce\(|refreshOrInsertActiveAlert\(/;
|
|
const srcFiles = walk(resolvePath(repoRoot, "src"));
|
|
let raiseSites = 0;
|
|
|
|
for (const file of srcFiles) {
|
|
if (file.endsWith("src/db/queries/alerts.ts")) continue; // the helpers themselves
|
|
const code = codeOf(file);
|
|
if (!RAISERS.test(code)) continue;
|
|
raiseSites += 1;
|
|
|
|
const literals = new Set<string>();
|
|
// `type:` must be a key of its own, not the tail of one. Without the
|
|
// preceding boundary this also matched `grant_type: "refresh_token"` in the
|
|
// OAuth refresh beside a raise site, and reported "refresh_token" as an
|
|
// unregistered alert type — a true-looking failure about a line that has
|
|
// nothing to do with alerts, on a file whose only crime was raising one.
|
|
for (const m of code.matchAll(/(?:ALERT_TYPE\s*=\s*|(?<![A-Za-z0-9_])type:\s*)"([a-z_]+)"/g)) {
|
|
literals.add(m[1]);
|
|
}
|
|
assert(
|
|
literals.size > 0,
|
|
`${file.replace(repoRoot + "/", "")} raises an alert but no type literal could ` +
|
|
`be read from it — this check would silently skip the file, which is how an ` +
|
|
`unregistered type gets in.`,
|
|
);
|
|
for (const literal of literals) {
|
|
assert(
|
|
isRegisteredAlertType(literal),
|
|
`${file.replace(repoRoot + "/", "")} raises alert type "${literal}", which is ` +
|
|
`not in ALERT_TYPES. Unregistered types are swept as orphans, so this alarm ` +
|
|
`would be cleared the morning after it first fires.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
// The scan is only meaningful if it found the raise sites at all — a regex that
|
|
// matches nothing passes every assertion above it.
|
|
assert(
|
|
raiseSites >= 5,
|
|
`only ${raiseSites} alert raise-site(s) found; expected at least 5. The scan ` +
|
|
`has stopped seeing the code it is supposed to police, so its silence means ` +
|
|
`nothing.`,
|
|
);
|
|
|
|
// ── The sweep must clear only UNREGISTERED types ───────────────────────────
|
|
|
|
const alertsQueries = codeOf(resolvePath(repoRoot, "src/db/queries/alerts.ts"));
|
|
|
|
/** Just one exported function's body. Scanning the whole file let a SIBLING
|
|
* satisfy the assertion: `eq(alerts.dismissed, false)` appears in
|
|
* insertActiveAlertOnce and dismissActiveAlertsByType too, so removing it from
|
|
* the sweep left this check green. Same scope blind spot that let a CI gate
|
|
* read a neighbouring job's setting as its own. */
|
|
function fnBody(source: string, name: string): string {
|
|
const start = source.indexOf(`export async function ${name}(`);
|
|
if (start === -1) return "";
|
|
const rest = source.slice(start + 1);
|
|
const end = rest.search(/^export /m);
|
|
return end === -1 ? rest : rest.slice(0, end);
|
|
}
|
|
|
|
const sweepBody = fnBody(alertsQueries, "dismissUnregisteredAlerts");
|
|
assert(
|
|
sweepBody.length > 0,
|
|
"src/db/queries/alerts.ts no longer exports dismissUnregisteredAlerts — the " +
|
|
"orphan sweep has no implementation to run.",
|
|
);
|
|
assert(
|
|
/notInArray\(\s*alerts\.type\s*,\s*ALERT_TYPE_IDS\s*\)/.test(sweepBody),
|
|
"dismissUnregisteredAlerts must clear only types absent from ALERT_TYPE_IDS. " +
|
|
"Anything broader would auto-dismiss live alarms, which is far worse than the " +
|
|
"noise it is cleaning up.",
|
|
);
|
|
assert(
|
|
/eq\(alerts\.dismissed,\s*false\)/.test(sweepBody),
|
|
"the sweep must touch only OPEN alerts — rewriting dismissed history would " +
|
|
"destroy the record of what was once true.",
|
|
);
|
|
|
|
// ── And it must actually be scheduled ──────────────────────────────────────
|
|
|
|
const sweepRoute = resolvePath(repoRoot, "src/app/api/crons/sweep-orphan-alerts/route.ts");
|
|
assert(
|
|
existsSync(sweepRoute) && /dismissUnregisteredAlerts\s*\(/.test(codeOf(sweepRoute)),
|
|
"sweep-orphan-alerts must exist and CALL the sweep — a mention is not a use.",
|
|
);
|
|
|
|
const sched = readFileSync(resolvePath(repoRoot, "scripts/install-hetzner-crons.sh"), "utf8");
|
|
assert(
|
|
/\[sweep-orphan-alerts\]="\d\d:\d\d"/.test(sched),
|
|
"sweep-orphan-alerts must be in the SCHED table of install-hetzner-crons.sh. " +
|
|
"An API route with no timer is a janitor nobody calls.",
|
|
);
|
|
|
|
console.log(
|
|
`✓ alert registry: ${ALERT_TYPE_IDS.length} types, all with live producers; ` +
|
|
`${raiseSites} raise sites, all registered`,
|
|
);
|