Simplify list command and inline resolveAllStatuses

Remove generic helper that obscured a three-line call site, flatten
try/catch nesting in backfillPrompts, and push results directly in
loadAll instead of collecting intermediate objects. Also export
normalizePath for use in actionRemove and drop redundant `acquired`
flag from the lock loop.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Wanstrath 2026-03-20 10:33:42 -07:00
parent e5f1de4717
commit 3ba27c80b4
2 changed files with 32 additions and 52 deletions

View File

@ -63,20 +63,9 @@ async function resolveStatus(
}
}
async function resolveAllStatuses<T extends { branch: string; worktree: string; in_review?: boolean; repoRoot: string }>(
sessions: T[],
vmRunning: boolean,
keyFn: (s: T) => string,
): Promise<Record<string, string>> {
const results = await Promise.all(sessions.map(s => resolveStatus(s, vmRunning)))
const statuses = Object.fromEntries(sessions.map((s, i) => [keyFn(s), results[i]]))
await clearStaleReviews(sessions, results)
return statuses
}
/** Clear in_review flags for sessions where Claude is no longer active. */
async function clearStaleReviews<T extends { branch: string; in_review?: boolean; repoRoot: string }>(
sessions: T[],
async function clearStaleReviews(
sessions: state.GlobalSession[],
results: string[],
) {
const staleByRepo = new Map<string, string[]>()
@ -101,14 +90,13 @@ async function backfillPrompts(sessions: { worktree: string; prompt?: string }[]
if (!vmRunning) return
const needsPrompt = sessions.filter(s => !s.prompt)
if (needsPrompt.length === 0) return
try {
const result = await vm.exec(homedir() + "/.sandlot", "cat /home/ubuntu/.claude/history.jsonl 2>/dev/null")
if (result.exitCode === 0 && result.stdout) {
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 entries = result.stdout.split("\n").filter(Boolean).map(line => {
try { return JSON.parse(line) } catch { return null }
}).filter(Boolean)
// Later entries overwrite earlier ones so the most recent prompt wins
const byProject = new Map<string, string>()
for (const e of entries) {
if (e.project && e.display) byProject.set(e.project, e.display)
@ -119,8 +107,6 @@ async function backfillPrompts(sessions: { worktree: string; prompt?: string }[]
if (display) s.prompt = display
}
}
} catch {}
}
// ── Commands ─────────────────────────────────────────────────────────
@ -149,7 +135,9 @@ export async function action(opts: { json?: boolean; all?: boolean; add?: string
// Key by repoRoot/branch to avoid collisions across repos with the same basename
const keyFn = (s: state.GlobalSession) => `${s.repoRoot}/${s.branch}`
const statuses = await resolveAllStatuses(sessions, vmRunning, keyFn)
const results = await Promise.all(sessions.map(s => resolveStatus(s, vmRunning)))
const statuses = Object.fromEntries(sessions.map((s, i) => [keyFn(s), results[i]]))
await clearStaleReviews(sessions, results)
if (opts.json) {
const withStatus = sessions.map(s => ({ ...s, status: statuses[keyFn(s)] }))
@ -189,6 +177,7 @@ async function actionAdd(dir: string) {
}
async function actionRemove(dir: string) {
const resolved = state.normalizePath(dir)
let removed = false
try {
removed = await state.unregisterProject(dir)
@ -196,8 +185,8 @@ async function actionRemove(dir: string) {
die("Could not acquire registry lock")
}
if (removed) {
console.log(`${red}-${reset} ${dir}`)
console.log(`${red}-${reset} ${resolved}`)
} else {
die(`Project not found in registry: ${dir}`)
die(`Project not found in registry: ${resolved}`)
}
}

View File

@ -66,11 +66,9 @@ const GLOBAL_STATE_PATH = join(GLOBAL_DIR, "registry.json")
async function withGlobalLock<T>(fn: () => Promise<T>): Promise<T> {
const lockPath = join(GLOBAL_DIR, "registry.lock")
await mkdir(GLOBAL_DIR, { recursive: true })
let acquired = false
for (let i = 0; i < 20; i++) {
try {
await mkdir(lockPath)
acquired = true
break
} catch {
// If the lock is older than 5 minutes, assume it's stale (crashed process)
@ -86,7 +84,6 @@ async function withGlobalLock<T>(fn: () => Promise<T>): Promise<T> {
await Bun.sleep(50)
}
}
if (!acquired) throw new Error("Could not acquire registry lock")
try {
return await fn()
} finally {
@ -113,7 +110,7 @@ async function saveGlobal(gs: GlobalState): Promise<void> {
await rename(tmpPath, GLOBAL_STATE_PATH)
}
function normalizePath(dir: string): string {
export function normalizePath(dir: string): string {
return resolve(dir.replace(/^~(?=\/|$)/, homedir()))
}
@ -198,32 +195,26 @@ export async function loadAll(): Promise<GlobalSession[]> {
const all: GlobalSession[] = []
const stale: string[] = []
const results = await Promise.all(gs.projects.map(async (project) => {
await Promise.all(gs.projects.map(async (project) => {
let st: State
try {
st = await load(project)
} catch {
return { project, stale: true } as const
// Corrupt state.json — skip but don't prune (self-heals on next session activity)
return
}
if (Object.keys(st.sessions).length === 0) {
const file = Bun.file(join(project, ".sandlot", "state.json"))
if (!(await file.exists())) {
return { project, stale: true } as const
if (!(await Bun.file(join(project, ".sandlot", "state.json")).exists())) {
stale.push(project)
return
}
}
const repo = basename(project)
const sessions = Object.values(st.sessions).map(s => ({ ...s, repo, repoRoot: project }))
return { project, stale: false, sessions } as const
for (const s of Object.values(st.sessions)) {
all.push({ ...s, repo, repoRoot: project })
}
}))
for (const r of results) {
if (r.stale) {
stale.push(r.project)
} else if ('sessions' in r) {
all.push(...r.sessions)
}
}
// Prune projects whose state files no longer exist
if (stale.length > 0) {
const staleSet = new Set(stale)