Skip to content

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

fix([issue-9515]): verify client consumers when shared server modules change (#9522)

[._.]/e920099ca7a0
diff --git a/scripts/ci-test-plan.js b/scripts/ci-test-plan.js
index b70dcff197..aa2d000ff1 100644
--- a/scripts/ci-test-plan.js
+++ b/scripts/ci-test-plan.js
@@ -1,12 +1,14 @@
#!/usr/bin/env node
import { execFileSync } from 'child_process';
-import { existsSync } from 'fs';
-import { join } from 'path';
+import { existsSync, readFileSync } from 'fs';
+import { join, posix } from 'path';
+import { staticImportSpecifiersFromSource } from '../server/lib/staticImportGraph.js';
import { isDirectlyInvoked } from './lib/directInvocation.js';
import { writeStepOutput } from './lib/githubOutput.js';
const TEST_FILE_RE = /\.(?:test|spec)\.[cm]?[jt]sx?$/i;
+const MODULE_SOURCE_RE = /\.[cm]?[jt]sx?$/i;
// `git grep` pathspecs matching every extension TEST_FILE_RE recognizes. A
// hardcoded `*.test.js`/`.jsx` pair would silently stop finding a `.test.ts(x)`
// or `.spec.*` contract test added later, reopening the exact under-selection
@@ -602,6 +604,9 @@ export function buildCiTestPlan(changedFiles, {
// sourceReferencePattern). Covers every changed source, not only python
// scripts — see rule 1 of issue #6363.
pathContractTests = {},
+ // Repository-derived browser dependencies; null means discovery failed and
+ // a shared-source change must retain conservative full coverage.
+ clientDependencies = [],
} = {}) {
const changed = uniqueSorted(changedFiles.filter(Boolean));
const trackedSet = new Set(trackedFiles);
@@ -723,6 +728,12 @@ export function buildCiTestPlan(changedFiles, {
// scoping below.
const pythonSources = sourceFiles.filter(isPythonScript);
const jsSources = sourceFiles.filter((path) => !isPythonScript(path));
+ if (clientDependencies === null && jsSources.some((path) => path.startsWith('server/lib/'))) {
+ return fullPlan(changed, 'client dependency discovery unresolved', { appRouteOnly, trackedSet });
+ }
+ const clientDependencySet = new Set(clientDependencies || []);
+ const isClientSource = (path) => path.startsWith('client/') || clientDependencySet.has(path);
+ const sharedClientSources = jsSources.filter((path) => clientDependencySet.has(path));
const features = uniqueSorted(jsSources.map(featureDirectory).filter(Boolean));
const unscopedSources = jsSources.filter((path) => !featureDirectory(path));
const selectedTests = [
@@ -765,16 +776,16 @@ export function buildCiTestPlan(changedFiles, {
}
const hasServerSource = jsSources.some(isServerRunnerFile);
- const hasClientSource = jsSources.some((path) => path.startsWith('client/'));
+ const hasClientSource = jsSources.some(isClientSource);
const hasUnscopedServer = unscopedSources.some(isServerRunnerFile);
- const hasUnscopedClient = unscopedSources.some((path) => path.startsWith('client/'));
+ const hasUnscopedClient = sharedClientSources.length > 0 || unscopedSources.some(isClientSource);
const serverSources = jsSources
.filter(isServerRunnerFile)
.filter((path) => !isStructuralBarrel(path));
const clientSources = jsSources
- .filter((path) => path.startsWith('client/'))
- .filter((path) => !isStructuralBarrel(path));
+ .filter(isClientSource)
+ .filter((path) => clientDependencySet.has(path) || !isStructuralBarrel(path));
// Feature-directory plans already enumerate their boundary tests. Flat and
// shared modules use Vitest's import graph, except a barrel-only edit whose
@@ -938,6 +949,39 @@ export function forceFullReasonFor({ forceFull, baseRef }) {
return null;
}
+// Start from every browser source, including lazy-loaded pages, then follow
+// relative static imports and re-exports across directory boundaries. Parsing
+// is shared with the structural graph guards, but unresolved edges must widen
+// CI instead of disappearing as they do in that helper's negative graph walk.
+function collectClientDependencies(trackedFiles, cwd) {
+ const known = new Set(trackedFiles);
+ const pending = trackedFiles.filter((path) => path.startsWith('client/src/')
+ && MODULE_SOURCE_RE.test(path) && !isTestFile(path));
+ const seen = new Set();
+ const extensions = ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.mts', '.cts', '.json'];
+ try {
+ for (let index = 0; index < pending.length; index++) {
+ const file = pending[index];
+ if (seen.has(file)) continue;
+ seen.add(file);
+ if (!MODULE_SOURCE_RE.test(file)) continue;
+ for (const specifier of staticImportSpecifiersFromSource(readFileSync(join(cwd, file), 'utf8'))) {
+ if (!specifier.startsWith('.')) continue;
+ const path = posix.normalize(posix.join(posix.dirname(file), specifier.split(/[?#]/)[0]));
+ // Keep every resolvable candidate when extensions collide rather than
+ // guessing a bundler-specific precedence and dropping a real consumer.
+ const dependencies = [path, ...extensions.map((extension) => `${path}${extension}`),
+ ...extensions.map((extension) => `${path}/index${extension}`)].filter((candidate) => known.has(candidate));
+ if (dependencies.length === 0) return null;
+ pending.push(...dependencies);
+ }
+ }
+ } catch {
+ return null;
+ }
+ return uniqueSorted([...seen].filter((path) => path.startsWith('server/lib/')));
+}
+
/**
* Collect the repository-derived inputs that surround the pure plan builder.
*
@@ -984,7 +1028,12 @@ export function collectPlanInputs({ baseSha, forceFull = false, changedFiles, cw
.filter((path) => isExecutable(path) && !isTestFile(path))
.map((path) => [path, gitGrepFiles(sourceReferencePattern(path), TEST_FILE_GLOBS, cwd)]));
- return { changedFiles: collectedChangedFiles, trackedFiles: presentFiles, appDiff, pathContractTests };
+ const clientDependencies = collectedChangedFiles.some((path) => path.startsWith('server/lib/')
+ && isExecutable(path) && !isTestFile(path))
+ ? collectClientDependencies(presentFiles, cwd)
+ : [];
+
+ return { changedFiles: collectedChangedFiles, trackedFiles: presentFiles, appDiff, pathContractTests, clientDependencies };
}
function main() {
@@ -1008,7 +1057,7 @@ function main() {
&& (!Array.isArray(changedFilesOverride) || changedFilesOverride.some((path) => typeof path !== 'string'))) {
throw new Error('CI_CHANGED_FILES must be a JSON array of repository-relative path strings.');
}
- const { changedFiles, trackedFiles, appDiff, pathContractTests } = collectPlanInputs({
+ const { changedFiles, trackedFiles, appDiff, pathContractTests, clientDependencies } = collectPlanInputs({
baseSha: base,
forceFull,
changedFiles: changedFilesOverride,
@@ -1019,6 +1068,7 @@ function main() {
forceFullReason,
appRouteOnly: isRouteOnlyAppDiff(appDiff),
pathContractTests,
+ clientDependencies,
}));
}
diff --git a/scripts/ci-test-plan.test.js b/scripts/ci-test-plan.test.js
index 77c1673d89..231ffdcf79 100644
--- a/scripts/ci-test-plan.test.js
+++ b/scripts/ci-test-plan.test.js
@@ -4,6 +4,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
+import { staticImportClosure } from '../server/lib/staticImportGraph.js';
import {
ALWAYS_RUN_TESTS,
@@ -169,6 +170,76 @@ it('builds the same plan from an explicit working-tree override as from the equi
}
});
+it('schedules the real client precision-copy import chain for a shared scoring change', () => {
+ const cwd = fileURLToPath(new URL('../', import.meta.url));
+ const source = 'server/lib/postScoring.js';
+ const inputs = collectPlanInputs({ baseSha: 'HEAD', changedFiles: [source], cwd });
+ const plan = buildCiTestPlan(inputs.changedFiles, inputs);
+
+ expect(plan.full).toBe(false);
+ expect(plan.server.mode).toBe('related');
+ expect(plan.server.sources).toContain(source);
+ expect(plan.client.mode).toBe('related');
+ expect(plan.client.sources).toContain(source);
+ expect(plan.build).toBe(true);
+ // The test names its client helper, not postScoring: only import-graph
+ // selection can reach this regression through the intermediate module.
+ expect(inputs.pathContractTests[source]).not.toContain('client/src/lib/estimationTolerance.test.js');
+ expect(staticImportClosure(join(cwd, 'client/src/lib/estimationTolerance.test.js')).files)
+ .toContain(join(cwd, source));
+});
+
+it('carries transitive browser re-exports through the planner CLI and fails closed on unresolved edges', () => {
+ const root = mkdtempSync(join(tmpdir(), 'portos-ci-browser-deps-'));
+ const planner = fileURLToPath(new URL('./ci-test-plan.js', import.meta.url));
+ const git = (...args) => execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim();
+ const write = (path, source) => {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), source);
+ };
+ const leaf = 'server/lib/shared/leaf.js';
+ const clientSource = 'client/src/lib/consumer.js';
+ const clientTest = 'client/src/lib/consumer.test.js';
+ const serverOnly = 'server/lib/privateOnly.js';
+ const plan = (changedFiles) => JSON.parse(execFileSync(process.execPath, [planner], {
+ cwd: root,
+ encoding: 'utf8',
+ env: { ...process.env, CI_BASE_SHA: 'HEAD', CI_BASE_REF: 'main', CI_FORCE_FULL: 'false',
+ CI_CHANGED_FILES: JSON.stringify(changedFiles), GITHUB_OUTPUT: '', GITHUB_STEP_SUMMARY: '' },
+ }));
+ try {
+ git('init', '-q');
+ write(leaf, 'export const value = 1;\n');
+ write('server/lib/shared/leaf.ts', 'export const value = 3;\n');
+ write('server/lib/bridge.js', "export { value } from './shared/leaf';\n");
+ write(clientSource, "export { value } from '../../../server/lib/bridge.js';\n");
+ write(clientTest, "import { value } from './consumer.js';\n");
+ write(serverOnly, 'export const privateValue = 2;\n');
+ write('client/vite.config.js', 'export default {};\n');
+ git('add', '--all');
+
+ const shared = plan([leaf]);
+ expect(shared).toMatchObject({ full: false, build: true,
+ client_mode: 'related', client_sources: JSON.stringify([leaf]) });
+ expect(shared.server_mode).not.toBe('skip');
+ expect(plan(['server/lib/shared/leaf.ts'])).toMatchObject({ build: true,
+ client_mode: 'related', client_sources: JSON.stringify(['server/lib/shared/leaf.ts']) });
+
+ const mixed = plan([leaf, clientSource, clientTest]);
+ expect(mixed).toMatchObject({ client_mode: 'related', client_sources: JSON.stringify([clientSource, leaf]),
+ client_files: JSON.stringify([clientTest]) });
+ expect(mixed.build).toBe(true);
+ expect(plan([serverOnly])).toMatchObject({ full: false, build: false, client_mode: 'skip' });
+ expect(plan([leaf, 'client/vite.config.js'])).toMatchObject({ full: true, build: true, client_mode: 'full' });
+
+ write('server/lib/bridge.js', "export { value } from './missing.js';\n");
+ expect(plan([leaf])).toMatchObject({ full: true, build: true, client_mode: 'full',
+ reason: 'client dependency discovery unresolved' });
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
describe('CI test impact planner', () => {
it('skips all expensive jobs for documentation-only changes, keeping the always-run guards', () => {
const plan = buildCiTestPlan([
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index c535328e2c..c9bd5853c6 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -118,7 +118,7 @@ describe('pregate hidden-content invocation', () => {
// No runners or test files: docs-only changes produce an empty test plan.
for (const path of [
'scripts/pregate.js', 'scripts/checkNodeVersion.js', 'scripts/scan-diff-hidden-content.js',
- 'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js',
+ 'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js', 'server/lib/staticImportGraph.js',
'scripts/lib/directInvocation.js', 'scripts/lib/githubOutput.js',
'server/lib/diffHiddenContentScan.js', 'server/lib/modelAbuseGuard.js',
'server/lib/textUtils.js',
@@ -397,7 +397,7 @@ describe('the gate end to end', () => {
git('config', 'user.email', '[email protected]');
for (const path of [
'scripts/pregate.js', 'scripts/checkNodeVersion.js', 'scripts/scan-diff-hidden-content.js',
- 'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js',
+ 'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js', 'server/lib/staticImportGraph.js',
'scripts/lib/directInvocation.js', 'scripts/lib/githubOutput.js',
'server/lib/diffHiddenContentScan.js', 'server/lib/modelAbuseGuard.js',
'server/lib/textUtils.js',
diff --git a/scripts/run-ci-tests.js b/scripts/run-ci-tests.js
index b8919cea3b..297409efe4 100644
--- a/scripts/run-ci-tests.js
+++ b/scripts/run-ci-tests.js
@@ -102,8 +102,7 @@ export function requiresSourceFiles(mode, repoSources) {
export function toRunnerPath(scope, path) {
// Prefix in-root selectors so a contributor-controlled filename beginning
// with "-" cannot be interpreted as another Vitest CLI option.
- if (scope === 'client') return `./${path.replace(/^client\//, '')}`;
- if (path.startsWith('server/')) return `./${path.replace(/^server\//, '')}`;
+ if (path.startsWith(`${scope}/`)) return `./${path.slice(scope.length + 1)}`;
return `../${path}`;
}
diff --git a/scripts/run-ci-tests.test.js b/scripts/run-ci-tests.test.js
index e266267ad1..bd8977065b 100644
--- a/scripts/run-ci-tests.test.js
+++ b/scripts/run-ci-tests.test.js
@@ -89,6 +89,7 @@ describe('toRunnerPath', () => {
expect(toRunnerPath('client', 'client/src/lib/index.test.js')).toBe('./src/lib/index.test.js');
expect(toRunnerPath('server', 'server/lib/index.test.js')).toBe('./lib/index.test.js');
expect(toRunnerPath('server', 'scripts/checkNodeVersion.test.js')).toBe('../scripts/checkNodeVersion.test.js');
+ expect(toRunnerPath('client', 'server/lib/postScoring.js')).toBe('../server/lib/postScoring.js');
});
});