@mailwoman/resolver 9.1.0 → 9.3.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 (142) hide show
  1. package/README.md +89 -0
  2. package/lib/admin/coherence-passes.ts +435 -0
  3. package/lib/admin/containment.ts +92 -0
  4. package/lib/admin/index.ts +10 -0
  5. package/lib/admin/jp-municipality.ts +97 -0
  6. package/lib/admin/winner.ts +189 -0
  7. package/lib/backend-capabilities.ts +88 -0
  8. package/lib/bare-toponym-race.ts +129 -0
  9. package/lib/decorate-node.ts +127 -0
  10. package/lib/fold-name.ts +36 -0
  11. package/{index.ts → lib/index.ts} +27 -18
  12. package/{plausibility.ts → lib/plausibility.ts} +5 -4
  13. package/{postcode-country-coherence.ts → lib/postcode/country-coherence.ts} +173 -86
  14. package/lib/postcode/index.ts +9 -0
  15. package/{postcode-prefix.ts → lib/postcode/prefix.ts} +7 -6
  16. package/{postcode-shape-coherence.ts → lib/postcode/shape-coherence.ts} +10 -24
  17. package/{remote-resolver.ts → lib/remote-resolver.ts} +13 -4
  18. package/{rerank.ts → lib/rerank.ts} +2 -2
  19. package/lib/resolve/index.ts +982 -0
  20. package/lib/resolve/passes.ts +544 -0
  21. package/{span-rescore.ts → lib/span-rescore.ts} +114 -99
  22. package/{street-evidence.ts → lib/street/evidence.ts} +40 -7
  23. package/lib/street/index.ts +8 -0
  24. package/{street-tier.ts → lib/street/tier.ts} +138 -99
  25. package/{toponym-prior.ts → lib/toponym-prior.ts} +97 -8
  26. package/out/admin/coherence-passes.d.ts +70 -0
  27. package/out/admin/coherence-passes.d.ts.map +1 -0
  28. package/out/admin/coherence-passes.js +327 -0
  29. package/out/admin/coherence-passes.js.map +1 -0
  30. package/out/admin/containment.d.ts +48 -0
  31. package/out/admin/containment.d.ts.map +1 -0
  32. package/out/admin/containment.js +76 -0
  33. package/out/admin/containment.js.map +1 -0
  34. package/out/admin/index.d.ts +10 -0
  35. package/out/admin/index.d.ts.map +1 -0
  36. package/out/admin/index.js +10 -0
  37. package/out/admin/index.js.map +1 -0
  38. package/out/admin/jp-municipality.d.ts +50 -0
  39. package/out/admin/jp-municipality.d.ts.map +1 -0
  40. package/out/admin/jp-municipality.js +54 -0
  41. package/out/admin/jp-municipality.js.map +1 -0
  42. package/out/admin/winner.d.ts +114 -0
  43. package/out/admin/winner.d.ts.map +1 -0
  44. package/out/admin/winner.js +130 -0
  45. package/out/admin/winner.js.map +1 -0
  46. package/out/backend-capabilities.d.ts +37 -0
  47. package/out/backend-capabilities.d.ts.map +1 -0
  48. package/out/backend-capabilities.js +75 -0
  49. package/out/backend-capabilities.js.map +1 -0
  50. package/out/bare-toponym-race.d.ts +55 -0
  51. package/out/bare-toponym-race.d.ts.map +1 -0
  52. package/out/bare-toponym-race.js +110 -0
  53. package/out/bare-toponym-race.js.map +1 -0
  54. package/out/decorate-node.d.ts +24 -0
  55. package/out/decorate-node.d.ts.map +1 -0
  56. package/out/decorate-node.js +103 -0
  57. package/out/decorate-node.js.map +1 -0
  58. package/out/fold-name.d.ts +18 -3
  59. package/out/fold-name.d.ts.map +1 -1
  60. package/out/fold-name.js +19 -3
  61. package/out/fold-name.js.map +1 -1
  62. package/out/index.d.ts +15 -12
  63. package/out/index.d.ts.map +1 -1
  64. package/out/index.js +9 -7
  65. package/out/index.js.map +1 -1
  66. package/out/plausibility.d.ts +5 -4
  67. package/out/plausibility.d.ts.map +1 -1
  68. package/out/plausibility.js +5 -4
  69. package/out/plausibility.js.map +1 -1
  70. package/out/{postcode-country-coherence.d.ts → postcode/country-coherence.d.ts} +46 -25
  71. package/out/postcode/country-coherence.d.ts.map +1 -0
  72. package/out/{postcode-country-coherence.js → postcode/country-coherence.js} +157 -77
  73. package/out/postcode/country-coherence.js.map +1 -0
  74. package/out/postcode/index.d.ts +9 -0
  75. package/out/postcode/index.d.ts.map +1 -0
  76. package/out/postcode/index.js +9 -0
  77. package/out/postcode/index.js.map +1 -0
  78. package/out/{postcode-prefix.d.ts → postcode/prefix.d.ts} +4 -4
  79. package/out/postcode/prefix.d.ts.map +1 -0
  80. package/out/{postcode-prefix.js → postcode/prefix.js} +7 -6
  81. package/out/postcode/prefix.js.map +1 -0
  82. package/out/{postcode-shape-coherence.d.ts → postcode/shape-coherence.d.ts} +3 -3
  83. package/out/postcode/shape-coherence.d.ts.map +1 -0
  84. package/out/{postcode-shape-coherence.js → postcode/shape-coherence.js} +11 -20
  85. package/out/postcode/shape-coherence.js.map +1 -0
  86. package/out/remote-resolver.d.ts +18 -3
  87. package/out/remote-resolver.d.ts.map +1 -1
  88. package/out/remote-resolver.js +2 -2
  89. package/out/remote-resolver.js.map +1 -1
  90. package/out/rerank.d.ts +1 -1
  91. package/out/rerank.d.ts.map +1 -1
  92. package/out/rerank.js +2 -2
  93. package/out/rerank.js.map +1 -1
  94. package/out/{resolve.d.ts → resolve/index.d.ts} +2 -2
  95. package/out/resolve/index.d.ts.map +1 -0
  96. package/out/resolve/index.js +795 -0
  97. package/out/resolve/index.js.map +1 -0
  98. package/out/resolve/passes.d.ts +218 -0
  99. package/out/resolve/passes.d.ts.map +1 -0
  100. package/out/resolve/passes.js +333 -0
  101. package/out/resolve/passes.js.map +1 -0
  102. package/out/span-rescore.d.ts +16 -16
  103. package/out/span-rescore.d.ts.map +1 -1
  104. package/out/span-rescore.js +87 -71
  105. package/out/span-rescore.js.map +1 -1
  106. package/out/{street-evidence.d.ts → street/evidence.d.ts} +18 -3
  107. package/out/street/evidence.d.ts.map +1 -0
  108. package/out/{street-evidence.js → street/evidence.js} +23 -7
  109. package/out/street/evidence.js.map +1 -0
  110. package/out/street/index.d.ts +8 -0
  111. package/out/street/index.d.ts.map +1 -0
  112. package/out/street/index.js +8 -0
  113. package/out/street/index.js.map +1 -0
  114. package/out/{street-tier.d.ts → street/tier.d.ts} +14 -3
  115. package/out/street/tier.d.ts.map +1 -0
  116. package/out/{street-tier.js → street/tier.js} +127 -90
  117. package/out/street/tier.js.map +1 -0
  118. package/out/toponym-prior.d.ts +43 -19
  119. package/out/toponym-prior.d.ts.map +1 -1
  120. package/out/toponym-prior.js +76 -8
  121. package/out/toponym-prior.js.map +1 -1
  122. package/package.json +167 -19
  123. package/vitest.config.ts +5 -6
  124. package/fold-name.ts +0 -20
  125. package/out/postcode-country-coherence.d.ts.map +0 -1
  126. package/out/postcode-country-coherence.js.map +0 -1
  127. package/out/postcode-prefix.d.ts.map +0 -1
  128. package/out/postcode-prefix.js.map +0 -1
  129. package/out/postcode-shape-coherence.d.ts.map +0 -1
  130. package/out/postcode-shape-coherence.js.map +0 -1
  131. package/out/resolve.d.ts.map +0 -1
  132. package/out/resolve.js +0 -1049
  133. package/out/resolve.js.map +0 -1
  134. package/out/street-evidence.d.ts.map +0 -1
  135. package/out/street-evidence.js.map +0 -1
  136. package/out/street-tier.d.ts.map +0 -1
  137. package/out/street-tier.js.map +0 -1
  138. package/out/vitest.config.d.ts +0 -16
  139. package/out/vitest.config.d.ts.map +0 -1
  140. package/out/vitest.config.js +0 -36
  141. package/out/vitest.config.js.map +0 -1
  142. package/resolve.ts +0 -1369
@@ -69,23 +69,24 @@
69
69
  * CONFIRMS it instead.
70
70
  *
71
71
  * **D-rule: opt-in behind `ResolveOpts.postcodeShapeCoherence`, default-OFF.** Demotion is the
72
- * failure mode with teeth, so a default-on promotion needs the full pre-registered gate (B1-1
72
+ * failure mode with teeth, so a default-on promotion needs the full pre-registered eval (B1-1
73
73
  * byte-stability, B1-2 exclusion ≥90% with the correct sibling tag surviving, B1-3 confound ≤2%
74
74
  * false exclusions; kill on any B1-3 δ).
75
75
  */
76
76
 
77
- import { candidateSystemsForPostcode, type SystemCode } from "@mailwoman/codex"
77
+ import { candidateSystemsForPostcode, SYSTEM_CODES } from "@mailwoman/codex"
78
78
  import { matchCountry, matchSubdivision } from "@mailwoman/codex/country"
79
79
  import { isUSStateAbbreviation } from "@mailwoman/codex/us"
80
- import type { AddressNode } from "@mailwoman/core/decoder"
80
+ import { collectNodes, walkNodes, type AddressNode } from "@mailwoman/core/decoder"
81
81
 
82
82
  /**
83
83
  * The codex address systems a sibling signal can speak for — the universe `candidateSystemsForPostcode` can return, in
84
84
  * the upper-case ISO form this module's signals are emitted in (`SystemCode` itself is lower-case). Signals from
85
85
  * countries with no codex slice are filtered out BEFORE the intersection test, so a slice-less country can never
86
- * manufacture an empty intersection (the false-exclusion trap; see the header).
86
+ * manufacture an empty intersection (the false-exclusion trap; see the header). Derived from codex's own list so a
87
+ * system added there is admitted here in the same change.
87
88
  */
88
- const SYSTEM_UNIVERSE: ReadonlySet<string> = new Set<string>(["US", "DE", "FR", "CA", "GB", "JP", "AU", "NZ"])
89
+ const SYSTEM_UNIVERSE: ReadonlySet<string> = new Set<string>(SYSTEM_CODES.map((system) => system.toUpperCase()))
89
90
 
90
91
  /**
91
92
  * The pass's per-tree verdict — the caller (and the B1 board tests) can see exactly which spans were confirmed,
@@ -119,12 +120,8 @@ export interface PostcodeShapeVerdict {
119
120
  */
120
121
  function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
121
122
  const out = new Set<string>()
122
- const stack: AddressNode[] = [...roots]
123
-
124
- while (stack.length) {
125
- const n = stack.pop()!
126
- stack.push(...n.children)
127
123
 
124
+ for (const n of walkNodes(roots)) {
128
125
  if (n.tag === "country") {
129
126
  const matched = matchCountry(n.value)
130
127
 
@@ -143,7 +140,7 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
143
140
  if (n.tag === "region") {
144
141
  // (a) The region's value as a subdivision ("CA" → US, "ON" → CA — the US-wins tiebreak for
145
142
  // "CA" is matchSubdivision's own). (b) The `country_hint` stamp annotateUSRegions writes on
146
- // 2-letter US state abbreviations — the same evidence via the pipeline's other seam.
143
+ // 2-letter US state abbreviations — the same evidence via the pipeline's other path.
147
144
  const sub = matchSubdivision(n.value)
148
145
 
149
146
  if (sub && SYSTEM_UNIVERSE.has(sub.country.toUpperCase())) {
@@ -169,20 +166,9 @@ function collectSiblingSystems(roots: readonly AddressNode[]): Set<string> {
169
166
  export function applyPostcodeShapeCoherence(roots: readonly AddressNode[]): PostcodeShapeVerdict {
170
167
  const verdict: PostcodeShapeVerdict = { confirmed: [], excluded: [], abstained: [] }
171
168
 
172
- // The postcode spans in tree order — the same DFS `firstPostcodeValue` uses, so the verdict's
169
+ // The postcode spans in document order — the same walk `firstPostcodeValue` uses, so the verdict's
173
170
  // "first confirmed" is the node `state.postcode` will read.
174
- const postcodes: AddressNode[] = []
175
- const stack: AddressNode[] = [...roots]
176
-
177
- while (stack.length) {
178
- const n = stack.pop()!
179
-
180
- if (n.tag === "postcode" && n.value.trim().length) {
181
- postcodes.push(n)
182
- }
183
-
184
- stack.push(...n.children)
185
- }
171
+ const postcodes = collectNodes(roots, (n) => n.tag === "postcode" && n.value.trim().length > 0)
186
172
 
187
173
  if (!postcodes.length) return verdict
188
174
 
@@ -5,11 +5,11 @@
5
5
  *
6
6
  * `RemoteResolver` — the `Resolver` interface (`resolveTree`) over HTTP. The adapter the interface
7
7
  * docstring anticipated (Phase 4.4): a client POSTs a parsed `AddressTree` + the serializable
8
- * `ResolveOpts` to a resolver service, which owns the gazetteer + situs/interpolation shards,
8
+ * `ResolveOpts` to a resolver service, which owns the gazetteer + situs/interpolation extracts,
9
9
  * runs the cascade, and returns the resolved tree. Two payoffs:
10
10
  *
11
11
  * 1. **Multi-instance** — stateless parser nodes (the ~30 MB ONNX model) talk to ONE resolver service
12
- * (the multi-GB gazetteer + shards). `parse` locally, `new RemoteResolver(...).resolveTree`
12
+ * (the multi-GB gazetteer + extracts). `parse` locally, `new RemoteResolver(...).resolveTree`
13
13
  * remotely — same interface the in-process `WOFResolver` satisfies, so it's a drop-in.
14
14
  * 2. **Canary** — point it at a second resolver build (or an adapter fronting Pelias/Nominatim/BAN)
15
15
  * and diff the resolved trees through the identical contract.
@@ -39,6 +39,11 @@ export function serializableResolveOpts(opts?: ResolveOpts): SerializableResolve
39
39
  return rest
40
40
  }
41
41
 
42
+ export type RemoteResolverFetch = (
43
+ url: string,
44
+ init: { method: string; headers: Record<string, string>; body: string; signal?: AbortSignal }
45
+ ) => Promise<{ ok: boolean; status: number; statusText: string; json(): Promise<unknown> }>
46
+
42
47
  export interface RemoteResolverOpts {
43
48
  /**
44
49
  * Full URL of the resolver service's resolve-tree endpoint, e.g. `http://resolver:7081/v1/resolve`.
@@ -46,8 +51,12 @@ export interface RemoteResolverOpts {
46
51
  endpoint: string
47
52
  /**
48
53
  * Injectable fetch (tests / custom agents). Defaults to the global `fetch`.
54
+ *
55
+ * Typed by what this module reads — `ok`, `status`, `statusText`, `json()` — rather than the full `fetch`, whose
56
+ * `Response` carries a body stream and headers no code here touches. The global `fetch` satisfies this shape, and a
57
+ * test double can be an object instead of an assertion.
49
58
  */
50
- fetch?: typeof fetch
59
+ fetch?: RemoteResolverFetch
51
60
  /**
52
61
  * Per-request timeout in ms. Default 10000.
53
62
  */
@@ -75,7 +84,7 @@ export interface ResolveTreeResponse {
75
84
 
76
85
  export class RemoteResolver implements Resolver {
77
86
  readonly #endpoint: string
78
- readonly #fetch: typeof fetch
87
+ readonly #fetch: RemoteResolverFetch
79
88
  readonly #timeoutMs: number
80
89
  readonly #headers: Record<string, string>
81
90
 
@@ -32,7 +32,7 @@
32
32
 
33
33
  import type { AddressTree } from "@mailwoman/core/decoder"
34
34
 
35
- import { isImplausibleResolution } from "./plausibility.ts"
35
+ import { isImplausibleResolution } from "#plausibility"
36
36
 
37
37
  /**
38
38
  * One candidate parse from the k-best decode, with its (within-input comparable) score.
@@ -107,7 +107,7 @@ export interface RerankOpts {
107
107
  *
108
108
  * **When every resolved candidate is implausible, the model's rank-1 wins.** A reranker that returns nothing is worse
109
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 gate).
110
+ * grounds to flag low confidence (the Phase-4b ambiguity check).
111
111
  */
112
112
  export async function rerankByResolution<T>(
113
113
  candidates: ReadonlyArray<RerankCandidate<T>>,