Skip to content
MSO CloudDocumentation

ADR 0026 — Metric semantic layer + KPI auto-classification (chat-with-data knows what a KPI is)

On this page
  • Status: Accepted (2026-07-31) — after evidence review of the live KPI/budget model and the chat metric surface. Grounded in a real production incident (below).
  • Date: 2026-07-31
  • Context owner: Platform / Marketing
  • Related: ADR 0021 (plan↔measurement bindings — the seam this ADR generalizes), ADR 0008 (marketing pillar), ADR 0017 (data-kind-as-release), ADR 0019 (metric catalog / widget registry). CLAUDE.md §32 §35 §36 §49 §58 §62 §65.

The incident (why this ADR exists)#

An operator (YouNet Media) asked the Data Assistant "Google Ads đang đúng pacing không?". The assistant answered "Ngân sách 685 triệu, đã chi ~21,98 triệu (3,2%)" — wrong: 685M is the org's WHOLE marketing budget rollup, not Google Ads. The real per-channel truth in the same DB: Google Ads line = 180M target / 108.6M actual (~60%, roughly on pace at ~58% of year elapsed); Facebook line = 160M target / 303M actual = 189% — badly over budget. The assistant could not tell these apart because a KPI/budget row carries no machine-readable meaning: "Google Ads" lives only as a raw AD_PLATFORMS enum value on one spend-table column, budget lines from the source-of-truth sheet (business_kpi_facts) have no platform field at all (Google vs Facebook is encoded only inside the metric_key string budget_middle_google/_fb), and the metric catalog has no synonyms/category the AI can reason over. The AI was, in the operator's words, "quá ngu" — not because the data was missing, but because nothing mapped the data to meaning.

Decision#

A semantic layer: one machine-readable place where every metric and every load-bearing dimension VALUE carries its meaning (label + synonyms + category + how to compare it), read by BOTH the dashboard (auto progress/compare) AND chat-with-data (describe/answer). KPI/budget lines are AUTO-CLASSIFIED into that layer by deterministic rules, landing as suggested bindings the admin confirms (ADR 0021 posture — never auto-trusted). This is the industrial pattern (dbt semantic layer / Cube / LookML metrics store): meaning lives with the metric, not scattered across prose in a system prompt.

D1 — The metric catalog carries queryable semantics (not just a label)

MetricDefinition (packages/metrics/src/types.ts) gains, additive + optional:

  • synonyms?: { vi: string[]; en: string[] } — natural-language AND platform-vocabulary terms a user or the AI might say ("chi phí quảng cáo Google", "Google Ads cost", "GDN spend").
  • category?: MetricCategory — a compiled grouping slug from a NEW METRIC_CATEGORIES registry (advertising, commerce_revenue, lead_gen, web_traffic, …), each with { label(vi/en), description(vi/en) }. category is the semantic "what family is this" the AI groups by; it is NOT pillar (data plane) or ownedBy (team) — those stay for routing/governance.
  • comparison?: { kind: 'target_vs_actual' | 'period_over_period' | 'none'; direction: 'higher_better' | 'lower_better' | 'on_pace' } — how the number is judged, so the dashboard progress bar and the AI both know "over budget is bad" vs "over target is good."

Existing metrics keep working (all new fields optional); the marketing + commerce metrics are backfilled. §49 stays: computation is unchanged, only the metric's SELF-DESCRIPTION grows.

D2 — ONE dimension-value semantics registry (the "Google Ads" name lives here)

A compiled DIMENSION_VALUE_SEMANTICS registry keyed by (dimension, value) for the load-bearing enums — starting with platform over AD_PLATFORMS — each carrying { label: {vi,en}, synonyms: {vi,en} }. google-ads → label "Google Ads", synonyms ["quảng cáo Google","GDN","Google"]. This is the ONE place a raw slug gets a human name + the phrases that resolve to it; formatPlatform (§36) and the chat both read it, so the display, the dashboard and the AI can never disagree (§49). A resolveDimensionValue(dimension, phrase) helper turns "Google Ads" (any synonym) → google-ads.

D3 — Deterministic KPI-line classifier → suggested plan_line_bindings (closes the sheet gap)

A pure, LLM-free, per-client-agnostic classifyKpiLine(line, semantics) reads a budget/KPI line's metric_key / budget_line_key / budget_line_label / existing ad_platform and emits a SUGGESTED binding: { metric_slug, dimension_filter (e.g. {platform:'google-ads'}), category, funnel_stage?, confidence_bp, rationale, status }. Rules evaluate over the D1/D2 semantics (synonym match on the label/key), never on a hardcoded client string:

  • a platform synonym in the label/key → dimension_filter.platform = <slug>, metric mkt_ad_spend;
  • funnel words ("upper/middle/lower funnel", "lead gen", "user acquisition") → funnel_stage;
  • a line with no measurable metric (PR, "tools & others") → status: 'unmeasurable' with a stated reason (honest — §32: not every budget line has an actual to pace against). Wired into the sheet→kpi_targets promoter and a suggestion pass so EVERY budget line (including the business_kpi_facts-derived ones that today land platform-less) gets a suggestion. Admin confirms in the existing plan-bindings review UI (ADR 0021 — confirmed before load-bearing). The classifier is replayable (§65): same line + same semantics → same suggestion + rationale.

