@mailwoman/corpus 11.4.0 → 12.0.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 (162) hide show
  1. package/data/address-source-register.json +9 -1
  2. package/data/builds/v0.8.22-nz-overlay/OVERLAY_PLAN.json +19 -0
  3. package/data/builds/v0.8.22-wofrank-overlay/OVERLAY_PLAN.json +51 -0
  4. package/data/builds/v0.8.24-everyplace/inputs.json +35 -0
  5. package/data/license-decisions.json +9 -1
  6. package/data/training-configs/v8.14.0-base-c6-wofreps-cursor-v0822-s42.yaml +926 -0
  7. package/data/training-configs/v8.15.0-base-c6-wofreps-cursor-emit-v0822-s42.yaml +973 -0
  8. package/data/training-configs/v8.16.0-base-c6-wofreps-positions-v0822-s42.yaml +988 -0
  9. package/data/training-configs/v8.17.0-stage1-gazetteer-v0822-s42.yaml +557 -0
  10. package/data/training-configs/v8.17.0-stage2-corpus-v0822-s42.yaml +1008 -0
  11. package/data/training-configs/v8.18.0-gazetteer-then-mixture-v0822-s42.yaml +1225 -0
  12. package/data/training-configs/v8.19.0-nz-v0822-s42.yaml +870 -0
  13. package/data/training-configs/v8.20.0-wofrank-v0822-s42.yaml +863 -0
  14. package/data/training-configs/v8.21.0-outcomes-v0822-s42.yaml +873 -0
  15. package/data/training-configs/v8.22.0-stage1-everyplace-v0824-s42.yaml +374 -0
  16. package/data/training-configs/v8.22.0-stage2-corpus-v0824-s42.yaml +808 -0
  17. package/data/training-configs/v8.22.1-stage1-everyplace-v0824-s42.yaml +376 -0
  18. package/data/training-configs/v8.22.1-stage2-corpus-v0824-s42.yaml +793 -0
  19. package/data/training-configs/v9.0.0-bytestem-v090-s42.yaml +665 -0
  20. package/data/training-manifests/v0.8.22-nz-overlay.effective.json +2556 -0
  21. package/data/training-manifests/v0.8.22-nz-overlay.json +9000 -0
  22. package/data/training-manifests/v0.8.22-wofrank-overlay.json +9000 -0
  23. package/data/training-manifests/v0.8.24-everyplace-overlay.json +9000 -0
  24. package/data/training-manifests/v0.9.0-compact.effective.json +2556 -0
  25. package/data/training-manifests/v0.9.0-compact.json +9000 -0
  26. package/lib/adapters/wof/admin/json/adapter.ts +38 -3
  27. package/lib/adapters/wof/json-rows.ts +84 -5
  28. package/lib/adapters/wof/sample.ts +42 -6
  29. package/lib/build/code-hash.ts +1 -1
  30. package/lib/cjk/foreign-places.ts +1 -1
  31. package/lib/kr/adapters/localdata/permit-rows.ts +1 -1
  32. package/lib/parquet/schema.ts +7 -0
  33. package/lib/tools/anthropic/batches.ts +221 -0
  34. package/lib/tools/anthropic/labeling.ts +263 -0
  35. package/lib/tools/compact-v9.ts +872 -0
  36. package/lib/tools/fetch/wikidata/vital-places/entities.ts +20 -2
  37. package/lib/tools/fetch/wikidata/vital-places/qrank.ts +5 -4
  38. package/lib/tools/fragment-assay.run.ts +2 -1
  39. package/lib/tools/name-roles/name-roles.run.ts +296 -0
  40. package/lib/tools/name-roles/prompt.ts +253 -0
  41. package/lib/tools/overlay/deletions.ts +1 -1
  42. package/lib/tools/overlay/draw-weight.ts +275 -0
  43. package/lib/tools/overlay/manifest.ts +36 -5
  44. package/lib/tools/place-twins/place-twins.run.ts +528 -0
  45. package/lib/tools/place-twins/prompt.ts +276 -0
  46. package/lib/tools/prominent-shapes/prominent-shapes.run.ts +420 -0
  47. package/lib/tools/prominent-shapes/prompt.ts +189 -0
  48. package/lib/tools/prominent-shapes/read-seeds.ts +286 -0
  49. package/lib/tools/prominent-shapes/seeds.ts +281 -0
  50. package/lib/tools/prominent-shapes/validate.ts +563 -0
  51. package/lib/training/config/admission.ts +9 -2
  52. package/lib/training/config/schema.ts +84 -0
  53. package/lib/types.ts +23 -0
  54. package/lib/us/adapters/usgov/nad/adapter.ts +8 -3
  55. package/lib/utils/split.ts +1 -1
  56. package/out/adapters/wof/admin/json/adapter.d.ts +7 -0
  57. package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
  58. package/out/adapters/wof/admin/json/adapter.js +28 -3
  59. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  60. package/out/adapters/wof/json-rows.d.ts +8 -0
  61. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  62. package/out/adapters/wof/json-rows.js +56 -4
  63. package/out/adapters/wof/json-rows.js.map +1 -1
  64. package/out/adapters/wof/sample.d.ts +12 -0
  65. package/out/adapters/wof/sample.d.ts.map +1 -1
  66. package/out/adapters/wof/sample.js +37 -6
  67. package/out/adapters/wof/sample.js.map +1 -1
  68. package/out/build/code-hash.js +1 -1
  69. package/out/build/code-hash.js.map +1 -1
  70. package/out/cjk/foreign-places.js +1 -1
  71. package/out/cjk/foreign-places.js.map +1 -1
  72. package/out/kr/adapters/localdata/permit-rows.js +1 -1
  73. package/out/kr/adapters/localdata/permit-rows.js.map +1 -1
  74. package/out/parquet/schema.d.ts +6 -0
  75. package/out/parquet/schema.d.ts.map +1 -1
  76. package/out/parquet/schema.js +6 -0
  77. package/out/parquet/schema.js.map +1 -1
  78. package/out/tools/anthropic/batches.d.ts +117 -0
  79. package/out/tools/anthropic/batches.d.ts.map +1 -0
  80. package/out/tools/anthropic/batches.js +141 -0
  81. package/out/tools/anthropic/batches.js.map +1 -0
  82. package/out/tools/anthropic/labeling.d.ts +97 -0
  83. package/out/tools/anthropic/labeling.d.ts.map +1 -0
  84. package/out/tools/anthropic/labeling.js +146 -0
  85. package/out/tools/anthropic/labeling.js.map +1 -0
  86. package/out/tools/compact-v9.d.ts +117 -0
  87. package/out/tools/compact-v9.d.ts.map +1 -0
  88. package/out/tools/compact-v9.js +494 -0
  89. package/out/tools/compact-v9.js.map +1 -0
  90. package/out/tools/fetch/wikidata/vital-places/entities.d.ts +3 -0
  91. package/out/tools/fetch/wikidata/vital-places/entities.d.ts.map +1 -1
  92. package/out/tools/fetch/wikidata/vital-places/entities.js +19 -1
  93. package/out/tools/fetch/wikidata/vital-places/entities.js.map +1 -1
  94. package/out/tools/fetch/wikidata/vital-places/qrank.d.ts.map +1 -1
  95. package/out/tools/fetch/wikidata/vital-places/qrank.js +4 -4
  96. package/out/tools/fetch/wikidata/vital-places/qrank.js.map +1 -1
  97. package/out/tools/fragment-assay.run.js +1 -1
  98. package/out/tools/fragment-assay.run.js.map +1 -1
  99. package/out/tools/name-roles/name-roles.run.d.ts +2 -0
  100. package/out/tools/name-roles/name-roles.run.d.ts.map +1 -0
  101. package/out/tools/name-roles/name-roles.run.js +230 -0
  102. package/out/tools/name-roles/name-roles.run.js.map +1 -0
  103. package/out/tools/name-roles/prompt.d.ts +166 -0
  104. package/out/tools/name-roles/prompt.d.ts.map +1 -0
  105. package/out/tools/name-roles/prompt.js +176 -0
  106. package/out/tools/name-roles/prompt.js.map +1 -0
  107. package/out/tools/overlay/deletions.js +1 -1
  108. package/out/tools/overlay/deletions.js.map +1 -1
  109. package/out/tools/overlay/draw-weight.d.ts +86 -0
  110. package/out/tools/overlay/draw-weight.d.ts.map +1 -0
  111. package/out/tools/overlay/draw-weight.js +131 -0
  112. package/out/tools/overlay/draw-weight.js.map +1 -0
  113. package/out/tools/overlay/manifest.d.ts.map +1 -1
  114. package/out/tools/overlay/manifest.js +29 -5
  115. package/out/tools/overlay/manifest.js.map +1 -1
  116. package/out/tools/place-twins/place-twins.run.d.ts +2 -0
  117. package/out/tools/place-twins/place-twins.run.d.ts.map +1 -0
  118. package/out/tools/place-twins/place-twins.run.js +385 -0
  119. package/out/tools/place-twins/place-twins.run.js.map +1 -0
  120. package/out/tools/place-twins/prompt.d.ts +169 -0
  121. package/out/tools/place-twins/prompt.d.ts.map +1 -0
  122. package/out/tools/place-twins/prompt.js +183 -0
  123. package/out/tools/place-twins/prompt.js.map +1 -0
  124. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts +2 -0
  125. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts.map +1 -0
  126. package/out/tools/prominent-shapes/prominent-shapes.run.js +302 -0
  127. package/out/tools/prominent-shapes/prominent-shapes.run.js.map +1 -0
  128. package/out/tools/prominent-shapes/prompt.d.ts +74 -0
  129. package/out/tools/prominent-shapes/prompt.d.ts.map +1 -0
  130. package/out/tools/prominent-shapes/prompt.js +156 -0
  131. package/out/tools/prominent-shapes/prompt.js.map +1 -0
  132. package/out/tools/prominent-shapes/read-seeds.d.ts +23 -0
  133. package/out/tools/prominent-shapes/read-seeds.d.ts.map +1 -0
  134. package/out/tools/prominent-shapes/read-seeds.js +219 -0
  135. package/out/tools/prominent-shapes/read-seeds.js.map +1 -0
  136. package/out/tools/prominent-shapes/seeds.d.ts +132 -0
  137. package/out/tools/prominent-shapes/seeds.d.ts.map +1 -0
  138. package/out/tools/prominent-shapes/seeds.js +178 -0
  139. package/out/tools/prominent-shapes/seeds.js.map +1 -0
  140. package/out/tools/prominent-shapes/validate.d.ts +152 -0
  141. package/out/tools/prominent-shapes/validate.d.ts.map +1 -0
  142. package/out/tools/prominent-shapes/validate.js +389 -0
  143. package/out/tools/prominent-shapes/validate.js.map +1 -0
  144. package/out/training/config/admission.d.ts +2 -1
  145. package/out/training/config/admission.d.ts.map +1 -1
  146. package/out/training/config/admission.js +9 -2
  147. package/out/training/config/admission.js.map +1 -1
  148. package/out/training/config/schema.d.ts +321 -0
  149. package/out/training/config/schema.d.ts.map +1 -1
  150. package/out/training/config/schema.js +76 -0
  151. package/out/training/config/schema.js.map +1 -1
  152. package/out/types.d.ts +21 -0
  153. package/out/types.d.ts.map +1 -1
  154. package/out/types.js +13 -0
  155. package/out/types.js.map +1 -1
  156. package/out/us/adapters/usgov/nad/adapter.d.ts +4 -0
  157. package/out/us/adapters/usgov/nad/adapter.d.ts.map +1 -1
  158. package/out/us/adapters/usgov/nad/adapter.js +8 -3
  159. package/out/us/adapters/usgov/nad/adapter.js.map +1 -1
  160. package/out/utils/split.js +1 -1
  161. package/out/utils/split.js.map +1 -1
  162. package/package.json +24 -24
