Skip to main content

govrule

治理规则 DSL 的最小实现:把「判据」写成可读的规则文件,跑出四值结论、可读证据与基线棘轮。

定位:它不是编译器框架,也不是通用策略引擎。它只解决一件事—— 让「没跑」「跳过」「判不了」不再长得像「通过」。

为什么需要它

原有门禁是 20 个 Python 脚本,7,434 行判据 + 2,699 行共享管道 + 1,550 行自证夹具。 管道(枚举、读取、基线、报告、夹具沙箱)占了 79%,每写一条新判据都要重付一次。 把管道固化进语言,判据就只剩「选什么、抽什么、断言什么、为什么红」。

三个设计决定

1. 四值,不是布尔

Verdict = Pass | Fail | Skip | Unknown
combine(a, b) 取最坏(Fail > Unknown > Skip > Pass)
空集 → Skip;全 Skip → exit 3;Unknown → exit 3

bool(Verdict.UNKNOWN) 会直接抛异常——不允许 if verdict: 把它当假。

2. 判定集合与显示截断分离

--max-findings 只影响打印。任何为了输出好看而做的截断,一旦进了判据, 就会把「截断」读成「漏报」——这是实测踩过的坑。

3. 基线只能显式扩张

baseline-write 遇「纯增长」(一条没修、只是账单变长)拒绝写入; 基线里有、当下不再命中的键是「过期账单」,默认判红。

快速开始

govrule check --rules rules                    # 静态检查:规则本身写对了吗
govrule run --rules rules --root G:/repos/allin-governance
govrule run --rules rules --root . --format sarif   # 给编辑器/扫描平台
govrule shadow --rules rules --root . \        # 影子运行:只观察不掌权
        --reference-cmd "python gates/g01_paths.py --root ."         --ledger shadow.jsonl --require 20
python -m govrule list --rules rules
python -m govrule baseline-write --rules rules --root . --out baseline.txt --reason "冻结存量"
python -m govrule run --rules rules --root . --baseline baseline.txt

退出码:0 干净 · 1 有违规 · 2 用法错 · 3 不完整(Unknown / 全 Skip)。

规则长什么样

rule g01.dead-root
code    F-DEAD-ROOT
scope   repo "**/*" exclude ["**/.venv*/**"]
select  each file
extract text-lines(comments)
where   matches(line, /(?<![A-Za-z0-9_])[A-Za-z]:[\\\/][A-Za-z0-9_.~]/) and is-dead(line)
assert  count == 0
evidence cite=line.span expected="仓内相对路径或盘符变量" found=line.text
severity blocking
applies platforms=[nt, posix]

exempt  url     when matches(line, /:\/\//)
exempt  doc     when matches(path, /\.(md|html?)$/i) severity warn

子句只有九个:scope / select / extract / where / assert / evidence / severity / applies / exempt。没有循环、没有递归、没有函数定义—— 少不是未完成,是判据:非图灵完备换来的是「规则不可能挂死」「判据可穷举」「解释器可零依赖」。

内建

名称 语义
text-lines(comments, fences) 逐行抽取;可选剔除注释行 / 围栏块
filename() 按文件名抽取
file-meta() 按文件元数据抽取(line.size)
matches(x, /re/) 正则匹配;/.../i 大小写不敏感
is-dead(text) 行内盘符路径的父目录不存在 → true
and / or / not 布尔组合

绑定:line(当前行文本)、path(相对路径)、line.span、line.size、line.name。

三道闸门

阶段 命令 作用
写规则时 govrule check 类型/契约错误在提交前暴露(域、名、型、码)
跑规则时 govrule run 四值判定 + 证据 + 退出码(0/1/2/3)
换实现时 govrule shadow 与参照实现比 (path,line) 与退出码,台账满 N 次零分歧才允许切换

shadow 的判据是 同一份规则摘要 + 最近 N 次零分歧:改了规则,旧账不算数—— 否则可以靠不断放宽规则把分歧清零。

边界(不要误用)

  • 不是通用策略引擎:没有 Datalog、没有 join、没有跨文档聚合。
  • 不做语义分析:text-lines 是文本层,不建 AST。
  • is-dead 是本机视角:在 A 机上判「死」的路径,在 B 机上可能存在。
  • 还没有 CST / join / 结构化断言:same_shape、bijection 只有设计位,未实现。
  • SARIF 只覆盖 finding,不覆盖「规则本身有类型错误」(那走 check --json)。
  • 未声明 scope = 不可判定,不是「全部」。

许可

MIT-0。

Metadata

Release files for govrule 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for govrule 0.9.0
File Size Uploaded Provenance Actions
govrule-0.9.0.tar.gz 61.6 kB Without provenance Details for govrule-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for govrule 0.9.0
File Interpreter ABI Platform Actions
govrule-0.9.0-py3-none-any.whl Python 3 none any Details for govrule-0.9.0-py3-none-any.whl

Total release size: 123.2 kB

Release files / govrule-0.9.0.tar.gz

Download URL govrule-0.9.0.tar.gz
Size 61.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5a2c4a62a3540c3208a155b561b19cda52daeded7c050e0a17d1c956d6dfde90
BLAKE2b-256 checksum
How to use checksums
06c28d2f149a221bf9d86caeed641063354950e5d7e82ef38b35a994ace3e0ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / govrule-0.9.0-py3-none-any.whl

Download URL govrule-0.9.0-py3-none-any.whl
Size 61.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efa7296270b97c8823f2e3966308d4614c4e27db9da2dd15ecc3b7e807ee0606
BLAKE2b-256 checksum
How to use checksums
2e94cfe9fa81e370fe300acd791d941fac0df0c5f03ebb6baa4cb51ec3aaa90a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for govrule 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page