ppuzio Claude Opus 5 commited on
Commit
4fbe513
·
1 Parent(s): 10da95c

feat: audit source licenses and table footnotes

Browse files

Extends the paper verifier past names and token counts: every source row in
the composition table is now checked against src/sources.py and against the
source's own datasheet, and the table's footnote markers are checked in both
directions (used but unexplained, explained but on no row).

License strings are compared by (family, version), so the paper may abbreviate
"CC-BY-4.0" to "CC-BY" and print public domain as either PD or CC0 without
tripping the check, while a genuine family or version mismatch fails.

The audit's one real hit: parliamentary is public-domain content (PL art. 4
pr. aut.) redistributed by IPI PAN under CC-BY-4.0, so the registry's `license`
and `license_spdx` disagree by design. A one-line cell that prints only one of
the two layers now has to carry a footnote.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SLqJTEbW4oU78vmBMPVQs2

src/test_verify_paper_claims.py CHANGED
@@ -32,7 +32,7 @@ v0.1.1 & 3.00 & 2 \\
32
  \begin{tabular}{llr}
33
  Source & License & Tokens [M] \\
34
  \texttt{alpha} & CC0 & 2000.0 \\
35
- \texttt{beta\_two} & CC-BY-4.0 & 1000.0 \\
36
  \textbf{Total} & CC-BY-SA-4.0 & \textbf{$\approx$3000} \\
37
  \end{tabular}
38
  \end{table}
@@ -56,18 +56,38 @@ TOKENS = [1_000_000_000, 3_000_000_000]
56
 
57
  STATS = {"alpha": (200, 2_000_000_000), "beta_two": (100, 1_000_000_000)}
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  def build(root: Path) -> None:
61
  (root / "src").mkdir(parents=True)
62
  (root / "CHANGELOG.md").write_text(CHANGELOG)
63
  (root / "README.md").write_text(README)
64
  (root / "src" / "plot_version_growth.py").write_text(GROWTH)
 
65
  for name, (docs, tokens) in STATS.items():
66
  directory = root / "data" / name
67
  directory.mkdir(parents=True)
68
  (directory / f"{name}.stats.json").write_text(
69
  json.dumps({"kept": docs, "tokens": tokens, "license": "CC0"})
70
  )
 
 
 
71
  (root / "paper.tex").write_text(TEX)
72
 
73
 
@@ -96,10 +116,40 @@ def wrong_source_count(root: Path) -> None:
96
  path.write_text(path.read_text().replace("v0.1.1 & 3.00 & 2", "v0.1.1 & 3.00 & ---"))
97
 
98
 
99
- def unlisted_source(root: Path) -> None:
100
  directory = root / "data" / "gamma"
101
  directory.mkdir(parents=True)
102
  (directory / "gamma.stats.json").write_text(json.dumps({"kept": 1, "tokens": 1, "license": "CC0"}))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
 
105
  def stale_growth_series(root: Path) -> None:
@@ -113,4 +163,9 @@ if __name__ == "__main__":
113
  assert scenario(wrong_source_count) == 1, "carried-over '---' source count must fail"
114
  assert scenario(unlisted_source) == 1, "source present in repo but not in Table 3 must fail"
115
  assert scenario(stale_growth_series) == 1, "growth series vs composition drift must fail"
 
 
 
 
 
116
  print("ok")
 
32
  \begin{tabular}{llr}
33
  Source & License & Tokens [M] \\
34
  \texttt{alpha} & CC0 & 2000.0 \\
35
+ \texttt{beta\_two} & CC-BY-SA-4.0 & 1000.0 \\
36
  \textbf{Total} & CC-BY-SA-4.0 & \textbf{$\approx$3000} \\
37
  \end{tabular}
38
  \end{table}
 
56
 
57
  STATS = {"alpha": (200, 2_000_000_000), "beta_two": (100, 1_000_000_000)}
58
 
