ai-powered-conversational-intelligence

09 — Tenant-Aware Semantic Catalog

Prompt 09 of the 32-prompt Enterprise AI Analytics & Recommendation Platform initiative (00_MASTER_IMPLEMENTATION_CONTRACT.md), following 08_ONBOARDING_ENGINE_CONTRACT.md. INSPECT/PLAN/IMPLEMENT/TEST/ REVIEW/FIX/DOCUMENT — a new semantic/catalog.py+catalog_policy.py (typed model + ABAC, zero DB dependency), a new identity/ persistence slice (1 ORM table + Alembic migration + repository module, following identity/repositories/onboarding.py’s own split exactly), a new RBAC permission pair, a new ChunkType.BUSINESS_CONCEPT + two small retrieval/ extensions (never a second retrieval stack), a new planning- prompt renderer, and a new REST surface (api/semantic_catalog.py). No existing route, node, chunk type, or Settings field changed behavior for a caller that doesn’t use the new capabilities.

1. Inspection: what already existed vs. the real gaps

retrieval/ already had a 7-type (ChunkType), per-database-Chroma- collection RAG pipeline with its own retriever/reranker/ingestion, already injected into generate_sql_node’s prompt (agent.llm_client._build_business_context_block) and, for relationship chunks specifically, into plan_query_node’s prompt (_build_plan_relationship_block, Prompt 07). semantic/metrics.py already had MetricDefinition/MetricStatus (draft/approved/ deprecated)/MetricRegistry, but flat, global, not tenant-aware, and with no real approval workflow — 02_TARGET_ARCHITECTURE.md itself named this as two explicit future gaps: “(a) retrieval renders a governed MetricDefinition into a METRIC chunk’s extra” and “(b) a real approval-workflow/CRUD surface sets owner/APPROVED.” agent/ provenance.py already had the DataTruthLevel vocabulary this prompt needed, reused (always AI_INFERENCE) by Prompt 07/08’s own inference dataclasses. onboarding/policy.py + identity/repositories/ onboarding.py already established the exact RBAC+ABAC+plain-CRUD split this prompt’s own review workflow needed.

Real gaps, found by reading the actual code (not assumed):

  1. No single typed concept covering names/descriptions/grain/keys/ relationships/domains/dimensions/synonyms/business rules/examples/ evidence/confidence/status/owner/version together — glossary.yaml covered synonyms+definition only; MetricDefinition covered metrics only; TableDescription/ColumnClassification were untyped-business- metadata, global, no status/owner/version at all.
  2. No draft/reviewed/published/superseded lifecycle anywhere — MetricStatus has a different 3-value vocabulary (draft/approved/ deprecated) with no intermediate “reviewed but not yet live” state.
  3. No tenant dimension on any retrieval/business-metadata construct — Chunk has no tenant_id; config/table_descriptions.yaml/ sensitive_columns.yaml are single global files.
  4. No review API — nothing catalog-specific existed to create, review, or publish a business concept.
  5. Planning never saw anything beyond relationship-type retrieved context — confirmed by reading generate_query_plan_from_llm’s actual signature before this prompt.

2. What was built

semantic/catalog.py (new)

CatalogConceptType (entity/metric/dimension/domain), CatalogStatus (draft/reviewed/published/superseded — the exact vocabulary this prompt specifies, deliberately distinct from MetricStatus), VALID_STATUS_ TRANSITIONS (the single source of truth both the repository and the API layer check), status_to_truth_level (draft/reviewed → AI_INFERENCE, published → CONFIRMED_BUSINESS_TRUTH, superseded → AI_INFERENCE, never re-promoted — the structural enforcement of master rules 9-10), CatalogEntrySnapshot (the ORM-independent read shape — mirrors onboarding/semantic_contract.py::ConfirmedReviewItem’s identical “no identity/ORM dependency” precedent, so retrieval/ never needs SQLAlchemy on its import graph), metric_definition_from_snapshot + build_metric_registry_from_catalog (the explicit bridge closing 02_TARGET_ARCHITECTURE.md’s named gap “(b)” — a METRIC-type published entry becomes a real MetricDefinition with status=APPROVED, reusing YamlMetricRegistry’s own constructor as a second, richer data source for the same MetricRegistry Protocol, not a new registry class).

