@mailwoman/corpus 11.3.0 → 11.4.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 (336) hide show
  1. package/data/PROVENANCE.md +3 -4
  2. package/data/address-source-register.json +4 -4
  3. package/data/builds/v0.8.23-overlay/OVERLAY_PLAN.json +3656 -0
  4. package/data/builds/v0.8.23-prominent-overlay/OVERLAY_PLAN.json +42 -0
  5. package/data/builds/v0.8.23-rebuild/inputs.json +141 -0
  6. package/data/builds/v0.8.24-rebuild/inputs.json +111 -0
  7. package/data/corpus-source-inputs.json +95 -1
  8. package/data/holdout/evaluation-groups.v0.8.24.json +222158 -0
  9. package/data/license-decisions.json +5 -0
  10. package/data/region-slot-second-level.json +1 -1
  11. package/data/training-configs/v8.11.0-base-c6-90k-v0823-s42.yaml +920 -0
  12. package/data/training-configs/v8.12.0-base-c6-wofreps-v0823-s42.yaml +944 -0
  13. package/data/training-configs/v8.13.0-base-c6-wofreps-v0822-s42.yaml +918 -0
  14. package/lib/adapters/cordis/adapter.ts +3 -2
  15. package/lib/adapters/gleif/adapter.ts +123 -20
  16. package/lib/adapters/openaddresses/adapter.ts +2 -2
  17. package/lib/adapters/osm/adapter.ts +62 -4
  18. package/lib/adapters/overture/adapter.ts +97 -15
  19. package/lib/adapters/overture/address-key.ts +89 -0
  20. package/lib/adapters/overture/places-in/adapter.ts +258 -0
  21. package/lib/adapters/street/convention.ts +49 -0
  22. package/lib/adapters/street/country-line.ts +65 -0
  23. package/lib/adapters/wof/admin/json/adapter.ts +4 -4
  24. package/lib/adapters/wof/admin/json/region-codes.ts +98 -0
  25. package/lib/adapters/wof/json-rows.ts +2 -2
  26. package/lib/adapters.ts +2 -0
  27. package/lib/build/align/range.ts +21 -11
  28. package/lib/build/align/totals.ts +20 -0
  29. package/lib/build/align.ts +218 -20
  30. package/lib/build/budget.ts +55 -20
  31. package/lib/build/checkpoint.ts +9 -0
  32. package/lib/build/component-check.ts +97 -3
  33. package/lib/build/inputs.ts +122 -31
  34. package/lib/build/manifest.ts +1 -0
  35. package/lib/build/parallel.ts +140 -23
  36. package/lib/build/{region-names.ts → region/names.ts} +16 -4
  37. package/lib/build/region/recount.ts +153 -0
  38. package/lib/build/reuse.ts +112 -0
  39. package/lib/build/settings.ts +84 -3
  40. package/lib/build/source-record.ts +45 -8
  41. package/lib/build/types.ts +26 -0
  42. package/lib/build/worker.ts +75 -10
  43. package/lib/build.ts +2 -1
  44. package/lib/de/recipes/locale.ts +9 -3
  45. package/lib/evaluation/corpus-adapter.ts +2 -2
  46. package/lib/fi/adapters/ryhti/adapter.ts +2 -2
  47. package/lib/fo/adapters/adressur/adapter.ts +2 -2
  48. package/lib/fr/recipes/lieudit.ts +3 -4
  49. package/lib/in/street-line/segment.ts +370 -0
  50. package/lib/international/recipes/locale.ts +6 -3
  51. package/lib/li/adapters/llv/adapter.ts +2 -2
  52. package/lib/lu/adapters/bd-adresses/adapter.ts +2 -2
  53. package/lib/no/adapters/matrikkelen/adapter.ts +2 -2
  54. package/lib/parquet/streams.ts +18 -3
  55. package/lib/parquet/writers.ts +161 -29
  56. package/lib/recipes/country-balanced.ts +32 -18
  57. package/lib/recipes/fragment-assay/build.ts +8 -5
  58. package/lib/recipes/fragment-assay/rows.ts +4 -4
  59. package/lib/recipes/house/venue.ts +2 -1
  60. package/lib/recipes/postal/guideline/leg.ts +3 -2
  61. package/lib/recipes/prominent/places.ts +4 -4
  62. package/lib/runner.ts +139 -20
  63. package/lib/sg/recipes/register.ts +6 -1
  64. package/lib/source-register/row-license.ts +12 -2
  65. package/lib/surfaces/corpus-row.ts +84 -0
  66. package/lib/surfaces/locale.ts +20 -24
  67. package/lib/synthesizers/house-venue.ts +33 -40
  68. package/lib/synthesizers/no-street.ts +2 -2
  69. package/lib/synthesizers/po-box.ts +3 -3
  70. package/lib/synthesizers/tail.ts +3 -2
  71. package/lib/synthesizers/utils.ts +95 -56
  72. package/lib/test-kit.ts +50 -1
  73. package/lib/tools/fetch/overture-places-in.ts +118 -0
  74. package/lib/tools/fetch/storage.ts +3 -0
  75. package/lib/tools/fetch.ts +2 -0
  76. package/lib/tools/region-slot-census.run.ts +2 -2
  77. package/lib/types.ts +39 -0
  78. package/lib/us/adapters/state/tx-notaries/adapter.ts +2 -1
  79. package/lib/us/adapters/usgov/imls-pls/adapter.ts +2 -1
  80. package/lib/us/adapters/usgov/nad/adapter.ts +32 -4
  81. package/lib/us/adapters/usgov/nad/address-key.ts +8 -47
  82. package/lib/us/adapters/usgov/nppes/adapter.ts +2 -1
  83. package/lib/utils/address-key.ts +57 -0
  84. package/lib/utils/align.ts +22 -10
  85. package/lib/utils/evaluation-groups.ts +13 -6
  86. package/lib/utils/split.ts +24 -3
  87. package/out/adapters/cordis/adapter.d.ts.map +1 -1
  88. package/out/adapters/cordis/adapter.js +3 -2
  89. package/out/adapters/cordis/adapter.js.map +1 -1
  90. package/out/adapters/gleif/adapter.d.ts +4 -0
  91. package/out/adapters/gleif/adapter.d.ts.map +1 -1
  92. package/out/adapters/gleif/adapter.js +100 -20
  93. package/out/adapters/gleif/adapter.js.map +1 -1
  94. package/out/adapters/openaddresses/adapter.js +2 -2
  95. package/out/adapters/openaddresses/adapter.js.map +1 -1
  96. package/out/adapters/osm/adapter.d.ts +9 -0
  97. package/out/adapters/osm/adapter.d.ts.map +1 -1
  98. package/out/adapters/osm/adapter.js +55 -13
  99. package/out/adapters/osm/adapter.js.map +1 -1
  100. package/out/adapters/overture/adapter.d.ts +6 -0
  101. package/out/adapters/overture/adapter.d.ts.map +1 -1
  102. package/out/adapters/overture/adapter.js +72 -9
  103. package/out/adapters/overture/adapter.js.map +1 -1
  104. package/out/adapters/overture/address-key.d.ts +39 -0
  105. package/out/adapters/overture/address-key.d.ts.map +1 -0
  106. package/out/adapters/overture/address-key.js +75 -0
  107. package/out/adapters/overture/address-key.js.map +1 -0
  108. package/out/adapters/overture/places-in/adapter.d.ts +20 -0
  109. package/out/adapters/overture/places-in/adapter.d.ts.map +1 -0
  110. package/out/adapters/overture/places-in/adapter.js +191 -0
  111. package/out/adapters/overture/places-in/adapter.js.map +1 -0
  112. package/out/adapters/street/convention.d.ts +11 -0
  113. package/out/adapters/street/convention.d.ts.map +1 -0
  114. package/out/adapters/street/convention.js +40 -0
  115. package/out/adapters/street/convention.js.map +1 -0
  116. package/out/adapters/street/country-line.d.ts +31 -0
  117. package/out/adapters/street/country-line.d.ts.map +1 -0
  118. package/out/adapters/street/country-line.js +31 -0
  119. package/out/adapters/street/country-line.js.map +1 -0
  120. package/out/adapters/wof/admin/json/adapter.js +4 -4
  121. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  122. package/out/adapters/wof/admin/json/region-codes.d.ts +5 -0
  123. package/out/adapters/wof/admin/json/region-codes.d.ts.map +1 -1
  124. package/out/adapters/wof/admin/json/region-codes.js +83 -0
  125. package/out/adapters/wof/admin/json/region-codes.js.map +1 -1
  126. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  127. package/out/adapters/wof/json-rows.js +2 -2
  128. package/out/adapters/wof/json-rows.js.map +1 -1
  129. package/out/adapters.d.ts.map +1 -1
  130. package/out/adapters.js +2 -0
  131. package/out/adapters.js.map +1 -1
  132. package/out/build/align/range.d.ts +7 -3
  133. package/out/build/align/range.d.ts.map +1 -1
  134. package/out/build/align/range.js +17 -9
  135. package/out/build/align/range.js.map +1 -1
  136. package/out/build/align/totals.d.ts +12 -0
  137. package/out/build/align/totals.d.ts.map +1 -1
  138. package/out/build/align/totals.js +8 -0
  139. package/out/build/align/totals.js.map +1 -1
  140. package/out/build/align.d.ts +60 -5
  141. package/out/build/align.d.ts.map +1 -1
  142. package/out/build/align.js +136 -12
  143. package/out/build/align.js.map +1 -1
  144. package/out/build/budget.d.ts +35 -11
  145. package/out/build/budget.d.ts.map +1 -1
  146. package/out/build/budget.js +52 -20
  147. package/out/build/budget.js.map +1 -1
  148. package/out/build/checkpoint.d.ts +9 -0
  149. package/out/build/checkpoint.d.ts.map +1 -1
  150. package/out/build/checkpoint.js.map +1 -1
  151. package/out/build/component-check.d.ts +29 -1
  152. package/out/build/component-check.d.ts.map +1 -1
  153. package/out/build/component-check.js +57 -2
  154. package/out/build/component-check.js.map +1 -1
  155. package/out/build/inputs.d.ts +58 -0
  156. package/out/build/inputs.d.ts.map +1 -1
  157. package/out/build/inputs.js +57 -20
  158. package/out/build/inputs.js.map +1 -1
  159. package/out/build/manifest.d.ts.map +1 -1
  160. package/out/build/manifest.js +1 -0
  161. package/out/build/manifest.js.map +1 -1
  162. package/out/build/parallel.d.ts +19 -1
  163. package/out/build/parallel.d.ts.map +1 -1
  164. package/out/build/parallel.js +92 -11
  165. package/out/build/parallel.js.map +1 -1
  166. package/out/build/{region-names.d.ts → region/names.d.ts} +10 -1
  167. package/out/build/region/names.d.ts.map +1 -0
  168. package/out/build/{region-names.js → region/names.js} +16 -5
  169. package/out/build/region/names.js.map +1 -0
  170. package/out/build/region/recount.d.ts +47 -0
  171. package/out/build/region/recount.d.ts.map +1 -0
  172. package/out/build/region/recount.js +105 -0
  173. package/out/build/region/recount.js.map +1 -0
  174. package/out/build/reuse.d.ts +31 -0
  175. package/out/build/reuse.d.ts.map +1 -0
  176. package/out/build/reuse.js +50 -0
  177. package/out/build/reuse.js.map +1 -0
  178. package/out/build/settings.d.ts +38 -0
  179. package/out/build/settings.d.ts.map +1 -1
  180. package/out/build/settings.js +67 -3
  181. package/out/build/settings.js.map +1 -1
  182. package/out/build/source-record.d.ts +32 -7
  183. package/out/build/source-record.d.ts.map +1 -1
  184. package/out/build/source-record.js +17 -4
  185. package/out/build/source-record.js.map +1 -1
  186. package/out/build/types.d.ts +24 -0
  187. package/out/build/types.d.ts.map +1 -1
  188. package/out/build/worker.d.ts +17 -0
  189. package/out/build/worker.d.ts.map +1 -1
  190. package/out/build/worker.js +44 -9
  191. package/out/build/worker.js.map +1 -1
  192. package/out/build.d.ts.map +1 -1
  193. package/out/build.js +2 -1
  194. package/out/build.js.map +1 -1
  195. package/out/de/recipes/locale.d.ts.map +1 -1
  196. package/out/de/recipes/locale.js +9 -3
  197. package/out/de/recipes/locale.js.map +1 -1
  198. package/out/evaluation/corpus-adapter.d.ts +14 -0
  199. package/out/evaluation/corpus-adapter.d.ts.map +1 -1
  200. package/out/evaluation/corpus-adapter.js +2 -16
  201. package/out/evaluation/corpus-adapter.js.map +1 -1
  202. package/out/fi/adapters/ryhti/adapter.js +2 -2
  203. package/out/fi/adapters/ryhti/adapter.js.map +1 -1
  204. package/out/fo/adapters/adressur/adapter.js +2 -2
  205. package/out/fo/adapters/adressur/adapter.js.map +1 -1
  206. package/out/fr/recipes/lieudit.d.ts.map +1 -1
  207. package/out/fr/recipes/lieudit.js +3 -3
  208. package/out/fr/recipes/lieudit.js.map +1 -1
  209. package/out/in/street-line/segment.d.ts +75 -0
  210. package/out/in/street-line/segment.d.ts.map +1 -0
  211. package/out/in/street-line/segment.js +298 -0
  212. package/out/in/street-line/segment.js.map +1 -0
  213. package/out/international/recipes/locale.d.ts.map +1 -1
  214. package/out/international/recipes/locale.js +6 -3
  215. package/out/international/recipes/locale.js.map +1 -1
  216. package/out/li/adapters/llv/adapter.js +2 -2
  217. package/out/li/adapters/llv/adapter.js.map +1 -1
  218. package/out/lu/adapters/bd-adresses/adapter.js +2 -2
  219. package/out/lu/adapters/bd-adresses/adapter.js.map +1 -1
  220. package/out/no/adapters/matrikkelen/adapter.js +2 -2
  221. package/out/no/adapters/matrikkelen/adapter.js.map +1 -1
  222. package/out/parquet/streams.d.ts +9 -0
  223. package/out/parquet/streams.d.ts.map +1 -1
  224. package/out/parquet/streams.js +7 -3
  225. package/out/parquet/streams.js.map +1 -1
  226. package/out/parquet/writers.d.ts +55 -2
  227. package/out/parquet/writers.d.ts.map +1 -1
  228. package/out/parquet/writers.js +109 -27
  229. package/out/parquet/writers.js.map +1 -1
  230. package/out/recipes/country-balanced.d.ts.map +1 -1
  231. package/out/recipes/country-balanced.js +32 -15
  232. package/out/recipes/country-balanced.js.map +1 -1
  233. package/out/recipes/fragment-assay/build.d.ts +5 -2
  234. package/out/recipes/fragment-assay/build.d.ts.map +1 -1
  235. package/out/recipes/fragment-assay/build.js +8 -5
  236. package/out/recipes/fragment-assay/build.js.map +1 -1
  237. package/out/recipes/fragment-assay/rows.d.ts +2 -2
  238. package/out/recipes/fragment-assay/rows.js +4 -4
  239. package/out/recipes/fragment-assay/rows.js.map +1 -1
  240. package/out/recipes/house/venue.d.ts.map +1 -1
  241. package/out/recipes/house/venue.js +2 -1
  242. package/out/recipes/house/venue.js.map +1 -1
  243. package/out/recipes/postal/guideline/leg.d.ts.map +1 -1
  244. package/out/recipes/postal/guideline/leg.js +2 -7
  245. package/out/recipes/postal/guideline/leg.js.map +1 -1
  246. package/out/recipes/prominent/places.js +4 -4
  247. package/out/recipes/prominent/places.js.map +1 -1
  248. package/out/runner.d.ts +28 -0
  249. package/out/runner.d.ts.map +1 -1
  250. package/out/runner.js +85 -18
  251. package/out/runner.js.map +1 -1
  252. package/out/sg/recipes/register.d.ts.map +1 -1
  253. package/out/sg/recipes/register.js +6 -1
  254. package/out/sg/recipes/register.js.map +1 -1
  255. package/out/source-register/row-license.d.ts +8 -0
  256. package/out/source-register/row-license.d.ts.map +1 -1
  257. package/out/source-register/row-license.js +10 -2
  258. package/out/source-register/row-license.js.map +1 -1
  259. package/out/surfaces/corpus-row.d.ts +32 -0
  260. package/out/surfaces/corpus-row.d.ts.map +1 -0
  261. package/out/surfaces/corpus-row.js +70 -0
  262. package/out/surfaces/corpus-row.js.map +1 -0
  263. package/out/surfaces/locale.d.ts.map +1 -1
  264. package/out/surfaces/locale.js +16 -22
  265. package/out/surfaces/locale.js.map +1 -1
  266. package/out/synthesizers/house-venue.d.ts +9 -4
  267. package/out/synthesizers/house-venue.d.ts.map +1 -1
  268. package/out/synthesizers/house-venue.js +25 -35
  269. package/out/synthesizers/house-venue.js.map +1 -1
  270. package/out/synthesizers/no-street.js +2 -9
  271. package/out/synthesizers/no-street.js.map +1 -1
  272. package/out/synthesizers/po-box.js +3 -7
  273. package/out/synthesizers/po-box.js.map +1 -1
  274. package/out/synthesizers/tail.d.ts.map +1 -1
  275. package/out/synthesizers/tail.js +2 -2
  276. package/out/synthesizers/tail.js.map +1 -1
  277. package/out/synthesizers/utils.d.ts +20 -3
  278. package/out/synthesizers/utils.d.ts.map +1 -1
  279. package/out/synthesizers/utils.js +74 -57
  280. package/out/synthesizers/utils.js.map +1 -1
  281. package/out/test-kit.d.ts +11 -0
  282. package/out/test-kit.d.ts.map +1 -1
  283. package/out/test-kit.js +30 -1
  284. package/out/test-kit.js.map +1 -1
  285. package/out/tools/fetch/overture-places-in.d.ts +14 -0
  286. package/out/tools/fetch/overture-places-in.d.ts.map +1 -0
  287. package/out/tools/fetch/overture-places-in.js +82 -0
  288. package/out/tools/fetch/overture-places-in.js.map +1 -0
  289. package/out/tools/fetch/storage.d.ts +2 -0
  290. package/out/tools/fetch/storage.d.ts.map +1 -1
  291. package/out/tools/fetch/storage.js +3 -0
  292. package/out/tools/fetch/storage.js.map +1 -1
  293. package/out/tools/fetch.d.ts +2 -0
  294. package/out/tools/fetch.d.ts.map +1 -1
  295. package/out/tools/fetch.js +2 -0
  296. package/out/tools/fetch.js.map +1 -1
  297. package/out/tools/region-slot-census.run.js +2 -2
  298. package/out/types.d.ts +41 -0
  299. package/out/types.d.ts.map +1 -1
  300. package/out/types.js.map +1 -1
  301. package/out/us/adapters/state/tx-notaries/adapter.d.ts.map +1 -1
  302. package/out/us/adapters/state/tx-notaries/adapter.js +2 -1
  303. package/out/us/adapters/state/tx-notaries/adapter.js.map +1 -1
  304. package/out/us/adapters/usgov/imls-pls/adapter.d.ts.map +1 -1
  305. package/out/us/adapters/usgov/imls-pls/adapter.js +2 -1
  306. package/out/us/adapters/usgov/imls-pls/adapter.js.map +1 -1
  307. package/out/us/adapters/usgov/nad/adapter.d.ts +15 -0
  308. package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
  309. package/out/us/adapters/usgov/nad/adapter.js +21 -3
  310. package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
  311. package/out/us/adapters/usgov/nad/address-key.d.ts +1 -5
  312. package/out/us/adapters/usgov/nad/address-key.d.ts.map +1 -1
  313. package/out/us/adapters/usgov/nad/address-key.js +8 -33
  314. package/out/us/adapters/usgov/nad/address-key.js.map +1 -1
  315. package/out/us/adapters/usgov/nppes/adapter.d.ts.map +1 -1
  316. package/out/us/adapters/usgov/nppes/adapter.js +2 -1
  317. package/out/us/adapters/usgov/nppes/adapter.js.map +1 -1
  318. package/out/utils/address-key.d.ts +23 -0
  319. package/out/utils/address-key.d.ts.map +1 -0
  320. package/out/utils/address-key.js +43 -0
  321. package/out/utils/address-key.js.map +1 -0
  322. package/out/utils/align.d.ts +7 -0
  323. package/out/utils/align.d.ts.map +1 -1
  324. package/out/utils/align.js +18 -8
  325. package/out/utils/align.js.map +1 -1
  326. package/out/utils/evaluation-groups.d.ts +5 -1
  327. package/out/utils/evaluation-groups.d.ts.map +1 -1
  328. package/out/utils/evaluation-groups.js +12 -1
  329. package/out/utils/evaluation-groups.js.map +1 -1
  330. package/out/utils/split.d.ts +11 -0
  331. package/out/utils/split.d.ts.map +1 -1
  332. package/out/utils/split.js +18 -3
  333. package/out/utils/split.js.map +1 -1
  334. package/package.json +19 -19
  335. package/out/build/region-names.d.ts.map +0 -1
  336. package/out/build/region-names.js.map +0 -1
