Setting the file. One moment.
Select Candidates · Vercel Optimize · vercel-labs/agent-skills · Skills Docs
ContentsBack to the top of the page Bundled file Scanner Driven
lib/gates/ select-candidates.mjs
JavaScript · 134 lines · 5 KB
],
11 ]);
12
13 const DIVERSITY_ELIGIBILITY = new Map ([
14 // A handful of 5xx errors can pass the route_errors gate because the rate is
15 // high, but that should not displace much larger cost/performance signals in
16 // the default six-candidate pass.
17 [ 'route_errors' , ( candidate ) => numberFromEvidence (candidate, 'count' ) >= 1000 ],
18
19 // Scanner-driven cache findings are valuable, but the default pass should
20 // spend a slot only when observability shows meaningful route traffic or a
21 // very slow route handler.
22 [ 'cache_header_gap' , ( candidate ) => {
23 const invocations = numberFromSignal (candidate?.o11ySignal, 'inv' );
24 const p95Ms = durationMsFromSignal (candidate?.o11ySignal, 'p95' );
25 return invocations >= 50_000 || p95Ms >= 2000 ;
26 }],
27 [ 'rendering_candidate' , ( candidate ) => numberFromSignal (candidate?.o11ySignal, 'inv' ) >= 50_000 ],
28 ]);
29
30 export function selectLaunchCandidates ( candidates , budget , { diversify = false } = {}) {
31 const pool = Array. isArray (candidates) ? candidates : [];
32 if (budget === Infinity ) {
33 return { selected: pool, skipped: [], selectionMode: 'all' };
34 }
35 if ( ! Number. isInteger (budget) || budget < 1 ) {
36 throw new TypeError ( 'selectLaunchCandidates budget must be a positive integer or Infinity' );
37 }
38 if ( ! diversify) {
39 return {
40 selected: pool. slice ( 0 , budget),
41 skipped: pool. slice (budget),
42 selectionMode: 'priority' ,
43 };
44 }
45
46 const selected = [];
47 const selectedKeys = new Set ();
48 const countsByKind = new Map ();
49
50 const add = ( candidate ) => {
51 const key = candidateIdentity (candidate);
52 if (selectedKeys. has (key)) return false ;
53 selectedKeys. add (key);
54 selected. push (candidate);
55 const kind = candidate.kind ?? '<unknown>' ;
56 countsByKind. set (kind, (countsByKind. get (kind) ?? 0 ) + 1 );
57 return true ;
58 };
59
60 // First pass: one candidate per failure mode, preserving the existing sorted
61 // order. This is where the default run gets broad coverage, but only for
62 // kinds whose signal is strong enough for a default slot.
63 for ( const candidate of pool) {
64 if (selected. length >= budget) break ;
65 const kind = candidate.kind ?? '<unknown>' ;
66 if ((countsByKind. get (kind) ?? 0 ) > 0 ) continue ;
67 if ( ! isDiversityEligible (candidate)) continue ;
68 add (candidate);
69 }
70
71 // Second pass: allow a second entry for high-frequency families, but avoid
72 // letting slow_route consume the entire default budget when other kinds exist.
73 for ( const candidate of pool) {
74 if (selected. length >= budget) break ;
75 const kind = candidate.kind ?? '<unknown>' ;
76 const cap = DEFAULT_KIND_CAPS . get (kind) ?? 1 ;
77 if ((countsByKind. get (kind) ?? 0 ) >= cap) continue ;
78 if ( ! isDiversityEligible (candidate)) continue ;
79 add (candidate);
80 }
81
82 // Final fill: if the project only has one or two candidate kinds, use the
83 // whole requested budget rather than leaving slots empty.
84 for ( const candidate of pool) {
85 if (selected. length >= budget) break ;
86 add (candidate);
87 }
88
89 return {
90 selected,
91 skipped: pool. filter (( candidate ) => ! selectedKeys. has ( candidateIdentity (candidate))),
92 selectionMode: 'diverse-default' ,
93 };
94 }
95
96 function candidateIdentity ( candidate ) {
97 return [
98 candidate?.kind ?? '' ,
99 candidate?.route ?? '' ,
100 candidate?.hostname ?? '' ,
101 candidate?.scope ?? '' ,
102 candidate?.o11ySignal ?? '' ,
103 ]. join ( ' \u0000 ' );
104 }
105
106 function isDiversityEligible ( candidate ) {
107 const fn = DIVERSITY_ELIGIBILITY . get (candidate?.kind);
108 return fn ? fn (candidate) : true ;
109 }
110
111 function numberFromEvidence ( candidate , key ) {
112 const value = candidate?.evidence?.[key];
113 return typeof value === 'number' && Number. isFinite (value) ? value : 0 ;
114 }
115
116 function numberFromSignal ( signal , key ) {
117 if ( typeof signal !== 'string' ) return 0 ;
118 const escaped = key. replace ( / [.*+?^${}()|[ \]\\ ] / g , ' \\ $&' );
119 const re = new RegExp ( `(?:^|,)${ escaped }=([ \\ d.]+)` );
120 const m = re. exec (signal);
121 if ( ! m) return 0 ;
122 const n = Number (m[ 1 ]);
123 return Number. isFinite (n) ? n : 0 ;
124 }
125
126 function durationMsFromSignal ( signal , key ) {
127 if ( typeof signal !== 'string' ) return 0 ;
128 const escaped = key. replace ( / [.*+?^${}()|[ \]\\ ] / g , ' \\ $&' );
129 const re = new RegExp ( `(?:^|,)${ escaped }=([ \\ d.]+)ms` );
130 const m = re. exec (signal);
131 if ( ! m) return 0 ;
132 const n = Number (m[ 1 ]);
133 return Number. isFinite (n) ? n : 0 ;
134 }