@mailwoman/resolver 9.0.0 → 9.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.
- package/README.md +89 -0
- package/admin-coherence-passes.ts +435 -0
- package/admin-containment.ts +101 -0
- package/admin-winner.ts +178 -0
- package/backend-capabilities.ts +88 -0
- package/bare-toponym-race.ts +129 -0
- package/decorate-node.ts +127 -0
- package/fold-name.ts +19 -3
- package/index.ts +14 -0
- package/out/admin-coherence-passes.d.ts +70 -0
- package/out/admin-coherence-passes.d.ts.map +1 -0
- package/out/admin-coherence-passes.js +327 -0
- package/out/admin-coherence-passes.js.map +1 -0
- package/out/admin-containment.d.ts +48 -0
- package/out/admin-containment.d.ts.map +1 -0
- package/out/admin-containment.js +82 -0
- package/out/admin-containment.js.map +1 -0
- package/out/admin-winner.d.ts +109 -0
- package/out/admin-winner.d.ts.map +1 -0
- package/out/admin-winner.js +119 -0
- package/out/admin-winner.js.map +1 -0
- package/out/backend-capabilities.d.ts +37 -0
- package/out/backend-capabilities.d.ts.map +1 -0
- package/out/backend-capabilities.js +75 -0
- package/out/backend-capabilities.js.map +1 -0
- package/out/bare-toponym-race.d.ts +55 -0
- package/out/bare-toponym-race.d.ts.map +1 -0
- package/out/bare-toponym-race.js +110 -0
- package/out/bare-toponym-race.js.map +1 -0
- package/out/decorate-node.d.ts +24 -0
- package/out/decorate-node.d.ts.map +1 -0
- package/out/decorate-node.js +103 -0
- package/out/decorate-node.js.map +1 -0
- package/out/fold-name.d.ts +18 -3
- package/out/fold-name.d.ts.map +1 -1
- package/out/fold-name.js +19 -3
- package/out/fold-name.js.map +1 -1
- package/out/index.d.ts +4 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -1
- package/out/index.js.map +1 -1
- package/out/postcode-country-coherence.d.ts +91 -14
- package/out/postcode-country-coherence.d.ts.map +1 -1
- package/out/postcode-country-coherence.js +279 -42
- package/out/postcode-country-coherence.js.map +1 -1
- package/out/postcode-prefix.d.ts +101 -0
- package/out/postcode-prefix.d.ts.map +1 -0
- package/out/postcode-prefix.js +125 -0
- package/out/postcode-prefix.js.map +1 -0
- package/out/postcode-shape-coherence.d.ts +113 -0
- package/out/postcode-shape-coherence.d.ts.map +1 -0
- package/out/postcode-shape-coherence.js +193 -0
- package/out/postcode-shape-coherence.js.map +1 -0
- package/out/resolve.d.ts +1 -1
- package/out/resolve.d.ts.map +1 -1
- package/out/resolve.js +592 -410
- package/out/resolve.js.map +1 -1
- package/out/span-rescore.d.ts +28 -0
- package/out/span-rescore.d.ts.map +1 -1
- package/out/span-rescore.js +151 -17
- package/out/span-rescore.js.map +1 -1
- package/out/street-evidence.js +1 -1
- package/out/street-evidence.js.map +1 -1
- package/out/street-tier.d.ts.map +1 -1
- package/out/street-tier.js +28 -5
- package/out/street-tier.js.map +1 -1
- package/out/toponym-prior.d.ts +139 -0
- package/out/toponym-prior.d.ts.map +1 -0
- package/out/toponym-prior.js +308 -0
- package/out/toponym-prior.js.map +1 -0
- package/package.json +98 -7
- package/postcode-country-coherence.ts +355 -50
- package/postcode-prefix.ts +166 -0
- package/postcode-shape-coherence.ts +248 -0
- package/resolve.ts +809 -527
- package/span-rescore.ts +206 -19
- package/street-evidence.ts +1 -1
- package/street-tier.ts +31 -5
- package/toponym-prior.ts +367 -0
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/resolver",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "The address resolver: walk an AddressTree, decorate nodes with gazetteer-supplied coordinates + attribution. Backend-agnostic (any ResolverBackend), with the span-rescore + postcode-consistency levers. Lifted out of @mailwoman/core so it can depend on @mailwoman/spatial (haversine) + @mailwoman/codex (USPS directionals) instead of reinventing them; the type contract stays in core so the pipeline composes it without a cycle.",
|
|
5
5
|
"license": "AGPL-3.0-only OR LicenseRef-Commercial",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/sister-software/mailwoman.git",
|
|
9
|
-
"directory": "resolver"
|
|
9
|
+
"directory": "packages/resolver"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"out/**/*.js",
|
|
@@ -20,9 +20,11 @@
|
|
|
20
20
|
"!*.test.ts",
|
|
21
21
|
"!*.test.tsx",
|
|
22
22
|
"!**/*.test.ts",
|
|
23
|
-
"!**/*.test.tsx"
|
|
23
|
+
"!**/*.test.tsx",
|
|
24
|
+
"!test/**"
|
|
24
25
|
],
|
|
25
26
|
"type": "module",
|
|
27
|
+
"imports": {},
|
|
26
28
|
"exports": {
|
|
27
29
|
"./package.json": "./package.json",
|
|
28
30
|
".": {
|
|
@@ -40,6 +42,50 @@
|
|
|
40
42
|
"./span-rescore": {
|
|
41
43
|
"types": "./out/span-rescore.d.ts",
|
|
42
44
|
"default": "./out/span-rescore.js"
|
|
45
|
+
},
|
|
46
|
+
"./backend-capabilities": {
|
|
47
|
+
"types": "./out/backend-capabilities.d.ts",
|
|
48
|
+
"default": "./out/backend-capabilities.js"
|
|
49
|
+
},
|
|
50
|
+
"./admin-containment": {
|
|
51
|
+
"types": "./out/admin-containment.d.ts",
|
|
52
|
+
"default": "./out/admin-containment.js"
|
|
53
|
+
},
|
|
54
|
+
"./admin-winner": {
|
|
55
|
+
"types": "./out/admin-winner.d.ts",
|
|
56
|
+
"default": "./out/admin-winner.js"
|
|
57
|
+
},
|
|
58
|
+
"./decorate-node": {
|
|
59
|
+
"types": "./out/decorate-node.d.ts",
|
|
60
|
+
"default": "./out/decorate-node.js"
|
|
61
|
+
},
|
|
62
|
+
"./plausibility": {
|
|
63
|
+
"types": "./out/plausibility.d.ts",
|
|
64
|
+
"default": "./out/plausibility.js"
|
|
65
|
+
},
|
|
66
|
+
"./postcode-prefix": {
|
|
67
|
+
"types": "./out/postcode-prefix.d.ts",
|
|
68
|
+
"default": "./out/postcode-prefix.js"
|
|
69
|
+
},
|
|
70
|
+
"./postcode-shape-coherence": {
|
|
71
|
+
"types": "./out/postcode-shape-coherence.d.ts",
|
|
72
|
+
"default": "./out/postcode-shape-coherence.js"
|
|
73
|
+
},
|
|
74
|
+
"./remote-resolver": {
|
|
75
|
+
"types": "./out/remote-resolver.d.ts",
|
|
76
|
+
"default": "./out/remote-resolver.js"
|
|
77
|
+
},
|
|
78
|
+
"./rerank": {
|
|
79
|
+
"types": "./out/rerank.d.ts",
|
|
80
|
+
"default": "./out/rerank.js"
|
|
81
|
+
},
|
|
82
|
+
"./street-evidence": {
|
|
83
|
+
"types": "./out/street-evidence.d.ts",
|
|
84
|
+
"default": "./out/street-evidence.js"
|
|
85
|
+
},
|
|
86
|
+
"./toponym-prior": {
|
|
87
|
+
"types": "./out/toponym-prior.d.ts",
|
|
88
|
+
"default": "./out/toponym-prior.js"
|
|
43
89
|
}
|
|
44
90
|
},
|
|
45
91
|
"publishConfig": {
|
|
@@ -61,12 +107,57 @@
|
|
|
61
107
|
"./span-rescore": {
|
|
62
108
|
"types": "./out/span-rescore.d.ts",
|
|
63
109
|
"default": "./out/span-rescore.js"
|
|
110
|
+
},
|
|
111
|
+
"./backend-capabilities": {
|
|
112
|
+
"types": "./out/backend-capabilities.d.ts",
|
|
113
|
+
"default": "./out/backend-capabilities.js"
|
|
114
|
+
},
|
|
115
|
+
"./admin-containment": {
|
|
116
|
+
"types": "./out/admin-containment.d.ts",
|
|
117
|
+
"default": "./out/admin-containment.js"
|
|
118
|
+
},
|
|
119
|
+
"./admin-winner": {
|
|
120
|
+
"types": "./out/admin-winner.d.ts",
|
|
121
|
+
"default": "./out/admin-winner.js"
|
|
122
|
+
},
|
|
123
|
+
"./decorate-node": {
|
|
124
|
+
"types": "./out/decorate-node.d.ts",
|
|
125
|
+
"default": "./out/decorate-node.js"
|
|
126
|
+
},
|
|
127
|
+
"./plausibility": {
|
|
128
|
+
"types": "./out/plausibility.d.ts",
|
|
129
|
+
"default": "./out/plausibility.js"
|
|
130
|
+
},
|
|
131
|
+
"./postcode-prefix": {
|
|
132
|
+
"types": "./out/postcode-prefix.d.ts",
|
|
133
|
+
"default": "./out/postcode-prefix.js"
|
|
134
|
+
},
|
|
135
|
+
"./postcode-shape-coherence": {
|
|
136
|
+
"types": "./out/postcode-shape-coherence.d.ts",
|
|
137
|
+
"default": "./out/postcode-shape-coherence.js"
|
|
138
|
+
},
|
|
139
|
+
"./remote-resolver": {
|
|
140
|
+
"types": "./out/remote-resolver.d.ts",
|
|
141
|
+
"default": "./out/remote-resolver.js"
|
|
142
|
+
},
|
|
143
|
+
"./rerank": {
|
|
144
|
+
"types": "./out/rerank.d.ts",
|
|
145
|
+
"default": "./out/rerank.js"
|
|
146
|
+
},
|
|
147
|
+
"./street-evidence": {
|
|
148
|
+
"types": "./out/street-evidence.d.ts",
|
|
149
|
+
"default": "./out/street-evidence.js"
|
|
150
|
+
},
|
|
151
|
+
"./toponym-prior": {
|
|
152
|
+
"types": "./out/toponym-prior.d.ts",
|
|
153
|
+
"default": "./out/toponym-prior.js"
|
|
64
154
|
}
|
|
65
|
-
}
|
|
155
|
+
},
|
|
156
|
+
"imports": {}
|
|
66
157
|
},
|
|
67
158
|
"dependencies": {
|
|
68
|
-
"@mailwoman/codex": "9.
|
|
69
|
-
"@mailwoman/core": "9.
|
|
70
|
-
"@mailwoman/spatial": "9.
|
|
159
|
+
"@mailwoman/codex": "9.2.0",
|
|
160
|
+
"@mailwoman/core": "9.2.0",
|
|
161
|
+
"@mailwoman/spatial": "9.2.0"
|
|
71
162
|
}
|
|
72
163
|
}
|
|
@@ -53,6 +53,34 @@
|
|
|
53
53
|
* geographic tie) both return `null`. The mechanism only ever speaks when exactly one country
|
|
54
54
|
* makes the pair consistent, and only ever when the default country could not.
|
|
55
55
|
*
|
|
56
|
+
* ## What #24 changed (2026-08-10)
|
|
57
|
+
*
|
|
58
|
+
* Two structural gaps, both found by replaying the 420-row panel-v2 benchmark. The pass is default-ON,
|
|
59
|
+
* the eu-mixed block gave it 29 chances, and it fired **zero** times — while the right answer sat in
|
|
60
|
+
* the shipped gazetteer within 4 km of truth on 21 of them.
|
|
61
|
+
*
|
|
62
|
+
* 1. **The candidate countries came from a proxy.** `candidateSystemsForPostcode` is a shape test over
|
|
63
|
+
* the eight slices codex specifies (`us de fr ca gb jp au nz`), so CZ, CH, BE, DK, AT, NL and IT
|
|
64
|
+
* could never be PROPOSED, however good the evidence. `13000` shapes as `[us, de, fr]`; the
|
|
65
|
+
* gazetteer holds it in exactly one country, and that country is CZ. The candidate set now unions
|
|
66
|
+
* the shape list with the countries the gazetteer actually holds the postcode in — one unscoped
|
|
67
|
+
* lookup, exhaustive at the measured cardinality (the most-shared code in the artifact carries 12
|
|
68
|
+
* rows across 8 countries). Every candidate still has to pass the same geometric pair test, so the
|
|
69
|
+
* widening can only convert an abstention into a verdict or a verdict into an abstention, never a
|
|
70
|
+
* verdict into a different one.
|
|
71
|
+
* 2. **The pair was all-or-nothing.** Ten panel rows had unimpeachable one-sided evidence and no pair:
|
|
72
|
+
* the gazetteer carries no CH or BE postcodes at all (so `Sarnen` and `Kelmis` could never agree
|
|
73
|
+
* with anything), and `Praha 3` / `Praha 9` are municipal districts no admin gazetteer names (so
|
|
74
|
+
* `13000` had nothing to agree WITH). Each half may now speak alone — but only when the DEFAULT
|
|
75
|
+
* country corroborates NEITHER half, and only when the speaking half names exactly one country in
|
|
76
|
+
* the whole gazetteer. See {@link PostcodeCountryScopeEvidence}.
|
|
77
|
+
*
|
|
78
|
+
* The abstention doctrine is unchanged and is what bounds the new rungs: a two-country tie is a HARD
|
|
79
|
+
* abstention that never falls through to the single-sided rungs. Corroboration at home bounds them
|
|
80
|
+
* asymmetrically: the address's own POSTCODE existing in the default country stops the pass dead,
|
|
81
|
+
* while a LOCALITY value at home neutralizes that value as a foreign witness without silencing its
|
|
82
|
+
* siblings (the per-value rule in the single-sided section below).
|
|
83
|
+
*
|
|
56
84
|
* ## Evidence
|
|
57
85
|
*
|
|
58
86
|
* Prototyped out-of-tree against the live gazetteer over the public `findPlace` surface (2026-08-03):
|
|
@@ -64,9 +92,9 @@
|
|
|
64
92
|
* below is the 25 km the scale run measured.
|
|
65
93
|
*
|
|
66
94
|
* Cost: 2 lookups on the byte-stable path (postcode + locality under the default country), and at
|
|
67
|
-
* most `2 × |candidateSystems|` — bounded at
|
|
68
|
-
* — when the default is incoherent. Nothing runs at all
|
|
69
|
-
* the tree carries both a postcode and a locality.
|
|
95
|
+
* most `2 × |candidateSystems| × |localityValues|` — candidates bounded at 12, locality values at 3,
|
|
96
|
+
* and the common tree carries exactly one — when the default is incoherent. Nothing runs at all
|
|
97
|
+
* unless the tree carries both a postcode and a locality.
|
|
70
98
|
*
|
|
71
99
|
* **Default-ON** since the operator promotion of 2026-08-05 (#1477) — the resolver gauntlet, pinned
|
|
72
100
|
* both ways, returned zero newly-failing gated cases, and 56,000 pair evaluations across both
|
|
@@ -89,7 +117,21 @@ import { haversineKm } from "@mailwoman/spatial"
|
|
|
89
117
|
export const POSTCODE_COUNTRY_COHERENCE_GATE_KM = 25
|
|
90
118
|
|
|
91
119
|
/**
|
|
92
|
-
*
|
|
120
|
+
* Which half (or halves) of the address bought the verdict — the firing receipt, so a reader of a scoped result can
|
|
121
|
+
* tell a two-sided agreement from a one-sided uniqueness claim without re-deriving it.
|
|
122
|
+
*
|
|
123
|
+
* - `pair` — postcode AND locality both resolve in this country, within the gate. The strongest rung and the only one
|
|
124
|
+
* that existed before #24.
|
|
125
|
+
* - `locality` — the locality names exactly one country in the whole gazetteer, and the postcode names none that
|
|
126
|
+
* contradict it. This is the CH/BE class: the gazetteer carries no Swiss or Belgian postcodes at all, so the pair
|
|
127
|
+
* test can never fire there no matter how good the locality evidence is.
|
|
128
|
+
* - `postcode` — the postcode is held in exactly one country and the locality is in no gazetteer at all. The `Praha 3`
|
|
129
|
+
* class: a municipal district nobody's admin gazetteer names.
|
|
130
|
+
*/
|
|
131
|
+
export type PostcodeCountryScopeEvidence = "pair" | "locality" | "postcode"
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A country in which the address's own evidence is geographically consistent.
|
|
93
135
|
*/
|
|
94
136
|
export interface PostcodeCountryScope {
|
|
95
137
|
/**
|
|
@@ -104,18 +146,24 @@ export interface PostcodeCountryScope {
|
|
|
104
146
|
* The locality string the verdict was reached on.
|
|
105
147
|
*/
|
|
106
148
|
locality: string
|
|
149
|
+
/**
|
|
150
|
+
* Which evidence carried the verdict. See {@link PostcodeCountryScopeEvidence}.
|
|
151
|
+
*/
|
|
152
|
+
evidence: PostcodeCountryScopeEvidence
|
|
107
153
|
/**
|
|
108
154
|
* Distance between the resolved postcode point and the nearest same-named locality, in km. Always `<= gateKm`.
|
|
155
|
+
* Present ONLY for the `pair` rung — a single-sided verdict has no two points to measure between, and reporting a `0`
|
|
156
|
+
* there would assert an agreement nobody tested (the meaning-of-zero rule).
|
|
109
157
|
*/
|
|
110
|
-
distanceKm
|
|
158
|
+
distanceKm?: number
|
|
111
159
|
/**
|
|
112
|
-
* The postcode place that anchored the verdict.
|
|
160
|
+
* The postcode place that anchored the verdict. Absent on the `locality` rung.
|
|
113
161
|
*/
|
|
114
|
-
postcodePlace
|
|
162
|
+
postcodePlace?: ResolvedPlace
|
|
115
163
|
/**
|
|
116
|
-
* The locality place that anchored the verdict.
|
|
164
|
+
* The locality place that anchored the verdict. Absent on the `postcode` rung.
|
|
117
165
|
*/
|
|
118
|
-
localityPlace
|
|
166
|
+
localityPlace?: ResolvedPlace
|
|
119
167
|
}
|
|
120
168
|
|
|
121
169
|
export interface PostcodeCountryScopeOpts {
|
|
@@ -125,14 +173,27 @@ export interface PostcodeCountryScopeOpts {
|
|
|
125
173
|
*/
|
|
126
174
|
postcode: string
|
|
127
175
|
/**
|
|
128
|
-
* The country the caller's `defaultCountry` would hard-filter to
|
|
129
|
-
*
|
|
176
|
+
* The country the caller's `defaultCountry` would hard-filter to — or `undefined` when no default is in force (the
|
|
177
|
+
* browser cascade), where the pass CONSTRAINS instead of overriding: the default-coherence short-circuit is vacuous
|
|
178
|
+
* and the exactly-one-country abstention carries the safety alone. Historically required: this pass was built to
|
|
179
|
+
* correct a wrong one, so with no default there is nothing to correct and the caller should not be calling.
|
|
130
180
|
*/
|
|
131
|
-
defaultCountry: string
|
|
181
|
+
defaultCountry: string | undefined
|
|
132
182
|
/**
|
|
133
183
|
* Consistency gate radius in km. Defaults to {@link POSTCODE_COUNTRY_COHERENCE_GATE_KM}.
|
|
134
184
|
*/
|
|
135
185
|
gateKm?: number
|
|
186
|
+
/**
|
|
187
|
+
* Optional narrowing of the SHAPE half of the candidate-country set — the shape-coherence pass's intersection for a
|
|
188
|
+
* CONFIRMED postcode span (see `resolver/postcode-shape-coherence.ts`, #31 Mechanism 1). When present it REPLACES
|
|
189
|
+
* `candidateSystemsForPostcode`'s own list; it is a pure subset of that list (codex systems ∩ confident sibling
|
|
190
|
+
* systems). Upper-case ISO-3166 alpha-2, e.g. `["US"]` for a 5-digit code whose siblings all say US.
|
|
191
|
+
*
|
|
192
|
+
* It narrows the shape half only. Since #24 the candidate set also carries the countries the GAZETTEER holds this
|
|
193
|
+
* postcode in, and that half is not narrowable — it is the artifact reporting what it contains, which is the same
|
|
194
|
+
* class of evidence the pair test itself runs on. Every candidate from either half still has to pass that test.
|
|
195
|
+
*/
|
|
196
|
+
candidateSystems?: readonly string[]
|
|
136
197
|
}
|
|
137
198
|
|
|
138
199
|
/**
|
|
@@ -143,46 +204,152 @@ function hasCoord(p: ResolvedPlace): boolean {
|
|
|
143
204
|
}
|
|
144
205
|
|
|
145
206
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
207
|
+
* Upper bound on locality values the pass will consider. A parse rarely tags more than two; the cap keeps a
|
|
208
|
+
* pathological tree from multiplying the pair sweep.
|
|
148
209
|
*/
|
|
149
|
-
|
|
210
|
+
const MAX_LOCALITY_VALUES = 3
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The address's locality strings in DOCUMENT ORDER — every `locality` value first, then every `dependent_locality`,
|
|
214
|
+
* case-insensitively deduplicated. Two passes so a real locality always beats a dependent one regardless of tree
|
|
215
|
+
* order.
|
|
216
|
+
*
|
|
217
|
+
* Document order is load-bearing, not cosmetic. The original stack-pop traversal visited the LAST node first, so on `92
|
|
218
|
+
* Laurell Road, Gander, NL A1V 0A9` — where the model tags both `Gander` and the province abbreviation `NL` as
|
|
219
|
+
* localities — the pass keyed its whole country verdict on "NL", whose only exact locality-band bearer is an alias of
|
|
220
|
+
* Nal, Afghanistan, and the walk resolved a Newfoundland street 10,000 km away. The FIRST-written locality is the one
|
|
221
|
+
* the address is about.
|
|
222
|
+
*
|
|
223
|
+
* ALL values matter, not just the first. On `Calle Mayor 12, Aravaca, 28023 Madrid` the model tags both `Aravaca` and
|
|
224
|
+
* `Madrid` as localities; `Aravaca` is a neighbourhood the locality band cannot see, while `Madrid` carries the ES pair
|
|
225
|
+
* verdict (the ES 28023 row sits 9.6 km from the Madrid locality row). A pass keyed on a single value bets the whole
|
|
226
|
+
* country verdict on whichever one a traversal order happens to pick — first-only re-scoped that address to the US ZIP
|
|
227
|
+
* 28023 centroid in North Carolina. So the verdict rungs try each value in order until one produces evidence.
|
|
228
|
+
*/
|
|
229
|
+
export function localityValuesInDocumentOrder(roots: readonly AddressNode[]): string[] {
|
|
230
|
+
const out: string[] = []
|
|
231
|
+
const seen = new Set<string>()
|
|
232
|
+
|
|
150
233
|
for (const tag of ["locality", "dependent_locality"] as const) {
|
|
151
|
-
|
|
234
|
+
collectInDocumentOrder(roots, tag, out, seen)
|
|
235
|
+
}
|
|
152
236
|
|
|
153
|
-
|
|
154
|
-
|
|
237
|
+
return out.slice(0, MAX_LOCALITY_VALUES)
|
|
238
|
+
}
|
|
155
239
|
|
|
156
|
-
|
|
240
|
+
/**
|
|
241
|
+
* The first of {@link localityValuesInDocumentOrder} — the locality the address is about.
|
|
242
|
+
*/
|
|
243
|
+
export function firstLocalityValue(roots: readonly AddressNode[]): string | undefined {
|
|
244
|
+
return localityValuesInDocumentOrder(roots)[0]
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function collectInDocumentOrder(nodes: readonly AddressNode[], tag: string, out: string[], seen: Set<string>): void {
|
|
248
|
+
for (const n of nodes) {
|
|
249
|
+
if (n.tag === tag && n.value.trim().length) {
|
|
250
|
+
const value = n.value.trim()
|
|
251
|
+
const key = value.toLowerCase()
|
|
157
252
|
|
|
158
|
-
|
|
253
|
+
if (!seen.has(key)) {
|
|
254
|
+
seen.add(key)
|
|
255
|
+
out.push(value)
|
|
256
|
+
}
|
|
159
257
|
}
|
|
258
|
+
|
|
259
|
+
collectInDocumentOrder(n.children, tag, out, seen)
|
|
160
260
|
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Over-fetch for the UNSCOPED holder probes (#24). Postcodes are bounded and small — the 2026-08-10 candidate
|
|
265
|
+
* gazetteer's most-shared code carries 12 rows across 8 countries — so 20 sees every bearer and the postcode holder set
|
|
266
|
+
* is COMPLETE, not a sample. Locality names are not bounded that way (`rampur` has 1,096 rows, `bara` spans 42
|
|
267
|
+
* countries), so 30 is a population-first WINDOW: it can only ever hide a country, which turns a "more than one
|
|
268
|
+
* country" abstention into a false "exactly one". The explicit default-country probe below is what closes that gap for
|
|
269
|
+
* the case that matters (the address is domestic after all).
|
|
270
|
+
*/
|
|
271
|
+
const POSTCODE_HOLDER_FETCH = 20
|
|
272
|
+
const LOCALITY_HOLDER_FETCH = 30
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Upper bound on countries the pair rung will test, so a pathological name can't turn one incoherent parse into a
|
|
276
|
+
* hundred lookups. The postcode holder set is ≤8 countries in the measured artifact and the codex shape list is ≤8, so
|
|
277
|
+
* this only ever binds on a gazetteer very unlike today's.
|
|
278
|
+
*/
|
|
279
|
+
const MAX_CANDIDATE_COUNTRIES = 12
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Countries that hold an exact, coordinate-bearing row for `text` at `placetype`, mapped to their best (first) such
|
|
283
|
+
* row. One UNSCOPED lookup.
|
|
284
|
+
*
|
|
285
|
+
* This is the #24 candidate source, and it replaces a proxy with the thing itself. The pass used to ask codex "which
|
|
286
|
+
* address SYSTEMS could this shape be?" — a model-free shape test over the eight slices codex specifies. That answer is
|
|
287
|
+
* correct and useless for two thirds of Europe: `13000` shapes as `[us, de, fr]`, the gazetteer holds it in exactly one
|
|
288
|
+
* country (CZ), and CZ has no codex slice, so the pass could not propose the only country that could possibly be right.
|
|
289
|
+
* Membership in the gazetteer is positive evidence of the same kind the pair test already runs on, available for one
|
|
290
|
+
* lookup, and it is EXHAUSTIVE for postcodes at the measured cardinality.
|
|
291
|
+
*
|
|
292
|
+
* `exactMatch !== false` is the admission bar: a fuzzy postcode hit is a DIFFERENT postcode (the 2026-08-05 Code-Point
|
|
293
|
+
* lesson — `BT3 9QQ` trigram-matching Sheffield's `S3 9QQ`), and a fuzzy locality hit is evidence about the index, not
|
|
294
|
+
* about the country. Backends that do not stamp the flag leave it undefined and still contribute.
|
|
295
|
+
*/
|
|
296
|
+
async function countriesHolding(
|
|
297
|
+
backend: ResolverBackend,
|
|
298
|
+
text: string,
|
|
299
|
+
placetype: "postalcode" | "locality",
|
|
300
|
+
limit: number,
|
|
301
|
+
opts?: { primaryOnly?: boolean }
|
|
302
|
+
): Promise<Map<string, ResolvedPlace>> {
|
|
303
|
+
const out = new Map<string, ResolvedPlace>()
|
|
304
|
+
|
|
305
|
+
let hits: ResolvedPlace[]
|
|
161
306
|
|
|
162
|
-
|
|
307
|
+
try {
|
|
308
|
+
hits = await backend.findPlace({ text, placetype, limit, ...(opts?.primaryOnly ? { primaryOnly: true } : {}) })
|
|
309
|
+
} catch {
|
|
310
|
+
return out // a backend hiccup degrades to "no evidence", never to a crashed resolve
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
for (const hit of hits) {
|
|
314
|
+
const country = hit.country?.trim().toUpperCase()
|
|
315
|
+
|
|
316
|
+
if (!country || hit.exactMatch === false || !hasCoord(hit)) continue
|
|
317
|
+
|
|
318
|
+
if (!out.has(country)) {
|
|
319
|
+
out.set(country, hit)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return out
|
|
163
324
|
}
|
|
164
325
|
|
|
165
326
|
/**
|
|
166
327
|
* Is the (postcode, locality) pair geographically consistent in `country`? Returns the winning pair and its distance,
|
|
167
328
|
* or `null` when the postcode does not resolve there, no same-named locality exists there, or the nearest one is
|
|
168
|
-
* outside the gate. Costs one postcode lookup plus (only if that hit) one locality lookup
|
|
329
|
+
* outside the gate. Costs one postcode lookup plus (only if that hit) one locality lookup — or just the locality lookup
|
|
330
|
+
* when the caller already knows the country's postcode row (`knownPostcodePlace`, from the exhaustive unscoped probe).
|
|
169
331
|
*/
|
|
170
332
|
async function coherenceIn(
|
|
171
333
|
country: string,
|
|
172
334
|
postcode: string,
|
|
173
335
|
locality: string,
|
|
174
336
|
backend: ResolverBackend,
|
|
175
|
-
gateKm: number
|
|
337
|
+
gateKm: number,
|
|
338
|
+
knownPostcodePlace?: ResolvedPlace
|
|
176
339
|
): Promise<{ postcodePlace: ResolvedPlace; localityPlace: ResolvedPlace; distanceKm: number } | null> {
|
|
177
|
-
let
|
|
340
|
+
let postcodePlace = knownPostcodePlace
|
|
178
341
|
|
|
179
|
-
|
|
180
|
-
postcodeHits
|
|
181
|
-
} catch {
|
|
182
|
-
return null // a backend hiccup degrades to "no evidence here", never to a crashed resolve
|
|
183
|
-
}
|
|
342
|
+
if (!postcodePlace) {
|
|
343
|
+
let postcodeHits: ResolvedPlace[]
|
|
184
344
|
|
|
185
|
-
|
|
345
|
+
try {
|
|
346
|
+
postcodeHits = await backend.findPlace({ text: postcode, placetype: "postalcode", country, limit: 3 })
|
|
347
|
+
} catch {
|
|
348
|
+
return null // a backend hiccup degrades to "no evidence here", never to a crashed resolve
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
postcodePlace = postcodeHits.find(hasCoord)
|
|
352
|
+
}
|
|
186
353
|
|
|
187
354
|
if (!postcodePlace) return null
|
|
188
355
|
|
|
@@ -233,43 +400,176 @@ export async function findPostcodeCountryScope(
|
|
|
233
400
|
opts: PostcodeCountryScopeOpts
|
|
234
401
|
): Promise<PostcodeCountryScope | null> {
|
|
235
402
|
const postcode = opts.postcode.trim()
|
|
236
|
-
const defaultCountry = opts.defaultCountry
|
|
403
|
+
const defaultCountry = opts.defaultCountry?.trim().toUpperCase() || undefined
|
|
237
404
|
|
|
238
|
-
if (!postcode
|
|
405
|
+
if (!postcode) return null
|
|
239
406
|
|
|
240
|
-
const
|
|
407
|
+
const localities = localityValuesInDocumentOrder(roots)
|
|
241
408
|
|
|
242
409
|
// Both halves of the pair are required. A postcode with no locality has nothing to be coherent WITH, which is what
|
|
243
410
|
// keeps this pass inert on "Springfield, IL 62701" (the parser tags Springfield as a `street` — a separate defect)
|
|
244
411
|
// and on every bare-postcode query.
|
|
245
|
-
if (!
|
|
412
|
+
if (!localities.length) return null
|
|
246
413
|
|
|
247
414
|
const gateKm = opts.gateKm ?? POSTCODE_COUNTRY_COHERENCE_GATE_KM
|
|
248
415
|
|
|
249
|
-
// 1. Is the caller's own default country coherent? If so we are done —
|
|
250
|
-
// no override, and the common domestic path
|
|
251
|
-
|
|
416
|
+
// 1. Is the caller's own default country coherent with ANY of the address's locality values? If so we are done —
|
|
417
|
+
// positive evidence for the default, no override, and the common domestic path (one locality value) costs two
|
|
418
|
+
// lookups and changes nothing. With no default in force (the browser cascade) there is nothing to test — the
|
|
419
|
+
// sweep below carries the whole verdict.
|
|
420
|
+
if (defaultCountry) {
|
|
421
|
+
for (const locality of localities) {
|
|
422
|
+
if (await coherenceIn(defaultCountry, postcode, locality, backend, gateKm)) return null
|
|
423
|
+
}
|
|
424
|
+
}
|
|
252
425
|
|
|
253
|
-
// 2. The default could not place this pair. Which
|
|
254
|
-
|
|
255
|
-
|
|
426
|
+
// 2. The default could not place this pair. Which countries could? Two sources, unioned:
|
|
427
|
+
//
|
|
428
|
+
// - the SHAPE list (`candidateSystemsForPostcode`, or the shape-coherence pass's narrowing when #31
|
|
429
|
+
// Mechanism 1 supplied one — a pure subset of it), and
|
|
430
|
+
// - the countries the GAZETTEER actually holds this postcode in (#24). Exhaustive at the measured
|
|
431
|
+
// cardinality, and the only source that can name a country codex has no slice for — which was the
|
|
432
|
+
// whole reason the CZ/CH/BE/DK/AT/NL block of the 2026-08-09 panel resolved to US namesakes while
|
|
433
|
+
// their own postcodes sat in the shipped gazetteer within 4 km of truth.
|
|
434
|
+
//
|
|
435
|
+
// The union can only ADD countries, and every added one must still pass the same geometric pair test,
|
|
436
|
+
// so this widens recall without weakening the verdict: a second coherent country turns a verdict into
|
|
437
|
+
// an abstention (safe), never into a wrong answer.
|
|
438
|
+
const pcHolders = await countriesHolding(backend, postcode, "postalcode", POSTCODE_HOLDER_FETCH)
|
|
439
|
+
|
|
440
|
+
const shapeSystems = (
|
|
441
|
+
opts.candidateSystems ?? candidateSystemsForPostcode(postcode).map((system) => system.toUpperCase())
|
|
442
|
+
).map((country) => country.trim().toUpperCase())
|
|
443
|
+
|
|
444
|
+
const candidates = [...new Set([...shapeSystems, ...pcHolders.keys()])]
|
|
256
445
|
.filter((country) => country !== defaultCountry)
|
|
446
|
+
.slice(0, MAX_CANDIDATE_COUNTRIES)
|
|
447
|
+
|
|
448
|
+
// 3. The pair sweep, per locality value in document order: the first value that produces evidence decides. A value
|
|
449
|
+
// the band cannot see (`Aravaca`, a neighbourhood) yields zero coherent countries and the NEXT value gets its
|
|
450
|
+
// turn — betting the verdict on a single value re-scoped that address to a US ZIP centroid (see
|
|
451
|
+
// localityValuesInDocumentOrder). Exactly one coherent country is a verdict. Two coherent countries mean the
|
|
452
|
+
// geometry genuinely does not decide, and guessing between them is precisely what this mechanism exists not to
|
|
453
|
+
// do — a TIE is a hard abstention, never a fall-through to a later value or the weaker rungs below.
|
|
454
|
+
for (const locality of localities) {
|
|
455
|
+
const coherent: PostcodeCountryScope[] = []
|
|
456
|
+
|
|
457
|
+
for (const country of candidates) {
|
|
458
|
+
const hit = await coherenceIn(country, postcode, locality, backend, gateKm, pcHolders.get(country))
|
|
459
|
+
|
|
460
|
+
if (hit) {
|
|
461
|
+
coherent.push({ country, postcode, locality, evidence: "pair", ...hit })
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (coherent.length === 1) return coherent[0]!
|
|
466
|
+
|
|
467
|
+
if (coherent.length > 1) return null
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// The single-sided rungs below lean on the DEFAULT country as the domestic-plausibility guard
|
|
471
|
+
// ("Vienna, VA" stays in Virginia because the default corroborates a half). With no default there
|
|
472
|
+
// is no such anchor, so the no-default path ends at the pair rung: a pair verdict (Zabiče) is
|
|
473
|
+
// in-scope, a single-sided guess is not.
|
|
474
|
+
if (!defaultCountry) return null
|
|
475
|
+
|
|
476
|
+
// 4. No country makes the PAIR consistent. On the 2026-08-09 panel that happened 10 times for two opposite
|
|
477
|
+
// reasons, and in both the address's country was never in doubt — only unrepresentable as a pair:
|
|
478
|
+
//
|
|
479
|
+
// - the gazetteer holds no postcodes for the country at all (CH, BE), so the postcode half can never
|
|
480
|
+
// agree with anything no matter how good the locality evidence is; or
|
|
481
|
+
// - the locality is a name no admin gazetteer carries (`Praha 3`, `Praha 9` — municipal districts).
|
|
482
|
+
//
|
|
483
|
+
// So each half gets to speak alone, under conditions that keep this from becoming a guess. The
|
|
484
|
+
// POSTCODE half is all-or-nothing: the default country holding this postcode makes the address
|
|
485
|
+
// domestically plausible outright, and nothing foreign may be proposed. The LOCALITY half is
|
|
486
|
+
// judged PER VALUE — a value the default country holds is domestic corroboration for THAT value
|
|
487
|
+
// (it is neutralized as a foreign witness) without silencing its siblings. `14 Long St, Green
|
|
488
|
+
// Point, Cape Town, 8001` is why: "Green Point" has pop-0 US namesakes, and letting that one
|
|
489
|
+
// value abstain the whole pass handed a uniquely-ZA "Cape Town" to a hard US filter (the walk
|
|
490
|
+
// answered Green Point, Pennsylvania). `123 Main St, Vienna, VA 22180` still holds in Virginia:
|
|
491
|
+
// its ONLY locality value is domestic, so no foreign witness remains. The speaking value must
|
|
492
|
+
// name EXACTLY ONE country in the entire gazetteer — the same uniqueness bar the pair rung uses,
|
|
493
|
+
// applied to one side — and all speaking values must AGREE on that country.
|
|
494
|
+
if (pcHolders.has(defaultCountry)) return null
|
|
495
|
+
|
|
496
|
+
// 4a. A locality value names exactly one country. Stronger than the postcode rung — a place name is far
|
|
497
|
+
// more discriminative than a four-digit code — so it is tried first. A verdict stands down when that
|
|
498
|
+
// country's OWN postcode row contradicts the locality (outside the gate): the two halves disagreeing
|
|
499
|
+
// inside one country is exactly what the pair test exists to catch, and a single-sided rung must not
|
|
500
|
+
// launder it.
|
|
501
|
+
// PRIMARY-keyed rows only (#1626's re-reading discipline, applied to a country verdict): "the locality
|
|
502
|
+
// names exactly one country" must mean a place NAMED that, never a place aliased to it — "NL"'s only
|
|
503
|
+
// locality-band bearer was an exact ALIAS of Nal, Afghanistan, and a single alias row steered the whole
|
|
504
|
+
// walk's country. The pair rung keeps aliases: there the postcode's own geometry corroborates them.
|
|
505
|
+
let anyDomestic = false
|
|
506
|
+
let anyLocalityKnown = false
|
|
507
|
+
const verdicts = new Map<string, PostcodeCountryScope>()
|
|
508
|
+
|
|
509
|
+
for (const locality of localities) {
|
|
510
|
+
if (await holdsLocality(backend, locality, defaultCountry)) {
|
|
511
|
+
anyDomestic = true
|
|
512
|
+
|
|
513
|
+
continue
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const locHolders = await countriesHolding(backend, locality, "locality", LOCALITY_HOLDER_FETCH, {
|
|
517
|
+
primaryOnly: true,
|
|
518
|
+
})
|
|
519
|
+
|
|
520
|
+
if (locHolders.size) {
|
|
521
|
+
anyLocalityKnown = true
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (locHolders.size !== 1) continue
|
|
525
|
+
|
|
526
|
+
const [country, localityPlace] = [...locHolders.entries()][0]!
|
|
527
|
+
const ownPostcode = pcHolders.get(country)
|
|
528
|
+
|
|
529
|
+
const contradicted =
|
|
530
|
+
ownPostcode !== undefined &&
|
|
531
|
+
haversineKm(ownPostcode.lat, ownPostcode.lon, localityPlace.lat, localityPlace.lon) > gateKm
|
|
532
|
+
|
|
533
|
+
if (country !== defaultCountry && !contradicted && !verdicts.has(country)) {
|
|
534
|
+
verdicts.set(country, { country, postcode, locality, evidence: "locality", localityPlace })
|
|
535
|
+
}
|
|
536
|
+
}
|
|
257
537
|
|
|
258
|
-
|
|
538
|
+
// Every speaking value must agree: two values uniquely naming DIFFERENT countries is a genuine
|
|
539
|
+
// ambiguity, and guessing between them is what this mechanism exists not to do.
|
|
540
|
+
if (verdicts.size === 1) return [...verdicts.values()][0]!
|
|
259
541
|
|
|
260
|
-
|
|
542
|
+
if (verdicts.size > 1) return null
|
|
261
543
|
|
|
262
|
-
|
|
263
|
-
|
|
544
|
+
// 4b. The postcode is held in exactly one country and the locality half is silent EVERYWHERE — no value
|
|
545
|
+
// is domestic and no value names any country. `Biskupcova 1843/3, 13000 Praha 3`: `13000` is a CZ
|
|
546
|
+
// code and nothing else, anywhere, and `Praha 3` (a municipal district) is in no admin gazetteer.
|
|
547
|
+
if (!anyDomestic && !anyLocalityKnown && pcHolders.size === 1) {
|
|
548
|
+
const [country, postcodePlace] = [...pcHolders.entries()][0]!
|
|
264
549
|
|
|
265
|
-
if (
|
|
266
|
-
|
|
550
|
+
if (country !== defaultCountry) {
|
|
551
|
+
return { country, postcode, locality: localities[0]!, evidence: "postcode", postcodePlace }
|
|
267
552
|
}
|
|
268
553
|
}
|
|
269
554
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
555
|
+
return null
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Does `country` hold an exact, coordinate-bearing locality row for `locality`? One scoped lookup — deliberately NOT
|
|
560
|
+
* read off the unscoped holder window, which is population-first and truncated, so a small domestic bearer could fall
|
|
561
|
+
* below its fold and let a foreign scope through.
|
|
562
|
+
*/
|
|
563
|
+
async function holdsLocality(backend: ResolverBackend, locality: string, country: string): Promise<boolean> {
|
|
564
|
+
try {
|
|
565
|
+
const hits = await backend.findPlace({ text: locality, placetype: "locality", country, limit: 3 })
|
|
566
|
+
|
|
567
|
+
return hits.some((hit) => hit.exactMatch && hasCoord(hit))
|
|
568
|
+
} catch {
|
|
569
|
+
// A backend hiccup must not be read as "the default country has nothing" — that would license a foreign
|
|
570
|
+
// scope on no evidence. Treat it as corroboration and abstain.
|
|
571
|
+
return true
|
|
572
|
+
}
|
|
273
573
|
}
|
|
274
574
|
|
|
275
575
|
/**
|
|
@@ -290,7 +590,12 @@ export function stampPostcodeCountryScope(roots: readonly AddressNode[], scope:
|
|
|
290
590
|
n.metadata = {
|
|
291
591
|
...n.metadata,
|
|
292
592
|
postcode_country_scope: scope.country,
|
|
293
|
-
|
|
593
|
+
// WHICH rung spoke — a two-sided agreement and a one-sided uniqueness claim are different evidence,
|
|
594
|
+
// and a consumer that cannot tell them apart cannot weight them differently.
|
|
595
|
+
postcode_country_scope_evidence: scope.evidence,
|
|
596
|
+
// Only the `pair` rung measured a distance. Writing a 0 for the single-sided rungs would assert an
|
|
597
|
+
// agreement nobody tested.
|
|
598
|
+
...(scope.distanceKm !== undefined ? { postcode_country_scope_km: scope.distanceKm } : {}),
|
|
294
599
|
}
|
|
295
600
|
}
|
|
296
601
|
}
|