Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: reconcile lockfile-only dependency updates on startup
[._.]/ Adam Eivy9d41966f3183
diff --git a/scripts/ensure-deps.js b/scripts/ensure-deps.jsindex 37168924a4..9085767605 100644--- a/scripts/ensure-deps.js+++ b/scripts/ensure-deps.js@@ -22,8 +22,9 @@ const npmSpawn = (args, options) => {const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');-// Persisted package.json hashes (per workspace) from the last successful install.-// A changed hash means the manifest moved since we last resolved the tree, so an+// Persisted dependency-input hashes (per workspace) from the last successful install.+// Both package.json and package-lock.json select the installed tree.+// A changed hash means an input moved since we last resolved the tree. An// in-place `npm install` over the existing node_modules could leave a stale /// duplicated tree (e.g. a react@18 copy lingering beside react@19 after a major// bump) — which builds fine but throws "Invalid hook call" at runtime. When the@@ -39,10 +40,15 @@ export const WORKSPACES = [{ dir: join(ROOT, 'autofixer'), label: 'autofixer' }];-function pkgHash(dir) {+function dependencyHash(dir) {const pkgPath = join(dir, 'package.json');if (!existsSync(pkgPath)) return null;- return createHash('sha256').update(readFileSync(pkgPath)).digest('hex');+ const lockPath = join(dir, 'package-lock.json');+ // Legacy receipts hashed only package.json and cannot prove which lockfile+ // was installed. The v2 prefix forces one reconciliation on upgrade.+ const hash = createHash('sha256').update(readFileSync(pkgPath)).update('\0');+ hash.update(existsSync(lockPath) ? readFileSync(lockPath) : '<no-lockfile>');+ return `v2:${hash.digest('hex')}`;}function loadHashes() {@@ -66,16 +72,20 @@ function saveHashes(hashes) {// Filesystem fallback for the no-baseline case (first run after this feature// lands, or a fresh manual checkout): npm writes node_modules/.package-lock.json// at the end of every install, so its mtime is the last-install time. If-// package.json was modified more recently — e.g. a `git pull` just brought a-// new manifest over a still-present node_modules — the tree is stale and must-// be clean-reinstalled even though we have no stored hash to compare against.+// either dependency input was modified more recently — e.g. a `git pull`+// brought a new lockfile over a still-present node_modules — clean-reinstall+// even though we have no stored hash to compare against.// Returns false when we can't tell (missing marker, stat error) so we never// wipe a tree we can't prove is stale.-function manifestNewerThanInstall(dir) {+function dependenciesNewerThanInstall(dir) {const markerPath = join(dir, 'node_modules', '.package-lock.json');const installMarker = existsSync(markerPath) ? markerPath : join(dir, 'node_modules');try {- return statSync(join(dir, 'package.json')).mtimeMs > statSync(installMarker).mtimeMs;+ const installedAt = statSync(installMarker).mtimeMs;+ return ['package.json', 'package-lock.json'].some((name) => {+ const input = join(dir, name);+ return existsSync(input) && statSync(input).mtimeMs > installedAt;+ });} catch {return false;}@@ -133,24 +143,24 @@ function main() {let needed = false;for (const { dir, label } of WORKSPACES) {- const currentHash = pkgHash(dir);+ const currentHash = dependencyHash(dir);const nodeModulesMissing = !existsSync(join(dir, 'node_modules'));const storedHash = storedHashes[label];- // With a stored baseline, a differing hash means the manifest moved since the- // last install. Without one (first run after this feature lands, or a fresh+ // With a stored baseline, a differing hash means a dependency input moved+ // since the last install. Without one (first run after this feature lands, or a fresh// manual checkout), fall back to the install-marker mtime so a `git pull` +- // `npm start` that changed package.json over a present node_modules is still- // caught — instead of silently seeding the stale tree.+ // `npm start` catches a changed manifest or lockfile over a present+ // node_modules instead of silently seeding the stale tree.const depsChanged = storedHash != null? currentHash != null && storedHash !== currentHash- : !nodeModulesMissing && manifestNewerThanInstall(dir);+ : !nodeModulesMissing && dependenciesNewerThanInstall(dir);if (nodeModulesMissing || depsChanged) {if (depsChanged) {- // Clean whenever the manifest changed — even if node_modules is already- // gone — so npm rebuilds the tree from the committed lockfile instead of+ // Clean whenever dependency inputs changed — even if node_modules is+ // already gone — so npm rebuilds the tree from the committed lockfile instead of// layering onto a tree resolved against the previous manifest.- console.log(`🧹 ${label} package.json changed since last install — clean reinstall...`);+ console.log(`🧹 ${label} dependency inputs changed since last install — clean reinstall...`);cleanWorkspaceDeps(dir);} else {console.log(`📦 Missing node_modules for ${label} — installing...`);diff --git a/scripts/ensure-deps.test.js b/scripts/ensure-deps.test.jsindex a01610fd0d..ab70eb8e9a 100644--- a/scripts/ensure-deps.test.js+++ b/scripts/ensure-deps.test.js@@ -17,7 +17,8 @@*/import { describe, expect, it } from 'vitest';import { execFileSync } from 'child_process';-import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync, readFileSync } from 'fs';+import { createHash } from 'crypto';+import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync, readFileSync, copyFileSync, utimesSync } from 'fs';import { tmpdir } from 'os';import { dirname, join, relative } from 'path';import { fileURLToPath } from 'url';@@ -84,3 +85,98 @@ describe('clean reinstall keeps the committed lockfile (#5691)', () => {expect(offenders).toEqual([]);});});++// Exercise the startup CLI against a disposable checkout. Only the npm process+// and native rebuild boundary are replaced; selection, cleanup and receipts use+// the real filesystem. No package manager touches this checkout's dependencies.+describe('startup dependency reconciliation', () => {+ function fixture() {+ const root = mkdtempSync(join(tmpdir(), 'portos-deps-startup-'));+ mkdirSync(join(root, 'scripts', 'lib'), { recursive: true });+ mkdirSync(join(root, 'server', 'lib'), { recursive: true });+ writeFileSync(join(root, 'package.json'), '{"type":"module"}');+ copyFileSync(join(REPO_ROOT, 'scripts', 'ensure-deps.js'), join(root, 'scripts', 'ensure-deps.js'));+ copyFileSync(join(REPO_ROOT, 'scripts', 'lib', 'directInvocation.js'), join(root, 'scripts', 'lib', 'directInvocation.js'));+ writeFileSync(join(root, 'scripts', 'trusted-rebuilds.js'), 'export const rebuildTrusted = () => true;');+ writeFileSync(join(root, 'server', 'lib', 'bufferedSpawn.js'), `+ import { fileURLToPath } from 'url';+ export const prepareCliSpawn = (command, args) => ({+ command: process.execPath,+ args: [fileURLToPath(new URL('../../fake-npm.js', import.meta.url)), ...args]+ });+ `);+ writeFileSync(join(root, 'fake-npm.js'), `+ import { mkdirSync, writeFileSync, readFileSync } from 'fs';+ import { join } from 'path';+ const dir = process.cwd();+ for (const file of ['pm2/package.json', 'vite/bin/vite.js', 'express/package.json', 'pg/package.json']) {+ mkdirSync(join(dir, 'node_modules', file, '..'), { recursive: true });+ writeFileSync(join(dir, 'node_modules', file), '{}');+ }+ writeFileSync(join(dir, 'node_modules', 'installed-lock.json'), readFileSync(join(dir, 'package-lock.json')));+ writeFileSync(join(dir, 'node_modules', '.package-lock.json'), '{}');+ `);+ for (const label of ['root', 'client', 'server', 'autofixer']) {+ const dir = label === 'root' ? root : join(root, label);+ mkdirSync(dir, { recursive: true });+ if (label !== 'root') writeFileSync(join(dir, 'package.json'), label === 'server' ? '{"type":"module"}' : '{}');+ writeFileSync(join(dir, 'package-lock.json'), '{"version":"old"}');+ }+ return {+ root,+ run: () => execFileSync(process.execPath, [join(root, 'scripts', 'ensure-deps.js')], { cwd: root, encoding: 'utf8' }),+ client: join(root, 'client'),+ receipt: join(root, 'data', 'deps-hashes.json'),+ };+ }++ it('installs a lockfile-only update once and preserves the committed inputs', () => {+ const f = fixture();+ try {+ f.run();+ const updated = '{"version":"new"}';+ writeFileSync(join(f.client, 'package-lock.json'), updated);+ writeFileSync(join(f.client, 'node_modules', 'stale'), 'old tree');+ f.run();+ expect(readFileSync(join(f.client, 'node_modules', 'installed-lock.json'), 'utf8')).toBe(updated);+ expect(existsSync(join(f.client, 'node_modules', 'stale'))).toBe(false);+ expect(readFileSync(join(f.client, 'package-lock.json'), 'utf8')).toBe(updated);+ expect(readFileSync(join(f.client, 'package.json'), 'utf8')).toBe('{}');+ writeFileSync(join(f.client, 'node_modules', 'keep'), 'unchanged tree');+ expect(f.run()).not.toContain('reinstall');+ expect(existsSync(join(f.client, 'node_modules', 'keep'))).toBe(true);+ } finally {+ rmSync(f.root, { recursive: true, force: true });+ }+ });++ it('detects a newer lockfile without a receipt and upgrades legacy manifest-only receipts', () => {+ const f = fixture();+ try {+ f.run();+ rmSync(f.receipt);+ const updated = '{"version":"new"}';+ writeFileSync(join(f.client, 'package-lock.json'), updated);+ const oldTime = new Date('2020-01-01T00:00:00Z');+ const installTime = new Date('2020-01-02T00:00:00Z');+ const updateTime = new Date('2020-01-03T00:00:00Z');+ utimesSync(join(f.client, 'package.json'), oldTime, oldTime);+ utimesSync(join(f.client, 'node_modules', '.package-lock.json'), installTime, installTime);+ utimesSync(join(f.client, 'package-lock.json'), updateTime, updateTime);+ f.run();+ expect(readFileSync(join(f.client, 'node_modules', 'installed-lock.json'), 'utf8')).toBe(updated);++ // Old installs persist a bare SHA-256 of package.json. They cannot prove+ // which lockfile was installed, so reconcile once before trusting v2.+ const hashes = JSON.parse(readFileSync(f.receipt, 'utf8'));+ hashes.client = createHash('sha256').update('{}').digest('hex');+ writeFileSync(f.receipt, JSON.stringify(hashes));+ writeFileSync(join(f.client, 'node_modules', 'stale'), 'legacy tree');+ f.run();+ expect(existsSync(join(f.client, 'node_modules', 'stale'))).toBe(false);+ expect(f.run()).not.toContain('reinstall');+ } finally {+ rmSync(f.root, { recursive: true, force: true });+ }+ });+});