guanshu 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 (47) hide show
  1. guanshu-0.1.0/.gitignore +15 -0
  2. guanshu-0.1.0/LICENSE +201 -0
  3. guanshu-0.1.0/NOTICE +31 -0
  4. guanshu-0.1.0/PKG-INFO +140 -0
  5. guanshu-0.1.0/README.md +121 -0
  6. guanshu-0.1.0/docs/PRD-MVP.md +176 -0
  7. guanshu-0.1.0/docs/ROADMAP.md +83 -0
  8. guanshu-0.1.0/examples/claude-code-settings.example.json +16 -0
  9. guanshu-0.1.0/examples/claude_code_pretooluse_hook.py +99 -0
  10. guanshu-0.1.0/examples/hooktest/.claude/settings.json +15 -0
  11. guanshu-0.1.0/examples/hooktest/README.md +33 -0
  12. guanshu-0.1.0/examples/relay_poisoning_demo.py +14 -0
  13. guanshu-0.1.0/plugins/guanshu-enterprise-demo/pyproject.toml +21 -0
  14. guanshu-0.1.0/plugins/guanshu-enterprise-demo/src/guanshu_enterprise_demo/__init__.py +8 -0
  15. guanshu-0.1.0/plugins/guanshu-enterprise-demo/src/guanshu_enterprise_demo/mapper.py +59 -0
  16. guanshu-0.1.0/plugins/guanshu-enterprise-demo/src/guanshu_enterprise_demo/signer.py +20 -0
  17. guanshu-0.1.0/policies/baidu-only.yaml +23 -0
  18. guanshu-0.1.0/policies/egress-allowlist.yaml +20 -0
  19. guanshu-0.1.0/policies/relay-guard.yaml +29 -0
  20. guanshu-0.1.0/pyproject.toml +32 -0
  21. guanshu-0.1.0/src/guanshu/__init__.py +28 -0
  22. guanshu-0.1.0/src/guanshu/_demo.py +89 -0
  23. guanshu-0.1.0/src/guanshu/_packages.py +58 -0
  24. guanshu-0.1.0/src/guanshu/audit.py +77 -0
  25. guanshu-0.1.0/src/guanshu/cli.py +264 -0
  26. guanshu-0.1.0/src/guanshu/data/npm_top.txt +11 -0
  27. guanshu-0.1.0/src/guanshu/data/pypi_top.txt +22 -0
  28. guanshu-0.1.0/src/guanshu/interceptor.py +100 -0
  29. guanshu-0.1.0/src/guanshu/models.py +74 -0
  30. guanshu-0.1.0/src/guanshu/plugins.py +56 -0
  31. guanshu-0.1.0/src/guanshu/policy.py +104 -0
  32. guanshu-0.1.0/src/guanshu/proxy.py +183 -0
  33. guanshu-0.1.0/src/guanshu/receipt.py +70 -0
  34. guanshu-0.1.0/src/guanshu/screening.py +249 -0
  35. guanshu-0.1.0/src/guanshu/selftest.py +67 -0
  36. guanshu-0.1.0/src/guanshu/spi.py +72 -0
  37. guanshu-0.1.0/tests/test_audit.py +44 -0
  38. guanshu-0.1.0/tests/test_audit_tamper.py +239 -0
  39. guanshu-0.1.0/tests/test_egress.py +114 -0
  40. guanshu-0.1.0/tests/test_features_v01.py +160 -0
  41. guanshu-0.1.0/tests/test_interceptor.py +313 -0
  42. guanshu-0.1.0/tests/test_plugins.py +98 -0
  43. guanshu-0.1.0/tests/test_policy.py +280 -0
  44. guanshu-0.1.0/tests/test_proxy.py +71 -0
  45. guanshu-0.1.0/tests/test_rulepack.py +43 -0
  46. guanshu-0.1.0/tests/test_screen.py +25 -0
  47. guanshu-0.1.0/tests/test_screen_corpus.py +375 -0
