Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(issues): keep issue row pages in sync on observer reattach (MUL-5341) (#5978)
Bohan Jiang6da0407b0380
diff --git a/packages/core/issues/queries.test.ts b/packages/core/issues/queries.test.tsindex afa0d2cae6..c6b84a744e 100644--- a/packages/core/issues/queries.test.ts+++ b/packages/core/issues/queries.test.ts@@ -5,6 +5,8 @@ import { setApiInstance } from "../api";import type { ApiClient } from "../api/client";import type {Issue,+ IssueTableRowsRequest,+ IssueTableRowsResponse,ListIssuesParams,ListIssuesResponse,SearchIssuesResponse,@@ -21,6 +23,7 @@ import {issueFlatListOptions,issueIdentifierOptions,issueKeys,+ issueTableRowPageOptions,projectGanttIssuesOptions,} from "./queries";@@ -113,6 +116,198 @@ describe("childIssuesOptions", () => {});});+describe("issueTableRowPageOptions", () => {+ // Reproduces the "count correct, issue missing until page refresh" bug: a row+ // page gets invalidated while its dynamic useQueries observer is detached, then+ // the observer reattaches. Under the global `staleTime: Infinity` default the+ // page is stale only because it is invalidated, so the reattaching observer+ // MUST refetch it. `refetchOnMount: false` used to suppress that refetch and+ // strand the row stale; `retryOnMount: false` does not.+ const request: IssueTableRowsRequest = {+ query: {+ scope: { kind: "workspace" },+ filters: {},+ sort: { field: "position", direction: "asc" },+ },+ group: { kind: "status" },+ group_key: "todo",+ hierarchy: { enabled: false },+ parent_id: null,+ page: { limit: 50, cursor: null },+ };++ function makeRowsResponse(issues: Issue[]): IssueTableRowsResponse {+ return {+ query_fingerprint: "fp",+ group_key: "todo",+ parent_id: null,+ total: issues.length,+ rows: issues.map((issue) => ({ issue, direct_child_count: 0 })),+ branch_total: issues.length,+ next_cursor: null,+ };+ }++ function rowIssueIds(response: IssueTableRowsResponse | undefined): string[] {+ return response?.rows.map((row) => row.issue.id) ?? [];+ }++ function installFakeTableRowsApi(+ listIssueTableRows: (+ params: IssueTableRowsRequest,+ ) => Promise<IssueTableRowsResponse>,+ ) {+ setApiInstance({ listIssueTableRows } as unknown as ApiClient);+ }++ afterEach(() => {+ vi.restoreAllMocks();+ });++ it("refetches an invalidated page when its observer reattaches", async () => {+ // Global default: server state stays fresh until explicitly invalidated.+ const qc = new QueryClient({+ defaultOptions: { queries: { staleTime: Infinity } },+ });+ const listIssueTableRows = vi+ .fn<+ (params: IssueTableRowsRequest) => Promise<IssueTableRowsResponse>+ >()+ // Head snapshot, then the post-move snapshot that includes the new issue.+ .mockResolvedValueOnce(makeRowsResponse([makeIssue(1)]))+ .mockResolvedValueOnce(makeRowsResponse([makeIssue(1), makeIssue(2)]));+ installFakeTableRowsApi(listIssueTableRows);++ const options = issueTableRowPageOptions(WS_ID, request);++ const observer1 = new QueryObserver(qc, options);+ const unsubscribe1 = observer1.subscribe(() => {});+ await vi.waitFor(() => {+ expect(listIssueTableRows).toHaveBeenCalledTimes(1);+ expect(rowIssueIds(observer1.getCurrentResult().data)).toEqual([+ "issue-1",+ ]);+ });++ // Observer detaches (sibling branch left the viewport), then the row page is+ // invalidated while no observer is active — it only gets marked stale.+ unsubscribe1();+ await qc.invalidateQueries({ queryKey: options.queryKey });+ const cached = qc.getQueryState(options.queryKey);+ expect(cached?.isInvalidated).toBe(true);+ expect(cached?.fetchStatus).toBe("idle");++ // Observer reattaches: the invalidated page must refetch and pick up issue-2.+ const observer2 = new QueryObserver(qc, options);+ const unsubscribe2 = observer2.subscribe(() => {});+ await vi.waitFor(() => {+ expect(listIssueTableRows).toHaveBeenCalledTimes(2);+ expect(rowIssueIds(observer2.getCurrentResult().data)).toEqual([+ "issue-1",+ "issue-2",+ ]);+ });++ unsubscribe2();+ qc.clear();+ });++ it("keeps an errored page errored on reattach (no auto-retry)", async () => {+ const qc = new QueryClient({+ defaultOptions: { queries: { staleTime: Infinity } },+ });+ const listIssueTableRows = vi+ .fn<+ (params: IssueTableRowsRequest) => Promise<IssueTableRowsResponse>+ >()+ .mockRejectedValue(new Error("boom"));+ installFakeTableRowsApi(listIssueTableRows);++ const options = issueTableRowPageOptions(WS_ID, request);++ const observer1 = new QueryObserver(qc, options);+ const unsubscribe1 = observer1.subscribe(() => {});+ await vi.waitFor(() => {+ expect(observer1.getCurrentResult().status).toBe("error");+ });+ expect(listIssueTableRows).toHaveBeenCalledTimes(1);+ unsubscribe1();++ // Reattaching an errored page stays idle — `retryOnMount: false` blocks the+ // automatic retry; only an explicit Retry re-runs it. The fetch decision is+ // synchronous, so the observer never enters `fetching`.+ const observer2 = new QueryObserver(qc, options);+ const unsubscribe2 = observer2.subscribe(() => {});+ expect(observer2.getCurrentResult().status).toBe("error");+ expect(observer2.getCurrentResult().fetchStatus).toBe("idle");+ expect(listIssueTableRows).toHaveBeenCalledTimes(1);++ unsubscribe2();+ qc.clear();+ });++ it("does not auto-retry a background-refetch error on a page that still has data", async () => {+ // The tricky case: a page loads OK, then an invalidation-triggered background+ // refetch fails. TanStack flags such a page `isInvalidated: true` (see its+ // "error" reducer), so it is stale AND errored while keeping the old data. A+ // plain `refetchOnMount: true` would re-fire the failing request on every+ // observer reattach; `retryOnMount: false` alone does NOT cover this path+ // because it only guards no-data first-load errors. The `refetchOnMount`+ // status guard is what keeps the errored page stable until an explicit Retry.+ const qc = new QueryClient({+ defaultOptions: { queries: { staleTime: Infinity } },+ });+ const listIssueTableRows = vi+ .fn<+ (params: IssueTableRowsRequest) => Promise<IssueTableRowsResponse>+ >()+ .mockResolvedValueOnce(makeRowsResponse([makeIssue(1)])) // initial load+ .mockRejectedValueOnce(new Error("refetch failed")) // background refetch+ .mockResolvedValueOnce(makeRowsResponse([makeIssue(1), makeIssue(2)])); // explicit Retry+ installFakeTableRowsApi(listIssueTableRows);++ const options = issueTableRowPageOptions(WS_ID, request);++ const observer1 = new QueryObserver(qc, options);+ const unsubscribe1 = observer1.subscribe(() => {});+ await vi.waitFor(() => {+ expect(observer1.getCurrentResult().status).toBe("success");+ expect(rowIssueIds(observer1.getCurrentResult().data)).toEqual([+ "issue-1",+ ]);+ });++ // Invalidate while the observer is active: the background refetch fires and+ // fails, leaving the page errored-with-data and flagged invalidated.+ void qc.invalidateQueries({ queryKey: options.queryKey }).catch(() => {});+ await vi.waitFor(() => {+ expect(observer1.getCurrentResult().status).toBe("error");+ });+ expect(listIssueTableRows).toHaveBeenCalledTimes(2);+ expect(rowIssueIds(observer1.getCurrentResult().data)).toEqual(["issue-1"]);+ expect(qc.getQueryState(options.queryKey)?.isInvalidated).toBe(true);++ // Detach + reattach must NOT re-fire the failing request.+ unsubscribe1();+ const observer2 = new QueryObserver(qc, options);+ const unsubscribe2 = observer2.subscribe(() => {});+ expect(observer2.getCurrentResult().status).toBe("error");+ expect(observer2.getCurrentResult().fetchStatus).toBe("idle");+ expect(listIssueTableRows).toHaveBeenCalledTimes(2);++ // Only an explicit Retry re-runs it — and then the fresh page renders.+ await observer2.refetch();+ expect(listIssueTableRows).toHaveBeenCalledTimes(3);+ expect(rowIssueIds(observer2.getCurrentResult().data)).toEqual([+ "issue-1",+ "issue-2",+ ]);++ unsubscribe2();+ qc.clear();+ });+});+describe("projectGanttIssuesOptions", () => {let qc: QueryClient;diff --git a/packages/core/issues/queries.ts b/packages/core/issues/queries.tsindex ecda86ff67..e80a35dcca 100644--- a/packages/core/issues/queries.ts+++ b/packages/core/issues/queries.ts@@ -456,9 +456,20 @@ export function issueTableRowPageOptions(queryFn: () => api.listIssueTableRows(request),placeholderData: keepPreviousData,retry: false,- // Dynamic useQueries observers can detach/reinstall as sibling branches- // enter the viewport. An errored page stays errored until explicit Retry.- refetchOnMount: false,+ // Dynamic useQueries observers detach/reinstall as sibling branches enter+ // and leave the viewport. Keep an errored page errored until the user hits+ // Retry — but still let a successful-but-invalidated page refetch on+ // reattach, so a row page that gets WS-invalidated while its observer is+ // detached doesn't stay stranded stale under the global `staleTime: Infinity`+ // default (correct count, missing issue).+ //+ // Both guards are needed: `retryOnMount` covers a first-load error (no+ // data), while the `refetchOnMount` guard covers a background-refetch error+ // on a page that still holds data — TanStack flags such a page+ // `isInvalidated`, so a plain `refetchOnMount: true` would re-fire the+ // failing request on every reattach instead of waiting for explicit Retry.+ retryOnMount: false,+ refetchOnMount: (query) => query.state.status !== "error",});}diff --git a/packages/views/issues/surface/use-issue-status-branches.test.tsx b/packages/views/issues/surface/use-issue-status-branches.test.tsxindex f849a00822..ef4988126a 100644--- a/packages/views/issues/surface/use-issue-status-branches.test.tsx+++ b/packages/views/issues/surface/use-issue-status-branches.test.tsx@@ -80,8 +80,11 @@ describe("useIssueStatusBranches", () => {},);setApiInstance({ listIssueTableRows } as unknown as ApiClient);+ // Mirror the production client (createQueryClient): row pages stay fresh+ // until explicitly invalidated, so re-expanding a collapsed section reuses+ // settled cursor pages instead of refetching them on observer reattach.const queryClient = new QueryClient({- defaultOptions: { queries: { retry: false } },+ defaultOptions: { queries: { retry: false, staleTime: Infinity } },});const { result, rerender } = renderHook(