UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

143 lines • 6.74 kB
import { existsSync, statSync } from "node:fs"; import { readdir } from "node:fs/promises"; import { join } from "node:path"; import { getSessionsDir } from "../config.js"; import { normalizePath, resolvePath } from "../utils/paths.js"; import { sessionCwdMatcher } from "./moved-session-cwd.js"; import { compareRepositoryIdentities, readRepositoryIdentity } from "./repository-identity.js"; import { listSessionsFromDir } from "./session-discovery.js"; import { findMostRecentSession, getDefaultSessionDir } from "./session-manager.js"; import { readFileLines } from "./session-summary.js"; function vanishedClassifier(cwd, current) { const ownCwd = sessionCwdMatcher(cwd); const kind = (session) => { if (!session.cwd || resolvePath(session.cwd) === cwd || existsSync(session.cwd)) return undefined; if (ownCwd(session.cwd)) return "here"; if (current === undefined) return undefined; return compareRepositoryIdentities(session.repositoryIdentity, current) === "same" ? "moved" : undefined; }; return { ownCwd, kind }; } function newestFirst(a, b) { return b.modified.getTime() - a.modified.getTime(); } // A session dir that cannot be read contributes nothing; it never fails a listing or --continue (senpi#2990 // review L4). Every error is skipped, as findMostRecentSession and listSessionsFromDir already do: --continue // reads other folders' dirs before its own, so one broken unrelated dir must not block the user's own session. async function readableEntries(dir) { try { return await readdir(dir, { withFileTypes: true }); } catch { return []; } } async function recordedCwd(dir) { const names = (await readableEntries(dir)).map((entry) => entry.name).filter((name) => name.endsWith(".jsonl")); for (const name of names) { let firstLine; try { await readFileLines(join(dir, name), (line) => { firstLine = line; return false; }); const header = JSON.parse(firstLine ?? ""); if (typeof header === "object" && header !== null && "cwd" in header && typeof header.cwd === "string") { return header.cwd; } } catch (error) { if (!(error instanceof Error)) throw error; } } return undefined; } // Each project directory holds one cwd, so one header decides whether its sessions can be moved ones; only // directories whose recorded path is gone and that `keep` accepts are listed in full. async function sessionsAtVanishedPaths(cwd, keep) { const root = getSessionsDir(); if (!existsSync(root)) return []; const own = normalizePath(getDefaultSessionDir(cwd)); const sessions = []; for (const entry of await readableEntries(root)) { if (!entry.isDirectory()) continue; const dir = join(root, entry.name); if (normalizePath(dir) === own) continue; const recorded = await recordedCwd(dir); if (recorded === undefined || existsSync(recorded) || !keep(recorded)) continue; sessions.push(...(await listSessionsFromDir(dir))); } return sessions; } /** Sessions at vanished paths of the `wanted` kinds, newest first; the identity is read only when "moved" is wanted. */ async function vanishedSessions(cwd, options, wanted) { const found = { here: [], moved: [] }; const here = resolvePath(cwd); const wantHere = wanted.includes("here"); const current = wanted.includes("moved") ? await (options.readIdentity ?? readRepositoryIdentity)(here) : undefined; if (!wantHere && current === undefined) return found; const { ownCwd, kind } = vanishedClassifier(here, current); const candidates = options.sessionDir ? await listSessionsFromDir(normalizePath(options.sessionDir)) : await sessionsAtVanishedPaths(here, (recorded) => current !== undefined || ownCwd(recorded)); for (const session of candidates) { const sessionKind = kind(session); if (sessionKind === "moved") found.moved.push({ ...session, moved: true }); else if (sessionKind === "here" && wantHere) found.here.push(session); } found.here.sort(newestFirst); found.moved.sort(newestFirst); return found; } export async function listMovedSessions(cwd, options = {}) { return (await vanishedSessions(cwd, options, ["moved"])).moved; } // findMostRecentSession's basis; a file that vanished since it was listed counts as oldest. function mtimeMs(path) { return statSync(path, { throwIfNoEntry: false })?.mtimeMs ?? 0; } /** * `--continue` in the default per-folder layout (senpi#2990): the newest session the OmO desktop moved to `cwd` when it * is newer than the folder's own newest, else undefined. It lists only the dirs whose recorded path moved here and * reads no git identity, so a folder with its own session pays one header read per project dir. */ export async function movedHereSessionToContinue(cwd) { const { here } = await vanishedSessions(cwd, {}, ["here"]); const newest = here .map((session) => session.path) .reduce((best, path) => (best === undefined || mtimeMs(path) > mtimeMs(best) ? path : best), undefined); if (newest === undefined) return undefined; const own = findMostRecentSession(getDefaultSessionDir(cwd)); return own === null || mtimeMs(newest) > mtimeMs(own) ? newest : undefined; } export async function markMovedSessions(sessions, cwd, options = {}) { const here = resolvePath(cwd); const current = await (options.readIdentity ?? readRepositoryIdentity)(here); if (current === undefined) return [...sessions]; const { kind } = vanishedClassifier(here, current); return sessions.map((session) => (kind(session) === "moved" ? { ...session, moved: true } : session)); } export async function withMovedSessions(local, cwd, options = {}) { // A shared session dir's own listing already holds the sessions the desktop moved here, so only the default // per-folder layout looks for them elsewhere. const wanted = options.sessionDir ? ["moved"] : ["here", "moved"]; const [own, vanished] = await Promise.all([local, vanishedSessions(cwd, options, wanted)]); // Defensive dedupe: a session listed by both lookups stays one row, the folder's own. const listed = new Set(own.map((session) => session.path)); const extra = [...vanished.here, ...vanished.moved].filter((session) => !listed.has(session.path)); return [...own, ...extra].sort(newestFirst); } //# sourceMappingURL=moved-sessions.js.map