ostrict 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. ostrict-0.3.0/LICENSE +202 -0
  2. ostrict-0.3.0/NOTICE +5 -0
  3. ostrict-0.3.0/PKG-INFO +240 -0
  4. ostrict-0.3.0/README.md +202 -0
  5. ostrict-0.3.0/pyproject.toml +60 -0
  6. ostrict-0.3.0/setup.cfg +4 -0
  7. ostrict-0.3.0/src/ostrict/__init__.py +22 -0
  8. ostrict-0.3.0/src/ostrict/cli.py +155 -0
  9. ostrict-0.3.0/src/ostrict/compiler/__init__.py +123 -0
  10. ostrict-0.3.0/src/ostrict/compiler/guardrails.py +153 -0
  11. ostrict-0.3.0/src/ostrict/compiler/resolver.py +90 -0
  12. ostrict-0.3.0/src/ostrict/compiler/sql_builder.py +436 -0
  13. ostrict-0.3.0/src/ostrict/connectors/__init__.py +29 -0
  14. ostrict-0.3.0/src/ostrict/connectors/base.py +16 -0
  15. ostrict-0.3.0/src/ostrict/connectors/bigquery.py +53 -0
  16. ostrict-0.3.0/src/ostrict/connectors/duckdb.py +25 -0
  17. ostrict-0.3.0/src/ostrict/eval/__init__.py +0 -0
  18. ostrict-0.3.0/src/ostrict/eval/runner.py +120 -0
  19. ostrict-0.3.0/src/ostrict/interchange/__init__.py +10 -0
  20. ostrict-0.3.0/src/ostrict/interchange/osi.py +487 -0
  21. ostrict-0.3.0/src/ostrict/linter/__init__.py +3 -0
  22. ostrict-0.3.0/src/ostrict/linter/engine.py +85 -0
  23. ostrict-0.3.0/src/ostrict/linter/rules_governance.py +92 -0
  24. ostrict-0.3.0/src/ostrict/linter/rules_logic.py +281 -0
  25. ostrict-0.3.0/src/ostrict/linter/rules_structure.py +162 -0
  26. ostrict-0.3.0/src/ostrict/mcp_server/__init__.py +0 -0
  27. ostrict-0.3.0/src/ostrict/mcp_server/server.py +303 -0
  28. ostrict-0.3.0/src/ostrict/models/__init__.py +0 -0
  29. ostrict-0.3.0/src/ostrict/models/ast.py +202 -0
  30. ostrict-0.3.0/src/ostrict/models/errors.py +75 -0
  31. ostrict-0.3.0/src/ostrict/models/query.py +63 -0
  32. ostrict-0.3.0/src/ostrict/parsers/__init__.py +34 -0
  33. ostrict-0.3.0/src/ostrict/parsers/common.py +196 -0
  34. ostrict-0.3.0/src/ostrict/parsers/snowflake_ddl.py +232 -0
  35. ostrict-0.3.0/src/ostrict/parsers/yaml_spec.py +108 -0
  36. ostrict-0.3.0/src/ostrict/plugins/__init__.py +0 -0
  37. ostrict-0.3.0/src/ostrict/plugins/base.py +29 -0
  38. ostrict-0.3.0/src/ostrict.egg-info/PKG-INFO +240 -0
  39. ostrict-0.3.0/src/ostrict.egg-info/SOURCES.txt +51 -0
  40. ostrict-0.3.0/src/ostrict.egg-info/dependency_links.txt +1 -0
  41. ostrict-0.3.0/src/ostrict.egg-info/entry_points.txt +5 -0
  42. ostrict-0.3.0/src/ostrict.egg-info/requires.txt +18 -0
  43. ostrict-0.3.0/src/ostrict.egg-info/top_level.txt +2 -0
  44. ostrict-0.3.0/src/strict_semantic/__init__.py +54 -0
  45. ostrict-0.3.0/tests/test_compiler_determinism.py +63 -0
  46. ostrict-0.3.0/tests/test_duckdb_execution.py +180 -0
  47. ostrict-0.3.0/tests/test_golden.py +260 -0
  48. ostrict-0.3.0/tests/test_interchange_osi.py +111 -0
  49. ostrict-0.3.0/tests/test_linter_rules.py +287 -0
  50. ostrict-0.3.0/tests/test_mcp_server.py +94 -0
  51. ostrict-0.3.0/tests/test_parsers.py +115 -0
  52. ostrict-0.3.0/tests/test_v0_1_1.py +108 -0
  53. ostrict-0.3.0/tests/test_v0_3_0_rebrand.py +61 -0
