@devries-cameron20 / t3code Mirror · read-only / commits / 756545a917
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(web): successive custom model saves keep earlier edits (#36906)

Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 22:00 UTC 756545a917fcd16c0edffe84a4400a91491289d7
8 files changed +519 -27
apps/web/src/components/settings/CustomModelEditor.tsx
+13 -7
@@ -1,5 +1,5 @@
1 import { PlusIcon, XIcon } from "lucide-react";
2 -import { useState } from "react";
2 +import { useState, type SetStateAction } from "react";
3 import type { ProviderDriverKind, ServerProviderModel } from "@t3tools/contracts";
4 import type { CustomModelDefinition } from "@t3tools/shared/model";
5
@@ -33,6 +33,7 @@ interface CustomModelEditorProps {
33 readonly builtInModels: ReadonlyArray<ServerProviderModel>;
34 readonly onSave: (next: CustomModelDefinition) => void;
35 readonly onCancel: () => void;
36 + readonly onDraftChange: () => void;
37 }
38
39 export function CustomModelEditor({
@@ -42,9 +43,14 @@ export function CustomModelEditor({
43 builtInModels,
44 onSave,
45 onCancel,
46 + onDraftChange,
47 }: CustomModelEditorProps) {
48 const [draft, setDraft] = useState<CustomModelDraft>(() => draftFromDefinition(entry));
49 const [error, setError] = useState<string | null>(null);
50 + const updateDraft = (next: SetStateAction<CustomModelDraft>) => {
51 + onDraftChange();
52 + setDraft(next);
53 + };
54 const presets = driverKind ? (DESCRIPTOR_PRESETS_BY_KIND[driverKind] ?? []) : [];
55 const startFromCandidates = builtInModels.filter(
56 (model) => (model.capabilities?.optionDescriptors?.length ?? 0) > 0,
@@ -53,7 +59,7 @@ export function CustomModelEditor({
59
60 const updateDescriptor = (key: string, patch: Partial<EditorDescriptor>) => {
61 setError(null);
56 - setDraft((current) => ({
62 + updateDraft((current) => ({
63 ...current,
64 descriptors: current.descriptors.map((descriptor) =>
65 descriptor.key === key ? { ...descriptor, ...patch } : descriptor,
@@ -63,7 +69,7 @@ export function CustomModelEditor({
69
70 const updateChoice = (descriptorKey: string, choiceKey: string, patch: Partial<EditorChoice>) => {
71 setError(null);
66 - setDraft((current) => ({
72 + updateDraft((current) => ({
73 ...current,
74 descriptors: current.descriptors.map((descriptor) => {
75 if (descriptor.key !== descriptorKey) return descriptor;
@@ -80,7 +86,7 @@ export function CustomModelEditor({
86
87 const removeDescriptor = (key: string) => {
88 setError(null);
83 - setDraft((current) => ({
89 + updateDraft((current) => ({
90 ...current,
91 descriptors: current.descriptors.filter((descriptor) => descriptor.key !== key),
92 }));
@@ -88,7 +94,7 @@ export function CustomModelEditor({
94
95 const addDescriptor = (descriptor: EditorDescriptor) => {
96 setError(null);
91 - setDraft((current) => ({ ...current, descriptors: [...current.descriptors, descriptor] }));
97 + updateDraft((current) => ({ ...current, descriptors: [...current.descriptors, descriptor] }));
98 };
99
100 const applyPresetId = (descriptor: EditorDescriptor, value: string | null) => {
@@ -114,7 +120,7 @@ export function CustomModelEditor({
120 const model = startFromCandidates.find((candidate) => candidate.slug === slug);
121 if (!model) return;
122 setError(null);
117 - setDraft((current) => ({
123 + updateDraft((current) => ({
124 ...current,
125 descriptors: descriptorsFromCapabilities(model.capabilities, driverKind),
126 }));
@@ -294,7 +300,7 @@ export function CustomModelEditor({
300 size="sm"
301 autoFocus
302 value={draft.name}
297 - onChange={(event) => setDraft((current) => ({ ...current, name: event.target.value }))}
303 + onChange={(event) => updateDraft((current) => ({ ...current, name: event.target.value }))}
304 placeholder={draft.slug}
305 className="sm:w-72"
306 spellCheck={false}
apps/web/src/components/settings/ProviderInstanceCard.tsx
+45 -12
@@ -70,6 +70,12 @@ import { SettingResetButton, SettingsRow, SettingsSection } from "./settingsLayo
70 import { AcpSessionManagementSection } from "./AcpSessionManagementSection";
71 import { FoldedSettingsSection } from "./FoldedSettingsSection";
72 import { readCodexSetupMode } from "./CodexSetupSection.logic";
73 +import {
74 + type CustomModelSave,
75 + customModelsKey,
76 + pendingCustomModelSaves,
77 + settleCustomModelSave,
78 +} from "./customModelSaves.logic";
79 import { getProviderRestartNotice } from "../ProviderUpdateLaunchNotification.logic";
80 import {
81 getProviderVersionAdvisoryPresentation,
@@ -711,6 +717,33 @@ const EMPTY_ACP_PROJECTS: NonNullable<ProviderInstanceCardProps["acpProjects"]>
717 const NO_SECRET_SOURCES: ReadonlyArray<SecretSource> = [];
718 const NO_CUSTOM_MODELS: ReadonlyArray<CustomModelDefinition> = [];
719
720 +/**
721 + * Every custom model save writes the whole list, so the next edit starts from the last list sent
722 + * until the config stream echoes it, which can land before or after the save's reply.
723 + */
724 +function useCustomModelSaves(
725 + stored: ReadonlyArray<CustomModelDefinition>,
726 + save: (next: ReadonlyArray<CustomModelDefinition>) => Promise<boolean>,
727 +) {
728 + const [saves, setSaves] = useState<ReadonlyArray<CustomModelSave>>([]);
729 + const storedKey = customModelsKey(stored);
730 + const [previousStoredKey, setPreviousStoredKey] = useState(storedKey);
731 + if (previousStoredKey !== storedKey) {
732 + setPreviousStoredKey(storedKey);
733 + // Unchanged contents can match a queued restore, so only a stream change can be an echo.
734 + setSaves((list) => pendingCustomModelSaves(list, storedKey));
735 + }
736 + const current = saves.at(-1)?.models ?? stored;
737 + const update = useStableCallback(async (next: ReadonlyArray<CustomModelDefinition>) => {
738 + const sent: CustomModelSave = { models: next, key: customModelsKey(next) };
739 + setSaves((list) => [...list, sent]);
740 + const saved = await save(next);
741 + setSaves((list) => settleCustomModelSave(list, sent, saved));
742 + return saved;
743 + });
744 + return [current, update] as const;
745 +}
746 +
747 /** Unknown drivers stay read-only to preserve fork config. projectOverride switches affect only the
748 * project, not instance.enabled. */
749 export function ProviderInstanceCard({
@@ -825,10 +858,20 @@ export function ProviderInstanceCard({
858 instance.config !== null && typeof instance.config === "object"
859 ? (instance.config as Record<string, unknown>).customModels
860 : undefined;
828 - const customModels =
861 + const [customModels, updateCustomModels] = useCustomModelSaves(
862 instance.driver === "antigravity"
863 ? NO_CUSTOM_MODELS
831 - : readCustomModelEntries(storedCustomModels);
864 + : readCustomModelEntries(storedCustomModels),
865 + (next) => {
866 + const nextConfig = nextConfigBlobWithValue(
867 + instance.config,
868 + "customModels",
869 + next.map(toCustomModelSetting),
870 + );
871 + const { config: _omit, ...rest } = instance;
872 + return onUpdate({ ...rest, config: nextConfig } as ProviderInstanceConfig);
873 + },
874 + );
875 const modelsForDisplay = deriveProviderModelsForDisplay({
876 liveModels: liveProvider?.models,
877 customModels,
@@ -870,16 +913,6 @@ export function ProviderInstanceCard({
913 );
914 };
915
873 - const updateCustomModels = useStableCallback((next: ReadonlyArray<CustomModelDefinition>) => {
874 - const nextConfig = nextConfigBlobWithValue(
875 - instance.config,
876 - "customModels",
877 - next.map(toCustomModelSetting),
878 - );
879 - const { config: _omit, ...rest } = instance;
880 - return onUpdate({ ...rest, config: nextConfig } as ProviderInstanceConfig);
881 - });
882 -
916 const updateEnvironment = (environment: ReadonlyArray<ProviderInstanceEnvironmentVariable>) => {
917 const cleaned = environment.filter((variable) => variable.name.trim().length > 0);
918 const { environment: _omit, ...rest } = instance;
apps/web/src/components/settings/ProviderModelsSection.browser.test.tsx
+35 -2
@@ -36,10 +36,13 @@ it("lists a favorite the provider no longer reports so it can be unstarred", asy
36 expect(onToggleFavorite).toHaveBeenCalledWith("retired-model");
37 });
38
39 -it("keeps a custom model draft until it saves, so a failed save can be retried", async () => {
39 +it("keeps custom model drafts until they save, so a failed save can be retried", async () => {
40 const saves: Array<(saved: boolean) => void> = [];
41 const onChange = () => new Promise<boolean>((resolve) => saves.push(resolve));
42 - await using screen = await render(section({ onChange }));
42 + const kept = { slug: "kept-model", name: "kept-model", capabilities: null };
43 + await using screen = await render(
44 + section({ onChange, models: [{ ...kept, isCustom: true }], customModels: [kept] }),
45 + );
46 const addCustomModel = screen.getByRole("button", { name: "Add custom model" });
47 const input = screen.getByRole("textbox");
48 const add = screen.getByRole("button", { name: "Add", exact: true });
@@ -64,4 +67,34 @@ it("keeps a custom model draft until it saves, so a failed save can be retried",
67 await userEvent.click(addCustomModel);
68 await act(() => saves[2]!(true));
69 await expect.element(add).toBeEnabled();
70 +
71 + await userEvent.click(screen.getByRole("button", { name: "Edit kept-model", exact: true }));
72 + const displayName = screen.getByRole("textbox", { name: "Display name", exact: true });
73 + await userEvent.fill(displayName, "Renamed");
74 + await userEvent.click(screen.getByRole("button", { name: "Save", exact: true }));
75 + await act(() => saves[3]!(false));
76 + await expect.element(displayName).toHaveValue("Renamed");
77 + await userEvent.click(screen.getByRole("button", { name: "Save", exact: true }));
78 + await act(() => saves[4]!(true));
79 + await expect.element(displayName).not.toBeInTheDocument();
80 +});
81 +
82 +it("keeps changes typed while an earlier save succeeds", async () => {
83 + const saves: Array<(saved: boolean) => void> = [];
84 + const onChange = () => new Promise<boolean>((resolve) => saves.push(resolve));
85 + const kept = { slug: "kept-model", name: "kept-model", capabilities: null };
86 + await using screen = await render(
87 + section({ onChange, models: [{ ...kept, isCustom: true }], customModels: [kept] }),
88 + );
89 + await screen.getByRole("button", { name: "Edit kept-model", exact: true }).click();
90 + const displayName = screen.getByRole("textbox", { name: "Display name", exact: true });
91 + const save = screen.getByRole("button", { name: "Save", exact: true });
92 + await userEvent.fill(displayName, "Submitted draft");
93 + await save.click();
94 + await userEvent.fill(displayName, "New draft");
95 + await act(() => saves[0]!(true));
96 + await expect.element(displayName).toHaveValue("New draft");
97 + await save.click();
98 + await act(() => saves[1]!(true));
99 + await expect.element(displayName).not.toBeInTheDocument();
100 });
apps/web/src/components/settings/ProviderModelsSection.tsx
+20 -6
@@ -143,6 +143,7 @@ export function ProviderModelsSection({
143 const [filter, setFilter] = useState("");
144 const [error, setError] = useState<string | null>(null);
145 const [editingSlug, setEditingSlug] = useState<string | null>(null);
146 + const editAttemptRef = useRef(0);
147 const listRef = useRef<HTMLDivElement>(null);
148 const scrollToSlugRef = useRef<string | null>(null);
149 const hiddenModelSet = new Set(hiddenModels);
@@ -238,17 +239,23 @@ export function ProviderModelsSection({
239
240 const handleRemove = (slug: string) => {
241 if (!canManageCustomModels) return;
241 - if (editingSlug === slug) setEditingSlug(null);
242 + if (editingSlug === slug) {
243 + editAttemptRef.current += 1;
244 + setEditingSlug(null);
245 + }
246 void onChange(customModels.filter((entry) => entry.slug !== slug));
247 onModelOrderChange(modelOrder.filter((model) => model !== slug));
248 onFavoriteModelsChange(favoriteModels.filter((model) => model !== slug));
249 setError(null);
250 };
251
248 - const handleSaveEdit = (next: CustomModelDefinition) => {
252 + const handleSaveEdit = async (next: CustomModelDefinition) => {
253 if (!canManageCustomModels) return;
250 - void onChange(customModels.map((entry) => (entry.slug === next.slug ? next : entry)));
251 - setEditingSlug(null);
254 + const attempt = ++editAttemptRef.current;
255 + const saved = await onChange(
256 + customModels.map((entry) => (entry.slug === next.slug ? next : entry)),
257 + );
258 + if (saved && attempt === editAttemptRef.current) setEditingSlug(null);
259 };
260
261 const setHidden = (slug: string, hidden: boolean) => {
@@ -283,6 +290,7 @@ export function ProviderModelsSection({
290
291 const handleToggleEdit = useStableCallback((slug: string) => {
292 if (!canManageCustomModels) return;
293 + editAttemptRef.current += 1;
294 setEditingSlug((current) => (current === slug ? null : slug));
295 });
296 const rowHandlers = {
@@ -409,8 +417,14 @@ export function ProviderModelsSection({
417 driverKind={driverKind}
418 entry={editingEntry}
419 builtInModels={builtInModels}
412 - onSave={handleSaveEdit}
413 - onCancel={() => setEditingSlug(null)}
420 + onSave={(next) => void handleSaveEdit(next)}
421 + onDraftChange={() => {
422 + editAttemptRef.current += 1;
423 + }}
424 + onCancel={() => {
425 + editAttemptRef.current += 1;
426 + setEditingSlug(null);
427 + }}
428 />
429 ) : null}
430 </div>
apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx
+99
@@ -8,6 +8,7 @@ import {
8 type ServerProvider,
9 type UnifiedSettings,
10 } from "@t3tools/contracts";
11 +import * as Cause from "effect/Cause";
12 import * as Duration from "effect/Duration";
13 import { AsyncResult, type Atom } from "effect/reactivity";
14 import type { ReactNode } from "react";
@@ -284,6 +285,104 @@ describe("EnvironmentProviderSettings routing", () => {
285 );
286 });
287
288 + it.each([false, true])(
289 + "builds a custom model save on the last one before its config echo (replied %s)",
290 + async (replied) => {
291 + await setup();
292 + const first = { slug: "model-a", name: "Original A" };
293 + const second = { slug: "model-b", name: "Original B" };
294 + const stored = (customModels: ReadonlyArray<{ slug: string; name: string }>) => ({
295 + ...DEFAULT_UNIFIED_SETTINGS,
296 + providerInstances: {
297 + [customId]: { driver: ProviderDriverKind.make("codex"), config: { customModels } },
298 + },
299 + });
300 + state.settings = stored([first, second]);
301 + const replies: Array<() => void> = [];
302 + state.mutateProviderInstance.mockImplementation(
303 + () => new Promise((resolve) => replies.push(() => resolve(AsyncResult.success({})))),
304 + );
305 + await using screen = await render(panel({ targetInstanceId: customId }));
306 + const rename = async (slug: string, name: string) => {
307 + await userEvent.click(screen.getByRole("button", { name: `Edit ${slug}`, exact: true }));
308 + await userEvent.fill(
309 + screen.getByRole("textbox", { name: "Display name", exact: true }),
310 + name,
311 + );
312 + await userEvent.click(screen.getByRole("button", { name: "Save", exact: true }));
313 + };
314 +
315 + await rename("model-a", "Entered A");
316 + if (replied) await act(() => replies[0]!());
317 + await rename("model-b", "Entered B");
318 +
319 + expect(state.mutateProviderInstance.mock.calls[1]?.[0]).toEqual({
320 + operation: "upsert",
321 + instanceId: customId,
322 + instance: {
323 + driver: ProviderDriverKind.make("codex"),
324 + config: {
325 + customModels: [
326 + { ...first, name: "Entered A" },
327 + { ...second, name: "Entered B" },
328 + ],
329 + },
330 + },
331 + });
332 +
333 + // The first save's echo lands after both replied, ahead of the second's.
334 + await act(() => replies.forEach((reply) => reply()));
335 + state.settings = stored([{ ...first, name: "Entered A" }, second]);
336 + await screen.rerender(panel({ targetInstanceId: customId }));
337 + await rename("model-a", "Again A");
338 + expect(state.mutateProviderInstance.mock.calls[2]?.[0].instance.config.customModels).toEqual([
339 + { ...first, name: "Again A" },
340 + { ...second, name: "Entered B" },
341 + ]);
342 + },
343 + );
344 +
345 + it("preserves a failed model draft for retry without including it in another edit", async () => {
346 + await setup();
347 + const first = { slug: "model-a", name: "Original A" };
348 + const second = { slug: "model-b", name: "Original B" };
349 + state.settings = {
350 + ...DEFAULT_UNIFIED_SETTINGS,
351 + providerInstances: {
352 + [customId]: {
353 + driver: ProviderDriverKind.make("codex"),
354 + config: { customModels: [first, second] },
355 + },
356 + },
357 + };
358 + state.mutateProviderInstance
359 + .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Save failed"))))
360 + .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Still offline"))));
361 + await using screen = await render(panel({ targetInstanceId: customId }));
362 + await screen.getByRole("button", { name: "Edit model-a", exact: true }).click();
363 + const displayName = screen.getByRole("textbox", { name: "Display name", exact: true });
364 + await userEvent.fill(displayName, "Entered A");
365 + const save = screen.getByRole("button", { name: "Save", exact: true });
366 + await save.click();
367 + await expect.element(save).toBeEnabled();
368 + await expect.element(displayName).toHaveValue("Entered A");
369 + await save.click();
370 + await expect.element(save).toBeEnabled();
371 + expect(state.mutateProviderInstance.mock.calls[1]?.[0].instance.config.customModels).toEqual([
372 + { ...first, name: "Entered A" },
373 + second,
374 + ]);
375 + await screen.getByRole("button", { name: "Cancel", exact: true }).click();
376 + await screen.getByRole("button", { name: "Edit model-b", exact: true }).click();
377 + await userEvent.fill(displayName, "Entered B");
378 + await save.click();
379 + await expect.element(displayName).not.toBeInTheDocument();
380 + expect(state.mutateProviderInstance.mock.calls[2]?.[0].instance.config.customModels).toEqual([
381 + first,
382 + { ...second, name: "Entered B" },
383 + ]);
384 + });
385 +
386 it("routes explicit ACP browser authentication consent to the selected environment", async () => {
387 await setup();
388 const instanceId = ProviderInstanceId.make("acpRegistry_antigravity");
apps/web/src/components/settings/customModelSaves.logic.test.ts new
+101
@@ -0,0 +1,101 @@
1 +import { describe, expect, it } from "vite-plus/test";
2 +
3 +import {
4 + type CustomModelSave,
5 + pendingCustomModelSaves,
6 + settleCustomModelSave,
7 +} from "./customModelSaves.logic";
8 +
9 +type Step =
10 + | { readonly send: string }
11 + | { readonly reply: string; readonly saved?: boolean }
12 + | { readonly echo: string };
13 +
14 +/** Replays sends, replies and stored-list echoes; returns the pending saves' keys. */
15 +function replay(initial: string, steps: ReadonlyArray<Step>) {
16 + let stored = initial;
17 + let saves: ReadonlyArray<CustomModelSave> = [];
18 + const sent: Array<CustomModelSave> = [];
19 + for (const step of steps) {
20 + if ("send" in step) {
21 + const save: CustomModelSave = { models: [], key: step.send };
22 + sent.push(save);
23 + saves = saves.concat(save);
24 + } else if ("reply" in step) {
25 + const save = sent.find((entry) => entry.key === step.reply)!;
26 + sent.splice(sent.indexOf(save), 1);
27 + saves = settleCustomModelSave(saves, save, step.saved ?? true);
28 + } else if (step.echo !== stored) {
29 + stored = step.echo;
30 + saves = pendingCustomModelSaves(saves, stored);
31 + }
32 + }
33 + return saves.map((entry) => entry.key);
34 +}
35 +
36 +describe("pending custom model saves", () => {
37 + it.each<[string, ReadonlyArray<Step>, ReadonlyArray<string>]>([
38 + [
39 + "an echo settles its save, not a later one",
40 + [{ send: "a" }, { send: "ab" }, { echo: "a" }],
41 + ["ab"],
42 + ],
43 + [
44 + "a reply before the echo keeps the save",
45 + [{ send: "a" }, { reply: "a" }, { send: "ab" }, { echo: "a" }],
46 + ["ab"],
47 + ],
48 + ["a failed save is dropped", [{ send: "a" }, { reply: "a", saved: false }], []],
49 + [
50 + "a coalesced repeated snapshot settles every save, so a later external match is not an echo",
51 + [
52 + { send: "a" },
53 + { reply: "a" },
54 + { send: "b" },
55 + { reply: "b" },
56 + { send: "a" },
57 + { reply: "a" },
58 + { echo: "a" },
59 + { echo: "b" },
60 + ],
61 + [],
62 + ],
63 + [
64 + "a save racing an external change is dropped on reply",
65 + [{ send: "a" }, { echo: "x" }, { reply: "a" }],
66 + [],
67 + ],
68 + [
69 + "a rename then restore survives both echoes",
70 + [
71 + { send: "a" },
72 + { reply: "a" },
73 + { send: "0" },
74 + { reply: "0" },
75 + { send: "b" },
76 + { echo: "a" },
77 + { echo: "0" },
78 + ],
79 + ["b"],
80 + ],
81 + [
82 + "an external change drops replied saves but keeps in-flight ones",
83 + [{ send: "a" }, { reply: "a" }, { send: "ab" }, { echo: "x" }],
84 + ["ab"],
85 + ],
86 + [
87 + "a late reply after the echo leaves nothing pending",
88 + [
89 + { send: "a" },
90 + { send: "ab" },
91 + { echo: "ab" },
92 + { echo: "a" },
93 + { reply: "a" },
94 + { reply: "ab" },
95 + ],
96 + [],
97 + ],
98 + ])("%s", (_name, steps, pending) => {
99 + expect(replay("0", steps)).toEqual(pending);
100 + });
101 +});
apps/web/src/components/settings/customModelSaves.logic.ts new
+47
@@ -0,0 +1,47 @@
1 +import {
2 + type CustomModelDefinition,
3 + readCustomModelEntries,
4 + toCustomModelSetting,
5 +} from "@t3tools/shared/model";
6 +
7 +export interface CustomModelSave {
8 + readonly models: ReadonlyArray<CustomModelDefinition>;
9 + /** The models as the config stream will read them back once this save lands. */
10 + readonly key: string;
11 + /** The server accepted it; only its echo is outstanding. */
12 + readonly replied?: true;
13 + /** Another writer changed the list while this save was in flight, so its echo may never come. */
14 + readonly raced?: true;
15 +}
16 +
17 +export const customModelsKey = (models: ReadonlyArray<CustomModelDefinition>) =>
18 + JSON.stringify(readCustomModelEntries(models.map(toCustomModelSetting)));
19 +
20 +/**
21 + * Saves land in order, so an echo settles its save and every one before. The latest match wins
22 + * because the stream can coalesce repeats, and a stale save would overwrite another writer later.
23 + * Any other change drops replied saves and marks in-flight ones raced.
24 + */
25 +export function pendingCustomModelSaves(
26 + saves: ReadonlyArray<CustomModelSave>,
27 + storedKey: string,
28 +): ReadonlyArray<CustomModelSave> {
29 + const echoed = saves.findLastIndex((entry) => entry.key === storedKey);
30 + if (echoed >= 0) return saves.slice(echoed + 1);
31 + return saves.flatMap((entry) => (entry.replied ? [] : [{ ...entry, raced: true as const }]));
32 +}
33 +
34 +/** A failed or raced save is dropped; a successful one waits for its echo. */
35 +export function settleCustomModelSave(
36 + saves: ReadonlyArray<CustomModelSave>,
37 + sent: CustomModelSave,
38 + saved: boolean,
39 +): ReadonlyArray<CustomModelSave> {
40 + return saves.flatMap((entry) =>
41 + entry.models !== sent.models
42 + ? [entry]
43 + : saved && !entry.raced
44 + ? [{ ...entry, replied: true as const }]
45 + : [],
46 + );
47 +}
scripts/scenarios/custom-model-edits-saved-before-echo-both-persist.e2e.ts new
+159
@@ -0,0 +1,159 @@
1 +/// <reference lib="dom" />
2 +// The DOM lib types the functions this scenario runs in the page.
3 +import { expect } from "e2e";
4 +import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 +import * as Path from "effect/Path";
7 +import * as Schema from "effect/Schema";
8 +
9 +import { T3_HOME } from "../e2e.config.ts";
10 +import { runPromise, test, WEB } from "./t3.ts";
11 +
12 +const SettingsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown));
13 +const decodeSettings = Schema.decodeEffect(SettingsJson);
14 +const encodeSettings = Schema.encodeEffect(SettingsJson);
15 +interface CodexInstance {
16 + readonly config?: Readonly<Record<string, unknown>>;
17 +}
18 +
19 +interface ConfigHold {
20 + active: boolean;
21 + readonly held: Array<() => void>;
22 + readonly saveReplies: Set<string>;
23 +}
24 +
25 +declare global {
26 + interface Window {
27 + configHold?: ConfigHold;
28 + }
29 +}
30 +
31 +const settingsFile = Effect.gen(function* () {
32 + const fs = yield* FileSystem.FileSystem;
33 + const settingsPath = (yield* Path.Path).join(T3_HOME, "userdata/settings.json");
34 + const original = yield* fs.readFileString(settingsPath);
35 + const settings = yield* decodeSettings(original);
36 + const instances = settings.providerInstances as Record<string, CodexInstance>;
37 + const codex = instances.codex!;
38 + const customModels = [
39 + { slug: "model-a", name: "Original A" },
40 + { slug: "model-b", name: "Original B" },
41 + ];
42 + return {
43 + seed: fs.writeFileString(
44 + settingsPath,
45 + yield* encodeSettings({
46 + ...settings,
47 + providerInstances: {
48 + ...instances,
49 + codex: { ...codex, config: { ...codex.config, customModels } },
50 + },
51 + }),
52 + ),
53 + savedNames: fs.readFileString(settingsPath).pipe(
54 + Effect.flatMap(decodeSettings),
55 + Effect.map((saved) => {
56 + const config = (saved.providerInstances as Record<string, CodexInstance>).codex?.config;
57 + const models = (config?.customModels ?? []) as ReadonlyArray<{ readonly name: string }>;
58 + return models.map((model) => model.name);
59 + }),
60 + ),
61 + restore: fs.writeFileString(settingsPath, original),
62 + };
63 +});
64 +
65 +test(
66 + "Two custom model edits saved before the first one's settings echo both persist",
67 + WEB,
68 + async ({ t3, app, browser, screen }) => {
69 + // Holds the server config stream while active, so a save's reply arrives before its settings
70 + // echo, and records settings save replies by request id.
71 + await browser.addInitScript(() => {
72 + const hold: ConfigHold = { active: false, held: [], saveReplies: new Set() };
73 + window.configHold = hold;
74 + const tags = new Map<string, string>();
75 + const send = WebSocket.prototype.send;
76 + WebSocket.prototype.send = function (data) {
77 + if (typeof data === "string") {
78 + for (const message of [JSON.parse(data)].flat()) {
79 + if (message?._tag === "Request") tags.set(String(message.id), message.tag);
80 + }
81 + }
82 + return send.call(this, data);
83 + };
84 + const addEventListener = WebSocket.prototype.addEventListener;
85 + WebSocket.prototype.addEventListener = function (
86 + this: WebSocket,
87 + type: string,
88 + listener: EventListenerOrEventListenerObject,
89 + options?: boolean | AddEventListenerOptions,
90 + ) {
91 + if (type !== "message" || typeof listener !== "function") {
92 + return addEventListener.call(this, type, listener, options);
93 + }
94 + const wrapped = (event: Event) => {
95 + const data = (event as MessageEvent).data;
96 + if (typeof data !== "string") return listener.call(this, event);
97 + const parsed = JSON.parse(data);
98 + const messages = [parsed].flat();
99 + const isConfig = (message: { requestId?: unknown }) =>
100 + tags.get(String(message.requestId)) === "subscribeServerConfig";
101 + for (const message of messages) {
102 + if (
103 + message?._tag === "Exit" &&
104 + tags.get(String(message.requestId)) === "server.updateSettings"
105 + ) {
106 + hold.saveReplies.add(String(message.requestId));
107 + }
108 + }
109 + const deliver = (part: ReadonlyArray<unknown>) =>
110 + listener.call(
111 + this,
112 + new MessageEvent("message", {
113 + data: JSON.stringify(Array.isArray(parsed) ? part : part[0]),
114 + }),
115 + );
116 + const held = hold.active ? messages.filter(isConfig) : [];
117 + const passed = messages.filter((message) => !held.includes(message));
118 + if (held.length > 0) hold.held.push(() => deliver(held));
119 + if (passed.length > 0) deliver(passed);
120 + };
121 + return addEventListener.call(this, type, wrapped, options);
122 + } as typeof WebSocket.prototype.addEventListener;
123 + });
124 + const hold = (active: boolean) =>
125 + browser.evaluate((active) => {
126 + const hold = window.configHold!;
127 + hold.active = active;
128 + if (!active) for (const deliver of hold.held.splice(0)) deliver();
129 + return null;
130 + }, active);
131 + const saveReplies = () => browser.evaluate(() => window.configHold!.saveReplies.size);
132 + const rename = async (slug: string, name: string) => {
133 + await screen.getByRole("button", `Edit ${slug}`, { exact: true }).tap();
134 + await screen.getByRole("textbox", "Display name", { exact: true }).fill(name);
135 + await screen.getByRole("button", "Save", { exact: true }).tap();
136 + };
137 +
138 + const settings = await runPromise(settingsFile);
139 + try {
140 + await runPromise(settings.seed);
141 + await t3.open("/settings/providers?instanceId=codex");
142 + await expect(screen.getByRole("button", "Edit model-a", { exact: true })).toBeVisible();
143 +
144 + const before = await saveReplies();
145 + await hold(true);
146 + await rename("model-a", "Entered A");
147 + await expect.poll(saveReplies).toBe(before + 1);
148 + await rename("model-b", "Entered B");
149 + await expect.poll(saveReplies).toBe(before + 2);
150 + await hold(false);
151 +
152 + await expect.poll(() => runPromise(settings.savedNames)).toContain("Entered B");
153 + await app.screenshot("after-both-saves");
154 + expect(await runPromise(settings.savedNames)).toEqual(["Entered A", "Entered B"]);
155 + } finally {
156 + await runPromise(settings.restore);
157 + }
158 + },
159 +);