@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
@@ -0,0 +1,347 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Diff two parses of the SAME input, over spans rather than over the component map.
7
+ *
8
+ * The component map is what a comparison reaches for and it loses the thing you need. Two arms that both emit
9
+ * `locality` tell you nothing about whether the locality MOVED, and a map keyed by tag cannot represent a span that
10
+ * slid one token left — it looks identical to a span that was replaced. This is not hypothetical: it is how a
11
+ * regression that turned
12
+ *
13
+ * Ye Three Lords, 27 Minories, London EC3N 1DE
14
+ *
15
+ * from `venue "Ye Three Lords" · locality London · street Minories` into `locality "Ye Three Lords"` reads as "the
16
+ * locality changed" in a map diff, when what actually happened is that two spans were destroyed and a third was
17
+ * retagged onto the text of one of them.
18
+ *
19
+ * So spans are matched by OVERLAP first and tag second, which lets the four events be told apart:
20
+ *
21
+ * - `retagged` — same text, different tag. The venue that became a locality.
22
+ * - `moved` — same tag, different span. The locality that slid onto the neighbouring segment.
23
+ * - `removed` / `added` — a span with no counterpart at all.
24
+ * - `confidence` — same tag, same span, the model simply became more or less sure.
25
+ *
26
+ * That last one is why the confidence delta is carried per span rather than as a headline: a row that did not change
27
+ * its answer but lost 0.3 of confidence on the deciding span is a row about to flip, and an aggregate cannot say so.
28
+ */
29
+
30
+ import { flattenTreeNodes } from "./tree-shape.ts"
31
+ import type { AddressTree } from "./types.ts"
32
+
33
+ /**
34
+ * What happened to one span between the two arms.
35
+ *
36
+ * `unchanged` is emitted rather than dropped so a renderer can show context lines; a caller wanting only the changes
37
+ * filters on {@linkcode isChange}.
38
+ */
39
+ export type SpanDeltaKind = "added" | "removed" | "retagged" | "moved" | "confidence" | "unchanged"
40
+
41
+ /**
42
+ * A span-level change, carrying both sides where both exist.
43
+ */
44
+ export interface SpanDelta {
45
+ kind: SpanDeltaKind
46
+ /**
47
+ * The tag on each side. Equal unless `kind` is `retagged`, and one side is absent for `added`/`removed`.
48
+ */
49
+ tagBefore?: string
50
+ tagAfter?: string
51
+ valueBefore?: string
52
+ valueAfter?: string
53
+ spanBefore?: [number, number]
54
+ spanAfter?: [number, number]
55
+ confidenceBefore?: number
56
+ confidenceAfter?: number
57
+ /**
58
+ * `after - before`, present only when both sides are. Negative means the arm under test is LESS sure.
59
+ */
60
+ confidenceDelta?: number
61
+ /**
62
+ * Where the assertion came from — `rule`, `neural`, `resolver`. A span whose tag is unchanged but whose source moved
63
+ * from `resolver` to `neural` lost its gazetteer backing, which no tag-level diff can show.
64
+ */
65
+ sourceBefore?: string
66
+ sourceAfter?: string
67
+ sourceIDBefore?: string
68
+ sourceIDAfter?: string
69
+ }
70
+
71
+ /**
72
+ * Whether a delta represents an actual change, as opposed to a context line.
73
+ */
74
+ export function isChange(delta: SpanDelta): boolean {
75
+ return delta.kind !== "unchanged"
76
+ }
77
+
78
+ /**
79
+ * One input's parse on both sides, plus the span-level story of how they differ.
80
+ */
81
+ export interface ParseDiff {
82
+ input: string
83
+ spans: SpanDelta[]
84
+ /**
85
+ * The locale/country call and how sure each arm was of it. A parse that changed nothing else but moved its country
86
+ * confidence across the scope threshold will geocode somewhere else entirely.
87
+ */
88
+ localeCountryBefore?: { country: string; confidence: number }
89
+ localeCountryAfter?: { country: string; confidence: number }
90
+ /**
91
+ * True when no span changed and the locale call is identical — the arms agree on this input.
92
+ */
93
+ identical: boolean
94
+ }
95
+
96
+ interface Flat {
97
+ tag: string
98
+ value: string
99
+ start: number
100
+ end: number
101
+ confidence: number
102
+ source?: string
103
+ sourceID?: string
104
+ }
105
+
106
+ function toFlat(tree: AddressTree | null | undefined): Flat[] {
107
+ return flattenTreeNodes(tree).map((node) => ({
108
+ tag: String(node.tag),
109
+ value: node.value,
110
+ start: node.start,
111
+ end: node.end,
112
+ confidence: node.confidence,
113
+ ...(node.source === undefined ? {} : { source: node.source }),
114
+ ...(node.sourceID === undefined ? {} : { sourceID: node.sourceID }),
115
+ }))
116
+ }
117
+
118
+ /**
119
+ * How much of the shorter span the two share, in [0, 1].
120
+ *
121
+ * Overlap rather than equality because the interesting failures move a boundary by a token or two, and an
122
+ * equality-keyed match reports those as a delete plus an insert — which is exactly the information loss this file
123
+ * exists to prevent.
124
+ */
125
+ function overlap(a: Flat, b: Flat): number {
126
+ const lo = Math.max(a.start, b.start)
127
+ const hi = Math.min(a.end, b.end)
128
+
129
+ if (hi <= lo) return 0
130
+
131
+ return (hi - lo) / Math.min(a.end - a.start, b.end - b.start)
132
+ }
133
+
134
+ /**
135
+ * The share of overlap below which two spans are treated as unrelated rather than moved.
136
+ *
137
+ * Half the shorter span: a boundary that slid by a token still matches, while two spans that merely touch at their
138
+ * edges do not, and reporting those as a `moved` would invent a relationship the parse does not assert.
139
+ */
140
+ const RELATED_OVERLAP = 0.5
141
+
142
+ /**
143
+ * Diff two parses of the same input.
144
+ *
145
+ * Matching is greedy on overlap, strongest pair first, with tag equality breaking ties — so a span that kept its tag is
146
+ * preferred over one that merely sits in the same place.
147
+ */
148
+ export function diffParse(
149
+ input: string,
150
+ before: AddressTree | null | undefined,
151
+ after: AddressTree | null | undefined,
152
+ locale?: {
153
+ before?: { country: string; confidence: number }
154
+ after?: { country: string; confidence: number }
155
+ }
156
+ ): ParseDiff {
157
+ const a = toFlat(before)
158
+ const b = toFlat(after)
159
+ const pairs: Array<{ i: number; j: number; score: number }> = []
160
+
161
+ for (const [i, left] of a.entries()) {
162
+ for (const [j, right] of b.entries()) {
163
+ const share = overlap(left, right)
164
+
165
+ if (share >= RELATED_OVERLAP) {
166
+ pairs.push({ i, j, score: share + (left.tag === right.tag ? 1 : 0) })
167
+ }
168
+ }
169
+ }
170
+
171
+ pairs.sort((x, y) => y.score - x.score)
172
+
173
+ const takenA = new Set<number>()
174
+ const takenB = new Set<number>()
175
+ const spans: SpanDelta[] = []
176
+
177
+ for (const { i, j } of pairs) {
178
+ if (takenA.has(i) || takenB.has(j)) continue
179
+
180
+ takenA.add(i)
181
+ takenB.add(j)
182
+
183
+ const left = a[i]!
184
+ const right = b[j]!
185
+ const sameSpan = left.start === right.start && left.end === right.end
186
+
187
+ const kind: SpanDeltaKind =
188
+ left.tag !== right.tag
189
+ ? "retagged"
190
+ : sameSpan
191
+ ? left.confidence === right.confidence
192
+ ? "unchanged"
193
+ : "confidence"
194
+ : "moved"
195
+
196
+ spans.push({
197
+ kind,
198
+ tagBefore: left.tag,
199
+ tagAfter: right.tag,
200
+ valueBefore: left.value,
201
+ valueAfter: right.value,
202
+ spanBefore: [left.start, left.end],
203
+ spanAfter: [right.start, right.end],
204
+ confidenceBefore: left.confidence,
205
+ confidenceAfter: right.confidence,
206
+ confidenceDelta: right.confidence - left.confidence,
207
+ ...(left.source === undefined ? {} : { sourceBefore: left.source }),
208
+ ...(right.source === undefined ? {} : { sourceAfter: right.source }),
209
+ ...(left.sourceID === undefined ? {} : { sourceIDBefore: left.sourceID }),
210
+ ...(right.sourceID === undefined ? {} : { sourceIDAfter: right.sourceID }),
211
+ })
212
+ }
213
+
214
+ for (const [i, left] of a.entries()) {
215
+ if (takenA.has(i)) continue
216
+
217
+ spans.push({
218
+ kind: "removed",
219
+ tagBefore: left.tag,
220
+ valueBefore: left.value,
221
+ spanBefore: [left.start, left.end],
222
+ confidenceBefore: left.confidence,
223
+ ...(left.source === undefined ? {} : { sourceBefore: left.source }),
224
+ })
225
+ }
226
+
227
+ for (const [j, right] of b.entries()) {
228
+ if (takenB.has(j)) continue
229
+
230
+ spans.push({
231
+ kind: "added",
232
+ tagAfter: right.tag,
233
+ valueAfter: right.value,
234
+ spanAfter: [right.start, right.end],
235
+ confidenceAfter: right.confidence,
236
+ ...(right.source === undefined ? {} : { sourceAfter: right.source }),
237
+ })
238
+ }
239
+
240
+ // Document order, so the rendering reads left-to-right across the address rather than in match order.
241
+ spans.sort((x, y) => (x.spanBefore?.[0] ?? x.spanAfter?.[0] ?? 0) - (y.spanBefore?.[0] ?? y.spanAfter?.[0] ?? 0))
242
+
243
+ const localeMoved =
244
+ locale?.before?.country !== locale?.after?.country || locale?.before?.confidence !== locale?.after?.confidence
245
+
246
+ return {
247
+ input,
248
+ spans,
249
+ ...(locale?.before ? { localeCountryBefore: locale.before } : {}),
250
+ ...(locale?.after ? { localeCountryAfter: locale.after } : {}),
251
+ identical: !spans.some(isChange) && !localeMoved,
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Confidence movement below which a same-tag same-span pair is not worth a line of its own.
257
+ *
258
+ * Two hundredths: the decoder's aggregate is a mean over a span's tokens, so a one-token re-scoring moves a long span
259
+ * by a hair and reporting that as a change buries the spans that actually moved.
260
+ */
261
+ export const CONFIDENCE_NOISE_FLOOR = 0.02
262
+
263
+ /**
264
+ * Render a diff the way a reader reads one — the ADDRESS first, then the spans that moved under it.
265
+ *
266
+ * Address-first is the point. An aggregate that reports "18 regressed" without the strings is the shape that let a
267
+ * venue-destroying regression read as a routine count for five runs.
268
+ */
269
+ export function renderParseDiff(diff: ParseDiff, options: { context?: boolean } = {}): string {
270
+ const lines: string[] = [diff.input]
271
+
272
+ if (diff.identical) {
273
+ lines.push(" (identical)")
274
+
275
+ return lines.join("\n")
276
+ }
277
+
278
+ const conf = (value?: number): string => (value === undefined ? "—" : value.toFixed(2))
279
+
280
+ for (const span of diff.spans) {
281
+ if (span.kind === "unchanged" && !options.context) continue
282
+
283
+ if (span.kind === "confidence") {
284
+ const delta = span.confidenceDelta ?? 0
285
+
286
+ if (Math.abs(delta) < CONFIDENCE_NOISE_FLOOR && !options.context) continue
287
+
288
+ lines.push(
289
+ ` ~ ${span.tagAfter}="${span.valueAfter}" confidence ${conf(span.confidenceBefore)} → ${conf(span.confidenceAfter)} (${delta >= 0 ? "+" : ""}${delta.toFixed(2)})`
290
+ )
291
+
292
+ continue
293
+ }
294
+
295
+ if (span.kind === "unchanged") {
296
+ lines.push(` ${span.tagAfter}="${span.valueAfter}" ${conf(span.confidenceAfter)}`)
297
+
298
+ continue
299
+ }
300
+
301
+ if (span.kind === "removed") {
302
+ lines.push(
303
+ ` - ${span.tagBefore}="${span.valueBefore}" ${conf(span.confidenceBefore)}${span.sourceBefore ? ` [${span.sourceBefore}]` : ""}`
304
+ )
305
+
306
+ continue
307
+ }
308
+
309
+ if (span.kind === "added") {
310
+ lines.push(
311
+ ` + ${span.tagAfter}="${span.valueAfter}" ${conf(span.confidenceAfter)}${span.sourceAfter ? ` [${span.sourceAfter}]` : ""}`
312
+ )
313
+
314
+ continue
315
+ }
316
+
317
+ // retagged / moved — both sides exist, so show the transition on one line.
318
+ const what = span.kind === "retagged" ? `${span.tagBefore} → ${span.tagAfter}` : `${span.tagAfter} moved`
319
+ const where = span.kind === "moved" ? ` [${span.spanBefore?.join()}] → [${span.spanAfter?.join()}]` : ""
320
+ const delta = span.confidenceDelta ?? 0
321
+
322
+ lines.push(` ! ${what} "${span.valueBefore}" → "${span.valueAfter}"${where}`)
323
+
324
+ lines.push(
325
+ ` confidence ${conf(span.confidenceBefore)} → ${conf(span.confidenceAfter)} (${delta >= 0 ? "+" : ""}${delta.toFixed(2)})`
326
+ )
327
+
328
+ if (span.sourceBefore !== span.sourceAfter) {
329
+ lines.push(` source ${span.sourceBefore ?? "—"} → ${span.sourceAfter ?? "—"}`)
330
+ }
331
+ }
332
+
333
+ const lb = diff.localeCountryBefore
334
+ const la = diff.localeCountryAfter
335
+
336
+ if (lb || la) {
337
+ const moved = lb?.country !== la?.country || lb?.confidence !== la?.confidence
338
+
339
+ if (moved) {
340
+ lines.push(
341
+ ` ! locale country ${lb?.country ?? "—"} (${conf(lb?.confidence)}) → ${la?.country ?? "—"} (${conf(la?.confidence)})`
342
+ )
343
+ }
344
+ }
345
+
346
+ return lines.join("\n")
347
+ }
@@ -60,7 +60,7 @@ export function treeToProposals(
60
60
 
61
61
  const visit = (node: AddressNode): void => {
62
62
  if (!emits || emits.has(node.tag)) {
63
- const span = { start: node.start, end: node.end, body: node.value } as unknown as Span
63
+ const span = { start: node.start, end: node.end, body: node.value } as Span
64
64
 
65
65
  proposals.push({
66
66
  span,
@@ -17,6 +17,29 @@ import type { ComponentTag } from "../types/component.ts"
17
17
  import type { AddressNode, AddressTree } from "./types.ts"
18
18
  import { type UnknownSpan, unknownSpans } from "./unknown-spans.ts"
19
19
 
20
+ /**
21
+ * A span the flat projection could not represent, and why.
22
+ *
23
+ * The flat map holds one value per tag, so a tree carrying two `locality` spans emits one and the other ceases to
24
+ * exist. `region: null` then means BOTH "the input named no region" and "the input named one, we parsed it, and the
25
+ * projection deleted it" — and #1755 is what that costs: the #1748 trailing region is PARSED, tagged `locality`, and
26
+ * dropped here, which is why no decode lever could ever move that class.
27
+ */
28
+ export interface DroppedSpan {
29
+ /**
30
+ * The tag the span carried. Always one already present in the output — a drop happens because the slot was taken.
31
+ */
32
+ tag: ComponentTag
33
+ /**
34
+ * The value that was discarded.
35
+ */
36
+ value: string
37
+ /**
38
+ * The value that held the slot, so a reader can see which of the two survived without re-walking the tree.
39
+ */
40
+ kept: string
41
+ }
42
+
20
43
  /**
21
44
  * Options for {@link decodeAsJSON}.
22
45
  */
@@ -26,10 +49,22 @@ export interface SerializeJSONOpts {
26
49
  * libpostal-compatible (a flat tag→value map) unless the caller asks for the gaps.
27
50
  */
28
51
  includeUnknown?: boolean
52
+ /**
53
+ * Add a `dropped` array naming every span first-occurrence-wins discarded (#1755). Default false, keeping the output
54
+ * libpostal-compatible; the geocode path opts in, because a silently deleted component is the one thing a caller
55
+ * cannot recover for itself.
56
+ */
57
+ includeDropped?: boolean
29
58
  }
30
59
 
31
- function visit(node: AddressNode, out: Partial<Record<ComponentTag, string>>): void {
32
- if (!(node.tag in out)) {
60
+ function visit(node: AddressNode, out: Partial<Record<ComponentTag, string>>, dropped: DroppedSpan[]): void {
61
+ if (node.tag in out) {
62
+ // The slot is taken and this span is about to cease to exist. Record it BEFORE the recursion, so the order of
63
+ // `dropped` follows the walk rather than the tree's depth.
64
+ if (out[node.tag] !== node.value) {
65
+ dropped.push({ tag: node.tag, value: node.value, kept: out[node.tag]! })
66
+ }
67
+ } else {
33
68
  out[node.tag] = node.value
34
69
  }
35
70
 
@@ -37,12 +72,14 @@ function visit(node: AddressNode, out: Partial<Record<ComponentTag, string>>): v
37
72
  for (const interp of node.interpretations) {
38
73
  if (!(interp.tag in out)) {
39
74
  out[interp.tag] = node.value
75
+ } else if (out[interp.tag] !== node.value) {
76
+ dropped.push({ tag: interp.tag, value: node.value, kept: out[interp.tag]! })
40
77
  }
41
78
  }
42
79
  }
43
80
 
44
81
  for (const child of node.children) {
45
- visit(child, out)
82
+ visit(child, out, dropped)
46
83
  }
47
84
  }
48
85
 
@@ -54,16 +91,17 @@ export function decodeAsJSON(tree: AddressTree): Partial<Record<ComponentTag, st
54
91
  export function decodeAsJSON(
55
92
  tree: AddressTree,
56
93
  opts: SerializeJSONOpts
57
- ): Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[] }
94
+ ): Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[]; dropped?: DroppedSpan[] }
58
95
 
59
96
  export function decodeAsJSON(
60
97
  tree: AddressTree,
61
98
  opts: SerializeJSONOpts = {}
62
- ): Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[] } {
63
- const out: Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[] } = {}
99
+ ): Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[]; dropped?: DroppedSpan[] } {
100
+ const out: Partial<Record<ComponentTag, string>> & { unknown?: UnknownSpan[]; dropped?: DroppedSpan[] } = {}
101
+ const dropped: DroppedSpan[] = []
64
102
 
65
103
  for (const root of tree.roots) {
66
- visit(root, out)
104
+ visit(root, out, dropped)
67
105
  }
68
106
 
69
107
  // Always emit `unknown` (even `[]`) when asked — a consumer that opted in can iterate it without a
@@ -72,5 +110,11 @@ export function decodeAsJSON(
72
110
  out.unknown = unknownSpans(tree)
73
111
  }
74
112
 
113
+ // Always emitted when asked, `[]` included — the same reasoning as `unknown` above, and load-bearing here: a caller
114
+ // that has to presence-check cannot tell "nothing was dropped" from "this build does not report drops".
115
+ if (opts.includeDropped) {
116
+ out.dropped = dropped
117
+ }
118
+
75
119
  return out
76
120
  }
@@ -25,7 +25,13 @@ export interface SerializeTuplesOpts {
25
25
  includeUnknown?: boolean
26
26
  }
27
27
 
28
- function flatten(node: AddressNode, out: AddressNode[]): void {
28
+ /**
29
+ * Recursively flattens an `AddressNode` tree into a source-ordered list of nodes.
30
+ *
31
+ * @param node The root node to flatten.
32
+ * @param out The output array to which flattened nodes will be appended.
33
+ */
34
+ export function flatten(node: AddressNode, out: AddressNode[]): void {
29
35
  out.push(node)
30
36
 
31
37
  for (const child of node.children) {
@@ -179,7 +179,7 @@ function serializeNode(node: AddressNode, indent: string, opts: Required<Seriali
179
179
  }
180
180
 
181
181
  const childrenStr = node.children.map((c) => serializeNode(c, childIndent, opts)).join(nl)
182
- const inner = [childrenStr, altsBlock].filter(Boolean).join(nl)
182
+ const inner = [childrenStr, altsBlock].filter((part) => part.length > 0).join(nl)
183
183
 
184
184
  return `${indent}<${node.tag}${a}>${text}${nl}${inner}${nl}${indent}</${node.tag}>`
185
185
  }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Tree-SHAPE predicates over an `AddressTree` — the one stack walk behind the pipeline's bare-tree
7
+ * guards and the "lone bare toponym" gates. Two quantifiers cover every consumer:
8
+ *
9
+ * - {@link isBareTreeOf} — every value-bearing node carries the given tag (several allowed). The
10
+ * #912 / #1589 posture guards (`isBareLocalityTree`, `isBarePostcodeTree`) are this with the tag
11
+ * bound.
12
+ * - {@link loneValueBearingNode} — the tree has EXACTLY one value-bearing node. The street-miss
13
+ * fallback and the resolver's bare-country race bind the tag at the call site.
14
+ *
15
+ * The distinction is load-bearing: a two-segment parse can satisfy the first and never the second,
16
+ * and both behaviors are pinned by their consumers' boards.
17
+ */
18
+
19
+ import type { ComponentTag } from "../types/component.ts"
20
+ import { flatten } from "./serialize-tuples.ts"
21
+ import type { AddressNode, AddressTree } from "./types.ts"
22
+
23
+ /**
24
+ * True when every node in the tree either carries `tag` or bears no value — i.e. the only EVIDENCE in the parse is
25
+ * `tag`-shaped. A tag-matching node counts even when its value is empty (the guard asks "did the parser emit this
26
+ * shape", not "is the span non-blank"); any OTHER tag with a non-empty value disqualifies. False for a tree with no
27
+ * `tag` node at all.
28
+ */
29
+ export function isBareTreeOf(tree: AddressTree, tag: ComponentTag): boolean {
30
+ let sawTag = false
31
+ const stack = [...tree.roots]
32
+
33
+ while (stack.length) {
34
+ const node = stack.pop()!
35
+
36
+ if (node.tag === tag) {
37
+ sawTag = true
38
+ } else if (node.value.trim() !== "") return false
39
+
40
+ stack.push(...node.children)
41
+ }
42
+
43
+ return sawTag
44
+ }
45
+
46
+ /**
47
+ * The tree's single value-bearing node, or null when the tree holds none or more than one. Callers gate on the returned
48
+ * node's `tag` — the quantifier ("this is the WHOLE query") is what this walk answers.
49
+ */
50
+ export function loneValueBearingNode(tree: AddressTree): AddressNode | null {
51
+ let lone: AddressNode | null = null
52
+ const stack = [...tree.roots]
53
+
54
+ while (stack.length) {
55
+ const node = stack.pop()!
56
+
57
+ if (node.value.trim().length) {
58
+ if (lone !== null) return null
59
+ lone = node
60
+ }
61
+
62
+ stack.push(...node.children)
63
+ }
64
+
65
+ return lone
66
+ }
67
+
68
+ /**
69
+ * One flattened node, projected for display.
70
+ *
71
+ * A structural copy rather than the `AddressNode` itself: a consumer rendering a span list must not be handed the live
72
+ * node, whose `children` and `metadata` invite a walk it has already been given the result of.
73
+ */
74
+ export interface FlatTreeNode {
75
+ tag: ComponentTag
76
+ value: string
77
+ confidence: number
78
+ start: number
79
+ end: number
80
+ /**
81
+ * Where the assertion came from — `rule`, `neural`, `resolver`.
82
+ *
83
+ * Carried because a span that keeps its tag, its text and its confidence while its source moves from `resolver` to
84
+ * `neural` has lost its gazetteer backing, and a projection that drops this reports that span as unchanged.
85
+ */
86
+ source?: string
87
+ sourceID?: string
88
+ /**
89
+ * The resolver's answer for this span, when one won.
90
+ *
91
+ * Carried for the same reason `source` is: a projection that keeps only the text and the tag cannot tell a span that
92
+ * resolved to a DIFFERENT place from one that did not move at all, and those are a ranking problem and a non-event
93
+ * respectively. `alternatives` is reduced to its LENGTH — the retrieval breadth is what a consumer reads, and handing
94
+ * over the candidate objects invites a walk this projection exists to have already done.
95
+ */
96
+ placeID?: string
97
+ lat?: number
98
+ lon?: number
99
+ alternatives?: number
100
+ }
101
+
102
+ /**
103
+ * Flatten a tree to its nodes in SOURCE order — sorted by `start`, the same order `decodeAsTuples` means by it.
104
+ *
105
+ * The two copies this replaces (the demo's `demo-helpers.ts` and the `demo-cascade-smoke` mirror of it) relied on
106
+ * TRAVERSAL order instead: depth-first onto a stack, then reversed. That coincides with source order only while every
107
+ * parent's span precedes its children's, and the decoder does not promise it — measured, the two orders disagree on **7
108
+ * of 10** ordinary addresses, always with a child ahead of its parent:
109
+ *
110
+ * Queen Street, Bristol street_suffix@6 before street@0
111
+ * Via Roma, 5, 50123 Firenze, … house_number@10 before street@0
112
+ * 30 St Mary Axe …, London EC3A 8BF postcode@37 before locality@30
113
+ *
114
+ * Sorting is what the tuple projection already does, so this makes a rendered span list and `decodeAsTuples` agree by
115
+ * construction rather than by luck.
116
+ *
117
+ * The smoke test could not import the demo's copy (that would be a `mailwoman` → `docs` project reference, and `docs`
118
+ * already depends on `mailwoman`), which is why a third home was the only way to collapse them.
119
+ */
120
+ export function flattenTreeNodes(tree?: AddressTree | null): FlatTreeNode[] {
121
+ if (!tree) return []
122
+
123
+ const all: AddressNode[] = []
124
+
125
+ for (const root of tree.roots) {
126
+ flatten(root, all)
127
+ }
128
+
129
+ return all
130
+ .map((node) => ({
131
+ tag: node.tag,
132
+ value: node.value,
133
+ confidence: node.confidence,
134
+ start: node.start,
135
+ end: node.end,
136
+ ...(node.source === undefined ? {} : { source: node.source }),
137
+ ...(node.sourceID === undefined ? {} : { sourceID: node.sourceID }),
138
+ ...(node.placeID === undefined ? {} : { placeID: node.placeID }),
139
+ ...(node.lat === undefined ? {} : { lat: node.lat }),
140
+ ...(node.lon === undefined ? {} : { lon: node.lon }),
141
+ ...(node.alternatives === undefined ? {} : { alternatives: node.alternatives.length }),
142
+ }))
143
+ .toSorted((a, b) => a.start - b.start)
144
+ }
package/decoder/types.ts CHANGED
@@ -169,6 +169,14 @@ export interface AddressTree {
169
169
  * pre-classifier (Phase 6+) is the intended source of this value.
170
170
  */
171
171
  system?: AddressSystem
172
+ /**
173
+ * The parse-time locale-head verdict when it was CONFIDENT (softmax >= the action threshold): the model's own read of
174
+ * which country's addressing this text is shaped like. Absent = under threshold or the head never ran — unknown,
175
+ * never "domestic". Evidence about the TEXT, not a resolved country: the head is a 9-way classifier, so a Chinese
176
+ * address may read GB — right about "not the locale's country", wrong about which. The scope gate this exists for
177
+ * (#1684) therefore only ever DROPS an inferred scope on a mismatch; it never re-points one.
178
+ */
179
+ localeCountry?: { country: string; confidence: number }
172
180
  }
173
181
 
174
182
  /**
@@ -30,8 +30,11 @@ import type { AddressNode, AddressTree } from "./types.ts"
30
30
  /**
31
31
  * Tags that cannot stand alone: each is a sub-component of a specific structural anchor (street / locality / venue /
32
32
  * postcode). If none of a tag's allowed parents appear anywhere in the tree, the node is an orphan fragment.
33
+ *
34
+ * This set is also the DENOMINATOR of the stranded-dependent check — the classes that CAN fire — so a caller counting
35
+ * which ones do reads it here rather than re-listing the tags, which would drift the moment one is added.
33
36
  */
34
- const STRICT_DEPENDENTS: ReadonlySet<ComponentTag> = new Set<ComponentTag>([
37
+ export const STRICT_DEPENDENTS: ReadonlySet<ComponentTag> = new Set<ComponentTag>([
35
38
  "street_prefix",
36
39
  "street_prefix_particle",
37
40
  "street_suffix",
@@ -40,10 +43,19 @@ const STRICT_DEPENDENTS: ReadonlySet<ComponentTag> = new Set<ComponentTag>([
40
43
  "dependent_locality",
41
44
  "attention",
42
45
  "cedex",
43
- "intersection_a",
44
- "intersection_b",
45
46
  ])
46
47
 
48
+ /*
49
+ * `intersection_a` / `intersection_b` are deliberately NOT strict dependents, for the same reason the geographic
50
+ * containers above are exempt: `Main St and 5th Ave` is a bare intersection QUERY — a degenerate-but-valid parse with
51
+ * no street or locality to anchor to, and the correct answer for that input. Treating the pair as stranded flagged it
52
+ * identically to `Elephant and Castle Road`, which is a genuine defect (one street read as a junction), so the rule
53
+ * had no power to separate a right answer from a wrong one. A check that fires on both is not evidence about either.
54
+ *
55
+ * The `Elephant and Castle Road` defect is real and stays tracked — by the board's own expectation for that row
56
+ * (#1750), which is where a claim needing TRUTH belongs. This file only makes claims a tree can settle about itself.
57
+ */
58
+
47
59
  export interface TreeViolation {
48
60
  type: "illegal-edge" | "stranded-dependent"
49
61
  tag: ComponentTag