@mailwoman/resolver 9.2.0 → 9.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/README.md +5 -5
  2. package/{admin-coherence-passes.ts → lib/admin/coherence-passes.ts} +87 -9
  3. package/{admin-containment.ts → lib/admin/containment.ts} +5 -14
  4. package/lib/admin/index.ts +10 -0
  5. package/lib/admin/jp-municipality.ts +97 -0
  6. package/{admin-winner.ts → lib/admin/winner.ts} +18 -2
  7. package/{backend-capabilities.ts → lib/backend-capabilities.ts} +3 -3
  8. package/{bare-toponym-race.ts → lib/bare-toponym-race.ts} +1 -1
  9. package/{decorate-node.ts → lib/decorate-node.ts} +2 -2
  10. package/{fold-name.ts → lib/fold-name.ts} +2 -2
  11. package/{index.ts → lib/index.ts} +16 -22
  12. package/{plausibility.ts → lib/plausibility.ts} +5 -4
  13. package/{postcode-country-coherence.ts → lib/postcode/country-coherence.ts} +27 -32
  14. package/lib/postcode/index.ts +9 -0
  15. package/{postcode-prefix.ts → lib/postcode/prefix.ts} +4 -4
  16. package/{postcode-shape-coherence.ts → lib/postcode/shape-coherence.ts} +10 -24
  17. package/{remote-resolver.ts → lib/remote-resolver.ts} +13 -4
  18. package/{rerank.ts → lib/rerank.ts} +2 -2
  19. package/{resolve.ts → lib/resolve/index.ts} +133 -602
  20. package/lib/resolve/passes.ts +544 -0
  21. package/{span-rescore.ts → lib/span-rescore.ts} +63 -97
  22. package/{street-evidence.ts → lib/street/evidence.ts} +39 -6
  23. package/lib/street/index.ts +8 -0
  24. package/{street-tier.ts → lib/street/tier.ts} +131 -110
  25. package/{toponym-prior.ts → lib/toponym-prior.ts} +8 -8
  26. package/out/{admin-coherence-passes.d.ts → admin/coherence-passes.d.ts} +29 -7
  27. package/out/admin/coherence-passes.d.ts.map +1 -0
  28. package/out/{admin-coherence-passes.js → admin/coherence-passes.js} +73 -9
  29. package/out/admin/coherence-passes.js.map +1 -0
  30. package/out/{admin-containment.d.ts → admin/containment.d.ts} +5 -5
  31. package/out/admin/containment.d.ts.map +1 -0
  32. package/out/{admin-containment.js → admin/containment.js} +6 -12
  33. package/out/admin/containment.js.map +1 -0
  34. package/out/admin/index.d.ts +10 -0
  35. package/out/admin/index.d.ts.map +1 -0
  36. package/out/admin/index.js +10 -0
  37. package/out/admin/index.js.map +1 -0
  38. package/out/admin/jp-municipality.d.ts +50 -0
  39. package/out/admin/jp-municipality.d.ts.map +1 -0
  40. package/out/admin/jp-municipality.js +54 -0
  41. package/out/admin/jp-municipality.js.map +1 -0
  42. package/out/{admin-winner.d.ts → admin/winner.d.ts} +11 -3
  43. package/out/admin/winner.d.ts.map +1 -0
  44. package/out/{admin-winner.js → admin/winner.js} +19 -3
  45. package/out/admin/winner.js.map +1 -0
  46. package/out/backend-capabilities.d.ts.map +1 -1
  47. package/out/backend-capabilities.js +3 -3
  48. package/out/backend-capabilities.js.map +1 -1
  49. package/out/bare-toponym-race.d.ts.map +1 -1
  50. package/out/bare-toponym-race.js +1 -1
  51. package/out/bare-toponym-race.js.map +1 -1
  52. package/out/decorate-node.d.ts +1 -1
  53. package/out/decorate-node.d.ts.map +1 -1
  54. package/out/decorate-node.js +1 -1
  55. package/out/decorate-node.js.map +1 -1
  56. package/out/fold-name.d.ts +2 -2
  57. package/out/fold-name.d.ts.map +1 -1
  58. package/out/fold-name.js +2 -2
  59. package/out/fold-name.js.map +1 -1
  60. package/out/index.d.ts +15 -16
  61. package/out/index.d.ts.map +1 -1
  62. package/out/index.js +9 -10
  63. package/out/index.js.map +1 -1
  64. package/out/plausibility.d.ts +5 -4
  65. package/out/plausibility.d.ts.map +1 -1
  66. package/out/plausibility.js +5 -4
  67. package/out/plausibility.js.map +1 -1
  68. package/out/{postcode-country-coherence.d.ts → postcode/country-coherence.d.ts} +16 -16
  69. package/out/postcode/country-coherence.d.ts.map +1 -0
  70. package/out/{postcode-country-coherence.js → postcode/country-coherence.js} +24 -25
  71. package/out/postcode/country-coherence.js.map +1 -0
  72. package/out/postcode/index.d.ts +9 -0
  73. package/out/postcode/index.d.ts.map +1 -0
  74. package/out/postcode/index.js +9 -0
  75. package/out/postcode/index.js.map +1 -0
  76. package/out/{postcode-prefix.d.ts → postcode/prefix.d.ts} +4 -4
  77. package/out/postcode/prefix.d.ts.map +1 -0
  78. package/out/{postcode-prefix.js → postcode/prefix.js} +4 -4
  79. package/out/postcode/prefix.js.map +1 -0
  80. package/out/{postcode-shape-coherence.d.ts → postcode/shape-coherence.d.ts} +3 -3
  81. package/out/postcode/shape-coherence.d.ts.map +1 -0
  82. package/out/{postcode-shape-coherence.js → postcode/shape-coherence.js} +11 -20
  83. package/out/postcode/shape-coherence.js.map +1 -0
  84. package/out/remote-resolver.d.ts +18 -3
  85. package/out/remote-resolver.d.ts.map +1 -1
  86. package/out/remote-resolver.js +2 -2
  87. package/out/remote-resolver.js.map +1 -1
  88. package/out/rerank.d.ts +1 -1
  89. package/out/rerank.d.ts.map +1 -1
  90. package/out/rerank.js +2 -2
  91. package/out/rerank.js.map +1 -1
  92. package/out/{resolve.d.ts → resolve/index.d.ts} +1 -1
  93. package/out/resolve/index.d.ts.map +1 -0
  94. package/out/{resolve.js → resolve/index.js} +100 -387
  95. package/out/resolve/index.js.map +1 -0
  96. package/out/resolve/passes.d.ts +218 -0
  97. package/out/resolve/passes.d.ts.map +1 -0
  98. package/out/resolve/passes.js +333 -0
  99. package/out/resolve/passes.js.map +1 -0
  100. package/out/span-rescore.d.ts +16 -16
  101. package/out/span-rescore.d.ts.map +1 -1
  102. package/out/span-rescore.js +43 -69
  103. package/out/span-rescore.js.map +1 -1
  104. package/out/{street-evidence.d.ts → street/evidence.d.ts} +18 -3
  105. package/out/street/evidence.d.ts.map +1 -0
  106. package/out/{street-evidence.js → street/evidence.js} +22 -6
  107. package/out/street/evidence.js.map +1 -0
  108. package/out/street/index.d.ts +8 -0
  109. package/out/street/index.d.ts.map +1 -0
  110. package/out/street/index.js +8 -0
  111. package/out/street/index.js.map +1 -0
  112. package/out/{street-tier.d.ts → street/tier.d.ts} +14 -3
  113. package/out/street/tier.d.ts.map +1 -0
  114. package/out/{street-tier.js → street/tier.js} +121 -99
  115. package/out/street/tier.js.map +1 -0
  116. package/out/toponym-prior.d.ts +8 -8
  117. package/out/toponym-prior.d.ts.map +1 -1
  118. package/out/toponym-prior.js +8 -8
  119. package/out/toponym-prior.js.map +1 -1
  120. package/package.json +116 -59
  121. package/vitest.config.ts +5 -6
  122. package/out/admin-coherence-passes.d.ts.map +0 -1
  123. package/out/admin-coherence-passes.js.map +0 -1
  124. package/out/admin-containment.d.ts.map +0 -1
  125. package/out/admin-containment.js.map +0 -1
  126. package/out/admin-winner.d.ts.map +0 -1
  127. package/out/admin-winner.js.map +0 -1
  128. package/out/postcode-country-coherence.d.ts.map +0 -1
  129. package/out/postcode-country-coherence.js.map +0 -1
  130. package/out/postcode-prefix.d.ts.map +0 -1
  131. package/out/postcode-prefix.js.map +0 -1
  132. package/out/postcode-shape-coherence.d.ts.map +0 -1
  133. package/out/postcode-shape-coherence.js.map +0 -1
  134. package/out/resolve.d.ts.map +0 -1
  135. package/out/resolve.js.map +0 -1
  136. package/out/street-evidence.d.ts.map +0 -1
  137. package/out/street-evidence.js.map +0 -1
  138. package/out/street-tier.d.ts.map +0 -1
  139. package/out/street-tier.js.map +0 -1
  140. package/out/vitest.config.d.ts +0 -16
  141. package/out/vitest.config.d.ts.map +0 -1
  142. package/out/vitest.config.js +0 -36
  143. package/out/vitest.config.js.map +0 -1
