reasonweave 0.0.0-stage → 1.3.2
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.
- package/AGENTS.md +101 -0
- package/INSTALL.md +258 -0
- package/README.md +280 -2
- package/bin/lab.mjs +888 -0
- package/constitution.md +55 -0
- package/docs/CONSUMER-PROMPTS.md +474 -0
- package/docs/backlogs/01-forward-wait-self-detach.md +18 -0
- package/docs/backlogs/02-decouple-wait-and-daemon-for-agent-hosts.md +35 -0
- package/docs/backlogs/03-baton-wait-multi-id-lightweight-pubsub.md +34 -0
- package/docs/backlogs/04-adr-cross-repo-automated-dispatch-boundary.md +19 -0
- package/docs/backlogs/05-feat-units-worktree-isolation-engine.md +23 -0
- package/docs/backlogs/06-feat-blind-audit-automated-clean-runner.md +22 -0
- package/docs/backlogs/07-feat-attention-throttle-silent-pipe.md +23 -0
- package/docs/backlogs/08-future-units-shadow-ledger.md +22 -0
- package/docs/backlogs/09-feat-agent-baton-core-convergence-and-resilience.md +42 -0
- package/docs/backlogs/10-feat-anti-orphan-deterministic-wiring-governance.md +35 -0
- package/docs/backlogs/11-perf-orphan-index-fs-read.md +16 -0
- package/docs/backlogs/12-feat-vibe-security-anti-suppression-and-orphan-deps.md +43 -0
- package/docs/backlogs/13-feat-sdd-reppit-tiered-guardrails.md +40 -0
- package/docs/backlogs/14-feat-universal-cli-contract-and-hermetic-test-fixtures.md +48 -0
- package/docs/backlogs/15-feat-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +44 -0
- package/docs/backlogs/16-feat-micro-engineering-skills-and-sdd-baton-fusion.md +52 -0
- package/docs/codegraph-readiness-check.md +49 -0
- package/group/architecture/adr/0001-handoff-async-wait-and-reactive-wakeup.md +87 -0
- package/group/architecture/adr/0002-de-ritualize-handoff-prompt-for-intra-repo.md +53 -0
- package/group/architecture/adr/0003-attention-throttle-and-workspace-driver.md +65 -0
- package/group/architecture/adr/0004-spec-guard-and-drift-prevention.md +90 -0
- package/group/architecture/adr/0005-global-orphan-scavenger-and-read-only-invariants.md +94 -0
- package/group/architecture/adr/0006-triad-engineering-defense-and-mutation-sensitivity.md +68 -0
- package/group/architecture/adr/0007-agent-baton-state-machine-defense-and-workspace-jail-spec.md +86 -0
- package/group/architecture/adr/0008-agent-baton-core-convergence-and-heterogeneous-session-resilience.md +133 -0
- package/group/architecture/adr/0009-anti-orphan-deterministic-wiring-governance-and-federated-guardrails.md +108 -0
- package/group/architecture/adr/0010-agent-baton-identity-security-cryptographic-credentials-and-prompt-anti-confusion.md +119 -0
- package/group/architecture/adr/0011-vibe-security-anti-suppression-and-dependency-reachability.md +161 -0
- package/group/architecture/adr/0012-spec-driven-development-reppit-and-tiered-guardrails.md +130 -0
- package/group/architecture/adr/0013-universal-cli-contract-hermetic-testing-and-defensive-probes.md +110 -0
- package/group/architecture/adr/0014-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +108 -0
- package/group/architecture/adr/0015-micro-engineering-skills-and-dual-core-gate-invariant.md +127 -0
- package/group/architecture/adr/INDEX.md +29 -0
- package/group/architecture/adr/templates/adr-template.md +33 -0
- package/group/architecture/dependency-graph.md +60 -0
- package/group/contracts/npm-packaging.md +69 -0
- package/group/product/intent.md +24 -0
- package/package.json +93 -4
- package/scripts/sync-all-consumers.mjs +174 -0
- package/specs/RFC-20261110-SPEC-GUARD-UNIT.md +353 -0
- package/specs/templates/01-research.md.template +23 -0
- package/specs/templates/02-proposals.md.template +22 -0
- package/specs/templates/03-spec.md.template +33 -0
- package/specs/templates/04-tasks.md.template +29 -0
- package/specs/templates/05-evidence.md.template +29 -0
- package/specs/templates/README.md +26 -0
- package/units/adr/AGENTS.md +85 -0
- package/units/adr/CHANGELOG.md +20 -0
- package/units/adr/README.md +88 -0
- package/units/adr/VERSION.md +10 -0
- package/units/adr/docs/existing-project-prompt.md +24 -0
- package/units/adr/docs/new-project-prompt.md +36 -0
- package/units/adr/scripts/adr-init.sh +101 -0
- package/units/adr/templates/INDEX.md +19 -0
- package/units/adr/templates/adr-frozen.md +33 -0
- package/units/adr/templates/adr-light.md +56 -0
- package/units/adr/templates/adr-standard.md +61 -0
- package/units/backlog/AGENTS.md +53 -0
- package/units/backlog/AUTONOMOUS-GOVERNANCE.md +98 -0
- package/units/backlog/CHANGELOG.md +37 -0
- package/units/backlog/README.md +124 -0
- package/units/backlog/VERSION.md +11 -0
- package/units/backlog/docs/existing-project-ironrules-prompt.md +31 -0
- package/units/backlog/docs/existing-project-prompt.md +24 -0
- package/units/backlog/docs/new-project-prompt.md +19 -0
- package/units/backlog/templates/entry.md +39 -0
- package/units/backlog/templates/project-ironrules.md +36 -0
- package/units/handoffs/AGENT-GUIDE.md +118 -0
- package/units/handoffs/AGENTS.md +28 -0
- package/units/handoffs/CHANGELOG.md +498 -0
- package/units/handoffs/CLAUDE.md +28 -0
- package/units/handoffs/COMMANDS.md +187 -0
- package/units/handoffs/DESIGN.md +74 -0
- package/units/handoffs/README.md +456 -0
- package/units/handoffs/RESPONSE-TEMPLATE.md +138 -0
- package/units/handoffs/VERSION.md +1 -0
- package/units/handoffs/handoff-status.mjs +1017 -0
- package/units/handoffs/handoff.mjs +5675 -0
- package/units/orphan/CHANGELOG.md +118 -0
- package/units/orphan/DESIGN.md +484 -0
- package/units/orphan/README.md +143 -0
- package/units/orphan/VERSION.md +1 -0
- package/units/orphan/adapters/android/index.mjs +140 -0
- package/units/orphan/adapters/python/index.mjs +232 -0
- package/units/orphan/adapters/web/index.mjs +280 -0
- package/units/orphan/bin/lab-orphan.mjs +298 -0
- package/units/orphan/core/blob-reader.mjs +142 -0
- package/units/orphan/core/classifier.mjs +261 -0
- package/units/orphan/core/cleaner.mjs +166 -0
- package/units/orphan/core/diff-checker.mjs +341 -0
- package/units/orphan/core/inverted-index.mjs +124 -0
- package/units/orphan/core/orphan-deps.mjs +192 -0
- package/units/orphan/core/protocol-s1-s3.mjs +194 -0
- package/units/orphan/core/solver.mjs +217 -0
- package/units/orphan/index.mjs +105 -0
- package/units/orphan/templates/ci/orphan-guard.yml +36 -0
- package/units/orphan/templates/pre-commit.d/30-orphan-guard +34 -0
- package/units/orphan/templates/wiring/README.md +51 -0
- package/units/orphan/templates/wiring/miniprogram-route-guard.js.template +80 -0
- package/units/orphan/templates/wiring/pre-commit-node.sh.template +73 -0
- package/units/orphan/templates/wiring/project-agents-gemini-templates.md +60 -0
- package/units/orphan/templates/wiring/python-parity-test.py.template +100 -0
- package/units/orphan/templates/wiring/rust-exhaustive-match.md +65 -0
- package/units/orphan/templates/wiring/swift-periphery.md +56 -0
- package/units/orphan/tests/adapters.test.mjs +243 -0
- package/units/orphan/tests/orphan-deps.test.mjs +131 -0
- package/units/orphan/tests/phase3-gates.test.mjs +135 -0
- package/units/playbook/README.md +106 -0
- package/units/playbook/ZSPEC-AGENT-GUIDE.md +99 -0
- package/units/playbook/docs/01-architecture.md +99 -0
- package/units/playbook/docs/02-bootstrap.md +126 -0
- package/units/playbook/docs/03-units.md +91 -0
- package/units/playbook/docs/04-constitution.md +113 -0
- package/units/playbook/docs/05-adr.md +33 -0
- package/units/playbook/docs/06-spec-kit.md +76 -0
- package/units/playbook/docs/07-backlog.md +30 -0
- package/units/playbook/docs/08-codegraph.md +79 -0
- package/units/playbook/docs/09-skills.md +101 -0
- package/units/playbook/docs/10-handoff.md +72 -0
- package/units/playbook/docs/11-evidence.md +163 -0
- package/units/playbook/docs/12-gates.md +124 -0
- package/units/playbook/docs/13-parallel.md +115 -0
- package/units/playbook/docs/14-dependency-graph.md +86 -0
- package/units/playbook/docs/15-memory.md +153 -0
- package/units/playbook/docs/16-change-control.md +121 -0
- package/units/playbook/docs/17-prompts.md +121 -0
- package/units/playbook/docs/18-shift-left.md +97 -0
- package/units/playbook/docs/19-anti-orphan-vertical-slicing.md +102 -0
- package/units/playbook/docs/20-vibe-security-and-anti-suppression.md +88 -0
- package/units/playbook/docs/21-spec-driven-reppit-closed-loop.md +88 -0
- package/units/playbook/examples/owisel.md +80 -0
- package/units/playbook/references/Agent-Engineering-Framework-AEF-v1.0.md +2033 -0
- package/units/playbook/references/links.md +103 -0
- package/units/playbook/templates/AGENTS.root.md +44 -0
- package/units/playbook/templates/AGENTS.unit.md +52 -0
- package/units/playbook/templates/adr/INDEX.md +17 -0
- package/units/playbook/templates/adr/NNNN-template.md +56 -0
- package/units/playbook/templates/backlogs/entry.md +30 -0
- package/units/playbook/templates/constitution.md +93 -0
- package/units/playbook/templates/evidence/README.md +36 -0
- package/units/playbook/templates/group/architecture/dependency-graph.md +48 -0
- package/units/playbook/templates/group/architecture/system.md +59 -0
- package/units/playbook/templates/group/contracts/api.md +67 -0
- package/units/playbook/templates/group/product/intent.md +30 -0
- package/units/playbook/templates/group/product/requirements.md +48 -0
- package/units/playbook/templates/unit-skeleton/README.md +35 -0
- package/units/preflight/AGENTS.md +45 -0
- package/units/preflight/CHANGELOG.md +55 -0
- package/units/preflight/INSTALL.md +65 -0
- package/units/preflight/README.md +104 -0
- package/units/preflight/VERSION.md +3 -0
- package/units/preflight/docs/audit-requirements.md +114 -0
- package/units/preflight/docs/existing-project-prompt.md +25 -0
- package/units/preflight/docs/new-project-prompt.md +25 -0
- package/units/preflight/docs/transfers/CONSUMER-INSTALL.md +40 -0
- package/units/preflight/docs/transfers/CONSUMER-SELFCHECK.md +51 -0
- package/units/preflight/docs/transfers/README.md +39 -0
- package/units/preflight/docs/transfers/prospero.md +26 -0
- package/units/preflight/templates/check.sh +43 -0
- package/units/preflight/templates/pre-commit +55 -0
- package/units/preflight/templates/pre-commit.d/20-preflight +15 -0
- package/units/preflight/templates/preflight.conf.example +21 -0
- package/units/preflight/templates/preflight.sh +189 -0
- package/units/preflight/templates/wrap-up.md +9 -0
- package/units/review-gate/AGENTS.md +42 -0
- package/units/review-gate/CHANGELOG.md +58 -0
- package/units/review-gate/README.md +118 -0
- package/units/review-gate/VERSION.md +3 -0
- package/units/review-gate/clean-runner.mjs +350 -0
- package/units/review-gate/docs/existing-project-prompt.md +28 -0
- package/units/review-gate/docs/new-project-prompt.md +38 -0
- package/units/review-gate/docs/transfers/iggle.md +34 -0
- package/units/review-gate/docs/transfers/prospero.md +35 -0
- package/units/review-gate/docs/transfers/swoop.md +36 -0
- package/units/review-gate/docs/transfers/yiqiyihuihua.md +35 -0
- package/units/review-gate/templates/.semgrepignore +37 -0
- package/units/review-gate/templates/pre-commit +130 -0
- package/units/review-gate/templates/scan.sh +158 -0
- package/units/skills/AGENTS.md +17 -0
- package/units/skills/CHANGELOG.md +18 -0
- package/units/skills/README.md +63 -0
- package/units/skills/VERSION.md +1 -0
- package/units/skills/bin/review-package.mjs +99 -0
- package/units/skills/bin/task-brief.mjs +102 -0
- package/units/skills/catalog/brainstorming/SKILL.md +75 -0
- package/units/skills/catalog/brainstorming/references/design-doc-template.md +28 -0
- package/units/skills/catalog/systematic-debugging/SKILL.md +101 -0
- package/units/skills/catalog/systematic-debugging/references/root-cause-tracing.md +23 -0
- package/units/skills/catalog/test-driven-development/SKILL.md +88 -0
- package/units/skills/catalog/test-driven-development/references/writing-good-tests.md +13 -0
- package/units/skills/catalog/verification-before-completion/SKILL.md +73 -0
- package/units/skills/skills.mjs +181 -0
- package/units/skills/tests/pipeline.test.mjs +131 -0
- package/units/skills/tests/skill-contract.test.mjs +67 -0
- package/units/skills/tests/skills-cli.test.mjs +107 -0
- package/units/spec-guard/AGENTS.md +43 -0
- package/units/spec-guard/CHANGELOG.md +88 -0
- package/units/spec-guard/README.md +148 -0
- package/units/spec-guard/VERSION.md +1 -0
- package/units/spec-guard/bin/spec-guard.mjs +431 -0
- package/units/spec-guard/core/adr-parser.mjs +540 -0
- package/units/spec-guard/core/matrix-checker.mjs +587 -0
- package/units/spec-guard/core/mutation-runner.mjs +267 -0
- package/units/spec-guard/core/scanner.mjs +333 -0
- package/units/spec-guard/core/telemetry_authenticator.mjs +484 -0
- package/units/spec-guard/core/wiring_tracer.mjs +306 -0
- package/units/spec-guard/templates/annotations/ContractOnly.kt +19 -0
- package/units/spec-guard/templates/annotations/Invariant.kt +16 -0
- package/units/spec-guard/templates/annotations/invariant.go +19 -0
- package/units/spec-guard/templates/annotations/invariant.py +38 -0
- package/units/spec-guard/templates/annotations/invariant.ts +39 -0
- package/units/spec-guard/templates/ci/spec-guard-audit.yml +23 -0
- package/units/spec-guard/templates/pre-commit.d/15-spec-guard +72 -0
- package/units/spec-guard/templates/spec-matrix.overlay.yml +70 -0
- package/units/spec-guard/tests/spec-guard.test.mjs +716 -0
- package/units/spec-guard/tests/telemetry_authenticator.test.mjs +633 -0
- package/units/worktree/AGENTS.md +22 -0
- package/units/worktree/CHANGELOG.md +14 -0
- package/units/worktree/README.md +65 -0
- package/units/worktree/VERSION.md +1 -0
- package/units/worktree/worktree.mjs +352 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.md +322 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# spec-guard (drift-gate) — 全局架构漂移与规约守卫
|
|
2
|
+
|
|
3
|
+
> 本目录是「规约与实现脱钩 / 架构漂移 / 装饰性空壳接线」治理的**单一事实源**,独立于任何具体业务项目。
|
|
4
|
+
> **理论背书**:ThoughtWorks 架构适应度函数(Fitness Functions)、Google No Premature Dead Abstractions 哲学、Cyrille Martraire 活文档(Living Documentation)。
|
|
5
|
+
> **同范式模块**:`units/adr`(决策事实源)、`units/preflight`(不变量门禁)、`units/review-gate`(SAST 静态防线)、`units/orphan`(孤儿资产清道夫)。
|
|
6
|
+
> **接入方式**:项目通过 `$(lab path spec-guard)` 脚本与 CLI 接入,**严禁**在消费方内联复制规则正文(防漂移)。
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 0. 这是什么 / 为什么独立
|
|
11
|
+
|
|
12
|
+
在 Agent 辅助工程与自顶向下设计体系中,存在一个经典的破坏性失真循环:
|
|
13
|
+
1. **半山腰挂起(Top-Down Spec Hang)**:架构师或 Agent 在撰写并冻结顶层架构决策(ADR)时,为了让规范显得完备,过早把纯算法写进了生产代码(`src/main/`),但底层调度、跨端通信与业务锁尚未就绪;
|
|
14
|
+
2. **死代码误杀**:几天后静态扫描(如孤儿清理工具)将这批函数当作“零引用代码”删掉,导致 Frozen ADR 与实现彻底脱节;
|
|
15
|
+
3. **装饰性空壳接线(Shallow Wiring)**:若为了通过门禁在守护循环中挂一行空调用,调用图(CallGraph)虽全绿,但底层根本未做业务互锁与条件判断,形成了更隐蔽的带病运行。
|
|
16
|
+
|
|
17
|
+
**「调用图可达」≠「语义闭环」**。
|
|
18
|
+
本模块从 playbook 与 Tappy 事故治理经验中抽离为**正交、自洽、可版本化**的独立单元,用客观机器手段守护“顶层意图 ↔ 机器断言 ↔ 生产代码”三元闭环。
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 1. 核心铁律(不可违反)
|
|
23
|
+
|
|
24
|
+
1. **P3′ 三元映射对账(Triad Mapping)**:
|
|
25
|
+
每一个关键架构不变量(如 ADR Invariant `H-1`)必须强制绑定至少一条**可机判断言(Machine Assertion)**。门禁校验的是“不变量断言覆盖率”,而非“符号是否有调用点”。
|
|
26
|
+
2. **Code-as-Spec-Matrix(代码即矩阵,零 YAML 摩擦力)**:
|
|
27
|
+
CLI 默认通过 AST/源码直接扫描各语言的 `@Invariant("ADR-XXX:H-Y")` 注解与 ADR 文本中的 `## Implementation Status`,在内存中动态构建对账矩阵。`spec-matrix.overlay.yml` 仅作为跨端/跨语言协议层按需覆写(Overlay)。
|
|
28
|
+
3. **防伪铁律(No-Cheat Rule)**:
|
|
29
|
+
测试类(`src/test/`、`tests/`、`*_test.go` 等)中对算法原型的直接调用,**一律不计入生产可达性证明**。未接线算法若需提前沉淀,必须标注 `@ContractOnly`。
|
|
30
|
+
4. **严格词界防假阳性(Strict Word Boundaries)**:
|
|
31
|
+
所有 Invariant 匹配强制遵循严格边界正则(`(?<=^|[^a-zA-Z0-9_-])ADR-\d+:[A-Z]-\d+(?=$|[^a-zA-Z0-9_-])`),严禁裸子串匹配,坚决杜绝 `H-1` 误伤 `H-10` 或 `H-1-OBSOLETE`。
|
|
32
|
+
5. **只出报告、不改状态(Evidence-Driven, Never Auto-Mutate)**:
|
|
33
|
+
遵循 `units/adr` 宪法(append-only + 用户单句批准 + 🔒Frozen 不可逆)。`spec-guard` 只产出机器证据报告,**严禁通过脚本擅自将 ADR 修改为 🔒Frozen**。
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 2. 命令行与功能 (CLI Usage)
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 查看帮助
|
|
41
|
+
spec-guard --help
|
|
42
|
+
# 或通过中央调度
|
|
43
|
+
lab spec-guard --help
|
|
44
|
+
|
|
45
|
+
# 1. 初始化项目配置与预置模板
|
|
46
|
+
spec-guard init
|
|
47
|
+
|
|
48
|
+
# 2. 扫描并对账全仓规约覆盖率与架构漂移
|
|
49
|
+
spec-guard check [--full] [--strict] [--telemetry] [--check-callers] [--overlay <path>]
|
|
50
|
+
|
|
51
|
+
# 3. 专项数据自述验签(精准排查 ADR-066 类违规,强制 ADR-064 机器信任根)
|
|
52
|
+
spec-guard verify-evidence [ADR-ID] [--repo <path>]
|
|
53
|
+
|
|
54
|
+
# 4. 专项生产代码物理接线探测(拦截 🔒Frozen 孤儿决策)
|
|
55
|
+
spec-guard check-wiring [ADR-ID] [--repo <path>]
|
|
56
|
+
|
|
57
|
+
# 5. 执行变异反向测试敏感度自检(消灭回音壁假测试)
|
|
58
|
+
spec-guard mutate --file <file> --cmd "<test-command>" [--mutator <type>]
|
|
59
|
+
|
|
60
|
+
# 6. 针对指定 ADR 产出机器审计证据报告(冻结前的机器背书)
|
|
61
|
+
spec-guard audit-adr ADR-060 [--out report.md]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 输出状态语义 (Reconciliation Statuses)
|
|
65
|
+
- `✅ VERIFIED`:ADR Invariant 既有生产代码接线,又有可机判断言覆盖并通过;
|
|
66
|
+
- `📦 SANDBOXED`:标注了 `@ContractOnly` 的算法原型(存在测试断言,生产未接线,处于垂直切片初级阶段,受保护不被当死代码误删);
|
|
67
|
+
- `⏳ IN_PROGRESS`:有部分实现或单测,但尚未形成完整闭环;
|
|
68
|
+
- `🚨 DRIFT_DETECTED`:ADR 声称已实现(`✅ 已实现`)或已 `🔒Frozen`,但代码库中**零断言**或**生产零接线**;
|
|
69
|
+
- `🚨 TELEMETRY_DRIFT`:ADR 文本声明了性能量化指标(ms, MB, P95 等),但缺少机器签名凭据或实测数据与凭据矛盾(容差 $0\%$,违反 ADR-064 机器信任根);
|
|
70
|
+
- `🚨 ORPHAN_DECISION`:ADR 标记为 `🔒Frozen`,但决策中声明的核心架构实体在生产代码中零命中;
|
|
71
|
+
- `⚠️ ORPHAN_ASSERTION`:代码中标记了 `@Invariant`,但在 ADR 中不存在对应不变量定义。
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## 3. 多语言断言打标规范 (Polyglot Annotations)
|
|
76
|
+
|
|
77
|
+
无需引入重型外部依赖,使用标准注解或约定格式注释即可:
|
|
78
|
+
|
|
79
|
+
| 语言栈 | 机器断言打标形式 (Machine Assertion Anchor) | 沙箱隔离标记 (Sandbox / Unwired Marker) |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| **Kotlin / Java** | `@Test @Invariant("ADR-060:H-1")` | `@ContractOnly(adr = "ADR-060", expire = "2026-12-01")` |
|
|
82
|
+
| **Python** | `@pytest.mark.invariant("ADR-060:H-1")` 或 `# [INVARIANT: ADR-060:H-1]` | `@contract_only(adr="ADR-060", expire="2026-12-01")` |
|
|
83
|
+
| **Golang** | `// Invariant: ADR-060:H-1`(放在 `TestXxx` 上方) | `// [CONTRACT-ONLY: ADR-060]` |
|
|
84
|
+
| **TypeScript / Node** | `// @invariant ADR-060:H-1` 或 `it('...', { tags: ['INV:ADR-060:H-1'] })` | `/** @contractOnly ADR-060 */` |
|
|
85
|
+
| **Rust** | `#[test] #[doc = "invariant: ADR-060:H-1"]` | `#[cfg(feature = "contract-sandbox")]` |
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## 4. 与 lab 其他单元的协同编排
|
|
90
|
+
|
|
91
|
+
1. **协同 `lab adr`**:
|
|
92
|
+
- 提取 ADR 中的 `## Implementation Status`(落地情况表)与 `## Invariants`。
|
|
93
|
+
- `spec-guard audit-adr <ID>` 输出机器审计报告,辅助架构师判定该 ADR 是否具备 `🔒Frozen` 的现实基础。
|
|
94
|
+
2. **协同 `lab preflight`**:
|
|
95
|
+
- 将 `templates/pre-commit.d/15-spec-guard` 复制到项目的 `.githooks/pre-commit.d/`。
|
|
96
|
+
- 本地 pre-commit 保持极速(< 0.05s),拦截针对 Frozen ADR 的未经授权修改与裸原型合入。
|
|
97
|
+
3. **协同 `lab review-gate`**:
|
|
98
|
+
- 结合 OpenGrep 规则,拦截未经 `@ContractOnly` 打标的裸孤立函数。
|
|
99
|
+
4. **协同 `lab orphan`**:
|
|
100
|
+
- 孤儿清道夫清理零引用代码前,向 `spec-guard` 确认该符号是否带有有效期内的 `@ContractOnly` 标记;若有则豁免,若无且超期则彻底清理。
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 5. 项目接入步骤 (Per-Repo Setup)
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# 1. 确保安装 lab-agent-toolkit
|
|
108
|
+
lab list
|
|
109
|
+
|
|
110
|
+
# 2. 在项目根目录初始化
|
|
111
|
+
node "$(lab path spec-guard)/bin/spec-guard.mjs" init
|
|
112
|
+
|
|
113
|
+
# 3. 复制 pre-commit 门禁片(可选)
|
|
114
|
+
mkdir -p .githooks/pre-commit.d
|
|
115
|
+
cp "$(lab path spec-guard)/templates/pre-commit.d/15-spec-guard" .githooks/pre-commit.d/
|
|
116
|
+
chmod +x .githooks/pre-commit.d/15-spec-guard
|
|
117
|
+
|
|
118
|
+
# 4. 跑一次全仓自检
|
|
119
|
+
node "$(lab path spec-guard)/bin/spec-guard.mjs" check
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## 6. 单元目录布局 (Unit Directory Layout)
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
units/spec-guard/
|
|
128
|
+
├── README.md # 单元核心规约与单一事实源(本文件)
|
|
129
|
+
├── AGENTS.md # 面向 AI Agent 的专属行为法则与防漂移守则
|
|
130
|
+
├── VERSION.md # 语义版本号 (v2.0.0)
|
|
131
|
+
├── CHANGELOG.md # 版本演进记录
|
|
132
|
+
├── bin/
|
|
133
|
+
│ └── spec-guard.mjs # CLI 入口 (支持 init, check, verify-evidence, check-wiring, audit-adr)
|
|
134
|
+
├── core/
|
|
135
|
+
│ ├── adr-parser.mjs # ADR Invariants 与 Implementation Status 抽取器
|
|
136
|
+
│ ├── scanner.mjs # 多语言源码 @Invariant 扫描器与 No-Cheat 防伪探针
|
|
137
|
+
│ ├── telemetry_authenticator.mjs # 实测数据自述凭据验签与 ADR-064 机器信任根对账引擎
|
|
138
|
+
│ ├── wiring_tracer.mjs # ADR 决策核心符号物理接线与孤儿决策探针
|
|
139
|
+
│ └── matrix-checker.mjs # 三元映射对账计算与全景热力表生成引擎
|
|
140
|
+
├── templates/
|
|
141
|
+
│ ├── spec-matrix.overlay.yml # 跨端跨语言协议层按需覆写模板 (Overlay)
|
|
142
|
+
│ ├── pre-commit.d/15-spec-guard # preflight 分发器轻量提交门禁片
|
|
143
|
+
│ ├── ci/spec-guard-audit.yml # 异步 CI 持续审计工作流模板
|
|
144
|
+
│ └── annotations/ # Kotlin, Python, Go, TypeScript 打标模板
|
|
145
|
+
└── tests/
|
|
146
|
+
├── spec-guard.test.mjs # 基础测试套件 (覆盖词界、ADR、No-Cheat、矩阵全流程)
|
|
147
|
+
└── telemetry_authenticator.test.mjs # v2.0 测试套件 (覆盖数据自述验签、签名熔断、0%容差、孤儿决策)
|
|
148
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v2.2.1 — 2026-11-22
|
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* spec-guard CLI
|
|
5
|
+
*
|
|
6
|
+
* 全局架构漂移与规约守卫命令行工具 (v2.0.0)
|
|
7
|
+
* 具备实测数据机器信任根对账与物理接线守卫
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { execSync } from 'node:child_process';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import {
|
|
15
|
+
buildAndCheckMatrix,
|
|
16
|
+
formatConsoleReport,
|
|
17
|
+
generateAdrAuditReport,
|
|
18
|
+
buildFullTriadMatrix,
|
|
19
|
+
formatTriadHeatmap,
|
|
20
|
+
loadOverlayConfig,
|
|
21
|
+
} from '../core/matrix-checker.mjs';
|
|
22
|
+
import { runMutationTest } from '../core/mutation-runner.mjs';
|
|
23
|
+
import { verifyAllAdrsTelemetry } from '../core/telemetry_authenticator.mjs';
|
|
24
|
+
import { traceAllAdrsWiring } from '../core/wiring_tracer.mjs';
|
|
25
|
+
|
|
26
|
+
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
27
|
+
const TEMPLATES_DIR = path.join(PKG_ROOT, 'templates');
|
|
28
|
+
const SPEC_GUARD_VERSION = (() => {
|
|
29
|
+
try {
|
|
30
|
+
const vFile = path.join(PKG_ROOT, 'VERSION.md');
|
|
31
|
+
if (fs.existsSync(vFile)) {
|
|
32
|
+
const line = fs.readFileSync(vFile, 'utf8').trim().split('\n')[0];
|
|
33
|
+
const match = line.match(/v?(\d+\.\d+\.\d+)/);
|
|
34
|
+
if (match) return match[1];
|
|
35
|
+
}
|
|
36
|
+
} catch {}
|
|
37
|
+
return '2.2.0';
|
|
38
|
+
})();
|
|
39
|
+
|
|
40
|
+
const args = process.argv.slice(2);
|
|
41
|
+
const command = args[0] || 'check';
|
|
42
|
+
|
|
43
|
+
function getRepoRoot() {
|
|
44
|
+
const repoIdx = args.indexOf('--repo');
|
|
45
|
+
if (repoIdx !== -1 && args[repoIdx + 1]) {
|
|
46
|
+
return path.resolve(args[repoIdx + 1]);
|
|
47
|
+
}
|
|
48
|
+
return process.cwd();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function printHelp() {
|
|
52
|
+
console.log(`
|
|
53
|
+
🛡️ spec-guard (drift-gate) - 架构规约与漂移守卫 CLI (v${SPEC_GUARD_VERSION})
|
|
54
|
+
|
|
55
|
+
用法:
|
|
56
|
+
spec-guard check [options] 对账全仓规约断言覆盖率与架构漂移
|
|
57
|
+
spec-guard verify-evidence [ADR-ID] 验签 ADR 实测数据自述凭据 (ADR-064 机器信任根)
|
|
58
|
+
spec-guard check-wiring [ADR-ID] 探测 ADR 决策在生产代码中的物理接线存活性 (拦截孤儿决策)
|
|
59
|
+
spec-guard audit-adr <ADR-ID> 输出指定 ADR 的机器审计证据报告
|
|
60
|
+
spec-guard mutate [options] 执行变异反向测试敏感度校验,防御回音壁假测试
|
|
61
|
+
spec-guard init 在当前项目初始化 spec-guard 规约配置
|
|
62
|
+
spec-guard help 显示帮助信息
|
|
63
|
+
|
|
64
|
+
选项:
|
|
65
|
+
--full 一键输出全景对账热力表 (覆盖断言、证据、接线三元)
|
|
66
|
+
--telemetry 严格模式下校验实测数据机器签名与凭据
|
|
67
|
+
--strict 严格模式:存在漂移、自述造假或孤儿断言时 Exit 1 (适合 CI)
|
|
68
|
+
--check-callers 检查架构函数在非测试生产代码中的接线调用者数 (Production Callers >= 1)
|
|
69
|
+
--overlay <path> 指定自定义 spec-matrix.overlay.yml 路径
|
|
70
|
+
--repo <path> 指定目标被测仓库根目录 (默认当前工作区)
|
|
71
|
+
--file <path> 变异测试目标被测文件路径
|
|
72
|
+
--cmd "<command>" 变异测试执行命令
|
|
73
|
+
--mutator <type> 变异策略 (auto | endpoint | boolean | operator | return)
|
|
74
|
+
--json 以 JSON 格式输出对账矩阵原始数据
|
|
75
|
+
--out <file> 将审计报告输出到指定文件
|
|
76
|
+
`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function handleInit() {
|
|
80
|
+
const cwd = getRepoRoot();
|
|
81
|
+
console.log(`\n🚀 正在当前目录初始化 spec-guard 规约配置: ${cwd}`);
|
|
82
|
+
|
|
83
|
+
const overlayTarget = path.join(cwd, 'spec-matrix.overlay.yml');
|
|
84
|
+
const overlaySource = path.join(TEMPLATES_DIR, 'spec-matrix.overlay.yml');
|
|
85
|
+
|
|
86
|
+
if (!fs.existsSync(overlayTarget) && fs.existsSync(overlaySource)) {
|
|
87
|
+
fs.copyFileSync(overlaySource, overlayTarget);
|
|
88
|
+
console.log(` ✅ 已创建可选跨端契约覆写层: spec-matrix.overlay.yml`);
|
|
89
|
+
} else {
|
|
90
|
+
console.log(` ℹ️ spec-matrix.overlay.yml 已存在或跳过`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 检查是否配置了 pre-commit
|
|
94
|
+
const hooksDir = path.join(cwd, '.githooks', 'pre-commit.d');
|
|
95
|
+
if (fs.existsSync(hooksDir)) {
|
|
96
|
+
const hookTarget = path.join(hooksDir, '15-spec-guard');
|
|
97
|
+
const hookSource = path.join(TEMPLATES_DIR, 'pre-commit.d', '15-spec-guard');
|
|
98
|
+
if (!fs.existsSync(hookTarget) && fs.existsSync(hookSource)) {
|
|
99
|
+
fs.copyFileSync(hookSource, hookTarget);
|
|
100
|
+
fs.chmodSync(hookTarget, 0o755);
|
|
101
|
+
console.log(` ✅ 已将轻量门禁接入 .githooks/pre-commit.d/15-spec-guard`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
console.log(`
|
|
106
|
+
🎉 spec-guard 初始化完成!
|
|
107
|
+
- 默认推行 Code-as-Spec-Matrix,请在单测中直接使用 @Invariant("ADR-XXX:H-Y") 标注;
|
|
108
|
+
- 运行 "spec-guard check" 即可随时对账架构覆盖率。\n`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function handleCheck() {
|
|
112
|
+
const repoRoot = getRepoRoot();
|
|
113
|
+
const isStrict = args.includes('--strict');
|
|
114
|
+
const isCheckCallers = args.includes('--check-callers');
|
|
115
|
+
const isFull = args.includes('--full');
|
|
116
|
+
const isTelemetry = args.includes('--telemetry');
|
|
117
|
+
const isJson = args.includes('--json');
|
|
118
|
+
const isIncremental = args.includes('--incremental') || args.includes('--staged');
|
|
119
|
+
|
|
120
|
+
let overlayPath = '';
|
|
121
|
+
const overlayIdx = args.indexOf('--overlay');
|
|
122
|
+
if (overlayIdx !== -1 && args[overlayIdx + 1]) {
|
|
123
|
+
overlayPath = args[overlayIdx + 1];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 增量/暂存区模式:仅当暂存区涉及规约、不变量或凭据改动时才执行门禁,否则快速放行 (< 0.02s)
|
|
127
|
+
if (isIncremental) {
|
|
128
|
+
let stagedFiles = [];
|
|
129
|
+
try {
|
|
130
|
+
const out = execSync('git diff --cached --name-only --diff-filter=ACMR', {
|
|
131
|
+
cwd: repoRoot,
|
|
132
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
133
|
+
encoding: 'utf8',
|
|
134
|
+
});
|
|
135
|
+
stagedFiles = out.split('\n').map((f) => f.trim()).filter(Boolean);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
// 非 git 目录或无暂存改动
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (stagedFiles.length > 0) {
|
|
141
|
+
const overlayCfg = loadOverlayConfig(repoRoot, overlayPath);
|
|
142
|
+
const configuredDirs = overlayCfg.adr_dirs || ['group/architecture/adr', 'docs/adr', 'adr', 'docs/proposals'];
|
|
143
|
+
|
|
144
|
+
const touchesSpec = stagedFiles.some((f) => {
|
|
145
|
+
if (/spec-matrix\.overlay\.ya?ml$/i.test(f)) return true;
|
|
146
|
+
if (/evidence\/[^/]+\.json$/i.test(f)) return true;
|
|
147
|
+
if (f.endsWith('.md') || f.endsWith('.markdown')) {
|
|
148
|
+
if (/(?:adr|decisions|proposals|architecture)/i.test(f)) return true;
|
|
149
|
+
return configuredDirs.some((d) => f.startsWith(d));
|
|
150
|
+
}
|
|
151
|
+
return false;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
let touchesInvariants = false;
|
|
155
|
+
if (!touchesSpec) {
|
|
156
|
+
try {
|
|
157
|
+
const invDiff = execSync('git diff --cached -G"@(i|I)nvariant" --name-only', {
|
|
158
|
+
cwd: repoRoot,
|
|
159
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
160
|
+
encoding: 'utf8',
|
|
161
|
+
});
|
|
162
|
+
touchesInvariants = invDiff.trim().length > 0;
|
|
163
|
+
} catch (e) {}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!touchesSpec && !touchesInvariants) {
|
|
167
|
+
if (!isJson) {
|
|
168
|
+
console.log(`\n🛡️ [spec-guard --incremental] 增量检查:暂存区未包含架构规约、不变量或凭据变更,门禁快速放行 (< 0.02s)。\n`);
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
if (!isJson) {
|
|
174
|
+
console.log(`\n🛡️ [spec-guard --incremental] 暂存区无改动文件,快速放行。\n`);
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (isFull) {
|
|
181
|
+
const triadResult = buildFullTriadMatrix(repoRoot, {
|
|
182
|
+
overlay: overlayPath,
|
|
183
|
+
checkCallers: isCheckCallers,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (isJson) {
|
|
187
|
+
console.log(JSON.stringify(triadResult, null, 2));
|
|
188
|
+
} else {
|
|
189
|
+
console.log(formatTriadHeatmap(triadResult));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (!triadResult.isHealthy) {
|
|
193
|
+
if (triadResult.totalAdrs === 0 || triadResult.zeroCoverage) {
|
|
194
|
+
console.error(`\x1b[31m❌ [spec-guard check --full] 门禁阻断:未发现任何有效 ADR 规约 (ZERO_COVERAGE)!零覆盖不可伪装为健康。\x1b[0m\n`);
|
|
195
|
+
} else {
|
|
196
|
+
console.error(`\x1b[31m❌ [spec-guard check --full] 门禁阻断:全景三元对账发现漂移、自述伪造或孤儿决策!\x1b[0m\n`);
|
|
197
|
+
}
|
|
198
|
+
process.exit(1);
|
|
199
|
+
}
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const result = buildAndCheckMatrix(repoRoot, {
|
|
204
|
+
overlay: overlayPath,
|
|
205
|
+
checkCallers: isCheckCallers,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
if (isJson) {
|
|
209
|
+
console.log(JSON.stringify(result, null, 2));
|
|
210
|
+
} else {
|
|
211
|
+
console.log(formatConsoleReport(result));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (result.zeroCoverage) {
|
|
215
|
+
console.error(`\x1b[31m⚠️ [spec-guard] 零覆盖警示:当前工程未发现任何有效 ADR 规约 (ZERO_COVERAGE)!\x1b[0m\n`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (isTelemetry) {
|
|
219
|
+
const telResult = verifyAllAdrsTelemetry(repoRoot, { overlay: overlayPath });
|
|
220
|
+
if (!telResult.isHealthy) {
|
|
221
|
+
console.error(`\x1b[31m❌ [spec-guard --telemetry] 实测数据自述校验失败:存在违背 ADR-064 机器信任根的伪造指标!\x1b[0m\n`);
|
|
222
|
+
process.exit(1);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (isCheckCallers && result.unwiredInvariants && result.unwiredInvariants.length > 0) {
|
|
227
|
+
console.error(`\x1b[31m❌ [spec-guard --check-callers] 门禁阻断:检测到 ${result.unwiredInvariants.length} 处架构函数未在生产接线 (Zero Production Callers)!\x1b[0m\n`);
|
|
228
|
+
process.exit(1);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (isStrict && (!result.isHealthy || result.zeroCoverage)) {
|
|
232
|
+
console.error(`\x1b[31m❌ [spec-guard --strict] 严格模式检查失败:检测到未修复的架构漂移、孤儿断言或零覆盖 (ZERO_COVERAGE)!\x1b[0m\n`);
|
|
233
|
+
process.exit(1);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function handleVerifyEvidence(targetAdrId) {
|
|
238
|
+
const repoRoot = getRepoRoot();
|
|
239
|
+
console.log(`\n🛡️ [spec-guard verify-evidence] 正在校验实测数据机器信任根凭证 (ADR-064): ${repoRoot}`);
|
|
240
|
+
|
|
241
|
+
let overlayPath = '';
|
|
242
|
+
const overlayIdx = args.indexOf('--overlay');
|
|
243
|
+
if (overlayIdx !== -1 && args[overlayIdx + 1]) {
|
|
244
|
+
overlayPath = args[overlayIdx + 1];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const result = verifyAllAdrsTelemetry(repoRoot, { targetAdrId, overlay: overlayPath });
|
|
248
|
+
|
|
249
|
+
if (result.auditedCount === 0 && result.skippedCount === 0) {
|
|
250
|
+
console.log(`ℹ️ 未找到匹配的 ADR 规约文件。`);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
console.log(
|
|
255
|
+
`\n📊 审计规约数: ${result.totalAdrs} | 量化指标 ADR: ${result.auditedCount} | 验签通过: \x1b[32m${result.passedCount}\x1b[0m | 自述漂移: \x1b[31m${result.failedCount}\x1b[0m | 豁免: ${result.skippedCount}\n`
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
for (const r of result.reports) {
|
|
259
|
+
if (r.status === 'FAIL') {
|
|
260
|
+
console.error(`\x1b[31m❌ [FAIL] ${r.reason}\x1b[0m`);
|
|
261
|
+
console.error(` 文件: ${r.filePath}`);
|
|
262
|
+
for (const d of r.details) {
|
|
263
|
+
console.error(` └─ 违规详情: ${d}`);
|
|
264
|
+
}
|
|
265
|
+
} else if (r.status === 'PASS') {
|
|
266
|
+
console.log(`\x1b[32m✅ [PASS] ${r.adrId}\x1b[0m: ${r.reason}`);
|
|
267
|
+
} else {
|
|
268
|
+
console.log(`ℹ️ [SKIP] ${r.adrId}: ${r.reason}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (!result.isHealthy) {
|
|
273
|
+
console.error(
|
|
274
|
+
`\n\x1b[31m❌ [spec-guard verify-evidence] 门禁阻断:检测到 ${result.failedCount} 处 ADR 实测数据自述漂移,违背 ADR-064 机器信任根!\x1b[0m\n`
|
|
275
|
+
);
|
|
276
|
+
process.exit(1);
|
|
277
|
+
} else {
|
|
278
|
+
console.log(`\n\x1b[32m✅ [spec-guard verify-evidence] 校验通过!所有量化指标均与机器实测凭证吻合。\x1b[0m\n`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function handleCheckWiring(targetAdrId) {
|
|
283
|
+
const repoRoot = getRepoRoot();
|
|
284
|
+
console.log(`\n🛡️ [spec-guard check-wiring] 正在反查 ADR 决策在生产代码中的物理接线: ${repoRoot}`);
|
|
285
|
+
|
|
286
|
+
let overlayPath = '';
|
|
287
|
+
const overlayIdx = args.indexOf('--overlay');
|
|
288
|
+
if (overlayIdx !== -1 && args[overlayIdx + 1]) {
|
|
289
|
+
overlayPath = args[overlayIdx + 1];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const result = traceAllAdrsWiring(repoRoot, { targetAdrId, overlay: overlayPath });
|
|
293
|
+
|
|
294
|
+
console.log(
|
|
295
|
+
`\n📊 规约总数: ${result.totalAdrs} | 🔒Frozen: ${result.frozenAdrsCount} | 孤儿决策: \x1b[31m${result.orphanAdrsCount}\x1b[0m | 接线存活: \x1b[32m${result.passedCount}\x1b[0m\n`
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
for (const r of result.reports) {
|
|
299
|
+
if (r.isOrphan) {
|
|
300
|
+
console.error(`\x1b[31m🚨 [FAIL] ${r.reason}\x1b[0m`);
|
|
301
|
+
console.error(` 规约路径: ${r.filePath}`);
|
|
302
|
+
console.error(` 候选符号: ${r.candidateSymbols.join(', ')}`);
|
|
303
|
+
} else if (r.status === 'PASS') {
|
|
304
|
+
console.log(`\x1b[32m✅ [PASS] ${r.adrId}\x1b[0m: ${r.reason}`);
|
|
305
|
+
} else if (r.status === 'SKIPPED') {
|
|
306
|
+
console.log(`ℹ️ [SKIP] ${r.adrId}: ${r.reason}`);
|
|
307
|
+
} else {
|
|
308
|
+
console.log(`⏳ [UNWIRED] ${r.adrId}: ${r.reason}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (!result.isHealthy) {
|
|
313
|
+
console.error(
|
|
314
|
+
`\n\x1b[31m❌ [spec-guard check-wiring] 门禁阻断:检测到 ${result.orphanAdrsCount} 处 🔒Frozen 规约在生产代码中零命中孤儿漂移!\x1b[0m\n`
|
|
315
|
+
);
|
|
316
|
+
process.exit(1);
|
|
317
|
+
} else {
|
|
318
|
+
console.log(`\n\x1b[32m✅ [spec-guard check-wiring] 校验通过!生产代码接线正常。\x1b[0m\n`);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function handleAuditAdr(adrId) {
|
|
323
|
+
if (!adrId) {
|
|
324
|
+
console.error('错误: 请提供 ADR 编号,例如 "spec-guard audit-adr ADR-060"');
|
|
325
|
+
process.exit(1);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const cwd = getRepoRoot();
|
|
329
|
+
const result = buildAndCheckMatrix(cwd);
|
|
330
|
+
const report = generateAdrAuditReport(result, adrId);
|
|
331
|
+
|
|
332
|
+
const outIdx = args.indexOf('--out');
|
|
333
|
+
if (outIdx !== -1 && args[outIdx + 1]) {
|
|
334
|
+
const outFile = path.resolve(cwd, args[outIdx + 1]);
|
|
335
|
+
fs.writeFileSync(outFile, report, 'utf8');
|
|
336
|
+
console.log(`✅ 审计报告已写入: ${outFile}`);
|
|
337
|
+
} else {
|
|
338
|
+
console.log('\n' + report);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// [C5] 退出码严格判定:未找到 ADR、存在架构漂移、或覆盖率为 0% 时 Exit 1
|
|
342
|
+
const rep = result.adrReports.find((r) => r.adr.adrId.toUpperCase() === adrId.toUpperCase());
|
|
343
|
+
if (!rep || rep.invariants.length === 0 || rep.invariants.some((i) => i.status === 'drift_detected')) {
|
|
344
|
+
process.exit(1);
|
|
345
|
+
}
|
|
346
|
+
const verifiedCount = rep.invariants.filter((i) => i.status === 'verified').length;
|
|
347
|
+
if (rep.invariants.length > 0 && verifiedCount === 0) {
|
|
348
|
+
process.exit(1);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function handleMutate() {
|
|
353
|
+
const cwd = getRepoRoot();
|
|
354
|
+
const fileIdx = args.indexOf('--file') !== -1 ? args.indexOf('--file') : args.indexOf('--target');
|
|
355
|
+
const targetFile = fileIdx !== -1 && args[fileIdx + 1] ? args[fileIdx + 1] : '';
|
|
356
|
+
|
|
357
|
+
const cmdIdx = args.indexOf('--cmd');
|
|
358
|
+
const testCmd = cmdIdx !== -1 && args[cmdIdx + 1] ? args[cmdIdx + 1] : '';
|
|
359
|
+
|
|
360
|
+
const mutatorIdx = args.indexOf('--mutator');
|
|
361
|
+
const mutator = mutatorIdx !== -1 && args[mutatorIdx + 1] ? args[mutatorIdx + 1] : 'auto';
|
|
362
|
+
|
|
363
|
+
const isJson = args.includes('--json');
|
|
364
|
+
|
|
365
|
+
if (!targetFile || !testCmd) {
|
|
366
|
+
console.error('用法: spec-guard mutate --file <target-file> --cmd "<test-command>" [--mutator <type>]');
|
|
367
|
+
process.exit(1);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const result = runMutationTest({
|
|
371
|
+
repoRoot: cwd,
|
|
372
|
+
targetFile,
|
|
373
|
+
testCmd,
|
|
374
|
+
mutator,
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
if (isJson) {
|
|
378
|
+
console.log(JSON.stringify(result, null, 2));
|
|
379
|
+
} else {
|
|
380
|
+
if (result.status === 'MUTATION_KILLED') {
|
|
381
|
+
console.log(`\n\x1b[32m${result.message}\x1b[0m\n`);
|
|
382
|
+
} else {
|
|
383
|
+
console.error(`\n\x1b[31m${result.message}\x1b[0m\n`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
process.exit(result.exitCode);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const targetAdrArg = args[1] && !args[1].startsWith('--') ? args[1] : null;
|
|
391
|
+
|
|
392
|
+
switch (command) {
|
|
393
|
+
case '--version':
|
|
394
|
+
case '-v':
|
|
395
|
+
case 'version':
|
|
396
|
+
console.log(`spec-guard CLI · v${SPEC_GUARD_VERSION} (units/spec-guard)`);
|
|
397
|
+
process.exit(0);
|
|
398
|
+
break;
|
|
399
|
+
case 'init':
|
|
400
|
+
handleInit();
|
|
401
|
+
break;
|
|
402
|
+
case 'check':
|
|
403
|
+
handleCheck();
|
|
404
|
+
break;
|
|
405
|
+
case 'verify-evidence':
|
|
406
|
+
handleVerifyEvidence(targetAdrArg);
|
|
407
|
+
break;
|
|
408
|
+
case 'check-wiring':
|
|
409
|
+
handleCheckWiring(targetAdrArg);
|
|
410
|
+
break;
|
|
411
|
+
case 'mutate':
|
|
412
|
+
handleMutate();
|
|
413
|
+
break;
|
|
414
|
+
case 'audit-adr':
|
|
415
|
+
handleAuditAdr(args[1]);
|
|
416
|
+
break;
|
|
417
|
+
case 'help':
|
|
418
|
+
case '--help':
|
|
419
|
+
case '-h':
|
|
420
|
+
printHelp();
|
|
421
|
+
break;
|
|
422
|
+
default:
|
|
423
|
+
if (command.startsWith('ADR-')) {
|
|
424
|
+
handleAuditAdr(command);
|
|
425
|
+
} else {
|
|
426
|
+
console.error(`未知命令: ${command}`);
|
|
427
|
+
printHelp();
|
|
428
|
+
process.exit(1);
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
}
|