Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.
TypeScript
fix: ignore stale workspace resource list responses
bingqilinweimaotai64deb943e96c
diff --git a/src/stores/boards.ts b/src/stores/boards.tsindex 98863c5..6eb960e 100644--- a/src/stores/boards.ts+++ b/src/stores/boards.ts@@ -4,6 +4,7 @@ import type {BoardSummary, BoardSnapshot, BoardCard, BoardCardComment, BoardCardLookup,} from '@/types'import { pendingCreateRequestId } from '@/lib/create-idempotency'+import { commitIfContextCurrent, useAuth } from '@/stores/auth'interface BoardsState {/** Workspace-wide list of board summaries. Loaded once on view mount;@@ -92,12 +93,15 @@ export const useBoards = create<BoardsState>((set, get) => ({loadingCardId: null,loadList: async () => {+ const epoch = useAuth.getState().contextEpochset({ loadingList: true })try {- const rows = await api.listBoards()- set({ list: rows })+ await commitIfContextCurrent(+ () => api.listBoards(),+ (list) => set({ list }),+ )} finally {- set({ loadingList: false })+ if (useAuth.getState().contextEpoch === epoch) set({ loadingList: false })}},diff --git a/src/stores/calendar.ts b/src/stores/calendar.tsindex 8b5c312..9c9b249 100644--- a/src/stores/calendar.ts+++ b/src/stores/calendar.ts@@ -2,6 +2,7 @@ import { create } from 'zustand'import { api, ws, type CalendarEventInput } from '@/api/client'import type { CalendarEvent, CalendarEventStatus } from '@/types'import { pendingCreateRequestId } from '@/lib/create-idempotency'+import { commitIfContextCurrent, useAuth } from '@/stores/auth'interface CalendarState {events: CalendarEvent[]@@ -42,13 +43,7 @@ export const useCalendar = create<CalendarState>((set, get) => ({async load() {if (get().loaded || get().loading) return- set({ loading: true, error: null })- try {- const { events } = await api.listCalendarEvents()- set({ events: events.sort(byStart), loaded: true, loading: false })- } catch (err) {- set({ loading: false, error: err instanceof Error ? err.message : String(err) })- }+ await get().reload()},async loadEvent(id) {@@ -77,12 +72,17 @@ export const useCalendar = create<CalendarState>((set, get) => ({},async reload() {+ const epoch = useAuth.getState().contextEpochset({ loading: true, error: null })try {- const { events } = await api.listCalendarEvents()- set({ events: events.sort(byStart), loaded: true, loading: false })+ await commitIfContextCurrent(+ () => api.listCalendarEvents(),+ ({ events }) => set({ events: events.sort(byStart), loaded: true, loading: false }),+ )} catch (err) {- set({ loading: false, error: err instanceof Error ? err.message : String(err) })+ if (useAuth.getState().contextEpoch === epoch) {+ set({ loading: false, error: err instanceof Error ? err.message : String(err) })+ }}},diff --git a/src/stores/documents.ts b/src/stores/documents.tsindex 106545b..283dc50 100644--- a/src/stores/documents.ts+++ b/src/stores/documents.ts@@ -1,6 +1,7 @@import { create } from 'zustand'import { api, ws, type ApiDocument } from '@/api/client'import { pendingCreateRequestId } from '@/lib/create-idempotency'+import { commitIfContextCurrent } from '@/stores/auth'interface DocumentsState {list: ApiDocument[]@@ -25,16 +26,20 @@ export const useDocuments = create<DocumentsState>((set, get) => ({select: (id) => set({ selectedId: id }),load: async () => {if (get().loaded) return- const { documents } = await api.listDocuments()- set({ list: documents, loaded: true })+ await commitIfContextCurrent(+ () => api.listDocuments(),+ ({ documents }) => set({ list: documents, loaded: true }),+ )},reload: async () => {- const { documents } = await api.listDocuments()- set((s) => ({- list: documents,- loaded: true,- selectedId: s.selectedId && documents.some((d) => d.id === s.selectedId) ? s.selectedId : null,- }))+ await commitIfContextCurrent(+ () => api.listDocuments(),+ ({ documents }) => set((s) => ({+ list: documents,+ loaded: true,+ selectedId: s.selectedId && documents.some((d) => d.id === s.selectedId) ? s.selectedId : null,+ })),+ )},reset: () => set({ list: [], loaded: false, selectedId: null }),create: async (input) => {diff --git a/tests/workspace-resource-stores.test.ts b/tests/workspace-resource-stores.test.tsnew file mode 100644index 0000000..93df393--- /dev/null+++ b/tests/workspace-resource-stores.test.ts@@ -0,0 +1,111 @@+import assert from 'node:assert/strict'+import { after, before, test, mock } from 'node:test'+import { fileURLToPath } from 'node:url'+import { createServer, type ViteDevServer } from 'vite'++let server: ViteDevServer+let auth: typeof import('../src/stores/auth')+let api: typeof import('../src/api/client')['api']+let documents: typeof import('../src/stores/documents')['useDocuments']+let boards: typeof import('../src/stores/boards')['useBoards']+let calendar: typeof import('../src/stores/calendar')['useCalendar']+const storageDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'localStorage')++before(async () => {+ const storage = new Map<string, string>()+ Object.defineProperty(globalThis, 'localStorage', { configurable: true, value: {+ getItem: (key: string) => storage.get(key) ?? null,+ setItem: (key: string, value: string) => storage.set(key, value),+ removeItem: (key: string) => storage.delete(key),+ } })+ // Load the real frontend modules with Vite's env and alias handling.+ server = await createServer({+ configFile: false,+ root: fileURLToPath(new URL('..', import.meta.url)),+ resolve: { alias: { '@': fileURLToPath(new URL('../src', import.meta.url)) } },+ server: { middlewareMode: true, watch: null },+ appType: 'custom',+ })+ auth = await server.ssrLoadModule('/src/stores/auth.ts') as typeof auth+ const client = await server.ssrLoadModule('/src/api/client.ts')+ api = client.api+ mock.method(client.ws, 'reconnect', () => {})+ documents = (await server.ssrLoadModule('/src/stores/documents.ts')).useDocuments+ boards = (await server.ssrLoadModule('/src/stores/boards.ts')).useBoards+ calendar = (await server.ssrLoadModule('/src/stores/calendar.ts')).useCalendar+})++after(async () => {+ mock.restoreAll()+ await server?.close()+ if (storageDescriptor) Object.defineProperty(globalThis, 'localStorage', storageDescriptor)+ else Reflect.deleteProperty(globalThis, 'localStorage')+})++async function switchWorkspace(id: string) {+ const resets = [documents, boards, calendar].map((store) => new Promise<void>((resolve) => {+ const unsubscribe = store.subscribe(() => { unsubscribe(); resolve() })+ }))+ auth.useAuth.getState().setActiveCompany(id)+ await Promise.all(resets)+}++function deferred() {+ let resolve!: (value: any) => void+ let reject!: (error: Error) => void+ const promise = new Promise<any>((done, fail) => { resolve = done; reject = fail })+ return { promise, resolve, reject }+}++for (const path of ['documents.load', 'documents.reload', 'boards.loadList', 'calendar.load', 'calendar.reload']) {+ for (const outcome of ['success', 'failure', 'before-new-result', 'failure-before-new-result', 'switch-back']) {+ test(`${path}: ignores stale ${outcome} after a workspace switch`, async () => {+ await switchWorkspace(`${path}-${outcome}-A`)+ const isDocuments = path.startsWith('documents')+ const isBoards = path.startsWith('boards')+ const store = isDocuments ? documents : isBoards ? boards : calendar+ const method = path.split('.')[1]+ const load = () => (store.getState() as any)[method]() as Promise<void>+ const response = (id: string) => {+ const rows = [{ id, title: id, startAt: '2026-09-16T00:00:00Z' }]+ return isDocuments ? { documents: rows } : isBoards ? rows : { events: rows }+ }+ const requestA = deferred()+ const requestB = deferred()+ const beforeNewResult = outcome.endsWith('before-new-result')+ const apiMethod = isDocuments ? 'listDocuments' : isBoards ? 'listBoards' : 'listCalendarEvents'+ let calls = 0+ const stub = mock.method(api, apiMethod, () => (++calls === 1 ? requestA.promise : requestB.promise))+ try {+ // Observe rejection immediately: documents/boards intentionally propagate API errors.+ const oldLoad = load().catch(() => {})+ await switchWorkspace(`${path}-${outcome}-B`)+ if (outcome === 'switch-back') await switchWorkspace(`${path}-${outcome}-A`)+ const newLoad = load()+ if (!beforeNewResult) {+ requestB.resolve(response('current'))+ await newLoad+ if (isDocuments) documents.getState().select('current')+ }+ const expected = store.getState()+ if (outcome.startsWith('failure')) requestA.reject(new Error('old workspace failed'))+ else requestA.resolve(response('stale'))+ await oldLoad+ assert.deepEqual(store.getState(), expected)+ if (beforeNewResult) {+ requestB.resolve(response('current'))+ await newLoad+ }+ const state = store.getState() as any+ assert.equal((isDocuments || isBoards ? state.list : state.events)[0].id, 'current')+ if (!isBoards) {+ assert.equal(state.loaded, true)+ await (store.getState() as any).load()+ assert.equal(calls, 2)+ }+ } finally {+ stub.mock.restore()+ }+ })+ }+}