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
@@ -1,161 +1,17 @@
|
||||
import React, { type JSX, type ReactNode } from 'react';
|
||||
import { RuntimeAPIContext } from '@/contexts/runtimeAPIContext';
|
||||
import type { FilesAPI, RuntimeAPIs } from '@/lib/api/types';
|
||||
import {
|
||||
approxStringBytes,
|
||||
evictContentLru,
|
||||
setContentBytes,
|
||||
touchContent as touchContentLru,
|
||||
removeContentBytes,
|
||||
} from '@/sync/content-cache';
|
||||
|
||||
/** Wrap a FilesAPI with an in-memory LRU content cache. */
|
||||
function withContentCache(files: FilesAPI): FilesAPI {
|
||||
const cache = new Map<string, { content: string; path: string; size?: number; mtimeMs?: number }>();
|
||||
|
||||
const removeCacheEntry = (path: string) => {
|
||||
cache.delete(path);
|
||||
removeContentBytes(path);
|
||||
};
|
||||
|
||||
const removeCacheEntriesByPrefix = (path: string) => {
|
||||
const prefix = path.endsWith('/') ? path : `${path}/`;
|
||||
for (const key of cache.keys()) {
|
||||
if (key === path || key.startsWith(prefix)) {
|
||||
removeCacheEntry(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/** Whether cached metadata still matches the file on disk. */
|
||||
const statMatches = (
|
||||
cached: { size?: number; mtimeMs?: number },
|
||||
latest: { isFile: boolean; size: number; mtimeMs?: number },
|
||||
): boolean => {
|
||||
if (!latest.isFile) return false;
|
||||
// If mtimeMs is available on both sides, it is the strongest signal.
|
||||
if (cached.mtimeMs !== undefined && latest.mtimeMs !== undefined) {
|
||||
return cached.mtimeMs === latest.mtimeMs && cached.size === latest.size;
|
||||
}
|
||||
return cached.size === latest.size;
|
||||
};
|
||||
|
||||
const syncCacheEntry = (
|
||||
path: string,
|
||||
result: { content: string; path: string },
|
||||
stat?: { isFile: boolean; size: number; mtimeMs?: number } | null,
|
||||
): { content: string; path: string } => {
|
||||
const bytes = approxStringBytes(result.content);
|
||||
cache.set(path, {
|
||||
...result,
|
||||
size: stat?.isFile ? stat.size : undefined,
|
||||
mtimeMs: stat?.isFile ? stat.mtimeMs : undefined,
|
||||
});
|
||||
setContentBytes(path, bytes);
|
||||
|
||||
const keep = new Set<string>();
|
||||
evictContentLru(keep, (evictPath) => {
|
||||
cache.delete(evictPath);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const readFreshFile = async (path: string, options?: Parameters<NonNullable<FilesAPI['readFile']>>[1]): Promise<{ content: string; path: string }> => {
|
||||
// stat → read → stat to avoid TOCTOU:
|
||||
// if the file changes between read and either stat, metadata won't match and we retry.
|
||||
const statBefore = await files.statFile?.(path, options).catch(() => null);
|
||||
|
||||
const result = await files.readFile!(path, options);
|
||||
|
||||
const statAfter = await files.statFile?.(path, options).catch(() => null);
|
||||
|
||||
// If both stats are available and agree, the read was atomic with respect to file changes.
|
||||
if (statBefore && statAfter && statBefore.isFile && statAfter.isFile) {
|
||||
if (statBefore.size === statAfter.size && statBefore.mtimeMs === statAfter.mtimeMs) {
|
||||
return syncCacheEntry(path, result, statAfter);
|
||||
}
|
||||
// File changed during read — discard and re-read once.
|
||||
const retryStatBefore = await files.statFile?.(path, options).catch(() => null);
|
||||
const retry = await files.readFile!(path, options);
|
||||
const retryStat = await files.statFile?.(path, options).catch(() => null);
|
||||
// Accept retry only if file was stable across the read.
|
||||
if (retryStatBefore && retryStat && retryStatBefore.isFile && retryStat.isFile
|
||||
&& retryStatBefore.size === retryStat.size && retryStatBefore.mtimeMs === retryStat.mtimeMs) {
|
||||
return syncCacheEntry(path, retry, retryStat);
|
||||
}
|
||||
// Best-effort: file was still changing, cache what we got. Next hit will re-validate.
|
||||
return syncCacheEntry(path, retry, retryStat);
|
||||
}
|
||||
|
||||
return syncCacheEntry(path, result, statAfter ?? statBefore);
|
||||
};
|
||||
|
||||
const cachedReadFile: FilesAPI['readFile'] = files.readFile
|
||||
? async (path: string, options) => {
|
||||
if (options?.allowOutsideWorkspace) {
|
||||
return readFreshFile(path, options);
|
||||
}
|
||||
const hit = cache.get(path);
|
||||
if (hit) {
|
||||
// Validate cached entry is still fresh
|
||||
if (files.statFile) {
|
||||
const latest = await files.statFile(path, options).catch(() => {
|
||||
removeCacheEntry(path);
|
||||
return null;
|
||||
});
|
||||
if (!latest || !statMatches(hit, latest)) {
|
||||
removeCacheEntry(path);
|
||||
return readFreshFile(path, options);
|
||||
}
|
||||
}
|
||||
touchContentLru(path);
|
||||
return { content: hit.content, path: hit.path };
|
||||
}
|
||||
|
||||
return readFreshFile(path, options);
|
||||
}
|
||||
: undefined;
|
||||
|
||||
// Invalidate cache on writes, deletes, renames
|
||||
const cachedWriteFile: FilesAPI['writeFile'] = files.writeFile
|
||||
? async (path, content) => {
|
||||
removeCacheEntry(path);
|
||||
return files.writeFile!(path, content);
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const cachedDelete: FilesAPI['delete'] = files.delete
|
||||
? async (path) => {
|
||||
removeCacheEntriesByPrefix(path);
|
||||
return files.delete!(path);
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const cachedRename: FilesAPI['rename'] = files.rename
|
||||
? async (oldPath, newPath) => {
|
||||
removeCacheEntriesByPrefix(oldPath);
|
||||
removeCacheEntriesByPrefix(newPath);
|
||||
return files.rename!(oldPath, newPath);
|
||||
}
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
...files,
|
||||
readFile: cachedReadFile,
|
||||
writeFile: cachedWriteFile,
|
||||
delete: cachedDelete,
|
||||
rename: cachedRename,
|
||||
};
|
||||
}
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import { createContentCachedFiles } from '@/contexts/content-cache-owner';
|
||||
|
||||
export function RuntimeAPIProvider({ apis, children }: { apis: RuntimeAPIs; children: ReactNode }): JSX.Element {
|
||||
const cachedFiles = React.useMemo(() => createContentCachedFiles(apis.files), [apis.files]);
|
||||
React.useEffect(() => () => cachedFiles.dispose(), [cachedFiles]);
|
||||
const cachedApis = React.useMemo<RuntimeAPIs>(
|
||||
() => ({
|
||||
...apis,
|
||||
files: withContentCache(apis.files),
|
||||
files: cachedFiles.files,
|
||||
}),
|
||||
[apis],
|
||||
[apis, cachedFiles],
|
||||
);
|
||||
return <RuntimeAPIContext.Provider value={cachedApis}>{children}</RuntimeAPIContext.Provider>;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import React, {
|
||||
useEffect,
|
||||
useMemo,
|
||||
useCallback,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { flushSync } from 'react-dom';
|
||||
@@ -24,6 +25,7 @@ import { runtimeFetch } from '@/lib/runtime-fetch';
|
||||
import { getInitialSystemPreference, readEmbeddedThemeSearchParams } from './theme-embedded-bootstrap';
|
||||
import { isValidTheme } from './theme-validation';
|
||||
import { getSyncedThemeFromPayload, getSyncedThemeVariant } from './theme-sync-payload';
|
||||
import { getRuntimeKey, subscribeRuntimeEndpointChanged } from '@/lib/runtime-switch';
|
||||
|
||||
type ThemePreferences = {
|
||||
themeMode: ThemeMode;
|
||||
@@ -172,8 +174,8 @@ export function ThemeSystemProvider({ children, defaultThemeId }: ThemeSystemPro
|
||||
return existing || null;
|
||||
});
|
||||
const isVSCode = useMemo(() => isVSCodeRuntime(), []);
|
||||
const isLocalDesktopOrigin = useMemo(() => isDesktopLocalOriginActive(), []);
|
||||
const isDesktopShell = useMemo(() => detectDesktopShell(), []);
|
||||
const customThemesRequestRef = useRef(0);
|
||||
const receivesParentThemeSync = useMemo(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
@@ -249,11 +251,13 @@ export function ThemeSystemProvider({ children, defaultThemeId }: ThemeSystemPro
|
||||
return;
|
||||
}
|
||||
|
||||
const runtimeKey = getRuntimeKey();
|
||||
const request = ++customThemesRequestRef.current;
|
||||
setCustomThemesLoading(true);
|
||||
try {
|
||||
const res = await runtimeFetch('/api/config/themes', {
|
||||
method: 'GET',
|
||||
credentials: isLocalDesktopOrigin ? 'omit' : 'include',
|
||||
credentials: isDesktopLocalOriginActive() ? 'omit' : 'include',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
},
|
||||
@@ -269,20 +273,31 @@ export function ThemeSystemProvider({ children, defaultThemeId }: ThemeSystemPro
|
||||
}
|
||||
|
||||
const payload = await res.json();
|
||||
if (request !== customThemesRequestRef.current || runtimeKey !== getRuntimeKey()) return;
|
||||
const incoming = Array.isArray(payload?.themes) ? payload.themes : [];
|
||||
const normalized = incoming.filter(isValidTheme);
|
||||
setCustomThemes(normalized);
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
setCustomThemesLoading(false);
|
||||
if (request === customThemesRequestRef.current && runtimeKey === getRuntimeKey()) {
|
||||
setCustomThemesLoading(false);
|
||||
}
|
||||
}
|
||||
}, [isLocalDesktopOrigin, isVSCode]);
|
||||
}, [isVSCode]);
|
||||
|
||||
useEffect(() => {
|
||||
void reloadCustomThemes();
|
||||
}, [reloadCustomThemes]);
|
||||
|
||||
useEffect(() => subscribeRuntimeEndpointChanged((detail) => {
|
||||
if (detail.runtimeKey === detail.previousRuntimeKey || isVSCode) return;
|
||||
customThemesRequestRef.current += 1;
|
||||
setCustomThemes([]);
|
||||
setCustomThemesLoading(false);
|
||||
void reloadCustomThemes();
|
||||
}), [isVSCode, reloadCustomThemes]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isVSCode) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import type { FilesAPI } from "@/lib/api/types"
|
||||
import { createContentCachedFiles } from "./content-cache-owner"
|
||||
|
||||
const deferred = <T>() => {
|
||||
let resolve!: (value: T) => void
|
||||
const promise = new Promise<T>((res) => { resolve = res })
|
||||
return { promise, resolve }
|
||||
}
|
||||
|
||||
describe("content cache owner", () => {
|
||||
test("reuses only strongly validated content", async () => {
|
||||
let reads = 0
|
||||
const files = {
|
||||
readFile: async (path: string) => ({ path, content: `value-${++reads}` }),
|
||||
statFile: async () => ({ isFile: true, isDirectory: false, size: 7, mtimeMs: 1 }),
|
||||
} as unknown as FilesAPI
|
||||
const owner = createContentCachedFiles(files)
|
||||
|
||||
expect((await owner.files.readFile!("file.ts")).content).toBe("value-1")
|
||||
expect((await owner.files.readFile!("file.ts")).content).toBe("value-1")
|
||||
expect(reads).toBe(1)
|
||||
owner.dispose()
|
||||
})
|
||||
|
||||
test("does not retain size-only reads without mtime", async () => {
|
||||
let reads = 0
|
||||
const owner = createContentCachedFiles({
|
||||
readFile: async (path: string) => ({ path, content: `value-${++reads}` }),
|
||||
statFile: async () => ({ isFile: true, isDirectory: false, size: 7 }),
|
||||
} as unknown as FilesAPI)
|
||||
|
||||
await owner.files.readFile!("file.ts")
|
||||
await owner.files.readFile!("file.ts")
|
||||
expect(reads).toBe(2)
|
||||
owner.dispose()
|
||||
})
|
||||
|
||||
test("retries a read that overlaps a write", async () => {
|
||||
const firstRead = deferred<{ path: string; content: string }>()
|
||||
let content = "old"
|
||||
let reads = 0
|
||||
const owner = createContentCachedFiles({
|
||||
readFile: async (path: string) => {
|
||||
reads += 1
|
||||
return reads === 1 ? firstRead.promise : { path, content }
|
||||
},
|
||||
statFile: async () => ({ isFile: true, isDirectory: false, size: content.length, mtimeMs: content === "old" ? 1 : 2 }),
|
||||
writeFile: async (_path: string, next: string) => { content = next },
|
||||
} as unknown as FilesAPI)
|
||||
|
||||
const reading = owner.files.readFile!("file.ts")
|
||||
await owner.files.writeFile!("file.ts", "new")
|
||||
firstRead.resolve({ path: "file.ts", content: "old" })
|
||||
|
||||
expect((await reading).content).toBe("new")
|
||||
expect(reads).toBe(2)
|
||||
owner.dispose()
|
||||
})
|
||||
|
||||
test("separates identical paths by directory scope", async () => {
|
||||
let reads = 0
|
||||
const owner = createContentCachedFiles({
|
||||
readFile: async (path: string, options?: Parameters<NonNullable<FilesAPI['readFile']>>[1]) => ({ path, content: `${options?.directory}-${++reads}` }),
|
||||
statFile: async () => ({ isFile: true, isDirectory: false, size: 1, mtimeMs: 1 }),
|
||||
} as unknown as FilesAPI)
|
||||
|
||||
const first = await owner.files.readFile!("file.ts", { directory: "/a" })
|
||||
const second = await owner.files.readFile!("file.ts", { directory: "/b" })
|
||||
expect(first.content).toBe("/a-1")
|
||||
expect(second.content).toBe("/b-2")
|
||||
owner.dispose()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
import type { FilesAPI } from '@/lib/api/types';
|
||||
import { subscribeRuntimeEndpointWillChange } from '@/lib/runtime-switch';
|
||||
|
||||
const MAX_ENTRIES = 40;
|
||||
const MAX_BYTES = 20 * 1024 * 1024;
|
||||
type Entry = { content: string; path: string; sourcePath: string; size: number; mtimeMs: number; bytes: number };
|
||||
|
||||
export function createContentCachedFiles(files: FilesAPI): { files: FilesAPI; dispose: () => void } {
|
||||
const cache = new Map<string, Entry>();
|
||||
let totalBytes = 0;
|
||||
let generation = 0;
|
||||
let active = true;
|
||||
let mutationBarrier = Promise.resolve();
|
||||
|
||||
const cacheKey = (path: string, options?: Parameters<NonNullable<FilesAPI['readFile']>>[1]) =>
|
||||
JSON.stringify([options?.directory ?? '', path]);
|
||||
const contentBytes = (content: string) => new TextEncoder().encode(content).byteLength;
|
||||
const removeEntry = (key: string) => {
|
||||
const entry = cache.get(key);
|
||||
if (entry) totalBytes = Math.max(0, totalBytes - entry.bytes);
|
||||
cache.delete(key);
|
||||
};
|
||||
const removePrefix = (path: string) => {
|
||||
const prefix = path.endsWith('/') ? path : `${path}/`;
|
||||
for (const [key, entry] of cache) {
|
||||
if (entry.sourcePath === path || entry.sourcePath.startsWith(prefix)) removeEntry(key);
|
||||
}
|
||||
};
|
||||
const metadataMatches = (cached: Entry, latest: { isFile: boolean; size: number; mtimeMs?: number }) => (
|
||||
latest.isFile
|
||||
&& latest.mtimeMs !== undefined
|
||||
&& cached.mtimeMs === latest.mtimeMs
|
||||
&& cached.size === latest.size
|
||||
);
|
||||
const cacheResult = (
|
||||
key: string,
|
||||
sourcePath: string,
|
||||
result: { content: string; path: string },
|
||||
stat: { isFile: boolean; size: number; mtimeMs?: number },
|
||||
) => {
|
||||
if (!active || !stat.isFile || stat.mtimeMs === undefined) return result;
|
||||
const bytes = contentBytes(result.content);
|
||||
if (bytes > MAX_BYTES) return result;
|
||||
removeEntry(key);
|
||||
cache.set(key, { ...result, sourcePath, size: stat.size, mtimeMs: stat.mtimeMs, bytes });
|
||||
totalBytes += bytes;
|
||||
while (cache.size > MAX_ENTRIES || totalBytes > MAX_BYTES) {
|
||||
const oldest = cache.keys().next().value;
|
||||
if (!oldest) break;
|
||||
removeEntry(oldest);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const readFresh = async (
|
||||
key: string,
|
||||
path: string,
|
||||
options: Parameters<NonNullable<FilesAPI['readFile']>>[1] | undefined,
|
||||
capturedGeneration: number,
|
||||
): Promise<{ content: string; path: string }> => {
|
||||
const before = await files.statFile?.(path, options).catch(() => null);
|
||||
const result = await files.readFile!(path, options);
|
||||
const after = await files.statFile?.(path, options).catch(() => null);
|
||||
if (!active) throw new Error('File read invalidated by runtime change');
|
||||
if (capturedGeneration !== generation) return cachedReadFile!(path, options);
|
||||
const stable = before && after && before.isFile && after.isFile
|
||||
&& before.mtimeMs !== undefined && after.mtimeMs !== undefined
|
||||
&& before.size === after.size && before.mtimeMs === after.mtimeMs;
|
||||
return stable ? cacheResult(key, path, result, after) : result;
|
||||
};
|
||||
|
||||
const cachedReadFile: FilesAPI['readFile'] = files.readFile
|
||||
? async (path, options) => {
|
||||
await mutationBarrier;
|
||||
if (!active) throw new Error('File cache owner disposed');
|
||||
const capturedGeneration = generation;
|
||||
if (options?.allowOutsideWorkspace) return files.readFile!(path, options);
|
||||
const key = cacheKey(path, options);
|
||||
const hit = cache.get(key);
|
||||
if (!hit) return readFresh(key, path, options, capturedGeneration);
|
||||
const latest = await files.statFile?.(path, options).catch(() => null);
|
||||
if (!active) throw new Error('File read invalidated by runtime change');
|
||||
if (capturedGeneration !== generation) return cachedReadFile!(path, options);
|
||||
if (!latest || !metadataMatches(hit, latest)) {
|
||||
removeEntry(key);
|
||||
return readFresh(key, path, options, capturedGeneration);
|
||||
}
|
||||
cache.delete(key);
|
||||
cache.set(key, hit);
|
||||
return { content: hit.content, path: hit.path };
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const mutate = async <T>(paths: string[], operation: () => Promise<T>): Promise<T> => {
|
||||
const previous = mutationBarrier;
|
||||
let release!: () => void;
|
||||
mutationBarrier = new Promise<void>((resolve) => { release = resolve; });
|
||||
await previous;
|
||||
generation += 1;
|
||||
paths.forEach(removePrefix);
|
||||
try {
|
||||
return await operation();
|
||||
} finally {
|
||||
paths.forEach(removePrefix);
|
||||
generation += 1;
|
||||
release();
|
||||
}
|
||||
};
|
||||
|
||||
const cachedFiles: FilesAPI = {
|
||||
...files,
|
||||
readFile: cachedReadFile,
|
||||
writeFile: files.writeFile ? (path, content) => mutate([path], () => files.writeFile!(path, content)) : undefined,
|
||||
delete: files.delete ? (path) => mutate([path], () => files.delete!(path)) : undefined,
|
||||
rename: files.rename ? (oldPath, newPath) => mutate([oldPath, newPath], () => files.rename!(oldPath, newPath)) : undefined,
|
||||
};
|
||||
const unsubscribeRuntime = subscribeRuntimeEndpointWillChange((detail) => {
|
||||
if (detail.runtimeKey === detail.previousRuntimeKey) return;
|
||||
active = false;
|
||||
generation += 1;
|
||||
cache.clear();
|
||||
totalBytes = 0;
|
||||
});
|
||||
return {
|
||||
files: cachedFiles,
|
||||
dispose: () => {
|
||||
active = false;
|
||||
generation += 1;
|
||||
cache.clear();
|
||||
totalBytes = 0;
|
||||
unsubscribeRuntime();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user