@@ -20,17 +20,17 @@
20
20
  * (`scripts/eval/span-rescore-validate.ts`, eval
21
21
  * `docs/articles/evals/experiments/2026-06-23-370-span-rescore.mdx`): longest-exact-match-wins (the gold
22
22
  * locality is usually the LONGER, more-specific name — shortest- wins grabs the ambiguous prefix
23
- * "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency gate that rejects
23
+ * "Tomaszów" of "Tomaszów Mazowiecki", 135 km off), and a postcode- consistency check that rejects
24
24
  * a match far from where the postcode resolves (kills coverage-gap false-positives where the
25
25
  * backend has postcode coverage).
26
26
  */
27
27
 
28
- import type { AddressNode } from "@mailwoman/core/decoder"
28
+ import { firstNodeWhere, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
29
29
  import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver"
30
30
  import { haversineKm } from "@mailwoman/spatial"
31
31
 
32
- import { foldName } from "./fold-name.ts"
33
- import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "./toponym-prior.ts"
32
+ import { foldName } from "#fold-name"
33
+ import { DEFAULT_COUNTRY_PRIOR_WEIGHT, rankByCountryPrior, rankByImportance } from "#toponym-prior"
34
34
 
35
35
  export interface SpanRescoreOptions {
36
36
  /**
@@ -38,15 +38,15 @@ export interface SpanRescoreOptions {
38
38
  */
39
39
  country?: string
40
40
  /**
41
- * Sibling postcode — used both as the backend disambiguation hint AND the consistency-gate anchor.
41
+ * Sibling postcode — used both as the backend disambiguation hint AND the consistency-check anchor.
42
42
  */
43
43
  postcode?: string
44
44
  /**
45
- * Reject a candidate whose coordinate is farther than this (km) from the postcode anchor. The gate only fires when
45
+ * Reject a candidate whose coordinate is farther than this (km) from the postcode anchor. The check only fires when
46
46
  * the postcode resolves to a point in the backend; otherwise it can't and the match is accepted (so it never
47
47
  * penalizes a backend without postcode coverage). 0 disables. Default 50.
48
48
  */
49
- gateKm?: number
49
+ thresholdKm?: number
50
50
  /**
51
51
  * Max contiguous raw tokens to treat as one locality span. Default 4.
52
52
  */
@@ -94,18 +94,18 @@ export interface RescoreCandidate {
94
94
  */
95
95
  place: ResolvedPlace
96
96
  /**
97
- * Whether the postcode-consistency gate FIRED for this recovery — i.e. the postcode resolved to a point and the match
98
- * was validated within `gateKm` of it. `true` = high-precision (postcode- consistent); `false` = ungated (no
99
- * postcode→point coverage for this country, so the match wasn't geo-validated — the ~83%-precision case). The caller
100
- * surfaces this as `metadata.rescore_gated` so a consumer can threshold on it WITHOUT a hidden per-country coverage
101
- * map. Deliberately NOT folded into the calibrated `confidence` — that would break the isotonic guarantee (a true
102
- * calibrated 0.83 must not be confused with a rescore plug-in estimate).
97
+ * Whether the postcode-consistency check FIRED for this recovery — i.e. the postcode resolved to a point and the
98
+ * match was validated within `thresholdKm` of it. `true` = high-precision (postcode- consistent); `false` =
99
+ * unrestricted (no postcode→point coverage for this country, so the match wasn't geo-validated — the ~83%-precision
100
+ * case). The caller surfaces this as `metadata.rescore_postcode_verified` so a consumer can threshold on it WITHOUT a
101
+ * hidden per-country coverage map. Deliberately NOT folded into the calibrated `confidence` — that would break the
102
+ * isotonic guarantee (a true calibrated 0.83 must not be confused with a rescore plug-in estimate).
103
103
  */
104
- gated: boolean
104
+ postcodeVerified: boolean
105
105
  /**
106
106
  * The SAME-SPAN namesake runner-ups — the other exact-name matches this recovery's own lookup already returned, in
107
- * the backend's rank order, minus the winner and minus anything the postcode gate rejected. Empty when the span named
108
- * exactly one place.
107
+ * the backend's rank order, minus the winner and minus anything the postcode check rejected. Empty when the span
108
+ * named exactly one place.
109
109
  *
110
110
  * #1537: these were being discarded. A name the model reads as a `street` ("Springfield", "Berlin", "Manchester",
111
111
  * "Moscow", "Fulda") never reaches the admin walk, so the whole tree comes back unresolved and THIS tier is what
@@ -115,7 +115,7 @@ export interface RescoreCandidate {
115
115
  * backend, 2026-08-07: those five queries returned 1 candidate each while `Cambridge`/`Athens`/`Paris` — the same
116
116
  * class, but parsed as `locality` — returned 4-5.
117
117
  *
118
- * The winner is unchanged by their presence: the pick is still the first gate-passing exact match, and these are the
118
+ * The winner is unchanged by their presence: the pick is still the first check-passing exact match, and these are the
119
119
  * ones that came after it. This costs no extra query — they were in the same `findPlace` response.
120
120
  */
121
121
  alternatives: ResolvedPlace[]
@@ -169,47 +169,25 @@ const BARE_TOPONYM_FETCH = 20
169
169
  * the hard filter keeps its say: this is what holds 'Berlin, Wisconsin' on Berlin, Wisconsin.
170
170
  */
171
171
  function hasAdminQualifier(roots: readonly AddressNode[]): boolean {
172
- const stack: AddressNode[] = [...roots]
173
-
174
- while (stack.length) {
175
- const n = stack.pop()!
176
-
177
- if (
178
- (n.tag === "region" ||
179
- n.tag === "subregion" ||
180
- n.tag === "country" ||
181
- n.tag === "postcode" ||
182
- n.tag === "house_number") &&
183
- n.value.trim().length
184
- ) {
185
- return true
186
- }
187
-
188
- if (n.children?.length) {
189
- stack.push(...n.children)
190
- }
191
- }
192
-
193
- return false
172
+ return (
173
+ firstNodeWhere(
174
+ roots,
175
+ (n) =>
176
+ (n.tag === "region" ||
177
+ n.tag === "subregion" ||
178
+ n.tag === "country" ||
179
+ n.tag === "postcode" ||
180
+ n.tag === "house_number") &&
181
+ n.value.trim().length > 0
182
+ ) !== undefined
183
+ )
194
184
  }
195
185
 
196
186
  /**
197
187
  * True if any node in the tree already carries a resolved place id — the #685 brake.
198
188
  */
199
189
  export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
200
- const stack: AddressNode[] = [...roots]
201
-
202
- while (stack.length) {
203
- const n = stack.pop()!
204
-
205
- if (n.placeID) return true
206
-
207
- if (n.children?.length) {
208
- stack.push(...n.children)
209
- }
210
- }
211
-
212
- return false
190
+ return firstNodeWhere(roots, (n) => Boolean(n.placeID)) !== undefined
213
191
  }
214
192
 
215
193
  /**
@@ -226,18 +204,15 @@ export function hasResolvedPlace(roots: readonly AddressNode[]): boolean {
226
204
  * modifier inside a name, not a name. Probing it anyway matches a real US place (`New`, wof:1276997945) and pins the
227
205
  * address to Kentucky — an answer strictly worse than none, because it is confident and wrong.
228
206
  *
229
- * Deliberately NOT confidence-gated, unlike {@link confidentRanges}. The country node in that case carries 0.68, under
230
- * the 0.7 bar, and a low-confidence GROUPING is still a grouping: the tokens were read as one name either way, and the
231
- * interior of a name the parse doubts is not thereby a better standalone candidate. Single-token spans are excluded —
232
- * there is no interior to protect.
207
+ * Deliberately NOT confidence-conditioned, unlike {@link confidentRanges}. The country node in that case carries 0.68,
208
+ * under the 0.7 bar, and a low-confidence GROUPING is still a grouping: the tokens were read as one name either way,
209
+ * and the interior of a name the parse doubts is not thereby a better standalone candidate. Single-token spans are
210
+ * excluded — there is no interior to protect.
233
211
  */
234
212
  function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Array<[number, number]> {
235
213
  const out: Array<[number, number]> = []
236
- const stack: AddressNode[] = [...roots]
237
-
238
- while (stack.length) {
239
- const n = stack.pop()!
240
214
 
215
+ for (const n of walkNodes(roots)) {
241
216
  if (
242
217
  (n.tag === "country" || n.tag === "region") &&
243
218
  Number.isFinite(n.start) &&
@@ -246,8 +221,6 @@ function multiTokenNameInteriors(roots: readonly AddressNode[], raw: string): Ar
246
221
  ) {
247
222
  out.push([n.start, n.end])
248
223
  }
249
-
250
- stack.push(...n.children)
251
224
  }
252
225
 
253
226
  return out
@@ -260,11 +233,8 @@ function confidentRanges(
260
233
  postalCompoundRecovery: boolean
261
234
  ): Array<[number, number]> {
262
235
  const out: Array<[number, number]> = []
263
- const stack: AddressNode[] = [...roots]
264
-
265
- while (stack.length) {
266
- const n = stack.pop()!
267
236
 
237
+ for (const n of walkNodes(roots)) {
268
238
  if (
269
239
  (n.tag === "postcode" ||
270
240
  n.tag === "house_number" ||
@@ -288,10 +258,6 @@ function confidentRanges(
288
258
  out.push([n.start, n.end])
289
259
  }
290
260
  }
291
-
292
- if (n.children?.length) {
293
- stack.push(...n.children)
294
- }
295
261
  }
296
262
 
297
263
  return out
@@ -299,7 +265,7 @@ function confidentRanges(
299
265
 
300
266
  /**
301
267
  * Find the best locality the raw text exact-matches in the gazetteer. Returns null when nothing matches (or the
302
- * postcode gate rejects every match). Callers gate on `hasResolvedPlace` first.
268
+ * postcode check rejects every match). Callers test `hasResolvedPlace` first.
303
269
  */
304
270
  export async function findRescoreCandidate(
305
271
  raw: string,
@@ -307,21 +273,21 @@ export async function findRescoreCandidate(
307
273
  backend: ResolverBackend,
308
274
  opts: SpanRescoreOptions = {}
309
275
  ): Promise<RescoreCandidate | null> {
310
- const gateKm = opts.gateKm ?? 50
276
+ const thresholdKm = opts.thresholdKm ?? 50
311
277
  const maxSpan = opts.maxSpanTokens ?? 4
312
278
  const threshold = opts.confidentThreshold ?? 0.7
313
279
  const country = opts.country
314
280
  const postcode = opts.postcode?.trim() || undefined
315
281
 
316
282
  // Postcode-consistency anchor: where does the postcode itself resolve? (No-op when the backend has
317
- // no postcode coverage — findPlace returns nothing → no anchor → gate can't fire → match accepted.)
283
+ // no postcode coverage — findPlace returns nothing → no anchor → check can't fire → match accepted.)
318
284
  let anchor: { lat: number; lon: number } | null = null
319
285
 
320
- if (postcode && gateKm > 0) {
286
+ if (postcode && thresholdKm > 0) {
321
287
  // #961: both anchor probes are postalcode-TYPED. Untyped, a truncated code fragment (v5.3.0
322
288
  // emits "250 Zabiče" → subset "250") name-matches arbitrary places ("Chak No 250", PK) and the
323
- // false anchor then GATES OUT the true village. A typed miss leaves anchor=null → the match is
324
- // accepted ungated, which is the correct degradation for an unverifiable code.
289
+ // false anchor then EXCLUDES the true village. A typed miss leaves anchor=null → the match is
290
+ // accepted unrestricted, which is the correct degradation for an unverifiable code.
325
291
  const pcHits = await backend.findPlace({ text: postcode, country, placetype: "postalcode", limit: 2 })
326
292
  const a = pcHits.find((h) => h.lat !== 0 || h.lon !== 0)
327
293
 
@@ -330,7 +296,7 @@ export async function findRescoreCandidate(
330
296
  }
331
297
 
332
298
  // #942: the globbed compound ("1382 Kožljek") matches no bare-code row — retry the anchor with
333
- // the code-shaped token subset so the consistency gate can validate the recovered city.
299
+ // the code-shaped token subset so the consistency check can validate the recovered city.
334
300
  if (!anchor && opts.postalCompoundRecovery) {
335
301
  const code = postcodeCodeSubset(postcode)
336
302
 
@@ -387,12 +353,12 @@ export async function findRescoreCandidate(
387
353
  //
388
354
  // So for this ONE query shape the filter is demoted to an additive prior: probe the gazetteer
389
355
  // unscoped, then rank with `rankByCountryPrior` so an in-country place may be up to 100x smaller and
390
- // still win. This is the #912 lever ("Paris under en-US must not be hard-scoped to Paris, Texas")
356
+ // still win. This is the #912 change ("Paris under en-US must not be hard-scoped to Paris, Texas")
391
357
  // applied at the tier that actually decides the class — #912 lives upstream in the CLI and keys on a
392
358
  // `locality`-tagged tree, but the model reads a bare famous name as a `street`, so the shape that
393
359
  // needs it most is exactly the shape that guard cannot see.
394
360
  //
395
- // The shape gate is deliberately narrow. `qualified` below is the D-rule guard: a postcode, a region,
361
+ // The shape check is deliberately narrow. `qualified` below is the D-rule guard: a postcode, a region,
396
362
  // a country or a house number in the tree means the address named its own context and a locale guess
397
363
  // is no longer the only evidence — 'Berlin, Wisconsin' keeps resolving to Berlin, Wisconsin. And the
398
364
  // span must cover the WHOLE input: a sub-span of a longer query is not a bare toponym, so
@@ -408,10 +374,10 @@ export async function findRescoreCandidate(
408
374
  const key = foldName(sp.text)
409
375
 
410
376
  if (key.length < 2 || /^\d+$/.test(key)) continue // skip bare numbers / empties
411
- // Whole-input coverage and the soft-country prior are SEPARATE gates. `bare` (the prior) also
377
+ // Whole-input coverage and the soft-country prior are SEPARATE checks. `bare` (the prior) also
412
378
  // needs an unqualified tree + a caller country; `wholeSpan` alone decides the alias tier below —
413
379
  // a scope-less bare "Riyadh"/"Frankfurt" is still a NAMING (their Latin surfaces live in alias
414
- // rows: الرياض / Frankfurt am Main), and conflating the two gates cost exactly those rows on the
380
+ // rows: الرياض / Frankfurt am Main), and conflating the two checks cost exactly those rows on the
415
381
  // 2026-08-15 board before this line split them.
416
382
  const wholeSpan = !!wholeInput && sp.start === wholeInput.start && sp.end === wholeInput.end
417
383
  const bare = softCountryEligible && wholeSpan
@@ -441,7 +407,7 @@ export async function findRescoreCandidate(
441
407
  // non-Latin-primary class: Москва folds to "" and could never equal "moscow", so Moscow RU never
442
408
  // entered the list and Moscow, Idaho won by default among the Latin-named bearers — population-
443
409
  // first ranking starved, not violated. The alias surface is the recall; ranking then does its job.
444
- // The postcode gate below still applies to every admitted candidate, Moscow RU included.
410
+ // The postcode check below still applies to every admitted candidate, Moscow RU included.
445
411
  //
446
412
  // #17: importance-first WITHIN the admitted set. The key is the #28 blended fame prior
447
413
  // (`PlaceCandidate.importance`, produced by the candidate build) — the only key that separates
@@ -451,25 +417,25 @@ export async function findRescoreCandidate(
451
417
  // put it.
452
418
  const exact = rankByImportance(hits.filter((h) => h.exactMatch && (h.lat !== 0 || h.lon !== 0)))
453
419
 
454
- const withinGate = (p: ResolvedPlace): boolean =>
455
- !anchor || gateKm <= 0 || haversineKm(anchor.lat, anchor.lon, p.lat, p.lon) <= gateKm
420
+ const withinThreshold = (p: ResolvedPlace): boolean =>
421
+ !anchor || thresholdKm <= 0 || haversineKm(anchor.lat, anchor.lon, p.lat, p.lon) <= thresholdKm
456
422
 
457
423
  for (const h of exact) {
458
- if (!withinGate(h)) continue
424
+ if (!withinThreshold(h)) continue
459
425
 
460
- // gated = the postcode anchor existed AND validated this match (within gateKm). When no anchor
461
- // (no postcode→point coverage), the match is ungated — returned, but flagged lower-precision.
426
+ // conditional = the postcode anchor existed AND validated this match (within thresholdKm). When no anchor
427
+ // (no postcode→point coverage), the match is unrestricted — returned, but flagged lower-precision.
462
428
  //
463
- // #1537: carry the rest of the SAME lookup's exact matches as the namesake runner-ups. Gate-filtered on the
464
- // same rule as the winner — a candidate the postcode gate rejected is not a namesake worth offering, it is a
429
+ // #1537: carry the rest of the SAME lookup's exact matches as the namesake runner-ups. Check-filtered on the
430
+ // same rule as the winner — a candidate the postcode check rejected is not a namesake worth offering, it is a
465
431
  // place the evidence already excluded. Rank order is the backend's, preserved by `filter`.
466
432
  return {
467
433
  text: sp.text,
468
434
  start: sp.start,
469
435
  end: sp.end,
470
436
  place: h,
471
- gated: anchor !== null,
472
- alternatives: exact.filter((a) => a !== h && withinGate(a)),
437
+ postcodeVerified: anchor !== null,
438
+ alternatives: exact.filter((a) => a !== h && withinThreshold(a)),
473
439
  }
474
440
  }
475
441
  }
@@ -478,12 +444,12 @@ export async function findRescoreCandidate(
478
444
  // CLI's en-US default scoped both the anchor and the village probe to US, so the SI floor never
479
445
  // fired through geocode-core while the same rows resolved 25/25 on the resolver harness. When the
480
446
  // scoped pass finds nothing and a postcode is present, re-probe the spans UNSCOPED (the admin
481
- // gazetteer is one shard, all countries), then verify each exact candidate against the postcode's
482
- // code subset resolved in the CANDIDATE's own country (postcode shards route by country). A
483
- // cross-country promotion is accepted ONLY postcode-verified within the gate — never ungated —
447
+ // gazetteer is one extract, all countries), then verify each exact candidate against the postcode's
448
+ // code subset resolved in the CANDIDATE's own country (postcode extracts route by country). A
449
+ // cross-country promotion is accepted ONLY postcode-verified within the radius — never unverified —
484
450
  // so a US-shaped query can't wander abroad on a name coincidence (the 48026 guard: resolved
485
451
  // trees never reach this code, and unresolved ones must pass the joint postcode check).
486
- if (opts.postalCompoundRecovery && postcode && gateKm > 0) {
452
+ if (opts.postalCompoundRecovery && postcode && thresholdKm > 0) {
487
453
  const code = postcodeCodeSubset(postcode) || postcode.trim()
488
454
 
489
455
  for (const sp of spans) {
@@ -509,13 +475,13 @@ export async function findRescoreCandidate(
509
475
 
510
476
  const verified = pcHits.find((p) => p.lat !== 0 || p.lon !== 0)
511
477
 
512
- if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <= gateKm) {
478
+ if (verified && haversineKm(verified.lat, verified.lon, h.lat, h.lon) <= thresholdKm) {
513
479
  // No alternatives from this pass, deliberately. Admission here is per-candidate postcode
514
480
  // verification — one `findPlace` per runner-up — and the class it serves is the opposite of the
515
481
  // namesake one: a postcode is present and has already picked the country, so there is nothing
516
482
  // ambiguous left to declare. The bare-toponym queries #1537 is about never reach this branch (it
517
483
  // requires a postcode).
518
- return { text: sp.text, start: sp.start, end: sp.end, place: h, gated: true, alternatives: [] }
484
+ return { text: sp.text, start: sp.start, end: sp.end, place: h, postcodeVerified: true, alternatives: [] }
519
485
  }
520
486
  }
521
487
  }
@@ -27,6 +27,8 @@
27
27
  * mirroring the `PlaceLookup` pattern.
28
28
  */
29
29
 
30
+ import type { Exclusion } from "@mailwoman/evidence"
31
+
30
32
  /**
31
33
  * A street-name existence probe. Backend-agnostic; the FR instance is BAN street-centroids, a future US instance is
32
34
  * TIGER, etc. (per the registry-backed-structured-prediction doctrine tiers).
@@ -147,10 +149,18 @@ export interface PickByStreetEvidenceOpts {
147
149
  * G2 — the margin cap. A candidate whose score is more than this far below rank-1 is never promoted by evidence
148
150
  * (without it, evidence reaches deep down the list and moves off correct rank-1 parses). Default 2.5 — the value the
149
151
  * v2 board measured (148 fixes / 3 breaks). UNCALIBRATED across models: re-fit when the span head retrains, since raw
150
- * score margins are not comparable across models. (Plan #1134 pre-registers an isotonic ambiguity gate to replace
152
+ * score margins are not comparable across models. (Plan #1134 pre-registers an isotonic ambiguity check to replace
151
153
  * it.)
152
154
  */
153
155
  marginCap?: number
156
+ /**
157
+ * One entry per candidate, positionally aligned. A non-null entry DEMOTES that candidate by one bit — it is
158
+ * considered only after every un-excluded sibling. It is never removed: with every candidate excluded the pick is
159
+ * still rank-1, because the worst case this policy accepts is the model's own ranking.
160
+ *
161
+ * Omitted or all-null reproduces the measured v2 policy exactly.
162
+ */
163
+ exclusions?: ReadonlyArray<Exclusion | null>
154
164
  }
155
165
 
156
166
  export interface StreetEvidencePick<T = unknown> {
@@ -166,6 +176,11 @@ export interface StreetEvidencePick<T = unknown> {
166
176
  * True when evidence MOVED the pick off rank-1 (a rank-2-beats-rank-1 correction — loggable training signal).
167
177
  */
168
178
  moved: boolean
179
+ /**
180
+ * Indices an exclusion demoted, in input order. Empty when no exclusion applied — a loggable record of what the
181
+ * coverage check licensed, distinct from what evidence found.
182
+ */
183
+ demoted: number[]
169
184
  }
170
185
 
171
186
  /**
@@ -173,7 +188,8 @@ export interface StreetEvidencePick<T = unknown> {
173
188
  * first candidate whose street surface passes ALL of: (1) exists in the index, (2) G1 — not pure type vocabulary, (3)
174
189
  * G2 — within `marginCap` of rank-1. If none passes, return rank-1 (fail-open). Positive evidence only; the model's
175
190
  * order is preserved among equal-evidence candidates. This is the `resolver/rerank.ts` anti-Pelias discipline applied
176
- * to the name signal: one bit, no blending.
191
+ * to the name signal: one bit, no blending. `opts.exclusions` adds one more bit in the same fold: a coverage-licensed
192
+ * absence demotes its candidate behind every un-excluded sibling and never removes it.
177
193
  *
178
194
  * @param candidates Parse candidates, rank-1 FIRST (the caller sorts by score descending).
179
195
  */
@@ -190,22 +206,39 @@ export function pickByStreetEvidence<T>(
190
206
 
191
207
  const rank1 = candidates[0]!
192
208
  const topScore = rank1.score
209
+ const demoted: number[] = []
210
+ const considered: number[] = []
193
211
 
194
212
  for (let i = 0; i < candidates.length; i++) {
213
+ if (opts.exclusions?.[i]) {
214
+ demoted.push(i)
215
+ } else {
216
+ considered.push(i)
217
+ }
218
+ }
219
+
220
+ // An excluded candidate is considered only after every un-excluded sibling; it is never dropped.
221
+ const order = demoted.length ? [...considered, ...demoted] : considered
222
+
223
+ for (const i of order) {
195
224
  const c = candidates[i]!
196
225
 
197
226
  if (!c.streetSurface) continue
198
227
 
199
- // G2: candidates are score-ordered, so once one falls past the cap nothing deeper qualifies either.
200
- if (topScore - c.score > marginCap) break
228
+ // G2: the margin is measured from rank-1's score, whatever position the candidate is considered at.
229
+ if (topScore - c.score > marginCap) continue
201
230
 
202
231
  // G1: a pure street-type/particle surface (bare `rue`) carries no name — no evidence credit.
203
232
  if (isPureTypeVocabulary(foldStreetSurface(c.streetSurface))) continue
204
233
 
205
234
  if (evidence.hasStreetName(c.streetSurface, opts.scope)) {
206
- return { candidate: c, index: i, moved: i > 0 }
235
+ return { candidate: c, index: i, moved: i > 0, demoted }
207
236
  }
208
237
  }
209
238
 
210
- return { candidate: rank1, index: 0, moved: false }
239
+ // Fail-open: the first un-excluded candidate, which is rank-1 unless an exclusion demoted it, and rank-1 again when
240
+ // every candidate is excluded.
241
+ const fallback = order[0] ?? 0
242
+
243
+ return { candidate: candidates[fallback]!, index: fallback, moved: fallback > 0, demoted }
211
244
  }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ */
6
+
7
+ export * from "#street/evidence"
8
+ export * from "#street/tier"