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

快速开始

python -m govrule run --rules rules --root G:/repos/allin-governance
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。

边界(不要误用)

  • 不是通用策略引擎:没有 Datalog、没有 join、没有跨文档聚合。
  • 不做语义分析:text-lines 是文本层,不建 AST。
  • is-dead 是本机视角:在 A 机上判「死」的路径,在 B 机上可能存在。
  • 未声明 scope = 不可判定,不是「全部」。

许可

MIT-0。

Metadata

Release files for govrule 0.2.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.2.0
File Size Uploaded Provenance Actions
govrule-0.2.0.tar.gz 35.4 kB Without provenance Details for govrule-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 72.2 kB

Release files / govrule-0.2.0.tar.gz

Download URL govrule-0.2.0.tar.gz
Size 35.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0d9be9d5a697513b4160c00261d8c43c198fc0c5c573e0bc266da3c10ee15296
BLAKE2b-256 checksum
How to use checksums
14fedba5021f39816e007cd9d0f098f80acbabf7afc7e9f4843b4eb4e5c4b8d6
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.2.0-py3-none-any.whl

Download URL govrule-0.2.0-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afb1c5c0707e70ba49140d25387d0b102a342ffd1c24d8ac146416b34081cec2
BLAKE2b-256 checksum
How to use checksums
f692669c78139c180365034520051ba8ccf76462bdfc1e7281c180cc2bc63e44
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.2.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.2.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