import { join } from "path" import { rename } from "fs/promises" export interface Session { branch: string worktree: string created_at: string prompt?: string } export interface State { sessions: Record } function statePath(repoRoot: string): string { return join(repoRoot, ".sandlot", "state.json") } export async function load(repoRoot: string): Promise { const path = statePath(repoRoot) const file = Bun.file(path) if (await file.exists()) { return await file.json() } return { sessions: {} } } export async function save(repoRoot: string, state: State): Promise { const path = statePath(repoRoot) const tmpPath = path + ".tmp" const dir = join(repoRoot, ".sandlot") await Bun.write(join(dir, ".gitkeep"), "") // ensure dir exists await Bun.write(tmpPath, JSON.stringify(state, null, 2) + "\n") await rename(tmpPath, path) } export async function getSession(repoRoot: string, branch: string): Promise { const state = await load(repoRoot) return state.sessions[branch] } export async function setSession(repoRoot: string, session: Session): Promise { const state = await load(repoRoot) state.sessions[session.branch] = session await save(repoRoot, state) } export async function removeSession(repoRoot: string, branch: string): Promise { const state = await load(repoRoot) delete state.sessions[branch] await save(repoRoot, state) }