@behavioral-agentic-design-system/core 0.39.0 → 0.40.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 (78) hide show
  1. package/package.json +8 -3
  2. package/scripts/a11y-lint.cjs +93 -39
  3. package/scripts/a11y-ratchet.mjs +10 -23
  4. package/scripts/bads-components.mjs +118 -0
  5. package/scripts/build-tokens.cjs +175 -17
  6. package/scripts/check-stories.mjs +1 -1
  7. package/scripts/health-report.mjs +52 -10
  8. package/scripts/lib/scan-roots.cjs +143 -0
  9. package/scripts/lint-component-source.mjs +100 -0
  10. package/scripts/lint-microanimation.mjs +267 -0
  11. package/scripts/lint-slop.mjs +21 -28
  12. package/scripts/lint-token-intent.mjs +10 -16
  13. package/scripts/size-audit.cjs +94 -27
  14. package/scripts/source-of-truth.cjs +58 -0
  15. package/scripts/ux-smoke.mjs +438 -0
  16. package/scripts/wcag-audit.cjs +49 -1
  17. package/src/api/layers/file.ts +2 -0
  18. package/src/app/preview/components/page.tsx +972 -0
  19. package/src/app/preview/health/page.tsx +8 -5
  20. package/src/app/preview/history/page.tsx +11 -0
  21. package/src/app/shell.tsx +261 -87
  22. package/src/components/customize/brand-mark-section.tsx +4 -2
  23. package/src/components/customize/design-controls.tsx +1 -1
  24. package/src/components/customize/discipline-section.tsx +91 -0
  25. package/src/components/customize/font-pickers.tsx +1 -1
  26. package/src/components/customize/letter-spacing.tsx +1 -1
  27. package/src/components/customize/save-bar.tsx +2 -2
  28. package/src/components/customize/theme-io.tsx +7 -7
  29. package/src/components/customize/type-scale.tsx +1 -1
  30. package/src/components/customize-tab.tsx +9 -4
  31. package/src/components/home-tab.tsx +3 -0
  32. package/src/components/ui/accordion.tsx +7 -0
  33. package/src/components/ui/alert.tsx +1 -0
  34. package/src/components/ui/announcement-bar.tsx +256 -0
  35. package/src/components/ui/badge.tsx +1 -0
  36. package/src/components/ui/callout.tsx +218 -0
  37. package/src/components/ui/chart.tsx +2 -14
  38. package/src/components/ui/color-picker.tsx +1 -1
  39. package/src/components/ui/format-relative-time.tsx +139 -0
  40. package/src/components/ui/grid.tsx +255 -0
  41. package/src/components/ui/icon.tsx +108 -0
  42. package/src/components/ui/index.ts +8 -0
  43. package/src/components/ui/page-header.tsx +63 -0
  44. package/src/components/ui/portal.tsx +111 -0
  45. package/src/components/ui/presence.tsx +172 -0
  46. package/src/components/ui/progress-stack.tsx +258 -0
  47. package/src/components/ui/select.tsx +89 -4
  48. package/src/components/ui/separator.tsx +1 -0
  49. package/src/components/ui/slider.tsx +83 -3
  50. package/src/components/ui/sparkline.tsx +15 -15
  51. package/src/components/ui/textarea.tsx +28 -1
  52. package/src/data/docs-index.ts +5 -0
  53. package/src/data/history.json +269 -0
  54. package/src/hooks/use-reduced-motion.ts +6 -0
  55. package/src/stories/Accordion.stories.tsx +59 -0
  56. package/src/stories/Alert.stories.tsx +3 -1
  57. package/src/stories/AnnouncementBar.stories.tsx +261 -0
  58. package/src/stories/Badge.stories.tsx +3 -0
  59. package/src/stories/Calendar.stories.tsx +1 -1
  60. package/src/stories/Callout.stories.tsx +219 -0
  61. package/src/stories/Chart.stories.tsx +4 -0
  62. package/src/stories/ColorPicker.stories.tsx +23 -0
  63. package/src/stories/FormatRelativeTime.stories.tsx +116 -0
  64. package/src/stories/Grid.stories.tsx +197 -0
  65. package/src/stories/Icon.stories.tsx +175 -0
  66. package/src/stories/MultiSelect.stories.tsx +1 -1
  67. package/src/stories/PageHeader.stories.tsx +102 -0
  68. package/src/stories/Portal.stories.tsx +189 -0
  69. package/src/stories/Presence.stories.tsx +228 -0
  70. package/src/stories/ProgressStack.stories.tsx +287 -0
  71. package/src/stories/Select.stories.tsx +25 -0
  72. package/src/stories/Separator.stories.tsx +3 -0
  73. package/src/stories/Sheet.stories.tsx +9 -3
  74. package/src/stories/Slider.stories.tsx +54 -5
  75. package/src/stories/Sparkline.stories.tsx +39 -0
  76. package/src/stories/Textarea.stories.tsx +18 -0
  77. package/src/stories/story-groups.json +14 -5
  78. package/src/styles.css +133 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behavioral-agentic-design-system/core",
