MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / findSessionDir

Function findSessionDir

apps/vis/server/src/lib/session-store.ts:303–331  ·  view source on GitHub ↗
(home: string, sessionId: string)

Source from the content-addressed store, hash-verified

301}
302
303async function findSessionDir(home: string, sessionId: string): Promise<string | null> {
304 if (!SESSION_ID_RE.test(sessionId)) return null;
305 const sessionsRoot = resolve(join(home, 'sessions'));
306 const sessionsRootPrefix = sessionsRoot + sep;
307 // Try index first — but only trust entries that point *under*
308 // `<home>/sessions/` AND whose basename matches the requested id.
309 // This blocks stale/poisoned index lines from redirecting reads to
310 // unrelated directories.
311 try {
312 const indexLines = (await readFile(join(home, 'session_index.jsonl'), 'utf8')).split(/\r?\n/);
313 for (const line of indexLines) {
314 if (!line.trim()) continue;
315 const entry = JSON.parse(line) as { sessionId?: string; sessionDir?: string };
316 if (entry.sessionId !== sessionId || typeof entry.sessionDir !== 'string') continue;
317 const candidate = resolve(entry.sessionDir);
318 if (!candidate.startsWith(sessionsRootPrefix)) continue;
319 if (candidate.split(sep).pop() !== sessionId) continue;
320 if (await pathExists(candidate)) return candidate;
321 }
322 } catch { /* no index */ }
323 // Fall back to scanning buckets
324 const buckets = await readdir(sessionsRoot, { withFileTypes: true }).catch(() => []);
325 for (const bucket of buckets) {
326 if (!bucket.isDirectory()) continue;
327 const candidate = join(sessionsRoot, bucket.name, sessionId);
328 if (await pathExists(candidate)) return candidate;
329 }
330 return null;
331}
332
333async function scanWire(path: string): Promise<{ count: number; protocolVersion: string }> {
334 const stream = createReadStream(path, { encoding: 'utf8' });

Callers 1

readSessionDetailFunction · 0.85

Calls 5

popMethod · 0.80
pathExistsFunction · 0.70
resolveFunction · 0.50
readFileFunction · 0.50
readdirFunction · 0.50

Tested by

no test coverage detected