@mailwoman/core 9.1.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 (371) 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 +1 -1
  5. package/coarse-placer/tools/build-outlier-exposure.ts +1 -1
  6. package/coarse-placer/tools/build-outlier-latin.ts +13 -7
  7. package/coarse-placer/tools/build-outlier-oa.ts +11 -5
  8. package/coarse-placer/tools/eval-latin-offmap.ts +3 -2
  9. package/coarse-placer/tools/eval-openset.ts +3 -2
  10. package/coarse-placer/tools/eval-quant-compare.ts +3 -2
  11. package/coarse-placer/tools/eval.ts +3 -2
  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 -2
  15. package/coarse-placer/tools/quantize.ts +2 -2
  16. package/coarse-placer/tools/train.ts +2 -1
  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/containment.ts +5 -1
  22. package/decoder/index.ts +1 -0
  23. package/decoder/parse-diff.ts +347 -0
  24. package/decoder/proposals-to-tree.ts +1 -1
  25. package/decoder/serialize-json.ts +51 -7
  26. package/decoder/serialize-tuples.ts +7 -1
  27. package/decoder/serialize-xml.ts +1 -1
  28. package/decoder/tree-shape.ts +144 -0
  29. package/decoder/types.ts +8 -0
  30. package/decoder/validate-tree.ts +15 -3
  31. package/env/index.ts +3 -1
  32. package/env/paths.ts +21 -33
  33. package/env/schema.ts +26 -3
  34. package/fs/utils/formatters.ts +107 -0
  35. package/fs/utils/index.ts +7 -0
  36. package/fs/zip.ts +1 -1
  37. package/layers/manifest.ts +56 -15
  38. package/layers/schema.ts +54 -2
  39. package/locale/locale.ts +1 -1
  40. package/locale/registry.ts +1 -1
  41. package/objects.ts +20 -8
  42. package/out/api/responses.d.ts +9 -0
  43. package/out/api/responses.d.ts.map +1 -1
  44. package/out/api/responses.js +11 -0
  45. package/out/api/responses.js.map +1 -1
  46. package/out/coarse-placer/featurize.d.ts +0 -1
  47. package/out/coarse-placer/featurize.d.ts.map +1 -1
  48. package/out/coarse-placer/featurize.js.map +1 -1
  49. package/out/coarse-placer/tools/build-dataset.js +1 -1
  50. package/out/coarse-placer/tools/build-dataset.js.map +1 -1
  51. package/out/coarse-placer/tools/build-outlier-exposure.js +1 -1
  52. package/out/coarse-placer/tools/build-outlier-exposure.js.map +1 -1
  53. package/out/coarse-placer/tools/build-outlier-latin.d.ts.map +1 -1
  54. package/out/coarse-placer/tools/build-outlier-latin.js +12 -7
  55. package/out/coarse-placer/tools/build-outlier-latin.js.map +1 -1
  56. package/out/coarse-placer/tools/build-outlier-oa.d.ts.map +1 -1
  57. package/out/coarse-placer/tools/build-outlier-oa.js +10 -5
  58. package/out/coarse-placer/tools/build-outlier-oa.js.map +1 -1
  59. package/out/coarse-placer/tools/eval-latin-offmap.d.ts.map +1 -1
  60. package/out/coarse-placer/tools/eval-latin-offmap.js +2 -2
  61. package/out/coarse-placer/tools/eval-latin-offmap.js.map +1 -1
  62. package/out/coarse-placer/tools/eval-openset.d.ts.map +1 -1
  63. package/out/coarse-placer/tools/eval-openset.js +2 -2
  64. package/out/coarse-placer/tools/eval-openset.js.map +1 -1
  65. package/out/coarse-placer/tools/eval-quant-compare.d.ts.map +1 -1
  66. package/out/coarse-placer/tools/eval-quant-compare.js +2 -2
  67. package/out/coarse-placer/tools/eval-quant-compare.js.map +1 -1
  68. package/out/coarse-placer/tools/eval.d.ts.map +1 -1
  69. package/out/coarse-placer/tools/eval.js +2 -2
  70. package/out/coarse-placer/tools/eval.js.map +1 -1
  71. package/out/coarse-placer/tools/fnv-hash.d.ts +1 -6
  72. package/out/coarse-placer/tools/fnv-hash.d.ts.map +1 -1
  73. package/out/coarse-placer/tools/fnv-hash.js +1 -6
  74. package/out/coarse-placer/tools/fnv-hash.js.map +1 -1
  75. package/out/coarse-placer/tools/index.d.ts +1 -1
  76. package/out/coarse-placer/tools/index.js +1 -1
  77. package/out/coarse-placer/tools/probe-frontier.d.ts.map +1 -1
  78. package/out/coarse-placer/tools/probe-frontier.js +2 -2
  79. package/out/coarse-placer/tools/probe-frontier.js.map +1 -1
  80. package/out/coarse-placer/tools/quantize.js +2 -2
  81. package/out/coarse-placer/tools/quantize.js.map +1 -1
  82. package/out/coarse-placer/tools/train.d.ts.map +1 -1
  83. package/out/coarse-placer/tools/train.js +1 -1
  84. package/out/coarse-placer/tools/train.js.map +1 -1
  85. package/out/crypto/node.d.ts.map +1 -1
  86. package/out/crypto/node.js +2 -1
  87. package/out/crypto/node.js.map +1 -1
  88. package/out/db/schema.d.ts +12 -3
  89. package/out/db/schema.d.ts.map +1 -1
  90. package/out/db/schema.js +6 -0
  91. package/out/db/schema.js.map +1 -1
  92. package/out/decoder/containment.d.ts.map +1 -1
  93. package/out/decoder/containment.js +5 -1
  94. package/out/decoder/containment.js.map +1 -1
  95. package/out/decoder/index.d.ts +1 -0
  96. package/out/decoder/index.d.ts.map +1 -1
  97. package/out/decoder/index.js +1 -0
  98. package/out/decoder/index.js.map +1 -1
  99. package/out/decoder/parse-diff.d.ts +125 -0
  100. package/out/decoder/parse-diff.d.ts.map +1 -0
  101. package/out/decoder/parse-diff.js +219 -0
  102. package/out/decoder/parse-diff.js.map +1 -0
  103. package/out/decoder/proposals-to-tree.js.map +1 -1
  104. package/out/decoder/serialize-json.d.ts +29 -0
  105. package/out/decoder/serialize-json.d.ts.map +1 -1
  106. package/out/decoder/serialize-json.js +20 -4
  107. package/out/decoder/serialize-json.js.map +1 -1
  108. package/out/decoder/serialize-tuples.d.ts +8 -1
  109. package/out/decoder/serialize-tuples.d.ts.map +1 -1
  110. package/out/decoder/serialize-tuples.js +7 -1
  111. package/out/decoder/serialize-tuples.js.map +1 -1
  112. package/out/decoder/serialize-xml.js +1 -1
  113. package/out/decoder/serialize-xml.js.map +1 -1
  114. package/out/decoder/tree-shape.d.ts +84 -0
  115. package/out/decoder/tree-shape.d.ts.map +1 -0
  116. package/out/decoder/tree-shape.js +98 -0
  117. package/out/decoder/tree-shape.js.map +1 -0
  118. package/out/decoder/types.d.ts +11 -0
  119. package/out/decoder/types.d.ts.map +1 -1
  120. package/out/decoder/validate-tree.d.ts +8 -0
  121. package/out/decoder/validate-tree.d.ts.map +1 -1
  122. package/out/decoder/validate-tree.js +4 -3
  123. package/out/decoder/validate-tree.js.map +1 -1
  124. package/out/env/index.d.ts +10 -1
  125. package/out/env/index.d.ts.map +1 -1
  126. package/out/env/index.js +2 -1
  127. package/out/env/index.js.map +1 -1
  128. package/out/env/paths.d.ts +15 -8
  129. package/out/env/paths.d.ts.map +1 -1
  130. package/out/env/paths.js +19 -29
  131. package/out/env/paths.js.map +1 -1
  132. package/out/env/schema.d.ts +9 -1
  133. package/out/env/schema.d.ts.map +1 -1
  134. package/out/env/schema.js +25 -3
  135. package/out/env/schema.js.map +1 -1
  136. package/out/fs/utils/formatters.d.ts +57 -0
  137. package/out/fs/utils/formatters.d.ts.map +1 -0
  138. package/out/fs/utils/formatters.js +79 -0
  139. package/out/fs/utils/formatters.js.map +1 -0
  140. package/out/fs/utils/index.d.ts +7 -0
  141. package/out/fs/utils/index.d.ts.map +1 -0
  142. package/out/fs/utils/index.js +7 -0
  143. package/out/fs/utils/index.js.map +1 -0
  144. package/out/fs/zip.js +1 -1
  145. package/out/fs/zip.js.map +1 -1
  146. package/out/layers/manifest.d.ts +32 -7
  147. package/out/layers/manifest.d.ts.map +1 -1
  148. package/out/layers/manifest.js +31 -6
  149. package/out/layers/manifest.js.map +1 -1
  150. package/out/layers/schema.d.ts +48 -2
  151. package/out/layers/schema.d.ts.map +1 -1
  152. package/out/layers/schema.js +32 -0
  153. package/out/layers/schema.js.map +1 -1
  154. package/out/locale/locale.d.ts +1 -1
  155. package/out/locale/locale.d.ts.map +1 -1
  156. package/out/locale/registry.js +1 -1
  157. package/out/locale/registry.js.map +1 -1
  158. package/out/objects.d.ts +12 -6
  159. package/out/objects.d.ts.map +1 -1
  160. package/out/objects.js +4 -1
  161. package/out/objects.js.map +1 -1
  162. package/out/pipeline/index.d.ts +2 -6
  163. package/out/pipeline/index.d.ts.map +1 -1
  164. package/out/pipeline/index.js +1 -3
  165. package/out/pipeline/index.js.map +1 -1
  166. package/out/pipeline/runtime-pipeline.d.ts +51 -9
  167. package/out/pipeline/runtime-pipeline.d.ts.map +1 -1
  168. package/out/pipeline/runtime-pipeline.js +29 -132
  169. package/out/pipeline/runtime-pipeline.js.map +1 -1
  170. package/out/pipeline/span-proposer.js +1 -1
  171. package/out/pipeline/span-proposer.js.map +1 -1
  172. package/out/pipeline/types.d.ts +22 -14
  173. package/out/pipeline/types.d.ts.map +1 -1
  174. package/out/pipeline/types.js.map +1 -1
  175. package/out/policy/defaults.d.ts +1 -1
  176. package/out/policy/defaults.d.ts.map +1 -1
  177. package/out/policy/defaults.js +1 -1
  178. package/out/policy/defaults.js.map +1 -1
  179. package/out/policy/policy.d.ts +1 -1
  180. package/out/policy/policy.d.ts.map +1 -1
  181. package/out/policy/registry.d.ts +1 -1
  182. package/out/policy/registry.d.ts.map +1 -1
  183. package/out/resolver/coverage-facts.d.ts +89 -0
  184. package/out/resolver/coverage-facts.d.ts.map +1 -0
  185. package/out/resolver/coverage-facts.js +22 -0
  186. package/out/resolver/coverage-facts.js.map +1 -0
  187. package/out/resolver/index.d.ts +2 -1
  188. package/out/resolver/index.d.ts.map +1 -1
  189. package/out/resolver/index.js +1 -0
  190. package/out/resolver/index.js.map +1 -1
  191. package/out/resolver/lookup-types.d.ts +178 -0
  192. package/out/resolver/lookup-types.d.ts.map +1 -0
  193. package/out/resolver/lookup-types.js +11 -0
  194. package/out/resolver/lookup-types.js.map +1 -0
  195. package/out/resolver/placetype-map.d.ts +71 -0
  196. package/out/resolver/placetype-map.d.ts.map +1 -0
  197. package/out/resolver/placetype-map.js +98 -0
  198. package/out/resolver/placetype-map.js.map +1 -0
  199. package/out/resolver/postcode-format.d.ts +38 -0
  200. package/out/resolver/postcode-format.d.ts.map +1 -0
  201. package/out/resolver/postcode-format.js +83 -0
  202. package/out/resolver/postcode-format.js.map +1 -0
  203. package/out/resolver/types.d.ts +271 -297
  204. package/out/resolver/types.d.ts.map +1 -1
  205. package/out/resolver/types.js +4 -97
  206. package/out/resolver/types.js.map +1 -1
  207. package/out/resources/git.d.ts +8 -1
  208. package/out/resources/git.d.ts.map +1 -1
  209. package/out/resources/git.js +4 -3
  210. package/out/resources/git.js.map +1 -1
  211. package/out/resources/languages/types.gen.d.ts +1 -1
  212. package/out/resources/languages/types.gen.d.ts.map +1 -1
  213. package/out/resources/languages/types.gen.js +40 -0
  214. package/out/resources/languages/types.gen.js.map +1 -1
  215. package/out/resources/libpostal.d.ts +2 -2
  216. package/out/resources/libpostal.d.ts.map +1 -1
  217. package/out/resources/libpostal.js +2 -2
  218. package/out/resources/libpostal.js.map +1 -1
  219. package/out/resources/whosonfirst/DataSourceCache.d.ts.map +1 -1
  220. package/out/resources/whosonfirst/DataSourceCache.js.map +1 -1
  221. package/out/resources/whosonfirst/PlacetypeDataSource.d.ts +2 -2
  222. package/out/resources/whosonfirst/PlacetypeDataSource.d.ts.map +1 -1
  223. package/out/resources/whosonfirst/PlacetypeDataSource.js +28 -3
  224. package/out/resources/whosonfirst/PlacetypeDataSource.js.map +1 -1
  225. package/out/resources/whosonfirst/loader.d.ts +2 -2
  226. package/out/resources/whosonfirst/loader.d.ts.map +1 -1
  227. package/out/resources/whosonfirst/loader.js.map +1 -1
  228. package/out/resources/whosonfirst/placetypes/Placetype.d.ts.map +1 -1
  229. package/out/resources/whosonfirst/placetypes/Placetype.js +2 -2
  230. package/out/resources/whosonfirst/placetypes/Placetype.js.map +1 -1
  231. package/out/resources/whosonfirst/placetypes/admin.d.ts +9 -1
  232. package/out/resources/whosonfirst/placetypes/admin.d.ts.map +1 -1
  233. package/out/resources/whosonfirst/placetypes/admin.js +1 -1
  234. package/out/resources/whosonfirst/placetypes/admin.js.map +1 -1
  235. package/out/resources/whosonfirst/placetypes/definition.d.ts.map +1 -1
  236. package/out/resources/whosonfirst/placetypes/definition.js +1 -1
  237. package/out/resources/whosonfirst/placetypes/definition.js.map +1 -1
  238. package/out/resources/whosonfirst/placetypes/index.d.ts +1 -0
  239. package/out/resources/whosonfirst/placetypes/index.d.ts.map +1 -1
  240. package/out/resources/whosonfirst/placetypes/index.js +1 -0
  241. package/out/resources/whosonfirst/placetypes/index.js.map +1 -1
  242. package/out/resources/whosonfirst/placetypes/specificity.d.ts +65 -0
  243. package/out/resources/whosonfirst/placetypes/specificity.d.ts.map +1 -0
  244. package/out/resources/whosonfirst/placetypes/specificity.js +101 -0
  245. package/out/resources/whosonfirst/placetypes/specificity.js.map +1 -0
  246. package/out/resources/whosonfirst/sharded-repo.d.ts +28 -0
  247. package/out/resources/whosonfirst/sharded-repo.d.ts.map +1 -1
  248. package/out/resources/whosonfirst/sharded-repo.js +46 -2
  249. package/out/resources/whosonfirst/sharded-repo.js.map +1 -1
  250. package/out/scripting/arguments.d.ts +27 -0
  251. package/out/scripting/arguments.d.ts.map +1 -0
  252. package/out/scripting/arguments.js +32 -0
  253. package/out/scripting/arguments.js.map +1 -0
  254. package/out/scripting/command.d.ts +28 -0
  255. package/out/scripting/command.d.ts.map +1 -0
  256. package/out/scripting/command.js +47 -0
  257. package/out/scripting/command.js.map +1 -0
  258. package/out/scripting/index.js +3 -3
  259. package/out/scripting/index.js.map +1 -1
  260. package/out/scripting/utils/index.d.ts +1 -12
  261. package/out/scripting/utils/index.d.ts.map +1 -1
  262. package/out/scripting/utils/index.js +4 -21
  263. package/out/scripting/utils/index.js.map +1 -1
  264. package/out/tools/download-libpostal-resources.d.ts.map +1 -1
  265. package/out/tools/download-libpostal-resources.js +4 -7
  266. package/out/tools/download-libpostal-resources.js.map +1 -1
  267. package/out/tools/download-ssl-address.d.ts +3 -3
  268. package/out/tools/download-ssl-address.d.ts.map +1 -1
  269. package/out/tools/download-ssl-address.js +26 -13
  270. package/out/tools/download-ssl-address.js.map +1 -1
  271. package/out/tools/generate-language-types.d.ts +16 -1
  272. package/out/tools/generate-language-types.d.ts.map +1 -1
  273. package/out/tools/generate-language-types.js +38 -5
  274. package/out/tools/generate-language-types.js.map +1 -1
  275. package/out/tools/index.d.ts +1 -2
  276. package/out/tools/index.d.ts.map +1 -1
  277. package/out/tools/index.js +1 -2
  278. package/out/tools/index.js.map +1 -1
  279. package/out/utils/data-root.d.ts +27 -7
  280. package/out/utils/data-root.d.ts.map +1 -1
  281. package/out/utils/data-root.js +36 -13
  282. package/out/utils/data-root.js.map +1 -1
  283. package/out/utils/hash.d.ts +2 -3
  284. package/out/utils/hash.d.ts.map +1 -1
  285. package/out/utils/hash.js +3 -3
  286. package/out/utils/hash.js.map +1 -1
  287. package/out/utils/index.d.ts +1 -0
  288. package/out/utils/index.d.ts.map +1 -1
  289. package/out/utils/index.js +1 -0
  290. package/out/utils/index.js.map +1 -1
  291. package/out/utils/jsonl.js +1 -1
  292. package/out/utils/jsonl.js.map +1 -1
  293. package/out/utils/repo.d.ts +8 -0
  294. package/out/utils/repo.d.ts.map +1 -1
  295. package/out/utils/repo.js +10 -1
  296. package/out/utils/repo.js.map +1 -1
  297. package/out/utils/sealed-db.d.ts +11 -0
  298. package/out/utils/sealed-db.d.ts.map +1 -1
  299. package/out/utils/sealed-db.js +28 -1
  300. package/out/utils/sealed-db.js.map +1 -1
  301. package/out/utils/sqlite-rows.d.ts +34 -0
  302. package/out/utils/sqlite-rows.d.ts.map +1 -0
  303. package/out/utils/sqlite-rows.js +37 -0
  304. package/out/utils/sqlite-rows.js.map +1 -0
  305. package/out/utils/stats.d.ts +1 -6
  306. package/out/utils/stats.d.ts.map +1 -1
  307. package/out/utils/stats.js +1 -6
  308. package/out/utils/stats.js.map +1 -1
  309. package/package.json +476 -7
  310. package/pipeline/index.ts +12 -14
  311. package/pipeline/runtime-pipeline.ts +29 -183
  312. package/pipeline/span-proposer.ts +1 -1
  313. package/pipeline/types.ts +26 -15
  314. package/policy/defaults.ts +1 -1
  315. package/policy/policy.ts +1 -1
  316. package/policy/registry.ts +1 -1
  317. package/resolver/coverage-facts.ts +102 -0
  318. package/resolver/index.ts +5 -0
  319. package/resolver/lookup-types.ts +174 -0
  320. package/resolver/placetype-map.ts +122 -0
  321. package/resolver/postcode-format.ts +89 -0
  322. package/resolver/types.ts +293 -351
  323. package/resources/git.ts +18 -4
  324. package/resources/languages/types.gen.ts +60 -0
  325. package/resources/libpostal.ts +4 -3
  326. package/resources/whosonfirst/DataSourceCache.ts +2 -1
  327. package/resources/whosonfirst/PlacetypeDataSource.ts +36 -11
  328. package/resources/whosonfirst/loader.ts +3 -2
  329. package/resources/whosonfirst/placetypes/Placetype.ts +3 -2
  330. package/resources/whosonfirst/placetypes/admin.ts +11 -2
  331. package/resources/whosonfirst/placetypes/definition.ts +4 -3
  332. package/resources/whosonfirst/placetypes/index.ts +1 -0
  333. package/resources/whosonfirst/placetypes/specificity.ts +114 -0
  334. package/resources/whosonfirst/sharded-repo.ts +55 -2
  335. package/scripting/arguments.ts +38 -0
  336. package/scripting/command.ts +55 -0
  337. package/scripting/index.ts +3 -3
  338. package/scripting/utils/index.ts +6 -23
  339. package/tools/download-libpostal-resources.ts +4 -7
  340. package/tools/download-ssl-address.ts +28 -12
  341. package/tools/generate-language-types.ts +39 -5
  342. package/tools/index.ts +1 -2
  343. package/utils/data-root.ts +40 -14
  344. package/utils/hash.ts +5 -4
  345. package/utils/index.ts +1 -0
  346. package/utils/jsonl.ts +1 -1
  347. package/utils/repo.ts +12 -1
  348. package/utils/sealed-db.ts +30 -1
  349. package/utils/sqlite-rows.ts +40 -0
  350. package/utils/stats.ts +1 -6
  351. package/data/libpostal/dictionaries/all/chains.txt +0 -504
  352. package/kysley/adapter.ts +0 -29
  353. package/out/kysley/adapter.d.ts +0 -13
  354. package/out/kysley/adapter.d.ts.map +0 -1
  355. package/out/kysley/adapter.js +0 -25
  356. package/out/kysley/adapter.js.map +0 -1
  357. package/out/pipeline/reconcile-lookups.d.ts +0 -39
  358. package/out/pipeline/reconcile-lookups.d.ts.map +0 -1
  359. package/out/pipeline/reconcile-lookups.js +0 -88
  360. package/out/pipeline/reconcile-lookups.js.map +0 -1
  361. package/out/pipeline/reconcile.d.ts +0 -153
  362. package/out/pipeline/reconcile.d.ts.map +0 -1
  363. package/out/pipeline/reconcile.js +0 -388
  364. package/out/pipeline/reconcile.js.map +0 -1
  365. package/out/pipeline/span-logit-aggregation.d.ts +0 -58
  366. package/out/pipeline/span-logit-aggregation.d.ts.map +0 -1
  367. package/out/pipeline/span-logit-aggregation.js +0 -117
  368. package/out/pipeline/span-logit-aggregation.js.map +0 -1
  369. package/pipeline/reconcile-lookups.ts +0 -111
  370. package/pipeline/reconcile.ts +0 -583
  371. package/pipeline/span-logit-aggregation.ts +0 -156