@@ -24,13 +24,15 @@ import { PathBuilder, type PathBuilderLike } from "path-ts"
24
24
  import { JSONSpliterator } from "spliterator"
25
25
 
26
26
  import { createUnitSplitter } from "#adapters/street/unit"
27
- import { alignSettings, createRowAligner, emptyAlignResult, type AlignTally } from "#build/align"
27
+ import { alignSettings, createRowAligner, emptyAlignResult, labeledSourceID, type AlignTally } from "#build/align"
28
28
  import { alignTotalsOf, serializeAlignTotals, type AlignTotalsRecord } from "#build/align/totals"
29
29
  import type { AlignSettings } from "#build/checkpoint"
30
30
  import { createIneligibilityReader } from "#build/eligibility"
31
- import { tryReadDefaultRegionNames } from "#build/region-names"
31
+ import { tryReadDefaultRegionNames } from "#build/region/names"
32
32
  import type { BuildProfile } from "#build/types"
33
+ import { encodeStagedRow, stageRow } from "#parquet/writers"
33
34
  import { flushStream, once } from "#runner"
35
+ import { committedRowLicenses } from "#source-register/row-license"
34
36
  import type { CanonicalRow } from "#types"
35
37
  import { SPLIT_NAMES, type CountryHoldout, type SplitName } from "#utils/split"