59
+ SOURCES = """SOURCES = {
60
+ "alpha": {"license": "CC0-1.0", "license_spdx": "CC0-1.0", "release": "0.1.0"},
61
+ "beta_two": {"license": "CC-BY-SA-4.0", "license_spdx": "CC-BY-SA-4.0", "release": "0.1.1"},
62
+ }
63
+ """
64
+
65
+
66
+ def registry(root: Path) -> dict:
67
+ return vpc.load_registry(root)
68
+
69
+
70
+ def edit(path: Path, old: str, new: str) -> None:
71
+ text = path.read_text()
72
+ assert old in text, f"fixture no longer contains {old!r}"
73
+ path.write_text(text.replace(old, new))
74
+
75
 
76
  def build(root: Path) -> None:
77
  (root / "src").mkdir(parents=True)
78
  (root / "CHANGELOG.md").write_text(CHANGELOG)
79
  (root / "README.md").write_text(README)
80
  (root / "src" / "plot_version_growth.py").write_text(GROWTH)
81
+ (root / "src" / "sources.py").write_text(SOURCES)
82
  for name, (docs, tokens) in STATS.items():
83
  directory = root / "data" / name
84
  directory.mkdir(parents=True)
85
  (directory / f"{name}.stats.json").write_text(
86
  json.dumps({"kept": docs, "tokens": tokens, "license": "CC0"})
87
  )
88
+ (directory / f"{name}.md").write_text(
89
+ f"# {name}\n- **License:** `{registry(root)[name]['license']}`\n"
90
+ )
91
  (root / "paper.tex").write_text(TEX)
92
 
93
 
 
116
  path.write_text(path.read_text().replace("v0.1.1 & 3.00 & 2", "v0.1.1 & 3.00 & ---"))
117
 
118
 
119
+ def add_source(root: Path, release: str | None) -> None:
120
  directory = root / "data" / "gamma"
121
  directory.mkdir(parents=True)
122
  (directory / "gamma.stats.json").write_text(json.dumps({"kept": 1, "tokens": 1, "license": "CC0"}))
123
+ (directory / "gamma.md").write_text("# gamma\n- **License:** `CC0-1.0`\n")
124
+ edit(root / "src" / "sources.py", "}\n",
125
+ f' "gamma": {{"license": "CC0-1.0", "release": {release!r}}},\n}}\n')
126
+
127
+
128
+ def unlisted_source(root: Path) -> None:
129
+ add_source(root, "0.1.1")
130
+
131
+
132
+ def unreleased_source(root: Path) -> None:
133
+ add_source(root, None)
134
+
135
+
136
+ def wrong_license(root: Path) -> None:
137
+ edit(root / "paper.tex", "CC-BY-SA-4.0 & 1000.0", "CC-BY-4.0 & 1000.0")
138
+
139
+
140
+ def datasheet_license_drift(root: Path) -> None:
141
+ edit(root / "data" / "alpha" / "alpha.md", "CC0-1.0", "CC-BY-4.0")
142
+
143
+
144
+ def undefined_footnote(root: Path) -> None:
145
+ edit(root / "paper.tex", "& CC0 &", r"& CC0$^{\dagger}$ &")
146
+
147
+
148
+ def unfootnoted_dual_license(root: Path) -> None:
149
+ """alpha's content is CC0 but its platform redistributes under CC-BY-4.0."""
150
+ edit(root / "src" / "sources.py",
151
+ '"alpha": {"license": "CC0-1.0", "license_spdx": "CC0-1.0"',
152
+ '"alpha": {"license": "CC0-1.0", "license_spdx": "CC-BY-4.0"')
153
 
154
 
155
  def stale_growth_series(root: Path) -> None:
 
163
  assert scenario(wrong_source_count) == 1, "carried-over '---' source count must fail"
164
  assert scenario(unlisted_source) == 1, "source present in repo but not in Table 3 must fail"
165
  assert scenario(stale_growth_series) == 1, "growth series vs composition drift must fail"
