perf: overhaul session loading, caching, and runtime isolation (#2360)
Improve OpenChamber responsiveness under large session workloads while fixing cache, synchronization, and persistence correctness across runtimes, projects, directories, and worktrees. - prioritize selected and visible sessions during bootstrap and defer non-critical enrichment work - reduce redundant message loading, event processing, store publication, and hidden sidebar work - prevent stale session and message requests from overwriting newer authoritative state - preserve existing data when authoritative fetches fail instead of treating failures as successful empty responses - scope session materialization, messages, drafts, queues, todos, pins, permissions, folders, tabs, Git state, and pull request data by runtime and directory identity - harden runtime switching, reconnect, cleanup, mutation reconciliation, and persisted-state ordering - preserve live subagent Task linkage when metadata arrives after an older message request or while streaming parts are suspended - coalesce overlapping tail refreshes without losing newer refresh demand - improve cold-session loading by moving deferrable work out of the critical bootstrap path - isolate URL authentication, mobile credentials, native secrets, and other runtime-owned state across endpoint changes - bound long-lived caches and remove avoidable allocations from event and rendering hot paths - limit virtualization to archive collections where it improves rendering without disrupting active sidebar layout - stabilize session folders, pin ordering, expanded state, and persisted sidebar behavior - open skill files through the same secure editor and outside-workspace grant flow used by file navigation, including worktree sessions - expand regression coverage for stale completions, runtime collisions, reconnect behavior, persistence races, authoritative empty results, and subagent refresh ordering - document the updated synchronization, cache ownership, performance, and runtime-isolation invariants
This commit is contained in:
committed by
GitHub
parent
485efc7117
commit
85400459e9
@@ -2,6 +2,7 @@ import { create } from 'zustand';
|
||||
import { devtools, persist } from 'zustand/middleware';
|
||||
|
||||
import { createDeferredSafeJSONStorage } from './utils/safeStorage';
|
||||
import { getRuntimeKey } from '@/lib/runtime-switch';
|
||||
|
||||
type RootTabsState = {
|
||||
openPaths: string[];
|
||||
@@ -12,6 +13,8 @@ type RootTabsState = {
|
||||
|
||||
type FilesViewTabsState = {
|
||||
byRoot: Record<string, RootTabsState>;
|
||||
activeRuntimeKey: string;
|
||||
runtimeSnapshots: Record<string, { byRoot: Record<string, RootTabsState>; updatedAt: number }>;
|
||||
};
|
||||
|
||||
type FilesViewTabsActions = {
|
||||
@@ -24,10 +27,18 @@ type FilesViewTabsActions = {
|
||||
toggleExpandedPath: (root: string, path: string) => void;
|
||||
expandPath: (root: string, path: string) => void;
|
||||
expandPaths: (root: string, paths: string[]) => void;
|
||||
resetForRuntimeSwitch: (runtimeKey: string) => void;
|
||||
};
|
||||
|
||||
export type FilesViewTabsStore = FilesViewTabsState & FilesViewTabsActions;
|
||||
|
||||
const MAX_ROOTS = 20;
|
||||
const MAX_RUNTIME_SNAPSHOTS = 8;
|
||||
const MAX_OPEN_PATHS_PER_ROOT = 50;
|
||||
const MAX_EXPANDED_PATHS_PER_ROOT = 500;
|
||||
const MAX_PATH_LENGTH = 4096;
|
||||
const ROOT_TTL_MS = 90 * 24 * 60 * 60_000;
|
||||
|
||||
const normalizePath = (value: string): string => {
|
||||
if (!value) return '';
|
||||
|
||||
@@ -90,14 +101,16 @@ const sanitizeByRoot = (input: unknown): Record<string, RootTabsState> => {
|
||||
? Array.from(new Set(state.openPaths
|
||||
.filter((value): value is string => typeof value === 'string')
|
||||
.map((value) => normalizePath(value))
|
||||
.filter((value) => isPathWithinRoot(value, root))))
|
||||
.filter((value) => value.length <= MAX_PATH_LENGTH && isPathWithinRoot(value, root))))
|
||||
.slice(-MAX_OPEN_PATHS_PER_ROOT)
|
||||
: [];
|
||||
|
||||
const expandedPaths = Array.isArray(state.expandedPaths)
|
||||
? Array.from(new Set(state.expandedPaths
|
||||
.filter((value): value is string => typeof value === 'string')
|
||||
.map((value) => normalizePath(value))
|
||||
.filter((value) => isPathWithinRoot(value, root))))
|
||||
.filter((value) => value.length <= MAX_PATH_LENGTH && isPathWithinRoot(value, root))))
|
||||
.slice(-MAX_EXPANDED_PATHS_PER_ROOT)
|
||||
: [];
|
||||
|
||||
const selectedPathCandidate = typeof state.selectedPath === 'string'
|
||||
@@ -111,6 +124,7 @@ const sanitizeByRoot = (input: unknown): Record<string, RootTabsState> => {
|
||||
const touchedAt = typeof state.touchedAt === 'number' && Number.isFinite(state.touchedAt)
|
||||
? state.touchedAt
|
||||
: Date.now();
|
||||
if (Date.now() - touchedAt > ROOT_TTL_MS) continue;
|
||||
|
||||
const existing = next[root];
|
||||
if (existing) {
|
||||
@@ -134,7 +148,7 @@ const sanitizeByRoot = (input: unknown): Record<string, RootTabsState> => {
|
||||
};
|
||||
}
|
||||
|
||||
return next;
|
||||
return clampRoots(next, MAX_ROOTS);
|
||||
};
|
||||
|
||||
const clampRoots = (byRoot: Record<string, RootTabsState>, maxRoots: number): Record<string, RootTabsState> => {
|
||||
@@ -163,6 +177,22 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
byRoot: {},
|
||||
activeRuntimeKey: getRuntimeKey(),
|
||||
runtimeSnapshots: {},
|
||||
|
||||
resetForRuntimeSwitch: (runtimeKey) => {
|
||||
set((state) => {
|
||||
const runtimeSnapshots = {
|
||||
...state.runtimeSnapshots,
|
||||
[state.activeRuntimeKey]: { byRoot: sanitizeByRoot(state.byRoot), updatedAt: Date.now() },
|
||||
};
|
||||
return {
|
||||
activeRuntimeKey: runtimeKey,
|
||||
runtimeSnapshots,
|
||||
byRoot: sanitizeByRoot(runtimeSnapshots[runtimeKey]?.byRoot),
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
addOpenPath: (root, path, options) => {
|
||||
const normalizedRoot = normalizePath((root || '').trim());
|
||||
@@ -189,7 +219,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
selectedPath: nextSelectedPath,
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -225,7 +255,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
touchedAt: Date.now(),
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -267,7 +297,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
},
|
||||
};
|
||||
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -304,7 +334,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
},
|
||||
};
|
||||
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -334,7 +364,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
selectedPath: normalizedPath,
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -383,7 +413,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
expandedPaths: nextExpandedPaths,
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -410,7 +440,7 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
expandedPaths: [...current.expandedPaths, normalizedPath],
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
|
||||
@@ -444,25 +474,49 @@ export const useFilesViewTabsStore = create<FilesViewTabsStore>()(
|
||||
expandedPaths: [...current.expandedPaths, ...newPaths],
|
||||
},
|
||||
};
|
||||
return { byRoot: clampRoots(byRoot, 20) };
|
||||
return { byRoot: clampRoots(byRoot, MAX_ROOTS) };
|
||||
});
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'files-view-tabs-store',
|
||||
version: 2,
|
||||
version: 3,
|
||||
storage: createDeferredSafeJSONStorage(),
|
||||
migrate: (persistedState) => {
|
||||
if (!persistedState || typeof persistedState !== 'object') {
|
||||
return { byRoot: {} };
|
||||
migrate: (persistedState, version) => {
|
||||
if (version < 3 || !persistedState || typeof persistedState !== 'object') {
|
||||
return { byRoot: {}, activeRuntimeKey: getRuntimeKey(), runtimeSnapshots: {} };
|
||||
}
|
||||
|
||||
const rawByRoot = (persistedState as { byRoot?: unknown }).byRoot;
|
||||
return persistedState;
|
||||
},
|
||||
partialize: (state) => {
|
||||
const currentSnapshots = {
|
||||
...state.runtimeSnapshots,
|
||||
[state.activeRuntimeKey]: { byRoot: sanitizeByRoot(state.byRoot), updatedAt: Date.now() },
|
||||
};
|
||||
const runtimeSnapshots = Object.fromEntries(Object.entries(currentSnapshots)
|
||||
.sort(([, left], [, right]) => right.updatedAt - left.updatedAt)
|
||||
.slice(0, MAX_RUNTIME_SNAPSHOTS)
|
||||
.map(([runtimeKey, snapshot]) => [runtimeKey, {
|
||||
byRoot: sanitizeByRoot(snapshot.byRoot),
|
||||
updatedAt: snapshot.updatedAt,
|
||||
}]));
|
||||
return { activeRuntimeKey: state.activeRuntimeKey, runtimeSnapshots };
|
||||
},
|
||||
merge: (persistedState, currentState) => {
|
||||
const persisted = persistedState && typeof persistedState === 'object'
|
||||
? persistedState as Partial<FilesViewTabsState>
|
||||
: {};
|
||||
const runtimeSnapshots = persisted.runtimeSnapshots && typeof persisted.runtimeSnapshots === 'object'
|
||||
? persisted.runtimeSnapshots
|
||||
: {};
|
||||
const activeRuntimeKey = getRuntimeKey();
|
||||
return {
|
||||
byRoot: sanitizeByRoot(rawByRoot),
|
||||
...currentState,
|
||||
activeRuntimeKey,
|
||||
runtimeSnapshots,
|
||||
byRoot: sanitizeByRoot(runtimeSnapshots[activeRuntimeKey]?.byRoot),
|
||||
};
|
||||
},
|
||||
partialize: (state) => ({ byRoot: sanitizeByRoot(state.byRoot) }),
|
||||
}
|
||||
),
|
||||
{ name: 'files-view-tabs-store' }
|
||||
|
||||
Reference in New Issue
Block a user