2026-05-10 23:12:26 +02:00
|
|
|
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
|
|
|
import { tmpdir } from 'node:os';
|
|
|
|
|
import { join } from 'node:path';
|
2026-05-25 13:17:46 +02:00
|
|
|
import { initKtxProject } from '../src/context/project/project.js';
|
|
|
|
|
import { parseKtxProjectConfig } from '../src/context/project/config.js';
|
|
|
|
|
import { readKtxSetupState, writeKtxSetupState } from '../src/context/project/setup-config.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
2026-05-25 16:14:22 +02:00
|
|
|
import { ManagedPythonDaemonStartError } from '../src/managed-python-daemon.js';
|
2026-05-25 13:17:46 +02:00
|
|
|
import { type KtxSetupEmbeddingsPromptAdapter, runKtxSetupEmbeddingsStep } from '../src/setup-embeddings.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const EMBEDDING_OPTION_PROMPT_MESSAGE = [
|
2026-05-10 23:51:24 +02:00
|
|
|
'Which embedding option should KTX use?',
|
2026-05-10 23:12:26 +02:00
|
|
|
'',
|
2026-05-10 23:51:24 +02:00
|
|
|
'KTX uses embeddings for semantic search over semantic-layer sources, wiki context, schema metadata, ' +
|
2026-05-10 23:12:26 +02:00
|
|
|
'and relationship evidence.',
|
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
|
|
function makeIo() {
|
|
|
|
|
let stdout = '';
|
|
|
|
|
let stderr = '';
|
|
|
|
|
return {
|
|
|
|
|
io: {
|
|
|
|
|
stdout: {
|
|
|
|
|
isTTY: true,
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stdout += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stderr: {
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stderr += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stdout: () => stdout,
|
|
|
|
|
stderr: () => stderr,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makePromptAdapter(options: {
|
|
|
|
|
selectValues?: string[];
|
|
|
|
|
passwordValue?: string;
|
2026-05-10 23:51:24 +02:00
|
|
|
}): KtxSetupEmbeddingsPromptAdapter {
|
2026-05-10 23:12:26 +02:00
|
|
|
const selectValues = [...(options.selectValues ?? [])];
|
|
|
|
|
return {
|
|
|
|
|
select: vi.fn(async () => selectValues.shift() ?? 'retry'),
|
|
|
|
|
password: vi.fn(async () => options.passwordValue ?? 'embedding-secret'),
|
|
|
|
|
cancel: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 18:18:56 +02:00
|
|
|
function managedDaemon(
|
|
|
|
|
baseUrl = 'http://127.0.0.1:61234',
|
|
|
|
|
logs: { stdoutLog?: string; stderrLog?: string } = {},
|
|
|
|
|
) {
|
2026-05-11 15:50:34 +02:00
|
|
|
return {
|
|
|
|
|
baseUrl,
|
2026-05-19 18:18:56 +02:00
|
|
|
stdoutLog: logs.stdoutLog ?? '/tmp/ktx-daemon.stdout.log',
|
|
|
|
|
stderrLog: logs.stderrLog ?? '/tmp/ktx-daemon.stderr.log',
|
2026-05-11 15:50:34 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
describe('setup embeddings step', () => {
|
|
|
|
|
let tempDir: string;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2026-05-10 23:51:24 +02:00
|
|
|
tempDir = await mkdtemp(join(tmpdir(), 'ktx-setup-embeddings-'));
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir: tempDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('explains why interactive users choose an embedding option before validating embeddings', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['back'] });
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{ prompts, env: {}, healthCheck },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('back');
|
|
|
|
|
expect(healthCheck).not.toHaveBeenCalled();
|
|
|
|
|
expect(prompts.select).toHaveBeenCalledWith({
|
|
|
|
|
message: EMBEDDING_OPTION_PROMPT_MESSAGE,
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'sentence-transformers', label: 'Local sentence-transformers embeddings' },
|
2026-05-13 17:01:48 +02:00
|
|
|
{ value: 'openai', label: 'OpenAI embeddings', hint: 'recommended' },
|
2026-05-10 23:12:26 +02:00
|
|
|
{ value: 'back', label: 'Back' },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns from the OpenAI credential prompt to embedding option selection when Back is selected', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['openai', 'back', 'sentence-transformers'] });
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
2026-05-11 15:50:34 +02:00
|
|
|
{ prompts, env: {}, healthCheck, ensureLocalEmbeddings: vi.fn(async () => managedDaemon()) },
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
|
|
|
|
expect(healthCheck).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(healthCheck).toHaveBeenCalledWith({
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
2026-05-11 15:50:34 +02:00
|
|
|
sentenceTransformers: { baseURL: 'http://127.0.0.1:61234', pathPrefix: '' },
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
expect(vi.mocked(prompts.select).mock.calls.map((call) => call[0].message)).toEqual([
|
|
|
|
|
EMBEDDING_OPTION_PROMPT_MESSAGE,
|
2026-05-10 23:51:24 +02:00
|
|
|
'How should KTX find your OpenAI embedding API key?',
|
2026-05-10 23:12:26 +02:00
|
|
|
EMBEDDING_OPTION_PROMPT_MESSAGE,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('configures local sentence-transformers embeddings after interactive selection', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['sentence-transformers'] });
|
2026-05-11 15:50:34 +02:00
|
|
|
const ensureLocalEmbeddings = vi.fn(async () => managedDaemon());
|
2026-05-12 21:50:41 -07:00
|
|
|
const spinnerEvents: string[] = [];
|
|
|
|
|
const spinner = vi.fn(() => ({
|
|
|
|
|
start: (msg: string) => spinnerEvents.push(`start:${msg}`),
|
2026-05-13 17:01:48 +02:00
|
|
|
message: (msg: string) => spinnerEvents.push(`message:${msg}`),
|
2026-05-12 21:50:41 -07:00
|
|
|
stop: (msg: string) => spinnerEvents.push(`stop:${msg}`),
|
|
|
|
|
error: (msg: string) => spinnerEvents.push(`error:${msg}`),
|
|
|
|
|
}));
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
2026-05-12 21:50:41 -07:00
|
|
|
{ prompts, env: {}, healthCheck, ensureLocalEmbeddings, spinner },
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
2026-05-11 15:50:34 +02:00
|
|
|
expect(ensureLocalEmbeddings).toHaveBeenCalledWith({
|
|
|
|
|
cliVersion: '0.2.0',
|
2026-05-14 14:35:55 +02:00
|
|
|
projectDir: tempDir,
|
2026-05-11 15:50:34 +02:00
|
|
|
installPolicy: 'auto',
|
|
|
|
|
io: io.io,
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(healthCheck).toHaveBeenCalledWith({
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
2026-05-11 15:50:34 +02:00
|
|
|
sentenceTransformers: { baseURL: 'http://127.0.0.1:61234', pathPrefix: '' },
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.ingest.embeddings).toMatchObject({
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
|
|
|
|
});
|
2026-05-21 02:21:22 +02:00
|
|
|
expect(config.ingest.embeddings.sentenceTransformers).toBeUndefined();
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.scan.enrichment.embeddings).toMatchObject(config.ingest.embeddings);
|
2026-05-13 13:55:21 +02:00
|
|
|
expect(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8')).not.toContain('completed_steps:');
|
2026-05-12 16:26:23 -07:00
|
|
|
expect((await readKtxSetupState(tempDir)).completed_steps).toContain('embeddings');
|
2026-05-17 19:15:09 +02:00
|
|
|
expect(spinnerEvents).toContainEqual('start:Testing local embeddings (all-MiniLM-L6-v2)');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(io.stdout()).toContain('Embeddings ready: yes');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-17 19:15:09 +02:00
|
|
|
it('uses a short non-animated local embeddings health-check status by default', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['sentence-transformers'] });
|
|
|
|
|
|
|
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{ prompts, env: {}, healthCheck, ensureLocalEmbeddings: vi.fn(async () => managedDaemon()) },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
|
|
|
|
expect(io.stderr()).toContain('Testing local embeddings (all-MiniLM-L6-v2)');
|
|
|
|
|
expect(io.stderr()).not.toContain('First run may take up to 60 seconds');
|
|
|
|
|
expect(io.stderr().match(/Testing local embeddings/g)).toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
it('shows live progress while local sentence-transformers embeddings are being tested', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['sentence-transformers'] });
|
|
|
|
|
let resolveHealthCheck: ((result: { ok: true }) => void) | undefined;
|
|
|
|
|
const healthCheck = vi.fn(
|
|
|
|
|
() =>
|
|
|
|
|
new Promise<{ ok: true }>((resolve) => {
|
|
|
|
|
resolveHealthCheck = resolve;
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-05-12 21:50:41 -07:00
|
|
|
const spinnerEvents: string[] = [];
|
|
|
|
|
const spinner = vi.fn(() => ({
|
|
|
|
|
start: (msg: string) => spinnerEvents.push(`start:${msg}`),
|
2026-05-13 17:01:48 +02:00
|
|
|
message: (msg: string) => spinnerEvents.push(`message:${msg}`),
|
2026-05-12 21:50:41 -07:00
|
|
|
stop: (msg: string) => spinnerEvents.push(`stop:${msg}`),
|
|
|
|
|
error: (msg: string) => spinnerEvents.push(`error:${msg}`),
|
|
|
|
|
}));
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
2026-05-12 21:50:41 -07:00
|
|
|
{ prompts, env: {}, healthCheck, ensureLocalEmbeddings: vi.fn(async () => managedDaemon()), spinner },
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await vi.waitFor(() => {
|
2026-05-17 19:15:09 +02:00
|
|
|
expect(spinnerEvents).toContainEqual('start:Testing local embeddings (all-MiniLM-L6-v2)');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(resolveHealthCheck).toBeDefined();
|
|
|
|
|
resolveHealthCheck?.({ ok: true });
|
|
|
|
|
await expect(result).resolves.toMatchObject({ status: 'ready' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('uses default local sentence-transformers embeddings in non-interactive setup', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
2026-05-11 15:50:34 +02:00
|
|
|
{ env: {}, healthCheck, ensureLocalEmbeddings: vi.fn(async () => managedDaemon()) },
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
|
|
|
|
expect(healthCheck).toHaveBeenCalledWith({
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
2026-05-11 15:50:34 +02:00
|
|
|
sentenceTransformers: { baseURL: 'http://127.0.0.1:61234', pathPrefix: '' },
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.ingest.embeddings).toMatchObject({
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
|
|
|
|
});
|
2026-05-21 02:21:22 +02:00
|
|
|
expect(config.ingest.embeddings.sentenceTransformers).toBeUndefined();
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.scan.enrichment.embeddings).toMatchObject(config.ingest.embeddings);
|
2026-05-13 13:55:21 +02:00
|
|
|
expect(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8')).not.toContain('completed_steps:');
|
2026-05-12 16:26:23 -07:00
|
|
|
expect((await readKtxSetupState(tempDir)).completed_steps).toContain('embeddings');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-11 15:50:34 +02:00
|
|
|
it('fails non-interactive local setup when the managed local embeddings runtime is missing', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const ensureLocalEmbeddings = vi.fn(async () => {
|
|
|
|
|
throw new Error(
|
2026-05-20 01:36:54 +02:00
|
|
|
'KTX Python runtime is required for this command. Run: ktx admin runtime install --feature local-embeddings --yes',
|
2026-05-11 15:50:34 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'never',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{ env: {}, ensureLocalEmbeddings },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('failed');
|
|
|
|
|
expect(io.stderr()).toContain(
|
2026-05-20 01:36:54 +02:00
|
|
|
'KTX Python runtime is required for this command. Run: ktx admin runtime install --feature local-embeddings --yes',
|
2026-05-11 15:50:34 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
it('does not persist embedding completion when the health check fails', async () => {
|
|
|
|
|
const io = makeIo();
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{
|
|
|
|
|
env: {},
|
2026-05-11 15:50:34 +02:00
|
|
|
ensureLocalEmbeddings: vi.fn(async () => managedDaemon()),
|
2026-05-10 23:12:26 +02:00
|
|
|
healthCheck: vi.fn(async () => ({ ok: false as const, message: '401 invalid api key [redacted]' })),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('failed');
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-13 13:55:21 +02:00
|
|
|
expect(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8')).not.toContain('completed_steps:');
|
2026-05-19 16:40:01 +02:00
|
|
|
expect(config.ingest.embeddings.backend).toBe('none');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(io.stderr()).toContain('Local embedding health check failed: 401 invalid api key [redacted]');
|
2026-05-20 01:36:54 +02:00
|
|
|
expect(io.stderr()).toContain('Prepare the runtime with: ktx admin runtime start --feature local-embeddings');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(io.stderr()).not.toContain('skip for now');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-19 18:18:56 +02:00
|
|
|
it('prints the recent daemon stderr tail when local embedding health check fails', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const stderrLog = join(tempDir, '.ktx', 'runtime', 'daemon.stderr.log');
|
|
|
|
|
await mkdir(join(tempDir, '.ktx', 'runtime'), { recursive: true });
|
|
|
|
|
await writeFile(
|
|
|
|
|
stderrLog,
|
|
|
|
|
Array.from({ length: 45 }, (_value, index) => `daemon traceback line ${index + 1}`).join('\n'),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{
|
|
|
|
|
env: {},
|
|
|
|
|
ensureLocalEmbeddings: vi.fn(async () => managedDaemon('http://127.0.0.1:61234', { stderrLog })),
|
|
|
|
|
healthCheck: vi.fn(async () => ({ ok: false as const, message: 'HTTP 500' })),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('failed');
|
refactor(release): drop release-policy.json runtime dep and next branch (#180)
* chore: standardize daemon naming on "KTX daemon"
Replace inconsistent names ("KTX Python daemon", "KTX local embeddings
daemon", "KTX managed daemon", "Python daemon") with the single name
"KTX daemon" in CLI output, errors, command descriptions, test
assertions, smoke scripts, docs, AGENTS.md, issue templates, and
codecov flags. The daemon is a portable compute server with endpoints
for SQL analysis, semantic layer, LookML, database introspection, and
embeddings; the previous labels misrepresented it as embeddings-only or
exposed implementation details ("Python", "managed").
The "KTX Python runtime" concept (installed interpreter + packages) is
deliberately left as-is — it is a separate concept from the daemon
process.
* refactor(release): drop release-policy.json runtime dep and next branch
Strips the release-policy.json fallback from release-version.ts so the CLI
reads its version straight from packages/cli/package.json. dev → 0.0.0-private,
installed @kaelio/ktx → the real semver baked into the published package.json.
KtxCliPackageInfo collapses to { name, version, contextPackageName }; /health
no longer depends on version files surviving past a CI run.
Replaces the dual-branch (main + next) semantic-release model with a single-
branch model on main. rcs and stables interleave on the same branch via
{ name: 'main', prerelease: 'rc', channel: 'next' } / ['main']. Drops
@semantic-release/git and @semantic-release/changelog (nothing is committed
back to the repo on any channel) and the workflow's "Prepare next prerelease
branch" step plus the KTX_PRERELEASE_BRANCH plumbing. The git tag plus the
published npm artifact carry the version forward.
Updates docs/release.md, removes the two now-unused devDeps, regenerates
pnpm-lock.yaml. 611/611 @ktx/cli tests, 173/173 script tests, type-check,
biome, knip all clean.
* fix(release): don't throw on non-main branches at config-load time
knip loads .releaserc.cjs on every PR run, where GITHUB_REF_NAME is the
merge ref (e.g. 180/merge). The previous version of releaseBranches threw
immediately when the branch wasn't main, which made knip fail to evaluate
the config and then mis-flag @semantic-release/exec as an unused dep.
semantic-release already refuses to publish when the current branch doesn't
match a configured release branch, so the explicit throw was redundant.
Drop it (and the unused currentBranch helper) and replace the
"rejects releases from non-main" assertion with one that exercises a CI-
shaped GITHUB_REF_NAME and confirms the config loads.
2026-05-20 13:53:14 +02:00
|
|
|
expect(io.stderr()).toContain('Recent KTX daemon stderr:');
|
2026-05-19 18:18:56 +02:00
|
|
|
expect(io.stderr()).toContain('daemon traceback line 6');
|
|
|
|
|
expect(io.stderr()).toContain('daemon traceback line 45');
|
|
|
|
|
expect(io.stderr()).not.toContain('daemon traceback line 5');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-25 16:14:22 +02:00
|
|
|
it('prints the daemon stderr tail when the daemon fails to start', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const stderrLog = join(tempDir, '.ktx', 'runtime', 'daemon.stderr.log');
|
|
|
|
|
await mkdir(join(tempDir, '.ktx', 'runtime'), { recursive: true });
|
|
|
|
|
await writeFile(
|
|
|
|
|
stderrLog,
|
|
|
|
|
Array.from({ length: 45 }, (_value, index) => `daemon startup traceback ${index + 1}`).join('\n'),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{
|
|
|
|
|
env: {},
|
|
|
|
|
ensureLocalEmbeddings: vi.fn(async () => {
|
|
|
|
|
throw new ManagedPythonDaemonStartError('fetch failed: connect ECONNREFUSED 127.0.0.1:61234', stderrLog);
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('failed');
|
|
|
|
|
expect(io.stderr()).toContain('Local embedding health check failed: fetch failed: connect ECONNREFUSED');
|
|
|
|
|
expect(io.stderr()).toContain('Recent KTX daemon stderr:');
|
|
|
|
|
expect(io.stderr()).toContain('daemon startup traceback 6');
|
|
|
|
|
expect(io.stderr()).toContain('daemon startup traceback 45');
|
|
|
|
|
expect(io.stderr()).not.toContain('daemon startup traceback 5');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-19 18:18:56 +02:00
|
|
|
it('does not print daemon stderr diagnostics when the log is unavailable or empty', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
|
|
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{
|
|
|
|
|
env: {},
|
|
|
|
|
ensureLocalEmbeddings: vi.fn(async () =>
|
|
|
|
|
managedDaemon('http://127.0.0.1:61234', {
|
|
|
|
|
stderrLog: join(tempDir, '.ktx', 'runtime', 'missing.stderr.log'),
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
healthCheck: vi.fn(async () => ({ ok: false as const, message: 'HTTP 500' })),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('failed');
|
refactor(release): drop release-policy.json runtime dep and next branch (#180)
* chore: standardize daemon naming on "KTX daemon"
Replace inconsistent names ("KTX Python daemon", "KTX local embeddings
daemon", "KTX managed daemon", "Python daemon") with the single name
"KTX daemon" in CLI output, errors, command descriptions, test
assertions, smoke scripts, docs, AGENTS.md, issue templates, and
codecov flags. The daemon is a portable compute server with endpoints
for SQL analysis, semantic layer, LookML, database introspection, and
embeddings; the previous labels misrepresented it as embeddings-only or
exposed implementation details ("Python", "managed").
The "KTX Python runtime" concept (installed interpreter + packages) is
deliberately left as-is — it is a separate concept from the daemon
process.
* refactor(release): drop release-policy.json runtime dep and next branch
Strips the release-policy.json fallback from release-version.ts so the CLI
reads its version straight from packages/cli/package.json. dev → 0.0.0-private,
installed @kaelio/ktx → the real semver baked into the published package.json.
KtxCliPackageInfo collapses to { name, version, contextPackageName }; /health
no longer depends on version files surviving past a CI run.
Replaces the dual-branch (main + next) semantic-release model with a single-
branch model on main. rcs and stables interleave on the same branch via
{ name: 'main', prerelease: 'rc', channel: 'next' } / ['main']. Drops
@semantic-release/git and @semantic-release/changelog (nothing is committed
back to the repo on any channel) and the workflow's "Prepare next prerelease
branch" step plus the KTX_PRERELEASE_BRANCH plumbing. The git tag plus the
published npm artifact carry the version forward.
Updates docs/release.md, removes the two now-unused devDeps, regenerates
pnpm-lock.yaml. 611/611 @ktx/cli tests, 173/173 script tests, type-check,
biome, knip all clean.
* fix(release): don't throw on non-main branches at config-load time
knip loads .releaserc.cjs on every PR run, where GITHUB_REF_NAME is the
merge ref (e.g. 180/merge). The previous version of releaseBranches threw
immediately when the branch wasn't main, which made knip fail to evaluate
the config and then mis-flag @semantic-release/exec as an unused dep.
semantic-release already refuses to publish when the current branch doesn't
match a configured release branch, so the explicit throw was redundant.
Drop it (and the unused currentBranch helper) and replace the
"rejects releases from non-main" assertion with one that exercises a CI-
shaped GITHUB_REF_NAME and confirms the config loads.
2026-05-20 13:53:14 +02:00
|
|
|
expect(io.stderr()).not.toContain('Recent KTX daemon stderr:');
|
2026-05-19 18:18:56 +02:00
|
|
|
});
|
|
|
|
|
|
2026-05-10 23:12:26 +02:00
|
|
|
it('uses fixed OpenAI defaults and only asks for credentials when OpenAI is selected', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-10 23:12:26 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
embeddingBackend: 'openai',
|
2026-05-13 19:49:25 +02:00
|
|
|
embeddingApiKeyEnv: 'OPENAI_API_KEY', // pragma: allowlist secret
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
2026-05-10 23:12:26 +02:00
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
io.io,
|
|
|
|
|
{
|
2026-05-13 19:49:25 +02:00
|
|
|
env: { OPENAI_API_KEY: 'sk-openai-test' }, // pragma: allowlist secret
|
2026-05-10 23:12:26 +02:00
|
|
|
healthCheck,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
|
|
|
|
expect(healthCheck).toHaveBeenCalledWith({
|
|
|
|
|
backend: 'openai',
|
|
|
|
|
model: 'text-embedding-3-small',
|
|
|
|
|
dimensions: 1536,
|
2026-05-13 19:49:25 +02:00
|
|
|
openai: { apiKey: 'sk-openai-test' }, // pragma: allowlist secret
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.ingest.embeddings).toMatchObject({
|
|
|
|
|
backend: 'openai',
|
|
|
|
|
model: 'text-embedding-3-small',
|
|
|
|
|
dimensions: 1536,
|
2026-05-13 19:49:25 +02:00
|
|
|
openai: { api_key: 'env:OPENAI_API_KEY' }, // pragma: allowlist secret
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
expect(io.stdout()).not.toContain('sk-openai-test');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can fall back to OpenAI after the default local daemon is unavailable', async () => {
|
|
|
|
|
const io = makeIo();
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['sentence-transformers', 'openai', 'env'] });
|
|
|
|
|
const healthCheck = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ ok: false as const, message: 'fetch failed' })
|
|
|
|
|
.mockResolvedValueOnce({ ok: true as const });
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-11 15:50:34 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
2026-05-10 23:12:26 +02:00
|
|
|
io.io,
|
2026-05-11 15:50:34 +02:00
|
|
|
{
|
|
|
|
|
prompts,
|
2026-05-13 19:49:25 +02:00
|
|
|
env: { OPENAI_API_KEY: 'sk-openai-test' }, // pragma: allowlist secret
|
2026-05-11 15:50:34 +02:00
|
|
|
healthCheck,
|
|
|
|
|
ensureLocalEmbeddings: vi.fn(async () => managedDaemon()),
|
|
|
|
|
},
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('ready');
|
|
|
|
|
expect(healthCheck).toHaveBeenNthCalledWith(1, {
|
|
|
|
|
backend: 'sentence-transformers',
|
|
|
|
|
model: 'all-MiniLM-L6-v2',
|
|
|
|
|
dimensions: 384,
|
2026-05-11 15:50:34 +02:00
|
|
|
sentenceTransformers: { baseURL: 'http://127.0.0.1:61234', pathPrefix: '' },
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
expect(healthCheck).toHaveBeenNthCalledWith(2, {
|
|
|
|
|
backend: 'openai',
|
|
|
|
|
model: 'text-embedding-3-small',
|
|
|
|
|
dimensions: 1536,
|
2026-05-13 19:49:25 +02:00
|
|
|
openai: { apiKey: 'sk-openai-test' }, // pragma: allowlist secret
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
expect(prompts.select).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
2026-05-10 23:51:24 +02:00
|
|
|
message: 'Local embeddings are not reachable. Start the local KTX daemon, then retry.',
|
2026-05-10 23:12:26 +02:00
|
|
|
options: expect.arrayContaining([expect.objectContaining({ value: 'openai' })]),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
expect(vi.mocked(prompts.select).mock.calls[1]?.[0].options).toEqual([
|
|
|
|
|
{ value: 'retry', label: 'Retry' },
|
|
|
|
|
{ value: 'openai', label: 'Use OpenAI embeddings' },
|
|
|
|
|
{ value: 'back', label: 'Back' },
|
|
|
|
|
]);
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(config.ingest.embeddings.backend).toBe('openai');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('leaves setup incomplete when skipped', async () => {
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-11 15:50:34 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: true,
|
|
|
|
|
},
|
2026-05-10 23:12:26 +02:00
|
|
|
makeIo().io,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('skipped');
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-13 13:55:21 +02:00
|
|
|
expect(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8')).not.toContain('completed_steps:');
|
2026-05-19 16:40:01 +02:00
|
|
|
expect(config.ingest.embeddings.backend).toBe('none');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns back without writing config when the local health check fails and Back is selected', async () => {
|
|
|
|
|
const prompts = makePromptAdapter({ selectValues: ['sentence-transformers', 'back'] });
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await runKtxSetupEmbeddingsStep(
|
2026-05-11 15:50:34 +02:00
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'auto',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
2026-05-10 23:12:26 +02:00
|
|
|
makeIo().io,
|
2026-05-11 15:50:34 +02:00
|
|
|
{
|
|
|
|
|
prompts,
|
|
|
|
|
env: {},
|
|
|
|
|
ensureLocalEmbeddings: vi.fn(async () => managedDaemon()),
|
|
|
|
|
healthCheck: vi.fn(async () => ({ ok: false as const, message: 'daemon unavailable' })),
|
|
|
|
|
},
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.status).toBe('back');
|
2026-05-10 23:51:24 +02:00
|
|
|
const config = parseKtxProjectConfig(await readFile(join(tempDir, 'ktx.yaml'), 'utf-8'));
|
2026-05-19 16:40:01 +02:00
|
|
|
expect(config.ingest.embeddings.backend).toBe('none');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('preserves already completed embeddings setup when no embedding args request changes', async () => {
|
2026-05-10 23:51:24 +02:00
|
|
|
await mkdir(join(tempDir, '.ktx'), { recursive: true });
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir: tempDir, force: true });
|
2026-05-10 23:12:26 +02:00
|
|
|
await writeFile(
|
2026-05-10 23:51:24 +02:00
|
|
|
join(tempDir, 'ktx.yaml'),
|
2026-05-10 23:12:26 +02:00
|
|
|
[
|
|
|
|
|
'setup:',
|
|
|
|
|
' database_connection_ids: []',
|
|
|
|
|
'connections: {}',
|
|
|
|
|
'ingest:',
|
|
|
|
|
' embeddings:',
|
|
|
|
|
' backend: sentence-transformers',
|
|
|
|
|
' model: all-MiniLM-L6-v2',
|
|
|
|
|
' dimensions: 384',
|
|
|
|
|
' sentenceTransformers:',
|
|
|
|
|
' base_url: http://127.0.0.1:8765',
|
|
|
|
|
" pathPrefix: ''",
|
|
|
|
|
].join('\n'),
|
|
|
|
|
'utf-8',
|
|
|
|
|
);
|
2026-05-13 13:55:21 +02:00
|
|
|
await writeKtxSetupState(tempDir, { completed_steps: ['project', 'llm', 'embeddings'] });
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const healthCheck = vi.fn(async () => ({ ok: true as const }));
|
|
|
|
|
await expect(
|
2026-05-11 15:50:34 +02:00
|
|
|
runKtxSetupEmbeddingsStep(
|
|
|
|
|
{
|
|
|
|
|
projectDir: tempDir,
|
|
|
|
|
inputMode: 'disabled',
|
|
|
|
|
cliVersion: '0.2.0',
|
|
|
|
|
runtimeInstallPolicy: 'auto',
|
|
|
|
|
skipEmbeddings: false,
|
|
|
|
|
},
|
|
|
|
|
makeIo().io,
|
|
|
|
|
{
|
2026-05-13 19:49:25 +02:00
|
|
|
env: { OPENAI_API_KEY: 'sk-openai-test' }, // pragma: allowlist secret
|
2026-05-11 15:50:34 +02:00
|
|
|
healthCheck,
|
|
|
|
|
},
|
|
|
|
|
),
|
2026-05-10 23:12:26 +02:00
|
|
|
).resolves.toMatchObject({ status: 'ready' });
|
|
|
|
|
expect(healthCheck).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|