Skip to content
Open
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
9 changes: 6 additions & 3 deletions packages/cli/src/managed-local-embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { KtxEmbeddingConfig } from './llm/types.js';
import type { KtxCliIo } from './cli-runtime.js';
import { createCliSpinner } from './clack.js';
import { createStaticCliSpinner } from './clack.js';
import {
ensureManagedPythonCommandRuntime,
type KtxManagedPythonInstallPolicy,
Expand Down Expand Up @@ -66,8 +66,11 @@ export async function ensureManagedLocalEmbeddingsDaemon(
io: options.io,
feature: 'local-embeddings',
});
const spinner = createCliSpinner(options.io);
spinner.start('Starting ktx embedding daemon (first run downloads the model)…');
// Static, stderr-only spinner on purpose: the animated clack spinner seizes
// stdin through @clack/core's block() and leaves the tty in raw mode, which
// stops Ctrl-C from generating SIGINT for the rest of the ingest.
const spinner = createStaticCliSpinner(options.io);
spinner.start('Starting ktx embedding daemon (first run downloads the model)\u2026');
let daemon: ManagedPythonDaemonStartResult;
try {
daemon = await startDaemon({
Expand Down
46 changes: 45 additions & 1 deletion packages/cli/test/managed-local-embeddings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import type { ManagedPythonCommandRuntime } from '../src/managed-python-command.
import type { ManagedPythonDaemonStartResult } from '../src/managed-python-daemon.js';
import type { ManagedPythonDaemonLayout } from '../src/managed-python-runtime.js';

function makeIo() {
function makeIo(stdoutIsTty = false) {
let stdout = '';
let stderr = '';
return {
io: {
stdout: {
isTTY: stdoutIsTty,
write: (chunk: string) => {
stdout += chunk;
},
Expand Down Expand Up @@ -161,6 +162,49 @@ describe('ensureManagedLocalEmbeddingsDaemon', () => {

expect(io.stderr()).toContain('Using ktx daemon: http://127.0.0.1:61234');
});

it('keeps the spinner static on a TTY so the daemon start cannot leave stdin in raw mode', async () => {
// Regression test for #361. On a TTY the animated clack spinner takes
// stdin over through @clack/core's block(), which calls setRawMode(true)
// and is never undone, killing Ctrl-C for the rest of the ingest. The
// static spinner writes to stderr only and never touches stdin, so the
// giveaway is that stdout stays completely empty even when it is a TTY.
const io = makeIo(true);

await ensureManagedLocalEmbeddingsDaemon({
cliVersion: '0.2.0',
projectDir: '/work/proj',
installPolicy: 'auto',
io: io.io,
ensureRuntime: vi.fn(async () => runtime()),
startDaemon: vi.fn(async () => daemonResult('started')),
});

expect(io.stdout()).toBe('');
expect(io.stderr()).toContain('Starting ktx embedding daemon');
expect(io.stderr()).toContain('Started ktx daemon: http://127.0.0.1:61234');
});

it('reports the failure through the same static spinner and rethrows', async () => {
const io = makeIo(true);
const boom = new Error('daemon exploded');

await expect(
ensureManagedLocalEmbeddingsDaemon({
cliVersion: '0.2.0',
projectDir: '/work/proj',
installPolicy: 'auto',
io: io.io,
ensureRuntime: vi.fn(async () => runtime()),
startDaemon: vi.fn(async () => {
throw boom;
}),
}),
).rejects.toBe(boom);

expect(io.stdout()).toBe('');
expect(io.stderr()).toContain('ktx embedding daemon failed to start');
});
});

describe('tryUseManagedLocalEmbeddingsDaemon', () => {
Expand Down