create-ailk 0.43.0 → 0.45.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.
package/dist/update.js CHANGED
@@ -87,6 +87,15 @@
87
87
  * additively. A written web file that uses, through `t(…)`, a template key a
88
88
  * catalog still lacks after that is held with the catalog.
89
89
  *
90
+ * The web build script comes with the template (#11940). `package.json` is
91
+ * never recorded, so `apps/web/package.json`'s `scripts.build` is read
92
+ * against the published history, as a stale copy is (#11147): a value equal
93
+ * to a published template's in the run's range is set to the running
94
+ * template's and listed under "Build scripts carried"; one matching none is
95
+ * the site's own, left as it is and listed under "Needs review". The carry
96
+ * waits, under review, while the site would lack a `tsx` file the new value
97
+ * runs.
98
+ *
90
99
  * A route the site declined in `customizations.yaml` `declined_routes`
91
100
  * (#11260) is the site's decision: no file under its directory is written —
92
101
  * not updated, not added, not supplied as another file's dependency — and
@@ -97,7 +106,7 @@
97
106
  */
98
107
  import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, rmdirSync, statSync, writeFileSync, } from "node:fs";
99
108
  import { createRequire } from "node:module";
100
- import { dirname, join, relative, resolve, sep } from "node:path";
109
+ import { dirname, join, posix, relative, resolve, sep } from "node:path";
101
110
  import { pathToFileURL } from "node:url";
102
111
  import { parseArgs } from "node:util";
103
112
  import { AddModuleRefusedError, readArchitectureHeader } from "./add-module-core.js";
@@ -111,16 +120,20 @@ import { ENV_CATALOG } from "./lib/env-catalog.js";
111
120
  import { assertContentLengthWithinLimit, assertHttpsRegistryUrl, fetchTimeoutSignal, } from "./lib/fetch-guard.js";
112
121
  import { DEFAULT_AILK_NPM_REGISTRY_URL, fetchTemplateSnapshot } from "./lib/fetch-template-snapshot.js";
113
122
  import { exportsName, listFilesUnder, namedImportsOf } from "./lib/import-graph.js";
123
+ import { readSiteKey } from "./lib/license-key-files.js";
114
124
  import { mergeThreeWay } from "./lib/merge-three-way.js";
125
+ import { deriveLevel } from "./lib/module-license-gate.js";
126
+ import { LEVEL_LABEL } from "./lib/module-tier.js";
115
127
  import { publicTreeRemoved } from "./lib/site-modules.js";
116
128
  import { escapeBareBraces, findBareBraces } from "./mdx-braces.js";
117
129
  import { MODULE_SLICES } from "./module-architecture.js";
118
- import { MODULE_FLAGS_PATH, PAID_MCP_TOOLS_PATH, PAID_SECTIONS_PATH, renderSitePaidSections, writeModuleFlags, writePaidMcpTools, writePaidSectionsRegistry, } from "./paid-sections.js";
130
+ import { MODULE_FLAGS_PATH, PAID_MCP_TOOLS_PATH, PAID_SECTIONS_PATH, levelMcpPackages, renderSitePaidSections, withLevelMcpPackages, writeModuleFlags, writePaidMcpTools, writePaidSectionsRegistry, } from "./paid-sections.js";
119
131
  import { ParseError } from "./parse-args.js";
120
132
  import { PAGE_DECISION_GUIDANCE, RoutesRefusedError, checkRoutes, customizationsRecord, isGenerated, writeRoutes } from "./routes.js";
121
133
  import { planSeamRefresh, seamRow, siteSeamRenders } from "./seam-refresh.js";
122
134
  import { SITE_LISTING_PATH } from "./site-listing-path.js";
123
135
  import { SITE_ROSTER_PATH, planServedRoster, readRosterPageRoutes } from "./site-roster.js";
136
+ import { substituteMarkers } from "./substitute.js";
124
137
  import { GATEABLE_SURFACES, excludedRoutePaths, isRemovedPublicRoutePath, resolveSurfaces, surfacesNamedByRoutes, } from "./surfaces.js";
125
138
  import { DATABASE_BOOT_FILES, ROOT_TOOL_FILES, TEMPLATE_RECORD_PATH, isRecordable, parseTemplateRecord, serializeTemplateRecord, sha256Hex, } from "./template-record.js";
126
139
  import { MCP_SEAM_PATH, MCP_SERVER_HUB, SPOKE_SEAM_FILES, TopologyError, isSpokeExcluded, readSpokeShape, spokeExclusions, } from "./topology.js";
@@ -1306,6 +1319,25 @@ const WEB_PACKAGE_JSON = "apps/web/package.json";
1306
1319
  * Frozen; a name joins only with a spec.
1307
1320
  */
1308
1321
  const WEB_CARRIED_DEPENDENCIES = Object.freeze(["botid"]);
1322
+ /**
1323
+ * The `apps/web/package.json` scripts `update` carries (#11940): a value that
1324
+ * is a published template's in the run's range moves to the running
1325
+ * template's; one matching none is the site's own and is reviewed. Frozen; a
1326
+ * name joins only with a spec.
1327
+ */
1328
+ const WEB_CARRIED_SCRIPTS = Object.freeze(["build"]);
1329
+ /** Each `tsx <file>.ts` a script runs (#11940 D4), resolved under `apps/web/`. */
1330
+ const TSX_STEP = /\btsx\s+(\S+\.ts)\b/g;
1331
+ /** The `scripts` of a template's `apps/web/package.json` text — `undefined` when not JSON or not an object (#11940 D3). */
1332
+ function webScriptsOf(text) {
1333
+ try {
1334
+ const scripts = JSON.parse(text).scripts;
1335
+ return isPlainObject(scripts) ? scripts : undefined;
1336
+ }
1337
+ catch {
1338
+ return undefined;
1339
+ }
1340
+ }
1309
1341
  /** Whether the site's `pkg` declares `name`, in `dependencies` or `devDependencies`, at any range. */
1310
1342
  function declares(pkg, name) {
1311
1343
  return Object.hasOwn(pkg.dependencies ?? {}, name) || Object.hasOwn(pkg.devDependencies ?? {}, name);
@@ -1456,8 +1488,20 @@ export async function updateSite(opts) {
1456
1488
  // ── The running version's rendered copies ─────────────────────────────
1457
1489
  const runningRaw = walkLocalTemplates(opts.templatesDir);
1458
1490
  const newMap = renderSnapshot(runningRaw, record.projectName, version);
1491
+ // #11940 D3 — a version's `apps/web/package.json` scripts, read before
1492
+ // `renderSnapshot` drops the unrecorded path. Markers only: the workspace
1493
+ // version pass rewrites `dependencies`, never `scripts`, and would refuse
1494
+ // here for want of a versions map.
1495
+ const webScriptsIn = (raw, v) => {
1496
+ const bytes = raw.get(WEB_PACKAGE_JSON);
1497
+ return bytes === undefined
1498
+ ? undefined
1499
+ : webScriptsOf(substituteMarkers(Buffer.from(bytes).toString("utf8"), markersForVersion(record.projectName, v)));
1500
+ };
1501
+ const runningWebScripts = webScriptsIn(runningRaw, version);
1459
1502
  // ── A cache of rendered copies by version, fetched at most once each ──
1460
1503
  const versionCache = new Map();
1504
+ const webScriptsByVersion = new Map();
1461
1505
  async function loadVersionMap(v) {
1462
1506
  const cached = versionCache.get(v);
1463
1507
  if (cached !== undefined)
@@ -1474,6 +1518,7 @@ export async function updateSite(opts) {
1474
1518
  ...(opts.fetchImpl !== undefined ? { fetchImpl: opts.fetchImpl } : {}),
1475
1519
  });
1476
1520
  result = renderSnapshot(raw, record.projectName, v);
1521
+ webScriptsByVersion.set(v, webScriptsIn(raw, v));
1477
1522
  }
1478
1523
  catch (err) {
1479
1524
  result = { error: err.message };
@@ -1570,6 +1615,55 @@ export async function updateSite(opts) {
1570
1615
  const mcpMove = existsSync(mcpPkgPath)
1571
1616
  ? planMcpMove(targetDir, header.modules, spoke === undefined, new Set(Object.keys(record.files)), opts.versions ?? {})
1572
1617
  : undefined;
1618
+ // ── The level's MCP packs (#11952 D4), planned before the first write ──
1619
+ // A site missing a paid MCP pack its licence level ships gets it. The
1620
+ // level comes only from the site's own key (NFR1); a site already holding
1621
+ // every pack either paid level ships reads no key and sends no request.
1622
+ let mcpLevelPlan;
1623
+ let mcpLevelUnchecked;
1624
+ if (existsSync(mcpPkgPath)) {
1625
+ const haveText = mcpMove?.packageJson ?? readFileSync(mcpPkgPath, "utf8");
1626
+ let have;
1627
+ try {
1628
+ have = JSON.parse(haveText).dependencies ?? {};
1629
+ }
1630
+ catch (err) {
1631
+ throw new UpdateRefusedError(`${MCP_PACKAGE_JSON} is not valid JSON: ${err.message}`);
1632
+ }
1633
+ const paidPacks = [...new Set([...levelMcpPackages("pro"), ...levelMcpPackages("pro-plus")])];
1634
+ if (!paidPacks.every((name) => Object.hasOwn(have, name))) {
1635
+ const key = readSiteKey(targetDir, opts.env ?? process.env);
1636
+ let level = "free";
1637
+ if (key === undefined) {
1638
+ if (paidPacks.some((name) => Object.hasOwn(have, name))) {
1639
+ mcpLevelUnchecked = "no licence key (ailk.key or AILK_LICENSE_KEY)";
1640
+ }
1641
+ }
1642
+ else {
1643
+ try {
1644
+ ({ level } = await deriveLevel({
1645
+ licenseKey: key,
1646
+ ...(opts.moduleRegistryUrl !== undefined ? { registryUrl: opts.moduleRegistryUrl } : {}),
1647
+ ...(opts.fetchImpl !== undefined ? { fetchImpl: opts.fetchImpl } : {}),
1648
+ }));
1649
+ }
1650
+ catch (err) {
1651
+ mcpLevelUnchecked = `could not determine this site's licence level: ${err.message}`;
1652
+ }
1653
+ }
1654
+ if (level !== "free") {
1655
+ let next;
1656
+ try {
1657
+ next = withLevelMcpPackages(haveText, level, opts.versions ?? {});
1658
+ }
1659
+ catch (err) {
1660
+ throw new UpdateRefusedError(err.message);
1661
+ }
1662
+ if (next.added.length > 0)
1663
+ mcpLevelPlan = { level, packages: next.added, text: next.text };
1664
+ }
1665
+ }
1666
+ }
1573
1667
  // ── The API move (#9566), planned before the first write ──────────────
1574
1668
  // A site scaffolded before #9564 carries the server under apps/api/src, an
1575
1669
  // apps/api still named @working-theory/api and no @working-theory/api
@@ -2401,9 +2495,58 @@ export async function updateSite(opts) {
2401
2495
  }
2402
2496
  }
2403
2497
  }
2404
- const webPackageJson = webDependencies !== undefined && Object.keys(webDependencies.entries).length > 0
2405
- ? withDependencies(webDependencies.pkg, webDependencies.entries)
2406
- : undefined;
2498
+ // #11940 — each carried script is the running template's when the site's
2499
+ // value is a published template's in the run's range, oldest first (the
2500
+ // scaffold's version is the floor, so the common case reads one tarball);
2501
+ // one matching none is the site's own, reviewed and left as it is. The
2502
+ // carry waits while a `tsx` file the running value runs would be absent
2503
+ // once the run lands. The hold cascade is final here and no write has run.
2504
+ const scriptsCarried = [];
2505
+ if (webDependencies !== undefined) {
2506
+ const siteScripts = webDependencies.pkg.scripts ?? {};
2507
+ for (const name of WEB_CARRIED_SCRIPTS) {
2508
+ const running = runningWebScripts?.[name];
2509
+ const site = siteScripts[name];
2510
+ if (typeof running !== "string" || site === running)
2511
+ continue;
2512
+ let matched;
2513
+ for (const v of [...(await (versionsInRange ??= listVersionsInRange()))].reverse()) {
2514
+ await loadVersionMap(v);
2515
+ const scripts = webScriptsByVersion.get(v);
2516
+ if (scripts !== undefined && scripts[name] === site) {
2517
+ matched = v;
2518
+ break;
2519
+ }
2520
+ }
2521
+ if (matched === undefined) {
2522
+ review.push({
2523
+ path: WEB_PACKAGE_JSON,
2524
+ reason: `scripts.${name} is ${site === undefined ? "absent" : JSON.stringify(site)}, which matches no create-ailk template this run read; this template's is ${JSON.stringify(running)} — left as it is`,
2525
+ });
2526
+ continue;
2527
+ }
2528
+ const missing = [...running.matchAll(TSX_STEP)]
2529
+ .map((m) => posix.join(WEB_ROOT, m[1]))
2530
+ .find((path) => !presentAfter(path));
2531
+ if (missing !== undefined) {
2532
+ review.push({
2533
+ path: WEB_PACKAGE_JSON,
2534
+ reason: `scripts.${name} not carried: this template's ${JSON.stringify(running)} runs ${missing}, which this site will not have`,
2535
+ });
2536
+ continue;
2537
+ }
2538
+ (webDependencies.pkg.scripts ??= {})[name] = running;
2539
+ scriptsCarried.push({ script: name, version: matched });
2540
+ }
2541
+ }
2542
+ const webDependenciesAdded = Object.keys(webDependencies?.entries ?? {}).sort();
2543
+ const webPackageJson = webDependencies === undefined
2544
+ ? undefined
2545
+ : webDependenciesAdded.length > 0
2546
+ ? withDependencies(webDependencies.pkg, webDependencies.entries)
2547
+ : scriptsCarried.length > 0
2548
+ ? `${JSON.stringify(webDependencies.pkg, null, 2)}\n`
2549
+ : undefined;
2407
2550
  // #11216 — a dependency written this run is in the site after all.
2408
2551
  for (let i = skipped.length - 1; i >= 0; i--) {
2409
2552
  const path = skipped[i].path;
@@ -2538,6 +2681,12 @@ export async function updateSite(opts) {
2538
2681
  }
2539
2682
  pruneEmptyDirs(join(targetDir, ...MCP_SRC.split("/")));
2540
2683
  }
2684
+ // #11952 D4 — the level's packs, planned over the move's text so its packs are kept.
2685
+ if (mcpLevelPlan !== undefined) {
2686
+ writeFileSync(mcpPkgPath, mcpLevelPlan.text);
2687
+ if (!moved.updated.includes(MCP_PACKAGE_JSON))
2688
+ moved.updated.push(MCP_PACKAGE_JSON);
2689
+ }
2541
2690
  const registry = writePaidMcpTools(targetDir);
2542
2691
  if (registry === "added")
2543
2692
  moved.added.push(PAID_MCP_TOOLS_PATH);
@@ -2693,6 +2842,12 @@ export async function updateSite(opts) {
2693
2842
  ...(adopted.some((row) => notHeld(row.path))
2694
2843
  ? { adopted: adopted.filter((row) => notHeld(row.path)) }
2695
2844
  : {}),
2845
+ ...(scriptsCarried.length > 0 ? { scriptsCarried } : {}),
2846
+ ...(webDependenciesAdded.length > 0 ? { webDependenciesAdded } : {}),
2847
+ ...(mcpLevelPlan !== undefined
2848
+ ? { mcpPackagesAdded: { level: mcpLevelPlan.level, packages: mcpLevelPlan.packages } }
2849
+ : {}),
2850
+ ...(mcpLevelUnchecked !== undefined ? { mcpLevelUnchecked } : {}),
2696
2851
  headingsAdded: headings.written.map(sitePath),
2697
2852
  bracesEscaped,
2698
2853
  ...(regenerated.length > 0 ? { regenerated: regenerated.sort() } : {}),
@@ -2808,6 +2963,15 @@ export function formatUpdateReport(result) {
2808
2963
  lines.push(`Adopted (${result.adopted.length}):`);
2809
2964
  lines.push(...result.adopted.map(({ path, version }) => ` ${path} — matched the ${version} template`));
2810
2965
  }
2966
+ if (result.scriptsCarried !== undefined) {
2967
+ lines.push(`Build scripts carried (${result.scriptsCarried.length}):`);
2968
+ lines.push(...result.scriptsCarried.map(({ script, version }) => ` ${WEB_PACKAGE_JSON} scripts.${script} — was the ${version} template's`));
2969
+ }
2970
+ if (result.mcpPackagesAdded !== undefined) {
2971
+ const { level, packages } = result.mcpPackagesAdded;
2972
+ lines.push(`MCP packages added for the ${LEVEL_LABEL[level]} level (${packages.length}):`);
2973
+ lines.push(...packages.map((pkg) => ` ${pkg}`));
2974
+ }
2811
2975
  if (result.review.length > 0) {
2812
2976
  lines.push(`Needs review (${result.review.length}):`);
2813
2977
  lines.push(...result.review.map((row) => ` ${row.path} — ${row.reason}`));
@@ -2864,6 +3028,8 @@ export function formatUpdateReport(result) {
2864
3028
  result.regenerated === undefined &&
2865
3029
  result.routesWritten === undefined &&
2866
3030
  result.cliPin === undefined &&
3031
+ result.scriptsCarried === undefined &&
3032
+ result.mcpPackagesAdded === undefined &&
2867
3033
  result.review.length === 0 &&
2868
3034
  result.skipped.length === 0 &&
2869
3035
  result.heldBack.length === 0 &&
@@ -2871,18 +3037,24 @@ export function formatUpdateReport(result) {
2871
3037
  result.droppedRows === undefined &&
2872
3038
  result.surfacesDeclared === undefined &&
2873
3039
  result.rosterRowsDropped === undefined;
3040
+ // #11952 D4 — why the level's packs were not checked: the report's last line.
3041
+ const unchecked = result.mcpLevelUnchecked !== undefined
3042
+ ? `MCP packages for this site's level not checked — ${result.mcpLevelUnchecked}\n`
3043
+ : "";
2874
3044
  if (nothingHappened) {
2875
- return `[create-ailk] Already up to date (create-ailk ${result.version}).\n`;
3045
+ return `[create-ailk] Already up to date (create-ailk ${result.version}).\n${unchecked}`;
2876
3046
  }
2877
3047
  // #9567 / #9566 — the MCP and API moves add dependencies (the mcp README
2878
3048
  // § Move, step 7, names the install; the API half says the same), and so
2879
- // does the web app's carried-dependency step (#9889 D9).
3049
+ // does the web app's carried-dependency step (#9889 D9) — never a write
3050
+ // that only carried a build script (#11940).
2880
3051
  for (const pkgJson of [MCP_PACKAGE_JSON, API_PACKAGE_JSON, WEB_PACKAGE_JSON]) {
2881
- if (result.updated.includes(pkgJson)) {
3052
+ const gained = pkgJson === WEB_PACKAGE_JSON ? result.webDependenciesAdded !== undefined : result.updated.includes(pkgJson);
3053
+ if (gained) {
2882
3054
  lines.push(`${pkgJson} gained dependencies: run \`pnpm install\`.`);
2883
3055
  }
2884
3056
  }
2885
- return lines.join("\n") + "\n";
3057
+ return lines.join("\n") + "\n" + unchecked;
2886
3058
  }
2887
3059
  export const UPDATE_HELP = `create-ailk update — bring template fixes into a site's copied app code.
2888
3060
 
@@ -2908,7 +3080,13 @@ on one side and a value on the other; a written apps/web file that uses
2908
3080
  through t(...) a key the catalog then lacks is held with it, listed under
2909
3081
  "Held back with <catalog>". Each @working-theory/* package a written apps/web
2910
3082
  file imports that apps/web/package.json does not declare is added to its
2911
- dependencies at this release's version. An edited file whose
3083
+ dependencies at this release's version. A site missing a paid MCP package
3084
+ its licence level ships gets it in apps/mcp/package.json; the level is read
3085
+ from the site's key (ailk.key, else AILK_LICENSE_KEY) through the module
3086
+ registry, and a run with no key adds none. apps/web/package.json's
3087
+ scripts.build is set to this template's when it is a published create-ailk
3088
+ template's, listed under "Build scripts carried"; a build script matching no
3089
+ template's is left as it is and listed under "Needs review". An edited file whose
2912
3090
  bytes are another published create-ailk version's copy is a stale copy, not
2913
3091
  an edit: it is overwritten or removed as an untouched file is, listed with
2914
3092
  the version it matched. An edit that touches only comment lines is listed as
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ailk",
3
- "version": "0.43.0",
3
+ "version": "0.45.0",
4
4
  "description": "Scaffolder for AI Launch Kit — `pnpm create ailk@latest <dir>` produces a starter repo consuming @working-theory/* from npm (per ADR 0007).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -28,8 +28,8 @@
28
28
  "tar": "^7.5.21"
29
29
  },
30
30
  "devDependencies": {
31
- "@working-theory/schema": "0.71.0",
32
- "@working-theory/templates": "0.71.0"
31
+ "@working-theory/schema": "0.73.0",
32
+ "@working-theory/templates": "0.73.0"
33
33
  },
34
34
  "repository": {
35
35
  "type": "git",