sandlot/src/commands/review.ts
Chris Wanstrath e8e5b850a0 Fix race conditions in state persistence by reading fresh session before writing
Batch-clearing stale flags in list and blindly writing back the session
in review could clobbер concurrent changes. Use per-session get/set
instead of whole-state load/save, and re-read before clearing in_review.
2026-03-19 11:24:18 -07:00

94 lines
2.8 KiB
TypeScript

import * as vm from "../vm.ts"
import * as state from "../state.ts"
import { spinner } from "../spinner.ts"
import { requireSession, saveChanges } from "./helpers.ts"
export async function action(branch: string, extra: string | undefined, opts: { print?: boolean }) {
const { root, session } = await requireSession(branch)
const spin = spinner("Starting container", branch)
await vm.ensure((msg) => { spin.text = msg })
let prompt = `
You're a grumpy old senior software engineer. You need to review some code my co-worker wrote.
Launch four agents to review the diff between this branch and main with the following specializations:
1. Checks CLAUDE.md compliance
2. Looks specifically for bugs
3. Also looks specifically for bugs
4. Looks for opportunities to simplify code
Have them focus only on the diff! + lines are added in this branch, - lines are overwritten or deleted. They must focus mostly on the + lines.
Each agent should deliver you a report in this format (the <tags> are just for you, not part of their output):
<agentOutput>
# Problem Identified
Description of problem.
</agentOutput>
Tell each agent: Run \`git diff main...HEAD\` and focus on the "+" lines, not the "-" lines.
Once the agents are done, look at all their suggestions and let me know what you think.
Give me your opinion in this format, with the :
<grumpySeniorDevResponse>
# {branch name} Review
**OK TO SHIP: yes or no**
# Showstoppers
1. BUG: Bug that both bug hunters found.
2. BUG: Bug that one of the hunters found.
3. COMPLIANCE: Describe CLAUDE.md compliance issue.
# Recommendations
4. BUG: Bug that both bug hunters found.
5. BUG: Bug that one of the hunters found.
6. COMPLIANCE: Describe CLAUDE.md compliance issue.
7. SIMPLIFY: Opportunities to simplify code.
# Optional
8. BUG: Bug that both bug hunters found.
9. BUG: Bug that one of the hunters found.
10. COMPLIANCE: Describe CLAUDE.md compliance issue.
11. SIMPLIFY: Opportunities to simplify code.
# Summary
Your thoughts, in brief.
</grumpySeniorDevResponse>
`
if (extra) prompt += "\n\n" + extra
session.in_review = true
await state.setSession(root, session)
try {
if (opts.print) {
spin.text = "Running review…"
const result = await vm.claude(session.worktree, { print: prompt })
if (result.output) process.stdout.write(result.output + "\n")
} else {
spin.succeed("Session ready")
await vm.claude(session.worktree, { prompt })
}
} finally {
spin.stop()
// Load fresh session to avoid clobbering changes made during the review
const fresh = await state.getSession(root, session.branch)
if (fresh) {
fresh.in_review = false
await state.setSession(root, fresh).catch(() => {})
}
if (!opts.print) await saveChanges(session.worktree, session.branch).catch(() => {})
}
}