@@ -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
package/env/index.ts CHANGED
@@ -4,6 +4,8 @@ import { loadEnvFile } from "./load.ts"
4
4
  import { cwdPathBuilder } from "./paths.ts"
5
5
  import { PrivateEnvSchema, PublicEnvSchema } from "./schema.ts"
6
6
 
7
+ export { defaultMailwomanPaths } from "./paths.ts"
8
+
7
9
  // The optional `.env` is read once (it can't change mid-process); the real environment is layered on top
8
10
  // LIVE. `process.env` can change during a process — a test stubbing a var, a late setter — and `$public` /
9
11
  // `$private` must reflect it, exactly like `process.env` itself. So each key is a getter that re-parses the
@@ -17,7 +19,7 @@ function liveEnv<Shape extends z.ZodRawShape>(schema: z.ZodObject<Shape>): z.inf
17
19
  for (const key of Object.keys(schema.shape)) {
18
20
  Object.defineProperty(view, key, {
19
21
  enumerable: true,
20
- // oxlint-disable-next-line sister-software/no-process-globals
22
+ // oxlint-disable-next-line sister-software/no-process-globals -- this module is the typed process.env boundary
21
23
  get: () => schema.parse({ ...dotEnv, ...process.env })[key as keyof z.infer<z.ZodObject<Shape>>],
22
24
  })
23
25
  }
