Skip to content

Commit 0140e31

Browse files
committed
fix(gui): keep routing dry-run state aligned with selection
Refresh the selected profile on reload and clear stale dry-run results when the profile or evidence inputs change.
1 parent 2fd66d3 commit 0140e31

2 files changed

Lines changed: 159 additions & 35 deletions

File tree

‎gui/src/pages/RoutingProfiles.tsx‎

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ type Analytics = {
3333
breakdown: Array<{ provider: string; model: string; requests: number; successRate: number | null; p50DurationMs?: number }>;
3434
};
3535

36+
type DryRunResult = {
37+
candidates: DryRunCandidate[];
38+
selectedIndex: number | null;
39+
trace?: { profile?: { revision?: string } };
40+
};
41+
3642
function fmtMs(value: number | undefined): string {
3743
return value === undefined ? "–" : `${Math.round(value)}ms`;
3844
}
@@ -41,6 +47,14 @@ function fmtRate(value: number | null | undefined): string {
4147
return value === null || value === undefined ? "–" : `${Math.round(value * 100)}%`;
4248
}
4349

50+
function pickSelectedProfile(next: ProfileDto[], current: ProfileDto | null): ProfileDto | null {
51+
if (current) {
52+
const refreshed = next.find(profile => profile.id === current.id);
53+
if (refreshed) return refreshed;
54+
}
55+
return next[0] ?? null;
56+
}
57+
4458
export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
4559
const t = useT();
4660
const [profiles, setProfiles] = useState<ProfileDto[]>([]);
@@ -51,10 +65,20 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
5165
const [tools, setTools] = useState(false);
5266
const [image, setImage] = useState(false);
5367
const [structured, setStructured] = useState(false);
54-
const [dryRunResult, setDryRunResult] = useState<{ candidates: DryRunCandidate[]; selectedIndex: number | null; trace?: { profile?: { revision?: string } } } | null>(null);
68+
const [dryRunResult, setDryRunResult] = useState<DryRunResult | null>(null);
5569
const [dryRunError, setDryRunError] = useState("");
5670
const [running, setRunning] = useState(false);
5771

72+
const clearDryRun = useCallback(() => {
73+
setDryRunResult(null);
74+
setDryRunError("");
75+
}, []);
76+
77+
const selectProfile = useCallback((profile: ProfileDto | null) => {
78+
setSelected(profile);
79+
clearDryRun();
80+
}, [clearDryRun]);
81+
5882
const load = useCallback(async () => {
5983
setLoadError("");
6084
try {
@@ -66,15 +90,22 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
6690
const profilesJson = await profilesRes.json() as { profiles?: ProfileDto[] };
6791
const next = profilesJson.profiles ?? [];
6892
setProfiles(next);
69-
setSelected(current => current ?? next[0] ?? null);
93+
setSelected(current => {
94+
const refreshed = pickSelectedProfile(next, current);
95+
if (current && refreshed && current.revision !== refreshed.revision) {
96+
clearDryRun();
97+
}
98+
if (current && !refreshed) clearDryRun();
99+
return refreshed;
100+
});
70101
if (analyticsRes.ok) {
71102
const analyticsJson = await analyticsRes.json() as Analytics;
72103
setAnalytics(analyticsJson);
73104
}
74105
} catch (error) {
75106
setLoadError(error instanceof Error ? error.message : String(error));
76107
}
77-
}, [apiBase]);
108+
}, [apiBase, clearDryRun]);
78109

