(home: string, sessionId: string)
| 301 | } |
| 302 | |
| 303 | async 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 | |
| 333 | async function scanWire(path: string): Promise<{ count: number; protocolVersion: string }> { |
| 334 | const stream = createReadStream(path, { encoding: 'utf8' }); |
no test coverage detected