test(sandbox): derive packed workspace closure

The Landlock packed-install rehearsal packed a hand-maintained list of
workspace tarballs. When dsh-llm gained the dsh-util-crypto runtime
dependency, the list stayed stale and npm tried to fetch the unpublished
release candidate from the public registry, failing both Linux master jobs
with E404 before confinement ran.

Read the current pnpm workspace inventory and traverse dependencies,
optionalDependencies, and required peerDependencies from the packed test
roots. Verify package identities, fail loudly on unresolved workspace names,
sort the closure deterministically, and leave native-family packages to the
existing mode-preserving native packer.

Cover runtime traversal, optional-peer exclusion, native filtering, and
invalid workspace metadata. Remove the obsolete vendoring exact edit for the
deleted manual list so future runtime workspace additions are included by
their manifests instead of becoming post-merge CI failures.
This commit is contained in:
Tianyi Cui
2026-08-23 19:24:50 +08:00
parent 4f3a47d792
commit b6b08beb0d
7 changed files with 154 additions and 50 deletions
@@ -5,6 +5,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { packedWorkspaceClosure, readWorkspacePackages } from './packed-workspace-closure.ts'
/**
* Keyless publish-path rehearsal. It packs the provider, its workspace peers, the vendored framework
@@ -25,31 +26,7 @@ const nativeDir = join(repoRoot, 'native/landlock-run')
const sourceLauncher = join(nativeDir, 'packages', `linux-${process.arch}`, 'bin', 'landlock-run')
const platformPackageName = `@deepseek-ai/node-addon-landlock-run-linux-${process.arch}`
/** The harness closure the consumer needs; native tarballs are packed through their mode-preserving release script. */
const WORKSPACE_CLOSURE = [
'packages/sandbox/sandbox-local',
// sandbox-local's win32 chain rung is a runtime dependency: a packed
// consumer resolves it like any other @deepseek-ai peer (koffi arrives
// from the registry).
'packages/sandbox/sandbox-windows-acl',
'packages/subprocess/win32-process',
'packages/sandbox/sandbox',
'packages/core/session',
'packages/core/scope',
'packages/llm/llm',
'packages/typert/protocol',
'packages/attachment/attachment',
'packages/util/brand',
'packages/util/timeout',
'packages/runtime-diagnostics/invariants',
// The framework and the vendored packages the closure declares outright:
// rescoped into @deepseek-ai, so the consumer installs this repository's
// copies. Schemastery is a hard dependency of three members above, not a
// peer, so npm resolves it while installing them.
'vendor/cordis',
'vendor/cosmokit',
'vendor/schemastery',
]
const NATIVE_PACKAGE_PREFIX = '@deepseek-ai/node-addon-landlock-run'
/** ELF `e_machine` (offset 18, LE) for this host: x86-64 = 62, AArch64 = 183. */
const E_MACHINE = { x64: 62, arm64: 183 }[process.arch as 'x64' | 'arm64']
@@ -93,15 +70,22 @@ describe.skipIf(!packable)('sandbox-local: packed-tarball distribution (publish-
.split('\n')
.map(tarball => join(nativePackDest, tarball))
// Derive the current runtime closure so a newly introduced workspace
// dependency cannot fall through to an unpublished registry version.
const workspaceClosure = packedWorkspaceClosure(
'@deepseek-ai/dsh-sandbox-local',
readWorkspacePackages(repoRoot),
).filter(member => !member.name.startsWith(NATIVE_PACKAGE_PREFIX))
// Pack each harness closure member with the exact bytes publish would upload.
const tarballs: string[] = []
for (const pkg of WORKSPACE_CLOSURE) {
for (const pkg of workspaceClosure) {
const pack = spawnSync('pnpm', ['pack', '--pack-destination', packDest], {
cwd: join(repoRoot, pkg),
cwd: pkg.directory,
encoding: 'utf8',
timeout: 120_000,
})
expect(pack.status, `pnpm pack failed for ${pkg}:\n${pack.stdout}\n${pack.stderr}`).toBe(0)
expect(pack.status, `pnpm pack failed for ${pkg.name}:\n${pack.stdout}\n${pack.stderr}`).toBe(0)
const lines = pack.stdout.trim().split('\n')
tarballs.push(lines[lines.length - 1] as string)
}
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest'
import { packedWorkspaceClosure, type WorkspacePackage } from './packed-workspace-closure.ts'
function pkg(name: string, manifest: Record<string, unknown> = {}): WorkspacePackage {
return { name, directory: `/workspace/${name}`, manifest }
}
describe('packed workspace closure', () => {
it('follows install edges and required peers but excludes development and optional peers', () => {
const packages = new Map([
['root', pkg('root', {
dependencies: { installed: 'workspace:^' },
optionalDependencies: { optional: 'workspace:^' },
peerDependencies: { required: 'workspace:^', omitted: 'workspace:^' },
peerDependenciesMeta: { omitted: { optional: true } },
devDependencies: { development: 'workspace:^' },
})],
['installed', pkg('installed', { dependencies: { transitive: 'workspace:^', external: '^1.0.0' } })],
['optional', pkg('optional')],
['required', pkg('required')],
['omitted', pkg('omitted')],
['development', pkg('development')],
['transitive', pkg('transitive')],
])
expect(packedWorkspaceClosure('root', packages).map(entry => entry.name))
.toEqual(['installed', 'optional', 'required', 'root', 'transitive'])
})
it('fails when a workspace dependency is absent from the inventory', () => {
const packages = new Map([
['root', pkg('root', { dependencies: { missing: 'workspace:^' } })],
])
expect(() => packedWorkspaceClosure('root', packages))
.toThrow('packed workspace closure cannot resolve missing')
})
})
@@ -0,0 +1,101 @@
import { spawnSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
const RUNTIME_SECTIONS = ['dependencies', 'optionalDependencies', 'peerDependencies'] as const
interface WorkspaceListEntry {
name: string
path: string
}
/** One workspace manifest available to the packed-install rehearsal. */
export interface WorkspacePackage {
name: string
directory: string
manifest: Record<string, unknown>
}
function dependencyEntries(
manifest: Record<string, unknown>,
section: (typeof RUNTIME_SECTIONS)[number],
): [string, string][] {
const value = manifest[section]
if (value === null || typeof value !== 'object' || Array.isArray(value)) return []
return Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === 'string')
}
function optionalPeer(manifest: Record<string, unknown>, name: string): boolean {
const metadata = manifest.peerDependenciesMeta
if (metadata === null || typeof metadata !== 'object' || Array.isArray(metadata)) return false
const entry = (metadata as Record<string, unknown>)[name]
return entry !== null && typeof entry === 'object' && !Array.isArray(entry)
&& (entry as Record<string, unknown>).optional === true
}
/**
* Read the root pnpm workspace inventory and its package manifests.
* @param repoRoot - repository root containing the pnpm workspace.
* @returns Workspace packages indexed by package name.
*/
export function readWorkspacePackages(repoRoot: string): Map<string, WorkspacePackage> {
const listed = spawnSync('pnpm', ['list', '--recursive', '--depth', '-1', '--json'], {
cwd: repoRoot,
encoding: 'utf8',
timeout: 30_000,
})
if (listed.status !== 0) {
throw new Error(`pnpm workspace inventory failed:\n${listed.stdout}\n${listed.stderr}`)
}
const parsed: unknown = JSON.parse(listed.stdout)
if (!Array.isArray(parsed)) throw new Error('pnpm workspace inventory is not an array')
const packages = new Map<string, WorkspacePackage>()
for (const value of parsed) {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
throw new Error('pnpm workspace inventory contains a non-object entry')
}
const { name, path } = value as Partial<WorkspaceListEntry>
if (typeof name !== 'string' || typeof path !== 'string') {
throw new Error('pnpm workspace inventory entry lacks name/path')
}
const parsedManifest: unknown = JSON.parse(readFileSync(join(path, 'package.json'), 'utf8'))
if (parsedManifest === null || typeof parsedManifest !== 'object' || Array.isArray(parsedManifest)) {
throw new Error(`${path}/package.json is not an object`)
}
const manifest = parsedManifest as Record<string, unknown>
if (manifest.name !== name) throw new Error(`${path}/package.json does not declare ${name}`)
if (packages.has(name)) throw new Error(`pnpm workspace inventory repeats ${name}`)
packages.set(name, { name, directory: path, manifest })
}
return packages
}
/**
* Follow install dependencies and required peers inside one workspace.
* @param rootName - package whose consumer closure is required.
* @param packages - workspace packages indexed by package name.
* @returns Transitive runtime closure sorted by package directory.
*/
export function packedWorkspaceClosure(
rootName: string,
packages: ReadonlyMap<string, WorkspacePackage>,
): WorkspacePackage[] {
const closure: WorkspacePackage[] = []
const visited = new Set<string>()
const visit = (name: string): void => {
if (visited.has(name)) return
visited.add(name)
const current = packages.get(name)
if (current === undefined) throw new Error(`packed workspace closure cannot resolve ${name}`)
closure.push(current)
for (const section of RUNTIME_SECTIONS) {
for (const [dependency, range] of dependencyEntries(current.manifest, section)) {
if (!range.startsWith('workspace:')) continue
if (section === 'peerDependencies' && optionalPeer(current.manifest, dependency)) continue
visit(dependency)
}
}
}
visit(rootName)
return closure.sort((left, right) => left.directory.localeCompare(right.directory))
}