verify-packed-install.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  27. * Environment for the installed artifact: no host Node hooks, no host DeepSeek
  28. * Harness home, and no ambient npm user agent that would confuse npm.
  29. * @param consumerRoot - the throwaway consumer directory.
  30. * @returns The child environment.
  31. */
  32. function consumerEnvironment(consumerRoot: string): NodeJS.ProcessEnv {
  33. const environment = { ...process.env }
  34. delete environment.npm_config_user_agent
  35. delete environment.NPM_CONFIG_USER_AGENT
  36. delete environment.NODE_OPTIONS
  37. delete environment.NODE_PATH
  38. environment.DSH_HOME = resolve(consumerRoot, '.dsh')
  39. environment.DSH_AGENTS_HOME = resolve(consumerRoot, '.agents')
  40. environment.DSH_TELEMETRY_DISABLED = '1'
  41. return environment
  42. }
  43. /**
  44. * Every packed tarball in the given directories, as `file:` dependency entries.
  45. *
  46. * The directories are read by their contents rather than a pack order file: a
  47. * directory here can hold tarballs packed only to satisfy a cross-sequence
  48. * dependency, which no release order describes.
  49. * @param directories - absolute directories holding packed tarballs.
  50. * @returns Package name to tarball file URL, and the version each carries.
  51. */
  52. function packedDependencies(directories: readonly string[]): Map<string, { url: string; version: string }> {
  53. const dependencies = new Map<string, { url: string; version: string }>()
  54. for (const directory of directories) {
  55. const tarballs = readdirSync(directory).filter(name => name.endsWith('.tgz')).sort()
  56. if (tarballs.length === 0) throw new Error(`${directory} holds no packed tarball`)
  57. for (const filename of tarballs) {
  58. const tarball = join(directory, filename)
  59. const { name, version } = packedIdentity(tarball)
  60. dependencies.set(name, { url: pathToFileURL(tarball).href, version })
  61. }
  62. }
  63. return dependencies
  64. }
  65. /** Install every tarball under `--from` and drive the `--family` entry. */
  66. function main(): void {
  67. const { values } = parseArgs({
  68. options: { family: { type: 'string' }, from: { type: 'string', multiple: true } },
  69. allowPositionals: false,
  70. })
  71. if (values.family === undefined || values.from === undefined || values.from.length === 0) {
  72. throw new Error('usage: verify-packed-install.ts --family <dsh|vendor> --from <packed directory> [--from ...]')
  73. }
  74. const family = releaseFamily(values.family)
  75. const entry = family.installedEntry
  76. if (entry === undefined) {
  77. console.log(`release verify-packed-install: family ${family.id} publishes no executable, nothing to drive`)
  78. return
  79. }
  80. const root = process.cwd()
  81. const packed = packedDependencies(values.from.map(directory => resolve(root, directory)))
  82. const expected = packed.get(entry.packageName)
  83. if (expected === undefined) throw new Error(`${entry.packageName} is not among the packed tarballs`)
  84. const consumerRoot = mkdtempSync(join(tmpdir(), `dsh-packed-${family.id}-`))
  85. try {
  86. writeFileSync(join(consumerRoot, 'package.json'), `${JSON.stringify({
  87. name: `dsh-packed-install-${family.id}`,
  88. version: '0.0.0',
  89. private: true,
  90. dependencies: Object.fromEntries([...packed].map(([name, entryPacked]) => [name, entryPacked.url])),
  91. }, null, 2)}\n`)
  92. const environment = consumerEnvironment(consumerRoot)
  93. console.log(`release verify-packed-install: installing ${String(packed.size)} tarball(s) into ${consumerRoot}`)
  94. // Optional dependencies are omitted: the Landlock platform packages behind
  95. // them need a musl toolchain and one build per architecture, and a consumer
  96. // that cannot install them must still start — which is what optional means
  97. // here. Their entry package is a plain dependency of dsh-sandbox-local, so
  98. // its tarball is supplied through --from.
  99. capture('npm', ['install', '--no-audit', '--no-fund', '--package-lock=false', '--omit=optional'],
  100. { cwd: consumerRoot, env: environment })
  101. const bin = join(consumerRoot, 'node_modules', ...entry.packageName.split('/'), entry.binPath)
  102. const version = capture(process.execPath, [bin, '--version'], { cwd: consumerRoot, env: environment })
  103. if (version !== expected.version) {
  104. throw new Error(`installed ${entry.packageName} --version reported ${JSON.stringify(version)}, expected ${expected.version}`)
  105. }
  106. console.log(`release verify-packed-install: installed ${entry.packageName} reports ${version}`)
  107. } finally {
  108. rmSync(consumerRoot, { recursive: true, force: true })
  109. }
  110. }
  111. if (isEntry(import.meta.url)) main()