import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { getTerminalId } from "@oh-my-pi/pi-tui"; import { getSessionsDir, getTerminalSessionsDir, isEnoent, logger, resolveEquivalentPath } from "@oh-my-pi/pi-utils"; import type { SessionStorage } from "./session-storage"; const migratedSessionRoots = new Set(); /** * Merge or rename a legacy session directory into its canonical target. * Best effort: callers decide whether migration failures should surface. */ function migrateSessionDirPath(oldPath: string, newPath: string): void { const existing = fs.statSync(newPath, { throwIfNoEntry: false }); if (existing?.isDirectory()) { for (const file of fs.readdirSync(oldPath)) { const src = path.join(oldPath, file); const dst = path.join(newPath, file); if (!fs.existsSync(dst)) { fs.renameSync(src, dst); } } fs.rmSync(oldPath, { recursive: true, force: true }); return; } if (existing) { fs.rmSync(newPath, { recursive: true, force: true }); } fs.renameSync(oldPath, newPath); } function encodeLegacyAbsoluteSessionDirName(cwd: string): string { const resolvedCwd = path.resolve(cwd); return `--${resolvedCwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`; } function encodeRelativeSessionDirName(prefix: string, relative: string): string { const encoded = relative.replace(/[/\\:]/g, "-"); return encoded ? (prefix.endsWith("-") ? `${prefix}${encoded}` : `${prefix}-${encoded}`) : prefix; } function getDefaultSessionDirName(cwd: string): { encodedDirName: string; resolvedCwd: string } { const resolvedCwd = path.resolve(cwd); const canonicalCwd = resolveEquivalentPath(resolvedCwd); const home = os.homedir(); const canonicalHome = resolveEquivalentPath(home); const tempRoot = os.tmpdir(); const canonicalTempRoot = resolveEquivalentPath(tempRoot); const homeRelative = path.relative(canonicalHome, canonicalCwd); const tempRelative = path.relative(canonicalTempRoot, canonicalCwd); const encodedDirName = homeRelative === "" || (!homeRelative.startsWith("..") && !path.isAbsolute(homeRelative)) ? encodeRelativeSessionDirName("-", homeRelative) : tempRelative === "" || (!tempRelative.startsWith("..") && !path.isAbsolute(tempRelative)) ? encodeRelativeSessionDirName("-tmp", tempRelative) : encodeLegacyAbsoluteSessionDirName(canonicalCwd); return { encodedDirName, resolvedCwd }; } /** * Migrate old `---*--` session dirs to the new `-*` format. * Runs once per sessions root on first access, best-effort. */ function migrateHomeSessionDirs(sessionsRoot: string): void { if (migratedSessionRoots.has(sessionsRoot)) return; migratedSessionRoots.add(sessionsRoot); const home = os.homedir(); const homeEncoded = home.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-"); const oldPrefix = `--${homeEncoded}-`; const oldExact = `--${homeEncoded}--`; let entries: string[]; try { entries = fs.readdirSync(sessionsRoot); } catch { return; } for (const entry of entries) { let remainder: string; if (entry === oldExact) { remainder = ""; } else if (entry.startsWith(oldPrefix) && entry.endsWith("--")) { remainder = entry.slice(oldPrefix.length, -2); } else { continue; } const newName = remainder ? `-${remainder}` : "-"; const oldPath = path.join(sessionsRoot, entry); const newPath = path.join(sessionsRoot, newName); try { migrateSessionDirPath(oldPath, newPath); } catch { // Best effort } } } function migrateLegacyAbsoluteSessionDir(cwd: string, sessionDir: string, sessionsRoot: string): void { const legacyDir = path.join(sessionsRoot, encodeLegacyAbsoluteSessionDirName(cwd)); if (legacyDir === sessionDir || !fs.existsSync(legacyDir)) return; try { migrateSessionDirPath(legacyDir, sessionDir); } catch { // Best effort } } export function resolveManagedSessionRoot(sessionDir: string, cwd: string): string | undefined { const currentDirName = path.basename(sessionDir); const { encodedDirName } = getDefaultSessionDirName(cwd); if (currentDirName !== encodedDirName && currentDirName !== encodeLegacyAbsoluteSessionDirName(cwd)) { return undefined; } return path.dirname(sessionDir); } /** * Compute the default session directory for a cwd. * Classifies cwd by canonical location so symlink/alias paths resolve to the * same home-relative or temp-root directory names as their real targets. */ export function computeDefaultSessionDir( cwd: string, storage: SessionStorage, sessionsRoot: string = getSessionsDir(), ): string { const { encodedDirName, resolvedCwd } = getDefaultSessionDirName(cwd); migrateHomeSessionDirs(sessionsRoot); const sessionDir = path.join(sessionsRoot, encodedDirName); migrateLegacyAbsoluteSessionDir(resolvedCwd, sessionDir, sessionsRoot); storage.ensureDirSync(sessionDir); return sessionDir; } // ============================================================================= // Terminal breadcrumbs: maps terminal (TTY) -> last session file for --continue // ============================================================================= /** * Write a breadcrumb linking the current terminal to a session file. * The breadcrumb contains the cwd and session path so --continue can * find "this terminal's last session" even when running concurrent instances. * * `fresh` marks a `/new` (or freshly-minted) session boundary whose JSONL is * not yet materialized (new-session persistence is lazy until assistant output * exists). A fresh breadcrumb is honored by {@link readTerminalBreadcrumbEntry} * even when its target file is still absent, so relaunch/auto-resume reopens the * post-`/new` session instead of falling back to the pre-`/new` transcript. Once * the session materializes the caller rewrites the breadcrumb with `fresh:false` * so a later external delete is still treated as a genuinely stale crumb. */ export function writeTerminalBreadcrumb(cwd: string, sessionFile: string, fresh = false): void { const terminalId = getTerminalId(); if (!terminalId) return; const breadcrumbDir = getTerminalSessionsDir(); const breadcrumbFile = path.join(breadcrumbDir, terminalId); const content = fresh ? `${cwd}\n${sessionFile}\nfresh\n` : `${cwd}\n${sessionFile}\n`; // Synchronous + best-effort. Infrequent (session create/switch/reset, never // per-append), and writing in order matters: a lazy `/new` fresh crumb is // re-stamped non-fresh the instant the session materializes, so an async // fire-and-forget could land the two writes out of order and leave a // materialized session marked fresh. try { fs.mkdirSync(breadcrumbDir, { recursive: true }); fs.writeFileSync(breadcrumbFile, content); } catch (err) { if (!isEnoent(err)) logger.debug("Terminal breadcrumb write failed", { err }); } } export interface TerminalBreadcrumb { cwd: string; sessionFile: string; /** The recorded session file exists on disk right now. */ exists: boolean; /** Recorded as a `/new` fresh-session boundary whose JSONL may not exist yet. */ fresh: boolean; } /** * Read the raw terminal breadcrumb for the current terminal. * Returns the recorded cwd + session file regardless of whether the recorded * cwd still matches the current one. Callers decide how to interpret a cwd * mismatch (e.g. a moved/renamed worktree). * * A missing target file yields `null` UNLESS the breadcrumb is a `fresh` * boundary — a lazy `/new` session whose JSONL was never written — in which case * the entry is returned with `exists:false` so the caller can distinguish it * from a genuinely stale/deleted breadcrumb. */ export async function readTerminalBreadcrumbEntry(): Promise { const terminalId = getTerminalId(); if (!terminalId) return null; try { const breadcrumbFile = path.join(getTerminalSessionsDir(), terminalId); const content = await Bun.file(breadcrumbFile).text(); const lines = content.trim().split("\n"); if (lines.length < 2) return null; const breadcrumbCwd = lines[0]; const sessionFile = lines[1]; const fresh = lines[2] === "fresh"; const stat = fs.statSync(sessionFile, { throwIfNoEntry: false }); const exists = stat?.isFile() === true; // A materialized target resumes normally; a missing target is honored only // for a fresh `/new` boundary (never-written lazy session). if (exists || fresh) return { cwd: breadcrumbCwd, sessionFile, exists, fresh }; } catch (err) { if (!isEnoent(err)) logger.debug("Terminal breadcrumb read failed", { err }); // Breadcrumb doesn't exist or is corrupt — fall through } return null; }