rescope-vendor.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /**
  2. * Rescope the vendored Cordis packages into the `@deepseek-ai` scope, and undo
  3. * that rescope with `--reverse`. Every harness package declares `cordis` as a
  4. * peer dependency, so publication carries this framework layer too; publishing
  5. * it under the upstream names would squat them on the registry
  6. * ([rationale](../.agents/notes/implemented/process/2026-08-10-vendor-package-rescope.md),
  7. * [name mapping](../docs/rescope.md)).
  8. *
  9. * The generic pass rewrites ONLY delimited, complete package-name tokens:
  10. * `'old'` / `"old"` / `` `old` `` / `'old/subpath'`, plus a YAML `name: old`
  11. * scalar. A match needs a quote (or `name: `) immediately left and the matching
  12. * quote — optionally after a `/subpath` — immediately right, which excludes
  13. * `cordis.yml`, the Loader's `cordis:` builtin prefix, `cordis-config-entry`,
  14. * `@deepseek-ai/dsh-tool-cordis`, and `cordiverse/cordis`, and makes the
  15. * rewrite idempotent because the scoped name's `cordis` is preceded by `/`.
  16. * Markdown follows the rename inside every fence, and in `docs/` prose too:
  17. * a tutorial that teaches an unresolvable name is wrong, while prose elsewhere
  18. * records what was true when it was written.
  19. *
  20. * Sites the token rule cannot express (dot-notation access, unquoted object
  21. * keys, regex literals, the vendored-manifest table) are listed in
  22. * {@link EXACT_EDITS} with an exact hit count, so an upstream change to one of
  23. * them fails loudly instead of being silently skipped.
  24. *
  25. * Usage: `pnpm run rescope-vendor [--apply|--check] [--reverse]`. Without a
  26. * mode it reports what would change. `--check` asserts the post-state: no
  27. * residue, every exact edit landed, every postcondition holds, and a second
  28. * `--apply` would be a no-op.
  29. */
  30. import { execFileSync } from 'node:child_process'
  31. import { existsSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'
  32. import { resolve } from 'node:path'
  33. import { fileURLToPath } from 'node:url'
  34. const root = resolve(import.meta.dirname, '..')
  35. /** One vendored package's directory, upstream npm name, and rescoped name. */
  36. interface Rename {
  37. readonly directory: string
  38. readonly upstream: string
  39. readonly scoped: string
  40. }
  41. /** The mapping this codemod applies; `vendor/README.md` carries the same table. */
  42. const RENAMES: readonly Rename[] = [
  43. { directory: 'cordis', upstream: 'cordis', scoped: '@deepseek-ai/cordis' },
  44. { directory: 'cosmokit', upstream: 'cosmokit', scoped: '@deepseek-ai/cosmokit' },
  45. { directory: 'schemastery', upstream: 'schemastery', scoped: '@deepseek-ai/schemastery' },
  46. { directory: 'loader', upstream: '@cordisjs/plugin-loader', scoped: '@deepseek-ai/cordis-plugin-loader' },
  47. { directory: 'include', upstream: '@cordisjs/plugin-include', scoped: '@deepseek-ai/cordis-plugin-include' },
  48. { directory: 'group', upstream: '@cordisjs/plugin-group', scoped: '@deepseek-ai/cordis-plugin-group' },
  49. { directory: 'timer', upstream: '@cordisjs/plugin-timer', scoped: '@deepseek-ai/cordis-plugin-timer' },
  50. { directory: 'hmr', upstream: '@cordisjs/plugin-hmr', scoped: '@deepseek-ai/cordis-plugin-hmr' },
  51. { directory: 'logger-console', upstream: '@cordisjs/plugin-logger-console', scoped: '@deepseek-ai/cordis-plugin-logger-console' },
  52. ]
  53. const EXTENSIONS = ['.ts', '.tsx', '.js', '.mjs', '.cjs', '.tpl', '.json', '.yml', '.yaml', '.md'] as const
  54. /** An exact-string edit the token rule cannot express, with its required hit count. */
  55. interface ExactEdit {
  56. readonly id: string
  57. readonly file: string
  58. readonly find: string
  59. readonly replace: string
  60. readonly expect: number
  61. }
  62. /**
  63. * A file where an upstream name also appears as a vendor DIRECTORY name or an
  64. * upstream runtime identifier: the generic pass is disabled for the listed
  65. * names and {@link EXACT_EDITS} renames the real package-name occurrences.
  66. */
  67. interface GenericSkip {
  68. readonly file: string
  69. readonly upstream: readonly string[]
  70. }
  71. const GENERIC_SKIPS: readonly GenericSkip[] = [
  72. // `vendorPackages` lists vendor/ directory names, joined with 'vendor' below it.
  73. { file: 'packages/examples/acp-demo/tests/built-bin.e2e.ts', upstream: ['cordis', 'cosmokit', 'schemastery'] },
  74. // `Symbol.for('schemastery')` and the `vendor:` metadata field are upstream identifiers.
  75. { file: 'vendor/schemastery/src/index.ts', upstream: ['schemastery'] },
  76. // Asserts the vendored-manifest table, which gains an upstream-name column.
  77. { file: 'scripts/gen-third-party-notices.spec.ts', upstream: RENAMES.map(rename => rename.upstream) },
  78. // `cordis` is also an agent-preset id — the directory name under
  79. // apps/cli/config/agent-presets/ — so in these files the bare name is
  80. // product data, not a package reference. Renaming it changed which preset
  81. // the creator flow stages and which id the roster reports.
  82. { file: 'packages/client/ui-agent-preset/src/client/AgentPresetSection.tsx', upstream: ['cordis'] },
  83. { file: 'packages/client/ui-agent-preset/src/client/index.ts', upstream: ['cordis'] },
  84. { file: 'packages/client/ui-agent-preset/tests/apply.client.spec.ts', upstream: ['cordis'] },
  85. { file: 'packages/client/ui-agent-preset/tests/locales.client.spec.ts', upstream: ['cordis'] },
  86. { file: 'packages/client/ui-agent-preset/tests/section.client.spec.tsx', upstream: ['cordis'] },
  87. { file: 'apps/cli/tests/web-agent-presets.e2e.ts', upstream: ['cordis'] },
  88. { file: 'apps/web/tests/agent-preset-authoring.e2e.ts', upstream: ['cordis'] },
  89. { file: 'packages/preset/agent-presets/tests/session.spec.ts', upstream: ['cordis'] },
  90. // The preset's own composition: its header comment and its system prompt name
  91. // the preset a model mounts, so the scoped name would send the model after an
  92. // id no roster reports.
  93. { file: 'apps/cli/config/agent-presets/cordis/agent.cordis.yml', upstream: ['cordis'] },
  94. // The preset-roster loop names the `cordis` preset id, not a package.
  95. { file: 'apps/cli/tests/windows-shell.spec.ts', upstream: ['cordis'] },
  96. // GROUP_ORDER holds `packages/<group>/` directory names, not package names.
  97. { file: 'scripts/gen-module-graph.ts', upstream: ['cordis'] },
  98. { file: 'scripts/gen-doc-graphs.ts', upstream: ['cordis'] },
  99. ]
  100. /** A string that must appear exactly `count` times once the rescope has run. */
  101. interface PostCondition {
  102. readonly file: string
  103. readonly text: string
  104. readonly count: number
  105. }
  106. const POSTCONDITIONS: readonly PostCondition[] = [
  107. { file: 'vendor/cordis/package.json', text: '"name": "@deepseek-ai/cordis"', count: 1 },
  108. { file: 'vendor/hmr/package.json', text: '"name": "@deepseek-ai/cordis-plugin-hmr"', count: 1 },
  109. { file: 'scripts/cordis-walk.ts', text: '@deepseek-ai\\/cordis', count: 1 },
  110. { file: 'scripts/cordis-walk.ts', text: '!== \'@deepseek-ai/cordis\'', count: 1 },
  111. { file: 'scripts/gen-scoped-events.ts', text: '=== \'@deepseek-ai/cordis\'', count: 1 },
  112. { file: 'packages/typert/generator/src/analyzer.ts', text: '!== \'@deepseek-ai/cordis\'', count: 2 },
  113. { file: 'scripts/check-workspace-constraints.ts', text: '?.[\'@deepseek-ai/cordis\']', count: 2 },
  114. { file: 'packages/boot/app-boot/tsdown.config.ts', text: '[\'@deepseek-ai/cordis-plugin-include\']', count: 1 },
  115. { file: 'tsconfig.base.json', text: '"@deepseek-ai/cordis-plugin-loader": ["./vendor/loader/src"]', count: 1 },
  116. // The vendored README owns this required entry; reject its deletion or duplication.
  117. { file: 'vendor/README.md', text: '17. **`@deepseek-ai` rescope**', count: 1 },
  118. { file: 'knip.json', text: '@cordisjs', count: 0 },
  119. { file: 'pnpm-workspace.yaml', text: 'cordis@4.0.0-rc.7', count: 0 },
  120. // The preset ids in this table are product data, not package names.
  121. { file: 'packages/client/ui-agent-preset/tests/locales.client.spec.ts', text: '[\'cordis\', \'presetCordisName\'', count: 1 },
  122. // The preset id the shipped composition documents to its own model.
  123. { file: 'apps/cli/config/agent-presets/cordis/agent.cordis.yml', text: 'The `cordis` agent preset', count: 1 },
  124. { file: 'apps/cli/config/agent-presets/cordis/agent.cordis.yml', text: 'corrupting the `cordis` preset', count: 1 },
  125. { file: 'packages/examples/acp-demo/tests/built-bin.e2e.ts', text: '\'cordis\', \'loader\', \'include\', \'timer\', \'hmr\', \'logger-console\',', count: 1 },
  126. ]
  127. /**
  128. * Every exact edit, in application order. Each `find` is written against the
  129. * PRE-rename text because these run before the generic pass, so no `find` may
  130. * quote a neighbouring line the generic pass would rewrite.
  131. */
  132. const EXACT_EDITS: readonly ExactEdit[] = [
  133. {
  134. id: 'cordis-walk-merge-head',
  135. file: 'scripts/cordis-walk.ts',
  136. find: 'const MERGE_HEAD = /declare module [\'"](?:cordis|\\.\\/context\\.ts)[\'"]/',
  137. replace: 'const MERGE_HEAD = /declare module [\'"](?:@deepseek-ai\\/cordis|\\.\\/context\\.ts)[\'"]/',
  138. expect: 1,
  139. },
  140. {
  141. id: 'constraints-manifest-lookup',
  142. file: 'scripts/check-workspace-constraints.ts',
  143. find: ` const peer = manifest.peerDependencies?.cordis
  144. const dev = manifest.devDependencies?.cordis
  145. if (!peer) errors.push(\`\${label}: cordis must be a peerDependency\`)
  146. if (!dev) errors.push(\`\${label}: cordis must also be a devDependency\`)
  147. if (peer && dev && peer !== dev) {
  148. errors.push(\`\${label}: cordis peer (\${peer}) and dev (\${dev}) ranges must match\`)`,
  149. replace: ` const peer = manifest.peerDependencies?.['@deepseek-ai/cordis']
  150. const dev = manifest.devDependencies?.['@deepseek-ai/cordis']
  151. if (!peer) errors.push(\`\${label}: @deepseek-ai/cordis must be a peerDependency\`)
  152. if (!dev) errors.push(\`\${label}: @deepseek-ai/cordis must also be a devDependency\`)
  153. if (peer && dev && peer !== dev) {
  154. errors.push(\`\${label}: @deepseek-ai/cordis peer (\${peer}) and dev (\${dev}) ranges must match\`)`,
  155. expect: 1,
  156. },
  157. {
  158. // The rescoped name is already covered by the `@deepseek-ai/.+` pattern beside it.
  159. id: 'knip-logger-console',
  160. file: 'knip.json',
  161. find: ` "ignoreDependencies": [
  162. "@cordisjs/plugin-logger-console",
  163. "@deepseek-ai/.+"
  164. ]
  165. },
  166. "packages/util/home": {`,
  167. replace: ` "ignoreDependencies": [
  168. "@deepseek-ai/.+"
  169. ]
  170. },
  171. "packages/util/home": {`,
  172. expect: 1,
  173. },
  174. {
  175. id: 'knip-bundle-base',
  176. file: 'knip.json',
  177. find: ` "packages/bundle/base": {
  178. "ignoreDependencies": [
  179. "@deepseek-ai/.+",
  180. "@cordisjs/.+"
  181. ]`,
  182. replace: ` "packages/bundle/base": {
  183. "ignoreDependencies": [
  184. "@deepseek-ai/.+"
  185. ]`,
  186. expect: 1,
  187. },
  188. {
  189. // Rescoped packages are never fetched from a registry, so the exclusion is dead config.
  190. id: 'pnpm-release-age',
  191. file: 'pnpm-workspace.yaml',
  192. find: `minimumReleaseAgeExclude:
  193. # Cordis release candidates are source-vendored and pinned in vendor/README.md
  194. # during the same-day sync that updates package manifests and the lockfile.
  195. - '@cordisjs/plugin-loader@1.0.0-rc.5'
  196. - cordis@4.0.0-rc.7
  197. `,
  198. replace: 'minimumReleaseAgeExclude:\n',
  199. expect: 1,
  200. },
  201. {
  202. id: 'publication-set-scope-assertion',
  203. file: 'scripts/publish-npm-baseline.ts',
  204. find: ' if (!isVendored && !name.startsWith(\'@deepseek-ai/\')) {',
  205. replace: ` // Vendored packages are rescoped too (vendor/README.md), so publication
  206. // never carries an upstream name that would squat it on the registry.
  207. if (!name.startsWith('@deepseek-ai/')) {`,
  208. expect: 1,
  209. },
  210. {
  211. id: 'vendor-readme-preamble',
  212. file: 'vendor/README.md',
  213. find: 'All vendored packages keep their **original npm names** and are marked `private: true` — they are never published from this repo. `pnpm-workspace.yaml#linkWorkspacePackages` makes matching upstream semver ranges resolve these pinned workspaces, including imports from built `lib/`; disabling it substitutes npm copies behind the same names.',
  214. replace: 'All vendored packages are **renamed into the `@deepseek-ai` scope** (`cordis` → `@deepseek-ai/cordis`, `@cordisjs/plugin-<x>` → `@deepseek-ai/cordis-plugin-<x>`): every harness package declares `cordis` as a peer dependency, so publishing the harness publishes this framework layer too, and a publication under the upstream names would squat them on the registry. Directory names and upstream version numbers are deliberately unchanged, so the manifest below still reads as an upstream snapshot. `pnpm-workspace.yaml#linkWorkspacePackages` makes those preserved semver ranges resolve these pinned workspaces, including imports from built `lib/`.',
  215. expect: 1,
  216. },
  217. {
  218. id: 'vendor-readme-schemastery-note',
  219. file: 'vendor/README.md',
  220. find: 'whose lazy `require(\'cosmokit\')` can race',
  221. replace: 'whose lazy `require(\'@deepseek-ai/cosmokit\')` can race',
  222. expect: 1,
  223. },
  224. {
  225. id: 'vendor-readme-table-head',
  226. file: 'vendor/README.md',
  227. find: '| Directory | npm name | Version | Upstream repo | Commit |\n|---|---|---|---|---|',
  228. replace: '| Directory | npm name | Upstream name | Version | Upstream repo | Commit |\n|---|---|---|---|---|---|',
  229. expect: 1,
  230. },
  231. {
  232. // A plain fence listing the bundle's mounted tree: a bare token, no quotes.
  233. id: 'agent-spine-demo-mounted-tree',
  234. file: 'packages/examples/agent-spine-demo/README.md',
  235. find: '@cordisjs/plugin-timer timer service',
  236. replace: '@deepseek-ai/cordis-plugin-timer timer service',
  237. expect: 1,
  238. },
  239. {
  240. id: 'agent-spine-demo-mounted-tree-zh',
  241. file: 'packages/examples/agent-spine-demo/README.zh.md',
  242. find: '@cordisjs/plugin-timer timer service',
  243. replace: '@deepseek-ai/cordis-plugin-timer timer service',
  244. expect: 1,
  245. },
  246. {
  247. // The root contract claimed vendored packages keep their upstream names.
  248. id: 'root-agents-vendored-name-contract',
  249. file: 'AGENTS.md',
  250. find: 'vendored packages keep upstream names and are `private: true`. `cordis` is a peerDependency (+ dev) of every harness package.',
  251. replace: 'vendored packages are rescoped ([mapping](docs/rescope.md)) and `private: true`. `@deepseek-ai/cordis` is a peerDependency (+ dev) of every harness package.',
  252. expect: 1,
  253. },
  254. {
  255. // The client purity gate reads `@deepseek-ai/` as "another plugin package".
  256. // The rescope moves the vendored framework and its libraries into that
  257. // namespace, where the gate would reject the library imports client
  258. // bundles have always inlined, so it needs their names.
  259. id: 'client-purity-vendored-libraries',
  260. file: 'packages/client/tsdown.client.ts',
  261. find: '/** Generated descriptor/codec contribution with no shared runtime identity. */',
  262. replace: `/**
  263. * Vendored framework libraries: rescoped into @deepseek-ai, so the gate below
  264. * would read them as plugin packages. They carry no cross-plugin runtime
  265. * identity to share — the framework itself is a platform module (external),
  266. * while these are ordinary libraries a browser bundle inlines.
  267. */
  268. const VENDORED_LIBRARY = /^@deepseek-ai\\/(cosmokit|schemastery)(\\/|$)/
  269. /** Generated descriptor/codec contribution with no shared runtime identity. */`,
  270. expect: 1,
  271. },
  272. {
  273. id: 'client-purity-vendored-libraries-predicate',
  274. file: 'packages/client/tsdown.client.ts',
  275. find: ' if (INLINE_SAFE.test(source) || GENERATED_REMOTE.test(source)) return null // wire contribution: inline is the point',
  276. replace: ` if (VENDORED_LIBRARY.test(source)) return null // vendored library: inline, no shared identity
  277. if (INLINE_SAFE.test(source) || GENERATED_REMOTE.test(source)) return null // wire contribution: inline is the point`,
  278. expect: 1,
  279. },
  280. {
  281. // The step-1 file tree told the reader to keep the upstream name, one
  282. // paragraph above the invariant that says to rescope it.
  283. id: 'vendoring-cookbook-tree-comment',
  284. file: 'docs/cookbook/adding-a-vendored-package.md',
  285. find: ' package.json # from upstream; set "private": true, keep name/exports/type',
  286. replace: ' package.json # from upstream; set "private": true, rescope the name, keep exports/type',
  287. expect: 1,
  288. },
  289. {
  290. id: 'vendoring-cookbook-tree-comment-zh',
  291. file: 'docs/cookbook/adding-a-vendored-package.zh.md',
  292. find: ' package.json # from upstream; set "private": true, keep name/exports/type',
  293. replace: ' package.json # from upstream; set "private": true, rescope the name, keep exports/type',
  294. expect: 1,
  295. },
  296. {
  297. // The checklist told the next vendoring to keep upstream's name.
  298. id: 'vendoring-cookbook-name-invariant',
  299. file: 'docs/cookbook/adding-a-vendored-package.md',
  300. find: "keep upstream's `name`/`version`/`exports`/`type`",
  301. replace: "rescope the `name` ([mapping](../rescope.md)) while keeping upstream's `version`/`exports`/`type`",
  302. expect: 1,
  303. },
  304. {
  305. id: 'vendoring-cookbook-name-invariant-zh',
  306. file: 'docs/cookbook/adding-a-vendored-package.zh.md',
  307. find: '保留上游的 `name`/`version`/`exports`/`type`',
  308. replace: '改写 `name` 的 scope([映射](../rescope.md)),保留上游的 `version`/`exports`/`type`',
  309. expect: 1,
  310. },
  311. {
  312. // The real package references in files whose other `cordis` strings are preset ids.
  313. id: 'agent-preset-spec-framework-import',
  314. file: 'packages/client/ui-agent-preset/tests/apply.client.spec.ts',
  315. find: "import { Context } from 'cordis'",
  316. replace: "import { Context } from '@deepseek-ai/cordis'",
  317. expect: 1,
  318. },
  319. {
  320. id: 'web-agent-presets-e2e-framework-import',
  321. file: 'apps/cli/tests/web-agent-presets.e2e.ts',
  322. find: "import { Context } from 'cordis'",
  323. replace: "import { Context } from '@deepseek-ai/cordis'",
  324. expect: 1,
  325. },
  326. {
  327. id: 'notices-vendored-row-type',
  328. file: 'scripts/gen-third-party-notices.ts',
  329. find: `export interface VendoredRow {
  330. npmName: string
  331. upstream: string
  332. }`,
  333. replace: `export interface VendoredRow {
  334. npmName: string
  335. /** The name this package carries upstream; MIT attribution names the fork's origin, not our scope. */
  336. upstreamName: string
  337. upstream: string
  338. }`,
  339. expect: 1,
  340. },
  341. {
  342. id: 'notices-vendored-row-parse',
  343. file: 'scripts/gen-third-party-notices.ts',
  344. find: ` const match = /^\\| \\x60\\S+\\/\\x60 \\| \\x60([^\\x60]+)\\x60 \\| \\S+ \\| (https:\\/\\/\\S+?)(?: \\([^)]*\\))? \\| \\x60[0-9a-f]+\\x60 \\|$/.exec(line)
  345. if (match === null) continue
  346. const [, npmName, upstream] = match
  347. if (npmName === undefined || upstream === undefined) continue
  348. rows.push({ npmName, upstream })`,
  349. replace: ` const match = new RegExp(String.raw\`^\\| \\x60\\S+\\/\\x60 \\| \\x60([^\\x60]+)\\x60 \\| \\x60([^\\x60]+)\\x60 \\| \\S+ \\| \`
  350. + String.raw\`(https:\\/\\/\\S+?)(?: \\([^)]*\\))? \\| \\x60[0-9a-f]+\\x60 \\|$\`).exec(line)
  351. if (match === null) continue
  352. const [, npmName, upstreamName, upstream] = match
  353. if (npmName === undefined || upstreamName === undefined || upstream === undefined) continue
  354. rows.push({ npmName, upstreamName, upstream })`,
  355. expect: 1,
  356. },
  357. {
  358. id: 'notices-vendored-section',
  359. file: 'scripts/gen-third-party-notices.ts',
  360. find: 'The Cordis framework and its foundation libraries are source-vendored into this repository rather than consumed from npm. All are MIT-licensed',
  361. replace: 'The Cordis framework and its foundation libraries are source-vendored into this repository rather than consumed from npm, and republished under the \\`@deepseek-ai\\` scope. All are MIT-licensed',
  362. expect: 1,
  363. },
  364. {
  365. id: 'notices-vendored-table',
  366. file: 'scripts/gen-third-party-notices.ts',
  367. find: `| Package | Upstream | License |
  368. | --- | --- | --- |
  369. \${vendored.map(row => \`| \\\`\${row.npmName}\\\` | [\${row.upstream.replace('https://', '')}](\${row.upstream}) | MIT |\`).join('\\n')}`,
  370. replace: `| Package | Upstream name | Upstream | License |
  371. | --- | --- | --- | --- |
  372. \${vendored.map(row => \`| \\\`\${row.npmName}\\\` | \\\`\${row.upstreamName}\\\` | [\${row.upstream.replace('https://', '')}](\${row.upstream}) | MIT |\`).join('\\n')}`,
  373. expect: 1,
  374. },
  375. {
  376. id: 'notices-spec-row-fixture',
  377. file: 'scripts/gen-third-party-notices.spec.ts',
  378. find: ' expect(rows).toContainEqual({ npmName: \'cordis\', upstream: \'https://github.com/cordiverse/cordis\' })',
  379. replace: ` expect(rows).toContainEqual({
  380. npmName: '@deepseek-ai/cordis',
  381. upstreamName: 'cordis',
  382. upstream: 'https://github.com/cordiverse/cordis',
  383. })`,
  384. expect: 1,
  385. },
  386. {
  387. id: 'notices-spec-shape-fixture',
  388. file: 'scripts/gen-third-party-notices.spec.ts',
  389. find: 'parseVendoredRows(\'| `cordis/` | cordis | 4.0.0 | https://example.com | `abc123` |\\n\')',
  390. replace: 'parseVendoredRows(\'| `cordis/` | `@deepseek-ai/cordis` | cordis | 4.0.0 | https://example.com | `abc123` |\\n\')',
  391. expect: 1,
  392. },
  393. {
  394. // The framework peer is no longer a registry name, so the rehearsal must install this
  395. // repository's vendored copies; cosmokit comes along as cordis's own dependency.
  396. id: 'packed-install-vendored-peer',
  397. file: 'packages/sandbox/sandbox-local/tests/packed-install.e2e.ts',
  398. find: ` 'packages/runtime-diagnostics/invariants',
  399. ]`,
  400. replace: ` 'packages/runtime-diagnostics/invariants',
  401. // The framework and the vendored packages the closure declares outright:
  402. // rescoped into @deepseek-ai, so the consumer installs this repository's
  403. // copies. Schemastery is a hard dependency of three members above, not a
  404. // peer, so npm resolves it while installing them.
  405. 'vendor/cordis',
  406. 'vendor/cosmokit',
  407. 'vendor/schemastery',
  408. ]`,
  409. expect: 1,
  410. },
  411. {
  412. id: 'packed-install-registry-spec',
  413. file: 'packages/sandbox/sandbox-local/tests/packed-install.e2e.ts',
  414. find: ` // Peer ranges resolve to the tarballs; Cordis is pinned to their peer range. Do not omit optional
  415. // dependencies because the launcher selects its OS/CPU package through one.
  416. writeFileSync(join(consumerDir, 'package.json'), JSON.stringify({ name: 'dsh-packed-consumer', private: true, type: 'module' }))
  417. const install = spawnSync('npm', ['install', '--no-audit', '--no-fund', ...tarballs, 'cordis@4.0.0-rc.7'], {`,
  418. replace: ` // Peer ranges resolve to the tarballs, the framework peer included. Do not omit optional
  419. // dependencies because the launcher selects its OS/CPU package through one.
  420. writeFileSync(join(consumerDir, 'package.json'), JSON.stringify({ name: 'dsh-packed-consumer', private: true, type: 'module' }))
  421. const install = spawnSync('npm', ['install', '--no-audit', '--no-fund', ...tarballs], {`,
  422. expect: 1,
  423. },
  424. {
  425. id: 'packed-install-module-doc',
  426. file: 'packages/sandbox/sandbox-local/tests/packed-install.e2e.ts',
  427. find: ` * Keyless publish-path rehearsal. It packs the provider, its workspace peers, and the current
  428. * repository's Landlock entry/platform packages, then installs those exact tarballs in an external
  429. * plain-Node consumer. The host launcher comes from the exact local tarballs, so no registry copy,
  430. * tsx, path mapping, or workspace resolution can hide missing files, dependency errors, or lost
  431. * executable modes.`,
  432. replace: ` * Keyless publish-path rehearsal. It packs the provider, its workspace peers, the vendored framework
  433. * peer, and the current repository's Landlock entry/platform packages, then installs those exact
  434. * tarballs in an external plain-Node consumer. The host launcher comes from the exact local tarballs,
  435. * so no registry copy, tsx, path mapping, or workspace resolution can hide missing files, dependency
  436. * errors, or lost executable modes.`,
  437. expect: 1,
  438. },
  439. // The manifest table's name column plus the new upstream-name column, one edit per row.
  440. ...RENAMES.map(rename => ({
  441. id: `vendor-readme-row-${rename.directory}`,
  442. file: 'vendor/README.md',
  443. find: `| \`${rename.directory}/\` | \`${rename.upstream}\` | `,
  444. replace: `| \`${rename.directory}/\` | \`${rename.scoped}\` | \`${rename.upstream}\` | `,
  445. expect: 1,
  446. })),
  447. ]
  448. /** Files the rescope must never rewrite. */
  449. function excluded(file: string): boolean {
  450. if (file === 'scripts/rescope-vendor.ts') return true // the mapping itself
  451. if (file.startsWith('.agents/notes/')) return true // notes record what was true when written
  452. // Recorded model payloads quote documentation verbatim, so they must mirror the
  453. // sources on disk — including the notes this rescope leaves alone.
  454. if (file.startsWith('scripts/snapshots/')) return true
  455. // The mapping documents state both names on purpose.
  456. if (file === 'docs/rescope.md' || file === 'docs/rescope.zh.md') return true
  457. if (file.endsWith('.i18n.yaml')) return true // blob-hash records, re-recorded by the pairing gate
  458. if (file === 'pnpm-lock.yaml') return true // regenerated by pnpm install
  459. if (/^vendor\/[^/]+\/(README\.md|LICENSE)$/.test(file)) return true // upstream files kept verbatim
  460. return !EXTENSIONS.some(extension => file.endsWith(extension))
  461. }
  462. function escapeRegExp(value: string): string {
  463. return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  464. }
  465. /** One name's rewrite, precompiled for both delimited forms. */
  466. interface Pattern {
  467. readonly upstream: string
  468. readonly from: string
  469. readonly to: string
  470. readonly token: RegExp
  471. readonly yamlName: RegExp
  472. }
  473. function patterns(reverse: boolean): Pattern[] {
  474. return RENAMES
  475. .map(rename => ({
  476. upstream: rename.upstream,
  477. from: reverse ? rename.scoped : rename.upstream,
  478. to: reverse ? rename.upstream : rename.scoped,
  479. }))
  480. .sort((left, right) => right.from.length - left.from.length)
  481. .map(rename => ({
  482. ...rename,
  483. token: new RegExp(`(['"\`])${escapeRegExp(rename.from)}((?:/[^'"\`\\s]*)?)\\1`, 'g'),
  484. yamlName: new RegExp(`^(\\s*(?:-\\s*)?name:[ \\t]+)${escapeRegExp(rename.from)}([ \\t]*(?:#.*)?)$`, 'gm'),
  485. }))
  486. }
  487. function skipped(file: string, pattern: Pattern): boolean {
  488. return GENERIC_SKIPS.some(skip => skip.file === file && skip.upstream.includes(pattern.upstream))
  489. }
  490. function rewriteLine(line: string, file: string, all: readonly Pattern[]): string {
  491. let out = line
  492. for (const pattern of all) {
  493. if (skipped(file, pattern)) continue
  494. out = out.replace(pattern.token, (_match, quote: string, subpath: string) => `${quote}${pattern.to}${subpath}${quote}`)
  495. out = out.replace(pattern.yamlName, (_match, prefix: string, suffix: string) => `${prefix}${pattern.to}${suffix}`)
  496. }
  497. return out
  498. }
  499. /**
  500. * Rewrite a file's eligible lines.
  501. *
  502. * Markdown splits in two. Every fence is code a reader copies or a
  503. * configuration they mount, so every fence follows the rename regardless of its
  504. * info string. Prose follows it only under `docs/`, where a sentence quoting
  505. * `` `cordis` `` teaches a name this repository no longer resolves; elsewhere
  506. * prose is a record of what was true when it was written, and the same spelling
  507. * can mean something else entirely — the Python SDK's `cordis` option, or the
  508. * unvendored `@cordisjs/plugin-http`.
  509. */
  510. function rewrite(text: string, file: string, all: readonly Pattern[]): { text: string; lines: number } {
  511. const markdown = file.endsWith('.md')
  512. const prose = markdown && file.startsWith('docs/')
  513. let insideFence = false
  514. let lines = 0
  515. const out = text.split('\n').map((line) => {
  516. if (markdown) {
  517. if (/^\s*```/.test(line)) {
  518. insideFence = !insideFence
  519. return line
  520. }
  521. if (!insideFence && !prose) return line
  522. }
  523. const next = rewriteLine(line, file, all)
  524. if (next !== line) lines += 1
  525. return next
  526. })
  527. return { text: out.join('\n'), lines }
  528. }
  529. function classify(file: string): string {
  530. if (/^vendor\/[^/]+\/package\.json$/.test(file)) return 'vendor manifest name'
  531. if (file.endsWith('package.json')) return 'package.json dependencies'
  532. if (/\.(ts|tsx|js|mjs|cjs|tpl)$/.test(file)) return 'code specifiers'
  533. if (/\.(yml|yaml)$/.test(file)) return 'YAML plugin names'
  534. if (file.endsWith('.json')) return 'JSON configuration'
  535. return 'Markdown fences and docs prose'
  536. }
  537. /**
  538. * One exact edit's state in the text it targets. `pending` means the source
  539. * form is present and the target form absent; `applied` means the reverse;
  540. * anything else — a partial application, a moved site, or a DUPLICATED
  541. * insertion — is `invalid`, so it fails the run instead of being applied again.
  542. */
  543. export type ExactEditState = 'pending' | 'applied' | 'invalid'
  544. /**
  545. * Classify one exact edit against its target text.
  546. *
  547. * An insertion keeps its anchor (`replace` contains `find`) and a deletion
  548. * keeps its remainder (`find` contains `replace`), so neither can be judged by
  549. * the source form alone: the surviving side counts the target form instead.
  550. * @param text - the complete current text of the edited file.
  551. * @param find - the source form, already oriented for the running direction.
  552. * @param replace - the target form, already oriented for the running direction.
  553. * @param expect - how many occurrences one complete application produces.
  554. * @returns Whether the edit is pending, already applied, or invalid.
  555. */
  556. export function exactEditState(text: string, find: string, replace: string, expect: number): ExactEditState {
  557. const hits = text.split(find).length - 1
  558. const landed = text.split(replace).length - 1
  559. if (replace.includes(find)) {
  560. if (landed === expect) return 'applied'
  561. return landed === 0 && hits === expect ? 'pending' : 'invalid'
  562. }
  563. if (find.includes(replace)) {
  564. if (hits === 0) return landed === expect ? 'applied' : 'invalid'
  565. return hits === expect ? 'pending' : 'invalid'
  566. }
  567. if (hits === 0 && landed === expect) return 'applied'
  568. return hits === expect && landed === 0 ? 'pending' : 'invalid'
  569. }
  570. function main(): void {
  571. const args = process.argv.slice(2)
  572. const mode = args.includes('--apply') ? 'apply' : args.includes('--check') ? 'check' : 'dry'
  573. const reverse = args.includes('--reverse')
  574. const all = patterns(reverse)
  575. const files = execFileSync('git', ['ls-files', '-z'], { cwd: root, encoding: 'utf8' })
  576. .split('\0')
  577. .filter(file => file !== '' && !excluded(file))
  578. const counts = new Map<string, { files: number; lines: number }>()
  579. const failures: string[] = []
  580. const outstanding: string[] = []
  581. // Classify every exact edit before writing anything: a single invalid site
  582. // means the mapping and the tree disagree, and a half-applied tree is worse
  583. // than an untouched one.
  584. const planned: { edit: ExactEdit; path: string; find: string; replace: string }[] = []
  585. for (const edit of EXACT_EDITS) {
  586. const path = resolve(root, edit.file)
  587. const before = readFileSync(path, 'utf8')
  588. const find = reverse ? edit.replace : edit.find
  589. const replace = reverse ? edit.find : edit.replace
  590. const state = exactEditState(before, find, replace, edit.expect)
  591. if (state === 'invalid') {
  592. failures.push(`exact edit ${edit.id}: ${edit.file} is neither pending nor cleanly applied (duplicated, partial, or moved)`)
  593. continue
  594. }
  595. if (mode === 'check') {
  596. if (state !== 'applied') failures.push(`exact edit ${edit.id} did not land in ${edit.file}`)
  597. continue
  598. }
  599. if (state === 'pending') planned.push({ edit, path, find, replace })
  600. }
  601. if (failures.length > 0) {
  602. for (const failure of failures) console.error(`rescope-vendor: ${failure}`)
  603. console.error(`rescope-vendor: ${String(failures.length)} problem(s); nothing was written.`)
  604. process.exitCode = 1
  605. return
  606. }
  607. if (mode === 'apply') {
  608. // Re-read per edit: two edits can target one file, and a stale snapshot
  609. // would let the second write discard the first.
  610. for (const { path, find, replace } of planned) {
  611. writeFileSync(path, readFileSync(path, 'utf8').split(find).join(replace))
  612. }
  613. }
  614. for (const file of files) {
  615. const path = resolve(root, file)
  616. const before = readFileSync(path, 'utf8')
  617. const { text: after, lines } = rewrite(before, file, all)
  618. if (after === before) continue
  619. outstanding.push(file)
  620. const kind = classify(file)
  621. const current = counts.get(kind) ?? { files: 0, lines: 0 }
  622. counts.set(kind, { files: current.files + 1, lines: current.lines + lines })
  623. if (mode === 'apply') writeFileSync(path, after)
  624. }
  625. console.log(`rescope-vendor: ${mode}${reverse ? ' --reverse' : ''} over ${String(files.length)} tracked files`)
  626. for (const kind of [...counts.keys()].sort()) {
  627. const { files: count, lines } = counts.get(kind) ?? { files: 0, lines: 0 }
  628. console.log(` ${kind.padEnd(24)} ${String(count).padStart(4)} file(s), ${String(lines)} line(s)`)
  629. }
  630. if (mode !== 'dry') {
  631. for (const check of POSTCONDITIONS) {
  632. if (reverse) break
  633. const path = resolve(root, check.file)
  634. const hits = existsSync(path) ? readFileSync(path, 'utf8').split(check.text).length - 1 : -1
  635. if (hits !== check.count) {
  636. failures.push(`postcondition: ${check.file} has ${String(hits)} occurrence(s) of ${JSON.stringify(check.text)}, expected ${String(check.count)}`)
  637. }
  638. }
  639. // The generic pass above already told us which files would still change,
  640. // which in check mode is exactly the residue-and-idempotency signal.
  641. if (mode === 'check') {
  642. for (const file of outstanding) failures.push(`residue: ${file} still carries a pre-rescope name token`)
  643. }
  644. }
  645. if (failures.length > 0) {
  646. for (const failure of failures) console.error(`rescope-vendor: ${failure}`)
  647. console.error(`rescope-vendor: ${String(failures.length)} problem(s); the mapping or an upstream site moved.`)
  648. process.exitCode = 1
  649. } else if (mode === 'check') {
  650. console.log('rescope-vendor: post-state verified — no residue, every exact edit landed, idempotent.')
  651. } else if (mode === 'apply') {
  652. console.log('rescope-vendor: applied. Run `pnpm install`, `pnpm run gen-third-party-notices`, and re-record the touched bilingual pairs.')
  653. }
  654. }
  655. // Importing this module for its exported classifier must not run the codemod.
  656. if (process.argv[1] !== undefined && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
  657. main()
  658. }