规则引擎
规则引擎允许你在 .sentrux/rules.toml 中定义架构约束,并自动执行——在 CI 中、合并前,或通过 MCP 让你的 AI 代理在编写代码前就知道边界。
规则文件示例
Section titled “规则文件示例”[constraints]max_cycles = 0 # zero tolerance for circular dependenciesmax_coupling = "B" # maximum coupling grademax_cc = 25 # maximum cyclomatic complexity per functionno_god_files = true # no file with >500 lines or fan-out >15
[[layers]]name = "core"paths = ["src/core/*"]order = 0
[[layers]]name = "service"paths = ["src/service/*"]order = 1
[[layers]]name = "app"paths = ["src/app/*"]order = 2
[[boundaries]]from = "src/app/*"to = "src/core/internal/*"reason = "App must not depend on core internals"sentrux check .# ✓ All rules pass — Quality: 7342退出码 0 = 通过,1 = 失败。在 CI 中使用:
- name: Architecture check run: sentrux check .层级定义了依赖层次结构。order 值越低 = 越基础。高层可以依赖低层,反之不行。
app (order 2) → 可以依赖 service 和 coreservice (order 1) → 可以依赖 corecore (order 0) → 不能依赖 app 或 service边界规则阻断特定的依赖路径:
[[boundaries]]from = "src/api/*"to = "src/db/internal/*"reason = "API layer must use the DB interface, not internals"MCP 集成
Section titled “MCP 集成”你的 AI 代理可以在提交前检查规则:
Agent: check_rules()→ { pass: true, violations: [] }或者在做出变更之后:
Agent: check_rules()→ { pass: false, violations: [ { rule: "boundary", from: "src/api/handler.rs", to: "src/db/internal/pool.rs", reason: "API layer must use the DB interface, not internals" } ]}代理看到违规并在你看到之前就修复它。