From e38c009a90094e9e0eafaf412cbc5106da08d0f3 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:31:36 -0600 Subject: [PATCH] fix(app): show a failed site cookie read and let it retry (#61857) --- .../app/component-tests/browser-pane.spec.ts | 13 ++ .../gui-plugins/src/browser/panel.fixture.tsx | 13 +- packages/gui-plugins/src/browser/panel.tsx | 122 +++++++++------ .../src/browser/site-machine.test.ts | 147 ++++++++++++++++++ .../gui-plugins/src/browser/site-machine.ts | 133 ++++++++++++++++ 5 files changed, 376 insertions(+), 52 deletions(-) create mode 100644 packages/gui-plugins/src/browser/site-machine.test.ts create mode 100644 packages/gui-plugins/src/browser/site-machine.ts diff --git a/packages/app/component-tests/browser-pane.spec.ts b/packages/app/component-tests/browser-pane.spec.ts index 58168ac5c57..9e4170bfd97 100644 --- a/packages/app/component-tests/browser-pane.spec.ts +++ b/packages/app/component-tests/browser-pane.spec.ts @@ -792,6 +792,19 @@ story("shows the page's cookies in the site information and clears them", async await expect(site).toContainText("0 cookies in use") }) +story("says a failed cookie read failed and reads again on retry", async ({ page }) => { + const root = page.getByTestId("browser-pane-fixture") + await root.getByRole("button", { name: "Fail next site read", exact: true }).click() + await root.getByRole("button", { name: "Site information", exact: true }).click() + const site = page.getByRole("dialog") + await expect(site).toContainText("Request failed") + const retry = site.getByRole("button", { name: "Try again", exact: true }) + await expect(retry).toBeEnabled() + await retry.click() + await expect(site).toContainText("3 cookies in use") + await expect(site.getByRole("button", { name: "Clear", exact: true })).toBeEnabled() +}) + story("zooms the page from the browser options in view and keeps its zoom in the address field", async ({ page }) => { const root = page.getByTestId("browser-pane-fixture") const options = root.getByRole("button", { name: "Browser options", exact: true }) diff --git a/packages/gui-plugins/src/browser/panel.fixture.tsx b/packages/gui-plugins/src/browser/panel.fixture.tsx index 0faf00b4362..587115423b7 100644 --- a/packages/gui-plugins/src/browser/panel.fixture.tsx +++ b/packages/gui-plugins/src/browser/panel.fixture.tsx @@ -103,6 +103,8 @@ type PaneFixtureState = { external: string[] /** Cookies the page's address can read. */ cookies: number + /** Main fails the next cookie read, as when the pane is suspended. */ + failSite: boolean selection: string } @@ -162,6 +164,7 @@ export function mountBrowserPane(input: PaneHost) { history: { visits: [] }, external: [], cookies: 3, + failSite: false, selection: "default", }) @@ -368,7 +371,14 @@ export function mountBrowserPane(input: PaneHost) { setStore("zoom", next) emit(store.session, { type: "page", tabID: value.tabID, zoom: next }) }, - site: async () => ({ cookies: store.cookies }), + site: async () => { + if (store.failSite) { + setStore("failSite", false) + throw new Error("browser.pane.unavailable") + } + + return { cookies: store.cookies } + }, // Main deletes the site's data and reloads the page. clearSite: async () => { setStore({ cookies: 0, generation: store.generation + 1 }) @@ -537,6 +547,7 @@ export function mountBrowserPane(input: PaneHost) { {(value) => } + diff --git a/packages/gui-plugins/src/browser/panel.tsx b/packages/gui-plugins/src/browser/panel.tsx index 06f0b8fcb42..54106ea100f 100644 --- a/packages/gui-plugins/src/browser/panel.tsx +++ b/packages/gui-plugins/src/browser/panel.tsx @@ -12,7 +12,7 @@ import { showToast } from "@opencode/ui/toast" import { Tooltip } from "@opencode/ui/tooltip" import { createEventListener } from "@solid-primitives/event-listener" import { createResizeObserver } from "@solid-primitives/resize-observer" -import { createMemo, For, on, onCleanup, Show, type Accessor, type JSX } from "solid-js" +import { createMemo, createSignal, For, on, onCleanup, Show, type Accessor, type JSX } from "solid-js" import { createStore } from "solid-js/store" import type { Browser } from "@opencode/plugin-browser/rpc" import { @@ -29,6 +29,15 @@ import { bare, completes, highlight, suggest, type Visit } from "./history" import type { Model } from "./model" import type { PaneElement } from "./ipc" import { PageIcon } from "./page-icon" +import { + initialSiteState, + SITE_CLEAR_TIMEOUT_MS, + SITE_LOAD_TIMEOUT_MS, + siteView, + transitionSite, + type SiteEffect, + type SiteEvent, +} from "./site-machine" type PaneState = { /** The address field's text while the user edits it, and the submitted address it keeps afterwards. */ @@ -1185,23 +1194,55 @@ function SiteInformation(props: { clear: () => Promise | undefined }) { const plugin = usePlugin() - - // `load` counts openings and clears, so a count for an earlier one never replaces a later one. - const [state, setState] = createStore<{ - open: boolean - pointer: boolean - cookies?: number - clearing: boolean - load: number - }>({ - open: false, - pointer: false, - clearing: false, - load: 0, - }) - + const [siteState, setSiteState] = createSignal(initialSiteState) + // Whether a pointer opened the popover, so closing it returns focus without a focus ring. + const [pointer, setPointer] = createSignal(false) + const view = createMemo(() => siteView(siteState())) + let timer: ReturnType | undefined let trigger: HTMLButtonElement | undefined + const dispatch = (event: SiteEvent) => { + const next = transitionSite(siteState(), event) + setSiteState(next.state) + next.effects.forEach(run) + } + + const run = (effect: SiteEffect) => { + if (effect.type === "notify-clear-failed") return showToast({ title: plugin.t("common.requestFailed") }) + const attempt = effect.attempt + const phase = effect.type === "load" ? "loading" : "clearing" + clearTimeout(timer) + timer = setTimeout( + () => dispatch({ type: "timeout", phase, attempt }), + effect.type === "load" ? SITE_LOAD_TIMEOUT_MS : SITE_CLEAR_TIMEOUT_MS, + ) + + if (effect.type === "load") { + const pending = props.site() + + if (!pending) return dispatch({ type: "load.failed", attempt }) + void pending.then( + (value) => dispatch({ type: "load.succeeded", attempt, cookies: value.cookies }), + () => dispatch({ type: "load.failed", attempt }), + ) + + return + } + + const pending = props.clear() + + if (!pending) return dispatch({ type: "clear.failed", attempt }) + void pending.then( + () => dispatch({ type: "clear.succeeded", attempt }), + () => dispatch({ type: "clear.failed", attempt }), + ) + } + + onCleanup(() => { + clearTimeout(timer) + dispatch({ type: "disposed" }) + }) + const kind = () => { const value = connection(props.url) @@ -1215,47 +1256,30 @@ function SiteInformation(props: { return !!value && value !== "file" } - const load = () => { - const run = state.load + 1 - setState({ load: run, cookies: undefined }) - void props.site()?.then( - (value) => { - if (state.load === run) setState("cookies", value.cookies) - }, - () => undefined, - ) - } + const cookieDetail = () => { + const current = view() - const clear = () => { - setState("clearing", true) - void (props.clear() ?? Promise.reject(new Error("browser.pane.unavailable"))) - .catch(() => showToast({ title: plugin.t("common.requestFailed") })) - .finally(() => { - setState("clearing", false) - load() - }) + if (current.failed) return plugin.t("common.requestFailed") + + return current.cookies === undefined ? undefined : plugin.plural("site.cookies.count", current.cookies) } return ( { - setState("open", open) - - if (open) load() - }} + onOpenChange={(open) => dispatch({ type: "open.changed", open })} > setState("pointer", true)} - onKeyDown={() => setState("pointer", false)} + onPointerDown={() => setPointer(true)} + onKeyDown={() => setPointer(false)} variant="ghost-muted" size="small" aria-label={plugin.t("site.info")} @@ -1273,7 +1297,7 @@ function SiteInformation(props: { }} // Focus returns to the trigger on close, which Chromium rings after Escape; a pointer opening keeps it unringed. onCloseAutoFocus={(event: Event) => { - if (!state.pointer) return + if (!pointer()) return event.preventDefault() const active = document.activeElement @@ -1301,18 +1325,14 @@ function SiteInformation(props: { )} - + + events.reduce((last, event) => transitionSite(last.state, event), { state, effects: [] }) + +// The popover opened and read 3 cookies with attempt 1. +const ready = () => + replay([ + { type: "open.changed", open: true }, + { type: "load.succeeded", attempt: 1, cookies: 3 }, + ]).state + +describe("a failed read", () => { + test("says so and offers a retry that reads again", () => { + const failed = replay([ + { type: "open.changed", open: true }, + { type: "load.failed", attempt: 1 }, + ]) + + expect(siteView(failed.state)).toMatchObject({ + open: true, + failed: true, + actionDisabled: false, + cookies: undefined, + }) + + const retried = transitionSite(failed.state, { type: "retry" }) + expect(retried.effects).toEqual([{ type: "load", attempt: 2 }]) + expect(siteView(retried.state).actionDisabled).toBe(true) + + const loaded = transitionSite(retried.state, { type: "load.succeeded", attempt: 2, cookies: 4 }) + expect(siteView(loaded.state)).toMatchObject({ failed: false, cookies: 4, actionDisabled: false }) + }) + + test("a read that never answers fails at its timeout", () => { + const result = replay([ + { type: "open.changed", open: true }, + { type: "timeout", phase: "loading", attempt: 1 }, + ]) + + expect(siteView(result.state).failed).toBe(true) + }) + + test("a read from an earlier opening is ignored", () => { + const reopened = replay([ + { type: "open.changed", open: true }, + { type: "open.changed", open: false }, + { type: "open.changed", open: true }, + ]).state + + const stale = transitionSite(reopened, { type: "load.succeeded", attempt: 1, cookies: 9 }) + const late = transitionSite(reopened, { type: "timeout", phase: "loading", attempt: 1 }) + + expect(stale.state).toBe(reopened) + expect(late.state).toBe(reopened) + expect(transitionSite(reopened, { type: "load.succeeded", attempt: 2, cookies: 2 }).state).toEqual({ + status: "ready", + attempt: 2, + cookies: 2, + }) + }) +}) + +describe("clearing", () => { + test("hides the old count and disables the button until the next read", () => { + const clearing = transitionSite(ready(), { type: "clear.clicked" }) + + expect(clearing.effects).toEqual([{ type: "clear", attempt: 2 }]) + expect(siteView(clearing.state)).toMatchObject({ open: true, cookies: undefined, actionDisabled: true }) + expect(transitionSite(clearing.state, { type: "clear.clicked" }).state).toBe(clearing.state) + + const cleared = transitionSite(clearing.state, { type: "clear.succeeded", attempt: 2 }) + expect(cleared.effects).toEqual([{ type: "load", attempt: 3 }]) + }) + + test("a failure tells the user and reads the count again", () => { + const clearing = transitionSite(ready(), { type: "clear.clicked" }).state + + expect(transitionSite(clearing, { type: "clear.failed", attempt: 2 }).effects).toEqual([ + { type: "notify-clear-failed" }, + { type: "load", attempt: 3 }, + ]) + expect(transitionSite(clearing, { type: "timeout", phase: "clearing", attempt: 2 }).effects).toEqual([ + { type: "notify-clear-failed" }, + { type: "load", attempt: 3 }, + ]) + }) + + test("closing the popover keeps the clear, and it settles without a read", () => { + const closed = replay([{ type: "clear.clicked" }, { type: "open.changed", open: false }], ready()) + expect(siteView(closed.state).open).toBe(false) + + const settled = transitionSite(closed.state, { type: "clear.succeeded", attempt: 2 }) + expect(settled.state).toEqual({ status: "closed", attempt: 2 }) + expect(settled.effects).toEqual([]) + }) + + test("reopening during the clear waits for it, then reads", () => { + const reopened = replay( + [{ type: "clear.clicked" }, { type: "open.changed", open: false }, { type: "open.changed", open: true }], + ready(), + ) + + expect(reopened.effects).toEqual([]) + expect(siteView(reopened.state)).toMatchObject({ open: true, actionDisabled: true }) + + expect(transitionSite(reopened.state, { type: "clear.succeeded", attempt: 2 }).effects).toEqual([ + { type: "load", attempt: 3 }, + ]) + }) + + test("a clear that settles after the popover unmounted does nothing", () => { + const disposed = replay([{ type: "clear.clicked" }, { type: "disposed" }], ready()).state + + const events: SiteEvent[] = [ + { type: "clear.failed", attempt: 2 }, + { type: "clear.succeeded", attempt: 2 }, + { type: "timeout", phase: "clearing", attempt: 2 }, + { type: "open.changed", open: true }, + ] + + events.forEach((event) => { + const next = transitionSite(disposed, event) + expect(next.state).toBe(disposed) + expect(next.effects).toEqual([]) + }) + }) +}) + +test("closing while reading drops the read", () => { + const closed = replay([ + { type: "open.changed", open: true }, + { type: "open.changed", open: false }, + ]).state + + expect(closed).toEqual({ status: "closed", attempt: 1 }) + expect(transitionSite(closed, { type: "load.succeeded", attempt: 1, cookies: 3 }).state).toBe(closed) +}) diff --git a/packages/gui-plugins/src/browser/site-machine.ts b/packages/gui-plugins/src/browser/site-machine.ts new file mode 100644 index 00000000000..daa5fba52d7 --- /dev/null +++ b/packages/gui-plugins/src/browser/site-machine.ts @@ -0,0 +1,133 @@ +// The site information popover's cookie count and clear. Each read and clear takes the next attempt number, so an +// answer whose attempt the state no longer holds is stale. + +/** A read that has not answered by then fails, so the popover offers a retry rather than an endless blank. */ +export const SITE_LOAD_TIMEOUT_MS = 5_000 + +/** Main waits up to 30 s for the page to reload after a clear; past this the clear counts as failed. */ +export const SITE_CLEAR_TIMEOUT_MS = 35_000 + +export type SiteState = + /** `attempt` is the last one handed out. */ + | { readonly status: "closed"; readonly attempt: number } + | { readonly status: "loading"; readonly attempt: number } + | { readonly status: "ready"; readonly attempt: number; readonly cookies: number } + | { readonly status: "failed"; readonly attempt: number } + /** A clear outlives the popover closing, so a reopen waits for it and its failure still reports. */ + | { readonly status: "clearing"; readonly attempt: number; readonly open: boolean } + | { readonly status: "disposed" } + +export type SiteEvent = + | { readonly type: "open.changed"; readonly open: boolean } + | { readonly type: "retry" } + | { readonly type: "load.succeeded"; readonly attempt: number; readonly cookies: number } + | { readonly type: "load.failed"; readonly attempt: number } + | { readonly type: "clear.clicked" } + | { readonly type: "clear.succeeded"; readonly attempt: number } + | { readonly type: "clear.failed"; readonly attempt: number } + | { readonly type: "timeout"; readonly phase: "loading" | "clearing"; readonly attempt: number } + /** The popover unmounted: another tab or URL took its place, or the pane closed. */ + | { readonly type: "disposed" } + +export type SiteEffect = + | { readonly type: "load"; readonly attempt: number } + | { readonly type: "clear"; readonly attempt: number } + | { readonly type: "notify-clear-failed" } + +export type SiteTransition = { readonly state: SiteState; readonly effects: readonly SiteEffect[] } + +export const initialSiteState: SiteState = { status: "closed", attempt: 0 } + +const DISPOSED: SiteState = { status: "disposed" } + +const NOTIFY: SiteEffect = { type: "notify-clear-failed" } + +export function transitionSite(state: SiteState, event: SiteEvent): SiteTransition { + if (state.status === "disposed") return unchanged(state) + + switch (event.type) { + case "disposed": + return { state: DISPOSED, effects: [] } + case "open.changed": + return event.open ? opened(state) : closed(state) + case "retry": + return state.status === "failed" ? load(state.attempt + 1) : unchanged(state) + case "load.succeeded": + if (state.status !== "loading" || event.attempt !== state.attempt) return unchanged(state) + + return { state: { status: "ready", attempt: state.attempt, cookies: event.cookies }, effects: [] } + case "load.failed": + if (state.status !== "loading" || event.attempt !== state.attempt) return unchanged(state) + + return { state: { status: "failed", attempt: state.attempt }, effects: [] } + case "clear.clicked": + if (state.status !== "ready") return unchanged(state) + + return { + state: { status: "clearing", attempt: state.attempt + 1, open: true }, + effects: [{ type: "clear", attempt: state.attempt + 1 }], + } + case "clear.succeeded": + return cleared(state, event.attempt, []) + case "clear.failed": + return cleared(state, event.attempt, [NOTIFY]) + case "timeout": + if (event.phase === "clearing") return cleared(state, event.attempt, [NOTIFY]) + + if (state.status !== "loading" || event.attempt !== state.attempt) return unchanged(state) + + return { state: { status: "failed", attempt: state.attempt }, effects: [] } + } +} + +/** What the popover shows. */ +export function siteView(state: SiteState) { + return { + open: + state.status === "loading" || + state.status === "ready" || + state.status === "failed" || + (state.status === "clearing" && state.open), + /** Undefined while reading or clearing, so a count from before a clear never shows. */ + cookies: state.status === "ready" ? state.cookies : undefined, + /** The read failed: the cookie row says so, and its button reads again instead of clearing. */ + failed: state.status === "failed", + actionDisabled: state.status !== "ready" && state.status !== "failed", + } +} + +function opened(state: Exclude): SiteTransition { + if (state.status === "loading") return unchanged(state) + + if (state.status === "clearing") + return state.open ? unchanged(state) : { state: { ...state, open: true }, effects: [] } + + return load(state.attempt + 1) +} + +function closed(state: Exclude): SiteTransition { + if (state.status === "closed") return unchanged(state) + + if (state.status === "clearing") + return state.open ? { state: { ...state, open: false }, effects: [] } : unchanged(state) + + return { state: { status: "closed", attempt: state.attempt }, effects: [] } +} + +/** A clear that settled reads the count again while the popover is open; a failure also tells the user. */ +function cleared(state: SiteState, attempt: number, effects: readonly SiteEffect[]): SiteTransition { + if (state.status !== "clearing" || attempt !== state.attempt) return unchanged(state) + + if (!state.open) return { state: { status: "closed", attempt: state.attempt }, effects } + const next = load(state.attempt + 1) + + return { state: next.state, effects: [...effects, ...next.effects] } +} + +function load(attempt: number): SiteTransition { + return { state: { status: "loading", attempt }, effects: [{ type: "load", attempt }] } +} + +function unchanged(state: SiteState): SiteTransition { + return { state, effects: [] } +}