rescope-vendor.ts 34 KB

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