import { createHash, randomUUID } from "node:crypto"; /** * Derive a stable 12-hex agent id from the FULL session id. * Hashing matters: pi session ids are time-ordered UUIDs whose leading chars encode a * timestamp, so any prefix collides for sessions launched in the same instant (e.g. cmux * opening several panes at once). A hash spreads the random bits across the whole id. * Stable across resume: same session id ⇒ same agent id. */ export function deriveSelfId(sessionId: string | undefined | null): string { if (sessionId && typeof sessionId === "string" && sessionId.trim()) { return createHash("sha1").update(sessionId).digest("hex").slice(0, 12); } return randomUUID().replace(/-/g, "").slice(0, 12); // ephemeral (--no-session) fallback }