ai-powered-conversational-intelligence

12 — Analytical Planning Layer

Prompt 12 of the 32-prompt Enterprise AI Analytics & Recommendation Platform initiative (00_MASTER_IMPLEMENTATION_CONTRACT.md), following 11_ANALYTICAL_INTENT_CONTRACT.md. INSPECT/PLAN/IMPLEMENT/TEST/ REVIEW/FIX/DOCUMENT — two new modules (a typed contract + a deterministic validator), one new LangGraph node sitting between classify_analytical_intent and plan_query, a small, non-duplicative plan_query_node gate addition, additive generate_sql_from_llm/ config.settings surface, and a DDL-parsing refactor that removes one real duplicate implementation. No existing route, node, or Settings field changed behavior for a question that doesn’t trigger planning, has planning disabled, or whose structured plan fails validation.

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

agent.nodes.plan_query_node (Prompt 00-era) already produces an up-front plan for a non-trivial question, but it is free text — AgentState["query_plan"]: list[str] — and the only thing that ever checks it against the generated SQL is a second LLM call (review_sql_against_plan_from_llm). Nothing in the codebase before this prompt deterministically verified that a plan’s own metric/dimension/ filter/time references were real, authorized, joinable, or supported by the target database — the exact gap agent.sql_validator.validate_sql already closed for generated SQL itself (an AST-based allowlist, not a second LLM judgment) was open one layer earlier, for the plan. agent.intent.AnalyticalIntentClassification (Prompt 11) gets closer — it extracts metric_candidates/dimensions/time_requirement/ comparison/filters from the question’s own text — but it’s a classification, not a resolvable-to-real-schema structure, and agent.nodes.plan_query_node’s only consumer of it is a gate/prompt hint, never a validated object.

db/relationship_inference.py (Prompt 07) already had the exact type-family classification (_TYPE_FAMILY_KEYWORDS/_type_family) this prompt’s time-feasibility check needed, and db/adapter.py (Prompt 03/04) already had a DatabaseCapabilities object for connection-level capability facts — but no per-dialect-SQL-feature support table existed anywhere (window functions/CTEs/PERCENTILE_CONT/STRING_AGG support vary by DB_TYPE, and nothing tracked that). agent/nodes.py’s own _extract_column_names (a DDL-text column-name extractor) was a private, single-use helper with no public counterpart agent.plan_validator could reuse without duplicating it.

agent.authz.has_role_permission/config.sensitive_columns .load_sensitive_columns (2026 Phase 2 security review) already gate a restricted column at the generated-SQL layer (validate_sql_node) — confirmed reusable verbatim as an earlier, additional layer for a plan’s column references, never a replacement for that existing gate.

2. What was built

Typed contract — agent/analytical_plan.py (new)

Mirrors agent/intent.py’s “typed contract module, separate from the LLM-calling module and the validation module” split:

No LLM call lives here — agent.llm_client.generate_analytical_plan_from_llm makes the call, agent.plan_validator.validate_plan re-verifies it, agent.nodes.build_analytical_plan_node wires both into the graph.

Deterministic validator — agent/plan_validator.py (new)

validate_plan(plan, schema_tables, governing_metrics, caller_roles, db_type) -> PlanValidationResult — a pure function, no I/O, every check runs (violations are never short-circuited at the first one found):

  1. Metric/dimension existence — every (table, column) pair the plan names (metrics unless governed_metric_key‘d, dimensions, filters, time range) must resolve to a real table/column in schema_tables (the already-retrieved top-k tables for this question — the identical boundary generate_sql’s own prompt is already scoped to). A governed_metric_key must match a name in governing_metrics; a metric with neither a table/column nor a valid governed_metric_key is incomplete_metric. A plan with zero metrics is no_metrics.
  2. Relationship paths — when the plan touches more than one table, a BFS over an adjacency graph built from FOREIGN KEY (...) REFERENCES ... edges parsed out of the already-retrieved schema_tables’ own DDL text (new db.schema_introspection .extract_ddl_foreign_key_targets) must connect every pair, or it’s no_relationship_path. Disclosed, bounded scope: only FK edges visible in DDL already shown to the LLM are considered — never a full cross-schema graph search, and never an inferred (non-FK) candidate relationship (db/relationship_inference.py’s own, separate, lower-confidence kind of claim).
  3. Filter/sensitive-field policy — any referenced column classified "restricted" (config.sensitive_columns.load_sensitive_columns) without the caller holding Permission.VIEW_RESTRICTED_COLUMNS (agent.authz.has_role_permission) is restricted_column — the identical gate validate_sql_node already applies to generated SQL, reused (not duplicated) as an earlier, additional layer.
  4. Time feasibility — a time_range column (once confirmed to exist) must classify as the "datetime" type family (db .relationship_inference.type_family, promoted from private to public specifically for this reuse) or it’s infeasible_time_range.
  5. Database capabilities — each tag in required_operations that appears in a small, honestly-scoped _OPERATION_SUPPORT table (six tags: window_function/lag_lead/cte/percentile_cont/ string_agg/rollup) is checked against db_type; a known incompatibility (e.g. percentile_cont + mysql) is unsupported_operation. An unrecognized tag is never blocked — fail open on ignorance, fail closed only on a documented incompatibility.

