--- title: Semantic Layer Internals description: How KTX uses join graphs, grain, and relationship metadata to turn context into safe SQL. --- KTX is a context layer for agents. This page focuses on one internal subsystem: the semantic execution layer that turns reviewed context into safe SQL. The semantic layer is important, but it is not the whole product. KTX also handles schema evidence, wiki context, provenance, validation, and agent workflows around those files. Read the page as a pipeline: - context inputs feed the semantic engine; - evidence becomes a join graph with grain and relationship metadata; - review and corrections keep that graph current; - the execution engine uses the graph to avoid fan-out and ambiguous joins. ## Where the semantic layer fits The semantic layer is not a separate product category inside KTX. It is the engine that makes the rest of the context actionable for SQL generation.
{"Context inputs"}
semantic-layer/
{"source YAML, measures, joins, grain"}
wiki/
{"business rules, definitions, caveats"}
raw-sources/
{"schema scans, keys, imported metadata"}
provenance
{"ingest decisions and review history"}
{"Semantic layer engine"}
Join graph
{"sources as nodes, joins as typed edges"}
Grain
{"row identity before aggregation"}
Measures
{"verified formulas and filters"}
Relationships
{"many_to_one, one_to_many, one_to_one"}
{"Agent workflows"}
customers
grain: customer_id
orders
grain: order_id
order_items
grain: order_id, line_id
{"Semantic maintenance loop"}
{"Every accepted correction becomes input to the next graph build."}
{"reviewed context"}
{"The accepted graph becomes the starting point for the next build."}
{"Step 1"}
{"ingest evidence"}
{"scan schemas, imports, and accepted files"}
{"Step 2"}
{"YAML diff"}
{"draft source, join, grain, and measure changes"}
{"Step 3"}
{"validation"}
{"check relationships, syntax, and unsafe query shapes"}
{"Step 4"}
{"analyst review"}
{"accept, edit, or reject generated context"}
{"Step 5"}
{"agent use"}
{"serve context to search, explain, and query"}
{"Step 6"}
{"corrections"}
{"agent and analyst fixes become new evidence"}
{"Unsafe shape"}
{`orders
join order_items
join customers
group by customer_segment
sum(orders.amount)`}
{"The order measure is exposed to line-item fan-out before aggregation."}
{"KTX shape"}
{`orders_agg as (
select customer_id, sum(amount) revenue
from orders
group by customer_id
)
select customers.segment, sum(revenue)
from orders_agg
join customers`}
{"KTX pre-aggregates fact measures at their own grain before joining dimensions."}