3
- "version": "0.39.0",
3
+ "version": "0.40.0",
4
4
  "description": "BADS core — tokens, shadcn/ui components, and the BADS template shell (app routes, customize tab, theme hooks). Forks consume this and re-export, keeping client repos to ~30 lines of glue.",
5
5
  "license": "MIT",
6
6
  "author": "Matthew Stephens <work@matthewstephens.com> (https://github.com/matthewlarn)",
@@ -43,23 +43,28 @@
43
43
  "bads-build-tokens": "scripts/build-tokens.cjs",
44
44
  "bads-check-stories": "scripts/check-stories.mjs",
45
45
  "bads-check-sync": "scripts/check-sync.mjs",
46
+ "bads-components": "scripts/bads-components.mjs",
46
47
  "bads-doctor": "scripts/bads-doctor.mjs",
47
48
  "bads-fix-stories": "scripts/fix-stories.mjs",
48
49
  "bads-generate-stories": "scripts/generate-stories.cjs",
49
50
  "bads-health-report": "scripts/health-report.mjs",
50
51
  "bads-lint-accent-text": "scripts/accent-as-text.mjs",
52
+ "bads-lint-component-source": "scripts/lint-component-source.mjs",
51
53
  "bads-lint-ethics": "scripts/lint-ethics-contract.mjs",
54
+ "bads-lint-microanimation": "./scripts/lint-microanimation.mjs",
52
55
  "bads-lint-slop": "scripts/lint-slop.mjs",
53
56
  "bads-lint-token-intent": "scripts/lint-token-intent.mjs",
54
- "bads-token-nearest": "scripts/token-nearest.mjs",
55
57
  "bads-oklch-to-hex": "scripts/oklch-to-hex.cjs",
56
58
  "bads-size-audit": "scripts/size-audit.cjs",
57
59
  "bads-sync": "scripts/bads-sync.mjs",
60
+ "bads-token-nearest": "scripts/token-nearest.mjs",
61
+ "bads-ux-smoke": "./scripts/ux-smoke.mjs",
58
62
  "bads-wcag-audit": "scripts/wcag-audit.cjs"
59
63
  },
60
64
  "exports": {
61
65
  ".": "./src/index.ts",
62
66
  "./package.json": "./package.json",
67
+ "./scripts/*": "./scripts/*",
63
68
  "./styles.css": "./src/styles.css",
64
69
  "./fonts.css": "./src/fonts.css",
65
70
  "./fonts-cdn.css": "./src/fonts-cdn.css",
@@ -261,6 +266,6 @@
261
266
  }
262
267
  },
263
268
  "devDependencies": {
264
- "@types/react-dom": "^19.2.7"
269
+ "@types/react-dom": "^19.3.0"
265
270
  }
266
271
  }
@@ -17,50 +17,58 @@
17
17
  * 2.4.13 Focus Appearance — outline-none / focus:outline-none w/o replacement
18
18
  * 3.3.2 Labels or Instructions — <input> without associated <Label> or aria-label
19
19
  * 4.1.2 Name, Role, Value — <button> with no text and no aria-label
20
- * SADS-1 Font weight floor — text-xs/text-sm must be font-medium (500) or heavier
20
+ * BADS-WEIGHT Font weight floor — text-xs/text-sm must be font-medium (500) or heavier
21
21
  *
22
22
  * Run:
23
- * node scripts/a11y-lint.js # report violations, exit 1 on fail
24
- * node scripts/a11y-lint.js --warn # report but exit 0 (advisory)
25
- * node scripts/a11y-lint.js path/to.tsx # scan a specific file
23
+ * bads-a11y-lint # report violations, exit 1 on fail
24
+ * bads-a11y-lint --warn # report but exit 0 (advisory)
25
+ * bads-a11y-lint path/to.tsx # scan a specific file
26
26
  */
