Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
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',
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 } } } });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,111 @@
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',
};
import publishedManifestFree from '../../../../../e2e/fixtures/plugin-manifest-free/published.json' with {
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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading
Loading