sandlot/src/commands/review.ts
Chris Wanstrath 69ba73b3c3 Use patchSession to avoid race conditions in review flag updates
The load-modify-save pattern could overwrite concurrent state changes.
patchSession does an atomic read-patch-write, and the list command now
re-checks activity before clearing stale flags to avoid racing with a
review that just started.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-19 10:56:50 -07:00

90 lines
2.7 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
await state.patchSession(root, session.branch, { in_review: true })
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()
// Clear review flag before saveChanges to minimize the race window
await state.patchSession(root, session.branch, { in_review: false }).catch(() => {})
// Save worktree changes only in interactive mode
if (!opts.print) await saveChanges(session.worktree, session.branch)
}
}