Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/pages/review-tryout/ReviewTryOut.tsx
iris cffe8895df review classifier: the most classifications a run carries is a setting
The limit was a constant of ten in swarm-wire, so a repo with eleven rules
could not be reviewed and nothing short of a rebuild changed it. It is now
`max_classifications` in the classifier settings, beside
`max_concurrent_reviews`: 1 to 1000, ten until set, and a document stored
before the field decodes with the default. The classifier settings form in
the swarm UI gets a "rules per review" field, and the try-out's add button
stops at the saved limit instead of a hard-coded ten.

The controller checks a try-out, and a requested review's rules file, against
the setting where the run starts. The rules file itself may hold up to the
1000 the setting allows, so raising the limit needs no change to a repo.
A review whose rules exceed the limit posts a no-verdict comment saying so.

A run's reply deadline was a constant that covered ten classifications, a
minute plus three each. It is now computed from the run's own size, so a
bigger run waits as long as it needs and a default-sized one still waits 31
minutes. The classifier's answer window in the queue permissions covers the
biggest run the setting allows.
2026-11-07 11:32:04 +01:00

519 lines
17 KiB
TypeScript

// <ReviewTryOut> — write a review classification and see how the model
// answers it on a real PR, before it goes into a repo's config
// (`docs/4-implementation/swarm/review-classifier.md`, "Try-out"). Pick a repo and a PR, write
// one or more classifications, run: the page posts them to
// swarm-controller's `POST /api/review-classifier/try-out`, which answers
// `202 {id}`, and polls `GET .../try-out/{id}` until the run is `done` or
// `failed`. A run takes a minute or more, so the page says it is running.
//
// The repo and PR fields offer the repos that have an open PR and the open PRs
// of the repo (`GET /api/pulls/repos`, `GET /api/pulls?repo=…`), and stay free
// text: the run also works on a merged or closed PR.
//
// **load the repo's rules** fills the rows from the rules on the repo's default
// branch (`GET /api/review-classifier/rules`), to run as they are or to edit.
//
// **open a PR** posts the rows to `POST /api/review-classifier/open-pr`, which
// has the classifier's forge account push a branch adding them to the repo's
// rules file, with the operator as commit author, and links to the forge's page
// for opening the PR from that branch.
//
// The inputs stay as typed after a run, so the operator can edit and run
// again. The section keeps nothing you typed and no
// result, and a run posts nothing to the forge; the only thing the browser
// remembers is the result table's column filters, as for every table in the UI.
import { useEffect, useState } from "preact/hooks";
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
import { ActionRow } from "../../ui/action-row/ActionRow.js";
import { Button } from "../../ui/button/Button.js";
import { FieldGrid } from "../../ui/field-grid/FieldGrid.js";
import { ConfirmDialog } from "../../ui/confirm-dialog/ConfirmDialog.js";
import { Disclosure } from "../../ui/disclosure/Disclosure.js";
import { ExternalLink } from "../../ui/external-link/ExternalLink.js";
import { Panel } from "../../ui/panel/Panel.js";
import { TextField } from "../../ui/text-field/TextField.js";
import { ClassificationEditor } from "./ClassificationEditor.js";
import { TryOutResult } from "../review-common/TryOutResult.js";
import {
addProblem,
draftFromRule,
hasTypedText,
prNumber,
repoProblem,
rowsProblem,
runProblem,
toBody,
type ClassificationDraft,
type OpenPrAnswer,
type OpenPull,
type RulesAnswer,
type TryOutResult as Result,
} from "./TryOutTypes.js";
import "./ReviewTryOut.css";
const TRY_OUT = "/api/review-classifier/try-out";
const RULES = "/api/review-classifier/rules";
const OPEN_PR = "/api/review-classifier/open-pr";
const SETTINGS = "/api/review-classifier/settings";
const PULL_REPOS = "/api/pulls/repos";
const PULLS = "/api/pulls";
// How long the repo field rests before its PRs are fetched, so typing a repo
// does not ask for the PRs of each prefix of it.
const PICK_DELAY_MS = 300;
const POLL_MS = 2000;
// Ids the buttons' `aria-describedby` points at.
const HINT_ID = "tryout-hint";
const CAP_ID = "tryout-cap";
let nextKey = 0;
function blankDraft(): ClassificationDraft {
return {
key: nextKey++,
name: "",
description: "",
polarity: "positive",
threshold: "80",
};
}
export function ReviewTryOut() {
const [repo, setRepo] = useState("");
const [pr, setPr] = useState("");
const [drafts, setDrafts] = useState<ClassificationDraft[]>(() => [
blankDraft(),
]);
// The id of the run being polled; null when none is in flight.
const [runId, setRunId] = useState<string | null>(null);
const [starting, setStarting] = useState(false);
const [result, setResult] = useState<Result | null>(null);
const [error, setError] = useState<ProblemDetails | null>(null);
// Whole seconds since the run started, so a long wait shows it is alive.
const [elapsed, setElapsed] = useState(0);
const [loadingRules, setLoadingRules] = useState(false);
const [rulesError, setRulesError] = useState<ProblemDetails | null>(null);
// Where the rows came from, until the next load replaces it.
const [loaded, setLoaded] = useState<(RulesAnswer & { repo: string }) | null>(
null,
);
const [confirmLoad, setConfirmLoad] = useState(false);
const [opening, setOpening] = useState(false);
const [opened, setOpened] = useState<
(OpenPrAnswer & { repo: string }) | null
>(null);
const [openError, setOpenError] = useState<ProblemDetails | null>(null);
// What the two fields offer to complete to. The fields stay free text, so a
// repo or a PR the lists lack, a merged one say, can still be typed.
const [repoChoices, setRepoChoices] = useState<string[]>([]);
const [repoChoicesError, setRepoChoicesError] =
useState<ProblemDetails | null>(null);
const [pulls, setPulls] = useState<OpenPull[]>([]);
const [pullsError, setPullsError] = useState<ProblemDetails | null>(null);
const [maxClassifications, setMaxClassifications] = useState<number | null>(
null,
);
const running = starting || runId !== null;
// Folded until wanted. What was typed survives a fold, and so does a run's
// result, which sits under the form rather than in it.
const [open, setOpen] = useState(false);
// The repos with an open PR, read once when the page opens.
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const res = await fetch(PULL_REPOS);
if (cancelled) return;
if (!res.ok) {
setRepoChoicesError(await readApiError(res));
return;
}
setRepoChoices((await res.json()) as string[]);
} catch (e: unknown) {
if (!cancelled) setRepoChoicesError({ detail: String(e) });
}
})();
return () => {
cancelled = true;
};
}, []);
// How many rows a run takes: the classifier settings', read once. Until it
// is known, or when it cannot be read, the run endpoint is the only cap.
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const res = await fetch(SETTINGS);
if (cancelled || !res.ok) return;
const body = (await res.json()) as { max_classifications?: number };
if (!cancelled && typeof body.max_classifications === "number") {
setMaxClassifications(body.max_classifications);
}
} catch {
// The endpoint still refuses a run that is too long.
}
})();
return () => {
cancelled = true;
};
}, []);
// The open PRs of the repo once it is typed or picked and has stopped
// changing. A repo that is not valid yet has no list.
useEffect(() => {
setPulls([]);
setPullsError(null);
const target = repo.trim();
if (repoProblem(target) !== null) return;
let cancelled = false;
const timer = setTimeout(() => {
void (async () => {
try {
const res = await fetch(
`${PULLS}?repo=${encodeURIComponent(target)}`,
);
if (cancelled) return;
if (!res.ok) {
setPullsError(await readApiError(res));
return;
}
setPulls((await res.json()) as OpenPull[]);
} catch (e: unknown) {
if (!cancelled) setPullsError({ detail: String(e) });
}
})();
}, PICK_DELAY_MS);
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [repo]);
useEffect(() => {
if (!running) return;
setElapsed(0);
const t = setInterval(() => setElapsed((s) => s + 1), 1000);
return () => clearInterval(t);
}, [running]);
// Polls the run until it leaves `running`. Cancelled by the next run, or by
// leaving the page.
useEffect(() => {
if (runId === null) return;
let cancelled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const stop = (problem: ProblemDetails) => {
setError(problem);
setRunId(null);
};
async function poll() {
try {
const res = await fetch(
`${TRY_OUT}/${encodeURIComponent(runId ?? "")}`,
);
if (cancelled) return;
if (res.status === 404) {
stop({
detail:
"the result is gone, because it expired or the controller restarted: run it again",
});
return;
}
if (!res.ok) {
const problem = await readApiError(res);
if (!cancelled) stop(problem);
return;
}
const body = (await res.json()) as Result;
if (cancelled) return;
setResult(body);
if (body.state === "running")
timer = setTimeout(() => void poll(), POLL_MS);
else setRunId(null);
} catch (e: unknown) {
if (!cancelled) stop({ detail: String(e) });
}
}
void poll();
return () => {
cancelled = true;
if (timer !== undefined) clearTimeout(timer);
};
}, [runId]);
const openPrProblem = rowsProblem(repo, drafts);
const problem = runProblem(repo, pr, drafts);
const capProblem = addProblem(drafts.length, maxClassifications);
async function run(e: Event) {
e.preventDefault();
const number = prNumber(pr);
if (problem !== null || number === null) return;
setStarting(true);
setError(null);
setResult(null);
try {
const res = await fetch(TRY_OUT, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
repo: repo.trim(),
pr: number,
classifications: drafts.map(toBody),
}),
});
if (!res.ok) {
setError(await readApiError(res));
return;
}
setRunId(((await res.json()) as { id: string }).id);
} catch (err: unknown) {
setError({ detail: String(err) });
} finally {
setStarting(false);
}
}
// Reads the repo's rules from its default branch into the rows. A failed
// load leaves the rows as they were.
async function loadRules() {
const target = repo.trim();
setConfirmLoad(false);
setLoadingRules(true);
setRulesError(null);
try {
const res = await fetch(`${RULES}?repo=${encodeURIComponent(target)}`);
if (!res.ok) {
setRulesError(await readApiError(res));
return;
}
const answer = (await res.json()) as RulesAnswer;
setDrafts(answer.classifications.map((c) => draftFromRule(c, nextKey++)));
setLoaded({ ...answer, repo: target });
} catch (err: unknown) {
setRulesError({ detail: String(err) });
} finally {
setLoadingRules(false);
}
}
// Pushes the rows on a new branch of the repo and keeps the link to open
// the PR from it.
async function openPr() {
const target = repo.trim();
setOpening(true);
setOpened(null);
setOpenError(null);
try {
const res = await fetch(OPEN_PR, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
repo: target,
classifications: drafts.map(toBody),
}),
});
if (!res.ok) {
setOpenError(await readApiError(res));
return;
}
setOpened({ ...((await res.json()) as OpenPrAnswer), repo: target });
} catch (err: unknown) {
setOpenError({ detail: String(err) });
} finally {
setOpening(false);
}
}
// Typed rows are the operator's work, so replacing them is asked first.
function askLoadRules() {
if (hasTypedText(drafts)) setConfirmLoad(true);
else void loadRules();
}
function changeDraft(next: ClassificationDraft) {
setDrafts((ds) => ds.map((d) => (d.key === next.key ? next : d)));
}
return (
<div class="ui-tryout-page">
<Disclosure
title="try a classification"
icon="🧪"
hint="run one on a real PR before it goes into a repo's rules"
open={open}
onToggle={setOpen}
>
<p>
Run a review classification against a real PR and see how the model
answers it. A run posts nothing to the forge and changes no rules, but
it does appear under recent runs: change the classification and run it
again until it answers the way you want. Then open a PR to add it to
the repo's rules.
</p>
<form class="ui-tryout-form" onSubmit={(e) => void run(e)}>
<FieldGrid class="ui-tryout-target">
<TextField
id="tryout-repo"
label="repo"
value={repo}
placeholder="owner/name"
onInput={setRepo}
suggestions={repoChoices.map((value) => ({ value }))}
/>
<TextField
id="tryout-pr"
label="PR number"
value={pr}
pattern="[0-9]+"
onInput={setPr}
suggestions={pulls.map((p) => ({
value: String(p.number),
label: p.title,
}))}
/>
<Button
onClick={askLoadRules}
disabled={loadingRules || running || repoProblem(repo) !== null}
>
{loadingRules ? "loading…" : "load the repo's rules"}
</Button>
</FieldGrid>
{repoChoicesError ? (
<ApiErrorPanel
context="the repos with an open PR could not be listed"
problem={repoChoicesError}
/>
) : null}
{pullsError ? (
<ApiErrorPanel
context="the repo's open PRs could not be listed"
problem={pullsError}
/>
) : null}
{rulesError ? (
<ApiErrorPanel
context="the repo's rules could not be loaded"
problem={rulesError}
/>
) : null}
{loaded ? (
<p class="ui-tryout-loaded">
Loaded {loaded.classifications.length} from{" "}
<code>{loaded.path}</code> on <code>{loaded.branch}</code> of{" "}
{loaded.repo}, read at commit{" "}
<code>{loaded.commit.slice(0, 12)}</code>. Edit them below, then
run.
</p>
) : null}
{drafts.map((d) => (
<ClassificationEditor
key={d.key}
draft={d}
onChange={changeDraft}
onRemove={
drafts.length > 1
? () => setDrafts((ds) => ds.filter((x) => x.key !== d.key))
: undefined
}
/>
))}
<ActionRow class="ui-tryout-actions">
<Button
onClick={() => setDrafts((ds) => [...ds, blankDraft()])}
disabled={capProblem !== null}
describedBy={capProblem !== null ? CAP_ID : undefined}
>
add a classification
</Button>
<Button
variant="primary"
type="submit"
disabled={running || problem !== null}
describedBy={problem !== null ? HINT_ID : undefined}
>
{running ? "running…" : "run"}
</Button>
<Button
onClick={() => void openPr()}
disabled={opening || openPrProblem !== null}
describedBy={openPrProblem !== null ? HINT_ID : undefined}
>
{opening ? "pushing…" : "open a PR"}
</Button>
</ActionRow>
{capProblem !== null ? (
<p id={CAP_ID} class="ui-tryout-hint">
{capProblem}
</p>
) : null}
{problem !== null ? (
<p id={HINT_ID} class="ui-tryout-hint">
{problem}
</p>
) : null}
{openError ? (
<ApiErrorPanel
context="the branch for the PR could not be pushed"
problem={openError}
/>
) : null}
{opened ? (
<p class="ui-tryout-loaded">
Pushed <code>{opened.branch}</code> to {opened.repo}, changing{" "}
<code>{opened.path}</code> with you as the author.{" "}
<ExternalLink href={opened.compare_url}>
Open the PR on the forge
</ExternalLink>
.
</p>
) : null}
</form>
</Disclosure>
<ConfirmDialog
open={confirmLoad}
label="replace the classifications"
confirmLabel="replace them"
onCancel={() => setConfirmLoad(false)}
onConfirm={() => void loadRules()}
>
<p>
Loading the rules of {repo.trim()} replaces the classifications you
have written below.
</p>
</ConfirmDialog>
{running || result || error ? (
<Panel title="result" icon="📋">
{running ? (
<>
<p class="ui-tryout-running" role="status">
Running — a run takes a minute or more, and up to half an hour
for a long list.
</p>
<p class="ui-tryout-running">{elapsed}s so far</p>
</>
) : null}
{error ? (
<ApiErrorPanel context="the try-out failed" problem={error} />
) : null}
{result?.state === "failed" ? (
<ApiErrorPanel
context="the run failed"
problem={{
title: "the classifier could not finish",
detail: result.error ?? "no reason given",
}}
/>
) : null}
{result?.state === "done" ? <TryOutResult result={result} /> : null}
</Panel>
) : null}
</div>
);
}