import fs from 'node:fs'; import path from 'node:path'; import { createHash } from 'node:crypto'; import { REA_VERSION, REA_PROTOCOL, resolveReaInstallation } from '../src/core/tools/rea-adapter.mjs'; const digest = file => createHash('sha256').update(fs.readFileSync(file)).digest('hex'); const inside = (root, filename) => path.resolve(filename).startsWith(path.resolve(root) + path.sep); const packageMetadata = root => JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); export function planReaBundle(appRoot) { const installation = resolveReaInstallation(appRoot); if (installation.status !== 'available') throw new Error('REA release dependency is unavailable: ' + installation.message); const modules = path.dirname(installation.packageRoot), sourceRoot = path.dirname(modules); const originalLock = path.join(sourceRoot, 'package-lock.json'), lockPath = fs.existsSync(originalLock) ? originalLock : path.join(sourceRoot, 'REA-PACKAGE-LOCK.json'); if (path.basename(modules) !== 'node_modules' || !fs.existsSync(lockPath)) throw new Error('Use the isolated, locked REA production installation when packaging.'); const lock = JSON.parse(fs.readFileSync(lockPath, 'utf8')); if (lock.packages?.['node_modules/rea-agents']?.version !== REA_VERSION || lock.packages['']?.dependencies?.['rea-agents'] !== REA_VERSION) throw new Error('REA installation lock does not match the fixed release contract.'); const packages = [], absentOptional = []; for (const [relative, expected] of Object.entries(lock.packages)) { if (!relative.startsWith('node_modules/')) continue; const absolute = path.resolve(sourceRoot, relative); if (!inside(modules, absolute)) throw new Error('REA dependency escapes its installation: ' + relative); if (!fs.existsSync(path.join(absolute, 'package.json'))) { if (expected.optional || expected.dev) { if (expected.optional) absentOptional.push(relative); continue; } throw new Error('REA required installed dependency is missing: ' + relative); } if (expected.dev) throw new Error('REA release input must contain only production dependencies: ' + relative); if (fs.lstatSync(absolute).isSymbolicLink() || !inside(modules, fs.realpathSync.native(absolute))) throw new Error('REA release dependencies must be regular contained directories.'); const metadata = packageMetadata(absolute); if (metadata.version !== expected.version) throw new Error('REA installed dependency declaration differs from its lock: ' + relative); const licenseFiles = fs.readdirSync(absolute).filter(name => /^(licen[cs]e|copying)([.-].*)?$/i.test(name) && fs.statSync(path.join(absolute, name)).isFile()); packages.push({ path: relative, name: metadata.name, version: metadata.version, license: metadata.license ?? expected.license ?? null, integrity: expected.integrity ?? null, licenseFiles }); } const topLevel = packages.filter(item => !packages.some(parent => item !== parent && item.path.startsWith(parent.path + '/'))); if (!packages.some(item => item.name === 'rea-agents' && item.version === REA_VERSION)) throw new Error('REA package is absent from the installed production graph.'); return { installation, sourceRoot, lockPath, lockSha256: digest(lockPath), packages, topLevel, absentOptional, builtAt: new Date().toISOString() }; } export function copyReaBundle(destinationRuntime, plan) { fs.mkdirSync(destinationRuntime, { recursive: true }); for (const item of plan.topLevel) { const destination = path.join(destinationRuntime, item.path); if (!inside(destinationRuntime, destination)) throw new Error('REA destination is outside the new release runtime.'); if (fs.existsSync(destination)) throw new Error('Refusing to overwrite a runtime dependency: ' + item.path); fs.cpSync(path.join(plan.sourceRoot, item.path), destination, { recursive: true, filter: file => { if (fs.lstatSync(file).isSymbolicLink()) throw new Error('REA runtime contains an unsupported link: ' + file); return true; } }); } fs.copyFileSync(plan.lockPath, path.join(destinationRuntime, 'REA-PACKAGE-LOCK.json')); const files = []; const walk = relative => { for (const entry of fs.readdirSync(path.join(destinationRuntime, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) { const filename = path.join(relative, entry.name); if (entry.isDirectory()) walk(filename); else if (entry.isFile()) files.push({ path: filename.replaceAll('\\', '/'), bytes: fs.statSync(path.join(destinationRuntime, filename)).size, sha256: digest(path.join(destinationRuntime, filename)) }); else throw new Error('REA runtime contains a non-regular entry: ' + filename); } }; for (const item of plan.topLevel) walk(item.path); const manifest = { schemaVersion: 1, name: 'rea-agents', version: REA_VERSION, protocolVersion: REA_PROTOCOL, builtAt: plan.builtAt, installationLock: { path: 'REA-PACKAGE-LOCK.json', sha256: plan.lockSha256 }, packages: plan.packages, absentOptional: plan.absentOptional, files, identityScope: 'Copied installed production package declarations and file snapshots; MCP handshake and static operations are independently verified.', capabilities: ['javascript-asar-static', 'pe-imports-static', 'pe-resources-static'] }; fs.writeFileSync(path.join(destinationRuntime, 'REA-RUNTIME.json'), JSON.stringify(manifest, null, 2) + '\n'); return { version: manifest.version, protocolVersion: manifest.protocolVersion, packages: manifest.packages.length, files: files.length, bytes: files.reduce((sum, item) => sum + item.bytes, 0), lockSha256: plan.lockSha256 }; }