publish-npm-baseline.ts 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /** Build, publish, and verify one commit-addressed npm workspace baseline. */
  2. import { spawnSync, type SpawnSyncReturns } from 'node:child_process'
  3. import { createHash } from 'node:crypto'
  4. import {
  5. existsSync,
  6. globSync,
  7. mkdirSync,
  8. mkdtempSync,
  9. readFileSync,
  10. realpathSync,
  11. readdirSync,
  12. rmSync,
  13. writeFileSync,
  14. } from 'node:fs'
  15. import { tmpdir } from 'node:os'
  16. import { basename, dirname, isAbsolute, join, normalize, relative, resolve, sep } from 'node:path'
  17. import { createInterface } from 'node:readline/promises'
  18. import { pathToFileURL } from 'node:url'
  19. import { parseArgs } from 'node:util'
  20. import { validateTarballPayload } from './publication-payload.ts'
  21. const DEFAULT_REGISTRY = 'https://registry.npm.harnessment.com'
  22. const DEFAULT_OUTPUT_DIRECTORY = '.artifacts/npm-baseline'
  23. const PACKAGE_PATTERNS = [
  24. 'vendor/*/package.json',
  25. 'packages/*/*/package.json',
  26. 'apps/*/package.json',
  27. ] as const
  28. const DEPENDENCY_SECTIONS = [
  29. 'dependencies',
  30. 'devDependencies',
  31. 'optionalDependencies',
  32. 'peerDependencies',
  33. ] as const
  34. const RELEASE_MANIFEST_NAME = 'manifest.json'
  35. const RELEASE_ENTRY_PACKAGE = '@deepseek-ai/dsh'
  36. const LATEST_DIST_TAG = 'latest'
  37. const POSIX_WEB_PROBE = String.raw`
  38. import errno, os, pty, select, signal, sys, time
  39. node, bin_path, cwd, timeout_seconds = sys.argv[1:]
  40. pid, fd = pty.fork()
  41. if pid == 0:
  42. os.chdir(cwd)
  43. os.execvpe(node, [node, bin_path, "web", "--host", "127.0.0.1", "--port", "0"], os.environ.copy())
  44. output = bytearray()
  45. ready_seen = False
  46. termination_sent = False
  47. deadline = time.monotonic() + float(timeout_seconds)
  48. status = None
  49. while time.monotonic() < deadline:
  50. ready, _, _ = select.select([fd], [], [], 0.05)
  51. if ready:
  52. try:
  53. chunk = os.read(fd, 65536)
  54. except OSError as error:
  55. if error.errno != errno.EIO:
  56. raise
  57. chunk = b""
  58. if chunk:
  59. output.extend(chunk)
  60. snapshot = bytes(output)
  61. if not termination_sent and b"dsh web: http://127.0.0.1:" in snapshot:
  62. ready_seen = True
  63. os.kill(pid, signal.SIGTERM)
  64. termination_sent = True
  65. waited, candidate = os.waitpid(pid, os.WNOHANG)
  66. if waited == pid:
  67. status = candidate
  68. break
  69. if status is None:
  70. os.kill(pid, signal.SIGKILL)
  71. _, status = os.waitpid(pid, 0)
  72. sys.stdout.buffer.write(output)
  73. if not ready_seen:
  74. sys.stderr.write("installed dsh web did not reach its ready URL\n")
  75. sys.exit(124)
  76. actual_exit = os.waitstatus_to_exitcode(status)
  77. if actual_exit != 0:
  78. sys.stderr.write(f"installed dsh web exited {actual_exit}, expected 0\n")
  79. sys.exit(125)
  80. `
  81. interface CommandResult {
  82. status: number
  83. stdout: string
  84. stderr: string
  85. }
  86. interface PackageTarget {
  87. name: string
  88. directory: string
  89. origin: PackageOrigin
  90. }
  91. type PackageOrigin = 'harness' | 'vendor'
  92. interface PackedPackage {
  93. name: string
  94. tarball: string
  95. sha256: string
  96. integrity: string
  97. origin: PackageOrigin
  98. }
  99. interface ReleaseManifest {
  100. schemaVersion: 1
  101. commit: string
  102. version: string
  103. distTag: string
  104. registry: string
  105. packages: PackedPackage[]
  106. }
  107. interface PackOptions {
  108. ref: string
  109. registry: string
  110. outputDirectory: string
  111. }
  112. /** Fixes the identity of one pack attempt before any expensive work begins. */
  113. class BaselinePackPlan {
  114. constructor(
  115. readonly commit: string,
  116. readonly shortCommit: string,
  117. readonly timestamp: string,
  118. readonly baseVersion: string,
  119. readonly version: string,
  120. readonly distTag: string,
  121. readonly registry: string,
  122. readonly artifactDirectory: string,
  123. ) {}
  124. async confirm(assumeYes: boolean): Promise<void> {
  125. console.log('publish-npm-baseline: planned pack')
  126. console.log(` commit: ${this.commit}`)
  127. console.log(` timestamp: ${this.timestamp} UTC`)
  128. console.log(` version: ${this.version}`)
  129. console.log(` dist-tag: ${this.distTag}`)
  130. console.log(` registry: ${this.registry}`)
  131. console.log(` output: ${this.artifactDirectory}`)
  132. if (assumeYes) return
  133. await confirmEnter(
  134. 'Press Enter to start packing or type anything to cancel: ',
  135. 'pack requires an interactive terminal or --yes',
  136. 'pack cancelled',
  137. )
  138. }
  139. }
  140. /** Runs child processes without involving a command shell. */
  141. class CommandRunner {
  142. run(
  143. command: string,
  144. args: string[],
  145. cwd: string,
  146. environment: NodeJS.ProcessEnv = process.env,
  147. ): void {
  148. const result = spawnSync(command, args, { cwd, env: environment, stdio: 'inherit' })
  149. if (result.error !== undefined) throw result.error
  150. if (result.status !== 0) {
  151. throw new Error(`${formatCommand(command, args)} exited with status ${String(result.status)}`)
  152. }
  153. }
  154. capture(
  155. command: string,
  156. args: string[],
  157. cwd: string,
  158. environment: NodeJS.ProcessEnv = process.env,
  159. ): string {
  160. const result = this.result(command, args, cwd, environment)
  161. if (result.status !== 0) throw commandFailure(command, args, result)
  162. return result.stdout.trim()
  163. }
  164. result(
  165. command: string,
  166. args: string[],
  167. cwd: string,
  168. environment: NodeJS.ProcessEnv = process.env,
  169. ): CommandResult {
  170. const result: SpawnSyncReturns<string> = spawnSync(command, args, {
  171. cwd,
  172. encoding: 'utf8',
  173. env: environment,
  174. maxBuffer: 16 * 1024 * 1024,
  175. })
  176. if (result.error !== undefined) throw result.error
  177. return {
  178. status: result.status ?? 1,
  179. stdout: result.stdout,
  180. stderr: result.stderr,
  181. }
  182. }
  183. }
  184. /** Owns a temporary detached worktree and removes it after staging. */
  185. class DetachedWorktree {
  186. private constructor(
  187. readonly path: string,
  188. private readonly temporaryRoot: string,
  189. private readonly repositoryRoot: string,
  190. private readonly runner: CommandRunner,
  191. ) {}
  192. static create(repositoryRoot: string, commit: string, runner: CommandRunner): DetachedWorktree {
  193. const temporaryRoot = mkdtempSync(join(tmpdir(), 'dsh-npm-baseline-'))
  194. const path = join(temporaryRoot, 'worktree')
  195. try {
  196. runner.run('git', ['worktree', 'add', '--detach', path, commit], repositoryRoot)
  197. return new DetachedWorktree(path, temporaryRoot, repositoryRoot, runner)
  198. } catch (error: unknown) {
  199. rmSync(temporaryRoot, { recursive: true, force: true })
  200. throw error
  201. }
  202. }
  203. dispose(): void {
  204. const result = this.runner.result(
  205. 'git',
  206. ['worktree', 'remove', '--force', this.path],
  207. this.repositoryRoot,
  208. )
  209. if (result.status !== 0) {
  210. console.error(`publish-npm-baseline: could not remove worktree ${this.path}`)
  211. if (result.stderr.trim() !== '') console.error(result.stderr.trim())
  212. }
  213. rmSync(this.temporaryRoot, { recursive: true, force: true })
  214. }
  215. }
  216. /** Discovers and stages every package published in one repository baseline. */
  217. class WorkspacePackageSet {
  218. private constructor(
  219. readonly packages: PackageTarget[],
  220. readonly baseVersion: string,
  221. ) {}
  222. static discover(root: string): WorkspacePackageSet {
  223. const manifestPaths = globSync(PACKAGE_PATTERNS, { cwd: root }).sort()
  224. if (manifestPaths.length === 0) {
  225. throw new Error('no package manifests found under vendor/, packages/, or apps/')
  226. }
  227. const packages: PackageTarget[] = []
  228. const names = new Set<string>()
  229. const baseVersion = expectString(readObject(resolve(root, 'package.json')), 'version', 'package.json')
  230. if (!/^\d+\.\d+\.\d+$/.test(baseVersion)) {
  231. throw new Error(`package.json must have a stable X.Y.Z version, got ${baseVersion}`)
  232. }
  233. for (const manifestPath of manifestPaths) {
  234. const manifest = readObject(resolve(root, manifestPath))
  235. const name = expectString(manifest, 'name', manifestPath)
  236. const version = expectString(manifest, 'version', manifestPath)
  237. const isVendored = manifestPath.startsWith('vendor/')
  238. if (!isVendored && !name.startsWith('@deepseek-ai/')) {
  239. throw new Error(`${manifestPath} must name an @deepseek-ai package`)
  240. }
  241. if (name === '@deepseek-ai/dsh-root') {
  242. throw new Error(`${manifestPath} unexpectedly selected the workspace root`)
  243. }
  244. if (names.has(name)) throw new Error(`duplicate package name: ${name}`)
  245. if (!isVendored && version !== baseVersion) {
  246. throw new Error(`${manifestPath} has version ${version}; expected ${baseVersion}`)
  247. }
  248. names.add(name)
  249. packages.push({
  250. name,
  251. directory: dirname(manifestPath),
  252. origin: isVendored ? 'vendor' : 'harness',
  253. })
  254. }
  255. packages.sort((left, right) => left.name.localeCompare(right.name))
  256. return new WorkspacePackageSet(packages, baseVersion)
  257. }
  258. stage(root: string, releaseVersion: string): void {
  259. const internalNames = new Set(this.packages.map(pkg => pkg.name))
  260. for (const target of this.packages) {
  261. const manifestPath = resolve(root, target.directory, 'package.json')
  262. const manifest = readObject(manifestPath)
  263. manifest.version = releaseVersion
  264. delete manifest.private
  265. stageInternalDependencies(manifest, internalNames, releaseVersion, manifestPath)
  266. writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`)
  267. }
  268. }
  269. }
  270. /** Immutable local release bundle consumed by publish and verify. */
  271. class ReleaseBundle {
  272. private constructor(
  273. readonly directory: string,
  274. readonly manifest: ReleaseManifest,
  275. ) {}
  276. static create(
  277. directory: string,
  278. expectedPackages: PackageTarget[],
  279. commit: string,
  280. version: string,
  281. distTag: string,
  282. registry: string,
  283. runner: CommandRunner,
  284. ): ReleaseBundle {
  285. const internalNames = new Set(expectedPackages.map(pkg => pkg.name))
  286. const expectedByName = new Map(expectedPackages.map(pkg => [pkg.name, pkg]))
  287. const missingNames = new Set(internalNames)
  288. const packages = readdirSync(directory)
  289. .filter(name => name.endsWith('.tgz'))
  290. .sort()
  291. .map((tarball) => {
  292. const artifact = inspectTarball(resolve(directory, tarball), runner)
  293. const expected = expectedByName.get(artifact.name)
  294. if (expected === undefined || !missingNames.delete(artifact.name)) {
  295. throw new Error(`unexpected or duplicate packed package: ${artifact.name}`)
  296. }
  297. if (expected.origin === 'harness') validateTarballPayload(artifact.files, tarball)
  298. if (artifact.version !== version) {
  299. throw new Error(`${tarball} has version ${artifact.version}; expected ${version}`)
  300. }
  301. if (artifact.private === true) throw new Error(`${tarball} is still private`)
  302. if (containsWorkspaceProtocol(artifact.manifest)) {
  303. throw new Error(`${tarball} still contains a workspace: dependency`)
  304. }
  305. validateInternalDependencyPins(artifact.manifest, internalNames, version, tarball)
  306. return packedPackage(artifact.name, resolve(directory, tarball), expected.origin)
  307. })
  308. .sort((left, right) => left.name.localeCompare(right.name))
  309. if (missingNames.size !== 0) {
  310. throw new Error(`missing tarballs for: ${[...missingNames].sort().join(', ')}`)
  311. }
  312. const manifest: ReleaseManifest = {
  313. schemaVersion: 1,
  314. commit,
  315. version,
  316. distTag,
  317. registry,
  318. packages,
  319. }
  320. writeFileSync(resolve(directory, RELEASE_MANIFEST_NAME), `${JSON.stringify(manifest, null, 2)}\n`)
  321. writeFileSync(
  322. resolve(directory, 'SHA256SUMS'),
  323. `${packages.map(pkg => `${pkg.sha256} ${pkg.tarball}`).join('\n')}\n`,
  324. )
  325. return new ReleaseBundle(directory, manifest)
  326. }
  327. static load(manifestPath: string, runner: CommandRunner): ReleaseBundle {
  328. const absoluteManifestPath = resolve(manifestPath)
  329. const raw = readObject(absoluteManifestPath)
  330. if (raw.schemaVersion !== 1) {
  331. throw new Error(`unsupported release manifest schema: ${String(raw.schemaVersion)}`)
  332. }
  333. const directory = dirname(absoluteManifestPath)
  334. const packageValues = raw.packages
  335. if (!Array.isArray(packageValues) || packageValues.length === 0) {
  336. throw new Error('release manifest contains no packages')
  337. }
  338. const packages = packageValues.map((value, index) => parsePackedPackage(value, index))
  339. const names = new Set<string>()
  340. for (const pkg of packages) {
  341. if (names.has(pkg.name)) throw new Error(`duplicate package in release manifest: ${pkg.name}`)
  342. names.add(pkg.name)
  343. }
  344. const manifest: ReleaseManifest = {
  345. schemaVersion: 1,
  346. commit: expectString(raw, 'commit', RELEASE_MANIFEST_NAME),
  347. version: expectString(raw, 'version', RELEASE_MANIFEST_NAME),
  348. distTag: expectString(raw, 'distTag', RELEASE_MANIFEST_NAME),
  349. registry: normalizeRegistry(expectString(raw, 'registry', RELEASE_MANIFEST_NAME)),
  350. packages,
  351. }
  352. const bundle = new ReleaseBundle(directory, manifest)
  353. bundle.verifyLocal(runner)
  354. return bundle
  355. }
  356. private verifyLocal(runner: CommandRunner): void {
  357. const internalNames = new Set(this.manifest.packages.map(pkg => pkg.name))
  358. for (const pkg of this.manifest.packages) {
  359. if (isAbsolute(pkg.tarball) || dirname(pkg.tarball) !== '.' || normalize(pkg.tarball) !== pkg.tarball) {
  360. throw new Error(`invalid tarball path for ${pkg.name}: ${pkg.tarball}`)
  361. }
  362. const path = resolve(this.directory, pkg.tarball)
  363. const actual = packedPackage(pkg.name, path, pkg.origin)
  364. if (actual.sha256 !== pkg.sha256 || actual.integrity !== pkg.integrity) {
  365. throw new Error(`tarball checksum mismatch: ${pkg.tarball}`)
  366. }
  367. const artifact = inspectTarball(path, runner)
  368. if (pkg.origin === 'harness') validateTarballPayload(artifact.files, pkg.tarball)
  369. if (artifact.name !== pkg.name || artifact.version !== this.manifest.version) {
  370. throw new Error(`tarball identity mismatch: ${pkg.tarball}`)
  371. }
  372. if (artifact.private === true) throw new Error(`${pkg.tarball} is still private`)
  373. if (containsWorkspaceProtocol(artifact.manifest)) {
  374. throw new Error(`${pkg.tarball} still contains a workspace: dependency`)
  375. }
  376. validateInternalDependencyPins(
  377. artifact.manifest,
  378. internalNames,
  379. this.manifest.version,
  380. pkg.tarball,
  381. )
  382. }
  383. }
  384. tarballPath(pkg: PackedPackage): string {
  385. return resolve(this.directory, pkg.tarball)
  386. }
  387. }
  388. /** Installs one complete bundle outside the workspace and probes the shipped dsh entry. */
  389. class InstalledBundleSmoke {
  390. constructor(
  391. private readonly bundle: ReleaseBundle,
  392. private readonly runner: CommandRunner,
  393. ) {}
  394. run(): void {
  395. const consumerRoot = mkdtempSync(join(tmpdir(), 'dsh-npm-consumer-'))
  396. try {
  397. const dependencies = Object.fromEntries(this.bundle.manifest.packages.map(pkg => [
  398. pkg.name,
  399. pathToFileURL(this.bundle.tarballPath(pkg)).href,
  400. ]))
  401. writeFileSync(resolve(consumerRoot, 'package.json'), `${JSON.stringify({
  402. name: 'dsh-npm-baseline-consumer',
  403. version: '0.0.0',
  404. private: true,
  405. dependencies,
  406. }, null, 2)}\n`)
  407. console.log(
  408. `publish-npm-baseline: installing ${this.bundle.manifest.packages.length} local tarballs`,
  409. )
  410. this.runner.run('npm', [
  411. 'install',
  412. '--no-audit',
  413. '--no-fund',
  414. '--package-lock=false',
  415. `--registry=${this.bundle.manifest.registry}`,
  416. ], consumerRoot, npmClientEnvironment())
  417. const bin = resolve(consumerRoot, 'node_modules/@deepseek-ai/dsh/lib/bin.js')
  418. assertPathWithin(consumerRoot, bin, 'installed dsh bin')
  419. const environment = installedArtifactEnvironment(consumerRoot)
  420. const version = this.runner.capture(
  421. process.execPath,
  422. [bin, '--version'],
  423. consumerRoot,
  424. environment,
  425. )
  426. if (version !== this.bundle.manifest.version) {
  427. throw new Error(
  428. `installed dsh --version returned ${JSON.stringify(version)}; `
  429. + `expected ${this.bundle.manifest.version}`,
  430. )
  431. }
  432. const config = this.runner.capture(
  433. process.execPath,
  434. [bin, '--dump-default-config'],
  435. consumerRoot,
  436. environment,
  437. )
  438. if (config === '') throw new Error('installed dsh --dump-default-config returned no output')
  439. this.probeWeb(bin, consumerRoot, environment)
  440. console.log('publish-npm-baseline: installed dsh entry and Web startup probes passed')
  441. } finally {
  442. rmSync(consumerRoot, { recursive: true, force: true })
  443. }
  444. }
  445. private probeWeb(bin: string, consumerRoot: string, environment: NodeJS.ProcessEnv): void {
  446. if (process.platform === 'win32') {
  447. throw new Error('installed dsh Web probe requires a POSIX host with python3')
  448. }
  449. const result = this.runner.result(
  450. 'python3',
  451. ['-c', POSIX_WEB_PROBE, process.execPath, bin, consumerRoot, '60'],
  452. consumerRoot,
  453. environment,
  454. )
  455. if (result.status !== 0) {
  456. throw commandFailure('python3', ['installed-dsh-web-probe'], result)
  457. }
  458. }
  459. }
  460. /** Builds a release bundle without mutating the caller's checkout. */
  461. class BaselinePackager {
  462. constructor(
  463. private readonly repositoryRoot: string,
  464. private readonly runner: CommandRunner,
  465. private readonly now: () => Date = () => new Date(),
  466. ) {}
  467. plan(options: PackOptions): BaselinePackPlan {
  468. const timestamp = formatUtcTimestamp(this.now())
  469. const registry = normalizeRegistry(options.registry)
  470. const commit = this.runner.capture(
  471. 'git',
  472. ['rev-parse', '--verify', `${options.ref}^{commit}`],
  473. this.repositoryRoot,
  474. )
  475. const shortCommit = this.runner.capture(
  476. 'git',
  477. ['rev-parse', '--short=10', commit],
  478. this.repositoryRoot,
  479. )
  480. const rootManifest = parseObject(
  481. this.runner.capture('git', ['show', `${commit}:package.json`], this.repositoryRoot),
  482. `${commit}:package.json`,
  483. )
  484. const baseVersion = expectString(rootManifest, 'version', `${commit}:package.json`)
  485. validateBaseVersion(baseVersion, `${commit}:package.json`)
  486. const version = `${baseVersion}-${timestamp}-${shortCommit}`
  487. const distTag = `dev-${baseVersion}`
  488. validateDistTag(distTag)
  489. const artifactDirectory = resolve(options.outputDirectory, version)
  490. if (existsSync(artifactDirectory)) {
  491. throw new Error(`output already exists: ${artifactDirectory}`)
  492. }
  493. return new BaselinePackPlan(
  494. commit,
  495. shortCommit,
  496. timestamp,
  497. baseVersion,
  498. version,
  499. distTag,
  500. registry,
  501. artifactDirectory,
  502. )
  503. }
  504. pack(plan: BaselinePackPlan): ReleaseBundle {
  505. const { artifactDirectory } = plan
  506. if (existsSync(artifactDirectory)) {
  507. throw new Error(`output already exists: ${artifactDirectory}`)
  508. }
  509. const worktree = DetachedWorktree.create(this.repositoryRoot, plan.commit, this.runner)
  510. let createdArtifactDirectory = false
  511. try {
  512. const packageSet = WorkspacePackageSet.discover(worktree.path)
  513. if (packageSet.baseVersion !== plan.baseVersion) {
  514. throw new Error(
  515. `workspace package version ${packageSet.baseVersion} does not match root version `
  516. + `${plan.baseVersion} at ${plan.commit}`,
  517. )
  518. }
  519. console.log(`publish-npm-baseline: installing detached worktree ${plan.shortCommit}`)
  520. this.runner.run('pnpm', ['install', '--frozen-lockfile'], worktree.path)
  521. this.runner.run('pnpm', ['run', 'constraints'], worktree.path)
  522. packageSet.stage(worktree.path, plan.version)
  523. mkdirSync(artifactDirectory, { recursive: true })
  524. createdArtifactDirectory = true
  525. console.log(
  526. `publish-npm-baseline: building ${packageSet.packages.length} packages as ${plan.version}`,
  527. )
  528. this.runner.run('pnpm', ['run', 'build'], worktree.path)
  529. this.runner.run('pnpm', ['run', 'publint'], worktree.path)
  530. this.runner.run('pnpm', ['run', 'verify-built-package-invariants'], worktree.path)
  531. this.runner.run('pnpm', [
  532. '--filter', './vendor/**',
  533. '--filter', './packages/**',
  534. '--filter', './apps/**',
  535. '--recursive',
  536. 'pack',
  537. '--pack-destination', artifactDirectory,
  538. ], worktree.path)
  539. const bundle = ReleaseBundle.create(
  540. artifactDirectory,
  541. packageSet.packages,
  542. plan.commit,
  543. plan.version,
  544. plan.distTag,
  545. plan.registry,
  546. this.runner,
  547. )
  548. new InstalledBundleSmoke(bundle, this.runner).run()
  549. createdArtifactDirectory = false
  550. console.log(`publish-npm-baseline: packed ${bundle.manifest.packages.length} packages`)
  551. console.log(` version: ${bundle.manifest.version}`)
  552. console.log(` dist-tag: ${bundle.manifest.distTag}`)
  553. console.log(` manifest: ${resolve(bundle.directory, RELEASE_MANIFEST_NAME)}`)
  554. console.log(' publish: ' + formatCopyableCommand('pnpm', [
  555. '--dir',
  556. this.repositoryRoot,
  557. 'exec',
  558. 'tsx',
  559. resolve(this.repositoryRoot, 'scripts/publish-npm-baseline.ts'),
  560. 'publish',
  561. '--manifest',
  562. resolve(bundle.directory, RELEASE_MANIFEST_NAME),
  563. '--yes',
  564. ]))
  565. return bundle
  566. } finally {
  567. worktree.dispose()
  568. if (createdArtifactDirectory) {
  569. rmSync(artifactDirectory, { recursive: true, force: true })
  570. }
  571. }
  572. }
  573. }
  574. /** Publishes and verifies a release bundle against its recorded registry. */
  575. class RegistryPublication {
  576. private readonly npmEnvironment = npmClientEnvironment()
  577. private readonly npmWorkingDirectory = tmpdir()
  578. constructor(
  579. private readonly bundle: ReleaseBundle,
  580. private readonly runner: CommandRunner,
  581. ) {}
  582. async publish(assumeYes: boolean): Promise<void> {
  583. this.pingRegistry()
  584. this.requireIdentity()
  585. if (!assumeYes) await this.confirm()
  586. for (const pkg of this.bundle.manifest.packages) {
  587. const existingIntegrity = this.remoteIntegrity(pkg.name)
  588. if (existingIntegrity === undefined) {
  589. this.runner.run('npm', [
  590. 'publish',
  591. this.bundle.tarballPath(pkg),
  592. `--registry=${this.bundle.manifest.registry}`,
  593. `--tag=${this.bundle.manifest.distTag}`,
  594. ], this.npmWorkingDirectory, this.npmEnvironment)
  595. } else {
  596. if (existingIntegrity !== pkg.integrity) {
  597. throw new Error(
  598. `${pkg.name}@${this.bundle.manifest.version} already exists with different integrity`,
  599. )
  600. }
  601. console.log(
  602. `publish-npm-baseline: already published ${pkg.name}@${this.bundle.manifest.version}`,
  603. )
  604. }
  605. this.ensureDistTag(pkg.name, this.bundle.manifest.distTag)
  606. }
  607. this.ensureDistTag(RELEASE_ENTRY_PACKAGE, LATEST_DIST_TAG)
  608. this.verifyRemote()
  609. this.verifyReleaseEntryDistTag()
  610. }
  611. verify(): void {
  612. this.pingRegistry()
  613. this.verifyRemote()
  614. this.verifyReleaseEntryDistTag()
  615. }
  616. private verifyRemote(): void {
  617. for (const pkg of this.bundle.manifest.packages) {
  618. const integrity = this.remoteIntegrity(pkg.name)
  619. if (integrity === undefined) {
  620. throw new Error(`package is missing: ${pkg.name}@${this.bundle.manifest.version}`)
  621. }
  622. if (integrity !== pkg.integrity) {
  623. throw new Error(`integrity mismatch: ${pkg.name}@${this.bundle.manifest.version}`)
  624. }
  625. const tagVersion = this.remoteDistTag(pkg.name, this.bundle.manifest.distTag)
  626. if (tagVersion !== this.bundle.manifest.version) {
  627. throw new Error(
  628. `${pkg.name}@${this.bundle.manifest.distTag} points to ${tagVersion ?? '<missing>'}; `
  629. + `expected ${this.bundle.manifest.version}`,
  630. )
  631. }
  632. console.log(`publish-npm-baseline: verified ${pkg.name}@${this.bundle.manifest.version}`)
  633. }
  634. console.log(
  635. `publish-npm-baseline: verified ${this.bundle.manifest.packages.length} packages and `
  636. + `dist-tag ${this.bundle.manifest.distTag}`,
  637. )
  638. }
  639. private verifyReleaseEntryDistTag(): void {
  640. const tagVersion = this.remoteDistTag(RELEASE_ENTRY_PACKAGE, LATEST_DIST_TAG)
  641. if (tagVersion !== this.bundle.manifest.version) {
  642. throw new Error(
  643. `${RELEASE_ENTRY_PACKAGE}@${LATEST_DIST_TAG} points to ${tagVersion ?? '<missing>'}; `
  644. + `expected ${this.bundle.manifest.version}`,
  645. )
  646. }
  647. console.log(
  648. `publish-npm-baseline: verified ${RELEASE_ENTRY_PACKAGE}@${LATEST_DIST_TAG} at `
  649. + this.bundle.manifest.version,
  650. )
  651. }
  652. private pingRegistry(): void {
  653. const { registry } = this.bundle.manifest
  654. this.runner.capture(
  655. 'npm', ['ping', `--registry=${registry}`], this.npmWorkingDirectory, this.npmEnvironment,
  656. )
  657. }
  658. private requireIdentity(): void {
  659. const { registry } = this.bundle.manifest
  660. const identity = this.runner.capture(
  661. 'npm', ['whoami', `--registry=${registry}`], this.npmWorkingDirectory, this.npmEnvironment,
  662. )
  663. console.log(`publish-npm-baseline: registry identity ${identity} at ${registry}`)
  664. }
  665. private async confirm(): Promise<void> {
  666. await confirmEnter(
  667. `Publish ${this.bundle.manifest.packages.length} packages as `
  668. + `${this.bundle.manifest.version} to ${this.bundle.manifest.registry}? `
  669. + 'Press Enter to continue or type anything to cancel: ',
  670. 'publish requires an interactive terminal or --yes',
  671. 'publication cancelled',
  672. )
  673. }
  674. private remoteIntegrity(name: string): string | undefined {
  675. const { registry, version } = this.bundle.manifest
  676. const result = this.runner.result(
  677. 'npm',
  678. ['view', `${name}@${version}`, 'dist.integrity', '--json', `--registry=${registry}`],
  679. this.npmWorkingDirectory,
  680. this.npmEnvironment,
  681. )
  682. if (result.status !== 0) {
  683. if (/E404|NOT_FOUND|404 Not Found/.test(`${result.stdout}\n${result.stderr}`)) return undefined
  684. throw commandFailure('npm', ['view', `${name}@${version}`], result)
  685. }
  686. const value: unknown = result.stdout.trim() === '' ? undefined : JSON.parse(result.stdout)
  687. if (typeof value !== 'string' || !value.startsWith('sha512-')) {
  688. throw new Error(`registry returned no integrity for ${name}@${version}`)
  689. }
  690. return value
  691. }
  692. private remoteDistTag(name: string, distTag: string): string | undefined {
  693. const { registry } = this.bundle.manifest
  694. const raw = this.runner.capture(
  695. 'npm',
  696. ['dist-tag', 'ls', name, `--registry=${registry}`],
  697. this.npmWorkingDirectory,
  698. this.npmEnvironment,
  699. )
  700. return parseDistTagListing(raw, name).get(distTag)
  701. }
  702. private ensureDistTag(name: string, distTag: string): void {
  703. if (this.remoteDistTag(name, distTag) === this.bundle.manifest.version) return
  704. const { registry, version } = this.bundle.manifest
  705. this.runner.run(
  706. 'npm',
  707. ['dist-tag', 'add', `${name}@${version}`, distTag, `--registry=${registry}`],
  708. this.npmWorkingDirectory,
  709. this.npmEnvironment,
  710. )
  711. }
  712. }
  713. interface InspectedTarball {
  714. name: string
  715. version: string
  716. private: unknown
  717. manifest: Record<string, unknown>
  718. files: string[]
  719. }
  720. function inspectTarball(path: string, runner: CommandRunner): InspectedTarball {
  721. const manifest = JSON.parse(
  722. runner.capture('tar', ['-xOf', path, 'package/package.json'], dirname(path)),
  723. ) as unknown
  724. if (!isRecord(manifest)) throw new Error(`${path} contains an invalid package.json`)
  725. return {
  726. name: expectString(manifest, 'name', path),
  727. version: expectString(manifest, 'version', path),
  728. private: manifest.private,
  729. manifest,
  730. files: runner.capture('tar', ['-tf', path], dirname(path)).split(/\r?\n/),
  731. }
  732. }
  733. function packedPackage(name: string, path: string, origin: PackageOrigin): PackedPackage {
  734. const bytes = readFileSync(path)
  735. return {
  736. name,
  737. tarball: basename(path),
  738. sha256: createHash('sha256').update(bytes).digest('hex'),
  739. integrity: `sha512-${createHash('sha512').update(bytes).digest('base64')}`,
  740. origin,
  741. }
  742. }
  743. function parsePackedPackage(value: unknown, index: number): PackedPackage {
  744. if (!isRecord(value)) throw new Error(`invalid release manifest package at index ${index}`)
  745. const context = `release manifest package at index ${index}`
  746. const name = expectString(value, 'name', context)
  747. const origin = value.origin === undefined ? 'harness' : value.origin
  748. if (origin !== 'harness' && origin !== 'vendor') {
  749. throw new Error(`invalid package origin in release manifest: ${JSON.stringify(origin)}`)
  750. }
  751. if (origin === 'harness' && (!name.startsWith('@deepseek-ai/') || name === '@deepseek-ai/dsh-root')) {
  752. throw new Error(`invalid package name in release manifest: ${name}`)
  753. }
  754. return {
  755. name,
  756. tarball: expectString(value, 'tarball', context),
  757. sha256: expectString(value, 'sha256', context),
  758. integrity: expectString(value, 'integrity', context),
  759. origin,
  760. }
  761. }
  762. function containsWorkspaceProtocol(value: unknown): boolean {
  763. if (typeof value === 'string') return value.startsWith('workspace:')
  764. if (Array.isArray(value)) return value.some(containsWorkspaceProtocol)
  765. return isRecord(value) && Object.values(value).some(containsWorkspaceProtocol)
  766. }
  767. function stageInternalDependencies(
  768. manifest: Record<string, unknown>,
  769. internalNames: ReadonlySet<string>,
  770. releaseVersion: string,
  771. context: string,
  772. ): void {
  773. for (const { dependencies, name } of internalDependencyEntries(manifest, internalNames, context)) {
  774. dependencies[name] = releaseVersion
  775. }
  776. }
  777. function validateInternalDependencyPins(
  778. manifest: Record<string, unknown>,
  779. internalNames: ReadonlySet<string>,
  780. releaseVersion: string,
  781. context: string,
  782. ): void {
  783. for (const { section, name, range } of internalDependencyEntries(manifest, internalNames, context)) {
  784. if (range !== releaseVersion) {
  785. throw new Error(
  786. `${context} has internal ${section} ${name}@${String(range)}; `
  787. + `expected exact version ${releaseVersion}`,
  788. )
  789. }
  790. }
  791. }
  792. function* internalDependencyEntries(
  793. manifest: Record<string, unknown>,
  794. internalNames: ReadonlySet<string>,
  795. context: string,
  796. ): Generator<{
  797. section: typeof DEPENDENCY_SECTIONS[number]
  798. dependencies: Record<string, unknown>
  799. name: string
  800. range: unknown
  801. }> {
  802. for (const section of DEPENDENCY_SECTIONS) {
  803. const dependencies = manifest[section]
  804. if (dependencies === undefined) continue
  805. if (!isRecord(dependencies)) throw new Error(`${context} ${section} must be an object`)
  806. for (const [name, range] of Object.entries(dependencies)) {
  807. if (!internalNames.has(name)) continue
  808. yield { section, dependencies, name, range }
  809. }
  810. }
  811. }
  812. function readObject(path: string): Record<string, unknown> {
  813. return parseObject(readFileSync(path, 'utf8'), path)
  814. }
  815. function parseObject(source: string, context: string): Record<string, unknown> {
  816. const value: unknown = JSON.parse(source)
  817. if (!isRecord(value)) throw new Error(`${context} must contain a JSON object`)
  818. return value
  819. }
  820. function isRecord(value: unknown): value is Record<string, unknown> {
  821. return value !== null && typeof value === 'object' && !Array.isArray(value)
  822. }
  823. function expectString(value: Record<string, unknown>, key: string, context: string): string {
  824. const result = value[key]
  825. if (typeof result !== 'string' || result === '') {
  826. throw new Error(`${context} must contain a non-empty ${key}`)
  827. }
  828. return result
  829. }
  830. function normalizeRegistry(value: string): string {
  831. const url = new URL(value)
  832. if (url.protocol !== 'http:' && url.protocol !== 'https:') {
  833. throw new Error(`registry must use HTTP or HTTPS: ${value}`)
  834. }
  835. return value.replace(/\/+$/, '')
  836. }
  837. function npmClientEnvironment(): NodeJS.ProcessEnv {
  838. const environment = { ...process.env }
  839. delete environment.npm_config_user_agent
  840. delete environment.NPM_CONFIG_USER_AGENT
  841. return environment
  842. }
  843. function installedArtifactEnvironment(consumerRoot: string): NodeJS.ProcessEnv {
  844. const environment = npmClientEnvironment()
  845. delete environment.NODE_OPTIONS
  846. delete environment.NODE_PATH
  847. environment.DSH_HOME = resolve(consumerRoot, '.dsh')
  848. environment.DSH_AGENTS_HOME = resolve(consumerRoot, '.agents')
  849. environment.DSH_TELEMETRY_DISABLED = '1'
  850. environment.DEEPSEEK_API_KEY = 'keyless-installed-web-no-call'
  851. environment.LANG = 'en_US.UTF-8'
  852. environment.LC_ALL = 'en_US.UTF-8'
  853. environment.LC_CTYPE = 'en_US.UTF-8'
  854. environment.TERM = 'xterm-256color'
  855. environment.COLUMNS = '100'
  856. environment.LINES = '30'
  857. delete environment.COLORTERM
  858. return environment
  859. }
  860. function assertPathWithin(root: string, path: string, label: string): void {
  861. const rootPath = realpathSync.native(root)
  862. const candidate = realpathSync.native(path)
  863. const fromRoot = relative(rootPath, candidate)
  864. if (fromRoot === '..' || fromRoot.startsWith(`..${sep}`) || isAbsolute(fromRoot)) {
  865. throw new Error(`${label} resolved outside the isolated consumer: ${candidate}`)
  866. }
  867. }
  868. function validateDistTag(value: string): void {
  869. if (value === '' || /\s/.test(value)) throw new Error(`invalid dist-tag: ${JSON.stringify(value)}`)
  870. }
  871. function validateBaseVersion(value: string, context: string): void {
  872. if (!/^\d+\.\d+\.\d+$/.test(value)) {
  873. throw new Error(`${context} must have a stable X.Y.Z version, got ${value}`)
  874. }
  875. }
  876. function parseDistTagListing(raw: string, name: string): Map<string, string> {
  877. const tags = new Map<string, string>()
  878. for (const line of raw.split(/\r?\n/)) {
  879. if (line === '') continue
  880. const separator = line.indexOf(': ')
  881. if (separator <= 0 || separator + 2 === line.length) {
  882. throw new Error(`registry returned an invalid dist-tag for ${name}: ${line}`)
  883. }
  884. const tag = line.slice(0, separator)
  885. if (tags.has(tag)) throw new Error(`registry returned duplicate dist-tag ${tag} for ${name}`)
  886. tags.set(tag, line.slice(separator + 2))
  887. }
  888. return tags
  889. }
  890. async function confirmEnter(
  891. prompt: string,
  892. nonInteractiveError: string,
  893. cancellationError: string,
  894. ): Promise<void> {
  895. if (!process.stdin.isTTY || !process.stdout.isTTY) throw new Error(nonInteractiveError)
  896. const readline = createInterface({ input: process.stdin, output: process.stdout })
  897. try {
  898. const answer = await readline.question(prompt)
  899. if (answer !== '') throw new Error(cancellationError)
  900. } finally {
  901. readline.close()
  902. }
  903. }
  904. function formatUtcTimestamp(value: Date): string {
  905. if (!Number.isFinite(value.getTime())) throw new Error('pack timestamp must be a valid date')
  906. return value.toISOString().replaceAll(/[-:TZ.]/g, '').slice(0, 14)
  907. }
  908. function commandFailure(command: string, args: string[], result: CommandResult): Error {
  909. const detail = [result.stdout.trim(), result.stderr.trim()].filter(Boolean).join('\n')
  910. return new Error(
  911. `${formatCommand(command, args)} exited with status ${result.status}${detail === '' ? '' : `\n${detail}`}`,
  912. )
  913. }
  914. function formatCommand(command: string, args: string[]): string {
  915. return [command, ...args].map(value => JSON.stringify(value)).join(' ')
  916. }
  917. function formatCopyableCommand(command: string, args: string[]): string {
  918. return [command, ...args].map(quoteShellArgument).join(' ')
  919. }
  920. function quoteShellArgument(value: string): string {
  921. if (/^[\w./:@=+-]+$/.test(value)) return value
  922. const singleQuote = String.fromCodePoint(39)
  923. const escapedSingleQuote = `${singleQuote}"${singleQuote}"${singleQuote}`
  924. return `${singleQuote}${value.replaceAll(singleQuote, escapedSingleQuote)}${singleQuote}`
  925. }
  926. function printUsage(): void {
  927. console.log(`Usage:
  928. pnpm exec tsx scripts/publish-npm-baseline.ts pack [options]
  929. pnpm exec tsx scripts/publish-npm-baseline.ts release [options] [--yes]
  930. pnpm exec tsx scripts/publish-npm-baseline.ts publish --manifest <path> [--yes]
  931. pnpm exec tsx scripts/publish-npm-baseline.ts verify --manifest <path>
  932. Pack/release options:
  933. --ref <git-ref> Git commit to stage (default: HEAD)
  934. --registry <url> npm registry (default: ${DEFAULT_REGISTRY})
  935. --output-dir <path> Artifact root (default: ${DEFAULT_OUTPUT_DIRECTORY})
  936. --yes pack/release without waiting for Enter`)
  937. }
  938. async function main(): Promise<void> {
  939. const command = process.argv[2]
  940. if (command === undefined || command === 'help' || command === '--help' || command === '-h') {
  941. printUsage()
  942. return
  943. }
  944. if (process.argv.slice(3).some(value => value === '--help' || value === '-h')) {
  945. printUsage()
  946. return
  947. }
  948. const runner = new CommandRunner()
  949. const repositoryRoot = runner.capture('git', ['rev-parse', '--show-toplevel'], process.cwd())
  950. if (command === 'pack' || command === 'release') {
  951. const { values } = parseArgs({
  952. args: process.argv.slice(3),
  953. options: {
  954. ref: { type: 'string', default: 'HEAD' },
  955. registry: { type: 'string', default: DEFAULT_REGISTRY },
  956. 'output-dir': { type: 'string', default: resolve(repositoryRoot, DEFAULT_OUTPUT_DIRECTORY) },
  957. yes: { type: 'boolean', default: false },
  958. },
  959. strict: true,
  960. })
  961. const packager = new BaselinePackager(repositoryRoot, runner)
  962. const plan = packager.plan({
  963. ref: values.ref,
  964. registry: values.registry,
  965. outputDirectory: resolve(values['output-dir']),
  966. })
  967. await plan.confirm(values.yes)
  968. const bundle = packager.pack(plan)
  969. if (command === 'release') {
  970. await new RegistryPublication(bundle, runner).publish(values.yes)
  971. }
  972. return
  973. }
  974. if (command === 'publish' || command === 'verify') {
  975. const { values } = parseArgs({
  976. args: process.argv.slice(3),
  977. options: {
  978. manifest: { type: 'string' },
  979. yes: { type: 'boolean', default: false },
  980. },
  981. strict: true,
  982. })
  983. if (values.manifest === undefined) throw new Error(`${command} requires --manifest`)
  984. if (command === 'verify' && values.yes) throw new Error('verify does not accept --yes')
  985. const bundle = ReleaseBundle.load(values.manifest, runner)
  986. const publication = new RegistryPublication(bundle, runner)
  987. if (command === 'publish') await publication.publish(values.yes)
  988. else publication.verify()
  989. return
  990. }
  991. throw new Error(`unknown command: ${command}`)
  992. }
  993. try {
  994. await main()
  995. } catch (error: unknown) {
  996. console.error(`publish-npm-baseline: ${error instanceof Error ? error.message : String(error)}`)
  997. process.exitCode = 1
  998. }