@@ -0,0 +1,15 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ .eggs/
5
+ build/
6
+ dist/
7
+ .venv/
8
+ venv/
9
+ .env
10
+ *.jsonl
11
+ !policies/*.yaml
12
+ .pytest_cache/
13
+ .mypy_cache/
14
+ .ruff_cache/
15
+ .DS_Store
guanshu-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
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 Derivative
95
+ 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
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 guanshu contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
guanshu-0.1.0/NOTICE ADDED
@@ -0,0 +1,31 @@
1
+ guanshu (关枢)
2
+ Copyright (c) 2026 guanshu contributors
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
+
6
+ ------------------------------------------------------------------------
7
+ Clean-room statement & attributions
8
+ ------------------------------------------------------------------------
9
+
10
+ guanshu is a CLEAN-ROOM implementation. It contains NO source code copied from
11
+ any third-party project. Some designs and detection rules were INSPIRED BY public
12
+ materials and reimplemented independently in Python:
13
+
14
+ - Microsoft Agent Governance Toolkit (AGT), MIT-licensed.
15
+ Concepts that influenced guanshu's design (independently reimplemented, no source
16
+ copied): the tamper-evident hash-chain audit pattern, the entry-points / SPI
17
+ open-core extension mechanism, and the deterministic "screen -> evaluate ->
18
+ audit" governance flow. "Agent Governance Toolkit" and any AGT marks/logos are
19
+ trademarks of Microsoft Corporation and are NOT used by this project; guanshu is
20
+ not affiliated with or endorsed by Microsoft.
21
+
22
+ - Relay-poisoning detection rules (slopsquat, secret exposure, malicious URL,
23
+ dangerous code) were reimplemented from public security research, including:
24
+ UCSB "Your Agent Is Mine" (arXiv 2604.08407) and
25
+ CISPA "Real Money, Fake Models" (arXiv 2603.01919).
26
+
27
+ If, in the future, any third-party source code is incorporated, its original
28
+ license header and copyright will be retained and listed here.
29
+
30
+ No third-party hosted service is required or contacted at runtime. All screening
31
+ is local and deterministic.
guanshu-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,140 @@
1
+ Metadata-Version: 2.4
2
+ Name: guanshu
3
+ Version: 0.1.0
4
+ Summary: 关枢 · 确定性智能体动作治理 — 第一张脸:中转站投毒防护 (deterministic agent action governance; first face: LLM-relay poisoning protection)
5
+ Author: guanshu contributors
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ License-File: NOTICE
9
+ Keywords: agent,audit,governance,llm,mcp,relay,security,slopsquat
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Requires-Python: >=3.10
14
+ Requires-Dist: pydantic>=2
15
+ Requires-Dist: pyyaml>=6
16
+ Provides-Extra: dev
17
+ Requires-Dist: pytest>=7; extra == 'dev'
18
+ Description-Content-Type: text/markdown
19
+
20
+ # 关枢 / guanshu
21
+
22
+ > **确定性的智能体动作治理**:在 agent 每一次工具调用 / 消息 / 委派到达外部**之前**做策略裁决(默认拒绝),被拒动作不是"概率上不会发生",而是**结构上不可能发生**;每次裁决写入防篡改哈希链审计。
23
+ >
24
+ > *Deterministic agent action governance — every tool call is screened & policy-checked before it hits the wire. Fully local, offline, no third-party cloud.*
25
+ >
26
+ > ⚠️ `guanshu/关枢` 是**工作代号**,非最终品牌名。
27
+
28
+ ---
29
+
30
+ ## 第一张脸:中转站投毒防护
31
+
32
+ 中国开发者大面积用 LLM 中转站,而中转站会**掺水(模型掉包/降级)**与**投毒(注入恶意代码、外泄密钥)**(CISPA 实测 45.83% 指纹验证失败;UCSB 实测 428 站 9 个主动注入恶意代码)。
33
+
34
+ guanshu 在动作落地前做 **L3 输出侧防御**——纯本地、确定性、无需联网、无需 LLM:
35
+
36
+ - **slopsquat**:幻觉/仿冒包名(`pip install reqursts`)
37
+ - **secret_exposure**:密钥/私钥外泄(AKIA…、`sk-ant-…`、私钥)
38
+ - **dangerous_code**:`curl | sh`、`rm -rf /`、`base64 | sh`、`eval/exec`
39
+ - **suspicious_url**:非白名单外联域名 / IP 直连
40
+
41
+ 判定 `MALICIOUS` 即 **fail-closed 拦截**,无论策略是否放行;每次裁决产出**防篡改 provenance 回执**。
42
+
43
+ ---
44
+
45
+ ## 60 秒上手(本地代理 · 主形态)
46
+
47
+ 把 AI 工具/SDK 的 `base_url` 指向本机 guanshu 代理 —— 它在模型响应到达 agent **之前**筛掉中转站投毒(剔除恶意工具调用),再转发到你**自己的**上游。**数据只在本机、key 不碰我们、工具无关**(Cursor / Cherry Studio / 任何用 base_url 的 SDK 都覆盖)。
48
+
49
+ ```bash
50
+ pip install guanshu
51
+ guanshu init # 初始化 + 自动跑「首拦自测」:60 秒看它当场拦一次投毒(不用等真攻击)
52
+ guanshu proxy --upstream https://api.openai.com/v1 # 或换成你现在用的中转站 base_url
53
+ export OPENAI_BASE_URL=http://127.0.0.1:8788/v1 # 让你的工具指过来
54
+ ```
55
+
56
+ CLI:`guanshu selftest`(首拦自测)· `guanshu doctor`(自检)· `guanshu verify/receipt <audit.jsonl>` · `guanshu lint-policy <yaml>` · `guanshu plugins` · `guanshu report`
57
+
58
+ ### 补充形态 ① 库内嵌(`@govern`)
59
+
60
+ 不走代理也可在代码里直接给工具上门:
61
+
62
+ ```python
63
+ from guanshu import govern, Governor, PolicyEngine, HashChainJSONLSink, set_active, GovernanceDenied
64
+ set_active(Governor(PolicyEngine.from_yaml("policies/relay-guard.yaml"), audit=HashChainJSONLSink("audit.jsonl")))
65
+
66
+ @govern(tool="pip_install")
67
+ def pip_install(package: str) -> str: ...
68
+
69
+ pip_install(package="requests") # ✓ 放行 + 审计
70
+ pip_install(package="reqursts") # ✗ GovernanceDenied(slopsquat,fail-closed)
71
+ ```
72
+
73
+ ### 补充形态 ② Claude Code PreToolUse hook
74
+
75
+ `examples/claude_code_pretooluse_hook.py` 可作 Claude Code 的 PreToolUse hook(执行前最后一道门,与代理叠加);settings.json 接法见该文件头注释,实测见 `examples/hooktest/`。
76
+
77
+ ### open-core "装 wheel 即解锁" 演示
78
+
79
+ ```bash
80
+ guanshu plugins # 社区版:合规/国密/存证 等插槽显示"未解锁"
81
+ guanshu report audit.jsonl # 🔒 需企业版插件(ComplianceMapperSPI)
82
+
83
+ pip install -e plugins/guanshu-enterprise-demo # 装一个(假的)企业插件
84
+ guanshu plugins # 现在 compliance=gbt45654-demo, signers=sm2-demo
85
+ guanshu report audit.jsonl # ✓ 生成 GB/T 45654 合规证据报告(demo)
86
+ ```
87
+
88
+ 核心代码**一行不改**,装个 wheel 能力就亮——企业版的合规/国密/存证全靠 `spi.py` 的 entry-point 插槽后插。
89
+
90
+ ---
91
+
92
+ ### 出站白名单 + 指定搜索引擎
93
+
94
+ 在 `policy.screening` 里约束 agent 的联网范围(动作门层,default-deny):
95
+
96
+ ```yaml
97
+ screening:
98
+ allow_schemes: [https] # 只允许 https
99
+ search_engines: ["searxng.internal"] # 唯一指定搜索引擎(自动放行)
100
+ block_unsanctioned_search: true # 用 google/baidu/bing 等 → 判恶意
101
+ url_allowlist: ["*.gov.cn", "docs.python.org"] # 允许域(精确/*.后缀,标签边界安全)
102
+ ```
103
+
104
+ - **配了白名单即进 egress 强制**:非白名单域名硬判 MALICIOUS 拦截;没配则维持中转站默认(域名仅提示)。
105
+ - 含 punycode/同形字归一、`user@host` 取真实 host、`:port` 剥离、IP 字面量管控。
106
+ - 完整示例见 `policies/egress-allowlist.yaml`。
107
+ - ⚠️ 这是「动作门」层(决策+审计)——**物理「只能访问」**(封死直连/DNS 白名单/出口代理)是 **v0.2 的 L4 网络出口**;动作门拦得住"调 fetch",拦不住"自己写代码绕",两层叠加才完整。
108
+
109
+ ## 架构(8 模块)
110
+
111
+ | 模块 | 职责 |
112
+ |---|---|
113
+ | `models` | GovernanceRequest / Decision / ScreenResult(Pydantic) |
114
+ | `screening` | L3 纯本地筛查(slopsquat/密钥/危险代码/恶意URL) |
115
+ | `policy` | 最小 YAML DSL 策略引擎,default-deny + fail-closed |
116
+ | `interceptor` | `@govern` 装饰器 + `Governor.check()/.run()` |
117
+ | `audit` | SHA-256 哈希链 JSONL,只存 result digest,可 `verify` |
118
+ | `cli` | `demo` / `verify` / `lint-policy`(stdlib argparse) |
119
+ | `spi` | open-core 扩展点(Protocol + entry points) |
120
+ | `_packages` | slopsquat 离线种子包名表(生产精度走 RegistrySPI) |
121
+
122
+ ## Open-core 边界(生死线)
123
+
124
+ 社区核心只依赖 `spi.py` 的 Protocol,**绝不 import 企业版**;企业能力经 Python entry points 安装即生效:
125
+
126
+ | 企业能力 | SPI | 为何闭源 |
127
+ |---|---|---|
128
+ | ⭐ 中国合规映射(GB/T 45654/等保/密评)+ 监管报告 | `ComplianceMapperSPI` | 核心变现 |
129
+ | 国密 SM2/SM3 审计签名 | `SignerSPI` | 信创门槛 |
130
+ | 区块链存证 / 监管提交格式 | `AuditSinkSPI` | 私有化交付 |
131
+ | 真实 registry + 威胁情报(精准 slopsquat) | `RegistrySPI` | 数据服务 |
132
+ | 本地国产 LLM 深度意图筛查 | `ScreenerSPI` | 企业集成 |
133
+
134
+ > **红线:任何深度判定都挂本地/国产模型,绝不依赖第三方云(如 Snyk/agent-scan)——数据不出域是信创/金融政务的硬要求,也是差异化卖点。**
135
+
136
+ ---
137
+
138
+ ## License
139
+
140
+ Apache-2.0(见 `LICENSE`、`NOTICE`)。本项目为 **clean-room 实现,未 copy 任何第三方源码**;哈希链审计 / open-core 机制等设计受微软 AGT(MIT)及公开论文**启发后独立重写**。**不使用微软 AGT 的名称/Logo,与微软无隶属或背书关系。**
@@ -0,0 +1,121 @@
1
+ # 关枢 / guanshu
2
+
3
+ > **确定性的智能体动作治理**:在 agent 每一次工具调用 / 消息 / 委派到达外部**之前**做策略裁决(默认拒绝),被拒动作不是"概率上不会发生",而是**结构上不可能发生**;每次裁决写入防篡改哈希链审计。
4
+ >
5
+ > *Deterministic agent action governance — every tool call is screened & policy-checked before it hits the wire. Fully local, offline, no third-party cloud.*
6
+ >
7
+ > ⚠️ `guanshu/关枢` 是**工作代号**,非最终品牌名。
8
+
9
+ ---
10
+
11
+ ## 第一张脸:中转站投毒防护
12
+
13
+ 中国开发者大面积用 LLM 中转站,而中转站会**掺水(模型掉包/降级)**与**投毒(注入恶意代码、外泄密钥)**(CISPA 实测 45.83% 指纹验证失败;UCSB 实测 428 站 9 个主动注入恶意代码)。
14
+
15
+ guanshu 在动作落地前做 **L3 输出侧防御**——纯本地、确定性、无需联网、无需 LLM:
16
+
17
+ - **slopsquat**:幻觉/仿冒包名(`pip install reqursts`)
18
+ - **secret_exposure**:密钥/私钥外泄(AKIA…、`sk-ant-…`、私钥)
19
+ - **dangerous_code**:`curl | sh`、`rm -rf /`、`base64 | sh`、`eval/exec`
20
+ - **suspicious_url**:非白名单外联域名 / IP 直连
21
+
22
+ 判定 `MALICIOUS` 即 **fail-closed 拦截**,无论策略是否放行;每次裁决产出**防篡改 provenance 回执**。
23
+
24
+ ---
25
+
26
+ ## 60 秒上手(本地代理 · 主形态)
27
+
28
+ 把 AI 工具/SDK 的 `base_url` 指向本机 guanshu 代理 —— 它在模型响应到达 agent **之前**筛掉中转站投毒(剔除恶意工具调用),再转发到你**自己的**上游。**数据只在本机、key 不碰我们、工具无关**(Cursor / Cherry Studio / 任何用 base_url 的 SDK 都覆盖)。
29
+
30
+ ```bash
31
+ pip install guanshu
32
+ guanshu init # 初始化 + 自动跑「首拦自测」:60 秒看它当场拦一次投毒(不用等真攻击)
33
+ guanshu proxy --upstream https://api.openai.com/v1 # 或换成你现在用的中转站 base_url
34
+ export OPENAI_BASE_URL=http://127.0.0.1:8788/v1 # 让你的工具指过来
35
+ ```
36
+
37
+ CLI:`guanshu selftest`(首拦自测)· `guanshu doctor`(自检)· `guanshu verify/receipt <audit.jsonl>` · `guanshu lint-policy <yaml>` · `guanshu plugins` · `guanshu report`
38
+
39
+ ### 补充形态 ① 库内嵌(`@govern`)
40
+
41
+ 不走代理也可在代码里直接给工具上门:
42
+
43
+ ```python
44
+ from guanshu import govern, Governor, PolicyEngine, HashChainJSONLSink, set_active, GovernanceDenied
45
+ set_active(Governor(PolicyEngine.from_yaml("policies/relay-guard.yaml"), audit=HashChainJSONLSink("audit.jsonl")))
46
+
47
+ @govern(tool="pip_install")
48
+ def pip_install(package: str) -> str: ...
49
+
50
+ pip_install(package="requests") # ✓ 放行 + 审计
51
+ pip_install(package="reqursts") # ✗ GovernanceDenied(slopsquat,fail-closed)
52
+ ```
53
+
54
+ ### 补充形态 ② Claude Code PreToolUse hook
55
+
56
+ `examples/claude_code_pretooluse_hook.py` 可作 Claude Code 的 PreToolUse hook(执行前最后一道门,与代理叠加);settings.json 接法见该文件头注释,实测见 `examples/hooktest/`。
57
+
58
+ ### open-core "装 wheel 即解锁" 演示
59
+
60
+ ```bash
61
+ guanshu plugins # 社区版:合规/国密/存证 等插槽显示"未解锁"
62
+ guanshu report audit.jsonl # 🔒 需企业版插件(ComplianceMapperSPI)
63
+
64
+ pip install -e plugins/guanshu-enterprise-demo # 装一个(假的)企业插件
65
+ guanshu plugins # 现在 compliance=gbt45654-demo, signers=sm2-demo
66
+ guanshu report audit.jsonl # ✓ 生成 GB/T 45654 合规证据报告(demo)
67
+ ```
68
+
69
+ 核心代码**一行不改**,装个 wheel 能力就亮——企业版的合规/国密/存证全靠 `spi.py` 的 entry-point 插槽后插。
70
+
71
+ ---
72
+
73
+ ### 出站白名单 + 指定搜索引擎
74
+
75
+ 在 `policy.screening` 里约束 agent 的联网范围(动作门层,default-deny):
76
+
77
+ ```yaml
78
+ screening:
79
+ allow_schemes: [https] # 只允许 https
80
+ search_engines: ["searxng.internal"] # 唯一指定搜索引擎(自动放行)
81
+ block_unsanctioned_search: true # 用 google/baidu/bing 等 → 判恶意
82
+ url_allowlist: ["*.gov.cn", "docs.python.org"] # 允许域(精确/*.后缀,标签边界安全)
83
+ ```
84
+
85
+ - **配了白名单即进 egress 强制**:非白名单域名硬判 MALICIOUS 拦截;没配则维持中转站默认(域名仅提示)。
86
+ - 含 punycode/同形字归一、`user@host` 取真实 host、`:port` 剥离、IP 字面量管控。
87
+ - 完整示例见 `policies/egress-allowlist.yaml`。
88
+ - ⚠️ 这是「动作门」层(决策+审计)——**物理「只能访问」**(封死直连/DNS 白名单/出口代理)是 **v0.2 的 L4 网络出口**;动作门拦得住"调 fetch",拦不住"自己写代码绕",两层叠加才完整。
89
+
90
+ ## 架构(8 模块)
91
+
92
+ | 模块 | 职责 |
93
+ |---|---|
94
+ | `models` | GovernanceRequest / Decision / ScreenResult(Pydantic) |
95
+ | `screening` | L3 纯本地筛查(slopsquat/密钥/危险代码/恶意URL) |
96
+ | `policy` | 最小 YAML DSL 策略引擎,default-deny + fail-closed |
97
+ | `interceptor` | `@govern` 装饰器 + `Governor.check()/.run()` |
98
+ | `audit` | SHA-256 哈希链 JSONL,只存 result digest,可 `verify` |
99
+ | `cli` | `demo` / `verify` / `lint-policy`(stdlib argparse) |
100
+ | `spi` | open-core 扩展点(Protocol + entry points) |
101
+ | `_packages` | slopsquat 离线种子包名表(生产精度走 RegistrySPI) |
102
+
103
+ ## Open-core 边界(生死线)
104
+
105
+ 社区核心只依赖 `spi.py` 的 Protocol,**绝不 import 企业版**;企业能力经 Python entry points 安装即生效:
106
+
107
+ | 企业能力 | SPI | 为何闭源 |
108
+ |---|---|---|
109
+ | ⭐ 中国合规映射(GB/T 45654/等保/密评)+ 监管报告 | `ComplianceMapperSPI` | 核心变现 |
110
+ | 国密 SM2/SM3 审计签名 | `SignerSPI` | 信创门槛 |
111
+ | 区块链存证 / 监管提交格式 | `AuditSinkSPI` | 私有化交付 |
112
+ | 真实 registry + 威胁情报(精准 slopsquat) | `RegistrySPI` | 数据服务 |
113
+ | 本地国产 LLM 深度意图筛查 | `ScreenerSPI` | 企业集成 |
114
+
115
+ > **红线:任何深度判定都挂本地/国产模型,绝不依赖第三方云(如 Snyk/agent-scan)——数据不出域是信创/金融政务的硬要求,也是差异化卖点。**
116
+
117
+ ---
118
+
119
+ ## License
120
+
121
+ Apache-2.0(见 `LICENSE`、`NOTICE`)。本项目为 **clean-room 实现,未 copy 任何第三方源码**;哈希链审计 / open-core 机制等设计受微软 AGT(MIT)及公开论文**启发后独立重写**。**不使用微软 AGT 的名称/Logo,与微软无隶属或背书关系。**