fix(rules-injector): memoize project-root lookup per process lifecycle

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
Sisyphus
2026-04-18 14:11:14 +09:00
parent 76945bf3c8
commit bcd7b8e348
2 changed files with 19 additions and 0 deletions
@@ -2,6 +2,12 @@ import { existsSync, statSync } from "node:fs";
import { dirname, join } from "node:path";
import { PROJECT_MARKERS } from "./constants";
const projectRootCache = new Map<string, string | null>();
export function clearProjectRootCache(): void {
projectRootCache.clear();
}
/**
* Find project root by walking up from startPath.
* Checks for PROJECT_MARKERS (.git, pyproject.toml, package.json, etc.)
@@ -10,6 +16,16 @@ import { PROJECT_MARKERS } from "./constants";
* @returns Project root path or null if not found
*/
export function findProjectRoot(startPath: string): string | null {
if (projectRootCache.has(startPath)) {
return projectRootCache.get(startPath) ?? null;
}
const projectRoot = findProjectRootWithoutCache(startPath);
projectRootCache.set(startPath, projectRoot);
return projectRoot;
}
function findProjectRootWithoutCache(startPath: string): string | null {
let current: string;
try {