@mailwoman/resolver 10.0.0 → 10.1.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 +19 -160
  3. package/lib/admin/containment.ts +12 -30
  4. package/lib/admin/jp-municipality.ts +20 -13
  5. package/lib/admin/winner.ts +30 -62
  6. package/lib/backend-capabilities.ts +25 -24
  7. package/lib/bare-toponym-race.ts +69 -37
  8. package/lib/decorate-node.ts +17 -41
  9. package/lib/fold-name.ts +5 -18
  10. package/lib/index.ts +5 -9
  11. package/lib/plausibility.ts +35 -62
  12. package/lib/postcode/country-coherence.ts +64 -284
  13. package/lib/postcode/prefix.ts +20 -84
  14. package/lib/postcode/shape-coherence.ts +23 -113
  15. package/lib/remote-resolver.ts +32 -29
  16. package/lib/rerank.ts +34 -50
  17. package/lib/resolve/passes.ts +144 -147
  18. package/lib/resolve.ts +726 -0
  19. package/lib/span-rescore.ts +215 -172
  20. package/lib/street/evidence.ts +65 -73
  21. package/lib/street/tier.ts +33 -175
  22. package/lib/toponym-prior.ts +53 -179
  23. package/out/admin/coherence-passes.d.ts +17 -68
  24. package/out/admin/coherence-passes.d.ts.map +1 -1
  25. package/out/admin/coherence-passes.js +18 -160
  26. package/out/admin/coherence-passes.js.map +1 -1
  27. package/out/admin/containment.d.ts +11 -29
  28. package/out/admin/containment.d.ts.map +1 -1
  29. package/out/admin/containment.js +12 -30
  30. package/out/admin/containment.js.map +1 -1
  31. package/out/admin/jp-municipality.d.ts +20 -13
  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 +30 -62
  36. package/out/admin/winner.d.ts.map +1 -1
  37. package/out/admin/winner.js +18 -51
  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 +18 -19
  44. package/out/backend-capabilities.d.ts.map +1 -1
  45. package/out/backend-capabilities.js +25 -24
  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 +17 -41
  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 +27 -57
  64. package/out/plausibility.d.ts.map +1 -1
  65. package/out/plausibility.js +16 -49
  66. package/out/plausibility.js.map +1 -1
  67. package/out/postcode/country-coherence.d.ts +48 -162
  68. package/out/postcode/country-coherence.d.ts.map +1 -1
  69. package/out/postcode/country-coherence.js +19 -252
  70. package/out/postcode/country-coherence.js.map +1 -1
  71. package/out/postcode/prefix.d.ts +17 -68
  72. package/out/postcode/prefix.d.ts.map +1 -1
  73. package/out/postcode/prefix.js +8 -77
  74. package/out/postcode/prefix.js.map +1 -1
  75. package/out/postcode/shape-coherence.d.ts +17 -84
  76. package/out/postcode/shape-coherence.d.ts.map +1 -1
  77. package/out/postcode/shape-coherence.js +9 -105
  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 +27 -28
  84. package/out/remote-resolver.d.ts.map +1 -1
  85. package/out/remote-resolver.js +9 -19
  86. package/out/remote-resolver.js.map +1 -1
  87. package/out/rerank.d.ts +28 -45
  88. package/out/rerank.d.ts.map +1 -1
  89. package/out/rerank.js +13 -38
  90. package/out/rerank.js.map +1 -1
  91. package/out/resolve/passes.d.ts +95 -85
  92. package/out/resolve/passes.d.ts.map +1 -1
  93. package/out/resolve/passes.js +40 -105
  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 +543 -0
  98. package/out/resolve.js.map +1 -0
  99. package/out/span-rescore.d.ts +51 -64
  100. package/out/span-rescore.d.ts.map +1 -1
  101. package/out/span-rescore.js +132 -130
  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 +17 -29
  108. package/out/street/tier.d.ts.map +1 -1
  109. package/out/street/tier.js +27 -175
  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 +21 -109
  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,139 +2,38 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * #17 bare city-name disambiguation — two SOFT ranking keys for the bare-toponym class.
