Skip to content
Closed
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
33 changes: 18 additions & 15 deletions src/queue/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,17 @@ export async function runAiSlopForAdvisory(
author: string | null;
files: Awaited<ReturnType<typeof listPullRequestFiles>>;
deterministicBand: SlopBand;
confirmedContributor: boolean;
},
): Promise<void> {
if (!args.advisory.headSha) return;
try {
// BYOK (opt-in): reuse the repo's encrypted key + aiReviewByok flag — one BYOK key serves both AI
// features. A declared provider must match the stored key's provider, else skip BYOK (Workers-AI
// fallback). The slop advisory stays advisory-only regardless of which model writes it.
const storedKey = args.settings.aiReviewByok ? await getDecryptedRepositoryAiKey(env, args.repoFullName) : null;
// fallback). Because BYOK bills the maintainer, only confirmed contributors may use it;
// unconfirmed PRs fall back to Workers AI. The slop advisory stays advisory-only regardless of
// which model writes it.
const storedKey = args.settings.aiReviewByok && args.confirmedContributor ? await getDecryptedRepositoryAiKey(env, args.repoFullName) : null;
const providerKey =
storedKey && (!args.settings.aiReviewProvider || args.settings.aiReviewProvider === storedKey.provider)
? { provider: storedKey.provider, key: storedKey.key, model: args.settings.aiReviewModel ?? storedKey.model }
Expand Down Expand Up @@ -1157,6 +1160,18 @@ async function maybePublishPrPublicSurface(
scopedOverlapCount: unionScopedOverlapClusters(collisions, pr, preflight.collisions).length,
});

if (gateEnabled && author && !publicSurfaceSkipped && !official) {
official = await getCachedOfficialMinerDetection(env, author, {
targetKey: `${repoFullName}#${pr.number}`,
deliveryId: webhook.deliveryId,
});
}

// Only CONFIRMED gittensor contributors can be hard-blocked; everyone else (or an unavailable
// detection) gets a neutral, non-blocking gate. Gate-only runs still verify confirmation before
// evaluating blockers so confirmed contributors cannot bypass a required Gate check.
const confirmedContributor = official?.status === "confirmed";

// Anti-slop (#530/#532): only when opted in (slopGateMode !== "off"). Surface the deterministic slop
// findings as advisory context, and feed the score to the gate (it only blocks under slop: block + the
// threshold). Loads files lazily so disabled repos pay nothing.
Expand All @@ -1175,22 +1190,10 @@ async function maybePublishPrPublicSurface(
// AI-assisted slop advisory (#533, opt-in). Reuses the already-fetched files; appends at most one
// advisory-only finding. Deliberately does NOT update slopRisk — only the deterministic core blocks.
if (settings.slopAiAdvisory) {
await runAiSlopForAdvisory(env, { settings, advisory, repoFullName, pr, author, files: slopFiles, deterministicBand: slop.band });
await runAiSlopForAdvisory(env, { settings, advisory, repoFullName, pr, author, files: slopFiles, deterministicBand: slop.band, confirmedContributor });
}
}

if (gateEnabled && author && !publicSurfaceSkipped && !official) {
official = await getCachedOfficialMinerDetection(env, author, {
targetKey: `${repoFullName}#${pr.number}`,
deliveryId: webhook.deliveryId,
});
}

// Only CONFIRMED gittensor contributors can be hard-blocked; everyone else (or an unavailable
// detection) gets a neutral, non-blocking gate. Gate-only runs still verify confirmation before
// evaluating blockers so confirmed contributors cannot bypass a required Gate check.
const confirmedContributor = official?.status === "confirmed";

// AI maintainer review (opt-in via aiReviewMode). Mutates `advisory` with a consensus defect (if any)
// BEFORE the gate evaluates, and returns advisory notes for the panel. Inside the try so any AI
// failure is caught and the gate is still finalized (never left in_progress).
Expand Down
37 changes: 35 additions & 2 deletions test/unit/ai-slop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ describe("runAiSlopForAdvisory (processor wiring)", () => {
author: "alice",
files,
deterministicBand: "elevated",
confirmedContributor: true,
});
expect(adv.findings.map((f) => f.code)).toEqual([AI_SLOP_FINDING_CODE]);
});
Expand All @@ -283,7 +284,7 @@ describe("runAiSlopForAdvisory (processor wiring)", () => {
const noSha = advisory();
delete (noSha as Partial<Advisory>).headSha;
const run = vi.fn();
await runAiSlopForAdvisory(enabledEnv(run), { settings: noByok, advisory: noSha, repoFullName: "acme/widgets", pr, author: "alice", files, deterministicBand: "low" });
await runAiSlopForAdvisory(enabledEnv(run), { settings: noByok, advisory: noSha, repoFullName: "acme/widgets", pr, author: "alice", files, deterministicBand: "low", confirmedContributor: true });
expect(noSha.findings).toEqual([]);
expect(run).not.toHaveBeenCalled();
});
Expand All @@ -298,14 +299,15 @@ describe("runAiSlopForAdvisory (processor wiring)", () => {
author: "alice",
files,
deterministicBand: "clean",
confirmedContributor: true,
});
expect(adv.findings).toEqual([]);
});

