diff --git a/apps/web/src/components/issue/IssuesPage.tsx b/apps/web/src/components/issue/IssuesPage.tsx index 707cda40fb889..5181a897a1a17 100644 --- a/apps/web/src/components/issue/IssuesPage.tsx +++ b/apps/web/src/components/issue/IssuesPage.tsx @@ -482,7 +482,7 @@ export function IssuesPage() { ) : noneAnswered && list.error ? ( ) : groups.length === 0 ? ( @@ -508,7 +508,7 @@ export function IssuesPage() { {list.error} Showing the issues that loaded. - diff --git a/apps/web/src/state/issues.ts b/apps/web/src/state/issues.ts index 16679aa25f1c7..c92bf81154a12 100644 --- a/apps/web/src/state/issues.ts +++ b/apps/web/src/state/issues.ts @@ -46,9 +46,13 @@ const mergedIssueLists = createMergedIssueListAtoms( /** The same listing read across every connected environment, as the pull-request page does. */ export function useIssueList( targets: ReadonlyArray, -): MergedIssueList & { readonly refresh: () => void } { +): MergedIssueList & { readonly refresh: () => void; readonly retry: () => void } { const view = useAtomValue(mergedIssueLists.atom(targets)); - return { ...view, refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets) }; + return { + ...view, + refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets), + retry: () => mergedIssueLists.retry(appAtomRegistry, targets), + }; } /** After a write made beside the list, such as a state change in the detail panel. */ diff --git a/packages/client-runtime/src/state/issues.test.ts b/packages/client-runtime/src/state/issues.test.ts index 7c52da1c5004b..587280be65912 100644 --- a/packages/client-runtime/src/state/issues.test.ts +++ b/packages/client-runtime/src/state/issues.test.ts @@ -218,6 +218,39 @@ describe("createMergedIssueListAtoms", () => { } }); + it("retries only failed environments, leaving one still loading to finish", () => { + const registry = AtomRegistry.make(); + const targets = ["slow", "flaky"].map((environmentId) => ({ + environmentId: EnvironmentId.make(environmentId), + input: { state: "open" as const }, + })); + const requests: string[] = []; + const lists = Atom.family((environmentId: string) => + Atom.make( + Effect.suspend(() => { + requests.push(environmentId); + return environmentId === "flaky" ? Effect.fail("Tracker unavailable") : Effect.never; + }), + ), + ); + const listings = createMergedIssueListAtoms( + (target) => lists(target.environmentId), + () => "Tracker unavailable", + ); + const view = listings.atom(targets); + const unmount = registry.mount(view); + try { + expect(registry.get(view)).toMatchObject({ error: "Tracker unavailable", isPending: true }); + + listings.retry(registry, targets); + + expect(requests).toEqual(["slow", "flaky", "flaky"]); + } finally { + unmount(); + registry.dispose(); + } + }); + it("releases historical search targets when the list closes", async () => { NodeV8.setFlagsFromString("--expose-gc"); const gc = NodeVM.runInNewContext("gc") as () => void; diff --git a/packages/client-runtime/src/state/issues.ts b/packages/client-runtime/src/state/issues.ts index ac87ed088f6ff..b48ec184a7c7e 100644 --- a/packages/client-runtime/src/state/issues.ts +++ b/packages/client-runtime/src/state/issues.ts @@ -281,10 +281,23 @@ export interface MergedIssueList { const NO_ISSUE_LISTS: MergedIssueList = { answers: [], error: null, isPending: false }; +function refreshFailed( + registry: AtomRegistry.AtomRegistry, + targets: ReadonlyArray, + query: (target: Target) => Atom.Atom>, +) { + for (const target of targets) { + const atom = query(target); + const result = registry.get(atom); + if (result._tag === "Failure" && !result.waiting) registry.refresh(atom); + } +} + /** * One listing read across every environment at once: React cannot subscribe to a list of atoms * whose length changes, so the fan-out lives in one derived atom keyed by the targets. An - * environment that fails contributes nothing rather than blanking the list. + * environment that fails contributes nothing rather than blanking the list. Retry reloads only + * the environments that failed, so a read still in flight elsewhere is not restarted. */ export function createMergedIssueListAtoms( list: (target: IssueListTarget) => Atom.Atom>, @@ -323,6 +336,8 @@ export function createMergedIssueListAtoms( return atom; }, refresh, + retry: (registry: AtomRegistry.AtomRegistry, targets: ReadonlyArray) => + refreshFailed(registry, targets, list), /** Refreshes each listing of `environmentId` that something still reads. */ refreshEnvironment: (registry: AtomRegistry.AtomRegistry, environmentId: EnvironmentId) => { for (const node of registry.getNodes().values()) { @@ -401,12 +416,7 @@ export function createMergedIssueCandidatesAtoms( return { atom: (targets: ReadonlyArray) => targets.length === 0 ? empty : family(JSON.stringify(targets)), - retry: (registry: AtomRegistry.AtomRegistry, targets: ReadonlyArray) => { - for (const target of targets) { - const atom = candidates(target); - const result = registry.get(atom); - if (result._tag === "Failure" && !result.waiting) registry.refresh(atom); - } - }, + retry: (registry: AtomRegistry.AtomRegistry, targets: ReadonlyArray) => + refreshFailed(registry, targets, candidates), }; } diff --git a/scripts/scenarios/issues-retry-keeps-pending-environment.e2e.ts b/scripts/scenarios/issues-retry-keeps-pending-environment.e2e.ts new file mode 100644 index 0000000000000..8cf489e4ca6c2 --- /dev/null +++ b/scripts/scenarios/issues-retry-keeps-pending-environment.e2e.ts @@ -0,0 +1,153 @@ +/// +import type { IssueListResult } from "@t3tools/contracts"; +import { expect } from "e2e"; + +import { SECOND_HOME, startSecondServer } from "./second-server.ts"; +import { makeState, test, WEB } from "./t3.ts"; + +test( + "Retrying a failed environment keeps the Issues refresh already in progress", + WEB, + async ({ t3, app, screen, browser }) => { + await t3.project("demo"); + const server = startSecondServer(app.baseUrl ?? ""); + try { + const { host, token } = await server.ready; + await makeState(SECOND_HOME).state.project("remote-demo"); + await browser.addInitScript((remoteHost) => { + const { promise: slowRead, resolve: release } = Promise.withResolvers(); + let refreshing = false; + let primaryRefreshStarted = false; + let remoteRefreshStarted = false; + Object.assign(window, { + armIssuesRefresh: () => { + refreshing = true; + }, + releaseSlowIssuesRead: release, + }); + const date = "2026-10-01T00:00:00.000Z"; + const tracker = "github" as IssueListResult["trackers"][number]["tracker"]; + const list = (title: string, remote: boolean): IssueListResult => { + const key = remote ? "2" : "1"; + return { + entries: [ + { + tracker, + host: "github.com", + scope: "example/demo", + key, + displayKey: "#" + key, + title, + projectId: (remote + ? "remote-demo" + : "demo") as IssueListResult["entries"][number]["projectId"], + projectTitle: remote ? "remote-demo" : "demo", + url: "https://github.com/example/demo/issues/" + key, + author: null, + state: { category: "open", name: "Open" }, + createdAt: date, + updatedAt: date, + closedAt: null, + assignees: [], + labels: [], + commentCount: 0, + }, + ], + errors: [], + nextCursors: {}, + viewers: {}, + trackers: [ + { + tracker, + host: "github.com", + projectCount: 1, + capabilities: null, + configured: true, + detail: null, + }, + ], + }; + }; + const send = WebSocket.prototype.send; + WebSocket.prototype.send = function (data) { + const request: { _tag: string; id: string; tag: string } | null = + typeof data === "string" ? JSON.parse(data) : null; + if (request?._tag !== "Request" || request.tag !== "issues.list") + return send.call(this, data); + const remote = new URL(this.url).host === remoteHost; + const reply = (exit: unknown) => + this.dispatchEvent( + new MessageEvent("message", { + data: JSON.stringify({ _tag: "Exit", requestId: request.id, exit }), + }), + ); + const success = (title: string) => reply({ _tag: "Success", value: list(title, remote) }); + + if (!refreshing) { + queueMicrotask(() => + success(remote ? "Issue from the linked environment" : "Before refresh"), + ); + } else if (remote && !remoteRefreshStarted) { + remoteRefreshStarted = true; + queueMicrotask(() => + reply({ + _tag: "Failure", + cause: [ + { + _tag: "Fail", + error: { + _tag: "IssueOperationError", + operation: "list", + detail: "The linked environment could not list issues.", + }, + }, + ], + }), + ); + } else if (remote) { + queueMicrotask(() => success("Issues recovered on the linked environment")); + } else if (!primaryRefreshStarted) { + primaryRefreshStarted = true; + // Only the original slow read is allowed to finish. Restarting it strands its result. + void slowRead.then(() => success("Updated issue from the slow environment")); + } + }; + }, new URL(host).host); + + await t3.open("/settings/connections"); + await screen.getByRole("button", "Add environment").tap(); + const dialog = screen.getByRole("dialog"); + await dialog.getByPlaceholder("backend.example.com").fill(host); + await dialog.getByPlaceholder("PAIRCODE").fill(token); + await dialog.getByRole("button", "Add environment").tap(); + await screen.getByRole("button", "More actions for Scenario").waitFor({ timeout: 60_000 }); + await app.open("/issues"); + + await screen.getByText("Before refresh", { exact: true }).waitFor({ timeout: 60_000 }); + await screen.getByText("Issue from the linked environment", { exact: true }).waitFor(); + await browser.evaluate(() => { + (window as unknown as { armIssuesRefresh: () => void }).armIssuesRefresh(); + return null; + }); + await screen.getByRole("button", "Refresh issues", { exact: true }).tap(); + await screen.getByText(/Showing the issues that loaded/).waitFor(); + await screen.getByRole("button", "Retry", { exact: true }).tap(); + await screen + .getByText("Issues recovered on the linked environment", { exact: true }) + .waitFor(); + await browser.evaluate(() => { + (window as unknown as { releaseSlowIssuesRead: () => void }).releaseSlowIssuesRead(); + return null; + }); + try { + await expect( + screen.getByText("Updated issue from the slow environment", { exact: true }), + ).toBeVisible(); + } finally { + await app.screenshot("partial-issues-after-retry"); + } + } finally { + await server.stop(); + } + }, +);