check-workspace-constraints.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/shell/… — 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-web-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. '@deepseek-ai/dsh-base': ['cordis.patch.yml'],
  124. '@deepseek-ai/dsh-web-app': ['cordis.patch.yml'],
  125. '@deepseek-ai/dsh-headless': ['cordis.patch.yml'],
  126. '@deepseek-ai/dsh-client-ui-theme': ['lib/styles'],
  127. // The Python runtime uses a distinct closed-resolution bin; the public CLI
  128. // keeps config-owned bare-package resolution through lib/bin.js.
  129. '@deepseek-ai/dsh-sdk-jsonrpc-demo': ['lib/packaged-bin.js'],
  130. // The argv-prefix runner entry ships beside the lib as its own bundle;
  131. // sandbox-local resolves it through the package's ./runner export. tsdown
  132. // also shares its generated FFI code through a hashed runtime chunk.
  133. '@deepseek-ai/dsh-sandbox-windows-acl': ['lib/runner.js', 'lib/types-*.js'],
  134. '@deepseek-ai/dsh-skill-badge': ['assets'],
  135. '@deepseek-ai/dsh-subprocess-local': ['scripts/ensure-spawn-helper.mjs'],
  136. }
  137. function sameStringList(actual: readonly string[] | undefined, expected: readonly string[]): boolean {
  138. return !!actual && actual.length === expected.length && actual.every((value, index) => value === expected[index])
  139. }
  140. function expectedDshPackageFiles(manifest: PackageManifest): readonly string[] {
  141. const extras = manifest.name ? packageFileExtras[manifest.name] ?? [] : []
  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. ...hasTypertRemoteNavigation(manifest)
  175. ? ['lib/typert.remote-client.js', 'lib/typert.remote-client.d.ts']
  176. : [],
  177. ]
  178. }
  179. /** Whether one conditional export exactly names the generated runtime and declaration pair. */
  180. function hasExportPair(
  181. manifest: PackageManifest,
  182. subpath: string,
  183. types: string,
  184. runtime: string,
  185. ): boolean {
  186. const entry = manifest.exports?.[subpath]
  187. return typeof entry === 'object'
  188. && entry !== null
  189. && entry.types === types
  190. && entry.default === runtime
  191. }
  192. /** Runtime target of an export entry: conditional `default`, or the bare-string shorthand. */
  193. function exportDefault(manifest: PackageManifest, subpath: string): string | undefined {
  194. const entry = manifest.exports?.[subpath]
  195. if (typeof entry === 'string') return entry
  196. if (typeof entry === 'object' && entry !== null) return entry.default
  197. return undefined
  198. }
  199. /** Whether any export's runtime default points into the tsc-emitted lib/types tree. */
  200. function usesEmittedTreeDefaults(manifest: PackageManifest): boolean {
  201. return Object.keys(manifest.exports ?? {}).some(subpath =>
  202. exportDefault(manifest, subpath)?.startsWith('./lib/types/') === true)
  203. }
  204. function checkWorkspace({ dir, manifest }: WorkspaceManifest): string[] {
  205. const errors: string[] = []
  206. const label = manifest.name ?? dir
  207. const isLandlockPackageDir = dir.startsWith('native/landlock-run/packages/')
  208. const isPublicLandlockPackage = isLandlockPackageDir
  209. && manifest.name !== undefined
  210. && publicLandlockPackages.has(manifest.name)
  211. if (isPublicLandlockPackage) {
  212. if (manifest.private === true) {
  213. errors.push(`${label}: published Landlock package must not set "private": true`)
  214. }
  215. if (manifest.publishConfig?.access !== 'restricted') {
  216. errors.push(`${label}: published Landlock package must set publishConfig.access to "restricted"`)
  217. }
  218. const expectedDirectory = dir
  219. if (manifest.repository?.type !== 'git'
  220. || manifest.repository.url !== repositoryUrl
  221. || manifest.repository.directory !== expectedDirectory) {
  222. errors.push(`${label}: published Landlock package repository must use ${repositoryUrl} with directory ${expectedDirectory} for trusted publishing`)
  223. }
  224. } else if (releaseMemberDirectory.test(dir)) {
  225. // Release members state that they are publishable: npm refuses a private
  226. // package, the scope is published privately, and the repository field is
  227. // how a consumer of a private package finds its source.
  228. if (manifest.private === true) {
  229. errors.push(`${label}: release member must not set "private": true`)
  230. }
  231. if (manifest.publishConfig?.access !== 'restricted') {
  232. errors.push(`${label}: release member must set publishConfig.access to "restricted"`)
  233. }
  234. if (manifest.repository?.type !== 'git'
  235. || manifest.repository.url !== publishedRepositoryUrl
  236. || manifest.repository.directory !== dir) {
  237. errors.push(`${label}: release member repository must use ${publishedRepositoryUrl} with directory ${dir}`)
  238. }
  239. } else if (manifest.private !== true) {
  240. errors.push(`${label}: package.json must set "private": true`)
  241. }
  242. if (manifest.name && vendoredPackages.has(manifest.name)) {
  243. return errors
  244. }
  245. if (manifest.name?.startsWith('@deepseek-ai/')) {
  246. const allowedSources = publicationSourceAllowlist[manifest.name] ?? []
  247. for (const file of manifest.files ?? []) {
  248. if (isForbiddenPublicationFile(file) && !allowedSources.includes(file)) {
  249. errors.push(`${label}: package.json files must not publish ${JSON.stringify(file)}`)
  250. }
  251. }
  252. }
  253. if (dir.startsWith('apps/') && manifest.name?.startsWith('@deepseek-ai/')) {
  254. const expectedFiles = appPackageFiles[manifest.name]
  255. if (expectedFiles === undefined) {
  256. errors.push(`${label}: app package has no publication files policy`)
  257. } else if (!sameStringList(manifest.files, expectedFiles)) {
  258. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  259. }
  260. }
  261. if (isLandlockPackageDir) {
  262. if (!isPublicLandlockPackage) {
  263. errors.push(`${label}: unexpected package in the public Landlock package family`)
  264. }
  265. if (manifest.version !== landlockVersion) {
  266. errors.push(`${label}: package.json version must match Landlock workspace version ${landlockVersion ?? '(missing)'}`)
  267. }
  268. }
  269. if (dir.startsWith('packages/') && manifest.name?.startsWith('@deepseek-ai/dsh-')) {
  270. const peer = manifest.peerDependencies?.['@deepseek-ai/cordis']
  271. const dev = manifest.devDependencies?.['@deepseek-ai/cordis']
  272. if (!peer) errors.push(`${label}: @deepseek-ai/cordis must be a peerDependency`)
  273. if (!dev) errors.push(`${label}: @deepseek-ai/cordis must also be a devDependency`)
  274. if (peer && dev && peer !== dev) {
  275. errors.push(`${label}: @deepseek-ai/cordis peer (${peer}) and dev (${dev}) ranges must match`)
  276. }
  277. if (manifest.version !== repositoryVersion) {
  278. errors.push(`${label}: package.json version must match root version ${repositoryVersion ?? '(missing)'}`)
  279. }
  280. if (manifest.type !== 'module') {
  281. errors.push(`${label}: package.json must set "type": "module"`)
  282. }
  283. if (manifest.main !== 'lib/index.js') {
  284. errors.push(`${label}: package.json must set "main": "lib/index.js"`)
  285. }
  286. if (manifest.types !== 'lib/types/index.d.ts') {
  287. errors.push(`${label}: package.json must set "types": "lib/types/index.d.ts"`)
  288. }
  289. const rootExport = manifest.exports?.['.']
  290. const rootEntry = typeof rootExport === 'object' && rootExport !== null ? rootExport : undefined
  291. if (rootEntry?.types !== './lib/types/index.d.ts') {
  292. errors.push(`${label}: package.json exports["."].types must be "./lib/types/index.d.ts"`)
  293. }
  294. if (rootEntry?.default !== './lib/index.js') {
  295. errors.push(`${label}: package.json exports["."].default must be "./lib/index.js"`)
  296. }
  297. const invariantRaw = manifest.exports?.['./invariant']
  298. const invariantExport = typeof invariantRaw === 'object' && invariantRaw !== null ? invariantRaw : undefined
  299. if (invariantExport?.types !== undefined && invariantExport.types !== './lib/types/invariant.d.ts') {
  300. errors.push(`${label}: package.json exports["./invariant"].types must be "./lib/types/invariant.d.ts"`)
  301. }
  302. if (invariantExport?.default !== undefined && invariantExport.default !== './lib/invariant.js') {
  303. errors.push(`${label}: package.json exports["./invariant"].default must be "./lib/invariant.js"`)
  304. }
  305. if (invariantExport && (invariantExport.types === undefined || invariantExport.default === undefined)) {
  306. errors.push(`${label}: package.json exports["./invariant"] must declare both types and default targets`)
  307. }
  308. const expectedFiles = expectedDshPackageFiles(manifest)
  309. if (!sameStringList(manifest.files, expectedFiles)) {
  310. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  311. }
  312. }
  313. return errors.map(error => `${relative(root, join(root, dir, 'package.json'))}: ${error}`)
  314. }
  315. /**
  316. * Enforce `packages/<group>/<pkg>`: groups are open-named containers without a
  317. * package.json, and packages may be neither flat nor more deeply nested.
  318. */
  319. function checkHierarchyShape(): string[] {
  320. const errors: string[] = []
  321. const packagesRoot = join(root, 'packages')
  322. for (const group of readdirSync(packagesRoot, { withFileTypes: true })) {
  323. if (!group.isDirectory()) continue
  324. const groupRel = join('packages', group.name)
  325. if (existsSync(join(packagesRoot, group.name, 'package.json'))) {
  326. errors.push(`${groupRel}: a group dir must not contain a package.json — packages live at packages/<group>/<pkg>, not directly under packages/`)
  327. continue
  328. }
  329. for (const pkg of readdirSync(join(packagesRoot, group.name), { withFileTypes: true })) {
  330. if (!pkg.isDirectory()) continue
  331. if (localArtifactDirs.has(pkg.name)) continue
  332. const pkgRel = join(groupRel, pkg.name)
  333. if (!existsSync(join(packagesRoot, group.name, pkg.name, 'package.json'))) {
  334. errors.push(`${pkgRel}: expected a package here (no package.json found) — the hierarchy is exactly packages/<group>/<pkg>, no deeper nesting`)
  335. }
  336. }
  337. }
  338. return errors
  339. }
  340. function checkRepositoryVersion(): string[] {
  341. // The root carries the dsh release family's version, so a prerelease such as
  342. // 0.0.1-rc.1 is a valid state between `release:dsh` and its publication.
  343. if (repositoryVersion && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(repositoryVersion)) return []
  344. return ['package.json: version must be X.Y.Z with an optional prerelease segment']
  345. }
  346. /** Dependency sections whose ranges reach a published tarball or a local install. */
  347. const dependencySections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const
  348. /**
  349. * Require the `workspace:` protocol for every reference to a workspace member.
  350. *
  351. * A hand-written range says nothing about the version the workspace actually
  352. * carries, and `pnpm pack` leaves it alone: `^0.0.1` published from version
  353. * `0.0.2` names a version that does not exist. The protocol makes pack
  354. * substitute the member's real version, so no release step rewrites ranges.
  355. * @param manifests - every workspace manifest.
  356. * @returns One error per reference that names a workspace member without the protocol.
  357. */
  358. function checkWorkspaceProtocol(manifests: readonly WorkspaceManifest[]): string[] {
  359. const members = new Set(manifests.map(entry => entry.manifest.name).filter(name => name !== undefined))
  360. const errors: string[] = []
  361. for (const { dir, manifest } of manifests) {
  362. for (const section of dependencySections) {
  363. for (const [name, range] of Object.entries(manifest[section] ?? {})) {
  364. if (!members.has(name) || range.startsWith('workspace:')) continue
  365. errors.push(`${manifest.name ?? dir}: ${section}.${name} must use the workspace: protocol, got ${range}`)
  366. }
  367. }
  368. }
  369. return errors
  370. }
  371. const manifests = workspaceManifests()
  372. const errors = [
  373. ...checkRepositoryVersion(),
  374. ...manifests.flatMap(checkWorkspace),
  375. ...checkWorkspaceProtocol(manifests),
  376. ...checkHierarchyShape(),
  377. ...collectProjectReferenceFaceViolations(root),
  378. ]
  379. if (errors.length > 0) {
  380. console.error(errors.join('\n'))
  381. process.exitCode = 1
  382. }