7
- *
8
- * A bare city name is the one query shape where the resolver has no geography to reason with: no
9
- * postcode to anchor against, no region to scope by, no country the address itself named. All that
10
- * is left is a prior over which same-named place a person typing that word alone probably meant.
11
- * The shipped prior is POPULATION, and population is measurably the wrong prior for this class —
12
- * the panel run of 2026-08-10 caught it four times over:
13
- *
14
- * | query | population winner | gold | miss |
15
- * | ------- | -------------------- | --------------------- | -------- |
16
- * | Whitby | Whitby ON (128,377) | Whitby, N Yorks (13,130) | 5,508 km |
17
- * | Warwick | Warwick RI (82,999) | Warwick, Warks (32,719) | 5,211 km |
18
- * | Epping | Epping VIC (32,395) | Epping, Essex (12,547) | 16,874 km |
19
- * | Windsor | Windsor ON (217,188) | Windsor, Berks (26,885) | 6,007 km |
20
- *
21
- * Every one of those is a place people know for something other than its size, and the gazetteer
22
- * already measures that: `admin-global-priority-importance.db` ranks all four the RIGHT way round
23
- * (Whitby GB 0.5496 over CA 0.5089; Windsor GB 0.5648 over CA 0.5607 — a 0.004 margin population
24
- * inverts by a factor of eight). What it measures is a BLENDED global toponym prior
25
- * (`ResolvedPlace.importance`): the concordance's encyclopedia-derived channel where a concordance
26
- * matched, a population-derived proxy everywhere else — the one scale on which every bearer of a
27
- * name is scored comparably, which is the precondition for comparing them at all.
28
- * {@link rankByImportance} consumes it. (The STRICT encyclopedia-evidence channel keeps its own
29
- * reserved slot, `ResolvedPlace.encyclopedic`, which no ranking key reads and no shipped artifact
30
- * yet populates — see `core/resolver/types.ts`.)
31
- *
32
- * {@link rankByCountryPrior} covers the other half of the class — the query where a country IS
33
- * known, but only because a locale said so. See `span-rescore.ts` for where that one applies.
34
- *
35
- * Both obey the same three house rules, and neither is a check:
36
- *
37
- * 1. **Tier-safe.** `exactMatch` stays the primary key. A soft prior re-orders WITHIN a tier; it never
38
- * promotes a partial match over an exact one.
39
- * 2. **Positive evidence only.** An absent score is unmeasured, not zero (the meaning-of-zero rule), so an
40
- * unscored candidate is never moved BY the signal and never penalized FOR lacking it — it keeps the
41
- * rank population gave it while the scored rows reorder among themselves. On a candidate.db built
42
- * before the #28 `importance` column, nothing is scored, so {@link rankByImportance} is
43
- * byte-stable by construction.
44
- * 3. **Stable.** Equal keys keep their incoming order, so the backend's own ranking survives underneath. That order
45
- * carries decisions only the backend can make — the candidate backend's seat tiebreak
46
- * (`resolver-wof-sqlite/primary-preference.ts`) reaches an end-to-end answer solely through this stability
47
- * (#1729), so equal-key stability here is required, not cosmetic.
48
5
  */
49
6
 
50
7
  import type { ResolvedPlace } from "@mailwoman/core/resolver"
51
8
 
52
9
  /**
53
- * Weight of the locale-country bonus, in the same log10-population units the candidate backend's `prominence` uses
54
- * (`-neg_rank` = log10(population + 1)). 2 == "the in-country place may be up to 100x smaller and still win".
10
+ * The default bonus, in log10-population units, that {@link rankByCountryPrior}
11
+ * gives a candidate in the locale country.
55
12
  *
56
- * The value is the resolver's existing `anchorWeight` default (`resolve.ts`), reused deliberately: that knob is the
57
- * postcode-anchor country pin, and this is the same quantity from a weaker source, so the two should not disagree about
58
- * what a country signal is worth. Calibrated against the live board (2026-08-10, shipped `candidate.db`), it moves
59
- * exactly the queries where the locale is implausible and leaves the rest alone:
60
- *
61
- * - `Zürich` en-US — Zurich KS 1.91 + 2 = 3.91 loses to Zürich CH 5.65. **Flips** (was 8,043 km off).
62
- * - `Berlin` en-US — Berlin CT 4.30 + 2 = 6.30 loses to Berlin DE 6.56. **Flips.**
63
- * - `Manchester` en-US — Manchester NH 5.06 + 2 = 7.06 beats Manchester GB 5.74. **Held.**
64
- * - `Springfield` en-US — every bearer is US, so the bonus is uniform. **Held.**
65
- * - `Moscow` en-US — Moscow ID 4.42 + 2 = 6.42 beats Москва 6.10. **Held.**
66
- * - `Fulda` en-US — Fulda MN 3.13 + 2 = 5.13 beats Fulda DE 4.84. **Held.**
13
+ * A weight of 2 lets an in-country place be up to 100 times smaller and still win.
67
14
  */
68
15
  export const DEFAULT_COUNTRY_PRIOR_WEIGHT = 2
69
16
 
70
- /**
71
- * Importance margin below which two SAME-COUNTRY bearers are a tie, and the tie falls back to referential (size) order.
72
- * Ratified 2026-08-11 (the five bare-query flip decisions): bare `Springfield` must stay on the referential answer —
73
- * the live chain is IL 0.612605 → MA 0.611142 → MO 0.596195 (adjacent gaps 0.0015 and 0.0149, full span 0.0164), so the
74
- * band must cover at least the 0.0149 adjacent gap for the trio to chain into one cluster.
75
- *
76
- * The band never compares across countries, and that scope is forced by decided rows, not preference: Windsor's
77
- * accepted flip (GB 0.564842 over CA 0.560687) sits at a 0.0042 gap — inside ANY band that covers Springfield. The two
78
- * decisions are only co-satisfiable if the band binds same-country pairs alone. That is also what the §2 referential
79
- * policy (ROAD_TO_V9) says: within a country the geocoder ranks referentially; the blended prior's job is the
80
- * cross-country question — which country's bearer a bare query meant.
81
- */
82
17
  const SAME_COUNTRY_IMPORTANCE_TIE_BAND = 0.02
83
18
 
84
- /**
85
- * The fields a ranking key reads. Structural rather than `ResolvedPlace` so the backend's own `PlaceCandidate` (a
86
- * structural twin) can be ranked without a cast.
87
- */
88
19
  type Rankable = Pick<ResolvedPlace, "score"> &
89
- Partial<Pick<ResolvedPlace, "country" | "exactMatch" | "importance" | "prominence">>
20
+ Partial<Pick<ResolvedPlace, "country" | "exactMatch" | "importance" | "population" | "prominence">>
90
21
 
91
- /**
92
- * Partition into (exact, rest), sort each half with `key` DESC, and re-join. The partition is what keeps a soft prior
93
- * soft: it can only ever re-order candidates the backend already considers equally good matches. Backends that don't
94
- * stamp `exactMatch` land every row in one tier, which is the correct degradation — the key then applies to the whole
95
- * list.
96
- */
97
22
  function rankWithinTier<T extends Rankable>(candidates: readonly T[], compare: (a: T, b: T) => number): T[] {
98
23
  const exact: T[] = []
99
24
  const rest: T[] = []
100
25
 
101
26
  for (const c of candidates) {
102
- // `exactMatch` is TRI-STATE: true / false / undefined (a backend path that stamps no flag —
103
- // e.g. WOFSQLitePlaceLookup's postcode-area neighbours from #fetchLocalitiesByID). Only a
104
- // stated TRUE earns the exact tier; undefined must not outrank a real fuzzy name match
105
- // (the 2026-08-10 de.native_locality incident: 75 Saxon towns lost to nameless neighbours).
106
27
  ;(c.exactMatch === true ? exact : rest).push(c)
107
28
  }
108
29
 
109
- // `toSorted` is stable, so equal keys keep the backend's incoming order.
110
30
  return [...exact.toSorted(compare), ...rest.toSorted(compare)]
111
31
  }
112
32
 
113
- /**
114
- * Size, in the backend's own units — the candidate backend's `prominence` is log10(population + 1); a backend that
115
- * computes no prominence falls back to its raw `score`.
116
- */
117
33
  const size = (c: Rankable): number => c.prominence ?? c.score
118
34
 
119
- /**
120
- * A candidate the gazetteer actually scored. An absent value means "the score source never measured this place" OR
121
- * "this artifact predates the column" OR "the join refused the row" — three different things, none of them zero.
122
- */
123
35
  const measured = (c: Rankable): boolean => typeof c.importance === "number" && Number.isFinite(c.importance)
124
36
 
125
- /**
126
- * Reorder ONLY the measured candidates, and only among the positions they already occupy.
127
- *
128
- * This is what "positive evidence only" has to mean when coverage is partial, which it always is — measured on the
129
- * shipped importance artifact, the four bare GB panel rows have 2/7, 8/10, 8/10 and 9/10 of their candidates scored. A
130
- * blanket "abstain unless everything is measured" throws the signal away on all four; treating absent as 0 would let a
131
- * scored hamlet leapfrog an unscored metropolis. Neither is right, and the resolution is that an unmeasured row simply
132
- * does not participate: it holds the rank population gave it, and the scored rows permute among the slots they hold
133
- * between them.
134
- *
135
- * So for `Whitby` — CA(0.5089), GB(0.5496), TC(—), then four unscored bearers — the swap is confined to slots 0 and 1,
136
- * GB takes the lead, and TC never moves.
137
- */
138
37
  function reorderMeasured<T extends Rankable>(tier: readonly T[], order: (measuredRows: T[]) => T[]): T[] {
139
38
  const slots: number[] = []
140
39
 
@@ -155,18 +54,38 @@ function reorderMeasured<T extends Rankable>(tier: readonly T[], order: (measure
155
54
  return out
156
55
  }
157
56
 
158
- /**
159
- * The importance ordering over MEASURED rows, with the {@link SAME_COUNTRY_IMPORTANCE_TIE_BAND} applied.
160
- *
161
- * Same-country bearers whose adjacent importance gaps sit inside the band chain into one cluster (transitive on purpose
162
- * — otherwise the boundary would depend on comparison order), and a cluster orders its members referentially (size
163
- * DESC). Clusters — including every cross-country row, which is always its own cluster — rank by their most important
164
- * member, then head size, then input order. A cluster therefore moves as a unit: a same-country near-tie cannot be
165
- * split by a foreign row falling between its members' scores.
166
- */
57
+ const counted = (c: Rankable): boolean => typeof c.population === "number" && c.population > 0
58
+
59
+ function countedFirstWithinCountry<T extends Rankable>(rows: readonly T[]): T[] {
60
+ const out = [...rows]
61
+ const slotsByCountry = new Map<string, number[]>()
62
+
63
+ for (const [index, row] of out.entries()) {
64
+ const country = row.country?.toUpperCase()
65
+
66
+ if (!country) continue
67
+
68
+ const slots = slotsByCountry.get(country) ?? []
69
+
70
+ slots.push(index)
71
+ slotsByCountry.set(country, slots)
72
+ }
73
+
74
+ for (const slots of slotsByCountry.values()) {
75
+ if (slots.length < 2) continue
76
+
77
+ const members = slots.map((slot) => out[slot]!)
78
+ const reordered = [...members.filter((row) => counted(row)), ...members.filter((row) => !counted(row))]
79
+
80
+ for (const [k, slot] of slots.entries()) {
81
+ out[slot] = reordered[k]!
82
+ }
83
+ }
84
+
85
+ return out
86
+ }
87
+
167
88
  function orderMeasuredByImportance<T extends Rankable>(rows: readonly T[]): T[] {
168
- // Group by country in first-appearance order; a row without a country can never substantiate a
169
- // same-country tie, so it stays a singleton.
170
89
  const groups = new Map<string, { firstIndex: number; members: T[] }>()
171
90
  const clusters: Array<{ firstIndex: number; members: T[] }> = []
172
91
 
@@ -214,42 +133,26 @@ function orderMeasuredByImportance<T extends Rankable>(rows: readonly T[]): T[]
214
133
 
215
134
  keyed.sort((a, b) => b.key - a.key || size(b.members[0]!) - size(a.members[0]!) || a.firstIndex - b.firstIndex)
216
135
 
217
- return keyed.flatMap((c) => c.members)
136
+ return countedFirstWithinCountry(keyed.flatMap((c) => c.members))
218
137
  }
219
138
 
220
139
  /**
221
- * Importance-first ordering — the #28 fame prior, consumed.
140
+ * Reorders candidates by gazetteer `importance`, highest first, within the exact-match
141
+ * and partial-match tiers.
222
142
  *
223
- * Ranks by `importance` DESC within the exact tier, with `prominence`/`score` as the tiebreak (two places of equal fame
224
- * separate on size). Candidates the gazetteer never scored do not participate — they hold the rank population gave them
225
- * while the scored rows permute among their own slots (see {@link reorderMeasured}). Returns the input untouched when
226
- * fewer than two candidates are scored.
227
- *
228
- * The producer is the candidate build's `importance` column (#28) — the BLENDED prior (the concordance's
229
- * encyclopedia-derived channel clamped around a population-derived base — `place-importance-schema.ts`'s
230
- * `blendImportance`; see `candidate-schema.ts` → `CandidateTable.importance` for why the strict channel is deliberately
231
- * NOT what lands there). On an artifact predating the column, `importance` is `undefined` on every candidate the
232
- * backend produces and the abstention below keeps the ranking byte-stable.
233
- *
234
- * Flip decisions, ratified 2026-08-11: bare `Moscow`→Москва, `Manchester`→GB, `Fulda`→DE, `Cambridge`→GB are ACCEPTED
235
- * behavior — all cross-country contests, where this prior answers the question population cannot. Bare `Springfield`
236
- * ABSTAINS to the referential answer via {@link SAME_COUNTRY_IMPORTANCE_TIE_BAND}; no candidate is dropped, so the
237
- * runner-up survives as a declared alternative downstream.
143
+ * Candidates without an importance keep their positions.
144
+ * Same-country candidates whose importance differs by at most 0.02 are ordered by size
145
+ * instead, with populated places first within each country.
238
146
  */
239
147
  export function rankByImportance<T extends Rankable>(candidates: readonly T[]): T[] {
240
148
  if (candidates.length < 2) return [...candidates]
241
149
 
242
- // Abstain entirely until at least one candidate carries a MEASURED score — the tier split
243
- // below is only meaningful as a tiebreak among scored rows. Without this, wiring the consumer
244
- // before the #28 producer reordered candidates on the exactMatch flag alone (meaning-of-zero:
245
- // an absent score must not act as evidence).
246
150
  if (!candidates.some(measured)) return [...candidates]
247
151
 
248
152
  const exact: T[] = []
249
153
  const rest: T[] = []
250
154
 
251
155
  for (const c of candidates) {
252
- // Tri-state exactMatch: only a stated TRUE earns the exact tier (see rankWithinTier).
253
156
  ;(c.exactMatch === true ? exact : rest).push(c)
254
157
  }
255
158
 
@@ -257,15 +160,10 @@ export function rankByImportance<T extends Rankable>(candidates: readonly T[]):
257
160
  }
258
161
 
259
162
  /**
260
- * Soft locale-country prior: `(prominence ?? score) + weight` for a candidate in `country`.
261
- *
262
- * Additive, never a filter — a far more prominent foreign namesake still wins (Zürich CH over Zurich KS) while a close
263
- * contest goes to the locale (Manchester NH over Manchester GB). No-op without a `country`, so the un-scoped path stays
264
- * byte-identical.
163
+ * Reorders candidates within each match tier by size, adding `weight` for candidates in `country`.
265
164
  *
266
- * The units matter: `prominence` on the candidate backend is log10(population + 1), so `weight` is read as "orders of
267
- * magnitude of population the in-country place is allowed to give away". Pass a large weight to make the country
268
- * effectively hard, `0` to disable.
165
+ * It returns the candidates unchanged when no country is given.
166
+ * The bonus is additive, so a much larger foreign place of the same name still wins.
269
167
  */
270
168
  export function rankByCountryPrior<T extends Rankable>(
271
169
  candidates: readonly T[],
@@ -281,49 +179,26 @@ export function rankByCountryPrior<T extends Rankable>(
281
179
  }
282
180
 
283
181
  /**
284
- * Capital status of a resolved candidate, answered by the caller's reference (#1880 —
285
- * `@mailwoman/resolver-wof-sqlite/capitals` is the shipped implementation): 2 for a national capital, 1 for an admin-1
286
- * seat, 0 for neither. The resolver stays backend-agnostic — it never loads the reference, it only consumes the
287
- * verdict.
182
+ * Returns a place's capital status: 2 for a national capital, 1 for an admin-1 seat and 0 otherwise.
183
+ *
184
+ * The caller supplies it, so the resolver never loads a capitals reference itself.
288
185
  */
289
186
  export type CapitalLevelFn = (place: Pick<ResolvedPlace, "name" | "country" | "lat" | "lon">) => number
290
187
 
291
- /**
292
- * The level a candidate must hold to be PROMOTED: national capitals only. Admin-1 seats stay un-promoted, and that
293
- * scope is forced by decided rows, not caution — a seat margin of even 1 log10 unit flips bare `Springfield` to
294
- * Springfield, Illinois against the ratified 2026-08-11 referential decision
295
- * ({@link SAME_COUNTRY_IMPORTANCE_TIE_BAND}), and sends bare `Hamilton` to the Waikato seat (Hamilton NZ, 2.8x smaller
296
- * than Hamilton, Ontario), both measured on the shipped `candidate.db`. The reference still records seats; no consumer
297
- * promotes on them.
298
- */
299
188
  const PROMOTABLE_CAPITAL_LEVEL = 2
300
189
 
301
190
  /**
302
- * Margin a national capital may give away, in log10-population units: a namesake must be more than 10^2 = 100x more
303
- * populous to hold the lead. Bare "San José" reaches the Costa Rican capital (342,188) over San Jose, California
304
- * (969,655 — 2.8x); bare "Hamilton" stays on Hamilton, Ontario (519,949) over Bermuda's 902-person capital (576x).
191
+ * The size lead, in log10-population units, that another place needs to stay ahead of a national capital.
192
+ *
193
+ * A value of 2 means 100 times more populous.
305
194
  */
306
195
  export const NATIONAL_CAPITAL_MARGIN_LOG10 = 2
307
196
 
308
197
  /**
309
- * Bounded capital promotion (#1880) — the third soft key for the bare-toponym class, applied AFTER
310
- * {@link rankByImportance} because the fame prior is what decides this class and a capital signal parked below the
311
- * deciding stage measurably never reaches an answer (the iteration-1 board run: the candidate-order bonus reordered
312
- * `findPlace` on every target row and moved 0 of the 12).
313
- *
314
- * The rule: within the exact tier, take the first NATIONAL-capital row and walk it upward past each row it is within
315
- * {@link NATIONAL_CAPITAL_MARGIN_LOG10} of, stopping at the first row over the margin or at another national capital.
316
- * Same three house rules as the other keys — tier-safe (never crosses the exact/partial boundary.
317
- *
318
- * A capital that only PARTIALLY matched the query — Saint George's, Grenada for the abbreviated "St. George's" — stays
319
- * behind every exact match, because crossing that line is how iteration 1 answered bare "Djibouti" with the capital
320
- * city instead of the country), positive evidence only (no capital in the list → identity, and the margin reads the
321
- * same `prominence ?? score` size every other key does), and stable (non-promoted rows keep their order).
198
+ * Moves the first national capital in the leading match tier above each preceding row that
199
+ * is at most {@link NATIONAL_CAPITAL_MARGIN_LOG10} larger, stopping at another capital.
322
200
  *
323
- * The margin deliberately re-uses log10-population units rather than the importance scale: the ratified importance flip
324
- * decisions (Whitby/Windsor/Springfield) sit on gaps of 0.004–0.02, while the capital contests this key exists for sit
325
- * on gaps of 0.21–0.24 — an importance-scale bonus big enough to win them would dwarf every ratified decision on the
326
- * same scale.
201
+ * It runs after {@link rankByImportance} and never moves a capital across the exact-match boundary.
327
202
  */
328
203
  export function promoteCapitals<T extends Rankable & Pick<ResolvedPlace, "name" | "country" | "lat" | "lon">>(
329
204
  candidates: readonly T[],
@@ -335,7 +210,6 @@ export function promoteCapitals<T extends Rankable & Pick<ResolvedPlace, "name"
335
210
  const rest: T[] = []
336
211
 
337
212
  for (const c of candidates) {
338
- // Tri-state exactMatch, same reading as rankWithinTier: only a stated TRUE earns the tier.
339
213
  ;(c.exactMatch === true ? exact : rest).push(c)
340
214
  }
341
215
 
@@ -2,91 +2,40 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * The three post-walk admin coherence passes (#263 / #822 / the region-country pass) — split from
7
- * `resolve.ts` on the `bare-toponym-race.ts` precedent: the walk file holds the walk, this one
8
- * holds the joint-consistency re-picks that run after it. Each pass's contract, triggers, and
9
- * byte-stability guarantees are documented on the pass itself; `resolveTree` sequences them.
10
5
  */
11
6
  import type { AddressNode } from "@mailwoman/core/decoder";
12
7
  import type { ResolverBackend } from "@mailwoman/core/resolver";
13
8
  /**
14
- * Refuse a pick admitted from outside its parent's scope whose own lineage names a DIFFERENT region than the one the
15
- * walk resolved above it.
16
- *
17
- * The walk scopes a child lookup to its resolved parent. When that scoped probe misses, two widenings exist so an
18
- * incomplete hierarchy (a real place whose ancestor chain lacks its region) still resolves: the resolver's retry
19
- * without the parent, and the candidate backend's interior region-scope fallback (#1731), which re-admits rows from
20
- * outside the region and stamps them `regionScopeMiss`. Both also admit a namesake under another region: `臺南市北區`
21
- * resolved 臺南市 (Tainan City), found no 北區 under it — the candidate table carries no Han key for Tainan's — and the
22
- * widening answered Hsinchu's 北區, 214 km away, which the coherence report then marked `region: contradicted` while the
23
- * point stood. Of the 289 held-out Taiwanese 鄉鎮市區, 21 resolved this way at 15.6–238 km when the register was read as
24
- * bare `縣市鄉鎮市區` lines.
9
+ * Un-resolves a parent-fallback pick whose stamped ancestors name a region other than
10
+ * the resolved region above it, marking it `parent_fallback_refused`.
25
11
  *
26
- * The two cases a widening conflates are told apart by the pick's stamped ancestors: an incomplete chain names NO
27
- * region and is kept, the case the widenings exist for; a chain that names a region other than the resolved parent is a
28
- * different place and is un-resolved here, so the admin ladder answers the parent's own point rather than a namesake
29
- * elsewhere. Only widened picks are examined (`metadata.parent_fallback`, stamped by the walk for either mechanism),
30
- * and only when the parent region resolved to a place identity; no-op without the ancestor sidecar, since a chain that
31
- * cannot be read is not a contradiction. The classifier attribution stays on the node; `parent_fallback_refused`
32
- * records what happened.
12
+ * A pick whose ancestor chain names no region is kept, because the fallback
13
+ * exists for places with incomplete hierarchies.
33
14
  */
34
15
  export declare function applyParentFallbackContradiction(roots: readonly AddressNode[]): void;
35
16
  /**
36
- * Admin descendant-consistency (#263) — the joint-consistency resolve, scoped to the admin assignment. The greedy walk
37
- * resolves a region on its own (name + population), so "ME" picks Messina (IT) over Maine, then scopes "Portland" to
38
- * Messina's descendants, finds nothing, and the result falls back to the region centroid (Sicily). The region's
39
- * same-named runner-ups (Maine, Missouri, …) were already captured as `alternatives`; this pass asks the question the
40
- * greedy order skipped — _which "ME" has a "Portland" under it?_ — and re-picks the (region, locality) pair where a
41
- * same-named locality descends from a same-named region candidate. Geography decides; no country prior, no list.
17
+ * Re-picks a resolved region and its unresolved child locality as a pair, choosing the
18
+ * region candidate that actually contains a locality with the same name.
42
19
  *
43
- * Fires ONLY for a resolved region whose child locality fell through (the unresolved-locality signal), so a
44
- * well-resolved tree ("Springfield, IL" → Illinois, Springfield) is byte-identical. Costs one unscoped locality lookup
45
- * per triggering pair. Needs {@link ResolverBackend.ancestors}; no-op without it. See `ResolveOpts.adminCoherence`.
20
+ * This recovers cases such as "Portland, ME" where the region was picked in isolation
21
+ * before the locality was known.
22
+ * It leaves trees with a resolved locality untouched.
46
23
  */
47
24
  export declare function applyAdminCoherence(roots: readonly AddressNode[], backend: ResolverBackend): Promise<void>;
48
25
  /**
49
- * Explicit-country coherence (#822) — the joint-consistency resolve keyed on the query's own EXPLICIT country token.
50
- * The greedy walk resolves a locality on name + population alone, so "Vienna, Austria" picks the populous US namesake
51
- * (Vienna WV) and IGNORES the "Austria" the address named. This pass asks the question the greedy order skipped —
52
- * _which "Vienna" is in the country the address names?_ — and re-picks the locality to the same-named place under that
53
- * country. The country code comes from the parser's OWN `country` emission via codex's ISO-3166 table (a name→code
54
- * normalization of a token the model already classified, NOT a routing prior or safelist); the gazetteer's `country`
55
- * column does the geographic confirmation. No pin, no list; generalizes to every country.
26
+ * Re-picks a locality to the place with the same name inside the country specified in
27
+ * the address, when that country is the locality's nearest admin context.
56
28
  *
57
- * Disjoint from {@link applyAdminCoherence} by the region guard: that pass owns the case where a REGION scopes the
58
- * locality; this one fires only when the explicit country is the locality's nearest admin context (no region between),
59
- * and then regardless of the locality's resolution state — so it covers both the resolved-but-foreign locality (Sydney
60
- * → the greedy AU pick was wrong) and the unresolved locality the span-rescore tier would otherwise back-fill with the
61
- * US namesake (Vienna → Vienna WV). Byte-stable when the locality already resolved in-country (the id guard) or the
62
- * named country holds no same-named locality (the fail-safe — what also protects "Turkey, TX": no country token ⇒ no
63
- * trigger; and an in-country lookup that finds nothing keeps the greedy result). Costs one country-scoped locality
64
- * lookup per triggering pair. See `ResolveOpts.adminCoherence`.
29
+ * It leaves the locality unchanged when it already resolved to that place or
30
+ * when that country has no locality with the same name.
65
31
  */
66
32
  export declare function applyExplicitCountryCoherence(roots: readonly AddressNode[], backend: ResolverBackend): Promise<void>;
67
33
  /**
68
- * Region-country coherence — the joint-consistency resolve keyed on a REGION token the locale-inferred default-country
69
- * filter could not resolve. Companion to {@link applyExplicitCountryCoherence} (which keys on an explicit COUNTRY token)
70
- * and disjoint from {@link applyAdminCoherence} (which needs a region that DID resolve): this pass owns the mirror case,
71
- * where the region qualifier is a foreign subdivision the default-country hard filter (`spr.country = ?`) discarded.
72
- *
73
- * "Montreal QC" under a US locale: the walk applies `defaultCountry="US"` as a hard candidate filter to every admin
74
- * lookup, so the region "QC" (a Canadian subdivision) resolves to nothing and is dropped — the one signal that would
75
- * redirect the country to CA — and the locality "Montreal" is force-matched to the populous US namesake (Montreal, WI).
76
- * The greedy order threw away the evidence that could correct it.
77
- *
78
- * The fix expands the region token to its country via codex's ISO-3166-2 subdivision table (`matchSubdivision`: "QC" →
79
- * `{ name: "Quebec", country: "CA" }`, handling the FTS index's missing "QC" alt-name code-side), then asks the two
80
- * questions the greedy walk skipped: does that subdivision genuinely resolve UNDER its own country, and is there a
81
- * same-named locality under it? Only when BOTH hold does it swap the region and locality to the in-country pair.
82
- * Geography confirms; the subdivision table is a soft name→country prior, not a routing decision.
34
+ * Re-picks the region and locality into a foreign country when the region token is that
35
+ * country's subdivision and the default-country filter left it unresolved.
83
36
  *
84
- * Evidence-conditional to stay byte-stable on the domestic path. It fires ONLY when (a) a default country is in force,
85
- * (b) the region node is UNRESOLVED (the default-country filter came up empty — a US region resolves fine under `US`,
86
- * so a well-formed US query never trips this), (c) the token is a subdivision of a DIFFERENT country than the default,
87
- * and (d) both the foreign region and a same-named foreign locality resolve. "Springfield, IL" / "Portland, ME": the
88
- * region resolves under `US`, so check (b) fails and the tree is untouched. Costs one region + one locality lookup per
89
- * triggering pair. See `ResolveOpts.adminCoherence`.
37
+ * Both the foreign region and a locality with the same name under that country must resolve,
38
+ * so domestic queries whose region resolves are never changed.
90
39
  */
91
40
  export declare function applyRegionCountryCoherence(roots: readonly AddressNode[], backend: ResolverBackend, defaultCountry: string | undefined): Promise<void>;
92
41
  //# sourceMappingURL=coherence-passes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"coherence-passes.d.ts","sourceRoot":"","sources":["../../lib/admin/coherence-passes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,KAAK,EAAiB,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAoB9E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI,CAuCpF;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBhH;AAoKD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,6BAA6B,CAClD,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,GACtB,OAAO,CAAC,IAAI,CAAC,CA2Bf;AA2CD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,2BAA2B,CAChD,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,EACxB,cAAc,EAAE,MAAM,GAAG,SAAS,GAChC,OAAO,CAAC,IAAI,CAAC,CA8Bf"}
1
+ {"version":3,"file":"coherence-passes.d.ts","sourceRoot":"","sources":["../../lib/admin/coherence-passes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,KAAK,EAAiB,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAY9E;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI,CAuCpF;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBhH;AA4HD;;;;;;GAMG;AACH,wBAAsB,6BAA6B,CAClD,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,GACtB,OAAO,CAAC,IAAI,CAAC,CAkBf;AAiCD;;;;;;GAMG;AACH,wBAAsB,2BAA2B,CAChD,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,EACxB,cAAc,EAAE,MAAM,GAAG,SAAS,GAChC,OAAO,CAAC,IAAI,CAAC,CAuBf"}