@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
76 lines • 3.38 kB
JavaScript
import { existsSync } from "node:fs";
import { readdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { assertValidSessionId, SessionManager } from "../../core/session-manager.js";
import { canonicalSessionPath } from "./session-path-key.js";
/** Resolve anew on every invocation: a durable session can have a new routing handle. */
export async function resolveSessionRef(client, ref, agentDir) {
const rows = await client.listSessions();
const handle = rows.find((row) => row.sessionId === ref);
if (handle)
return { kind: "live", row: handle };
if (isSessionId(ref)) {
const byId = await resolveDurableId(ref, rows, agentDir);
if (byId)
return byId;
}
const named = rows.filter((row) => row.name === ref);
if (named.length > 1)
return { kind: "ambiguous", candidates: named.map((row) => row.sessionId) };
const live = named[0] ?? rows.find((row) => row.sessionPath !== undefined && samePath(row.sessionPath, ref));
if (live)
return { kind: "live", row: live };
return looksLikePath(ref) ? parkedByPath(ref) : { kind: "unknown" };
}
/**
* A durable id names ONE session only when every live row and every transcript carrying it is the
* same file: two transcripts with one id (a caller-minted id reused while the first was parked) are
* refused rather than resolved to whichever is listed first.
*/
async function resolveDurableId(id, rows, agentDir) {
const liveRows = rows.filter((row) => row.durableSessionId === id);
const transcripts = (await transcriptsUnder(agentDir)).filter((session) => session.id === id);
const files = new Set([
...liveRows.map((row) => (row.sessionPath === undefined ? row.sessionId : canonicalSessionPath(row.sessionPath))),
...transcripts.map((session) => canonicalSessionPath(session.path)),
]);
if (files.size > 1)
return { kind: "ambiguous", candidates: [...files] };
if (liveRows[0])
return { kind: "live", row: liveRows[0] };
return transcripts[0] ? parked(transcripts[0]) : undefined;
}
function isSessionId(ref) {
try {
assertValidSessionId(ref);
return true;
}
catch {
return false;
}
}
function looksLikePath(ref) {
return ref.includes("/") || ref.includes("\\") || ref.endsWith(".jsonl");
}
async function parkedByPath(ref) {
const path = canonicalSessionPath(ref);
if (!existsSync(path))
return { kind: "unknown" };
const info = (await SessionManager.listAll(dirname(path))).find((session) => samePath(session.path, path));
return info ? parked(info) : { kind: "unknown" };
}
/** The host keeps every transcript under `<agentDir>/sessions/<cwd-dir>/`. */
async function transcriptsUnder(agentDir) {
const root = join(agentDir, "sessions");
if (!existsSync(root))
return [];
const dirs = (await readdir(root, { withFileTypes: true })).filter((entry) => entry.isDirectory());
return (await Promise.all(dirs.map((entry) => SessionManager.listAll(join(root, entry.name))))).flat();
}
function parked(info) {
return { kind: "parked", sessionPath: info.path, cwd: info.cwd, durableSessionId: info.id };
}
function samePath(left, right) {
return canonicalSessionPath(left) === canonicalSessionPath(right);
}
//# sourceMappingURL=host-session-ref.js.map