Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pm-stack/cloudflare-export/cloudflare-quota-export.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ export const AI_NEURONS_DAY_GROUP_LIMIT = 10000;
export const AI_NEURONS_DAY_ALERT_THRESHOLD_DEFAULT = 50000;

/** Threshold from the environment: CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD,
* retunable without a code change. Unset, empty, non-numeric, or
* negative values fall back to the default — a typo must never
* silently disable (or zero) the alert. */
* retunable without a code change. Unset, empty, non-numeric, negative, or
* zero values fall back to the default — a typo must never silently disable
* (or zero) the alert, so the accepted range is strictly positive. A hex or
* fractional spelling that denotes a positive number (`0x10`, `0.5`) is
* accepted as that number, not silently reinterpreted. */
export function aiNeuronsDayAlertThreshold(env = process.env) {
const raw = env?.CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD;
if (raw == null || String(raw).trim() === "") return AI_NEURONS_DAY_ALERT_THRESHOLD_DEFAULT;
const n = Number(raw);
return Number.isFinite(n) && n >= 0 ? n : AI_NEURONS_DAY_ALERT_THRESHOLD_DEFAULT;
return Number.isFinite(n) && n > 0 ? n : AI_NEURONS_DAY_ALERT_THRESHOLD_DEFAULT;
}
/** The analytics scopes the day-to-day token needs for this collector. */
export const ANALYTICS_SCOPES = [
Expand Down
20 changes: 20 additions & 0 deletions pm-stack/cloudflare-export/cloudflare-quota-export.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,26 @@ test("aiNeuronsDayAlertThreshold: env override with honest fallback (#2287)", ()
assert.equal(aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: "" }), 50000);
assert.equal(aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: "abc" }), 50000);
assert.equal(aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: "-5" }), 50000);
// Zero is a typo, never a threshold: every spelling falls back to the
// default, so a single neuron can never trip the daily alert (#4381).
for (const zero of ["0", "-0", "0.0", " 0 ", "0e5"]) {
assert.equal(
aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: zero }),
50000,
`zero spelling ${JSON.stringify(zero)} must fall back to the default`,
);
}
// Spellings that denote a positive number are accepted as that number;
// pinned so the parse cannot drift silently either way.
assert.equal(aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: "0x10" }), 16);
assert.equal(aiNeuronsDayAlertThreshold({ CLOUDFLARE_AI_NEURONS_DAY_ALERT_THRESHOLD: "0.5" }), 0.5);
// The gate a zeroed threshold would have opened: at the threshold exactly
// nothing fires, one neuron above it does.
const oneDay = (neurons) => [
{ dimensions: { date: "2026-11-01", modelId: "@cf/meta/llama" }, sum: { totalNeurons: neurons } },
];
assert.deepEqual(aiNeuronsDayAlerts(oneDay(50000), 50000).alerts, []);
assert.equal(aiNeuronsDayAlerts(oneDay(50001), 50000).alerts.length, 1);
// A custom threshold actually gates the alert through parseGraphqlUsage.
const parsed = parseGraphqlUsage(ACCOUNT, { now: NOW, cycleStart: START, aiThreshold: 4000000 });
assert.deepEqual(parsed.aiNeurons.alerts, []);
Expand Down