@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/out/fold-name.js
CHANGED
|
@@ -3,16 +3,32 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* One shared name fold, used by the resolver's commune reconciliation
|
|
7
|
-
* locality comparison. Its own module because
|
|
6
|
+
* One shared name fold, used by the resolver's commune reconciliation, by the street tier's
|
|
7
|
+
* locality comparison and by span-rescore's key guards. Its own module because those would
|
|
8
|
+
* otherwise import each other for it.
|
|
9
|
+
*
|
|
10
|
+
* `@mailwoman/codex`'s `foldName` (`packages/codex/normalize.ts`) is a near twin and is also
|
|
11
|
+
* correct; it differs in collapsing every non-alphanumeric RUN, whitespace included. The two are
|
|
12
|
+
* equivalent on the inputs either sees today. They are kept apart because this one answers to a
|
|
13
|
+
* comparison contract the resolver owns, not because the codex copy is wrong.
|
|
8
14
|
*/
|
|
9
15
|
/**
|
|
10
|
-
* Case/diacritic-insensitive fold for commune-name comparison (#1058)
|
|
16
|
+
* Case/diacritic-insensitive fold for commune-name comparison (#1058).
|
|
17
|
+
*
|
|
18
|
+
* Marks are DELETED, never spaced. `[^a-z0-9 ]` alone maps the combining cedilla that NFD has just produced to a SPACE,
|
|
19
|
+
* so `Besançon` keys as `besanc on` and matches nothing it should. The gazetteer side of the same comparison deletes
|
|
20
|
+
* them — 0 of 32,539 distinct `locality_base` keys in `street-centroids-fr.db` carry a mark — so spacing puts the two
|
|
21
|
+
* halves of one comparison on different keys. Order is load-bearing: after the class filter the strip is a no-op.
|
|
22
|
+
*
|
|
23
|
+
* Not fixed here, and worth knowing before trusting the fold on a non-Latin-1 name: a letter with no decomposition is
|
|
24
|
+
* DROPPED rather than folded (`Łódź` → `odz`, `Đà Nẵng` → `a nang`). That needs a transliteration table, not a regex,
|
|
25
|
+
* and it is shared with every other fold in the repo.
|
|
11
26
|
*/
|
|
12
27
|
export function foldName(s) {
|
|
13
28
|
return s
|
|
14
29
|
.toLowerCase()
|
|
15
30
|
.normalize("NFD")
|
|
31
|
+
.replaceAll(/\p{M}/gu, "")
|
|
16
32
|
.replaceAll(/[^a-z0-9 ]/g, " ")
|
|
17
33
|
.replaceAll(/\s+/g, " ")
|
|
18
34
|
.trim();
|
package/out/fold-name.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fold-name.js","sourceRoot":"","sources":["../fold-name.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"fold-name.js","sourceRoot":"","sources":["../fold-name.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAS;IACjC,OAAO,CAAC;SACN,WAAW,EAAE;SACb,SAAS,CAAC,KAAK,CAAC;SAChB,UAAU,CAAC,SAAS,EAAE,EAAE,CAAC;SACzB,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC;SAC9B,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC;SACvB,IAAI,EAAE,CAAA;AACT,CAAC"}
|
package/out/index.d.ts
CHANGED
|
@@ -17,10 +17,13 @@ export { COUNTRY_BBOX, finestResolvedCoordinate, isImplausibleResolution, outsid
|
|
|
17
17
|
export type { PlausibilityOpts, PlausibilityVerdict, ResolvedCoordinate } from "./plausibility.ts";
|
|
18
18
|
export { foldStreetSurface, isPureTypeVocabulary, pickByStreetEvidence } from "./street-evidence.ts";
|
|
19
19
|
export type { PickByStreetEvidenceOpts, StreetCandidate, StreetEvidencePick, StreetEvidenceScope, StreetLocalityEvidence, } from "./street-evidence.ts";
|
|
20
|
-
export { findPostcodeCountryScope, firstLocalityValue, POSTCODE_COUNTRY_COHERENCE_GATE_KM, stampPostcodeCountryScope, } from "./postcode-country-coherence.ts";
|
|
20
|
+
export { findPostcodeCountryScope, firstLocalityValue, localityValuesInDocumentOrder, POSTCODE_COUNTRY_COHERENCE_GATE_KM, stampPostcodeCountryScope, } from "./postcode-country-coherence.ts";
|
|
21
21
|
export type { PostcodeCountryScope, PostcodeCountryScopeOpts } from "./postcode-country-coherence.ts";
|
|
22
22
|
export { findRescoreCandidate, hasResolvedPlace } from "./span-rescore.ts";
|
|
23
23
|
export type { RescoreCandidate, SpanRescoreOptions } from "./span-rescore.ts";
|
|
24
|
+
export { adminContainmentVerdict, partitionByContainment } from "./admin-containment.ts";
|
|
25
|
+
export { ADMIN_LADDER_LOCALITY_FIRST, ADMIN_LADDER_POSTCODE_FIRST, adminLadderFor, adminLadderForNodes, AREA_GRADE_POSTALCODE_SPECIFICITY, mostSpecificResolved, resolvedSpecificity, } from "./admin-winner.ts";
|
|
26
|
+
export type { ResolvedPostcodeHit, ResolvedSpecificityInput } from "./admin-winner.ts";
|
|
24
27
|
export * from "@mailwoman/core/resolver";
|
|
25
28
|
export * from "./rerank.ts";
|
|
26
29
|
//# sourceMappingURL=index.d.ts.map
|
package/out/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAE9E,YAAY,EACX,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,GACvB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,EACN,YAAY,EACZ,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,mBAAmB,CAAA;AAE1B,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAEpG,YAAY,EACX,wBAAwB,EACxB,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,GACtB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EACN,wBAAwB,EACxB,kBAAkB,EAClB,kCAAkC,EAClC,yBAAyB,GACzB,MAAM,iCAAiC,CAAA;AAExC,YAAY,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAA;AACrG,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAE9E,YAAY,EACX,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,GACvB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,EACN,YAAY,EACZ,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,mBAAmB,CAAA;AAE1B,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAEpG,YAAY,EACX,wBAAwB,EACxB,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,GACtB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EACN,wBAAwB,EACxB,kBAAkB,EAClB,6BAA6B,EAC7B,kCAAkC,EAClC,yBAAyB,GACzB,MAAM,iCAAiC,CAAA;AAExC,YAAY,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAA;AACrG,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC7E,OAAO,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAA;AAExF,OAAO,EACN,2BAA2B,EAC3B,2BAA2B,EAC3B,cAAc,EACd,mBAAmB,EACnB,iCAAiC,EACjC,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,mBAAmB,CAAA;AAE1B,YAAY,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAItF,cAAc,0BAA0B,CAAA;AAExC,cAAc,aAAa,CAAA"}
|
package/out/index.js
CHANGED
|
@@ -14,8 +14,10 @@ export { RemoteResolver, serializableResolveOpts } from "./remote-resolver.js";
|
|
|
14
14
|
export { createWOFResolver } from "./resolve.js";
|
|
15
15
|
export { COUNTRY_BBOX, finestResolvedCoordinate, isImplausibleResolution, outsideExpectedCountry, } from "./plausibility.js";
|
|
16
16
|
export { foldStreetSurface, isPureTypeVocabulary, pickByStreetEvidence } from "./street-evidence.js";
|
|
17
|
-
export { findPostcodeCountryScope, firstLocalityValue, POSTCODE_COUNTRY_COHERENCE_GATE_KM, stampPostcodeCountryScope, } from "./postcode-country-coherence.js";
|
|
17
|
+
export { findPostcodeCountryScope, firstLocalityValue, localityValuesInDocumentOrder, POSTCODE_COUNTRY_COHERENCE_GATE_KM, stampPostcodeCountryScope, } from "./postcode-country-coherence.js";
|
|
18
18
|
export { findRescoreCandidate, hasResolvedPlace } from "./span-rescore.js";
|
|
19
|
+
export { adminContainmentVerdict, partitionByContainment } from "./admin-containment.js";
|
|
20
|
+
export { ADMIN_LADDER_LOCALITY_FIRST, ADMIN_LADDER_POSTCODE_FIRST, adminLadderFor, adminLadderForNodes, AREA_GRADE_POSTALCODE_SPECIFICITY, mostSpecificResolved, resolvedSpecificity, } from "./admin-winner.js";
|
|
19
21
|
// The type contract + placetype helpers live in core (pure types, keep core a leaf). Re-export so
|
|
20
22
|
// consumers get the whole surface from `@mailwoman/resolver`.
|
|
21
23
|
export * from "@mailwoman/core/resolver";
|
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAS9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,EACN,YAAY,EACZ,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAUpG,OAAO,EACN,wBAAwB,EACxB,kBAAkB,EAClB,kCAAkC,EAClC,yBAAyB,GACzB,MAAM,iCAAiC,CAAA;AAGxC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAS9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEhD,OAAO,EACN,YAAY,EACZ,wBAAwB,EACxB,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAUpG,OAAO,EACN,wBAAwB,EACxB,kBAAkB,EAClB,6BAA6B,EAC7B,kCAAkC,EAClC,yBAAyB,GACzB,MAAM,iCAAiC,CAAA;AAGxC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAE1E,OAAO,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAA;AAExF,OAAO,EACN,2BAA2B,EAC3B,2BAA2B,EAC3B,cAAc,EACd,mBAAmB,EACnB,iCAAiC,EACjC,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,mBAAmB,CAAA;AAI1B,kGAAkG;AAClG,8DAA8D;AAC9D,cAAc,0BAA0B,CAAA;AAExC,cAAc,aAAa,CAAA"}
|
|
@@ -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
|
|
@@ -82,7 +110,20 @@ import type { ResolvedPlace, ResolverBackend } from "@mailwoman/core/resolver";
|
|
|
82
110
|
*/
|
|
83
111
|
export declare const POSTCODE_COUNTRY_COHERENCE_GATE_KM = 25;
|
|
84
112
|
/**
|
|
85
|
-
*
|
|
113
|
+
* Which half (or halves) of the address bought the verdict — the firing receipt, so a reader of a scoped result can
|
|
114
|
+
* tell a two-sided agreement from a one-sided uniqueness claim without re-deriving it.
|
|
115
|
+
*
|
|
116
|
+
* - `pair` — postcode AND locality both resolve in this country, within the gate. The strongest rung and the only one
|
|
117
|
+
* that existed before #24.
|
|
118
|
+
* - `locality` — the locality names exactly one country in the whole gazetteer, and the postcode names none that
|
|
119
|
+
* contradict it. This is the CH/BE class: the gazetteer carries no Swiss or Belgian postcodes at all, so the pair
|
|
120
|
+
* test can never fire there no matter how good the locality evidence is.
|
|
121
|
+
* - `postcode` — the postcode is held in exactly one country and the locality is in no gazetteer at all. The `Praha 3`
|
|
122
|
+
* class: a municipal district nobody's admin gazetteer names.
|
|
123
|
+
*/
|
|
124
|
+
export type PostcodeCountryScopeEvidence = "pair" | "locality" | "postcode";
|
|
125
|
+
/**
|
|
126
|
+
* A country in which the address's own evidence is geographically consistent.
|
|
86
127
|
*/
|
|
87
128
|
export interface PostcodeCountryScope {
|
|
88
129
|
/**
|
|
@@ -97,18 +138,24 @@ export interface PostcodeCountryScope {
|
|
|
97
138
|
* The locality string the verdict was reached on.
|
|
98
139
|
*/
|
|
99
140
|
locality: string;
|
|
141
|
+
/**
|
|
142
|
+
* Which evidence carried the verdict. See {@link PostcodeCountryScopeEvidence}.
|
|
143
|
+
*/
|
|
144
|
+
evidence: PostcodeCountryScopeEvidence;
|
|
100
145
|
/**
|
|
101
146
|
* Distance between the resolved postcode point and the nearest same-named locality, in km. Always `<= gateKm`.
|
|
147
|
+
* Present ONLY for the `pair` rung — a single-sided verdict has no two points to measure between, and reporting a `0`
|
|
148
|
+
* there would assert an agreement nobody tested (the meaning-of-zero rule).
|
|
102
149
|
*/
|
|
103
|
-
distanceKm
|
|
150
|
+
distanceKm?: number;
|
|
104
151
|
/**
|
|
105
|
-
* The postcode place that anchored the verdict.
|
|
152
|
+
* The postcode place that anchored the verdict. Absent on the `locality` rung.
|
|
106
153
|
*/
|
|
107
|
-
postcodePlace
|
|
154
|
+
postcodePlace?: ResolvedPlace;
|
|
108
155
|
/**
|
|
109
|
-
* The locality place that anchored the verdict.
|
|
156
|
+
* The locality place that anchored the verdict. Absent on the `postcode` rung.
|
|
110
157
|
*/
|
|
111
|
-
localityPlace
|
|
158
|
+
localityPlace?: ResolvedPlace;
|
|
112
159
|
}
|
|
113
160
|
export interface PostcodeCountryScopeOpts {
|
|
114
161
|
/**
|
|
@@ -117,18 +164,48 @@ export interface PostcodeCountryScopeOpts {
|
|
|
117
164
|
*/
|
|
118
165
|
postcode: string;
|
|
119
166
|
/**
|
|
120
|
-
* The country the caller's `defaultCountry` would hard-filter to
|
|
121
|
-
*
|
|
167
|
+
* The country the caller's `defaultCountry` would hard-filter to — or `undefined` when no default is in force (the
|
|
168
|
+
* browser cascade), where the pass CONSTRAINS instead of overriding: the default-coherence short-circuit is vacuous
|
|
169
|
+
* and the exactly-one-country abstention carries the safety alone. Historically required: this pass was built to
|
|
170
|
+
* correct a wrong one, so with no default there is nothing to correct and the caller should not be calling.
|
|
122
171
|
*/
|
|
123
|
-
defaultCountry: string;
|
|
172
|
+
defaultCountry: string | undefined;
|
|
124
173
|
/**
|
|
125
174
|
* Consistency gate radius in km. Defaults to {@link POSTCODE_COUNTRY_COHERENCE_GATE_KM}.
|
|
126
175
|
*/
|
|
127
176
|
gateKm?: number;
|
|
177
|
+
/**
|
|
178
|
+
* Optional narrowing of the SHAPE half of the candidate-country set — the shape-coherence pass's intersection for a
|
|
179
|
+
* CONFIRMED postcode span (see `resolver/postcode-shape-coherence.ts`, #31 Mechanism 1). When present it REPLACES
|
|
180
|
+
* `candidateSystemsForPostcode`'s own list; it is a pure subset of that list (codex systems ∩ confident sibling
|
|
181
|
+
* systems). Upper-case ISO-3166 alpha-2, e.g. `["US"]` for a 5-digit code whose siblings all say US.
|
|
182
|
+
*
|
|
183
|
+
* It narrows the shape half only. Since #24 the candidate set also carries the countries the GAZETTEER holds this
|
|
184
|
+
* postcode in, and that half is not narrowable — it is the artifact reporting what it contains, which is the same
|
|
185
|
+
* class of evidence the pair test itself runs on. Every candidate from either half still has to pass that test.
|
|
186
|
+
*/
|
|
187
|
+
candidateSystems?: readonly string[];
|
|
128
188
|
}
|
|
129
189
|
/**
|
|
130
|
-
* The address's locality
|
|
131
|
-
* Two passes so a real locality always beats a dependent one regardless of tree
|
|
190
|
+
* The address's locality strings in DOCUMENT ORDER — every `locality` value first, then every `dependent_locality`,
|
|
191
|
+
* case-insensitively deduplicated. Two passes so a real locality always beats a dependent one regardless of tree
|
|
192
|
+
* order.
|
|
193
|
+
*
|
|
194
|
+
* Document order is load-bearing, not cosmetic. The original stack-pop traversal visited the LAST node first, so on `92
|
|
195
|
+
* Laurell Road, Gander, NL A1V 0A9` — where the model tags both `Gander` and the province abbreviation `NL` as
|
|
196
|
+
* localities — the pass keyed its whole country verdict on "NL", whose only exact locality-band bearer is an alias of
|
|
197
|
+
* Nal, Afghanistan, and the walk resolved a Newfoundland street 10,000 km away. The FIRST-written locality is the one
|
|
198
|
+
* the address is about.
|
|
199
|
+
*
|
|
200
|
+
* ALL values matter, not just the first. On `Calle Mayor 12, Aravaca, 28023 Madrid` the model tags both `Aravaca` and
|
|
201
|
+
* `Madrid` as localities; `Aravaca` is a neighbourhood the locality band cannot see, while `Madrid` carries the ES pair
|
|
202
|
+
* verdict (the ES 28023 row sits 9.6 km from the Madrid locality row). A pass keyed on a single value bets the whole
|
|
203
|
+
* country verdict on whichever one a traversal order happens to pick — first-only re-scoped that address to the US ZIP
|
|
204
|
+
* 28023 centroid in North Carolina. So the verdict rungs try each value in order until one produces evidence.
|
|
205
|
+
*/
|
|
206
|
+
export declare function localityValuesInDocumentOrder(roots: readonly AddressNode[]): string[];
|
|
207
|
+
/**
|
|
208
|
+
* The first of {@link localityValuesInDocumentOrder} — the locality the address is about.
|
|
132
209
|
*/
|
|
133
210
|
export declare function firstLocalityValue(roots: readonly AddressNode[]): string | undefined;
|
|
134
211
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postcode-country-coherence.d.ts","sourceRoot":"","sources":["../postcode-country-coherence.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"postcode-country-coherence.d.ts","sourceRoot":"","sources":["../postcode-country-coherence.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuGG;AAKH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAG9E;;;GAGG;AACH,eAAO,MAAM,kCAAkC,KAAK,CAAA;AAEpD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,4BAA4B,GAAG,MAAM,GAAG,UAAU,GAAG,UAAU,CAAA;AAE3E;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,4BAA4B,CAAA;IACtC;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;OAEG;IACH,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B;;OAEG;IACH,aAAa,CAAC,EAAE,aAAa,CAAA;CAC7B;AAED,MAAM,WAAW,wBAAwB;IACxC;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,cAAc,EAAE,MAAM,GAAG,SAAS,CAAA;IAClC;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;;;;;OASG;IACH,gBAAgB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACpC;AAeD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,EAAE,CASrF;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,GAAG,SAAS,CAEpF;AA0ID;;;;;;;;;;;;;GAaG;AACH,wBAAsB,wBAAwB,CAC7C,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,GAC5B,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAC,CA2JtC;AAmBD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,SAAS,WAAW,EAAE,EAAE,KAAK,EAAE,oBAAoB,GAAG,IAAI,CAqB1G"}
|
|
@@ -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
|
|
@@ -90,35 +118,122 @@ function hasCoord(p) {
|
|
|
90
118
|
return p.lat !== 0 || p.lon !== 0;
|
|
91
119
|
}
|
|
92
120
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
121
|
+
* Upper bound on locality values the pass will consider. A parse rarely tags more than two; the cap keeps a
|
|
122
|
+
* pathological tree from multiplying the pair sweep.
|
|
95
123
|
*/
|
|
96
|
-
|
|
124
|
+
const MAX_LOCALITY_VALUES = 3;
|
|
125
|
+
/**
|
|
126
|
+
* The address's locality strings in DOCUMENT ORDER — every `locality` value first, then every `dependent_locality`,
|
|
127
|
+
* case-insensitively deduplicated. Two passes so a real locality always beats a dependent one regardless of tree
|
|
128
|
+
* order.
|
|
129
|
+
*
|
|
130
|
+
* Document order is load-bearing, not cosmetic. The original stack-pop traversal visited the LAST node first, so on `92
|
|
131
|
+
* Laurell Road, Gander, NL A1V 0A9` — where the model tags both `Gander` and the province abbreviation `NL` as
|
|
132
|
+
* localities — the pass keyed its whole country verdict on "NL", whose only exact locality-band bearer is an alias of
|
|
133
|
+
* Nal, Afghanistan, and the walk resolved a Newfoundland street 10,000 km away. The FIRST-written locality is the one
|
|
134
|
+
* the address is about.
|
|
135
|
+
*
|
|
136
|
+
* ALL values matter, not just the first. On `Calle Mayor 12, Aravaca, 28023 Madrid` the model tags both `Aravaca` and
|
|
137
|
+
* `Madrid` as localities; `Aravaca` is a neighbourhood the locality band cannot see, while `Madrid` carries the ES pair
|
|
138
|
+
* verdict (the ES 28023 row sits 9.6 km from the Madrid locality row). A pass keyed on a single value bets the whole
|
|
139
|
+
* country verdict on whichever one a traversal order happens to pick — first-only re-scoped that address to the US ZIP
|
|
140
|
+
* 28023 centroid in North Carolina. So the verdict rungs try each value in order until one produces evidence.
|
|
141
|
+
*/
|
|
142
|
+
export function localityValuesInDocumentOrder(roots) {
|
|
143
|
+
const out = [];
|
|
144
|
+
const seen = new Set();
|
|
97
145
|
for (const tag of ["locality", "dependent_locality"]) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
146
|
+
collectInDocumentOrder(roots, tag, out, seen);
|
|
147
|
+
}
|
|
148
|
+
return out.slice(0, MAX_LOCALITY_VALUES);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The first of {@link localityValuesInDocumentOrder} — the locality the address is about.
|
|
152
|
+
*/
|
|
153
|
+
export function firstLocalityValue(roots) {
|
|
154
|
+
return localityValuesInDocumentOrder(roots)[0];
|
|
155
|
+
}
|
|
156
|
+
function collectInDocumentOrder(nodes, tag, out, seen) {
|
|
157
|
+
for (const n of nodes) {
|
|
158
|
+
if (n.tag === tag && n.value.trim().length) {
|
|
159
|
+
const value = n.value.trim();
|
|
160
|
+
const key = value.toLowerCase();
|
|
161
|
+
if (!seen.has(key)) {
|
|
162
|
+
seen.add(key);
|
|
163
|
+
out.push(value);
|
|
164
|
+
}
|
|
104
165
|
}
|
|
166
|
+
collectInDocumentOrder(n.children, tag, out, seen);
|
|
105
167
|
}
|
|
106
|
-
return undefined;
|
|
107
168
|
}
|
|
108
169
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
170
|
+
* Over-fetch for the UNSCOPED holder probes (#24). Postcodes are bounded and small — the 2026-08-10 candidate
|
|
171
|
+
* gazetteer's most-shared code carries 12 rows across 8 countries — so 20 sees every bearer and the postcode holder set
|
|
172
|
+
* is COMPLETE, not a sample. Locality names are not bounded that way (`rampur` has 1,096 rows, `bara` spans 42
|
|
173
|
+
* countries), so 30 is a population-first WINDOW: it can only ever hide a country, which turns a "more than one
|
|
174
|
+
* country" abstention into a false "exactly one". The explicit default-country probe below is what closes that gap for
|
|
175
|
+
* the case that matters (the address is domestic after all).
|
|
112
176
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
177
|
+
const POSTCODE_HOLDER_FETCH = 20;
|
|
178
|
+
const LOCALITY_HOLDER_FETCH = 30;
|
|
179
|
+
/**
|
|
180
|
+
* Upper bound on countries the pair rung will test, so a pathological name can't turn one incoherent parse into a
|
|
181
|
+
* hundred lookups. The postcode holder set is ≤8 countries in the measured artifact and the codex shape list is ≤8, so
|
|
182
|
+
* this only ever binds on a gazetteer very unlike today's.
|
|
183
|
+
*/
|
|
184
|
+
const MAX_CANDIDATE_COUNTRIES = 12;
|
|
185
|
+
/**
|
|
186
|
+
* Countries that hold an exact, coordinate-bearing row for `text` at `placetype`, mapped to their best (first) such
|
|
187
|
+
* row. One UNSCOPED lookup.
|
|
188
|
+
*
|
|
189
|
+
* This is the #24 candidate source, and it replaces a proxy with the thing itself. The pass used to ask codex "which
|
|
190
|
+
* address SYSTEMS could this shape be?" — a model-free shape test over the eight slices codex specifies. That answer is
|
|
191
|
+
* correct and useless for two thirds of Europe: `13000` shapes as `[us, de, fr]`, the gazetteer holds it in exactly one
|
|
192
|
+
* country (CZ), and CZ has no codex slice, so the pass could not propose the only country that could possibly be right.
|
|
193
|
+
* Membership in the gazetteer is positive evidence of the same kind the pair test already runs on, available for one
|
|
194
|
+
* lookup, and it is EXHAUSTIVE for postcodes at the measured cardinality.
|
|
195
|
+
*
|
|
196
|
+
* `exactMatch !== false` is the admission bar: a fuzzy postcode hit is a DIFFERENT postcode (the 2026-08-05 Code-Point
|
|
197
|
+
* lesson — `BT3 9QQ` trigram-matching Sheffield's `S3 9QQ`), and a fuzzy locality hit is evidence about the index, not
|
|
198
|
+
* about the country. Backends that do not stamp the flag leave it undefined and still contribute.
|
|
199
|
+
*/
|
|
200
|
+
async function countriesHolding(backend, text, placetype, limit, opts) {
|
|
201
|
+
const out = new Map();
|
|
202
|
+
let hits;
|
|
115
203
|
try {
|
|
116
|
-
|
|
204
|
+
hits = await backend.findPlace({ text, placetype, limit, ...(opts?.primaryOnly ? { primaryOnly: true } : {}) });
|
|
117
205
|
}
|
|
118
206
|
catch {
|
|
119
|
-
return
|
|
207
|
+
return out; // a backend hiccup degrades to "no evidence", never to a crashed resolve
|
|
208
|
+
}
|
|
209
|
+
for (const hit of hits) {
|
|
210
|
+
const country = hit.country?.trim().toUpperCase();
|
|
211
|
+
if (!country || hit.exactMatch === false || !hasCoord(hit))
|
|
212
|
+
continue;
|
|
213
|
+
if (!out.has(country)) {
|
|
214
|
+
out.set(country, hit);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Is the (postcode, locality) pair geographically consistent in `country`? Returns the winning pair and its distance,
|
|
221
|
+
* or `null` when the postcode does not resolve there, no same-named locality exists there, or the nearest one is
|
|
222
|
+
* outside the gate. Costs one postcode lookup plus (only if that hit) one locality lookup — or just the locality lookup
|
|
223
|
+
* when the caller already knows the country's postcode row (`knownPostcodePlace`, from the exhaustive unscoped probe).
|
|
224
|
+
*/
|
|
225
|
+
async function coherenceIn(country, postcode, locality, backend, gateKm, knownPostcodePlace) {
|
|
226
|
+
let postcodePlace = knownPostcodePlace;
|
|
227
|
+
if (!postcodePlace) {
|
|
228
|
+
let postcodeHits;
|
|
229
|
+
try {
|
|
230
|
+
postcodeHits = await backend.findPlace({ text: postcode, placetype: "postalcode", country, limit: 3 });
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return null; // a backend hiccup degrades to "no evidence here", never to a crashed resolve
|
|
234
|
+
}
|
|
235
|
+
postcodePlace = postcodeHits.find(hasCoord);
|
|
120
236
|
}
|
|
121
|
-
const postcodePlace = postcodeHits.find(hasCoord);
|
|
122
237
|
if (!postcodePlace)
|
|
123
238
|
return null;
|
|
124
239
|
let localityHits;
|
|
@@ -160,36 +275,153 @@ async function coherenceIn(country, postcode, locality, backend, gateKm) {
|
|
|
160
275
|
*/
|
|
161
276
|
export async function findPostcodeCountryScope(roots, backend, opts) {
|
|
162
277
|
const postcode = opts.postcode.trim();
|
|
163
|
-
const defaultCountry = opts.defaultCountry
|
|
164
|
-
if (!postcode
|
|
278
|
+
const defaultCountry = opts.defaultCountry?.trim().toUpperCase() || undefined;
|
|
279
|
+
if (!postcode)
|
|
165
280
|
return null;
|
|
166
|
-
const
|
|
281
|
+
const localities = localityValuesInDocumentOrder(roots);
|
|
167
282
|
// Both halves of the pair are required. A postcode with no locality has nothing to be coherent WITH, which is what
|
|
168
283
|
// keeps this pass inert on "Springfield, IL 62701" (the parser tags Springfield as a `street` — a separate defect)
|
|
169
284
|
// and on every bare-postcode query.
|
|
170
|
-
if (!
|
|
285
|
+
if (!localities.length)
|
|
171
286
|
return null;
|
|
172
287
|
const gateKm = opts.gateKm ?? POSTCODE_COUNTRY_COHERENCE_GATE_KM;
|
|
173
|
-
// 1. Is the caller's own default country coherent? If so we are done —
|
|
174
|
-
// no override, and the common domestic path
|
|
175
|
-
|
|
288
|
+
// 1. Is the caller's own default country coherent with ANY of the address's locality values? If so we are done —
|
|
289
|
+
// positive evidence for the default, no override, and the common domestic path (one locality value) costs two
|
|
290
|
+
// lookups and changes nothing. With no default in force (the browser cascade) there is nothing to test — the
|
|
291
|
+
// sweep below carries the whole verdict.
|
|
292
|
+
if (defaultCountry) {
|
|
293
|
+
for (const locality of localities) {
|
|
294
|
+
if (await coherenceIn(defaultCountry, postcode, locality, backend, gateKm))
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// 2. The default could not place this pair. Which countries could? Two sources, unioned:
|
|
299
|
+
//
|
|
300
|
+
// - the SHAPE list (`candidateSystemsForPostcode`, or the shape-coherence pass's narrowing when #31
|
|
301
|
+
// Mechanism 1 supplied one — a pure subset of it), and
|
|
302
|
+
// - the countries the GAZETTEER actually holds this postcode in (#24). Exhaustive at the measured
|
|
303
|
+
// cardinality, and the only source that can name a country codex has no slice for — which was the
|
|
304
|
+
// whole reason the CZ/CH/BE/DK/AT/NL block of the 2026-08-09 panel resolved to US namesakes while
|
|
305
|
+
// their own postcodes sat in the shipped gazetteer within 4 km of truth.
|
|
306
|
+
//
|
|
307
|
+
// The union can only ADD countries, and every added one must still pass the same geometric pair test,
|
|
308
|
+
// so this widens recall without weakening the verdict: a second coherent country turns a verdict into
|
|
309
|
+
// an abstention (safe), never into a wrong answer.
|
|
310
|
+
const pcHolders = await countriesHolding(backend, postcode, "postalcode", POSTCODE_HOLDER_FETCH);
|
|
311
|
+
const shapeSystems = (opts.candidateSystems ?? candidateSystemsForPostcode(postcode).map((system) => system.toUpperCase())).map((country) => country.trim().toUpperCase());
|
|
312
|
+
const candidates = [...new Set([...shapeSystems, ...pcHolders.keys()])]
|
|
313
|
+
.filter((country) => country !== defaultCountry)
|
|
314
|
+
.slice(0, MAX_CANDIDATE_COUNTRIES);
|
|
315
|
+
// 3. The pair sweep, per locality value in document order: the first value that produces evidence decides. A value
|
|
316
|
+
// the band cannot see (`Aravaca`, a neighbourhood) yields zero coherent countries and the NEXT value gets its
|
|
317
|
+
// turn — betting the verdict on a single value re-scoped that address to a US ZIP centroid (see
|
|
318
|
+
// localityValuesInDocumentOrder). Exactly one coherent country is a verdict. Two coherent countries mean the
|
|
319
|
+
// geometry genuinely does not decide, and guessing between them is precisely what this mechanism exists not to
|
|
320
|
+
// do — a TIE is a hard abstention, never a fall-through to a later value or the weaker rungs below.
|
|
321
|
+
for (const locality of localities) {
|
|
322
|
+
const coherent = [];
|
|
323
|
+
for (const country of candidates) {
|
|
324
|
+
const hit = await coherenceIn(country, postcode, locality, backend, gateKm, pcHolders.get(country));
|
|
325
|
+
if (hit) {
|
|
326
|
+
coherent.push({ country, postcode, locality, evidence: "pair", ...hit });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (coherent.length === 1)
|
|
330
|
+
return coherent[0];
|
|
331
|
+
if (coherent.length > 1)
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
// The single-sided rungs below lean on the DEFAULT country as the domestic-plausibility guard
|
|
335
|
+
// ("Vienna, VA" stays in Virginia because the default corroborates a half). With no default there
|
|
336
|
+
// is no such anchor, so the no-default path ends at the pair rung: a pair verdict (Zabiče) is
|
|
337
|
+
// in-scope, a single-sided guess is not.
|
|
338
|
+
if (!defaultCountry)
|
|
339
|
+
return null;
|
|
340
|
+
// 4. No country makes the PAIR consistent. On the 2026-08-09 panel that happened 10 times for two opposite
|
|
341
|
+
// reasons, and in both the address's country was never in doubt — only unrepresentable as a pair:
|
|
342
|
+
//
|
|
343
|
+
// - the gazetteer holds no postcodes for the country at all (CH, BE), so the postcode half can never
|
|
344
|
+
// agree with anything no matter how good the locality evidence is; or
|
|
345
|
+
// - the locality is a name no admin gazetteer carries (`Praha 3`, `Praha 9` — municipal districts).
|
|
346
|
+
//
|
|
347
|
+
// So each half gets to speak alone, under conditions that keep this from becoming a guess. The
|
|
348
|
+
// POSTCODE half is all-or-nothing: the default country holding this postcode makes the address
|
|
349
|
+
// domestically plausible outright, and nothing foreign may be proposed. The LOCALITY half is
|
|
350
|
+
// judged PER VALUE — a value the default country holds is domestic corroboration for THAT value
|
|
351
|
+
// (it is neutralized as a foreign witness) without silencing its siblings. `14 Long St, Green
|
|
352
|
+
// Point, Cape Town, 8001` is why: "Green Point" has pop-0 US namesakes, and letting that one
|
|
353
|
+
// value abstain the whole pass handed a uniquely-ZA "Cape Town" to a hard US filter (the walk
|
|
354
|
+
// answered Green Point, Pennsylvania). `123 Main St, Vienna, VA 22180` still holds in Virginia:
|
|
355
|
+
// its ONLY locality value is domestic, so no foreign witness remains. The speaking value must
|
|
356
|
+
// name EXACTLY ONE country in the entire gazetteer — the same uniqueness bar the pair rung uses,
|
|
357
|
+
// applied to one side — and all speaking values must AGREE on that country.
|
|
358
|
+
if (pcHolders.has(defaultCountry))
|
|
176
359
|
return null;
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
360
|
+
// 4a. A locality value names exactly one country. Stronger than the postcode rung — a place name is far
|
|
361
|
+
// more discriminative than a four-digit code — so it is tried first. A verdict stands down when that
|
|
362
|
+
// country's OWN postcode row contradicts the locality (outside the gate): the two halves disagreeing
|
|
363
|
+
// inside one country is exactly what the pair test exists to catch, and a single-sided rung must not
|
|
364
|
+
// launder it.
|
|
365
|
+
// PRIMARY-keyed rows only (#1626's re-reading discipline, applied to a country verdict): "the locality
|
|
366
|
+
// names exactly one country" must mean a place NAMED that, never a place aliased to it — "NL"'s only
|
|
367
|
+
// locality-band bearer was an exact ALIAS of Nal, Afghanistan, and a single alias row steered the whole
|
|
368
|
+
// walk's country. The pair rung keeps aliases: there the postcode's own geometry corroborates them.
|
|
369
|
+
let anyDomestic = false;
|
|
370
|
+
let anyLocalityKnown = false;
|
|
371
|
+
const verdicts = new Map();
|
|
372
|
+
for (const locality of localities) {
|
|
373
|
+
if (await holdsLocality(backend, locality, defaultCountry)) {
|
|
374
|
+
anyDomestic = true;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
const locHolders = await countriesHolding(backend, locality, "locality", LOCALITY_HOLDER_FETCH, {
|
|
378
|
+
primaryOnly: true,
|
|
379
|
+
});
|
|
380
|
+
if (locHolders.size) {
|
|
381
|
+
anyLocalityKnown = true;
|
|
382
|
+
}
|
|
383
|
+
if (locHolders.size !== 1)
|
|
384
|
+
continue;
|
|
385
|
+
const [country, localityPlace] = [...locHolders.entries()][0];
|
|
386
|
+
const ownPostcode = pcHolders.get(country);
|
|
387
|
+
const contradicted = ownPostcode !== undefined &&
|
|
388
|
+
haversineKm(ownPostcode.lat, ownPostcode.lon, localityPlace.lat, localityPlace.lon) > gateKm;
|
|
389
|
+
if (country !== defaultCountry && !contradicted && !verdicts.has(country)) {
|
|
390
|
+
verdicts.set(country, { country, postcode, locality, evidence: "locality", localityPlace });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
// Every speaking value must agree: two values uniquely naming DIFFERENT countries is a genuine
|
|
394
|
+
// ambiguity, and guessing between them is what this mechanism exists not to do.
|
|
395
|
+
if (verdicts.size === 1)
|
|
396
|
+
return [...verdicts.values()][0];
|
|
397
|
+
if (verdicts.size > 1)
|
|
182
398
|
return null;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
399
|
+
// 4b. The postcode is held in exactly one country and the locality half is silent EVERYWHERE — no value
|
|
400
|
+
// is domestic and no value names any country. `Biskupcova 1843/3, 13000 Praha 3`: `13000` is a CZ
|
|
401
|
+
// code and nothing else, anywhere, and `Praha 3` (a municipal district) is in no admin gazetteer.
|
|
402
|
+
if (!anyDomestic && !anyLocalityKnown && pcHolders.size === 1) {
|
|
403
|
+
const [country, postcodePlace] = [...pcHolders.entries()][0];
|
|
404
|
+
if (country !== defaultCountry) {
|
|
405
|
+
return { country, postcode, locality: localities[0], evidence: "postcode", postcodePlace };
|
|
188
406
|
}
|
|
189
407
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Does `country` hold an exact, coordinate-bearing locality row for `locality`? One scoped lookup — deliberately NOT
|
|
412
|
+
* read off the unscoped holder window, which is population-first and truncated, so a small domestic bearer could fall
|
|
413
|
+
* below its fold and let a foreign scope through.
|
|
414
|
+
*/
|
|
415
|
+
async function holdsLocality(backend, locality, country) {
|
|
416
|
+
try {
|
|
417
|
+
const hits = await backend.findPlace({ text: locality, placetype: "locality", country, limit: 3 });
|
|
418
|
+
return hits.some((hit) => hit.exactMatch && hasCoord(hit));
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
// A backend hiccup must not be read as "the default country has nothing" — that would license a foreign
|
|
422
|
+
// scope on no evidence. Treat it as corroboration and abstain.
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
193
425
|
}
|
|
194
426
|
/**
|
|
195
427
|
* Stamp the adopted scope onto the tree's postcode and locality nodes, so a consumer can see that the walk's country
|
|
@@ -206,7 +438,12 @@ export function stampPostcodeCountryScope(roots, scope) {
|
|
|
206
438
|
n.metadata = {
|
|
207
439
|
...n.metadata,
|
|
208
440
|
postcode_country_scope: scope.country,
|
|
209
|
-
|
|
441
|
+
// WHICH rung spoke — a two-sided agreement and a one-sided uniqueness claim are different evidence,
|
|
442
|
+
// and a consumer that cannot tell them apart cannot weight them differently.
|
|
443
|
+
postcode_country_scope_evidence: scope.evidence,
|
|
444
|
+
// Only the `pair` rung measured a distance. Writing a 0 for the single-sided rungs would assert an
|
|
445
|
+
// agreement nobody tested.
|
|
446
|
+
...(scope.distanceKm !== undefined ? { postcode_country_scope_km: scope.distanceKm } : {}),
|
|
210
447
|
};
|
|
211
448
|
}
|
|
212
449
|
}
|