feat(cli): shell completion for commands, flags, and entity names (#244)

* feat(completion): complete known argument values

* fix(completion): hide Commander-hidden subcommands from completions

Replace the `__`-prefix name heuristic with Commander's `_hidden` flag so
internal subcommands registered with { hidden: true } (e.g. `mcp serve-internal`)
are excluded from completions, mirroring `ktx --help`.

* test: cover wiki and sl read command routing

* test: cover raw wiki and sl reads

* feat: add wiki read command

* feat: add sl read command

* feat: complete read command entity names

* docs: document wiki and sl read commands

* test: include read commands in command tree

* feat(sl): read and validate unique sources by name

* feat(sl): make read and validate connection id optional

* fix(completion): dedupe semantic source names

* docs(sl): document connection-optional read and validate

* fix(sl): require connection id for query command

* docs(sl): clarify query connection requirement

* fix(completion): don't resolve option values as subcommands

resolveCommand skipped flag tokens but not the value consumed by a
value-taking option in the `--flag value` form, so a connection id like
`query` was matched as the `sl query` subcommand and yielded no `sl`
completions. Track value-taking options and skip their consumed value
before matching subcommands.

* test(telemetry): assert first-run notice via TELEMETRY_NOTICE constant

CI (which tests this branch merged with main) failed because #243 changed
the first-run notice wording in identity.ts (dropped "anonymous") but left
this test grepping for the old literal 'ktx collects anonymous usage data',
so indexOf returned -1. Assert against the exported TELEMETRY_NOTICE
constant instead so the test tracks the source of truth and cannot drift
when the notice text changes again.
This commit is contained in:
Andrey Avtomonov 2026-05-31 23:44:33 +02:00 committed by GitHub
parent c196d1f192
commit d320d54ab2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1596 additions and 54 deletions

View file

@ -6,6 +6,7 @@ import { initKtxProject, type KtxLocalProject } from '../../../src/context/proje
import {
listLocalSlSources,
readLocalSlSource,
resolveLocalSlSource,
searchLocalSlSources,
validateLocalSlSource,
writeLocalSlSource,
@ -90,6 +91,101 @@ describe('local semantic-layer helpers', () => {
await expect(validateLocalSlSource(ORDERS_YAML)).resolves.toEqual({ valid: true, errors: [] });
});
it('resolves a scoped source by connection id', async () => {
await writeLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await expect(
resolveLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
}),
).resolves.toEqual({
kind: 'found',
source: expect.objectContaining({
connectionId: 'warehouse',
name: 'orders',
path: 'semantic-layer/warehouse/orders.yaml',
yaml: ORDERS_YAML,
}),
});
});
it('returns not-found for a missing scoped source', async () => {
await writeLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await expect(
resolveLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'missing_orders',
}),
).resolves.toEqual({ kind: 'not-found' });
});
it('resolves a unique source name across all connections', async () => {
await writeLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await writeLocalSlSource(project, {
connectionId: 'analytics',
sourceName: 'tickets',
yaml: SUPPORT_YAML,
});
await expect(
resolveLocalSlSource(project, {
sourceName: 'tickets',
}),
).resolves.toEqual({
kind: 'found',
source: expect.objectContaining({
connectionId: 'analytics',
name: 'tickets',
path: 'semantic-layer/analytics/tickets.yaml',
yaml: SUPPORT_YAML,
}),
});
});
it('returns not-found for a missing unscoped source', async () => {
await writeLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await expect(resolveLocalSlSource(project, { sourceName: 'missing_orders' })).resolves.toEqual({
kind: 'not-found',
});
});
it('reports sorted ambiguous connection ids for duplicate source names', async () => {
await writeLocalSlSource(project, {
connectionId: 'warehouse',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await writeLocalSlSource(project, {
connectionId: 'analytics',
sourceName: 'orders',
yaml: ORDERS_YAML,
});
await expect(resolveLocalSlSource(project, { sourceName: 'orders' })).resolves.toEqual({
kind: 'ambiguous',
connectionIds: ['analytics', 'warehouse'],
});
});
it('validates table-backed sources against matching physical manifests when project context is provided', async () => {
await project.fileStore.writeFile(
'semantic-layer/postgres-warehouse/_schema/orbit_analytics.yaml',

View file

@ -4,6 +4,7 @@ import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { initKtxProject, type KtxLocalProject } from '../../../src/context/project/project.js';
import {
listLocalKnowledgePageKeys,
listLocalKnowledgePages,
readLocalKnowledgePage,
searchLocalKnowledgePages,
@ -102,6 +103,35 @@ describe('local knowledge helpers', () => {
await expect(access(join(project.projectDir, '.ktx', 'db.sqlite'))).resolves.toBeUndefined();
});
it('lists page keys across scopes, deduped and sorted, for completion', async () => {
await writeLocalKnowledgePage(project, {
key: 'metrics-revenue',
scope: 'GLOBAL',
summary: 'Revenue metric definition',
content: 'Revenue is recognized when an order is paid.',
});
await writeLocalKnowledgePage(project, {
key: 'metrics-churn',
scope: 'USER',
userId: 'local',
summary: 'Churn metric definition',
content: 'Churn is measured monthly.',
});
// Same key in both scopes must collapse to a single completion candidate.
await writeLocalKnowledgePage(project, {
key: 'metrics-revenue',
scope: 'USER',
userId: 'local',
summary: 'User override of revenue',
content: 'Local revenue note.',
});
await expect(listLocalKnowledgePageKeys(project, { userId: 'local' })).resolves.toEqual([
'metrics-churn',
'metrics-revenue',
]);
});
it('adds the token lane alongside lexical wiki matches', async () => {
await writeLocalKnowledgePage(project, {
key: 'metrics-revenue',