check-workspace-constraints.ts 18 KB

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