166
+ assert scenario(unreleased_source) == 0, "a source with release=None is not part of the release"
167
+ assert scenario(wrong_license) == 1, "license in Table 3 vs registry must fail"
168
+ assert scenario(datasheet_license_drift) == 1, "datasheet vs registry license drift must fail"
169
+ assert scenario(undefined_footnote) == 1, "a footnote marker with no definition must fail"
170
+ assert scenario(unfootnoted_dual_license) == 1, "an unfootnoted dual license must fail"
171
  print("ok")
src/verify_paper_claims.py CHANGED
@@ -36,6 +36,8 @@ TOL_MILLIONS = 0.05 # Table 3: 1 decimal place
36
 
37
  OK, FAIL, WARN = "OK", "FAIL", "WARN"
38
 
 
 
39
 
40
  class Report:
41
  def __init__(self) -> None:
@@ -117,6 +119,31 @@ def parse_source_table(tex: str) -> tuple[dict[str, float], float | None]:
117
  return sources, total
118
 
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  def parse_inline_claims(tex: str, current: str | None = None) -> dict:
121
  """Claims the prose/captions make about the *current release* totals.
122
 
@@ -183,13 +210,11 @@ def load_growth_series(root: Path) -> dict[str, tuple[int, int]]:
183
  return dict(zip(found["VERSIONS"], zip(found["DOCUMENTS"], found["TOKENS"])))
184
 
185
 
186
- def load_release_map(root: Path) -> dict[str, str | None]:
187
- """Source name -> the release it entered, from the src/sources.py registry.
188
 
189
- Empty when the registry is absent or predates the `release` field, in which
190
- case the CHANGELOG prose stays the only ground truth. The registry is always
191
- read from the working tree: it defines what a release *is*, independently of
192
- the ref the parquet stats are read from.
193
  """
194
  path = root / "src" / "sources.py"
195
  if not path.exists():
@@ -197,10 +222,17 @@ def load_release_map(root: Path) -> dict[str, str | None]:
197
  for node in ast.parse(path.read_text(encoding="utf-8")).body:
198
  if (isinstance(node, ast.Assign) and len(node.targets) == 1
199
  and getattr(node.targets[0], "id", None) == "SOURCES"):
200
- sources = ast.literal_eval(node.value)
201
- break
202
- else:
203
- return {}
 
 
 
 
 
 
 
204
  if not any("release" in cfg for cfg in sources.values()):
205
  return {}
206
  return {name: cfg.get("release") for name, cfg in sources.items()}
@@ -419,6 +451,98 @@ def check_composition(report: Report, tex: str, root: Path, ref: str) -> None:
419
  series[last][0], sum_docs)
420
 
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  def main(argv: list[str] | None = None) -> int:
423
  parser = argparse.ArgumentParser(description=__doc__,
424
  formatter_class=argparse.RawDescriptionHelpFormatter)
@@ -436,6 +560,7 @@ def main(argv: list[str] | None = None) -> int:
436
  report = Report()
437
  check_trajectory(report, tex, root)
438
  check_composition(report, tex, root, ref)
 
439
  return report.render()
440
 
441
 
 
36
 
37
  OK, FAIL, WARN = "OK", "FAIL", "WARN"
38
 
39
+ MARKER = re.compile(r"\$\^\{([^}]*)\}\$")
40
+
41
 
42
  class Report:
43
  def __init__(self) -> None:
 
119
  return sources, total
120
 
121
 
122
+ def parse_license_rows(tex: str) -> dict[str, tuple[str, set[str]]]:
123
+ """Table 3 source rows -> (license cell without its markers, marker names)."""
124
+ body = _table_body(tex, "tab:s05-zrodla").split(r"\begin{tabular}")[-1]
125
+ rows = {}
126
+ for name, cell in re.findall(r"\\texttt\{([^}]+)\}\s*&(.*?)&\s*[\d.]+\s*\\\\", body):
127
+ rows[_unescape(name)] = (MARKER.sub("", cell).strip(), _markers(cell))
128
+ return rows
129
+
130
+
131
+ def parse_footnote_defs(tex: str) -> set[str]:
132
+ """Markers the Table 3 caption actually explains (a definition is followed by ~)."""
133
+ caption = _table_body(tex, "tab:s05-zrodla").split(r"\begin{tabular}")[0]
134
+ return {m for cell in re.findall(r"(\$\^\{[^}]*\}\$)~", caption) for m in _markers(cell)}
135
+
136
+
137
+ def parse_total_license(tex: str) -> str | None:
138
+ body = _table_body(tex, "tab:s05-zrodla")
139
+ row = re.search(r"\\textbf\{Total\}\s*&\s*(.*?)\s*&", body)
140
+ return MARKER.sub("", row.group(1)).strip() if row else None
141
+
142
+
143
+ def _markers(cell: str) -> set[str]:
144
+ return {m for group in MARKER.findall(cell) for m in re.findall(r"\\([a-zA-Z]+)", group)}
145
+
146
+
147
  def parse_inline_claims(tex: str, current: str | None = None) -> dict:
148
  """Claims the prose/captions make about the *current release* totals.
