sandlot/src/commands/list.ts

145 lines
5.6 KiB
TypeScript

import { basename } from "path"
import { homedir } from "os"
import { stat } from "fs/promises"
import * as git from "../git.ts"
import * as vm from "../vm.ts"
import * as state from "../state.ts"
import { reset, dim, green, yellow, cyan, magenta, red } from "../fmt.ts"
// ── Rendering ────────────────────────────────────────────────────────
const styles: Record<string, { icon: string; color: string }> = {
idle: { icon: `${dim}${reset}`, color: dim },
active: { icon: `${cyan}${reset}`, color: cyan },
dirty: { icon: `${yellow}${reset}`, color: yellow },
saved: { icon: `${green}${reset}`, color: green },
review: { icon: `${magenta}⦿${reset}`, color: magenta },
}
function renderSessions(
sessions: state.GlobalSession[],
statusMap: Map<state.GlobalSession, string>,
) {
const branchWidth = Math.max(6, ...sessions.map(s => s.branch.length))
const cols = process.stdout.columns || 80
const prefixWidth = branchWidth + 4
console.log(` ${dim}${"BRANCH".padEnd(branchWidth)} PROMPT${reset}`)
for (const s of sessions) {
const prompt = (s.prompt ?? "").split("\n")[0]
const status = statusMap.get(s) ?? "idle"
const { icon, color: bc } = styles[status]
const maxPrompt = cols - prefixWidth
const truncated = maxPrompt <= 3 ? "" : prompt.length <= maxPrompt ? prompt : prompt.slice(0, maxPrompt - 3) + "..."
console.log(`${icon} ${bc}${s.branch.padEnd(branchWidth)}${reset} ${dim}${truncated}${reset}`)
}
}
// ── Status resolution ────────────────────────────────────────────────
async function resolveStatus(
s: { branch: string; worktree: string; in_review?: boolean },
vmRunning: boolean,
): Promise<string> {
try { await stat(s.worktree) } catch { return "idle" }
if (vmRunning) {
const active = await vm.isClaudeActive(s.worktree, s.branch).catch(() => false)
if (active && s.in_review) return "review"
if (active) return "active"
}
try {
const dirty = await git.isDirty(s.worktree)
if (dirty) return "dirty"
const commits = await git.hasNewCommits(s.worktree)
return commits ? "saved" : "idle"
} catch {
return "idle"
}
}
/** Clear in_review flags for sessions where Claude is no longer active. */
async function clearStaleReviews(
sessions: state.GlobalSession[],
statusMap: Map<state.GlobalSession, string>,
) {
const stale = sessions.filter(s => s.in_review && statusMap.get(s) !== "review")
if (stale.length === 0) return
const byRepo = Map.groupBy(stale, s => s.repoRoot)
for (const [repoRoot, staleSessions] of byRepo) {
const fresh = await state.load(repoRoot)
for (const s of staleSessions) {
if (fresh.sessions[s.branch]) fresh.sessions[s.branch].in_review = false
}
await state.save(repoRoot, fresh).catch(() => {})
}
}
async function backfillPrompts(sessions: { worktree: string; prompt?: string }[], vmRunning: boolean) {
if (!vmRunning) return
const needsPrompt = sessions.filter(s => !s.prompt)
if (needsPrompt.length === 0) return
const result = await vm.exec(homedir() + "/.sandlot", "cat /home/ubuntu/.claude/history.jsonl 2>/dev/null").catch(() => null)
if (!result || result.exitCode !== 0 || !result.stdout) return
const byProject = new Map<string, string>()
for (const line of result.stdout.split("\n")) {
if (!line) continue
try {
const e = JSON.parse(line)
if (e.project && e.display) byProject.set(e.project, e.display)
} catch {}
}
for (const s of needsPrompt) {
const display = byProject.get(vm.containerPath(s.worktree))
if (display) s.prompt = display
}
}
// ── Command ──────────────────────────────────────────────────────────
export async function action(opts: { json?: boolean; all?: boolean }) {
let sessions: state.GlobalSession[]
if (opts.all) {
sessions = await state.loadAll()
} else {
const root = await git.repoRoot()
const st = await state.load(root)
sessions = Object.values(st.sessions).map(s => ({ ...s, repoRoot: root }))
}
const vmRunning = (await vm.status()) === "running"
if (sessions.length === 0 && !opts.json) {
console.log(opts.all ? "◆ No active sessions across any project." : "◆ No active sessions.")
if (!opts.all && !vmRunning) console.log(`\n${red}VM is not running.${reset}`)
return
}
await backfillPrompts(sessions, vmRunning)
const results = await Promise.all(sessions.map(s => resolveStatus(s, vmRunning)))
const statusMap = new Map(sessions.map((s, i) => [s, results[i]]))
await clearStaleReviews(sessions, statusMap)
if (opts.json) {
const withStatus = sessions.map(s => ({ ...s, status: statusMap.get(s) ?? "idle" }))
console.log(JSON.stringify(withStatus, null, 2))
return
}
if (opts.all) {
const byRepo = Map.groupBy(sessions, s => s.repoRoot)
const sorted = [...byRepo.entries()].sort((a, b) => basename(a[0]).localeCompare(basename(b[0])))
for (const [repoRoot, repoSessions] of sorted) {
console.log(`\n${dim}── ${reset}${basename(repoRoot)}${dim} ──${reset}`)
renderSessions(repoSessions, statusMap)
}
} else {
renderSessions(sessions, statusMap)
}
console.log(`\n${dim}◯ idle${reset} · ${cyan}◎ active${reset} · ${yellow}◐ unsaved${reset} · ${green}● saved${reset} · ${magenta}⦿ review${reset}`)
if (!vmRunning) console.log(`\n${red}VM is not running.${reset}`)
}