@mailwoman/core 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.
Files changed (380) hide show
  1. package/README.md +1 -1
  2. package/api/responses.ts +12 -0
  3. package/coarse-placer/featurize.ts +0 -2
  4. package/coarse-placer/tools/build-dataset.ts +2 -2
  5. package/coarse-placer/tools/build-outlier-exposure.ts +2 -2
  6. package/coarse-placer/tools/build-outlier-latin.ts +14 -8
  7. package/coarse-placer/tools/build-outlier-oa.ts +12 -6
  8. package/coarse-placer/tools/eval-latin-offmap.ts +3 -4
  9. package/coarse-placer/tools/eval-openset.ts +3 -3
  10. package/coarse-placer/tools/eval-quant-compare.ts +3 -3
  11. package/coarse-placer/tools/eval.ts +3 -3
  12. package/coarse-placer/tools/fnv-hash.ts +1 -6
  13. package/coarse-placer/tools/index.ts +1 -1
  14. package/coarse-placer/tools/probe-frontier.ts +3 -4
  15. package/coarse-placer/tools/quantize.ts +3 -2
  16. package/coarse-placer/tools/train.ts +2 -2
  17. package/crypto/node.ts +3 -1
  18. package/data/internal/dictionaries/languages.csv +184 -184
  19. package/data/libpostal/README.md +8 -2
  20. package/db/schema.ts +12 -3
  21. package/decoder/build-tree.ts +19 -0
  22. package/decoder/containment.ts +5 -1
  23. package/decoder/index.ts +1 -0
  24. package/decoder/parse-diff.ts +347 -0
  25. package/decoder/proposals-to-tree.ts +1 -1
  26. package/decoder/serialize-json.ts +51 -7
  27. package/decoder/serialize-tuples.ts +7 -1
  28. package/decoder/serialize-xml.ts +1 -1
  29. package/decoder/tree-shape.ts +144 -0
  30. package/decoder/types.ts +8 -0
  31. package/decoder/validate-tree.ts +15 -3
  32. package/env/index.ts +3 -1
  33. package/env/paths.ts +21 -33
  34. package/env/schema.ts +35 -3
  35. package/fs/utils/formatters.ts +107 -0
  36. package/fs/utils/index.ts +7 -0
  37. package/fs/zip.ts +297 -0
  38. package/layers/manifest.ts +56 -15
  39. package/layers/schema.ts +54 -2
  40. package/locale/locale.ts +1 -1
  41. package/locale/registry.ts +1 -1
  42. package/objects.ts +20 -8
  43. package/out/api/responses.d.ts +9 -0
  44. package/out/api/responses.d.ts.map +1 -1
  45. package/out/api/responses.js +11 -0
  46. package/out/api/responses.js.map +1 -1
  47. package/out/coarse-placer/featurize.d.ts +0 -1
  48. package/out/coarse-placer/featurize.d.ts.map +1 -1
  49. package/out/coarse-placer/featurize.js.map +1 -1
  50. package/out/coarse-placer/tools/build-dataset.js +1 -2
  51. package/out/coarse-placer/tools/build-dataset.js.map +1 -1
  52. package/out/coarse-placer/tools/build-outlier-exposure.js +1 -2
  53. package/out/coarse-placer/tools/build-outlier-exposure.js.map +1 -1
  54. package/out/coarse-placer/tools/build-outlier-latin.d.ts.map +1 -1
  55. package/out/coarse-placer/tools/build-outlier-latin.js +12 -8
  56. package/out/coarse-placer/tools/build-outlier-latin.js.map +1 -1
  57. package/out/coarse-placer/tools/build-outlier-oa.d.ts.map +1 -1
  58. package/out/coarse-placer/tools/build-outlier-oa.js +10 -6
  59. package/out/coarse-placer/tools/build-outlier-oa.js.map +1 -1
  60. package/out/coarse-placer/tools/eval-latin-offmap.d.ts.map +1 -1
  61. package/out/coarse-placer/tools/eval-latin-offmap.js +2 -4
  62. package/out/coarse-placer/tools/eval-latin-offmap.js.map +1 -1
  63. package/out/coarse-placer/tools/eval-openset.js +2 -3
  64. package/out/coarse-placer/tools/eval-openset.js.map +1 -1
  65. package/out/coarse-placer/tools/eval-quant-compare.js +2 -3
  66. package/out/coarse-placer/tools/eval-quant-compare.js.map +1 -1
  67. package/out/coarse-placer/tools/eval.js +2 -3
  68. package/out/coarse-placer/tools/eval.js.map +1 -1
  69. package/out/coarse-placer/tools/fnv-hash.d.ts +1 -6
  70. package/out/coarse-placer/tools/fnv-hash.d.ts.map +1 -1
  71. package/out/coarse-placer/tools/fnv-hash.js +1 -6
  72. package/out/coarse-placer/tools/fnv-hash.js.map +1 -1
  73. package/out/coarse-placer/tools/index.d.ts +1 -1
  74. package/out/coarse-placer/tools/index.js +1 -1
  75. package/out/coarse-placer/tools/probe-frontier.d.ts.map +1 -1
  76. package/out/coarse-placer/tools/probe-frontier.js +2 -4
  77. package/out/coarse-placer/tools/probe-frontier.js.map +1 -1
  78. package/out/coarse-placer/tools/quantize.d.ts.map +1 -1
  79. package/out/coarse-placer/tools/quantize.js +2 -2
  80. package/out/coarse-placer/tools/quantize.js.map +1 -1
  81. package/out/coarse-placer/tools/train.js +1 -2
  82. package/out/coarse-placer/tools/train.js.map +1 -1
  83. package/out/crypto/node.d.ts.map +1 -1
  84. package/out/crypto/node.js +2 -1
  85. package/out/crypto/node.js.map +1 -1
  86. package/out/db/schema.d.ts +12 -3
  87. package/out/db/schema.d.ts.map +1 -1
  88. package/out/db/schema.js +6 -0
  89. package/out/db/schema.js.map +1 -1
  90. package/out/decoder/build-tree.d.ts.map +1 -1
  91. package/out/decoder/build-tree.js +17 -0
  92. package/out/decoder/build-tree.js.map +1 -1
  93. package/out/decoder/containment.d.ts.map +1 -1
  94. package/out/decoder/containment.js +5 -1
  95. package/out/decoder/containment.js.map +1 -1
  96. package/out/decoder/index.d.ts +1 -0
  97. package/out/decoder/index.d.ts.map +1 -1
  98. package/out/decoder/index.js +1 -0
  99. package/out/decoder/index.js.map +1 -1
  100. package/out/decoder/parse-diff.d.ts +125 -0
  101. package/out/decoder/parse-diff.d.ts.map +1 -0
  102. package/out/decoder/parse-diff.js +219 -0
  103. package/out/decoder/parse-diff.js.map +1 -0
  104. package/out/decoder/proposals-to-tree.js.map +1 -1
  105. package/out/decoder/serialize-json.d.ts +29 -0
  106. package/out/decoder/serialize-json.d.ts.map +1 -1
  107. package/out/decoder/serialize-json.js +20 -4
  108. package/out/decoder/serialize-json.js.map +1 -1
  109. package/out/decoder/serialize-tuples.d.ts +8 -1
  110. package/out/decoder/serialize-tuples.d.ts.map +1 -1
  111. package/out/decoder/serialize-tuples.js +7 -1
  112. package/out/decoder/serialize-tuples.js.map +1 -1
  113. package/out/decoder/serialize-xml.js +1 -1
  114. package/out/decoder/serialize-xml.js.map +1 -1
  115. package/out/decoder/tree-shape.d.ts +84 -0
  116. package/out/decoder/tree-shape.d.ts.map +1 -0
  117. package/out/decoder/tree-shape.js +98 -0
  118. package/out/decoder/tree-shape.js.map +1 -0
  119. package/out/decoder/types.d.ts +11 -0
  120. package/out/decoder/types.d.ts.map +1 -1
  121. package/out/decoder/validate-tree.d.ts +8 -0
  122. package/out/decoder/validate-tree.d.ts.map +1 -1
  123. package/out/decoder/validate-tree.js +4 -3
  124. package/out/decoder/validate-tree.js.map +1 -1
  125. package/out/env/index.d.ts +13 -1
  126. package/out/env/index.d.ts.map +1 -1
  127. package/out/env/index.js +2 -1
  128. package/out/env/index.js.map +1 -1
  129. package/out/env/paths.d.ts +15 -8
  130. package/out/env/paths.d.ts.map +1 -1
  131. package/out/env/paths.js +19 -29
  132. package/out/env/paths.js.map +1 -1
  133. package/out/env/schema.d.ts +12 -1
  134. package/out/env/schema.d.ts.map +1 -1
  135. package/out/env/schema.js +33 -3
  136. package/out/env/schema.js.map +1 -1
  137. package/out/fs/utils/formatters.d.ts +57 -0
  138. package/out/fs/utils/formatters.d.ts.map +1 -0
  139. package/out/fs/utils/formatters.js +79 -0
  140. package/out/fs/utils/formatters.js.map +1 -0
  141. package/out/fs/utils/index.d.ts +7 -0
  142. package/out/fs/utils/index.d.ts.map +1 -0
  143. package/out/fs/utils/index.js +7 -0
  144. package/out/fs/utils/index.js.map +1 -0
  145. package/out/fs/zip.d.ts +116 -0
  146. package/out/fs/zip.d.ts.map +1 -0
  147. package/out/fs/zip.js +216 -0
  148. package/out/fs/zip.js.map +1 -0
  149. package/out/layers/manifest.d.ts +32 -7
  150. package/out/layers/manifest.d.ts.map +1 -1
  151. package/out/layers/manifest.js +31 -6
  152. package/out/layers/manifest.js.map +1 -1
  153. package/out/layers/schema.d.ts +48 -2
  154. package/out/layers/schema.d.ts.map +1 -1
  155. package/out/layers/schema.js +32 -0
  156. package/out/layers/schema.js.map +1 -1
  157. package/out/locale/locale.d.ts +1 -1
  158. package/out/locale/locale.d.ts.map +1 -1
  159. package/out/locale/registry.js +1 -1
  160. package/out/locale/registry.js.map +1 -1
  161. package/out/objects.d.ts +12 -6
  162. package/out/objects.d.ts.map +1 -1
  163. package/out/objects.js +4 -1
  164. package/out/objects.js.map +1 -1
  165. package/out/pipeline/index.d.ts +3 -7
  166. package/out/pipeline/index.d.ts.map +1 -1
  167. package/out/pipeline/index.js +2 -4
  168. package/out/pipeline/index.js.map +1 -1
  169. package/out/pipeline/runtime-pipeline.d.ts +51 -9
  170. package/out/pipeline/runtime-pipeline.d.ts.map +1 -1
  171. package/out/pipeline/runtime-pipeline.js +46 -133
  172. package/out/pipeline/runtime-pipeline.js.map +1 -1
  173. package/out/pipeline/span-proposer.js +1 -1
  174. package/out/pipeline/span-proposer.js.map +1 -1
  175. package/out/pipeline/types.d.ts +137 -16
  176. package/out/pipeline/types.d.ts.map +1 -1
  177. package/out/pipeline/types.js +30 -0
  178. package/out/pipeline/types.js.map +1 -1
  179. package/out/policy/defaults.d.ts +1 -1
  180. package/out/policy/defaults.d.ts.map +1 -1
  181. package/out/policy/defaults.js +1 -1
  182. package/out/policy/defaults.js.map +1 -1
  183. package/out/policy/policy.d.ts +1 -1
  184. package/out/policy/policy.d.ts.map +1 -1
  185. package/out/policy/registry.d.ts +1 -1
  186. package/out/policy/registry.d.ts.map +1 -1
  187. package/out/resolver/coverage-facts.d.ts +89 -0
  188. package/out/resolver/coverage-facts.d.ts.map +1 -0
  189. package/out/resolver/coverage-facts.js +22 -0
  190. package/out/resolver/coverage-facts.js.map +1 -0
  191. package/out/resolver/index.d.ts +4 -1
  192. package/out/resolver/index.d.ts.map +1 -1
  193. package/out/resolver/index.js +2 -0
  194. package/out/resolver/index.js.map +1 -1
  195. package/out/resolver/lookup-types.d.ts +178 -0
  196. package/out/resolver/lookup-types.d.ts.map +1 -0
  197. package/out/resolver/lookup-types.js +11 -0
  198. package/out/resolver/lookup-types.js.map +1 -0
  199. package/out/resolver/placetype-map.d.ts +71 -0
  200. package/out/resolver/placetype-map.d.ts.map +1 -0
  201. package/out/resolver/placetype-map.js +98 -0
  202. package/out/resolver/placetype-map.js.map +1 -0
  203. package/out/resolver/postcode-format.d.ts +38 -0
  204. package/out/resolver/postcode-format.d.ts.map +1 -0
  205. package/out/resolver/postcode-format.js +83 -0
  206. package/out/resolver/postcode-format.js.map +1 -0
  207. package/out/resolver/referential.d.ts +73 -0
  208. package/out/resolver/referential.d.ts.map +1 -0
  209. package/out/resolver/referential.js +71 -0
  210. package/out/resolver/referential.js.map +1 -0
  211. package/out/resolver/types.d.ts +401 -264
  212. package/out/resolver/types.d.ts.map +1 -1
  213. package/out/resolver/types.js +4 -97
  214. package/out/resolver/types.js.map +1 -1
  215. package/out/resources/git.d.ts +8 -1
  216. package/out/resources/git.d.ts.map +1 -1
  217. package/out/resources/git.js +4 -3
  218. package/out/resources/git.js.map +1 -1
  219. package/out/resources/languages/types.gen.d.ts +1 -1
  220. package/out/resources/languages/types.gen.d.ts.map +1 -1
  221. package/out/resources/languages/types.gen.js +40 -0
  222. package/out/resources/languages/types.gen.js.map +1 -1
  223. package/out/resources/libpostal.d.ts +2 -2
  224. package/out/resources/libpostal.d.ts.map +1 -1
  225. package/out/resources/libpostal.js +2 -2
  226. package/out/resources/libpostal.js.map +1 -1
  227. package/out/resources/whosonfirst/DataSourceCache.d.ts.map +1 -1
  228. package/out/resources/whosonfirst/DataSourceCache.js.map +1 -1
  229. package/out/resources/whosonfirst/PlacetypeDataSource.d.ts +2 -2
  230. package/out/resources/whosonfirst/PlacetypeDataSource.d.ts.map +1 -1
  231. package/out/resources/whosonfirst/PlacetypeDataSource.js +28 -3
  232. package/out/resources/whosonfirst/PlacetypeDataSource.js.map +1 -1
  233. package/out/resources/whosonfirst/loader.d.ts +2 -2
  234. package/out/resources/whosonfirst/loader.d.ts.map +1 -1
  235. package/out/resources/whosonfirst/loader.js.map +1 -1
  236. package/out/resources/whosonfirst/placetypes/Placetype.d.ts.map +1 -1
  237. package/out/resources/whosonfirst/placetypes/Placetype.js +1 -1
  238. package/out/resources/whosonfirst/placetypes/Placetype.js.map +1 -1
  239. package/out/resources/whosonfirst/placetypes/admin.d.ts +9 -1
  240. package/out/resources/whosonfirst/placetypes/admin.d.ts.map +1 -1
  241. package/out/resources/whosonfirst/placetypes/admin.js +1 -1
  242. package/out/resources/whosonfirst/placetypes/admin.js.map +1 -1
  243. package/out/resources/whosonfirst/placetypes/definition.d.ts.map +1 -1
  244. package/out/resources/whosonfirst/placetypes/definition.js +1 -1
  245. package/out/resources/whosonfirst/placetypes/definition.js.map +1 -1
  246. package/out/resources/whosonfirst/placetypes/index.d.ts +1 -0
  247. package/out/resources/whosonfirst/placetypes/index.d.ts.map +1 -1
  248. package/out/resources/whosonfirst/placetypes/index.js +1 -0
  249. package/out/resources/whosonfirst/placetypes/index.js.map +1 -1
  250. package/out/resources/whosonfirst/placetypes/specificity.d.ts +65 -0
  251. package/out/resources/whosonfirst/placetypes/specificity.d.ts.map +1 -0
  252. package/out/resources/whosonfirst/placetypes/specificity.js +101 -0
  253. package/out/resources/whosonfirst/placetypes/specificity.js.map +1 -0
  254. package/out/resources/whosonfirst/sharded-repo.d.ts +28 -0
  255. package/out/resources/whosonfirst/sharded-repo.d.ts.map +1 -1
  256. package/out/resources/whosonfirst/sharded-repo.js +46 -2
  257. package/out/resources/whosonfirst/sharded-repo.js.map +1 -1
  258. package/out/scripting/arguments.d.ts +27 -0
  259. package/out/scripting/arguments.d.ts.map +1 -0
  260. package/out/scripting/arguments.js +32 -0
  261. package/out/scripting/arguments.js.map +1 -0
  262. package/out/scripting/command.d.ts +28 -0
  263. package/out/scripting/command.d.ts.map +1 -0
  264. package/out/scripting/command.js +47 -0
  265. package/out/scripting/command.js.map +1 -0
  266. package/out/scripting/index.js +3 -3
  267. package/out/scripting/index.js.map +1 -1
  268. package/out/scripting/utils/index.d.ts +1 -12
  269. package/out/scripting/utils/index.d.ts.map +1 -1
  270. package/out/scripting/utils/index.js +4 -21
  271. package/out/scripting/utils/index.js.map +1 -1
  272. package/out/tools/download-libpostal-resources.d.ts.map +1 -1
  273. package/out/tools/download-libpostal-resources.js +4 -7
  274. package/out/tools/download-libpostal-resources.js.map +1 -1
  275. package/out/tools/download-ssl-address.d.ts +3 -3
  276. package/out/tools/download-ssl-address.d.ts.map +1 -1
  277. package/out/tools/download-ssl-address.js +26 -13
  278. package/out/tools/download-ssl-address.js.map +1 -1
  279. package/out/tools/generate-language-types.d.ts +16 -1
  280. package/out/tools/generate-language-types.d.ts.map +1 -1
  281. package/out/tools/generate-language-types.js +38 -5
  282. package/out/tools/generate-language-types.js.map +1 -1
  283. package/out/tools/index.d.ts +1 -2
  284. package/out/tools/index.d.ts.map +1 -1
  285. package/out/tools/index.js +1 -2
  286. package/out/tools/index.js.map +1 -1
  287. package/out/utils/data-root.d.ts +27 -7
  288. package/out/utils/data-root.d.ts.map +1 -1
  289. package/out/utils/data-root.js +36 -13
  290. package/out/utils/data-root.js.map +1 -1
  291. package/out/utils/hash.d.ts +2 -3
  292. package/out/utils/hash.d.ts.map +1 -1
  293. package/out/utils/hash.js +3 -3
  294. package/out/utils/hash.js.map +1 -1
  295. package/out/utils/index.d.ts +1 -0
  296. package/out/utils/index.d.ts.map +1 -1
  297. package/out/utils/index.js +1 -0
  298. package/out/utils/index.js.map +1 -1
  299. package/out/utils/jsonl.js +1 -1
  300. package/out/utils/jsonl.js.map +1 -1
  301. package/out/utils/repo.d.ts +8 -0
  302. package/out/utils/repo.d.ts.map +1 -1
  303. package/out/utils/repo.js +10 -1
  304. package/out/utils/repo.js.map +1 -1
  305. package/out/utils/sealed-db.d.ts +11 -0
  306. package/out/utils/sealed-db.d.ts.map +1 -1
  307. package/out/utils/sealed-db.js +28 -1
  308. package/out/utils/sealed-db.js.map +1 -1
  309. package/out/utils/sqlite-rows.d.ts +34 -0
  310. package/out/utils/sqlite-rows.d.ts.map +1 -0
  311. package/out/utils/sqlite-rows.js +37 -0
  312. package/out/utils/sqlite-rows.js.map +1 -0
  313. package/out/utils/stats.d.ts +1 -6
  314. package/out/utils/stats.d.ts.map +1 -1
  315. package/out/utils/stats.js +1 -6
  316. package/out/utils/stats.js.map +1 -1
  317. package/package.json +488 -8
  318. package/pipeline/index.ts +14 -15
  319. package/pipeline/runtime-pipeline.ts +48 -184
  320. package/pipeline/span-proposer.ts +1 -1
  321. package/pipeline/types.ts +143 -16
  322. package/policy/defaults.ts +1 -1
  323. package/policy/policy.ts +1 -1
  324. package/policy/registry.ts +1 -1
  325. package/resolver/coverage-facts.ts +102 -0
  326. package/resolver/index.ts +18 -0
  327. package/resolver/lookup-types.ts +174 -0
  328. package/resolver/placetype-map.ts +122 -0
  329. package/resolver/postcode-format.ts +89 -0
  330. package/resolver/referential.ts +84 -0
  331. package/resolver/types.ts +423 -315
  332. package/resources/git.ts +18 -4
  333. package/resources/languages/types.gen.ts +60 -0
  334. package/resources/libpostal.ts +4 -3
  335. package/resources/whosonfirst/DataSourceCache.ts +2 -1
  336. package/resources/whosonfirst/PlacetypeDataSource.ts +36 -11
  337. package/resources/whosonfirst/loader.ts +3 -2
  338. package/resources/whosonfirst/placetypes/Placetype.ts +2 -1
  339. package/resources/whosonfirst/placetypes/admin.ts +11 -2
  340. package/resources/whosonfirst/placetypes/definition.ts +4 -3
  341. package/resources/whosonfirst/placetypes/index.ts +1 -0
  342. package/resources/whosonfirst/placetypes/specificity.ts +114 -0
  343. package/resources/whosonfirst/sharded-repo.ts +55 -2
  344. package/scripting/arguments.ts +38 -0
  345. package/scripting/command.ts +55 -0
  346. package/scripting/index.ts +3 -3
  347. package/scripting/utils/index.ts +5 -23
  348. package/tools/download-libpostal-resources.ts +4 -7
  349. package/tools/download-ssl-address.ts +28 -12
  350. package/tools/generate-language-types.ts +39 -5
  351. package/tools/index.ts +1 -2
  352. package/utils/data-root.ts +40 -14
  353. package/utils/hash.ts +5 -4
  354. package/utils/index.ts +1 -0
  355. package/utils/jsonl.ts +1 -1
  356. package/utils/repo.ts +12 -1
  357. package/utils/sealed-db.ts +30 -1
  358. package/utils/sqlite-rows.ts +40 -0
  359. package/utils/stats.ts +1 -6
  360. package/data/libpostal/dictionaries/all/chains.txt +0 -504
  361. package/kysley/adapter.ts +0 -29
  362. package/out/kysley/adapter.d.ts +0 -13
  363. package/out/kysley/adapter.d.ts.map +0 -1
  364. package/out/kysley/adapter.js +0 -25
  365. package/out/kysley/adapter.js.map +0 -1
  366. package/out/pipeline/reconcile-lookups.d.ts +0 -39
  367. package/out/pipeline/reconcile-lookups.d.ts.map +0 -1
  368. package/out/pipeline/reconcile-lookups.js +0 -88
  369. package/out/pipeline/reconcile-lookups.js.map +0 -1
  370. package/out/pipeline/reconcile.d.ts +0 -153
  371. package/out/pipeline/reconcile.d.ts.map +0 -1
  372. package/out/pipeline/reconcile.js +0 -388
  373. package/out/pipeline/reconcile.js.map +0 -1
  374. package/out/pipeline/span-logit-aggregation.d.ts +0 -58
  375. package/out/pipeline/span-logit-aggregation.d.ts.map +0 -1
  376. package/out/pipeline/span-logit-aggregation.js +0 -117
  377. package/out/pipeline/span-logit-aggregation.js.map +0 -1
  378. package/pipeline/reconcile-lookups.ts +0 -111
  379. package/pipeline/reconcile.ts +0 -583
  380. package/pipeline/span-logit-aggregation.ts +0 -156
@@ -10,7 +10,7 @@
10
10
  * per-locale overrides at startup. Look-up and `apply` are pure.
11
11
  */
12
12
 
13
- import type { ClassificationProposal, ComponentTag } from "@mailwoman/core/types"
13
+ import type { ClassificationProposal, ComponentTag } from "#types"
14
14
 
15
15
  import { buildDefaultPolicies, defaultPolicyFor } from "./defaults.ts"
16
16
  import type { ClassifierPolicy, PolicyMode, PolicyRegistry } from "./policy.ts"
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * What a loaded gazetteer artifact declares about its own country coverage, and the one derivation the
7
+ * reader and the build share. A country ABSENT from a coverage map was never measured — never measured and
8
+ * failed; the failed case is present with `hardFilterSafe: false`.
9
+ */
10
+
11
+ /**
12
+ * One country's measured hard-filter coverage fact, as recorded at a promote gate. Facts about the gazetteer artifact
13
+ * live IN the artifact (the `country_coverage` table the gazetteer build emits) — code constants are only the fallback
14
+ * for artifacts that predate the manifest.
15
+ *
16
+ * Meaning-of-zero discipline: a country ABSENT from the coverage map was never measured — never "measured and failed".
17
+ * A measured-and-failed country is PRESENT with `hardFilterSafe: false` (e.g. FI at 69.5% hard-resolve), so the
18
+ * negative result is a first-class record, distinguishable from ignorance.
19
+ */
20
+ export interface CountryCoverageFact {
21
+ /**
22
+ * ISO 3166-1 alpha-2, uppercase.
23
+ */
24
+ country: string
25
+ /**
26
+ * The promote-gate VERDICT: hard-filtering this country is a pure win (a hard-filter miss is almost always a genuine
27
+ * non-match, not a coverage gap). Stored as a verdict — not re-derived from `hardResolveRate` at read time — because
28
+ * the gate is a judgment over a panel, not a pure rate function (CA cleared at the #928 promote on the
29
+ * postcode-format-prior rationale despite a sub-95% panel resolve rate).
30
+ */
31
+ hardFilterSafe: boolean
32
+ /**
33
+ * Measured hard-resolve rate (0..1) on the panel named in `source`, when the receipt recorded one.
34
+ */
35
+ hardResolveRate?: number
36
+ /**
37
+ * Panel size behind `hardResolveRate`, when recorded.
38
+ */
39
+ sampleSize?: number
40
+ /**
41
+ * ISO-8601 date of the measurement / promote gate.
42
+ */
43
+ measuredAt: string
44
+ /**
45
+ * The receipt: which panel/gate produced this row (issue + date, human-readable).
46
+ */
47
+ source: string
48
+ }
49
+
50
+ /**
51
+ * One country's coarse guard-B bounding box, as carried by the gazetteer artifact's `country_bbox` table.
52
+ */
53
+ export interface CountryBBoxFact {
54
+ /**
55
+ * ISO 3166-1 alpha-2, uppercase.
56
+ */
57
+ country: string
58
+ latMin: number
59
+ latMax: number
60
+ lonMin: number
61
+ lonMax: number
62
+ /**
63
+ * Provenance of the box (harness + date).
64
+ */
65
+ source: string
66
+ }
67
+
68
+ /**
69
+ * Facts a loaded gazetteer artifact declares about itself — read from the artifact's own manifest tables at open time,
70
+ * carried on the {@link ResolverBackend}/{@link Resolver} handle so consumers read the facts from the artifact they are
71
+ * actually resolving against. `undefined` on the handle = the artifact predates the manifest → consumers fall back to
72
+ * the code constants (byte-identical legacy behavior).
73
+ */
74
+ export interface GazetteerArtifactCoverage {
75
+ /**
76
+ * Country → measured coverage fact. ABSENCE = never measured (meaning-of-zero), never "failed".
77
+ */
78
+ countryCoverage: ReadonlyMap<string, CountryCoverageFact>
79
+ /**
80
+ * Country → guard-B bbox. ABSENCE = no box → the plausibility guard fails open for that country.
81
+ */
82
+ countryBBoxes: ReadonlyMap<string, CountryBBoxFact>
83
+ /**
84
+ * Derived at load: the countries whose fact says `hardFilterSafe` — the artifact's hard-country safelist.
85
+ */
86
+ hardCountrySafelist: ReadonlySet<string>
87
+ }
88
+
89
+ /**
90
+ * Derive the hard-country safelist from coverage facts — the ONE derivation both the reader and the build share.
91
+ */
92
+ export function hardCountrySafelistFromCoverage(facts: Iterable<CountryCoverageFact>): ReadonlySet<string> {
93
+ const out = new Set<string>()
94
+
95
+ for (const fact of facts) {
96
+ if (fact.hardFilterSafe) {
97
+ out.add(fact.country.toUpperCase())
98
+ }
99
+ }
100
+
101
+ return out
102
+ }
package/resolver/index.ts CHANGED
@@ -11,6 +11,18 @@
11
11
  * `@mailwoman/codex`.
12
12
  */
13
13
 
14
+ export {
15
+ compareReferential,
16
+ REFERENTIAL_LOG2_SCALE,
17
+ REFERENTIAL_POPULATION_DIVISOR,
18
+ REFERENTIAL_SATURATION_POPULATION,
19
+ referentialFromPopulation,
20
+ } from "./referential.ts"
21
+
22
+ export type { ReferentiallyRankable } from "./referential.ts"
23
+
24
+ export { countriesFromPostcodeFormat, countryFromPostcodeFormat, POSTCODE_FORMAT_COUNTRY } from "./postcode-format.ts"
25
+
14
26
  export {
15
27
  DEFAULT_PLACETYPE_MAP,
16
28
  PLACETYPE_FILTER_GROUPS,
@@ -23,6 +35,7 @@ export type {
23
35
  AddressPointHit,
24
36
  AddressPointLookup,
25
37
  Ancestor,
38
+ BackendCapabilityGap,
26
39
  CoincidentLocality,
27
40
  CountryBBoxFact,
28
41
  CountryCoverageFact,
@@ -30,6 +43,11 @@ export type {
30
43
  InterpolatedPointHit,
31
44
  InterpolationLookup,
32
45
  PlacetypeMap,
46
+ PostcodePrefixAncestor,
47
+ PostcodePrefixIndexLike,
48
+ PostcodePrefixNode,
49
+ ResolveCandidateTrace,
50
+ ResolveNodeTrace,
33
51
  ResolveOpts,
34
52
  ResolvedPlace,
35
53
  Resolver,
@@ -0,0 +1,174 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The SYNCHRONOUS street-tier lookup contracts the resolution ladder is built on — address point,
7
+ * interpolation, street centroid, postcode prefix. Synchronous by design: `@mailwoman/neural` calls into this
8
+ * ladder, so turning one member async forces every implementer and both package boundaries async with it.
9
+ */
10
+
11
+ /**
12
+ * One exact address-point hit (#476): a real situs coordinate for `(street, number)` within a postcode/locality scope —
13
+ * the street-level tier in front of admin-centroid resolution.
14
+ */
15
+ export interface AddressPointHit {
16
+ lat: number
17
+ lon: number
18
+ /**
19
+ * Provenance, e.g. `"overture:NAD"`.
20
+ */
21
+ source: string
22
+ /**
23
+ * Pinned data release the point came from, e.g. `"2026-05-20.0"`.
24
+ */
25
+ release: string
26
+ /**
27
+ * The point's OWN scope tags, when the shard row carries them — the register's locality (normalized key form) and
28
+ * postcode. A rooftop answer can then be DECORATED with the commune/postcode the register attests, which a query that
29
+ * never named them cannot supply. Optional: not every source carries both, and existing readers/consumers predate the
30
+ * fields.
31
+ */
32
+ localityNorm?: string
33
+ postcode?: string
34
+ }
35
+
36
+ /**
37
+ * Street-level exact-point lookup (#476). Implementations own their normalization — both the shard build and this
38
+ * lookup must apply the SAME normalizer (see `resolver-wof-sqlite/street-normalize.ts`). Core depends only on this
39
+ * contract.
40
+ */
41
+ export interface AddressPointLookup {
42
+ find(query: {
43
+ street: string
44
+ number: string
45
+ postcode?: string
46
+ locality?: string
47
+ /**
48
+ * Optional bbox scope (`minLat`/`maxLat`/`minLon`/`maxLon`), tried AFTER postcode/locality. For shards whose points
49
+ * carry no postcode/locality of their own (OSM addr nodes often don't) but DO carry a coordinate — the resolved
50
+ * locality's bounding box scopes the `(street, number)` probe instead. US situs never passes it (byte-stable).
51
+ */
52
+ bbox?: { minLat: number; maxLat: number; minLon: number; maxLon: number }
53
+ }): AddressPointHit | null
54
+ }
55
+
56
+ /**
57
+ * One interpolated coordinate estimate (#483) — NEVER an exact situs point (`uncertaintyM` prices the estimate
58
+ * honestly). Structural mirror of `InterpolatedHit` in `resolver-wof-sqlite/interpolation.ts`; keep this a SUBSET of
59
+ * that shape so the concrete `StreetInterpolator`/`AddressPointInterpolator` satisfy {@link InterpolationLookup} with no
60
+ * adapter (the {@link AddressPointHit} precedent).
61
+ */
62
+ export interface InterpolatedPointHit {
63
+ lat: number
64
+ lon: number
65
+ interpolated: true
66
+ /**
67
+ * `address_point` = bracketed between real neighbor points; `tiger_range` = linear within a segment range.
68
+ */
69
+ method: "address_point" | "tiger_range"
70
+ /**
71
+ * False when only the opposite side's range contained the number (right block, wrong side).
72
+ */
73
+ parityMatched?: boolean
74
+ /**
75
+ * `both` = neighbors bracketed it; `single` = one-sided extrapolation (larger uncertainty).
76
+ */
77
+ bracket?: "both" | "single"
78
+ /**
79
+ * Honest uncertainty radius in METERS (half the matched segment length).
80
+ */
81
+ uncertaintyM: number
82
+ source: string
83
+ release: string
84
+ }
85
+
86
+ /**
87
+ * House-number interpolation lookup (#483). Like {@link AddressPointLookup}, implementations own their normalization
88
+ * (the shared `resolver-wof-sqlite/street-normalize.ts`); core depends only on this contract. Postcode-scoped; without
89
+ * a postcode the tier answers only when the covering ranges agree on ONE postcode — `near` (the resolved locality's
90
+ * coordinate) lets an implementation break a multi-postcode tie by segment proximity instead of abstaining (the
91
+ * Brooklyn-vs-Great-Neck namesake class). Optional and advisory: implementations may ignore it.
92
+ */
93
+ export interface InterpolationLookup {
94
+ find(query: {
95
+ street: string
96
+ number: string
97
+ postcode?: string
98
+ near?: { lat: number; lon: number }
99
+ }): InterpolatedPointHit | null
100
+ /**
101
+ * The ARTIFACT's own conformal radius multiplier for `uncertaintyM` (#374), read from the shard's
102
+ * `interp_calibration` metadata table at open time (the pair-index δ/transitionBeta header precedent): the multiplier
103
+ * is a property of the calibration set the artifact was built against, so it ships in the artifact, not in caller
104
+ * code. The resolver applies it as the DEFAULT whenever `ResolveOpts.interpolationRadiusCalibration` is absent.
105
+ * `undefined` (or an implementation without the property) = the artifact carries none — shards built before the
106
+ * metadata table existed; behavior is then exactly the pre-artifact ladder (caller-supplied factor or raw).
107
+ * Implementations must read this at OPEN time (constructor/factory), never per-lookup — `find()` is synchronous by
108
+ * design.
109
+ */
110
+ readonly radiusCalibration?: number
111
+ }
112
+
113
+ /**
114
+ * One street-CENTROID hit (#1042) — the street-level tier BELOW the exact address-point tier and ABOVE admin-centroid
115
+ * resolution. A street's centroid + an honest extent-derived radius, for a street-only query (no house number) that an
116
+ * address-point tier cannot serve by definition. Derived from a national register's rooftop points
117
+ * (`street-centroids-<cc>.db`, a `GROUP BY street` roll-up). `uncertaintyM` prices the coarseness (half the street's
118
+ * bbox diagonal) so a consumer never mistakes it for a rooftop.
119
+ */
120
+ export interface StreetCentroidHit {
121
+ lat: number
122
+ lon: number
123
+ /**
124
+ * Honest coarse radius in METERS — half the street's bounding-box diagonal.
125
+ */
126
+ uncertaintyM: number
127
+ source: string
128
+ release: string
129
+ }
130
+
131
+ /**
132
+ * Street-centroid lookup (#1042). Like {@link AddressPointLookup}, implementations own their normalization (the shared
133
+ * `resolver-wof-sqlite/street-normalize.ts`); core depends only on this contract. Scoped by `postcode` (preferred) or
134
+ * `locality` (the base commune) — NO house number: this is the street-only tier.
135
+ */
136
+ export interface StreetCentroidLookup {
137
+ find(query: { street: string; postcode?: string; locality?: string }): StreetCentroidHit | null
138
+ }
139
+
140
+ /**
141
+ * One admin-ancestry entry a PFX1 node asserts (coarsest-first: country → constituent country → district).
142
+ */
143
+ export interface PostcodePrefixAncestor {
144
+ placetype: string
145
+ wofID: number
146
+ name: string
147
+ }
148
+
149
+ /**
150
+ * A PFX1 postcode-prefix node — the partial-code prior's payload ({@link ResolveOpts.postcodePrefixPrior}, #31
151
+ * Mechanism 3). The coordinate is OPTIONAL and its absence is meaningful: an ancestry-only tier (NI's 80 BT districts)
152
+ * carries `ancestors` and no `lat`/`lon` — representable as absence, never as `0,0` (the meaning-of-zero rule).
153
+ * `radiusP95Km` is mandatory whenever a coordinate is present (M-3's receipt: a 1-digit US band and a GB outward code
154
+ * are both "a prefix with a centroid" and differ by 200×).
155
+ */
156
+ export interface PostcodePrefixNode {
157
+ prefix: string
158
+ ancestors: readonly PostcodePrefixAncestor[]
159
+ lat?: number
160
+ lon?: number
161
+ radiusP95Km?: number
162
+ unitCount: number
163
+ }
164
+
165
+ /**
166
+ * The PFX1 index the resolver probes — minimal, structural (`probe` + optional `country`), so `@mailwoman/resolver`
167
+ * consumes an index built in `@mailwoman/neural` without depending on it (B3-5: the partial-code prior touches zero
168
+ * model inputs). `country` is the ISO-3166 alpha-2 the index was built for (upper-case); the resolver only probes an
169
+ * index whose country matches the query's country scope.
170
+ */
171
+ export interface PostcodePrefixIndexLike {
172
+ probe(prefix: string): PostcodePrefixNode | null
173
+ readonly country?: string
174
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Component-tag → resolver-placetype mapping, the equivalence groups a query expands into, and the test
7
+ * that tells a widened match from an exact one.
8
+ */
9
+
10
+ import type { ComponentTag } from "../decoder/types.ts"
11
+
12
+ /**
13
+ * Mapping from mailwoman's address-component tags to the resolver's placetype taxonomy.
14
+ *
15
+ * PARTIAL on purpose: a tag absent from the map is NOT queried, and the resolver pass leaves its classifier attribution
16
+ * untouched. Omission is therefore a routing decision, not an oversight.
17
+ */
18
+ export type PlacetypeMap = Partial<Record<ComponentTag, string>>
19
+
20
+ /**
21
+ * The map used when a backend does not supply its own.
22
+ *
23
+ * `street` and `house_number` are absent because WOF admin has no rows for them — they resolve through the situs shards
24
+ * instead, which are keyed by street, not by placetype. Locale-specific tiers (JP-style prefecture subdivisions) are
25
+ * absent for the same reason: a different shard entirely.
26
+ */
27
+ export const DEFAULT_PLACETYPE_MAP: PlacetypeMap = {
28
+ country: "country",
29
+ region: "region",
30
+ locality: "locality",
31
+ dependent_locality: "locality",
32
+ subregion: "county",
33
+ // `postcode` (mailwoman tag) maps to WOF's `postalcode` placetype. Resolves only when the
34
+ // backend has the postcode shard available — `WOFSQLitePlaceLookup` auto-routes `postalcode`
35
+ // queries to a `postalcode_us` (or similarly-named) shard, falling back to main if absent.
36
+ postcode: "postalcode",
37
+ }
38
+
39
+ /**
40
+ * Placetype-equivalence groups for lookup FILTERING. WOF splits a single addressing tier across several placetypes, but
41
+ * an address's span can name ANY of them. A backend that filters to the one "obvious" placetype makes the equivalents
42
+ * unreachable, so a fuzzy same-name place in the wrong tier wins instead.
43
+ *
44
+ * Three tiers are affected (the value of each entry is the set the SQL filter should accept; the FIRST entry is the
45
+ * canonical/requested type, which shard routing keys off):
46
+ *
47
+ * - **`locality`** — `locality` (most cities), `borough` (Brooklyn, the Paris arrondissements, the London boroughs), and
48
+ * `localadmin` (FR communes, US towns/townships in New England). Without the group, Brooklyn-the-borough (pop 2.5M)
49
+ * was unreachable and the fuzzy "Brooklyn Park, MN" won.
50
+ * - **`region`** — `region` + `macroregion` (#718). WOF does NOT model every country's top-level civil division as
51
+ * `region`: Italian regions (Lombardia, Veneto, Toscana…) are `macroregion` (their PROVINCES are `region`), and the
52
+ * post-2016 French régions (Île-de-France) are `macroregion` too. An address's `region` span names exactly those, so
53
+ * a `region`-only filter resolved them to NOTHING (confirmed against the IT/FR eval rows). US states / DE
54
+ * Bundesländer / ES provincias are genuine `region`, so the EXACT-type match is preferred in ranking (see the
55
+ * resolve.ts fallback-quality annotation) — the macro is the recall safety net, not a demotion.
56
+ * - **`county`** — `county` + `macrocounty` (#718). The `subregion` ComponentTag maps to `county` via
57
+ * {@link DEFAULT_PLACETYPE_MAP}; WOF carries `macrocounty` for FR départements-grouping / DE / GB tiers above the
58
+ * county. Proactive (no eval row exercises `subregion` today) but symmetric with `region` — biasing to inclusion,
59
+ * since a missed resolution costs more than a too-broad candidate (which is QA-visible). Same exact-type preference
60
+ * applies.
61
+ *
62
+ * This table is the single source of truth for that expansion, shared by every lookup backend
63
+ * (`@mailwoman/core/resolver-wof-sqlite`, `@mailwoman/core/resolver-wof-wasm`, and the demo's httpvfs lookup) so the
64
+ * Node and browser resolvers can't drift. Keyed by the REQUESTED placetype. Placetypes without an entry pass through
65
+ * unchanged — an explicit `placetype: "borough"` query stays narrow.
66
+ */
67
+ export const PLACETYPE_FILTER_GROUPS: Readonly<Record<string, readonly string[]>> = {
68
+ locality: ["locality", "borough", "localadmin"],
69
+ region: ["region", "macroregion"],
70
+ county: ["county", "macrocounty"],
71
+ }
72
+
73
+ /**
74
+ * Expand a placetype filter through {@link PLACETYPE_FILTER_GROUPS}, deduplicated and order-preserving (the first entry
75
+ * stays first — shard routing keys off it). `null`/`undefined` (no filter) passes through untouched.
76
+ */
77
+ export function expandPlacetypeFilter(placetypes: null): null
78
+ export function expandPlacetypeFilter(placetypes: readonly string[]): string[]
79
+ export function expandPlacetypeFilter(placetypes: readonly string[] | null): string[] | null
80
+
81
+ export function expandPlacetypeFilter(placetypes: readonly string[] | null): string[] | null {
82
+ if (!placetypes) return null
83
+ const out: string[] = []
84
+
85
+ for (const placetype of placetypes) {
86
+ for (const expanded of PLACETYPE_FILTER_GROUPS[placetype] ?? [placetype]) {
87
+ if (!out.includes(expanded)) {
88
+ out.push(expanded)
89
+ }
90
+ }
91
+ }
92
+
93
+ return out
94
+ }
95
+
96
+ /**
97
+ * Macro/broader-tier members of {@link PLACETYPE_FILTER_GROUPS} — the recall safety net a query may fall through to
98
+ * when no candidate of the EXACT requested placetype exists (#718). DELIBERATELY scoped to the `macro*` tiers only: the
99
+ * `locality` group's `borough`/`localadmin` are genuine peers (Brooklyn-the-borough is a first-class locality answer,
100
+ * #404-class), NOT fallbacks — so they must NOT be deprioritized or annotated. Only `macroregion`/`macrocounty` are a
101
+ * broader admin tier standing in for a true `region`/`county`.
102
+ */
103
+ const MACRO_FALLBACK_PLACETYPES: ReadonlySet<string> = new Set(["macroregion", "macrocounty"])
104
+
105
+ /**
106
+ * Did `candidatePlacetype` resolve `requestedPlacetype` only via a BROADER admin tier (a macro-type fallback within the
107
+ * {@link PLACETYPE_FILTER_GROUPS} expansion), rather than the exact type (#718)?
108
+ *
109
+ * `region` → `region` is exact (false); `region` → `macroregion` is a fallback (true). Scoped to the `macro*` tiers
110
+ * (see {@link MACRO_FALLBACK_PLACETYPES}) so the `locality` group's borough/localadmin peers stay exact. The resolver
111
+ * uses this to (a) prefer an exact-type candidate in ranking and (b) annotate `resolutionQuality: "fallback"` when only
112
+ * a macro-type matched. A placetype outside the requested group, or any non-macro member, is treated as exact (false).
113
+ */
114
+ export function isPlacetypeFallback(requestedPlacetype: string, candidatePlacetype: string): boolean {
115
+ const group = PLACETYPE_FILTER_GROUPS[requestedPlacetype]
116
+
117
+ if (!group) return false
118
+
119
+ if (candidatePlacetype === requestedPlacetype) return false
120
+
121
+ return MACRO_FALLBACK_PLACETYPES.has(candidatePlacetype) && group.includes(candidatePlacetype)
122
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Postcode FORMAT → country evidence, pure and platform-free — the #928 unforgeable singles and the
7
+ * #1589 bare-context implied set. Lifted out of `mailwoman/geocode-core` so `@mailwoman/resolver`
8
+ * (which runs in the browser via the demo cascade) can derive the implied set itself instead of
9
+ * depending on a CLI-side caller to thread it — the 2026-08-11 staged-repoint e2e caught exactly
10
+ * that gap (`100 00` resolved in Node and not in the browser, same artifact). `geocode-core`
11
+ * re-exports everything here, so its consumers and tests are unmoved.
12
+ */
13
+
14
+ /**
15
+ * #928: distinctive postcode FORMATS that unambiguously indicate a country — a stronger country signal than the
16
+ * language-based coarse placer, which conflates GB/US (both carry English street patterns) and mis-routes GB addresses
17
+ * to US namesakes (`London E4 9AZ` → London, Ohio) at 0.94–0.96 confidence. The format is unforgeable across these
18
+ * countries: the GB pattern (letters-first) never matches a US ZIP or an NL `\d{4} [A-Z]{2}` code. Extend ONLY with
19
+ * formats validated as non-overlapping. Feeds the `postcodeCountryPrior` lever (gated, default-off pending its gate).
20
+ */
21
+ export const POSTCODE_FORMAT_COUNTRY: ReadonlyArray<{ readonly re: RegExp; readonly country: string }> = [
22
+ // GB `E4 9AZ` — letters-first, ends `\d[A-Z]{2}`. Never matches a US ZIP / NL / FR / CA code.
23
+ { re: /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i, country: "GB" },
24
+ // CA `K2P 1L4` — `A#A #A#`, ends `\d[A-Z]\d` (distinct from GB's `\d[A-Z]{2}`). The placer conflates CA
25
+ // with US (English) / FR (Québec) at 0.9–1.0 confidence, same failure as GB; the format is unambiguous.
26
+ { re: /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i, country: "CA" },
27
+ // IE Eircode `D02 AF30` — routing key (letter + 2 digits, or the D6W special) + a 4-alnum unique part.
28
+ // The 4-char unique part is what separates it from GB's 3-char `\d[A-Z]{2}` inward (no real-code
29
+ // overlap; Belfast `BT1 5GS` stays GB — Northern Ireland uses GB postcodes). The placer mis-routes IE
30
+ // 5/5 (Cork→US 0.99, Drogheda→US 1.00) — the same conflation class as GB/CA.
31
+ { re: /^(?:[A-Z]\d{2}|D6W)\s?[A-Z\d]{4}$/i, country: "IE" },
32
+ // NL PC6 is DELIBERATELY ABSENT: `\d{4} [A-Z]{2}` is forgeable in parse context — a US
33
+ // house-number + directional fragment (`1234 NE`, `8990 SW`) matches it exactly, and this table
34
+ // feeds recognizeBarePostcode, which must never touch a street name. NL lives in
35
+ // countriesFromPostcodeFormat instead, whose consumers gate on a bare-postcode TREE.
36
+ ]
37
+
38
+ /**
39
+ * The country a parsed postcode's FORMAT implies, or null. See {@link POSTCODE_FORMAT_COUNTRY}.
40
+ */
41
+ export function countryFromPostcodeFormat(postcode: string | undefined): string | null {
42
+ const p = postcode?.trim()
43
+
44
+ if (!p) return null
45
+
46
+ for (const { re, country } of POSTCODE_FORMAT_COUNTRY) if (re.test(p)) return country
47
+
48
+ return null
49
+ }
50
+
51
+ /**
52
+ * Spaced `NNN NN` — the CZ/SK/SE/GR shared postcode space (#1589's `100 00`). Unlike the
53
+ * {@link POSTCODE_FORMAT_COUNTRY} singles, this shape implies a SET: no single country owns it, so it can gate a
54
+ * locale-inferred scope but never name one country outright.
55
+ */
56
+ const SHARED_NNN_NN = /^\d{3} \d{2}$/
57
+
58
+ /**
59
+ * NL PC6 (`1012 LG`) — digits-first then exactly two letters. NL-unique as a POSTCODE shape (GB/CA/IE are
60
+ * letters-first; the digit-only families carry no letters), but too forgeable for {@link POSTCODE_FORMAT_COUNTRY}: a US
61
+ * house-number + directional fragment (`1234 NE`) matches it, so it must never feed recognizeBarePostcode. It belongs
62
+ * only here, where every consumer gates on a tree that IS a bare postcode.
63
+ */
64
+ const NL_PC6 = /^\d{4}\s?[A-Z]{2}$/i
65
+
66
+ /**
67
+ * EVERY country a parsed postcode's FORMAT is consistent with — the singles table, the NL PC6 shape, and the shared
68
+ * `NNN NN` family. Empty when the shape implies nothing (a bare 5-digit reads US/FR/DE and more; that family stays with
69
+ * the locale prior on purpose — the `75008` contract).
70
+ *
71
+ * Unlike {@link countryFromPostcodeFormat}, this is NOT an unforgeable-in-any-context claim: consumers (the resolver's
72
+ * implied-set probe, the CLI's withheld-scope guard) apply it only to a tree that is a bare postcode, where the
73
+ * street-fragment collision the singles table must exclude cannot arise.
74
+ */
75
+ export function countriesFromPostcodeFormat(postcode: string | undefined): readonly string[] {
76
+ const p = postcode?.trim()
77
+
78
+ if (!p) return []
79
+
80
+ const single = countryFromPostcodeFormat(p)
81
+
82
+ if (single) return [single]
83
+
84
+ if (NL_PC6.test(p)) return ["NL"]
85
+
86
+ if (SHARED_NNN_NN.test(p)) return ["CZ", "SK", "SE", "GR"]
87
+
88
+ return []
89
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The REFERENTIAL score and its comparator — the ranking half of the two-score split (ROAD_TO_V9 §2
7
+ * R1, ratified 2026-08-06).
8
+ *
9
+ * THE POLICY. "The importance of a knowledge-base article is not the probability that this is the
10
+ * place the user means." A geocoder ranks by REFERENTIAL likelihood; encyclopedic importance is
11
+ * carried as data and is never a ranking key. Saint-Denis is the canonical case: the
12
+ * Seine-Saint-Denis suburb (pop 96,128) carries encyclopedic 0.1173 and the Aude hamlet (pop 418)
13
+ * carries 0.5683, so encyclopedic ranking inverts the answer every user means, by 4.8x.
14
+ *
15
+ * WHY THIS LIVES IN `@mailwoman/core`. Three packages need the same number and must not drift:
16
+ * `@mailwoman/resolver` (backend-agnostic — it cannot import a backend),
17
+ * `@mailwoman/resolver-wof-sqlite` (the gazetteer schema + both lookups), and the FST builder that
18
+ * stamps referential scores into the decode-bias artifact. `core/resolver/types.ts` already owns the
19
+ * `ResolvedPlace` contract these three share, so the score that contract carries belongs beside it.
20
+ */
21
+
22
+ /**
23
+ * Population divisor in {@link referentialFromPopulation}. A 1,000-person place scores `log2(2)/14` — the curve starts
24
+ * counting at the scale where WOF actually records population.
25
+ */
26
+ export const REFERENTIAL_POPULATION_DIVISOR = 1000
27
+
28
+ /**
29
+ * Log2 denominator in {@link referentialFromPopulation}. 14 puts the ceiling at `2^14 · 1000` people.
30
+ */
31
+ export const REFERENTIAL_LOG2_SCALE = 14
32
+
33
+ /**
34
+ * The population at which {@link referentialFromPopulation} saturates at exactly 1.0: `(2^14 − 1) · 1000` = 16,383,000.
35
+ *
36
+ * Load-bearing, not trivia. Above this the score CLAMPS, so two megacities that population would order (Tokyo ~37 M vs
37
+ * Delhi ~33 M) tie at 1.0. Any ranking keyed on referential alone must break that tie on raw population to stay
38
+ * ordering-identical to the population-first path — which is exactly what {@link compareReferential} does, and why it
39
+ * exists rather than a bare subtraction at each call site.
40
+ */
41
+ export const REFERENTIAL_SATURATION_POPULATION = (2 ** REFERENTIAL_LOG2_SCALE - 1) * REFERENTIAL_POPULATION_DIVISOR
42
+
43
+ /**
44
+ * Population → referential likelihood in [0, 1].
45
+ *
46
+ * `min(1, log2(1 + pop/1000) / 14)` — the formula the FST builder has used for its population fallback since the FST
47
+ * shipped, and the one `gazetteer importance` used for its fallback rows. It is defined ONCE here so the decode-bias
48
+ * artifact's values, the gazetteer's `referential` column, and the resolver's ranking key are the same number by
49
+ * construction rather than by three matching copies.
50
+ *
51
+ * MEANING OF ZERO: an absent population row and a recorded population of 0 both return 0, and 0 means "no population
52
+ * evidence" — the ranking treats it as no boost, never a penalty. WOF carries population for roughly 15% of localities,
53
+ * so absence is the common case and must stay cheap.
54
+ */
55
+ export function referentialFromPopulation(population: number | null | undefined): number {
56
+ if (population === null || population === undefined || population <= 0) return 0
57
+
58
+ return Math.min(1, Math.log2(1 + population / REFERENTIAL_POPULATION_DIVISOR) / REFERENTIAL_LOG2_SCALE)
59
+ }
60
+
61
+ /**
62
+ * A thing that can be ranked referentially. Both fields optional — a candidate with neither sorts last, which is the
63
+ * same place a candidate with no population has always sorted.
64
+ */
65
+ export interface ReferentiallyRankable {
66
+ referential?: number
67
+ population?: number
68
+ }
69
+
70
+ /**
71
+ * The ranking comparator: referential DESC, raw population DESC as the tiebreak. Negative when `a` outranks `b`, so it
72
+ * drops straight into `Array#sort`.
73
+ *
74
+ * The population tiebreak is not a hedge — it is what makes "rank by referential" and "rank by population" the SAME
75
+ * ORDER on every input, because {@link referentialFromPopulation} is strictly increasing below
76
+ * {@link REFERENTIAL_SATURATION_POPULATION} and constant above it. Without the tiebreak this comparator would silently
77
+ * re-order the world's largest cities: a real behavior change, and the one the D-rule would catch.
78
+ *
79
+ * Encyclopedic importance is deliberately not a parameter. Ranking by it is the thing §2 forbids, and a comparator that
80
+ * cannot express it is a stronger guarantee than a comment asking nobody to.
81
+ */
82
+ export function compareReferential(a: ReferentiallyRankable, b: ReferentiallyRankable): number {
83
+ return (b.referential ?? 0) - (a.referential ?? 0) || (b.population ?? 0) - (a.population ?? 0)
84
+ }