mirror of
https://github.com/Kaelio/ktx.git
synced 2026-07-04 10:52:13 +02:00
* fix(ingest): recover textual-conflict gate failures; fix query-history adapter Two latent gaps in the isolated-diff local-ingest pipeline that can abort an otherwise-successful ingest: - Metabase: when a work-unit patch hit both a textual conflict and a post-merge dangling sl_ref, the after-textual-resolution branch returned a hard semantic_conflict and rolled back the whole job. It now runs the same repairGateFailure recovery the clean-apply branch already uses (re-validate, then commit the union of resolved + repaired paths), reaching parity. - Query history: the historic-sql adapter was registered only when ktx.yaml had context.queryHistory.enabled=true, so `--query-history` threw "Adapter not available for local ingest". Registration now resolves the dialect from driver capability, since the explicit --query-history request is itself the opt-in; the config-gated helper is unchanged for status/setup/probes. Adds the previously-missing tests for both paths. * chore: sync uv.lock to 0.8.0 (regenerated with pinned uv 0.11.11) * fix(ingest): drop ktx's own scan probes and dedup tables in query history Query history (historic-sql) mined two kinds of noise back into context: - ktx's own warehouse scan emits relationship- and column-profiling probes (the relationship_profile_values aggregation and the child_values/parent_values FK-overlap CTEs) into pg_stat_statements. shouldDropBySql now filters these ktx-owned, dialect-stable signatures so ktx introspection is not ingested as usage history. - The same physical table appears both bare (accounts, via search_path) and schema-qualified (orbit_raw.accounts), producing duplicate per-table work units. canonicalizeTableIdentifiers collapses a bare name into its unique qualified form before work-unit keying; ambiguous names are left untouched. On the orbit demo this removes ~35% of sampled query templates (ktx self-probes) and ~45 duplicate per-table work units. * docs(agents): add Design Reasoning Defaults section
233 lines
6.8 KiB
TypeScript
233 lines
6.8 KiB
TypeScript
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { loadKtxProject } from '../src/context/project/project.js';
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
import { createKtxCliLocalIngestAdapters } from '../src/local-adapters.js';
|
|
|
|
function sqlAnalysisStub() {
|
|
return {
|
|
async analyzeForFingerprint(sql: string) {
|
|
return {
|
|
fingerprint: 'fp',
|
|
normalizedSql: sql,
|
|
tablesTouched: [],
|
|
literalSlots: [],
|
|
};
|
|
},
|
|
async analyzeBatch() {
|
|
return new Map();
|
|
},
|
|
async validateReadOnly() {
|
|
return { ok: true };
|
|
},
|
|
};
|
|
}
|
|
|
|
async function writeProject(projectDir: string, body: string): Promise<void> {
|
|
await writeFile(join(projectDir, 'ktx.yaml'), body, 'utf-8');
|
|
}
|
|
|
|
describe('CLI local ingest adapters', () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(async () => {
|
|
tempDir = await mkdtemp(join(tmpdir(), 'ktx-cli-local-adapters-'));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it('registers Postgres historic SQL from connection context query history', async () => {
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' warehouse:',
|
|
' driver: postgres',
|
|
' url: env:WAREHOUSE_DATABASE_URL',
|
|
' readonly: true',
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: true',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
const adapters = createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'warehouse',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
});
|
|
|
|
expect(adapters.find((adapter) => adapter.source === 'historic-sql')?.skillNames).toEqual([
|
|
'historic_sql_table_digest',
|
|
'historic_sql_patterns',
|
|
]);
|
|
});
|
|
|
|
it('registers historic SQL when explicitly requested even if connection query history is disabled', async () => {
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' warehouse:',
|
|
' driver: postgres',
|
|
' url: env:WAREHOUSE_DATABASE_URL',
|
|
' readonly: true',
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: false',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
// `--query-history` sets historicSqlConnectionId for the run; that explicit
|
|
// request is the opt-in, so the persisted context.queryHistory.enabled flag
|
|
// must not gate adapter registration.
|
|
const adapters = createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'warehouse',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
});
|
|
|
|
expect(adapters.some((adapter) => adapter.source === 'historic-sql')).toBe(true);
|
|
});
|
|
|
|
it('registers BigQuery historic SQL from the requested connection', async () => {
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' bq:',
|
|
' driver: bigquery',
|
|
' dataset_id: analytics',
|
|
' location: us',
|
|
' credentials_json: \'{"project_id":"demo-project"}\'',
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: true',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
const adapters = createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'bq',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
});
|
|
|
|
expect(adapters.find((adapter) => adapter.source === 'historic-sql')?.skillNames).toEqual([
|
|
'historic_sql_table_digest',
|
|
'historic_sql_patterns',
|
|
]);
|
|
});
|
|
|
|
it('registers Snowflake historic SQL from the requested connection', async () => {
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' sf:',
|
|
' driver: snowflake',
|
|
' account: acct',
|
|
' warehouse: wh',
|
|
' database: ANALYTICS',
|
|
' schema_name: PUBLIC',
|
|
' username: reader',
|
|
' password: env:SNOWFLAKE_PASSWORD',
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: true',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
const adapters = createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'sf',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
});
|
|
|
|
expect(adapters.find((adapter) => adapter.source === 'historic-sql')?.skillNames).toEqual([
|
|
'historic_sql_table_digest',
|
|
'historic_sql_patterns',
|
|
]);
|
|
});
|
|
|
|
it('resolves BigQuery credentials_json from a file: reference for query history ingest', async () => {
|
|
const credentialsPath = join(tempDir, 'credentials.json');
|
|
await writeFile(credentialsPath, JSON.stringify({ project_id: 'demo-project' }), 'utf-8');
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' bq:',
|
|
' driver: bigquery',
|
|
' dataset_id: analytics',
|
|
' location: us',
|
|
` credentials_json: 'file:${credentialsPath}'`,
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: true',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
const adapters = createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'bq',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
});
|
|
|
|
expect(adapters.find((adapter) => adapter.source === 'historic-sql')?.skillNames).toEqual([
|
|
'historic_sql_table_digest',
|
|
'historic_sql_patterns',
|
|
]);
|
|
});
|
|
|
|
it('uses query-history wording for public BigQuery capability errors', async () => {
|
|
await writeProject(
|
|
tempDir,
|
|
[
|
|
'connections:',
|
|
' bq:',
|
|
' driver: bigquery',
|
|
' readonly: true',
|
|
' dataset_id: analytics',
|
|
' credentials_json: "{}"',
|
|
' context:',
|
|
' queryHistory:',
|
|
' enabled: true',
|
|
'ingest:',
|
|
' adapters:',
|
|
' - historic-sql',
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
const project = await loadKtxProject({ projectDir: tempDir });
|
|
|
|
expect(() =>
|
|
createKtxCliLocalIngestAdapters(project, {
|
|
historicSqlConnectionId: 'bq',
|
|
sqlAnalysis: sqlAnalysisStub(),
|
|
}),
|
|
).toThrow('Query history BigQuery connection requires credentials_json.project_id');
|
|
});
|
|
});
|