@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
@@ -1,583 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * `reconcileSpans` — Stage 5 joint-decoding path.
7
- *
8
- * Today's runtime Stage 5 (in `runtime-pipeline.ts`) just keeps the classifier-emitted spans in
9
- * start order. That fallback covers callers who don't yet have top-k inputs from Stages 2.7 / 3 /
10
- * 6. This file is the **opt-in** joint-decoding path that turns Stage 5 from a sort into a
11
- * reconciler: pick the parse tree that maximizes
12
- *
13
- * ```
14
- * ∏ phrase_grouper_confidence
15
- * × classifier_confidence
16
- * × resolver_score
17
- * × concordance_bonus(parent_id chain)
18
- * ```
19
- *
20
- * Closes the kryptonite catalogue (`NY-NY Steakhouse, Houston, TX`, `Paris, Texas`, `Saint
21
- * Petersburg, FL`, `Buffalo Buffalo`) where today's argmax-per-stage produces internally-
22
- * inconsistent parses that the layer above can't repair.
23
- *
24
- * The implementation is a beam search over candidate slots — each slot is one `(phrase_span,
25
- * classifier_tag, resolver_place)` triple. Three knobs prune the cross product: `kSpan` (de-duped
26
- * phrase span proposals), `kTag` (classifier tag interpretations per span), and `kResolver`
27
- * (resolver candidates per `(span, tag)`). Concordance scoring is incremental — when a slot is
28
- * added, we check the new place's `parent_id` chain against the running admin assignment and
29
- * adjust the running score before pruning.
30
- *
31
- * The Thread C-s classifier top-k contract is mocked locally for v0.5.0 (see `ClassifierCandidate`
32
- * below). The real classifier output adapter ships with `@mailwoman/neural` once Thread C-s
33
- * lands; the existing `ResolvedPlace` shape from `@mailwoman/core/resolver` is what backs the
34
- * resolver-candidates axis (parent_id chains live there).
35
- *
36
- * See `docs/articles/plan/phases/PHASE_8_v0_5_0_fresh_slate.md` § D for the v0.5.0 context and
37
- * `docs/articles/concepts/the-knowledge-ladder.md` § Reconcile for the design rationale.
38
- */
39
-
40
- import type { AddressNode, AddressTree, ComponentTag } from "../decoder/types.ts"
41
- import type { ResolvedPlace } from "../resolver/types.ts"
42
- import type { PhraseProposal } from "./types.ts"
43
-
44
- /**
45
- * One classifier interpretation for one input span. Pins the Thread C-s contract for the reconciler — the real
46
- * classifier emits this directly once that thread lands. Until then, tests hand-build a list of these to simulate top-k
47
- * output.
48
- *
49
- * The classifier may emit multiple candidates for the same span (different tag hypotheses); the reconciler treats those
50
- * as the top-k tag axis. Candidates whose `span` does not exactly match a phrase proposal's span are ignored — the
51
- * phrase grouper is the boundary-discovery authority.
52
- */
53
- export interface ClassifierCandidate {
54
- span: { start: number; end: number }
55
- tag: ComponentTag
56
- /**
57
- * Calibrated confidence in [0, 1].
58
- */
59
- score: number
60
- }
61
-
62
- /**
63
- * Resolver lookup for the (span, tag) axis. In production wraps a `ResolverBackend` (Stage 6); in tests an in-memory
64
- * table built per fixture. Returned candidates must already be sorted descending by score — the reconciler takes the
65
- * top `kResolver` as-is.
66
- */
67
- export interface ResolverCandidatesLookup {
68
- candidatesFor(span: { start: number; end: number }, tag: ComponentTag): ReadonlyArray<ResolvedPlace>
69
- }
70
-
71
- /**
72
- * Parent-chain lookup for concordance scoring. WOF SQLite is the source of truth in production (Stage 6); tests pass a
73
- * `Map`-backed mock. Returns the place's ancestors (order unimportant — the reconciler only checks membership).
74
- */
75
- export interface ParentChainLookup {
76
- parentsOf(place: ResolvedPlace): ReadonlyArray<ResolvedPlace>
77
- }
78
-
79
- export interface ReconcileInputs {
80
- /**
81
- * Raw input text — used to materialize `value` strings on the resulting tree.
82
- */
83
- raw: string
84
- phraseProposals: ReadonlyArray<PhraseProposal>
85
- /**
86
- * Sorted descending by score. May be empty (returns an empty tree).
87
- */
88
- classifierTopK: ReadonlyArray<ClassifierCandidate>
89
- resolverCandidates?: ResolverCandidatesLookup
90
- parentChain?: ParentChainLookup
91
- opts?: ReconcileOpts
92
- }
93
-
94
- export interface ReconcileOpts {
95
- /**
96
- * Top-k phrase span proposals retained per (start, end) — default 3.
97
- */
98
- kSpan?: number
99
- /**
100
- * Top-k classifier tag interpretations retained per span — default 3.
101
- */
102
- kTag?: number
103
- /**
104
- * Top-k resolver candidates retained per (span, tag) — default 5.
105
- */
106
- kResolver?: number
107
- /**
108
- * Concordance bonus multiplier. 1.0 weights chain-consistency equal to one classifier-score factor; lower values
109
- * trust the classifier more, higher values trust the gazetteer more. Default 1.0.
110
- */
111
- concordanceWeight?: number
112
- /**
113
- * Beam width during search — default 16.
114
- */
115
- beamWidth?: number
116
- /**
117
- * Runner-up parses returned alongside the winner — default 3.
118
- */
119
- runnersUp?: number
120
- }
121
-
122
- export interface ScoreBreakdown {
123
- phrase: number
124
- classifier: number
125
- resolver: number
126
- concordance: number
127
- /**
128
- * Composite multiplicative score in real space, [0, ∞).
129
- */
130
- total: number
131
- }
132
-
133
- export interface ParseTree {
134
- tree: AddressTree
135
- /**
136
- * Softmaxed confidence in [0, 1] over the finalized beam.
137
- */
138
- confidence: number
139
- runnersUp: AddressTree[]
140
- scoreBreakdown: ScoreBreakdown
141
- }
142
-
143
- const DEFAULTS = {
144
- kSpan: 3,
145
- kTag: 3,
146
- kResolver: 5,
147
- concordanceWeight: 1,
148
- beamWidth: 16,
149
- runnersUp: 3,
150
- }
151
-
152
- /**
153
- * Log-space bonus per **word covered** by an accepted slot. Counterweight for the multiplicative penalty inherent in
154
- * "score = ∏ confidences": each factor in [0, 1] strictly lowers the running score, which would otherwise make the
155
- * empty parse always win. With per-word scaling, a slot is worth including when the per-word geometric mean of its
156
- * factors exceeds 1/2.5 = 0.4; below that, the search prefers to skip.
157
- *
158
- * Why per-word and not per-slot: a constant per-slot bonus carries two structural biases, both measured on `"New York
159
- * City"` (2026-06-11):
160
- *
161
- * 1. **Fragments beat coverage.** The per-token logit aggregation gives interior fragments inflated confidence (`City`
162
- * aggregates I-locality mass → locality 0.92 vs 0.60 for the full span), and a per-slot bonus charges nothing for
163
- * leaving the rest of the input unexplained — so the beam picked the bare `locality="City"` (+0.475) over
164
- * `locality="New York City"` (+0.199) and the grouper-audit then promoted the orphaned `York` to a spurious
165
- * `region`.
166
- * 2. **More spans collect more bonuses.** For two interpretations covering the same words, the one split into more slots
167
- * banked one bonus per slot — a fragmentation subsidy.
168
- *
169
- * Scaling the bonus by the slot's word count makes coverage the thing being rewarded: equal-coverage interpretations
170
- * collect equal bonus (the classifier/phrase/resolver factors alone decide), and an interpretation that explains more
171
- * of the input out-scores one that silently drops words unless the extra evidence is weak (per-word factor mean <
172
- * 0.4).
173
- *
174
- * The choice of 2.5 is a tuning constant, not a free parameter we expose — exposing it would invite callers to disable
175
- * inclusion entirely (defeating the purpose of the reconciler) and the sensible range is narrow. Lives here rather than
176
- * `ReconcileOpts` for that reason. Words are whitespace-delimited; scripts without spaces (CJK) count as one word,
177
- * which degrades to the old per-slot behavior rather than misbehaving.
178
- */
179
- const INCLUSION_LOG_BONUS = Math.log(2.5)
180
-
181
- /**
182
- * Admin levels from coarse to fine. Concordance scoring walks pairs (parent_level, child_level) and checks the child's
183
- * parent_id chain for the parent's place id.
184
- */
185
- const ADMIN_LEVELS: ReadonlyArray<ComponentTag> = ["country", "region", "locality", "dependent_locality"]
186
- const ADMIN_LEVEL_SET = new Set<ComponentTag>(ADMIN_LEVELS)
187
-
188
- interface SlotChoice {
189
- span: PhraseProposal["span"]
190
- phraseConf: number
191
- tag: ComponentTag
192
- classifierScore: number
193
- place: ResolvedPlace | null
194
- resolverScore: number
195
- /**
196
- * Whitespace-delimited word count of the span's surface text — scales the inclusion bonus.
197
- */
198
- wordCount: number
199
- }
200
-
201
- interface Beam {
202
- assignments: SlotChoice[]
203
- /**
204
- * Log-space score combining phrase × classifier × resolver × concordance (running).
205
- */
206
- logScore: number
207
- /**
208
- * Cached last-end so we can extend left-to-right without re-scanning assignments.
209
- */
210
- lastEnd: number
211
- }
212
-
213
- /**
214
- * Joint-decode the best parse tree from Stage 2.7 phrase proposals, Stage 3 classifier top-k, and Stage 6 resolver
215
- * candidates. See file header for the scoring formula.
216
- *
217
- * The result tree's roots are the winning `(span, tag, place)` triples in source order. Each node's `placeID`, `lat`,
218
- * `lon` come from the chosen resolver candidate; `confidence` reflects the per- factor score for that slot. `runnersUp`
219
- * are the next-best parses for caller inspection.
220
- *
221
- * Empty `classifierTopK` short-circuits to an empty tree at confidence 0 — no joint decode is possible without tag
222
- * interpretations. Callers without top-k should use the runtime-pipeline fallback (which keeps classifier-emitted spans
223
- * sorted by start).
224
- */
225
- export function reconcileSpans(inputs: ReconcileInputs): ParseTree {
226
- const opts = { ...DEFAULTS, ...inputs.opts }
227
-
228
- const slots = buildSlots(inputs, opts)
229
-
230
- if (!slots.length) {
231
- return emptyParseTree(inputs.raw)
232
- }
233
-
234
- // Beam search over left-to-right slot inclusion. Each slot is either accepted (if non-
235
- // overlapping with the beam's claimed range) or skipped. Beam pruning keeps the top
236
- // `beamWidth` by running log-score.
237
- // oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
238
- const slotsByStart = slots.slice().sort((a, b) => a.span.start - b.span.start)
239
- let beams: Beam[] = [{ assignments: [], logScore: 0, lastEnd: -1 }]
240
-
241
- for (const slot of slotsByStart) {
242
- const next: Beam[] = []
243
-
244
- for (const beam of beams) {
245
- next.push(beam)
246
-
247
- if (slot.span.start >= beam.lastEnd) {
248
- const concordanceDelta = concordanceDeltaFor(beam.assignments, slot, inputs, opts)
249
-
250
- if (concordanceDelta === Number.NEGATIVE_INFINITY) continue
251
-
252
- const slotLog =
253
- logSafe(slot.phraseConf) +
254
- logSafe(slot.classifierScore) +
255
- logSafe(slot.resolverScore) +
256
- concordanceDelta +
257
- INCLUSION_LOG_BONUS * slot.wordCount
258
-
259
- next.push({
260
- assignments: [...beam.assignments, slot],
261
- logScore: beam.logScore + slotLog,
262
- lastEnd: slot.span.end,
263
- })
264
- }
265
- }
266
-
267
- next.sort((a, b) => b.logScore - a.logScore)
268
- beams = next.slice(0, opts.beamWidth)
269
- }
270
-
271
- beams.sort((a, b) => b.logScore - a.logScore)
272
- // Drop the empty beam if there's at least one non-empty competitor (the empty beam scores 0,
273
- // which would otherwise dominate when all real candidates have very low log-scores).
274
- const populated = beams.filter((b) => b.assignments.length > 0)
275
- const ordered = populated.length ? populated : beams
276
-
277
- const top = ordered[0]!
278
- const runners = ordered.slice(1, 1 + opts.runnersUp)
279
-
280
- const trees = ordered.map((b) => buildTree(b, inputs.raw))
281
- const confidences = softmax(ordered.map((b) => b.logScore))
282
-
283
- return {
284
- tree: trees[0]!,
285
- confidence: confidences[0]!,
286
- runnersUp: runners.map((_, i) => trees[i + 1]!),
287
- scoreBreakdown: breakdownFor(top, inputs, opts),
288
- }
289
- }
290
-
291
- /**
292
- * Build the candidate slot set: dedupe phrase proposals by (start, end), join with classifier top-k tag candidates,
293
- * join with resolver places per (span, tag).
294
- *
295
- * Spans with no classifier candidate are dropped — the reconciler cannot tag them. Spans with no resolver candidate
296
- * still survive; the resolver score defaults to 1 (neutral) and the slot's place is null (won't contribute to
297
- * concordance).
298
- */
299
- function buildSlots(inputs: ReconcileInputs, opts: Required<ReconcileOpts>): SlotChoice[] {
300
- const bySpanKey = new Map<string, PhraseProposal>()
301
-
302
- for (const p of inputs.phraseProposals) {
303
- const k = spanKey(p.span.start, p.span.end)
304
- const cur = bySpanKey.get(k)
305
-
306
- if (!cur || p.confidence > cur.confidence) {
307
- bySpanKey.set(k, p)
308
- }
309
- }
310
-
311
- // `kSpan` limits the number of overlapping proposals anchored at each start position — NOT a
312
- // global cap on phrase proposals. Two phrases at different starts (e.g. `Houston` at 18 +
313
- // `TX` at 27) are independent candidates and both must survive `kSpan = 3`. Without this per-
314
- // start grouping, a low-confidence-but-correct proposal (e.g. `Houston` @ 0.65) is dropped
315
- // just because the input has many higher-confidence proposals elsewhere.
316
- const byStart = new Map<number, PhraseProposal[]>()
317
-
318
- for (const p of bySpanKey.values()) {
319
- const arr = byStart.get(p.span.start) ?? []
320
- arr.push(p)
321
- byStart.set(p.span.start, arr)
322
- }
323
-
324
- const spans: PhraseProposal[] = []
325
-
326
- for (const arr of byStart.values()) {
327
- spans.push(...topN(arr, opts.kSpan, (p) => p.confidence))
328
- }
329
-
330
- const tagsBySpan = new Map<string, ClassifierCandidate[]>()
331
-
332
- for (const c of inputs.classifierTopK) {
333
- const k = spanKey(c.span.start, c.span.end)
334
- const arr = tagsBySpan.get(k) ?? []
335
- arr.push(c)
336
- tagsBySpan.set(k, arr)
337
- }
338
-
339
- const slots: SlotChoice[] = []
340
-
341
- for (const phrase of spans) {
342
- const key = spanKey(phrase.span.start, phrase.span.end)
343
- const tagCandidates = topN(tagsBySpan.get(key) ?? [], opts.kTag, (c) => c.score)
344
- const words = wordCountOf(inputs.raw, phrase.span)
345
-
346
- for (const tagC of tagCandidates) {
347
- const places = inputs.resolverCandidates
348
- ? inputs.resolverCandidates.candidatesFor(tagC.span, tagC.tag).slice(0, opts.kResolver)
349
- : []
350
-
351
- if (!places.length) {
352
- slots.push({
353
- span: phrase.span,
354
- phraseConf: phrase.confidence,
355
- tag: tagC.tag,
356
- classifierScore: tagC.score,
357
- place: null,
358
- resolverScore: 1,
359
- wordCount: words,
360
- })
361
-
362
- continue
363
- }
364
-
365
- for (const place of places) {
366
- slots.push({
367
- span: phrase.span,
368
- phraseConf: phrase.confidence,
369
- tag: tagC.tag,
370
- classifierScore: tagC.score,
371
- place,
372
- resolverScore: normalizeResolverScore(place.score),
373
- wordCount: words,
374
- })
375
- }
376
- }
377
- }
378
-
379
- return slots
380
- }
381
-
382
- /**
383
- * Whitespace-delimited word count of a span's surface text, floor 1. Scales the per-slot inclusion bonus so coverage —
384
- * not slot count — is what the search rewards (see `INCLUSION_LOG_BONUS`).
385
- */
386
- function wordCountOf(raw: string, span: { start: number; end: number }): number {
387
- const text = raw.slice(span.start, span.end).trim()
388
-
389
- if (!text.length) return 1
390
-
391
- return text.split(/\s+/).length
392
- }
393
-
394
- /**
395
- * Concordance delta for adding `slot` to an existing beam. Returns the log-space contribution to add to the beam's
396
- * running score, or `-Infinity` if the slot introduces a hard contradiction (would be admissible into the beam's admin
397
- * chain but explicitly disagrees).
398
- *
399
- * Behavior at the boundaries:
400
- *
401
- * - Slot has no place or is non-admin → 0 (no chain contribution).
402
- * - No admin neighbors yet → 0 (nothing to agree with).
403
- * - Admin chain agrees → `+ concordanceWeight × log(1 + match_ratio)`.
404
- * - Some admin pairs cannot be verified (missing parents) → 0 contribution per neutral pair.
405
- * - Admin chain has any explicit contradiction → `-Infinity` (hard veto).
406
- */
407
- function concordanceDeltaFor(
408
- existing: SlotChoice[],
409
- candidate: SlotChoice,
410
- inputs: ReconcileInputs,
411
- opts: Required<ReconcileOpts>
412
- ): number {
413
- if (!candidate.place || !ADMIN_LEVEL_SET.has(candidate.tag)) return 0
414
- const chainOf = inputs.parentChain
415
-
416
- if (!chainOf) return 0
417
- const candIdx = ADMIN_LEVELS.indexOf(candidate.tag)
418
- let matches = 0
419
- let neutrals = 0
420
- let pairs = 0
421
-
422
- for (const prior of existing) {
423
- if (!prior.place || !ADMIN_LEVEL_SET.has(prior.tag)) continue
424
- const priorIdx = ADMIN_LEVELS.indexOf(prior.tag)
425
- const child = priorIdx > candIdx ? prior : candidate
426
- const parent = priorIdx > candIdx ? candidate : prior
427
-
428
- if (child === parent) continue
429
- const chain = chainOf.parentsOf(child.place!)
430
-
431
- pairs++
432
-
433
- if (!chain.length) {
434
- neutrals++
435
-
436
- continue
437
- }
438
-
439
- const hit = chain.some((p) => idsEqual(p.id, parent.place!.id))
440
-
441
- if (hit) {
442
- matches++
443
- } else {
444
- return Number.NEGATIVE_INFINITY
445
- }
446
- }
447
-
448
- if (pairs === 0) return 0
449
- const matchRatio = matches / pairs
450
-
451
- // Linear in matchRatio so a fully-consistent chain contributes exactly `+concordanceWeight`
452
- // log-space — the briefing's "+1 for fully consistent" reading. Matches contribute
453
- // proportionally; neutrals (no chain data) don't penalize.
454
- return opts.concordanceWeight * matchRatio
455
- }
456
-
457
- function idsEqual(a: number | string, b: number | string): boolean {
458
- return String(a) === String(b)
459
- }
460
-
461
- /**
462
- * Compute the per-factor breakdown for the winning beam. Independently of the search's `logScore` (which carries the
463
- * inclusion-bonus prior), the breakdown surfaces the bare `phrase × classifier × resolver × concordance` product so
464
- * callers can introspect why a tree won.
465
- */
466
- function breakdownFor(beam: Beam, inputs: ReconcileInputs, opts: Required<ReconcileOpts>): ScoreBreakdown {
467
- let phrase = 1
468
- let classifier = 1
469
- let resolver = 1
470
-
471
- for (const a of beam.assignments) {
472
- phrase *= Math.max(a.phraseConf, 0)
473
- classifier *= Math.max(a.classifierScore, 0)
474
- resolver *= Math.max(a.resolverScore, 0)
475
- }
476
-
477
- const concordanceLog = totalConcordanceLog(beam.assignments, inputs, opts)
478
- const concordance = Math.exp(concordanceLog)
479
- const total = phrase * classifier * resolver * concordance
480
-
481
- return { phrase, classifier, resolver, concordance, total }
482
- }
483
-
484
- /**
485
- * Recompute the joint concordance contribution for the entire assignment list (vs the incremental `concordanceDeltaFor`
486
- * used during search). Used by the breakdown.
487
- */
488
- function totalConcordanceLog(
489
- assignments: SlotChoice[],
490
- inputs: ReconcileInputs,
491
- opts: Required<ReconcileOpts>
492
- ): number {
493
- if (!inputs.parentChain) return 0
494
- let acc = 0
495
-
496
- for (let i = 0; i < assignments.length; i++) {
497
- const delta = concordanceDeltaFor(assignments.slice(0, i), assignments[i]!, inputs, opts)
498
-
499
- if (delta === Number.NEGATIVE_INFINITY) return Number.NEGATIVE_INFINITY
500
- acc += delta
501
- }
502
-
503
- return acc
504
- }
505
-
506
- function buildTree(beam: Beam, raw: string): AddressTree {
507
- const roots: AddressNode[] = beam.assignments
508
- .slice()
509
- // oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
510
- .sort((a, b) => a.span.start - b.span.start)
511
- .map((slot) => ({
512
- tag: slot.tag,
513
- value: raw.slice(slot.span.start, slot.span.end),
514
- start: slot.span.start,
515
- end: slot.span.end,
516
- confidence: slot.classifierScore * slot.phraseConf,
517
- children: [],
518
- source: "reconcile",
519
- sourceID: "reconcile:stage-5-joint",
520
- ...(slot.place
521
- ? {
522
- lat: slot.place.lat,
523
- lon: slot.place.lon,
524
- placeID: `${placeIDPrefix(slot.place)}:${slot.place.id}`,
525
- }
526
- : {}),
527
- }))
528
-
529
- return { raw, roots }
530
- }
531
-
532
- function emptyParseTree(raw: string): ParseTree {
533
- return {
534
- tree: { raw, roots: [] },
535
- confidence: 0,
536
- runnersUp: [],
537
- scoreBreakdown: { phrase: 0, classifier: 0, resolver: 0, concordance: 1, total: 0 },
538
- }
539
- }
540
-
541
- function placeIDPrefix(place: ResolvedPlace): string {
542
- // Mirror the convention used by @mailwoman/core/resolver-wof-sqlite (`wof:<id>`). Resolver
543
- // backends that don't carry an implicit vendor in `id` get a neutral `place:` prefix.
544
- return typeof place.id === "number" ? "wof" : "place"
545
- }
546
-
547
- function spanKey(start: number, end: number): string {
548
- return `${start}:${end}`
549
- }
550
-
551
- function topN<T>(items: ReadonlyArray<T>, n: number, key: (t: T) => number): T[] {
552
- return (
553
- items
554
- .slice()
555
- // oxlint-disable-next-line unicorn/no-array-sort -- sorts a freshly-built array; toSorted would double-allocate on a hot path
556
- .sort((a, b) => key(b) - key(a))
557
- .slice(0, n)
558
- )
559
- }
560
-
561
- function logSafe(x: number): number {
562
- return x > 0 ? Math.log(x) : -50
563
- }
564
-
565
- function normalizeResolverScore(score: number): number {
566
- // Resolver score scale is implementation-defined; clamp to (0, 1] for the multiplicative
567
- // combiner. A backend that returns 0 still counts as a candidate (e.g. a partial match) but
568
- // contributes a tiny log-factor.
569
- if (!Number.isFinite(score) || score <= 0) return 0.01
570
-
571
- if (score > 1) return 1
572
-
573
- return score
574
- }
575
-
576
- function softmax(scores: number[]): number[] {
577
- if (!scores.length) return []
578
- const max = Math.max(...scores)
579
- const exps = scores.map((s) => Math.exp(s - max))
580
- const sum = exps.reduce((a, b) => a + b, 0)
581
-
582
- return exps.map((e) => e / sum)
583
- }