Skip to content

[Bug]: Session edit does not optimistically update SWR cache due to SWRInfinite key mismatch #13474

@EurFelux

Description

@EurFelux

Issue Checklist

  • I understand that issues are for feedback and problem solving, not for complaining in the comment section, and will provide as much information as possible to help solve the problem.
  • My issue is not listed in the FAQ.
  • I've looked at pinned issues and searched for existing Open Issues, Closed Issues, and Discussions, no similar issue or discussion was found.
  • I've filled in short, clear headings so that developers can quickly identify a rough idea of what to expect when flipping through the list of issues. And not "a suggestion", "stuck", etc.
  • I've confirmed that I am using the latest version of Cherry Studio.

Platform

All platforms

Version

latest (main branch)

Bug Description

useUpdateSession uses the global mutate(listKey, ...) from swr to update the session list cache after editing a session. However, useSessions uses useSWRInfinite, which stores its cache under a different internal key format ($inf$ prefix + serialized page key), not the plain listKey string.

This means mutate(listKey, ...) in useUpdateSession never actually updates the SWRInfinite list cache. The UI shows stale session data (e.g., old name after rename) until SWR's automatic revalidation kicks in a few seconds later.

Root cause in code:

// useUpdateSession.ts — uses global mutate with plain string key
mutate<ListAgentSessionsResponse['data']>(
  listKey,  // ❌ This key doesn't match useSWRInfinite's internal cache key
  (prev) => prev?.map((session) => (session.id === result.id ? result : session)) ?? []
)

// useSessions.ts — uses useSWRInfinite, cache key is [base, pageIndex, pageSize]
const getKey = (pageIndex, previousPageData) => {
  return [client.getSessionPaths(agentId).base, pageIndex, pageSize]
}
const { mutate } = useSWRInfinite(getKey, fetcher)

Additionally, useUpdateSession waits for the API response before updating the cache (no optimistic update), causing unnecessary UI delay.

Steps To Reproduce

  1. Open an Agent with multiple sessions
  2. Double-click a session name to rename it
  3. Type a new name and press Enter
  4. Observe: the session name in the sidebar does NOT update immediately
  5. Wait ~2-3 seconds, the name eventually updates via SWR revalidation

Expected Behavior

The session name should update immediately in the sidebar after editing, without waiting for API response or SWR revalidation.

Relevant Log Output

N/A

Additional Context

The fix requires using unstable_serialize from swr/infinite to generate the correct SWRInfinite cache key, and implementing optimistic updates (update UI before API call, rollback on failure).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions