@mailwoman/resolver 10.0.0 → 10.2.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 (134) hide show
  1. package/README.md +9 -7
  2. package/lib/admin/coherence-passes.ts +31 -172
  3. package/lib/admin/containment.ts +15 -33
  4. package/lib/admin/jp-municipality.ts +21 -14
  5. package/lib/admin/winner.ts +37 -69
  6. package/lib/backend-capabilities.ts +28 -27
  7. package/lib/bare-toponym-race.ts +69 -37
  8. package/lib/decorate-node.ts +25 -49
  9. package/lib/fold-name.ts +5 -18
  10. package/lib/index.ts +5 -9
  11. package/lib/plausibility.ts +41 -68
  12. package/lib/postcode/country-coherence.ts +92 -296
  13. package/lib/postcode/prefix.ts +24 -86
  14. package/lib/postcode/shape-coherence.ts +26 -116
  15. package/lib/remote-resolver.ts +38 -33
  16. package/lib/rerank.ts +41 -54
  17. package/lib/resolve/passes.ts +188 -177
  18. package/lib/resolve.ts +744 -0
  19. package/lib/span-rescore.ts +219 -175
  20. package/lib/street/evidence.ts +65 -73
  21. package/lib/street/tier.ts +51 -194
  22. package/lib/toponym-prior.ts +54 -180
  23. package/out/admin/coherence-passes.d.ts +18 -69
  24. package/out/admin/coherence-passes.d.ts.map +1 -1
  25. package/out/admin/coherence-passes.js +24 -166
  26. package/out/admin/coherence-passes.js.map +1 -1
  27. package/out/admin/containment.d.ts +13 -31
  28. package/out/admin/containment.d.ts.map +1 -1
  29. package/out/admin/containment.js +13 -31
  30. package/out/admin/containment.js.map +1 -1
  31. package/out/admin/jp-municipality.d.ts +21 -14
  32. package/out/admin/jp-municipality.d.ts.map +1 -1
  33. package/out/admin/jp-municipality.js +18 -12
  34. package/out/admin/jp-municipality.js.map +1 -1
  35. package/out/admin/winner.d.ts +32 -64
  36. package/out/admin/winner.d.ts.map +1 -1
  37. package/out/admin/winner.js +23 -56
  38. package/out/admin/winner.js.map +1 -1
  39. package/out/{admin/index.d.ts → admin.d.ts} +1 -1
  40. package/out/admin.d.ts.map +1 -0
  41. package/out/{admin/index.js → admin.js} +1 -1
  42. package/out/admin.js.map +1 -0
  43. package/out/backend-capabilities.d.ts +19 -20
  44. package/out/backend-capabilities.d.ts.map +1 -1
  45. package/out/backend-capabilities.js +28 -26
  46. package/out/backend-capabilities.js.map +1 -1
  47. package/out/bare-toponym-race.d.ts +54 -27
  48. package/out/bare-toponym-race.d.ts.map +1 -1
  49. package/out/bare-toponym-race.js +69 -37
  50. package/out/bare-toponym-race.js.map +1 -1
  51. package/out/decorate-node.d.ts +6 -8
  52. package/out/decorate-node.d.ts.map +1 -1
  53. package/out/decorate-node.js +25 -49
  54. package/out/decorate-node.js.map +1 -1
  55. package/out/fold-name.d.ts +5 -18
  56. package/out/fold-name.d.ts.map +1 -1
  57. package/out/fold-name.js +5 -18
  58. package/out/fold-name.js.map +1 -1
  59. package/out/index.d.ts +5 -6
  60. package/out/index.d.ts.map +1 -1
  61. package/out/index.js +5 -8
  62. package/out/index.js.map +1 -1
  63. package/out/plausibility.d.ts +30 -60
  64. package/out/plausibility.d.ts.map +1 -1
  65. package/out/plausibility.js +19 -52
  66. package/out/plausibility.js.map +1 -1
  67. package/out/postcode/country-coherence.d.ts +53 -167
  68. package/out/postcode/country-coherence.d.ts.map +1 -1
  69. package/out/postcode/country-coherence.js +42 -259
  70. package/out/postcode/country-coherence.js.map +1 -1
  71. package/out/postcode/prefix.d.ts +18 -69
  72. package/out/postcode/prefix.d.ts.map +1 -1
  73. package/out/postcode/prefix.js +11 -78
  74. package/out/postcode/prefix.js.map +1 -1
  75. package/out/postcode/shape-coherence.d.ts +18 -85
  76. package/out/postcode/shape-coherence.d.ts.map +1 -1
  77. package/out/postcode/shape-coherence.js +11 -107
  78. package/out/postcode/shape-coherence.js.map +1 -1
  79. package/out/{postcode/index.d.ts → postcode.d.ts} +1 -1
  80. package/out/postcode.d.ts.map +1 -0
  81. package/out/{postcode/index.js → postcode.js} +1 -1
  82. package/out/postcode.js.map +1 -0
  83. package/out/remote-resolver.d.ts +32 -31
  84. package/out/remote-resolver.d.ts.map +1 -1
  85. package/out/remote-resolver.js +12 -20
  86. package/out/remote-resolver.js.map +1 -1
  87. package/out/rerank.d.ts +32 -46
  88. package/out/rerank.d.ts.map +1 -1
  89. package/out/rerank.js +16 -41
  90. package/out/rerank.js.map +1 -1
  91. package/out/resolve/passes.d.ts +108 -95
  92. package/out/resolve/passes.d.ts.map +1 -1
  93. package/out/resolve/passes.js +67 -126
  94. package/out/resolve/passes.js.map +1 -1
  95. package/out/resolve.d.ts +15 -0
  96. package/out/resolve.d.ts.map +1 -0
  97. package/out/resolve.js +557 -0
  98. package/out/resolve.js.map +1 -0
  99. package/out/span-rescore.d.ts +56 -65
  100. package/out/span-rescore.d.ts.map +1 -1
  101. package/out/span-rescore.js +134 -132
  102. package/out/span-rescore.js.map +1 -1
  103. package/out/street/evidence.d.ts +56 -61
  104. package/out/street/evidence.d.ts.map +1 -1
  105. package/out/street/evidence.js +22 -46
  106. package/out/street/evidence.js.map +1 -1
  107. package/out/street/tier.d.ts +18 -30
  108. package/out/street/tier.d.ts.map +1 -1
  109. package/out/street/tier.js +43 -191
  110. package/out/street/tier.js.map +1 -1
  111. package/out/{street/index.d.ts → street.d.ts} +1 -1
  112. package/out/street.d.ts.map +1 -0
  113. package/out/{street/index.js → street.js} +1 -1
  114. package/out/street.js.map +1 -0
  115. package/out/toponym-prior.d.ts +22 -110
  116. package/out/toponym-prior.d.ts.map +1 -1
  117. package/out/toponym-prior.js +41 -170
  118. package/out/toponym-prior.js.map +1 -1
  119. package/package.json +44 -147
  120. package/lib/resolve/index.ts +0 -989
  121. package/out/admin/index.d.ts.map +0 -1
  122. package/out/admin/index.js.map +0 -1
  123. package/out/postcode/index.d.ts.map +0 -1
  124. package/out/postcode/index.js.map +0 -1
  125. package/out/resolve/index.d.ts +0 -20
  126. package/out/resolve/index.d.ts.map +0 -1
  127. package/out/resolve/index.js +0 -808
  128. package/out/resolve/index.js.map +0 -1
  129. package/out/street/index.d.ts.map +0 -1
  130. package/out/street/index.js.map +0 -1
  131. package/vitest.config.ts +0 -38
  132. /package/lib/{admin/index.ts → admin.ts} +0 -0
  133. /package/lib/{postcode/index.ts → postcode.ts} +0 -0
  134. /package/lib/{street/index.ts → street.ts} +0 -0
@@ -2,62 +2,46 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * #727 stage-2 phase 4c — street-NAME existence as the k-best arbiter signal.
7
- *
8
- * Phase 4a measured the planned arbiter (full-geocode resolution tier) at exactly ZERO collected
9
- * headroom: the failing class is context-free fragments, which never reach rooftop layers, so
10
- * every hypothesis ties at admin tier. The corrected signal is street-NAME existence — "does this
11
- * hypothesis's street surface exist as a street name in the national register?" — which IS
12
- * queryable for a bare fragment. Measured on the FR fragment board over the v3.10.1 8k span model:
13
- * the rerank collects +6.0pp street@1 (0.791 → 0.851), 96 fixes / 3 breaks (32:1), the value
14
- * concentrated on the date-name class (+16.7pp). Receipts:
15
- * `docs/articles/evals/2026-07-17-v3101-span-head-8k-result.md`, spec
16
- * `docs/superpowers/specs/2026-07-17-727-phase4c-street-name-evidence.md`.
17
- *
18
- * THE ANTI-PELIAS RULE (shared with `rerank.ts`): ONE bit of evidence, not a score. We do NOT
19
- * blend the name signal into the parse scores — those already share a partition function and rank
20
- * fine within an input. The reranker's only job is to prefer a sibling hypothesis whose street the
21
- * world confirms exists, over a rank-1 whose street it does not. Positive evidence only: the
22
- * ABSENCE of a name is never evidence against a parse (index incompleteness is the default state
23
- * of the world), so the policy always fails open to the model's own ranking.
24
- *
25
- * This module is PURE — the interface + the fold + the measured pick policy, no SQLite. The FR BAN
26
- * backend lives in `@mailwoman/resolver-wof-sqlite` (`SQLiteStreetNameLookup`); callers inject it,
27
- * mirroring the `PlaceLookup` pattern.
28
5
  */
