check-workspace-constraints.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * Workspace package invariant checks for package-manager-independent quality
  3. * gates.
  4. *
  5. * Run: `tsx scripts/check-workspace-constraints.ts`.
  6. */
  7. import { existsSync, readdirSync, readFileSync } from 'node:fs'
  8. import { join, relative, resolve } from 'node:path'
  9. import { hasTypeRTRemoteNavigation, isForbiddenPublicationFile } from './publication-payload.ts'
  10. import { collectProjectReferenceFaceViolations } from './project-reference-faces.ts'
  11. const root = resolve(import.meta.dirname, '..')
  12. // vendor/* is single-level; packages/<group>/<pkg> nests one level deeper
  13. // (the group dirs — core/llm/bash/… — are pure containers with no manifest).
  14. const workspaceGlobs = [
  15. { dir: 'vendor', depth: 1 },
  16. { dir: 'packages', depth: 2 },
  17. { dir: 'native', depth: 1 },
  18. { dir: 'native/landlock-run/packages', depth: 1 },
  19. { dir: 'apps', depth: 1 },
  20. ] as const
  21. const vendoredPackages = new Set([
  22. '@deepseek-ai/cordis',
  23. '@deepseek-ai/cosmokit',
  24. '@deepseek-ai/schemastery',
  25. '@deepseek-ai/cordis-plugin-loader',
  26. '@deepseek-ai/cordis-plugin-include',
  27. '@deepseek-ai/cordis-plugin-group',
  28. '@deepseek-ai/cordis-plugin-timer',
  29. '@deepseek-ai/cordis-plugin-hmr',
  30. '@deepseek-ai/cordis-plugin-logger-console',
  31. ])
  32. const publicLandlockPackages = new Set([
  33. '@deepseek-ai/node-addon-landlock-run',
  34. '@deepseek-ai/node-addon-landlock-run-linux-arm64',
  35. '@deepseek-ai/node-addon-landlock-run-linux-x64',
  36. ])
  37. /** Deliberate source payloads whose exact bytes are part of the package's audit surface. */
  38. const publicationSourceAllowlist: Readonly<Record<string, readonly string[]>> = {
  39. '@deepseek-ai/node-addon-landlock-run': ['src/main.c'],
  40. }
  41. const repositoryUrl = 'git+https://github.com/deepseek-harness/deepseek-harness.git'
  42. /**
  43. * Source home the published packages point consumers at. It differs from
  44. * {@link repositoryUrl}, which the Landlock packages keep because npm resolves
  45. * their trusted publishing against the repository that runs the workflow.
  46. */
  47. const publishedRepositoryUrl = 'git+https://github.com/deepseek-ai/deepseek-harness.git'
  48. /** Directories whose packages this repository publishes: one release member each. */
  49. const releaseMemberDirectory = /^(?:packages\/[^/]+\/[^/]+|apps\/[^/]+|vendor\/[^/]+)$/
  50. const localArtifactDirs = new Set(['node_modules'])
  51. const appPackageFiles: Readonly<Record<string, readonly string[]>> = {
  52. '@deepseek-ai/dsh': ['lib/*.js', 'config'],
  53. '@deepseek-ai/dsh-frontend': ['dist'],
  54. }
  55. /** The subset of package.json fields this constraint check cares about. */
  56. interface PackageManifest {
  57. name?: string
  58. version?: string
  59. private?: boolean
  60. type?: string
  61. main?: string
  62. types?: string
  63. bin?: string | Record<string, string>
  64. exports?: Record<
  65. string,
  66. | string
  67. | {
  68. types?: string
  69. default?: string
  70. }
  71. | null
  72. | undefined
  73. >
  74. files?: string[]
  75. publishConfig?: { access?: string }
  76. repository?: { type?: string; url?: string; directory?: string }
  77. peerDependencies?: Record<string, string>
  78. devDependencies?: Record<string, string>
  79. dependencies?: Record<string, string>
  80. optionalDependencies?: Record<string, string>
  81. }
  82. /** One workspace manifest and its repo-relative path. */
  83. interface WorkspaceManifest {
  84. dir: string
  85. manifest: PackageManifest
  86. }
  87. function readJson(path: string): PackageManifest {
  88. return JSON.parse(readFileSync(path, 'utf8')) as PackageManifest
  89. }
  90. const rootManifest = readJson(join(root, 'package.json'))
  91. const repositoryVersion = rootManifest.version
  92. const landlockWorkspaceManifest = readJson(join(root, 'native/landlock-run/package.json'))
  93. const landlockVersion = landlockWorkspaceManifest.version
  94. /** Repo-relative dirs holding a package.json, walked to the configured depth. */
  95. function packageDirs(base: string, depth: number): string[] {
  96. if (depth === 1) {
  97. return readdirSync(join(root, base), { withFileTypes: true })
  98. .filter(entry => entry.isDirectory())
  99. .filter(entry => !localArtifactDirs.has(entry.name))
  100. .filter(entry => existsSync(join(root, base, entry.name, 'package.json')))
  101. .map(entry => `${base}/${entry.name}`)
  102. }
  103. return readdirSync(join(root, base), { withFileTypes: true })
  104. .filter(entry => entry.isDirectory())
  105. .filter(entry => !localArtifactDirs.has(entry.name))
  106. .flatMap(group => packageDirs(`${base}/${group.name}`, depth - 1))
  107. }
  108. function workspaceManifests(): WorkspaceManifest[] {
  109. const manifests: WorkspaceManifest[] = [
  110. { dir: '.', manifest: rootManifest },
  111. ]
  112. for (const { dir: base, depth } of workspaceGlobs) {
  113. for (const dir of packageDirs(base, depth)) {
  114. manifests.push({ dir, manifest: readJson(join(root, dir, 'package.json')) })
  115. }
  116. }
  117. return manifests
  118. }
  119. const packageFileExtras: Readonly<Record<string, readonly string[]>> = {
  120. // Profile bundles publish their dsh.bundle.patch layer beside the lib.
  121. '@deepseek-ai/dsh-base': ['cordis.patch.yml'],
  122. '@deepseek-ai/dsh-web-app': ['cordis.patch.yml'],
  123. '@deepseek-ai/dsh-headless': ['cordis.patch.yml'],
  124. '@deepseek-ai/dsh-client-ui-theme': ['lib/styles'],
  125. '@deepseek-ai/dsh-helper': ['lib/assets'],
  126. // The Python runtime uses a distinct closed-resolution bin; the public CLI
  127. // keeps config-owned bare-package resolution through lib/bin.js.
  128. '@deepseek-ai/dsh-jsonrpc-demo': ['lib/packaged-bin.js'],
  129. // The argv-prefix runner entry ships beside the lib as its own bundle;
  130. // sandbox-local resolves it through the package's ./runner export. tsdown
  131. // also shares its generated FFI code through a hashed runtime chunk.
  132. '@deepseek-ai/dsh-sandbox-windows-acl': ['lib/runner.js', 'lib/types-*.js'],
  133. '@deepseek-ai/dsh-skill-badge': ['assets'],
  134. '@deepseek-ai/dsh-subprocess-local': ['scripts/ensure-spawn-helper.mjs'],
  135. '@deepseek-ai/dsh-scripts': [
  136. 'lib/dev/tsdown-config.js',
  137. 'lib/local-plugin-loader-hooks.js',
  138. 'lib/assets',
  139. ],
  140. }
  141. function sameStringList(actual: readonly string[] | undefined, expected: readonly string[]): boolean {
  142. return !!actual && actual.length === expected.length && actual.every((value, index) => value === expected[index])
  143. }
  144. function expectedDshPackageFiles(manifest: PackageManifest): readonly string[] {
  145. const extras = manifest.name ? packageFileExtras[manifest.name] ?? [] : []
  146. const typeRTRemoteNavigation = hasTypeRTRemoteNavigation(manifest)
  147. return [
  148. 'lib/index.js',
  149. // Every package publishes its invariant ownership companion as a separate
  150. // bundle; the package-invariant gate validates the companion itself.
  151. 'lib/invariant.js',
  152. ...manifest.bin ? ['lib/bin.js'] : [],
  153. ...manifest.exports?.['./worker'] ? ['lib/worker.cjs'] : [],
  154. // UI plugin packages ship their browser bundle beside the node lib
  155. // (single-artifact ruling: dist/ retired, ./client resolves lib/client.js).
  156. // Keyed on the artifact path, not the subpath name: apiproxy's ./client is
  157. // a browser-safe source channel, not a bundle.
  158. ...exportDefault(manifest, './client') === './lib/client.js' ? ['lib/client.js'] : [],
  159. // runtime's shell-held loader subpath ships as its own bundle beside the client half.
  160. ...exportDefault(manifest, './loader') === './lib/loader.js' ? ['lib/loader.js'] : [],
  161. // web-react's store subpath ships its own bundle (single-entry builds; no shared chunk).
  162. ...exportDefault(manifest, './store') === './lib/store/index.js' ? ['lib/store/index.js'] : [],
  163. // A surface bundle's startup row is its own bundle: the Loader imports it
  164. // as a row module, so it cannot ride inside the package entry.
  165. ...exportDefault(manifest, './startup') === './lib/startup.js' ? ['lib/startup.js'] : [],
  166. ...extras,
  167. // Subpaths whose runtime default is the tsc-emitted tree (lib/types/*.js —
  168. // browser-safe source channels rehomed off src so plain Node can import
  169. // them without type stripping) publish the emitted JS alongside the
  170. // declarations.
  171. ...usesEmittedTreeDefaults(manifest) ? ['lib/types/**/*.js'] : [],
  172. 'lib/types/**/*.d.ts',
  173. ...hasExportPair(manifest, './typert', './lib/typert.host.d.ts', './lib/typert.host.js')
  174. ? ['lib/typert.host.js', 'lib/typert.host.d.ts']
  175. : [],
  176. ...hasExportPair(manifest, './client/typert', './lib/typert.client.d.ts', './lib/typert.client.js')
  177. ? ['lib/typert.client.js', 'lib/typert.client.d.ts']
  178. : [],
  179. ...typeRTRemoteNavigation
  180. ? [
  181. 'lib/typert.remote-client.js',
  182. 'lib/typert.remote-client.d.ts',
  183. 'lib/typert.remote-client.d.ts.map',
  184. 'src',
  185. ]
  186. : [],
  187. ]
  188. }
  189. /** Whether one conditional export exactly names the generated runtime and declaration pair. */
  190. function hasExportPair(
  191. manifest: PackageManifest,
  192. subpath: string,
  193. types: string,
  194. runtime: string,
  195. ): boolean {
  196. const entry = manifest.exports?.[subpath]
  197. return typeof entry === 'object'
  198. && entry !== null
  199. && entry.types === types
  200. && entry.default === runtime
  201. }
  202. /** Runtime target of an export entry: conditional `default`, or the bare-string shorthand. */
  203. function exportDefault(manifest: PackageManifest, subpath: string): string | undefined {
  204. const entry = manifest.exports?.[subpath]
  205. if (typeof entry === 'string') return entry
  206. if (typeof entry === 'object' && entry !== null) return entry.default
  207. return undefined
  208. }
  209. /** Whether any export's runtime default points into the tsc-emitted lib/types tree. */
  210. function usesEmittedTreeDefaults(manifest: PackageManifest): boolean {
  211. return Object.keys(manifest.exports ?? {}).some(subpath =>
  212. exportDefault(manifest, subpath)?.startsWith('./lib/types/') === true)
  213. }
  214. function checkWorkspace({ dir, manifest }: WorkspaceManifest): string[] {
  215. const errors: string[] = []
  216. const label = manifest.name ?? dir
  217. const isLandlockPackageDir = dir.startsWith('native/landlock-run/packages/')
  218. const isPublicLandlockPackage = isLandlockPackageDir
  219. && manifest.name !== undefined
  220. && publicLandlockPackages.has(manifest.name)
  221. if (isPublicLandlockPackage) {
  222. if (manifest.private === true) {
  223. errors.push(`${label}: published Landlock package must not set "private": true`)
  224. }
  225. if (manifest.publishConfig?.access !== 'restricted') {
  226. errors.push(`${label}: published Landlock package must set publishConfig.access to "restricted"`)
  227. }
  228. const expectedDirectory = dir
  229. if (manifest.repository?.type !== 'git'
  230. || manifest.repository.url !== repositoryUrl
  231. || manifest.repository.directory !== expectedDirectory) {
  232. errors.push(`${label}: published Landlock package repository must use ${repositoryUrl} with directory ${expectedDirectory} for trusted publishing`)
  233. }
  234. } else if (releaseMemberDirectory.test(dir)) {
  235. // Release members state that they are publishable: npm refuses a private
  236. // package, the scope is published privately, and the repository field is
  237. // how a consumer of a private package finds its source.
  238. if (manifest.private === true) {
  239. errors.push(`${label}: release member must not set "private": true`)
  240. }
  241. if (manifest.publishConfig?.access !== 'restricted') {
  242. errors.push(`${label}: release member must set publishConfig.access to "restricted"`)
  243. }
  244. if (manifest.repository?.type !== 'git'
  245. || manifest.repository.url !== publishedRepositoryUrl
  246. || manifest.repository.directory !== dir) {
  247. errors.push(`${label}: release member repository must use ${publishedRepositoryUrl} with directory ${dir}`)
  248. }
  249. } else if (manifest.private !== true) {
  250. errors.push(`${label}: package.json must set "private": true`)
  251. }
  252. if (manifest.name && vendoredPackages.has(manifest.name)) {
  253. return errors
  254. }
  255. if (manifest.name?.startsWith('@deepseek-ai/')) {
  256. const allowedSources = publicationSourceAllowlist[manifest.name] ?? []
  257. const publicationPolicy = { typeRTRemoteNavigation: hasTypeRTRemoteNavigation(manifest) }
  258. for (const file of manifest.files ?? []) {
  259. if (isForbiddenPublicationFile(file, publicationPolicy) && !allowedSources.includes(file)) {
  260. errors.push(`${label}: package.json files must not publish ${JSON.stringify(file)}`)
  261. }
  262. }
  263. }
  264. if (dir.startsWith('apps/') && manifest.name?.startsWith('@deepseek-ai/')) {
  265. const expectedFiles = appPackageFiles[manifest.name]
  266. if (expectedFiles === undefined) {
  267. errors.push(`${label}: app package has no publication files policy`)
  268. } else if (!sameStringList(manifest.files, expectedFiles)) {
  269. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  270. }
  271. }
  272. if (isLandlockPackageDir) {
  273. if (!isPublicLandlockPackage) {
  274. errors.push(`${label}: unexpected package in the public Landlock package family`)
  275. }
  276. if (manifest.version !== landlockVersion) {
  277. errors.push(`${label}: package.json version must match Landlock workspace version ${landlockVersion ?? '(missing)'}`)
  278. }
  279. }
  280. if (dir.startsWith('packages/') && manifest.name?.startsWith('@deepseek-ai/dsh-')) {
  281. const peer = manifest.peerDependencies?.['@deepseek-ai/cordis']
  282. const dev = manifest.devDependencies?.['@deepseek-ai/cordis']
  283. if (!peer) errors.push(`${label}: @deepseek-ai/cordis must be a peerDependency`)
  284. if (!dev) errors.push(`${label}: @deepseek-ai/cordis must also be a devDependency`)
  285. if (peer && dev && peer !== dev) {
  286. errors.push(`${label}: @deepseek-ai/cordis peer (${peer}) and dev (${dev}) ranges must match`)
  287. }
  288. if (manifest.version !== repositoryVersion) {
  289. errors.push(`${label}: package.json version must match root version ${repositoryVersion ?? '(missing)'}`)
  290. }
  291. if (manifest.type !== 'module') {
  292. errors.push(`${label}: package.json must set "type": "module"`)
  293. }
  294. if (manifest.main !== 'lib/index.js') {
  295. errors.push(`${label}: package.json must set "main": "lib/index.js"`)
  296. }
  297. if (manifest.types !== 'lib/types/index.d.ts') {
  298. errors.push(`${label}: package.json must set "types": "lib/types/index.d.ts"`)
  299. }
  300. const rootExport = manifest.exports?.['.']
  301. const rootEntry = typeof rootExport === 'object' && rootExport !== null ? rootExport : undefined
  302. if (rootEntry?.types !== './lib/types/index.d.ts') {
  303. errors.push(`${label}: package.json exports["."].types must be "./lib/types/index.d.ts"`)
  304. }
  305. if (rootEntry?.default !== './lib/index.js') {
  306. errors.push(`${label}: package.json exports["."].default must be "./lib/index.js"`)
  307. }
  308. const invariantRaw = manifest.exports?.['./invariant']
  309. const invariantExport = typeof invariantRaw === 'object' && invariantRaw !== null ? invariantRaw : undefined
  310. if (invariantExport?.types !== undefined && invariantExport.types !== './lib/types/invariant.d.ts') {
  311. errors.push(`${label}: package.json exports["./invariant"].types must be "./lib/types/invariant.d.ts"`)
  312. }
  313. if (invariantExport?.default !== undefined && invariantExport.default !== './lib/invariant.js') {
  314. errors.push(`${label}: package.json exports["./invariant"].default must be "./lib/invariant.js"`)
  315. }
  316. if (invariantExport && (invariantExport.types === undefined || invariantExport.default === undefined)) {
  317. errors.push(`${label}: package.json exports["./invariant"] must declare both types and default targets`)
  318. }
  319. const expectedFiles = expectedDshPackageFiles(manifest)
  320. if (!sameStringList(manifest.files, expectedFiles)) {
  321. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  322. }
  323. }
  324. return errors.map(error => `${relative(root, join(root, dir, 'package.json'))}: ${error}`)
  325. }
  326. /**
  327. * Enforce `packages/<group>/<pkg>`: groups are open-named containers without a
  328. * package.json, and packages may be neither flat nor more deeply nested.
  329. */
  330. function checkHierarchyShape(): string[] {
  331. const errors: string[] = []
  332. const packagesRoot = join(root, 'packages')
  333. for (const group of readdirSync(packagesRoot, { withFileTypes: true })) {
  334. if (!group.isDirectory()) continue
  335. const groupRel = join('packages', group.name)
  336. if (existsSync(join(packagesRoot, group.name, 'package.json'))) {
  337. errors.push(`${groupRel}: a group dir must not contain a package.json — packages live at packages/<group>/<pkg>, not directly under packages/`)
  338. continue
  339. }
  340. for (const pkg of readdirSync(join(packagesRoot, group.name), { withFileTypes: true })) {
  341. if (!pkg.isDirectory()) continue
  342. if (localArtifactDirs.has(pkg.name)) continue
  343. const pkgRel = join(groupRel, pkg.name)
  344. if (!existsSync(join(packagesRoot, group.name, pkg.name, 'package.json'))) {
  345. errors.push(`${pkgRel}: expected a package here (no package.json found) — the hierarchy is exactly packages/<group>/<pkg>, no deeper nesting`)
  346. }
  347. }
  348. }
  349. return errors
  350. }
  351. function checkRepositoryVersion(): string[] {
  352. // The root carries the dsh release family's version, so a prerelease such as
  353. // 0.0.1-rc.1 is a valid state between `release:dsh` and its publication.
  354. if (repositoryVersion && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(repositoryVersion)) return []
  355. return ['package.json: version must be X.Y.Z with an optional prerelease segment']
  356. }
  357. /** Dependency sections whose ranges reach a published tarball or a local install. */
  358. const dependencySections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const
  359. /**
  360. * Require the `workspace:` protocol for every reference to a workspace member.
  361. *
  362. * A hand-written range says nothing about the version the workspace actually
  363. * carries, and `pnpm pack` leaves it alone: `^0.0.1` published from version
  364. * `0.0.2` names a version that does not exist. The protocol makes pack
  365. * substitute the member's real version, so no release step rewrites ranges.
  366. * @param manifests - every workspace manifest.
  367. * @returns One error per reference that names a workspace member without the protocol.
  368. */
  369. function checkWorkspaceProtocol(manifests: readonly WorkspaceManifest[]): string[] {
  370. const members = new Set(manifests.map(entry => entry.manifest.name).filter(name => name !== undefined))
  371. const errors: string[] = []
  372. for (const { dir, manifest } of manifests) {
  373. for (const section of dependencySections) {
  374. for (const [name, range] of Object.entries(manifest[section] ?? {})) {
  375. if (!members.has(name) || range.startsWith('workspace:')) continue
  376. errors.push(`${manifest.name ?? dir}: ${section}.${name} must use the workspace: protocol, got ${range}`)
  377. }
  378. }
  379. }
  380. return errors
  381. }
  382. const manifests = workspaceManifests()
  383. const errors = [
  384. ...checkRepositoryVersion(),
  385. ...manifests.flatMap(checkWorkspace),
  386. ...checkWorkspaceProtocol(manifests),
  387. ...checkHierarchyShape(),
  388. ...collectProjectReferenceFaceViolations(root),
  389. ]
  390. if (errors.length > 0) {
  391. console.error(errors.join('\n'))
  392. process.exitCode = 1
  393. }