27
27
 
28
28
  'use strict';
29
29
 
30
30
  const fs = require('fs');
31
31
  const path = require('path');
32
+ const { resolveScanFiles, guardEmptyScan } = require('./lib/scan-roots.cjs');
33
+ const { readSourceOfTruth } = require('./source-of-truth.cjs');
32
34
 
33
35
  // Shipped as the `bads-a11y-lint` bin. process.cwd() means the same script
34
36
  // scans the right tree whether it runs in upstream BADS (CWD = repo root) or
35
- // in a client fork (CWD = fork root). The `packages/core/src/components` scan
36
- // root no-ops in a fork (the dir is a stub) via the existsSync guard in walk().
37
+ // in a client fork (CWD = fork root). Scan roots (and the "did this actually
38
+ // scan anything" guard) live in ./lib/scan-roots.cjs, shared with
39
+ // size-audit.cjs, lint-slop.mjs, a11y-ratchet.mjs and lint-token-intent.mjs —
40
+ // see that file for why: these five used to carry drifted copies of the same
41
+ // list, and a consumer repo whose tree matched none of them got a silent
42
+ // false "no violations found" instead of an error. Set BADS_LINT_ROOTS to
43
+ // point this at a non-standard tree.
37
44
  const ROOT = process.cwd();
38
45
  const advisory = process.argv.includes('--warn');
39
- const explicitPaths = process.argv.slice(2).filter((a) => !a.startsWith('--'));
40
-
41
- const SCAN_ROOTS = [
42
- 'app',
43
- 'components',
44
- 'packages/core/src/components',
45
- ];
46
-
47
- function walk(dir, out = []) {
48
- if (!fs.existsSync(dir)) return out;
49
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
50
- if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
51
- const full = path.join(dir, entry.name);
52
- if (entry.isDirectory()) {
53
- walk(full, out);
54
- } else if (/\.tsx?$/.test(entry.name)) {
55
- out.push(full);
56
- }
57
- }
58
- return out;
46
+
47
+ // Client-owned mode (.client-identity.json → sourceOfTruth.components ===
48
+ // "client") downgrades the AAA and house-opinion rules below to warnings —
49
+ // still reported, never silently dropped — while every Level A rule keeps
50
+ // blocking. See docs/superpowers/specs/2026-09-10-client-owned-components-design.md
51
+ // → "Accessibility floor: flag, do not enforce" and CLAUDE.md → Accessibility
52
+ // Floor. readSourceOfTruth() never throws and defaults to "bads" when there's
53
+ // no identity file, so upstream BADS and every fork that hasn't opted in see
54
+ // zero behaviour change.
55
+ const CLIENT_OWNED = readSourceOfTruth(ROOT).components === 'client';
56
+
57
+ // SC codes that are a matter of AAA standard or a BADS house opinion, not a
58
+ // Level A defect. 1.4.6 (hex-literal contrast), 1.4.4 (inline-px-font,
59
+ // tiny-text — BADS' 14px floor), 2.4.13 (focus-removed), BADS-WEIGHT (font
60
+ // weight floor) and BADS-CHART (hand-rolled chart) all belong here. The four
61
+ // Level A codes (1.1.1, 2.1.1, 2.4.4, 4.1.2) are deliberately absent — those
62
+ // are real defects in anyone's markup and keep blocking in every mode.
63
+ const WARN_IN_CLIENT_MODE = new Set(['1.4.4', '1.4.6', '2.4.13', 'BADS-WEIGHT', 'BADS-CHART']);
64
+ const CLIENT_OWNED_NOTE = ' (warned, not failed: components are client-owned)';
65
+
66
+ function isDowngraded(it) {
67
+ return CLIENT_OWNED && WARN_IN_CLIENT_MODE.has(it.sc);
59
68
  }
60
69
 
61
- const files = explicitPaths.length
62
- ? explicitPaths.map((p) => path.resolve(p))
63
- : SCAN_ROOTS.flatMap((r) => walk(path.join(ROOT, r)));
70
+ const scan = resolveScanFiles({ root: ROOT, argv: process.argv.slice(2) });
71
+ const files = scan.files;
64
72
 
65
73
  const issues = [];
66
74
 