@@ -0,0 +1,275 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * Rewrites one source's stored parquet files with a `draw_weight` column,
6
+ * for a corpus overlay that replaces the source's base files.
7
+ *
8
+ * A row's raw weight is the score of the record its `source_id` names,
9
+ * raised to `floor`, and a record without a score takes `floor`.
10
+ * The raw weights are divided by their mean over the source's train rows,
11
+ * so the mean draw weight of the rows a train mixture reads is 1.
12
+ *
13
+ * The sampler multiplies a row's draw weight into its acceptance draw after the
14
+ * mixture has picked the source, so the weights move draws between the source's rows
15
+ * and the source's share of the run stays where the training config puts it.
16
+ *
17
+ * Each rewritten file keeps its rows, their order and every stored column, and appends the weight.
18
+ * Its split attribute is the base attribute with the new file's name, sha256 and row count,
19
+ * so the held-out ids and the policy hash are the ones the holdout policy wrote for the base file.
20
+ *
21
+ * The rewrite refuses a file whose rows it cannot match one to one by `source_id` and `raw`.
22
+ */
23
+
24
+ import { sha256File } from "@mailwoman/core/crypto/hash"
25
+ import { makeDirectories, movePath, writeLocalJSONFile } from "@mailwoman/core/fs/writers"
26
+ import { basename, PathBuilder, type PathBuilderLike } from "path-ts"
27
+
28
+ import { type DuckDBLimits, escapeSQLString, openDuckDB } from "#parquet/duckdb"
29
+ import { DRAW_WEIGHT_COLUMN, PARQUET_COMPRESSION, ROW_GROUP_SIZE } from "#parquet/schema"
30
+ import { readSplitAttribute, splitAttributeFilename } from "#tools/overlay/split/attribute"
31
+
32
+ /**
33
+ * One stored file of the source and its split attribute, by local path.
34
+ */
35
+ export interface DrawWeightInputFile {
36
+ parquet: string
37
+ splitAttribute: string
38
+ }
39
+
40
+ export interface DrawWeightOverlayOptions {
41
+ /**
42
+ * The `source` value every row of the files holds.
43
+ */
44
+ source: string
45
+ files: readonly DrawWeightInputFile[]
46
+ /**
47
+ * A regular expression whose first group captures the integer record id inside a row's `source_id`.
48
+ */
49
+ keyPattern: string
50
+ /**
51
+ * Each scored record's score, by id.
52
+ * A record absent from the table has no score.
53
+ */
54
+ scores: ReadonlyMap<number, number>
55
+ /**
56
+ * The raw weight of a row whose record has no score, and the least raw weight of any row.
57
+ */
58
+ floor: number
59
+ /**
60
+ * The overlay directory.
61
+ *
62
+ * Each rewritten file and its attribute are written at its top level.
63
+ */
64
+ outDir: PathBuilderLike
65
+ limits?: DuckDBLimits
66
+ }
67
+
68
+ /**
69
+ * One rewritten file, by its name under the overlay directory.
70
+ */
71
+ export interface DrawWeightedFile {
72
+ parquet: string
73
+ splitAttribute: string
74
+ rows: number
75
+ }
76
+
77
+ /**
78
+ * The weights the rewrite wrote, measured over the source's train rows.
79
+ */
80
+ export interface DrawWeightSummary {
81
+ files: DrawWeightedFile[]
82
+ floor: number
83
+ trainRows: number
84
+ /**
85
+ * Train rows whose record has a score.
86
+ */
87
+ scoredTrainRows: number
88
+ /**
89
+ * Train rows whose raw weight is the floor: every unscored row, and each scored row below it.
90
+ */
91
+ floorTrainRows: number
92
+ /**
93
+ * The mean raw weight over the train rows.
94
+ * Every raw weight is divided by this mean.
95
+ */
96
+ rawTrainMean: number
97
+ /**
98
+ * The draw weight of a floor row.
99
+ */
100
+ floorWeight: number
101
+ /**
102
+ * The largest draw weight in the files, the bound the sampler divides by.
103
+ */
104
+ maxWeight: number
105
+ /**
106
+ * Draw-weight quantiles over the train rows, keyed by quantile.
107
+ */
108
+ quantiles: Record<string, number>
109
+ }
110
+
111
+ const QUANTILES = [0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 0.999] as const
112
+
113
+ /**
114
+ * Writes each of `files` again with a `draw_weight` column and the split
115
+ * attribute that describes the new file.
116
+ *
117
+ * @throws When a row's `source_id` yields no record id, a row holds another source,
118
+ * a file already stores the column, or a rewritten file's rows differ from the input's.
119
+ */
120
+ export async function writeDrawWeightedFiles(options: DrawWeightOverlayOptions): Promise<DrawWeightSummary> {
121
+ const { source, files, keyPattern, scores, floor } = options
122
+
123
+ if (!(Number.isFinite(floor) && floor > 0)) throw new Error(`the floor must be a positive number, got ${floor}`)
124
+
125
+ const outDir = PathBuilder.from(options.outDir)
126
+
127
+ await makeDirectories(outDir)
128
+
129
+ using db = await openDuckDB(options.limits)
130
+
131
+ await db.run("CREATE TEMP TABLE scores (id BIGINT, score DOUBLE)")
132
+ await db.run("CREATE TEMP TABLE held_out (file INTEGER, id VARCHAR)")
133
+
134
+ const scoreAppender = await db.createAppender("scores")
135
+
136
+ for (const [id, score] of scores) {
137
+ scoreAppender.appendBigInt(BigInt(id))
138
+ scoreAppender.appendDouble(score)
139
+ scoreAppender.endRow()
140
+ }
141
+
142
+ scoreAppender.closeSync()
143
+
144
+ const attributes = await Promise.all(files.map((file) => readSplitAttribute(file.splitAttribute)))
145
+ const heldAppender = await db.createAppender("held_out")
146
+
147
+ for (const [index, attribute] of attributes.entries()) {
148
+ for (const id of [...attribute.val, ...attribute.test]) {
149
+ heldAppender.appendInteger(index)
150
+ heldAppender.appendVarchar(id)
151
+ heldAppender.endRow()
152
+ }
153
+ }
154
+
155
+ heldAppender.closeSync()
156
+
157
+ const scan = (path: string) => `read_parquet('${escapeSQLString(path)}', file_row_number = true)`
158
+
159
+ await db.run(
160
+ "CREATE TEMP TABLE weights (file INTEGER, file_row_number BIGINT, source VARCHAR, record BIGINT, " +
161
+ "scored BOOLEAN, raw DOUBLE, train BOOLEAN)"
162
+ )
163
+
164
+ for (const [index, file] of files.entries()) {
165
+ const columns = (await db.runAndReadAll(`SELECT name FROM parquet_schema('${escapeSQLString(file.parquet)}')`))
166
+ .getRowObjectsJson()
167
+ .map((row) => String(row["name"]))
168
+
169
+ if (columns.includes(DRAW_WEIGHT_COLUMN)) {
170
+ throw new Error(`${file.parquet} already stores ${DRAW_WEIGHT_COLUMN}, so a rewrite would hold it twice`)
171
+ }
172
+
173
+ await db.run(
174
+ `INSERT INTO weights SELECT ${index}, p.file_row_number, p.source, p.record, s.score IS NOT NULL, ` +
175
+ `greatest(coalesce(s.score, ${floor}), ${floor}), ` +
176
+ `p.source_id NOT IN (SELECT id FROM held_out WHERE file = ${index}) ` +
177
+ `FROM (SELECT file_row_number, source, source_id, ` +
178
+ `TRY_CAST(regexp_extract(source_id, '${escapeSQLString(keyPattern)}', 1) AS BIGINT) AS record ` +
179
+ `FROM ${scan(file.parquet)}) p LEFT JOIN scores s ON s.id = p.record`
180
+ )
181
+ }
182
+
183
+ const refused = (
184
+ await db.runAndReadAll(
185
+ `SELECT count(*) FILTER (WHERE record IS NULL) AS keyless, ` +
186
+ `count(*) FILTER (WHERE source IS DISTINCT FROM '${escapeSQLString(source)}') AS foreign FROM weights`
187
+ )
188
+ ).getRowObjectsJson()[0]!
189
+
190
+ if (Number(refused["keyless"]) || Number(refused["foreign"])) {
191
+ throw new Error(
192
+ `${refused["keyless"]} row(s) carry a source_id without a record id under ${keyPattern}, and ` +
193
+ `${refused["foreign"]} row(s) hold a source other than ${source}; each row needs one record's weight`
194
+ )
195
+ }
196
+
197
+ const stats = (
198
+ await db.runAndReadAll(
199
+ `SELECT count(*) AS train, count(*) FILTER (WHERE scored) AS scored, ` +
200
+ `count(*) FILTER (WHERE raw = ${floor}) AS at_floor, avg(raw) AS mean FROM weights WHERE train`
201
+ )
202
+ ).getRowObjectsJson()[0]!
203
+
204
+ const rawTrainMean = Number(stats["mean"])
205
+
206
+ if (!(rawTrainMean > 0)) throw new Error(`the ${source} files hold no train row to normalize over`)
207
+
208
+ const quantileRow = (
209
+ await db.runAndReadAll(
210
+ `SELECT quantile_cont(raw / ${rawTrainMean}, [${QUANTILES.join(", ")}]) AS q, ` +
211
+ `(SELECT max(raw) FROM weights) / ${rawTrainMean} AS max_weight FROM weights WHERE train`
212
+ )
213
+ ).getRowObjectsJson()[0]!
214
+
215
+ const quantileValues = quantileRow["q"] as number[]
216
+ const written: DrawWeightedFile[] = []
217
+
218
+ for (const [index, file] of files.entries()) {
219
+ const staging = outDir(`${source}.draw-weight.${index}.parquet.partial`).toString()
220
+
221
+ await db.run(
222
+ `COPY (SELECT p.* EXCLUDE (file_row_number), w.raw / ${rawTrainMean} AS ${DRAW_WEIGHT_COLUMN} ` +
223
+ `FROM ${scan(file.parquet)} p JOIN weights w ON w.file = ${index} AND w.file_row_number = p.file_row_number ` +
224
+ `ORDER BY p.file_row_number) TO '${escapeSQLString(staging)}' ` +
225
+ `(FORMAT parquet, COMPRESSION ${PARQUET_COMPRESSION}, ROW_GROUP_SIZE ${ROW_GROUP_SIZE})`
226
+ )
227
+
228
+ const matched = (
229
+ await db.runAndReadAll(
230
+ `SELECT (SELECT count(*) FROM ${scan(file.parquet)}) AS input, ` +
231
+ `(SELECT count(*) FROM ${scan(staging)}) AS output, count(*) AS same FROM ${scan(file.parquet)} a ` +
232
+ `JOIN ${scan(staging)} b USING (file_row_number) WHERE a.source_id = b.source_id AND a.raw = b.raw`
233
+ )
234
+ ).getRowObjectsJson()[0]!
235
+
236
+ const rows = Number(matched["input"])
237
+
238
+ if (Number(matched["output"]) !== rows || Number(matched["same"]) !== rows) {
239
+ throw new Error(
240
+ `${staging} holds ${matched["output"]} rows, ${matched["same"]} of them matching ${file.parquet}'s ${rows} ` +
241
+ `by position, source_id and raw, so it is not the same rows with a weight added`
242
+ )
243
+ }
244
+
245
+ const sha256 = await sha256File(staging)
246
+ const name = `${source}-${sha256}.parquet`
247
+
248
+ await movePath(staging, outDir(name))
249
+
250
+ const attribute = attributes[index]!
251
+ const attributeName = splitAttributeFilename(name, attribute.policyHash)
252
+
253
+ if (attribute.file.rows !== rows) {
254
+ throw new Error(
255
+ `${file.splitAttribute} describes ${attribute.file.rows} rows and ${basename(file.parquet)} holds ${rows}`
256
+ )
257
+ }
258
+
259
+ await writeLocalJSONFile({ ...attribute, file: { name, sha256, rows } }, outDir(attributeName))
260
+
261
+ written.push({ parquet: name, splitAttribute: attributeName, rows })
262
+ }
263
+
264
+ return {
265
+ files: written,
266
+ floor,
267
+ trainRows: Number(stats["train"]),
268
+ scoredTrainRows: Number(stats["scored"]),
269
+ floorTrainRows: Number(stats["at_floor"]),
270
+ rawTrainMean,
271
+ floorWeight: floor / rawTrainMean,
272
+ maxWeight: Number(quantileRow["max_weight"]),
273
+ quantiles: Object.fromEntries(QUANTILES.map((q, i) => [String(q), quantileValues[i]!])),
274
+ }
275
+ }
@@ -32,6 +32,7 @@ import {
32
32
  splitOfSourceID,
33
33
  } from "#tools/overlay/split/attribute"
