@kaelio/ktx 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/assets/python/{kaelio_ktx-0.12.0-py3-none-any.whl → kaelio_ktx-0.13.1-py3-none-any.whl} +0 -0
  2. package/assets/python/manifest.json +4 -4
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/commands/setup-commands.js +13 -0
  5. package/dist/connection.js +14 -2
  6. package/dist/connectors/bigquery/connector.js +1 -14
  7. package/dist/connectors/clickhouse/connector.js +1 -15
  8. package/dist/connectors/duckdb/federated-attach.d.ts +7 -0
  9. package/dist/connectors/duckdb/federated-attach.js +86 -0
  10. package/dist/connectors/duckdb/federated-executor.d.ts +5 -0
  11. package/dist/connectors/duckdb/federated-executor.js +59 -0
  12. package/dist/connectors/mysql/connector.js +1 -15
  13. package/dist/connectors/postgres/connector.js +1 -14
  14. package/dist/connectors/shared/string-reference.d.ts +6 -0
  15. package/dist/connectors/shared/string-reference.js +19 -0
  16. package/dist/connectors/snowflake/connector.js +1 -14
  17. package/dist/connectors/sqlserver/connector.js +4 -16
  18. package/dist/context/connections/federation.d.ts +33 -0
  19. package/dist/context/connections/federation.js +51 -0
  20. package/dist/context/connections/local-warehouse-descriptor.d.ts +2 -0
  21. package/dist/context/connections/project-sql-executor.d.ts +18 -0
  22. package/dist/context/connections/project-sql-executor.js +39 -0
  23. package/dist/context/connections/query-executor.d.ts +2 -2
  24. package/dist/context/connections/read-only-sql.d.ts +5 -0
  25. package/dist/context/connections/read-only-sql.js +143 -4
  26. package/dist/context/connections/resolve-connection.d.ts +12 -0
  27. package/dist/context/connections/resolve-connection.js +37 -0
  28. package/dist/context/core/git-env.d.ts +4 -0
  29. package/dist/context/core/git-env.js +5 -1
  30. package/dist/context/ingest/adapters/live-database/manifest.d.ts +3 -0
  31. package/dist/context/ingest/adapters/live-database/manifest.js +19 -11
  32. package/dist/context/llm/claude-code-runtime.js +18 -2
  33. package/dist/context/mcp/context-tools.js +27 -2
  34. package/dist/context/mcp/local-project-ports.js +55 -50
  35. package/dist/context/mcp/types.d.ts +2 -0
  36. package/dist/context/scan/local-enrichment-artifacts.js +31 -3
  37. package/dist/context/sl/local-query.js +29 -12
  38. package/dist/context/sl/local-sl.js +27 -1
  39. package/dist/context/sl/source-files.d.ts +2 -0
  40. package/dist/context/sl/source-files.js +7 -0
  41. package/dist/ingest-query-executor.d.ts +2 -0
  42. package/dist/ingest-query-executor.js +8 -22
  43. package/dist/setup-agents.d.ts +21 -15
  44. package/dist/setup-agents.js +128 -42
  45. package/dist/setup-databases.d.ts +3 -0
  46. package/dist/setup-databases.js +16 -0
  47. package/dist/setup-sources.js +1 -5
  48. package/dist/setup.d.ts +1 -0
  49. package/dist/setup.js +1 -0
  50. package/dist/sql.d.ts +2 -0
  51. package/dist/sql.js +35 -53
  52. package/dist/telemetry/events.d.ts +2 -1
  53. package/dist/telemetry/events.js +11 -1
  54. package/package.json +2 -1
@@ -1,6 +1,7 @@
1
1
  import YAML from 'yaml';
2
2
  import { buildLiveDatabaseManifestShards } from '../../context/ingest/adapters/live-database/manifest.js';
3
3
  import { isSlYamlPath } from '../../context/sl/source-files.js';
4
+ import { deriveFederatedConnection } from '../connections/federation.js';
4
5
  import { buildKtxRelationshipArtifacts, buildKtxRelationshipDiagnostics, emptyKtxRelationshipProfileArtifact, } from './relationship-diagnostics.js';
5
6
  const LIVE_DATABASE_ADAPTER = 'live-database';
6
7
  const LOCAL_AUTHOR = 'ktx';
@@ -113,7 +114,32 @@ function joinReferencesExistingColumns(join, columnsByTable) {
113
114
  }
114
115
  return true;
115
116
  }
