@@ -242,6 +242,65 @@ export function keySetMatches(set: KeySetGuidance, key: string): boolean {
242242 return set . keys instanceof RegExp ? key . search ( set . keys ) !== - 1 : set . keys . includes ( key ) ;
243243}
244244
245+ /**
246+ * One unknown key as the refusal names it: in backticks, the spelling the
247+ * author typed. An EMPTY key is spelled out, because backticks around nothing
248+ * render as an empty code span (or as two stray backticks), and the author is
249+ * left looking for a key the message never shows.
250+ */
251+ function renderUnknownKey ( key : string ) : string {
252+ return key === '' ? 'an empty key (`""`)' : `\`${ key } \`` ;
253+ }
254+
255+ /**
256+ * Edit distance in which swapping two adjacent letters costs ONE edit (the
257+ * optimal-string-alignment variant of Levenshtein). Used only by
258+ * {@link shortKeyScreenedGuess}; scoring for every other length is
259+ * {@link levenshteinDistance}, unchanged.
260+ */
261+ function adjacentSwapDistance ( a : string , b : string ) : number {
262+ const rows : number [ ] [ ] = [ ] ;
263+ for ( let i = 0 ; i <= a . length ; i ++ ) {
264+ rows . push ( [ i ] ) ;
265+ for ( let j = 1 ; j <= b . length ; j ++ ) rows [ i ] ! [ j ] = i === 0 ? j : 0 ;
266+ }
267+ for ( let i = 1 ; i <= a . length ; i ++ ) {
268+ for ( let j = 1 ; j <= b . length ; j ++ ) {
269+ const cost = a [ i - 1 ] === b [ j - 1 ] ? 0 : 1 ;
270+ let best = Math . min ( rows [ i - 1 ] ! [ j ] ! + 1 , rows [ i ] ! [ j - 1 ] ! + 1 , rows [ i - 1 ] ! [ j - 1 ] ! + cost ) ;
271+ if ( i > 1 && j > 1 && a [ i - 1 ] === b [ j - 2 ] && a [ i - 2 ] === b [ j - 1 ] ) {
272+ best = Math . min ( best , rows [ i - 2 ] ! [ j - 2 ] ! + 1 ) ;
273+ }
274+ rows [ i ] ! [ j ] = best ;
275+ }
276+ }
277+ return rows [ a . length ] ! [ b . length ] ! ;
278+ }
279+
280+ /**
281+ * The rename guess for one unknown key, or `undefined`.
282+ *
283+ * Above five characters this is {@link findClosestMatches}' first answer,
284+ * exactly as before. At five characters or fewer the budget is the floor of 2,
285+ * not a third of the key, and the floor exists for one slip: a swap of two
286+ * adjacent letters, which plain Levenshtein charges as two edits (`nmae` →
287+ * `name`). The same 2 also admits two UNRELATED edits, which on a four-letter
288+ * key is half of it — `aria` → `areas` (one substitution, one insertion), a
289+ * removed accessibility key answered with the app's navigation areas. So a
290+ * short key's guess must be one edit away counting a swap as one; the candidates
291+ * are still ranked by {@link findClosestMatches}, and the first that passes wins.
292+ */
293+ function shortKeyScreenedGuess (
294+ key : string ,
295+ knownKeys : readonly string [ ] ,
296+ maxDistance : number ,
297+ ) : string | undefined {
298+ if ( Math . floor ( key . length / 3 ) >= 2 ) return findClosestMatches ( key , knownKeys , maxDistance , 1 ) [ 0 ] ;
299+ const folded = foldForScoring ( key ) ;
300+ return findClosestMatches ( key , knownKeys , maxDistance , knownKeys . length )
301+ . find ( ( candidate ) => adjacentSwapDistance ( folded , foldForScoring ( candidate ) ) <= 1 ) ;
302+ }
303+
245304/** Options for {@link strictUnknownKeyError}. */
246305export interface StrictUnknownKeyErrorOptions {
247306 /** Prose name of the authoring surface the key was written on (e.g. `'this permission set'`). */
@@ -410,9 +469,13 @@ export function strictUnknownKeyError(options: StrictUnknownKeyErrorOptions): z.
410469 }
411470 continue ;
412471 }
472+ // An empty key names nothing, so nothing is a spelling of it: every
473+ // declared key of two letters or fewer sits inside the budget, and the
474+ // first of them would be offered as the fix.
475+ if ( key === '' ) continue ;
413476 const maxDistance = Math . max ( 2 , Math . floor ( key . length / 3 ) ) ;
414477 const declared = aliases [ aliasProbe ( key ) ] ;
415- const guessed = declared ? undefined : findClosestMatches ( key , knownKeys , maxDistance , 1 ) [ 0 ] ;
478+ const guessed = declared ? undefined : shortKeyScreenedGuess ( key , knownKeys , maxDistance ) ;
416479 // Screen the GUESS, never the declaration. `findClosestMatches` ranks by
417480 // edit distance and nothing else, so on a shape declaring both ends of a
418481 // range it answers a key that names neither end with whichever end is
@@ -441,7 +504,7 @@ export function strictUnknownKeyError(options: StrictUnknownKeyErrorOptions): z.
441504 // an AI — reads the front of that line and acts on it, so the prescription
442505 // has to be there. Nothing is dropped or made conditional: the sentence is
443506 // still emitted verbatim, once per message, just last (#5955).
444- let message = `Unrecognized key(s) on ${ surface } : ${ keys . map ( ( k ) => `\` ${ k } \`` ) . join ( ', ' ) } .` ;
507+ let message = `Unrecognized key(s) on ${ surface } : ${ keys . map ( renderUnknownKey ) . join ( ', ' ) } .` ;
445508 if ( renames . length ) message += ` Did you mean ${ renames . join ( ', ' ) } ?` ;
446509 if ( prescriptions . length ) message += `\n${ prescriptions . map ( ( p ) => ` • ${ p } ` ) . join ( '\n' ) } ` ;
447510 // The last fix channel, still ahead of `history`: where an aged-out
0 commit comments