|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Pin for WHICH exports the skill-reference `Exports: …` fallback publishes — |
| 5 | + * #12201. |
| 6 | + * |
| 7 | + * The fallback ranked by SOURCE ORDER and had no notion of authorable surface, |
| 8 | + * so `slice(0, 5)` kept whichever five exports happened to be declared first. |
| 9 | + * Three rows in the published catalog therefore headlined machine constants |
| 10 | + * whose own names say they are not for authoring — |
| 11 | + * `DEPRECATED_APPROVER_TYPES`, `NON_AUTHORABLE_APPROVER_TYPES`, |
| 12 | + * `CORE_PLUGIN_TYPES`, `CONSUMER_INSTALLABLE_TYPES`, |
| 13 | + * `LEGACY_OBJECT_FIRST_KEYS` — on a surface loaded whole into a customer |
| 14 | + * agent's context window to teach it what it may author. |
| 15 | + * |
| 16 | + * No gate could see it: `check:skill-refs` compares the artifact against the |
| 17 | + * generator, and the generator reproduced the ranking faithfully. That is the |
| 18 | + * same blind spot #5059 found one layer up, and the answer is the same one — |
| 19 | + * the rule is extracted to a pure module and this file IS its enforcement. |
| 20 | + * |
| 21 | + * MEASURED (reverse verification) — and the two halves of this file fail |
| 22 | + * DIFFERENTLY, which is why both exist. Dropping the `MACHINE_CONSTANT` test |
| 23 | + * from `exportListDescription` (keeping everything else) turns four of the six |
| 24 | + * unit cases below red immediately — the three about which names survive, plus |
| 25 | + * the fall-through case, whose `null` exists only because filtering can empty a |
| 26 | + * list. The corpus gate meanwhile stays GREEN: it reads the checked-in |
| 27 | + * artifacts, and those only move when someone regenerates. |
| 28 | + * What turns the corpus gate red is regenerating with the rule dropped — i.e. |
| 29 | + * the state this card found, measured before the fix as 7 offenders across the |
| 30 | + * 3 rows (`DEPRECATED_APPROVER_TYPES`, `NON_AUTHORABLE_APPROVER_TYPES`, |
| 31 | + * `ORG_MEMBERSHIP_LEVELS`, `APPROVER_EXPRESSION_ROOTS`, |
| 32 | + * `LEGACY_OBJECT_FIRST_KEYS`, `CORE_PLUGIN_TYPES`, |
| 33 | + * `CONSUMER_INSTALLABLE_TYPES`). |
| 34 | + * |
| 35 | + * So the unit cases catch a rule that was weakened, and the corpus gate catches |
| 36 | + * an artifact that was regenerated from one — including from a `.zod.ts` that |
| 37 | + * grew a new constant. Neither subsumes the other. The "keeps a lone all-caps |
| 38 | + * token", "no exports at all" and cap-of-five cases stay green under that |
| 39 | + * ablation either way, because for those inputs the two rules agree; that |
| 40 | + * asymmetry is the point, since the defect was invisible on exactly the inputs |
| 41 | + * anyone would have thought to check. |
| 42 | + * |
| 43 | + * The corpus gate at the end is the part that cannot rot. It re-derives the |
| 44 | + * verdict from the checked-in `skills/**` artifacts — the bytes a customer |
| 45 | + * agent actually loads — so a future `.zod.ts` that declares a new |
| 46 | + * `SCREAMING_SNAKE` const above its schemas cannot quietly re-acquire a |
| 47 | + * hazardous row. |
| 48 | + */ |
| 49 | + |
| 50 | +import fs from 'fs'; |
| 51 | +import path from 'path'; |
| 52 | +import url from 'url'; |
| 53 | + |
| 54 | +import { describe, expect, it } from 'vitest'; |
| 55 | + |
| 56 | +import { exportListDescription } from './lib/export-list'; |
| 57 | + |
| 58 | +const HERE = path.dirname(url.fileURLToPath(import.meta.url)); |
| 59 | +const REPO_ROOT = path.resolve(HERE, '../../..'); |
| 60 | +const SKILLS_DIR = path.resolve(REPO_ROOT, 'skills'); |
| 61 | + |
| 62 | +/** Same convention the filter encodes, restated so the gate is self-contained. */ |
| 63 | +const SCREAMING_SNAKE = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/; |
| 64 | + |
| 65 | +describe('exportListDescription — machine constants never headline a pointer row', () => { |
| 66 | + it('drops SCREAMING_SNAKE constants and keeps source order for the rest', () => { |
| 67 | + // `automation/approval.zod.ts`, reduced. The published row opened |
| 68 | + // "Exports: ApproverType, DEPRECATED_APPROVER_TYPES, |
| 69 | + // NON_AUTHORABLE_APPROVER_TYPES, ORG_MEMBERSHIP_LEVELS, |
| 70 | + // APPROVER_EXPRESSION_ROOTS" — four of five names unusable by an author. |
| 71 | + const source = [ |
| 72 | + "export const ApproverType = z.enum(['user', 'role']);", |
| 73 | + 'export const DEPRECATED_APPROVER_TYPES = [] as const;', |
| 74 | + 'export const NON_AUTHORABLE_APPROVER_TYPES = [] as const;', |
| 75 | + 'export const ORG_MEMBERSHIP_LEVELS = [] as const;', |
| 76 | + 'export const APPROVER_EXPRESSION_ROOTS = [] as const;', |
| 77 | + "export const ApprovalDecision = z.enum(['approve']);", |
| 78 | + 'export const ApprovalNodeApproverSchema = z.object({});', |
| 79 | + ].join('\n'); |
| 80 | + |
| 81 | + expect(exportListDescription(source)).toBe( |
| 82 | + 'Exports: ApproverType, ApprovalDecision, ApprovalNodeApproverSchema', |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('keeps source order — it does NOT sort *Schema exports first', () => { |
| 87 | + // Adjudicated on #12201 and pinned here so it is not "improved" later: |
| 88 | + // Schema-first ranking demotes `ApproverType`, the enum an author actually |
| 89 | + // writes, below the schema objects — worse by this surface's own standard. |
| 90 | + const source = [ |
| 91 | + 'export const PluginContextSchema = z.object({});', |
| 92 | + 'export const CORE_PLUGIN_TYPES = [] as const;', |
| 93 | + 'export const ApproverType = z.enum([]);', |
| 94 | + 'export const PluginSchema = z.object({});', |
| 95 | + ].join('\n'); |
| 96 | + |
| 97 | + expect(exportListDescription(source)).toBe( |
| 98 | + 'Exports: PluginContextSchema, ApproverType, PluginSchema', |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('applies the cap of five AFTER filtering, so authorable names are promoted', () => { |
| 103 | + // Slicing first would let the constants consume the row's five slots and |
| 104 | + // then be deleted from it, shortening the row instead of repairing it. |
| 105 | + const source = [ |
| 106 | + 'export const A_CONST = 1;', |
| 107 | + 'export const B_CONST = 1;', |
| 108 | + 'export const One = 1;', |
| 109 | + 'export const Two = 1;', |
| 110 | + 'export const Three = 1;', |
| 111 | + 'export const Four = 1;', |
| 112 | + 'export const Five = 1;', |
| 113 | + 'export const Six = 1;', |
| 114 | + ].join('\n'); |
| 115 | + |
| 116 | + expect(exportListDescription(source)).toBe('Exports: One, Two, Three, Four, Five'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('keeps a lone all-caps token — the boundary is deliberate', () => { |
| 120 | + // No export in the eleven-module fallback corpus is a lone all-caps token, |
| 121 | + // so the corpus cannot distinguish "all caps" from "all caps with an |
| 122 | + // underscore". The narrower rule is chosen; widening it is a decision, and |
| 123 | + // this case is where that decision gets made. |
| 124 | + expect(exportListDescription('export const URL = 1;')).toBe('Exports: URL'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('falls through (null) when every export is a machine constant', () => { |
| 128 | + // Not `Exports:` with nothing after it — the caller prints no description. |
| 129 | + const source = ['export const CORE_PLUGIN_TYPES = [];', 'export const OTHER_KEYS = [];'].join('\n'); |
| 130 | + expect(exportListDescription(source)).toBeNull(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('falls through (null) when the module exports no const at all', () => { |
| 134 | + expect(exportListDescription('export function f() {}\n')).toBeNull(); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('published catalog — no Exports: row names a machine constant', () => { |
| 139 | + /** Every `Exports: …` pointer row in the checked-in skill references. */ |
| 140 | + const publishedRows = (): { file: string; source: string; names: string[] }[] => { |
| 141 | + const rows: { file: string; source: string; names: string[] }[] = []; |
| 142 | + for (const skill of fs.readdirSync(SKILLS_DIR)) { |
| 143 | + const index = path.resolve(SKILLS_DIR, skill, 'references/_index.md'); |
| 144 | + if (!fs.existsSync(index)) continue; |
| 145 | + for (const line of fs.readFileSync(index, 'utf-8').split('\n')) { |
| 146 | + const match = /^- `([^`]+)` — Exports: (.+)$/.exec(line); |
| 147 | + if (match) { |
| 148 | + rows.push({ |
| 149 | + file: path.relative(REPO_ROOT, index), |
| 150 | + source: match[1], |
| 151 | + names: match[2].split(',').map(n => n.trim()), |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return rows; |
| 157 | + }; |
| 158 | + |
| 159 | + it('finds the fallback rows at all', () => { |
| 160 | + // Nothing parsed means nothing compared, and "no hazardous row" would read |
| 161 | + // as green — the same failure mode the generator's own emptiness guard has. |
| 162 | + expect(publishedRows().length).toBeGreaterThan(0); |
| 163 | + }); |
| 164 | + |
| 165 | + it('names no SCREAMING_SNAKE constant on any published row', () => { |
| 166 | + const offenders = publishedRows().flatMap(row => |
| 167 | + row.names.filter(name => SCREAMING_SNAKE.test(name)).map(name => `${row.file}: ${row.source} → ${name}`), |
| 168 | + ); |
| 169 | + expect(offenders).toEqual([]); |
| 170 | + }); |
| 171 | +}); |
0 commit comments