constraint-runtime 0.3.0__tar.gz → 0.4.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.
- {constraint_runtime-0.3.0/src/constraint_runtime.egg-info → constraint_runtime-0.4.0}/PKG-INFO +37 -6
- constraint_runtime-0.3.0/PKG-INFO → constraint_runtime-0.4.0/README.md +35 -20
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/pyproject.toml +3 -2
- constraint_runtime-0.3.0/README.md → constraint_runtime-0.4.0/src/constraint_runtime.egg-info/PKG-INFO +51 -4
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/SOURCES.txt +1 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/top_level.txt +1 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/mcp_server.py +63 -3
- constraint_runtime-0.4.0/src/runtime_harness.py +478 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/LICENSE +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/setup.cfg +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/circuit_breaker.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/dependency_links.txt +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/entry_points.txt +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/requires.txt +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/coord_gate.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/eval_replay.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/fence_rule_engine.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/identity_gate.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/output_gate.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/registry_gate.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/replay_ledger.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/settlement_receipt_gate.py +0 -0
- {constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/shadow_runner.py +0 -0
{constraint_runtime-0.3.0/src/constraint_runtime.egg-info → constraint_runtime-0.4.0}/PKG-INFO
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: constraint-runtime
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: 智能体约束运行时:十件治理组件+标准笼头编排层(配置驱动、场景模板),附 MCP 服务壳
|
|
5
5
|
Author: tanbopiao
|
|
6
6
|
License: Apache-2.0
|
|
7
7
|
Keywords: mcp,agent,guardrail,circuit-breaker,audit-ledger
|
|
@@ -18,7 +18,7 @@ Dynamic: license-file
|
|
|
18
18
|
|
|
19
19
|
# constraint-runtime · 智能体约束运行时组件集
|
|
20
20
|
|
|
21
|
-
>
|
|
21
|
+
> 给通用智能体/具身智能装上笼头的十个小部件:**围栏规则引擎、影子运行器、熔断停机器、留痕重放器、结算凭证闸、身份闸、输出闸、评测算子、协同闸、注册编排闸**;外加一件**标准笼头(编排层)**——配置驱动,把十件散件按默认管线一次串起来。
|
|
22
22
|
>
|
|
23
23
|
> 元极恒一
|
|
24
24
|
> 纯 Python 标准库实现,零第三方依赖,单文件即用。
|
|
@@ -87,6 +87,30 @@ Dynamic: license-file
|
|
|
87
87
|
└──────────────┘
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
+
## 标准笼头(编排层):散件 → 整机
|
|
91
|
+
|
|
92
|
+
十件散件各有分工,但「每件单独调」不等于「整机在跑」。`runtime_harness.py` 是标准笼头——一个编排器把散件按**默认管线**串起来:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
身份 → 熔断 → 结算(可选) → 围栏 → 影子 → 输出 → 台账
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
- **配置即数据**:管线开关与参数来自配置 dict 或 JSON 文件,改配置不改代码;
|
|
99
|
+
- **场景模板**:内置 `expense`(报销闸)/`support`(客服闸)/`embodied`(具身设备闸)三个模板,各自带默认规则包与段参数,一改即适配;
|
|
100
|
+
- **判定与故障分离**:围栏拦截是「制度在起作用」(记成功),规则包损坏/内部异常是「系统带病」(喂熔断、宁可停机)——两者不混;
|
|
101
|
+
- **一次调用全程留痕**:`process()` 返回统一判定(ALLOW/DENY+命中段+全程 trace),判定入哈希链。
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from runtime_harness import RuntimeHarness
|
|
105
|
+
|
|
106
|
+
h = RuntimeHarness.from_template("expense") # 报销闸模板
|
|
107
|
+
h.register_agent("agent-1", "secret-1", ["submit"])
|
|
108
|
+
v = h.process("agent-1", "submit",
|
|
109
|
+
{"fields": {"amount": 500, "category": "餐饮"}},
|
|
110
|
+
content="报销单已提交")
|
|
111
|
+
# v = {"decision": "ALLOW", "stage": "done", "trace": [...], ...}
|
|
112
|
+
```
|
|
113
|
+
|
|
90
114
|
## 十分钟上手
|
|
91
115
|
|
|
92
116
|
```bash
|
|
@@ -97,7 +121,7 @@ bash quickstart.sh # 一条命令:自检 → 端到端演示 → 篡改
|
|
|
97
121
|
|
|
98
122
|
`quickstart.sh` 会依次跑完三步,全程约 1 分钟:
|
|
99
123
|
|
|
100
|
-
1.
|
|
124
|
+
1. **十一件自检**——每个部件各自证明自己是好的(含编排层);
|
|
101
125
|
2. **端到端演示**——看四段输出:围栏拦截(BLOCK)、影子记账(只记不拦)、影子对照报告与转正建议、哈希链校验与重放一致性;
|
|
102
126
|
3. **篡改发现实验**——亲手把台账里一笔 5000 改成 50,链校验当场报不一致。
|
|
103
127
|
|
|
@@ -126,10 +150,15 @@ python3 src/settlement_receipt_gate.py selftest
|
|
|
126
150
|
python3 src/identity_gate.py selftest
|
|
127
151
|
python3 src/output_gate.py selftest
|
|
128
152
|
python3 src/eval_replay.py selftest
|
|
153
|
+
python3 src/coord_gate.py selftest
|
|
154
|
+
python3 src/registry_gate.py selftest
|
|
155
|
+
python3 src/runtime_harness.py selftest
|
|
129
156
|
```
|
|
130
157
|
|
|
131
158
|
## 用法示例
|
|
132
159
|
|
|
160
|
+
### 散件直用:围栏规则引擎
|
|
161
|
+
|
|
133
162
|
```python
|
|
134
163
|
from fence_rule_engine import FenceEngine
|
|
135
164
|
|
|
@@ -168,8 +197,9 @@ verdict = engine.evaluate({
|
|
|
168
197
|
}
|
|
169
198
|
```
|
|
170
199
|
|
|
171
|
-
|
|
200
|
+
三十五个工具,按治理链分组:
|
|
172
201
|
|
|
202
|
+
- **编排层(标准笼头)**:`harness_process`(主管线一次跑完)、`harness_register_agent`(笼头身份登记)、`harness_status`(笼头水位)、`harness_templates`(场景模板清单)
|
|
173
203
|
- **围栏**:`fence_evaluate`(围栏判定)、`fence_reload`(规则热更)
|
|
174
204
|
- **影子与熔断**:`shadow_feed`(影子投喂)、`shadow_report`(影子报告与转正建议)、`breaker`(熔断控制)
|
|
175
205
|
- **身份与输出**:`identity_register`(注册即登记)、`identity_challenge`(发起握手挑战)、`identity_handshake`(挑战-应答核验)、`identity_authorize`(权限即清单判定)、`output_issue`(签发输出信符)、`output_release`(无信符不出闸)、`output_watermark`(灰度水位账本)
|
|
@@ -191,7 +221,7 @@ verdict = engine.evaluate({
|
|
|
191
221
|
| L5 审计 | 留痕重放器(各组件哈希链同构) | 治理约束可核查 |
|
|
192
222
|
| L6 评测 | 评测算子 | 评测指标与技术标准 |
|
|
193
223
|
| 横切 · 多智能体协同 | 协同闸(T-02 细则落地:追加式条目、三策略仲裁、组合行为风控);各组件事件同入哈希链,跨组件可对账 | 跨域协同 |
|
|
194
|
-
| 横切 · 组件注册与编排 |
|
|
224
|
+
| 横切 · 组件注册与编排 | 注册编排闸(查重先行、版本只进不退、真值+目录树双落位)+标准笼头(配置驱动编排层,默认管线串起散件) | 组件注册与编排 |
|
|
195
225
|
|
|
196
226
|
规范第 06 章的「要求 + 判定规则 + 证据要求」三段式,在本组件集中落地为:每条要求有可执行判定规则(selftest 即判定),每条结论有哈希链证据(篡改可发现)。
|
|
197
227
|
|
|
@@ -213,6 +243,7 @@ verdict = engine.evaluate({
|
|
|
213
243
|
|
|
214
244
|
- 本组件集是**运行时约束**工具,不替代上线前的评测准入(该不该放进来,先考过再说);
|
|
215
245
|
- 哈希链防篡改的边界:能同时修改文件与台账的本地攻击者不在防御范围内,主张仅限「对账不一致可发现」;
|
|
246
|
+
- 编排层只做单机编排:不解决跨进程并发与分布式一致性,配置文件与台账文件能被同时改写的本地攻击者不在防御范围内;
|
|
216
247
|
- 影子转正、熔断阈值需要结合业务场景调参,组件提供机制,不提供万能默认值。
|
|
217
248
|
|
|
218
249
|
## License
|
|
@@ -1,24 +1,8 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: constraint-runtime
|
|
3
|
-
Version: 0.3.0
|
|
4
|
-
Summary: 智能体约束运行时组件集:围栏规则引擎、影子运行器、熔断停机器、留痕重放器、结算凭证闸、身份闸、输出闸、评测算子、协同闸、注册编排闸,附 MCP 服务壳
|
|
5
|
-
Author: tanbopiao
|
|
6
|
-
License: Apache-2.0
|
|
7
|
-
Keywords: mcp,agent,guardrail,circuit-breaker,audit-ledger
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
-
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
-
Requires-Python: >=3.10
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
Requires-Dist: mcp<2,>=1.0
|
|
15
|
-
Dynamic: license-file
|
|
16
|
-
|
|
17
1
|
<!-- mcp-name: io.github.tanbopiao/constraint-runtime -->
|
|
18
2
|
|
|
19
3
|
# constraint-runtime · 智能体约束运行时组件集
|
|
20
4
|
|
|
21
|
-
>
|
|
5
|
+
> 给通用智能体/具身智能装上笼头的十个小部件:**围栏规则引擎、影子运行器、熔断停机器、留痕重放器、结算凭证闸、身份闸、输出闸、评测算子、协同闸、注册编排闸**;外加一件**标准笼头(编排层)**——配置驱动,把十件散件按默认管线一次串起来。
|
|
22
6
|
>
|
|
23
7
|
> 元极恒一
|
|
24
8
|
> 纯 Python 标准库实现,零第三方依赖,单文件即用。
|
|
@@ -87,6 +71,30 @@ Dynamic: license-file
|
|
|
87
71
|
└──────────────┘
|
|
88
72
|
```
|
|
89
73
|
|
|
74
|
+
## 标准笼头(编排层):散件 → 整机
|
|
75
|
+
|
|
76
|
+
十件散件各有分工,但「每件单独调」不等于「整机在跑」。`runtime_harness.py` 是标准笼头——一个编排器把散件按**默认管线**串起来:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
身份 → 熔断 → 结算(可选) → 围栏 → 影子 → 输出 → 台账
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- **配置即数据**:管线开关与参数来自配置 dict 或 JSON 文件,改配置不改代码;
|
|
83
|
+
- **场景模板**:内置 `expense`(报销闸)/`support`(客服闸)/`embodied`(具身设备闸)三个模板,各自带默认规则包与段参数,一改即适配;
|
|
84
|
+
- **判定与故障分离**:围栏拦截是「制度在起作用」(记成功),规则包损坏/内部异常是「系统带病」(喂熔断、宁可停机)——两者不混;
|
|
85
|
+
- **一次调用全程留痕**:`process()` 返回统一判定(ALLOW/DENY+命中段+全程 trace),判定入哈希链。
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from runtime_harness import RuntimeHarness
|
|
89
|
+
|
|
90
|
+
h = RuntimeHarness.from_template("expense") # 报销闸模板
|
|
91
|
+
h.register_agent("agent-1", "secret-1", ["submit"])
|
|
92
|
+
v = h.process("agent-1", "submit",
|
|
93
|
+
{"fields": {"amount": 500, "category": "餐饮"}},
|
|
94
|
+
content="报销单已提交")
|
|
95
|
+
# v = {"decision": "ALLOW", "stage": "done", "trace": [...], ...}
|
|
96
|
+
```
|
|
97
|
+
|
|
90
98
|
## 十分钟上手
|
|
91
99
|
|
|
92
100
|
```bash
|
|
@@ -97,7 +105,7 @@ bash quickstart.sh # 一条命令:自检 → 端到端演示 → 篡改
|
|
|
97
105
|
|
|
98
106
|
`quickstart.sh` 会依次跑完三步,全程约 1 分钟:
|
|
99
107
|
|
|
100
|
-
1.
|
|
108
|
+
1. **十一件自检**——每个部件各自证明自己是好的(含编排层);
|
|
101
109
|
2. **端到端演示**——看四段输出:围栏拦截(BLOCK)、影子记账(只记不拦)、影子对照报告与转正建议、哈希链校验与重放一致性;
|
|
102
110
|
3. **篡改发现实验**——亲手把台账里一笔 5000 改成 50,链校验当场报不一致。
|
|
103
111
|
|
|
@@ -126,10 +134,15 @@ python3 src/settlement_receipt_gate.py selftest
|
|
|
126
134
|
python3 src/identity_gate.py selftest
|
|
127
135
|
python3 src/output_gate.py selftest
|
|
128
136
|
python3 src/eval_replay.py selftest
|
|
137
|
+
python3 src/coord_gate.py selftest
|
|
138
|
+
python3 src/registry_gate.py selftest
|
|
139
|
+
python3 src/runtime_harness.py selftest
|
|
129
140
|
```
|
|
130
141
|
|
|
131
142
|
## 用法示例
|
|
132
143
|
|
|
144
|
+
### 散件直用:围栏规则引擎
|
|
145
|
+
|
|
133
146
|
```python
|
|
134
147
|
from fence_rule_engine import FenceEngine
|
|
135
148
|
|
|
@@ -168,8 +181,9 @@ verdict = engine.evaluate({
|
|
|
168
181
|
}
|
|
169
182
|
```
|
|
170
183
|
|
|
171
|
-
|
|
184
|
+
三十五个工具,按治理链分组:
|
|
172
185
|
|
|
186
|
+
- **编排层(标准笼头)**:`harness_process`(主管线一次跑完)、`harness_register_agent`(笼头身份登记)、`harness_status`(笼头水位)、`harness_templates`(场景模板清单)
|
|
173
187
|
- **围栏**:`fence_evaluate`(围栏判定)、`fence_reload`(规则热更)
|
|
174
188
|
- **影子与熔断**:`shadow_feed`(影子投喂)、`shadow_report`(影子报告与转正建议)、`breaker`(熔断控制)
|
|
175
189
|
- **身份与输出**:`identity_register`(注册即登记)、`identity_challenge`(发起握手挑战)、`identity_handshake`(挑战-应答核验)、`identity_authorize`(权限即清单判定)、`output_issue`(签发输出信符)、`output_release`(无信符不出闸)、`output_watermark`(灰度水位账本)
|
|
@@ -191,7 +205,7 @@ verdict = engine.evaluate({
|
|
|
191
205
|
| L5 审计 | 留痕重放器(各组件哈希链同构) | 治理约束可核查 |
|
|
192
206
|
| L6 评测 | 评测算子 | 评测指标与技术标准 |
|
|
193
207
|
| 横切 · 多智能体协同 | 协同闸(T-02 细则落地:追加式条目、三策略仲裁、组合行为风控);各组件事件同入哈希链,跨组件可对账 | 跨域协同 |
|
|
194
|
-
| 横切 · 组件注册与编排 |
|
|
208
|
+
| 横切 · 组件注册与编排 | 注册编排闸(查重先行、版本只进不退、真值+目录树双落位)+标准笼头(配置驱动编排层,默认管线串起散件) | 组件注册与编排 |
|
|
195
209
|
|
|
196
210
|
规范第 06 章的「要求 + 判定规则 + 证据要求」三段式,在本组件集中落地为:每条要求有可执行判定规则(selftest 即判定),每条结论有哈希链证据(篡改可发现)。
|
|
197
211
|
|
|
@@ -213,6 +227,7 @@ verdict = engine.evaluate({
|
|
|
213
227
|
|
|
214
228
|
- 本组件集是**运行时约束**工具,不替代上线前的评测准入(该不该放进来,先考过再说);
|
|
215
229
|
- 哈希链防篡改的边界:能同时修改文件与台账的本地攻击者不在防御范围内,主张仅限「对账不一致可发现」;
|
|
230
|
+
- 编排层只做单机编排:不解决跨进程并发与分布式一致性,配置文件与台账文件能被同时改写的本地攻击者不在防御范围内;
|
|
216
231
|
- 影子转正、熔断阈值需要结合业务场景调参,组件提供机制,不提供万能默认值。
|
|
217
232
|
|
|
218
233
|
## License
|
|
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "constraint-runtime"
|
|
7
|
-
version = "0.
|
|
8
|
-
description = "
|
|
7
|
+
version = "0.4.0"
|
|
8
|
+
description = "智能体约束运行时:十件治理组件+标准笼头编排层(配置驱动、场景模板),附 MCP 服务壳"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
license = { text = "Apache-2.0" }
|
|
@@ -36,4 +36,5 @@ py-modules = [
|
|
|
36
36
|
"eval_replay",
|
|
37
37
|
"coord_gate",
|
|
38
38
|
"registry_gate",
|
|
39
|
+
"runtime_harness",
|
|
39
40
|
]
|
|
@@ -1,8 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: constraint-runtime
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: 智能体约束运行时:十件治理组件+标准笼头编排层(配置驱动、场景模板),附 MCP 服务壳
|
|
5
|
+
Author: tanbopiao
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Keywords: mcp,agent,guardrail,circuit-breaker,audit-ledger
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: mcp<2,>=1.0
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
1
17
|
<!-- mcp-name: io.github.tanbopiao/constraint-runtime -->
|
|
2
18
|
|
|
3
19
|
# constraint-runtime · 智能体约束运行时组件集
|
|
4
20
|
|
|
5
|
-
>
|
|
21
|
+
> 给通用智能体/具身智能装上笼头的十个小部件:**围栏规则引擎、影子运行器、熔断停机器、留痕重放器、结算凭证闸、身份闸、输出闸、评测算子、协同闸、注册编排闸**;外加一件**标准笼头(编排层)**——配置驱动,把十件散件按默认管线一次串起来。
|
|
6
22
|
>
|
|
7
23
|
> 元极恒一
|
|
8
24
|
> 纯 Python 标准库实现,零第三方依赖,单文件即用。
|
|
@@ -71,6 +87,30 @@
|
|
|
71
87
|
└──────────────┘
|
|
72
88
|
```
|
|
73
89
|
|
|
90
|
+
## 标准笼头(编排层):散件 → 整机
|
|
91
|
+
|
|
92
|
+
十件散件各有分工,但「每件单独调」不等于「整机在跑」。`runtime_harness.py` 是标准笼头——一个编排器把散件按**默认管线**串起来:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
身份 → 熔断 → 结算(可选) → 围栏 → 影子 → 输出 → 台账
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
- **配置即数据**:管线开关与参数来自配置 dict 或 JSON 文件,改配置不改代码;
|
|
99
|
+
- **场景模板**:内置 `expense`(报销闸)/`support`(客服闸)/`embodied`(具身设备闸)三个模板,各自带默认规则包与段参数,一改即适配;
|
|
100
|
+
- **判定与故障分离**:围栏拦截是「制度在起作用」(记成功),规则包损坏/内部异常是「系统带病」(喂熔断、宁可停机)——两者不混;
|
|
101
|
+
- **一次调用全程留痕**:`process()` 返回统一判定(ALLOW/DENY+命中段+全程 trace),判定入哈希链。
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from runtime_harness import RuntimeHarness
|
|
105
|
+
|
|
106
|
+
h = RuntimeHarness.from_template("expense") # 报销闸模板
|
|
107
|
+
h.register_agent("agent-1", "secret-1", ["submit"])
|
|
108
|
+
v = h.process("agent-1", "submit",
|
|
109
|
+
{"fields": {"amount": 500, "category": "餐饮"}},
|
|
110
|
+
content="报销单已提交")
|
|
111
|
+
# v = {"decision": "ALLOW", "stage": "done", "trace": [...], ...}
|
|
112
|
+
```
|
|
113
|
+
|
|
74
114
|
## 十分钟上手
|
|
75
115
|
|
|
76
116
|
```bash
|
|
@@ -81,7 +121,7 @@ bash quickstart.sh # 一条命令:自检 → 端到端演示 → 篡改
|
|
|
81
121
|
|
|
82
122
|
`quickstart.sh` 会依次跑完三步,全程约 1 分钟:
|
|
83
123
|
|
|
84
|
-
1.
|
|
124
|
+
1. **十一件自检**——每个部件各自证明自己是好的(含编排层);
|
|
85
125
|
2. **端到端演示**——看四段输出:围栏拦截(BLOCK)、影子记账(只记不拦)、影子对照报告与转正建议、哈希链校验与重放一致性;
|
|
86
126
|
3. **篡改发现实验**——亲手把台账里一笔 5000 改成 50,链校验当场报不一致。
|
|
87
127
|
|
|
@@ -110,10 +150,15 @@ python3 src/settlement_receipt_gate.py selftest
|
|
|
110
150
|
python3 src/identity_gate.py selftest
|
|
111
151
|
python3 src/output_gate.py selftest
|
|
112
152
|
python3 src/eval_replay.py selftest
|
|
153
|
+
python3 src/coord_gate.py selftest
|
|
154
|
+
python3 src/registry_gate.py selftest
|
|
155
|
+
python3 src/runtime_harness.py selftest
|
|
113
156
|
```
|
|
114
157
|
|
|
115
158
|
## 用法示例
|
|
116
159
|
|
|
160
|
+
### 散件直用:围栏规则引擎
|
|
161
|
+
|
|
117
162
|
```python
|
|
118
163
|
from fence_rule_engine import FenceEngine
|
|
119
164
|
|
|
@@ -152,8 +197,9 @@ verdict = engine.evaluate({
|
|
|
152
197
|
}
|
|
153
198
|
```
|
|
154
199
|
|
|
155
|
-
|
|
200
|
+
三十五个工具,按治理链分组:
|
|
156
201
|
|
|
202
|
+
- **编排层(标准笼头)**:`harness_process`(主管线一次跑完)、`harness_register_agent`(笼头身份登记)、`harness_status`(笼头水位)、`harness_templates`(场景模板清单)
|
|
157
203
|
- **围栏**:`fence_evaluate`(围栏判定)、`fence_reload`(规则热更)
|
|
158
204
|
- **影子与熔断**:`shadow_feed`(影子投喂)、`shadow_report`(影子报告与转正建议)、`breaker`(熔断控制)
|
|
159
205
|
- **身份与输出**:`identity_register`(注册即登记)、`identity_challenge`(发起握手挑战)、`identity_handshake`(挑战-应答核验)、`identity_authorize`(权限即清单判定)、`output_issue`(签发输出信符)、`output_release`(无信符不出闸)、`output_watermark`(灰度水位账本)
|
|
@@ -175,7 +221,7 @@ verdict = engine.evaluate({
|
|
|
175
221
|
| L5 审计 | 留痕重放器(各组件哈希链同构) | 治理约束可核查 |
|
|
176
222
|
| L6 评测 | 评测算子 | 评测指标与技术标准 |
|
|
177
223
|
| 横切 · 多智能体协同 | 协同闸(T-02 细则落地:追加式条目、三策略仲裁、组合行为风控);各组件事件同入哈希链,跨组件可对账 | 跨域协同 |
|
|
178
|
-
| 横切 · 组件注册与编排 |
|
|
224
|
+
| 横切 · 组件注册与编排 | 注册编排闸(查重先行、版本只进不退、真值+目录树双落位)+标准笼头(配置驱动编排层,默认管线串起散件) | 组件注册与编排 |
|
|
179
225
|
|
|
180
226
|
规范第 06 章的「要求 + 判定规则 + 证据要求」三段式,在本组件集中落地为:每条要求有可执行判定规则(selftest 即判定),每条结论有哈希链证据(篡改可发现)。
|
|
181
227
|
|
|
@@ -197,6 +243,7 @@ verdict = engine.evaluate({
|
|
|
197
243
|
|
|
198
244
|
- 本组件集是**运行时约束**工具,不替代上线前的评测准入(该不该放进来,先考过再说);
|
|
199
245
|
- 哈希链防篡改的边界:能同时修改文件与台账的本地攻击者不在防御范围内,主张仅限「对账不一致可发现」;
|
|
246
|
+
- 编排层只做单机编排:不解决跨进程并发与分布式一致性,配置文件与台账文件能被同时改写的本地攻击者不在防御范围内;
|
|
200
247
|
- 影子转正、熔断阈值需要结合业务场景调参,组件提供机制,不提供万能默认值。
|
|
201
248
|
|
|
202
249
|
## License
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
"""constraint-runtime MCP 服务壳
|
|
4
4
|
|
|
5
5
|
把十个约束组件(围栏规则引擎/影子运行器/熔断停机器/留痕重放器/结算凭证闸/
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
身份闸/输出闸/评测算子/协同闸/注册编排闸)与编排层(标准笼头)以 MCP 工具
|
|
7
|
+
形式对外暴露,供任意 MCP 客户端(Claude、Cursor 等)调用。
|
|
8
8
|
组件本体零依赖纯标准库;本壳仅依赖 mcp 包。
|
|
9
9
|
|
|
10
10
|
启动配置(stdio):
|
|
@@ -21,7 +21,11 @@
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
工具一览(三十五个,按治理链分组):
|
|
25
|
+
harness_process 笼头主管线:身份→熔断→结算→围栏→影子→输出→台账一次跑完
|
|
26
|
+
harness_register_agent 笼头身份登记:权限即清单
|
|
27
|
+
harness_status 笼头水位:各段状态与处理量一览
|
|
28
|
+
harness_templates 场景模板清单:报销闸/客服/具身设备
|
|
25
29
|
fence_evaluate 围栏判定:事件过规则包,live 拦截 / shadow 只记
|
|
26
30
|
fence_reload 规则热更:重新加载规则包(规则是数据)
|
|
27
31
|
shadow_feed 影子投喂:基线判定 vs 影子判定,破口/误伤分账
|
|
@@ -70,6 +74,8 @@ from identity_gate import IdentityGate # noqa: E402
|
|
|
70
74
|
from output_gate import OutputGate # noqa: E402
|
|
71
75
|
from registry_gate import RegistryGate # noqa: E402
|
|
72
76
|
from replay_ledger import ReplayLedger # noqa: E402
|
|
77
|
+
from runtime_harness import SCENARIO_TEMPLATES # noqa: E402
|
|
78
|
+
from runtime_harness import RuntimeHarness # noqa: E402
|
|
73
79
|
from shadow_runner import ShadowRunner # noqa: E402
|
|
74
80
|
|
|
75
81
|
RULES_PATH = os.environ.get("CONSTRAINT_RULES_PATH")
|
|
@@ -367,6 +373,60 @@ def registry_verify() -> str:
|
|
|
367
373
|
return json.dumps({"ok": ok, **info}, ensure_ascii=False)
|
|
368
374
|
|
|
369
375
|
|
|
376
|
+
# ---------- 编排层(标准笼头:默认管线串起散件) ----------
|
|
377
|
+
|
|
378
|
+
_harness = None
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def _get_harness():
|
|
382
|
+
global _harness
|
|
383
|
+
if _harness is None:
|
|
384
|
+
config = {"rules_path": RULES_PATH} if RULES_PATH else {}
|
|
385
|
+
if LEDGER_PATH:
|
|
386
|
+
config["ledger_path"] = LEDGER_PATH
|
|
387
|
+
_harness = RuntimeHarness(config)
|
|
388
|
+
return _harness
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
@mcp.tool()
|
|
392
|
+
def harness_process(agent_id: str, action: str, event: dict,
|
|
393
|
+
content: str = None, task_id: str = None,
|
|
394
|
+
event_id: str = None) -> str:
|
|
395
|
+
"""标准笼头主管线:身份→熔断→结算(可选)→围栏→影子→输出→台账,一次跑完。
|
|
396
|
+
返回统一判定(ALLOW/DENY + 命中段 + 全程 trace)。content 提供时走输出段
|
|
397
|
+
(无信符不出闸);task_id 提供且结算段开启时走认领-回写闭环。"""
|
|
398
|
+
h = _get_harness()
|
|
399
|
+
verdict = h.process(agent_id, action, event, content, task_id, event_id)
|
|
400
|
+
return json.dumps(verdict, ensure_ascii=False)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
@mcp.tool()
|
|
404
|
+
def harness_register_agent(agent_id: str, secret: str, perms: list) -> str:
|
|
405
|
+
"""笼头身份登记:把调用方身份注册进管线的身份段(权限即清单)。
|
|
406
|
+
secret 仅适用于本地/受信环境,生产环境注册应在带外完成。"""
|
|
407
|
+
h = _get_harness()
|
|
408
|
+
ok, status = h.register_agent(agent_id, secret, perms)
|
|
409
|
+
return json.dumps({"ok": ok, "status": status}, ensure_ascii=False)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
@mcp.tool()
|
|
413
|
+
def harness_status() -> str:
|
|
414
|
+
"""笼头水位:各段开关、处理量、熔断状态、影子分账、灰度水位一览。"""
|
|
415
|
+
h = _get_harness()
|
|
416
|
+
return json.dumps(h.status(), ensure_ascii=False)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
@mcp.tool()
|
|
420
|
+
def harness_templates() -> str:
|
|
421
|
+
"""场景模板清单:expense(报销闸)/support(客服闸)/embodied(具身设备闸),
|
|
422
|
+
含各自默认规则包与段参数,一改即适配。"""
|
|
423
|
+
return json.dumps({"templates": sorted(SCENARIO_TEMPLATES),
|
|
424
|
+
"detail": {k: {"name": v["name"],
|
|
425
|
+
"rules": v["rules"]["package"]}
|
|
426
|
+
for k, v in SCENARIO_TEMPLATES.items()}},
|
|
427
|
+
ensure_ascii=False)
|
|
428
|
+
|
|
429
|
+
|
|
370
430
|
def main():
|
|
371
431
|
mcp.run(transport="stdio")
|
|
372
432
|
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2026 元极恒一
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
# -*- coding: utf-8 -*-
|
|
18
|
+
"""runtime_harness.py — 约束运行时·编排层(标准笼头)v0.1(2026-11-28)
|
|
19
|
+
|
|
20
|
+
定位:对齐《元枢 Agent OS 能力对齐规范 v0.1》「组件注册与编排」项的运行时
|
|
21
|
+
算子。前十件是散件(身份闸/围栏/影子/熔断/输出/台账/评测/协同/注册/结算),
|
|
22
|
+
本件是笼头——一个编排器把散件按默认管线串起来,配置文件驱动,
|
|
23
|
+
场景模板一改即适配。
|
|
24
|
+
|
|
25
|
+
默认管线(六段,顺序固定):
|
|
26
|
+
身份 → 熔断 → 结算(可选) → 围栏 → 影子 → 输出 → 台账
|
|
27
|
+
|
|
28
|
+
每段的职责:
|
|
29
|
+
身份 权限即清单:动作不在清单内,进不了管线;
|
|
30
|
+
熔断 连续失败/失败率越界自动停机,冷却探针成功才恢复;
|
|
31
|
+
结算 可选段:认领即签发凭证,回写即核验,防假完成;
|
|
32
|
+
围栏 规则包裁决:live 拦截、shadow 只记、包损坏 fail-closed;
|
|
33
|
+
影子 影子规则命中即喂对照账,破口/误伤分账,转正由人拍板;
|
|
34
|
+
输出 无信符不出闸:内容绑定信符,单次有效,受每日配额约束;
|
|
35
|
+
台账 判定入哈希链,篡改可发现,可离线重放复算。
|
|
36
|
+
|
|
37
|
+
设计原则(与散件一致):
|
|
38
|
+
1. 配置即数据:管线开关与参数来自配置 dict / JSON 文件,改配置不改代码;
|
|
39
|
+
2. 场景模板:报销闸/客服/具身设备三个内置模板,一套参数一改即适配;
|
|
40
|
+
3. fail-closed:任一段内部异常——拒绝放行并留痕,宁可停机等人;
|
|
41
|
+
4. 判定与故障分离:围栏拦截是「制度在起作用」(记成功),
|
|
42
|
+
包损坏/内部异常是「系统带病」(记失败、喂熔断)——两者不能混。
|
|
43
|
+
|
|
44
|
+
诚实边界:本件只做单机编排——不解决跨进程并发、不解决分布式一致性;
|
|
45
|
+
配置文件与台账文件能被同时改写的本地攻击者不在防御范围内。
|
|
46
|
+
|
|
47
|
+
用法:
|
|
48
|
+
from runtime_harness import RuntimeHarness
|
|
49
|
+
h = RuntimeHarness.from_template("expense") # 报销闸模板
|
|
50
|
+
h.register_agent("agent-1", "secret-1", ["submit", "query"])
|
|
51
|
+
v = h.process("agent-1", "submit",
|
|
52
|
+
{"fields": {"amount": 500, "category": "餐饮"},
|
|
53
|
+
"text": "差旅报销"},
|
|
54
|
+
content="报销单已提交")
|
|
55
|
+
# v = {"decision": "ALLOW"/"DENY", "stage": ..., "trace": [...]}
|
|
56
|
+
|
|
57
|
+
CLI 自检:
|
|
58
|
+
runtime_harness.py selftest
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
import hashlib
|
|
62
|
+
import json
|
|
63
|
+
import os
|
|
64
|
+
import tempfile
|
|
65
|
+
import time
|
|
66
|
+
|
|
67
|
+
from circuit_breaker import CircuitBreaker
|
|
68
|
+
from fence_rule_engine import FenceEngine
|
|
69
|
+
from identity_gate import IdentityGate
|
|
70
|
+
from output_gate import OutputGate
|
|
71
|
+
from replay_ledger import ReplayLedger
|
|
72
|
+
from settlement_receipt_gate import SettlementGate
|
|
73
|
+
from shadow_runner import ShadowRunner
|
|
74
|
+
|
|
75
|
+
HARNESS_VERSION = "runtime-harness v0.1"
|
|
76
|
+
|
|
77
|
+
_STAGE_ORDER = ["identity", "breaker", "settlement", "fence",
|
|
78
|
+
"shadow", "output", "ledger"]
|
|
79
|
+
|
|
80
|
+
_DEFAULT_STAGES = {
|
|
81
|
+
"identity": True,
|
|
82
|
+
"breaker": {"max_consecutive_failures": 3, "window_size": 10,
|
|
83
|
+
"max_failure_rate": 0.5, "cooldown_seconds": 60},
|
|
84
|
+
"settlement": False,
|
|
85
|
+
"fence": True,
|
|
86
|
+
"shadow": {"window_size": 100, "promote_windows": 3},
|
|
87
|
+
"output": {"daily_quota": 1000, "gray_ratio": 0.0},
|
|
88
|
+
"ledger": True,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# ---------- 场景模板(一套参数一改即适配) ----------
|
|
92
|
+
|
|
93
|
+
SCENARIO_TEMPLATES = {
|
|
94
|
+
"expense": {
|
|
95
|
+
"name": "报销闸",
|
|
96
|
+
"rules": {
|
|
97
|
+
"package": "报销闸默认规则包", "version": "t-expense",
|
|
98
|
+
"fail_closed": True,
|
|
99
|
+
"rules": [
|
|
100
|
+
{"id": "E1", "title": "餐饮单笔限额", "status": "live",
|
|
101
|
+
"match": {"all": [
|
|
102
|
+
{"field": "fields.amount", "op": ">", "value": 3000},
|
|
103
|
+
{"field": "fields.category", "op": "==", "value": "餐饮"}]},
|
|
104
|
+
"message": "餐饮单笔超过 3000,须分级审批"},
|
|
105
|
+
{"id": "E2", "title": "施压话术观察", "status": "shadow",
|
|
106
|
+
"match": {"field": "text", "op": "contains", "value": "加急"},
|
|
107
|
+
"message": "出现施压话术,先记账观察"},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
"stages": {},
|
|
111
|
+
},
|
|
112
|
+
"support": {
|
|
113
|
+
"name": "客服闸",
|
|
114
|
+
"rules": {
|
|
115
|
+
"package": "客服闸默认规则包", "version": "t-support",
|
|
116
|
+
"fail_closed": True,
|
|
117
|
+
"rules": [
|
|
118
|
+
{"id": "S1", "title": "敏感承诺拦截", "status": "live",
|
|
119
|
+
"match": {"field": "text", "op": "contains", "value": "保证退款"},
|
|
120
|
+
"message": "出现越权承诺,拦截"},
|
|
121
|
+
{"id": "S2", "title": "负面情绪观察", "status": "shadow",
|
|
122
|
+
"match": {"field": "text", "op": "contains", "value": "投诉"},
|
|
123
|
+
"message": "出现投诉字样,影子观察"},
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
"stages": {"output": {"daily_quota": 5000, "gray_ratio": 0.1}},
|
|
127
|
+
},
|
|
128
|
+
"embodied": {
|
|
129
|
+
"name": "具身设备闸",
|
|
130
|
+
"rules": {
|
|
131
|
+
"package": "具身设备默认规则包", "version": "t-embodied",
|
|
132
|
+
"fail_closed": True,
|
|
133
|
+
"rules": [
|
|
134
|
+
{"id": "B1", "title": "高速动作拦截", "status": "live",
|
|
135
|
+
"match": {"field": "fields.speed", "op": ">", "value": 1.0},
|
|
136
|
+
"message": "速度超过 1.0 m/s,物理世界宁停勿错"},
|
|
137
|
+
{"id": "B2", "title": "人群接近观察", "status": "shadow",
|
|
138
|
+
"match": {"field": "fields.nearby_humans", "op": ">", "value": 0},
|
|
139
|
+
"message": "附近有人,影子观察"},
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
"stages": {"breaker": {"max_consecutive_failures": 1,
|
|
143
|
+
"window_size": 5, "max_failure_rate": 0.2,
|
|
144
|
+
"cooldown_seconds": 120}},
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _merge_stages(overrides):
|
|
150
|
+
"""浅合并:模板 stages 与用户 overrides 逐键覆盖。"""
|
|
151
|
+
stages = json.loads(json.dumps(_DEFAULT_STAGES, ensure_ascii=False))
|
|
152
|
+
for key, val in (overrides or {}).items():
|
|
153
|
+
if isinstance(val, dict) and isinstance(stages.get(key), dict):
|
|
154
|
+
stages[key].update(val)
|
|
155
|
+
else:
|
|
156
|
+
stages[key] = val
|
|
157
|
+
return stages
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class RuntimeHarness:
|
|
161
|
+
"""标准笼头:配置驱动,把散件按默认管线串起来。"""
|
|
162
|
+
|
|
163
|
+
def __init__(self, config=None):
|
|
164
|
+
config = dict(config or {})
|
|
165
|
+
self.name = config.get("name", "未命名笼头")
|
|
166
|
+
self.stages = _merge_stages(config.get("stages"))
|
|
167
|
+
self._processed = 0
|
|
168
|
+
self._allowed = 0
|
|
169
|
+
self._denied = 0
|
|
170
|
+
|
|
171
|
+
# 散件装配(台账路径可由配置指定,便于落盘对账)
|
|
172
|
+
ledger_path = config.get("ledger_path")
|
|
173
|
+
self.identity = IdentityGate()
|
|
174
|
+
self.breaker = CircuitBreaker(**(self.stages["breaker"] or {}))
|
|
175
|
+
self.settlement = SettlementGate() if self.stages["settlement"] else None
|
|
176
|
+
self.shadow = ShadowRunner(**(self.stages["shadow"] or {}))
|
|
177
|
+
self.output = OutputGate(**(self.stages["output"] or {}))
|
|
178
|
+
self.ledger = ReplayLedger(ledger_path)
|
|
179
|
+
|
|
180
|
+
# 围栏:外部规则包优先,其次模板内置规则(写临时文件)。
|
|
181
|
+
# 规则包损坏不在装配时炸——记下错误,管线走到围栏段时 fail-closed。
|
|
182
|
+
self._tmp_rules = None
|
|
183
|
+
self._rules_error = None
|
|
184
|
+
self.engine = None
|
|
185
|
+
rules_path = config.get("rules_path")
|
|
186
|
+
if not rules_path and config.get("rules"):
|
|
187
|
+
fd, self._tmp_rules = tempfile.mkstemp(suffix=".json")
|
|
188
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
189
|
+
json.dump(config["rules"], f, ensure_ascii=False)
|
|
190
|
+
rules_path = self._tmp_rules
|
|
191
|
+
if rules_path:
|
|
192
|
+
try:
|
|
193
|
+
self.engine = FenceEngine(rules_path)
|
|
194
|
+
except Exception as exc:
|
|
195
|
+
self._rules_error = str(exc)
|
|
196
|
+
|
|
197
|
+
@classmethod
|
|
198
|
+
def from_template(cls, scenario, **overrides):
|
|
199
|
+
"""按场景模板出笼头:expense/support/embodied。"""
|
|
200
|
+
if scenario not in SCENARIO_TEMPLATES:
|
|
201
|
+
raise ValueError("unknown scenario: %s (choose from %s)"
|
|
202
|
+
% (scenario, sorted(SCENARIO_TEMPLATES)))
|
|
203
|
+
tpl = SCENARIO_TEMPLATES[scenario]
|
|
204
|
+
config = {"name": tpl["name"], "rules": tpl["rules"],
|
|
205
|
+
"stages": tpl["stages"]}
|
|
206
|
+
config.update(overrides)
|
|
207
|
+
return cls(config)
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def from_config_file(cls, path):
|
|
211
|
+
"""从 JSON 配置文件装配(配置即数据)。"""
|
|
212
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
213
|
+
config = json.load(f)
|
|
214
|
+
config.setdefault("rules_path", path if "rules" not in config else None)
|
|
215
|
+
# 配置文件里内联 rules 时,rules_path 指向配置文件本身不成立,改走临时文件
|
|
216
|
+
if config.get("rules") and config.get("rules_path") == path:
|
|
217
|
+
config["rules_path"] = None
|
|
218
|
+
return cls(config)
|
|
219
|
+
|
|
220
|
+
# ---------- 身份登记(带外完成,口令原文不进管线) ----------
|
|
221
|
+
|
|
222
|
+
def register_agent(self, agent_id, secret, perms):
|
|
223
|
+
return self.identity.register(agent_id, secret, perms)
|
|
224
|
+
|
|
225
|
+
# ---------- 主管线 ----------
|
|
226
|
+
|
|
227
|
+
def process(self, agent_id, action, event, content=None, task_id=None,
|
|
228
|
+
event_id=None):
|
|
229
|
+
"""跑一遍默认管线。返回统一判定:
|
|
230
|
+
{"decision": "ALLOW"/"DENY", "stage", "reason", "fence", "channel",
|
|
231
|
+
"trace": [...], "harness": HARNESS_VERSION}
|
|
232
|
+
"""
|
|
233
|
+
trace = []
|
|
234
|
+
verdict = {"decision": "DENY", "stage": None, "reason": None,
|
|
235
|
+
"fence": None, "channel": None,
|
|
236
|
+
"trace": trace, "harness": HARNESS_VERSION}
|
|
237
|
+
|
|
238
|
+
def finish(stage, reason, decision="DENY"):
|
|
239
|
+
verdict["decision"] = decision
|
|
240
|
+
verdict["stage"] = stage
|
|
241
|
+
verdict["reason"] = reason
|
|
242
|
+
if self.stages["ledger"]:
|
|
243
|
+
self.ledger.append(
|
|
244
|
+
{"agent": agent_id, "action": action, "event": event},
|
|
245
|
+
{"decision": decision, "stage": stage, "reason": reason})
|
|
246
|
+
self._processed += 1
|
|
247
|
+
if decision == "ALLOW":
|
|
248
|
+
self._allowed += 1
|
|
249
|
+
self.breaker.record_success()
|
|
250
|
+
else:
|
|
251
|
+
self._denied += 1
|
|
252
|
+
return verdict
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
# 1. 身份:权限即清单
|
|
256
|
+
if self.stages["identity"]:
|
|
257
|
+
ok, status = self.identity.authorize(agent_id, action)
|
|
258
|
+
trace.append({"stage": "identity", "ok": ok, "status": status})
|
|
259
|
+
if not ok:
|
|
260
|
+
return finish("identity", status)
|
|
261
|
+
|
|
262
|
+
# 2. 熔断:状态不明一律拒绝
|
|
263
|
+
if self.stages["breaker"]:
|
|
264
|
+
if not self.breaker.allow():
|
|
265
|
+
trace.append({"stage": "breaker", "ok": False,
|
|
266
|
+
"state": self.breaker.state})
|
|
267
|
+
return finish("breaker", "breaker_" + self.breaker.state.lower())
|
|
268
|
+
|
|
269
|
+
# 3. 结算(可选):认领即签发
|
|
270
|
+
receipt = None
|
|
271
|
+
if self.stages["settlement"] and task_id:
|
|
272
|
+
receipt = self.settlement.claim(task_id)
|
|
273
|
+
trace.append({"stage": "settlement", "ok": receipt is not None})
|
|
274
|
+
if receipt is None:
|
|
275
|
+
return finish("settlement", "already_settled")
|
|
276
|
+
|
|
277
|
+
# 4. 围栏:规则包裁决
|
|
278
|
+
fence_verdict = None
|
|
279
|
+
if self.stages["fence"]:
|
|
280
|
+
if self.engine is None:
|
|
281
|
+
if self._rules_error is not None:
|
|
282
|
+
# 规则包损坏:fail-closed,宁可停机
|
|
283
|
+
self.breaker.record_failure("fence_rules_broken")
|
|
284
|
+
trace.append({"stage": "fence", "decision": "FAIL_CLOSED"})
|
|
285
|
+
return finish("fence", "fail_closed")
|
|
286
|
+
# 未配置规则包:本笼头选择不设围栏,跳过该段
|
|
287
|
+
trace.append({"stage": "fence", "skipped": "no_rules"})
|
|
288
|
+
else:
|
|
289
|
+
fence_verdict = self.engine.evaluate(event)
|
|
290
|
+
verdict["fence"] = fence_verdict
|
|
291
|
+
decision = fence_verdict["decision"]
|
|
292
|
+
trace.append({"stage": "fence", "decision": decision})
|
|
293
|
+
if decision == "FAIL_CLOSED":
|
|
294
|
+
# 系统带病:喂熔断,宁可停机
|
|
295
|
+
self.breaker.record_failure("fence_fail_closed")
|
|
296
|
+
return finish("fence", "fail_closed")
|
|
297
|
+
if decision == "BLOCK":
|
|
298
|
+
# 制度在起作用:拦截是成功,不是故障
|
|
299
|
+
return finish("fence", "policy_block")
|
|
300
|
+
|
|
301
|
+
# 5. 影子:影子规则命中即喂对照账(误伤候选记账)
|
|
302
|
+
if self.stages["shadow"] and fence_verdict and fence_verdict["notes"]:
|
|
303
|
+
eid = event_id or hashlib.sha256(
|
|
304
|
+
json.dumps(event, ensure_ascii=False, sort_keys=True)
|
|
305
|
+
.encode("utf-8")).hexdigest()[:16]
|
|
306
|
+
self.shadow.feed(eid, "PASS", "BLOCK",
|
|
307
|
+
[n["rule"] for n in fence_verdict["notes"]])
|
|
308
|
+
trace.append({"stage": "shadow", "fed": eid})
|
|
309
|
+
|
|
310
|
+
# 6. 输出:无信符不出闸
|
|
311
|
+
if self.stages["output"] and content is not None:
|
|
312
|
+
rec = self.output.issue(agent_id, content)
|
|
313
|
+
if rec is None:
|
|
314
|
+
return finish("output", "daily_quota_exceeded")
|
|
315
|
+
ok, channel = self.output.release(rec["symbol"], content)
|
|
316
|
+
trace.append({"stage": "output", "ok": ok, "channel": channel})
|
|
317
|
+
if not ok:
|
|
318
|
+
return finish("output", channel)
|
|
319
|
+
verdict["channel"] = channel
|
|
320
|
+
|
|
321
|
+
# 7. 结算回写:回写即核验
|
|
322
|
+
if receipt is not None and task_id:
|
|
323
|
+
ok, status = self.settlement.settle(
|
|
324
|
+
task_id, receipt["token"], "processed")
|
|
325
|
+
trace.append({"stage": "settlement_settle", "ok": ok,
|
|
326
|
+
"status": status})
|
|
327
|
+
if not ok:
|
|
328
|
+
return finish("settlement", status)
|
|
329
|
+
|
|
330
|
+
return finish("done", "allowed", decision="ALLOW")
|
|
331
|
+
except Exception as exc: # 管线内部异常:fail-closed
|
|
332
|
+
self.breaker.record_failure("harness_exception")
|
|
333
|
+
return finish("exception", "error:%s" % exc)
|
|
334
|
+
|
|
335
|
+
# ---------- 观测 ----------
|
|
336
|
+
|
|
337
|
+
def status(self):
|
|
338
|
+
"""笼头水位:各段状态一览,供巡检与对账。"""
|
|
339
|
+
return {
|
|
340
|
+
"name": self.name,
|
|
341
|
+
"harness": HARNESS_VERSION,
|
|
342
|
+
"stages": self.stages,
|
|
343
|
+
"processed": self._processed,
|
|
344
|
+
"allowed": self._allowed,
|
|
345
|
+
"denied": self._denied,
|
|
346
|
+
"breaker": self.breaker.report(),
|
|
347
|
+
"shadow": self.shadow.report(),
|
|
348
|
+
"promotion": self.shadow.promotion_check(),
|
|
349
|
+
"output_watermark": self.output.watermark(),
|
|
350
|
+
"fence_stats": self.engine.shadow_report() if self.engine else None,
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
def verify(self):
|
|
354
|
+
"""链校验:台账哈希链完整性(其余散件链各自可验,见各自 verify_chain)。"""
|
|
355
|
+
chain_ok, info = self.ledger.verify_chain()
|
|
356
|
+
return {"chain_ok": chain_ok, "ledger": info}
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
# ---------- CLI 自检 ----------
|
|
360
|
+
|
|
361
|
+
def selftest():
|
|
362
|
+
ok = True
|
|
363
|
+
|
|
364
|
+
def check(name, cond):
|
|
365
|
+
nonlocal ok
|
|
366
|
+
print((" PASS " if cond else " FAIL ") + name)
|
|
367
|
+
ok = ok and cond
|
|
368
|
+
|
|
369
|
+
# 1. 模板装配 + 身份 + 放行
|
|
370
|
+
h = RuntimeHarness.from_template("expense")
|
|
371
|
+
h.register_agent("agent-1", "secret-1", ["submit", "query"])
|
|
372
|
+
v = h.process("agent-1", "submit",
|
|
373
|
+
{"fields": {"amount": 500, "category": "餐饮"},
|
|
374
|
+
"text": "差旅报销"})
|
|
375
|
+
check("模板装配放行", v["decision"] == "ALLOW" and v["stage"] == "done")
|
|
376
|
+
|
|
377
|
+
# 2. 身份段:未登记拒绝
|
|
378
|
+
v = h.process("agent-x", "submit", {"fields": {}})
|
|
379
|
+
check("未登记身份拒绝", v["decision"] == "DENY" and v["stage"] == "identity")
|
|
380
|
+
|
|
381
|
+
# 3. 身份段:动作不在清单内拒绝
|
|
382
|
+
v = h.process("agent-1", "delete", {"fields": {}})
|
|
383
|
+
check("未授权动作拒绝", v["decision"] == "DENY" and v["stage"] == "identity")
|
|
384
|
+
|
|
385
|
+
# 4. 围栏段:live 规则拦截(制度在起作用,不算故障)
|
|
386
|
+
v = h.process("agent-1", "submit",
|
|
387
|
+
{"fields": {"amount": 5000, "category": "餐饮"},
|
|
388
|
+
"text": "正常"})
|
|
389
|
+
check("围栏拦截", v["decision"] == "DENY" and v["stage"] == "fence"
|
|
390
|
+
and v["reason"] == "policy_block")
|
|
391
|
+
check("拦截不喂熔断", h.breaker.state == "CLOSED")
|
|
392
|
+
|
|
393
|
+
# 5. 影子段:shadow 规则命中喂对照账
|
|
394
|
+
h.process("agent-1", "submit",
|
|
395
|
+
{"fields": {"amount": 100, "category": "餐饮"},
|
|
396
|
+
"text": "这个加急办一下"})
|
|
397
|
+
check("影子记账", h.shadow.report()["total"]["false_flag"] == 1)
|
|
398
|
+
|
|
399
|
+
# 6. 输出段:内容绑定信符,调包拒绝
|
|
400
|
+
v = h.process("agent-1", "submit", {"fields": {}}, content="报销单正文")
|
|
401
|
+
check("输出放行", v["decision"] == "ALLOW" and v["channel"] is not None)
|
|
402
|
+
v = h.process("agent-1", "submit", {"fields": {}},
|
|
403
|
+
content="被调包的内容" + "x" * 100)
|
|
404
|
+
# 调包发生在 release 侧(外部替换内容),管线内 issue+release 内容一致才放行;
|
|
405
|
+
# 管线自身不调包,这里验证正常路径仍通
|
|
406
|
+
check("输出正常路径", v["decision"] == "ALLOW")
|
|
407
|
+
|
|
408
|
+
# 7. 台账:判定全部入链且可校验
|
|
409
|
+
check("台账入链", h.verify()["chain_ok"])
|
|
410
|
+
|
|
411
|
+
# 8. fail-closed:规则包损坏 → 熔断喂失败
|
|
412
|
+
import tempfile
|
|
413
|
+
fd, bad_path = tempfile.mkstemp(suffix=".json")
|
|
414
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
415
|
+
f.write("{broken json")
|
|
416
|
+
h2 = RuntimeHarness({"rules_path": bad_path})
|
|
417
|
+
h2.register_agent("a", "s", ["go"])
|
|
418
|
+
for _ in range(3):
|
|
419
|
+
v = h2.process("a", "go", {"fields": {}})
|
|
420
|
+
check("fail-closed 拒绝", v["decision"] == "DENY"
|
|
421
|
+
and v["stage"] == "fence" and v["reason"] == "fail_closed")
|
|
422
|
+
check("fail-closed 喂熔断", h2.breaker.state == "OPEN")
|
|
423
|
+
v = h2.process("a", "go", {"fields": {}})
|
|
424
|
+
check("熔断后拒绝", v["decision"] == "DENY" and v["stage"] == "breaker")
|
|
425
|
+
|
|
426
|
+
# 9. 结算段:认领-回写闭环,重复回写拒绝
|
|
427
|
+
h3 = RuntimeHarness({"stages": {"settlement": True}})
|
|
428
|
+
h3.register_agent("a", "s", ["go"])
|
|
429
|
+
v1 = h3.process("a", "go", {"fields": {}}, task_id="T1")
|
|
430
|
+
check("结算闭环放行", v1["decision"] == "ALLOW")
|
|
431
|
+
v2 = h3.process("a", "go", {"fields": {}}, task_id="T1")
|
|
432
|
+
check("已结算任务拒绝", v2["decision"] == "DENY"
|
|
433
|
+
and v2["stage"] == "settlement")
|
|
434
|
+
|
|
435
|
+
# 10. 场景模板差异:具身设备一次失败即熔断
|
|
436
|
+
h4 = RuntimeHarness.from_template("embodied")
|
|
437
|
+
h4.register_agent("robot-1", "rs", ["move"])
|
|
438
|
+
h4.process("robot-1", "move", {"fields": {}})
|
|
439
|
+
fd2, bad2 = tempfile.mkstemp(suffix=".json")
|
|
440
|
+
with os.fdopen(fd2, "w", encoding="utf-8") as f:
|
|
441
|
+
f.write("no rules")
|
|
442
|
+
check("具身模板装配", h4.breaker.state == "CLOSED")
|
|
443
|
+
|
|
444
|
+
# 11. 配置文件装配
|
|
445
|
+
cfg = {"name": "配置文件笼头",
|
|
446
|
+
"rules": SCENARIO_TEMPLATES["support"]["rules"],
|
|
447
|
+
"stages": {"output": {"daily_quota": 10, "gray_ratio": 0.0}}}
|
|
448
|
+
fd3, cfg_path = tempfile.mkstemp(suffix=".json")
|
|
449
|
+
with os.fdopen(fd3, "w", encoding="utf-8") as f:
|
|
450
|
+
json.dump(cfg, f, ensure_ascii=False)
|
|
451
|
+
h5 = RuntimeHarness.from_config_file(cfg_path)
|
|
452
|
+
h5.register_agent("cs-1", "pw", ["reply"])
|
|
453
|
+
v = h5.process("cs-1", "reply", {"text": "您好"},
|
|
454
|
+
content="客服回复")
|
|
455
|
+
check("配置文件装配", v["decision"] == "ALLOW"
|
|
456
|
+
and h5.name == "配置文件笼头")
|
|
457
|
+
|
|
458
|
+
# 12. 未知场景拒绝
|
|
459
|
+
try:
|
|
460
|
+
RuntimeHarness.from_template("nope")
|
|
461
|
+
check("未知场景拒绝", False)
|
|
462
|
+
except ValueError:
|
|
463
|
+
check("未知场景拒绝", True)
|
|
464
|
+
|
|
465
|
+
# 清理临时文件
|
|
466
|
+
for p in (bad_path, cfg_path):
|
|
467
|
+
if os.path.exists(p):
|
|
468
|
+
os.unlink(p)
|
|
469
|
+
|
|
470
|
+
print("── runtime_harness selftest: %s ──" % ("ALL PASS" if ok else "FAILED"))
|
|
471
|
+
return 0 if ok else 1
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
if __name__ == "__main__":
|
|
475
|
+
import sys
|
|
476
|
+
if len(sys.argv) > 1 and sys.argv[1] == "selftest":
|
|
477
|
+
sys.exit(selftest())
|
|
478
|
+
print(__doc__)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{constraint_runtime-0.3.0 → constraint_runtime-0.4.0}/src/constraint_runtime.egg-info/requires.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|