Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
perf(community): paint a warm channel's cached tail without waiting on the anchor
Shelly#386331bc5dcb8a98
diff --git a/src/web/src/hooks/community/use-messages.loading-state.test.ts b/src/web/src/hooks/community/use-messages.loading-state.test.tsindex 746614666..f0db6ed69 100644--- a/src/web/src/hooks/community/use-messages.loading-state.test.ts+++ b/src/web/src/hooks/community/use-messages.loading-state.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest"import React from "react"import TestRenderer, { act } from "react-test-renderer"import { QueryClient, QueryClientProvider } from "@tanstack/react-query"+import { communityKeys } from "@/lib/query-keys"import { useMessages } from "./use-messages"const apiFetchMock = vi.fn()@@ -34,8 +35,18 @@ function Capture({ onResult, channelId, lastReadMessageId }: {function renderCapture(onResult: (r: { isLoading: boolean; messages: unknown[] }) => void,lastReadMessageId: string | null | undefined,+ seedTail?: { id: string; seq: number }[],) {const queryClient = new QueryClient()+ // Simulate a warm channel: the persisted `channelMessages` cache is already+ // hydrated with a newest-tail window before this hook mounts (bootstrap/read+ // snapshot not yet resolved).+ if (seedTail) {+ queryClient.setQueryData(communityKeys.channelMessages("ch_new"), {+ pages: [{ messages: seedTail, hasMore: false, latestSeq: seedTail.at(-1)?.seq ?? 0 }],+ pageParams: [{ mode: "newest" }],+ })+ }act(() => {TestRenderer.create(React.createElement(@@ -69,3 +80,32 @@ describe("useMessages — isLoading while the anchor snapshot is unresolved", ()expect(latest?.isLoading).toBe(true)})})++describe("useMessages — instant channel switch (slice a): warm cache paints without waiting on the anchor", () => {+ it("reports isLoading: false when the anchor is unresolved but the channelMessages cache is already hydrated", () => {+ let latest: { isLoading: boolean; messages: unknown[] } | undefined+ // Warm switch: anchor snapshot still resolving (`lastReadMessageId:+ // undefined`, query disabled) BUT the persisted newest-tail is already in+ // cache. The rows must paint now — no skeleton, no network wait.+ renderCapture((r) => { latest = r }, undefined, [+ { id: "m_1", seq: 1 },+ { id: "m_2", seq: 2 },+ ])++ expect(latest?.isLoading).toBe(false)+ expect(latest?.messages).toHaveLength(2)+ // Paints straight from cache — no fetch fired while the query is disabled.+ expect(apiFetchMock).not.toHaveBeenCalled()+ })++ it("still reports isLoading: true (skeleton) on a cold cache with the anchor unresolved", () => {+ let latest: { isLoading: boolean; messages: unknown[] } | undefined+ // Cold channel: anchor unresolved AND no hydrated tail → behave exactly as+ // before (skeleton), so the decoupling never turns a genuine cold load into+ // a flash of empty content.+ renderCapture((r) => { latest = r }, undefined)++ expect(latest?.isLoading).toBe(true)+ expect(latest?.messages).toEqual([])+ })+})diff --git a/src/web/src/hooks/community/use-messages.ts b/src/web/src/hooks/community/use-messages.tsindex 396d17225..606723396 100644--- a/src/web/src/hooks/community/use-messages.ts+++ b/src/web/src/hooks/community/use-messages.ts@@ -498,13 +498,24 @@ function useMessagesInner(return {...query,- // While the anchor snapshot is still resolving, the query is `enabled:- // false` — TanStack forces `isFetching` to `false` in that state, which- // makes the native `isLoading = isPending && isFetching` compute to- // `false` even though nothing has loaded yet. Left uncorrected, callers- // see one frame of "ready but empty" (no skeleton) on a brand-new scope- // before the query flips `enabled` and `isLoading` jumps back to `true`.- isLoading: query.isLoading || !anchorResolved,+ // Instant channel switch, slice (a): a warm channel already has its+ // newest-tail hydrated into `messages` (from the persisted `channelMessages`+ // cache) before the read-anchor resolves. Those rows must paint immediately+ // rather than wait on the bootstrap/read-snapshot round-trip — switching+ // must not happen on a network timescale. So only report loading when there+ // is genuinely nothing to show yet.+ //+ // `!anchorResolved` alone used to force loading=true on every mount: while+ // the anchor snapshot resolves the query is `enabled: false`, and TanStack+ // forces `isFetching` false in that state, so native+ // `isLoading = isPending && isFetching` computes to `false` even with no+ // data — leaving callers a frame of "ready but empty". We still guard that+ // empty case, but a non-empty hydrated cache is NOT empty: it's the warm+ // tail, and it renders now. The divider / scroll-to-unread / unread count+ // stay independently gated on `readSnapshotFetching` (page.tsx), so they+ // still resolve progressively when read-state lands — no premature/jumping+ // divider. A cold channel (empty cache) still shows the skeleton.+ isLoading: query.isLoading || (!anchorResolved && messages.length === 0),messages,latestSeq,hasMoreOlder,