repo-audit-tool 1.2.0

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.
Files changed (117) hide show
  1. package/AGENTS.md +67 -0
  2. package/AUDIT.md +802 -0
  3. package/BEST-PRACTICES.md +268 -0
  4. package/CLAUDE.md +1 -0
  5. package/CONTRIBUTING.md +42 -0
  6. package/DEVELOPMENT.md +112 -0
  7. package/HANDOFF.md +124 -0
  8. package/LICENSE +21 -0
  9. package/PUBLISHING.md +66 -0
  10. package/README.en.md +71 -0
  11. package/README.md +71 -0
  12. package/REPO-AUDIT.md +107 -0
  13. package/REPO-CLASSIFICATION.md +137 -0
  14. package/SECURITY.md +19 -0
  15. package/docs/repo-audit/AGENT-GUIDE.md +474 -0
  16. package/docs/repo-audit/HUMAN-GUIDE.md +749 -0
  17. package/package.json +36 -0
  18. package/repo-audit.mjs +1507 -0
  19. package/rules/categories/archive.yaml +48 -0
  20. package/rules/categories/content.yaml +34 -0
  21. package/rules/categories/dsh-plugin.yaml +79 -0
  22. package/rules/categories/go-service.yaml +67 -0
  23. package/rules/categories/product-oss.yaml +75 -0
  24. package/rules/categories/python-app.yaml +60 -0
  25. package/rules/categories/sandbox.yaml +33 -0
  26. package/rules/domains/docs.yaml +105 -0
  27. package/rules/domains/git.yaml +52 -0
  28. package/rules/domains/quality.yaml +69 -0
  29. package/rules/domains/security.yaml +79 -0
  30. package/shim/repo-audit.cmd +7 -0
  31. package/shim/repo-audit.ps1 +4 -0
  32. package/shim/repo-audit.sh +5 -0
  33. package/templates/AGENT-GUIDE.md +331 -0
  34. package/templates/README.md +192 -0
  35. package/templates/UPDATE-MODEL.md +141 -0
  36. package/templates/UPDATE-PLAN.md +79 -0
  37. package/templates/categories/content/AGENTS-append.md +6 -0
  38. package/templates/categories/content/CATEGORY.md +26 -0
  39. package/templates/categories/content/README.en.md +12 -0
  40. package/templates/categories/content/README.md +14 -0
  41. package/templates/categories/content/gitignore-append.md +6 -0
  42. package/templates/categories/dsh-plugin/AGENTS-append.md +21 -0
  43. package/templates/categories/dsh-plugin/CATEGORY.md +9 -0
  44. package/templates/categories/dsh-plugin/CONTRIBUTING-append.md +22 -0
  45. package/templates/categories/dsh-plugin/DEVELOPMENT-append.md +71 -0
  46. package/templates/categories/dsh-plugin/PUBLISHING-append.md +53 -0
  47. package/templates/categories/dsh-plugin/README-append.md +28 -0
  48. package/templates/categories/dsh-plugin/README.en.md +44 -0
  49. package/templates/categories/dsh-plugin/gitignore-append.md +15 -0
  50. package/templates/categories/go-service/.githooks/pre-commit +13 -0
  51. package/templates/categories/go-service/.github/workflows/ci.yml +31 -0
  52. package/templates/categories/go-service/.github/workflows/release.yml +58 -0
  53. package/templates/categories/go-service/.goreleaser.yaml +86 -0
  54. package/templates/categories/go-service/AGENTS-append.md +7 -0
  55. package/templates/categories/go-service/CATEGORY.md +33 -0
  56. package/templates/categories/go-service/CONTRIBUTING-append.md +13 -0
  57. package/templates/categories/go-service/DEVELOPMENT-append.md +21 -0
  58. package/templates/categories/go-service/Makefile +21 -0
  59. package/templates/categories/go-service/PUBLISHING-append.md +48 -0
  60. package/templates/categories/go-service/PUBLISHING.md +59 -0
  61. package/templates/categories/go-service/README-append.md +26 -0
  62. package/templates/categories/go-service/README.en.md +43 -0
  63. package/templates/categories/go-service/gitignore-append.md +13 -0
  64. package/templates/categories/product-oss/.github/CODEOWNERS +6 -0
  65. package/templates/categories/product-oss/.github/ISSUE_TEMPLATE/bug_report.yml +52 -0
  66. package/templates/categories/product-oss/.github/ISSUE_TEMPLATE/config.yml +8 -0
  67. package/templates/categories/product-oss/.github/dependabot.yml +28 -0
  68. package/templates/categories/product-oss/.github/workflows/dependency-review.yml +18 -0
  69. package/templates/categories/product-oss/.github/workflows/release-please.yml +39 -0
  70. package/templates/categories/product-oss/.release-please-manifest.json +3 -0
  71. package/templates/categories/product-oss/CATEGORY.md +27 -0
  72. package/templates/categories/product-oss/CHANGELOG.md +14 -0
  73. package/templates/categories/product-oss/PULL_REQUEST_TEMPLATE.md +15 -0
  74. package/templates/categories/product-oss/SUPPORT.md +71 -0
  75. package/templates/categories/product-oss/release-please-config.json +10 -0
  76. package/templates/categories/python-app/.githooks/pre-commit +13 -0
  77. package/templates/categories/python-app/.github/workflows/ci.yml +33 -0
  78. package/templates/categories/python-app/.github/workflows/publish.yml +112 -0
  79. package/templates/categories/python-app/AGENTS-append.md +18 -0
  80. package/templates/categories/python-app/CATEGORY.md +34 -0
  81. package/templates/categories/python-app/CONTRIBUTING-append.md +13 -0
  82. package/templates/categories/python-app/DEVELOPMENT-append.md +21 -0
  83. package/templates/categories/python-app/PUBLISHING-append.md +52 -0
  84. package/templates/categories/python-app/PUBLISHING.md +61 -0
  85. package/templates/categories/python-app/README-append.md +27 -0
  86. package/templates/categories/python-app/README.en.md +44 -0
  87. package/templates/categories/python-app/gitignore-append.md +19 -0
  88. package/templates/categories/python-app/pyproject.toml +41 -0
  89. package/templates/categories/python-app/scripts/verify.py +39 -0
  90. package/templates/categories/sandbox/AGENTS-append.md +7 -0
  91. package/templates/categories/sandbox/CATEGORY.md +31 -0
  92. package/templates/categories/sandbox/README.en.md +24 -0
  93. package/templates/categories/sandbox/README.md +23 -0
  94. package/templates/check-single-source.mjs +107 -0
  95. package/templates/common/AGENTS-core.md +28 -0
  96. package/templates/common/CONTRIBUTING-core.md +21 -0
  97. package/templates/common/DEVELOPMENT-core.md +45 -0
  98. package/templates/common/PUBLISHING-core.md +31 -0
  99. package/templates/common/README-core.md +60 -0
  100. package/templates/common/SECURITY-core.md +19 -0
  101. package/templates/common/copilot-instructions.md +5 -0
  102. package/templates/common/gitignore-core.md +11 -0
  103. package/templates/repo-root/.githooks/pre-commit +20 -0
  104. package/templates/repo-root/.github/workflows/ci.yml +45 -0
  105. package/templates/repo-root/.github/workflows/publish.yml +77 -0
  106. package/templates/repo-root/CLAUDE.md +3 -0
  107. package/templates/repo-root/LICENSE +21 -0
  108. package/templates/repo-root/package.json +71 -0
  109. package/templates/repo-root/scripts/build.mjs +24 -0
  110. package/templates/repo-root/scripts/check-deploy.mjs +176 -0
  111. package/templates/repo-root/scripts/verify.mjs +52 -0
  112. package/templates/repo-root/src/config.ts +25 -0
  113. package/templates/repo-root/src/index.ts +40 -0
  114. package/templates/repo-root/tsconfig.build.json +22 -0
  115. package/templates/repo-root/tsconfig.json +23 -0
  116. package/templates/scaffold.mjs +793 -0
  117. package/templates/skill/SKILL.md +47 -0