semantic/catalog_policy.py (new)

CatalogAction (create/view/edit_draft/review/request_changes/publish), AuthorizationDecision(allowed, reason), authorize_catalog_action — mirrors onboarding/policy.py exactly: deny-by-default, cross-tenant denial checked before any RBAC branch, CATALOG_MANAGE (admin-tier: create/edit/publish) vs. CATALOG_REVIEW (analyst-tier: review/ request-changes), either permission for viewing.

identity/ additions

RAG integration — the acceptance-criterion-critical piece

New ChunkType.BUSINESS_CONCEPT (retrieval/models.py) — deliberately additive, not a reuse of GLOSSARY/METRIC: a governed catalog entry carries structured governance fields (status/owner/ confidence/evidence/version) a hand-authored YAML chunk never does, and conflating the two under one type would make them indistinguishable during rerank/labeling (the same reasoning 02_TARGET_ARCHITECTURE.md already gives for keeping semantic/metrics.py separate from ChunkType.METRIC). Confirmed safe to add: no test anywhere enumerates ChunkType exhaustively.

The structural enforcement of “never silently convert AI inference into confirmed business truth” (master rule 10): only a PUBLISHED entry is ever converted into a chunk — business_concept_chunk_from_ catalog_entry/sync_catalog_entry_to_vector_store are only ever called from the publish route. A draft or reviewed entry simply never reaches retrieval, by construction, not by a runtime check that could be forgotten.

REST API — api/semantic_catalog.py + api/semantic_catalog_schemas.py (new)

Mirrors api/onboarding.py’s shape exactly (same _require_entry/ _authorize/_entry_out helper pattern, same anti-enumeration cross- tenant→404 mapping): POST /semantic-catalog/entries (create draft), GET /semantic-catalog/entries (list, tenant-scoped, filters), GET /semantic-catalog/entries/{id}, GET /semantic-catalog/entries/concept/ {concept_key}/versions (history), PATCH /semantic-catalog/entries/{id} (edit draft), POST .../review (draft→reviewed), POST .../request- changes (reviewed→draft), POST .../publish (reviewed→published + vector-store sync + supersede). A vector-store sync failure on publish does not roll back the already-committed status change — logged and treated the same “retrieval is best-effort, never a hard gate” philosophy retrieval.retriever.retrieve_business_context already establishes (see §6 for the disclosed limitation this creates).

3. Explicitly out of scope

4. Testing

7 new test files, ~91 tests, all passing:

Full suite: 2724 tests pass, zero regressions (pytest -q, re-run after every change in this prompt; 91 of those are new — 82 across the 6 brand-new test files plus 9 added to the existing tests/test_llm_client _planning.py). ruff check / black --check / mypy on every touched/new production and test file: clean (one pre-existing, unrelated api/main.py finding and 6 pre-existing tests/test_llm_client _planning.py findings confirmed via git stash to predate this prompt’s changes — left alone, not claimed as fixed, per CLAUDE.md’s own documented CI-hygiene-gap tracking).

5. Security / tenant-isolation / performance review

6. Known limitations / remaining risks

Per 02_TARGET_ARCHITECTURE.md §5’s own table: wire analytics/ (ResultSummaryAnalyticsProvider) into generate_insight_node and surface AnalyticsResult in AskResponse — unchanged recommendation carried forward from Prompts 04-08, still pending explicit sign-off since it changes a live response shape. Alternatively, closing this prompt’s own §6 vector-store-sync-failure gap with a small backfill script would be a natural, low-risk immediate follow-up.