PlanValidationResult(is_valid, violations: tuple[PlanViolation, ...]) — is_valid=False the moment any violation exists; the full list is always returned for logging/debugging.

Refactor — DDL-parsing helpers promoted to db/schema_introspection.py

agent.nodes._extract_column_names (DDL text → column names) moved to db.schema_introspection.extract_ddl_column_names (public); agent/ nodes.py now imports it under the same private name (_extract_column_names = extract_ddl_column_names) so _suggest_correct_column’s own call sites and docstring references are unaffected — one implementation, two callers (_suggest_correct_column and agent.plan_validator), not a duplicate. New sibling extract_ddl_foreign_key_targets (DDL text → referred table names) has no prior single-caller implementation to deduplicate; it’s genuinely new, narrowly scoped to table-level path existence (not column-level join correctness, which agent.sql_validator already checks on the real generated SQL). db.relationship_inference._type_family was renamed to the public type_family (same body, same internal call sites updated) for the identical “promote an existing private helper for cross-module reuse” reason.

LLM call + parsing — agent/llm_client.py (extended)

_ANALYTICAL_PLAN_SYSTEM_PROMPT (the standard DATA-framing/security boilerplate every system prompt in this file repeats, plus the full AnalyticalPlan JSON shape spelled out field-by-field, with an explicit “every table/column name MUST be copied exactly from the Schema section” rule — the single most important instruction for a plan that’s about to be deterministically checked against that exact schema). _build_analytical_plan_user_prompt reuses (never re-implements) _build_plan_relationship_block/_build_plan_business_concept_block (already built for generate_query_plan_from_llm) plus _build_intent_governing_metrics_block (already built for Prompt 11’s classification call) for the metric-name grounding. _parse_analytical_plan_response mirrors _parse_intent_response’s exact fail-open shape: JSON parse → AnalyticalPlan.model_validate → None on any ValidationError/ JSONDecodeError. generate_analytical_plan_from_llm clones generate_query_plan_from_llm’s call shape (temperature=0.0, new Settings.analytical_plan_max_tokens, OllamaUnavailableError re-raise, _log_ollama_timing).

_build_analytical_plan_block(plan: dict) -> str renders a validated plan via agent.analytical_plan.render_plan_as_steps (reused, not re-rendered a second way) with framing that states plainly the plan has already been deterministically re-checked against the live schema, sensitive-column policy, and the target database — distinct from, and more authoritative-sounding than, the ordinary advisory _build_plan_block. generate_sql_from_llm/_build_user_prompt both gained an analytical_plan: dict | None = None parameter; _build_user_prompt shows either the generic query_plan block or the stronger analytical_plan block (never both — avoiding showing the model the identical steps twice under two different framings), while query_plan remains set on state regardless so review_sql_node keeps working.

Node + graph wiring

New build_analytical_plan_node (agent/nodes.py), wired classify_analytical_intent → build_analytical_plan → plan_query (two straight edges, no conditional routing, no new END branch — structurally proven, see §3). Gated by a new shared _planning_gate helper (Settings.enable_query_planning AND (complexity_signals OR intent_triggers_planning) — the exact same condition plan_query_node already used, now factored out so the two nodes can never silently drift) plus Settings.enable_analytical_planning.

On success (LLM call succeeds, response parses, validate_plan finds zero violations): stores state["analytical_plan"] (the validated dict) and renders it into state["query_plan"] via render_plan_as_steps. On any failure mode — gate didn’t fire, feature flag off, Ollama unreachable, unparseable response, or (the one genuinely new behavior) the plan failed deterministic validation — state["analytical_plan"] stays None and state["analytical_plan_violations"] records why (when applicable), never a terminal status.

plan_query_node gained exactly one new early-return: when state["analytical_plan"] is already a validated plan, it returns {"status": "generating"} and does nothing else — a pure pass-through that leaves the already-set query_plan alone, avoiding a second, redundant free-text planning LLM call for the same question. Every other line of plan_query_node’s own logic is byte-identical to its pre-Prompt-12 shape.

Config

Settings.enable_analytical_planning: bool = True, Settings.analytical_plan_max_tokens: int = Field(default=400, gt=0) — same on-by-default-but-escapable posture and naming convention as enable_query_planning/query_plan_max_tokens immediately next to them.

3. Proof: this preserves the direct Text-to-SQL fallback by construction

4. Explicitly out of scope

5. Testing

5 test files touched (3 new, 2 extended), 61 new tests:

Full suite: 2938 tests pass, zero regressions (pytest -q, re-run after every change in this prompt — up from the 2877 Prompt 11 reported). ruff check/black --check/mypy/bandit clean on every new/touched file.

6. Security / tenant-isolation / performance review

7. 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 — the same carried-forward recommendation Prompts 04-11 have each named, still pending explicit sign-off since it changes a live response shape. Alternatively, a dedicated docs/ARCHITECTURE.md refresh pass (closing the gap §4/§7 above disclose, now two prompts deep) before it drifts further, or a live-LLM run of scripts/run_benchmark.py to empirically measure how often a real local model’s structured plans actually clear validate_plan (this prompt’s own natural empirical follow-up, mirroring Prompt 11’s identical one for intent classification).