From dc2f8571761e316b20aae5644df8a5c6b7aac6f7 Mon Sep 17 00:00:00 2001 From: huskcass <333684917+huskcass@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:30:00 -0800 Subject: [PATCH] swiss: read and write site profile inputs as a list of {key, value} swissd now serves the site profile's inputs as [{key, value}], the shape of the catalog's inputs and the inputs annotation. Lookups go by key, the profile pages list the entries, and remembering an answer for the site updates or appends an entry in the profile's inputs list. Co-authored-by: aceforeverd <11715200+aceforeverd@users.noreply.github.com> --- web/src/modules/inferences/SiteProfilePage.tsx | 10 +++++----- .../inferences/components/ProfileForm.test.tsx | 2 +- web/src/modules/inferences/profile-lib.test.ts | 2 +- .../swiss/components/DeploySettings.test.ts | 4 ++-- .../modules/swiss/components/DeploySettings.tsx | 4 ++-- .../modules/swiss/components/ProfileForm.test.tsx | 2 +- web/src/modules/swiss/lib/api.ts | 10 ++++++++-- web/src/modules/swiss/lib/inputs.test.ts | 2 +- web/src/modules/swiss/lib/inputs.ts | 12 ++++++------ web/src/modules/swiss/lib/siteAnswers.test.ts | 15 +++++++++++---- web/src/modules/swiss/lib/siteAnswers.ts | 14 ++++++++++---- web/src/modules/swiss/routes/SiteProfile.tsx | 4 ++-- 12 files changed, 50 insertions(+), 31 deletions(-) diff --git a/web/src/modules/inferences/SiteProfilePage.tsx b/web/src/modules/inferences/SiteProfilePage.tsx index 4a9f86e0..cd6601b5 100644 --- a/web/src/modules/inferences/SiteProfilePage.tsx +++ b/web/src/modules/inferences/SiteProfilePage.tsx @@ -166,7 +166,7 @@ function ProfileView({ p }: { p: SiteProfile }) { { label: f("overrides"), value: kv(p.model?.overrides) }, ])} - {p.inputs && Object.keys(p.inputs).length ? ( + {p.inputs?.length ? ( @@ -175,10 +175,10 @@ function ProfileView({ p }: { p: SiteProfile }) { - {Object.entries(p.inputs).map(([k, x]) => ( - - {k} - {answerText(x)} + {p.inputs.map((s) => ( + + {s.key} + {answerText(s.value)} ))} diff --git a/web/src/modules/inferences/components/ProfileForm.test.tsx b/web/src/modules/inferences/components/ProfileForm.test.tsx index d562189d..031fd7e3 100644 --- a/web/src/modules/inferences/components/ProfileForm.test.tsx +++ b/web/src/modules/inferences/components/ProfileForm.test.tsx @@ -7,7 +7,7 @@ describe("variant inputs", () => { it("lists the profile's answers as rows, a string that reads as a number quoted", () => { const html = renderToStaticMarkup( - {}} submitted={false} /> + {}} submitted={false} /> , ); for (const v of ["a.master", "mc:1", "a.port", "8000", "a.tag", ""8000""]) expect(html).toContain(`value="${v}"`); diff --git a/web/src/modules/inferences/profile-lib.test.ts b/web/src/modules/inferences/profile-lib.test.ts index 0b3a9f50..8145168a 100644 --- a/web/src/modules/inferences/profile-lib.test.ts +++ b/web/src/modules/inferences/profile-lib.test.ts @@ -55,7 +55,7 @@ describe("nodes", () => { describe("input answers", () => { it("maps a profile's answers to rows and back", () => { - const inputs = { "a.master": "mc:1", "a.port": 8000, "a.on": true }; + const inputs = [{ key: "a.master", value: "mc:1" }, { key: "a.port", value: 8000 }, { key: "v.[name=x].path", value: true }]; expect(parseAnswers(answersText(inputs))).toEqual(inputs); expect(parseAnswers(fromRows(toRows(answersText(inputs))))).toEqual(inputs); expect(parseAnswers(fromRows([]))).toBeUndefined(); diff --git a/web/src/modules/swiss/components/DeploySettings.test.ts b/web/src/modules/swiss/components/DeploySettings.test.ts index 13a02a8c..1d6ea797 100644 --- a/web/src/modules/swiss/components/DeploySettings.test.ts +++ b/web/src/modules/swiss/components/DeploySettings.test.ts @@ -124,7 +124,7 @@ describe("catalog input answers", () => { extraArgs: ["--foo=OLD", "--bar"], untouched: 1, }; - const opts = { model: "m", inputs: [master, arg], values, formLayer, inputsAnnotation: annotation, siteInputs: { [arg.key]: "site-{{model}}" }, hf: "" }; + const opts = { model: "m", inputs: [master, arg], values, formLayer, inputsAnnotation: annotation, siteInputs: [{ key: arg.key, value: "site-{{model}}" }], hf: "" }; const seeded = formFromPlan( { release: { name: "r", namespace: "n" }, @@ -176,7 +176,7 @@ describe("catalog input answers", () => { }); it("fills a list key from the site when nothing is typed", () => { - const req = planRequest(EMPTY, { model: "glm", inputs: [arg], values, siteInputs: { [arg.key]: "mc-{{model}}" }, hf: "org/name" }); + const req = planRequest(EMPTY, { model: "glm", inputs: [arg], values, siteInputs: [{ key: arg.key, value: "mc-{{model}}" }], hf: "org/name" }); expect(req.overrides?.extraArgs).toEqual(["mc-glm", "--bar"]); expect(req.additionalAnnotations).toEqual({ [INPUTS_ANNOTATION]: "[]" }); }); diff --git a/web/src/modules/swiss/components/DeploySettings.tsx b/web/src/modules/swiss/components/DeploySettings.tsx index 28100da2..a534632c 100644 --- a/web/src/modules/swiss/components/DeploySettings.tsx +++ b/web/src/modules/swiss/components/DeploySettings.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import { ChevronDown, Plus, RefreshCw, RotateCcw, TriangleAlert, X } from "lucide-react"; -import type { ApplyFlags, ClusterInfo, GPUSpec, Input as CatalogInput, Node, Plan, PlanRequest, Preset, ProfileResponse, Variant } from "@swiss/lib/api"; +import type { ApplyFlags, ClusterInfo, GPUSpec, Input as CatalogInput, Node, Plan, PlanRequest, Preset, ProfileResponse, SiteInput, Variant } from "@swiss/lib/api"; import { cn } from "@swiss/lib/utils"; import { cacheFieldError, type CacheCard, type CacheDefaults } from "@swiss/lib/cache"; import { pickableProducts, productChoices, unlistedProducts, type GPUCapacity } from "@swiss/lib/gpu"; @@ -2252,7 +2252,7 @@ export function planRequest( // deployed before answers were recorded. inputsAnnotation?: string; // Site profile answers. A list key writes one; a field key leaves it to swissd. - siteInputs?: Record; + siteInputs?: SiteInput[]; hf?: string; // The values of the preset f picks. preset?: Record; diff --git a/web/src/modules/swiss/components/ProfileForm.test.tsx b/web/src/modules/swiss/components/ProfileForm.test.tsx index 44c4da5b..f5151acb 100644 --- a/web/src/modules/swiss/components/ProfileForm.test.tsx +++ b/web/src/modules/swiss/components/ProfileForm.test.tsx @@ -7,7 +7,7 @@ describe("variant inputs", () => { it("lists the profile's answers as rows, a string that reads as a number quoted", () => { const html = renderToStaticMarkup( - {}} /> + {}} /> , ); expect(html).toContain("Variant inputs"); diff --git a/web/src/modules/swiss/lib/api.ts b/web/src/modules/swiss/lib/api.ts index 55427535..60beddd5 100644 --- a/web/src/modules/swiss/lib/api.ts +++ b/web/src/modules/swiss/lib/api.ts @@ -329,6 +329,12 @@ export interface Preset { values?: Record; } +// One site answer. A list entry, so a key like volumes.[name=x].path is a plain string. +export interface SiteInput { + key: string; + value: string | number | boolean; +} + export interface SiteProfile { name: string; namespace?: string; @@ -371,8 +377,8 @@ export interface SiteProfile { // The site's preferences, beneath the catalog: a variant overrides them. // features unset is on (featureOn). defaults?: { features?: { cart?: boolean; serviceMonitor?: boolean; sloRequirement?: boolean; scaler?: boolean } }; - // Answers a variant's inputs by chart path; strings take pathTemplate's placeholders. - inputs?: Record; + // Answers a variant's inputs by key, each key once; strings take pathTemplate's placeholders. + inputs?: SiteInput[]; // Named values a deploy may pick one of; the console merges it into the form layer. presets?: Record; nodes?: { diff --git a/web/src/modules/swiss/lib/inputs.test.ts b/web/src/modules/swiss/lib/inputs.test.ts index 5cbfd396..94c6d88b 100644 --- a/web/src/modules/swiss/lib/inputs.test.ts +++ b/web/src/modules/swiss/lib/inputs.test.ts @@ -12,7 +12,7 @@ const port: Input = { key: "service.port", title: "Port", type: "integer", examp const site = (inputs: Record): SiteProfile => ({ name: "prod", model: { pathTemplate: "/models/{{name}}" }, - inputs, + inputs: Object.entries(inputs).map(([key, value]) => ({ key, value })), }); describe("fillTemplate", () => { diff --git a/web/src/modules/swiss/lib/inputs.ts b/web/src/modules/swiss/lib/inputs.ts index 90038856..dcc4cb78 100644 --- a/web/src/modules/swiss/lib/inputs.ts +++ b/web/src/modules/swiss/lib/inputs.ts @@ -1,4 +1,4 @@ -import type { Input, SiteProfile } from "./api"; +import type { Input, SiteInput, SiteProfile } from "./api"; import { findAt, isFieldKey, readAt, splitKey, writeAt } from "./inputKey"; export interface InputChoice { @@ -47,7 +47,7 @@ export function inputRows( ): InputRow[] { const recorded = releaseInputState(inputs, variantValues, release, annotation).answers; return (inputs ?? []).map((input) => { - const v = profile?.inputs?.[input.key]; + const v = profile?.inputs?.find((s) => s.key === input.key)?.value; const site = v === undefined || v === null ? undefined : { answer: typeof v === "string" ? fillTemplate(v, hf, model) : String(v), written: String(v) }; const release = recorded[input.key]; const found: [string | undefined, InputChoice["from"]][] = [ @@ -212,14 +212,14 @@ export function releaseInputState( export function filledSiteAnswers( inputs: Input[] | undefined, - siteInputs: Record | undefined, + siteInputs: SiteInput[] | undefined, hf: string, model: string, ): Record { const out: Record = {}; if (!siteInputs) return out; for (const input of inputs ?? []) { - const v = siteInputs[input.key]; + const v = siteInputs.find((s) => s.key === input.key)?.value; if (v == null) continue; const text = typeof v === "string" ? fillTemplate(v, hf, model) : String(v); if (text) out[input.key] = text; @@ -273,5 +273,5 @@ export function parseAnswer(text: string): Answer { return /^"(-?\d+|true|false)"$/.test(text) ? text.slice(1, -1) : text; } -export const answersText = (a?: Record) => a && Object.fromEntries(Object.entries(a).map(([k, v]) => [k, answerText(v)])); -export const parseAnswers = (r?: Record) => r && Object.fromEntries(Object.entries(r).map(([k, v]) => [k, parseAnswer(v)])); +export const answersText = (a?: SiteInput[]) => a && Object.fromEntries(a.map((s) => [s.key, answerText(s.value)])); +export const parseAnswers = (r?: Record): SiteInput[] | undefined => r && Object.entries(r).map(([key, v]) => ({ key, value: parseAnswer(v) })); diff --git a/web/src/modules/swiss/lib/siteAnswers.test.ts b/web/src/modules/swiss/lib/siteAnswers.test.ts index 96ff9486..e9796071 100644 --- a/web/src/modules/swiss/lib/siteAnswers.test.ts +++ b/web/src/modules/swiss/lib/siteAnswers.test.ts @@ -16,16 +16,23 @@ describe("withSiteAnswers", () => { expect(parse(out)).toEqual({ name: "lab", model: { pathTemplate: "/models/{{name}}" }, - inputs: { "storageKVOffload.mooncake.master": "mc:1", "service.port": 8050 }, + inputs: [ + { key: "storageKVOffload.mooncake.master", value: "mc:1" }, + { key: "service.port", value: 8050 }, + ], }); }); it("replaces an answer and keeps the others", () => { - const out = withSiteAnswers(`${profile}inputs:\n a.b: old # site-wide\n c.d: kept\n`, [{ key: "a.b", value: "new" }]); - expect(parse(out).inputs).toEqual({ "a.b": "new", "c.d": "kept" }); + const out = withSiteAnswers(`${profile}inputs:\n - key: a.b\n value: old # site-wide\n - key: c.d\n value: kept\n`, [{ key: "a.b", value: "new" }]); + expect(parse(out).inputs).toEqual([{ key: "a.b", value: "new" }, { key: "c.d", value: "kept" }]); expect(out).toContain("# site-wide"); }); it("fills an empty inputs section", () => { - expect(parse(withSiteAnswers(`${profile}inputs:\n`, [{ key: "a.b", value: true }])).inputs).toEqual({ "a.b": true }); + expect(parse(withSiteAnswers(`${profile}inputs:\n`, [{ key: "a.b", value: true }])).inputs).toEqual([{ key: "a.b", value: true }]); + }); + it("adds a key with brackets as a plain string", () => { + const key = "volumes.[name=draft-model].hostPath.path"; + expect(parse(withSiteAnswers(profile, [{ key, value: "/data/x" }])).inputs).toEqual([{ key, value: "/data/x" }]); }); it("refuses a document that does not parse", () => { expect(() => withSiteAnswers("name: [", [{ key: "a.b", value: 1 }])).toThrow(); diff --git a/web/src/modules/swiss/lib/siteAnswers.ts b/web/src/modules/swiss/lib/siteAnswers.ts index 7932750a..0116ea54 100644 --- a/web/src/modules/swiss/lib/siteAnswers.ts +++ b/web/src/modules/swiss/lib/siteAnswers.ts @@ -1,13 +1,19 @@ -import { isMap, parseDocument } from "yaml"; +import { isMap, isSeq, parseDocument } from "yaml"; import { api, type ProfileResponse } from "./api"; import type { SiteSave } from "./inputs"; -// The profile's text with each answer set under inputs, its comments and every other line kept. +// The profile's text with each answer set in the inputs list, its comments and every other line kept. export function withSiteAnswers(yaml: string, saves: Pick[]): string { const doc = parseDocument(yaml); if (doc.errors.length) throw new Error(doc.errors[0].message); - if (doc.has("inputs") && !isMap(doc.get("inputs", true))) doc.set("inputs", doc.createNode({})); - for (const s of saves) doc.setIn(["inputs", s.key], s.value); + if (!isSeq(doc.get("inputs", true))) doc.set("inputs", doc.createNode([])); + const list = doc.get("inputs", true); + if (!isSeq(list)) throw new Error("inputs is not a list"); + for (const s of saves) { + const at = list.items.find((it) => isMap(it) && it.get("key") === s.key); + if (isMap(at)) at.set("value", s.value); + else list.add(doc.createNode({ key: s.key, value: s.value })); + } return String(doc); } diff --git a/web/src/modules/swiss/routes/SiteProfile.tsx b/web/src/modules/swiss/routes/SiteProfile.tsx index 2313f0bc..06b75fb1 100644 --- a/web/src/modules/swiss/routes/SiteProfile.tsx +++ b/web/src/modules/swiss/routes/SiteProfile.tsx @@ -155,8 +155,8 @@ export function SiteProfile() { title="Variant inputs" hint="Answers to the inputs catalog variants declare, by chart key. Only a variant that declares a key takes its answer, and a deploy can still override it." > - {p.inputs && Object.keys(p.inputs).length > 0 ? ( - Object.entries(p.inputs).map(([k, v]) => ) + {p.inputs?.length ? ( + p.inputs.map((s) => ) ) : ( )}