@@ -0,0 +1,474 @@
1
+ # repo-audit Agent 操作手册
2
+
3
+ > 机器可读的操作协议。Agent 直接执行,无需人类解释。
4
+
5
+ ---
6
+
7
+ ## 一、工具定位
8
+
9
+ `repo-audit` 是一个**纯只读**的仓库审计工具。输入:仓库路径 + 可选参数。输出:审计报告(JSON + Markdown)+ 退出码。不修改目标仓库任何文件。
10
+
11
+ ---
12
+
13
+ ## 二、调用协议
14
+
15
+ ### 2.1 基本调用
16
+
17
+ ```bash
18
+ node <repo-audit-path>/repo-audit.mjs --repo <repo-path> [options]
19
+ ```
20
+
21
+ ### 2.2 参数清单
22
+
23
+ | 参数 | 必填 | 说明 | 示例 |
24
+ |---|---|---|---|
25
+ | `--repo` | 否 | 目标仓库(默认 `.`)| `--repo /path/to/repo` |
26
+ | `--type` | 否 | 强制分类(跳过检测)| `--type python-app` |
27
+ | `--format` | 否 | 输出格式 | `--format json` |
28
+ | `--output` | 否 | 输出目录 | `--output /tmp/audit` |
29
+ | `--strict` | 否 | 严格模式 | `--strict` |
30
+ | `--rules` | 否 | 自定义规则(可多次)| `--rules a.yaml --rules b.yaml` |
31
+ | `--dim` | 否 | 只审计指定维度(可多次)| `--dim security --dim docs` |
32
+ | `--llm-provider` | 否 | LLM 供应商 | `--llm-provider deepseek` |
33
+ | `--llm-model` | 否 | 模型名称 | `--llm-model deepseek-chat` |
34
+ | `--help` | — | 帮助信息 | `--help` |
35
+
36
+ ### 2.3 环境变量
37
+
38
+ | 变量 | 必填 | 说明 | 默认 |
39
+ |---|---|---|---|
40
+ | `LLM_PROVIDER` | 否 | 协议或别名(openai/anthropic/none/openrouter/deepseek/qwen等) | `none` |
41
+ | `LLM_API_KEY` | LLM 时需要 | API Key | 无 |
42
+ | `LLM_MODEL` | 否 | 模型名称 | `auto` |
43
+ | `LLM_BASE_URL` | 非默认端点时必填 | 自定义 API 端点 | 协议默认 |
44
+ | `REPO_AUDIT_TYPES` | 否 | 自定义分类列表 | 内置 |
45
+ | `REPO_AUDIT_WAIVE` | 否 | 命令行豁免规则 ID(逗号分隔,替代 .auditrc.yaml)| 无 |
46
+
47
+ ### 2.4 退出码
48
+
49
+ | 码 | 含义 | Agent 行为 |
50
+ |---|---|---|
51
+ | `0` | 通过 | 记录评分,继续 |
52
+ | `1` | 有 Critical/Major + --strict | 告警,报告给调用方 |
53
+ | `2` | 参数错误 | 修正参数后重试 |
54
+ | `3` | 非 git 仓库 | 报告错误,不继续 |
55
+
56
+ ---
57
+
58
+ ## 三、输出解析
59
+
60
+ ### 3.1 JSON 报告结构(机器可读)
61
+
62
+ ```json
63
+ {
64
+ "generated_at": "<ISO timestamp>",
65
+ "audit_type": "<分类名>",
66
+ "llm_used": <bool>,
67
+ "metadata": {
68
+ "remote": "<git remote URL or null>",
69
+ "branch": "<branch name or '(detached)'>",
70
+ "commitCount": <int>,
71
+ "fileCount": <int>,
72
+ "lastCommit": "<ISO date>"
73
+ },
74
+ "summary": {
75
+ "critical": <int>,
76
+ "major": <int>,
77
+ "minor": <int>,
78
+ "info": <int>,
79
+ "pass": <int>,
80
+ "total": <int>,
81
+ "llmUsed": <bool>,
82
+ "waived": <int>
83
+ },
84
+ "findings": [
85
+ {
86
+ "id": "<规则ID>",
87
+ "title": "<规则标题>",
88
+ "severity": "critical\|major\|minor\|info",
89
+ "domain": "<维度>",
90
+ "applies_to": ["<分类列表>"],
91
+ "status": "pass\|fail\|llm\|waived",
92
+ "message": "<描述>",
93
+ "evidence": "<检查结果>",
94
+ "hint": "<修复建议 or null>",
95
+ "template_ref": "<模板路径 or null>",
96
+ "waived_reason": "<豁免原因 or null>",
97
+ "waived_since": "<豁免日期 or null>",
98
+ "llm_analysis": { ... } or null
99
+ }
100
+ ]
101
+ }
102
+ ```
103
+
104
+ ### 3.2 评分计算
105
+
106
+ ```
107
+ score = max(0, 100 - critical×25 - major×10 - minor×3)
108
+ grade = score≥90→A, ≥80→B, ≥70→C, ≥60→D, <60→F
109
+ ```
110
+
111
+ ### 3.3 Agent 应关注的字段
112
+
113
+ | 场景 | 关注字段 |
114
+ |---|---|
115
+ | CI 门禁 | `summary.critical`, `summary.major`, `exit_code==1` |
116
+ | 生成后验证 | `findings.filter(f=>f.status==='fail')` 列表 |
117
+ | 升级前后对比 | `summary` 中的各级别计数差值 |
118
+ | LLM 增强 | `findings[*].llm_analysis.root_cause` |
119
+
120
+ ### 3.4 仓库级豁免(.auditrc.yaml)
121
+
122
+ 当某条规则为**已知接受的历史决策**(非工具缺陷),可创建 `.auditrc.yaml` 豁免,避免每次审计重报:
123
+
124
+ ```yaml
125
+ # .auditrc.yaml(受版本控制,团队共享)
126
+ waive:
127
+ - id: GIT-001
128
+ reason: "历史 commit fix(template)+docs 单条非标准;Conventional Commits 145 条 1 miss,已知接受不回改"
129
+ since: "2026-09-06"
130
+ - id: DOC-002
131
+ reason: "纯中文项目,无英文镜像需求"
132
+ since: "2026-09-06"
133
+ ```
134
+
135
+ **行为**:
136
+ - 规则照常运行,finding 标 `status: "waived"` + `waived_reason`
137
+ - 评分**排除 waived 项**(`summary.waived` 计数)
138
+ - 命令行批量豁免:`--waive GIT-001 --waive DOC-002`
139
+
140
+ **触发条件**(自动识别,无需手动配置):
141
+ - README 首段含 `自用/solo/personal/个人` → `personal` 模式,自动豁免 7 条规则
142
+ - 最后 commit 含 `archived`/`封版` → `archive` 分类,仅 SEC-001/SEC-003
143
+
144
+ ---
145
+
146
+ ### 3.5 Monorepo 感知(P6)
147
+
148
+ 工具自动探测 workspace 布局,无需手动指定子目录:
149
+
150
+ ```yaml
151
+ # neonforge 结构示例
152
+ neonforge/
153
+ apps/
154
+ desktop/
155
+ package.json ← 工程实体
156
+ package-lock.json ← 锁文件
157
+ package.json ← 根(可能无或仅为 workspace 根)
158
+ ```
159
+
160
+ **行为**:
161
+ - `detectType`:根无 `package.json` 时递归搜索 `apps/*/packages/*/workspaces/*` 等常见布局
162
+ - `file_exists` fallback:`package.json` 不存在于根时自动使用 workspace 位置
163
+ - `QUA-004` 锁文件:根无锁文件时搜索 workspace 子目录
164
+
165
+ **分类影响**:monorepo 正确识别为 `javascript`/`npm-package`,而非误判为 `content`。
166
+
167
+ **已知限制**:
168
+ - `json_field` 检查器(DOC-003b 等)仍只读仓库根路径,不递归 workspace
169
+ - 若规则依赖 `package.json` 字段(如 `license`),monorepo 下可能 false negative
170
+ - workaround:`--type` 强制分类 + 人工确认子目录配置
171
+
172
+ ---
173
+
174
+ ## 四、标准操作流程
175
+
176
+ ### 4.1 新建仓库后验证
177
+
178
+ ```bash
179
+ # 1. scaffold 生成
180
+ node scaffold.mjs --type <type> --name <name> --org <org> --desc "<desc>"
181
+
182
+ # 2. 进入新仓
183
+ cd <name>
184
+
185
+ # 3. 跑审计
186
+ node ../repo-audit.mjs --format json --output ./audit-report
187
+
188
+ # 4. 检查关键发现
189
+ cat audit-report/report.json | jq '.findings[] | select(.status=="fail") | .id'
190
+
191
+ # 5. 如有 Critical 发现,阻断生成流程
192
+ ```
193
+
194
+ ### 4.2 模板升级后校验
195
+
196
+ ```bash
197
+ # 1. 审计当前状态
198
+ node repo-audit.mjs --repo <repo> --format json --output ./audit-before
199
+
200
+ # 2. 跑 scaffold update
201
+ node scaffold.mjs --update <repo>
202
+
203
+ # 3. 再次审计
204
+ node repo-audit.mjs --repo <repo> --format json --output ./audit-after
205
+
206
+ # 4. 对比
207
+ before=$(cat ./audit-before/report.json | jq '.summary')
208
+ after=$(cat ./audit-after/report.json | jq '.summary')
209
+ # 如果 after.critical > before.critical → 告警:模板升级引入了问题
210
+ ```
211
+
212
+ ### 4.3 CI 门禁集成
213
+
214
+ ```yaml
215
+ # GitHub Actions 示例
216
+ - name: Audit repo
217
+ run: |
218
+ node repo-audit.mjs --repo . --strict --format json --output ./.ci-audit
219
+ echo "Score: $(jq '.summary | 100 - (.critical * 25) - (.major * 10) - (.minor * 3)' ./.ci-audit/report.json)"
220
+ ```
221
+
222
+ ### 4.4 增量维度审计
223
+
224
+ ```bash
225
+ # 只审计安全(快速)
226
+ node repo-audit.mjs --dim security --format json
227
+
228
+ # 只审计文档
229
+ node repo-audit.mjs --dim docs --format json
230
+
231
+ # 审计所有工程类维度
232
+ node repo-audit.mjs --dim git --dim docs --dim security --dim quality --format json
233
+ ```
234
+
235
+ ### 4.5 自定义规则叠加
236
+
237
+ ```bash
238
+ # 创建自定义规则
239
+ cat > /tmp/my-rules.yaml << 'EOF'
240
+ rules:
241
+ - id: CUSTOM-001
242
+ title: "检查特定文件存在"
243
+ severity: minor
244
+ domain: custom
245
+ applies_to: ["*"]
246
+ check: file_exists
247
+ params:
248
+ paths: ["SPECIAL_FILE.md"]
249
+ description: "项目需要 SPECIAL_FILE.md"
250
+ EOF
251
+
252
+ # 叠加运行
253
+ node repo-audit.mjs --repo /path/to/repo --rules /tmp/my-rules.yaml --format json
254
+ ```
255
+
256
+ ---
257
+
258
+ ## 五、错误处理协议
259
+
260
+ ### 5.4 GIT-004 非 scaffold 仓豁免
261
+
262
+ `GIT-004` 检查 `.scaffold/lock/manifest.json` 是否存在。对**从未被 scaffold 管理的仓库**(如 monorepo subtree split 迁出的仓),工具会自动识别父目录 `.scaffold` 不存在并记 `pass`(evidence 提示「非 scaffold 管理仓,豁免」)。Agent 无需特殊处理,视为正常通过即可。
263
+
264
+ ### 5.1 非 git 仓库(exit 3)
265
+
266
+ ```bash
267
+ # 检测
268
+ if ! git -C <repo> rev-parse --is-inside-work-tree >/dev/null 2>&1; then
269
+ echo "ERROR: <repo> is not a git repository"
270
+ exit 3
271
+ fi
272
+ ```
273
+
274
+ ### 5.2 LLM 不可用时
275
+
276
+ LLM 未配置时工具自动降级为纯规则模式,不影响结果完整性。Agent 不应将其视为错误:
277
+
278
+ ```bash
279
+ # 检查 LLM 是否启用
280
+ if [ -z "$LLM_API_KEY" ]; then
281
+ echo "INFO: LLM not configured, running in rule-only mode"
282
+ fi
283
+ ```
284
+
285
+ ### 5.3 参数错误(exit 2)
286
+
287
+ ```bash
288
+ # 捕获 stderr
289
+ output=$(node repo-audit.mjs --repo <repo> 2>&1)
290
+ exit_code=$?
291
+ if [ $exit_code -eq 2 ]; then
292
+ echo "ERROR: $output"
293
+ # 打印帮助
294
+ node repo-audit.mjs --help
295
+ fi
296
+ ```
297
+
298
+ ### 5.4 规则加载失败
299
+
300
+ 如果自定义规则文件不存在或格式错误,工具会输出警告并继续(只加载成功的规则)。
301
+
302
+ ---
303
+
304
+ ## 六、性能参考
305
+
306
+ | 指标 | 值 |
307
+ |---|---|
308
+ | 规则检查(28 条) | ~50ms |
309
+ | LLM 增强(5 条 fail) | ~5-15s(取决于网络和模型)|
310
+ | 大仓(2000+ 文件)grep 扫描 | ~200-500ms |
311
+ | 总耗时(无 LLM) | <100ms |
312
+ | 总耗时(有 LLM) | <20s |
313
+
314
+ ---
315
+
316
+ ## 七、Agent 决策树
317
+
318
+ ```
319
+ 接收任务:审计仓库 <path>
320
+ │
321
+ ├─ 检查是否是 git 仓库 ── 否 ──→ 报错 exit 3
322
+ │
323
+ ├─ 自动检测分类 ── 置信度低 ──→ 询问用户或强制 --type
324
+ │
325
+ ├─ 加载规则 ── 内置 + 自定义 ──→ 按 applies_to 过滤
326
+ │
327
+ ├─ 执行规则检查 ── 全量覆盖 ──→ 生成 findings
328
+ │
329
+ ├─ 检查 LLM 配置 ── 有 ──→ 对 fail 发现做增强(最多 5 条)
330
+ │ ── 无 ──→ 跳过
331
+ │
332
+ ├─ 生成报告 ── JSON + Markdown ──→ 写入 <output>/
333
+ │
334
+ └─ 输出摘要 + 退出码
335
+ ├─ exit 0 ──→ 通过
336
+ ├─ exit 1 ──→ 有 Critical/Major,报告详情
337
+ └─ exit 2/3 ──→ 报错
338
+ ```
339
+
340
+ ---
341
+
342
+ ## 八、操作授权与完成路径
343
+
344
+ ### 8.1 权限需求
345
+
346
+ `repo-audit` 是**纯只读工具**,Agent 无需任何写权限即可执行。
347
+
348
+ | 操作 | 权限 | Agent 行为 |
349
+ |---|---|---|
350
+ | 读仓库 | 读权限 | 直接执行,无需确认 |
351
+ | 写报告 | 输出目录写权限 | 自动创建 `mkdir -p` |
352
+ | LLM 调用 | `LLM_API_KEY` | 环境变量已配置才启用 |
353
+ | CI 集成 | GitHub OIDC | 已在 workflow permissions 中配置 |
354
+
355
+ ### 8.2 执行路径(Agent 视角)
356
+
357
+ ```
358
+ Agent 接收审计任务
359
+ │
360
+ ├─ ① 参数构建
361
+ │ ├─ repo 路径来自任务参数
362
+ │ ├─ type 优先用参数,否则自动推断
363
+ │ └─ format 默认 json(机器可读)
364
+ │
365
+ ├─ ② 执行命令
366
+ │ cmd = `node repo-audit.mjs --repo <path> --format json [--strict] [--dim <dims>]`
367
+ │ timeout = 30s(无 LLM)/ 120s(有 LLM)
368
+ │
369
+ ├─ ③ 结果解析
370
+ │ ├─ exit 0 ──→ 审计通过,解析 summary
371
+ │ ├─ exit 1 ──→ 有 Critical/Major,提取 fail findings
372
+ │ ├─ exit 2 ──→ 参数错误,检查 stderr 修正后重试
373
+ │ └─ exit 3 ──→ 非 git 仓,报告错误不重试
374
+ │
375
+ ├─ ④ 行动决策
376
+ │ ├─ exit 0 ──→ 记录评分,返回结果
377
+ │ ├─ exit 1 + strict ──→ 告警调用方,附 findings 详情
378
+ │ └─ LLM 增强成功 ──→ 附加 llm_analysis 给调用方
379
+ │
380
+ └─ ⑤ 完成
381
+ ├─ 报告文件已写入 <output>/report.json
382
+ ├─ stdout 摘要已捕获
383
+ └─ 返回结构化结果给调用方
384
+ ```
385
+
386
+ ### 8.3 Agent 标准调用模板
387
+
388
+ ```python
389
+ # 伪代码:Agent 调用 repo-audit
390
+ def audit_repo(repo_path: str, strict: bool = False, dims: list[str] = None) -> dict:
391
+ cmd = [
392
+ "node", "repo-audit.mjs",
393
+ "--repo", repo_path,
394
+ "--format", "json",
395
+ "--output", f"{repo_path}/.audit/{timestamp}"
396
+ ]
397
+ if strict:
398
+ cmd.append("--strict")
399
+ if dims:
400
+ for d in dims:
401
+ cmd.extend(["--dim", d])
402
+
403
+ result = run_command(cmd, timeout=120)
404
+
405
+ if result.exit_code == 0:
406
+ return {"status": "pass", "score": calculate_score(result.json)}
407
+ elif result.exit_code == 1:
408
+ return {"status": "fail", "findings": result.json["findings"], "score": calculate_score(result.json)}
409
+ elif result.exit_code == 3:
410
+ return {"status": "error", "reason": "not_git", "repo": repo_path}
411
+ else:
412
+ return {"status": "error", "reason": "runtime", "stderr": result.stderr}
413
+ ```
414
+
415
+ ### 8.4 完成判定(Agent)
416
+
417
+ | 指标 | 通过条件 |
418
+ |---|---|
419
+ | exit code | `0` 或 `1`(可接受,有报告)|
420
+ | 报告文件 | `<output>/report.json` 存在且可解析 |
421
+ | 关键发现 | `critical == 0` 且 `major < 3`(可配置)|
422
+ | 耗时 | `< 30s`(无 LLM)/ `< 120s`(有 LLM)|
423
+
424
+ ### 8.5 异常处理路径
425
+
426
+ | 异常 | Agent 行为 |
427
+ |---|---|
428
+ | 参数错误 (exit 2) | 检查 stderr → 修正参数 → 最多重试 1 次 |
429
+ | 非 git 仓 (exit 3) | 报告错误,不重试,标记仓库状态为 "invalid" |
430
+ | LLM 超时/失败 | 静默降级为纯规则模式,结果仍然完整 |
431
+ | 输出目录不可写 | 尝试父目录 → 再尝试 /tmp → 报告错误 |
432
+ | 规则文件缺失 (--rules) | 警告跳过该规则文件,继续审计 |
433
+
434
+ ---
435
+
436
+ ## 九、快速参考卡
437
+
438
+ ```bash
439
+ # 最常见用法
440
+ node repo-audit.mjs --repo <path> --format json
441
+
442
+ # CI 门禁
443
+ node repo-audit.mjs --repo <path> --strict --format json
444
+
445
+ # Agent 批量审计(多仓库)
446
+ for repo in $(find /path -maxdepth 2 -name ".git" -type d | xargs dirname); do
447
+ node repo-audit.mjs --repo "$repo" --format json --output "$repo/.audit/" 2>/dev/null &
448
+ done
449
+ wait
450
+ ```
451
+
452
+ # 快速文档审计
453
+ node repo-audit.mjs --repo <path> --dim docs
454
+
455
+ # 带 LLM 增强(任意 OpenAI 兼容厂商/平台)
456
+ LLM_PROVIDER=openai LLM_BASE_URL=https://api.deepseek.com LLM_MODEL=deepseek-chat LLM_API_KEY=sk-xxx \
457
+ node repo-audit.mjs --repo <path> --format both
458
+
459
+ # OpenRouter 聚合平台
460
+ LLM_PROVIDER=openrouter LLM_BASE_URL=https://openrouter.ai/api/v1 LLM_MODEL=deepseek/deepseek-chat LLM_API_KEY=sk-or-v1-xxx \
461
+ node repo-audit.mjs --repo <path> --format both
462
+
463
+ # 本地 Ollama
464
+ LLM_PROVIDER=openai LLM_BASE_URL=http://localhost:11434/v1 LLM_MODEL=deepseek-r1:8b LLM_API_KEY=ollama \
465
+ node repo-audit.mjs --repo <path> --format both
466
+
467
+ # 仅看 fail 项
468
+ node repo-audit.mjs --repo <path> --format json \
469
+ | jq '.findings[] | select(.status=="fail") | "\(.id) [\(.severity)]: \(.title)"'
470
+ ```
471
+
472
+ ---
473
+
474
+ *Agent 操作手册 v1.2 · 2026-09-06(新增 monorepo 感知 P6 + .auditrc.yaml 豁免)