@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
package/lib/rerank.ts CHANGED
@@ -3,34 +3,13 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * #727 stage-2 Phase 4a — reranking a k-best parse list on RESOLUTION EVIDENCE.
7
- *
8
- * The arc's thesis, stated by the operator (2026-07-15): the arbiter for a k-best list is the
9
- * resolver, not hand-weights. A rank-2 parse that resolves to a real place beats a rank-1 that
10
- * resolves to a country centroid.
11
- *
12
- * The measured case for this existing at all: on the triaged parity corpus the shipped decode gets
13
- * street 0.573 while the span decode's top-10 CONTAINS the right answer 0.775 of the time
14
- * (oracle@5 0.723). Rank-1 cannot reach that headroom; only a reranker can.
15
- *
16
- * ## Why this is deliberately, almost embarrassingly small
17
- *
18
- * The failure mode we are avoiding is Pelias's: a ladder of per-class hand-tuned weights that
19
- * nobody can re-derive (see `docs/articles/evals/competitive-parity/2026-07-15-pelias-dictionary-overrides.md`
20
- * — 94 of 276 dictionary lines are hand-written deletions, each resolving a collision in favour of
21
- * whoever complained loudest). The moment this file grows a per-hypothesis score blend, we have
22
- * rebuilt that with extra steps.
23
- *
24
- * So the rule is ONE bit of evidence, not a score: **drop hypotheses whose resolution is
25
- * implausible; otherwise keep the model's own ranking.** The parse scores already share a partition
26
- * function and are comparable within an input — the reranker's job is not to re-score them, it is to
27
- * veto the ones the world says are wrong.
28
- *
29
- * Adding a second signal here requires the same bar the first one cleared: a MEASURED win on the
30
- * parity + Paris fixtures, stated in a verdict doc. Not a plausible story.
6
+ * Rerank a k-best parse list on resolution evidence with one bit — drop hypotheses whose resolution
7
+ * is implausible, otherwise keep the model's own ranking — because a second signal here needs a
8
+ * measured win on the parity + Paris fixtures.
31
9
  */
32
10
 
33
11
  import type { AddressTree } from "@mailwoman/core/decoder"
12
+ import type { CountryBBoxFact } from "@mailwoman/core/resolver"
34
13
 
35
14
  import { isImplausibleResolution } from "#plausibility"
36
15
 
@@ -39,15 +18,15 @@ import { isImplausibleResolution } from "#plausibility"
39
18
  */
40
19
  export interface RerankCandidate<T = unknown> {
41
20
  /**
42
- * The parse's own score. Comparable to its siblings from the SAME input; not across inputs.
21
+ * The parse's own score, comparable to its siblings from the same input and not across inputs.
43
22
  */
44
23
  score: number
45
24
  /**
46
- * The parse tree, UNRESOLVED — `rerankByResolution` resolves it via the injected resolver.
25
+ * The parse tree, unresolved — `rerankByResolution` resolves it via the injected resolver.
47
26
  */
48
27
  tree: AddressTree
49
28
  /**
50
- * Opaque caller payload carried through to the result (the segmentation, a surface string, …).
29
+ * Opaque caller payload passed through to the result (the segmentation, a surface string, …).
51
30
  */
52
31
  payload?: T
53
32
  }
@@ -64,50 +43,57 @@ export interface RerankedCandidate<T = unknown> extends RerankCandidate<T> {
64
43
  /**
65
44
  * Why it was vetoed, when it was.
66
45
  */
67
- reason?: string
46
+ reason: string | null
68
47
  }
69
48
 
70
49
  export interface RerankResult<T = unknown> {
71
50
  /**
72
- * Candidates in FINAL order: plausible ones first (model order preserved), vetoed ones after.
51
+ * Candidates in final order: plausible ones first (model order preserved), vetoed ones after.
73
52
  */
74
53
  ranked: Array<RerankedCandidate<T>>
75
54
  /**
76
- * The winner — the first plausible candidate, or the model's rank-1 when ALL were vetoed.
55
+ * The winner — the first plausible candidate, or the model's rank-1 when all were vetoed.
77
56
  */
78
57
  best: RerankedCandidate<T>
79
58
  /**
80
- * True when the winner is NOT the model's rank-1 — i.e. resolution evidence actually changed the answer. This is the
81
- * metric the arc is judged on (`rank-2-beats-rank-1 rate`); log it, because a rerank that never fires is a rerank
82
- * that is not earning its resolver calls.
59
+ * True when resolution evidence actually changed the answer from the model's rank-1.
83
60
  */
84
61
  changed: boolean
85
62
  }
86
63
 
