/**
* Review sampling panel: statistically grounded human review. Build a
* stratified sampling plan, execute (simulated) reviews, then read precision
* and recall estimates with Wilson confidence intervals per stratum.
*/
import { useState } from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Typography from '@mui/material/Typography';
import { ClipboardList, UserCheck } from 'lucide-react';
import type { MetricEstimate, ReviewState, SamplingFunnel, StratumPlan } from '../../types/megaeval';
import { executeReview, fmtCompact, planReview } from '../../services/megaeval';
import { ErrorNote, HBar, SectionCard, fmtPct } from '../labeleval/shared';
import { HeadCell } from '../help/InfoTip';
const FUNNEL_STAGES: Array<{ key: keyof SamplingFunnel; label: string; info: string }> = [
{ key: 'population_objects', label: 'Population objects', info: 'Every evaluated object in this run — the frame the estimates generalize to.' },
{ key: 'containers', label: 'Containers', info: 'Physical scene groupings containing those objects.' },
{ key: 'suspicious_containers', label: 'Suspicious containers', info: 'Containers with elevated risk score — dense errors, anomalies or safety-critical failures.' },
{ key: 'candidate_pool', label: 'Candidate pool', info: 'All review-eligible labels: error-index hits, low-confidence, anomalies, safety-critical.' },
{ key: 'statistically_selected', label: 'Statistically selected', info: 'The stratified risk-weighted sample actually drawn from the candidate pool.' },
{ key: 'reviewed', label: 'Reviewed', info: 'Sampled labels a human has actually reviewed so far.' },
];
function EstimateCard({ label, est }: { label: string; est: MetricEstimate }) {
return (
{fmtPct(est.estimate)}
95% CI {fmtPct(est.ci_low)}–{fmtPct(est.ci_high)} · n={est.n_reviewed.toLocaleString()} reviewed · frame{' '}
{fmtCompact(est.frame_size)}
{est.method}
{est.strata.map((s) => (
{s.stratum}
{fmtCompact(s.N)}
{s.n.toLocaleString()}
{fmtPct(s.p)}
{fmtPct(s.wilson_ci[0])}–{fmtPct(s.wilson_ci[1])}
))}
);
}
function PlanTable({ title, strata }: { title: string; strata: Record }) {
const entries = Object.entries(strata);
return (
{entries.map(([key, plan]) => (
{plan.label || key}
{fmtCompact(plan.N)}
{plan.allocated.toLocaleString()}
))}
);
}
export default function ReviewTab({
runId,
review,
onReview,
}: {
runId: string;
review: ReviewState | null;
onReview: (r: ReviewState) => void;
}) {
const [busy, setBusy] = useState<'plan' | 'execute' | null>(null);
const [error, setError] = useState(null);
const act = async (kind: 'plan' | 'execute') => {
setBusy(kind);
setError(null);
try {
onReview(kind === 'plan' ? await planReview(runId) : await executeReview(runId));
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setBusy(null);
}
};
const funnel = review?.funnel;
const max = funnel ? Math.max(1, funnel.population_objects) : 1;
return (
: }
disabled={busy !== null}
onClick={() => void act('plan')}
>
Build sampling plan
: }
disabled={busy !== null || !review?.planned}
onClick={() => void act('execute')}
>
Execute reviews (simulated)
}
>
{error ? : null}
{!review || !review.planned ? (
No sampling plan yet. Reviewing everything at this scale is impossible — build a stratified plan to
estimate precision and recall from a small, statistically selected sample.
) : (
<>
{review.method ?? 'stratified sampling'}
{review.target_n !== undefined ? ` · target n=${review.target_n.toLocaleString()}` : ''}
{review.executed ? ' · executed' : ' · planned, not executed'}
{funnel
? FUNNEL_STAGES.map((s) => (
))
: null}
>
)}
{review?.planned && !review.executed && review.strata ? (
) : null}
{review?.results ? (
) : null}
);
}