// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2026 avtc /** * A feature's tracked session files can come from more than one host when hosts share a data * root. Each host keeps its sessions under its own root (`//`), * and a session can only be resumed by the host that wrote it. */ import path from "node:path"; /** The folder two levels above a session file: the host's sessions root. */ function sessionsRoot(file: string): string { const root = path.dirname(path.dirname(path.resolve(file))); return process.platform === "win32" ? root.toLowerCase() : root; } /** * The last tracked session file written by the current host: the last entry that shares the * current session file's sessions root. Without a current session file, the last entry. */ export function lastHostSessionFile(files: readonly string[] | null, currentFile: string | null): string | undefined { if (!files || files.length === 0) return undefined; if (!currentFile) return files.at(-1); const currentRoot = sessionsRoot(currentFile); for (let i = files.length - 1; i >= 0; i--) { const file = files[i]; if (file !== undefined && sessionsRoot(file) === currentRoot) return file; } return undefined; }