Update project files
This commit is contained in:
@@ -584,7 +584,35 @@ interface FileScan {
|
||||
documentProject: string | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* mtime-keyed scan cache. The previous implementation re-read (and re-classified)
|
||||
* every brain file from disk on every chat message. We now reuse a parsed
|
||||
* `FileScan` while the file's mtime is unchanged — re-reading only when the file
|
||||
* actually changes. This mirrors the mtime-keyed caching style of
|
||||
* `retrieval/brainIndex.ts` (whose `getBrainTokenIndex` caches tokens the same
|
||||
* way) while keeping the scan output byte-identical, so scoring is unaffected.
|
||||
*/
|
||||
interface ScanCacheEntry {
|
||||
mtimeMs: number;
|
||||
size: number;
|
||||
scan: FileScan;
|
||||
}
|
||||
const _scanCache = new Map<string, ScanCacheEntry>();
|
||||
|
||||
function scanFile(file: string, brainRoot: string): FileScan {
|
||||
let mtimeMs = 0;
|
||||
let size = 0;
|
||||
try {
|
||||
const stat = fs.statSync(file);
|
||||
mtimeMs = stat.mtimeMs;
|
||||
size = stat.size;
|
||||
const cached = _scanCache.get(file);
|
||||
if (cached && cached.mtimeMs === mtimeMs && cached.size === size) {
|
||||
return cached.scan;
|
||||
}
|
||||
} catch {
|
||||
// stat failed — fall through and attempt a fresh read (which will also fail safely)
|
||||
}
|
||||
const relative = path.relative(brainRoot, file);
|
||||
const title = path.basename(file, path.extname(file));
|
||||
let content = '';
|
||||
@@ -598,7 +626,11 @@ function scanFile(file: string, brainRoot: string): FileScan {
|
||||
const lower = content.toLowerCase();
|
||||
const documentProject = inferDocumentProject(relative, lower);
|
||||
const titleWithPath = `${relative.replace(/[\\/]/g, ' ')} ${title}`;
|
||||
return { file, relative, title, titleWithPath, content, lower, sourceType, knowledgeRole, documentProject };
|
||||
const scan: FileScan = { file, relative, title, titleWithPath, content, lower, sourceType, knowledgeRole, documentProject };
|
||||
if (mtimeMs > 0) {
|
||||
_scanCache.set(file, { mtimeMs, size, scan });
|
||||
}
|
||||
return scan;
|
||||
}
|
||||
|
||||
function scoreScan(scan: FileScan, terms: string[], intent: SecondBrainQueryIntent, targetProject?: string): SecondBrainTraceDocument {
|
||||
|
||||
Reference in New Issue
Block a user