79110
useEffect(() => {
80111
const timer = window.setTimeout(() => void load(), 0);
@@ -84,8 +115,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
84115
const runDryRun = async () => {
85116
if (!selected) return;
86117
setRunning(true);
87-
setDryRunError("");
88-
setDryRunResult(null);
118+
clearDryRun();
89119
try {
90120
const response = await fetch(`${apiBase}/api/routing-profiles/dry-run`, {
91121
method: "POST",
@@ -111,7 +141,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
111141
setDryRunError(message);
112142
return;
113143
}
114-
setDryRunResult(await response.json() as typeof dryRunResult);
144+
setDryRunResult(await response.json() as DryRunResult);
115145
} catch (error) {
116146
setDryRunError(error instanceof Error ? error.message : String(error));
117147
} finally {
@@ -139,7 +169,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
139169
type="button"
140170
className="model-card"
141171
style={{ textAlign: "left", cursor: "pointer" }}
142-
onClick={() => setSelected(profile)}
172+
onClick={() => selectProfile(profile)}
143173
aria-pressed={selected?.id === profile.id}
144174
>
145175
<div className="card-badges">
@@ -165,30 +195,21 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
165195
))}
166196
</div>
167197
</div>
168-
<div>
169-
<span className="field-label">{t("routing.require")}</span>
170-
<pre className="muted" style={{ margin: 0, whiteSpace: "pre-wrap" }}>
171-
{Object.keys(selected.require).length ? JSON.stringify(selected.require, null, 2) : t("routing.none")}
172-
</pre>
173-
</div>
174-
<div>
175-
<span className="field-label">{t("routing.optimize")}</span>
176-
<pre className="muted" style={{ margin: 0, whiteSpace: "pre-wrap" }}>
177-
{JSON.stringify(selected.optimize, null, 2)}
178-
</pre>
179-
</div>
180-
<div>
181-
<span className="field-label">{t("routing.limits")}</span>
182-
<pre className="muted" style={{ margin: 0, whiteSpace: "pre-wrap" }}>
183-
{Object.keys(selected.limits).length ? JSON.stringify(selected.limits, null, 2) : t("routing.none")}
184-
</pre>
185-
</div>
186-
<div>
187-
<span className="field-label">{t("routing.unknownEvidence")}</span>
188-
<pre className="muted" style={{ margin: 0, whiteSpace: "pre-wrap" }}>
189-
{JSON.stringify(selected.unknownEvidence, null, 2)}
190-
</pre>
191-
</div>
198+
{([
199+
["routing.require", selected.require, true],
200+
["routing.optimize", selected.optimize, false],
201+
["routing.limits", selected.limits, true],
202+
["routing.unknownEvidence", selected.unknownEvidence, false],
203+
] as const).map(([labelKey, value, allowEmpty]) => (
204+
<div key={labelKey}>
205+
<span className="field-label">{t(labelKey)}</span>
206+
<pre className="muted" style={{ margin: 0, whiteSpace: "pre-wrap" }}>
207+
{allowEmpty && Object.keys(value).length === 0
208+
? t("routing.none")
209+
: JSON.stringify(value, null, 2)}
210+
</pre>
211+
</div>
212+
))}
192213
</div>
193214
) : null}
194215

@@ -202,19 +223,43 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
202223
type="number"
203224
min={1}
204225
value={context}
205-
onChange={event => setContext(event.target.value)}
226+
onChange={event => {
227+
setContext(event.target.value);
228+
clearDryRun();
229+
}}
206230
/>
207231
</label>
208232
<label className="checkbox">
209-
<input type="checkbox" checked={tools} onChange={event => setTools(event.target.checked)} />
233+
<input
234+
type="checkbox"
235+
checked={tools}
236+
onChange={event => {
237+
setTools(event.target.checked);
238+
clearDryRun();
239+
}}
240+
/>
210241
{t("routing.dryRunTools")}
211242
</label>
212243
<label className="checkbox">
213-
<input type="checkbox" checked={image} onChange={event => setImage(event.target.checked)} />
244+
<input
245+
type="checkbox"
246+
checked={image}
247+
onChange={event => {
248+
setImage(event.target.checked);
249+
clearDryRun();
250+
}}
251+
/>
214252
{t("routing.dryRunImage")}
215253
</label>
216254
<label className="checkbox">
217-
<input type="checkbox" checked={structured} onChange={event => setStructured(event.target.checked)} />
255+
<input
256+
type="checkbox"
257+
checked={structured}
258+
onChange={event => {
259+
setStructured(event.target.checked);
260+
clearDryRun();
261+
}}
262+
/>
218263
{t("routing.dryRunStructured")}
219264
</label>
220265
<button type="button" className="btn btn-primary" disabled={!selected || running} onClick={() => void runDryRun()}>

