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
40 changes: 23 additions & 17 deletions src/queue/processors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {

Check warning on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 9 meaningful terms.

Check warning on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 11 meaningful terms.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 6 meaningful terms.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Issue discovery is disabled for this repo

This repo is configured for direct contribution review rather than issue-discovery flow.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Open PR queue is busy

This repo has a busy open PR queue in the local Gittensory cache.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

PR author has maintainer association

This PR appears to come from a maintainer-associated account.

Check warning on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 10 meaningful terms.

Check warning on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 11 meaningful terms.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 6 meaningful terms.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Issue discovery is disabled for this repo

This repo is configured for direct contribution review rather than issue-discovery flow.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Open PR queue is busy

This repo has a busy open PR queue in the local Gittensory cache.

Check notice on line 1 in src/queue/processors.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

PR author has maintainer association

This PR appears to come from a maintainer-associated account.
countOpenIssues,
countOpenPullRequests,
getAgentCommandAnswer,
Expand Down Expand Up @@ -940,8 +940,10 @@
/**
* AI-assisted slop advisory (opt-in `slopAiAdvisory`). Appends at most one ADVISORY-only `ai_slop_advisory`
* finding to the advisory; NEVER touches slopRisk or the gate (only the deterministic core can block). The
* caller gates on `settings.slopAiAdvisory` and reuses the already-fetched changed files. Fail-safe: any AI
* error is swallowed so the gate still finalizes.
* caller gates on `settings.slopAiAdvisory` and reuses the already-fetched changed files. Like the AI review
* path, it runs ONLY for confirmed contributors so an unconfirmed/untrusted PR author cannot spend either the
* shared Workers AI budget or the maintainer-paid BYOK quota. Fail-safe: any AI error is swallowed so the
* gate still finalizes.
*/
export async function runAiSlopForAdvisory(
env: Env,
Expand All @@ -953,13 +955,17 @@
author: string | null;
files: Awaited<ReturnType<typeof listPullRequestFiles>>;
deterministicBand: SlopBand;
confirmedContributor: boolean;
},
): Promise<void> {
if (!args.advisory.headSha) return;
// Confirmed-contributor gate (matches runAiReviewForAdvisory): no AI spend — free OR BYOK — on a PR from
// an unconfirmed author. The deterministic slop core still ran for everyone; only the AI layer is gated.
if (!args.confirmedContributor || !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.
// fallback). The contributor is already confirmed (early return above), so BYOK billing is authorized.
// The slop advisory stays advisory-only regardless of which model writes it.
const storedKey = args.settings.aiReviewByok ? await getDecryptedRepositoryAiKey(env, args.repoFullName) : null;
const providerKey =
storedKey && (!args.settings.aiReviewProvider || args.settings.aiReviewProvider === storedKey.provider)
Expand Down Expand Up @@ -1161,6 +1167,18 @@
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 @@ -1179,22 +1197,10 @@
// 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
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it, vi } from "vitest";

Check warning on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 9 meaningful terms.

Check warning on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 11 meaningful terms.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 6 meaningful terms.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Issue discovery is disabled for this repo

This repo is configured for direct contribution review rather than issue-discovery flow.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Open PR queue is busy

This repo has a busy open PR queue in the local Gittensory cache.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

PR author has maintainer association

This PR appears to come from a maintainer-associated account.

Check warning on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 10 meaningful terms.

Check warning on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 11 meaningful terms.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Possible duplicate overlap

Titles/paths share 6 meaningful terms.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Issue discovery is disabled for this repo

This repo is configured for direct contribution review rather than issue-discovery flow.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

Open PR queue is busy

This repo has a busy open PR queue in the local Gittensory cache.

Check notice on line 1 in test/unit/ai-slop.test.ts

View check run for this annotation

Deleted GitHub App / Gittensory Context

PR author has maintainer association

This PR appears to come from a maintainer-associated account.
import {
AI_SLOP_FINDING_CODE,
__aiSlopInternals,
Expand Down Expand Up @@ -275,6 +275,7 @@
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 @@
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 @@
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 @@
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("no-ops entirely for unconfirmed contributors — neither the maintainer BYOK key nor free Workers AI is spent", 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,
});

// Matches the AI review path: an unconfirmed author triggers no AI spend at all, so no finding lands.
expect(adv.findings).toEqual([]);
expect(fetchMock).not.toHaveBeenCalled();
expect(run).not.toHaveBeenCalled();
});
});