29
6
 
30
7
  import type { Exclusion } from "@mailwoman/evidence"
31
8
 
32
9
  /**
33
- * A street-name existence probe. Backend-agnostic; the FR instance is BAN street-centroids, a future US instance is
34
- * TIGER, etc. (per the registry-backed-structured-prediction doctrine tiers).
10
+ * Answers whether a street name exists, optionally within a locality or postcode,
11
+ * for the countries a backend such as BAN street centroids covers.
35
12
  */
36
13
  export interface StreetLocalityEvidence {
37
14
  /**
38
- * True when `streetSurface` exists as a street name — optionally scoped to a locality or postcode when the hypothesis
39
- * carries one (fragments usually don't; unscoped is the measured mode). The implementation is responsible for folding
40
- * the surface with {@link foldStreetSurface} so the caller passes raw text.
15
+ * Returns whether the raw street surface exists as a street name within `scope`, when provided.
41
16
  *
42
- * POSITIVE EVIDENCE ONLY: return `false` on any doubt — a missing index, an unsupported country, a read error — so
43
- * {@link pickByStreetEvidence} fails open to the model's ranking. Absence is never a veto.
17
+ * The implementation folds the surface itself.
18
+ * It returns `false` when an index is missing or a read fails.
19
+ * A `false` answer only withholds promotion.
44
20
  */
45
21
  hasStreetName(streetSurface: string, scope?: StreetEvidenceScope): boolean
22
+
46
23
  /**
47
- * ISO-2 (upper-case) countries this instance can answer for. Anything else → no evidence, never a veto.
24
+ * The upper-case ISO 3166-1 alpha-2 countries this instance covers.
48
25
  */
49
26
  readonly countries: ReadonlySet<string>
50
27
  }
51
28
 
29
+ /**
30
+ * Narrows a street-name existence probe to a locality, a postcode, or both.
31
+ */
52
32
  export interface StreetEvidenceScope {
53
33
  locality?: string
54
34
  postcode?: string
55
35
  }
56
36
 
57
37
  /**
58
- * The fold CONTRACT — the index builder and every runtime prober MUST share this exact function, or lookups silently
59
- * miss (the 4 v1-policy breaks were fold mismatches: `pillet-will` stored unhyphenated). NFD strip-diacritics,
60
- * lowercase, hyphen/apostrophe → space, whitespace-collapse. Import this beside the interface; never re-implement it.
38
+ * Folds a street surface the same way for index build and lookup.
39
+ *
40
+ * It strips diacritics and lowercases.
41
+ * It turns hyphens and dashes into spaces.
42
+ *
43
+ * It maps apostrophes to spaces too.
44
+ * It also collapses whitespace.
61
45
  */
