Metadata-Version: 2.4
Name: guanshu
Version: 0.1.1
Summary: 关枢 · 确定性智能体动作治理 — 第一张脸:中转站投毒防护 (deterministic agent action governance; first face: LLM-relay poisoning protection)
Author: guanshu contributors
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: agent,audit,governance,llm,mcp,relay,security,slopsquat
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Requires-Dist: pydantic>=2
Requires-Dist: pyyaml>=6
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == 'dev'
Description-Content-Type: text/markdown

# 关枢 / guanshu

> **确定性的智能体动作治理**:在 agent 每一次工具调用 / 消息 / 委派到达外部**之前**做策略裁决(默认拒绝),被拒动作不是"概率上不会发生",而是**结构上不可能发生**;每次裁决写入防篡改哈希链审计。
>
> *Deterministic agent action governance — every tool call is screened & policy-checked before it hits the wire. Fully local, offline, no third-party cloud.*

---

## 第一张脸:中转站投毒防护

中国开发者大面积用 LLM 中转站,而中转站会**掺水(模型掉包/降级)**与**投毒(注入恶意代码、外泄密钥)**(CISPA 实测 45.83% 指纹验证失败;UCSB 实测 428 站 9 个主动注入恶意代码)。

guanshu 在动作落地前做 **L3 输出侧防御**——纯本地、确定性、无需联网、无需 LLM:

- **slopsquat**:幻觉/仿冒包名(`pip install reqursts`)
- **secret_exposure**:密钥/私钥外泄(AKIA…、`sk-ant-…`、私钥)
- **dangerous_code**:`curl | sh`、`rm -rf /`、`base64 | sh`、`eval/exec`
- **suspicious_url**:非白名单外联域名 / IP 直连

判定 `MALICIOUS` 即 **fail-closed 拦截**,无论策略是否放行;每次裁决产出**防篡改 provenance 回执**。

---

## 60 秒上手(本地代理 · 主形态)

把 AI 工具/SDK 的 `base_url` 指向本机 guanshu 代理 —— 它在模型响应到达 agent **之前**筛掉中转站投毒(剔除恶意工具调用),再转发到你**自己的**上游。**数据只在本机、key 不碰我们、工具无关**(Cursor / Cherry Studio / 任何用 base_url 的 SDK 都覆盖)。

```bash
pip install guanshu
guanshu init        # 初始化 + 自动跑「首拦自测」:60 秒看它当场拦一次投毒(不用等真攻击)
guanshu proxy --upstream https://api.openai.com/v1   # 或换成你现在用的中转站 base_url
export OPENAI_BASE_URL=http://127.0.0.1:8788/v1      # 让你的工具指过来
```

CLI:`guanshu selftest`(首拦自测)· `guanshu doctor`(自检)· `guanshu verify/receipt <audit.jsonl>` · `guanshu lint-policy <yaml>` · `guanshu plugins` · `guanshu report`

### 补充形态 ① 库内嵌(`@govern`)

不走代理也可在代码里直接给工具上门:

```python
from guanshu import govern, Governor, PolicyEngine, HashChainJSONLSink, set_active, GovernanceDenied
set_active(Governor(PolicyEngine.from_yaml("policies/relay-guard.yaml"), audit=HashChainJSONLSink("audit.jsonl")))

@govern(tool="pip_install")
def pip_install(package: str) -> str: ...

pip_install(package="requests")    # ✓ 放行 + 审计
pip_install(package="reqursts")    # ✗ GovernanceDenied(slopsquat,fail-closed)
```

### 补充形态 ② Claude Code PreToolUse hook

`examples/claude_code_pretooluse_hook.py` 可作 Claude Code 的 PreToolUse hook(执行前最后一道门,与代理叠加);settings.json 接法见该文件头注释,实测见 `examples/hooktest/`。

### open-core "装 wheel 即解锁" 演示

```bash
guanshu plugins                 # 社区版:合规/国密/存证 等插槽显示"未解锁"
guanshu report audit.jsonl      # 🔒 需企业版插件(ComplianceMapperSPI)

pip install -e plugins/guanshu-enterprise-demo   # 装一个(假的)企业插件
guanshu plugins                 # 现在 compliance=gbt45654-demo, signers=sm2-demo
guanshu report audit.jsonl      # ✓ 生成 GB/T 45654 合规证据报告(demo)
```

核心代码**一行不改**,装个 wheel 能力就亮——企业版的合规/国密/存证全靠 `spi.py` 的 entry-point 插槽后插。

---

### 出站白名单 + 指定搜索引擎

在 `policy.screening` 里约束 agent 的联网范围(动作门层,default-deny):

```yaml
screening:
  allow_schemes: [https]                 # 只允许 https
  search_engines: ["searxng.internal"]   # 唯一指定搜索引擎(自动放行)
  block_unsanctioned_search: true        # 用 google/baidu/bing 等 → 判恶意
  url_allowlist: ["*.gov.cn", "docs.python.org"]  # 允许域(精确/*.后缀,标签边界安全)
```

- **配了白名单即进 egress 强制**:非白名单域名硬判 MALICIOUS 拦截;没配则维持中转站默认(域名仅提示)。
- 含 punycode/同形字归一、`user@host` 取真实 host、`:port` 剥离、IP 字面量管控。
- 完整示例见 `policies/egress-allowlist.yaml`。
- ⚠️ 这是「动作门」层(决策+审计)——**物理「只能访问」**(封死直连/DNS 白名单/出口代理)是 **v0.2 的 L4 网络出口**;动作门拦得住"调 fetch",拦不住"自己写代码绕",两层叠加才完整。

## 架构(8 模块)

| 模块 | 职责 |
|---|---|
| `models` | GovernanceRequest / Decision / ScreenResult(Pydantic) |
| `screening` | L3 纯本地筛查(slopsquat/密钥/危险代码/恶意URL) |
| `policy` | 最小 YAML DSL 策略引擎,default-deny + fail-closed |
| `interceptor` | `@govern` 装饰器 + `Governor.check()/.run()` |
| `audit` | SHA-256 哈希链 JSONL,只存 result digest,可 `verify` |
| `cli` | `demo` / `verify` / `lint-policy`(stdlib argparse) |
| `spi` | open-core 扩展点(Protocol + entry points) |
| `_packages` | slopsquat 离线种子包名表(生产精度走 RegistrySPI) |

## Open-core 扩展点

社区核心只依赖 `spi.py` 的 Protocol,**绝不 import 企业版**;企业能力经 Python entry points 安装即生效:

| 企业能力 | SPI |
|---|---|
| 中国合规映射(GB/T 45654/等保/密评)+ 报告 | `ComplianceMapperSPI` |
| 国密 SM2/SM3 审计签名 | `SignerSPI` |
| 外部/区块链存证 + 监管提交格式 | `AuditSinkSPI` |
| 真实 registry + 威胁情报(精准 slopsquat) | `RegistrySPI` |
| 本地国产 LLM 深度意图筛查 | `ScreenerSPI` |

> 任何深度判定都挂本地/国产模型,绝不依赖第三方云——数据不出域。

---

## License

Apache-2.0(见 `LICENSE`)。检测规则源自公开安全研究。