149
 
 
210
  return dict(zip(found["VERSIONS"], zip(found["DOCUMENTS"], found["TOKENS"])))
211
 
212
 
213
+ def load_registry(root: Path) -> dict[str, dict]:
214
+ """The SOURCES table from src/sources.py, read without importing the module.
215
 
216
+ Always read from the working tree: the registry defines what a release *is*,
217
+ independently of the ref the parquet stats are read from.
 
 
218
  """
219
  path = root / "src" / "sources.py"
220
  if not path.exists():
 
222
  for node in ast.parse(path.read_text(encoding="utf-8")).body:
223
  if (isinstance(node, ast.Assign) and len(node.targets) == 1
224
  and getattr(node.targets[0], "id", None) == "SOURCES"):
225
+ return ast.literal_eval(node.value)
226
+ return {}
227
+
228
+
229
+ def load_release_map(root: Path) -> dict[str, str | None]:
230
+ """Source name -> the release it entered.
231
+
232
+ Empty when the registry is absent or predates the `release` field, in which
233
+ case the CHANGELOG prose stays the only ground truth.
234
+ """
235
+ sources = load_registry(root)
236
  if not any("release" in cfg for cfg in sources.values()):
237
  return {}
238
  return {name: cfg.get("release") for name, cfg in sources.items()}
 
451
  series[last][0], sum_docs)
452
 
453
 
