ai-powered-conversational-intelligence

07 — Relationship & Schema Intelligence

Prompt 07 of the 32-prompt Enterprise AI Analytics & Recommendation Platform initiative (00_MASTER_IMPLEMENTATION_CONTRACT.md), following 06_DATABASE_DISCOVERY_CONTRACT.md. INSPECT/PLAN/IMPLEMENT/TEST/ REVIEW/FIX/DOCUMENT — one new module (db/relationship_inference.py), a small, catalog-only extension to db/schema_introspection.py, a new chunk builder in retrieval/chunking.py, additive wiring in retrieval/ingestion.py, and a real, previously-missing plumbing fix in agent/llm_client.py/agent/nodes.py (planning never saw relationship context at all). No existing route, node, or Settings field changed behavior for a caller that doesn’t use the new capabilities.

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

db/schema_introspection.py already discovered real, declared foreign keys (ForeignKeyInfo) — a DATABASE_FACT, no inference needed. retrieval/chunking.py::relationship_chunks_from_schema already turned every declared FK into a dedicated, independently-retrievable relationship-type chunk. agent.provenance.DataTruthLevel (built in Prompt 02) already defined the DATABASE_FACT/AI_INFERENCE/ CONFIRMED_BUSINESS_TRUTH vocabulary this prompt needed — built ahead of any consumer, per that module’s own docstring, and never consumed by anything until now. None of this needed rebuilding.

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

  1. No unique-constraint/index discovery — needed to know whether a column is even a legal FK target (a real FK’s target is always a primary key or a unique constraint).
  2. No relationship inference at all — only a real, declared FK ever produced a relationship chunk; a missing one never did, regardless of how obvious the naming convention.
  3. No confidence/evidence vocabulary for an unconfirmed relationship.
  4. Planning never saw relationship context, confirmed or inferred — confirmed by reading generate_query_plan_from_llm’s actual signature: question + schema_context only. The prompt’s own “feed approved relationships to planning” phrasing named a real, verifiable gap, not a rhetorical one.

2. What was built

db/schema_introspection.py

TableSchemaInfo.unique_constraints — each entry one unique constraint’s column names, from Inspector.get_unique_constraints (catalog-only, same cost class as the existing PK/FK calls, skipped for views exactly like PK/FK already are).

db/relationship_inference.py (new)

retrieval/chunking.py

inferred_relationship_chunks_from_schema — one more ChunkType.RELATIONSHIP chunk per candidate, deliberately the same type a real FK’s chunk uses (both retrieved/scored/labeled the same way). What keeps rule 10 true is the chunk’s own text, not a separate type or an extra field the LLM never sees (retrieval.models .Chunk’s own docstring: extra is never rendered into the prompt): every inferred chunk opens with "CANDIDATE relationship (NOT a declared foreign key -- inferred, not confirmed; confidence 0.XX): ...". object_name is prefixed candidate: so its chunk ID structurally cannot collide with a real relationship’s.

retrieval/ingestion.py

build_schema_chunks gained settings/relationship_candidates (both keyword-only, both optional, defaulting to the exact structural- only, engine-free behavior that keeps this function testable without a connection — unchanged for every existing caller). run_ingestion computes structural candidates whenever it has a live engine (i.e. never when a caller passes tables in directly) and additionally runs the data-verification pass when Settings .enable_relationship_data_verification is on, passing the richer, already-verified candidate list into build_schema_chunks instead of letting it recompute a structural-only one and lose the refinement.

agent/llm_client.py / agent/nodes.py

_build_plan_relationship_block renders only the relationship-type entries of AgentState["retrieved_context"] (real and candidate alike, each keeping its own chunk’s framing) into generate_query_plan_from_llm’s prompt — a parameter that didn’t exist before this prompt. plan_query_node threads state["retrieved_context"] through (already populated by retrieve_business_context_node, which runs immediately before plan_query in the graph — verified from agent/graph.py’s own edge list, not assumed). Every other business-context chunk type (glossary, metric, …) is deliberately left out of the planning prompt — still generation-only, unchanged.

3. Explicitly out of scope

4. Testing

Full suite: 2519 tests pass (2479 pre-existing + 40 new), zero regressions (pytest -q, ~120-135s across runs). ruff check / black --check / mypy on every touched/new file: clean.

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-06, still pending explicit sign-off since it changes a live response shape.