perf(server): cache deterministic git rev-parse reads in fs exec route (#1399)
* perf(server): cache deterministic git rev-parse reads in fs exec route A fresh client (e.g. immediately after a page reload) has an empty git store and re-resolves every project's root from scratch, firing identical `git rev-parse --absolute-git-dir` / `--git-common-dir` lookups against `/api/fs/exec`. Each spawns a git subprocess server-side, so re-opening a workspace recomputes everything. Add a small TTL cache for an allowlist of deterministic, side-effect-free git plumbing path queries, keyed by `(resolvedCwd, command)`: - Only `git rev-parse` path lookups (absolute-git-dir, git-common-dir, show-toplevel) are cacheable; any other command — including any non-git command — always executes and is never stored. - Only successful results are cached (failures may be transient). - TTL is configurable via OPENCHAMBER_GIT_READ_CACHE_TTL_MS (default 30s, 0 disables). The git directory layout is effectively static while the app runs, so a short TTL safely absorbs the post-reload burst. - Expired entries are pruned alongside exec jobs. Complements the client-side root-resolution cache: that one collapses the in-session N² cascade, this one absorbs the cold-start burst on reload. Adds tests covering cache hit, per-cwd keying, non-allowlisted commands, failed-result bypass and the disable switch. * fix(server): bound git-read cache with count + byte limits; test TTL expiry Per the project caching policy (AGENTS.md: cap in-memory caches with both count and byte limits), the git-read cache was unbounded between prunes. Add dual-constraint LRU eviction (500 entries / 1MB, oldest-first) with recency refresh on cache hits. Add tests for TTL expiry (fake timers) and count-cap eviction. * fix(server): dedupe in-flight git read cache hits
This commit is contained in:
committed by
GitHub
parent
f94d87b5fd
commit
0776aca9c4
@@ -6,6 +6,36 @@ const createCommandTimeoutMs = () => {
|
||||
return 5 * 60 * 1000;
|
||||
};
|
||||
|
||||
// How long a cached git-read result stays fresh. The location of a repo's git
|
||||
// directory is effectively static while the app runs, so a short TTL safely
|
||||
// absorbs the burst of identical lookups a fresh client (e.g. right after a
|
||||
// page reload) fires for every project. Set to 0 to disable caching.
|
||||
const createGitReadCacheTtlMs = () => {
|
||||
const raw = Number(process.env.OPENCHAMBER_GIT_READ_CACHE_TTL_MS);
|
||||
if (Number.isFinite(raw) && raw >= 0) return raw;
|
||||
return 30 * 1000;
|
||||
};
|
||||
|
||||
// Only deterministic, side-effect-free git plumbing path queries are cacheable.
|
||||
// Anything outside this allowlist (including any non-git command) runs normally
|
||||
// — we never cache arbitrary exec.
|
||||
const normalizeCommand = (command) =>
|
||||
typeof command === 'string' ? command.trim().replace(/\s+/g, ' ') : '';
|
||||
|
||||
const isCacheableGitReadCommand = (command) => {
|
||||
const normalized = normalizeCommand(command);
|
||||
return /^git rev-parse(?: --(?:absolute-git-dir|git-common-dir|show-toplevel)){1,3}$/.test(normalized);
|
||||
};
|
||||
|
||||
// Dual-constraint bound per the project's caching policy (count + bytes). Git
|
||||
// rev-parse outputs are tiny, so these ceilings are generous and only guard
|
||||
// against pathological growth on long-lived, many-directory deployments.
|
||||
const GIT_READ_CACHE_MAX_ENTRIES = 500;
|
||||
const GIT_READ_CACHE_MAX_BYTES = 1024 * 1024;
|
||||
|
||||
const gitReadEntryBytes = (key, result) =>
|
||||
key.length + (result?.stdout?.length || 0) + (result?.stderr?.length || 0);
|
||||
|
||||
const isPathWithinRoot = (resolvedPath, rootPath, path, os) => {
|
||||
const resolvedRoot = path.resolve(rootPath || os.homedir());
|
||||
const relative = path.relative(resolvedRoot, resolvedPath);
|
||||
@@ -243,6 +273,9 @@ export const registerFsRoutes = (app, dependencies) => {
|
||||
|
||||
const execJobs = new Map();
|
||||
const commandTimeoutMs = createCommandTimeoutMs();
|
||||
const gitReadCacheTtlMs = createGitReadCacheTtlMs();
|
||||
const gitReadCache = new Map();
|
||||
const inFlightGitReadCache = new Map();
|
||||
|
||||
const pruneExecJobs = () => {
|
||||
const now = Date.now();
|
||||
@@ -258,6 +291,94 @@ export const registerFsRoutes = (app, dependencies) => {
|
||||
}
|
||||
};
|
||||
|
||||
const pruneGitReadCache = () => {
|
||||
if (gitReadCacheTtlMs <= 0) {
|
||||
return;
|
||||
}
|
||||
const now = Date.now();
|
||||
for (const [key, entry] of gitReadCache.entries()) {
|
||||
if (!entry || now - entry.at > gitReadCacheTtlMs) {
|
||||
gitReadCache.delete(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Insert with LRU (oldest-first) eviction enforcing both count and byte caps.
|
||||
// Map iteration order is insertion order, so deleting+re-setting a key moves
|
||||
// it to the most-recently-used position.
|
||||
const setGitReadCacheEntry = (key, result) => {
|
||||
gitReadCache.delete(key);
|
||||
gitReadCache.set(key, { result, at: Date.now() });
|
||||
|
||||
let totalBytes = 0;
|
||||
for (const [k, entry] of gitReadCache) {
|
||||
totalBytes += gitReadEntryBytes(k, entry.result);
|
||||
}
|
||||
while (
|
||||
gitReadCache.size > GIT_READ_CACHE_MAX_ENTRIES ||
|
||||
(totalBytes > GIT_READ_CACHE_MAX_BYTES && gitReadCache.size > 1)
|
||||
) {
|
||||
const oldest = gitReadCache.entries().next().value;
|
||||
if (!oldest) {
|
||||
break;
|
||||
}
|
||||
totalBytes -= gitReadEntryBytes(oldest[0], oldest[1].result);
|
||||
gitReadCache.delete(oldest[0]);
|
||||
}
|
||||
};
|
||||
|
||||
// Runs a command, transparently serving/storing cacheable git-read results.
|
||||
// Non-cacheable commands always execute and are never stored.
|
||||
const runCommandWithGitReadCache = async ({ shell, shellFlag, command, resolvedCwd }) => {
|
||||
const cacheable = gitReadCacheTtlMs > 0 && isCacheableGitReadCommand(command);
|
||||
const cacheKey = cacheable ? `${resolvedCwd} | ||||