116
- async function loadExistingManifestState(project, connectionId, snapshot) {
117
+ async function federatedSiblingTargets(project, connectionId) {
118
+ const descriptor = deriveFederatedConnection(project.config.connections, project.projectDir);
119
+ if (!descriptor) {
120
+ return new Set();
121
+ }
122
+ const siblings = descriptor.members.filter((member) => member.connectionId !== connectionId);
123
+ const perSibling = await Promise.all(siblings.map((sibling) => siblingJoinTargets(project, sibling.connectionId)));
124
+ return new Set(perSibling.flat());
125
+ }
126
+ async function siblingJoinTargets(project, connectionId) {
127
+ const listed = await project.fileStore.listFiles(schemaDir(connectionId)).catch(() => ({ files: [] }));
128
+ const files = listed.files.filter(isSlYamlPath);
129
+ const perFile = await Promise.all(files.map(async (file) => {
130
+ const shard = await project.fileStore
131
+ .readFile(file)
132
+ .then(({ content }) => YAML.parse(content))
133
+ .catch(() => null);
134
+ // entry.table is buildTableRef's member-local ref (1-3 parts:
135
+ // table / schema.table / catalog.schema.table), never connectionId-
136
+ // prefixed — so prefixing with the member id yields the fully-qualified
137
+ // `to:` form authored in cross-DB joins.
138
+ return Object.values(shard?.tables ?? {}).map((entry) => `${connectionId}.${entry.table}`);
139
+ }));
140
+ return perFile.flat();
141
+ }
142
+ async function loadExistingManifestState(project, connectionId, snapshot, siblingTargets) {
117
143
  const descriptions = new Map();
118
144
  const preservedJoins = new Map();
119
145
  const usage = new Map();
@@ -146,7 +172,7 @@ async function loadExistingManifestState(project, connectionId, snapshot) {
146
172
  }
147
173
  const joins = (entry.joins ?? []).filter((join) => {
148
174
  return ((join.source === 'manual' || join.source === 'inferred') &&
149
- validTableNames.has(join.to) &&
175
+ (validTableNames.has(join.to) || siblingTargets.has(join.to)) &&
150
176
  joinReferencesExistingColumns(join, columnsByTable));
151
177
  });
152
178
  if (joins.length > 0) {
@@ -170,7 +196,8 @@ export async function writeLocalScanManifestShards(input) {
170
196
  manifestShardsWritten: 0,
171
197
  };
172
198
  }
173
- const existing = await loadExistingManifestState(input.project, input.connectionId, input.snapshot);
199
+ const siblingTargets = await federatedSiblingTargets(input.project, input.connectionId);
200
+ const existing = await loadExistingManifestState(input.project, input.connectionId, input.snapshot, siblingTargets);
174
201
  const { shards } = buildLiveDatabaseManifestShards({
175
202
  connectionType: input.driver.toUpperCase(),
176
203
  tables: snapshotTablesToManifestData(input.snapshot, input.descriptionUpdates),
@@ -178,6 +205,7 @@ export async function writeLocalScanManifestShards(input) {
178
205
  existingDescriptions: existing.descriptions,
179
206
  existingPreservedJoins: existing.preservedJoins,
180
207
  existingUsage: existing.usage,
208
+ federatedSiblingTargets: siblingTargets,
181
209
  mapColumnType: (dimensionType) => dimensionType,
182
210
  });
183
211
  const manifestShards = [];
@@ -1,22 +1,35 @@
1
+ import { FEDERATED_CONNECTION_ID } from '../connections/federation.js';
2
+ import { resolveRequiredConnectionId } from '../connections/resolve-connection.js';
1
3
  import { sqlAnalysisDialectForDriver } from '../sql-analysis/dialect.js';
2
4
  import { loadLocalSlSourceRecords } from './local-sl.js';
3
5
  import { toResolvedWire } from './semantic-layer.service.js';
4
6
  import { assertSafeConnectionId } from './source-files.js';
5
7
  const COMPILE_ONLY_REASON = 'Local semantic-layer query compiled SQL but no data-source execution adapter is configured.';
8
+ const FEDERATED_SL_QUERY_UNSUPPORTED = `Semantic-layer queries are per-connection and cannot target the federated connection '${FEDERATED_CONNECTION_ID}'. ` +
9
+ `Run a cross-database query as read-only SQL instead — ktx sql -c ${FEDERATED_CONNECTION_ID} "SELECT ..." or the sql_execution tool — ` +
10
+ 'using catalog-qualified table names (connectionId.schema.table, or connectionId.table for sqlite; ' +
11
+ 'double-quote ids that are not bare identifiers, e.g. "books-db".public.books).';
6
12
  function resolveLocalConnectionId(project, requested) {
7
- if (requested) {
8
- return assertSafeConnectionId(requested);
9
- }
10
- const ids = Object.keys(project.config.connections).sort();
11
- if (ids.length === 1) {
12
- return assertSafeConnectionId(ids[0]);
13
+ return assertSafeConnectionId(resolveRequiredConnectionId(project.config, requested));
14
+ }
15
+ // The planner rejects a source set carrying a join whose `to` names a source
16
+ // outside that set, which would break every query for this connection. Keep only
17
+ // joins resolvable within the connection's own sources; a cross-database join
18
+ // (its `to` qualified by a sibling connection id) is just one such unresolvable
19
+ // target and runs as raw SQL instead. Membership is the test, not a connection-id
20
+ // prefix match, so a same-connection target whose name collides with a sibling
21
+ // connection id is preserved.
22
+ function withResolvableJoinsOnly(source, knownSourceNames) {
23
+ if (source.joins.length === 0) {
24
+ return source;
13
25
  }
14
- throw new Error('connectionId is required when the local project has zero or multiple connections.');
26
+ const joins = source.joins.filter((join) => knownSourceNames.has(join.to));
27
+ return joins.length === source.joins.length ? source : { ...source, joins };
15
28
  }
16
29
  async function loadComputableSources(project, connectionId) {
17
- return (await loadLocalSlSourceRecords(project, { connectionId: assertSafeConnectionId(connectionId) }))
18
- .filter((record) => record.source.table || record.source.sql)
19
- .map((record) => toResolvedWire(record.source));
30
+ const records = (await loadLocalSlSourceRecords(project, { connectionId })).filter((record) => record.source.table || record.source.sql);
31
+ const knownSourceNames = new Set(records.map((record) => record.source.name));
32
+ return records.map((record) => toResolvedWire(withResolvableJoinsOnly(record.source, knownSourceNames)));
20
33
  }
21
34
  function headersFromColumns(columns) {
22
35
  return columns
@@ -24,9 +37,13 @@ function headersFromColumns(columns) {
24
37
  .filter((name) => typeof name === 'string' && name.length > 0);
25
38
  }
26
39
  export async function compileLocalSlQuery(project, options) {
40
+ if (options.connectionId === FEDERATED_CONNECTION_ID) {
41
+ throw new Error(FEDERATED_SL_QUERY_UNSUPPORTED);
42
+ }
27
43
  await options.onProgress?.({ progress: 0, message: 'Compiling query' });
28
44
  const connectionId = resolveLocalConnectionId(project, options.connectionId);
29
- const dialect = sqlAnalysisDialectForDriver(project.config.connections[connectionId]?.driver);
45
+ const driver = project.config.connections[connectionId]?.driver;
46
+ const dialect = sqlAnalysisDialectForDriver(driver);
30
47
  const sources = await loadComputableSources(project, connectionId);
31
48
  await options.onProgress?.({ progress: 0.3, message: 'Generating SQL' });
32
49
  const response = await options.compute.query({
@@ -76,7 +93,7 @@ export async function compileLocalSlQuery(project, options) {
76
93
  ...response.plan,
77
94
  execution: {
78
95
  mode: 'executed',
79
- driver: project.config.connections[connectionId]?.driver ?? 'unknown',
96
+ driver: driver ?? 'unknown',
80
97
  maxRows,
81
98
  rowCount: execution.rowCount,
82
99
  },
@@ -1,6 +1,7 @@
1
1
  import { join } from 'node:path';
2
2
  import YAML from 'yaml';
3
3
  import { z } from 'zod';
4
+ import { deriveFederatedConnection, FEDERATED_CONNECTION_ID } from '../connections/federation.js';
4
5
  import { HybridSearchCore } from '../../context/search/hybrid-search-core.js';
5
6
  import { DEFAULT_PRIORITY, resolveDescription } from './descriptions.js';
6
7
  import { normalizeSemanticLayerDescriptions } from './description-normalization.js';
@@ -86,7 +87,32 @@ function parsedStandaloneSource(parsed, name) {
86
87
  });
87
88
  }
88
89
  export async function loadLocalSlSourceRecords(project, input) {
89
- const connectionId = assertSafeConnectionId(input.connectionId);
90
+ if (input.connectionId === FEDERATED_CONNECTION_ID) {
91
+ const descriptor = deriveFederatedConnection(project.config.connections, project.projectDir);
92
+ if (!descriptor) {
93
+ return [];
94
+ }
95
+ const perMember = await Promise.all(descriptor.members.map(async (member) => {
96
+ const records = await loadSingleConnectionSourceRecords(project, member.connectionId);
97
+ return records.map((record) => {
98
+ // The federated view is one virtual connection: rows carry its id and a
99
+ // member-prefixed name, so a listing/search row round-trips to
100
+ // `ktx sl -c _ktx_federated read <name>`. Member origin lives in the name.
101
+ const name = `${member.connectionId}.${record.name}`;
102
+ return {
103
+ ...record,
104
+ connectionId: FEDERATED_CONNECTION_ID,
105
+ name,
106
+ source: { ...record.source, name },
107
+ };
108
+ });
109
+ }));
110
+ return perMember.flat();
111
+ }
112
+ return loadSingleConnectionSourceRecords(project, input.connectionId);
113
+ }
114
+ async function loadSingleConnectionSourceRecords(project, rawConnectionId) {
115
+ const connectionId = assertSafeConnectionId(rawConnectionId);
90
116
  const dir = `semantic-layer/${connectionId}`;
91
117
  const schemaDir = `${dir}/_schema`;
92
118
  const listed = await project.fileStore.listFiles(dir);
@@ -1,4 +1,6 @@
1
1
  import type { KtxFileStorePort } from '../../context/core/file-store.js';
2
+ /** @internal */
3
+ export declare function isReservedConnectionId(connectionId: string): boolean;
2
4
  export declare function assertSafeConnectionId(connectionId: string): string;
3
5
  export declare function isSafeConnectionId(connectionId: string | undefined): connectionId is string;
4
6
  export declare function sourceNameFromPath(path: string): string;
@@ -17,7 +17,14 @@ function assertSafePathToken(kind, value) {
17
17
  }
18
18
  return value;
19
19
  }
20
+ /** @internal */
21
+ export function isReservedConnectionId(connectionId) {
22
+ return connectionId.startsWith('_ktx_');
23
+ }
20
24
  export function assertSafeConnectionId(connectionId) {
25
+ if (isReservedConnectionId(connectionId)) {
26
+ throw new Error(`Connection id "${connectionId}" uses the reserved "_ktx_" prefix.`);
27
+ }
21
28
  if (!isSafeConnectionId(connectionId)) {
22
29
  throw new Error(`Unsafe connection id: ${connectionId}`);
23
30
  }
@@ -1,9 +1,11 @@
1
+ import { executeFederatedQuery } from './connectors/duckdb/federated-executor.js';
1
2
  import type { KtxSqlQueryExecutorPort } from './context/connections/query-executor.js';
2
3
  import type { KtxLocalProject } from './context/project/project.js';
3
4
  import { createKtxCliScanConnector } from './local-scan-connectors.js';
4
5
  type CreateConnector = typeof createKtxCliScanConnector;
5
6
  export interface KtxCliIngestQueryExecutorDeps {
6
7
  createConnector?: CreateConnector;
8
+ executeFederated?: typeof executeFederatedQuery;
7
9
  }
8
10
  export declare function createKtxCliIngestQueryExecutor(project: KtxLocalProject, deps?: KtxCliIngestQueryExecutorDeps): KtxSqlQueryExecutorPort;
9
11
  export {};
@@ -1,30 +1,16 @@
1
+ import { executeProjectReadOnlySql } from './context/connections/project-sql-executor.js';
1
2
  import { createKtxCliScanConnector } from './local-scan-connectors.js';
2
- async function cleanupConnector(connector) {
3
- await connector?.cleanup?.();
4
- }
5
3
  export function createKtxCliIngestQueryExecutor(project, deps = {}) {
6
4
  const createConnector = deps.createConnector ?? createKtxCliScanConnector;
7
5
  return {
8
6
  async execute(input) {
9
- let connector = null;
10
- try {
11
- connector = await createConnector(project, input.connectionId);
12
- if (!connector.capabilities.readOnlySql || !connector.executeReadOnly) {
13
- throw new Error(`Connection "${input.connectionId}" driver "${connector.driver}" does not support read-only SQL execution.`);
14
- }
15
- const ctx = { runId: 'ingest-sql-execution' };
16
- const result = await connector.executeReadOnly({ connectionId: input.connectionId, sql: input.sql, maxRows: input.maxRows }, ctx);
17
- return {
18
- headers: result.headers,
19
- rows: result.rows,
20
- totalRows: result.totalRows,
21
- command: 'SELECT',
22
- rowCount: result.rowCount,
23
- };
24
- }
25
- finally {
26
- await cleanupConnector(connector);
27
- }
7
+ return executeProjectReadOnlySql({
8
+ project,
9
+ input,
10
+ createConnector: (connectionId) => createConnector(project, connectionId),
11
+ executeFederated: deps.executeFederated,
12
+ runId: 'ingest-sql-execution',
13
+ });
28
14
  },
29
15
  };
30
16
  }
@@ -14,15 +14,24 @@ export interface KtxSetupAgentsArgs {
14
14
  mode: KtxAgentInstallMode;
15
15
  skipAgents: boolean;
16
16
  showNextActions?: boolean;
17
+ installRoot?: string;
18
+ cwd?: string;
17
19
  }
20
+ /** The directory project-scoped agent files land in; equals projectDir unless an install root is chosen. */
21
+ interface KtxAgentInstall {
22
+ target: KtxAgentTarget;
23
+ scope: KtxAgentScope;
24
+ mode: KtxAgentInstallMode;
25
+ installRoot: string;
26
+ }
27
+ /** Install shape for formatting helpers; installRoot falls back to projectDir when absent. */
28
+ type KtxAgentInstallLike = Omit<KtxAgentInstall, 'installRoot'> & {
29
+ installRoot?: string;
30
+ };
18
31
  export type KtxSetupAgentsResult = {
19
32
  status: 'ready';
20
33
  projectDir: string;
21
- installs: Array<{
22
- target: KtxAgentTarget;
23
- scope: KtxAgentScope;
24
- mode: KtxAgentInstallMode;
25
- }>;
34
+ installs: KtxAgentInstall[];
26
35
  nextActions?: string;
27
36
  } | {
28
37
  status: 'skipped';
@@ -41,11 +50,7 @@ export interface KtxAgentInstallManifest {
41
50
  version: 1;
42
51
  projectDir: string;
43
52
  installedAt: string;
44
- installs: Array<{
45
- target: KtxAgentTarget;
46
- scope: KtxAgentScope;
47
- mode: KtxAgentInstallMode;
48
- }>;
53
+ installs: KtxAgentInstall[];
49
54
  entries: Array<{
50
55
  kind: 'file';
51
56
  path: string;
@@ -65,6 +70,7 @@ export declare function plannedKtxAgentFiles(input: {
65
70
  target: KtxAgentTarget;
66
71
  scope: KtxAgentScope;
67
72
  mode: KtxAgentInstallMode;
73
+ installRoot?: string;
68
74
  }): InstallEntry[];
69
75
  export declare function readKtxAgentInstallManifest(projectDir: string): Promise<KtxAgentInstallManifest | null>;
70
76
  /** @internal */
@@ -79,6 +85,10 @@ interface KtxSetupAgentsPromptAdapter {
79
85
  options: KtxSetupPromptOption[];
80
86
  required?: boolean;
81
87
  }): Promise<string[]>;
88
+ text(options: {
89
+ message: string;
90
+ placeholder?: string;
91
+ }): Promise<string | undefined>;
82
92
  cancel(message: string): void;
83
93
  }
84
94
  export interface KtxSetupAgentsDeps {
@@ -91,10 +101,6 @@ export interface InstallSummaryEntry {
91
101
  lines: string[];
92
102
  }
93
103
  /** @internal */
94
- export declare function formatInstallSummaryLines(installs: Array<{
95
- target: KtxAgentTarget;
96
- scope: KtxAgentScope;
97
- mode: KtxAgentInstallMode;
98
- }>, entries: InstallEntry[], projectDir: string): InstallSummaryEntry[];
104
+ export declare function formatInstallSummaryLines(installs: KtxAgentInstallLike[], entries: InstallEntry[], projectDir: string): InstallSummaryEntry[];
99
105
  export declare function runKtxSetupAgentsStep(args: KtxSetupAgentsArgs, io: KtxCliIo, deps?: KtxSetupAgentsDeps): Promise<KtxSetupAgentsResult>;
100
106
  export {};
@@ -1,5 +1,5 @@
1
1
  import { existsSync } from 'node:fs';
2
- import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
3
3
  import { dirname, join, relative, resolve } from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
5
  import { styleText } from 'node:util';
@@ -166,7 +166,7 @@ function universalMcpSnippet(endpoint) {
166
166
  ...(endpoint.tokenAuth ? ['Header: Authorization: Bearer ${KTX_MCP_TOKEN}'] : []),
167
167
  ].join('\n');
168
168
  }
169
- function claudeConfigPath(projectDir, scope) {
169
+ function claudeConfigPath(projectDir, installRoot, scope) {
170
170
  const home = process.env.HOME ?? '';
171
171
  if (scope === 'global') {
172
172
  return { path: join(home, '.claude.json'), jsonPath: ['mcpServers', 'ktx'] };
@@ -174,12 +174,12 @@ function claudeConfigPath(projectDir, scope) {
174
174
  if (scope === 'local') {
175
175
  return { path: join(home, '.claude.json'), jsonPath: ['projects', resolve(projectDir), 'mcpServers', 'ktx'] };
176
176
  }
177
- return { path: join(resolve(projectDir), '.mcp.json'), jsonPath: ['mcpServers', 'ktx'] };
177
+ return { path: join(resolve(installRoot), '.mcp.json'), jsonPath: ['mcpServers', 'ktx'] };
178
178
  }
179
- function cursorConfigPath(projectDir, scope) {
179
+ function cursorConfigPath(installRoot, scope) {
180
180
  const home = process.env.HOME ?? '';
181
181
  return {
182
- path: scope === 'global' ? join(home, '.cursor/mcp.json') : join(resolve(projectDir), '.cursor/mcp.json'),
182
+ path: scope === 'global' ? join(home, '.cursor/mcp.json') : join(resolve(installRoot), '.cursor/mcp.json'),
183
183
  jsonPath: ['mcpServers', 'ktx'],
184
184
  };
185
185
  }
@@ -226,12 +226,12 @@ async function installMcpClientConfig(input) {
226
226
  notices.push(MCP_DAEMON_REQUIRED_NOTICE);
227
227
  }
228
228
  if (input.target === 'claude-code') {
229
- const config = claudeConfigPath(input.projectDir, input.scope);
229
+ const config = claudeConfigPath(input.projectDir, input.installRoot, input.scope);
230
230
  await writeJsonKey(config.path, config.jsonPath, claudeMcpEntry(endpoint));
231
231
  entries.push({ kind: 'json-key', path: config.path, jsonPath: config.jsonPath });
232
232
  }
233
233
  else if (input.target === 'cursor') {
234
- const config = cursorConfigPath(input.projectDir, input.scope);
234
+ const config = cursorConfigPath(input.installRoot, input.scope);
235
235
  await writeJsonKey(config.path, config.jsonPath, cursorMcpEntry(endpoint));
236
236
  entries.push({ kind: 'json-key', path: config.path, jsonPath: config.jsonPath });
237
237
  }
@@ -241,7 +241,7 @@ async function installMcpClientConfig(input) {
241
241
  else if (input.target === 'opencode') {
242
242
  const path = input.scope === 'global'
243
243
  ? '~/.config/opencode/opencode.json'
244
- : relative(input.projectDir, join(input.projectDir, 'opencode.json'));
244
+ : relative(input.installRoot, join(input.installRoot, 'opencode.json'));
245
245
  snippets.push(`Add this OpenCode MCP snippet to ${path}:\n${opencodeSnippet(endpoint)}`);
246
246
  }
247
247
  else if (input.target === 'universal') {
@@ -251,7 +251,7 @@ async function installMcpClientConfig(input) {
251
251
  }
252
252
  function plannedMcpJsonEntries(input) {
253
253
  if (input.target === 'claude-code') {
254
- const config = claudeConfigPath(input.projectDir, input.scope);
254
+ const config = claudeConfigPath(input.projectDir, input.installRoot, input.scope);
255
255
  return [{ kind: 'json-key', path: config.path, jsonPath: config.jsonPath }];
256
256
  }
257
257
  if (input.target === 'claude-desktop') {
@@ -259,7 +259,7 @@ function plannedMcpJsonEntries(input) {
259
259
  return [{ kind: 'json-key', path: config.path, jsonPath: config.jsonPath }];
260
260
  }
261
261
  if (input.target === 'cursor') {
262
- const config = cursorConfigPath(input.projectDir, input.scope);
262
+ const config = cursorConfigPath(input.installRoot, input.scope);
263
263
  return [{ kind: 'json-key', path: config.path, jsonPath: config.jsonPath }];
264
264
  }
265
265
  return [];
@@ -324,7 +324,7 @@ export function plannedKtxAgentFiles(input) {
324
324
  }
325
325
  throw new Error(`Global ${input.target} installation is not supported; omit --global.`);
326
326
  }
327
- const root = resolve(input.projectDir);
327
+ const root = resolve(input.installRoot ?? input.projectDir);
328
328
  const analyticsEntries = {
329
329
  'claude-code': [
330
330
  { kind: 'file', path: join(root, '.claude/skills/ktx-analytics/SKILL.md'), role: 'analytics-skill' },
@@ -502,7 +502,8 @@ function entryKey(entry) {
502
502
  function mergeManifest(projectDir, existing, installs, entries) {
503
503
  const installMap = new Map();
504
504
  for (const install of [...(existing?.installs ?? []), ...installs]) {
505
- installMap.set(`${install.target}:${install.scope}:${install.mode}`, install);
505
+ const installRoot = install.installRoot ?? resolve(projectDir);
506
+ installMap.set(`${install.target}:${install.scope}:${install.mode}:${installRoot}`, { ...install, installRoot });
506
507
  }
507
508
  const entryMap = new Map();
508
509
  for (const entry of [...(existing?.entries ?? []), ...entries]) {
@@ -611,18 +612,31 @@ function formatInlinePath(path) {
611
612
  }
612
613
  return path;
613
614
  }
615
+ function installSummaryTitle(install, projectDir) {
616
+ const name = targetDisplayName(install.target);
617
+ if (install.scope !== 'project') {
618
+ return `${name} · ${scopeDisplayName(install.scope)}`;
619
+ }
620
+ const installRoot = resolve(install.installRoot ?? projectDir);
621
+ if (installRoot === resolve(projectDir)) {
622
+ return `${name} · ${scopeDisplayName('project')}`;
623
+ }
624
+ return `${name} · ${formatInlinePath(installRoot)}`;
625
+ }
614
626
  /** @internal */
615
627
  export function formatInstallSummaryLines(installs, entries, projectDir) {
616
628
  const entriesByTarget = new Map();
617
629
  for (const install of installs) {
618
- const plannedFilePaths = new Set(plannedKtxAgentFiles({ projectDir, ...install })
630
+ const installRoot = install.installRoot ?? projectDir;
631
+ const plannedFilePaths = new Set(plannedKtxAgentFiles({ projectDir, ...install, installRoot })
619
632
  .filter((entry) => entry.kind === 'file')
620
633
  .map((entry) => entry.path));
621
634
  entriesByTarget.set(install.target, entries.filter((entry) => entry.kind === 'file' && plannedFilePaths.has(entry.path)));
622
635
  }
623
636
  const mcpEntriesByTarget = new Map();
624
637
  for (const install of installs) {
625
- const plannedMcpKeys = new Set(plannedMcpJsonEntries({ projectDir, ...install }).map(entryKey));
638
+ const installRoot = install.installRoot ?? projectDir;
639
+ const plannedMcpKeys = new Set(plannedMcpJsonEntries({ projectDir, installRoot, target: install.target, scope: install.scope }).map(entryKey));
626
640
  mcpEntriesByTarget.set(install.target, entries.filter((entry) => entry.kind === 'json-key' && plannedMcpKeys.has(entryKey(entry))));
627
641
  }
628
642
  return installs.map((install) => {
@@ -663,7 +677,7 @@ export function formatInstallSummaryLines(installs, entries, projectDir) {
663
677
  lines.push(`${guidanceInstallLine(install.target)}.`);
664
678
  }
665
679
  return {
666
- title: `${targetDisplayName(install.target)} · ${scopeDisplayName(install.scope)}`,
680
+ title: installSummaryTitle(install, projectDir),
667
681
  lines,
668
682
  };
669
683
  });
@@ -726,6 +740,9 @@ function manualActionFromSnippet(snippet) {
726
740
  body,
727
741
  };
728
742
  }
743
+ function openFromDirectoryLabel(installRoot, projectDir) {
744
+ return resolve(installRoot) === resolve(projectDir) ? 'the ktx project directory' : 'the install directory';
745
+ }
729
746
  function formatAgentNextActions(input) {
730
747
  const projectDir = resolve(input.projectDir);
731
748
  const lines = [];
@@ -762,10 +779,11 @@ function formatAgentNextActions(input) {
762
779
  if (claudeCodeInstall) {
763
780
  lines.push(`${step}. Open Claude Code`);
764
781
  if (claudeCodeInstall.scope === 'project') {
765
- lines.push(' Open Claude Code from the ktx project directory:');
782
+ const installRoot = resolve(claudeCodeInstall.installRoot ?? projectDir);
783
+ lines.push(` Open Claude Code from ${openFromDirectoryLabel(installRoot, projectDir)}:`);
766
784
  lines.push('');
767
785
  lines.push(' RUN:');
768
- lines.push(` cd ${shellScriptQuote(projectDir)}`);
786
+ lines.push(` cd ${shellScriptQuote(installRoot)}`);
769
787
  lines.push(' claude');
770
788
  }
771
789
  else {
@@ -779,10 +797,11 @@ function formatAgentNextActions(input) {
779
797
  if (cursorInstall) {
780
798
  lines.push(`${step}. Open Cursor`);
781
799
  if (cursorInstall.scope === 'project') {
782
- lines.push(' Open Cursor from the ktx project directory:');
800
+ const installRoot = resolve(cursorInstall.installRoot ?? projectDir);
801
+ lines.push(` Open Cursor from ${openFromDirectoryLabel(installRoot, projectDir)}:`);
783
802
  lines.push('');
784
803
  lines.push(' OPEN:');
785
- lines.push(` ${projectDir}`);
804
+ lines.push(` ${installRoot}`);
786
805
  }
787
806
  else {
788
807
  lines.push(' Open Cursor.');
@@ -844,6 +863,70 @@ async function markAgentsComplete(projectDir) {
844
863
  await writeFile(project.configPath, serializeKtxProjectConfig(project.config), 'utf-8');
845
864
  await markKtxSetupStateStepComplete(projectDir, 'agents');
846
865
  }
866
+ // A typed path never passes through a shell, so expand a leading ~ here; HOME
867
+ // matches formatInlinePath so the ~/… hints shown in the menu round-trip.
868
+ function resolveTypedInstallDir(cwd, raw) {
869
+ const home = process.env.HOME;
870
+ if (home && (raw === '~' || raw.startsWith('~/'))) {
871
+ return resolve(home, raw.slice(2));
872
+ }
873
+ return resolve(cwd, raw);
874
+ }
875
+ async function ensureInstallDir(resolvedPath) {
876
+ if (existsSync(resolvedPath)) {
877
+ if (!(await stat(resolvedPath)).isDirectory()) {
878
+ throw new Error(`Install directory path is a file, not a directory: ${resolvedPath}`);
879
+ }
880
+ return resolvedPath;
881
+ }
882
+ await mkdir(resolvedPath, { recursive: true });
883
+ return resolvedPath;
884
+ }
885
+ async function promptInstallDirectory(input) {
886
+ const { prompts, io, cwd, projectRoot, scopeTargets } = input;
887
+ const options = [
888
+ { value: 'project', label: 'ktx project directory', hint: formatInlinePath(projectRoot) },
889
+ ...(cwd !== projectRoot
890
+ ? [{ value: 'current', label: 'Current directory', hint: formatInlinePath(cwd) }]
891
+ : []),
892
+ { value: 'custom', label: 'Custom directory…', hint: 'Enter a path' },
893
+ ...(scopeTargets.every(targetSupportsGlobalScope)
894
+ ? [
895
+ {
896
+ value: 'global',
897
+ label: 'Global scope (user config)',
898
+ hint: 'Agents can load this ktx project from any working directory.',
899
+ },
900
+ ]
901
+ : []),
902
+ ];
903
+ const choice = await prompts.select({
904
+ message: `Where should ktx install agent config?\n\nktx project: ${projectRoot}`,
905
+ options,
906
+ });
907
+ if (choice === 'back')
908
+ return 'back';
909
+ if (choice === 'global')
910
+ return { scope: 'global', installRoot: projectRoot };
911
+ if (choice === 'current')
912
+ return { scope: 'project', installRoot: cwd };
913
+ if (choice === 'project')
914
+ return { scope: 'project', installRoot: projectRoot };
915
+ while (true) {
916
+ const typed = await prompts.text({ message: 'Enter the directory to install agent config into' });
917
+ if (typed === undefined)
918
+ return 'back';
919
+ const trimmed = typed.trim();
920
+ if (trimmed === '')
921
+ continue;
922
+ try {
923
+ return { scope: 'project', installRoot: await ensureInstallDir(resolveTypedInstallDir(cwd, trimmed)) };
924
+ }
925
+ catch (error) {
926
+ io.stderr.write(`${errorMessage(error)}\n`);
927
+ }
928
+ }
929
+ }
847
930
  export async function runKtxSetupAgentsStep(args, io, deps = {}) {
848
931
  if (args.skipAgents) {
849
932
  io.stdout.write('│ Agent integration skipped.\n');
@@ -905,30 +988,32 @@ export async function runKtxSetupAgentsStep(args, io, deps = {}) {
905
988
  : 'Missing agent target: pass --target or use interactive setup.\n');
906
989
  return { status: 'missing-input', projectDir: args.projectDir };
907
990
  }
991
+ const cwd = resolve(args.cwd ?? process.cwd());
992
+ const projectRoot = resolve(args.projectDir);
908
993
  const scopeTargets = targets.filter((target) => target !== 'claude-desktop');
909
- const selectedScope = args.inputMode !== 'disabled' &&
910
- args.scope === 'project' &&
911
- scopeTargets.length > 0 &&
912
- scopeTargets.every(targetSupportsGlobalScope)
913
- ? (await prompts.select({
914
- message: `Where should ktx install supported agent config?\n\nktx project: ${resolve(args.projectDir)}`,
915
- options: [
916
- {
917
- value: 'project',
918
- label: 'Project scope (ktx project directory)',
919
- hint: 'Only agents opened from this ktx project path load the project-scoped config.',
920
- },
921
- {
922
- value: 'global',
923
- label: 'Global scope (user config)',
924
- hint: 'Agents can load this ktx project from any working directory.',
925
- },
926
- ],
927
- }))
928
- : args.scope;
929
- if (selectedScope === 'back')
930
- return { status: 'back', projectDir: args.projectDir };
931
- const installs = targets.map((target) => ({ target, scope: effectiveInstallScope(target, selectedScope), mode }));
994
+ let selectedScope = args.scope;
995
+ let installRoot = projectRoot;
996
+ if (args.installRoot !== undefined) {
997
+ try {
998
+ installRoot = await ensureInstallDir(resolveTypedInstallDir(cwd, args.installRoot));
999
+ }
1000
+ catch (error) {
1001
+ writePrefixedLines((chunk) => io.stderr.write(chunk), errorMessage(error));
1002
+ return { status: 'failed', projectDir: args.projectDir };
1003
+ }
1004
+ selectedScope = 'project';
1005
+ }
1006
+ else if (args.inputMode !== 'disabled' && args.scope === 'project' && scopeTargets.length > 0) {
1007
+ const decision = await promptInstallDirectory({ prompts, io, cwd, projectRoot, scopeTargets });
1008
+ if (decision === 'back')
1009
+ return { status: 'back', projectDir: args.projectDir };
1010
+ selectedScope = decision.scope;
1011
+ installRoot = decision.installRoot;
1012
+ }
1013
+ const installs = targets.map((target) => {
1014
+ const scope = effectiveInstallScope(target, selectedScope);
1015
+ return { target, scope, mode, installRoot: scope === 'project' ? installRoot : projectRoot };
1016
+ });
932
1017
  const entries = [];
933
1018
  const snippets = [];
934
1019
  const notices = new Set();
@@ -938,6 +1023,7 @@ export async function runKtxSetupAgentsStep(args, io, deps = {}) {
938
1023
  entries.push(...targetEntries);
939
1024
  const mcpResult = await installMcpClientConfig({
940
1025
  projectDir: args.projectDir,
1026
+ installRoot: install.installRoot,
941
1027
  target: install.target,
942
1028
  scope: install.scope,
943
1029
  });
@@ -1,6 +1,7 @@
1
1
  import type { KtxLlmRuntimePort } from './context/llm/runtime-port.js';
2
2
  import { type ProposeQueryHistoryServiceAccountFiltersInput, type QueryHistoryFilterProposal } from './context/ingest/adapters/historic-sql/query-history-filter-picker.js';
3
3
  import { type HistoricSqlReadinessProbe } from './context/ingest/historic-sql-probes.js';
4
+ import { type KtxProjectConnectionConfig } from './context/project/config.js';
4
5
  import { loadKtxProject } from './context/project/project.js';
5
6
  import type { KtxTableListEntry } from './context/scan/types.js';
6
7
  import { type KtxCliIo } from './cli-runtime.js';
@@ -90,6 +91,8 @@ export interface KtxSetupDatabasesDeps {
90
91
  createQueryHistoryLlmRuntime?: (projectDir: string, project: Awaited<ReturnType<typeof loadKtxProject>>) => KtxLlmRuntimePort | null;
91
92
  }
92
93
  /** @internal */
94
+ export declare function federationNoticeFor(connections: Record<string, KtxProjectConnectionConfig>, projectDir: string): string | null;
95
+ /** @internal */
93
96
  export declare function managedDaemonOptionsForSetupQueryHistoryPicker(input: {
94
97
  projectDir: string;
95
98
  args: Pick<KtxSetupDatabasesArgs, 'cliVersion' | 'runtimeInstallPolicy' | 'inputMode'>;