Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(sandbox): reject a bare-array blocks, which zeroes the BLOCK count (#5097)

Cindy Zhangd2963e56fce3
diff --git a/apps/sandbox/scripts/generate-score-ledger.mjs b/apps/sandbox/scripts/generate-score-ledger.mjs
index 0f5f9fc0e3..b59bf929f3 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,
+ isBlocksShape,
isEvidenceItem,
listComponents,
} from '../../../scripts/score-ledger.mjs';
@@ -90,7 +91,15 @@ const KNOWN_ENTRY_KEYS = new Set([
* same repo-wide red one layer down.
*/
const ENTRY_KEY_SHAPES = {
- evidence: value => Array.isArray(value) && value.every(isEvidenceItem),
+ evidence: {check: value => Array.isArray(value) && value.every(isEvidenceItem)},
+ // `blocks` is a REQUIRED field of LedgerEntry, so dropping a malformed one
+ // trades one tsc error for another. A bare array is the shape that has
+ // actually occurred and it is losslessly repairable, so repair it.
+ blocks: {
+ check: isBlocksShape,
+ repair: value =>
+ Array.isArray(value) ? {count: value.length, open: value} : {count: 0, open: []},
+ },
};
/** Drop keys `LedgerEntry` doesn't declare, or whose shape it rejects. */
@@ -103,8 +112,10 @@ function pruneEntry(entry) {
droppedKeys.add(k);
continue;
}
- if (ENTRY_KEY_SHAPES[k] && !ENTRY_KEY_SHAPES[k](v)) {
+ const shape = ENTRY_KEY_SHAPES[k];
+ if (shape && !shape.check(v)) {
malformedKeys.add(`${entry.component ?? '?'}.${k}`);
+ if (shape.repair) kept[k] = shape.repair(v);
continue;
}
kept[k] = v;
@@ -153,7 +164,7 @@ 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. ` +
+ `(${[...malformedKeys].sort().join(', ')}) — repaired or dropped in the snapshot. ` +
`Fix the row in the wiki; the page's runtime fetch still reads it as written.`,
);
}
diff --git a/scripts/score-ledger.mjs b/scripts/score-ledger.mjs
index c67041e064..35246fc33f 100644
--- a/scripts/score-ledger.mjs
+++ b/scripts/score-ledger.mjs
@@ -958,6 +958,17 @@ export function isEvidenceItem(item) {
);
}
+/**
+ * Does `blocks` match the declared `{count, open}` shape? A bare array reads
+ * as zero open BLOCKs to `openBlockCount` and `blockList`, so it does not
+ * merely break the sandbox build: it skips the open-BLOCK grade cap and blinds
+ * the ratchet to every BLOCK in it.
+ */
+export function isBlocksShape(blocks) {
+ if (!blocks || typeof blocks !== 'object' || Array.isArray(blocks)) return false;
+ return typeof blocks.count === 'number' && Array.isArray(blocks.open);
+}
+
/**
* Merge a scorecard into a ledger entry and validate it.
* Unknown keys are rejected rather than silently stored — a typo in a field
@@ -988,6 +999,18 @@ export function applyScorecard(existing, scorecard, {component, pkg}) {
...scorecard,
};
if (!next.package) throw new Error(`${component}: no package — pass --package`);
+ // Checked before the grade, because a bare array (the shape a scorecard
+ // naturally takes if you think of blocks as a list) reads as zero open
+ // BLOCKs to `openBlockCount`. Left unchecked it does three things at once:
+ // the open-BLOCK grade cap never applies, the ratchet sees no BLOCKs to
+ // compare, and the sandbox inlines a literal tsc rejects, which reds
+ // `build-sandbox` on every open pull request (#5033).
+ if (!isBlocksShape(next.blocks)) {
+ throw new Error(
+ `${component}: blocks must be {count, open: [...]} — a bare array reads as ` +
+ 'zero open BLOCKs, which skips the grade cap and blinds the ratchet',
+ );
+ }
if (next.status !== 'audited') {
throw new Error(
`${component}: the ledger holds audited components only — an unaudited component ` +
diff --git a/scripts/score-ledger.test.mjs b/scripts/score-ledger.test.mjs
index 4309ca70f2..eb0bacfe2a 100644
--- a/scripts/score-ledger.test.mjs
+++ b/scripts/score-ledger.test.mjs
@@ -517,6 +517,38 @@ describe('recording a scorecard', () => {
).toThrow(/BLOCKs listed but blocks.count is 1/);
});
+ it('rejects blocks written as a bare array, which silently zeroes the BLOCK count', () => {
+ expect(() =>
+ applyScorecard(
+ null,
+ {...card, blocks: [{id: 'A5', summary: 'x'}, {id: 'A6', summary: 'y'}]},
+ {component: 'B', pkg: 'core'},
+ ),
+ ).toThrow(/blocks must be \{count, open: \[\.\.\.\]\}/);
+ });
+
+ it('does not let a bare-array blocks slip the open-BLOCK grade cap', () => {
+ // The reason this shape is worth an error rather than a repair: an A-range
+ // score with open BLOCKs is capped at C, and a bare array reads as zero
+ // open BLOCKs, so the row would record A.
+ expect(gradeFor(91, 3)).toBe('C');
+ expect(() =>
+ applyScorecard(
+ null,
+ {...card, score: 91, blocks: [{id: 'A5', summary: 'x'}]},
+ {component: 'B', pkg: 'core'},
+ ),
+ ).toThrow(/blocks must be/);
+ });
+
+ it('rejects a blocks object missing count or open', () => {
+ for (const blocks of [{open: []}, {count: 0}, {count: '0', open: []}, null, 'none']) {
+ expect(() =>
+ applyScorecard(null, {...card, blocks}, {component: 'B', pkg: 'core'}),
+ ).toThrow(/blocks must be/);
+ }
+ });
+
it('refuses to store an unaudited row — an unaudited component simply has none', () => {
expect(() =>
applyScorecard(null, {...card, status: 'unaudited'}, {component: 'B', pkg: 'core'}),