From 981efe36f21e9581b43bc6776b5d89fa755d15e5 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Mon, 16 Nov 2026 15:10:09 +0800 Subject: [PATCH 1/6] perf: import deploy prefilling speed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Narrowed prefilling to only the prefill requests: const prefilling = loading || entry.isFetching || preview.fetching; • Unblocked routeOptions so it starts as soon as seedPlan (releasePlan) arrives instead of waiting for upgradeSeed. --- .../inferences/components/DeployForm.test.tsx | 8 +++++ .../inferences/components/DeployForm.tsx | 12 +++++-- .../inferences/components/DeploySheet.tsx | 36 ++++++++++++------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/web/src/modules/inferences/components/DeployForm.test.tsx b/web/src/modules/inferences/components/DeployForm.test.tsx index 4c513bc..809611f 100644 --- a/web/src/modules/inferences/components/DeployForm.test.tsx +++ b/web/src/modules/inferences/components/DeployForm.test.tsx @@ -484,4 +484,12 @@ describe("PD role scheduling", () => { ); expect([en, zh].some((l) => noKeyHtml.includes(l.deploy.fields.pdGpuMissingLabelKey.replace("{role}", "prefill")))).toBe(true); }); + + it("freezes the form fieldset while prefilling", () => { + const frozen = renderToStaticMarkup(); + expect(frozen).toMatch(/^]*disabled=""[^>]*inert=""[^>]*aria-busy="true"/); + expect(frozen).toContain("pointer-events-none"); + const active = renderToStaticMarkup(); + expect(active).toMatch(/^]*disabled)/); + }); }); diff --git a/web/src/modules/inferences/components/DeployForm.tsx b/web/src/modules/inferences/components/DeployForm.tsx index 320dad3..7d33c8a 100644 --- a/web/src/modules/inferences/components/DeployForm.tsx +++ b/web/src/modules/inferences/components/DeployForm.tsx @@ -55,6 +55,7 @@ export interface DeployFormProps { form: Form; onChange: (patch: Partial
) => void; upgrade?: boolean; + disabled?: boolean; submitted: boolean; target: ReactNode; cluster?: ClusterInfo; @@ -99,7 +100,7 @@ export interface DeployFormProps { taints?: ClusterTaint[]; } -export function DeployForm({ form, onChange, upgrade, submitted, target, cluster, defaults, gpuOptions, gpuListed, gpuResources, capacity, expanded, onExpand, idPrefix, routeOptions, catalogRoute, helmValues, slots, slotsError, resources, pd, nodes, gpuSpecs, gpuVendor, recovery, inputs, droppedInputs, remember, onRemember, presets, taints }: DeployFormProps) { +export function DeployForm({ form, onChange, upgrade, disabled, submitted, target, cluster, defaults, gpuOptions, gpuListed, gpuResources, capacity, expanded, onExpand, idPrefix, routeOptions, catalogRoute, helmValues, slots, slotsError, resources, pd, nodes, gpuSpecs, gpuVendor, recovery, inputs, droppedInputs, remember, onRemember, presets, taints }: DeployFormProps) { const t = useT(); const checked = useDebounced(form.inputs, CHECK_MS); const on = effective(form); @@ -129,7 +130,12 @@ export function DeployForm({ form, onChange, upgrade, submitted, target, cluster ); return ( -
+
{section( "basic", <> @@ -332,7 +338,7 @@ export function DeployForm({ form, onChange, upgrade, submitted, target, cluster onChange({ edits })} source={helmValues} /> , )} -
+ ); } diff --git a/web/src/modules/inferences/components/DeploySheet.tsx b/web/src/modules/inferences/components/DeploySheet.tsx index 4d61844..da09896 100644 --- a/web/src/modules/inferences/components/DeploySheet.tsx +++ b/web/src/modules/inferences/components/DeploySheet.tsx @@ -1,6 +1,6 @@ import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router"; -import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; +import { keepPreviousData, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { Alert, AlertDescription, @@ -101,7 +101,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( const catalogName = moveTo || ownCatalog; const indexes = useQueries({ - queries: catalogs.map((c) => ({ queryKey: ["catalog", c.name], queryFn: () => api.catalog(c.name), enabled: upgrade || c.name === ownCatalog })), + queries: catalogs.map((c) => ({ queryKey: ["catalog", c.name], queryFn: () => api.catalog(c.name), enabled: upgrade || c.name === ownCatalog, retry: false })), }); const modelIn = (name?: string): CatalogModel | undefined => indexes[catalogs.findIndex((c) => c.name === name)]?.data?.models.find((m) => m.name === modelName); @@ -124,6 +124,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( queryKey: ["model", catalogName, modelName, keptVariant, version], queryFn: () => api.model(modelName!, { catalog: catalogName, variant: keptVariant || undefined, version: version || undefined }), enabled: !!modelName && !!catalogName, + placeholderData: keepPreviousData, }); const v = entry.data?.variant; const chartRange = !!v && isChartRange(v.chart.version); @@ -169,11 +170,15 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( setUpgradeSeed(seeded); setForm(seeded); }, [upgrade, seedPlan, v, profile.isPending, gpuSpecs, upgradeSeed]); - const catalogRoute = upgrade && upgradeSeed ? upgradeSeed.route || upgradeSeed.serviceId || target.release : undefined; + const catalogRoute = useMemo(() => { + if (!upgrade || !seedPlan) return undefined; + const base = upgradeSeed ?? formFromPlan(seedPlan); + return base.route || base.serviceId || target.release; + }, [upgrade, seedPlan, upgradeSeed, target]); const routeOptions = useQuery({ queryKey: ["route-options", catalogRoute ?? ""], queryFn: ({ signal }) => readRouteOptions(catalogRoute, signal), - enabled: !upgrade || !!upgradeSeed, + enabled: !upgrade || !!seedPlan, staleTime: 30_000, retry: false, }); @@ -194,7 +199,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( // The form shows only once the entry and profile have loaded, so the first // seed lands before any plan; a later one is another variant or version, and // a plan composed from the old switches goes with it. - const seedReady = !upgrade && !!entry.data && !profile.isPending; + const seedReady = !upgrade && !!entry.data && !entry.isPlaceholderData && !profile.isPending; const siteDefaults = useMemo(() => siteSwitchDefaults(profile.data), [profile.data]); const variantValues = entry.data?.variant.values; useEffect(() => { @@ -232,14 +237,15 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( profile.data?.profile, ); // An upgrade waits for the profile too: it decides Monitor (planRequest). - const loading = cluster.isPending || (upgrade && current.isPending) || profile.isPending || entry.isPending; + const baseLoading = cluster.isPending || (upgrade && current.isPending) || profile.isPending || entry.isPending; const error = cluster.error ?? current.error ?? entry.error; const nextSeed = useMemo(() => (seedReady ? switchDefaults(siteDefaults, variantValues) : null), [seedReady, siteDefaults, variantValues]); const formSeeded = upgrade ? !!upgradeSeed : !!seed && !!nextSeed && JSON.stringify(seed) === JSON.stringify(nextSeed); + const loading = baseLoading || (!error && !formSeeded); // Chart discovery (images and per-role chart defaults) runs once on open after // the form is seeded, on target changes, and on manual refresh. // Override YAML runs its own standalone dry-run when opened so form edits stay quiet. - const canDiscover = !loading && !error && formSeeded && (!upgrade || !!recorded) && cluster.data?.allowDeploy !== false; + const canDiscover = !loading && !error && !entry.isPlaceholderData && (!upgrade || !!recorded) && cluster.data?.allowDeploy !== false; const compositionError = (error: unknown) => { if (error instanceof RecoveryPlanChangedError) return new Error(t("deploy.recovery.errors.changedPlan")); if (error instanceof AdaptivePlanChangedError) return new Error(t("deploy.fields.adaptiveChanged")); @@ -251,6 +257,9 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( canDiscover, gpuSpecs, ); + // Freeze the form and actions while prefill (initial seed, target variant + // fetch, or chart dry-run discovery for resources/PD/images) is in flight. + const prefilling = loading || entry.isFetching || preview.fetching; const valuesKey = JSON.stringify( canDiscover ? withAdaptiveConcurrency(withRecovery(requestFor({ ...form, edits: "" }), recovery), form, routeOptions.data) @@ -495,11 +504,11 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( variant="outline" size="sm" className="shrink-0" - disabled={preview.fetching} + disabled={prefilling} onClick={() => void preview.reload()} title={t("deploy.refreshPreviewHint")} > - + {t("common:actions.refresh")} )} @@ -509,14 +518,14 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( {step === "form" && !loading && !error && (
{SECTIONS.map((s) => ( - ))}
)} - + {readOnly && ( @@ -542,6 +551,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( form={form} onChange={change} upgrade={upgrade} + disabled={prefilling} submitted={submitted} recovery={ setRecoveryPatch((previous) => ({ ...previous, ...patch }))} />} target={targetFields} @@ -603,8 +613,8 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( {step === "form" ? ( <> - change({ edits })} source={readOnly ? undefined : valuesSource} disabled={!!readOnly || loading} /> - From e9f8564e6f7cf6d8476db018b184efd6b11595c3 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Mon, 16 Nov 2026 15:45:07 +0800 Subject: [PATCH 2/6] fix(override yaml): duplicated and redundant api calls override yaml should only can /api/v2/compose?dryRun=true once --- .../modules/inferences/InferenceDetail.tsx | 8 ++-- web/src/modules/inferences/InferenceList.tsx | 2 +- .../inferences/components/DeploySheet.tsx | 48 ++++++++----------- .../inferences/components/OverrideYaml.tsx | 6 ++- .../inferences/components/RunsTable.tsx | 4 +- web/src/modules/inferences/tabs/Events.tsx | 8 ++-- web/src/modules/inferences/tabs/Logs.tsx | 6 +-- web/src/modules/inferences/tabs/Overview.tsx | 6 ++- web/src/modules/inferences/tabs/Runs.tsx | 4 +- .../swiss/components/ReleaseObjects.tsx | 4 +- 10 files changed, 48 insertions(+), 48 deletions(-) diff --git a/web/src/modules/inferences/InferenceDetail.tsx b/web/src/modules/inferences/InferenceDetail.tsx index acdeba0..602d855 100644 --- a/web/src/modules/inferences/InferenceDetail.tsx +++ b/web/src/modules/inferences/InferenceDetail.tsx @@ -161,16 +161,16 @@ export function InferenceDetail() { ({ value: key, label: t(`tabs.${key}`), count: counts[key] }))} /> - setTab("runs")} onAllEvents={() => setTab("events")} onViewInstances={() => setTab("instances")} /> + setTab("runs")} onAllEvents={() => setTab("events")} onViewInstances={() => setTab("instances")} /> setTab("logs", pod)} /> - + - + @@ -182,7 +182,7 @@ export function InferenceDetail() { - + {plan.data && ( diff --git a/web/src/modules/inferences/InferenceList.tsx b/web/src/modules/inferences/InferenceList.tsx index 4e230e9..3f8cf93 100644 --- a/web/src/modules/inferences/InferenceList.tsx +++ b/web/src/modules/inferences/InferenceList.tsx @@ -41,7 +41,7 @@ export function InferenceList() { const list = useQuery({ queryKey: ["deployments", "inferences", page, pageSize], queryFn: () => api.deployments(page, pageSize), - refetchInterval: 15_000, + refetchInterval: upgrading ? false : 15_000, placeholderData: (prev) => prev, }); const cluster = useQuery({ queryKey: ["cluster"], queryFn: api.cluster }); diff --git a/web/src/modules/inferences/components/DeploySheet.tsx b/web/src/modules/inferences/components/DeploySheet.tsx index da09896..43ae3b9 100644 --- a/web/src/modules/inferences/components/DeploySheet.tsx +++ b/web/src/modules/inferences/components/DeploySheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import { useNavigate } from "react-router"; import { keepPreviousData, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { @@ -26,7 +26,7 @@ import { } from "@modelsphere/ui"; import { ArrowRight, FlaskConical, RefreshCw, TriangleAlert } from "lucide-react"; import { useModulePath } from "@/shell"; -import { api, deployApi, type ApplyResult, type ApplyWith, type CatalogModel, type DiffResult, type Plan } from "@swiss/lib/api"; +import { api, deployApi, type ApplyResult, type ApplyWith, type CatalogModel, type DiffResult, type HelmValues, type Plan } from "@swiss/lib/api"; import { applyFlags, imageOf, reseed, siteSwitchDefaults, switchDefaults, type Form, type SwitchDefaults } from "@swiss/components/DeploySettings"; import { EMPTY, formFromPlan, planRequest } from "../deployment-settings"; import { useImageSlots } from "@swiss/lib/useImageSlots"; @@ -49,7 +49,7 @@ import { DeployForm } from "@/modules/inferences/components/DeployForm"; import { OverrideYamlButton, type HelmValuesSource } from "@/modules/inferences/components/OverrideYaml"; import { ExperimentalBadge } from "@/modules/inferences/components/InstallMethodLabel"; import { AdaptivePlanChangedError, composeAdaptivePlan, withAdaptiveConcurrency } from "../adaptive-concurrency"; -import { composeRecoveryPlan, composeRecoveryValuesRequest, RecoveryPlanChangedError } from "../recovery-compose"; +import { composeRecoveryPlan, RecoveryPlanChangedError } from "../recovery-compose"; import { RecoveryFields } from "./RecoveryFields"; import { mergeValues, planValues, recoveryError, recoveryInputError, recoverySettings, withRecovery } from "../recovery"; import { readRouteOptions } from "@/modules/inferences/api"; @@ -260,38 +260,30 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( // Freeze the form and actions while prefill (initial seed, target variant // fetch, or chart dry-run discovery for resources/PD/images) is in flight. const prefilling = loading || entry.isFetching || preview.fetching; - const valuesKey = JSON.stringify( - canDiscover - ? withAdaptiveConcurrency(withRecovery(requestFor({ ...form, edits: "" }), recovery), form, routeOptions.data) - : null, - ); + const valuesRequest = canDiscover + ? withAdaptiveConcurrency(withRecovery(requestFor({ ...form, edits: "" }), recovery), form, routeOptions.data) + : undefined; + const valuesKey = JSON.stringify(valuesRequest ?? null); const valuesM = useMutation({ - mutationFn: async (_key: string) => { - try { - const next = await composeRecoveryValuesRequest( - requestFor({ ...form, edits: "" }), - recovery, - (r) => composeAdaptivePlan(r, form, routeOptions.data, deployApi.plan, catalogName), - catalogName, - ); - return await deployApi.helmValues(next); - } catch (error) { - throw compositionError(error); - } - }, + mutationFn: async (_key: string) => deployApi.helmValues(valuesRequest!), }); + const valuesInflight = useRef<{ key: string; promise: Promise } | null>(null); const valuesFresh = valuesM.variables === valuesKey; const valuesSource: HelmValuesSource = { data: valuesFresh && !valuesM.isError ? valuesM.data : undefined, loading: valuesM.isPending, error: valuesFresh && valuesM.error ? (valuesM.error instanceof Error ? valuesM.error : new Error(String(valuesM.error))) : null, - load: async () => { - if (!canDiscover) return undefined; - try { - return await valuesM.mutateAsync(valuesKey); - } catch { - return undefined; - } + load: () => { + if (!canDiscover) return Promise.resolve(undefined); + if (valuesInflight.current?.key === valuesKey) return valuesInflight.current.promise; + const promise = valuesM + .mutateAsync(valuesKey) + .catch(() => undefined) + .finally(() => { + if (valuesInflight.current?.promise === promise) valuesInflight.current = null; + }); + valuesInflight.current = { key: valuesKey, promise }; + return promise; }, }; const compose = useMutation({ diff --git a/web/src/modules/inferences/components/OverrideYaml.tsx b/web/src/modules/inferences/components/OverrideYaml.tsx index f55a428..04c93c3 100644 --- a/web/src/modules/inferences/components/OverrideYaml.tsx +++ b/web/src/modules/inferences/components/OverrideYaml.tsx @@ -139,6 +139,7 @@ function OverrideDialog({ value, source, onClose, onSave }: { // Values read earlier for this form open the tabs straight away; otherwise load them on open. const [loaded, setLoaded] = useState(() => (source?.data ? loadedFor(source.data, value) : undefined)); const [final, setFinal] = useState(() => loaded?.computed ?? ""); + const loadingRef = useRef(false); useEffect(() => { if (loaded || !source) return; if (source.data) { @@ -148,12 +149,15 @@ function OverrideDialog({ value, source, onClose, onSave }: { setFinal(l.computed); return; } - if (!source.loading && !source.error) { + if (!source.loading && !source.error && !loadingRef.current) { + loadingRef.current = true; void source.load().then((values) => { const l = values && loadedFor(values, draft); if (!l) return; setLoaded(l); setFinal(l.computed); + }).finally(() => { + loadingRef.current = false; }); } }, [source?.data]); diff --git a/web/src/modules/inferences/components/RunsTable.tsx b/web/src/modules/inferences/components/RunsTable.tsx index 4150d2b..46802f8 100644 --- a/web/src/modules/inferences/components/RunsTable.tsx +++ b/web/src/modules/inferences/components/RunsTable.tsx @@ -24,6 +24,7 @@ export function RunsTable({ filters, activeFilters, onRemoveFilter, + poll = true, }: { namespace?: string; release?: string; @@ -35,13 +36,14 @@ export function RunsTable({ filters?: ReactNode; activeFilters?: { key: string; label: string; display: string }[]; onRemoveFilter?: (key: string) => void; + poll?: boolean; }) { const t = useT(); const p = useModulePath(); const runs = useQuery({ queryKey: ["runs", namespace ?? "", release ?? "", action ?? ""], queryFn: () => api.runs({ namespace, release, action, limit: 200 }), - refetchInterval: 20_000, + refetchInterval: poll ? 20_000 : false, }); const columns: ResourceColumn[] = [ diff --git a/web/src/modules/inferences/tabs/Events.tsx b/web/src/modules/inferences/tabs/Events.tsx index 4413be6..30249d1 100644 --- a/web/src/modules/inferences/tabs/Events.tsx +++ b/web/src/modules/inferences/tabs/Events.tsx @@ -9,19 +9,19 @@ import { workloadApi, type K8sEvent } from "@/modules/inferences/workload-api"; const ALL = "__all"; -export function useReleaseEvents(namespace: string, release: string, pods: readonly string[]) { +export function useReleaseEvents(namespace: string, release: string, pods: readonly string[], poll = true) { const q = useQuery({ queryKey: ["k8s-events", namespace, release], queryFn: () => workloadApi.events(namespace, release), - refetchInterval: 15_000, + refetchInterval: poll ? 15_000 : false, retry: false, }); return { ...q, events: (q.data?.items ?? []).filter((e) => ofRelease(e.name, release, pods)) }; } -export function Events({ namespace, release, pods }: { namespace: string; release: string; pods: readonly string[] }) { +export function Events({ namespace, release, pods, poll = true }: { namespace: string; release: string; pods: readonly string[]; poll?: boolean }) { const t = useT(); - const q = useReleaseEvents(namespace, release, pods); + const q = useReleaseEvents(namespace, release, pods, poll); const [object, setObject] = useState(ALL); const [onlyWarnings, setOnlyWarnings] = useState(false); const objects = [...new Set(q.events.map((e) => `${e.kind}/${e.name}`))].sort(); diff --git a/web/src/modules/inferences/tabs/Logs.tsx b/web/src/modules/inferences/tabs/Logs.tsx index 84fd7c8..9771914 100644 --- a/web/src/modules/inferences/tabs/Logs.tsx +++ b/web/src/modules/inferences/tabs/Logs.tsx @@ -16,7 +16,7 @@ const SINCE = [ { value: 86400, key: "since24h" }, ]; -export function Logs({ namespace, release, statusPods, initialPod }: { namespace: string; release: string; statusPods: readonly string[]; initialPod?: string }) { +export function Logs({ namespace, release, statusPods, initialPod, poll = true }: { namespace: string; release: string; statusPods: readonly string[]; initialPod?: string; poll?: boolean }) { const t = useT(); const [picked, setPod] = useState(initialPod ?? ""); const [pickedContainer, setContainer] = useState(""); @@ -30,7 +30,7 @@ export function Logs({ namespace, release, statusPods, initialPod }: { namespace const list = useQuery({ queryKey: ["k8s-pods", namespace, release], queryFn: () => workloadApi.pods(namespace, release), - refetchInterval: 15_000, + refetchInterval: poll ? 15_000 : false, retry: false, }); const listed = (list.data?.items ?? []).filter((p) => ofRelease(p.name, release, statusPods)); @@ -43,7 +43,7 @@ export function Logs({ namespace, release, statusPods, initialPod }: { namespace queryKey: ["k8s-log", namespace, pod, container, lines, since, previous], queryFn: () => workloadApi.log(namespace, pod, { container, tailLines: lines, sinceSeconds: since, previous }), enabled: !!pod && !!container, - refetchInterval: follow ? 5_000 : false, + refetchInterval: poll && follow ? 5_000 : false, retry: false, }); diff --git a/web/src/modules/inferences/tabs/Overview.tsx b/web/src/modules/inferences/tabs/Overview.tsx index 1ec0af0..bb8730a 100644 --- a/web/src/modules/inferences/tabs/Overview.tsx +++ b/web/src/modules/inferences/tabs/Overview.tsx @@ -32,6 +32,7 @@ export function Overview({ plan, objects, objectsUnreadable, + poll = true, onAllRuns, onAllEvents, onViewInstances, @@ -42,6 +43,7 @@ export function Overview({ plan?: Plan; objects?: ObjectResult[]; objectsUnreadable: boolean; + poll?: boolean; onAllRuns: () => void; onAllEvents: () => void; onViewInstances: () => void; @@ -49,12 +51,12 @@ export function Overview({ const t = useT(); const p = s.planStatus; const source = plan?.source; - const events = useReleaseEvents(namespace, release, s.pods.map((p) => p.name)); + const events = useReleaseEvents(namespace, release, s.pods.map((p) => p.name), poll); const runs = useQuery({ queryKey: ["runs", namespace, release], queryFn: () => api.runs({ namespace, release, limit: 200 }), }); - const autoscale = useAutoscaling(namespace, release).data; + const autoscale = useAutoscaling(namespace, release, poll).data; if (!s.exists && !p && !isUninstalling(s)) { return

{t("detail.notFound")}

; diff --git a/web/src/modules/inferences/tabs/Runs.tsx b/web/src/modules/inferences/tabs/Runs.tsx index 26086e3..640eda3 100644 --- a/web/src/modules/inferences/tabs/Runs.tsx +++ b/web/src/modules/inferences/tabs/Runs.tsx @@ -1,7 +1,7 @@ import { RunsTable } from "@/modules/inferences/components/RunsTable"; import { useT } from "@/modules/inferences/i18n"; -export function Runs({ namespace, release }: { namespace: string; release: string }) { +export function Runs({ namespace, release, poll = true }: { namespace: string; release: string; poll?: boolean }) { const t = useT(); - return ; + return ; } diff --git a/web/src/modules/swiss/components/ReleaseObjects.tsx b/web/src/modules/swiss/components/ReleaseObjects.tsx index 763e7d5..6871fb7 100644 --- a/web/src/modules/swiss/components/ReleaseObjects.tsx +++ b/web/src/modules/swiss/components/ReleaseObjects.tsx @@ -37,12 +37,12 @@ export function useReleaseObjects(namespace: string, release: string, poll = tru // Polled only while the release is autoscaling: for any other the answer is a // plan read on every call. -export function useAutoscaling(namespace: string, release: string) { +export function useAutoscaling(namespace: string, release: string, poll = true) { return useQuery({ queryKey: ["autoscaling", namespace, release], queryFn: () => api.autoscaling(namespace, release), retry: false, - refetchInterval: (q) => (q.state.data?.enabled ? 15_000 : false), + refetchInterval: (q) => (poll && q.state.data?.enabled ? 15_000 : false), }); } From f121b89e4ff9c0039987c4338a37b05770c6df8e Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Mon, 16 Nov 2026 16:13:35 +0800 Subject: [PATCH 3/6] test: guard the override yaml behavior --- .../inferences/components/DeploySheet.tsx | 23 +++--------- .../components/OverrideYaml.test.tsx | 36 +++++++++++++++++-- .../inferences/components/OverrideYaml.tsx | 30 +++++++++++++++- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/web/src/modules/inferences/components/DeploySheet.tsx b/web/src/modules/inferences/components/DeploySheet.tsx index 43ae3b9..b3f175c 100644 --- a/web/src/modules/inferences/components/DeploySheet.tsx +++ b/web/src/modules/inferences/components/DeploySheet.tsx @@ -26,7 +26,7 @@ import { } from "@modelsphere/ui"; import { ArrowRight, FlaskConical, RefreshCw, TriangleAlert } from "lucide-react"; import { useModulePath } from "@/shell"; -import { api, deployApi, type ApplyResult, type ApplyWith, type CatalogModel, type DiffResult, type HelmValues, type Plan } from "@swiss/lib/api"; +import { api, deployApi, type ApplyResult, type ApplyWith, type CatalogModel, type DiffResult, type Plan } from "@swiss/lib/api"; import { applyFlags, imageOf, reseed, siteSwitchDefaults, switchDefaults, type Form, type SwitchDefaults } from "@swiss/components/DeploySettings"; import { EMPTY, formFromPlan, planRequest } from "../deployment-settings"; import { useImageSlots } from "@swiss/lib/useImageSlots"; @@ -46,7 +46,7 @@ import { useT } from "@/modules/inferences/i18n"; import { detailPath, sloEnabled } from "@/modules/inferences/lib"; import { SECTIONS, chartChoice, clusterTaints, defaultServiceId, pipelineState, serviceIdError, whatMoves, type Section } from "@/modules/inferences/deploy-lib"; import { DeployForm } from "@/modules/inferences/components/DeployForm"; -import { OverrideYamlButton, type HelmValuesSource } from "@/modules/inferences/components/OverrideYaml"; +import { OverrideYamlButton, helmValuesMutationOptions, loadHelmValues, type HelmValuesInflight, type HelmValuesSource } from "@/modules/inferences/components/OverrideYaml"; import { ExperimentalBadge } from "@/modules/inferences/components/InstallMethodLabel"; import { AdaptivePlanChangedError, composeAdaptivePlan, withAdaptiveConcurrency } from "../adaptive-concurrency"; import { composeRecoveryPlan, RecoveryPlanChangedError } from "../recovery-compose"; @@ -264,27 +264,14 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( ? withAdaptiveConcurrency(withRecovery(requestFor({ ...form, edits: "" }), recovery), form, routeOptions.data) : undefined; const valuesKey = JSON.stringify(valuesRequest ?? null); - const valuesM = useMutation({ - mutationFn: async (_key: string) => deployApi.helmValues(valuesRequest!), - }); - const valuesInflight = useRef<{ key: string; promise: Promise } | null>(null); + const valuesM = useMutation(helmValuesMutationOptions(() => valuesRequest)); + const valuesInflight = useRef(null); const valuesFresh = valuesM.variables === valuesKey; const valuesSource: HelmValuesSource = { data: valuesFresh && !valuesM.isError ? valuesM.data : undefined, loading: valuesM.isPending, error: valuesFresh && valuesM.error ? (valuesM.error instanceof Error ? valuesM.error : new Error(String(valuesM.error))) : null, - load: () => { - if (!canDiscover) return Promise.resolve(undefined); - if (valuesInflight.current?.key === valuesKey) return valuesInflight.current.promise; - const promise = valuesM - .mutateAsync(valuesKey) - .catch(() => undefined) - .finally(() => { - if (valuesInflight.current?.promise === promise) valuesInflight.current = null; - }); - valuesInflight.current = { key: valuesKey, promise }; - return promise; - }, + load: () => loadHelmValues(valuesRequest, valuesKey, valuesM.mutateAsync, valuesInflight), }; const compose = useMutation({ mutationFn: async () => { diff --git a/web/src/modules/inferences/components/OverrideYaml.test.tsx b/web/src/modules/inferences/components/OverrideYaml.test.tsx index f9418d8..6460caf 100644 --- a/web/src/modules/inferences/components/OverrideYaml.test.tsx +++ b/web/src/modules/inferences/components/OverrideYaml.test.tsx @@ -1,9 +1,15 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { renderToStaticMarkup } from "react-dom/server"; -import { OverrideYaml } from "./OverrideYaml"; +import { MutationObserver, QueryClient } from "@tanstack/react-query"; +import { deployApi, type PlanRequest } from "@swiss/lib/api"; +import { OverrideYaml, helmValuesMutationOptions, loadHelmValues, type HelmValuesInflight } from "./OverrideYaml"; import en from "@/modules/inferences/locales/en-US.json"; import zh from "@/modules/inferences/locales/zh-CN.json"; +afterEach(() => { + vi.restoreAllMocks(); +}); + const render = (value: string) => renderToStaticMarkup( {}} />); describe("Override YAML in the form", () => { @@ -38,3 +44,29 @@ describe("key search", () => { expect(html).not.toContain('role="listbox"'); }); }); + +describe("Override YAML dry-run loader", () => { + it("calls /api/v2/compose dry-run once on open (even under concurrent StrictMode loads) and never calls /api/plans", async () => { + const helm = { defaults: "replicaCount: 1\n", overrides: "{}\n" }; + const helmSpy = vi.spyOn(deployApi, "helmValues").mockResolvedValue(helm); + const planSpy = vi.spyOn(deployApi, "plan"); + let req: PlanRequest | undefined = { model: "m", catalog: "c", overrides: { replicaCount: 2 } }; + const client = new QueryClient(); + const observer = new MutationObserver(client, helmValuesMutationOptions(() => req)); + const inflight: { current: HelmValuesInflight | null } = { current: null }; + const load = () => loadHelmValues(req, JSON.stringify(req ?? null), (k) => observer.mutate(k), inflight); + + const [first, second] = await Promise.all([load(), load()]); + expect(first).toEqual(helm); + expect(second).toEqual(helm); + expect(helmSpy).toHaveBeenCalledTimes(1); + expect(helmSpy).toHaveBeenCalledWith(req); + expect(planSpy).not.toHaveBeenCalled(); + + req = { model: "m", catalog: "c", overrides: { replicaCount: 4 } }; + expect(await load()).toEqual(helm); + expect(helmSpy).toHaveBeenCalledTimes(2); + expect(helmSpy).toHaveBeenLastCalledWith(req); + expect(planSpy).not.toHaveBeenCalled(); + }); +}); diff --git a/web/src/modules/inferences/components/OverrideYaml.tsx b/web/src/modules/inferences/components/OverrideYaml.tsx index 04c93c3..0b27501 100644 --- a/web/src/modules/inferences/components/OverrideYaml.tsx +++ b/web/src/modules/inferences/components/OverrideYaml.tsx @@ -20,7 +20,7 @@ import { TabsTrigger, } from "@modelsphere/ui"; import { Lock, Pencil, TriangleAlert } from "lucide-react"; -import type { HelmValues } from "@swiss/lib/api"; +import { deployApi, type HelmValues, type PlanRequest } from "@swiss/lib/api"; import { useT } from "@/modules/inferences/i18n"; import { KeySearch } from "./KeySearch"; import { YamlTextarea, type YamlTextareaHandle } from "./YamlTextarea"; @@ -28,6 +28,34 @@ import { computedYaml, keyLines, mergeValues, overrideEdit, overridePaths, parse const message = (error: unknown) => (error instanceof Error ? error.message : String(error)); +export interface HelmValuesInflight { + key: string; + promise: Promise; +} + +export function helmValuesMutationOptions(getRequest: () => PlanRequest | undefined) { + return { + mutationFn: async (_key: string): Promise => deployApi.helmValues(getRequest()!), + }; +} + +export function loadHelmValues( + request: PlanRequest | undefined, + key: string, + mutate: (key: string) => Promise, + inflight: { current: HelmValuesInflight | null }, +): Promise { + if (!request) return Promise.resolve(undefined); + if (inflight.current?.key === key) return inflight.current.promise; + const promise = mutate(key) + .catch(() => undefined) + .finally(() => { + if (inflight.current?.promise === promise) inflight.current = null; + }); + inflight.current = { key, promise }; + return promise; +} + // The Advanced section's Override YAML: a summary in the form, its editor in a dialog. // The chart's values and swissd's layers for the form as it stands, without // the overrides. The deploy sheet caches them per request; load reads them From 24055db17a595ecf27312ac6e9a423316667ac69 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Mon, 16 Nov 2026 16:38:07 +0800 Subject: [PATCH 4/6] logic simply --- .../inferences/components/DeployForm.tsx | 5 +- .../inferences/components/DeploySheet.tsx | 25 +++--- .../components/OverrideYaml.test.tsx | 65 +++++++++++---- .../inferences/components/OverrideYaml.tsx | 80 +++++++------------ 4 files changed, 95 insertions(+), 80 deletions(-) diff --git a/web/src/modules/inferences/components/DeployForm.tsx b/web/src/modules/inferences/components/DeployForm.tsx index 7d33c8a..07243a2 100644 --- a/web/src/modules/inferences/components/DeployForm.tsx +++ b/web/src/modules/inferences/components/DeployForm.tsx @@ -75,6 +75,7 @@ export interface DeployFormProps { catalogRoute?: string; // The chart's values and swissd's layers for the Override YAML editor. helmValues?: HelmValuesSource; + onHelmValuesOpenChange?: (open: boolean) => void; // Every image the chart renders, from the compose dry run. Absent until that // returns; the engine row still shows from defaults.image. slots?: ImageSlot[]; @@ -100,7 +101,7 @@ export interface DeployFormProps { taints?: ClusterTaint[]; } -export function DeployForm({ form, onChange, upgrade, disabled, submitted, target, cluster, defaults, gpuOptions, gpuListed, gpuResources, capacity, expanded, onExpand, idPrefix, routeOptions, catalogRoute, helmValues, slots, slotsError, resources, pd, nodes, gpuSpecs, gpuVendor, recovery, inputs, droppedInputs, remember, onRemember, presets, taints }: DeployFormProps) { +export function DeployForm({ form, onChange, upgrade, disabled, submitted, target, cluster, defaults, gpuOptions, gpuListed, gpuResources, capacity, expanded, onExpand, idPrefix, routeOptions, catalogRoute, helmValues, onHelmValuesOpenChange, slots, slotsError, resources, pd, nodes, gpuSpecs, gpuVendor, recovery, inputs, droppedInputs, remember, onRemember, presets, taints }: DeployFormProps) { const t = useT(); const checked = useDebounced(form.inputs, CHECK_MS); const on = effective(form); @@ -335,7 +336,7 @@ export function DeployForm({ form, onChange, upgrade, disabled, submitted, targe {sw("skipSchemaValidation", f("skipSchemaValidation"))} - onChange({ edits })} source={helmValues} /> + onChange({ edits })} source={helmValues} onOpenChange={onHelmValuesOpenChange} /> , )} diff --git a/web/src/modules/inferences/components/DeploySheet.tsx b/web/src/modules/inferences/components/DeploySheet.tsx index b3f175c..9c3e752 100644 --- a/web/src/modules/inferences/components/DeploySheet.tsx +++ b/web/src/modules/inferences/components/DeploySheet.tsx @@ -1,4 +1,4 @@ -import { useEffect, useMemo, useRef, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router"; import { keepPreviousData, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { @@ -46,7 +46,7 @@ import { useT } from "@/modules/inferences/i18n"; import { detailPath, sloEnabled } from "@/modules/inferences/lib"; import { SECTIONS, chartChoice, clusterTaints, defaultServiceId, pipelineState, serviceIdError, whatMoves, type Section } from "@/modules/inferences/deploy-lib"; import { DeployForm } from "@/modules/inferences/components/DeployForm"; -import { OverrideYamlButton, helmValuesMutationOptions, loadHelmValues, type HelmValuesInflight, type HelmValuesSource } from "@/modules/inferences/components/OverrideYaml"; +import { OverrideYamlButton, helmValuesQueryOptions, type HelmValuesSource } from "@/modules/inferences/components/OverrideYaml"; import { ExperimentalBadge } from "@/modules/inferences/components/InstallMethodLabel"; import { AdaptivePlanChangedError, composeAdaptivePlan, withAdaptiveConcurrency } from "../adaptive-concurrency"; import { composeRecoveryPlan, RecoveryPlanChangedError } from "../recovery-compose"; @@ -187,6 +187,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( const dirty = useDirty({ form, version, variant, moveTo, chartVersion, recovery }, { form: initial, version: upgrade ? "" : (target.version ?? ""), variant: upgrade ? "" : (target.variant ?? ""), moveTo: "", chartVersion: "", recovery: initialRecovery }); const [step, setStep] = useState<"form" | "review">("form"); + const [yamlOpen, setYamlOpen] = useState(false); const [submitted, setSubmitted] = useState(false); const [expanded, setExpanded] = useState(OPEN); const [plan, setPlan] = useState(null); @@ -263,15 +264,16 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( const valuesRequest = canDiscover ? withAdaptiveConcurrency(withRecovery(requestFor({ ...form, edits: "" }), recovery), form, routeOptions.data) : undefined; - const valuesKey = JSON.stringify(valuesRequest ?? null); - const valuesM = useMutation(helmValuesMutationOptions(() => valuesRequest)); - const valuesInflight = useRef(null); - const valuesFresh = valuesM.variables === valuesKey; + const helmValues = useQuery(helmValuesQueryOptions(valuesRequest, yamlOpen && step === "form" && canDiscover)); const valuesSource: HelmValuesSource = { - data: valuesFresh && !valuesM.isError ? valuesM.data : undefined, - loading: valuesM.isPending, - error: valuesFresh && valuesM.error ? (valuesM.error instanceof Error ? valuesM.error : new Error(String(valuesM.error))) : null, - load: () => loadHelmValues(valuesRequest, valuesKey, valuesM.mutateAsync, valuesInflight), + data: helmValues.data, + loading: helmValues.isFetching, + error: helmValues.error instanceof Error ? helmValues.error : helmValues.error ? new Error(String(helmValues.error)) : null, + load: async () => { + if (!valuesRequest) return undefined; + const res = await helmValues.refetch({ cancelRefetch: false }); + return res.error ? undefined : res.data; + }, }; const compose = useMutation({ mutationFn: async () => { @@ -557,6 +559,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( catalogRoute={catalogRoute} idPrefix={ID_PREFIX} helmValues={readOnly ? undefined : valuesSource} + onHelmValuesOpenChange={setYamlOpen} inputs={inputs} droppedInputs={droppedInputs} remember={canRemember ? remember : undefined} @@ -592,7 +595,7 @@ function DeploySheetBody({ target, onClose }: { target: DeployTarget; onClose: ( {step === "form" ? ( <> - change({ edits })} source={readOnly ? undefined : valuesSource} disabled={!!readOnly || prefilling} /> + change({ edits })} source={readOnly ? undefined : valuesSource} disabled={!!readOnly || prefilling} onOpenChange={setYamlOpen} /> diff --git a/web/src/modules/inferences/components/OverrideYaml.test.tsx b/web/src/modules/inferences/components/OverrideYaml.test.tsx index 6460caf..87a9e00 100644 --- a/web/src/modules/inferences/components/OverrideYaml.test.tsx +++ b/web/src/modules/inferences/components/OverrideYaml.test.tsx @@ -1,13 +1,14 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { renderToStaticMarkup } from "react-dom/server"; -import { MutationObserver, QueryClient } from "@tanstack/react-query"; +import { QueryClient, QueryObserver, environmentManager } from "@tanstack/react-query"; import { deployApi, type PlanRequest } from "@swiss/lib/api"; -import { OverrideYaml, helmValuesMutationOptions, loadHelmValues, type HelmValuesInflight } from "./OverrideYaml"; +import { OverrideYaml, helmValuesQueryOptions } from "./OverrideYaml"; import en from "@/modules/inferences/locales/en-US.json"; import zh from "@/modules/inferences/locales/zh-CN.json"; afterEach(() => { vi.restoreAllMocks(); + environmentManager.setIsServer(() => typeof window === "undefined"); }); const render = (value: string) => renderToStaticMarkup( {}} />); @@ -45,28 +46,60 @@ describe("key search", () => { }); }); -describe("Override YAML dry-run loader", () => { - it("calls /api/v2/compose dry-run once on open (even under concurrent StrictMode loads) and never calls /api/plans", async () => { +describe("Override YAML dry-run query", () => { + it("stays idle while closed, calls /api/v2/compose dry-run once across concurrent/reopen loads, and never calls /api/plans", async () => { + environmentManager.setIsServer(() => false); const helm = { defaults: "replicaCount: 1\n", overrides: "{}\n" }; const helmSpy = vi.spyOn(deployApi, "helmValues").mockResolvedValue(helm); const planSpy = vi.spyOn(deployApi, "plan"); - let req: PlanRequest | undefined = { model: "m", catalog: "c", overrides: { replicaCount: 2 } }; - const client = new QueryClient(); - const observer = new MutationObserver(client, helmValuesMutationOptions(() => req)); - const inflight: { current: HelmValuesInflight | null } = { current: null }; - const load = () => loadHelmValues(req, JSON.stringify(req ?? null), (k) => observer.mutate(k), inflight); + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const req1: PlanRequest = { model: "m", catalog: "c", overrides: { replicaCount: 2 } }; + const req2: PlanRequest = { model: "m", catalog: "c", overrides: { replicaCount: 4 } }; - const [first, second] = await Promise.all([load(), load()]); - expect(first).toEqual(helm); - expect(second).toEqual(helm); + const observerA = new QueryObserver(client, helmValuesQueryOptions(req1, false)); + const observerB = new QueryObserver(client, helmValuesQueryOptions(req1, false)); + const unsubA = observerA.subscribe(() => {}); + const unsubB = observerB.subscribe(() => {}); + + // Closed dialog + form edits while closed stay quiet. + await Promise.resolve(); + observerA.setOptions(helmValuesQueryOptions(req2, false)); + observerB.setOptions(helmValuesQueryOptions(req2, false)); + await Promise.resolve(); + expect(helmSpy).not.toHaveBeenCalled(); + + // Opening with two concurrent observers (e.g. StrictMode) triggers a single dry-run call. + observerA.setOptions(helmValuesQueryOptions(req1, true)); + observerB.setOptions(helmValuesQueryOptions(req1, true)); + await vi.waitFor(() => expect(observerA.getCurrentResult().data).toEqual(helm)); + expect(observerB.getCurrentResult().data).toEqual(helm); expect(helmSpy).toHaveBeenCalledTimes(1); - expect(helmSpy).toHaveBeenCalledWith(req); + expect(helmSpy).toHaveBeenCalledWith(req1); expect(planSpy).not.toHaveBeenCalled(); - req = { model: "m", catalog: "c", overrides: { replicaCount: 4 } }; - expect(await load()).toEqual(helm); + // Close and reopen without changing the request: uses cache, 0 extra calls. + observerA.setOptions(helmValuesQueryOptions(req1, false)); + await Promise.resolve(); + observerA.setOptions(helmValuesQueryOptions(req1, true)); + await Promise.resolve(); + expect(helmSpy).toHaveBeenCalledTimes(1); + + // Concurrent manual reloads dedupe to one new dry-run call; form change while open fetches once. + const [r1, r2] = await Promise.all([ + observerA.refetch({ cancelRefetch: false }), + observerB.refetch({ cancelRefetch: false }), + ]); + expect(r1.data).toEqual(helm); + expect(r2.data).toEqual(helm); expect(helmSpy).toHaveBeenCalledTimes(2); - expect(helmSpy).toHaveBeenLastCalledWith(req); + + observerA.setOptions(helmValuesQueryOptions(req2, true)); + observerB.setOptions(helmValuesQueryOptions(req2, true)); + await vi.waitFor(() => expect(helmSpy).toHaveBeenCalledTimes(3)); + expect(helmSpy).toHaveBeenLastCalledWith(req2); expect(planSpy).not.toHaveBeenCalled(); + + unsubA(); + unsubB(); }); }); diff --git a/web/src/modules/inferences/components/OverrideYaml.tsx b/web/src/modules/inferences/components/OverrideYaml.tsx index 0b27501..a19c76f 100644 --- a/web/src/modules/inferences/components/OverrideYaml.tsx +++ b/web/src/modules/inferences/components/OverrideYaml.tsx @@ -28,32 +28,26 @@ import { computedYaml, keyLines, mergeValues, overrideEdit, overridePaths, parse const message = (error: unknown) => (error instanceof Error ? error.message : String(error)); -export interface HelmValuesInflight { - key: string; - promise: Promise; -} - -export function helmValuesMutationOptions(getRequest: () => PlanRequest | undefined) { +export function helmValuesQueryOptions(request: PlanRequest | undefined, opened: boolean) { + const valuesKey = JSON.stringify(request ?? null); return { - mutationFn: async (_key: string): Promise => deployApi.helmValues(getRequest()!), + queryKey: ["helm-values", valuesKey] as const, + queryFn: (): Promise => deployApi.helmValues(request!), + enabled: opened && !!request, + retry: false as const, + staleTime: Infinity, + // Dropped when the sheet closes: they last one install or upgrade. + gcTime: 0, }; } -export function loadHelmValues( - request: PlanRequest | undefined, - key: string, - mutate: (key: string) => Promise, - inflight: { current: HelmValuesInflight | null }, -): Promise { - if (!request) return Promise.resolve(undefined); - if (inflight.current?.key === key) return inflight.current.promise; - const promise = mutate(key) - .catch(() => undefined) - .finally(() => { - if (inflight.current?.promise === promise) inflight.current = null; - }); - inflight.current = { key, promise }; - return promise; +function useDialogOpen(onOpenChange?: (open: boolean) => void) { + const [open, setOpen] = useState(false); + const set = (next: boolean) => { + setOpen(next); + onOpenChange?.(next); + }; + return [open, () => set(true), () => set(false)] as const; } // The Advanced section's Override YAML: a summary in the form, its editor in a dialog. @@ -67,17 +61,16 @@ export interface HelmValuesSource { load: () => Promise; } -export function OverrideYaml({ value, onChange, source }: { +export function OverrideYaml({ value, onChange, source, onOpenChange }: { value: string; onChange: (edits: string) => void; source?: HelmValuesSource; + onOpenChange?: (open: boolean) => void; }) { const t = useT(); const f = (k: string) => t(`deploy.fields.${k}`); - const [open, setOpen] = useState(false); + const [open, edit, close] = useDialogOpen(onOpenChange); const paths = overridePaths(value); - const close = () => setOpen(false); - const edit = () => setOpen(true); return ( <> void; source?: HelmValuesSource; disabled?: boolean; + onOpenChange?: (open: boolean) => void; }) { const t = useT(); - const [open, setOpen] = useState(false); + const [open, edit, close] = useDialogOpen(onOpenChange); const paths = overridePaths(value); - const close = () => setOpen(false); return ( <> -