D4 — Chat and dashboard read the semantic layer

  • listMetrics chat tool returns synonyms + category + comparison per metric.
  • A resolveKpi chat tool: given a natural-language phrase ("Google Ads budget", "ngân sách Facebook"), resolve via D1 synonyms + D2 resolveDimensionValue → the metric + dimension filter
    • the bound budget line, and return its pacing (target/actual/%/on-pace verdict) with the semantic label. This replaces "the model guesses from a hand-written example list."
  • The chat system prompt gains the platform synonym table + the org's confirmed+suggested bindings so the model reasons over meaning, not slugs. channelBudgetPacing accepts any synonym.
  • Dashboard: a per-channel budget-vs-actual progress surface reads the bound lines (computePlanBindingPacing, extended to every confirmed line), auto-showing YNM's Google (60%, on pace) and Facebook (189%, over) with the D3 comparison.direction colouring.

D5 — Governance (the class-of-bug guard)

  • scripts/audit/metric-semantics-coverage.py (in audit:all): every chat-exposed first-party metric has a non-empty description; every marketing/commerce metric has a category; every AD_PLATFORMS value has a DIMENSION_VALUE_SEMANTICS entry (no un-named platform can reach the UI or chat, §36 generalized).
  • A reconciliation assertion (§53 posture) that classifyKpiLine is deterministic and that a confirmed binding's pacing equals the raw ad_spend_daily sum for its dimension_filter (headline == source, §51 for budgets).

Alternatives considered#

  • "Just make the chat prompt smarter." Rejected — prose in the system prompt is exactly what fails now (the model guessed from an example list). Meaning must be structured DATA the dashboard shares, or the number on the chart and the number in chat drift (§49).
  • "Add a platform column to business_kpi_facts." Rejected as the primary fix — the sheet is the client's own vocabulary and is read-only/sheet-fed; forcing a platform column onto it fights the source. The classifier + plan_line_bindings (ADR 0021) is the sanctioned seam and already has a confirm gate; generalizing it is §49-correct.
  • "Auto-confirm the obvious classifications." Rejected — ADR 0021's posture is that a measurement binding is load-bearing only after a human confirms; a budget mis-mapping silently mis-states a client's pacing (the exact incident). Suggest, don't auto-trust; the admin's one click is cheap, a wrong auto-binding is expensive.
  • "Use an LLM to classify each line." Rejected for the classification itself (must be deterministic + replayable + free, §65/§60d) — the LLM's role is upstream (proposing synonyms/labels for a human to confirm, the ADR 0025-style proposer), never the load-bearing mapping.

Consequences#

  • A new metric, KPI line, or ad platform ships with its meaning attached — chat-with-data and the dashboard understand it with zero prompt edits. Onboarding a client's KPI sheet auto-suggests the channel mapping; the admin confirms once.
  • The AI answers per-channel budget questions correctly (the incident is fixed) and can DESCRIBE any metric from the catalog instead of latent knowledge.
  • Cost: every chat-exposed metric now owes a description + category, and every load-bearing dimension enum owes value semantics — enforced by D5, the point of the ADR.

Implementation status (landed 2026-07-31)#

Decision Status Evidence (as shipped)
D1 metric semantics Landed synonyms/category/comparison on MetricDefinition (packages/metrics/src/types.ts); METRIC_CATEGORIES (metric-categories.ts, 10 slugs); 85 catalog metrics backfilled; listMetrics returns them
D2 dimension-value semantics Landed DIMENSION_VALUE_SEMANTICS + resolveDimensionValue (dimension-value-semantics.ts), labels from PLATFORM_LABELS (§49); every AD_PLATFORMS value covered (D5 audit enforces)
D3 KPI classifier Landed pure classifyKpiLine (kpi-line-classify.ts) + suggestBindingsForOrg (kpi-line-suggest.ts); promoteBudgetLinesToPlanBindings populates suggested plan_line_bindings; replayable; proven on real YNM lines
D4 chat + dashboard Landed resolveKpi chat tool + synonym-aware channelBudgetPacing (chat/tools/marketing.ts); ChannelBudgetVsActual per-channel budget-vs-actual surface (kpi-view.tsx) reading confirmed bindings via pacingTone
D5 governance Landed metric-semantics-coverage.py in audit:all; data-reconciliation assertion binding-pacing == raw ad_spend_daily sum

Acceptance proof (YNM, 2026-07-31). classifyKpiLine on YNM's real kpi_targets (read from prod, read-only) auto-detected budget_middle_google → google-ads and budget_middle_fb → meta-ads (conf 9500), the four non-channel lines honestly unmeasurable. computePlanBindingPacing on a YNM-shape gate-DB seed reproduced the incident numbers CORRECTLY: Google 180M target / 108.6M actual = 60.3% → pacingTone=neutral (on pace, ~58% elapsed); Facebook 160M / 303M = 189.4% → pacingTone=danger (over budget) — each channel distinct, the 685M rollup never presented as one channel. The old assistant answer ("685 triệu, 3,2%") is now unreachable.