ostrict-0.3.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Son Chau
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
ostrict-0.3.0/NOTICE ADDED
@@ -0,0 +1,5 @@
1
+ ostrict
2
+ Copyright 2026 Son Chau
3
+
4
+ This product includes software developed by Son Chau
5
+ (https://github.com/noscn/ostrict).
ostrict-0.3.0/PKG-INFO ADDED
@@ -0,0 +1,240 @@
1
+ Metadata-Version: 2.4
2
+ Name: ostrict
3
+ Version: 0.3.0
4
+ Summary: Strict semantic-view linter and deterministic, FinOps-safe SQL compiler with an MCP server for AI data agents.
5
+ Author-email: Son Chau <46123450+noscn@users.noreply.github.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Repository, https://github.com/noscn/ostrict
8
+ Project-URL: Issues, https://github.com/noscn/ostrict/issues
9
+ Project-URL: Changelog, https://github.com/noscn/ostrict/blob/main/docs/REVISIONS.md
10
+ Keywords: semantic-layer,mcp,text-to-sql,sqlglot,bigquery,data-governance,finops
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Database
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ License-File: NOTICE
23
+ Requires-Dist: sqlglot>=26
24
+ Requires-Dist: pydantic>=2.5
25
+ Requires-Dist: pyyaml>=6
26
+ Provides-Extra: mcp
27
+ Requires-Dist: mcp>=2; extra == "mcp"
28
+ Provides-Extra: bigquery
29
+ Requires-Dist: google-cloud-bigquery>=3; extra == "bigquery"
30
+ Provides-Extra: duckdb
31
+ Requires-Dist: duckdb>=1; extra == "duckdb"
32
+ Provides-Extra: dev
33
+ Requires-Dist: pytest>=8; extra == "dev"
34
+ Requires-Dist: duckdb>=1; extra == "dev"
35
+ Requires-Dist: ruff>=0.6; extra == "dev"
36
+ Requires-Dist: mcp>=2; extra == "dev"
37
+ Dynamic: license-file
38
+
39
+ # ostrict
40
+
41
+ [![CI](https://github.com/noscn/ostrict/actions/workflows/ci.yml/badge.svg)](https://github.com/noscn/ostrict/actions/workflows/ci.yml)
42
+
43
+ **A strict CI linter for semantic views, and a deterministic, FinOps-safe
44
+ semantic compiler + MCP server for AI data agents.**
45
+
46
+ *Ostrict* = **O**SI + **strict**. Unlike the myth, Ostrict never buries its
47
+ head in the sand when AI hallucinates SQL.
48
+
49
+ Most data warehouses have no native, strictly-enforced semantic views. When
50
+ text-to-SQL agents query them through loose YAML or flexible graph layers,
51
+ the failure modes are always the same: non-deterministic SQL, fan-out double
52
+ counting, ambiguous join paths, raw-row dumps, and runaway scan costs.
53
+
54
+ This project makes the semantic model a **blueprint, never a materialization**:
55
+
56
+ ```
57
+ .sql DDL / .yaml spec ──parse──▶ canonical AST (Pydantic v2)
58
+ ──lint (SV-R1..R10)──▶ valid blueprint
59
+ ──compile(request)──▶ one deterministic SQL statement ──connector──▶ rows
60
+ ```
61
+
62
+ - **Dual input, one AST.** `CREATE SEMANTIC VIEW` DDL (human-authored) and a
63
+ native YAML contract (automation-friendly) build byte-identical models.
64
+ - **Static ground rules (SV-R1..R10).** Primary keys and grain comments, a
65
+ directed many-to-one join DAG with exactly one path between any two tables,
66
+ no numeric dimensions, no `CURRENT_DATE`, snapshot metrics declared
67
+ `NON ADDITIVE BY`, globally unique field names and synonyms, a security
68
+ deny-list, comment templates. `ostrict lint` blocks CI on ERRORs.
69
+ - **Deterministic compilation.** Same request, same SQL, byte for byte. Joins
70
+ only walk child→parent (zero fan-out, enforced); metrics of different grains
71
+ drill across via `UNION ALL`; derived ratios divide NULL-safely; every
72
+ expression is built as a sqlglot AST — no string concatenation anywhere.
73
+ - **FinOps guardrails.** At least one metric per query (no raw dumps),
74
+ partition filters required and pushed into each table's subquery (the only
75
+ placement BigQuery honors on joined tables), `LIMIT` injected and capped,
76
+ BigQuery dry-run byte ceiling.
77
+ - **MCP server (mcp ≥ 2).** Five tools: `list_semantic_views`,
78
+ `inspect_semantic_view`, `get_dimension_values`, `compile_semantic_query`,
79
+ `execute_semantic_query`. The agent picks names from the catalog and never
80
+ writes SQL; every refusal carries an error code and a repair hint.
81
+ - **Golden Questions eval.** `ostrict eval cases.yaml` grades
82
+ answer/refuse/clarify cases against DuckDB deterministically — no LLM judge.
83
+ - **OSI interchange.** `ostrict export|import` converts views to and
84
+ from Open Semantic Interchange YAML/JSON losslessly — guardrails travel in a
85
+ namespaced `custom_extensions.strict_semantic` block, and every import must
86
+ pass the full SV-R1..R10 gate.
87
+ - **Open-core plugin hooks.** `on_model_loaded`, `before_compile` (RBAC filter
88
+ injection), `after_compile` (cache/lineage), `on_query_executed` (audit).
89
+
90
+ ## Quickstart
91
+
92
+ ```bash
93
+ python -m venv .venv && .venv/bin/pip install -e '.[dev,mcp]'
94
+ .venv/bin/python -m pytest # 80 tests
95
+ .venv/bin/ostrict lint tests/fixtures/ecommerce.sql
96
+ .venv/bin/ostrict eval tests/fixtures/golden_questions.yaml
97
+ .venv/bin/ostrict compile tests/fixtures/ecommerce.yaml \
98
+ -m revenue -m aov -d region \
99
+ -f '{"field":"order_date","op":">=","value":"2026-01-01"}' \
100
+ -f '{"field":"order_date","op":"<","value":"2026-02-01"}' --dialect bigquery
101
+ ```
102
+
103
+ Run the MCP server against the demo warehouse (in-memory DuckDB):
104
+
105
+ ```bash
106
+ OSTRICT_CONFIG=$PWD/examples/config.duckdb.yml .venv/bin/ostrict-mcp
107
+ ```
108
+
109
+ or register it with Claude Code:
110
+
111
+ ```bash
112
+ claude mcp add ostrict -e OSTRICT_CONFIG=$PWD/examples/config.duckdb.yml -- $PWD/.venv/bin/ostrict-mcp
113
+ ```
114
+
115
+ For BigQuery, copy `examples/config.bigquery.example.yml`, point `views:` at
116
+ your private spec folder, and set `max_bytes`.
117
+
118
+ ## Writing a semantic view
119
+
120
+ The `CREATE SEMANTIC VIEW` DDL (`.sql`) and the native YAML (`.yml`) are
121
+ equivalent; metadata the DDL has no slot for travels in structured comment
122
+ directives (enforced by the linter as SV-R8.1 templates):
123
+
124
+ ```sql
125
+ CREATE SEMANTIC VIEW ecommerce
126
+ TABLES (
127
+ orders AS shop.orders PRIMARY KEY (order_id)
128
+ COMMENT = 'One row per order. Primary date: order_date. Partition: order_date BY order_date. Source: shop.orders.',
129
+ customers AS shop.customers PRIMARY KEY (customer_id)
130
+ COMMENT = 'One row per customer. Primary date: signup_date. Source: shop.customers.'
131
+ )
132
+ RELATIONSHIPS ( orders_customers AS orders (customer_id) REFERENCES customers (customer_id) )
133
+ DIMENSIONS (
134
+ customers.region AS region WITH SYNONYMS ('market') COMMENT = 'Customer home region. Values: EU, US, APAC.',
135
+ orders.order_date AS order_date COMMENT = 'Date the order was placed. Grains: day, week, month.'
136
+ )
137
+ METRICS (
138
+ orders.order_count AS COUNT(order_id) COMMENT = 'Number of orders placed. Population: all orders. Additivity: additive.'
139
+ )
140
+ COMMENT = 'Retail sales.'
141
+ ```
142
+
143
+ Directives: `One row per …` (grain), `Primary date: col.`,
144
+ `Point-in-time snapshot.`, `Partition: col BY dimension (lookback Nd, lookahead Nd).`,
145
+ `Grains: day, month.`, `Type: timestamp.`, `Values: a, b, c.`
146
+ See `tests/fixtures/ecommerce.{sql,yaml}` for the full twin fixtures.
147
+
148
+ ## Querying
149
+
150
+ ```json
151
+ {"metrics": ["revenue", "aov"], "dimensions": ["region", "order_date__month"],
152
+ "filters": [{"field": "order_date", "op": "between", "value": ["2026-01-01", "2026-03-31"]},
153
+ {"field": "aov", "op": ">", "value": 50}],
154
+ "order_by": ["-revenue"], "limit": 100}
155
+ ```
156
+
157
+ Time dimensions take a `__grain` suffix. Filters on dimensions apply before
158
+ aggregation, filters on metrics after. Dimension-only queries are refused
159
+ (`ERR_METRIC_REQUIRED`) — value discovery goes through `get_dimension_values`,
160
+ a bounded `SELECT DISTINCT … LIMIT ≤ 50`.
161
+
162
+ ## OSI interchange
163
+
164
+ Exchange views with other semantic-layer tools as OSI (Open Semantic
165
+ Interchange) documents:
166
+
167
+ ```bash
168
+ ostrict export tests/fixtures/ecommerce.sql -o ecommerce.osi.yaml
169
+ ostrict export tests/fixtures/ecommerce.yaml --format json --strip-extensions
170
+ ostrict import ecommerce.osi.yaml -o views/imported.yaml
171
+ ```
172
+
173
+ Exports are lossless: strict-semantic guardrails (dialect, partition pruning
174
+ bounds, snapshot flags, time types) ride in `custom_extensions.strict_semantic`
175
+ blocks, and an export → import round-trip rebuilds an equal AST.
176
+ `--strip-extensions` emits vanilla OSI instead and warns on stderr for every
177
+ guardrail that drops out. Imports are gated: the document is linted
178
+ (SV-R1..R10) before it is written as a native spec, so external models cannot
179
+ bypass the ground rules (`--no-lint` converts anyway, for inspection). The
180
+ field-by-field mapping lives in [docs/osi-mapping.md](docs/osi-mapping.md).
181
+
182
+ ## Layout
183
+
184
+ ```
185
+ src/ostrict/
186
+ ├── models/ # canonical AST, error codes, request/response contracts
187
+ ├── parsers/ # CREATE SEMANTIC VIEW DDL (token-stream) + native YAML -> one builder
188
+ ├── interchange/ # OSI (Open Semantic Interchange) export/import adapter
189
+ ├── linter/ # SV-R1..R10 engine: structure / logic / governance rules
190
+ ├── compiler/ # join-graph resolver, FinOps guardrails, sqlglot SQL builder
191
+ ├── connectors/ # DuckDB, BigQuery (dry-run + byte cap), protocol
192
+ ├── plugins/ # open-core lifecycle hooks
193
+ ├── eval/ # Golden Questions runner (deterministic, no LLM)
194
+ └── mcp_server/ # the five MCP tools over stdio
195
+ ```
196
+
197
+ Design departures from the original blueprint, with reasons, are in
198
+ [docs/REVISIONS.md](docs/REVISIONS.md).
199
+
200
+ ## Security & deployment responsibility
201
+
202
+ The server speaks **stdio** and is designed to run next to the agent, on your
203
+ machine, with your credentials. If you expose it over a network (HTTP/SSE,
204
+ a gateway, a shared host), authentication, transport security and tenant
205
+ isolation are yours to provide — nothing here does that for you.
206
+
207
+ Expect defense in depth from the warehouse side too: run the connector with a
208
+ **least-privilege, read-only** account scoped to the datasets the views name.
209
+ The compiler never concatenates SQL strings — filter values become typed
210
+ sqlglot literals, operators come from a fixed whitelist, and every identifier
211
+ must resolve against the linted model — but the SQL that reaches your warehouse
212
+ should still be gated by the warehouse's own permissions. Per the Apache 2.0
213
+ license, the software is provided as is, without warranty; securing the
214
+ deployment is the deployer's responsibility.
215
+
216
+ Report vulnerabilities privately via GitHub Security Advisories, not public
217
+ issues.
218
+
219
+ ## Contributing
220
+
221
+ Scope is deliberately strict and minimal — read
222
+ [CONTRIBUTING.md](CONTRIBUTING.md) before proposing features. Short version:
223
+ determinism is non-negotiable, escape hatches get declined, and extensions
224
+ belong in plugins.
225
+
226
+ ## Renamed from strict-semantic-mcp
227
+
228
+ v0.3.0 renamed the project to **ostrict**. Old integrations keep working:
229
+ `import strict_semantic` is a deprecated alias of `ostrict` (same module
230
+ objects), the `strict-semantic` / `strict-semantic-mcp` entry points remain,
231
+ and `STRICT_SEMANTIC_CONFIG` is honored when `OSTRICT_CONFIG` is unset. The
232
+ OSI extension namespace stays `custom_extensions.strict_semantic`, so 0.2.x
233
+ documents import unchanged. On PyPI, new releases ship as `ostrict`;
234
+ `strict-semantic-mcp` stays at 0.2.0.
235
+
236
+ ## License
237
+
238
+ Apache 2.0 (see [LICENSE](LICENSE) and [NOTICE](NOTICE)). Fixtures and examples
239
+ are a generic e-commerce schema; keep your company's semantic views in a
240
+ private repository and point the server at them via `OSTRICT_CONFIG`.
@@ -0,0 +1,202 @@
1
+ # ostrict
2
+
3
+ [![CI](https://github.com/noscn/ostrict/actions/workflows/ci.yml/badge.svg)](https://github.com/noscn/ostrict/actions/workflows/ci.yml)
4
+
5
+ **A strict CI linter for semantic views, and a deterministic, FinOps-safe
6
+ semantic compiler + MCP server for AI data agents.**
7
+
8
+ *Ostrict* = **O**SI + **strict**. Unlike the myth, Ostrict never buries its
9
+ head in the sand when AI hallucinates SQL.
10
+
11
+ Most data warehouses have no native, strictly-enforced semantic views. When
12
+ text-to-SQL agents query them through loose YAML or flexible graph layers,
13
+ the failure modes are always the same: non-deterministic SQL, fan-out double
14
+ counting, ambiguous join paths, raw-row dumps, and runaway scan costs.
15
+
16
+ This project makes the semantic model a **blueprint, never a materialization**:
17
+
18
+ ```
19
+ .sql DDL / .yaml spec ──parse──▶ canonical AST (Pydantic v2)
20
+ ──lint (SV-R1..R10)──▶ valid blueprint
21
+ ──compile(request)──▶ one deterministic SQL statement ──connector──▶ rows
22
+ ```
23
+
24
+ - **Dual input, one AST.** `CREATE SEMANTIC VIEW` DDL (human-authored) and a
25
+ native YAML contract (automation-friendly) build byte-identical models.
26
+ - **Static ground rules (SV-R1..R10).** Primary keys and grain comments, a
27
+ directed many-to-one join DAG with exactly one path between any two tables,
28
+ no numeric dimensions, no `CURRENT_DATE`, snapshot metrics declared
29
+ `NON ADDITIVE BY`, globally unique field names and synonyms, a security
30
+ deny-list, comment templates. `ostrict lint` blocks CI on ERRORs.
31
+ - **Deterministic compilation.** Same request, same SQL, byte for byte. Joins
32
+ only walk child→parent (zero fan-out, enforced); metrics of different grains
33
+ drill across via `UNION ALL`; derived ratios divide NULL-safely; every
34
+ expression is built as a sqlglot AST — no string concatenation anywhere.
35
+ - **FinOps guardrails.** At least one metric per query (no raw dumps),
36
+ partition filters required and pushed into each table's subquery (the only
37
+ placement BigQuery honors on joined tables), `LIMIT` injected and capped,
38
+ BigQuery dry-run byte ceiling.
39
+ - **MCP server (mcp ≥ 2).** Five tools: `list_semantic_views`,
40
+ `inspect_semantic_view`, `get_dimension_values`, `compile_semantic_query`,
41
+ `execute_semantic_query`. The agent picks names from the catalog and never
42
+ writes SQL; every refusal carries an error code and a repair hint.
43
+ - **Golden Questions eval.** `ostrict eval cases.yaml` grades
44
+ answer/refuse/clarify cases against DuckDB deterministically — no LLM judge.
45
+ - **OSI interchange.** `ostrict export|import` converts views to and
46
+ from Open Semantic Interchange YAML/JSON losslessly — guardrails travel in a
47
+ namespaced `custom_extensions.strict_semantic` block, and every import must
48
+ pass the full SV-R1..R10 gate.
49
+ - **Open-core plugin hooks.** `on_model_loaded`, `before_compile` (RBAC filter
50
+ injection), `after_compile` (cache/lineage), `on_query_executed` (audit).
51
+
52
+ ## Quickstart
53
+
54
+ ```bash
55
+ python -m venv .venv && .venv/bin/pip install -e '.[dev,mcp]'
56
+ .venv/bin/python -m pytest # 80 tests
57
+ .venv/bin/ostrict lint tests/fixtures/ecommerce.sql
58
+ .venv/bin/ostrict eval tests/fixtures/golden_questions.yaml
59
+ .venv/bin/ostrict compile tests/fixtures/ecommerce.yaml \
60
+ -m revenue -m aov -d region \
61
+ -f '{"field":"order_date","op":">=","value":"2026-01-01"}' \
62
+ -f '{"field":"order_date","op":"<","value":"2026-02-01"}' --dialect bigquery
63
+ ```
64
+
65
+ Run the MCP server against the demo warehouse (in-memory DuckDB):
66
+
67
+ ```bash
68
+ OSTRICT_CONFIG=$PWD/examples/config.duckdb.yml .venv/bin/ostrict-mcp
69
+ ```
70
+
71
+ or register it with Claude Code:
72
+
73
+ ```bash
74
+ claude mcp add ostrict -e OSTRICT_CONFIG=$PWD/examples/config.duckdb.yml -- $PWD/.venv/bin/ostrict-mcp
75
+ ```
76
+
77
+ For BigQuery, copy `examples/config.bigquery.example.yml`, point `views:` at
78
+ your private spec folder, and set `max_bytes`.
79
+
80
+ ## Writing a semantic view
81
+
82
+ The `CREATE SEMANTIC VIEW` DDL (`.sql`) and the native YAML (`.yml`) are
83
+ equivalent; metadata the DDL has no slot for travels in structured comment
84
+ directives (enforced by the linter as SV-R8.1 templates):
85
+
86
+ ```sql
87
+ CREATE SEMANTIC VIEW ecommerce
88
+ TABLES (
89
+ orders AS shop.orders PRIMARY KEY (order_id)
90
+ COMMENT = 'One row per order. Primary date: order_date. Partition: order_date BY order_date. Source: shop.orders.',
91
+ customers AS shop.customers PRIMARY KEY (customer_id)
92
+ COMMENT = 'One row per customer. Primary date: signup_date. Source: shop.customers.'
93
+ )
94
+ RELATIONSHIPS ( orders_customers AS orders (customer_id) REFERENCES customers (customer_id) )
95
+ DIMENSIONS (
96
+ customers.region AS region WITH SYNONYMS ('market') COMMENT = 'Customer home region. Values: EU, US, APAC.',
97
+ orders.order_date AS order_date COMMENT = 'Date the order was placed. Grains: day, week, month.'
98
+ )
99
+ METRICS (
100
+ orders.order_count AS COUNT(order_id) COMMENT = 'Number of orders placed. Population: all orders. Additivity: additive.'
101
+ )
102
+ COMMENT = 'Retail sales.'
103
+ ```
104
+
105
+ Directives: `One row per …` (grain), `Primary date: col.`,
106
+ `Point-in-time snapshot.`, `Partition: col BY dimension (lookback Nd, lookahead Nd).`,
107
+ `Grains: day, month.`, `Type: timestamp.`, `Values: a, b, c.`
108
+ See `tests/fixtures/ecommerce.{sql,yaml}` for the full twin fixtures.
109
+
110
+ ## Querying
111
+
112
+ ```json
113
+ {"metrics": ["revenue", "aov"], "dimensions": ["region", "order_date__month"],
114
+ "filters": [{"field": "order_date", "op": "between", "value": ["2026-01-01", "2026-03-31"]},
115
+ {"field": "aov", "op": ">", "value": 50}],
116
+ "order_by": ["-revenue"], "limit": 100}
117
+ ```
118
+
119
+ Time dimensions take a `__grain` suffix. Filters on dimensions apply before
120
+ aggregation, filters on metrics after. Dimension-only queries are refused
121
+ (`ERR_METRIC_REQUIRED`) — value discovery goes through `get_dimension_values`,
122
+ a bounded `SELECT DISTINCT … LIMIT ≤ 50`.
123
+
124
+ ## OSI interchange
125
+
126
+ Exchange views with other semantic-layer tools as OSI (Open Semantic
127
+ Interchange) documents:
128
+
129
+ ```bash
130
+ ostrict export tests/fixtures/ecommerce.sql -o ecommerce.osi.yaml
131
+ ostrict export tests/fixtures/ecommerce.yaml --format json --strip-extensions
132
+ ostrict import ecommerce.osi.yaml -o views/imported.yaml
133
+ ```
134
+
135
+ Exports are lossless: strict-semantic guardrails (dialect, partition pruning
136
+ bounds, snapshot flags, time types) ride in `custom_extensions.strict_semantic`
137
+ blocks, and an export → import round-trip rebuilds an equal AST.
138
+ `--strip-extensions` emits vanilla OSI instead and warns on stderr for every
139
+ guardrail that drops out. Imports are gated: the document is linted
140
+ (SV-R1..R10) before it is written as a native spec, so external models cannot
141
+ bypass the ground rules (`--no-lint` converts anyway, for inspection). The
142
+ field-by-field mapping lives in [docs/osi-mapping.md](docs/osi-mapping.md).
143
+
144
+ ## Layout
145
+
146
+ ```
147
+ src/ostrict/
148
+ ├── models/ # canonical AST, error codes, request/response contracts
149
+ ├── parsers/ # CREATE SEMANTIC VIEW DDL (token-stream) + native YAML -> one builder
150
+ ├── interchange/ # OSI (Open Semantic Interchange) export/import adapter
151
+ ├── linter/ # SV-R1..R10 engine: structure / logic / governance rules
152
+ ├── compiler/ # join-graph resolver, FinOps guardrails, sqlglot SQL builder
153
+ ├── connectors/ # DuckDB, BigQuery (dry-run + byte cap), protocol
154
+ ├── plugins/ # open-core lifecycle hooks
155
+ ├── eval/ # Golden Questions runner (deterministic, no LLM)
156
+ └── mcp_server/ # the five MCP tools over stdio
157
+ ```
158
+
159
+ Design departures from the original blueprint, with reasons, are in
160
+ [docs/REVISIONS.md](docs/REVISIONS.md).
161
+
162
+ ## Security & deployment responsibility
163
+
164
+ The server speaks **stdio** and is designed to run next to the agent, on your
165
+ machine, with your credentials. If you expose it over a network (HTTP/SSE,
166
+ a gateway, a shared host), authentication, transport security and tenant
167
+ isolation are yours to provide — nothing here does that for you.
168
+
169
+ Expect defense in depth from the warehouse side too: run the connector with a
170
+ **least-privilege, read-only** account scoped to the datasets the views name.
171
+ The compiler never concatenates SQL strings — filter values become typed
172
+ sqlglot literals, operators come from a fixed whitelist, and every identifier
173
+ must resolve against the linted model — but the SQL that reaches your warehouse
174
+ should still be gated by the warehouse's own permissions. Per the Apache 2.0
175
+ license, the software is provided as is, without warranty; securing the
176
+ deployment is the deployer's responsibility.
177
+
178
+ Report vulnerabilities privately via GitHub Security Advisories, not public
179
+ issues.
180
+
181
+ ## Contributing
182
+
183
+ Scope is deliberately strict and minimal — read
184
+ [CONTRIBUTING.md](CONTRIBUTING.md) before proposing features. Short version:
185
+ determinism is non-negotiable, escape hatches get declined, and extensions
186
+ belong in plugins.
187
+
188
+ ## Renamed from strict-semantic-mcp
189
+
190
+ v0.3.0 renamed the project to **ostrict**. Old integrations keep working:
191
+ `import strict_semantic` is a deprecated alias of `ostrict` (same module
192
+ objects), the `strict-semantic` / `strict-semantic-mcp` entry points remain,
193
+ and `STRICT_SEMANTIC_CONFIG` is honored when `OSTRICT_CONFIG` is unset. The
194
+ OSI extension namespace stays `custom_extensions.strict_semantic`, so 0.2.x
195
+ documents import unchanged. On PyPI, new releases ship as `ostrict`;
196
+ `strict-semantic-mcp` stays at 0.2.0.
197
+
198
+ ## License
199
+
200
+ Apache 2.0 (see [LICENSE](LICENSE) and [NOTICE](NOTICE)). Fixtures and examples
201
+ are a generic e-commerce schema; keep your company's semantic views in a
202
+ private repository and point the server at them via `OSTRICT_CONFIG`.