package/env/paths.ts CHANGED
@@ -1,40 +1,28 @@
1
- import { existsSync } from "node:fs"
2
- import { homedir } from "node:os"
3
- import { dirname, resolve } from "node:path"
4
-
5
- /**
6
- * Resolve a path relative to the directory Claude Code is running in (cwd).
7
- *
8
- * @param paths Path segments to join onto cwd.
9
- */
10
- export function cwdPathBuilder(...paths: string[]): string {
11
- return resolve(process.cwd(), ...paths)
12
- }
13
-
14
1
  /**
15
- * Find all `.env` files from the current working directory up to the home directory or repo root.
16
- *
17
- * @returns An array of `.env` file paths, ordered from shallowest to deepest (cwd last).
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
18
5
  */
19
- export function cwdEnvPaths(): string[] {
20
- const found: string[] = []
21
- let dir = process.cwd()
22
- const home = homedir()
23
6
 
24
- while (true) {
25
- const candidate = resolve(dir, ".env")
7
+ import envPaths from "env-paths"
8
+ import { resolvePath } from "path-ts"
26
9
 
27
- if (existsSync(candidate)) {
28
- found.push(candidate)
29
- }
10
+ const platformPaths = envPaths("mailwoman", { suffix: "" })
30
11
 
31
- const parent = dirname(dir)
32
- const atRepoRoot = existsSync(resolve(dir, ".git"))
33
-
34
- if (atRepoRoot || dir === home || parent === dir) break
35
- dir = parent
36
- }
12
+ /**
13
+ * Platform-native filesystem defaults used by the typed environment schema.
14
+ */
15
+ export const defaultMailwomanPaths = {
16
+ data: resolvePath(platformPaths.data),
17
+ config: resolvePath(platformPaths.config),
18
+ cache: resolvePath(platformPaths.cache),
19
+ log: resolvePath(platformPaths.log),
20
+ temp: resolvePath(platformPaths.temp),
21
+ } as const
37
22
 
38
- // `found` is deepest-first (cwd first); reverse so cwd is applied last and wins.
39
- return found.toReversed()
23
+ /**
24
+ * Resolve a path relative to the current working directory.
25
+ */
26
+ export function cwdPathBuilder(...paths: string[]): string {
27
+ return resolvePath(process.cwd(), ...paths)
40
28
  }
package/env/schema.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { z } from "zod"
2
2
 
3
+ import { defaultMailwomanPaths } from "./paths.ts"
4
+
3
5
  /**
4
6
  * Wrap a coerced schema so a BLANK value means the same as an absent one.
5
7
  *
@@ -23,6 +25,10 @@ export const PublicEnvSchema = z.object({
23
25
  // `.catch` (not `.default`) so an unknown value never throws — vitest sets NODE_ENV=test, CI may set others.
24
26
  NODE_ENV: z.enum(["development", "production", "test"]).catch("development"),
25
27
  CI: z.coerce.boolean().default(false),
28
+ /**
29
+ * Operator locale override used when a request supplies no locale. Safe to expose in diagnostics.
30
+ */
31
+ MW_LOCALE: z.string().min(1).optional(),
26
32
 
27
33
  // Gazetteer / resolver database paths. CLI flags (`--resolve-db`, …) override these at the call site.
28
34
  MAILWOMAN_WOF_DB: z.string().optional(),
@@ -33,6 +39,8 @@ export const PublicEnvSchema = z.object({
33
39
  MAILWOMAN_FST_BIN: z.string().optional(),
34
40
  MAILWOMAN_MODEL_CARD: z.string().optional(),
35
41
  MAILWOMAN_COARSE_PLACER_DIR: z.string().optional(),
42
+ // PMTiles archive for the geocode --debug map pane; --tiles outranks it at the call site.
43
+ MAILWOMAN_TILES: z.string().optional(),
36
44
 
37
45
  // ONNX intra-op thread cap. Deployment-shaped rather than code-shaped: the right value depends on how many
38
46
  // mailwoman processes share the host, which the library cannot know. See DEFAULT_INTRA_OP_THREADS.
@@ -50,8 +58,12 @@ export const PublicEnvSchema = z.object({
50
58
  // Receipts: `docs/engineering/reference/performance.mdx`.
51
59
  MAILWOMAN_BATCH_MAX: blankAsAbsent(z.coerce.number().int().positive().default(1000)),
52
60
 
53
- // The gazetteer/model data root (`core/utils/data-root.ts`, `scripts/copy-weights.ts`).
54
- MAILWOMAN_DATA_ROOT: z.string().optional(),
61
+ // Platform-native application directories. Environment values override these defaults.
62
+ MAILWOMAN_DATA_ROOT: z.string().default(defaultMailwomanPaths.data),
63
+ MAILWOMAN_CONFIG_ROOT: z.string().default(defaultMailwomanPaths.config),
64
+ MAILWOMAN_CACHE_ROOT: z.string().default(defaultMailwomanPaths.cache),
65
+ MAILWOMAN_LOG_ROOT: z.string().default(defaultMailwomanPaths.log),
66
+ MAILWOMAN_TEMP_ROOT: z.string().default(defaultMailwomanPaths.temp),
55
67
 
56
68
  // Corpus source-fetch tools (`corpus/src/tools/fetch/*` — env knobs are now command flags; these remain for compat). Callers do their own numeric/boolean parsing on these,
57
69
  // so they stay raw strings — the schema only gates which keys surface, not how they're coerced.
@@ -84,13 +96,22 @@ export const PublicEnvSchema = z.object({
84
96
 
85
97
  // Release-it publish flow (`scripts/publish-workspace.ts`). The OTP is a secret — see `$private`.
86
98
  MAILWOMAN_SKIP_WEIGHTS: z.string().optional(),
99
+ /**
100
+ * Set to `0` to publish WITHOUT a sigstore provenance attestation. Provenance is otherwise on by default under GitHub
101
+ * Actions — this exists so a release blocked by a sigstore or registry outage can still ship.
102
+ */
87
103
  MAILWOMAN_NPM_PROVENANCE: z.string().optional(),
104
+ /**
105
+ * Set by GitHub Actions itself. npm can only mint a provenance attestation from a CI provider it supports, so this is
106
+ * the predicate for `--provenance` rather than the generic {@link CI} flag.
107
+ */
108
+ GITHUB_ACTIONS: z.coerce.boolean().default(false),
88
109
  RELEASE_IT_WORKSPACES_PATH_TO_WORKSPACE: z.string().optional(),
89
110
  RELEASE_IT_WORKSPACES_TAG: z.string().optional(),
90
111
  RELEASE_IT_WORKSPACES_ACCESS: z.string().optional(),
91
112
  RELEASE_IT_WORKSPACES_DRY_RUN: z.string().optional(),
92
113
 
93
- // Demo resolver (`docs/plugins/demo-assets/resolve.ts`) + docs driver.
114
+ // Demo artifact staging (`docs/plugins/demo-assets/artifacts.ts`) + docs driver.
94
115
  PLAYPEN_WOF_ADMIN_DB: z.string().optional(),
95
116
  PLAYPEN_WOF_POSTCODE_DB: z.string().optional(),
96
117
  SLIM_COUNTRIES: z.string().optional(),
@@ -141,6 +162,8 @@ export const PublicEnvSchema = z.object({
141
162
  MW_DUMP_REGRESSIONS: z.string().optional(),
142
163
  PROBE_N: z.string().optional(),
143
164
  DEBUG: z.string().optional(),
165
+ // The informal-standard color kill switch (no-color.org). chalk/Ink honor it on their own; declared here because the debug view's map pane emits raw SGR and must consult it itself — the schema strips unlisted vars.
166
+ NO_COLOR: z.string().optional(),
144
167
  })
145
168
 
146
169
  /**
@@ -0,0 +1,107 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ */
6
+
7
+ /**
8
+ * A kibibyte. Named so a `< 1024` guard reads as a threshold rather than a magic number — several fetchers use it to
9
+ * reject a response too small to be the archive they asked for.
10
+ */
11
+ export const BYTES_PER_KIB = 1024
12
+
13
+ export interface ByteFormatterOptions {
14
+ /**
15
+ * Prefix an explicit `+` on a positive value. For a DELTA, where the sign is the information. A negative always
16
+ * carries its own sign.
17
+ */
18
+ signed?: boolean
19
+ /**
20
+ * Override the formatter's locale for this call.
21
+ */
22
+ locales?: Intl.LocalesArgument
23
+ }
24
+
25
+ /**
26
+ * Byte counts as a human reads them, in whichever of the two bases the number was actually measured in.
27
+ *
28
+ * Both bases, spelled correctly. A formatter that divides by 1024 and prints `KB` is off by 2.4% at KB and 10% by TB,
29
+ * and the label is the only thing telling a reader which it did — so the choice is named at the call site:
30
+ *
31
+ * - {@linkcode ByteFormatter.formatIEC} for anything a MACHINE measured — heap, file size on disk, buffer length.
32
+ * - {@linkcode ByteFormatter.formatSI} for a size a VENDOR reports. Disk capacity, download sizes and GitHub's own API
33
+ * are quoted in powers of ten; rendering GitHub's `41.3 GB` as `38.5 GiB` is correct arithmetic and the wrong
34
+ * answer.
35
+ *
36
+ * Rendering goes through `Intl.NumberFormat`, so the unit and the decimal separator follow the locale. Pass an explicit
37
+ * locale when a caller needs a stable string — a test asserting an exact rendering, not a line printed for a human.
38
+ */
39
+ export class ByteFormatter {
40
+ public static SI_UNITS = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte"] as const
41
+ public static IEC_UNITS = ["B", "KiB", "MiB", "GiB", "TiB"] as const
42
+
43
+ public static shared = new ByteFormatter()
44
+
45
+ public static formatSI(bytes: number, options?: ByteFormatterOptions): string {
46
+ return ByteFormatter.shared.formatSI(bytes, options)
47
+ }
48
+
49
+ public static formatIEC(bytes: number, options?: ByteFormatterOptions): string {
50
+ return ByteFormatter.shared.formatIEC(bytes, options)
51
+ }
52
+
53
+ protected locales?: Intl.LocalesArgument
54
+
55
+ constructor(locales?: Intl.LocalesArgument) {
56
+ this.locales = locales
57
+ }
58
+
59
+ /**
60
+ * Formats a file size in bytes into a human-readable string with appropriate SI units (B, kB, MB, GB, TB).
61
+ *
62
+ * @param bytes The file size in bytes.
63
+ */
64
+ public formatSI(bytes: number, options?: ByteFormatterOptions): string {
65
+ const units = ByteFormatter.SI_UNITS
66
+
67
+ let value = bytes
68
+ let unit = 0
69
+
70
+ while (Math.abs(value) >= 1000 && unit < units.length - 1) {
71
+ value /= 1000
72
+
73
+ unit++
74
+ }
75
+
76
+ return new Intl.NumberFormat(options?.locales ?? this.locales, {
77
+ style: "unit",
78
+ unit: units[unit],
79
+ maximumFractionDigits: 1,
80
+ signDisplay: options?.signed ? "exceptZero" : "auto",
81
+ }).format(value)
82
+ }
83
+
84
+ /**
85
+ * Formats a file size in bytes into a human-readable string with appropriate IEC units (B, KiB, MiB, GiB, TiB).
86
+ *
87
+ * @param bytes The file size in bytes.
88
+ */
89
+ public formatIEC(bytes: number, options?: ByteFormatterOptions): string {
90
+ let value = bytes
91
+ let unit = 0
92
+
93
+ // oxlint-disable-next-line sister-software/no-unnamed-threshold -- Well known.
94
+ while (Math.abs(value) >= 1024 && unit < ByteFormatter.IEC_UNITS.length - 1) {
95
+ value /= 1024
96
+
97
+ unit++
98
+ }
99
+
100
+ const formatted = new Intl.NumberFormat(options?.locales ?? this.locales, {
101
+ maximumFractionDigits: 1,
102
+ signDisplay: options?.signed ? "exceptZero" : "auto",
103
+ }).format(value)
104
+
105
+ return `${formatted} ${ByteFormatter.IEC_UNITS[unit]}`
106
+ }
107
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ */
6
+
7
+ export * from "./formatters.ts"
package/fs/zip.ts CHANGED
@@ -60,7 +60,7 @@ export function extractSingleFileZip(data: ArrayBuffer | Buffer): Promise<Buffer
60
60
 
61
61
  // We use the async version of getData to avoid blocking the event loop.
62
62
  return new Promise<Buffer>((resolve, reject) =>
63
- // oxlint-disable-next-line no-promise-executor-return
63
+ // oxlint-disable-next-line no-promise-executor-return -- return the callback registration from this expression-bodied executor
64
64
  entry!.getDataAsync((extractedData, error) => {
65
65
  if (error) {
66
66
  reject(error)
@@ -8,10 +8,8 @@
8
8
  * corrupted layer fails loudly at open time rather than misbehaving downstream.
9
9
  */
10
10
 
11
- import type { Kysely } from "kysely"
12
-
13
11
  import { parseJSONStrict } from "../objects.ts"
14
- import { LayerFreshnessPolicy, LayerTier, type LayerContractDatabase } from "./schema.ts"
12
+ import { CoverageBasis, LayerFreshnessPolicy, LayerTier, type LayerContractHandle } from "./schema.ts"
15
13
 
16
14
  /**
17
15
  * Which spine columns a layer carries. At least one key is required.
@@ -26,6 +24,16 @@ export interface SpineKeys {
26
24
  * Column name holding `@mailwoman/address-id` keys, when present.
27
25
  */
28
26
  addressID?: string
27
+ /**
28
+ * The normalized-street column a shard is probed by, for layers keyed by STREET rather than by cell or id.
29
+ *
30
+ * Added because the contract's first three keys describe the two layer shapes that existed when it was written — a
31
+ * cellular one (`poi.db`, H3) and an id-joined one — and the situs shards are a third. `address_point` and
32
+ * `street_segment` carry no H3 cell, no WOF id and no address-id; they are probed on `(postcode | locality,
33
+ * street_norm, number)`. Declaring one of the other three for them would name a column that does not exist, in the
34
+ * field a consumer uses to join.
35
+ */
36
+ street?: { column: string }
29
37
  }
30
38
 
31
39
  /**
@@ -50,9 +58,26 @@ export interface LayerManifest {
50
58
  export interface CoverageCell {
51
59
  h3Cell: number
52
60
  completeness: number
61
+ /**
62
+ * What `completeness` rests on. A writer that omits it is declaring {@link CoverageBasis.SourcePresent} — the weakest
63
+ * reading — because a builder that has not thought about basis is recording source presence whether or not it says
64
+ * so.
65
+ */
66
+ basis?: CoverageBasis
53
67
  observedRows: number
54
68
  }
55
69
 
70
+ /**
71
+ * Whether a coverage reading can support an EXCLUSION — a claim that the thing asked for is not there.
72
+ *
73
+ * Presence is supportable from any basis. Absence is not: `source_present` records that the source returned rows, which
74
+ * says nothing about what it missed. Callers building negative evidence must gate on this rather than on `completeness`
75
+ * alone, or an exclusion fires identically on a genuinely empty cell and on one we simply never surveyed.
76
+ */
77
+ export function supportsExclusion(cell: Pick<CoverageCell, "basis">): boolean {
78
+ return cell.basis === CoverageBasis.Designated || cell.basis === CoverageBasis.Surveyed
79
+ }
80
+
56
81
  const TIERS = new Set<string>(Object.values(LayerTier))
57
82
  const POLICIES = new Set<string>(Object.values(LayerFreshnessPolicy))
58
83
 
@@ -65,15 +90,20 @@ function assertManifestInvariants(manifest: Pick<LayerManifest, "tier" | "freshn
65
90
  throw new Error(`layer manifest: unknown freshness_policy ${JSON.stringify(manifest.freshnessPolicy)}`)
66
91
  }
67
92
 
68
- if (!manifest.spineKeys.h3 && !manifest.spineKeys.wofID && !manifest.spineKeys.addressID) {
69
- throw new Error("layer manifest: at least one spine key (h3, wofID, addressID) is required")
93
+ if (
94
+ !manifest.spineKeys.h3 &&
95
+ !manifest.spineKeys.wofID &&
96
+ !manifest.spineKeys.addressID &&
97
+ !manifest.spineKeys.street
98
+ ) {
99
+ throw new Error("layer manifest: at least one spine key (h3, wofID, addressID, street) is required")
70
100
  }
71
101
  }
72
102
 
73
103
  /**
74
104
  * Insert the single manifest row. Call exactly once, from the layer's build script.
75
105
  */
76
- export async function writeLayerManifest(db: Kysely<LayerContractDatabase>, manifest: LayerManifest): Promise<void> {
106
+ export async function writeLayerManifest(db: LayerContractHandle, manifest: LayerManifest): Promise<void> {
77
107
  assertManifestInvariants(manifest)
78
108
 
79
109
  await db
@@ -99,7 +129,7 @@ export async function writeLayerManifest(db: Kysely<LayerContractDatabase>, mani
99
129
  /**
100
130
  * Read + validate the manifest. Throws if the table is empty, multi-row, or invalid.
101
131
  */
102
- export async function readLayerManifest(db: Kysely<LayerContractDatabase>): Promise<LayerManifest> {
132
+ export async function readLayerManifest(db: LayerContractHandle): Promise<LayerManifest> {
103
133
  const rows = await db.selectFrom("layer_manifest").selectAll().execute()
104
134
 
105
135
  if (rows.length !== 1) {
@@ -130,7 +160,7 @@ export async function readLayerManifest(db: Kysely<LayerContractDatabase>): Prom
130
160
  }
131
161
 
132
162
  /**
133
- * Rows per INSERT statement (3 bound params/row = 15,000 params/statement), kept safely under SQLite's default 32,766
163
+ * Rows per INSERT statement (4 bound params/row = 16,000 params/statement), kept safely under SQLite's default 32,766
134
164
  * bound-variable ceiling — a continental-scale build's res-6 coverage cell count blows past that limit in a single
135
165
  * `.values()` call (found 2026-07-19).
136
166
  */
@@ -140,7 +170,7 @@ export const COVERAGE_INSERT_BATCH = 5000
140
170
  * Bulk-insert coverage cells (build-time; cold path, so Kysely inserts are fine), chunked to stay under SQLite's
141
171
  * bound-variable limit.
142
172
  */
143
- export async function writeLayerCoverage(db: Kysely<LayerContractDatabase>, cells: CoverageCell[]): Promise<void> {
173
+ export async function writeLayerCoverage(db: LayerContractHandle, cells: CoverageCell[]): Promise<void> {
144
174
  if (!cells.length) return
145
175
 
146
176
  for (let i = 0; i < cells.length; i += COVERAGE_INSERT_BATCH) {
@@ -148,7 +178,14 @@ export async function writeLayerCoverage(db: Kysely<LayerContractDatabase>, cell
148
178
 
149
179
  await db
150
180
  .insertInto("layer_coverage")
151
- .values(batch.map((c) => ({ h3_cell: c.h3Cell, completeness: c.completeness, observed_rows: c.observedRows })))
181
+ .values(
182
+ batch.map((c) => ({
183
+ h3_cell: c.h3Cell,
184
+ completeness: c.completeness,
185
+ basis: c.basis ?? CoverageBasis.SourcePresent,
186
+ observed_rows: c.observedRows,
187
+ }))
188
+ )
152
189
  .execute()
153
190
  }
154
191
  }
@@ -157,13 +194,17 @@ export async function writeLayerCoverage(db: Kysely<LayerContractDatabase>, cell
157
194
  * Look up coverage for one short H3 cell. `undefined` = the cell was never surveyed (UNKNOWN) — callers must not
158
195
  * conflate this with `{completeness: 0}`.
159
196
  */
160
- export async function readLayerCoverage(
161
- db: Kysely<LayerContractDatabase>,
162
- h3Cell: number
163
- ): Promise<CoverageCell | undefined> {
197
+ export async function readLayerCoverage(db: LayerContractHandle, h3Cell: number): Promise<CoverageCell | undefined> {
164
198
  const row = await db.selectFrom("layer_coverage").selectAll().where("h3_cell", "=", h3Cell).executeTakeFirst()
165
199
 
166
200
  if (!row) return undefined
167
201
 
168
- return { h3Cell: row.h3_cell, completeness: row.completeness, observedRows: row.observed_rows }
202
+ return {
203
+ h3Cell: row.h3_cell,
204
+ completeness: row.completeness,
205
+ // A NULL basis is an artifact built before the column existed. It was recording source presence,
206
+ // so that is what it reads back as — never a stronger basis than the builder actually had.
207
+ basis: (row.basis as CoverageBasis | null) ?? CoverageBasis.SourcePresent,
208
+ observedRows: row.observed_rows,
209
+ }
169
210
  }