paperlint 10.2.0 → 10.3.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 (46) hide show
  1. package/dist/adapters/study-registry/index.d.ts +352 -1
  2. package/dist/adapters/study-registry/index.d.ts.map +1 -1
  3. package/dist/adapters/study-registry/index.js +171 -8
  4. package/dist/adapters/study-registry/index.js.map +1 -1
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +6 -4
  7. package/dist/cli.js.map +1 -1
  8. package/dist/domain/study-registry.d.ts +201 -5
  9. package/dist/domain/study-registry.d.ts.map +1 -1
  10. package/dist/domain/study-registry.js +224 -2
  11. package/dist/domain/study-registry.js.map +1 -1
  12. package/dist/ports/study-registry-codec.d.ts +6 -2
  13. package/dist/ports/study-registry-codec.d.ts.map +1 -1
  14. package/dist/studies.d.ts.map +1 -1
  15. package/dist/studies.js +7 -4
  16. package/dist/studies.js.map +1 -1
  17. package/dist/study-registry-rule.d.ts +12 -5
  18. package/dist/study-registry-rule.d.ts.map +1 -1
  19. package/dist/study-registry-rule.js +67 -37
  20. package/dist/study-registry-rule.js.map +1 -1
  21. package/docs/design/study-registry.md +131 -15
  22. package/docs/layout.md +23 -21
  23. package/docs/rules/repro/study-plan-name.md +70 -0
  24. package/docs/rules/repro/study-registered.md +164 -6
  25. package/docs/rules.md +53 -52
  26. package/package.json +1 -1
  27. package/skills/build-benchmark/SKILL.md +24 -12
  28. package/skills/build-benchmark/SKILL.md.spec.ts +23 -11
  29. package/skills/build-label-page/SKILL.md +9 -7
  30. package/skills/build-label-page/SKILL.md.spec.ts +8 -6
  31. package/skills/run-agent-grid/SKILL.md +10 -5
  32. package/skills/run-agent-grid/SKILL.md.spec.ts +9 -4
  33. package/src/adapters/study-registry/index.test.ts +340 -0
  34. package/src/adapters/study-registry/index.ts +268 -10
  35. package/src/cli.ts +6 -4
  36. package/src/domain/study-measurements.test.ts +629 -0
  37. package/src/domain/study-registry.test.ts +10 -1
  38. package/src/domain/study-registry.ts +456 -10
  39. package/src/new-paper.test.ts +1 -0
  40. package/src/ports/study-registry-codec.ts +6 -2
  41. package/src/rule-cases.test.ts +27 -0
  42. package/src/studies-cli.test.ts +2 -2
  43. package/src/studies.test.ts +6 -0
  44. package/src/studies.ts +12 -3
  45. package/src/study-registry-rule.test.ts +140 -1
  46. package/src/study-registry-rule.ts +106 -39
@@ -5,15 +5,32 @@
5
5
  *
6
6
  * A row is a loose object: a paper merges its own measurement metadata into its rows (`role`, `frame`,
7
7
  * `rules_under_test`, `outcome` …), and those fields are kept as they are. The top level is strict, so
8
- * a misspelt key is an error rather than a key nobody reads.
8
+ * a misspelt key is an error rather than a key nobody reads. A measurement keeps its extra fields as a
9
+ * row does; a requirement and an approval are exactly their fields.
10
+ *
11
+ * A study's plan (`plan.json`) is parsed here too, strictly: a key of the paper's own goes in `extra`.
12
+ * Its texts may be empty — the template's are — and the parser says which: a plan comes out ready or
13
+ * a draft naming its empty texts, so nothing downstream looks at a text again. A plan still named
14
+ * `prereg.json` has a loose schema of its own.
9
15
  */
10
16
  import { z } from "zod";
11
17
  import {
12
18
  IN_PAPER,
19
+ MEASUREMENT_ROLES,
20
+ PLAN_FIELDS,
21
+ PLAN_TEXT_FIELDS,
13
22
  PREFIX_PATTERN,
14
23
  STUDY_KINDS,
15
24
  STUDY_STATUSES,
25
+ type Approval,
26
+ type AuthorCheck,
27
+ type Measurement,
28
+ type ParsedPlan,
16
29
  type ParsedRegistry,
30
+ type Requirement,
31
+ type Study,
32
+ type StudyPlan,
33
+ type StudyRegistry,
17
34
  } from "../../domain/study-registry.ts";
18
35
  import type { StudyRegistryCodec } from "../../ports/study-registry-codec.ts";
19
36
 
