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
6 changes: 5 additions & 1 deletion src/services/ai-slop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ export async function runGittensoryAiSlopAdvisory(env: Env, input: AiSlopInput):
// the daily neuron budget.
const freeCalls = input.providerKey ? 0 : WORKERS_SLOP_MAX_CALLS;
const estimatedNeurons = freeCalls === 0 ? 0 : estimateNeurons(SLOP_SYSTEM_PROMPT.length + user.length, maxTokens, freeCalls);
const budget = clampNumber(Number(env.AI_DAILY_NEURON_BUDGET || 10000), 0, 1_000_000);
// Resolve the shared daily neuron budget IDENTICALLY to the AI review path (ai-review.ts): default HIGH
// (10,000,000) and clamp to 10,000,000 — both features sum into ONE usage counter (sumAiEstimatedNeuronsSince),
// so the old 10k default + 1M ceiling here starved slop AI into quota_exceeded well under the real shared budget.
const rawNeuronBudget = Number(env.AI_DAILY_NEURON_BUDGET);
const budget = clampNumber(env.AI_DAILY_NEURON_BUDGET && Number.isFinite(rawNeuronBudget) ? rawNeuronBudget : 10_000_000, 0, 10_000_000);
const used = await sumAiEstimatedNeuronsSince(env, utcDayStartIso());
const remainingBudget = Math.max(0, budget - used);
if (estimatedNeurons > remainingBudget) {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/ai-slop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ describe("runGittensoryAiSlopAdvisory gating + fail-safe", () => {
expect(result.estimatedNeurons).toBeGreaterThan(result.remainingBudget);
});

it("uses the full 10M shared budget, not the old 1M ceiling — slop survives heavy review spend (#review-audit)", async () => {
const run = vi.fn(async () => ({ response: "not json" }));
const env = createTestEnv({ AI: { run } as unknown as Ai, AI_SUMMARIES_ENABLED: "true", AI_PUBLIC_COMMENTS_ENABLED: "true", AI_DAILY_NEURON_BUDGET: "2000000" });
// Prior shared spend of 1.5M neurons — OVER the old 1M ceiling, well under the 2M shared budget.
await recordAiUsageEvent(env, { feature: "ai_review", model: "m", status: "ok", estimatedNeurons: 1_500_000 });
const result = await runGittensoryAiSlopAdvisory(env, baseInput);
expect(result.status).not.toBe("quota_exceeded"); // the old clamp(2M, 0, 1M) = 1M budget → quota_exceeded at 1.5M used
expect(run).toHaveBeenCalled();
});

it("defaults the budget HIGH (10M) when AI_DAILY_NEURON_BUDGET is unset/invalid — no 10k starvation (#review-audit)", async () => {
const run = vi.fn(async () => ({ response: "not json" }));
const env = createTestEnv({ AI: { run } as unknown as Ai, AI_SUMMARIES_ENABLED: "true", AI_PUBLIC_COMMENTS_ENABLED: "true", AI_DAILY_NEURON_BUDGET: "" });
// 2M prior spend — OVER the old 10k default, under the 10M default the fix uses.
await recordAiUsageEvent(env, { feature: "ai_review", model: "m", status: "ok", estimatedNeurons: 2_000_000 });
const result = await runGittensoryAiSlopAdvisory(env, baseInput);
expect(result.status).not.toBe("quota_exceeded"); // the old `|| 10000` default → quota_exceeded at 2M used
expect(run).toHaveBeenCalled();
});

it("records the pre-budgeted retry and fallback estimate when all Workers AI outputs are unusable", async () => {
const run = vi.fn(async () => ({ response: "not json" }));
const env = enabledEnv(run);
Expand Down
Loading