import { useCallback, useEffect, useRef, useState } from "react"; import type { ChangeEvent, FormEvent } from "react"; import { api, operatorCredentialReference, operatorSessionVersion } from "../lib/api"; import type { OperatorIdentity } from "../lib/api"; import type { DeploymentCandidate, ExecutionApproval, HardwareState, LabCapability, LabInput, LabPreflightResponse, LabTarget, RuntimeProbe, RuntimeProfile } from "../types"; const families: { key: LabCapability; name: string; modality: string }[] = [ { key: "rag.embedding", name: "Text embedding", modality: "embedding" }, { key: "rag.reranking", name: "Reranking", modality: "reranking" }, { key: "document.ocr", name: "Document OCR", modality: "document" }, { key: "vision.embedding", name: "Visual embedding", modality: "vision" }, { key: "speech.transcription", name: "Speech transcription", modality: "audio" }, ]; const limits = { timeout_seconds: 60, queue_timeout_seconds: 10, retention_seconds: 300, max_output_bytes: 65536, concurrency: 1 as const }; const MAX_FILE_BYTES = 360 * 1024; const MAX_REQUEST_BYTES = 512 * 1024; type Evidence = { candidates: DeploymentCandidate[]; profiles: RuntimeProfile[]; approvals: ExecutionApproval[]; probes: RuntimeProbe[]; hardware: HardwareState }; const empty: Evidence = { candidates: [], profiles: [], approvals: [], probes: [], hardware: { overview: { status: "unknown", inventory_state: "unknown", node_count: 0, accelerator_count: 0 }, nodes: [] } }; const approvalUsable = (item: ExecutionApproval) => item.scope === "lab_execution" && item.status === "approved" && !item.stale && !item.revoked_at && (!item.expires_at || Date.parse(item.expires_at) > Date.now()); const explanations: Record = { INPUT_LIMIT_EXCEEDED: "The input exceeds the server's item or text limit. Reduce the input.", PAYLOAD_LIMIT_EXCEEDED: "The encoded input exceeds the configured gateway limit (64 KiB by default). Use a smaller sample.", CAPABILITY_CONTRACT_UNAVAILABLE: "This capability version is unavailable in the server configuration.", CANDIDATE_NOT_FOUND: "The selected candidate no longer exists. Refresh the evidence.", CANDIDATE_NOT_LAB_READY: "The candidate must remain a non-production LAB-ready candidate.", TARGET_IDENTITY_MISMATCH: "The selected artifact, profile or node no longer matches the candidate.", EVIDENCE_MISSING: "Required approval, profile, node, assessment or probe evidence is missing.", EVIDENCE_IDENTITY_MISMATCH: "The reviewed evidence belongs to a different exact target.", CAPABILITY_PROFILE_MISMATCH: "The selected runtime profile does not support this capability family.", RUNTIME_POLICY_UNSUPPORTED: "This Lab check requires offline execution with remote code disabled.", NODE_NOT_READY: "The node is not online, enabled and eligible for this Lab operation.", ASSESSMENT_STALE_OR_BLOCKED: "Compatibility evidence changed or has unresolved blockers. Review it in Runtime.", LAB_APPROVAL_STALE: "The LAB approval expired, was revoked or no longer matches current artifact evidence.", ARTIFACT_EVIDENCE_MISSING: "Current artifact evidence is unavailable.", PROBE_NOT_COMPLETED: "A successfully completed, matching runtime probe is required.", PROBE_OUTPUT_INVALID: "The completed probe does not establish the expected output contract.", PROBED_ACCELERATOR_MISSING: "The accelerator used by the probe is no longer available on this node.", }; export function LabPreflightWorkspace({ role = "viewer" }: { role?: OperatorIdentity["role"] }) { const [evidence, setEvidence] = useState(empty); const [capability, setCapability] = useState(""); const [candidateId, setCandidateId] = useState(""); const [approvalId, setApprovalId] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const mounted = useRef(false); const loadSequence = useRef(0); const generation = operatorSessionVersion(); const current = useCallback(() => mounted.current && operatorSessionVersion() === generation && Boolean(operatorCredentialReference()), [generation]); const load = useCallback(async () => { if (!current()) return; const sequence = ++loadSequence.current; const valid = () => current() && sequence === loadSequence.current; setLoading(true); try { const [candidates, profiles, approvals, probes, hardware] = await Promise.all([api.deploymentCandidates(), api.runtimeProfiles(), api.executionApprovals(), api.runtimeProbes(), api.hardware()]); if (!valid()) return; setEvidence({ candidates, profiles, approvals, probes, hardware }); setCandidateId(id => candidates.some(item => item.id === id) ? id : ""); setApprovalId(id => approvals.some(item => item.id === id && approvalUsable(item)) ? id : ""); setError(null); } catch { if (!valid()) return; setEvidence(empty); setCandidateId(""); setApprovalId(""); setError("Lab evidence could not be loaded. Refresh after checking the connection."); } finally { if (valid()) setLoading(false); } }, [current]); useEffect(() => { mounted.current = true; setEvidence(empty); setCapability(""); setCandidateId(""); setApprovalId(""); setError(null); void load(); return () => { mounted.current = false; loadSequence.current += 1; }; }, [load]); const candidates = evidence.candidates.filter(item => !item.production && item.channel === "lab" && item.status === "lab_ready" && evidence.profiles.some(profile => profile.id === item.runtime_profile_id && profile.modality === families.find(family => family.key === capability)?.modality)); const candidate = candidates.find(item => item.id === candidateId); const profile = evidence.profiles.find(item => item.id === candidate?.runtime_profile_id); const node = evidence.hardware.nodes.find(item => item.id === candidate?.compute_node_id); const probe = evidence.probes.find(item => item.id === candidate?.runtime_probe_id); const approvals = evidence.approvals.filter(item => item.artifact_set_id === candidate?.artifact_set_id); const approval = approvals.find(item => item.id === approvalId && approvalUsable(item) && item.id === probe?.execution_approval_id); const target: LabTarget | null = candidate && profile && node && approval ? { deployment_candidate_id: candidate.id, artifact_set_id: candidate.artifact_set_id, runtime_profile_id: candidate.runtime_profile_id, compute_node_id: candidate.compute_node_id, execution_approval_id: approval.id } : null; const bindingKey = JSON.stringify([generation, role, capability, candidate, profile, node, probe, approval]); return

Check a Lab request

This preview checks an exact candidate and a small input sample. It does not run inference, reserve resources, create a job or grant execution permission.

Input remains in this view's memory and is sent only when you check it. The server returns redacted metadata. Changing the target, session or capability clears the input and result.

{error ?

{error}

: null} {!current() || role === "viewer" ?

Read-only inspection. A verified operator or administrator session is required to check a request.

: null}
{capability && !loading && !candidates.length ?

No LAB-ready candidate with a matching profile is available for this family.

: null} {candidate ?
:

Select a capability and exact candidate to inspect its binding.

}
; } function Fact({ name, value }: { name: string; value: string | number }) { return
{name}
{value}
; } function LabRequestForm({ capability, target, probeId, assessmentId, generation, canCheck }: { capability: LabCapability | ""; target: LabTarget | null; probeId?: string; assessmentId?: string; generation: number; canCheck: boolean }) { const [text, setText] = useState(""); const [documents, setDocuments] = useState(""); const [visionMode, setVisionMode] = useState<"text" | "image">("text"); const [file, setFile] = useState<{ base64: string; mediaType: string; name: string } | null>(null); const [reading, setReading] = useState(false); const [working, setWorking] = useState(false); const [error, setError] = useState(null); const [result, setResult] = useState(null); const mounted = useRef(false); const sequence = useRef(0); const fileSequence = useRef(0); const lock = useRef(false); const reader = useRef(null); const current = () => mounted.current && generation === operatorSessionVersion() && Boolean(operatorCredentialReference()); useEffect(() => { mounted.current = true; return () => { mounted.current = false; sequence.current += 1; fileSequence.current += 1; reader.current?.abort(); }; }, []); function changed() { sequence.current += 1; setResult(null); setError(null); } function chooseFile(event: ChangeEvent) { changed(); fileSequence.current += 1; reader.current?.abort(); setFile(null); setReading(false); const chosen = event.target.files?.[0]; if (!chosen) return; const mediaType = chosen.type === "audio/x-wav" ? "audio/wav" : chosen.type; const allowed = capability === "speech.transcription" ? mediaType === "audio/wav" : mediaType === "image/png" || mediaType === "image/jpeg"; if (!allowed || chosen.size === 0 || chosen.size > MAX_FILE_BYTES) { setError("Choose a non-empty PNG/JPEG image or WAV audio sample for this family, at most 360 KiB."); event.target.value = ""; return; } const token = fileSequence.current; const next = new FileReader(); reader.current = next; setReading(true); next.onload = () => { if (!current() || token !== fileSequence.current) return; setReading(false); const data = typeof next.result === "string" ? next.result.split(",")[1] : undefined; if (!data) { setError("The sample could not be read."); return; } setFile({ base64: data, mediaType, name: chosen.name }); }; next.onerror = () => { if (current() && token === fileSequence.current) { setReading(false); setError("The sample could not be read."); } }; next.readAsDataURL(chosen); } function input(): LabInput | null { if (capability === "rag.embedding" && text.trim()) return { capability, payload: { input: text, input_type: "raw" } }; if (capability === "rag.reranking" && text.trim()) { const items = documents.split(/\r?\n/).filter(value => value.trim()).map((value, index) => ({ id: `document-${index + 1}`, text: value })); if (items.length && items.length <= 40) return { capability, payload: { query: text, documents: items, top_n: Math.min(10, items.length) } }; } if (capability === "vision.embedding" && visionMode === "text" && text.trim()) return { capability, payload: { items: [{ text }] } }; if (capability === "vision.embedding" && visionMode === "image" && file && (file.mediaType === "image/png" || file.mediaType === "image/jpeg")) return { capability, payload: { items: [{ image_base64: file.base64, media_type: file.mediaType }] } }; if (capability === "document.ocr" && file && (file.mediaType === "image/png" || file.mediaType === "image/jpeg")) return { capability, payload: { content_base64: file.base64, media_type: file.mediaType, language_hint: "auto" } }; if (capability === "speech.transcription" && file) return { capability, payload: { audio_base64: file.base64, media_type: "audio/wav", language: "auto" } }; return null; } async function submit(event: FormEvent) { event.preventDefault(); const selectedInput = input(); if (lock.current || !current() || !canCheck || !target || !selectedInput || reading) return; const body = { target, input: selectedInput, contract_version: 1 as const, limits }; if (new TextEncoder().encode(JSON.stringify(body)).length > MAX_REQUEST_BYTES) { setError("The complete request exceeds the 512 KiB preview limit. Use a smaller sample."); return; } lock.current = true; const token = ++sequence.current; setWorking(true); setResult(null); setError(null); try { const response = await api.labPreflight(body); if (!current() || token !== sequence.current) return; if ((response.checks_passed && (!response.binding || response.blockers.length > 0)) || response.execution_started !== false || response.permission_granted !== false || (response.binding && (response.binding.runtime_probe_id !== probeId || response.binding.compatibility_assessment_id !== assessmentId || response.binding.capability !== capability || response.binding.contract_version !== 1 || Object.entries(target).some(([key, value]) => response.binding?.[key as keyof LabTarget] !== value)))) { setError("The preflight response does not match this exact request. Refresh evidence and check again."); return; } setResult(response); } catch { if (current() && token === sequence.current) setError("The request could not be checked. Verify the input limits and session, then refresh evidence and retry."); } finally { lock.current = false; if (current()) setWorking(false); } } const needsFile = capability === "document.ocr" || capability === "speech.transcription" || (capability === "vision.embedding" && visionMode === "image"); return

Small input sample

Checks use a 60-second runtime limit, a 10-second queue limit, one concurrent run, a 64 KiB output limit and 300-second retention policy. These are proposed limits; nothing is scheduled or retained by this check.

void submit(event)}>
Preview input {capability === "vision.embedding" ? : null} {capability && !needsFile ?