Committed verbatim, per 00_MASTER_IMPLEMENTATION_CONTRACT.md’s own
instruction that every prompt in the 32-prompt series is executed against
that standing contract, not just the one that introduced it.
Act as a Principal Architect, senior engineer and enterprise product engineer responsible for safely extending an existing production-oriented AI platform.
Build the primary business-user analytics experience using real analytics APIs.
Read 00_MASTER_IMPLEMENTATION_CONTRACT.md.
Then inspect the repository before changing code.
Inspect current chat/question UI, analytics APIs, result engine, visualization specifications and frontend chart components.
Support natural-language questions and display KPI/answer, visualization, explanation, filters, time range, freshness, evidence, authorized SQL details, anomalies and related questions. Include KPI cards, trends, comparisons, rankings, distributions, anomaly panels and drill-down. Provide loading, empty, error and insufficient-evidence states. Respect field-level permissions. Link insights to evidence and semantic definitions.
Test business-user access, unauthorized fields, real API data, empty/error states and visualization rendering.
A business user can complete the full question→analysis→evidence experience from the UI.
INSPECT → PLAN → IMPLEMENT → TEST → REVIEW → FIX → DOCUMENT → REPORT
Before broad implementation, present the implementation plan. After implementation, run relevant tests and fix regressions.
Report:
Prompt 30 turned the analytics the platform already computed into a
business-user analysis view inside each answered question. No new
dashboard page was built. The question→answer flow already exists
(frontend/src/components/chat/TurnCard.tsx), and the master contract’s
“never duplicate dashboard functionality” rule makes a parallel page the
wrong shape. The analysis renders in the answered turn, under the insight,
gated exactly like the insight itself (after “Confirm and Run”, and only
while the SQL box still holds the SQL that produced the answer).
The backend already computed most of this. The frontend never received it:
AskResponse carried analytical_result, forecast_result, and
recommendations, but frontend/src/lib/types.ts declared none of them.AgentState held analytical_plan, analytical_intent, and
governing_metrics, and api/main.py::_ask_response_from_state dropped all
three. The dashboard’s filters, time range, intent, and semantic
definitions had no path to the client.ExecuteResponse.visualization_spec (Prompt 15) had no frontend type.X-Cache result-cache signal (Prompt 22) was a response header that
request() discards. The UI had no way to tell a served-from-cache result
from a live one.last_error_category == "restricted_column")
surfaced only as generic retry text.Backend (additive, pass-through only, no computation changed):
api/schemas.py: typed AnalyticalIntentOut, AnalyticalPlanOut (with
its sub-models), and GoverningMetricOut. Four new AskResponse fields,
each defaulting to “nothing to report”: analytical_intent,
analytical_plan, governing_metrics, restricted_field_notice.
ExecuteResponse.cache_status ("hit"/"miss"/null).api/main.py: _analytical_intent_out, _analytical_plan_out,
_governing_metrics_out convert the existing state dicts. The restricted
notice is a presentation-only translation of an already-computed failure
category (status == "failed" and last_error_category ==
"restricted_column"). The restricted-column check, its retry behavior, and
what it blocks are unchanged. /execute sets cache_status next to the
existing X-Cache header writes, from the same cache_status local.Frontend:
lib/types.ts: mirrors the above, plus the analytics result, forecast,
and recommendation shapes the panels read (TruthLevel,
ProvenancedClaim, GrowthStat, RankingStat, AnomalyPoint,
ForecastResult, Recommendation, and related types).lib/analyticsCharts.ts (new, DOM-free): typed-stat-to-PreparedChart
adapters, deriveKpis, sanitizeLabel, and drillDownQuestion. Every
database-originated label goes through sanitizeLabel before it is shown
or fed back into a question (master rule 8).components/analytics/ (new): AnalyticsSummary (the orchestrator) plus
KpiStrip, TrendPanel, RankingPanel (bars or share/doughnut),
ComparisonPanel, AnomalyPanel, ForecastPanel, EvidencePanel,
GoverningMetricsPanel, QueryScopePanel, TruthLevelBadge, and the
shared AnalyticsPanel shell.components/sql/KpiCard.tsx: extended in place with optional delta and
caption. Existing callers pass neither and render exactly as before.components/chat/TurnCard.tsx: renders AnalyticsSummary under the
insight, lazy-loaded, gated like the insight. Shows the restricted-field
notice in the failed-status branch.store/chatStore.ts, lib/history.ts: confirmedCacheStatus is carried
from the confirmed /execute response. It is session-only.hooks/useChartPalette.ts: theme-aware palette, following ResultChart’s
own subscription pattern.ResultChart / chartEngine’s PreparedChart renderer: every chart in
the analysis is rendered by the same component the SQL chart picker uses.
No new chart library, no second chart engine.KpiCard: extended, not forked.Expander, Badge, Markdown: existing UI primitives.chatStore.askQuestion: drill-down and related questions re-enter the
normal /ask path. No new query engine, no new route.recommendation.engine output is shown as-is, not
re-ranked or re-derived on the client.TurnCard’s existing lazy-loading boundary: the analysis joins the same
chunk boundary as ChartSection (see Known limitations for why this
matters).KpiStrip, TrendPanel, ComparisonPanel,
RankingPanel (bars for rankings, share for distributions), and
AnomalyPanel. The intent classification
(analytical_intent.intent) chooses which view leads: a distribution
question gets the share view, a comparison gets the side-by-side. Every
panel the data supports is still shown.QueryScopePanel, a read-only expander over the
validated analytical_plan (metrics, breakdown dimensions, filters, time
window, grain, ranking, row limit). It cannot change the query./execute cache signal.TruthLevelBadge). Each
recommendation’s evidence is listed with its own level. Every
recommendation is labelled an AI estimate.AnomalyPoint/AnomalySignal), with each method’s own formula and
threshold shown.next_question recommendations as one-click
buttons, which submit through askQuestion.GoverningMetricsPanel shows each approved
metric’s expression and description, labelled CONFIRMED_BUSINESS_TRUTH.PendingTurn state, plus a lazy-load fallback.failed branch, now with the field-permission
notice when the cause is a restricted column.insufficient_data_reasons). A rejected forecast shows its own rejection
reasons. A result with no breakdown says so.validate_sql_node, its retry, and the recommendation-engine suppression
from Prompt 17 all still apply).tests/test_api_ask.py::TestAnalyticsFieldPassThrough,
tests/test_api_execute.py::TestResultCache): the four new AskResponse
fields pass through with their typed shape. They default to empty for a
state that predates them. A restricted-column failure sets the notice.
An unrelated failure does not. cache_status mirrors X-Cache on a
miss, a hit, and when the cache is disabled.lib/analyticsCharts.test.ts, 15 tests): label
sanitizing (control characters, length cap), the drill-down template
against a hostile multi-line label, the adapters (with truncation and the
“Other” fold), and KPI derivation (including suppressing a total when the
ranking was row-capped, so no misleading partial sum appears).components/analytics/AnalyticsSummary.test.tsx,
16 tests, against backend-shaped fixtures in src/test/analyticsFixtures.ts):
real API-shaped data through every panel. Intent-driven panel choice. The
drill-down and related-question callbacks, and the read-only fallback when
no callback is given. Governed definitions and recommendation evidence
with their truth levels. Rejected and successful forecasts. A quiet
anomaly check. The query-scope expander, including the classifier’s
ambiguity flags. The empty, no-breakdown, cached/live, and row-capped
states.components/chat/TurnCard.analytics.test.tsx,
5 tests): the analysis is hidden until confirmation, renders with its
freshness after it, is hidden once the SQL box has been edited away from
the answer’s SQL, and the restricted-field notice shows only for that
failure.ResultChart
(the existing jsdom canvas polyfill), asserting each chart’s accessible
name (line chart, bar-horizontal chart, doughnut chart).Results: backend 3541/3541 (pytest, full suite, run twice; one
earlier run failed on a test that passes in isolation and in every pairwise
combination, and the rerun on an unchanged tree passed in full, see
Known limitations for the concurrent-edit note). Frontend 386/386
(vitest run, up from 349). tsc -b clean. oxlint reports no warnings
in any file this prompt wrote. npm run build passes. ruff check, black
--check, and mypy are clean on every backend file this prompt touched.
/ask or /execute for that
same caller. Nothing new is read, nothing is re-scoped, and the analysis is
computed from the caller’s own already-authorized result. Tenant isolation
is unchanged by construction.history.ts’s reloaded-turn
path (CLAUDE.md, chat history). Persisting analytical_result and
analytical_plan is a real follow-up.cache_status is session-only. Like every other confirmed* field,
it is not restored on reload. It is set only from the live /execute
response.visualization_spec is not consumed. Prompt 15’s richer chart spec
(ExecuteResponse.visualization_spec) remains unrendered on the frontend,
as Prompt 15 deliberately left it. The analysis panels use the typed
analytical_result stats directly. Wiring it in means extending the
persisted confirmed-result path as well, a larger change than this prompt
took on.analytics.root_cause is not wired
into any live graph node (Prompt 14’s disclosed scope), so the dashboard
does not show “why” a change happened. It shows what changed and which
periods were flagged.api/schemas.py,
api/tenant_admin*.py, security/tenancy.py, and others) were modified
outside this session. One run failed on a schema-refresh response carrying
an extra error key, which is not in the current code. A clean rerun on the
unchanged tree passed 3541/3541. Anything that edits the tree during a
run can produce a result that matches no single committed state.Prompt 31. Before it starts, reconcile the working tree: the files listed above were changed outside this prompt, and they should be reviewed and either committed or reverted deliberately, not carried into the next prompt by accident.