An open source design system that's fully customizable and agent ready
TypeScript
fix(sandbox): reject a ledger evidence shape that reds every build (#5033)
Cindy Zhang8138bfca723f
diff --git a/apps/sandbox/scripts/generate-score-ledger.mjs b/apps/sandbox/scripts/generate-score-ledger.mjsindex c7cb01437c..0f5f9fc0e3 100644--- a/apps/sandbox/scripts/generate-score-ledger.mjs+++ b/apps/sandbox/scripts/generate-score-ledger.mjs@@ -37,6 +37,7 @@ import {LEDGER_FETCH_TIMEOUT_MS,SECTION_TITLES,SECTION_WEIGHTS,+ isEvidenceItem,listComponents,} from '../../../scripts/score-ledger.mjs';@@ -80,13 +81,33 @@ const KNOWN_ENTRY_KEYS = new Set(['regression',]);-/** Drop keys `LedgerEntry` doesn't declare, warning once per key. */+/**+ * Known keys whose declared type is more than `unknown`, and the predicate that+ * decides whether the wiki's value matches. Surviving KNOWN_ENTRY_KEYS is not+ * enough: a declared key holding the WRONG SHAPE emits a literal tsc rejects+ * just as an undeclared key does. `evidence` written as an array of bare+ * strings did that on 2026-08-14, three days after the `regression` key, the+ * same repo-wide red one layer down.+ */+const ENTRY_KEY_SHAPES = {+ evidence: value => Array.isArray(value) && value.every(isEvidenceItem),+};++/** Drop keys `LedgerEntry` doesn't declare, or whose shape it rejects. */const droppedKeys = new Set();+const malformedKeys = new Set();function pruneEntry(entry) {const kept = {};for (const [k, v] of Object.entries(entry)) {- if (KNOWN_ENTRY_KEYS.has(k)) kept[k] = v;- else droppedKeys.add(k);+ if (!KNOWN_ENTRY_KEYS.has(k)) {+ droppedKeys.add(k);+ continue;+ }+ if (ENTRY_KEY_SHAPES[k] && !ENTRY_KEY_SHAPES[k](v)) {+ malformedKeys.add(`${entry.component ?? '?'}.${k}`);+ continue;+ }+ kept[k] = v;}return kept;}@@ -129,6 +150,14 @@ if (droppedKeys.size > 0) {);}+if (malformedKeys.size > 0) {+ console.warn(+ `componentScores: the wiki ledger holds ${malformedKeys.size} field(s) whose shape LedgerEntry rejects ` ++ `(${[...malformedKeys].sort().join(', ')}) — dropped from the snapshot. ` ++ `Fix the row in the wiki; the page's runtime fetch still reads it as written.`,+ );+}+const banner = `// Copyright (c) Meta Platforms, Inc. and affiliates./**diff --git a/scripts/score-ledger.mjs b/scripts/score-ledger.mjsindex edcdbc45be..c67041e064 100644--- a/scripts/score-ledger.mjs+++ b/scripts/score-ledger.mjs@@ -943,6 +943,21 @@ const SCORECARD_FIELDS = new Set(['notes',]);+const EVIDENCE_FIELDS = new Set(['label', 'path', 'note']);++/**+ * Does one `evidence` entry match the shape the sandbox's `LedgerEntry`+ * declares? Exported because the sandbox generator enforces the same shape on+ * the way out of the wiki, and one definition beats two that drift.+ */+export function isEvidenceItem(item) {+ if (!item || typeof item !== 'object' || Array.isArray(item)) return false;+ if (typeof item.label !== 'string') return false;+ return Object.entries(item).every(+ ([k, v]) => EVIDENCE_FIELDS.has(k) && (v === undefined || v === null || typeof v === 'string'),+ );+}+/*** Merge a scorecard into a ledger entry and validate it.* Unknown keys are rejected rather than silently stored — a typo in a field@@ -1022,6 +1037,15 @@ export function applyScorecard(existing, scorecard, {component, pkg}) {`${openBlockCount(next)}`,);}+ // The sandbox inlines this row into a typed literal at build time, so a bad+ // shape here is not one broken row: it reds `build-sandbox` on every open PR+ // at once, with no commit responsible (#4924).+ if (!Array.isArray(next.evidence) || !next.evidence.every(isEvidenceItem)) {+ throw new Error(+ `${component}: evidence must be an array of {label, path?, note?} objects — ` ++ 'a bare string is the shape that reds every build in the repo',+ );+ }return next;}diff --git a/scripts/score-ledger.test.mjs b/scripts/score-ledger.test.mjsindex 6f879e329c..4309ca70f2 100644--- a/scripts/score-ledger.test.mjs+++ b/scripts/score-ledger.test.mjs@@ -523,6 +523,47 @@ describe('recording a scorecard', () => {).toThrow(/audited components only/);});+ it('rejects evidence written as bare strings, which reds every build in the repo', () => {+ expect(() =>+ applyScorecard(+ null,+ {...card, evidence: ['33 before and 33 after screenshots']},+ {component: 'B', pkg: 'core'},+ ),+ ).toThrow(/evidence must be an array of \{label, path\?, note\?\} objects/);+ });++ it('rejects an evidence item with no label, or a stray key, or a non-string value', () => {+ for (const evidence of [+ [{note: 'no label'}],+ [{label: 'ok', paths: '/x'}],+ [{label: 'ok', path: 42}],+ [{label: 12}],+ ['a', {label: 'ok'}],+ 'not an array',+ ]) {+ expect(() =>+ applyScorecard(null, {...card, evidence}, {component: 'B', pkg: 'core'}),+ ).toThrow(/evidence must be/);+ }+ });++ it('accepts the declared evidence shape, with path and note optional', () => {+ const e = applyScorecard(+ null,+ {+ ...card,+ evidence: [+ {label: 'bare label'},+ {label: 'with a path', path: 'packages/core/src/Badge/Badge.tsx'},+ {label: 'with both', path: 'https://example.com/x.png', note: 'measured'},+ ],+ },+ {component: 'B', pkg: 'core'},+ );+ expect(e.evidence).toHaveLength(3);+ });+it('requires the rubric version, the date and the mode', () => {for (const field of ['rubricVersion', 'lastAudited', 'mode']) {const partial = {...card};