62
46
  export function foldStreetSurface(surface: string): string {
63
47
  return surface
@@ -69,12 +53,6 @@ export function foldStreetSurface(surface: string): string {
69
53
  .trim()
70
54
  }
71
55
 
72
- /**
73
- * FR street-type + particle vocabulary — the G1 guard. A street surface made ONLY of these words carries no NAME (bare
74
- * `rue`/`chemin` is a truncation, and it IS in the index), so it earns no evidence credit. Folded forms (particles are
75
- * pre-folded: `l'` → `l`). Kept small and lexical — it is a dictionary fact, not a tuned weight (the anti-Pelias
76
- * line).
77
- */
78
56
  const FR_STREET_TYPE_WORDS: ReadonlySet<string> = new Set([
79
57
  "rue",
80
58
  "avenue",
@@ -112,10 +90,11 @@ const FR_STREET_TYPE_WORDS: ReadonlySet<string> = new Set([
112
90
  ])
113
91
 
114
92
  /**
115
- * True when the folded surface contains no token outside {@link FR_STREET_TYPE_WORDS} — i.e. it is pure type/particle.
93
+ * Returns true when a folded street surface is empty or consists only of French
94
+ * street-type words and particles, such as "rue de la".
116
95
  */
117
96
  export function isPureTypeVocabulary(foldedSurface: string): boolean {
118
- const tokens = foldedSurface.split(" ").filter((value) => value.length > 0)
97
+ const tokens = foldedSurface.split(" ").filter((value) => value.length)
119
98
 
120
99
  if (!tokens.length) return true
121
100
 
@@ -123,75 +102,93 @@ export function isPureTypeVocabulary(foldedSurface: string): boolean {
123
102
  }
124
103
 
125
104
  /**
126
- * One candidate parse for the street-evidence rerank — its street surface + its (within-input comparable) score.
105
+ * One candidate parse for {@link pickByStreetEvidence}; scores are comparable
106
+ * only among candidates for the same input.
127
107
  */
128
108
  export interface StreetCandidate<T = unknown> {
129
109
  /**
130
- * The candidate's street surface (raw; folded internally). Empty string = no street parsed → never the evidence pick.
110
+ * The candidate's raw street surface.
111
+ *
112
+ * An empty string means the parse found no street.
113
+ * Evidence skips that candidate.
131
114
  */
132
115
  streetSurface: string
116
+
133
117
  /**
134
- * The parse score, comparable to its siblings from the SAME input. Higher is better.
118
+ * The parse score, where higher is better.
135
119
  */
136
120
  score: number
121
+
137
122
  /**
138
- * Opaque caller payload carried through to the result (the segmentation, the tree, …).
123
+ * An opaque caller value, such as the segmentation or tree, returned unchanged with the pick.
139
124
  */
140
125
  payload?: T
141
126
  }
142
127
 
128
+ /**
129
+ * Options for {@link pickByStreetEvidence}.
130
+ */
143
131
  export interface PickByStreetEvidenceOpts {
144
132
  /**
145
- * Locality/postcode scope forwarded to {@link StreetLocalityEvidence.hasStreetName} (fragments usually carry none).
133
+ * The locality or postcode scope passed to {@link StreetLocalityEvidence.hasStreetName}.
146
134
  */
147
135
  scope?: StreetEvidenceScope
136
+
148
137
  /**
149
- * G2 — the margin cap. A candidate whose score is more than this far below rank-1 is never promoted by evidence
150
- * (without it, evidence reaches deep down the list and moves off correct rank-1 parses). Default 2.5 — the value the
151
- * v2 board measured (148 fixes / 3 breaks). UNCALIBRATED across models: re-fit when the span head retrains, since raw
152
- * score margins are not comparable across models. (Plan #1134 pre-registers an isotonic ambiguity check to replace
153
- * it.)
138
+ * The largest score gap below rank 1 at which evidence may still promote a candidate.
139
+ * it defaults to 2.5 and needs refitting when the span head is retrained.
154
140
  */
155
141
  marginCap?: number
142
+
156
143
  /**
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.
144
+ * One entry per candidate, in the same order.
160
145
  *
161
- * Omitted or all-null reproduces the measured v2 policy exactly.
146
+ * A non-null entry moves that candidate behind every candidate without an exclusion.
147
+ * If every candidate has an exclusion, the fallback pick is rank 1.
162
148
  */
163
149
  exclusions?: ReadonlyArray<Exclusion | null>
164
150
  }
165
151
 
152
+ /**
153
+ * The result of {@link pickByStreetEvidence}.
154
+ */
166
155
  export interface StreetEvidencePick<T = unknown> {
167
156
  /**
168
- * The chosen candidate — the first evidence-passing sibling, or rank-1 when none passes (fail-open).
157
+ * The first candidate that passes the evidence checks, or the first non-excluded
158
+ * candidate when none passes.
169
159
  */
170
160
  candidate: StreetCandidate<T>
161
+
171
162
  /**
172
- * Index of the chosen candidate in the input array.
163
+ * The chosen candidate's index in the input array.
173
164
  */
174
165
  index: number
166
+
175
167
  /**
176
- * True when evidence MOVED the pick off rank-1 (a rank-2-beats-rank-1 correction — loggable training signal).
168
+ * Whether the pick differs from rank 1, through evidence or because rank 1 was excluded.
177
169
  */
178
170
  moved: boolean
171
+
179
172
  /**
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.
173
+ * The input indexes that exclusions demoted, in input order.
182
174
  */
183
175
  demoted: number[]
184
176
  }
185
177
 
186
178
  /**
187
- * The measured v2 rerank policy. Given candidates in PARSE-SCORE order (rank-1 first) and an evidence probe, return the
188
- * first candidate whose street surface passes ALL of: (1) exists in the index, (2) G1 — not pure type vocabulary, (3)
189
- * G2 — within `marginCap` of rank-1. If none passes, return rank-1 (fail-open). Positive evidence only; the model's
190
- * order is preserved among equal-evidence candidates. This is the `resolver/rerank.ts` anti-Pelias discipline applied
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.
179
+ * Checks candidates in score order.
180
+ *
181
+ * Selects the first candidate that meets all three checks.
182
+ * Its street name exists in the evidence index.
183
+ *
184
+ * The name contains more than street-type words.
185
+ * Its score falls within `marginCap` of rank 1.
186
+ *
187
+ * Excluded candidates are checked after all others.
188
+ * The first candidate without an exclusion wins when no candidate passes the evidence checks.
193
189
  *
194
- * @param candidates Parse candidates, rank-1 FIRST (the caller sorts by score descending).
190
+ * @param candidates Parse candidates sorted by score, rank 1 first.
191
+ * @throws When `candidates` is empty.
195
192
  */
196
193
  export function pickByStreetEvidence<T>(
197
194
  candidates: ReadonlyArray<StreetCandidate<T>>,
@@ -217,7 +214,6 @@ export function pickByStreetEvidence<T>(
217
214
  }
218
215
  }
219
216
 
220
- // An excluded candidate is considered only after every un-excluded sibling; it is never dropped.
221
217
  const order = demoted.length ? [...considered, ...demoted] : considered
222
218
 
223
219
  for (const i of order) {
@@ -225,10 +221,8 @@ export function pickByStreetEvidence<T>(
225
221
 
226
222
  if (!c.streetSurface) continue
227
223
 
228
- // G2: the margin is measured from rank-1's score, whatever position the candidate is considered at.
229
224
  if (topScore - c.score > marginCap) continue
230
225
 
231
- // G1: a pure street-type/particle surface (bare `rue`) carries no name — no evidence credit.
232
226
  if (isPureTypeVocabulary(foldStreetSurface(c.streetSurface))) continue
233
227
 
234
228
  if (evidence.hasStreetName(c.streetSurface, opts.scope)) {
@@ -236,8 +230,6 @@ export function pickByStreetEvidence<T>(
236
230
  }
237
231
  }
238
232
 
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
233
  const fallback = order[0] ?? 0
242
234
 
243
235
  return { candidate: candidates[fallback]!, index: fallback, moved: fallback > 0, demoted }