it("is fail-safe: a thrown error (broken DB) yields no finding and never throws", async () => {
const adv = advisory();
const env = { ...enabledEnv(async () => ({ response: slopJson() })), DB: undefined } as unknown as Env;
await expect(runAiSlopForAdvisory(env, { settings: noByok, advisory: adv, repoFullName: "acme/widgets", pr, author: "alice", files, deterministicBand: "high" })).resolves.toBeUndefined();
await expect(runAiSlopForAdvisory(env, { settings: noByok, advisory: adv, repoFullName: "acme/widgets", pr, author: "alice", files, deterministicBand: "high", confirmedContributor: true })).resolves.toBeUndefined();
expect(adv.findings).toEqual([]);
});

Expand All @@ -330,10 +332,41 @@ describe("runAiSlopForAdvisory (processor wiring)", () => {
author: "alice",
files,
deterministicBand: "elevated",
confirmedContributor: true,
});
// The advisory came from the BYOK provider (high band → finding), and Workers AI was never called.
expect(adv.findings.map((f) => f.code)).toEqual([AI_SLOP_FINDING_CODE]);
expect(fetchMock.mock.calls[0]?.[0]).toBe("https://api.anthropic.com/v1/messages");
expect(run).not.toHaveBeenCalled();
});

it("does not use the maintainer BYOK key for unconfirmed contributors", async () => {
const run = vi.fn(async () => ({ response: slopJson({ band: "high" }) }));
const env = createTestEnv({
AI: { run } as unknown as Ai,
AI_SUMMARIES_ENABLED: "true",
AI_PUBLIC_COMMENTS_ENABLED: "true",
AI_DAILY_NEURON_BUDGET: "100000",
TOKEN_ENCRYPTION_SECRET: "ai-slop-byok-test-encryption-secret-32b",
});
await upsertRepositoryAiKey(env, { repoFullName: "acme/widgets", provider: "anthropic", key: "sk-ant-byok-slop-9999", model: null });
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ content: [{ type: "text", text: slopJson({ band: "high" }) }] }), { status: 200 }));
vi.stubGlobal("fetch", fetchMock);
const adv = advisory();
await runAiSlopForAdvisory(env, {
settings: { aiReviewByok: true } as RepositorySettings,
advisory: adv,
repoFullName: "acme/widgets",
pr,
author: "mallory",
files,
deterministicBand: "elevated",
confirmedContributor: false,
});

expect(adv.findings.map((f) => f.code)).toEqual([AI_SLOP_FINDING_CODE]);
expect(fetchMock).not.toHaveBeenCalled();
expect(run).toHaveBeenCalled();
});

});