2026-05-10 23:12:26 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
import { createHash } from 'node:crypto';
|
|
|
|
|
import { execFile } from 'node:child_process';
|
|
|
|
|
import { access, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
|
|
|
|
import { tmpdir } from 'node:os';
|
2026-05-11 15:50:34 +02:00
|
|
|
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
import {
|
|
|
|
|
RUNTIME_WHEEL_DISTRIBUTION_NAME,
|
|
|
|
|
RUNTIME_WHEEL_NORMALIZED_NAME,
|
|
|
|
|
RUNTIME_WHEEL_PACKAGE_VERSION,
|
|
|
|
|
} from './build-python-runtime-wheel.mjs';
|
|
|
|
|
import {
|
|
|
|
|
PUBLIC_NPM_PACKAGE_NAME,
|
|
|
|
|
PUBLIC_NPM_PACKAGE_VERSION,
|
|
|
|
|
publicNpmPackageTarballName,
|
|
|
|
|
} from './build-public-npm-package.mjs';
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
RUNTIME_WHEEL_DISTRIBUTION_NAME,
|
|
|
|
|
RUNTIME_WHEEL_NORMALIZED_NAME,
|
|
|
|
|
RUNTIME_WHEEL_PACKAGE_VERSION,
|
|
|
|
|
};
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
export const INTERNAL_NPM_WORKSPACE_PACKAGES = [
|
2026-05-10 23:51:24 +02:00
|
|
|
{ name: '@ktx/context', packageRoot: 'packages/context' },
|
|
|
|
|
{ name: '@ktx/llm', packageRoot: 'packages/llm' },
|
|
|
|
|
{ name: '@ktx/connector-bigquery', packageRoot: 'packages/connector-bigquery' },
|
|
|
|
|
{ name: '@ktx/connector-clickhouse', packageRoot: 'packages/connector-clickhouse' },
|
|
|
|
|
{ name: '@ktx/connector-mysql', packageRoot: 'packages/connector-mysql' },
|
|
|
|
|
{ name: '@ktx/connector-postgres', packageRoot: 'packages/connector-postgres' },
|
|
|
|
|
{ name: '@ktx/connector-snowflake', packageRoot: 'packages/connector-snowflake' },
|
|
|
|
|
{ name: '@ktx/connector-sqlite', packageRoot: 'packages/connector-sqlite' },
|
|
|
|
|
{ name: '@ktx/connector-sqlserver', packageRoot: 'packages/connector-sqlserver' },
|
|
|
|
|
{ name: '@ktx/cli', packageRoot: 'packages/cli' },
|
2026-05-10 23:12:26 +02:00
|
|
|
];
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
export const NPM_ARTIFACT_PACKAGES = [{ name: PUBLIC_NPM_PACKAGE_NAME, packageRoot: 'packages/cli' }];
|
|
|
|
|
|
|
|
|
|
export const CLI_PYTHON_ASSET_MANIFEST = 'manifest.json';
|
|
|
|
|
|
|
|
|
|
const CONNECTOR_PACKAGE_NAMES = INTERNAL_NPM_WORKSPACE_PACKAGES
|
2026-05-10 23:12:26 +02:00
|
|
|
.map((packageInfo) => packageInfo.name)
|
2026-05-10 23:51:24 +02:00
|
|
|
.filter((packageName) => packageName.startsWith('@ktx/connector-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
const NPM_ARTIFACT_BUILD_ORDER = ['@ktx/llm', '@ktx/context', ...CONNECTOR_PACKAGE_NAMES, '@ktx/cli'];
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
function scriptRootDir() {
|
|
|
|
|
return resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function npmPackageTarballName(packageName) {
|
2026-05-11 15:50:34 +02:00
|
|
|
if (packageName !== PUBLIC_NPM_PACKAGE_NAME) {
|
|
|
|
|
throw new Error(`Unsupported npm artifact package: ${packageName}`);
|
|
|
|
|
}
|
|
|
|
|
return publicNpmPackageTarballName(PUBLIC_NPM_PACKAGE_VERSION);
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function npmPackageTarballs(npmDir) {
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
NPM_ARTIFACT_PACKAGES.map((packageInfo) => [packageInfo.name, join(npmDir, npmPackageTarballName(packageInfo.name))]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function packageArtifactLayout(rootDir = scriptRootDir()) {
|
|
|
|
|
const artifactDir = join(rootDir, 'dist', 'artifacts');
|
|
|
|
|
const npmDir = join(artifactDir, 'npm');
|
|
|
|
|
const pythonDir = join(artifactDir, 'python');
|
|
|
|
|
const npmTarballs = npmPackageTarballs(npmDir);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
rootDir,
|
|
|
|
|
artifactDir,
|
|
|
|
|
npmDir,
|
|
|
|
|
pythonDir,
|
|
|
|
|
npmTarballs,
|
2026-05-11 15:50:34 +02:00
|
|
|
contextTarball: npmTarballs[PUBLIC_NPM_PACKAGE_NAME],
|
|
|
|
|
cliTarball: npmTarballs[PUBLIC_NPM_PACKAGE_NAME],
|
|
|
|
|
connectorTarballs: {},
|
2026-05-10 23:12:26 +02:00
|
|
|
manifestPath: join(artifactDir, 'manifest.json'),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function buildArtifactCommands(layout) {
|
2026-05-11 15:50:34 +02:00
|
|
|
const packagesByName = new Map(INTERNAL_NPM_WORKSPACE_PACKAGES.map((packageInfo) => [packageInfo.name, packageInfo]));
|
|
|
|
|
const npmBuildCommands = NPM_ARTIFACT_BUILD_ORDER.map((packageName) => {
|
|
|
|
|
const packageInfo = packagesByName.get(packageName);
|
|
|
|
|
if (!packageInfo) {
|
|
|
|
|
throw new Error(`Unknown npm artifact build package: ${packageName}`);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
command: 'pnpm',
|
|
|
|
|
args: ['--filter', packageInfo.name, 'run', 'build'],
|
|
|
|
|
cwd: layout.rootDir,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
const publicPackageCommand = {
|
|
|
|
|
command: process.execPath,
|
|
|
|
|
args: ['scripts/build-public-npm-package.mjs'],
|
2026-05-10 23:12:26 +02:00
|
|
|
cwd: layout.rootDir,
|
2026-05-11 15:50:34 +02:00
|
|
|
};
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
...npmBuildCommands,
|
|
|
|
|
{
|
2026-05-11 15:50:34 +02:00
|
|
|
command: process.execPath,
|
|
|
|
|
args: ['scripts/build-python-runtime-wheel.mjs'],
|
2026-05-10 23:12:26 +02:00
|
|
|
cwd: layout.rootDir,
|
|
|
|
|
},
|
2026-05-11 15:50:34 +02:00
|
|
|
publicPackageCommand,
|
2026-05-10 23:12:26 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function pathExists(path) {
|
|
|
|
|
try {
|
|
|
|
|
await access(path);
|
|
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function assertPathExists(path, label) {
|
|
|
|
|
if (!(await pathExists(path))) {
|
|
|
|
|
throw new Error(`Missing ${label}: ${path}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizePythonDistributionName(name) {
|
|
|
|
|
return name.replaceAll('-', '_');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
function findOne(files, distributionName, suffix, label, pythonDir, version) {
|
2026-05-10 23:12:26 +02:00
|
|
|
const normalized = normalizePythonDistributionName(distributionName);
|
2026-05-11 15:50:34 +02:00
|
|
|
const found = files.find((file) => file.startsWith(`${normalized}-${version}`) && file.endsWith(suffix));
|
2026-05-10 23:12:26 +02:00
|
|
|
if (!found) {
|
|
|
|
|
throw new Error(`Missing Python artifact: ${label}`);
|
|
|
|
|
}
|
|
|
|
|
return join(pythonDir, found);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findPythonArtifacts(pythonDir) {
|
|
|
|
|
const files = await readdir(pythonDir);
|
|
|
|
|
|
|
|
|
|
return {
|
2026-05-11 15:50:34 +02:00
|
|
|
runtimeWheel: findOne(
|
|
|
|
|
files,
|
|
|
|
|
RUNTIME_WHEEL_DISTRIBUTION_NAME,
|
|
|
|
|
'.whl',
|
|
|
|
|
'kaelio-ktx runtime wheel',
|
|
|
|
|
pythonDir,
|
|
|
|
|
RUNTIME_WHEEL_PACKAGE_VERSION,
|
|
|
|
|
),
|
2026-05-10 23:12:26 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function artifactManifestPath(layout) {
|
|
|
|
|
return layout.manifestPath ?? join(layout.artifactDir, 'manifest.json');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readJson(path) {
|
|
|
|
|
return JSON.parse(await readFile(path, 'utf-8'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function releaseMetadataEntry({ ecosystem, packageName, packageRoot, packageVersion, privatePackage }) {
|
|
|
|
|
return {
|
|
|
|
|
ecosystem,
|
|
|
|
|
packageName,
|
|
|
|
|
packageRoot,
|
|
|
|
|
packageVersion,
|
|
|
|
|
private: privatePackage,
|
|
|
|
|
releaseMode: 'ci-artifact-only',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readNpmPackageMetadata(rootDir, packageInfo) {
|
|
|
|
|
const packageJson = await readJson(join(rootDir, packageInfo.packageRoot, 'package.json'));
|
2026-05-11 15:50:34 +02:00
|
|
|
const expectedSourceName = packageInfo.name === PUBLIC_NPM_PACKAGE_NAME ? '@ktx/cli' : packageInfo.name;
|
|
|
|
|
if (packageJson.name !== expectedSourceName) {
|
2026-05-10 23:12:26 +02:00
|
|
|
throw new Error(
|
2026-05-11 15:50:34 +02:00
|
|
|
`Unexpected package name in ${packageInfo.packageRoot}/package.json: expected ${expectedSourceName}, got ${packageJson.name}`,
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
}
|
2026-05-11 15:50:34 +02:00
|
|
|
const isPublicKtxPackage = packageInfo.name === PUBLIC_NPM_PACKAGE_NAME;
|
2026-05-10 23:12:26 +02:00
|
|
|
return releaseMetadataEntry({
|
|
|
|
|
ecosystem: 'npm',
|
2026-05-11 15:50:34 +02:00
|
|
|
packageName: packageInfo.name,
|
2026-05-10 23:12:26 +02:00
|
|
|
packageRoot: packageInfo.packageRoot,
|
2026-05-11 15:50:34 +02:00
|
|
|
packageVersion: isPublicKtxPackage ? PUBLIC_NPM_PACKAGE_VERSION : packageJson.version,
|
|
|
|
|
privatePackage: isPublicKtxPackage ? false : packageJson.private === true,
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function packageReleaseMetadata(rootDir = scriptRootDir()) {
|
|
|
|
|
const npmPackages = await Promise.all(
|
|
|
|
|
NPM_ARTIFACT_PACKAGES.map((packageInfo) => readNpmPackageMetadata(rootDir, packageInfo)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
...npmPackages,
|
|
|
|
|
releaseMetadataEntry({
|
|
|
|
|
ecosystem: 'python',
|
2026-05-11 15:50:34 +02:00
|
|
|
packageName: RUNTIME_WHEEL_DISTRIBUTION_NAME,
|
|
|
|
|
packageRoot: 'python/runtime-wheel',
|
|
|
|
|
packageVersion: RUNTIME_WHEEL_PACKAGE_VERSION,
|
2026-05-10 23:12:26 +02:00
|
|
|
privatePackage: false,
|
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function packageMetadataByName(packages) {
|
|
|
|
|
return new Map(packages.map((metadata) => [metadata.packageName, metadata]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requirePackageMetadata(packagesByName, packageName) {
|
|
|
|
|
const metadata = packagesByName.get(packageName);
|
|
|
|
|
if (!metadata) {
|
|
|
|
|
throw new Error(`Missing package release metadata for ${packageName}`);
|
|
|
|
|
}
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function artifactPackageRecords(layout, pythonArtifacts, packages) {
|
|
|
|
|
const packagesByName = packageMetadataByName(packages);
|
|
|
|
|
const npmRecords = NPM_ARTIFACT_PACKAGES.map((packageInfo) => ({
|
|
|
|
|
artifactKind: 'tarball',
|
|
|
|
|
artifactPath: layout.npmTarballs[packageInfo.name],
|
|
|
|
|
metadata: requirePackageMetadata(packagesByName, packageInfo.name),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
...npmRecords,
|
|
|
|
|
{
|
|
|
|
|
artifactKind: 'wheel',
|
2026-05-11 15:50:34 +02:00
|
|
|
artifactPath: pythonArtifacts.runtimeWheel,
|
|
|
|
|
metadata: requirePackageMetadata(packagesByName, RUNTIME_WHEEL_DISTRIBUTION_NAME),
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function artifactRelativePath(layout, artifactPath) {
|
|
|
|
|
return relative(layout.artifactDir, artifactPath).split(sep).join('/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatJson(value) {
|
|
|
|
|
return JSON.stringify(value, null, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assertJsonEqual(actual, expected, label) {
|
|
|
|
|
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
|
|
|
|
|
throw new Error(`${label} do not match\nExpected:\n${formatJson(expected)}\nActual:\n${formatJson(actual)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPlainObject(value) {
|
|
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assertString(value, label) {
|
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
|
throw new Error(`${label} must be a string`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function artifactPathFromManifest(layout, manifestPath) {
|
|
|
|
|
assertString(manifestPath, 'Artifact manifest file path');
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
manifestPath.length === 0 ||
|
|
|
|
|
manifestPath.startsWith('/') ||
|
|
|
|
|
manifestPath.includes('\\') ||
|
|
|
|
|
manifestPath.split('/').some((part) => part.length === 0 || part === '..')
|
|
|
|
|
) {
|
|
|
|
|
throw new Error(`Unsafe artifact manifest path: ${manifestPath}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resolvedPath = resolve(layout.artifactDir, manifestPath);
|
|
|
|
|
const relativePath = relative(layout.artifactDir, resolvedPath);
|
|
|
|
|
if (relativePath.startsWith('..') || isAbsolute(relativePath)) {
|
|
|
|
|
throw new Error(`Unsafe artifact manifest path: ${manifestPath}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolvedPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortedManifestFiles(files) {
|
|
|
|
|
return [...files].sort((a, b) => a.path.localeCompare(b.path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function assertManifestShape(manifest) {
|
|
|
|
|
if (!isPlainObject(manifest)) {
|
|
|
|
|
throw new Error('Artifact manifest must be a JSON object');
|
|
|
|
|
}
|
|
|
|
|
if (manifest.schemaVersion !== 2) {
|
|
|
|
|
throw new Error(`Unsupported artifact manifest schemaVersion: ${manifest.schemaVersion}`);
|
|
|
|
|
}
|
|
|
|
|
assertString(manifest.generatedAt, 'Artifact manifest generatedAt');
|
|
|
|
|
if (Number.isNaN(Date.parse(manifest.generatedAt))) {
|
|
|
|
|
throw new Error(`Artifact manifest generatedAt is not an ISO timestamp: ${manifest.generatedAt}`);
|
|
|
|
|
}
|
|
|
|
|
if (manifest.sourceRevision !== null && typeof manifest.sourceRevision !== 'string') {
|
|
|
|
|
throw new Error('Artifact manifest sourceRevision must be a string or null');
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(manifest.packages)) {
|
|
|
|
|
throw new Error('Artifact manifest packages must be an array');
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(manifest.files)) {
|
|
|
|
|
throw new Error('Artifact manifest files must be an array');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function artifactManifestEntry(layout, record) {
|
|
|
|
|
const contents = await readFile(record.artifactPath);
|
|
|
|
|
return {
|
|
|
|
|
path: artifactRelativePath(layout, record.artifactPath),
|
|
|
|
|
ecosystem: record.metadata.ecosystem,
|
|
|
|
|
artifactKind: record.artifactKind,
|
|
|
|
|
packageName: record.metadata.packageName,
|
|
|
|
|
packageVersion: record.metadata.packageVersion,
|
|
|
|
|
bytes: contents.byteLength,
|
|
|
|
|
sha256: createHash('sha256').update(contents).digest('hex'),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function buildArtifactManifest(layout, generatedAt = new Date(), options = {}) {
|
|
|
|
|
const pythonArtifacts = await findPythonArtifacts(layout.pythonDir);
|
|
|
|
|
const packages = await packageReleaseMetadata(layout.rootDir);
|
|
|
|
|
const artifactRecords = artifactPackageRecords(layout, pythonArtifacts, packages);
|
|
|
|
|
const files = await Promise.all(artifactRecords.map((record) => artifactManifestEntry(layout, record)));
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
schemaVersion: 2,
|
|
|
|
|
generatedAt: generatedAt.toISOString(),
|
|
|
|
|
sourceRevision: options.sourceRevision ?? process.env.GITHUB_SHA ?? null,
|
|
|
|
|
packages,
|
|
|
|
|
files: files.sort((a, b) => a.path.localeCompare(b.path)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function writeArtifactManifest(layout, generatedAt = new Date(), options = {}) {
|
|
|
|
|
const manifest = await buildArtifactManifest(layout, generatedAt, options);
|
|
|
|
|
await writeFile(artifactManifestPath(layout), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
|
|
|
return manifest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function verifyArtifactManifest(layout, options = {}) {
|
|
|
|
|
const manifest = await readJson(artifactManifestPath(layout));
|
|
|
|
|
assertManifestShape(manifest);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const expectedSourceRevision = options.expectedSourceRevision ?? process.env.KTX_EXPECTED_SOURCE_REVISION;
|
2026-05-10 23:12:26 +02:00
|
|
|
if (expectedSourceRevision !== undefined && manifest.sourceRevision !== expectedSourceRevision) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Artifact manifest sourceRevision mismatch: expected ${expectedSourceRevision}, got ${manifest.sourceRevision}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const expectedPackages = await packageReleaseMetadata(layout.rootDir);
|
|
|
|
|
assertJsonEqual(manifest.packages, expectedPackages, 'Artifact manifest packages');
|
|
|
|
|
|
|
|
|
|
for (const file of manifest.files) {
|
|
|
|
|
if (!isPlainObject(file)) {
|
|
|
|
|
throw new Error('Artifact manifest file entries must be JSON objects');
|
|
|
|
|
}
|
|
|
|
|
artifactPathFromManifest(layout, file.path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pythonArtifacts = await findPythonArtifacts(layout.pythonDir);
|
|
|
|
|
const expectedFiles = await Promise.all(
|
|
|
|
|
artifactPackageRecords(layout, pythonArtifacts, expectedPackages).map((record) => artifactManifestEntry(layout, record)),
|
|
|
|
|
);
|
|
|
|
|
assertJsonEqual(
|
|
|
|
|
sortedManifestFiles(manifest.files),
|
|
|
|
|
sortedManifestFiles(expectedFiles),
|
|
|
|
|
'Artifact manifest files do not match artifact contents',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return manifest;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
function runtimeWheelAssetName(runtimeWheelPath) {
|
|
|
|
|
return runtimeWheelPath.split(sep).at(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function copyRuntimeWheelAssets(layout, pythonArtifacts) {
|
|
|
|
|
const assetDir = join(layout.rootDir, 'packages', 'cli', 'assets', 'python');
|
|
|
|
|
const wheelFile = runtimeWheelAssetName(pythonArtifacts.runtimeWheel);
|
|
|
|
|
if (!wheelFile) {
|
|
|
|
|
throw new Error(`Unable to determine runtime wheel filename: ${pythonArtifacts.runtimeWheel}`);
|
|
|
|
|
}
|
|
|
|
|
const wheelContents = await readFile(pythonArtifacts.runtimeWheel);
|
|
|
|
|
await rm(assetDir, { recursive: true, force: true });
|
|
|
|
|
await mkdir(assetDir, { recursive: true });
|
|
|
|
|
const wheelPath = join(assetDir, wheelFile);
|
|
|
|
|
const manifestPath = join(assetDir, CLI_PYTHON_ASSET_MANIFEST);
|
|
|
|
|
await writeFile(wheelPath, wheelContents);
|
|
|
|
|
await writeFile(
|
|
|
|
|
manifestPath,
|
|
|
|
|
`${JSON.stringify(
|
|
|
|
|
{
|
|
|
|
|
schemaVersion: 1,
|
|
|
|
|
distributionName: RUNTIME_WHEEL_DISTRIBUTION_NAME,
|
|
|
|
|
normalizedName: RUNTIME_WHEEL_NORMALIZED_NAME,
|
|
|
|
|
version: RUNTIME_WHEEL_PACKAGE_VERSION,
|
|
|
|
|
wheel: {
|
|
|
|
|
file: wheelFile,
|
|
|
|
|
sha256: createHash('sha256').update(wheelContents).digest('hex'),
|
|
|
|
|
bytes: wheelContents.byteLength,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
2,
|
|
|
|
|
)}\n`,
|
|
|
|
|
);
|
|
|
|
|
return { assetDir, wheelPath, manifestPath };
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runCommand(command, args, options = {}) {
|
|
|
|
|
const cwd = options.cwd ?? process.cwd();
|
|
|
|
|
process.stdout.write(`$ ${command} ${args.join(' ')}\n`);
|
|
|
|
|
|
|
|
|
|
return new Promise((resolvePromise, reject) => {
|
|
|
|
|
const child = execFile(
|
|
|
|
|
command,
|
|
|
|
|
args,
|
|
|
|
|
{
|
|
|
|
|
cwd,
|
|
|
|
|
env: { ...process.env, ...options.env },
|
|
|
|
|
maxBuffer: 1024 * 1024 * 20,
|
|
|
|
|
},
|
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
|
if (stdout) {
|
|
|
|
|
process.stdout.write(stdout);
|
|
|
|
|
}
|
|
|
|
|
if (stderr) {
|
|
|
|
|
process.stderr.write(stderr);
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
resolvePromise({ stdout, stderr });
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (options.input !== undefined) {
|
|
|
|
|
child.stdin?.end(options.input);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function npmSmokePackageJson(layout) {
|
|
|
|
|
return {
|
2026-05-10 23:51:24 +02:00
|
|
|
name: 'ktx-artifact-npm-smoke',
|
2026-05-10 23:12:26 +02:00
|
|
|
version: '0.0.0',
|
|
|
|
|
private: true,
|
|
|
|
|
type: 'module',
|
|
|
|
|
dependencies: {
|
2026-05-11 15:50:34 +02:00
|
|
|
'@kaelio/ktx': `file:${layout.cliTarball}`,
|
|
|
|
|
},
|
|
|
|
|
devDependencies: {
|
|
|
|
|
'better-sqlite3': '^12.6.2',
|
2026-05-10 23:12:26 +02:00
|
|
|
},
|
|
|
|
|
pnpm: {
|
|
|
|
|
onlyBuiltDependencies: ['better-sqlite3'],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function npmVerifySource() {
|
|
|
|
|
return `
|
2026-05-11 15:50:34 +02:00
|
|
|
const cli = await import('@kaelio/ktx');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
if (cli.getKtxCliPackageInfo().name !== '@kaelio/ktx') {
|
|
|
|
|
throw new Error('Unexpected @kaelio/ktx package info');
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
2026-05-11 15:50:34 +02:00
|
|
|
if (typeof cli.runKtxCli !== 'function') {
|
|
|
|
|
throw new Error('Missing runKtxCli export');
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function npmRuntimeSmokeSource() {
|
|
|
|
|
return `
|
|
|
|
|
import assert from 'node:assert/strict';
|
2026-05-11 15:50:34 +02:00
|
|
|
import Database from 'better-sqlite3';
|
|
|
|
|
import { execFile } from 'node:child_process';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { access, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
|
|
|
import { tmpdir } from 'node:os';
|
2026-05-11 15:50:34 +02:00
|
|
|
import { join } from 'node:path';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { promisify } from 'node:util';
|
|
|
|
|
|
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
|
|
|
|
|
|
async function run(command, args, options = {}) {
|
|
|
|
|
process.stdout.write('$ ' + command + ' ' + args.join(' ') + '\\n');
|
|
|
|
|
try {
|
|
|
|
|
const result = await execFileAsync(command, args, {
|
|
|
|
|
cwd: options.cwd,
|
|
|
|
|
encoding: 'utf8',
|
|
|
|
|
timeout: 30_000,
|
|
|
|
|
});
|
|
|
|
|
return { code: 0, stdout: result.stdout, stderr: result.stderr };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
|
code: typeof error.code === 'number' ? error.code : 1,
|
|
|
|
|
stdout: error.stdout ?? '',
|
|
|
|
|
stderr: error.stderr ?? error.message,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireSuccess(label, result) {
|
|
|
|
|
assert.equal(
|
|
|
|
|
result.code,
|
|
|
|
|
0,
|
|
|
|
|
label + ' failed with code ' + result.code + '\\nstdout:\\n' + result.stdout + '\\nstderr:\\n' + result.stderr,
|
|
|
|
|
);
|
|
|
|
|
assert.equal(result.stderr, '', label + ' wrote unexpected stderr');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
function requireSuccessWithStderr(label, result, stderrPattern) {
|
|
|
|
|
assert.equal(
|
|
|
|
|
result.code,
|
|
|
|
|
0,
|
|
|
|
|
label + ' failed with code ' + result.code + '\\nstdout:\\n' + result.stdout + '\\nstderr:\\n' + result.stderr,
|
|
|
|
|
);
|
|
|
|
|
assert.match(result.stderr, stderrPattern, label + ' stderr did not match ' + stderrPattern);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
function requireOutput(label, result, text) {
|
|
|
|
|
assert.match(result.stdout, text, label + ' output did not match ' + text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseJsonResult(label, result) {
|
|
|
|
|
requireSuccess(label, result);
|
|
|
|
|
return JSON.parse(result.stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseJsonFailure(label, result) {
|
|
|
|
|
assert.equal(result.code, 1, label + ' should fail with exit code 1');
|
|
|
|
|
assert.equal(result.stdout, '', label + ' should not write stdout when failing');
|
|
|
|
|
return JSON.parse(result.stderr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireIncludes(values, expected, label) {
|
|
|
|
|
assert.ok(Array.isArray(values), label + ' must be an array');
|
|
|
|
|
assert.ok(values.includes(expected), label + ' did not include ' + expected + ': ' + values.join(', '));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRunId(stdout) {
|
|
|
|
|
const match = stdout.match(/^Run: (.+)$/m);
|
|
|
|
|
assert.ok(match, 'ingest run output did not include a run id');
|
|
|
|
|
return match[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function writeSqliteWarehouse(projectDir) {
|
2026-05-11 15:50:34 +02:00
|
|
|
const database = new Database(join(projectDir, 'warehouse.db'));
|
|
|
|
|
try {
|
|
|
|
|
database.exec(\`
|
|
|
|
|
DROP TABLE IF EXISTS orders;
|
|
|
|
|
CREATE TABLE orders (
|
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
|
status TEXT NOT NULL,
|
|
|
|
|
amount INTEGER NOT NULL
|
|
|
|
|
);
|
|
|
|
|
INSERT INTO orders (status, amount) VALUES ('paid', 20), ('paid', 30), ('open', 10);
|
|
|
|
|
\`);
|
|
|
|
|
} finally {
|
|
|
|
|
database.close();
|
|
|
|
|
}
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const root = await mkdtemp(join(tmpdir(), 'ktx-installed-cli-smoke-'));
|
2026-05-11 15:50:34 +02:00
|
|
|
const previousRuntimeRoot = process.env.KTX_RUNTIME_ROOT;
|
|
|
|
|
process.env.KTX_RUNTIME_ROOT = join(root, 'managed-runtime');
|
|
|
|
|
let daemonStarted = false;
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
|
|
|
|
const projectDir = join(root, 'project');
|
|
|
|
|
const sourceDir = join(root, 'source');
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
const version = await run('pnpm', ['exec', 'ktx', '--version']);
|
|
|
|
|
requireSuccess('ktx public package version', version);
|
|
|
|
|
requireOutput('ktx public package version', version, /@kaelio\\/ktx 0\\.1\\.0/);
|
|
|
|
|
|
|
|
|
|
const runtimeStatusBefore = parseJsonResult(
|
|
|
|
|
'ktx runtime status missing',
|
|
|
|
|
await run('pnpm', ['exec', 'ktx', 'runtime', 'status', '--json']),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(runtimeStatusBefore.kind, 'missing');
|
|
|
|
|
assert.equal(runtimeStatusBefore.layout.runtimeRoot, process.env.KTX_RUNTIME_ROOT);
|
|
|
|
|
process.stdout.write('ktx managed runtime starts missing in isolated root\\n');
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
const missingProjectDir = join(root, 'missing-project');
|
|
|
|
|
await mkdir(missingProjectDir, { recursive: true });
|
|
|
|
|
const missingProjectSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'sl',
|
|
|
|
|
'list',
|
|
|
|
|
'--json',
|
|
|
|
|
'--query',
|
|
|
|
|
'revenue',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
missingProjectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const missingProjectError = parseJsonFailure('ktx agent sl list missing project', missingProjectSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.equal(missingProjectError.error.code, 'agent_sl_search_missing_project');
|
|
|
|
|
assert.deepEqual(missingProjectError.error.nextSteps, [
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx demo',
|
|
|
|
|
'ktx setup --project-dir ' + missingProjectDir,
|
|
|
|
|
'ktx ingest <connection>',
|
|
|
|
|
'ktx agent sl list --json --query "revenue" --project-dir ' + missingProjectDir,
|
2026-05-10 23:12:26 +02:00
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx agent sl list missing project guidance verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const init = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'setup',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
'--new',
|
|
|
|
|
'--no-input',
|
|
|
|
|
'--yes',
|
|
|
|
|
'--skip-llm',
|
|
|
|
|
'--skip-embeddings',
|
|
|
|
|
'--skip-databases',
|
|
|
|
|
'--skip-sources',
|
|
|
|
|
'--skip-agents',
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx setup', init);
|
|
|
|
|
requireOutput('ktx setup', init, /Project: /);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const emptyProjectDir = join(root, 'empty-project');
|
|
|
|
|
const emptyInit = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'setup',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
emptyProjectDir,
|
|
|
|
|
'--new',
|
|
|
|
|
'--no-input',
|
|
|
|
|
'--yes',
|
|
|
|
|
'--skip-llm',
|
|
|
|
|
'--skip-embeddings',
|
|
|
|
|
'--skip-databases',
|
|
|
|
|
'--skip-sources',
|
|
|
|
|
'--skip-agents',
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx setup empty project', emptyInit);
|
2026-05-10 23:12:26 +02:00
|
|
|
const emptySearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'sl',
|
|
|
|
|
'list',
|
|
|
|
|
'--json',
|
|
|
|
|
'--query',
|
|
|
|
|
'revenue',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
emptyProjectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const emptySearchError = parseJsonFailure('ktx agent sl list no connections', emptySearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.equal(emptySearchError.error.code, 'agent_sl_search_no_connections');
|
|
|
|
|
assert.deepEqual(emptySearchError.error.nextSteps, [
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx demo',
|
|
|
|
|
'ktx setup --project-dir ' + emptyProjectDir,
|
|
|
|
|
'ktx ingest <connection>',
|
|
|
|
|
'ktx agent sl list --json --query "revenue" --project-dir ' + emptyProjectDir,
|
2026-05-10 23:12:26 +02:00
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx agent sl list no connections guidance verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
await writeFile(
|
2026-05-10 23:51:24 +02:00
|
|
|
join(projectDir, 'ktx.yaml'),
|
2026-05-10 23:12:26 +02:00
|
|
|
[
|
|
|
|
|
'project: warehouse',
|
|
|
|
|
'connections:',
|
|
|
|
|
' warehouse:',
|
|
|
|
|
' driver: sqlite',
|
|
|
|
|
' path: warehouse.db',
|
|
|
|
|
' readonly: true',
|
|
|
|
|
'storage:',
|
|
|
|
|
' state: sqlite',
|
|
|
|
|
' search: sqlite-fts5',
|
|
|
|
|
'scan:',
|
|
|
|
|
' enrichment:',
|
|
|
|
|
' mode: deterministic',
|
|
|
|
|
'ingest:',
|
|
|
|
|
' adapters:',
|
|
|
|
|
' - fake',
|
|
|
|
|
' - live-database',
|
|
|
|
|
'',
|
|
|
|
|
].join('\\n'),
|
|
|
|
|
'utf-8',
|
|
|
|
|
);
|
|
|
|
|
await writeSqliteWarehouse(projectDir);
|
|
|
|
|
|
|
|
|
|
await mkdir(join(projectDir, 'knowledge', 'global'), { recursive: true });
|
|
|
|
|
await writeFile(
|
|
|
|
|
join(projectDir, 'knowledge', 'global', 'revenue.md'),
|
|
|
|
|
[
|
|
|
|
|
'---',
|
|
|
|
|
'summary: Paid order value',
|
|
|
|
|
'tags:',
|
|
|
|
|
' - finance',
|
|
|
|
|
'refs: []',
|
|
|
|
|
'sl_refs: []',
|
|
|
|
|
'usage_mode: auto',
|
|
|
|
|
'---',
|
|
|
|
|
'',
|
|
|
|
|
'Revenue is the sum of paid order amounts.',
|
|
|
|
|
'',
|
|
|
|
|
].join('\\n'),
|
|
|
|
|
'utf-8',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const agentWikiSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'wiki',
|
|
|
|
|
'search',
|
|
|
|
|
'revenue',
|
|
|
|
|
'--json',
|
|
|
|
|
'--limit',
|
|
|
|
|
'5',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const agentWikiSearchJson = parseJsonResult('ktx agent wiki search', agentWikiSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.equal(agentWikiSearchJson.totalFound, 1);
|
|
|
|
|
assert.equal(agentWikiSearchJson.results[0].key, 'revenue');
|
|
|
|
|
assert.equal(agentWikiSearchJson.results[0].path, 'knowledge/global/revenue.md');
|
|
|
|
|
assert.equal(typeof agentWikiSearchJson.results[0].score, 'number');
|
|
|
|
|
requireIncludes(agentWikiSearchJson.results[0].matchReasons, 'lexical', 'agent wiki search match reasons');
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx agent wiki search hybrid metadata verified\\n');
|
|
|
|
|
await access(join(projectDir, '.ktx', 'db.sqlite'));
|
|
|
|
|
process.stdout.write('SQLite knowledge index: ' + join(projectDir, '.ktx', 'db.sqlite') + '\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const noSourceSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'sl',
|
|
|
|
|
'list',
|
|
|
|
|
'--json',
|
|
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
|
|
|
|
'--query',
|
|
|
|
|
'revenue',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const noSourceSearchError = parseJsonFailure('ktx agent sl list no indexed sources', noSourceSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.equal(noSourceSearchError.error.code, 'agent_sl_search_no_indexed_sources');
|
|
|
|
|
assert.deepEqual(noSourceSearchError.error.nextSteps, [
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx demo',
|
|
|
|
|
'ktx setup --project-dir ' + projectDir,
|
|
|
|
|
'ktx ingest <connection>',
|
|
|
|
|
'ktx agent sl list --json --query "revenue" --project-dir ' + projectDir,
|
2026-05-10 23:12:26 +02:00
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx agent sl list no indexed sources guidance verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const slYaml = [
|
|
|
|
|
'name: orders',
|
|
|
|
|
'table: orders',
|
|
|
|
|
'grain:',
|
|
|
|
|
' - id',
|
|
|
|
|
'columns:',
|
|
|
|
|
' - name: id',
|
|
|
|
|
' type: number',
|
|
|
|
|
' - name: amount',
|
|
|
|
|
' type: number',
|
|
|
|
|
'measures:',
|
|
|
|
|
' - name: order_count',
|
|
|
|
|
' expr: count(*)',
|
|
|
|
|
'joins: []',
|
|
|
|
|
'',
|
|
|
|
|
].join('\\n');
|
|
|
|
|
|
|
|
|
|
await mkdir(join(projectDir, 'semantic-layer', 'warehouse'), { recursive: true });
|
|
|
|
|
await writeFile(join(projectDir, 'semantic-layer', 'warehouse', 'orders.yaml'), slYaml, 'utf-8');
|
|
|
|
|
|
|
|
|
|
const agentSlSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'sl',
|
|
|
|
|
'list',
|
|
|
|
|
'--json',
|
|
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
|
|
|
|
'--query',
|
|
|
|
|
'orders',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const agentSlSearchJson = parseJsonResult('ktx agent sl list', agentSlSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.equal(agentSlSearchJson.totalSources, 1);
|
|
|
|
|
assert.equal(agentSlSearchJson.sources[0].connectionId, 'warehouse');
|
|
|
|
|
assert.equal(agentSlSearchJson.sources[0].name, 'orders');
|
|
|
|
|
assert.equal(typeof agentSlSearchJson.sources[0].score, 'number');
|
|
|
|
|
requireIncludes(agentSlSearchJson.sources[0].matchReasons, 'lexical', 'agent sl search match reasons');
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx agent sl list hybrid metadata verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
const slQuery = await run('pnpm', ['exec', 'ktx', 'sl', 'query',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
2026-05-11 15:50:34 +02:00
|
|
|
'--measure',
|
|
|
|
|
'orders.order_count',
|
|
|
|
|
'--format',
|
|
|
|
|
'json',
|
|
|
|
|
'--yes',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-11 15:50:34 +02:00
|
|
|
requireSuccessWithStderr(
|
|
|
|
|
'ktx sl query first managed runtime install',
|
|
|
|
|
slQuery,
|
|
|
|
|
/Installing KTX Python runtime \\(core\\) with uv[\\s\\S]*KTX Python runtime ready:/,
|
|
|
|
|
);
|
|
|
|
|
requireOutput('ktx sl query first managed runtime install', slQuery, /"mode": "compile_only"/);
|
|
|
|
|
requireOutput('ktx sl query first managed runtime install', slQuery, /orders/);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
const runtimeStatusAfter = parseJsonResult(
|
|
|
|
|
'ktx runtime status ready',
|
|
|
|
|
await run('pnpm', ['exec', 'ktx', 'runtime', 'status', '--json']),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(runtimeStatusAfter.kind, 'ready');
|
|
|
|
|
assert.deepEqual(runtimeStatusAfter.manifest.features, ['core']);
|
|
|
|
|
assert.equal(runtimeStatusAfter.layout.runtimeRoot, process.env.KTX_RUNTIME_ROOT);
|
|
|
|
|
process.stdout.write('ktx managed runtime lazy install verified\\n');
|
|
|
|
|
|
|
|
|
|
const sqliteSlQuery = await run('pnpm', ['exec', 'ktx', 'sl', 'query',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
2026-05-11 15:50:34 +02:00
|
|
|
'--measure',
|
|
|
|
|
'orders.order_count',
|
|
|
|
|
'--format',
|
|
|
|
|
'json',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--execute',
|
|
|
|
|
'--max-rows',
|
|
|
|
|
'100',
|
2026-05-11 15:50:34 +02:00
|
|
|
'--yes',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-11 15:50:34 +02:00
|
|
|
requireSuccess('ktx sl query sqlite execute', sqliteSlQuery);
|
|
|
|
|
requireOutput('ktx sl query sqlite execute', sqliteSlQuery, /"dialect": "sqlite"/);
|
|
|
|
|
requireOutput('ktx sl query sqlite execute', sqliteSlQuery, /"mode": "executed"/);
|
|
|
|
|
requireOutput('ktx sl query sqlite execute', sqliteSlQuery, /"driver": "sqlite"/);
|
|
|
|
|
requireOutput('ktx sl query sqlite execute', sqliteSlQuery, /"rows": \\[\\s*\\[\\s*3\\s*\\]\\s*\\]/);
|
|
|
|
|
process.stdout.write('ktx sl query sqlite execute verified\\n');
|
|
|
|
|
|
|
|
|
|
const runtimeDoctor = await run('pnpm', ['exec', 'ktx', 'runtime', 'doctor']);
|
|
|
|
|
requireSuccess('ktx runtime doctor', runtimeDoctor);
|
|
|
|
|
requireOutput('ktx runtime doctor', runtimeDoctor, /PASS uv/);
|
|
|
|
|
requireOutput('ktx runtime doctor', runtimeDoctor, /PASS Bundled Python wheel/);
|
|
|
|
|
requireOutput('ktx runtime doctor', runtimeDoctor, /PASS Managed Python runtime/);
|
|
|
|
|
process.stdout.write('ktx runtime doctor verified\\n');
|
|
|
|
|
|
|
|
|
|
const runtimeStart = await run('pnpm', ['exec', 'ktx', 'runtime', 'start']);
|
|
|
|
|
requireSuccess('ktx runtime start', runtimeStart);
|
|
|
|
|
daemonStarted = true;
|
|
|
|
|
requireOutput('ktx runtime start', runtimeStart, /Started KTX Python daemon/);
|
|
|
|
|
requireOutput('ktx runtime start', runtimeStart, /url: http:\\/\\/127\\.0\\.0\\.1:\\d+/);
|
|
|
|
|
requireOutput('ktx runtime start', runtimeStart, /features: core/);
|
|
|
|
|
|
|
|
|
|
const runtimeStartReuse = await run('pnpm', ['exec', 'ktx', 'runtime', 'start']);
|
|
|
|
|
requireSuccess('ktx runtime start reuse', runtimeStartReuse);
|
|
|
|
|
requireOutput('ktx runtime start reuse', runtimeStartReuse, /Using existing KTX Python daemon/);
|
|
|
|
|
requireOutput('ktx runtime start reuse', runtimeStartReuse, /features: core/);
|
|
|
|
|
|
|
|
|
|
const runtimeStop = await run('pnpm', ['exec', 'ktx', 'runtime', 'stop']);
|
|
|
|
|
requireSuccess('ktx runtime stop', runtimeStop);
|
|
|
|
|
daemonStarted = false;
|
|
|
|
|
requireOutput('ktx runtime stop', runtimeStop, /Stopped KTX Python daemon/);
|
|
|
|
|
process.stdout.write('ktx runtime daemon lifecycle verified\\n');
|
|
|
|
|
|
|
|
|
|
const staleRuntimeDir = join(process.env.KTX_RUNTIME_ROOT, '0.0.0');
|
|
|
|
|
await mkdir(staleRuntimeDir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
const runtimePruneDryRun = await run('pnpm', ['exec', 'ktx', 'runtime', 'prune', '--dry-run']);
|
|
|
|
|
requireSuccess('ktx runtime prune dry run', runtimePruneDryRun);
|
|
|
|
|
requireOutput('ktx runtime prune dry run', runtimePruneDryRun, /Stale KTX Python runtimes/);
|
|
|
|
|
requireOutput('ktx runtime prune dry run', runtimePruneDryRun, /0\\.0\\.0/);
|
|
|
|
|
await access(staleRuntimeDir);
|
|
|
|
|
|
|
|
|
|
const runtimePruneNeedsConfirmation = await run('pnpm', ['exec', 'ktx', 'runtime', 'prune']);
|
|
|
|
|
assert.equal(runtimePruneNeedsConfirmation.code, 1, 'ktx runtime prune needs confirmation');
|
|
|
|
|
assert.equal(runtimePruneNeedsConfirmation.stdout, '', 'ktx runtime prune needs confirmation wrote stdout');
|
|
|
|
|
assert.match(runtimePruneNeedsConfirmation.stderr, /Refusing to prune without --yes/);
|
|
|
|
|
|
|
|
|
|
const runtimePruneConfirmed = await run('pnpm', ['exec', 'ktx', 'runtime', 'prune', '--yes']);
|
|
|
|
|
requireSuccess('ktx runtime prune confirmed', runtimePruneConfirmed);
|
|
|
|
|
requireOutput('ktx runtime prune confirmed', runtimePruneConfirmed, /Removed stale KTX Python runtimes/);
|
|
|
|
|
requireOutput('ktx runtime prune confirmed', runtimePruneConfirmed, /0\\.0\\.0/);
|
|
|
|
|
await assert.rejects(() => access(staleRuntimeDir));
|
|
|
|
|
process.stdout.write('ktx runtime prune verified\\n');
|
2026-05-10 23:51:24 +02:00
|
|
|
|
|
|
|
|
const structuralScan = await run('pnpm', ['exec', 'ktx', 'dev', 'scan', 'warehouse',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx scan structural', structuralScan);
|
|
|
|
|
requireOutput('ktx scan structural', structuralScan, /Status: done/);
|
|
|
|
|
requireOutput('ktx scan structural', structuralScan, /Mode: structural/);
|
|
|
|
|
requireOutput('ktx scan structural', structuralScan, /Needs attention\\s+None/);
|
2026-05-10 23:12:26 +02:00
|
|
|
const structuralScanRunId = getRunId(structuralScan.stdout);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const scanStatus = await run('pnpm', ['exec', 'ktx', 'dev', 'scan', 'status',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
structuralScanRunId,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx scan status', scanStatus);
|
|
|
|
|
requireOutput('ktx scan status', scanStatus, new RegExp('Run: ' + structuralScanRunId));
|
|
|
|
|
requireOutput('ktx scan status', scanStatus, /Status: done/);
|
|
|
|
|
requireOutput('ktx scan status', scanStatus, /Mode: structural/);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const scanReport = await run('pnpm', ['exec', 'ktx', 'dev', 'scan', 'report',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
'--json',
|
|
|
|
|
structuralScanRunId,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx scan report', scanReport);
|
2026-05-10 23:12:26 +02:00
|
|
|
const scanReportJson = JSON.parse(scanReport.stdout);
|
|
|
|
|
assert.equal(scanReportJson.mode, 'structural');
|
|
|
|
|
assert.equal(scanReportJson.connectionId, 'warehouse');
|
|
|
|
|
assert.equal(scanReportJson.manifestShardsWritten, 1);
|
|
|
|
|
assert.deepEqual(scanReportJson.artifactPaths.enrichmentArtifacts, []);
|
|
|
|
|
assert.deepEqual(scanReportJson.artifactPaths.manifestShards, ['semantic-layer/warehouse/_schema/public.yaml']);
|
|
|
|
|
await access(join(projectDir, 'semantic-layer', 'warehouse', '_schema', 'public.yaml'));
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx scan structural verified: ' + structuralScanRunId + '\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const enrichedScan = await run('pnpm', ['exec', 'ktx', 'dev', 'scan', 'warehouse',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
'--mode',
|
|
|
|
|
'enriched',
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx scan enriched', enrichedScan);
|
|
|
|
|
requireOutput('ktx scan enriched', enrichedScan, /Status: done/);
|
|
|
|
|
requireOutput('ktx scan enriched', enrichedScan, /Mode: enriched/);
|
2026-05-10 23:12:26 +02:00
|
|
|
const enrichedScanRunId = getRunId(enrichedScan.stdout);
|
2026-05-10 23:51:24 +02:00
|
|
|
const enrichedScanReport = await run('pnpm', ['exec', 'ktx', 'dev', 'scan', 'report',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
'--json',
|
|
|
|
|
enrichedScanRunId,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx scan enriched report', enrichedScanReport);
|
2026-05-10 23:12:26 +02:00
|
|
|
const enrichedScanReportJson = JSON.parse(enrichedScanReport.stdout);
|
|
|
|
|
assert.equal(enrichedScanReportJson.mode, 'enriched');
|
|
|
|
|
assert.ok(enrichedScanReportJson.artifactPaths.enrichmentArtifacts.length > 0);
|
|
|
|
|
assert.deepEqual(enrichedScanReportJson.artifactPaths.manifestShards, ['semantic-layer/warehouse/_schema/public.yaml']);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx scan enriched verified: ' + enrichedScanRunId + '\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
await mkdir(join(sourceDir, 'orders'), { recursive: true });
|
|
|
|
|
await writeFile(join(sourceDir, 'orders', 'orders.json'), '{"name":"orders"}\\n', 'utf-8');
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const ingestRun = await run('pnpm', ['exec', 'ktx', 'dev', 'ingest', 'run',
|
2026-05-10 23:12:26 +02:00
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
'--connection-id',
|
|
|
|
|
'warehouse',
|
|
|
|
|
'--adapter',
|
|
|
|
|
'fake',
|
|
|
|
|
'--source-dir',
|
|
|
|
|
sourceDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
assert.equal(ingestRun.code, 1, 'ktx dev ingest run without an LLM provider must fail');
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.match(
|
|
|
|
|
ingestRun.stderr,
|
2026-05-10 23:51:24 +02:00
|
|
|
/ktx dev ingest run requires llm\\.provider\\.backend: anthropic, vertex, or gateway, or an injected agentRunner/,
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await access(join(projectDir, '.ktx', 'db.sqlite'));
|
|
|
|
|
process.stdout.write('ktx dev ingest provider guard verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
} finally {
|
2026-05-11 15:50:34 +02:00
|
|
|
if (daemonStarted) {
|
|
|
|
|
await run('pnpm', ['exec', 'ktx', 'runtime', 'stop']);
|
|
|
|
|
}
|
|
|
|
|
if (previousRuntimeRoot === undefined) {
|
|
|
|
|
delete process.env.KTX_RUNTIME_ROOT;
|
|
|
|
|
} else {
|
|
|
|
|
process.env.KTX_RUNTIME_ROOT = previousRuntimeRoot;
|
|
|
|
|
}
|
2026-05-10 23:12:26 +02:00
|
|
|
await rm(root, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function npmDemoSmokeSource() {
|
|
|
|
|
return `
|
|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import { execFile } from 'node:child_process';
|
2026-05-11 15:50:34 +02:00
|
|
|
import { mkdtemp, readFile, rm } from 'node:fs/promises';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { tmpdir } from 'node:os';
|
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
import { promisify } from 'node:util';
|
|
|
|
|
|
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
|
|
|
|
|
|
async function run(command, args, options = {}) {
|
|
|
|
|
process.stdout.write('$ ' + command + ' ' + args.join(' ') + '\\n');
|
|
|
|
|
try {
|
|
|
|
|
const result = await execFileAsync(command, args, {
|
|
|
|
|
cwd: options.cwd,
|
|
|
|
|
env: options.env ?? process.env,
|
|
|
|
|
encoding: 'utf8',
|
|
|
|
|
timeout: 45_000,
|
|
|
|
|
});
|
|
|
|
|
return { code: 0, stdout: result.stdout, stderr: result.stderr };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
|
code: typeof error.code === 'number' ? error.code : 1,
|
|
|
|
|
stdout: error.stdout ?? '',
|
|
|
|
|
stderr: error.stderr ?? error.message,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireSuccess(label, result) {
|
|
|
|
|
assert.equal(
|
|
|
|
|
result.code,
|
|
|
|
|
0,
|
|
|
|
|
label + ' failed with code ' + result.code + '\\nstdout:\\n' + result.stdout + '\\nstderr:\\n' + result.stderr,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function requireStdout(label, result, pattern) {
|
|
|
|
|
assert.match(result.stdout, pattern, label + ' stdout did not match ' + pattern);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const root = await mkdtemp(join(tmpdir(), 'ktx-packed-demo-smoke-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
|
|
|
|
const projectDir = join(root, 'demo-project');
|
2026-05-11 15:50:34 +02:00
|
|
|
const packageJson = JSON.parse(await readFile(join(process.cwd(), 'package.json'), 'utf8'));
|
|
|
|
|
assert.deepEqual(Object.keys(packageJson.dependencies), ['@kaelio/ktx']);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const help = await run('pnpm', ['exec', 'ktx', '--help']);
|
|
|
|
|
requireSuccess('ktx --help', help);
|
|
|
|
|
requireStdout('ktx --help', help, /Usage: ktx/);
|
|
|
|
|
requireStdout('ktx --help', help, /setup/);
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const seeded = await run(
|
|
|
|
|
'pnpm',
|
2026-05-10 23:51:24 +02:00
|
|
|
['exec', 'ktx', 'setup', 'demo', '--project-dir', projectDir, '--no-input', '--plain'],
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx setup demo seeded', seeded);
|
|
|
|
|
requireStdout('ktx setup demo seeded', seeded, /Mode: seeded/);
|
|
|
|
|
requireStdout('ktx setup demo seeded', seeded, /Source: packaged demo project/);
|
|
|
|
|
requireStdout('ktx setup demo seeded', seeded, /LLM calls: none/);
|
|
|
|
|
requireStdout('ktx setup demo seeded', seeded, /ktx serve --mcp stdio/);
|
2026-05-10 23:12:26 +02:00
|
|
|
assert.doesNotMatch(seeded.stdout, new RegExp(['--mode', 'deterministic'].join(' ')));
|
2026-05-10 23:51:24 +02:00
|
|
|
assert.doesNotMatch(seeded.stdout, /KTX memory flow/);
|
|
|
|
|
assert.equal(seeded.stderr, '', 'ktx setup demo seeded wrote unexpected stderr');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const demoWikiSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'wiki',
|
|
|
|
|
'search',
|
|
|
|
|
'ARR contract',
|
|
|
|
|
'--json',
|
|
|
|
|
'--limit',
|
|
|
|
|
'5',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx seeded demo agent wiki search', demoWikiSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
const demoWikiSearchJson = JSON.parse(demoWikiSearch.stdout);
|
|
|
|
|
assert.ok(demoWikiSearchJson.totalFound > 0, 'seeded demo wiki search should find results');
|
|
|
|
|
assert.ok(
|
|
|
|
|
demoWikiSearchJson.results.some((result) => Array.isArray(result.matchReasons) && result.matchReasons.length > 0),
|
|
|
|
|
'seeded demo wiki search should expose match reasons',
|
|
|
|
|
);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx seeded demo agent wiki search verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const demoSlSearch = await run('pnpm', [
|
|
|
|
|
'exec',
|
2026-05-10 23:51:24 +02:00
|
|
|
'ktx',
|
2026-05-10 23:12:26 +02:00
|
|
|
'agent',
|
|
|
|
|
'sl',
|
|
|
|
|
'list',
|
|
|
|
|
'--json',
|
|
|
|
|
'--query',
|
|
|
|
|
'ARR',
|
|
|
|
|
'--project-dir',
|
|
|
|
|
projectDir,
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
requireSuccess('ktx seeded demo agent sl search', demoSlSearch);
|
2026-05-10 23:12:26 +02:00
|
|
|
const demoSlSearchJson = JSON.parse(demoSlSearch.stdout);
|
|
|
|
|
assert.ok(demoSlSearchJson.totalSources > 0, 'seeded demo semantic-layer search should find sources');
|
|
|
|
|
assert.ok(
|
|
|
|
|
demoSlSearchJson.sources.some((source) => Array.isArray(source.matchReasons) && source.matchReasons.length > 0),
|
|
|
|
|
'seeded demo semantic-layer search should expose match reasons',
|
|
|
|
|
);
|
2026-05-10 23:51:24 +02:00
|
|
|
process.stdout.write('ktx seeded demo agent sl search verified\\n');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const doctor = await run('pnpm', ['exec', 'ktx', 'dev', 'doctor', 'setup', '--no-input']);
|
|
|
|
|
assert.ok([0, 1].includes(doctor.code), 'ktx dev doctor setup exit code must be 0 or 1');
|
|
|
|
|
requireStdout('ktx dev doctor setup', doctor, /KTX setup doctor/);
|
|
|
|
|
requireStdout('ktx dev doctor setup', doctor, /Node 22\\+/);
|
|
|
|
|
assert.equal(doctor.stderr, '', 'ktx dev doctor setup wrote unexpected stderr');
|
2026-05-10 23:12:26 +02:00
|
|
|
} finally {
|
|
|
|
|
await rm(root, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function buildArtifacts(layout) {
|
|
|
|
|
await rm(layout.artifactDir, { recursive: true, force: true });
|
|
|
|
|
await mkdir(layout.npmDir, { recursive: true });
|
|
|
|
|
await mkdir(layout.pythonDir, { recursive: true });
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
const commands = buildArtifactCommands(layout);
|
|
|
|
|
const npmBuildCount = NPM_ARTIFACT_BUILD_ORDER.length;
|
|
|
|
|
const npmPackStart = commands.length - 1;
|
|
|
|
|
|
|
|
|
|
for (const command of commands.slice(0, npmBuildCount)) {
|
|
|
|
|
await runCommand(command.command, command.args, { cwd: command.cwd });
|
|
|
|
|
}
|
|
|
|
|
for (const command of commands.slice(npmBuildCount, npmPackStart)) {
|
|
|
|
|
await runCommand(command.command, command.args, { cwd: command.cwd });
|
|
|
|
|
}
|
|
|
|
|
const pythonArtifacts = await findPythonArtifacts(layout.pythonDir);
|
|
|
|
|
await copyRuntimeWheelAssets(layout, pythonArtifacts);
|
|
|
|
|
for (const command of commands.slice(npmPackStart)) {
|
2026-05-10 23:12:26 +02:00
|
|
|
await runCommand(command.command, command.args, { cwd: command.cwd });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const packageInfo of NPM_ARTIFACT_PACKAGES) {
|
|
|
|
|
await assertPathExists(layout.npmTarballs[packageInfo.name], `${packageInfo.name} tarball`);
|
|
|
|
|
}
|
|
|
|
|
await writeArtifactManifest(layout);
|
|
|
|
|
await assertPathExists(artifactManifestPath(layout), 'artifact manifest');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyNpmArtifacts(layout, tmpRoot) {
|
|
|
|
|
for (const packageInfo of NPM_ARTIFACT_PACKAGES) {
|
|
|
|
|
await assertPathExists(layout.npmTarballs[packageInfo.name], `${packageInfo.name} tarball`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projectDir = join(tmpRoot, 'npm-clean-install');
|
|
|
|
|
await mkdir(projectDir, { recursive: true });
|
|
|
|
|
await writeFile(
|
|
|
|
|
join(projectDir, 'package.json'),
|
|
|
|
|
`${JSON.stringify(npmSmokePackageJson(layout), null, 2)}\n`,
|
|
|
|
|
);
|
|
|
|
|
await writeFile(join(projectDir, 'verify-npm.mjs'), npmVerifySource());
|
|
|
|
|
await writeFile(join(projectDir, 'verify-installed-cli.mjs'), npmRuntimeSmokeSource());
|
|
|
|
|
await writeFile(join(projectDir, 'verify-installed-demo.mjs'), npmDemoSmokeSource());
|
|
|
|
|
|
|
|
|
|
await runCommand('pnpm', ['install'], { cwd: projectDir });
|
|
|
|
|
await runCommand('pnpm', ['rebuild', 'better-sqlite3'], { cwd: projectDir });
|
|
|
|
|
await runCommand('node', ['verify-npm.mjs'], { cwd: projectDir });
|
2026-05-10 23:51:24 +02:00
|
|
|
await runCommand('pnpm', ['exec', 'ktx', '--version'], { cwd: projectDir });
|
2026-05-11 15:50:34 +02:00
|
|
|
await runCommand('node', ['verify-installed-cli.mjs'], { cwd: projectDir });
|
|
|
|
|
await runCommand('node', ['verify-installed-demo.mjs'], { cwd: projectDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyNpmDemoArtifacts(layout, tmpRoot) {
|
|
|
|
|
for (const packageInfo of NPM_ARTIFACT_PACKAGES) {
|
|
|
|
|
await assertPathExists(layout.npmTarballs[packageInfo.name], `${packageInfo.name} tarball`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const projectDir = join(tmpRoot, 'npm-demo-clean-install');
|
|
|
|
|
await mkdir(projectDir, { recursive: true });
|
|
|
|
|
await writeFile(join(projectDir, 'package.json'), `${JSON.stringify(npmSmokePackageJson(layout), null, 2)}\n`);
|
|
|
|
|
await writeFile(join(projectDir, 'verify-installed-demo.mjs'), npmDemoSmokeSource());
|
|
|
|
|
|
|
|
|
|
await runCommand('pnpm', ['install'], { cwd: projectDir });
|
|
|
|
|
await runCommand('node', ['verify-installed-demo.mjs'], { cwd: projectDir });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyArtifacts(layout) {
|
|
|
|
|
await verifyArtifactManifest(layout);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const tmpRoot = await mkdtemp(join(tmpdir(), 'ktx-artifacts-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
|
|
|
|
await verifyNpmArtifacts(layout, tmpRoot);
|
|
|
|
|
} finally {
|
|
|
|
|
await rm(tmpRoot, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyDemoArtifacts(layout) {
|
|
|
|
|
await verifyArtifactManifest(layout);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const tmpRoot = await mkdtemp(join(tmpdir(), 'ktx-demo-artifacts-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
|
|
|
|
await verifyNpmDemoArtifacts(layout, tmpRoot);
|
|
|
|
|
} finally {
|
|
|
|
|
await rm(tmpRoot, { recursive: true, force: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const command = process.argv[2] ?? 'check';
|
|
|
|
|
const layout = packageArtifactLayout();
|
|
|
|
|
|
|
|
|
|
if (command === 'build') {
|
|
|
|
|
await buildArtifacts(layout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (command === 'verify') {
|
|
|
|
|
await verifyArtifacts(layout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (command === 'verify-demo') {
|
|
|
|
|
await verifyDemoArtifacts(layout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (command === 'verify-manifest') {
|
|
|
|
|
await verifyArtifactManifest(layout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (command === 'check') {
|
|
|
|
|
await buildArtifacts(layout);
|
|
|
|
|
await verifyArtifacts(layout);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error(`Unknown package artifact command: ${command}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
|
|
|
|
|
try {
|
|
|
|
|
await main();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`);
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|