mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 07:09:56 +08:00
merge: invalidate scoped workspace rows and reject stale responses at auth boundaries (#3166)
This commit is contained in:
@@ -5,8 +5,11 @@ import { RECOVERY_PRINCIPAL_CHANGED, recoveryPrincipal } from "@/lib/recovery-pr
|
||||
import { subscribeSessionChanges } from "@/lib/session-events";
|
||||
import { listWorkspaces, type ChatWorkspaceRegistration } from "@/lib/workspaces-api";
|
||||
|
||||
import {
|
||||
readCachedChatWorkspaces, writeCachedChatWorkspaces, workspaceCacheGeneration,
|
||||
} from "@/lib/chat-workspaces-cache";
|
||||
|
||||
type WorkspaceList = { owner: string | null; rows: ChatWorkspaceRegistration[]; error: string };
|
||||
let cached: WorkspaceList | null = null;
|
||||
let latestRequest = 0;
|
||||
|
||||
function subscribePrincipal(notify: () => void) {
|
||||
@@ -15,7 +18,7 @@ function subscribePrincipal(notify: () => void) {
|
||||
}
|
||||
|
||||
function initialList(owner: string | null): WorkspaceList {
|
||||
return owner && cached?.owner === owner ? cached : { owner, rows: [], error: "" };
|
||||
return { owner, rows: readCachedChatWorkspaces(owner), error: "" };
|
||||
}
|
||||
|
||||
export function useChatWorkspaces() {
|
||||
@@ -27,13 +30,14 @@ export function useChatWorkspaces() {
|
||||
let requested = 0;
|
||||
async function refresh() {
|
||||
const request = ++latestRequest;
|
||||
const generation = workspaceCacheGeneration();
|
||||
requested = request;
|
||||
const current = () => alive && request === requested && recoveryPrincipal() === owner;
|
||||
const current = () => alive && request === requested && recoveryPrincipal() === owner && generation === workspaceCacheGeneration();
|
||||
try {
|
||||
const rows = await listWorkspaces(true);
|
||||
if (!current()) return;
|
||||
const next = { owner, rows: rows.filter((row) => row.kind === "workspace"), error: "" };
|
||||
if (owner && request === latestRequest) cached = next;
|
||||
if (owner && request === latestRequest) writeCachedChatWorkspaces(owner, next.rows);
|
||||
setList(next);
|
||||
} catch (err) {
|
||||
if (current()) setList((last) => ({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { invalidateClientCache } from "./client-cache";
|
||||
import { resetChatWorkspacesCache } from "./chat-workspaces-cache";
|
||||
import { browserStorage } from "@/shared/storage";
|
||||
import { apiFetch, apiUrl, setRuntimeAuthEnabled } from "@/lib/api";
|
||||
import { updateRecoveryPrincipal } from "@/lib/recovery-principal";
|
||||
@@ -44,6 +45,9 @@ let cachedAuthStatus: { value: AuthStatus | null; expiresAt: number } | null =
|
||||
null;
|
||||
|
||||
export function invalidateAuthStatusCache(): void {
|
||||
// Workspace names are account-scoped. A login, logout or cross-tab auth
|
||||
// boundary must not let the next mount paint the previous account's rows.
|
||||
resetChatWorkspacesCache();
|
||||
invalidateClientCache("navigation:");
|
||||
authGeneration += 1;
|
||||
authStatusRequest = null;
|
||||
|
||||
21
web/lib/chat-workspaces-cache.ts
Normal file
21
web/lib/chat-workspaces-cache.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { ChatWorkspaceRegistration } from "@/lib/workspaces-api";
|
||||
|
||||
let cached: { owner: string; rows: ChatWorkspaceRegistration[] } | null = null;
|
||||
let generation = 0;
|
||||
|
||||
export function workspaceCacheGeneration() {
|
||||
return generation;
|
||||
}
|
||||
|
||||
export function readCachedChatWorkspaces(owner: string | null): ChatWorkspaceRegistration[] {
|
||||
return owner && cached?.owner === owner ? cached.rows : [];
|
||||
}
|
||||
|
||||
export function writeCachedChatWorkspaces(owner: string, rows: ChatWorkspaceRegistration[]) {
|
||||
cached = { owner, rows };
|
||||
}
|
||||
|
||||
export function resetChatWorkspacesCache() {
|
||||
generation++;
|
||||
cached = null;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { afterEach, expect, test, vi } from "vitest";
|
||||
vi.mock("@/lib/recovery-principal", () => ({
|
||||
RECOVERY_PRINCIPAL_CHANGED: "test-principal-changed",
|
||||
recoveryPrincipal: () => "user:test",
|
||||
updateRecoveryPrincipal: vi.fn(),
|
||||
}));
|
||||
const listWorkspaces = vi.fn();
|
||||
|
||||
@@ -45,16 +46,69 @@ test("a remounted sidebar keeps the last workspace while it refreshes", async ()
|
||||
expect(second.result.current.workspaces[0]?.display_name).toBe("Anglais");
|
||||
});
|
||||
|
||||
test('a superseded response cannot replace a newer workspace list after remount',async()=>{
|
||||
const row=(display_name:string)=>[{workspace_id:display_name,display_name,kind:'workspace'}];
|
||||
let settle:(value:unknown)=>void=()=>{};
|
||||
listWorkspaces.mockReturnValueOnce(new Promise(resolve=>{settle=resolve;}));
|
||||
const {useChatWorkspaces}=await import('@/hooks/useChatWorkspaces');
|
||||
const a=renderHook(()=>useChatWorkspaces());a.unmount();
|
||||
listWorkspaces.mockResolvedValueOnce(row('New workspace'));
|
||||
const b=renderHook(()=>useChatWorkspaces());await waitFor(()=>expect(b.result.current.workspaces[0]?.display_name).toBe('New workspace'));
|
||||
await act(async()=>{settle(row('Old workspace'));await Promise.resolve();});
|
||||
b.unmount();listWorkspaces.mockReturnValueOnce(new Promise(()=>{}));
|
||||
const c=renderHook(()=>useChatWorkspaces());
|
||||
expect(c.result.current.workspaces[0]?.display_name).toBe('New workspace');
|
||||
test("a superseded response cannot replace a newer workspace list after remount", async () => {
|
||||
const row = (display_name: string) => [{ workspace_id: display_name, display_name, kind: "workspace" }];
|
||||
let settle: (value: unknown) => void = () => {};
|
||||
listWorkspaces.mockReturnValueOnce(new Promise(resolve => { settle = resolve; }));
|
||||
const { useChatWorkspaces } = await import("@/hooks/useChatWorkspaces");
|
||||
const first = renderHook(() => useChatWorkspaces());
|
||||
first.unmount();
|
||||
listWorkspaces.mockResolvedValueOnce(row("New workspace"));
|
||||
const second = renderHook(() => useChatWorkspaces());
|
||||
await waitFor(() => expect(second.result.current.workspaces[0]?.display_name).toBe("New workspace"));
|
||||
await act(async () => { settle(row("Old workspace")); });
|
||||
second.unmount();
|
||||
listWorkspaces.mockReturnValueOnce(new Promise(() => {}));
|
||||
const third = renderHook(() => useChatWorkspaces());
|
||||
expect(third.result.current.workspaces[0]?.display_name).toBe("New workspace");
|
||||
});
|
||||
|
||||
test("resetting the cache drops the previous account's rows", async () => {
|
||||
const rows = [
|
||||
{
|
||||
workspace_id: "ws-anglais",
|
||||
display_name: "Anglais",
|
||||
kind: "workspace",
|
||||
status: "ready",
|
||||
archived: false,
|
||||
},
|
||||
];
|
||||
listWorkspaces.mockResolvedValueOnce(rows);
|
||||
|
||||
const { useChatWorkspaces } = await import("@/hooks/useChatWorkspaces");
|
||||
const { resetChatWorkspacesCache } = await import("@/lib/chat-workspaces-cache");
|
||||
const first = renderHook(() => useChatWorkspaces());
|
||||
await waitFor(() => expect(first.result.current.workspaces).toHaveLength(1));
|
||||
|
||||
resetChatWorkspacesCache();
|
||||
first.unmount();
|
||||
|
||||
listWorkspaces.mockReturnValue(new Promise(() => {}));
|
||||
const second = renderHook(() => useChatWorkspaces());
|
||||
|
||||
expect(second.result.current.workspaces).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("auth invalidation clears the workspace cache", async () => {
|
||||
const { readCachedChatWorkspaces, writeCachedChatWorkspaces } = await import(
|
||||
"@/lib/chat-workspaces-cache"
|
||||
);
|
||||
writeCachedChatWorkspaces("user:test", [
|
||||
{
|
||||
workspace_id: "ws-anglais",
|
||||
display_name: "Anglais",
|
||||
kind: "workspace",
|
||||
follows_root: false,
|
||||
path: "/anglais",
|
||||
created_at: "2026-01-01T00:00:00Z",
|
||||
status: "ready",
|
||||
archived: false,
|
||||
error: "",
|
||||
},
|
||||
]);
|
||||
|
||||
const { invalidateAuthStatusCache } = await import("@/lib/auth");
|
||||
invalidateAuthStatusCache();
|
||||
|
||||
expect(readCachedChatWorkspaces("user:test")).toHaveLength(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user