36
38
 
@@ -56,11 +58,14 @@ export interface AlignRangePlan {
56
58
 
57
59
  /**
58
60
  * The files one range's task writes, by name.
61
+ *
62
+ * `staged-<split>.txt` holds each labeled row as {@linkcode encodeStagedRow} writes it, so the finish
63
+ * worker shuffles and writes the parquet staging lines without parsing a row or serializing it again.
59
64
  */
60
65
  export const ALIGN_RANGE_OUTPUTS = [
61
- "labeled-train.jsonl",
62
- "labeled-val.jsonl",
63
- "labeled-test.jsonl",
66
+ "staged-train.txt",
67
+ "staged-val.txt",
68
+ "staged-test.txt",
64
69
  "quarantine.jsonl",
65
70
  "source-ids-train.txt",
66
71
  "source-ids-val.txt",
@@ -169,6 +174,7 @@ export interface AlignRangeOptions {
169
174
  licensePolicy: LicensePolicy
170
175
  excludeLicenses: readonly RegExp[]
171
176
  holdouts: Record<string, CountryHoldout>
177
+ trainShares?: Readonly<Record<string, number>>
172
178
  eligibility: ReadonlyMap<string, readonly string[]> | null
173
179
  }
174
180
 
@@ -215,6 +221,7 @@ export async function runAlignRange(opts: AlignRangeOptions): Promise<AlignRange
215
221
 
216
222
  const ineligibility = createIneligibilityReader(opts.eligibility)
217
223
  const licenses = createLicenseVerdictCache(opts.licensePolicy, opts.excludeLicenses)
224
+ const rowLicenses = await committedRowLicenses()
218
225
  const result = emptyAlignResult(licenses, ineligibility.refused)
219
226
 
220
227
  const tally: AlignTally = {
@@ -242,6 +249,7 @@ export async function runAlignRange(opts: AlignRangeOptions): Promise<AlignRange
242
249
  const alignOne = createRowAligner({
243
250
  synthesize: opts.synthesize,
244
251
  holdouts: opts.holdouts,
252
+ trainShares: opts.trainShares,
245
253
  licenses,
246
254
  ineligibility,
247
255
  units: await createUnitSplitter(),
@@ -249,8 +257,10 @@ export async function runAlignRange(opts: AlignRangeOptions): Promise<AlignRange
249
257
  result,
250
258
  tally,
251
259
  sinks: {
252
- labeled: (split: SplitName, line, sourceID) => {
253
- write(`labeled-${split}.jsonl`, line)
260
+ labeled: (split: SplitName, row) => {
261
+ write(`staged-${split}.txt`, encodeStagedRow(stageRow(row, rowLicenses, "align range")))
262
+
263
+ const sourceID = labeledSourceID(row)
254
264
 
255
265
  if (sourceID != null) {
256
266
  write(`source-ids-${split}.txt`, `${sourceID}\n`)
@@ -291,19 +301,19 @@ export async function runAlignRange(opts: AlignRangeOptions): Promise<AlignRange
291
301
  }
292
302
 
293
303
  /**
294
- * Each split's labeled files and source-id lists across a source's ranges, in range order.
304
+ * Each split's staged-row files and source-id lists across a source's ranges, in range order.
295
305
  */
296
306
  export function alignRangeSplitFiles(
297
307
  sourceDir: PathBuilderLike,
298
308
  plans: readonly AlignRangePlan[]
299
- ): Record<SplitName, { labeled: PathBuilder[]; sourceIDs: PathBuilder[] }> {
309
+ ): Record<SplitName, { staged: PathBuilder[]; sourceIDs: PathBuilder[] }> {
300
310
  return Object.fromEntries(
301
311
  SPLIT_NAMES.map((split) => [
302
312
  split,
303
313
  {
304
- labeled: plans.map((plan) => alignRangeDirectory(sourceDir, plan)(`labeled-${split}.jsonl`)),
314
+ staged: plans.map((plan) => alignRangeDirectory(sourceDir, plan)(`staged-${split}.txt`)),
305
315
  sourceIDs: plans.map((plan) => alignRangeDirectory(sourceDir, plan)(`source-ids-${split}.txt`)),
306
316
  },
307
317
  ])
308
- ) as Record<SplitName, { labeled: PathBuilder[]; sourceIDs: PathBuilder[] }>
318
+ ) as Record<SplitName, { staged: PathBuilder[]; sourceIDs: PathBuilder[] }>
309
319
  }
@@ -27,6 +27,7 @@ export interface AlignTotals extends AlignTally {
27
27
  licenseCounts: Map<string, number>
28
28
  refusedByKind: Map<LicenseRefusalKind, number>
29
29
  refusedBySource: Map<string, number>
30
+ trainRowsLeftOutBySource: Map<string, number>
30
31
  rowsBySource: Map<string, { rows: number; license: string }>
31
32
  ineligibleSources: Map<string, readonly string[]>
32
33
  /**
@@ -35,6 +36,7 @@ export interface AlignTotals extends AlignTally {
35
36
  refusedValues: Map<string, LicenseRefusalKind>
36
37
  componentFindingsBySource: ComponentFindingCounts
37
38
  uncheckedComponentRules: Set<ComponentRule>
39
+ regionValues: Map<string, number>
38
40
  }
39
41
 
40
42
  /**
@@ -50,11 +52,21 @@ export interface AlignTotalsRecord {
50
52
  license_counts: [string, number][]
51
53
  refused_by_kind: [LicenseRefusalKind, number][]
52
54
  refused_by_source: [string, number][]
55
+ /**
56
+ * Absent from a record written before `trainShare` existed.
57
+ */
58
+ train_rows_left_out_by_source?: [string, number][]
53
59
  rows_by_source: [string, { rows: number; license: string }][]
54
60
  ineligible_sources: [string, readonly string[]][]
55
61
  refused_license_values: [string, LicenseRefusalKind][]
56
62
  component_findings_by_source: [string, [ComponentRule, number][]][]
57
63
  unchecked_component_rules: ComponentRule[]
64
+ /**
65
+ * The rows that reached the component check with a region, per `regionValueKey`.
66
+ *
67
+ * Absent from a record written before the region data left the source derivation.
68
+ */
69
+ region_values?: [string, number][]
58
70
  }
59
71
 
60
72
  /**
@@ -71,11 +83,13 @@ export function alignTotalsOf(result: AlignPhaseResult): AlignTotals {
71
83
  licenseCounts: result.licenseCounts,
72
84
  refusedByKind: result.refusedByKind,
73
85
  refusedBySource: result.refusedBySource,
86
+ trainRowsLeftOutBySource: result.trainRowsLeftOutBySource,
74
87
  rowsBySource: result.rowsBySource,
75
88
  ineligibleSources: result.ineligibleSources,
76
89
  refusedValues: result.licenses.refusedValues(),
77
90
  componentFindingsBySource: result.componentFindingsBySource,
78
91
  uncheckedComponentRules: result.uncheckedComponentRules,
92
+ regionValues: result.regionValues,
79
93
  }
80
94
  }
81
95
 
@@ -90,11 +104,13 @@ export function serializeAlignTotals(totals: AlignTotals): AlignTotalsRecord {
90
104
  license_counts: [...totals.licenseCounts],
91
105
  refused_by_kind: [...totals.refusedByKind],
92
106
  refused_by_source: [...totals.refusedBySource],
107
+ train_rows_left_out_by_source: [...totals.trainRowsLeftOutBySource],
93
108
  rows_by_source: [...totals.rowsBySource],
94
109
  ineligible_sources: [...totals.ineligibleSources],
95
110
  refused_license_values: [...totals.refusedValues],
96
111
  component_findings_by_source: [...totals.componentFindingsBySource].map(([source, rules]) => [source, [...rules]]),
97
112
  unchecked_component_rules: [...totals.uncheckedComponentRules],
113
+ region_values: [...totals.regionValues],
98
114
  }
99
115
  }
100
116
 
@@ -132,11 +148,13 @@ export function mergeAlignTotals(records: readonly AlignTotalsRecord[]): AlignTo
132
148
  licenseCounts: new Map(),
133
149
  refusedByKind: new Map(),
134
150
  refusedBySource: new Map(),
151
+ trainRowsLeftOutBySource: new Map(),
135
152
  rowsBySource: new Map(),
136
153
  ineligibleSources: new Map(),
137
154
  refusedValues: new Map(),
138
155
  componentFindingsBySource: new Map(),
139
156
  uncheckedComponentRules: new Set(),
157
+ regionValues: new Map(),
140
158
  }
141
159
 
142
160
  for (const record of records) {
@@ -153,6 +171,8 @@ export function mergeAlignTotals(records: readonly AlignTotalsRecord[]): AlignTo
153
171
  addCounts(totals.licenseCounts, record.license_counts)
154
172
  addCounts(totals.refusedByKind, record.refused_by_kind)
155
173
  addCounts(totals.refusedBySource, record.refused_by_source)
174
+ addCounts(totals.trainRowsLeftOutBySource, record.train_rows_left_out_by_source ?? [])
175
+ addCounts(totals.regionValues, record.region_values ?? [])
156
176
 
157
177
  for (const [source, contributed] of record.rows_by_source) {
158
178
  const before = totals.rowsBySource.get(source)
@@ -21,6 +21,7 @@ import {
21
21
  } from "@mailwoman/license/policy"
22
22
  import type { PathBuilder } from "path-ts"
23
23
 
24
+ import { recordIDOf } from "#adapters/record-id"
24
25
  import { createUnitSplitter, type UnitSplitter } from "#adapters/street/unit"
25
26
  import {
26
27
  ALIGN_OUTPUTS,
@@ -32,21 +33,30 @@ import {
32
33
  } from "#build/checkpoint"
33
34
  import {
34
35
  componentFindings,
36
+ createComponentCheckMemo,
35
37
  componentFindingsRecord,
36
38
  ComponentRule,
37
39
  ENFORCED_COMPONENT_RULES,
40
+ regionValueKey,
38
41
  type ComponentFindingCounts,
39
42
  } from "#build/component-check"
40
43
  import { createIneligibilityReader } from "#build/eligibility"
41
- import { tryReadDefaultRegionNames, type RegionNames } from "#build/region-names"
44
+ import { tryReadDefaultRegionNames, type RegionNames } from "#build/region/names"
42
45
  import { streamJSONL } from "#build/rows"
43
46
  import type { BuildProfile, BuildStage } from "#build/types"
44
47
  import { flushStream, once, type AdapterRunManifest } from "#runner"
45
48
  import { defaultAugmentationsForCountry, synthesizeRow } from "#synthesizers/utils"
46
- import type { CanonicalRow } from "#types"
47
- import { alignRow } from "#utils/align"
49
+ import type { AdapterOptions, CanonicalRow, LabeledRow } from "#types"
50
+ import { alignRow, nfcComponents } from "#utils/align"
48
51
  import type { SplitRetention } from "#utils/retention"
49
- import { SPLIT_NAMES, splitForRow, type CountryHoldout, type SplitName } from "#utils/split"
52
+ import {
53
+ hashBucket,
54
+ holdoutReadsOnlyPostcode,
55
+ SPLIT_NAMES,
56
+ splitForRow,
57
+ type CountryHoldout,
58
+ type SplitName,
59
+ } from "#utils/split"
50
60
 
51
61
  export interface AlignPhaseOptions {
52
62
  /**
@@ -61,6 +71,11 @@ export interface AlignPhaseOptions {
61
71
  excludeLicenses: readonly RegExp[]
62
72
  holdouts: Record<string, CountryHoldout>
63
73
 
74
+ /**
75
+ * Each capped source's `trainShare`, from {@linkcode trainSharesOf}.
76
+ */
77
+ trainShares?: Readonly<Record<string, number>>
78
+
64
79
  /**
65
80
  * The retention table applied to every aligned row, when one was registered
66
81
  * (HOLDOUT_AND_EVALUATION section 5.1).
@@ -111,6 +126,14 @@ export interface AlignPhaseResult {
111
126
  * and one present in model provenance.
112
127
  */
113
128
  refusedBySource: Map<string, number>
129
+ /**
130
+ * Training draws each source's `trainShare` left out: a row it dropped,
131
+ * or an augmentation it kept from running.
132
+ *
133
+ * A skipped augmentation would have written one row or none.
134
+ * Validation and test rows are never left out.
135
+ */
136
+ trainRowsLeftOutBySource: Map<string, number>
114
137
  excludedByLicense: number
115
138
  admittedUnresolved: number
116
139
  excludedByEligibility: number
@@ -129,6 +152,14 @@ export interface AlignPhaseResult {
129
152
  * Their counts in {@linkcode AlignPhaseResult.componentFindingsBySource} are absent rather than zero.
130
153
  */
131
154
  uncheckedComponentRules: Set<ComponentRule>
155
+ /**
156
+ * The base rows that reached the component check with a region, per {@linkcode regionValueKey}.
157
+ *
158
+ * A build that reuses a source under other region data recounts `region:unknown` from these.
159
+ * The serial build's checkpoint doesn't record them, because only a parallel
160
+ * build's source records are reused.
161
+ */
162
+ regionValues: Map<string, number>
132
163
 
133
164
  /**
134
165
  * The source-id lists this phase wrote, or `null` when it wrote none.
@@ -186,6 +217,10 @@ function restoreTally(checkpoint: AlignCheckpoint, result: AlignPhaseResult): Al
186
217
  result.refusedByKind.set(kind as LicenseRefusalKind, count)
187
218
  }
188
219
 
220
+ for (const [source, count] of Object.entries(checkpoint.train_rows_left_out_by_source ?? {})) {
221
+ result.trainRowsLeftOutBySource.set(source, count)
222
+ }
223
+
189
224
  for (const [source, count] of Object.entries(checkpoint.refused_by_source)) {
190
225
  result.refusedBySource.set(source, count)
191
226
  }
@@ -223,13 +258,18 @@ function restoreTally(checkpoint: AlignCheckpoint, result: AlignPhaseResult): Al
223
258
 
224
259
  /**
225
260
  * The settings an align checkpoint or completion record is written under.
261
+ *
262
+ * `train_shares` is present only when a source is capped, so a build without
263
+ * one writes the settings it always has.
226
264
  */
227
265
  export function alignSettings(
228
266
  opts: Pick<
229
267
  AlignPhaseOptions,
230
- "corpusVersion" | "synthesize" | "licensePolicy" | "excludeLicenses" | "profile" | "holdouts"
268
+ "corpusVersion" | "synthesize" | "licensePolicy" | "excludeLicenses" | "profile" | "holdouts" | "trainShares"
231
269
  >
232
270
  ): AlignSettings {
271
+ const shares = Object.entries(opts.trainShares ?? {}).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
272
+
233
273
  return {
234
274
  corpus_version: opts.corpusVersion,
235
275
  synthesize: opts.synthesize,
@@ -237,17 +277,86 @@ export function alignSettings(
237
277
  exclude_licenses: opts.excludeLicenses.map((pattern) => pattern.source).toSorted(),
238
278
  profile: opts.profile,
239
279
  holdout_digest: sha256Hex(stringifyJSON(opts.holdouts)),
280
+ ...(shares.length ? { train_shares: Object.fromEntries(shares) } : {}),
240
281
  }
241
282
  }
242
283
 
284
+ /**
285
+ * The salt that separates the `trainShare` draw from the hash that sends a
286
+ * held-out record to validation or test.
287
+ */
288
+ const TRAIN_SHARE_SALT = "train-share"
289
+
290
+ /**
291
+ * Hash buckets the `trainShare` draw resolves a share to: one per million.
292
+ */
293
+ const TRAIN_SHARE_BUCKETS = 1_000_000
294
+
295
+ /**
296
+ * One training record in 1,024 runs every augmentation, so the assertion that its
297
+ * variants train checks the rows a draw before synthesis never makes.
298
+ */
299
+ const AUDITED_DRAW_RECORDS = 1024
300
+
301
+ /**
302
+ * The salt of the draw that picks the audited records, apart from the share's own draw.
303
+ */
304
+ const DRAW_AUDIT_SALT = "train-share-audit"
305
+
306
+ /**
307
+ * Whether a training row whose source keeps `share` of its training rows is kept.
308
+ *
309
+ * The draw reads `recordIDOf(sourceID)`, which keeps the id up to its last `#`.
310
+ * A source whose ids hold no `#`, as NAD's don't, draws once per row: a variant synthesized
311
+ * from a record draws apart from its base row, so a kept record contributes some of its forms.
312
+ *
313
+ * Under a share of 0.11, 57% of NAD's training records keep at least one row.
314
+ */
315
+ export function keepsTrainRow(sourceID: string, share: number): boolean {
316
+ if (share >= 1) return true
317
+
318
+ return hashBucket(`${TRAIN_SHARE_SALT}|${recordIDOf(sourceID)}`, TRAIN_SHARE_BUCKETS) < share * TRAIN_SHARE_BUCKETS
319
+ }
320
+
321
+ /**
322
+ * Each configured source's `trainShare`, for the align phase.
323
+ */
324
+ export function trainSharesOf(adapterInputs: Readonly<Record<string, AdapterOptions>>): Record<string, number> {
325
+ return Object.fromEntries(
326
+ Object.entries(adapterInputs).flatMap(([source, options]) =>
327
+ typeof options.trainShare === "number" ? [[source, options.trainShare]] : []
328
+ )
329
+ )
330
+ }
331
+
332
+ /**
333
+ * One labeled row as a line of a labeled JSONL file.
334
+ */
335
+ export function labeledLine(row: LabeledRow): string {
336
+ return `${stringifyJSON(row)}\n`
337
+ }
338
+
339
+ /**
340
+ * The `source_id` a labeled row contributes to its split's id list, or `null` when it has none.
341
+ *
342
+ * The split phase once parsed every labeled row back for its `source_id`.
343
+ * The same guard it applied keeps the list identical to what that parse produced.
344
+ */
345
+ export function labeledSourceID(row: LabeledRow): string | null {
346
+ return typeof row.source_id === "string" ? row.source_id : null
347
+ }
348
+
243
349
  /**
244
350
  * Where one aligned row's output goes.
245
351
  */
246
352
  export interface RowAlignerSinks {
247
353
  /**
248
- * One labeled JSONL line for `split`, with the row's `source_id`, or `null` when the row has none.
354
+ * One labeled row for `split`.
355
+ *
356
+ * A sink that writes labeled JSONL calls {@linkcode labeledLine} and {@linkcode labeledSourceID}.
357
+ * A parallel build's align range stages the row for its parquet part instead.
249
358
  */
250
- labeled: (split: SplitName, line: string, sourceID: string | null) => void
359
+ labeled: (split: SplitName, row: LabeledRow) => void
251
360
  /**
252
361
  * One quarantine JSONL line.
253
362
  */
@@ -262,6 +371,10 @@ export interface RowAlignerOptions {
262
371
  * (HOLDOUT_AND_EVALUATION section 5.1).
263
372
  */
264
373
  retention?: SplitRetention
374
+ /**
375
+ * Each capped source's `trainShare` (`AdapterOptions.trainShare`).
376
+ */
377
+ trainShares?: Readonly<Record<string, number>>
265
378
  licenses: LicenseVerdictCache
266
379
  ineligibility: ReturnType<typeof createIneligibilityReader>
267
380
  /**
@@ -293,7 +406,7 @@ export interface RowAlignerOptions {
293
406
  */
294
407
  export function createRowAligner(opts: RowAlignerOptions): (row: CanonicalRow) => void {
295
408
  const { result, tally, sinks, licenses, ineligibility } = opts
296
- const context = { units: opts.units, regions: opts.regions }
409
+ const context = { units: opts.units, regions: opts.regions, memo: createComponentCheckMemo() }
297
410
 
298
411
  if (!opts.regions) {
299
412
  result.uncheckedComponentRules.add(ComponentRule.RegionUnknown)
@@ -342,6 +455,15 @@ export function createRowAligner(opts: RowAlignerOptions): (row: CanonicalRow) =
342
455
  return
343
456
  }
344
457
 
458
+ // The value `region:unknown` reads, counted under the condition the rule checks it.
459
+ const region = row.components.region
460
+
461
+ if (region) {
462
+ const key = regionValueKey(row.country, region)
463
+
464
+ result.regionValues.set(key, (result.regionValues.get(key) ?? 0) + 1)
465
+ }
466
+
345
467
  // A row whose values fail its country's rules or contradict each other (#2898) is
346
468
  // quarantined before augmentation, so no variant inherits the wrong value.
347
469
  const findings = componentFindings(row.components, row.country, context)
@@ -372,15 +494,80 @@ export function createRowAligner(opts: RowAlignerOptions): (row: CanonicalRow) =
372
494
 
373
495
  result.rowsBySource.set(row.source, { rows: (contributed?.rows ?? 0) + 1, license: row.license })
374
496
 
497
+ const leaveOut = (source: string): void => {
498
+ result.trainRowsLeftOutBySource.set(source, (result.trainRowsLeftOutBySource.get(source) ?? 0) + 1)
499
+ }
500
+
501
+ const baseShare = opts.trainShares?.[row.source]
502
+
503
+ // A training base row in a country whose holdout reads only postcodes has variants that
504
+ // all train (`holdoutReadsOnlyPostcode`), so each variant's share is drawn from its id
505
+ // before the augmentation runs, and an augmentation the share leaves out is never run.
506
+ // One record in AUDITED_DRAW_RECORDS runs every augmentation, so the assertion
507
+ // below checks the premise on rows the shortcut never makes.
508
+ const variantsTrain =
509
+ typeof baseShare === "number" &&
510
+ !opts.retention &&
511
+ holdoutReadsOnlyPostcode(opts.holdouts[row.country]) &&
512
+ splitForRow({ ...row, components: nfcComponents(row.components) }, opts.holdouts) === "train"
513
+
514
+ const drawsFirst = variantsTrain && hashBucket(`${DRAW_AUDIT_SALT}|${row.source_id}`, AUDITED_DRAW_RECORDS) !== 0
515
+
516
+ const wants = drawsFirst
517
+ ? (method: string): boolean => {
518
+ const kept = keepsTrainRow(`${row.source_id}+${method}`, baseShare)
519
+
520
+ if (!kept) {
521
+ leaveOut(row.source)
522
+ }
523
+
524
+ return kept
525
+ }
526
+ : undefined
527
+
375
528
  const fanned: CanonicalRow[] = [row]
376
529
 
377
530
  if (opts.synthesize) {
378
- for (const aug of synthesizeRow(row, defaultAugmentationsForCountry(row.country))) {
531
+ for (const aug of synthesizeRow(row, defaultAugmentationsForCountry(row.country), wants)) {
532
+ // The draw above read `${source_id}+${method}`, so a variant written under another id drew for the wrong row.
533
+ if (drawsFirst && aug.source_id !== `${row.source_id}+${aug.augmentation?.name}`) {
534
+ throw new Error(
535
+ `createRowAligner: variant ${aug.source_id} of ${row.source_id} doesn't carry its method's id, ` +
536
+ `so its training share was drawn for another id`
537
+ )
538
+ }
539
+
379
540
  fanned.push(aug)
380
541
  }
381
542
  }
382
543
 
383
544
  for (const r of fanned) {
545
+ const share = opts.trainShares?.[r.source]
546
+
547
+ // A capped source decides each row's split before aligning it, so a training
548
+ // row the share leaves out is never aligned.
549
+ // `alignRow` changes only the text's normal form.
550
+ // The split reads the components in that form, so the decision equals the one made after alignment.
551
+ // Retention reads the aligned row's span tags, so a build with a retention
552
+ // table decides after alignment.
553
+ const early =
554
+ typeof share === "number" && !opts.retention
555
+ ? splitForRow({ ...r, components: nfcComponents(r.components) }, opts.holdouts)
556
+ : null
557
+
558
+ if (variantsTrain && early !== "train") {
559
+ throw new Error(
560
+ `createRowAligner: variant ${r.source_id} of a training row splits ${String(early)} under a holdout that ` +
561
+ `reads only postcodes, so drawing its share before synthesis would have dropped a held-out row`
562
+ )
563
+ }
564
+
565
+ if (early === "train" && !keepsTrainRow(r.source_id, share!)) {
566
+ leaveOut(r.source)
567
+
568
+ continue
569
+ }
570
+
384
571
  let aligned: ReturnType<typeof alignRow>
385
572
 
386
573
  try {
@@ -396,15 +583,20 @@ export function createRowAligner(opts: RowAlignerOptions): (row: CanonicalRow) =
396
583
  }
397
584
 
398
585
  if (aligned.kind === "labeled") {
399
- const split = splitForRow(aligned.row, opts.holdouts, undefined, opts.retention)
400
-
401
- // The split phase used to parse every labeled row back for its `source_id`.
402
- // The same guard it applied keeps the list identical to what that parse produced.
403
- sinks.labeled(
404
- split,
405
- `${stringifyJSON(aligned.row)}\n`,
406
- typeof aligned.row.source_id === "string" ? aligned.row.source_id : null
407
- )
586
+ const split = early ?? splitForRow(aligned.row, opts.holdouts, undefined, opts.retention)
587
+
588
+ if (
589
+ early === null &&
590
+ split === "train" &&
591
+ typeof share === "number" &&
592
+ !keepsTrainRow(aligned.row.source_id, share)
593
+ ) {
594
+ leaveOut(aligned.row.source)
595
+
596
+ continue
597
+ }
598
+
599
+ sinks.labeled(split, aligned.row)
408
600
 
409
601
  result.counts[split]++
410
602
 
@@ -435,10 +627,12 @@ export function emptyAlignResult(
435
627
  admittedUnresolved: 0,
436
628
  excludedByEligibility: 0,
437
629
  refusedBySource: new Map(),
630
+ trainRowsLeftOutBySource: new Map(),
438
631
  rowsBySource: new Map(),
439
632
  ineligibleSources,
440
633
  componentFindingsBySource: new Map(),
441
634
  uncheckedComponentRules: new Set(),
635
+ regionValues: new Map(),
442
636
  sourceIDPaths: null,
443
637
  licenses,
444
638
  }
@@ -536,6 +730,7 @@ export async function runAlignPhase(opts: AlignPhaseOptions): Promise<AlignPhase
536
730
  synthesize: opts.synthesize,
537
731
  holdouts: opts.holdouts,
538
732
  retention: opts.retention,
733
+ trainShares: opts.trainShares,
539
734
  licenses,
540
735
  ineligibility,
541
736
  units: await createUnitSplitter(),
@@ -543,8 +738,10 @@ export async function runAlignPhase(opts: AlignPhaseOptions): Promise<AlignPhase
543
738
  result,
544
739
  tally,
545
740
  sinks: {
546
- labeled: (split, line, sourceID) => {
547
- writeLine(split, line)
741
+ labeled: (split, row) => {
742
+ writeLine(split, labeledLine(row))
743
+
744
+ const sourceID = labeledSourceID(row)
548
745
 
549
746
  if (sourceID != null) {
550
747
  writeSourceID(split, sourceID)
@@ -584,6 +781,7 @@ export async function runAlignPhase(opts: AlignPhaseOptions): Promise<AlignPhase
584
781
  license_counts: Object.fromEntries(result.licenseCounts),
585
782
  refused_by_kind: Object.fromEntries(result.refusedByKind),
586
783
  refused_by_source: Object.fromEntries(result.refusedBySource),
784
+ train_rows_left_out_by_source: Object.fromEntries(result.trainRowsLeftOutBySource),
587
785
  excluded_by_license: tally.excludedByLicense,
588
786
  admitted_unresolved_license_rows: tally.admittedUnresolved,
589
787
  excluded_by_eligibility: tally.excludedByEligibility,
@@ -41,15 +41,27 @@ export interface WorkerResources {
41
41
  }
42
42
 
43
43
  /**
44
- * The worker classes.
44
+ * The worker classes, one per task kind.
45
45
  *
46
- * `standard` runs every task: an adapter, an align range, or a source's sort and parquet write.
47
- * The WOF adapters read the WOF record store and peaked at 1,862 MiB
48
- * (`wof-admin`, 79,116,868 rows) and 636 MiB (`wof-postalcode`, 5,240,681 rows) by
49
- * kernel `maxRSS` over the lab's full clone set, inside the standard heap.
46
+ * An `adapter` worker reads one source's input.
47
+ * The WOF adapters read the WOF record store and peaked at 1,862 MiB (`wof-admin`, 79,116,868 rows)
48
+ * and 636 MiB (`wof-postalcode`, 5,240,681 rows) by kernel `maxRSS` over the lab's full clone set.
49
+ *
50
+ * An `align` worker aligns one byte range of a canonical file and opens no DuckDB instance.
51
+ * Align workers peaked at 0.43 GiB on 2026-11-24 and 0.48 GiB in the 2026-10-20 profile,
52
+ * so the class reserves a quarter of the adapter's memory and a host runs about
53
+ * four align workers in the memory of one adapter worker.
54
+ *
55
+ * A `partition` worker reads one row range of a large input and holds no dedup table.
56
+ * The `adapter` worker that merges the partitions holds it.
57
+ *
58
+ * A `finish` worker sorts one source's ids and writes its parquet files through DuckDB.
50
59
  */
51
60
  export const WORKER_CLASSES = {
52
- standard: { heapBytes: 6 * GiB, duckdbBytes: 1.5 * GiB, duckdbThreads: 1 },
61
+ adapter: { heapBytes: 6 * GiB, duckdbBytes: 1.5 * GiB, duckdbThreads: 1 },
62
+ partition: { heapBytes: 2 * GiB, duckdbBytes: 1.5 * GiB, duckdbThreads: 1 },
63
+ align: { heapBytes: 1.5 * GiB, duckdbBytes: 0, duckdbThreads: 1 },
64
+ finish: { heapBytes: 6 * GiB, duckdbBytes: 1.5 * GiB, duckdbThreads: 1 },
53
65
  } as const satisfies Record<string, WorkerResources>
54
66
 
55
67
  export type WorkerClass = keyof typeof WORKER_CLASSES
@@ -87,39 +99,62 @@ export interface WorkerBudget {
87
99
  const gib = (bytes: number): string => `${(bytes / GiB).toFixed(1)} GiB`
88
100
 
89
101
  /**
90
- * The `--jobs` a build takes when none is given: the most standard workers the
91
- * host's budget holds, capped at the host's CPU count.
102
+ * The reservation of the largest worker class.
103
+ * Every parallel build must fit one worker of it.
104
+ */
105
+ function largestReservationBytes(): number {
106
+ return Math.max(...(Object.keys(WORKER_CLASSES) as WorkerClass[]).map(workerReservationBytes))
107
+ }
108
+
109
+ /**
110
+ * The most worker processes `budgetBytes` holds: one worker of the largest
111
+ * class beside align workers in the rest.
112
+ *
113
+ * The pool admits each worker against its own class's reservation, so `--jobs`
114
+ * caps the process count and memory caps each class.
115
+ */
116
+ function workersThatFit(budgetBytes: number): number {
117
+ const rest = budgetBytes - largestReservationBytes()
118
+
119
+ if (rest < 0) return 0
120
+
121
+ return 1 + Math.floor(rest / workerReservationBytes("align"))
122
+ }
123
+
124
+ /**
125
+ * The `--jobs` a build takes when none is given: the most workers the host's
126
+ * budget holds, capped at the host's CPU count.
92
127
  *
93
128
  * Returns 1 for a serial build when fewer than two workers fit.
94
129
  */
95
130
  export function defaultBuildJobs(opts: { hostMemoryBytes?: number } = {}): number {
96
131
  const hostMemoryBytes = opts.hostMemoryBytes ?? totalMemoryBytes()
97
- const budgetBytes = hostMemoryBytes - HOST_RESERVE_BYTES
98
- const fits = Math.min(availableParallelism(), Math.floor(budgetBytes / workerReservationBytes("standard")))
132
+ const fits = Math.min(availableParallelism(), workersThatFit(hostMemoryBytes - HOST_RESERVE_BYTES))
99
133
 
100
134
  return fits >= 2 ? fits : 1
101
135
  }
102
136
 
103
137
  /**
104
- * Check that `jobs` standard workers fit in the host budget.
138
+ * Check that `jobs` workers fit in the host budget: one of the largest class and `jobs - 1` align workers.
105
139
  *
106
- * @throws When `jobs` standard workers exceed the budget.
107
- * The error lists each reservation and the largest `--jobs` that fits.
140
+ * @throws When they exceed the budget.
141
+ * The error lists each class's reservation and the largest `--jobs` that fits.
108
142
  */
109
143
  export function planWorkerBudget(opts: { jobs: number; hostMemoryBytes?: number }): WorkerBudget {
110
144
  const hostMemoryBytes = opts.hostMemoryBytes ?? totalMemoryBytes()
111
145
  const budgetBytes = hostMemoryBytes - HOST_RESERVE_BYTES
112
- const standard = workerReservationBytes("standard")
113
- const fits = Math.max(0, Math.floor(budgetBytes / standard))
146
+ const fits = workersThatFit(budgetBytes)
147
+ const needs = largestReservationBytes() + (opts.jobs - 1) * workerReservationBytes("align")
114
148
 
115
149
  const sizes =
116
- `each standard worker reserves ${gib(standard)} (heap ${gib(WORKER_CLASSES.standard.heapBytes)}, ` +
117
- `DuckDB ${gib(WORKER_CLASSES.standard.duckdbBytes)}, native ${gib(WORKER_NATIVE_ALLOWANCE_BYTES)}), ` +
118
- `and the budget is ${gib(hostMemoryBytes)} of host memory less ${gib(HOST_RESERVE_BYTES)} reserved, ${gib(budgetBytes)}`
150
+ (Object.keys(WORKER_CLASSES) as WorkerClass[])
151
+ .map((workerClass) => `a ${workerClass} worker reserves ${gib(workerReservationBytes(workerClass))}`)
152
+ .join(", ") +
153
+ `, and the budget is ${gib(hostMemoryBytes)} of host memory less ${gib(HOST_RESERVE_BYTES)} reserved, ${gib(budgetBytes)}`
119
154
 
120
- if (opts.jobs * standard > budgetBytes) {
155
+ if (needs > budgetBytes) {
121
156
  throw new Error(
122
- `corpus build refused: --jobs ${opts.jobs} needs ${gib(opts.jobs * standard)} and ${sizes}. ` +
157
+ `corpus build refused: --jobs ${opts.jobs} needs ${gib(needs)} and ${sizes}. ` +
123
158
  (fits >= 1 ? `--jobs ${fits} fits.` : "No parallel build fits this host; run with --jobs 1.")
124
159
  )
125
160
  }