ai-powered-conversational-intelligence

Prompt 35 – Advanced Semantic Intelligence

A deterministic analysis over the governed semantic catalog that finds synonym candidates, term clusters, ambiguous terms, conflicting definitions, metric relationships and candidate business rules. It also computes the impact of a proposed change. Findings go into a review queue ordered by risk, not volume. Behind ENABLE_SEMANTIC_INTELLIGENCE (default off).

Inspected first

Decisions

What was built

Module Does
semantic/intelligence/normalize.py Canonical form, token overlap, same_concept (with the false-match guards). Cached.
semantic/intelligence/detect.py Synonyms, term clusters, ambiguity, conflicts (proposed_definition_differs, conflicting_metric_definitions), candidate rules.
semantic/intelligence/impact.py Metric relationships (shares_source_tables, references_metric) and impact_of_change, including proposed source-table removal.
semantic/intelligence/risk.py The risk score, tiers, and risk-first ordering.
semantic/intelligence/engine.py run_analysis: runs every detector, counts dependents, orders the queue.
identity/repositories/semantic_intelligence.py Upsert with versioning, queue listing, accept/dismiss, finding rollback, catalog-version rollback as a new draft.
identity/models.py::SemanticFinding The persisted finding, unique per (tenant_id, database_id, finding_key).
identity/migrations/versions/a1f5c9e3b7d2_semantic_intelligence.py Additive: one table, two indexes. Downgrade drops only that table.
api/semantic_intelligence.py Six routes (below).

Each finding carries: kind, title, detail, subjects (with each concept’s business owner, status and version), evidence (entry ids and the fields that differ), confidence, truth_level (always ai_inference), risk_score, risk_tier, reasons, status (open / accepted / dismissed), version and history.

API

All under /semantic-intelligence. Behind ENABLE_SEMANTIC_INTELLIGENCE (404 when off).

Route Permission Does
POST /run?database_id= review Analyses one database’s catalog and upserts up to the cap. Returns counts (created, updated, unchanged, truncated) and the top 10 findings.
GET /findings?database_id=&status= review The queue, riskiest first.
POST /findings/{id}/decision review accept or dismiss an open finding. A decided finding cannot be decided again (409). Review state only.
POST /findings/{id}/rollback manage Restores an earlier finding version as a new version, reopened for review.
GET /impact/{concept_key}?database_id=&proposed_source_tables= review Dependents, and what a source-table change would affect. Unknown concept is 404.
POST /entries/{entry_id}/rollback manage Creates a new DRAFT from an older catalog version. The published row is untouched.

Authorization uses semantic.catalog_policy.authorize_catalog_action, the same check the catalog routes run. A resource in another tenant returns 404, not 403. A database_id the tenant may not use also returns 404. A user with no resolvable tenant gets 403.

Request bodies forbid unknown fields. Decision accepts only accept or dismiss. Responses carry truth_level on every finding.

Configuration

Setting Env var Default
enable_semantic_intelligence ENABLE_SEMANTIC_INTELLIGENCE false
semantic_intelligence_max_findings SEMANTIC_INTELLIGENCE_MAX_FINDINGS 500

The cap keeps the riskiest findings. Anything beyond it is reported as findings_truncated, never silently dropped.

Tests

Results: 77 pass across the three semantic suites. The full backend suite passes (3,993 tests) on the final code, including tests/security/. ruff and mypy are clean on all new code. Only a pre-existing mypy error in db/schema_introspection.py remains. The migration’s DDL was checked offline with alembic --sql, which shows one table, two indexes and the head bump.

Performance and cost

Measured on synthetic catalogs (n entries, every entry a metric sharing one of 20 tables, so this is the relationship-heavy case):

Entries Findings Time (before caching) Time (after caching)
50 40 111 ms 11 ms
200 900 2,049 ms 270 ms
500 6,000 17,082 ms 1,107 ms

Caching the pure normalization functions gave about a 15x improvement. The detectors are still pairwise, so cost grows roughly with the square of the number of terms. A catalog of a few thousand terms would need blocking by canonical form before the pairwise pass.

Persistence costs one SELECT per detected finding, capped at 500 per run. A run is an explicit request, never run on the chat path, so it adds no latency to a question.

Limitations (honest)

Risks

Next prompt

Prompt 36: a live run of the engine against a real tenant’s catalog (curated test tenant, not production). Measure the true synonym, conflict and ambiguity rates and the reviewer acceptance rate. Use that to tune the risk weights and decide whether to add blocking for large catalogs and a schema-aware ambiguity check.