1
0

check-workspace-constraints.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 !== 'public') {
  216. errors.push(`${label}: published Landlock package must set publishConfig.access to "public"`)
  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, and the repository field is how a consumer finds the source of
  227. // the package it installed.
  228. //
  229. // Access is per release sequence, not per scope: the vendored framework and
  230. // the Landlock packages publish publicly because outside consumers install
  231. // them, while the dsh family stays restricted until its own sequence goes
  232. // public. A mixed scope is why no publish path passes `--access` — one flag
  233. // cannot serve both, so each packed manifest decides
  234. // ([rationale](../.agents/notes/implemented/process/2026-08-13-public-vendor-and-native-sequences.md)).
  235. if (manifest.private === true) {
  236. errors.push(`${label}: release member must not set "private": true`)
  237. }
  238. if (manifest.publishConfig?.access !== 'public') {
  239. errors.push(`${label}: release member must set publishConfig.access to "public"`)
  240. }
  241. if (manifest.repository?.type !== 'git'
  242. || manifest.repository.url !== publishedRepositoryUrl
  243. || manifest.repository.directory !== dir) {
  244. errors.push(`${label}: release member repository must use ${publishedRepositoryUrl} with directory ${dir}`)
  245. }
  246. } else if (manifest.private !== true) {
  247. errors.push(`${label}: package.json must set "private": true`)
  248. }
  249. if (manifest.name && vendoredPackages.has(manifest.name)) {
  250. return errors
  251. }
  252. if (manifest.name?.startsWith('@deepseek-ai/')) {
  253. const allowedSources = publicationSourceAllowlist[manifest.name] ?? []
  254. for (const file of manifest.files ?? []) {
  255. if (isForbiddenPublicationFile(file) && !allowedSources.includes(file)) {
  256. errors.push(`${label}: package.json files must not publish ${JSON.stringify(file)}`)
  257. }
  258. }
  259. }
  260. if (dir.startsWith('apps/') && manifest.name?.startsWith('@deepseek-ai/')) {
  261. const expectedFiles = appPackageFiles[manifest.name]
  262. if (expectedFiles === undefined) {
  263. errors.push(`${label}: app package has no publication files policy`)
  264. } else if (!sameStringList(manifest.files, expectedFiles)) {
  265. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  266. }
  267. }
  268. if (isLandlockPackageDir) {
  269. if (!isPublicLandlockPackage) {
  270. errors.push(`${label}: unexpected package in the public Landlock package family`)
  271. }
  272. if (manifest.version !== landlockVersion) {
  273. errors.push(`${label}: package.json version must match Landlock workspace version ${landlockVersion ?? '(missing)'}`)
  274. }
  275. }
  276. if (dir.startsWith('packages/') && manifest.name?.startsWith('@deepseek-ai/dsh-')) {
  277. const peer = manifest.peerDependencies?.['@deepseek-ai/cordis']
  278. const dev = manifest.devDependencies?.['@deepseek-ai/cordis']
  279. if (!peer) errors.push(`${label}: @deepseek-ai/cordis must be a peerDependency`)
  280. if (!dev) errors.push(`${label}: @deepseek-ai/cordis must also be a devDependency`)
  281. if (peer && dev && peer !== dev) {
  282. errors.push(`${label}: @deepseek-ai/cordis peer (${peer}) and dev (${dev}) ranges must match`)
  283. }
  284. if (manifest.version !== repositoryVersion) {
  285. errors.push(`${label}: package.json version must match root version ${repositoryVersion ?? '(missing)'}`)
  286. }
  287. if (manifest.type !== 'module') {
  288. errors.push(`${label}: package.json must set "type": "module"`)
  289. }
  290. if (manifest.main !== 'lib/index.js') {
  291. errors.push(`${label}: package.json must set "main": "lib/index.js"`)
  292. }
  293. if (manifest.types !== 'lib/types/index.d.ts') {
  294. errors.push(`${label}: package.json must set "types": "lib/types/index.d.ts"`)
  295. }
  296. const rootExport = manifest.exports?.['.']
  297. const rootEntry = typeof rootExport === 'object' && rootExport !== null ? rootExport : undefined
  298. if (rootEntry?.types !== './lib/types/index.d.ts') {
  299. errors.push(`${label}: package.json exports["."].types must be "./lib/types/index.d.ts"`)
  300. }
  301. if (rootEntry?.default !== './lib/index.js') {
  302. errors.push(`${label}: package.json exports["."].default must be "./lib/index.js"`)
  303. }
  304. const invariantRaw = manifest.exports?.['./invariant']
  305. const invariantExport = typeof invariantRaw === 'object' && invariantRaw !== null ? invariantRaw : undefined
  306. if (invariantExport?.types !== undefined && invariantExport.types !== './lib/types/invariant.d.ts') {
  307. errors.push(`${label}: package.json exports["./invariant"].types must be "./lib/types/invariant.d.ts"`)
  308. }
  309. if (invariantExport?.default !== undefined && invariantExport.default !== './lib/invariant.js') {
  310. errors.push(`${label}: package.json exports["./invariant"].default must be "./lib/invariant.js"`)
  311. }
  312. if (invariantExport && (invariantExport.types === undefined || invariantExport.default === undefined)) {
  313. errors.push(`${label}: package.json exports["./invariant"] must declare both types and default targets`)
  314. }
  315. const expectedFiles = expectedDshPackageFiles(manifest)
  316. if (!sameStringList(manifest.files, expectedFiles)) {
  317. errors.push(`${label}: package.json files must be ${JSON.stringify(expectedFiles)}`)
  318. }
  319. }
  320. return errors.map(error => `${relative(root, join(root, dir, 'package.json'))}: ${error}`)
  321. }
  322. /**
  323. * Enforce `packages/<group>/<pkg>`: groups are open-named containers without a
  324. * package.json, and packages may be neither flat nor more deeply nested.
  325. */
  326. function checkHierarchyShape(): string[] {
  327. const errors: string[] = []
  328. const packagesRoot = join(root, 'packages')
  329. for (const group of readdirSync(packagesRoot, { withFileTypes: true })) {
  330. if (!group.isDirectory()) continue
  331. const groupRel = join('packages', group.name)
  332. if (existsSync(join(packagesRoot, group.name, 'package.json'))) {
  333. errors.push(`${groupRel}: a group dir must not contain a package.json — packages live at packages/<group>/<pkg>, not directly under packages/`)
  334. continue
  335. }
  336. for (const pkg of readdirSync(join(packagesRoot, group.name), { withFileTypes: true })) {
  337. if (!pkg.isDirectory()) continue
  338. if (localArtifactDirs.has(pkg.name)) continue
  339. const pkgRel = join(groupRel, pkg.name)
  340. if (!existsSync(join(packagesRoot, group.name, pkg.name, 'package.json'))) {
  341. errors.push(`${pkgRel}: expected a package here (no package.json found) — the hierarchy is exactly packages/<group>/<pkg>, no deeper nesting`)
  342. }
  343. }
  344. }
  345. return errors
  346. }
  347. function checkRepositoryVersion(): string[] {
  348. // The root carries the dsh release family's version, so a prerelease such as
  349. // 0.0.1-rc.1 is a valid state between `release:dsh` and its publication.
  350. if (repositoryVersion && /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(repositoryVersion)) return []
  351. return ['package.json: version must be X.Y.Z with an optional prerelease segment']
  352. }
  353. /** Dependency sections whose ranges reach a published tarball or a local install. */
  354. const dependencySections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const
  355. /**
  356. * Require the `workspace:` protocol for every reference to a workspace member.
  357. *
  358. * A hand-written range says nothing about the version the workspace actually
  359. * carries, and `pnpm pack` leaves it alone: `^0.0.1` published from version
  360. * `0.0.2` names a version that does not exist. The protocol makes pack
  361. * substitute the member's real version, so no release step rewrites ranges.
  362. * @param manifests - every workspace manifest.
  363. * @returns One error per reference that names a workspace member without the protocol.
  364. */
  365. function checkWorkspaceProtocol(manifests: readonly WorkspaceManifest[]): string[] {
  366. const members = new Set(manifests.map(entry => entry.manifest.name).filter(name => name !== undefined))
  367. const errors: string[] = []
  368. for (const { dir, manifest } of manifests) {
  369. for (const section of dependencySections) {
  370. for (const [name, range] of Object.entries(manifest[section] ?? {})) {
  371. if (!members.has(name) || range.startsWith('workspace:')) continue
  372. errors.push(`${manifest.name ?? dir}: ${section}.${name} must use the workspace: protocol, got ${range}`)
  373. }
  374. }
  375. }
  376. return errors
  377. }
  378. const manifests = workspaceManifests()
  379. const errors = [
  380. ...checkRepositoryVersion(),
  381. ...manifests.flatMap(checkWorkspace),
  382. ...checkWorkspaceProtocol(manifests),
  383. ...checkHierarchyShape(),
  384. ...collectProjectReferenceFaceViolations(root),
  385. ]
  386. if (errors.length > 0) {
  387. console.error(errors.join('\n'))
  388. process.exitCode = 1
  389. }