( sessionId: string, dir?: string, )
| 399 | * resolved path. |
| 400 | */ |
| 401 | export async function resolveSessionFilePath( |
| 402 | sessionId: string, |
| 403 | dir?: string, |
| 404 | ): Promise< |
| 405 | | { filePath: string; projectPath: string | undefined; fileSize: number } |
| 406 | | undefined |
| 407 | > { |
| 408 | const fileName = `${sessionId}.jsonl` |
| 409 | |
| 410 | if (dir) { |
| 411 | const canonical = await canonicalizePath(dir) |
| 412 | const projectDir = await findProjectDir(canonical) |
| 413 | if (projectDir) { |
| 414 | const filePath = join(projectDir, fileName) |
| 415 | try { |
| 416 | const s = await stat(filePath) |
| 417 | if (s.size > 0) |
| 418 | return { filePath, projectPath: canonical, fileSize: s.size } |
| 419 | } catch { |
| 420 | // ENOENT/EACCES — keep searching |
| 421 | } |
| 422 | } |
| 423 | // Worktree fallback — sessions may live under a different worktree root |
| 424 | let worktreePaths: string[] |
| 425 | try { |
| 426 | worktreePaths = await getWorktreePathsPortable(canonical) |
| 427 | } catch { |
| 428 | worktreePaths = [] |
| 429 | } |
| 430 | for (const wt of worktreePaths) { |
| 431 | if (wt === canonical) continue |
| 432 | const wtProjectDir = await findProjectDir(wt) |
| 433 | if (!wtProjectDir) continue |
| 434 | const filePath = join(wtProjectDir, fileName) |
| 435 | try { |
| 436 | const s = await stat(filePath) |
| 437 | if (s.size > 0) return { filePath, projectPath: wt, fileSize: s.size } |
| 438 | } catch { |
| 439 | // ENOENT/EACCES — keep searching |
| 440 | } |
| 441 | } |
| 442 | return undefined |
| 443 | } |
| 444 | |
| 445 | // No dir — scan all project directories |
| 446 | const projectsDir = getProjectsDir() |
| 447 | let dirents: string[] |
| 448 | try { |
| 449 | dirents = await readdir(projectsDir) |
| 450 | } catch { |
| 451 | return undefined |
| 452 | } |
| 453 | for (const name of dirents) { |
| 454 | const filePath = join(projectsDir, name, fileName) |
| 455 | try { |
| 456 | const s = await stat(filePath) |
| 457 | if (s.size > 0) |
| 458 | return { filePath, projectPath: undefined, fileSize: s.size } |
no test coverage detected