‎gui/tests/routing-profiles.test.tsx‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ const PROFILE = {
2626
unknownEvidence: { capability: "exclude", health: "penalize" },
2727
};
2828

29+
const PROFILE_B = {
30+
...PROFILE,
31+
id: "cheap",
32+
model: "policy/cheap",
33+
revision: "rev-cheap",
34+
candidates: [{ provider: "openai", model: "gpt-5-mini" }],
35+
};
36+
37+
const PROFILE_REFRESHED = {
38+
...PROFILE,
39+
revision: "rev-def",
40+
require: { tools: true, image: true },
41+
};
42+
2943
const ANALYTICS = {
3044
totalRequests: 12,
3145
successRate: 0.75,
@@ -198,3 +212,68 @@ test("routing dry-run checks response status before treating the body as success
198212
await act(async () => { root.unmount(); });
199213
}
200214
});
215+
216+
test("routing clears stale dry-run results when switching profiles", async () => {
217+
installFetch((url, init) => {
218+
if (url.endsWith("/api/routing-profiles") && (init?.method ?? "GET") === "GET") {
219+
return Response.json({ profiles: [PROFILE, PROFILE_B] });
220+
}
221+
if (url.endsWith("/api/routing-analytics")) {
222+
return Response.json(ANALYTICS);
223+
}
224+
if (url.endsWith("/api/routing-profiles/dry-run") && init?.method === "POST") {
225+
return Response.json(DRY_RUN_OK);
226+
}
227+
return new Response("missing", { status: 404 });
228+
});
229+
230+
const { container, root } = await mountPage();
231+
try {
232+
const evaluate = [...container.querySelectorAll<HTMLButtonElement>("button")]
233+
.find(button => button.textContent?.includes("Evaluate candidates"));
234+
expect(evaluate).toBeTruthy();
235+
await act(async () => { evaluate!.click(); });
236+
await tick(3);
237+
expect(container.textContent).toContain("0.910");
238+
239+
const cheap = [...container.querySelectorAll<HTMLButtonElement>("button")]
240+
.find(button => button.textContent?.includes("cheap"));
241+
expect(cheap).toBeTruthy();
242+
await act(async () => { cheap!.click(); });
243+
await tick(2);
244+
245+
expect(container.textContent).toContain("policy/cheap");
246+
expect(container.textContent).not.toContain("0.910");
247+
expect(container.textContent).not.toContain("unknown-capability");
248+
} finally {
249+
await act(async () => { root.unmount(); });
250+
}
251+
});
252+
253+
test("routing refreshes the selected profile after reload", async () => {
254+
let profilesPayload: unknown[] = [PROFILE];
255+
installFetch((url, init) => {
256+
if (url.endsWith("/api/routing-profiles") && (init?.method ?? "GET") === "GET") {
257+
return Response.json({ profiles: profilesPayload });
258+
}
259+
if (url.endsWith("/api/routing-analytics")) {
260+
return Response.json(ANALYTICS);
261+
}
262+
return new Response("missing", { status: 404 });
263+
});
264+
265+
const { container, root } = await mountPage();
266+
try {
267+
expect(container.textContent).toContain("rev-abc");
268+
profilesPayload = [PROFILE_REFRESHED];
269+
const retry = [...container.querySelectorAll<HTMLButtonElement>("button")]
270+
.find(button => button.textContent?.trim() === "Retry");
271+
expect(retry).toBeTruthy();
272+
await act(async () => { retry!.click(); });
273+
await tick(3);
274+
expect(container.textContent).toContain("rev-def");
275+
expect(container.textContent).toContain("\"image\": true");
276+
} finally {
277+
await act(async () => { root.unmount(); });
278+
}
279+
});

0 commit comments

Comments
 (0)