rescope-vendor.ts 34 KB

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