|
| 1 | +import type { EnvironmentInfo } from "./environment.js"; |
| 2 | +import { DIRECTOR_REGISTRY } from "./directors/registry.js"; |
| 3 | +import { formatDirectorSystemPrompt } from "./directors/identity.js"; |
| 4 | +import { |
| 5 | + DIRECTOR_IDS, |
| 6 | + type DirectorId, |
| 7 | + type DirectorPackage, |
| 8 | +} from "./directors/types.js"; |
| 9 | +import { buildSubAgentSystemPrompt } from "./prompts.js"; |
| 10 | +import { shouldApplyGrokAntiThrash } from "../subagent/provider-family.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Canonical prompt-size fixture (CL-7664). |
| 14 | + * |
| 15 | + * Assembles each director prompt exactly as src/subagent/run.ts does: |
| 16 | + * extensions=[director systemPromptRole] + environment + tools + |
| 17 | + * appendix, with the Grok finish-bias note gated by |
| 18 | + * shouldApplyGrokAntiThrash (leaves on Grok-family providers only). |
| 19 | + * |
| 20 | + * The env and provider inputs are pinned here so sizes never drift with the |
| 21 | + * machine, date, or checkout — only real prompt changes move the numbers. |
| 22 | + */ |
| 23 | +export const CANONICAL_PROMPT_ENV: EnvironmentInfo = { |
| 24 | + cwd: "/repo", |
| 25 | + platform: "Darwin 25.0.0", |
| 26 | + arch: "arm64", |
| 27 | + runtime: "Bun 1.2.0", |
| 28 | + date: new Date("2026-01-15T12:00:00Z"), |
| 29 | + isGitRepo: true, |
| 30 | + gitBranch: "main", |
| 31 | + gitDirtyCount: 0, |
| 32 | + topLevel: "AGENTS.md CONTRIBUTING.md src/ tests/ docs/ plugins/", |
| 33 | +}; |
| 34 | + |
| 35 | +const GROK_PROVIDER = { providerName: "xai/default", model: "grok-4.6" }; |
| 36 | +const DEFAULT_PROVIDER = { |
| 37 | + providerName: "anthropic", |
| 38 | + model: "claude-sonnet-4", |
| 39 | +}; |
| 40 | + |
| 41 | +/** Families in the size table: default assembly vs Grok (+finish-bias note). */ |
| 42 | +export type PromptSizeFamily = "default" | "grok"; |
| 43 | + |
| 44 | +/** |
| 45 | + * Canonical tool names per director, mirroring the run.ts mount order: |
| 46 | + * package allowlist, then always-mounted manage_tasks, then leaf-only |
| 47 | + * submit_result + ask_director, then orchestrator fleet tools |
| 48 | + * (search_agents is Tier-1 skywalker only). |
| 49 | + */ |
| 50 | +export function canonicalToolNamesForDirector( |
| 51 | + pkg: DirectorPackage, |
| 52 | +): readonly string[] { |
| 53 | + const names = [...(pkg.tools?.allow ?? [])]; |
| 54 | + names.push("manage_tasks"); |
| 55 | + if (pkg.tier === "leaf") { |
| 56 | + names.push("submit_result", "ask_director"); |
| 57 | + } |
| 58 | + if (pkg.spawn.maySpawn) { |
| 59 | + if (pkg.tier === "orchestrator") names.push("search_agents"); |
| 60 | + names.push( |
| 61 | + "read_agent_trace", |
| 62 | + "spawn_agent", |
| 63 | + "wait_agents", |
| 64 | + "list_agents", |
| 65 | + "close_agent", |
| 66 | + "resume_agent", |
| 67 | + "interrupt_agent", |
| 68 | + "send_input", |
| 69 | + ); |
| 70 | + } |
| 71 | + return names; |
| 72 | +} |
| 73 | + |
| 74 | +/** Assemble one director prompt exactly as run.ts does. */ |
| 75 | +export function assembleDirectorPrompt( |
| 76 | + directorId: DirectorId, |
| 77 | + family: PromptSizeFamily, |
| 78 | +): string { |
| 79 | + const pkg = DIRECTOR_REGISTRY[directorId]; |
| 80 | + const orchestrator = pkg.spawn.maySpawn; |
| 81 | + const provider = family === "grok" ? GROK_PROVIDER : DEFAULT_PROVIDER; |
| 82 | + return buildSubAgentSystemPrompt( |
| 83 | + [formatDirectorSystemPrompt(pkg)], |
| 84 | + CANONICAL_PROMPT_ENV, |
| 85 | + undefined, |
| 86 | + { |
| 87 | + orchestrator, |
| 88 | + toolNames: canonicalToolNamesForDirector(pkg), |
| 89 | + grokAntiThrash: shouldApplyGrokAntiThrash({ ...provider, orchestrator }), |
| 90 | + }, |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +export interface DirectorPromptSize { |
| 95 | + directorId: DirectorId; |
| 96 | + family: PromptSizeFamily; |
| 97 | + chars: number; |
| 98 | + bytes: number; |
| 99 | +} |
| 100 | + |
| 101 | +export function measureDirectorPrompt( |
| 102 | + directorId: DirectorId, |
| 103 | + family: PromptSizeFamily, |
| 104 | +): DirectorPromptSize { |
| 105 | + const prompt = assembleDirectorPrompt(directorId, family); |
| 106 | + return { |
| 107 | + directorId, |
| 108 | + family, |
| 109 | + chars: prompt.length, |
| 110 | + bytes: Buffer.byteLength(prompt, "utf8"), |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** Full per-director x per-family size table. */ |
| 115 | +export function directorPromptSizeTable(): DirectorPromptSize[] { |
| 116 | + const rows: DirectorPromptSize[] = []; |
| 117 | + for (const directorId of DIRECTOR_IDS) { |
| 118 | + for (const family of ["default", "grok"] as const) { |
| 119 | + rows.push(measureDirectorPrompt(directorId, family)); |
| 120 | + } |
| 121 | + } |
| 122 | + return rows; |
| 123 | +} |
| 124 | + |
| 125 | +/** Render the size table as markdown (for PR bodies and budget updates). */ |
| 126 | +export function formatPromptSizeTable(rows: DirectorPromptSize[]): string { |
| 127 | + const lines = [ |
| 128 | + "| director | default chars (bytes) | grok chars (bytes) |", |
| 129 | + "| --- | --- | --- |", |
| 130 | + ]; |
| 131 | + for (const directorId of DIRECTOR_IDS) { |
| 132 | + const base = rows.find( |
| 133 | + (r) => r.directorId === directorId && r.family === "default", |
| 134 | + ); |
| 135 | + const grok = rows.find( |
| 136 | + (r) => r.directorId === directorId && r.family === "grok", |
| 137 | + ); |
| 138 | + lines.push( |
| 139 | + `| ${directorId} | ${base?.chars} (${base?.bytes}) | ${grok?.chars} (${grok?.bytes}) |`, |
| 140 | + ); |
| 141 | + } |
| 142 | + return lines.join("\n"); |
| 143 | +} |
0 commit comments