Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(inbox): preserve stale lists across issue event patches (#8311)
Multica Eve2fc100263618
diff --git a/packages/core/inbox/ws-updaters.test.ts b/packages/core/inbox/ws-updaters.test.tsindex 4360054df2..4f19118395 100644--- a/packages/core/inbox/ws-updaters.test.ts+++ b/packages/core/inbox/ws-updaters.test.ts@@ -1,3 +1,4 @@+// @vitest-environment nodeimport { describe, it, expect, vi } from "vitest";import { QueryClient, QueryObserver } from "@tanstack/react-query";import {@@ -261,6 +262,54 @@ describe("patchInboxIssueProjection", () => {});});+// The hook-level reproduction lives in use-realtime-sync-inbox.test.tsx.+// These cases cover the partial writers and both list keys, including an+// update to an issue absent from the cached list (setQueryData's no-op trap).+describe.each([+ ["main", inboxKeys.list(wsId)],+ ["archived", inboxKeys.archived(wsId)],+] as const)("partial updates to an invalidated %s inbox", (view, queryKey) => {+ it.each([+ ["status", (qc: QueryClient) => onInboxIssueStatusChanged(qc, wsId, "issue-a", "done")],+ ["priority", (qc: QueryClient) => patchInboxIssueProjection(qc, wsId, "issue-a", { priority: "high" })],+ ["unrelated issue", (qc: QueryClient) => onInboxIssueStatusChanged(qc, wsId, "other-issue", "done")],+ ["deletion", (qc: QueryClient) => onInboxIssueDeleted(qc, wsId, "issue-a")],+ ] as const)("preserves the pending refresh after %s", async (_label, update) => {+ const qc = new QueryClient();+ qc.setQueryData(queryKey, [+ makeItem("i1", "issue-a", {+ issue_priority: "low",+ archived: view === "archived",+ }),+ ]);+ try {+ await onInboxInvalidate(qc, wsId);+ await update(qc);+ expect(qc.getQueryState(queryKey)?.isInvalidated).toBe(true);+ } finally {+ qc.clear();+ }+ });+});++it("does not refetch fresh lists for issue projection changes", async () => {+ const qc = new QueryClient({ defaultOptions: { queries: { staleTime: Infinity } } });+ const queryFn = vi.fn(async () => [makeItem("i1", "issue-a")]);+ const options = { queryKey: inboxKeys.list(wsId), queryFn };+ await qc.fetchQuery(options);+ const observer = new QueryObserver(qc, options);+ const unsubscribe = observer.subscribe(() => {});+ try {+ onInboxIssueStatusChanged(qc, wsId, "issue-a", "done");+ expect(observer.getCurrentResult().data?.[0]?.issue_status).toBe("done");+ expect(queryFn).toHaveBeenCalledTimes(1);+ expect(qc.getQueryState(options.queryKey)?.isInvalidated).toBe(false);+ } finally {+ unsubscribe();+ qc.clear();+ }+});+// MUL-6967 regression: the inbox list must pick up a change that happens while// its FIRST load is still in flight. Plain invalidation cannot do that —// TanStack only cancels an in-flight request on invalidation once the querydiff --git a/packages/core/inbox/ws-updaters.ts b/packages/core/inbox/ws-updaters.tsindex b01676c2f1..17478ee67c 100644--- a/packages/core/inbox/ws-updaters.ts+++ b/packages/core/inbox/ws-updaters.ts@@ -44,14 +44,35 @@ export async function onInboxNew(await onInboxInvalidate(qc, wsId);}+// An issue event only patches known rows; it cannot fulfill a pending inbox+// refresh. setQueryData clears isInvalidated even when the updater returns the+// same array. Preserve it so an inactive list still refetches on its next mount+// (MUL-7286). Do not restart an active fetch or introduce a fetch per issue event.+function patchInboxLists(+ qc: QueryClient,+ wsId: string,+ patch: (items: InboxItem[]) => InboxItem[],+) {+ for (const queryKey of [inboxKeys.list(wsId), inboxKeys.archived(wsId)]) {+ const invalidated = qc.getQueryState(queryKey)?.isInvalidated === true;+ qc.setQueryData<InboxItem[]>(queryKey, (old) => {+ if (!old) return undefined;+ const next = patch(old);+ return next === old ? undefined : next;+ });+ if (invalidated) {+ void qc.invalidateQueries({ queryKey, exact: true, refetchType: "none" });+ }+ }+}+export function patchInboxIssueProjection(qc: QueryClient,wsId: string,issueId: string,patch: { status?: IssueStatus; priority?: IssuePriority },) {- const project = (old: InboxItem[] | undefined) => {- if (!old) return old;+ const project = (old: InboxItem[]) => {let changed = false;const next = old.map((item) => {if (item.issue_id !== issueId) return item;@@ -71,9 +92,8 @@ export function patchInboxIssueProjection(});return changed ? next : old;};- qc.setQueryData<InboxItem[]>(inboxKeys.list(wsId), project);// Archived rows expose the same issue fields and filter controls.- qc.setQueryData<InboxItem[]>(inboxKeys.archived(wsId), project);+ patchInboxLists(qc, wsId, project);}export function patchInboxIssueStatus(@@ -110,10 +130,9 @@ export async function onInboxIssueDeleted(wsId: string,issueId: string,) {- const drop = (old: InboxItem[] | undefined) =>- old?.filter((i) => i.issue_id !== issueId);- qc.setQueryData<InboxItem[]>(inboxKeys.list(wsId), drop);- qc.setQueryData<InboxItem[]>(inboxKeys.archived(wsId), drop);+ patchInboxLists(qc, wsId, (items) =>+ items.filter((i) => i.issue_id !== issueId),+ );await onInboxSummaryInvalidate(qc);}diff --git a/packages/core/realtime/use-realtime-sync-inbox.test.tsx b/packages/core/realtime/use-realtime-sync-inbox.test.tsxnew file mode 100644index 0000000000..ab1b08d43e--- /dev/null+++ b/packages/core/realtime/use-realtime-sync-inbox.test.tsx@@ -0,0 +1,133 @@+/** @vitest-environment jsdom */+import { QueryClientProvider, useQuery } from "@tanstack/react-query";+import { act, cleanup, renderHook, waitFor } from "@testing-library/react";+import type { ReactNode } from "react";+import { afterEach, expect, it, vi } from "vitest";+import { setApiInstance } from "../api";+import type { ApiClient } from "../api/client";+import type { WSClient } from "../api/ws-client";+import {+ deduplicateInboxItems,+ inboxKeys,+ inboxListOptions,+ useInboxUnreadCount,+} from "../inbox/queries";+import { createQueryClient } from "../query-client";+import type { InboxItem } from "../types";+import { useRealtimeSync, type RealtimeSyncStores } from "./use-realtime-sync";++vi.mock("../platform/workspace-storage", () => ({+ getCurrentWsId: () => "ws-1",+ getCurrentSlug: () => "test-ws",+ createWorkspaceAwareStorage: (adapter: unknown) => adapter,+ registerForWorkspaceRehydration: () => {},+}));+vi.mock("../paths", () => ({+ useHasOnboarded: () => true,+ resolvePostAuthDestination: () => "/",+}));++afterEach(() => {+ cleanup();+ vi.restoreAllMocks();+});++it("refreshes an inactive Inbox after inbox:new then issue:updated (MUL-7286)", async () => {+ const qc = createQueryClient();+ const oldItem: InboxItem = {+ id: "n1",+ workspace_id: "ws-1",+ recipient_type: "member",+ recipient_id: "u1",+ actor_type: "agent",+ actor_id: "agent-1",+ type: "new_comment",+ severity: "info",+ issue_id: "issue-1",+ title: "Issue 1",+ body: null,+ issue_status: "todo",+ read: true,+ archived: false,+ created_at: "2026-09-11T00:00:00Z",+ details: null,+ };+ let rows = [oldItem];+ const listInbox = vi.fn(async () => rows);+ setApiInstance({+ listInbox,+ getInboxUnreadSummary: async () => [+ {+ workspace_id: "ws-1",+ count: deduplicateInboxItems(rows).filter((item) => !item.read).length,+ },+ ],+ } as unknown as ApiClient);++ const handlers: Record<string, (payload: unknown) => void> = {};+ const ws = {+ on: (event: string, handler: (payload: unknown) => void) => {+ handlers[event] = handler;+ return () => {+ delete handlers[event];+ };+ },+ onAny: () => () => {},+ onReconnect: () => () => {},+ } as unknown as WSClient;+ const stores = {+ authStore: { getState: () => ({ user: { id: "u1" } }) },+ } as unknown as RealtimeSyncStores;+ function Wrapper({ children }: { children: ReactNode }) {+ return <QueryClientProvider client={qc}>{children}</QueryClientProvider>;+ }+ vi.spyOn(document, "hasFocus").mockReturnValue(true);++ const shell = renderHook(+ () => {+ useRealtimeSync(ws, stores);+ return useInboxUnreadCount("ws-1");+ },+ { wrapper: Wrapper },+ );+ const mountInbox = () =>+ renderHook(() => useQuery(inboxListOptions("ws-1")), { wrapper: Wrapper });+ let page = mountInbox();+ try {+ await waitFor(() => expect(page.result.current.data).toEqual([oldItem]));+ // Switching desktop tabs unmounts the page but keeps its cache for 10 min.+ page.unmount();+ const newItem = {+ ...oldItem,+ id: "n2",+ read: false,+ created_at: "2026-09-11T00:01:00Z",+ };+ rows = [newItem, oldItem];+ await act(async () => {+ handlers["inbox:new"]!({ item: newItem });+ });+ await waitFor(() => expect(shell.result.current).toBe(1));+ expect(qc.getQueryState(inboxKeys.list("ws-1"))?.isInvalidated).toBe(true);+ expect(listInbox).toHaveBeenCalledTimes(1);++ // Any later issue update patches status, without re-reading notifications.+ rows = rows.map((item) => ({ ...item, issue_status: "in_progress" }));+ act(() => {+ handlers["issue:updated"]!({+ issue: { id: "issue-1", status: "in_progress", revision: 2 },+ });+ });+ page = mountInbox();+ await waitFor(() => expect(listInbox).toHaveBeenCalledTimes(2));+ await waitFor(() => expect(page.result.current.data).toEqual(rows));+ expect(+ deduplicateInboxItems(page.result.current.data!).filter((item) => !item.read),+ ).toHaveLength(1);+ expect(shell.result.current).toBe(1);+ } finally {+ page.unmount();+ shell.unmount();+ qc.clear();+ }+});