@@ -146,6 +154,17 @@ const ON_CLICK_NON_INTERACTIVE = /<(?:div|span|p)\b[^>]*onClick=/;
146
154
 
147
155
  const VAGUE_LINK_TEXT = /^(?:click here|here|read more|learn more|more|click)\.?$/i;
148
156
 
157
+ // next/og's ImageResponse (Satori) renders through its own layout engine
158
+ // with no access to computed CSS custom properties or a Tailwind stylesheet
159
+ // — only literal inline style values reach it. hex-literal and
160
+ // inline-px-font are structurally impossible to satisfy in that file, not a
161
+ // lapse (found 2026-09-09, expanding scan roots to include
162
+ // packages/core/src/app surfaced these for the first time — they were never
163
+ // scanned before). Skip just those two rules for the route, not the whole
164
+ // file. Forks only ever re-export this route (see the file's own header
165
+ // comment), so the violation only ever exists in this one upstream copy.
166
+ const SATORI_TOKEN_EXEMPT_FILE = /\bopengraph-image\.tsx$/;
167
+
149
168
  for (const file of files) {
150
169
  let src;
151
170
  try { src = fs.readFileSync(file, 'utf8'); } catch { continue; }
@@ -155,6 +174,7 @@ for (const file of files) {
155
174
  if (file.includes('/e2e/') || file.endsWith('.spec.ts') || file.endsWith('.test.ts')) continue;
156
175
  // Skip story files except for headline checks (they're fine to be terse).
157
176
  const isStory = file.endsWith('.stories.tsx');
177
+ const skipTokenRules = SATORI_TOKEN_EXEMPT_FILE.test(file);
158
178
 
159
179
  const TS = scaleFor(file);
160
180
 
@@ -163,14 +183,26 @@ for (const file of files) {
163
183
  const line = lines[i];
164
184
  const lineNum = i + 1;
165
185
 
166
- if (HARDCODED_HEX.test(line)) {
186
+ // Escape hatch, same convention as allow-small-text below: same line or
187
+ // the line directly above. Added 2026-09-09 alongside the Satori file
188
+ // exemption — until now these two rules had no bypass at all, unlike
189
+ // every other check in this file. Legitimate use: a hex value shown as
190
+ // literal TEXT CONTENT describing a color (e.g. the Customize tab's own
191
+ // "#171717 · selected" picker label) is data, not an uncontrolled style
192
+ // decision — the thing SC 1.4.6 actually cares about.
193
+ const allowHex = /\/\/\s*allow-hex-literal/.test(line)
194
+ || (i > 0 && /allow-hex-literal/.test(lines[i - 1]));
195
+ const allowInlinePx = /\/\/\s*allow-inline-px-font/.test(line)
196
+ || (i > 0 && /allow-inline-px-font/.test(lines[i - 1]));
197
+
198
+ if (HARDCODED_HEX.test(line) && !skipTokenRules && !allowHex) {
167
199
  flag(file, lineNum, '1.4.6', 'hex-literal',
168
- 'Hardcoded hex color. Use a semantic token or @theme variable instead.');
200
+ 'Hardcoded hex color. Use a semantic token or @theme variable instead, or add "// allow-hex-literal" if this is a hex value shown as text content, not a style.');
169
201
  }
170
202
 
171
- if (INLINE_PX_FONT.test(line)) {
203
+ if (INLINE_PX_FONT.test(line) && !skipTokenRules && !allowInlinePx) {
172
204
  flag(file, lineNum, '1.4.4', 'inline-px-font',
173
- 'Inline pixel font size. Use a text-* utility or rem-based value so 200% zoom still works.');
205
+ 'Inline pixel font size. Use a text-* utility or rem-based value so 200% zoom still works, or add "// allow-inline-px-font" with a reason (e.g. Satori/next-og cannot resolve tokens).');
174
206
  }
175
207
 
176
208
  // Same escape hatch as the named-step check below. A vendored component that ships
@@ -200,12 +232,12 @@ for (const file of files) {
200
232
  `${step} resolves to ${TS.scale[step]}px under ${TS.label}, below the ${TEXT_FLOOR_PX}px AAA floor. Use ${TS.smallestCompliant} or larger, or add "// allow-small-text" above if a vendored component forces it.`);
201
233
  }
202
234
 
203
- // SADS-1 — small text (text-xs/text-sm, <18px) must be font-medium (500) or heavier.
235
+ // BADS-WEIGHT — small text (text-xs/text-sm, <18px) must be font-medium (500) or heavier.
204
236
  if (TS.smallTextLightWeight && TS.smallTextLightWeight.test(line)
205
237
  && !/\/\/\s*allow-font-normal-small/.test(line)
206
238
  && !(i > 0 && /allow-font-normal-small/.test(lines[i - 1]))) {
207
- flag(file, lineNum, 'SADS-1', 'small-text-light-weight',
208
- `font-normal (or lighter) on a step below ${WEIGHT_FLOOR_PX}px (${TS.smallStepsDesc}). SADS weight floor: font-medium (500) minimum. Add "// allow-font-normal-small" above to intentionally bypass.`);
239
+ flag(file, lineNum, 'BADS-WEIGHT', 'small-text-light-weight',
240
+ `font-normal (or lighter) on a step below ${WEIGHT_FLOOR_PX}px (${TS.smallStepsDesc}). Weight floor: font-medium (500) minimum. Add "// allow-font-normal-small" above to intentionally bypass.`);
209
241
  }
210
242
 
211
243
  if (FOCUS_OUTLINE_NONE.test(line) && !/ring-|outline-|focus-visible:/.test(line)) {
@@ -283,7 +315,12 @@ for (const file of files) {
283
315
  // ---- output ------------------------------------------------------------
284
316
 
285
317
  if (issues.length === 0) {
286
- console.log('✓ a11y-lint: no AAA violations found.');
318
+ if (guardEmptyScan(scan, 'a11y-lint')) { /* exited */ }
319
+ console.log(
320
+ scan.files.length
321
+ ? '✓ a11y-lint: no AAA violations found.'
322
+ : '✓ a11y-lint: no AAA violations found (0 files matched under existing scan roots).'
323
+ );
287
324
  process.exit(0);
288
325
  }
289
326
 
@@ -296,9 +333,26 @@ for (const file of Object.keys(grouped).sort()) {
296
333
  console.log(`\n${file}`);
297
334
  for (const it of grouped[file]) {
298
335
  const tag = `[SC ${it.sc} ${it.rule}]`;
299
- console.log(` L${it.line} ${tag.padEnd(28)} ${it.message}`);
336
+ const note = isDowngraded(it) ? CLIENT_OWNED_NOTE : '';
337
+ console.log(` L${it.line} ${tag.padEnd(28)} ${it.message}${note}`);
300
338
  }
301
339
  }
302
340
 
341
+ const blocking = issues.filter((it) => !isDowngraded(it));
342
+ const downgraded = issues.length - blocking.length;
343
+
303
344
  console.log(`\n${issues.length} issue${issues.length === 1 ? '' : 's'} across ${Object.keys(grouped).length} file${Object.keys(grouped).length === 1 ? '' : 's'}.`);
304
- process.exit(advisory ? 0 : 1);
345
+ if (downgraded > 0) {
346
+ // Indented deliberately: a11y-ratchet.mjs parses this script's stdout and
347
+ // treats any *unindented* line that doesn't match /^\d+ issues?/ as a new
348
+ // file-header — an unindented "N of those are warned..." line would get
349
+ // mistaken for a bogus file path. Indenting keeps it inert to that parser
350
+ // (it fails the finding regex and is simply skipped) without changing the
351
+ // line the ratchet and bads-doctor.mjs already key off.
352
+ console.log(
353
+ ` ${downgraded} of those ${downgraded === 1 ? 'is' : 'are'} warned, not failed: components are ` +
354
+ `client-owned (sourceOfTruth.components === "client"), so the AAA and BADS house-opinion floor ` +
355
+ `is informational here. ${blocking.length} blocking issue${blocking.length === 1 ? '' : 's'} remain${blocking.length === 1 ? 's' : ''}.`,
356
+ );
357
+ }
358
+ process.exit(advisory || blocking.length === 0 ? 0 : 1);
@@ -33,7 +33,6 @@
33
33
  * gate exists to prevent.
34
34
  */
35
35
 
36
- import fs from "node:fs";
37
36
  import path from "node:path";
38
37
  import { fileURLToPath } from "node:url";
39
38
  import { execFileSync } from "node:child_process";
@@ -44,6 +43,8 @@ import {
44
43
  BASELINE_FILE,
45
44
  SCHEMA_VERSION,
46
45
  } from "./ratchet.mjs";
46
+ import scanRootsLib from "./lib/scan-roots.cjs";
47
+ const { resolveScanFiles, guardEmptyScan } = scanRootsLib;
47
48
 
48
49
  const ROOT = process.cwd();
49
50
  // The lint is resolved relative to THIS file, not to cwd, so a fork running the
@@ -61,32 +62,12 @@ const INIT = argv.includes("--init");
61
62
  const ADVISORY = argv.includes("--warn");
62
63
  const RULE_ID = `a11y:${RULE}`;
63
64
 
64
- // Positional args override the defaults, so a fork points it at its own tree
65
- // (`npx bads-a11y-ratchet app/_content app/shell`) the way size-audit does.
66
- const argRoots = argv.filter((a, i) => !a.startsWith("--") && !argv[i - 1]?.startsWith("--"));
67
- const SCAN_ROOTS = argRoots.length
68
- ? argRoots
69
- : ["app", "components", "packages/core/src/components", "packages/core/src/app"];
65
+ const scan = resolveScanFiles({ root: ROOT, argv: process.argv.slice(2), allowDirArgs: true });
70
66
 
71
67
  // ---------------------------------------------------------------------------
72
68
  // Measurement
73
69
  // ---------------------------------------------------------------------------
74
70
 
75
- function scanFiles() {
76
- const out = [];
77
- const walk = (dir) => {
78
- if (!fs.existsSync(dir)) return;
79
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
80
- if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
81
- const full = path.join(dir, entry.name);
82
- if (entry.isDirectory()) walk(full);
83
- else if (/\.tsx$/.test(entry.name) && !/\.stories\.tsx$/.test(entry.name)) out.push(full);
84
- }
85
- };
86
- for (const r of SCAN_ROOTS) walk(path.join(ROOT, r));
87
- return out.map((f) => path.relative(ROOT, f));
88
- }
89
-
90
71
  /**
91
72
  * `{ relativePath: count }` for one a11y-lint rule.
92
73
  *
@@ -131,7 +112,13 @@ function measure(files) {
131
112
  // Run
132
113
  // ---------------------------------------------------------------------------
133
114
 
134
- const files = scanFiles();
115
+ const files = scan.files;
116
+ // Guard BEFORE measuring, not just before printing: with 0 files, `measure`
117
+ // returns an all-zero count for every rule, which reads as "no violations,
118
+ // no growth" — indistinguishable from a genuinely clean scan. That's the
119
+ // exact false-green class this shares with a11y-lint/size-audit/lint-slop
120
+ // (see ./lib/scan-roots.cjs). Refuse before any ratchet comparison runs.
121
+ if (guardEmptyScan(scan, 'a11y-ratchet')) { /* exited */ }
135
122
  const current = measure(files);
136
123
 
137
124
  if (INIT) {
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * bads-components — flip the component ownership switch for a fork.
4
+ *
5
+ * pnpm bads:components-off --library @/components/acme [--docs URL] [--scan-roots app,components/acme]
6
+ * pnpm bads:components-on
7
+ *
8
+ * It writes `.client-identity.json`, and it patches the fork's CLAUDE.md in
9
+ * place. That patch is not a convenience: CLAUDE.md is fork-owned and never
10
+ * syncs over npm, so editing the fork's own copy is the ONLY way the agent
11
+ * layer of this feature reaches an existing fork.
12
+ */
13
+ import { readFileSync, writeFileSync, existsSync } from "node:fs";
14
+ import { join } from "node:path";
15
+
16
+ const ROOT = process.cwd();
17
+ const IDENTITY = join(ROOT, ".client-identity.json");
18
+ const CLAUDE = join(ROOT, "CLAUDE.md");
19
+ const OPEN = "<!-- bads:component-ownership -->";
20
+ const CLOSE = "<!-- /bads:component-ownership -->";
21
+
22
+ const [command, ...rest] = process.argv.slice(2);
23
+ const flag = (name) => {
24
+ const i = rest.indexOf(`--${name}`);
25
+ return i === -1 ? null : rest[i + 1] ?? null;
26
+ };
27
+
28
+ function die(message) {
29
+ console.error(`FAIL bads-components: ${message}`);
30
+ process.exit(1);
31
+ }
32
+
33
+ if (!["on", "off"].includes(command)) {
34
+ console.error("Usage: bads-components off --library <specifier> [--docs <url>] [--scan-roots a,b]");
35
+ console.error(" bads-components on");
36
+ process.exit(1);
37
+ }
38
+
39
+ if (!existsSync(IDENTITY)) {
40
+ die("no .client-identity.json here. Run `pnpm rename \"<Name>\" <slug>` first; this switch is a fork setting, not an upstream one.");
41
+ }
42
+
43
+ const identity = JSON.parse(readFileSync(IDENTITY, "utf8"));
44
+
45
+ function section(library, docs) {
46
+ return [
47
+ OPEN,
48
+ "",
49
+ "## Component ownership",
50
+ "",
51
+ `**This fork's components are client-owned.** They come from \`${library}\`${docs ? ` (docs: ${docs})` : ""}.`,
52
+ "",
53
+ "- Do not propose, build, or reach for a BADS primitive. Use the client's library.",
54
+ "- Do not add entries to `layer-5-component-registry/component-registry.json`.",
55
+ "- Do not run the component-builder skill against `@behavioral-agentic-design-system/core`.",
56
+ "- `pnpm lint:component-source` enforces this. The escape hatch is `// allow-bads-component <reason>`.",
57
+ "",
58
+ "Everything else in BADS still applies: the AAA accessibility floor, the CTA size contract, voice and tone, the ethics layers, the futures check, and the slop lint. Those are what this engagement is buying.",
59
+ "",
60
+ CLOSE,
61
+ ].join("\n");
62
+ }
63
+
64
+ function patchClaudeMd(block) {
65
+ if (!existsSync(CLAUDE)) {
66
+ console.log(" note: no CLAUDE.md here, skipping the agent-layer patch.");
67
+ return;
68
+ }
69
+ const current = readFileSync(CLAUDE, "utf8");
70
+ const start = current.indexOf(OPEN);
71
+ const end = current.indexOf(CLOSE);
72
+
73
+ if (start !== -1 && end !== -1) {
74
+ const next = current.slice(0, start) + (block ?? "").trimEnd() + current.slice(end + CLOSE.length);
75
+ writeFileSync(CLAUDE, block ? next : next.replace(/\n{3,}/g, "\n\n"));
76
+ } else if (block) {
77
+ writeFileSync(CLAUDE, `${current.trimEnd()}\n\n${block}\n`);
78
+ }
79
+ }
80
+
81
+ if (command === "off") {
82
+ const library = flag("library");
83
+ if (!library) die("`off` needs --library <specifier>. It accepts an npm package (@acme/ui) or a path alias (@/components/acme).");
84
+
85
+ const docs = flag("docs");
86
+ const scanRoots = (flag("scan-roots") || "").split(",").map((s) => s.trim()).filter(Boolean);
87
+
88
+ identity.sourceOfTruth = {
89
+ components: "client",
90
+ clientLibrary: library,
91
+ ...(docs ? { clientDocs: docs } : {}),
92
+ ...(scanRoots.length ? { scanRoots } : {}),
93
+ };
94
+ writeFileSync(IDENTITY, JSON.stringify(identity, null, 2) + "\n");
95
+ patchClaudeMd(section(library, docs));
96
+
97
+ console.log(`OK bads-components: components are now client-owned (${library}).`);
98
+ console.log(" - lint:component-source is now active");
99
+ console.log(` - it scans ${scanRoots.length ? scanRoots.join(", ") : "app/ (pass --scan-roots to widen)"}`);
100
+ console.log(" - CLAUDE.md now carries the component-ownership section");
101
+ } else {
102
+ delete identity.sourceOfTruth;
103
+ writeFileSync(IDENTITY, JSON.stringify(identity, null, 2) + "\n");
104
+ patchClaudeMd(null);
105
+ console.log("OK bads-components: BADS owns components again.");
106
+ }
107
+
108
+ // Follow-ups this script cannot perform, because they live at the fork's repo
109
+ // root and this script does not own that code.
110
+ for (const [path, pattern, note] of [
111
+ ["app/page.tsx", /DEFAULT_TABS/, "references DEFAULT_TABS directly; it needs visibleTabs() to drop the component tabs"],
112
+ [".storybook/main.ts", /core\/src\/stories/, "still globs the BADS component stories"],
113
+ ]) {
114
+ const full = join(ROOT, path);
115
+ if (existsSync(full) && pattern.test(readFileSync(full, "utf8"))) {
116
+ console.log(` follow-up: ${path} ${note}`);
117
+ }
118
+ }