publish-npm-baseline.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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/!(experimental)/*/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", "--no-open", "--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. // Vendored packages are rescoped too (vendor/README.md), so publication
  239. // never carries an upstream name that would squat it on the registry.
  240. if (!name.startsWith('@deepseek-ai/')) {
  241. throw new Error(`${manifestPath} must name an @deepseek-ai package`)
  242. }
  243. if (name === '@deepseek-ai/dsh-root') {
  244. throw new Error(`${manifestPath} unexpectedly selected the workspace root`)
  245. }
  246. if (names.has(name)) throw new Error(`duplicate package name: ${name}`)
  247. if (!isVendored && version !== baseVersion) {
  248. throw new Error(`${manifestPath} has version ${version}; expected ${baseVersion}`)
  249. }
  250. names.add(name)
  251. packages.push({
  252. name,
  253. directory: dirname(manifestPath),
  254. origin: isVendored ? 'vendor' : 'harness',
  255. })
  256. }
  257. packages.sort((left, right) => left.name.localeCompare(right.name))
  258. return new WorkspacePackageSet(packages, baseVersion)
  259. }
  260. stage(root: string, releaseVersion: string): void {
  261. const internalNames = new Set(this.packages.map(pkg => pkg.name))
  262. for (const target of this.packages) {
  263. const manifestPath = resolve(root, target.directory, 'package.json')
  264. const manifest = readObject(manifestPath)
  265. manifest.version = releaseVersion
  266. delete manifest.private
  267. stageInternalDependencies(manifest, internalNames, releaseVersion, manifestPath)
  268. writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`)
  269. }
  270. }
  271. }
  272. /** Immutable local release bundle consumed by publish and verify. */
  273. class ReleaseBundle {
  274. private constructor(
  275. readonly directory: string,
  276. readonly manifest: ReleaseManifest,
  277. ) {}
  278. static create(
  279. directory: string,
  280. expectedPackages: PackageTarget[],
  281. commit: string,
  282. version: string,
  283. distTag: string,
  284. registry: string,
  285. runner: CommandRunner,
  286. ): ReleaseBundle {
  287. const internalNames = new Set(expectedPackages.map(pkg => pkg.name))
  288. const expectedByName = new Map(expectedPackages.map(pkg => [pkg.name, pkg]))
  289. const missingNames = new Set(internalNames)
  290. const packages = readdirSync(directory)
  291. .filter(name => name.endsWith('.tgz'))
  292. .sort()
  293. .map((tarball) => {
  294. const artifact = inspectTarball(resolve(directory, tarball), runner)
  295. const expected = expectedByName.get(artifact.name)
  296. if (expected === undefined || !missingNames.delete(artifact.name)) {
  297. throw new Error(`unexpected or duplicate packed package: ${artifact.name}`)
  298. }
  299. if (expected.origin === 'harness') {
  300. validateTarballPayload(artifact.files, tarball)
  301. }
  302. if (artifact.version !== version) {
  303. throw new Error(`${tarball} has version ${artifact.version}; expected ${version}`)
  304. }
  305. if (artifact.private === true) throw new Error(`${tarball} is still private`)
  306. if (containsWorkspaceProtocol(artifact.manifest)) {
  307. throw new Error(`${tarball} still contains a workspace: dependency`)
  308. }
  309. validateInternalDependencyPins(artifact.manifest, internalNames, version, tarball)
  310. return packedPackage(artifact.name, resolve(directory, tarball), expected.origin)
  311. })
  312. .sort((left, right) => left.name.localeCompare(right.name))
  313. if (missingNames.size !== 0) {
  314. throw new Error(`missing tarballs for: ${[...missingNames].sort().join(', ')}`)
  315. }
  316. const manifest: ReleaseManifest = {
  317. schemaVersion: 1,
  318. commit,
  319. version,
  320. distTag,
  321. registry,
  322. packages,
  323. }
  324. writeFileSync(resolve(directory, RELEASE_MANIFEST_NAME), `${JSON.stringify(manifest, null, 2)}\n`)
  325. writeFileSync(
  326. resolve(directory, 'SHA256SUMS'),
  327. `${packages.map(pkg => `${pkg.sha256} ${pkg.tarball}`).join('\n')}\n`,
  328. )
  329. return new ReleaseBundle(directory, manifest)
  330. }
  331. static load(manifestPath: string, runner: CommandRunner): ReleaseBundle {
  332. const absoluteManifestPath = resolve(manifestPath)
  333. const raw = readObject(absoluteManifestPath)
  334. if (raw.schemaVersion !== 1) {
  335. throw new Error(`unsupported release manifest schema: ${String(raw.schemaVersion)}`)
  336. }
  337. const directory = dirname(absoluteManifestPath)
  338. const packageValues = raw.packages
  339. if (!Array.isArray(packageValues) || packageValues.length === 0) {
  340. throw new Error('release manifest contains no packages')
  341. }
  342. const packages = packageValues.map((value, index) => parsePackedPackage(value, index))
  343. const names = new Set<string>()
  344. for (const pkg of packages) {
  345. if (names.has(pkg.name)) throw new Error(`duplicate package in release manifest: ${pkg.name}`)
  346. names.add(pkg.name)
  347. }
  348. const manifest: ReleaseManifest = {
  349. schemaVersion: 1,
  350. commit: expectString(raw, 'commit', RELEASE_MANIFEST_NAME),
  351. version: expectString(raw, 'version', RELEASE_MANIFEST_NAME),
  352. distTag: expectString(raw, 'distTag', RELEASE_MANIFEST_NAME),
  353. registry: normalizeRegistry(expectString(raw, 'registry', RELEASE_MANIFEST_NAME)),
  354. packages,
  355. }
  356. const bundle = new ReleaseBundle(directory, manifest)
  357. bundle.verifyLocal(runner)
  358. return bundle
  359. }
  360. private verifyLocal(runner: CommandRunner): void {
  361. const internalNames = new Set(this.manifest.packages.map(pkg => pkg.name))
  362. for (const pkg of this.manifest.packages) {
  363. if (isAbsolute(pkg.tarball) || dirname(pkg.tarball) !== '.' || normalize(pkg.tarball) !== pkg.tarball) {
  364. throw new Error(`invalid tarball path for ${pkg.name}: ${pkg.tarball}`)
  365. }
  366. const path = resolve(this.directory, pkg.tarball)
  367. const actual = packedPackage(pkg.name, path, pkg.origin)
  368. if (actual.sha256 !== pkg.sha256 || actual.integrity !== pkg.integrity) {
  369. throw new Error(`tarball checksum mismatch: ${pkg.tarball}`)
  370. }
  371. const artifact = inspectTarball(path, runner)
  372. if (pkg.origin === 'harness') {
  373. validateTarballPayload(artifact.files, pkg.tarball)
  374. }
  375. if (artifact.name !== pkg.name || artifact.version !== this.manifest.version) {
  376. throw new Error(`tarball identity mismatch: ${pkg.tarball}`)
  377. }
  378. if (artifact.private === true) throw new Error(`${pkg.tarball} is still private`)
  379. if (containsWorkspaceProtocol(artifact.manifest)) {
  380. throw new Error(`${pkg.tarball} still contains a workspace: dependency`)
  381. }
  382. validateInternalDependencyPins(
  383. artifact.manifest,
  384. internalNames,
  385. this.manifest.version,
  386. pkg.tarball,
  387. )
  388. }
  389. }
  390. tarballPath(pkg: PackedPackage): string {
  391. return resolve(this.directory, pkg.tarball)
  392. }
  393. }
  394. /** Installs one complete bundle outside the workspace and probes the shipped dsh entry. */
  395. class InstalledBundleSmoke {
  396. constructor(
  397. private readonly bundle: ReleaseBundle,
  398. private readonly runner: CommandRunner,
  399. ) {}
  400. run(): void {
  401. const consumerRoot = mkdtempSync(join(tmpdir(), 'dsh-npm-consumer-'))
  402. try {
  403. const dependencies = Object.fromEntries(this.bundle.manifest.packages.map(pkg => [
  404. pkg.name,
  405. pathToFileURL(this.bundle.tarballPath(pkg)).href,
  406. ]))
  407. writeFileSync(resolve(consumerRoot, 'package.json'), `${JSON.stringify({
  408. name: 'dsh-npm-baseline-consumer',
  409. version: '0.0.0',
  410. private: true,
  411. dependencies,
  412. }, null, 2)}\n`)
  413. console.log(
  414. `publish-npm-baseline: installing ${this.bundle.manifest.packages.length} local tarballs`,
  415. )
  416. this.runner.run('npm', [
  417. 'install',
  418. '--no-audit',
  419. '--no-fund',
  420. '--package-lock=false',
  421. `--registry=${this.bundle.manifest.registry}`,
  422. ], consumerRoot, npmClientEnvironment())
  423. const bin = resolve(consumerRoot, 'node_modules/@deepseek-ai/dsh/lib/bin.js')
  424. assertPathWithin(consumerRoot, bin, 'installed dsh bin')
  425. const environment = installedArtifactEnvironment(consumerRoot)
  426. const version = this.runner.capture(
  427. process.execPath,
  428. [bin, '--version'],
  429. consumerRoot,
  430. environment,
  431. )
  432. if (version !== this.bundle.manifest.version) {
  433. throw new Error(
  434. `installed dsh --version returned ${JSON.stringify(version)}; `
  435. + `expected ${this.bundle.manifest.version}`,
  436. )
  437. }
  438. this.probeWeb(bin, consumerRoot, environment)
  439. console.log('publish-npm-baseline: installed dsh entry and Web startup probes passed')
  440. } finally {
  441. rmSync(consumerRoot, { recursive: true, force: true })
  442. }
  443. }
  444. private probeWeb(bin: string, consumerRoot: string, environment: NodeJS.ProcessEnv): void {
  445. if (process.platform === 'win32') {
  446. throw new Error('installed dsh Web probe requires a POSIX host with python3')
  447. }
  448. const result = this.runner.result(
  449. 'python3',
  450. ['-c', POSIX_WEB_PROBE, process.execPath, bin, consumerRoot, '60'],
  451. consumerRoot,
  452. environment,
  453. )
  454. if (result.status !== 0) {
  455. throw commandFailure('python3', ['installed-dsh-web-probe'], result)
  456. }
  457. }
  458. }
  459. /** Builds a release bundle without mutating the caller's checkout. */
  460. class BaselinePackager {
  461. constructor(
  462. private readonly repositoryRoot: string,
  463. private readonly runner: CommandRunner,
  464. private readonly now: () => Date = () => new Date(),
  465. ) {}
  466. plan(options: PackOptions): BaselinePackPlan {
  467. const timestamp = formatUtcTimestamp(this.now())
  468. const registry = normalizeRegistry(options.registry)
  469. const commit = this.runner.capture(
  470. 'git',
  471. ['rev-parse', '--verify', `${options.ref}^{commit}`],
  472. this.repositoryRoot,
  473. )
  474. const shortCommit = this.runner.capture(
  475. 'git',
  476. ['rev-parse', '--short=10', commit],
  477. this.repositoryRoot,
  478. )
  479. const rootManifest = parseObject(
  480. this.runner.capture('git', ['show', `${commit}:package.json`], this.repositoryRoot),
  481. `${commit}:package.json`,
  482. )
  483. const baseVersion = expectString(rootManifest, 'version', `${commit}:package.json`)
  484. validateBaseVersion(baseVersion, `${commit}:package.json`)
  485. const version = `${baseVersion}-${timestamp}-${shortCommit}`
  486. const distTag = `dev-${baseVersion}`
  487. validateDistTag(distTag)
  488. const artifactDirectory = resolve(options.outputDirectory, version)
  489. if (existsSync(artifactDirectory)) {
  490. throw new Error(`output already exists: ${artifactDirectory}`)
  491. }
  492. return new BaselinePackPlan(
  493. commit,
  494. shortCommit,
  495. timestamp,
  496. baseVersion,
  497. version,
  498. distTag,
  499. registry,
  500. artifactDirectory,
  501. )
  502. }
  503. pack(plan: BaselinePackPlan): ReleaseBundle {
  504. const { artifactDirectory } = plan
  505. if (existsSync(artifactDirectory)) {
  506. throw new Error(`output already exists: ${artifactDirectory}`)
  507. }
  508. const worktree = DetachedWorktree.create(this.repositoryRoot, plan.commit, this.runner)
  509. let createdArtifactDirectory = false
  510. try {
  511. const packageSet = WorkspacePackageSet.discover(worktree.path)
  512. if (packageSet.baseVersion !== plan.baseVersion) {
  513. throw new Error(
  514. `workspace package version ${packageSet.baseVersion} does not match root version `
  515. + `${plan.baseVersion} at ${plan.commit}`,
  516. )
  517. }
  518. console.log(`publish-npm-baseline: installing detached worktree ${plan.shortCommit}`)
  519. this.runner.run('pnpm', ['install', '--frozen-lockfile'], worktree.path)
  520. this.runner.run('pnpm', ['run', 'constraints'], worktree.path)
  521. packageSet.stage(worktree.path, plan.version)
  522. mkdirSync(artifactDirectory, { recursive: true })
  523. createdArtifactDirectory = true
  524. console.log(
  525. `publish-npm-baseline: building ${packageSet.packages.length} packages as ${plan.version}`,
  526. )
  527. this.runner.run('pnpm', ['run', 'build'], worktree.path)
  528. this.runner.run('pnpm', ['run', 'publint'], worktree.path)
  529. this.runner.run('pnpm', ['run', 'verify-built-package-invariants'], worktree.path)
  530. this.runner.run('pnpm', [
  531. '--filter', './vendor/**',
  532. '--filter', './packages/**',
  533. '--filter', './apps/**',
  534. '--recursive',
  535. 'pack',
  536. '--pack-destination', artifactDirectory,
  537. ], worktree.path)
  538. const bundle = ReleaseBundle.create(
  539. artifactDirectory,
  540. packageSet.packages,
  541. plan.commit,
  542. plan.version,
  543. plan.distTag,
  544. plan.registry,
  545. this.runner,
  546. )
  547. new InstalledBundleSmoke(bundle, this.runner).run()
  548. createdArtifactDirectory = false
  549. console.log(`publish-npm-baseline: packed ${bundle.manifest.packages.length} packages`)
  550. console.log(` version: ${bundle.manifest.version}`)
  551. console.log(` dist-tag: ${bundle.manifest.distTag}`)
  552. console.log(` manifest: ${resolve(bundle.directory, RELEASE_MANIFEST_NAME)}`)
  553. console.log(' publish: ' + formatCopyableCommand('pnpm', [
  554. '--dir',
  555. this.repositoryRoot,
  556. 'exec',
  557. 'tsx',
  558. resolve(this.repositoryRoot, 'scripts/publish-npm-baseline.ts'),
  559. 'publish',
  560. '--manifest',
  561. resolve(bundle.directory, RELEASE_MANIFEST_NAME),
  562. '--yes',
  563. ]))
  564. return bundle
  565. } finally {
  566. worktree.dispose()
  567. if (createdArtifactDirectory) {
  568. rmSync(artifactDirectory, { recursive: true, force: true })
  569. }
  570. }
  571. }
  572. }
  573. /** Publishes and verifies a release bundle against its recorded registry. */
  574. class RegistryPublication {
  575. private readonly npmEnvironment = npmClientEnvironment()
  576. private readonly npmWorkingDirectory = tmpdir()
  577. constructor(
  578. private readonly bundle: ReleaseBundle,
  579. private readonly runner: CommandRunner,
  580. ) {}
  581. async publish(assumeYes: boolean): Promise<void> {
  582. this.pingRegistry()
  583. this.requireIdentity()
  584. if (!assumeYes) await this.confirm()
  585. for (const pkg of this.bundle.manifest.packages) {
  586. const existingIntegrity = this.remoteIntegrity(pkg.name)
  587. if (existingIntegrity === undefined) {
  588. this.runner.run('npm', [
  589. 'publish',
  590. this.bundle.tarballPath(pkg),
  591. `--registry=${this.bundle.manifest.registry}`,
  592. `--tag=${this.bundle.manifest.distTag}`,
  593. ], this.npmWorkingDirectory, this.npmEnvironment)
  594. } else {
  595. if (existingIntegrity !== pkg.integrity) {
  596. throw new Error(
  597. `${pkg.name}@${this.bundle.manifest.version} already exists with different integrity`,
  598. )
  599. }
  600. console.log(
  601. `publish-npm-baseline: already published ${pkg.name}@${this.bundle.manifest.version}`,
  602. )
  603. }
  604. this.ensureDistTag(pkg.name, this.bundle.manifest.distTag)
  605. }
  606. this.ensureDistTag(RELEASE_ENTRY_PACKAGE, LATEST_DIST_TAG)
  607. this.verifyRemote()
  608. this.verifyReleaseEntryDistTag()
  609. }
  610. verify(): void {
  611. this.pingRegistry()
  612. this.verifyRemote()
  613. this.verifyReleaseEntryDistTag()
  614. }
  615. private verifyRemote(): void {
  616. for (const pkg of this.bundle.manifest.packages) {
  617. const integrity = this.remoteIntegrity(pkg.name)
  618. if (integrity === undefined) {
  619. throw new Error(`package is missing: ${pkg.name}@${this.bundle.manifest.version}`)
  620. }
  621. if (integrity !== pkg.integrity) {
  622. throw new Error(`integrity mismatch: ${pkg.name}@${this.bundle.manifest.version}`)
  623. }
  624. const tagVersion = this.remoteDistTag(pkg.name, this.bundle.manifest.distTag)
  625. if (tagVersion !== this.bundle.manifest.version) {
  626. throw new Error(
  627. `${pkg.name}@${this.bundle.manifest.distTag} points to ${tagVersion ?? '<missing>'}; `
  628. + `expected ${this.bundle.manifest.version}`,
  629. )
  630. }
  631. console.log(`publish-npm-baseline: verified ${pkg.name}@${this.bundle.manifest.version}`)
  632. }
  633. console.log(
  634. `publish-npm-baseline: verified ${this.bundle.manifest.packages.length} packages and `
  635. + `dist-tag ${this.bundle.manifest.distTag}`,
  636. )
  637. }
  638. private verifyReleaseEntryDistTag(): void {
  639. const tagVersion = this.remoteDistTag(RELEASE_ENTRY_PACKAGE, LATEST_DIST_TAG)
  640. if (tagVersion !== this.bundle.manifest.version) {
  641. throw new Error(
  642. `${RELEASE_ENTRY_PACKAGE}@${LATEST_DIST_TAG} points to ${tagVersion ?? '<missing>'}; `
  643. + `expected ${this.bundle.manifest.version}`,
  644. )
  645. }
  646. console.log(
  647. `publish-npm-baseline: verified ${RELEASE_ENTRY_PACKAGE}@${LATEST_DIST_TAG} at `
  648. + this.bundle.manifest.version,
  649. )
  650. }
  651. private pingRegistry(): void {
  652. const { registry } = this.bundle.manifest
  653. this.runner.capture(
  654. 'npm', ['ping', `--registry=${registry}`], this.npmWorkingDirectory, this.npmEnvironment,
  655. )
  656. }
  657. private requireIdentity(): void {
  658. const { registry } = this.bundle.manifest
  659. const identity = this.runner.capture(
  660. 'npm', ['whoami', `--registry=${registry}`], this.npmWorkingDirectory, this.npmEnvironment,
  661. )
  662. console.log(`publish-npm-baseline: registry identity ${identity} at ${registry}`)
  663. }
  664. private async confirm(): Promise<void> {
  665. await confirmEnter(
  666. `Publish ${this.bundle.manifest.packages.length} packages as `
  667. + `${this.bundle.manifest.version} to ${this.bundle.manifest.registry}? `
  668. + 'Press Enter to continue or type anything to cancel: ',
  669. 'publish requires an interactive terminal or --yes',
  670. 'publication cancelled',
  671. )
  672. }
  673. private remoteIntegrity(name: string): string | undefined {
  674. const { registry, version } = this.bundle.manifest
  675. const result = this.runner.result(
  676. 'npm',
  677. ['view', `${name}@${version}`, 'dist.integrity', '--json', `--registry=${registry}`],
  678. this.npmWorkingDirectory,
  679. this.npmEnvironment,
  680. )
  681. if (result.status !== 0) {
  682. if (/E404|NOT_FOUND|404 Not Found/.test(`${result.stdout}\n${result.stderr}`)) return undefined
  683. throw commandFailure('npm', ['view', `${name}@${version}`], result)
  684. }
  685. const value: unknown = result.stdout.trim() === '' ? undefined : JSON.parse(result.stdout)
  686. if (typeof value !== 'string' || !value.startsWith('sha512-')) {
  687. throw new Error(`registry returned no integrity for ${name}@${version}`)
  688. }
  689. return value
  690. }
  691. private remoteDistTag(name: string, distTag: string): string | undefined {
  692. const { registry } = this.bundle.manifest
  693. const raw = this.runner.capture(
  694. 'npm',
  695. ['dist-tag', 'ls', name, `--registry=${registry}`],
  696. this.npmWorkingDirectory,
  697. this.npmEnvironment,
  698. )
  699. return parseDistTagListing(raw, name).get(distTag)
  700. }
  701. private ensureDistTag(name: string, distTag: string): void {
  702. if (this.remoteDistTag(name, distTag) === this.bundle.manifest.version) return
  703. const { registry, version } = this.bundle.manifest
  704. this.runner.run(
  705. 'npm',
  706. ['dist-tag', 'add', `${name}@${version}`, distTag, `--registry=${registry}`],
  707. this.npmWorkingDirectory,
  708. this.npmEnvironment,
  709. )
  710. }
  711. }
  712. interface InspectedTarball {
  713. name: string
  714. version: string
  715. private: unknown
  716. manifest: Record<string, unknown>
  717. files: string[]
  718. }
  719. function inspectTarball(path: string, runner: CommandRunner): InspectedTarball {
  720. const manifest = JSON.parse(
  721. runner.capture('tar', ['-xOf', path, 'package/package.json'], dirname(path)),
  722. ) as unknown
  723. if (!isRecord(manifest)) throw new Error(`${path} contains an invalid package.json`)
  724. return {
  725. name: expectString(manifest, 'name', path),
  726. version: expectString(manifest, 'version', path),
  727. private: manifest.private,
  728. manifest,
  729. files: runner.capture('tar', ['-tf', path], dirname(path)).split(/\r?\n/),
  730. }
  731. }
  732. function packedPackage(name: string, path: string, origin: PackageOrigin): PackedPackage {
  733. const bytes = readFileSync(path)
  734. return {
  735. name,
  736. tarball: basename(path),
  737. sha256: createHash('sha256').update(bytes).digest('hex'),
  738. integrity: `sha512-${createHash('sha512').update(bytes).digest('base64')}`,
  739. origin,
  740. }
  741. }
  742. function parsePackedPackage(value: unknown, index: number): PackedPackage {
  743. if (!isRecord(value)) throw new Error(`invalid release manifest package at index ${index}`)
  744. const context = `release manifest package at index ${index}`
  745. const name = expectString(value, 'name', context)
  746. const origin = value.origin === undefined ? 'harness' : value.origin
  747. if (origin !== 'harness' && origin !== 'vendor') {
  748. throw new Error(`invalid package origin in release manifest: ${JSON.stringify(origin)}`)
  749. }
  750. if (origin === 'harness' && (!name.startsWith('@deepseek-ai/') || name === '@deepseek-ai/dsh-root')) {
  751. throw new Error(`invalid package name in release manifest: ${name}`)
  752. }
  753. return {
  754. name,
  755. tarball: expectString(value, 'tarball', context),
  756. sha256: expectString(value, 'sha256', context),
  757. integrity: expectString(value, 'integrity', context),
  758. origin,
  759. }
  760. }
  761. function containsWorkspaceProtocol(value: unknown): boolean {
  762. if (typeof value === 'string') return value.startsWith('workspace:')
  763. if (Array.isArray(value)) return value.some(containsWorkspaceProtocol)
  764. return isRecord(value) && Object.values(value).some(containsWorkspaceProtocol)
  765. }
  766. function stageInternalDependencies(
  767. manifest: Record<string, unknown>,
  768. internalNames: ReadonlySet<string>,
  769. releaseVersion: string,
  770. context: string,
  771. ): void {
  772. for (const { dependencies, name } of internalDependencyEntries(manifest, internalNames, context)) {
  773. dependencies[name] = releaseVersion
  774. }
  775. }
  776. function validateInternalDependencyPins(
  777. manifest: Record<string, unknown>,
  778. internalNames: ReadonlySet<string>,
  779. releaseVersion: string,
  780. context: string,
  781. ): void {
  782. for (const { section, name, range } of internalDependencyEntries(manifest, internalNames, context)) {
  783. if (range !== releaseVersion) {
  784. throw new Error(
  785. `${context} has internal ${section} ${name}@${String(range)}; `
  786. + `expected exact version ${releaseVersion}`,
  787. )
  788. }
  789. }
  790. }
  791. function* internalDependencyEntries(
  792. manifest: Record<string, unknown>,
  793. internalNames: ReadonlySet<string>,
  794. context: string,
  795. ): Generator<{
  796. section: typeof DEPENDENCY_SECTIONS[number]
  797. dependencies: Record<string, unknown>
  798. name: string
  799. range: unknown
  800. }> {
  801. for (const section of DEPENDENCY_SECTIONS) {
  802. const dependencies = manifest[section]
  803. if (dependencies === undefined) continue
  804. if (!isRecord(dependencies)) throw new Error(`${context} ${section} must be an object`)
  805. for (const [name, range] of Object.entries(dependencies)) {
  806. if (!internalNames.has(name)) continue
  807. yield { section, dependencies, name, range }
  808. }
  809. }
  810. }
  811. function readObject(path: string): Record<string, unknown> {
  812. return parseObject(readFileSync(path, 'utf8'), path)
  813. }
  814. function parseObject(source: string, context: string): Record<string, unknown> {
  815. const value: unknown = JSON.parse(source)
  816. if (!isRecord(value)) throw new Error(`${context} must contain a JSON object`)
  817. return value
  818. }
  819. function isRecord(value: unknown): value is Record<string, unknown> {
  820. return value !== null && typeof value === 'object' && !Array.isArray(value)
  821. }
  822. function expectString(value: Record<string, unknown>, key: string, context: string): string {
  823. const result = value[key]
  824. if (typeof result !== 'string' || result === '') {
  825. throw new Error(`${context} must contain a non-empty ${key}`)
  826. }
  827. return result
  828. }
  829. function normalizeRegistry(value: string): string {
  830. const url = new URL(value)
  831. if (url.protocol !== 'http:' && url.protocol !== 'https:') {
  832. throw new Error(`registry must use HTTP or HTTPS: ${value}`)
  833. }
  834. return value.replace(/\/+$/, '')
  835. }
  836. function npmClientEnvironment(): NodeJS.ProcessEnv {
  837. const environment = { ...process.env }
  838. delete environment.npm_config_user_agent
  839. delete environment.NPM_CONFIG_USER_AGENT
  840. return environment
  841. }
  842. function installedArtifactEnvironment(consumerRoot: string): NodeJS.ProcessEnv {
  843. const environment = npmClientEnvironment()
  844. delete environment.NODE_OPTIONS
  845. delete environment.NODE_PATH
  846. environment.DSH_HOME = resolve(consumerRoot, '.dsh')
  847. environment.DSH_AGENTS_HOME = resolve(consumerRoot, '.agents')
  848. environment.DSH_TELEMETRY_DISABLED = '1'
  849. environment.DEEPSEEK_API_KEY = 'keyless-installed-web-no-call'
  850. environment.LANG = 'en_US.UTF-8'
  851. environment.LC_ALL = 'en_US.UTF-8'
  852. environment.LC_CTYPE = 'en_US.UTF-8'
  853. environment.TERM = 'xterm-256color'
  854. environment.COLUMNS = '100'
  855. environment.LINES = '30'
  856. delete environment.COLORTERM
  857. return environment
  858. }
  859. function assertPathWithin(root: string, path: string, label: string): void {
  860. const rootPath = realpathSync.native(root)
  861. const candidate = realpathSync.native(path)
  862. const fromRoot = relative(rootPath, candidate)
  863. if (fromRoot === '..' || fromRoot.startsWith(`..${sep}`) || isAbsolute(fromRoot)) {
  864. throw new Error(`${label} resolved outside the isolated consumer: ${candidate}`)
  865. }
  866. }
  867. function validateDistTag(value: string): void {
  868. if (value === '' || /\s/.test(value)) throw new Error(`invalid dist-tag: ${JSON.stringify(value)}`)
  869. }
  870. function validateBaseVersion(value: string, context: string): void {
  871. if (!/^\d+\.\d+\.\d+$/.test(value)) {
  872. throw new Error(`${context} must have a stable X.Y.Z version, got ${value}`)
  873. }
  874. }
  875. function parseDistTagListing(raw: string, name: string): Map<string, string> {
  876. const tags = new Map<string, string>()
  877. for (const line of raw.split(/\r?\n/)) {
  878. if (line === '') continue
  879. const separator = line.indexOf(': ')
  880. if (separator <= 0 || separator + 2 === line.length) {
  881. throw new Error(`registry returned an invalid dist-tag for ${name}: ${line}`)
  882. }
  883. const tag = line.slice(0, separator)
  884. if (tags.has(tag)) throw new Error(`registry returned duplicate dist-tag ${tag} for ${name}`)
  885. tags.set(tag, line.slice(separator + 2))
  886. }
  887. return tags
  888. }
  889. async function confirmEnter(
  890. prompt: string,
  891. nonInteractiveError: string,
  892. cancellationError: string,
  893. ): Promise<void> {
  894. if (!process.stdin.isTTY || !process.stdout.isTTY) throw new Error(nonInteractiveError)
  895. const readline = createInterface({ input: process.stdin, output: process.stdout })
  896. try {
  897. const answer = await readline.question(prompt)
  898. if (answer !== '') throw new Error(cancellationError)
  899. } finally {
  900. readline.close()
  901. }
  902. }
  903. function formatUtcTimestamp(value: Date): string {
  904. if (!Number.isFinite(value.getTime())) throw new Error('pack timestamp must be a valid date')
  905. return value.toISOString().replaceAll(/[-:TZ.]/g, '').slice(0, 14)
  906. }
  907. function commandFailure(command: string, args: string[], result: CommandResult): Error {
  908. const detail = [result.stdout.trim(), result.stderr.trim()].filter(Boolean).join('\n')
  909. return new Error(
  910. `${formatCommand(command, args)} exited with status ${result.status}${detail === '' ? '' : `\n${detail}`}`,
  911. )
  912. }
  913. function formatCommand(command: string, args: string[]): string {
  914. return [command, ...args].map(value => JSON.stringify(value)).join(' ')
  915. }
  916. function formatCopyableCommand(command: string, args: string[]): string {
  917. return [command, ...args].map(quoteShellArgument).join(' ')
  918. }
  919. function quoteShellArgument(value: string): string {
  920. if (/^[\w./:@=+-]+$/.test(value)) return value
  921. const singleQuote = String.fromCodePoint(39)
  922. const escapedSingleQuote = `${singleQuote}"${singleQuote}"${singleQuote}`
  923. return `${singleQuote}${value.replaceAll(singleQuote, escapedSingleQuote)}${singleQuote}`
  924. }
  925. function printUsage(): void {
  926. console.log(`Usage:
  927. pnpm exec tsx scripts/publish-npm-baseline.ts pack [options]
  928. pnpm exec tsx scripts/publish-npm-baseline.ts release [options] [--yes]
  929. pnpm exec tsx scripts/publish-npm-baseline.ts publish --manifest <path> [--yes]
  930. pnpm exec tsx scripts/publish-npm-baseline.ts verify --manifest <path>
  931. Pack/release options:
  932. --ref <git-ref> Git commit to stage (default: HEAD)
  933. --registry <url> npm registry (default: ${DEFAULT_REGISTRY})
  934. --output-dir <path> Artifact root (default: ${DEFAULT_OUTPUT_DIRECTORY})
  935. --yes pack/release without waiting for Enter`)
  936. }
  937. async function main(): Promise<void> {
  938. const command = process.argv[2]
  939. if (command === undefined || command === 'help' || command === '--help' || command === '-h') {
  940. printUsage()
  941. return
  942. }
  943. if (process.argv.slice(3).some(value => value === '--help' || value === '-h')) {
  944. printUsage()
  945. return
  946. }
  947. const runner = new CommandRunner()
  948. const repositoryRoot = runner.capture('git', ['rev-parse', '--show-toplevel'], process.cwd())
  949. if (command === 'pack' || command === 'release') {
  950. const { values } = parseArgs({
  951. args: process.argv.slice(3),
  952. options: {
  953. ref: { type: 'string', default: 'HEAD' },
  954. registry: { type: 'string', default: DEFAULT_REGISTRY },
  955. 'output-dir': { type: 'string', default: resolve(repositoryRoot, DEFAULT_OUTPUT_DIRECTORY) },
  956. yes: { type: 'boolean', default: false },
  957. },
  958. strict: true,
  959. })
  960. const packager = new BaselinePackager(repositoryRoot, runner)
  961. const plan = packager.plan({
  962. ref: values.ref,
  963. registry: values.registry,
  964. outputDirectory: resolve(values['output-dir']),
  965. })
  966. await plan.confirm(values.yes)
  967. const bundle = packager.pack(plan)
  968. if (command === 'release') {
  969. await new RegistryPublication(bundle, runner).publish(values.yes)
  970. }
  971. return
  972. }
  973. if (command === 'publish' || command === 'verify') {
  974. const { values } = parseArgs({
  975. args: process.argv.slice(3),
  976. options: {
  977. manifest: { type: 'string' },
  978. yes: { type: 'boolean', default: false },
  979. },
  980. strict: true,
  981. })
  982. if (values.manifest === undefined) throw new Error(`${command} requires --manifest`)
  983. if (command === 'verify' && values.yes) throw new Error('verify does not accept --yes')
  984. const bundle = ReleaseBundle.load(values.manifest, runner)
  985. const publication = new RegistryPublication(bundle, runner)
  986. if (command === 'publish') await publication.publish(values.yes)
  987. else publication.verify()
  988. return
  989. }
  990. throw new Error(`unknown command: ${command}`)
  991. }
  992. try {
  993. await main()
  994. } catch (error: unknown) {
  995. console.error(`publish-npm-baseline: ${error instanceof Error ? error.message : String(error)}`)
  996. process.exitCode = 1
  997. }