87
64
  /**
88
- * Resolve a tree. Structural — any `Resolver`-shaped thing satisfies it.
65
+ * Resolve a tree.
66
+ * Structural, so any `Resolver`-shaped thing satisfies it.
89
67
  */
90
68
  export type ResolveTree = (tree: AddressTree) => Promise<AddressTree>
91
69
 
92
70
  export interface RerankOpts {
93
71
  /**
94
- * Resolve at most this many candidates (default 5). Each costs a resolver round-trip, so this is the latency knob.
95
- * oracle@5 (0.723) captures nearly all the measured headroom of oracle@10 (0.775), so 5 is the default rather than 10
96
- * — the last 5 candidates cost 2x the resolves for ~5pp of ceiling.
72
+ * Resolve at most this many candidates.
73
+ *
74
+ * Each costs a resolver round-trip, so this is the latency knob.
75
+ *
76
+ * @defaultValue `5`
97
77
  */
98
78
  maxResolve?: number
79
+ /**
80
+ * ISO-2 country the resolutions are expected to land in, threaded to {@link isImplausibleResolution}
81
+ * as guard B so a coordinate outside that country's coarse box is vetoed.
82
+ * Omitted, only the bare-country-centroid guard runs.
83
+ */
84
+ expectedCountry?: string
85
+ /**
86
+ * The loaded artifact's own guard-B boxes, replacing the built-in table wholesale when supplied.
87
+ */
88
+ countryBBoxes?: ReadonlyMap<string, CountryBBoxFact>
99
89
  }
100
90
 
101
91
  /**
102
- * Rerank a k-best parse list on resolution evidence.
103
- *
104
- * Resolves up to `maxResolve` candidates IN MODEL ORDER and returns the first whose resolution is plausible. Candidates
105
- * beyond `maxResolve` are never resolved (and never vetoed — they simply keep their model rank behind the resolved
106
- * ones).
92
+ * Rerank a k-best parse list on resolution evidence: resolve up to `maxResolve`
93
+ * candidates in model order and return the first plausible one.
107
94
  *
108
- * **When every resolved candidate is implausible, the model's rank-1 wins.** A reranker that returns nothing is worse
109
- * than one that defers: "all my evidence says these are all bad" is not grounds to invent a different answer, only
110
- * grounds to flag low confidence (the Phase-4b ambiguity check).
95
+ * Candidates beyond `maxResolve` are never resolved or vetoed. when every resolved
96
+ * candidate is implausible the model's rank-1 wins.
111
97
  */
112
98
  export async function rerankByResolution<T>(
113
99
  candidates: ReadonlyArray<RerankCandidate<T>>,
@@ -122,8 +108,7 @@ export async function rerankByResolution<T>(
122
108
  const candidate = candidates[i]!
123
109
 
124
110
  if (i >= maxResolve) {
125
- // Beyond the resolve budget: carried through unresolved, never vetoed.
126
- ranked.push({ ...candidate, resolved: null, implausible: false })
111
+ ranked.push({ ...candidate, resolved: null, implausible: false, reason: null })
127
112
 
128
113
  continue
129
114
  }
@@ -133,24 +118,26 @@ export async function rerankByResolution<T>(
133
118
  try {
134
119
  resolved = await resolveTree(candidate.tree)
135
120
  } catch {
136
- // A resolver failure is NOT evidence against the parse — treat it as "no evidence" and let
137
- // the model's rank stand, rather than vetoing a possibly-correct hypothesis on an outage.
138
- ranked.push({ ...candidate, resolved: null, implausible: false })
121
+ // A resolver failure is not evidence against the parse, so the model's rank stands
122
+ // rather than vetoing a possibly-correct hypothesis on an outage.
123
+ ranked.push({ ...candidate, resolved: null, implausible: false, reason: null })
139
124
 
140
125
  continue
141
126
  }
142
127
 
143
- const verdict = isImplausibleResolution(resolved)
128
+ const verdict = isImplausibleResolution(resolved, {
129
+ ...(opts.expectedCountry ? { expectedCountry: opts.expectedCountry } : {}),
130
+ ...(opts.countryBBoxes ? { countryBBoxes: opts.countryBBoxes } : {}),
131
+ })
144
132
 
145
133
  ranked.push({
146
134
  ...candidate,
147
135
  resolved,
148
136
  implausible: verdict.implausible,
149
- ...(verdict.reason ? { reason: verdict.reason } : {}),
137
+ reason: verdict.reason,
150
138
  })
151
139
  }
152
140
 
153
- // Stable partition: plausible first (model order preserved within each group), vetoed after.
154
141
  const plausible = ranked.filter((r) => !r.implausible)
155
142
  const vetoed = ranked.filter((r) => r.implausible)
156
143
  const finalOrder = [...plausible, ...vetoed]