erdl-formal 0.1.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 (34) hide show
  1. erdl_formal-0.1.0/LICENSE +176 -0
  2. erdl_formal-0.1.0/NOTICE +2 -0
  3. erdl_formal-0.1.0/PKG-INFO +169 -0
  4. erdl_formal-0.1.0/README.md +153 -0
  5. erdl_formal-0.1.0/pyproject.toml +27 -0
  6. erdl_formal-0.1.0/setup.cfg +4 -0
  7. erdl_formal-0.1.0/src/erdl_formal/__init__.py +21 -0
  8. erdl_formal-0.1.0/src/erdl_formal/calendar.py +128 -0
  9. erdl_formal-0.1.0/src/erdl_formal/compiler.py +199 -0
  10. erdl_formal-0.1.0/src/erdl_formal/field_contracts.py +63 -0
  11. erdl_formal-0.1.0/src/erdl_formal/fixed_point.py +95 -0
  12. erdl_formal-0.1.0/src/erdl_formal/properties.py +88 -0
  13. erdl_formal-0.1.0/src/erdl_formal/quantifiers.py +51 -0
  14. erdl_formal-0.1.0/src/erdl_formal/resolution.py +88 -0
  15. erdl_formal-0.1.0/src/erdl_formal/tvl.py +282 -0
  16. erdl_formal-0.1.0/src/erdl_formal.egg-info/PKG-INFO +169 -0
  17. erdl_formal-0.1.0/src/erdl_formal.egg-info/SOURCES.txt +32 -0
  18. erdl_formal-0.1.0/src/erdl_formal.egg-info/dependency_links.txt +1 -0
  19. erdl_formal-0.1.0/src/erdl_formal.egg-info/requires.txt +4 -0
  20. erdl_formal-0.1.0/src/erdl_formal.egg-info/top_level.txt +1 -0
  21. erdl_formal-0.1.0/tests/test_aggregate.py +47 -0
  22. erdl_formal-0.1.0/tests/test_between_days.py +73 -0
  23. erdl_formal-0.1.0/tests/test_calendar.py +112 -0
  24. erdl_formal-0.1.0/tests/test_compiler.py +200 -0
  25. erdl_formal-0.1.0/tests/test_field_contracts.py +51 -0
  26. erdl_formal-0.1.0/tests/test_fixed_point.py +115 -0
  27. erdl_formal-0.1.0/tests/test_g3.py +77 -0
  28. erdl_formal-0.1.0/tests/test_in_arith.py +70 -0
  29. erdl_formal-0.1.0/tests/test_mul_div.py +73 -0
  30. erdl_formal-0.1.0/tests/test_properties.py +62 -0
  31. erdl_formal-0.1.0/tests/test_quantifiers.py +94 -0
  32. erdl_formal-0.1.0/tests/test_resolution.py +101 -0
  33. erdl_formal-0.1.0/tests/test_string.py +65 -0
  34. erdl_formal-0.1.0/tests/test_tvl.py +134 -0
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,2 @@
1
+ erdl-formal
2
+ Copyright 2026 Shenzhen Miaojing Technology Co., Ltd.
@@ -0,0 +1,169 @@
1
+ Metadata-Version: 2.4
2
+ Name: erdl-formal
3
+ Version: 0.1.0
4
+ Summary: Determinism isn't tested — it's proven. SMT-based formal verification for ERDL, the rule language of enterprise AI agents.
5
+ Author-email: OpenOBA <support@openoba.com>
6
+ License: Apache-2.0
7
+ Keywords: erdl,formal-verification,smt,z3,cvc5,policy,ai-agent,deterministic
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ License-File: NOTICE
12
+ Requires-Dist: z3-solver>=4.13
13
+ Provides-Extra: dev
14
+ Requires-Dist: pytest>=8; extra == "dev"
15
+ Dynamic: license-file
16
+
17
+ # erdl-formal
18
+
19
+ **确定性,不是测出来的,是证出来的。**
20
+
21
+ erdl-formal 是 ERDL 表达内核的**形式化验证器**:把规则编译到 SMT(Z3),做**静态性质证明**——不是「测试通过」,而是「数学上不存在反例」。
22
+
23
+ > ERDL 是企业 AI Agent 的确定性规则语言(34 节点类型化表达式树 + E1–E12 求值约束)。本仓库回答一个问题:**这条规则,在所有输入下,会不会出错、会不会误放行、会不会该拦不拦?**
24
+
25
+ ## 为什么是现在:LLM 有蛮力,没方向
26
+
27
+ LLM 是概率性的:同一个输入,两种回答。把企业决策交给概率分布,审计迟早会问——「这个决定,依据是什么?」
28
+
29
+ 行业共识正在成形:**LLM 负责理解,规则负责裁决**——在 Agent 前面,必须有一层确定性规则引擎。但规则引擎的「确定性」通常靠单元测试背书,而测试只能证明**测过的输入**是对的。
30
+
31
+ 金融、保险、政务这类强监管行业,审计只问一个问题:
32
+
33
+ > **这条规则,对所有输入都成立吗?**
34
+
35
+ 抽样测试回答不了这个问题,回答只能是证明。Cedar Analysis 在 AWS 内部验证了这条路(Lean 形式化 + SMT 符号分析);erdl-formal 对 ERDL 表达内核做同样的事——把「确定性」从**抽样测试**升到**全量证明**。
36
+
37
+ **和 Cedar 同一条路(形式化策略分析),不同的战场:我们证明的是为钱、时间和决策对象而生的企业规则内核。**
38
+
39
+ ## 30 秒,看一个证明
40
+
41
+ G3 密级访问控制:文件密级 > 操作员密级 → DENY。要证明这条规则**可达**且 **fail-closed**:
42
+
43
+ ```python
44
+ from erdl_formal.field_contracts import FieldContract, Schema
45
+ from erdl_formal.properties import always_denies
46
+
47
+ schema = Schema()
48
+ schema.add(FieldContract(field="file_cls", type="int"))
49
+ schema.add(FieldContract(field="op_cls", type="int"))
50
+
51
+ # when: file_cls > op_cls → DENY
52
+ rule = ["gt", ["field", "file_cls"], ["field", "op_cls"]]
53
+
54
+ # 一条断言,同时证明两条性质:
55
+ # 1) 可达 —— 字段都在时,密级更高确实触发拦截
56
+ # 2) fail-closed —— op_cls 缺失时,比较按 E11 折叠为假,
57
+ # 规则绝不产生误放行绕过
58
+ assert always_denies(rule, schema, premises=["file_cls", "op_cls"], missing_field="op_cls")
59
+ ```
60
+
61
+ 背后没有测试用例,没有抽样。Z3 在**所有整数**的空间里搜索违反性质的输入:找到,返回**可回放的具体反例**(能丢回真实引擎复核);找不到,UNSAT——性质对全量输入成立,证毕。
62
+
63
+ ## 能证明什么
64
+
65
+ | 性质 | 含义 | 来源 |
66
+ |---|---|---|
67
+ | never-errors | 求值永不产生 EvalError | Cedar |
68
+ | always-denies | 命中即拦截(含 fail-closed:字段缺失不绕过)| Cedar + E11 |
69
+ | always-allows / subsumption / equivalence / disjointness | 放行 / 蕴含 / 等价 / 互斥 | Cedar |
70
+ | override-soundness | override 仅 DENY→ALLOW 方向(不覆盖到更不安全态)| **ERDL 特有** |
71
+ | ring-respect | 无 override 时,ring 顺序在 DENY 方向被尊重 | **ERDL 特有** |
72
+ | emergency-shortcut | EMERGENCY_HALT 命中即短路 | **ERDL 特有** |
73
+
74
+ 每个性质都能合成具体反例,反例可回放真实引擎交叉验证——证明 + 差分,双保险。
75
+
76
+ ## 34/34 节点覆盖,E1–E12 精确语义
77
+
78
+ - **34 节点全部有 SMT 编码**:取值 / 逻辑 / 比较 / 集合 / 字符串 / 存在量纲 / 量词 / 算术 / 时间 / 聚合。
79
+ - 关键语义不是「大致对」,是**逐位精确**:
80
+ - **E2** 定点小数:scale=14 + half-even——钱,不允许 `0.1 + 0.2` 式漂移;
81
+ - **E8** 量词空数组折叠:反空洞真,`all([])` 是假不是真;
82
+ - **E11** 三值逻辑:字段缺失在叶子处折叠为假(非 Kleene,不 fail-open);
83
+ - **E12** tier 折叠、**E10** NFC 归一化。
84
+
85
+ ## 独立验证者:三重独立,逐字节对拍
86
+
87
+ 验证者的可信度来自**不看被验证者的答案**。erdl-formal 只依据规范([`erdl-spec-v2.0`](https://github.com/OpenOBA/erdl-landing/blob/main/spec/erdl-spec-v2.0.md))编码,零依赖任何 ERDL 引擎实现,与 `erdl`(TS 引擎)、`erdl-vectors`(冻结向量)构成三重独立:
88
+
89
+ | 交叉验证 | 对象 | 结果 |
90
+ |---|---|---|
91
+ | 定点小数 | `fixed_point.py` ↔ erdl `fixed-point.js` | 逐字节一致 |
92
+ | 裁决语义 | `resolution.py` ↔ erdl `Evaluator` | 4 场景 |
93
+ | 算术向量 | ↔ erdl-vectors V-ENGINE | 7/7 |
94
+ | 日历向量 | ↔ erdl-vectors V-ENGINE | 6/6 |
95
+ | G3 反例回放 | `tvl.py` ↔ erdl engine | 逐场景一致 |
96
+
97
+ **Measurements, not endorsements.** 三个独立构建的系统逐字节一致——说明规范足够精确,能支撑独立实现的精确复现。
98
+
99
+ ## 与 Cedar / OPA 的差异
100
+
101
+ | | Cedar Analysis | OPA / Rego | **erdl-formal** |
102
+ |---|---|---|---|
103
+ | 形式化 | ✅ Lean + SMT(业界最强)| ❌ 无形式化语义(实现即规范)| ✅ SMT(Z3)|
104
+ | 钱 | 小数仅扩展插件 | float64 丢精度 | ✅ scale=14 定点 + half-even |
105
+ | 决策对象 | 只有策略 ID | 日志无签名 | ✅ 富决策对象(DO)|
106
+ | 自然语言 | 单向(NL→策略)| 单向 | ✅ 确定性 gloss 锚定回译(双向)|
107
+
108
+ 差异不是「更形式化」——Cedar 的形式化栈是业界标杆。差异在**被形式化的东西**:ERDL 是为钱、时间、聚合、量词、决策对象与双向自然语言而生的企业规则内核,这些在 Cedar / OPA 的世界里不存在。
109
+
110
+ ## 架构
111
+
112
+ ```
113
+ ERDL 规则 (S-expression)
114
+ │
115
+ ▼ compiler.py(符号编译器:schema 驱动字段类型 + 量词索引展开)
116
+ Z3 TVL 表达式(TVL(τ) = Def | Missing,叶子折叠)
117
+ │
118
+ ▼ properties.py(性质验证)/ resolution.py(裁决参考模型)
119
+ sat / unsat + 反例(回放真实引擎交叉验证)
120
+ ```
121
+
122
+ ## 安装
123
+
124
+ **用户**(从 PyPI,一条命令):
125
+
126
+ ```bash
127
+ pip install erdl-formal
128
+ ```
129
+
130
+ **开发者**(从源码,可编辑安装,改代码即时生效):
131
+
132
+ ```bash
133
+ git clone https://github.com/OpenOBA/erdl-formal.git
134
+ cd erdl-formal
135
+ python -m pip install -e ".[dev]" # Python ≥3.11(3.14 开发),z3-solver ≥4.13(5.1.0 验证)
136
+ ```
137
+
138
+ **从源码构建分发包**(wheel + sdist,供发布 / 离线分发):
139
+
140
+ ```bash
141
+ python -m pip install build
142
+ python -m build # 产出 dist/erdl_formal-0.1.0-py3-none-any.whl + .tar.gz
143
+ ```
144
+
145
+ ## 快速上手
146
+
147
+ ```bash
148
+ pytest # 130 全绿
149
+ python examples/verify_g3.py # 验证 G3 密级规则(可达 + fail-closed)
150
+ python replay/crosscheck-vectors.py # 与 erdl-vectors 冻结向量对拍
151
+ ```
152
+
153
+ ## 文档
154
+
155
+ - `docs/semantics.md` — 34 节点指称语义
156
+ - `docs/tvl-encoding.md` — 三值逻辑 SMT 编码规范
157
+ - `docs/field-contracts.md` — 验证 schema 契约
158
+ - `docs/DEVELOPER-GUIDE.md` — 二开指南(架构 / 加节点 / 加性质 / API)
159
+
160
+ ## 贡献与安全
161
+
162
+ - `CONTRIBUTING.md` — 贡献流程(先 issue 后 PR / review)
163
+ - `SECURITY.md` — 漏洞私下报,不开公开 issue
164
+ - `CHANGELOG.md` — 变更记录(Keep a Changelog)
165
+ - `CODE_OF_CONDUCT.md` / `style_guide.md`
166
+
167
+ ## 许可证
168
+
169
+ Apache-2.0 · © 2026 深圳市秒镜科技有限公司 (Shenzhen Miaojing Technology Co., Ltd.)
@@ -0,0 +1,153 @@
1
+ # erdl-formal
2
+
3
+ **确定性,不是测出来的,是证出来的。**
4
+
5
+ erdl-formal 是 ERDL 表达内核的**形式化验证器**:把规则编译到 SMT(Z3),做**静态性质证明**——不是「测试通过」,而是「数学上不存在反例」。
6
+
7
+ > ERDL 是企业 AI Agent 的确定性规则语言(34 节点类型化表达式树 + E1–E12 求值约束)。本仓库回答一个问题:**这条规则,在所有输入下,会不会出错、会不会误放行、会不会该拦不拦?**
8
+
9
+ ## 为什么是现在:LLM 有蛮力,没方向
10
+
11
+ LLM 是概率性的:同一个输入,两种回答。把企业决策交给概率分布,审计迟早会问——「这个决定,依据是什么?」
12
+
13
+ 行业共识正在成形:**LLM 负责理解,规则负责裁决**——在 Agent 前面,必须有一层确定性规则引擎。但规则引擎的「确定性」通常靠单元测试背书,而测试只能证明**测过的输入**是对的。
14
+
15
+ 金融、保险、政务这类强监管行业,审计只问一个问题:
16
+
17
+ > **这条规则,对所有输入都成立吗?**
18
+
19
+ 抽样测试回答不了这个问题,回答只能是证明。Cedar Analysis 在 AWS 内部验证了这条路(Lean 形式化 + SMT 符号分析);erdl-formal 对 ERDL 表达内核做同样的事——把「确定性」从**抽样测试**升到**全量证明**。
20
+
21
+ **和 Cedar 同一条路(形式化策略分析),不同的战场:我们证明的是为钱、时间和决策对象而生的企业规则内核。**
22
+
23
+ ## 30 秒,看一个证明
24
+
25
+ G3 密级访问控制:文件密级 > 操作员密级 → DENY。要证明这条规则**可达**且 **fail-closed**:
26
+
27
+ ```python
28
+ from erdl_formal.field_contracts import FieldContract, Schema
29
+ from erdl_formal.properties import always_denies
30
+
31
+ schema = Schema()
32
+ schema.add(FieldContract(field="file_cls", type="int"))
33
+ schema.add(FieldContract(field="op_cls", type="int"))
34
+
35
+ # when: file_cls > op_cls → DENY
36
+ rule = ["gt", ["field", "file_cls"], ["field", "op_cls"]]
37
+
38
+ # 一条断言,同时证明两条性质:
39
+ # 1) 可达 —— 字段都在时,密级更高确实触发拦截
40
+ # 2) fail-closed —— op_cls 缺失时,比较按 E11 折叠为假,
41
+ # 规则绝不产生误放行绕过
42
+ assert always_denies(rule, schema, premises=["file_cls", "op_cls"], missing_field="op_cls")
43
+ ```
44
+
45
+ 背后没有测试用例,没有抽样。Z3 在**所有整数**的空间里搜索违反性质的输入:找到,返回**可回放的具体反例**(能丢回真实引擎复核);找不到,UNSAT——性质对全量输入成立,证毕。
46
+
47
+ ## 能证明什么
48
+
49
+ | 性质 | 含义 | 来源 |
50
+ |---|---|---|
51
+ | never-errors | 求值永不产生 EvalError | Cedar |
52
+ | always-denies | 命中即拦截(含 fail-closed:字段缺失不绕过)| Cedar + E11 |
53
+ | always-allows / subsumption / equivalence / disjointness | 放行 / 蕴含 / 等价 / 互斥 | Cedar |
54
+ | override-soundness | override 仅 DENY→ALLOW 方向(不覆盖到更不安全态)| **ERDL 特有** |
55
+ | ring-respect | 无 override 时,ring 顺序在 DENY 方向被尊重 | **ERDL 特有** |
56
+ | emergency-shortcut | EMERGENCY_HALT 命中即短路 | **ERDL 特有** |
57
+
58
+ 每个性质都能合成具体反例,反例可回放真实引擎交叉验证——证明 + 差分,双保险。
59
+
60
+ ## 34/34 节点覆盖,E1–E12 精确语义
61
+
62
+ - **34 节点全部有 SMT 编码**:取值 / 逻辑 / 比较 / 集合 / 字符串 / 存在量纲 / 量词 / 算术 / 时间 / 聚合。
63
+ - 关键语义不是「大致对」,是**逐位精确**:
64
+ - **E2** 定点小数:scale=14 + half-even——钱,不允许 `0.1 + 0.2` 式漂移;
65
+ - **E8** 量词空数组折叠:反空洞真,`all([])` 是假不是真;
66
+ - **E11** 三值逻辑:字段缺失在叶子处折叠为假(非 Kleene,不 fail-open);
67
+ - **E12** tier 折叠、**E10** NFC 归一化。
68
+
69
+ ## 独立验证者:三重独立,逐字节对拍
70
+
71
+ 验证者的可信度来自**不看被验证者的答案**。erdl-formal 只依据规范([`erdl-spec-v2.0`](https://github.com/OpenOBA/erdl-landing/blob/main/spec/erdl-spec-v2.0.md))编码,零依赖任何 ERDL 引擎实现,与 `erdl`(TS 引擎)、`erdl-vectors`(冻结向量)构成三重独立:
72
+
73
+ | 交叉验证 | 对象 | 结果 |
74
+ |---|---|---|
75
+ | 定点小数 | `fixed_point.py` ↔ erdl `fixed-point.js` | 逐字节一致 |
76
+ | 裁决语义 | `resolution.py` ↔ erdl `Evaluator` | 4 场景 |
77
+ | 算术向量 | ↔ erdl-vectors V-ENGINE | 7/7 |
78
+ | 日历向量 | ↔ erdl-vectors V-ENGINE | 6/6 |
79
+ | G3 反例回放 | `tvl.py` ↔ erdl engine | 逐场景一致 |
80
+
81
+ **Measurements, not endorsements.** 三个独立构建的系统逐字节一致——说明规范足够精确,能支撑独立实现的精确复现。
82
+
83
+ ## 与 Cedar / OPA 的差异
84
+
85
+ | | Cedar Analysis | OPA / Rego | **erdl-formal** |
86
+ |---|---|---|---|
87
+ | 形式化 | ✅ Lean + SMT(业界最强)| ❌ 无形式化语义(实现即规范)| ✅ SMT(Z3)|
88
+ | 钱 | 小数仅扩展插件 | float64 丢精度 | ✅ scale=14 定点 + half-even |
89
+ | 决策对象 | 只有策略 ID | 日志无签名 | ✅ 富决策对象(DO)|
90
+ | 自然语言 | 单向(NL→策略)| 单向 | ✅ 确定性 gloss 锚定回译(双向)|
91
+
92
+ 差异不是「更形式化」——Cedar 的形式化栈是业界标杆。差异在**被形式化的东西**:ERDL 是为钱、时间、聚合、量词、决策对象与双向自然语言而生的企业规则内核,这些在 Cedar / OPA 的世界里不存在。
93
+
94
+ ## 架构
95
+
96
+ ```
97
+ ERDL 规则 (S-expression)
98
+ │
99
+ ▼ compiler.py(符号编译器:schema 驱动字段类型 + 量词索引展开)
100
+ Z3 TVL 表达式(TVL(τ) = Def | Missing,叶子折叠)
101
+ │
102
+ ▼ properties.py(性质验证)/ resolution.py(裁决参考模型)
103
+ sat / unsat + 反例(回放真实引擎交叉验证)
104
+ ```
105
+
106
+ ## 安装
107
+
108
+ **用户**(从 PyPI,一条命令):
109
+
110
+ ```bash
111
+ pip install erdl-formal
112
+ ```
113
+
114
+ **开发者**(从源码,可编辑安装,改代码即时生效):
115
+
116
+ ```bash
117
+ git clone https://github.com/OpenOBA/erdl-formal.git
118
+ cd erdl-formal
119
+ python -m pip install -e ".[dev]" # Python ≥3.11(3.14 开发),z3-solver ≥4.13(5.1.0 验证)
120
+ ```
121
+
122
+ **从源码构建分发包**(wheel + sdist,供发布 / 离线分发):
123
+
124
+ ```bash
125
+ python -m pip install build
126
+ python -m build # 产出 dist/erdl_formal-0.1.0-py3-none-any.whl + .tar.gz
127
+ ```
128
+
129
+ ## 快速上手
130
+
131
+ ```bash
132
+ pytest # 130 全绿
133
+ python examples/verify_g3.py # 验证 G3 密级规则(可达 + fail-closed)
134
+ python replay/crosscheck-vectors.py # 与 erdl-vectors 冻结向量对拍
135
+ ```
136
+
137
+ ## 文档
138
+
139
+ - `docs/semantics.md` — 34 节点指称语义
140
+ - `docs/tvl-encoding.md` — 三值逻辑 SMT 编码规范
141
+ - `docs/field-contracts.md` — 验证 schema 契约
142
+ - `docs/DEVELOPER-GUIDE.md` — 二开指南(架构 / 加节点 / 加性质 / API)
143
+
144
+ ## 贡献与安全
145
+
146
+ - `CONTRIBUTING.md` — 贡献流程(先 issue 后 PR / review)
147
+ - `SECURITY.md` — 漏洞私下报,不开公开 issue
148
+ - `CHANGELOG.md` — 变更记录(Keep a Changelog)
149
+ - `CODE_OF_CONDUCT.md` / `style_guide.md`
150
+
151
+ ## 许可证
152
+
153
+ Apache-2.0 · © 2026 深圳市秒镜科技有限公司 (Shenzhen Miaojing Technology Co., Ltd.)
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "erdl-formal"
7
+ version = "0.1.0"
8
+ description = "Determinism isn't tested — it's proven. SMT-based formal verification for ERDL, the rule language of enterprise AI agents."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = { text = "Apache-2.0" }
12
+ authors = [{ name = "OpenOBA", email = "support@openoba.com" }]
13
+ keywords = ["erdl", "formal-verification", "smt", "z3", "cvc5", "policy", "ai-agent", "deterministic"]
14
+ dependencies = ["z3-solver>=4.13"]
15
+
16
+ [project.optional-dependencies]
17
+ dev = ["pytest>=8"]
18
+
19
+ [tool.setuptools]
20
+ package-dir = { "" = "src" }
21
+
22
+ [tool.setuptools.packages.find]
23
+ where = ["src"]
24
+
25
+ [tool.pytest.ini_options]
26
+ testpaths = ["tests"]
27
+ pythonpath = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,21 @@
1
+ # Copyright 2026 Shenzhen Miaojing Technology Co., Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """erdl-formal — ERDL expression kernel formal verification (SMT).
16
+
17
+ Independent of any ERDL engine implementation (verifier independent of the
18
+ verified, mirroring the erdl-vectors-independent-of-erdl principle).
19
+ """
20
+
21
+ __version__ = "0.1.0"