34
34
  import type { SplitName } from "#utils/split"
35
+ import type { EvaluationGroupPolicyIdentity } from "#utils/validation-groups"
35
36
 
36
37
  /**
37
38
  * The attribute file that decides the split of each row of one manifest file.
@@ -518,6 +519,37 @@ export function localManifestFilePath(path: string): string {
518
519
  return path.startsWith("/data/") ? dataRootPath(path.slice("/data/".length)).toString() : path
519
520
  }
520
521
 
522
+ /**
523
+ * The base's `evaluation_groups` record, when every row of the corpus overlay
524
+ * is split by the two lists it names.
525
+ *
526
+ * Each attribute records the frozen and validation lists it applied, by sha256.
527
+ * The policy hash also covers the split code, so an attribute written after that
528
+ * code changed records another hash under the same lists.
529
+ *
530
+ * A file split by its own `split`, or an attribute under other lists or none, leaves the
531
+ * corpus overlay under no single evaluation-group policy, and the record is dropped.
532
+ */
533
+ function carriedEvaluationGroups(
534
+ base: BaseManifest,
535
+ splitByFile: number,
536
+ attributes: Iterable<SplitAttributeFile>
537
+ ): { evaluation_groups?: unknown } {
538
+ const record = base["evaluation_groups"] as Partial<EvaluationGroupPolicyIdentity> | null | undefined
539
+ const frozen = record?.evaluationGroups?.sha256
540
+ const validation = record?.validationGroups?.sha256
541
+
542
+ if (!frozen || !validation || splitByFile !== 0) return {}
543
+
544
+ for (const { policy } of attributes) {
545
+ if (policy.evaluationGroups?.evaluationGroups.sha256 !== frozen) return {}
546
+
547
+ if (policy.evaluationGroups.validationGroups.sha256 !== validation) return {}
548
+ }
549
+
550
+ return { evaluation_groups: record }
551
+ }
552
+
521
553
  /**
522
554
  * Writes a corpus manifest that keeps the files of `args.base` and appends `args.files`.
523
555
  *
@@ -690,6 +722,8 @@ export async function assembleOverlayManifest(args: OverlayManifestOptions): Pro
690
722
  base.counts[split] + baseDeleted[split] - droppedRows[split] + addedRows[split] - deletedRows[split]
691
723
 
692
724
  const sources = args.files.map((file) => file.source).join(", ")
725
+ const policyHashes = [...new Set([...attributes.values()].map((attribute) => attribute.policyHash))].toSorted()
726
+ const splitByFile = listedFiles.filter((file) => !file["split_attribute"]).length
693
727
 
694
728
  const defaultNote = replaced.size
695
729
  ? `${base.corpus_version} files without ${[...replaced].join(", ")} + ${sources}.`
@@ -708,11 +742,8 @@ export async function assembleOverlayManifest(args: OverlayManifestOptions): Pro
708
742
  total_rows: netCount("train") + netCount("val") + netCount("test"),
709
743
  // What decides a row's split: a file's own `split`, or for a file listing
710
744
  // `split_attribute`, the attribute file under these policy hashes.
711
- split_decided_by: {
712
- file: listedFiles.filter((file) => !file["split_attribute"]).length,
713
- attribute: SPLIT_ATTRIBUTE,
714
- policy_hashes: [...new Set([...attributes.values()].map((attribute) => attribute.policyHash))].toSorted(),
715
- },
745
+ split_decided_by: { file: splitByFile, attribute: SPLIT_ATTRIBUTE, policy_hashes: policyHashes },
746
+ ...carriedEvaluationGroups(base, splitByFile, attributes.values()),
716
747
  deletions,
717
748
  // An overlay corpus's manifest reads no row's `license` column, so it states no license set.
718
749
  // This value in the artifact prevents a reader from deriving an attribution table