verify-packed-install.ts 5.6 KB

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