@@ -29,15 +46,230 @@ const study = z.looseObject({
29
46
  result_file: z.string().optional(),
30
47
  superseded_by: z.string().optional(),
31
48
  in_paper: z.enum(IN_PAPER),
49
+ measurement: z.string().optional(),
50
+ plan: z.string().optional(),
51
+ });
52
+
53
+ const words = z.string().min(1);
54
+
55
+ const requirement = z.strictObject({
56
+ id: words,
57
+ text: words,
58
+ date: z.iso.date(),
59
+ source: words,
60
+ });
61
+
62
+ const approval = z.strictObject({
63
+ by: words,
64
+ date: z.iso.date(),
65
+ quote: words,
66
+ source: words.optional(),
67
+ });
68
+
69
+ const measurement = z.looseObject({
70
+ id: words,
71
+ title: words,
72
+ rq: z.string().optional(),
73
+ role: z.enum(MEASUREMENT_ROLES),
74
+ approval: approval.optional(),
75
+ requirements: z.array(z.string()).optional(),
32
76
  });
33
77
 
34
78
  const registry = z.strictObject({
35
79
  prefix: z
36
80
  .string()
37
81
  .regex(PREFIX_PATTERN, "a capital letter, then capitals or digits (P1)"),
82
+ author: words.optional(),
83
+ requirements: z.array(requirement).optional(),
84
+ measurements: z.array(measurement).optional(),
38
85
  studies: z.array(study),
39
86
  });
40
87
 
