import { formatRow, type Roster, type SectionName } from "./roster.ts"; export const BRIEF_FILE_NAME = "goal-brief.md"; const MAX_OBJECTIVE_LENGTH = 4000; /** Tiers in the order the supervisor tries them. */ const ROUTING_ORDER: readonly SectionName[] = ["cheap", "priority", "fallback"]; const TIER_HEADINGS: Record = { cheap: "Cheap (try first)", priority: "Priority (when no cheap row is strong enough)", fallback: "Fallback (only after a usage or rate limit)", }; const SCORING = `## Scoring Score every ready task in one call. \`TASKS\` holds one line per task, for example \`"T01: "\`. \`\`\`js const RUBRIC = [ "1 - trivial: routine daily-driver work, no design choices", "2 - easy: daily-driver with one small wrinkle", "3 - moderate: still daily-driver, but needs care", "4 - meaningfully hard: novel design, cross-cutting, real blast radius", "5 - very rare: hardest tier, needs deep investigation first", ]; const res = await tools.ask_sai({ state: TASKS.map((t, i) => \`t\${i}: \${t}\`).join("\\n"), questions: Object.fromEntries(TASKS.map((t, i) => [\`t\${i}\`, { type: "score", instructions: \`Difficulty tier of this task on the 1-5 scale: \${t}\`, criteria: RUBRIC, focus: "Rate intrinsic difficulty, not time spent or priority.", }])), }); // Real difficulty = score + (1 - confidence). // Example: score = 3.36 with confidence 0.67 -> 3.69. const difficulty = (i) => { const answer = res.answers?.[\`t\${i}\`]; if (!answer) return null; // unscored: treat as above 5 return answer.score + (1 - (answer.confidence ?? 0)); }; \`\`\``; const ROUTING = `## Routing - Each model row is: max difficulty | provider | model | effort | max concurrent writers. - A row can take a task whose real difficulty is at most the row's max difficulty. - Try tiers in this order: Cheap, then Priority, then Fallback. Skip a tier that has no rows. - Within a tier, pick the row with the lowest max difficulty that is still high enough. On a tie, take the row listed first. - Use Priority only when no Cheap row is strong enough, or when the Cheap model hits a usage or rate limit. - Use Fallback only when the Priority model hits a usage or rate limit. - Max concurrent writers counts per provider/model, summed across all rows that use that model. When every suitable model in the chosen tier is at its limit, wait for a slot; do not switch tiers for that. - an unscored task, or a task no row can take: do it yourself as the supervisor. Do not ask the user. - Launch each writer with \`model: "/:"\`.`; export function renderBrief({ taskDir, roster, implementerSkillPath, }: { taskDir: string; roster: Roster; implementerSkillPath: string; }): string { const models = ROUTING_ORDER.filter((section) => roster.sections[section].length > 0).map( (section) => `### ${TIER_HEADINGS[section]}\n\n${roster.sections[section].map((row) => `- ${formatRow(row)}`).join("\n")}`, ); return [ "# Goal", `Implement every task in \`${taskDir}/todo.md\` as specified in \`${taskDir}/plan.md\`. Make sure every writer's work matches the full plan. Do not stop until every task is accepted and the final audit passes.`, "## Procedure", [ `1. Read the skill at \`${implementerSkillPath}\` fully and follow it. This brief is the user's writer model and effort specification that the skill requires; do not ask for it again.`, "2. Commit only when a worktree writer needs a clean checkout, with a `wip:` message. Do not squash, amend, or reset commits; after `goal_complete`, /implement offers the user a squash.", "3. Score the difficulty of each ready task (see Scoring).", "4. Pick a model for each task (see Routing).", "5. Every writer brief must say: if the task is harder than described or you are blocked, call `contact_supervisor` with `need_decision`; do not guess.", "6. Test only what each task's Validation line requires. Do not run full test suites; time is tight.", '7. Before calling `goal_complete`, remove the worktrees this run created: `subagent({ action: "worktree.cleanup", options: { repo: "", mode: "plan" } })`, then the same call with `mode: "apply"` and the plan\'s `planId`. Name any worktree the plan keeps in the completion summary.', ].join("\n"), SCORING, ROUTING, "## Models", ...models, ].join("\n\n") + "\n"; } export function buildObjective(taskDir: string): string { const objective = `Implement the plan in ${taskDir} exactly as specified in ${taskDir}/${BRIEF_FILE_NAME}; read it and the skill file it names first.`; if (objective.length >= MAX_OBJECTIVE_LENGTH) { throw new Error(`Goal objective must be shorter than ${MAX_OBJECTIVE_LENGTH} characters.`); } return objective; }