diff --git a/packages/cli/src/kernel/adapters/plugin/browser-auth-reconciler_test.ts b/packages/cli/src/kernel/adapters/plugin/browser-auth-reconciler_test.ts index 66d0a3a3d5..ffb24de7da 100644 --- a/packages/cli/src/kernel/adapters/plugin/browser-auth-reconciler_test.ts +++ b/packages/cli/src/kernel/adapters/plugin/browser-auth-reconciler_test.ts @@ -142,6 +142,7 @@ Deno.test('auth install and subsequent service add emit the BFF topology before templateAdapter: templates, formatter, processRunner, + generateRegistries: () => Promise.resolve([]), workspaceMutator: new PluginWorkspaceMutator(fs), registryScaffolder: new PluginRegistryScaffolder(scaffolder), }); diff --git a/packages/cli/src/public/features/generate/plugins/configured-plugin-graph-preparation.ts b/packages/cli/src/public/features/generate/plugins/configured-plugin-graph-preparation.ts new file mode 100644 index 0000000000..00a1fa5a6d --- /dev/null +++ b/packages/cli/src/public/features/generate/plugins/configured-plugin-graph-preparation.ts @@ -0,0 +1,106 @@ +import { join } from '@std/path'; +import { loadProjectConfigForInspection } from '../../../../kernel/adapters/config/project-config-loader.ts'; +import { resolvePluginImportSpecifier } from '../../../../kernel/application/plugin/configured-plugin-specifier.ts'; +import type { FileSystemPort } from '../../../../kernel/ports/file-system-port.ts'; +import type { ProcessPort, ProcessResult } from '../../../../kernel/ports/process-port.ts'; + +const URL_SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]+:/; +const MAX_OUTPUT_BYTES = 1024 * 1024; + +function requireSuccess(result: ProcessResult, stage: string): ProcessResult { + if (result.timedOut) throw new Error(`Plugin graph ${stage} timed out.`); + if (result.code !== 0) { + throw new Error( + `Plugin graph ${stage} failed: ${(result.stderr || result.stdout).slice(0, 4096)}`, + ); + } + return result; +} + +/** Prepare configured public modules only on the normal generation write path. */ +export async function prepareConfiguredPluginGraphs( + projectRoot: string, + fs: FileSystemPort, + process: ProcessPort, +): Promise { + let configFile: string | undefined; + for (const name of ['netscript.config.ts', 'netscript.config.js', 'netscript.config.mjs']) { + if (await fs.exists(join(projectRoot, name))) { + configFile = name; + break; + } + } + if (!configFile) return; + const config = await loadProjectConfigForInspection({ cwd: projectRoot, configFile }, { + process: { + async exec(command, args, options) { + return requireSuccess( + await process.exec(command, args, { + ...options, + timeoutMs: 30_000, + maxOutputBytes: MAX_OUTPUT_BYTES, + }), + 'config loading', + ); + }, + }, + }); + const authored = config.plugins ?? []; + if (authored.some((spec) => !spec.trim() || spec.startsWith('-'))) { + throw new Error('Plugin graph has an invalid configured module specifier.'); + } + let specs = [...new Set(authored.map((spec) => resolvePluginImportSpecifier(projectRoot, spec)))]; + if (!specs.length) return; + let configArgs = ['--no-config']; + for (const name of ['deno.json', 'deno.jsonc']) { + if (await fs.exists(join(projectRoot, name))) { + configArgs = ['--config', join(projectRoot, name)]; + break; + } + } + if (specs.some((spec) => !URL_SCHEME.test(spec))) { + const result = requireSuccess( + await process.exec('deno', [ + 'run', + '--no-check', + '--no-lock', + '--node-modules-dir=none', + '--no-prompt', + '--allow-read', + '--deny-write', + '--deny-run', + '--deny-net', + '--deny-env', + ...configArgs, + '-', + ], { + cwd: projectRoot, + timeoutMs: 30_000, + maxOutputBytes: MAX_OUTPUT_BYTES, + stdin: `console.log(JSON.stringify(${ + JSON.stringify(specs) + }.map(s => import.meta.resolve(s).replace(/^(jsr|npm):\\//, '$1:'))));`, + }), + 'specifier resolution', + ); + const resolved: unknown = JSON.parse(result.stdout); + if ( + !Array.isArray(resolved) || resolved.length !== specs.length || + !resolved.every((spec): spec is string => typeof spec === 'string' && URL_SCHEME.test(spec)) + ) { + throw new Error('Plugin graph specifier resolution returned invalid URLs.'); + } + specs = [...new Set(resolved)]; + } + requireSuccess( + await process.exec('deno', [ + 'cache', + '--no-check', + '--frozen=false', + '--node-modules-dir=none', + ...configArgs, + ...specs, + ], { cwd: projectRoot, timeoutMs: 60_000, maxOutputBytes: MAX_OUTPUT_BYTES }), + 'caching', + ); +} diff --git a/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator.ts b/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator.ts index 69f4dbd966..2a66761e7c 100644 --- a/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator.ts +++ b/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator.ts @@ -23,6 +23,7 @@ import { } from './plugin-registry-renderer.ts'; import { frozenPluginInspection } from '../../../../kernel/adapters/runtime/process/frozen-plugin-inspection.ts'; import { removeInactivePluginRegistries } from './plugin-registry-writer.ts'; +import { prepareConfiguredPluginGraphs } from './configured-plugin-graph-preparation.ts'; import type { GeneratedPluginRegistry, GenerateInstalledPluginRegistries, @@ -145,6 +146,7 @@ export function createInstalledRuntimeRegistryGenerator( await dependencies.fs.writeFile(join(input.projectRoot, file.path), file.content); } for (const write of legacy) await write(); + await prepareConfiguredPluginGraphs(input.projectRoot, dependencies.fs, dependencies.process); } return generated; diff --git a/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator_test.ts b/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator_test.ts index a2c0ecbc5c..28bb131b2e 100644 --- a/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator_test.ts +++ b/packages/cli/src/public/features/generate/plugins/installed-runtime-registry-generator_test.ts @@ -1,5 +1,6 @@ import { assertEquals, assertRejects } from '@std/assert'; import { describe, it } from '@std/testing/bdd'; +import { join, toFileUrl } from '@std/path'; import type { DirEntry, FileInfo, WalkEntry } from '../../../../kernel/domain/core-types.ts'; import type { FileSystemPort } from '../../../../kernel/ports/file-system-port.ts'; @@ -529,6 +530,106 @@ describe('installed runtime registry generator', () => { }); }); +Deno.test('normal generation prepares configured module graphs; dry-run never evaluates config', async () => { + const root = Deno.cwd(); + const fs = new MemoryFileSystem({ + [join(root, 'appsettings.json')]: '{}', + [join(root, 'deno.json')]: '{}', + [join(root, 'netscript.config.ts')]: 'export default {};', + }); + const calls: string[][] = []; + const process: ProcessPort = { + exec(_command, args) { + calls.push([...args]); + return Promise.resolve({ + code: 0, + stderr: '', + stdout: args[0] === 'cache' ? '' : JSON.stringify({ plugins: ['./plugin.ts'] }), + }); + }, + }; + const generate = createInstalledRuntimeRegistryGenerator({ + fs, + process, + fetchManifest: () => Promise.resolve(jsonResponse({})), + }); + await generate({ projectRoot: root, dryRun: true }); + assertEquals(calls, []); + await generate({ projectRoot: root, dryRun: false }); + assertEquals(calls.length, 2); + assertEquals(calls[1][0], 'cache'); + assertEquals(calls[1].includes('--'), false); + assertEquals(calls[1].at(-1), toFileUrl(join(root, 'plugin.ts')).href); +}); + +for (const filename of ['netscript.config.ts', 'netscript.config.js', 'netscript.config.mjs']) { + Deno.test(`graph preparation handles ${filename}, aliases, empty lists and failures`, async () => { + const root = Deno.cwd(); + const fs = new MemoryFileSystem({ + [join(root, filename)]: '', + [join(root, 'deno.json')]: '{}', + [join(root, 'appsettings.json')]: '{}', + }); + for ( + const scenario of [ + 'alias', + 'empty', + 'invalid', + 'config-timeout', + 'resolve-timeout', + 'cache-timeout', + 'cache-error', + 'malformed', + ] + ) { + const stages: string[] = []; + const process: ProcessPort = { + exec(_command, args, options) { + const stage = args[0] === 'cache' ? 'cache' : args.at(-1) === '-' ? 'resolve' : 'config'; + stages.push(stage); + assertEquals(options?.timeoutMs, stage === 'cache' ? 60_000 : 30_000); + assertEquals(options?.maxOutputBytes, 1024 * 1024); + if (scenario === `${stage}-timeout`) { + return Promise.resolve({ code: 0, timedOut: true, stdout: '{}', stderr: '' }); + } + if (scenario === 'cache-error' && stage === 'cache') { + return Promise.resolve({ code: 1, stdout: '', stderr: 'cache failed' }); + } + return Promise.resolve({ + code: 0, + stderr: '', + stdout: stage === 'config' + ? JSON.stringify({ + plugins: scenario === 'empty' + ? [] + : scenario === 'invalid' + ? ['--reload'] + : ['alias/subpath'], + }) + : scenario === 'malformed' + ? 'null' + : '["jsr:@fixture/plugin@1.0.0/subpath"]', + }); + }, + }; + const generate = createInstalledRuntimeRegistryGenerator({ + fs, + process, + fetchManifest: () => Promise.resolve(jsonResponse({})), + }); + if (scenario === 'alias' || scenario === 'empty') { + await generate({ projectRoot: root, dryRun: false }); + assertEquals(stages, scenario === 'empty' ? ['config'] : ['config', 'resolve', 'cache']); + } else { + await assertRejects(() => generate({ projectRoot: root, dryRun: false })); + if (scenario === 'invalid' || scenario === 'config-timeout') { + assertEquals(stages, ['config']); + } + } + } + }); +} + function appsettings(name: string, entrypoint: string): string { return JSON.stringify({ NetScript: { Plugins: { [name]: { Entrypoint: entrypoint } } } }); } diff --git a/packages/cli/src/public/features/generate/plugins/installed-saga-declarations_test.ts b/packages/cli/src/public/features/generate/plugins/installed-saga-declarations_test.ts index 20aa7c65ed..4714a9a49d 100644 --- a/packages/cli/src/public/features/generate/plugins/installed-saga-declarations_test.ts +++ b/packages/cli/src/public/features/generate/plugins/installed-saga-declarations_test.ts @@ -72,12 +72,16 @@ export default defineAgentActionSaga(); Deno.test('installed root-config saga declaration outside scan roots is authoritative for doctor', async () => { await installedProject({ 'netscript.config.ts': - "throw new Error('static only');\nexport const entry = defineSagaConfig('agent-action', './plugins/sagas/agent-action.ts').build();", + "export default {name: 'saga-fixture', databases: {config: []}};\nconst defineSagaConfig = (..._args) => ({build: () => ({})});\nexport const entry = defineSagaConfig('agent-action', './plugins/sagas/agent-action.ts').build();", 'plugins/sagas/agent-action.ts': `export default { id: 'agent-action', durability: 't1', initialState: {}, handledMessageTypes: [], handlers: new Map(), };`, }, async (projectRoot) => { await generate({ dryRun: false, projectRoot }); + await write( + join(projectRoot, 'netscript.config.ts'), + "throw new Error('static only');\nexport const entry = defineSagaConfig('agent-action', './plugins/sagas/agent-action.ts').build();", + ); const registries = await generate({ dryRun: true, projectRoot }); assertEquals(registries[0].sourceFiles, ['plugins/sagas/agent-action.ts']); const checks = await checkRuntimeRegistryDrift({ fs, projectRoot, registries }); @@ -134,6 +138,7 @@ async function installedProject( catalog: config.catalog, imports: { ...config.imports, + '@netscript/config': toFileUrl(join(REPOSITORY_ROOT, 'packages/config/mod.ts')).href, '@netscript/plugin/cli': toFileUrl(join(REPOSITORY_ROOT, 'packages/plugin/src/cli/mod.ts')).href, '@netscript/plugin-sagas/runtime': diff --git a/packages/cli/src/public/features/generate/plugins/plugin-check-command_test.ts b/packages/cli/src/public/features/generate/plugins/plugin-check-command_test.ts index 008bf55585..132a10e724 100644 --- a/packages/cli/src/public/features/generate/plugins/plugin-check-command_test.ts +++ b/packages/cli/src/public/features/generate/plugins/plugin-check-command_test.ts @@ -1,7 +1,7 @@ import { netscriptJsrSpecifier } from '../../../../kernel/constants/jsr-specifiers.ts'; import { assert, assertEquals, assertMatch } from '@std/assert'; import { dirname, fromFileUrl, join, resolve, toFileUrl } from '@std/path'; -import { walk } from '@std/fs'; +import { expandGlob, walk } from '@std/fs'; import schema from '../../../../../assets/schema/plugin-generated-surface.v1.json' with { type: 'json', }; @@ -9,12 +9,103 @@ import publishedManifestFree from '../../../../../e2e/fixtures/plugin-manifest-f type: 'json', }; import type { PluginSurfaceReport } from '../../../../kernel/domain/plugin-generated-surface.ts'; +import { DenoFileSystem } from '../../../../kernel/adapters/runtime/file-system/deno-file-system.ts'; +import { DenoProcess } from '../../../../kernel/adapters/runtime/process/deno-process.ts'; +import { createInstalledRuntimeRegistryGenerator } from './installed-runtime-registry-generator.ts'; +import { probeConfiguredPluginManifest } from '../../../../kernel/adapters/config/plugin-manifest/configured-plugin-manifest-probe.ts'; const REPO = resolve(dirname(fromFileUrl(import.meta.url)), '../../../../../../..'); const CLI = Deno.env.get('NETSCRIPT_PLUGIN_CHECK_CLI') ?? join(REPO, 'packages/cli/bin/netscript.ts'); const OUTPUT = '.netscript/generated/plugin-triggers/triggers.registry.ts'; +Deno.test('normal generation prepares real published manifest graph with authored lock paths', async () => { + const workspace = JSON.parse(await Deno.readTextFile(join(REPO, 'deno.json'))); + const exclude: string[] = []; + for (const member of workspace.workspace) { + for await (const entry of expandGlob(`${member}/deno.json`, { root: REPO })) { + const pkg = JSON.parse(await Deno.readTextFile(entry.path)); + if (pkg.name?.startsWith('@netscript/')) exclude.push(`jsr:${pkg.name}`); + } + } + for (const lock of [undefined, 'locks/consumer.lock', { path: 'locks/object.lock' }]) { + const root = await Deno.makeTempDir({ prefix: 'plugin-graph-' }); + try { + const version = '0.0.10-canary.1'; + await write( + root, + 'deno.json', + JSON.stringify({ + lock, + nodeModulesDir: 'none', + minimumDependencyAge: { age: 'P1D', exclude }, + imports: { + '@netscript/config': `jsr:@netscript/config@${version}`, + '@netscript/plugin-workers': `jsr:@netscript/plugin-workers@${version}`, + '@netscript/plugin-streams': `jsr:@netscript/plugin-streams@${version}`, + }, + }), + ); + await write(root, 'appsettings.json', '{}'); + await write( + root, + 'netscript.config.ts', + "export default {name: 'graph-fixture', databases: {config: []}, plugins: ['@netscript/plugin-workers']};", + ); + await write( + root, + 'services.ts', + `import 'jsr:@netscript/cli@${version}';\nimport '@netscript/plugin-workers/services';\nimport '@netscript/plugin-streams/services';`, + ); + const seeded = await new Deno.Command(Deno.execPath(), { + args: ['cache', '--config', join(root, 'deno.json'), join(root, 'services.ts')], + cwd: root, + stdout: 'piped', + stderr: 'piped', + }).output(); + assertEquals(seeded.code, 0, new TextDecoder().decode(seeded.stderr)); + const process = new DenoProcess(); + const red = await probeConfiguredPluginManifest(root, '@netscript/plugin-workers', process, { + timeoutMs: 30_000, + }); + assertEquals(red.status, 'import-failure', JSON.stringify(red)); + const generate = createInstalledRuntimeRegistryGenerator({ + fs: new DenoFileSystem(), + process, + fetchManifest: (url) => fetch(url), + }); + await generate({ projectRoot: root, dryRun: false }); + const before = await snapshot(root); + const green = await probeConfiguredPluginManifest( + root, + '@netscript/plugin-workers', + process, + { timeoutMs: 30_000 }, + ); + assertEquals(green.status, 'resolved', JSON.stringify(green)); + assertEquals(await snapshot(root), before); + await generate({ projectRoot: root, dryRun: false }); + assertEquals(await snapshot(root), before); + const report = await inspect(root); + assertEquals(report.status, 'current', JSON.stringify(report)); + assertEquals(await snapshot(root), before); + await write( + root, + 'throwing.ts', + "throw new Error('plugin must not execute during caching');", + ); + await write( + root, + 'netscript.config.ts', + "export default {name: 'graph-fixture', databases: {config: []}, plugins: ['./throwing.ts']};", + ); + await generate({ projectRoot: root, dryRun: false }); + } finally { + await Deno.remove(root, { recursive: true }); + } + } +}); + async function command(root: string, check = true) { return await new Deno.Command(Deno.execPath(), { args: [ diff --git a/packages/cli/src/public/features/plugins/install/install-plugin-command_test.ts b/packages/cli/src/public/features/plugins/install/install-plugin-command_test.ts index 94ba151033..18a3195a73 100644 --- a/packages/cli/src/public/features/plugins/install/install-plugin-command_test.ts +++ b/packages/cli/src/public/features/plugins/install/install-plugin-command_test.ts @@ -97,6 +97,7 @@ async function runStreamsInstall(hostPort?: number, browserTopology?: string): P workspaceMutator: new PluginWorkspaceMutator(fs), processRunner: new DenoProcess(), regenerateHelpers: () => Promise.resolve([]), + generateRegistries: () => Promise.resolve([]), }, }).parse(args); diff --git a/packages/cli/src/public/features/plugins/install/install-plugin_test.ts b/packages/cli/src/public/features/plugins/install/install-plugin_test.ts index f187b996df..88249e90a1 100644 --- a/packages/cli/src/public/features/plugins/install/install-plugin_test.ts +++ b/packages/cli/src/public/features/plugins/install/install-plugin_test.ts @@ -41,6 +41,7 @@ import { importEntryForDependency } from '../../../../kernel/application/ui/regi import { loadRegisteredPlugins } from '../../../../kernel/adapters/config/plugin-registry.ts'; import { loadConfig } from '@netscript/config'; import { useLocalWorkspaceImports } from '../../../../../tests/support/local-workspace-imports.ts'; +import { createInstalledRuntimeRegistryGenerator } from '../../generate/plugins/installed-runtime-registry-generator.ts'; // This flow renders plugin/workspace files via sync template generators, which // require a previously-awaited registry hydration. The test drives the flow @@ -952,10 +953,15 @@ describe('public install plugin flow', () => { const workersRoot = repoPath('plugins/workers'); try { await writeRealProjectFiles(projectRoot); + await Deno.writeTextFile( + join(projectRoot, 'netscript.config.ts'), + "export default { name: 'install-order', databases: {config: []}, plugins: [] };\n", + ); const fs = new DenoFileSystem(); const templateAdapter = new StringTemplateAdapter(fs); const scaffolder = new Scaffolder(templateAdapter, fs); const registry = new PluginKindRegistry(); + let prepared = false; const result = await installPlugin({ kind: 'workers', @@ -977,8 +983,28 @@ describe('public install plugin flow', () => { workspaceMutator: new PluginWorkspaceMutator(fs), processRunner: new DenoProcess(), regenerateHelpers: () => Promise.resolve([]), + generateRegistries: async (input) => { + assertEquals(input, { projectRoot, dryRun: false }); + const config = JSON.parse(await fs.readFile(join(projectRoot, 'deno.json'))); + assert(config.imports['@netscript/plugin-workers']); + assert(Array.isArray(config.workspace)); + assertStringIncludes(await fs.readFile(join(projectRoot, 'appsettings.json')), 'workers'); + assertStringIncludes( + await fs.readFile(join(projectRoot, 'netscript.config.ts')), + './workers/plugin.ts', + ); + await useLocalWorkspaceImports(projectRoot, REPO_ROOT); + const output = await createInstalledRuntimeRegistryGenerator({ + fs, + process: new DenoProcess(), + fetchManifest: (url) => fetch(url), + })(input); + prepared = true; + return output; + }, }); + assert(prepared); assertEquals(result.pluginOwnedScaffold?.status, 'applied'); assertEquals(result.pluginOwnedScaffold?.databaseMigrationsAdded, false); assertStringIncludes( diff --git a/packages/cli/src/public/features/plugins/remove/remove-plugin_test.ts b/packages/cli/src/public/features/plugins/remove/remove-plugin_test.ts index 98148c7c3d..62f823cf89 100644 --- a/packages/cli/src/public/features/plugins/remove/remove-plugin_test.ts +++ b/packages/cli/src/public/features/plugins/remove/remove-plugin_test.ts @@ -267,6 +267,8 @@ Deno.test('public plugin install then bare-name remove restores owned state and workspaceMutator: new PluginWorkspaceMutator(fs), processRunner: process, regenerateHelpers: () => Promise.resolve([]), + // This round-trip owns removal; the registry sentinel is authored explicitly below. + generateRegistries: () => Promise.resolve([]), }, }).parse([ 'stream', diff --git a/packages/mcp/tests/project-wiring-doctor-family_test.ts b/packages/mcp/tests/project-wiring-doctor-family_test.ts index b24bff7018..b53da89237 100644 --- a/packages/mcp/tests/project-wiring-doctor-family_test.ts +++ b/packages/mcp/tests/project-wiring-doctor-family_test.ts @@ -69,7 +69,9 @@ async function snapshot(root: string) { for await (const entry of Deno.readDir(directory)) { const path = join(directory, entry.name); if (entry.isDirectory) await visit(path); - else files[path.slice(root.length + 1)] = [...await Deno.readFile(path)]; + else {files[path.slice(root.length + 1).replaceAll('\\', '/')] = [ + ...await Deno.readFile(path), + ];} } } await visit(root); @@ -141,7 +143,17 @@ for ( assertStringIncludes(check?.summary ?? '', `${plugins.length} installed producer(s)`); assertEquals(check?.fix, undefined); assertEquals(await snapshot(root), before); - await generate(root); + // These sources intentionally exercise static inspection, not executable config loading. + // Normal generation requires a valid authored config; restore the static probe input after it. + await Deno.writeTextFile( + join(root, 'netscript.config.ts'), + "export default { name: 'inventory-fixture', databases: { config: [] }, plugins: [] };\n", + ); + try { + await generate(root); + } finally { + await Deno.writeTextFile(join(root, 'netscript.config.ts'), source); + } assertEquals(await inspect(root, executor), check); assertEquals(await snapshot(root), before); });