Setting the file. One moment.
Budget Summary · Vercel Optimize · vercel-labs/agent-skills · Skills Docs
ContentsBack to the top of the page Deep Dive
Size
7 KB lib/ budget-summary.mjs
JavaScript · 182 lines · 7 KB
const
toLaunch
=
Array.
isArray
(gate?.toLaunch)
?
gate.toLaunch
:
[];
10 const gated = Array. isArray (gate?.gated) ? gate.gated : [];
11 const budgetSource = gate?.budget?.source ?? 'default' ;
12 const currentBudget =
13 typeof gate?.budget?.maxCandidates === 'number'
14 ? gate.budget.maxCandidates
15 : (gate?.budget?.maxCandidates === 'all' ? Infinity : 6 );
16
17 // Only budget skips can be reached by raising the budget; disqualified/coveredBy can't.
18 const skippedByBudget = gated. filter (( g ) =>
19 typeof g.gatedReason === 'string' && g.gatedReason. startsWith ( 'skippedByBudget' )
20 );
21 const skipped = skippedByBudget. length ;
22 const totalPassed = toLaunch. length + skipped;
23
24 const reasonParts = [];
25 if (budgetSource !== 'default' ) reasonParts. push ( `user pre-set budget via ${ budgetSource }` );
26 if (skipped === 0 ) reasonParts. push ( 'no candidates skipped by budget' );
27 const shouldAsk = budgetSource === 'default' && skipped > 0 ;
28 const reason = shouldAsk
29 ? `default budget skipped ${ skipped } candidate(s); ask user whether to expand`
30 : reasonParts. join ( '; ' ) || 'no expansion possible' ;
31
32 const summarize = ( c ) => ({
33 kind: c.kind,
34 route: c.route ?? c.hostname ?? null ,
35 displayRoute: c.displayRoute ?? null ,
36 o11ySignal: c.o11ySignal ?? null ,
37 priority: c.priority ?? null ,
38 });
39
40 const investigatingPreviewCount = typeof currentBudget === 'number' && currentBudget <= MAX_FULL_INVESTIGATING_PREVIEW
41 ? currentBudget
42 : TOP_INVESTIGATING_PREVIEW ;
43 const topInvestigating = toLaunch. slice ( 0 , investigatingPreviewCount). map (summarize);
44 const topSkipped = skippedByBudget. map (summarize);
45 const options = buildOptions (toLaunch. length , skipped);
46 const questionText = buildQuestionText ({ shouldAsk, totalPassed, currentBudget });
47 const printContract = shouldAsk
48 ? 'Print chatPreview verbatim by copying exactChatMessage.body as a chat message before asking questionText. Do not summarize, truncate, reorder, shorten, or rewrite options.'
49 : null ;
50 const questionPayload = shouldAsk ? buildQuestionPayload (questionText, options) : null ;
51 const chatPreview = buildChatPreview ({ shouldAsk, totalPassed, currentBudget, skipped, topInvestigating, topSkipped, reason });
52 const exactChatMessage = buildExactChatMessage (chatPreview);
53 return {
54 shouldAsk,
55 reason,
56 totalPassed,
57 currentBudget: currentBudget === Infinity ? 'all' : currentBudget,
58 budgetSource,
59 skipped,
60 topInvestigating,
61 topSkipped,
62 options,
63 printContract,
64 chatPreview,
65 exactChatMessage,
66 printCheck: shouldAsk ? buildPrintCheck ({ exactChatMessage, skipped }) : null ,
67 questionText,
68 questionPayload,
69 };
70 }
71
72 function buildChatPreview ({ shouldAsk , totalPassed , currentBudget , skipped , topInvestigating , topSkipped , reason }) {
73 if ( ! shouldAsk) return `Audit scope: no question needed — ${ reason }.` ;
74 const lines = [];
75 lines. push ( `Found ${ totalPassed } potential issue${ totalPassed === 1 ? '' : 's'} worth checking. By default I'll inspect the ${ currentBudget } strongest now; ${ skipped } will stay in the report for a larger run.` );
76 lines. push ( `Choose a larger scope if you want broader coverage. More checks take longer.` );
77 if (topInvestigating. length > 0 ) {
78 lines. push ( '' );
79 lines. push ( `Checking now${ topInvestigating . length < currentBudget ? ` (${ topInvestigating . length } shown)` : ''}:` );
80 topInvestigating. forEach (( c , i ) => lines. push ( ` ${ i + 1 }. ${ formatCandidateLine ( c ) }` ));
81 }
82 if (topSkipped. length > 0 ) {
83 lines. push ( '' );
84 lines. push ( `Only checked if you expand this run (${ topSkipped . length }):` );
85 topSkipped. forEach (( c , i ) => lines. push ( ` ${ i + 1 }. ${ formatCandidateLine ( c ) }` ));
86 }
87 return lines. join ( ' \n ' );
88 }
89
90 function buildExactChatMessage ( body ) {
91 return {
92 body,
93 lineCount: body. split ( ' \n ' ). length ,
94 sha256: createHash ( 'sha256' ). update (body). digest ( 'hex' ),
95 };
96 }
97
98 function buildPrintCheck ({ exactChatMessage , skipped }) {
99 return {
100 bodyField: 'exactChatMessage.body' ,
101 sameAs: 'chatPreview' ,
102 requiredLineCount: exactChatMessage.lineCount,
103 requiredSha256: exactChatMessage.sha256,
104 requiredSkippedRows: skipped,
105 requiredSkippedHeading: `Only checked if you expand this run (${ skipped }):` ,
106 forbiddenSummaryPatterns: [
107 ' \\ btop skipped \\ b' ,
108 ' \\ bmore (?:candidate|candidates|routes|entries|items|in gated list) \\ b' ,
109 ' \\ b \\ d+ \\ s*[-–—] \\ s* \\ d+ \\ . \\ s+ \\ d+ \\ s+more \\ b' ,
110 ' \\ betc \\ . \\ b' ,
111 ],
112 instruction: 'The budget message is valid only when every line from exactChatMessage.body is preserved exactly. If you cannot verify that, print exactChatMessage.body again before asking the question.' ,
113 };
114 }
115
116 function buildQuestionText ({ shouldAsk , totalPassed , currentBudget }) {
117 if ( ! shouldAsk) return '' ;
118 return `How many potential issues should I check in this run?` ;
119 }
120
121 function buildOptions ( currentCount , skippedCount ) {
122 if (skippedCount === 0 ) return [];
123 const total = currentCount + skippedCount;
124 return [
125 {
126 label: `Check ${ currentCount } (default)` ,
127 value: currentCount,
128 recommended: true ,
129 description: 'Fastest first pass; checks the strongest cost and performance signals.' ,
130 rationale: 'fastest first pass; checks the strongest cost and performance signals' ,
131 },
132 {
133 label: `Check all ${ total }` ,
134 value: 'all' ,
135 recommended: false ,
136 description: 'Most complete; takes longer because every flagged route is investigated.' ,
137 rationale: 'most complete; takes longer because every flagged route is investigated' ,
138 },
139 {
140 label: 'Pick a number' ,
141 value: 'custom' ,
142 recommended: false ,
143 description: `Check more than ${ currentCount } without running the full ${ total }.` ,
144 rationale: `checks more than ${ currentCount } without running the full ${ total }` ,
145 },
146 ];
147 }
148
149 function buildQuestionPayload ( questionText , options ) {
150 return {
151 questions: [{
152 question: questionText,
153 header: 'Audit scope' ,
154 multiSelect: false ,
155 options: options. map (( o ) => ({
156 label: o.label,
157 description: o.description ?? o.rationale,
158 })),
159 }],
160 };
161 }
162
163 export function renderBudgetSummaryMarkdown ( s ) {
164 const lines = [];
165 lines. push ( `## Audit scope` );
166 lines. push ( '' );
167 if ( ! s.shouldAsk) {
168 lines. push ( `_No question needed — ${ s . reason }._` );
169 return lines. join ( ' \n ' );
170 }
171 for ( const ln of s.chatPreview. split ( ' \n ' )) lines. push (ln);
172 lines. push ( '' );
173 lines. push ( '### Options' );
174 lines. push ( '' );
175 for ( const o of s.options) {
176 const tag = o.recommended ? ' (recommended)' : '' ;
177 lines. push ( `- **${ o . label }${ tag }** — ${ o . rationale }` );
178 }
179 lines. push ( '' );
180 lines. push ( `**Question:** ${ s . questionText }` );
181 return lines. join ( ' \n ' );
182 }