verify-packed-install.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Install packed tarballs into a throwaway consumer outside the repository and
  3. * drive the installed executable with plain Node.
  4. *
  5. * Every tarball the installed tree needs comes from `--from`, so the only
  6. * registry traffic is for external dependencies. That matters beyond hermetic
  7. * verification: the harness packages declare the vendored framework as a peer,
  8. * those packages live in another release sequence, and this job must not depend
  9. * on the registry already carrying versions that match — one pull request may
  10. * bump both families before either publishes — so a dsh verification passes the
  11. * vendored family's pack output too, while publishing only its own
  12. * ([rationale](../../.agents/notes/implemented/process/2026-08-10-npm-release-sequences.md)).
  13. *
  14. * What this proves is that `files` selected a complete payload and that the
  15. * published dependency ranges resolve. A workspace link or a stale `lib/` in the
  16. * checkout cannot stand in for a missing file here.
  17. */
  18. import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
  19. import { tmpdir } from 'node:os'
  20. import { join, resolve } from 'node:path'
  21. import { pathToFileURL } from 'node:url'
  22. import { parseArgs } from 'node:util'
  23. import { releaseFamily } from './families.ts'
  24. import { capture, isEntry } from './process.ts'
  25. import { packedIdentity } from './tarball.ts'
  26. import { verifyInstalledProductIsolation } from './installed-product-isolation.ts'
  27. /**
  28. * Environment for the installed artifact: no host Node hooks, no host DeepSeek
  29. * Harness home, and no ambient npm user agent that would confuse npm.
  30. * @param consumerRoot - the throwaway consumer directory.
  31. * @returns The child environment.
  32. */
  33. function consumerEnvironment(consumerRoot: string): NodeJS.ProcessEnv {
  34. const environment = { ...process.env }
  35. delete environment.npm_config_user_agent
  36. delete environment.NPM_CONFIG_USER_AGENT
  37. delete environment.NODE_OPTIONS
  38. delete environment.NODE_PATH
  39. environment.DSH_HOME = resolve(consumerRoot, '.dsh')
  40. environment.DSH_AGENTS_HOME = resolve(consumerRoot, '.agents')
  41. environment.DSH_TELEMETRY_DISABLED = '1'
  42. return environment
  43. }
  44. /**
  45. * Every packed tarball in the given directories, as `file:` dependency entries.
  46. *
  47. * The directories are read by their contents rather than a pack order file: a
  48. * directory here can hold tarballs packed only to satisfy a cross-sequence
  49. * dependency, which no release order describes.
  50. * @param directories - absolute directories holding packed tarballs.
  51. * @returns Package name to tarball file URL, and the version each carries.
  52. */
  53. function packedDependencies(directories: readonly string[]): Map<string, { url: string; version: string }> {
  54. const dependencies = new Map<string, { url: string; version: string }>()
  55. for (const directory of directories) {
  56. const tarballs = readdirSync(directory).filter(name => name.endsWith('.tgz')).sort()
  57. if (tarballs.length === 0) throw new Error(`${directory} holds no packed tarball`)
  58. for (const filename of tarballs) {
  59. const tarball = join(directory, filename)
  60. const { name, version } = packedIdentity(tarball)
  61. dependencies.set(name, { url: pathToFileURL(tarball).href, version })
  62. }
  63. }
  64. return dependencies
  65. }
  66. /** Install every tarball under `--from` and drive the `--family` entry. */
  67. function main(): void {
  68. const { values } = parseArgs({
  69. options: { family: { type: 'string' }, from: { type: 'string', multiple: true } },
  70. allowPositionals: false,
  71. })
  72. if (values.family === undefined || values.from === undefined || values.from.length === 0) {
  73. throw new Error('usage: verify-packed-install.ts --family <dsh|vendor> --from <packed directory> [--from ...]')
  74. }
  75. const family = releaseFamily(values.family)
  76. const entry = family.installedEntry
  77. if (entry === undefined) {
  78. console.log(`release verify-packed-install: family ${family.id} publishes no executable, nothing to drive`)
  79. return
  80. }
  81. const root = process.cwd()
  82. const packed = packedDependencies(values.from.map(directory => resolve(root, directory)))
  83. const expected = packed.get(entry.packageName)
  84. if (expected === undefined) throw new Error(`${entry.packageName} is not among the packed tarballs`)
  85. const consumerRoot = mkdtempSync(join(tmpdir(), `dsh-packed-${family.id}-`))
  86. try {
  87. writeFileSync(join(consumerRoot, 'package.json'), `${JSON.stringify({
  88. name: `dsh-packed-install-${family.id}`,
  89. version: '0.0.0',
  90. private: true,
  91. dependencies: Object.fromEntries([...packed].map(([name, entryPacked]) => [name, entryPacked.url])),
  92. }, null, 2)}\n`)
  93. const environment = consumerEnvironment(consumerRoot)
  94. console.log(`release verify-packed-install: installing ${String(packed.size)} tarball(s) into ${consumerRoot}`)
  95. // Optional dependencies are omitted: the Landlock platform packages behind
  96. // them need a musl toolchain and one build per architecture, and a consumer
  97. // that cannot install them must still start — which is what optional means
  98. // here. Their entry package is a plain dependency of dsh-sandbox-local, so
  99. // its tarball is supplied through --from.
  100. capture('npm', ['install', '--no-audit', '--no-fund', '--package-lock=false', '--omit=optional', '--loglevel=http'],
  101. { cwd: consumerRoot, env: environment })
  102. const installedEntry = join(consumerRoot, 'node_modules', entry.packageName)
  103. const packageCount = verifyInstalledProductIsolation(installedEntry)
  104. console.log(`release verify-packed-install: ${String(packageCount)} default-product packages exclude experimental packages`)
  105. const bin = join(consumerRoot, 'node_modules', ...entry.packageName.split('/'), entry.binPath)
  106. const version = capture(process.execPath, [bin, '--version'], { cwd: consumerRoot, env: environment })
  107. if (version !== expected.version) {
  108. throw new Error(`installed ${entry.packageName} --version reported ${JSON.stringify(version)}, expected ${expected.version}`)
  109. }
  110. console.log(`release verify-packed-install: installed ${entry.packageName} reports ${version}`)
  111. } finally {
  112. rmSync(consumerRoot, { recursive: true, force: true })
  113. }
  114. }
  115. if (isEntry(import.meta.url)) main()