Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(models): 同步预置写成功之后才收起确认框,被拒的合并退回原样(条目 7.11)

lmliheng24570540643b
diff --git a/changelog/unreleased/2026-10-19-models-sync-order.md b/changelog/unreleased/2026-10-19-models-sync-order.md
new file mode 100644
index 00000000..ade211c5
--- /dev/null
+++ b/changelog/unreleased/2026-10-19-models-sync-order.md
@@ -0,0 +1,37 @@
+# Preset sync: the confirmation closes only after the write lands
+
+- **Date:** 2026-10-19
+- **Type:** fix
+- **Scope:** `web`
+- **Breaking:** no
+
+[中文版](2026-10-19-models-sync-order.zh.md)
+
+The Models library's "update now" opens a confirmation that names every preset the sync would
+write — a sync rewrites each entry's context window, pricing, protocol and vision flag from the
+built-in catalog, so that sentence is the only warning before the click. It used to close as
+soon as the request returned, **whether the write had succeeded or not**. Worse, the refused
+merge also stayed in the page's table (`persist` keeps attempted rows so a field edit the user
+is still holding is not lost), so the retry computed an empty delta from a table that only
+*looked* synced: it answered "presets are already up to date" **without sending a request**,
+while the notice above kept naming every entry the server still lacked. Only a reload got out
+of that state.
+
+Now the merge is written before the confirmation is dismissed, and a refused one puts the
+table back as it was.
+
+## Details
+
+- `syncPresets` returns whether the table in hand now matches what the catalog holds — `true`
+ for the no-op too (a toast, no request).
+- A refused write restores the table from before the merge and re-probes the notice, so the
+ notice names exactly what the server still lacks.
+- The confirmation closes only when `syncPresets` answers `true`. On a refusal the sentence and
+ the list of refs stay verbatim, the error toast shows the server's own words, and the retry is
+ that same confirmation.
+- The toolbar button, the model card and the config dialog run the same action without a
+ confirmation; they now roll the table back on failure too.
+
+## Compatibility
+
+- No API, wire-format or dependency change: this is only the order of writing and dismissing.
diff --git a/changelog/unreleased/2026-10-19-models-sync-order.zh.md b/changelog/unreleased/2026-10-19-models-sync-order.zh.md
new file mode 100644
index 00000000..0b397479
--- /dev/null
+++ b/changelog/unreleased/2026-10-19-models-sync-order.zh.md
@@ -0,0 +1,28 @@
+# 同步预置:写成功才收起确认框,被拒的合并退回原样
+
+- **Date:** 2026-10-19
+- **Type:** fix
+- **Scope:** `web`
+- **Breaking:** no
+
+[English](2026-10-19-models-sync-order.md)
+
+模型库的「现在升级」会先开一个确认框,列出这次同步将写入的每一条预置 —— 同步会按内置目录重写
+每条的上下文、定价、协议与视觉标志,所以这句话是点击之前唯一的说明。而它此前在请求返回时就关闭,
+**不管写没写成功**;更难受的是,失败的那次合并仍留在页面的表里(`persist` 保留尝试中的表,是为了
+让正在填写的字段编辑不丢),于是重试从一张「看起来已经同步过」的表算出空差量,回一句
+「预置模型已是最新」、**一个请求都不发**,而上方提醒还列着服务器仍然缺的每一条。只有刷新页面才出得来。
+
+现在同步把合并结果写成功之后才收起确认框,被拒时把表退回写入前的样子。
+
+## 细节
+
+- `syncPresets` 返回「这张表现在是否已经是目录的样子」:无可同步时也是真(只提示、不发请求)。
+- 写入被拒时先用写入前的表恢复,再把提醒探针重读一次,因此提醒列出的正是服务器仍然缺的条目。
+- 确认框只在 `syncPresets` 回真时关闭;被拒时句子与它列的 refs 原样留着,错误提示照旧显示服务端
+ 那句话,重试就是同一张确认框。
+- 工具栏按钮、模型卡与配置对话框里的同步入口行为不变(它们本来就没有确认框),失败时同样把表退回。
+
+## 兼容性
+
+- 没有接口、传输格式或依赖变化;只改了页面里写入与收起的先后。
diff --git a/packages/web/e2e/models-sync-order.spec.mjs b/packages/web/e2e/models-sync-order.spec.mjs
new file mode 100644
index 00000000..794d8072
--- /dev/null
+++ b/packages/web/e2e/models-sync-order.spec.mjs
@@ -0,0 +1,179 @@
+/**
+ * Regression: "Sync presets" writes the merge before the confirmation that started it is
+ * dismissed — and a refused write takes the merge back off the screen.
+ *
+ * The confirmation (the Models page's notice → 「现在升级」) lists every entry the sync would
+ * write, because a sync rewrites the catalog-owned fields of each one. It used to close on the
+ * way out of the request, success or not. Two things followed from that, and the second is the
+ * one that trapped the user:
+ *
+ * 1. The refusal kept nothing on screen: the sentence, the list of refs and the reason it named
+ * were gone with the dialog, and the error toast was the only trace.
+ * 2. `persist` keeps the attempted rows for a field edit the user is still holding, so the
+ * refused merge stayed in the page's table. The retry then computed an empty delta from a
+ * table that only *looked* synced: it answered 「预置模型已是最新」 **without sending a
+ * request**, while the notice above kept naming every entry the server still lacked. Only a
+ * reload got out of it. (Measured on the pre-fix build: one refused PUT, then a retry with
+ * zero PUTs and the server's table unchanged.)
+ *
+ * The two tests drive the same refusal and then the retry, with the delta made through the API:
+ * one catalog entry is removed from the Project's table, so the notice has something real to
+ * name. Counts asserted here come from the responses the page itself receives and from the
+ * Project's own table read again, never from a fixed number in this file.
+ */
+import { test, expect } from "@playwright/test";
+import { provisionAndLogin } from "./auth.mjs";
+
+const BASE = process.env.BASE_URL;
+const P = "password123";
+
+/** A catalog entry a Project can be made to lack; unique enough that a search names only it. */
+const TARGET = { provider: "openrouter", modelId: "z-ai/glm-5.3-flash" };
+
+/** The table entry a `ModelInfo` came from: every field the response reports, sent back as-is. */
+function toEntry(m) {
+ const entry = { provider: m.provider, modelId: m.modelId };
+ if (m.displayName !== undefined) entry.displayName = m.displayName;
+ if (m.contextWindow !== undefined) entry.contextWindow = m.contextWindow;
+ if (m.clientType !== undefined) entry.clientType = m.clientType;
+ if (m.vision !== undefined) entry.vision = m.vision;
+ if (m.maxTokens !== undefined) entry.maxTokens = m.maxTokens;
+ if (m.fastMode !== undefined) entry.fastMode = m.fastMode;
+ if (m.pricing !== undefined) entry.pricing = m.pricing;
+ if (m.credential?.baseUrl !== undefined) entry.baseUrl = m.credential.baseUrl;
+ return entry;
+}
+
+/**
+ * Provision the user and make sure their Project lacks exactly one catalog entry (the second
+ * run on the same data root finds it already synced and leaves the table alone).
+ */
+async function projectMissingOnePreset(page, userId) {
+ await provisionAndLogin(page.request, userId, P);
+ const { projects } = await (await page.request.get(`${BASE}/api/projects`)).json();
+ const projectId = projects[0].projectId;
+ const read = async () =>
+ (await page.request.get(`${BASE}/api/projects/${projectId}/models`)).json();
+ const has = (table) =>
+ table.models.some((m) => m.provider === TARGET.provider && m.modelId === TARGET.modelId);
+
+ let table = await read();
+ if (has(table)) {
+ const put = await page.request.put(`${BASE}/api/projects/${projectId}/models`, {
+ data: {
+ defaultModel: table.defaultModel,
+ models: table.models
+ .filter((m) => !(m.provider === TARGET.provider && m.modelId === TARGET.modelId))
+ .map(toEntry),
+ },
+ });
+ expect(put.ok(), "remove one preset through the table PUT").toBeTruthy();
+ table = await read();
+ }
+ expect(has(table), "the Project lacks the target entry").toBe(false);
+ return { projectId, read };
+}
+
+/** Refuse every table write with a code the app has no local copy for, so the server's words show. */
+async function refuseWrites(page, seen) {
+ await page.route("**/api/projects/*/models", async (route) => {
+ if (route.request().method() !== "PUT") return route.fallback();
+ seen.push(route.request().url());
+ await route.fulfill({
+ status: 500,
+ contentType: "application/json",
+ body: JSON.stringify({
+ error: { code: "models_store_closed", message: "models store is closed" },
+ }),
+ });
+ });
+}
+
+test("a refused preset sync keeps its dialog, its sentence and the table the server has", async ({
+ page,
+}) => {
+ const { read } = await projectMissingOnePreset(page, "r88refuse");
+ const before = await read();
+
+ await page.goto("/models");
+ const notice = page.getByText(/检测到变更:1 个新增/);
+ await expect(notice).toBeVisible();
+ await page.getByRole("button", { name: "现在升级" }).click();
+ const dialog = page.getByRole("dialog");
+ await expect(dialog).toBeVisible();
+ const sentence = await dialog.innerText();
+ expect(sentence).toContain(`${TARGET.provider}/${TARGET.modelId}`);
+
+ const puts = [];
+ await refuseWrites(page, puts);
+ await dialog.getByRole("button", { name: "同步预置" }).click();
+
+ // The write was refused, and the dialog stays exactly as it was — same sentence, same list.
+ await expect(page.getByText("models store is closed")).toBeVisible();
+ expect(puts).toHaveLength(1);
+ await expect(dialog).toBeVisible();
+ expect(await dialog.innerText()).toBe(sentence);
+
+ // The server still holds the old table, and the page's own table agrees with it: the entry
+ // the merge would have added is not there to be found, although the notice still names it.
+ const after = await read();
+ expect(after.models.length).toBe(before.models.length);
+ expect(
+ after.models.some((m) => m.provider === TARGET.provider && m.modelId === TARGET.modelId),
+ ).toBe(false);
+ const search = page.getByPlaceholder(/搜索模型/);
+ await search.fill(TARGET.modelId);
+ await expect(page.getByText("没有匹配的模型")).toBeVisible();
+ await search.fill("");
+
+ // Closing the dialog leaves the notice where it was — nothing was decided by the refusal.
+ await dialog.getByRole("button", { name: "取消" }).click();
+ await expect(dialog).toHaveCount(0);
+ await expect(notice).toBeVisible();
+});
+
+test("the retry after a refused sync writes the merge, and only a written one closes the dialog", async ({
+ page,
+}) => {
+ const { read } = await projectMissingOnePreset(page, "r88retry");
+
+ await page.goto("/models");
+ await expect(page.getByText(/检测到变更:1 个新增/)).toBeVisible();
+
+ const puts = [];
+ page.on("request", (r) => {
+ if (r.method() === "PUT" && r.url().includes("/models")) puts.push(r.url());
+ });
+ let refuse = true;
+ await page.route("**/api/projects/*/models", async (route) => {
+ if (route.request().method() !== "PUT" || !refuse) return route.fallback();
+ await route.fulfill({
+ status: 500,
+ contentType: "application/json",
+ body: JSON.stringify({
+ error: { code: "models_store_closed", message: "models store is closed" },
+ }),
+ });
+ });
+
+ await page.getByRole("button", { name: "现在升级" }).click();
+ const dialog = page.getByRole("dialog");
+ await dialog.getByRole("button", { name: "同步预置" }).click();
+ await expect(page.getByText("models store is closed")).toBeVisible();
+ await expect(dialog).toBeVisible();
+ expect(puts).toHaveLength(1);
+
+ // The retry is the same confirmation, and it really writes: before the fix the merged rows
+ // were still in the table, the delta came out empty and this click sent nothing at all.
+ refuse = false;
+ await dialog.getByRole("button", { name: "同步预置" }).click();
+ await expect(dialog).toHaveCount(0);
+ expect(puts).toHaveLength(2);
+ await expect(page.getByText(/预置模型已同步:新增 1/)).toBeVisible();
+ await expect(page.getByText(/个预置模型可同步|检测到变更:/)).toHaveCount(0);
+
+ const after = await read();
+ expect(
+ after.models.some((m) => m.provider === TARGET.provider && m.modelId === TARGET.modelId),
+ ).toBe(true);
+});
diff --git a/packages/web/src/features/models/models-page.tsx b/packages/web/src/features/models/models-page.tsx
index 86b598f3..694a48cc 100644
--- a/packages/web/src/features/models/models-page.tsx
+++ b/packages/web/src/features/models/models-page.tsx
@@ -955,9 +955,12 @@ export function ModelsPage() {
* on the facts it tracks about a model, while base URLs, keys and output caps stay as this
* install has them, and local additions are untouched — see catalog-sync.ts). No-op with a
* toast when everything is already up to date.
+ *
+ * Returns whether the table in hand now matches what the catalog holds — `true` for the no-op
+ * too. The notice's confirmation keeps itself open until then.
*/
- const syncPresets = async () => {
- if (!rows) return;
+ const syncPresets = async (): Promise<boolean> => {
+ if (!rows) return false;
const merged = syncRowsWithCatalog(rows);
if (merged.added === 0 && merged.updated === 0) {
toastInfo(S.models.syncUpToDate);
@@ -966,18 +969,27 @@ export function ModelsPage() {
// of. Without it the notice keeps offering a button that can only answer "already
// up to date" — a loop the user gets out of only by dismissing or reloading.
refreshProjectTodos(projectId);
- return;
+ return true;
}
- await persist(
+ // A refused write takes the merge back off the screen. `persist` keeps the attempted rows
+ // for a field edit the user is still holding, but a preset sync is a batch the catalog
+ // describes, and those rows are exactly what the server declined: left on screen they make
+ // the retry compute an empty delta, so it answers "already up to date" without sending
+ // anything while the notice still names every missing entry (measured on a refused write:
+ // the retry produced no PUT at all and the server kept its old table).
+ const before = rows;
+ const saved = await persist(
merged.rows,
defaultModel,
visionModel,
S.models.syncDone(merged.added, merged.updated),
);
+ if (!saved) setRows(before);
// The Models nav badge is gated on the SAVED table, which this page holds only as row
// state: re-probe it so the dot goes down on the server's answer rather than on the
// assumption that the write covered everything the catalog offered.
refreshProjectTodos(projectId);
+ return saved;
};
const syncPlatformModels = async () => {
@@ -1712,10 +1724,15 @@ export function ModelsPage() {
onClose={() => setSyncConfirmOpen(false)}
onConfirm={() => {
setSyncing(true);
- void syncPresets().finally(() => {
- setSyncing(false);
- setSyncConfirmOpen(false);
- });
+ // Only a written merge dismisses the dialog. A refused one keeps the sentence and
+ // the list of refs it names, so the retry is this same confirmation — the table
+ // behind it has been put back, and finding the notice again to start over is work
+ // the failure does not justify.
+ void syncPresets()
+ .then((saved) => {
+ if (saved) setSyncConfirmOpen(false);
+ })
+ .finally(() => setSyncing(false));
}}
>
<div className="space-y-3">