/** * SetupTab/CortexActionsCard.tsx — confirmation-gated Setup Cortex actions. * * fetch-model / bench / verify-asset, plus the ENC-2c install-native-ort action * (rendered only while the native binding is NOT installed — derived from the * /api/setup-status poll's nativeOrtInstalledVersion). Uses the MaintenanceTab * ActionDef + window.confirm pattern; the POST body ALWAYS carries confirm:true * (the server hard-requires it). A 423 action_blocked_by_open_item surfaces the * gating blocker ids to the parent (which highlights the rows), a 400 * confirmation_required and a 404 disabled are rendered honestly, and on a * successful run the returned logName is shown with a bounded log tail fetch. * For install-native-ort the returned nativeOrtRetestResult (verdict/p95/RSS) is * rendered inline. */ import type React from "react"; import { useState, useCallback, useEffect } from "react"; import type { SetupCortexActionKind, SetupCortexStatusResponse, } from "../../types/setup-cortex"; import type { SetupStatusResponse } from "@contracts"; import { postSetupCortexAction, fetchSetupCortexActionLog, } from "../../api/setup-cortex"; import { fetchSetupStatus } from "../../api/client"; import { styles } from "./CortexSetupStyles"; interface ActionDef { key: SetupCortexActionKind; label: string; desc: string; dangerous: boolean; confirm: string; } const ACTIONS: ActionDef[] = [ { key: "fetch-model", label: "Fetch Model", desc: "Fetch the qualified encoder model asset", dangerous: true, confirm: "Fetch the encoder model asset onto this host? (Gated by any open hard gate.)", }, { key: "bench", label: "Bench Encoder", desc: "Benchmark the encoder runtime on this host", dangerous: true, confirm: "Run an encoder benchmark on this host? (Gated by any open hard gate.)", }, { key: "verify-asset", label: "Verify Asset", desc: "Re-verify the committed encoder asset", dangerous: false, confirm: "Re-verify the committed encoder asset?", }, { key: "install-native-ort", label: "Install Native ORT", desc: "Lazy-download + install the native onnxruntime binding", dangerous: true, confirm: "Download + install the native onnxruntime binding on this host? (npm-mediated, sha256-verified; then re-qualified.)", }, ]; /** ENC-2c: the retest portion rendered after a successful install-native-ort. */ interface RetestDisplay { verdict: string; p95Ms: number; rssMiB: number; backend: string | null; } interface ActionResultDisplay { action: string; logName: string; tail: string | null; complete: boolean | null; message: string; retest: RetestDisplay | null; } export function CortexActionsCard({ data, onBlocked, onAction, }: { data: SetupCortexStatusResponse | null; onBlocked: (blockerIds: string[]) => void; onAction: () => void; }): React.ReactElement { const [running, setRunning] = useState(null); const [result, setResult] = useState(null); const [actionError, setActionError] = useState(null); const [disabled, setDisabled] = useState(false); // ENC-2c: poll the general setup status so we can hide the install button once // the native binding is present (CortexActionsCard's cortex status prop does // not carry nativeOrtInstalledVersion). const [setupStatus, setSetupStatus] = useState(null); const loadSetupStatus = useCallback(() => { fetchSetupStatus() .then((s) => { setSetupStatus(s); // A successful poll proves the server is reachable and serving — any // latched "actions disabled" banner from an earlier transient error // (404/500 on a previous version) is stale; clear it. setDisabled(false); }) .catch(() => { /* best-effort poll; hide install button when unavailable */ }); }, []); useEffect(() => { loadSetupStatus(); const id = setInterval(loadSetupStatus, 5000); return () => clearInterval(id); }, [loadSetupStatus]); const nativeInstalled = (setupStatus?.nativeOrtInstalledVersion ?? null) !== null; const runAction = useCallback( async (def: ActionDef) => { setRunning(def.key); setActionError(null); setResult(null); setDisabled(false); onBlocked([]); try { const outcome = await postSetupCortexAction({ action: def.key, confirm: true, }); if (outcome.ok) { const r = outcome.result; let tail: string | null = null; let complete: boolean | null = null; try { const log = await fetchSetupCortexActionLog(r.logName); tail = log.tail; complete = log.complete; } catch { /* log tail is best-effort */ } setResult({ action: r.action, logName: r.logName, tail, complete, message: r.ok ? `${r.action} completed (spawned: ${r.spawned})` : `${r.action} did not complete cleanly`, retest: r.nativeOrtRetestResult != null ? { verdict: r.nativeOrtRetestResult.verdict, p95Ms: r.nativeOrtRetestResult.p95Ms, rssMiB: r.nativeOrtRetestResult.rssMiB, backend: r.nativeOrtBackendEffective ?? null, } : null, }); onAction(); } else if (outcome.error.error === "action_blocked_by_open_item") { onBlocked(outcome.error.blockers); setActionError( "The action is blocked by an open hard-gate item and was not run.", ); } else if (outcome.error.error === "confirmation_required") { setActionError( "The server rejected the action: explicit confirmation is required.", ); } else if (outcome.status === 404) { // 404 = the VC9B flag is genuinely off on the server — the only // response that justifies the "actions are disabled" banner. setDisabled(true); setActionError("Setup Cortex actions are disabled (off)."); } else { // Any other error (500, network, etc.) is transient — show it // inline, but do NOT latch the disabled banner on it. setActionError(`Action failed (${outcome.status}).`); } } catch (e) { setActionError(e instanceof Error ? e.message : String(e)); } finally { setRunning(null); } }, [onBlocked, onAction], ); const handleClick = (def: ActionDef) => { if (!window.confirm(def.confirm)) return; void runAction(def); }; return (

Cortex Actions

{ACTIONS.filter( // ENC-2c: hide the install button once the native binding is present. (def) => def.key !== "install-native-ort" || !nativeInstalled, ).map((def) => { const isRunning = running === def.key; return (
{def.label}
{def.desc}
); })} {disabled && (
Setup Cortex actions are disabled. Enable MEGACOMPACT_VC9B to run them.
)} {actionError && !disabled && (

{actionError}

)} {result && (
Result ({result.action}): {result.message} {result.logName && ( log: {result.logName} )} {result.retest !== null && (
Re-qualified:{" "} {result.retest.verdict} p95: {result.retest.p95Ms.toFixed(1)} ms RSS: {result.retest.rssMiB.toFixed(1)} MiB {result.retest.backend !== null && ( backend: {result.retest.backend} )}
)} {result.tail !== null && (
							{result.tail}
							{result.complete === false && "\n… (truncated)"}
						
)}
)} {!data?.enabled && !disabled && (

Cortex status unavailable — actions may not be schedulable.

)}
); }