mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:58:25 +01:00
The tick walked the projects in order and stopped at its budget, from the top, every time. So a project far enough down the list was never reached — not "eventually", never. Measured on prod while catching up the five-day outage: three consecutive ticks each reported `posted: 30, capped: true`, and across all three Heidi's wall went 0 → 0 → 0 correct links while the head of the list was re-sent its same thirty moments. The janitor looked busy and was starving its own tail, and the counters could not tell the difference: thirty posts is thirty posts whether they are thirty projects moving forward or one project repeating. Round-robin makes the budget a SHARE rather than a race — every project's queue is built first, then one moment is taken from each in turn. Every wall moves every tick, a project with a large backlog cannot hold the others hostage, and an empty queue simply stops being asked, so unused turns are reclaimed rather than reserved. verify: tsc, lint (0 errors), test:unit 252/252. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
97 lines
3.8 KiB
TypeScript
97 lines
3.8 KiB
TypeScript
/**
|
|
* The backfill janitor must not spend its budget being refused.
|
|
*
|
|
* OrangeCat allows 30 writes per minute per user (`rateLimitWriteAsync`,
|
|
* sliding 60s window). This janitor fired its whole tick as fast as the event
|
|
* loop allowed, so measured on prod 2026-09-22 — the first tick after a
|
|
* five-day publishing outage — it reported `posted: 30, failed: 20`. The first
|
|
* thirty landed, the bucket emptied, and every remaining attempt came back 429
|
|
* and was counted as a failure.
|
|
*
|
|
* That is not a flake to retry past. Any backlog larger than thirty burned the
|
|
* remainder of its budget on refusals, every tick, forever — and because the
|
|
* external ids are deterministic, the next day re-sent the same doomed twenty.
|
|
*
|
|
* Run: npx tsx scripts/test/backfill-paces-itself.ts
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import { join, dirname } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
const SRC = readFileSync(
|
|
join(ROOT, "src/app/api/crons/orangecat-promote-backfill/route.ts"),
|
|
"utf8",
|
|
);
|
|
|
|
let pass = 0;
|
|
let fail = 0;
|
|
function ok(cond: boolean, label: string) {
|
|
if (cond) {
|
|
pass++;
|
|
} else {
|
|
fail++;
|
|
console.error(`✗ ${label}`);
|
|
}
|
|
}
|
|
|
|
const paceMatch = /const PROMOTE_PACE_MS = ([\d_]+);/.exec(SRC);
|
|
const pace = paceMatch ? Number(paceMatch[1].replace(/_/g, "")) : 0;
|
|
|
|
ok(pace > 0, "the janitor declares a pace");
|
|
ok(
|
|
pace >= 2_000,
|
|
`the pace is at or under the neighbour's 30-per-minute limit (60000/30 = 2000ms); found ${pace}ms`,
|
|
);
|
|
ok(
|
|
/if \(attempted > 0\) await sleep\(PROMOTE_PACE_MS\);/.test(SRC),
|
|
"...and waits between emits, but not before the first one",
|
|
);
|
|
|
|
const budgetMatch = /const TIME_BUDGET_MS = ([\d_]+);/.exec(SRC);
|
|
const budget = budgetMatch ? Number(budgetMatch[1].replace(/_/g, "")) : 0;
|
|
ok(budget > 0, "the tick has a time budget");
|
|
ok(
|
|
budget < 120_000,
|
|
`it ends before the caller's 120s curl timeout, or the tick dies with no account of itself; found ${budget}ms`,
|
|
);
|
|
ok(
|
|
/Date\.now\(\) - startedAt > TIME_BUDGET_MS/.test(SRC) && /capped = true/.test(SRC),
|
|
"...and reports `capped` when it stops early, rather than looking complete",
|
|
);
|
|
|
|
// The pace only means something if a full tick still fits inside the budget —
|
|
// otherwise the cap is decorative and the real limit is the clock.
|
|
const capMatch = /const MAX_PROMOTES_PER_TICK = (\d+);/.exec(SRC);
|
|
const cap = capMatch ? Number(capMatch[1]) : 0;
|
|
ok(cap > 0, "the per-tick cap is still declared");
|
|
ok(
|
|
cap * pace > budget,
|
|
"the clock, not the cap, is what ends a large tick — which is why `capped` must be honest",
|
|
);
|
|
|
|
// ── every project gets a turn ───────────────────────────────────────────────
|
|
//
|
|
// The tick used to walk the projects in order and stop at the budget, from the
|
|
// top, every time — so a project far enough down the list was never reached.
|
|
// Not "eventually": never. Measured on prod while catching up a five-day
|
|
// outage, three consecutive ticks each reported `posted: 30, capped: true`,
|
|
// and across all three Heidi's wall went 0 → 0 → 0 while the head of the list
|
|
// was re-sent the same thirty moments. The janitor looked busy and was
|
|
// starving its own tail.
|
|
ok(
|
|
/const queues: Array<Array<\(\) => Promise<PromoteOutcome>>> = \[\];/.test(SRC),
|
|
"every project's work is queued before any of it is sent",
|
|
);
|
|
ok(
|
|
/for \(const queue of queues\) \{[\s\S]{0,200}queue\.shift\(\)/.test(SRC),
|
|
"...and the queues are drained round-robin, one moment per project per round",
|
|
);
|
|
ok(
|
|
!/outer: for \(const project of linked\)/.test(SRC),
|
|
"...not top-to-bottom, which is what could never reach the tail",
|
|
);
|
|
|
|
console.log(`${pass}/${pass + fail} backfill-paces-itself cases passed`);
|
|
if (fail > 0) process.exit(1);
|