| name | megacode-best-of-n |
| description | Plan N independent worktree attempts for exploration or implementation, plus judge criteria to select a winner. Use for best-of-n-explore workflows. |
| disable-model-invocation | true |
MegaCode Best-of-N
Set up N independent attempts in isolated worktrees and define how a judge picks the best outcome.
Inputs
- Task or exploration goal.
- N (typically 2–4; respect
max_concurrency and worktree policy).
- Constraints: time budget, models allowed, read-only vs write attempts.
- Evaluation criteria (correctness, simplicity, test pass, performance).
Output format
1. Attempt matrix
| Attempt | Branch | Worktree path | Profile | Model | Strategy summary |
|---|
| A | megacode/bon-<run>-A | .megacode/worktrees/bon-<run>-A | implementer | ... | ... |
2. Isolation rules
- One worktree per attempt; unique branch names tied to run ID.
- No shared uncommitted state between attempts.
- Record metadata: base SHA, owner profile, created time.
- Stop all attempts before any foldback.
3. Execution phases (workflow sketch)
phases:
- id: spawn-attempts
type: codex_batch
profile: implementer
concurrency: <N>
worktree: per_attempt
- id: local-verify-each
type: shell
depends_on: [spawn-attempts]
verification:
- pnpm verify
- id: judge
type: codex_run
profile: reviewer
depends_on: [local-verify-each]
context_policy: artifact_only
4. Judge prompt template
The judge receives artifacts only (diffs, test output, summaries) and returns:
{
"winner": "A",
"ranking": ["A", "C", "B"],
"criteria_scores": {},
"rationale": "...",
"losers_cleanup": "safe | keep_for_review"
}
5. Foldback
- Only the winner worktree proceeds to foldback (
megacode-foldback checklist).
- Losers: stop agents, archive or delete per policy — never delete dirty worktrees without explicit discard.
Rules
- Parallel implementation requires worktree safety rules from AGENTS.md.
- Local verify on every attempt before judging.
- No GitHub Actions comparison — judge uses local test results and diffs only.