88
+ const authorCheck = approval.omit({ source: true });
89
+
90
+ /** A draw: seeded, its seed part of it, or of all the population, with none. */
91
+ const draw = z.discriminatedUnion("kind", [
92
+ z.strictObject({
93
+ kind: z.literal("seeded"),
94
+ method: z.string(),
95
+ seed: z.union([z.string(), z.number()]),
96
+ }),
97
+ z.strictObject({ kind: z.literal("all"), method: z.string() }),
98
+ ]);
99
+
100
+ /** The values that occur more than once in `values`, each once. */
101
+ const repeated = (values: readonly string[]): readonly string[] => [
102
+ ...new Set(values.filter((v, i) => values.indexOf(v) !== i)),
103
+ ];
104
+
105
+ /** Arms whose names are unique: a repeated name is an error naming it. */
106
+ const uniquelyNamed = (
107
+ arms: readonly { readonly name: string }[],
108
+ ctx: Readonly<z.core.$RefinementCtx>,
109
+ ): void => {
110
+ repeated(arms.map((a) => a.name)).forEach((name) => {
111
+ ctx.addIssue({
112
+ code: "custom",
113
+ message: `more than one arm is named «${name}» — an arm's name is unique`,
114
+ });
115
+ });
116
+ };
117
+
118
+ /** The paper's own keys: none of them a field of the plan, which would say a thing twice. */
119
+ const extra = z.record(z.string(), z.unknown()).superRefine((e, ctx) => {
120
+ Object.keys(e)
121
+ .filter((k) => PLAN_FIELDS.some((f) => f === k))
122
+ .forEach((k) => {
123
+ ctx.addIssue({
124
+ code: "custom",
125
+ path: [k],
126
+ message: "is a field of the plan itself — set it there, not in extra",
127
+ });
128
+ });
129
+ });
130
+
131
+ /** A plan's shape: closed at every level; the paper's own keys go in `extra`. Null is the one "none". */
132
+ const planShape = z.strictObject({
133
+ question: z.string(),
134
+ population: z.strictObject({ source: z.string(), draw }),
135
+ arms: z.array(z.strictObject({ name: words })).superRefine(uniquelyNamed),
136
+ primary_outcome: z.string(),
137
+ author_check: authorCheck.nullable(),
138
+ extra: extra.optional(),
139
+ });
140
+
141
+ /**
142
+ * A plan under its old name, `prereg.json`, written by hand before the plan had a schema: open at
143
+ * every level, its own top-level keys kept in `extra`. A seed makes the draw seeded, none (or null) a
144
+ * draw of all; a size, and keys of its own deeper down, are not read; the file is never rewritten.
145
+ */
146
+ const legacyAsPlan = z
147
+ .looseObject({
148
+ question: z.string(),
149
+ population: z.looseObject({
150
+ source: z.string(),
151
+ draw: z.looseObject({
152
+ method: z.string(),
153
+ seed: z.union([z.string(), z.number()]).nullable().optional(),
154
+ }),
155
+ }),
156
+ arms: z.array(z.looseObject({ name: words })).superRefine(uniquelyNamed),
157
+ primary_outcome: z.string(),
158
+ author_check: authorCheck.nullable().optional(),
159
+ })
160
+ .transform((p): StudyPlan => {
161
+ const {
162
+ question,
163
+ population,
164
+ arms,
165
+ primary_outcome,
166
+ author_check,
167
+ ...rest
168
+ } = p;
169
+ const { method, seed } = population.draw;
170
+ return {
171
+ question,
172
+ population: {
173
+ source: population.source,
174
+ draw:
175
+ seed === undefined || seed === null
176
+ ? { kind: "all", method }
177
+ : { kind: "seeded", method, seed },
178
+ },
179
+ arms: arms.map((a) => ({ name: a.name })),
180
+ primary_outcome,
181
+ author_check: author_check ?? null,
182
+ extra: rest,
183
+ };
184
+ });
185
+
186
+ /** A text that says something: more than white space. */
187
+ const filled = z.string().regex(/\S/u);
188
+
189
+ /** The texts a plan fills before its study runs; what fails here is what is still empty. */
190
+ const readyTexts = z.object({
191
+ question: filled,
192
+ population: z.object({
193
+ source: filled,
194
+ draw: z.object({ method: filled }),
195
+ }),
196
+ primary_outcome: filled,
197
+ });
198
+
199
+ /** A plan that parsed: ready, or a draft. */
200
+ type ReadyOrDraft = Exclude<ParsedPlan, { readonly kind: "unreadable" }>;
201
+
202
+ /**
203
+ * A parsed plan, ready when every required text is filled, else a draft naming the empty ones. A
204
+ * draft that carries the author's check is no plan: the author approved one with empty texts.
205
+ */
206
+ const readyOrDraft = (
207
+ p: StudyPlan,
208
+ ctx: Readonly<z.core.$RefinementCtx>,
209
+ ): ReadyOrDraft => {
210
+ const checked = readyTexts.safeParse(p);
211
+ if (checked.success) return { kind: "ready", plan: p };
212
+ const paths = checked.error.issues.map((i) => i.path.join("."));
213
+ const empty = PLAN_TEXT_FIELDS.filter((f) => paths.includes(f));
214
+ if (p.author_check === null)
215
+ return { kind: "draft", plan: { ...p, author_check: null }, empty };
216
+ ctx.addIssue({
217
+ code: "custom",
218
+ path: ["author_check"],
219
+ message: `is set, and the plan leaves ${empty.map((f) => `\`${f}\``).join(", ")} empty — the author approves a complete plan: fill them, or set author_check to null`,
220
+ });
221
+ return z.NEVER;
222
+ };
223
+
224
+ /** A `plan.json`: strict, then ready or draft. */
225
+ const plan = planShape.transform(readyOrDraft);
226
+
227
+ /** A `prereg.json`: loose, mapped onto the plan, then ready or draft. */
228
+ const legacyPlan = legacyAsPlan.transform(readyOrDraft);
229
+
230
+ // ── the schemas ARE the domain's types ─────────────────────────────────────────
231
+ // Each schema's output and its hand-written domain type are the same type, both ways, once both are
232
+ // made readonly all the way down: a field added to one and not the other stops the build.
233
+
234
+ /** `T`, readonly all the way down: zod's outputs are mutable, the domain's are not. */
235
+ type Frozen<T> = T extends readonly (infer U)[]
236
+ ? readonly Frozen<U>[]
237
+ : T extends object
238
+ ? { readonly [K in keyof T]: Frozen<T[K]> }
239
+ : T;
240
+
241
+ /**
242
+ * True when `A` and `B`, both made readonly all the way down, are one type, optional modifiers
243
+ * included: two generic signatures whose returns are deferred conditionals are related only when
244
+ * the conditionals are identical. Written out, not through an alias: an alias is compared by the
245
+ * variance of its parameter, which misses a dropped optional field.
246
+ */
247
+ type Same<A, B> =
248
+ (<T>() => T extends Frozen<A> ? readonly [T] : 2) extends <
249
+ T,
250
+ >() => T extends Frozen<B> ? readonly [T] : 2
251
+ ? true
252
+ : false;
253
+
254
+ /** Compiles only when every schema's output is its domain type. */
255
+ export type SchemasAreTheDomain = [
256
+ Same<z.output<typeof registry>, StudyRegistry>,
257
+ Same<z.output<typeof study>, Study>,
258
+ Same<z.output<typeof measurement>, Measurement>,
259
+ Same<z.output<typeof requirement>, Requirement>,
260
+ Same<z.output<typeof approval>, Approval>,
261
+ Same<z.output<typeof authorCheck>, AuthorCheck>,
262
+ Same<z.output<typeof planShape>, StudyPlan>,
263
+ Same<z.output<typeof legacyAsPlan>, StudyPlan>,
264
+ Same<z.output<typeof plan>, ReadyOrDraft>,
265
+ Same<z.output<typeof legacyPlan>, ReadyOrDraft>,
266
+ ] extends true[]
267
+ ? true
268
+ : never;
269
+
270
+ /** The proof is used, so the build checks it. */
271
+ export const SCHEMAS_ARE_THE_DOMAIN: SchemasAreTheDomain = true;
272
+
41
273
  /** The JSON `text` holds, or undefined when it is not JSON. */
42
274
  function jsonOf(text: string): unknown {
43
275
  try {
@@ -47,6 +279,20 @@ function jsonOf(text: string): unknown {
47
279
  }
48
280
  }
49
281
 
282
+ /** Every reason zod gave, each with the path it is about. */
283
+ const reasons = (
284
+ issues: readonly {
285
+ readonly path: readonly PropertyKey[];
286
+ readonly message: string;
287
+ }[],
288
+ ): string =>
289
+ issues
290
+ .map(
291
+ (i) =>
292
+ `${i.path.length === 0 ? "(top)" : i.path.join(".")}: ${i.message}`,
293
+ )
294
+ .join("; ");
295
+
50
296
  /** A `studies.json`'s text, parsed once at the boundary: the registry, or every reason it is not one. */
51
297
  export function parseStudyRegistry(text: string): ParsedRegistry {
52
298
  const json = jsonOf(text);
@@ -54,17 +300,29 @@ export function parseStudyRegistry(text: string): ParsedRegistry {
54
300
  const parsed = registry.safeParse(json);
55
301
  return parsed.success
56
302
  ? { ok: true, registry: parsed.data }
57
- : {
58
- ok: false,
59
- why: parsed.error.issues
60
- .map(
61
- (i) =>
62
- `${i.path.length === 0 ? "(top)" : i.path.join(".")}: ${i.message}`,
63
- )
64
- .join("; "),
65
- };
303
+ : { ok: false, why: reasons(parsed.error.issues) };
66
304
  }
67
305
 
306
+ /** A plan's text, parsed once at the boundary by `schema`: ready, a draft, or every reason it is neither. */
307
+ const planParser =
308
+ (schema: Readonly<z.ZodType<ReadyOrDraft>>) =>
309
+ (text: string): ParsedPlan => {
310
+ const json = jsonOf(text);
311
+ if (json === undefined) return { kind: "unreadable", why: "not JSON" };
312
+ const parsed = schema.safeParse(json);
313
+ return parsed.success
314
+ ? parsed.data
315
+ : { kind: "unreadable", why: reasons(parsed.error.issues) };
316
+ };
317
+
318
+ /** A `plan.json`: strict. */
319
+ export const parseStudyPlan = planParser(plan);
320
+
321
+ /** A plan still named `prereg.json`: loose, read only when a row names it. */
322
+ export const parseLegacyPlan = planParser(legacyPlan);
323
+
68
324
  export const studyRegistryCodec: StudyRegistryCodec = {
69
325
  parse: parseStudyRegistry,
326
+ parsePlan: parseStudyPlan,
327
+ parseLegacyPlan,
70
328
  };
package/src/cli.ts CHANGED
@@ -238,8 +238,9 @@ const USAGE = `paperlint — machine-checkable gates for a paper kept in git
238
238
  change and keeps nothing
239
239
  npx paperlint studies new <paper-dir> "<title>" [--kind <kind>]
240
240
  register a study: the next id in repro/studies.json, the folder
241
- repro/<id>-<slug>/ with its README.md, the row (status designing,
242
- today's date), and the index in repro/README.md regenerated
241
+ repro/<id>-<slug>/ with its README.md and plan.json (a template),
242
+ the row (status designing, today's date, its plan), and the
243
+ index in repro/README.md regenerated
243
244
  npx paperlint studies index <paper-dir>
244
245
  regenerate the index in repro/README.md from repro/studies.json
245
246
  npx paperlint hook <name> run an editor hook (.claude/settings.json calls this)
@@ -264,8 +265,9 @@ new:
264
265
 
265
266
  studies:
266
267
  --kind <kind> new: measurement (the default), spike, tooling, data, artifact or salvage.
267
- A study's status, result and in_paper are edited in repro/studies.json;
268
- the rule repro/study-registered checks them (docs/rules/repro/study-registered.md)
268
+ A study's status, result and in_paper are edited in repro/studies.json, its
269
+ plan in its plan.json; the rule repro/study-registered checks them
270
+ (docs/rules/repro/study-registered.md)
269
271
 
270
272
  submission:
271
273
  The venue preset declares the portal — "portal": { "kind": "hotcrp", "url": "<site>" } — and the