A local-first multi-agent app development platform, built on PenguinHarness
fix(web): 内置浏览器清除数据被拒时,原因留在卡片里
lmliheng8db188dde6d5
diff --git a/changelog/unreleased/2026-10-27-builtin-clear-refusal.md b/changelog/unreleased/2026-10-27-builtin-clear-refusal.mdnew file mode 100644index 00000000..6ece3e7f--- /dev/null+++ b/changelog/unreleased/2026-10-27-builtin-clear-refusal.md@@ -0,0 +1,44 @@+# A refused clear of browsing data keeps its reason in the card++- **Date:** 2026-10-27+- **Type:** fix+- **Scope:** `web`, `docs`+- **Breaking:** no++[中文](2026-10-27-builtin-clear-refusal.zh.md)++The built-in browser's toolbar has a **Clear browsing data…** card: tick cookies, cache, site+storage and history, then press **Clear**. The card always stayed open on a failure — it closes+only once the clearing lands — but the reason never stayed with it: a toast dropped in, dismissed+itself seconds later, and the card went on saying only what it was asking, never why the last+attempt did nothing. That button is the one way back to the same selection, and nothing beside it+said what happened.++The refusal now renders inside the card, under the card's own sentence, next to the button that+retries it; the ticked kinds (and the unticked one) stay exactly as they were, so the retry asks+for the same thing.++## Details++- `packages/web/src/features/builtin-browser/clear-data-dialog.tsx`: a new `refusal` state feeds+ `ConfirmModal`'s `refusal` slot (the ui package's own slot, shared by every confirmation in the+ app); the `catch` no longer calls `toastError` and hands+ `S.builtinBrowser.clearFailed(apiErrorText(err))` to the card instead. Each press clears the+ previous reason first (an in-flight attempt owns the card), and opening the card clears it too,+ so a reopened card never shows the last attempt's words. The success path is unchanged: the same+ `toastSuccess`, and the card closes as before.+- `packages/web/e2e/builtin-browser-clear-refusal.spec.mjs` (new, one test): the browser only runs+ inside the desktop shell, so the spec stubs the one thing the app asks the shell for (a+ `webview` in the custom-element registry) and three browser routes inside the page; the server,+ the sign-in and the Project are real. A 500 (`clear_data_refused`, a code nothing translates, so+ the sentence on screen can only be the probe's) is refused once: the card stays, the reason is+ inside it, the unticked "history" is still unticked, the other three are still ticked — and the+ request body that went out carries no `history`. Allowed afterwards, the same button closes the+ card, and the retry asked for the same kinds the refused attempt did.+- Docs: the "Clear browsing data" section of `packages/docs/content/builtin-browser.{zh,en}.md`.++## Compatibility++- No dependency, API, on-disk or protocol change: the same `POST /api/builtin-browser/clear-data`+ (and `DELETE …/history`), with the server untouched; the copy reuses the existing `clearFailed`,+ so there is no new string.diff --git a/changelog/unreleased/2026-10-27-builtin-clear-refusal.zh.md b/changelog/unreleased/2026-10-27-builtin-clear-refusal.zh.mdnew file mode 100644index 00000000..0b5a06f0--- /dev/null+++ b/changelog/unreleased/2026-10-27-builtin-clear-refusal.zh.md@@ -0,0 +1,36 @@+# 内置浏览器「清除浏览数据」被拒时,原因留在卡片里++- **Date:** 2026-10-27+- **Type:** fix+- **Scope:** `web`, `docs`+- **Breaking:** no++[English](2026-10-27-builtin-clear-refusal.md)++内置浏览器的工具栏里有一个**清除浏览数据…**的确认卡:勾选要清掉的 Cookie、缓存、网站存储与历史+记录,再按**清除**。这张卡片本来就会在失败时留着(只有落地才收),但它的原因从不留在卡片里 ——+一条提示从天而降、几秒后自己走掉,而卡片照旧停在那儿,只说「选择要删除的数据」,不说这一次为什么+没删成。卡片上的重试是回到同一份勾选的唯一路径,可它旁边一个字都没有。++现在拒绝的话画在这张卡片里、就在卡片自己那句话下面,按的仍然是同一个**清除**按钮;勾好的种类+(连同取消勾选的那一个)原样留着,重试用的是同一份选择。++## 细节++- `packages/web/src/features/builtin-browser/clear-data-dialog.tsx`:新增 `refusal` 状态,接上+ `ConfirmModal` 的 `refusal` 槽(ui 包自带的那个槽,全 App 的确认框共用);`catch` 不再+ `toastError`,改成把 `S.builtinBrowser.clearFailed(apiErrorText(err))` 交给卡片。每次点**清除**+ 都先清掉上一次的原因(在飞的原因属于正在跑的这一次),每次开框也清一次 —— 重开的卡片不带上一次的+ 话。成功路径没变:同一句 `toastSuccess`、同样收框。+- `packages/web/e2e/builtin-browser-clear-refusal.spec.mjs`(新,1 条):内置浏览器只在桌面壳里跑,+ 所以用例把 App 唯一问桌面壳的那件事(自定义元素表里的 `webview`)与三条浏览器路由在页面里打桩,+ 服务端、登录与 Project 都是真的。用 500(`clear_data_refused`,一个没有翻译的码,屏幕上那句话只+ 可能来自探针)拒一次:卡片留着、原因在卡片里、取消勾选的「历史记录」还是没勾、其余三个还勾着,+ 并且**发出去的那个请求体里没有 `history`**;放行后同一个按钮再点一次,卡片收起,重试发出的种类与+ 被拒的那一次相同。+- 文档:`packages/docs/content/builtin-browser.{zh,en}.md` 的「清除浏览数据」一节加一句。++## 兼容性++- 不改依赖、不改接口、不改磁盘格式与协议:仍是同一个 `POST /api/builtin-browser/clear-data`(与+ `DELETE …/history`),服务端一行没动;文案沿用已有的 `clearFailed`,没有新字符串。diff --git a/packages/docs/content/builtin-browser.en.md b/packages/docs/content/builtin-browser.en.mdindex 8b60b513..6b06e8ec 100644--- a/packages/docs/content/builtin-browser.en.md+++ b/packages/docs/content/builtin-browser.en.md@@ -98,6 +98,8 @@ From a shell, `penguin browser import --list` lists the profiles and `penguin brSelect **Clear browsing data…** in the toolbar's menu and choose what to remove: cookies and sign-ins, cached files, site storage, or the browsing history. This affects only the built-in browser.+If the server refuses, the card stays: your selection stays with it, and the refusal is written inside the card, next to the button that retries it.+## Drive it from the command line`penguin browser` drives the tabs from a shell, and it is how agents use the browser. The preinstalled `browser-automation` plugin teaches them the loop: open a page, read it, act with JavaScript, and check what changed.diff --git a/packages/docs/content/builtin-browser.zh.md b/packages/docs/content/builtin-browser.zh.mdindex f3c53cc1..f3a5b5c0 100644--- a/packages/docs/content/builtin-browser.zh.md+++ b/packages/docs/content/builtin-browser.zh.md@@ -98,6 +98,8 @@ Agent 从不输入你的密码。Agent 需要的页面要求登录时,`browser在工具栏的菜单中选择**清除浏览数据…**,再选择要清除的内容:Cookie 与登录状态、缓存文件、网站存储或浏览历史记录。这只影响内置浏览器。+服务端如果拒绝,这张卡片不会走开:勾好的种类原样留着,拒绝的原话就写在这张卡片里、紧挨着重试它的那个按钮。+## 在命令行中驱动`penguin browser` 在 shell 中驱动这些标签页,Agent 也正是这样使用浏览器的。预装的 `browser-automation` 插件教给 Agent 一个循环:打开页面、读取页面、用 JavaScript 操作,再检查页面发生了什么变化。diff --git a/packages/web/e2e/builtin-browser-clear-refusal.spec.mjs b/packages/web/e2e/builtin-browser-clear-refusal.spec.mjsnew file mode 100644index 00000000..7eaafafe--- /dev/null+++ b/packages/web/e2e/builtin-browser-clear-refusal.spec.mjs@@ -0,0 +1,144 @@+/**+ * Regression: the built-in browser's "clear browsing data" card keeps the server's refusal inside+ * itself (`ConfirmModal`'s `refusal` slot), and its ticked kinds with it.+ *+ * The card always stayed open on a failure — what it lost was the reason: the words went to a toast+ * that dismissed itself while the card that had asked the question stood there saying nothing, and+ * the retry the card offers is the only way back to the same selection. The reason now renders+ * under the card's own sentence, next to the button that retries it.+ *+ * The panel normally needs the desktop shell, so this spec stubs the one thing the app asks it for+ * (the `<webview>` element in the custom-element registry) and the browser routes inside the page;+ * the server, the sign-in and the Project are real. The refusal carries a code nothing translates+ * (`clear_data_refused`), so the sentence on screen can only be the probe's.+ */+import { test, expect } from "@playwright/test";+import { ADMIN_ID, ADMIN_PASSWORD, login } from "./auth.mjs";++const BASE = process.env.BASE_URL;+const MOCK = process.env.MOCK_URL;++/** A code no client translation covers, so its message reaches the card verbatim. */+const REFUSAL = { code: "clear_data_refused", message: "clear data refused by the probe" };+const SHOWN = `清除失败:${REFUSAL.message}`;++const TAB = {+ id: 1,+ url: "https://example.com/",+ title: "示例站点",+ loading: false,+ canGoBack: false,+ canGoForward: false,+};++/** What the desktop shell provides and a plain browser does not: a `webview` in the registry. */+const STUB_WEBVIEW = () => {+ class StubView extends HTMLElement {+ loadURL() {+ return Promise.resolve();+ }+ reload() {}+ stop() {}+ goBack() {}+ goForward() {}+ focus() {}+ openDevTools() {}+ }+ const get = CustomElementRegistry.prototype.get;+ CustomElementRegistry.prototype.get = function (name) {+ return name === "webview" ? StubView : get.call(this, name);+ };+};++test("built-in browser: a refused clear keeps the card and its reason, and retries the same kinds", async ({+ page,+}) => {+ // The browser's routes are admin-only, so this runs as the seeded administrator.+ await login(page.request, ADMIN_ID, ADMIN_PASSWORD);++ const projects = await (await page.request.get(`${BASE}/api/projects`)).json();+ const projectId = projects.projects[0].projectId;+ // A Project needs a model, or the onboarding overlay covers the toolbar.+ const models = await page.request.put(`${BASE}/api/projects/${projectId}/models`, {+ data: {+ defaultModel: { provider: "custom", modelId: "claude-4-8" },+ models: [+ {+ provider: "custom",+ modelId: "claude-4-8",+ apiKey: "sk-mock",+ baseUrl: MOCK,+ contextWindow: 200000,+ },+ ],+ },+ });+ expect(models.ok(), "put models").toBeTruthy();++ // The dock's two toggles live on a conversation's toolbar, not on the new-chat draft.+ const created = await page.request.post(+ `${BASE}/api/projects/${projectId}/agents/default_agent/sessions`,+ { data: { provider: "custom", modelId: "claude-4-8" } },+ );+ expect(created.ok(), "create session").toBeTruthy();+ const sessionId = (await created.json()).session.sessionId;++ await page.addInitScript(STUB_WEBVIEW);+ await page.route("**/api/builtin-browser/status", (route) =>+ route.fulfill({+ status: 200,+ contentType: "application/json",+ body: JSON.stringify({ available: true, tabs: [TAB], activeTabId: TAB.id }),+ }),+ );+ await page.route("**/api/builtin-browser/settings", (route) =>+ route.fulfill({+ status: 200,+ contentType: "application/json",+ body: JSON.stringify({ homepage: null }),+ }),+ );++ /** Every clear-data request's body, so the retry can be checked against the first attempt. */+ const clears = [];+ let refuse = true;+ await page.route("**/api/builtin-browser/clear-data", (route) => {+ const body = route.request().postDataJSON();+ clears.push(body);+ if (!refuse) return route.fulfill({ status: 200, contentType: "application/json", body: "{}" });+ return route.fulfill({+ status: 500,+ contentType: "application/json",+ body: JSON.stringify({ error: REFUSAL }),+ });+ });++ await page.goto(`${BASE}/chat/${sessionId}`);+ await page.getByTestId("dock-toggle-right").click();+ const dock = page.locator('[data-testid="dock"][data-position="right"]');+ await dock.getByTestId("dock-pick-builtin-browser").click();+ await dock.getByRole("button", { name: "更多", exact: true }).click();+ await page.getByRole("menuitem", { name: "清除浏览数据…" }).click();++ const dialog = page.getByRole("dialog");+ await expect(dialog).toBeVisible();+ // Untick one kind: the refusal has to leave this selection exactly as it is.+ await dialog.getByRole("checkbox", { name: "历史记录" }).click();++ await dialog.getByRole("button", { name: "清除", exact: true }).click();+ await expect(dialog).toBeVisible();+ await expect(dialog.getByRole("alert")).toContainText(SHOWN);+ await expect(dialog.getByRole("checkbox", { name: "历史记录" })).not.toBeChecked();+ for (const kind of ["Cookie 与登录状态", "缓存的图片和文件", "网站存储的数据"]) {+ await expect(dialog.getByRole("checkbox", { name: kind })).toBeChecked();+ }+ expect(clears.at(-1).storages).not.toContain("history");++ // The same button, allowed this time: the card goes, and so does the reason.+ refuse = false;+ await dialog.getByRole("button", { name: "清除", exact: true }).click();+ await expect(dialog).toBeHidden();+ // The retry asked for the same kinds the refused attempt did.+ expect(clears.at(-1).storages).not.toContain("history");+ expect(clears.length).toBe(2);+});diff --git a/packages/web/src/features/builtin-browser/clear-data-dialog.tsx b/packages/web/src/features/builtin-browser/clear-data-dialog.tsxindex b0965689..ce3a141e 100644--- a/packages/web/src/features/builtin-browser/clear-data-dialog.tsx+++ b/packages/web/src/features/builtin-browser/clear-data-dialog.tsx@@ -2,9 +2,14 @@* Clear browsing data: which of the built-in browser's stores to empty — sign-ins, the cache,* site storage, history — in the app's compact confirmation card. Every kind starts checked* on each opening; the confirm stays disabled while none is.+ *+ * The card closes only once the clearing landed. A failure leaves it standing with the server's+ * words inside it (`ConfirmModal`'s `refusal` slot): the toast that used to carry the reason+ * dismissed itself while this card — the one thing that says what was asked for — stayed on+ * screen, and the ticked kinds go with the card, so the retry has to be this same card.*/import { useEffect, useState } from "react";-import { Checkbox, ConfirmModal, toastError, toastSuccess } from "@adelie/ui";+import { Checkbox, ConfirmModal, toastSuccess } from "@adelie/ui";import * as api from "../../api/endpoints";import type { BuiltinBrowserStorage } from "../../api/endpoints";import { apiErrorText } from "../../lib/api-error";@@ -30,9 +35,14 @@ function kindLabel(kind: ClearKind): string {export function ClearDataDialog({ open, onClose }: { open: boolean; onClose: () => void }) {const [chosen, setChosen] = useState<ReadonlySet<ClearKind>>(() => new Set(KINDS));const [busy, setBusy] = useState(false);+ /** The words of the last attempt's failure, inside this card; null while none failed. */+ const [refusal, setRefusal] = useState<string | null>(null);useEffect(() => {- if (open) setChosen(new Set(KINDS));+ if (!open) return;+ setChosen(new Set(KINDS));+ // A new question, not the last one: that sentence belonged to the previous attempt.+ setRefusal(null);}, [open]);const toggle = (kind: ClearKind, on: boolean) =>@@ -48,13 +58,16 @@ export function ClearDataDialog({ open, onClose }: { open: boolean; onClose: ()(kind): kind is BuiltinBrowserStorage => kind !== "history" && chosen.has(kind),);setBusy(true);+ // Retrying starts from a clean card: the reason belongs to the attempt that is running now.+ setRefusal(null);try {if (storages.length > 0) await api.clearBuiltinBrowserData(storages);if (chosen.has("history")) await api.clearBuiltinBrowserHistory();toastSuccess(S.builtinBrowser.clearDone);onClose();} catch (err) {- toastError(S.builtinBrowser.clearFailed(apiErrorText(err)));+ // The card stays (the ticked kinds stay with it); only the reason is new.+ setRefusal(S.builtinBrowser.clearFailed(apiErrorText(err)));} finally {setBusy(false);}@@ -70,6 +83,7 @@ export function ClearDataDialog({ open, onClose }: { open: boolean; onClose: ()cancelLabel={S.common.cancel}confirmDisabled={chosen.size === 0}busy={busy}+ refusal={refusal === null ? undefined : refusal}><p className="text-sm text-gray-600 dark:text-gray-300">{S.builtinBrowser.clearBody}</p><div className="mt-3 flex flex-col gap-1.5">