454
+ def norm_license(text: str) -> tuple[str, str | None]:
455
+ """Canonical (family, version) so paper, registry and datasheet can be compared.
456
+
457
+ Public domain and CC0 are one family: SPDX spells "official documents are
458
+ outside copyright" as CC0-1.0, and the paper prints both as PD. A version of
459
+ None means the text states no version, and compares equal to any version --
460
+ the paper abbreviates nkjp1m's CC-BY-4.0 to "CC-BY" on purpose.
461
+ """
462
+ t = re.sub(r"[`_]", " ", text).upper()
463
+ if re.search(r"PER-RECORD|PER-DOCUMENT|MIXED", t):
464
+ return ("MIXED", None)
465
+ if re.search(r"CC0|PUBLIC[- ]DOMAIN|\bPD\b", t):
466
+ return ("PD/CC0", None)
467
+ match = re.search(r"CC[- ]BY(?:[- ](SA))?(?:[- ](\d+\.\d+))?", t)
468
+ if match:
469
+ return ("CC-BY-SA" if match.group(1) else "CC-BY", match.group(2))
470
+ return (t.strip(), None)
471
+
472
+
473
+ def compatible(a: tuple[str, str | None], b: tuple[str, str | None]) -> bool:
474
+ """Same family, and same version unless one side states none."""
475
+ return a[0] == b[0] and (a[1] is None or b[1] is None or a[1] == b[1])
476
+
477
+
478
+ def load_datasheet_license(root: Path, name: str) -> str | None:
479
+ """The datasheet's declared License line (working tree, not the pinned ref)."""
480
+ path = root / "data" / name / f"{name}.md"
481
+ if not path.exists():
482
+ return None
483
+ match = re.search(r"\*\*License:\*\*\s*(.+)", path.read_text(encoding="utf-8"))
484
+ return match.group(1).strip() if match else None
485
+
486
+
487
+ def check_licenses(report: Report, tex: str, root: Path) -> None:
488
+ report.section("Check 3 - licenses and footnotes (Table 3 vs registry vs datasheets)")
489
+
490
+ rows = parse_license_rows(tex)
491
+ registry = load_registry(root)
492
+ if not registry:
493
+ report.add(WARN, "license audit", "src/sources.py carries no registry to compare against")
494
+ return
495
+
496
+ for name in sorted(rows, key=lambda n: n.lower()):
497
+ paper, markers = rows[name]
498
+ cfg = registry.get(name)
499
+ if cfg is None:
500
+ report.add(FAIL, f"{name} in registry", "listed in Table 3 but absent from src/sources.py")
501
+ continue
502
+
503
+ declared = norm_license(str(cfg.get("license", "")))
504
+ spdx = norm_license(str(cfg.get("license_spdx") or cfg.get("license", "")))
505
+ printed = norm_license(paper)
506
+
507
+ if compatible(printed, declared) or compatible(printed, spdx):
508
+ report.add(OK, f"{name} license", f"{paper} == {cfg.get('license')}")
509
+ else:
510
+ report.add(FAIL, f"{name} license",
511
+ f"paper {paper!r} != registry {cfg.get('license')!r} / {cfg.get('license_spdx')!r}")
512
+
513
+ sheet = load_datasheet_license(root, name)
514
+ if sheet is None:
515
+ report.add(WARN, f"{name} datasheet license", "no **License:** line in the datasheet")
516
+ elif not compatible(norm_license(sheet), declared):
517
+ report.add(FAIL, f"{name} datasheet license",
518
+ f"datasheet {sheet!r} != registry {cfg.get('license')!r}")
519
+
520
+ # A source whose content license differs from its redistribution license
521
+ # states only half the story in a one-line cell; the footnote carries the rest.
522
+ if not compatible(declared, spdx) and not markers:
523
+ report.add(FAIL, f"{name} dual license is footnoted",
524
+ f"content {cfg.get('license')!r} but redistributed as "
525
+ f"{cfg.get('license_spdx')!r}; the row prints only {paper!r}")
526
+
527
+ used = {m for _, markers in rows.values() for m in markers}
528
+ defined = parse_footnote_defs(tex)
529
+ if used - defined:
530
+ report.add(FAIL, "every footnote marker is defined", f"used but not explained: {sorted(used - defined)}")
531
+ elif defined - used:
532
+ report.add(FAIL, "every footnote is used", f"explained but on no row: {sorted(defined - used)}")
533
+ else:
534
+ report.add(OK, "footnote markers", f"{len(used)} defined and used: {sorted(used)}")
535
+
536
+ total = parse_total_license(tex)
537
+ strongest = [n for n, (lic, _) in rows.items() if norm_license(lic)[0] == "CC-BY-SA"]
538
+ if total is None:
539
+ report.add(WARN, "Total row license", "no license cell in the Total row")
540
+ elif strongest and norm_license(total)[0] != "CC-BY-SA":
541
+ report.add(FAIL, "Total row license", f"{total} does not inherit the share-alike of {sorted(strongest)}")
542
+ else:
543
+ report.add(OK, "Total row license", f"{total} inherits share-alike from {len(strongest)} sources")
544
+
545
+
546
  def main(argv: list[str] | None = None) -> int:
547
  parser = argparse.ArgumentParser(description=__doc__,
548
  formatter_class=argparse.RawDescriptionHelpFormatter)
 
560
  report = Report()
561
  check_trajectory(report, tex, root)
562
  check_composition(report, tex, root, ref)
563
+ check_licenses(report, tex, root)
564
  return report.render()
565
 
566