check-workspace-constraints.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. // dsh-base also ships the win32 shell platform layer the launcher reads.
  122. '@deepseek-ai/dsh-base': ['cordis.patch.yml', 'windows.cordis.patch.yml'],
  123. '@deepseek-ai/dsh-web-app': ['cordis.patch.yml'],
  124. '@deepseek-ai/dsh-headless': ['cordis.patch.yml'],
  125. '@deepseek-ai/dsh-client-ui-theme': ['lib/styles'],
  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. }
  136. function sameStringList(actual: readonly string[] | undefined, expected: readonly string[]): boolean {
  137. return !!actual && actual.length === expected.length && actual.every((value, index) => value === expected[index])
  138. }
  139. function expectedDshPackageFiles(manifest: PackageManifest): readonly string[] {
  140. const extras = manifest.name ? packageFileExtras[manifest.name] ?? [] : []
  141. const typeRTRemoteNavigation = hasTypeRTRemoteNavigation(manifest)
  142. return [
  143. 'lib/index.js',
  144. // Every package publishes its invariant ownership companion as a separate
  145. // bundle; the package-invariant gate validates the companion itself.
  146. 'lib/invariant.js',
  147. ...manifest.bin ? ['lib/bin.js'] : [],
  148. ...manifest.exports?.['./worker'] ? ['lib/worker.cjs'] : [],
  149. // UI plugin packages ship their browser bundle beside the node lib
  150. // (single-artifact ruling: dist/ retired, ./client resolves lib/client.js).
  151. // Keyed on the artifact path, not the subpath name: apiproxy's ./client is
  152. // a browser-safe source channel, not a bundle.
  153. ...exportDefault(manifest, './client') === './lib/client.js' ? ['lib/client.js'] : [],
  154. // runtime's shell-held loader subpath ships as its own bundle beside the client half.
  155. ...exportDefault(manifest, './loader') === './lib/loader.js' ? ['lib/loader.js'] : [],
  156. // web-react's store subpath ships its own bundle (single-entry builds; no shared chunk).
  157. ...exportDefault(manifest, './store') === './lib/store/index.js' ? ['lib/store/index.js'] : [],
  158. // A surface bundle's startup row is its own bundle: the Loader imports it
  159. // as a row module, so it cannot ride inside the package entry.
  160. ...exportDefault(manifest, './startup') === './lib/startup.js' ? ['lib/startup.js'] : [],
  161. ...extras,
  162. // Subpaths whose runtime default is the tsc-emitted tree (lib/types/*.js —
  163. // browser-safe source channels rehomed off src so plain Node can import
  164. // them without type stripping) publish the emitted JS alongside the
  165. // declarations.
  166. ...usesEmittedTreeDefaults(manifest) ? ['lib/types/**/*.js'] : [],
  167. 'lib/types/**/*.d.ts',
  168. ...hasExportPair(manifest, './typert', './lib/typert.host.d.ts', './lib/typert.host.js')
  169. ? ['lib/typert.host.js', 'lib/typert.host.d.ts']
  170. : [],
  171. ...hasExportPair(manifest, './client/typert', './lib/typert.client.d.ts', './lib/typert.client.js')
  172. ? ['lib/typert.client.js', 'lib/typert.client.d.ts']
  173. : [],
  174. ...typeRTRemoteNavigation
  175. ? [
  176. 'lib/typert.remote-client.js',
  177. 'lib/typert.remote-client.d.ts',
  178. 'lib/typert.remote-client.d.ts.map',
  179. 'src',
  180. ]
  181. : [],
  182. ]
  183. }
  184. /** Whether one conditional export exactly names the generated runtime and declaration pair. */
  185. function hasExportPair(
  186. manifest: PackageManifest,
  187. subpath: string,
  188. types: string,
  189. runtime: string,
  190. ): boolean {
  191. const entry = manifest.exports?.[subpath]
  192. return typeof entry === 'object'
  193. && entry !== null
  194. && entry.types === types
  195. && entry.default === runtime
  196. }
  197. /** Runtime target of an export entry: conditional `default`, or the bare-string shorthand. */
  198. function exportDefault(manifest: PackageManifest, subpath: string): string | undefined {
  199. const entry = manifest.exports?.[subpath]
  200. if (typeof entry === 'string') return entry
  201. if (typeof entry === 'object' && entry !== null) return entry.default
  202. return undefined
  203. }
  204. /** Whether any export's runtime default points into the tsc-emitted lib/types tree. */
  205. function usesEmittedTreeDefaults(manifest: PackageManifest): boolean {
  206. return Object.keys(manifest.exports ?? {}).some(subpath =>
  207. exportDefault(manifest, subpath)?.startsWith('./lib/types/') === true)
  208. }
  209. function checkWorkspace({ dir, manifest }: WorkspaceManifest): string[] {
  210. const errors: string[] = []
  211. const label = manifest.name ?? dir
  212. const isLandlockPackageDir = dir.startsWith('native/landlock-run/packages/')
  213. const isPublicLandlockPackage = isLandlockPackageDir
  214. && manifest.name !== undefined
  215. && publicLandlockPackages.has(manifest.name)
  216. if (isPublicLandlockPackage) {
  217. if (manifest.private === true) {
  218. errors.push(`${label}: published Landlock package must not set "private": true`)
  219. }
  220. if (manifest.publishConfig?.access !== 'restricted') {
  221. errors.push(`${label}: published Landlock package must set publishConfig.access to "restricted"`)
  222. }
  223. const expectedDirectory = dir
  224. if (manifest.repository?.type !== 'git'
  225. || manifest.repository.url !== repositoryUrl
  226. || manifest.repository.directory !== expectedDirectory) {
  227. errors.push(`${label}: published Landlock package repository must use ${repositoryUrl} with directory ${expectedDirectory} for trusted publishing`)
  228. }
  229. } else if (releaseMemberDirectory.test(dir)) {
  230. // Release members state that they are publishable: npm refuses a private
  231. // package, the scope is published privately, and the repository field is
  232. // how a consumer of a private package finds its source.
  233. if (manifest.private === true) {
  234. errors.push(`${label}: release member must not set "private": true`)
  235. }
  236. if (manifest.publishConfig?.access !== 'restricted') {
  237. errors.push(`${label}: release member must set publishConfig.access to "restricted"`)
  238. }
  239. if (manifest.repository?.type !== 'git'
  240. || manifest.repository.url !== publishedRepositoryUrl
  241. || manifest.repository.directory !== dir) {
  242. errors.push(`${label}: release member repository must use ${publishedRepositoryUrl} with directory ${dir}`)
  243. }
  244. } else if (manifest.private !== true) {
  245. errors.push(`${label}: package.json must set "private": true`)
  246. }
  247. if (manifest.name && vendoredPackages.has(manifest.name)) {
  248. return errors
  249. }
  250. if (manifest.name?.startsWith('@deepseek-ai/')) {
  251. const allowedSources = publicationSourceAllowlist[manifest.name] ?? []
  252. const publicationPolicy = { typeRTRemoteNavigation: hasTypeRTRemoteNavigation(manifest) }
  253. for (const file of manifest.files ?? []) {
  254. if (isForbiddenPublicationFile(file, publicationPolicy) && !allowedSources.includes(file)) {
  255. errors.push(`${label}: package.json files must not publish ${JSON.stringify(file)}`)
  256. }
  257. }
  258. }
  259. if (dir.startsWith('apps/') && manifest.name?.startsWith('@deepseek-ai/')) {
  260. const expectedFiles = appPackageFiles[manifest.name]
  261. if (expectedFiles === undefined) {
  262. errors.push(`${label}: app package has no publication files policy`)
  263. } else if (!sameStringList(manifest.files, expectedFiles)) {
  264. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  265. }
  266. }
  267. if (isLandlockPackageDir) {
  268. if (!isPublicLandlockPackage) {
  269. errors.push(`${label}: unexpected package in the public Landlock package family`)
  270. }
  271. if (manifest.version !== landlockVersion) {
  272. errors.push(`${label}: package.json version must match Landlock workspace version ${landlockVersion ?? '(missing)'}`)
  273. }
  274. }
  275. if (dir.startsWith('packages/') && manifest.name?.startsWith('@deepseek-ai/dsh-')) {
  276. const peer = manifest.peerDependencies?.['@deepseek-ai/cordis']
  277. const dev = manifest.devDependencies?.['@deepseek-ai/cordis']
  278. if (!peer) errors.push(`${label}: @deepseek-ai/cordis must be a peerDependency`)
  279. if (!dev) errors.push(`${label}: @deepseek-ai/cordis must also be a devDependency`)
  280. if (peer && dev && peer !== dev) {
  281. errors.push(`${label}: @deepseek-ai/cordis peer (${peer}) and dev (${dev}) ranges must match`)
  282. }
  283. if (manifest.version !== repositoryVersion) {
  284. errors.push(`${label}: package.json version must match root version ${repositoryVersion ?? '(missing)'}`)
  285. }
  286. if (manifest.type !== 'module') {
  287. errors.push(`${label}: package.json must set "type": "module"`)
  288. }
  289. if (manifest.main !== 'lib/index.js') {
  290. errors.push(`${label}: package.json must set "main": "lib/index.js"`)
  291. }
  292. if (manifest.types !== 'lib/types/index.d.ts') {
  293. errors.push(`${label}: package.json must set "types": "lib/types/index.d.ts"`)
  294. }
  295. const rootExport = manifest.exports?.['.']
  296. const rootEntry = typeof rootExport === 'object' && rootExport !== null ? rootExport : undefined
  297. if (rootEntry?.types !== './lib/types/index.d.ts') {
  298. errors.push(`${label}: package.json exports["."].types must be "./lib/types/index.d.ts"`)
  299. }
  300. if (rootEntry?.default !== './lib/index.js') {
  301. errors.push(`${label}: package.json exports["."].default must be "./lib/index.js"`)
  302. }
  303. const invariantRaw = manifest.exports?.['./invariant']
  304. const invariantExport = typeof invariantRaw === 'object' && invariantRaw !== null ? invariantRaw : undefined
  305. if (invariantExport?.types !== undefined && invariantExport.types !== './lib/types/invariant.d.ts') {
  306. errors.push(`${label}: package.json exports["./invariant"].types must be "./lib/types/invariant.d.ts"`)
  307. }
  308. if (invariantExport?.default !== undefined && invariantExport.default !== './lib/invariant.js') {
  309. errors.push(`${label}: package.json exports["./invariant"].default must be "./lib/invariant.js"`)
  310. }
  311. if (invariantExport && (invariantExport.types === undefined || invariantExport.default === undefined)) {
  312. errors.push(`${label}: package.json exports["./invariant"] must declare both types and default targets`)
  313. }
  314. const expectedFiles = expectedDshPackageFiles(manifest)
  315. if (!sameStringList(manifest.files, expectedFiles)) {
  316. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  317. }
  318. }
  319. return errors.map(error => `${relative(root, join(root, dir, 'package.json'))}: ${error}`)
  320. }
  321. /**
  322. * Enforce `packages/<group>/<pkg>`: groups are open-named containers without a
  323. * package.json, and packages may be neither flat nor more deeply nested.
  324. */
  325. function checkHierarchyShape(): string[] {
  326. const errors: string[] = []
  327. const packagesRoot = join(root, 'packages')
  328. for (const group of readdirSync(packagesRoot, { withFileTypes: true })) {
  329. if (!group.isDirectory()) continue
  330. const groupRel = join('packages', group.name)
  331. if (existsSync(join(packagesRoot, group.name, 'package.json'))) {
  332. errors.push(`${groupRel}: a group dir must not contain a package.json — packages live at packages/<group>/<pkg>, not directly under packages/`)
  333. continue
  334. }
  335. for (const pkg of readdirSync(join(packagesRoot, group.name), { withFileTypes: true })) {
  336. if (!pkg.isDirectory()) continue
  337. if (localArtifactDirs.has(pkg.name)) continue
  338. const pkgRel = join(groupRel, pkg.name)
  339. if (!existsSync(join(packagesRoot, group.name, pkg.name, 'package.json'))) {
  340. errors.push(`${pkgRel}: expected a package here (no package.json found) — the hierarchy is exactly packages/<group>/<pkg>, no deeper nesting`)
  341. }
  342. }
  343. }
  344. return errors
  345. }
  346. function checkRepositoryVersion(): string[] {
  347. // The root carries the dsh release family's version, so a prerelease such as
  348. // 0.0.1-rc.1 is a valid state between `release:dsh` and its publication.
  349. if (repositoryVersion && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(repositoryVersion)) return []
  350. return ['package.json: version must be X.Y.Z with an optional prerelease segment']
  351. }
  352. /** Dependency sections whose ranges reach a published tarball or a local install. */
  353. const dependencySections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const
  354. /**
  355. * Require the `workspace:` protocol for every reference to a workspace member.
  356. *
  357. * A hand-written range says nothing about the version the workspace actually
  358. * carries, and `pnpm pack` leaves it alone: `^0.0.1` published from version
  359. * `0.0.2` names a version that does not exist. The protocol makes pack
  360. * substitute the member's real version, so no release step rewrites ranges.
  361. * @param manifests - every workspace manifest.
  362. * @returns One error per reference that names a workspace member without the protocol.
  363. */
  364. function checkWorkspaceProtocol(manifests: readonly WorkspaceManifest[]): string[] {
  365. const members = new Set(manifests.map(entry => entry.manifest.name).filter(name => name !== undefined))
  366. const errors: string[] = []
  367. for (const { dir, manifest } of manifests) {
  368. for (const section of dependencySections) {
  369. for (const [name, range] of Object.entries(manifest[section] ?? {})) {
  370. if (!members.has(name) || range.startsWith('workspace:')) continue
  371. errors.push(`${manifest.name ?? dir}: ${section}.${name} must use the workspace: protocol, got ${range}`)
  372. }
  373. }
  374. }
  375. return errors
  376. }
  377. const manifests = workspaceManifests()
  378. const errors = [
  379. ...checkRepositoryVersion(),
  380. ...manifests.flatMap(checkWorkspace),
  381. ...checkWorkspaceProtocol(manifests),
  382. ...checkHierarchyShape(),
  383. ...collectProjectReferenceFaceViolations(root),
  384. ]
  385. if (errors.length > 0) {
  386. console.error(errors.join('\n'))
  387. process.exitCode = 1
  388. }