@mailwoman/resolver 9.4.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 +21 -161
  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 +73 -39
  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 +37 -63
  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 +145 -148
  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 +18 -69
  24. package/out/admin/coherence-passes.d.ts.map +1 -1
  25. package/out/admin/coherence-passes.js +19 -161
  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 +56 -28
  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 +29 -58
  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 +97 -86
  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 -995
  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 -807
  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
  }
@@ -69,45 +48,49 @@ export interface RerankedCandidate<T = unknown> extends RerankCandidate<T> {
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 (default 5); each costs a resolver round-trip,
73
+ * so this is the latency knob.
97
74
  */
98
75
  maxResolve?: number
76
+ /**
77
+ * ISO-2 country the resolutions are expected to land in, threaded to {@link isImplausibleResolution}
78
+ * as guard B so a coordinate outside that country's coarse box is vetoed.
79
+ * Omitted, only the bare-country-centroid guard runs.
80
+ */
81
+ expectedCountry?: string
82
+ /**
83
+ * The loaded artifact's own guard-B boxes, replacing the built-in table wholesale when supplied.
84
+ */
85
+ countryBBoxes?: ReadonlyMap<string, CountryBBoxFact>
99
86
  }
100
87
 
101
88
  /**
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).
89
+ * Rerank a k-best parse list on resolution evidence: resolve up to `maxResolve`
90
+ * candidates in model order and return the first plausible one.
107
91
  *
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).
92
+ * Candidates beyond `maxResolve` are never resolved or vetoed. when every resolved
93
+ * candidate is implausible the model's rank-1 wins.
111
94
  */
112
95
  export async function rerankByResolution<T>(
113
96
  candidates: ReadonlyArray<RerankCandidate<T>>,
@@ -122,7 +105,6 @@ export async function rerankByResolution<T>(
122
105
  const candidate = candidates[i]!
123
106
 
124
107
  if (i >= maxResolve) {
125
- // Beyond the resolve budget: carried through unresolved, never vetoed.
126
108
  ranked.push({ ...candidate, resolved: null, implausible: false })
127
109
 
128
110
  continue
@@ -133,14 +115,17 @@ export async function rerankByResolution<T>(
133
115
  try {
134
116
  resolved = await resolveTree(candidate.tree)
135
117
  } 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.
118
+ // A resolver failure is not evidence against the parse, so the model's rank stands
119
+ // rather than vetoing a possibly-correct hypothesis on an outage.
138
120
  ranked.push({ ...candidate, resolved: null, implausible: false })
139
121
 
140
122
  continue
141
123
  }
142
124
 
143
- const verdict = isImplausibleResolution(resolved)
125
+ const verdict = isImplausibleResolution(resolved, {
126
+ ...(opts.expectedCountry ? { expectedCountry: opts.expectedCountry } : {}),
127
+ ...(opts.countryBBoxes ? { countryBBoxes: opts.countryBBoxes } : {}),
128
+ })
144
129
 
145
130
  ranked.push({
146
131
  ...candidate,
@@ -150,7 +135,6 @@ export async function rerankByResolution<T>(
150
135
  })
151
136
  }
152
137
 
153
- // Stable partition: plausible first (model order preserved within each group), vetoed after.
154
138
  const plausible = ranked.filter((r) => !r.implausible)
155
139
  const vetoed = ranked.filter((r) => r.implausible)
156
140
  const finalOrder = [...plausible, ...vetoed]