fix: preserve legacy session route lookups

This commit is contained in:
Federico Jaramillo Martinez
2026-06-10 22:42:55 +02:00
parent 71510444c4
commit b99143f757
13 changed files with 350 additions and 129 deletions
+23 -1
View File
@@ -1,3 +1,5 @@
import type { Dirent } from "node:fs";
import { readdir } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, isAbsolute, join, resolve } from "node:path";
import { getAgentDir, SessionManager, SettingsManager } from "@earendil-works/pi-coding-agent";
@@ -27,6 +29,10 @@ export class SessionDirResolver {
this.env = options.env ?? process.env;
}
defaultSessionsRoot(): string {
return defaultPiSessionsRoot(this.agentDir);
}
resolve(cwd: string): SessionDirResolution {
const envSessionDir = this.env[PI_SESSION_DIR_ENV];
if (envSessionDir !== undefined && envSessionDir !== "") {
@@ -61,6 +67,10 @@ class SettingsAwarePiSessionManagerGateway implements PiSessionManagerGateway {
return SessionManager.create(cwd, resolution.sessionDir);
}
listAll(): Promise<PiSessionListEntry[]> {
return listSessionsInDefaultPiStore(this.resolver.defaultSessionsRoot());
}
open(path: string): PiSessionManager {
return SessionManager.open(path, dirname(path));
}
@@ -70,6 +80,19 @@ export async function listSessionsInDir(sessionDir: string): Promise<PiSessionLi
return SessionManager.list("", sessionDir);
}
export async function listSessionsInDefaultPiStore(storeRoot = defaultPiSessionsRoot()): Promise<PiSessionListEntry[]> {
let entries: Dirent[];
try {
entries = await readdir(storeRoot, { withFileTypes: true });
} catch {
return [];
}
const sessionDirs = entries.filter((entry) => entry.isDirectory()).map((entry) => join(storeRoot, entry.name));
const sessions = (await Promise.all(sessionDirs.map((dir) => listSessionsInDir(dir)))).flat();
return sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
}
export function filterSessionsForCwd(sessions: readonly PiSessionListEntry[], cwd: string): PiSessionListEntry[] {
return sessions.filter((session) => session.cwd === cwd);
}
@@ -97,4 +120,3 @@ function expandTildePath(path: string): string {
if (path.startsWith("~/")) return join(homedir(), path.slice(2));
return path;
}