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,749 @@
1
+ # repo-audit 使用手册(人类版)
2
+
3
+ > 审计 scaffold 生成的仓库是否符合工业最佳实践标准。
4
+ > 审计基准 = `templates/` 目录下的模板文件。
5
+ > 跨平台:Windows / Linux / macOS / Unix 一个命令即用。
6
+
7
+ ---
8
+
9
+ ## 一、安装
10
+
11
+ ### 方式一:直接运行(零配置)
12
+
13
+ Node.js 20+ 全局安装即可:
14
+
15
+ ```bash
16
+ # 在项目根目录直接运行
17
+ node repo-audit.mjs --repo /path/to/repo
18
+ ```
19
+
20
+ ### 方式二:加入 PATH(推荐,像普通命令一样用)
21
+
22
+ 把 `shim/` 目录加入 PATH:
23
+
24
+ ```bash
25
+ # Linux / macOS
26
+ export PATH="$PATH:/path/to/shim"
27
+
28
+ # Windows(PowerShell)
29
+ $env:PATH += ";C:\path\to\shim"
30
+
31
+ # Windows(CMD)
32
+ set PATH=%PATH%;C:\path\to\shim
33
+ ```
34
+
35
+ 之后任意位置直接调用:
36
+
37
+ ```bash
38
+ repo-audit --repo /path/to/repo # Linux/macOS
39
+ repo-audit.cmd --repo C:\path\to\repo # Windows CMD
40
+ repo-audit.ps1 --repo C:\path\to\repo # Windows PowerShell
41
+ ```
42
+
43
+ ### 方式三:全局 npm 安装(长期用)
44
+
45
+ ```bash
46
+ npm install -g /path/to/repo-audit-dir
47
+ ```
48
+
49
+ ---
50
+
51
+ ## 二、快速开始
52
+
53
+ ### 最简用法
54
+
55
+ ```bash
56
+ # 审计当前目录
57
+ node repo-audit.mjs
58
+
59
+ # 审计指定仓库
60
+ node repo-audit.mjs --repo /path/to/repo
61
+ ```
62
+
63
+ ### 典型工作流
64
+
65
+ ```bash
66
+ # 1. scaffold 生成新仓库后,立即验证出库质量
67
+ node repo-audit.mjs --repo ./my-new-repo
68
+
69
+ # 2. scaffold 升级模板后,对比前后差异
70
+ node repo-audit.mjs --repo ./my-repo --output ./audit-before
71
+ # ... 跑 scaffold --update ...
72
+ node repo-audit.mjs --repo ./my-repo --output ./audit-after
73
+
74
+ # 3. CI 中严格校验
75
+ node repo-audit.mjs --repo ./my-repo --strict --format json
76
+ ```
77
+
78
+ ### 常用组合
79
+
80
+ ```bash
81
+ # 强制指定分类(跳过自动检测,加速)
82
+ node repo-audit.mjs --repo ./my-repo --type python-app
83
+
84
+ # 只审计安全维度
85
+ node repo-audit.mjs --repo ./my-repo --dim security
86
+
87
+ # 叠加自定义规则
88
+ node repo-audit.mjs --repo ./my-repo --rules ./my-rules.yaml
89
+
90
+ # 严格模式(Critical/Major 发现即 exit 1)
91
+ node repo-audit.mjs --repo ./my-repo --strict
92
+ ```
93
+
94
+ ---
95
+
96
+ ## 三、命令行参数
97
+
98
+ | 参数 | 说明 | 默认值 |
99
+ |---|---|---|
100
+ | `--repo <path>` | 目标仓库路径 | 当前目录(`.`)|
101
+ | `--type <type>` | 强制指定分类(跳过自动检测) | 自动推断 |
102
+ | `--format <fmt>` | 输出格式:`md` / `json` / `both` | `both` |
103
+ | `--output <path>` | 报告输出目录 | `<repo>/audit-report/` |
104
+ | `--llm-provider <p>` | LLM 协议或厂商别名 | `none` |
105
+ | `--llm-base-url <url>` | 自定义 API 端点 | 协议默认 |
106
+ | `--llm-model <m>` | 模型名称 | `auto` |
107
+ | `--strict` | 严格模式:Critical/Major 发现即 exit 1 | 关 |
108
+ | `--rules <file>` | 自定义规则文件(可多次) | 无 |
109
+ | `--dim <domain>` | 只审计指定维度(可多次) | 全部 |
110
+ | `--help, -h` | 显示帮助 | — |
111
+
112
+ ### `--type` 支持的分类
113
+
114
+ 内置分类:`dsh-plugin` / `python-app` / `go-service` / `sandbox` / `content` / `product-oss` / `javascript` / `npm-package` / `rust` / `ruby` / `java-kotlin` / `java-maven` / `unknown`
115
+
116
+ 自定义分类可通过环境变量 `REPO_AUDIT_TYPES` 扩展(JSON 数组)。
117
+
118
+ ### `--dim` 支持的维度
119
+
120
+ `git` / `docs` / `security` / `quality` / `dsh` / `python` / `go` / `sandbox` / `content` / `oss`
121
+
122
+ ### `--format` 说明
123
+
124
+ | 值 | 说明 |
125
+ |---|---|
126
+ | `md` | 只输出 Markdown 报告到 stdout + 文件 |
127
+ | `json` | 只输出 JSON 报告到 stdout + 文件 |
128
+ | `both` | 同时输出两份(默认) |
129
+
130
+ ---
131
+
132
+ ## 四、环境变量
133
+
134
+ | 变量 | 说明 | 默认值 |
135
+ |---|---|---|
136
+ | `LLM_PROVIDER` | LLM 供应商 | `none` |
137
+ | `LLM_API_KEY` | API Key(必须手动指定) | 无 |
138
+ | `LLM_MODEL` | 模型名称 | `auto` |
139
+ | `LLM_BASE_URL` | 自定义 API 端点 | 供应商默认 |
140
+ | `REPO_AUDIT_TYPES` | 自定义分类列表(JSON 数组) | 内置默认 |
141
+
142
+ ### 协议与平台
143
+
144
+ 工具按**协议类型**区分,不绑定任何具体厂商。只要 API 兼容 OpenAI 或 Anthropic 格式,就能直接用——包括聚合平台、自建代理、本地运行服务等。
145
+
146
+ | 协议 | 覆盖范围 | 默认模型 | 默认端点 |
147
+ |---|---|---|---|
148
+ | `openai` | OpenAI 官方 / 聚合平台 / 国内厂商 / 自建代理 / 本地服务(Ollama/LM Studio 等) | gpt-4o-mini | api.openai.com |
149
+ | `anthropic` | Anthropic 官方 / 自建代理 | claude-3-5-haiku-20241022 | api.anthropic.com |
150
+ | `none` | 纯规则模式,无 LLM | — | — |
151
+
152
+ ### 核心用法:协议 + 端点 + Key
153
+
154
+ ```bash
155
+ LLM_PROVIDER=<协议> LLM_BASE_URL=<端点> LLM_API_KEY=<key> \
156
+ node repo-audit.mjs --repo ./my-repo
157
+ ```
158
+
159
+ ### 常见平台配置
160
+
161
+ #### 聚合平台
162
+
163
+ ```bash
164
+ # OpenRouter(推荐,支持几乎所有主流模型)
165
+ export LLM_PROVIDER=openai
166
+ export LLM_BASE_URL=https://openrouter.ai/api/v1
167
+ export LLM_API_KEY=sk-or-v1-xxx
168
+ export LLM_MODEL=deepseek/deepseek-chat # 或 any/deepseek-chat
169
+ node repo-audit.mjs --repo ./my-repo
170
+
171
+ # Perplexity
172
+ export LLM_PROVIDER=openai
173
+ export LLM_BASE_URL=https://api.perplexity.ai
174
+ export LLM_API_KEY=xxx
175
+ export LLM_MODEL=sonar
176
+
177
+ # Fireworks AI
178
+ export LLM_PROVIDER=openai
179
+ export LLM_BASE_URL=https://api.fireworks.ai/inference/v1
180
+ export LLM_API_KEY=xxx
181
+
182
+ # novita.ai
183
+ export LLM_PROVIDER=openai
184
+ export LLM_BASE_URL=https://api.novita.ai/v3/openai
185
+ export LLM_API_KEY=xxx
186
+ ```
187
+
188
+ #### 国内厂商
189
+
190
+ ```bash
191
+ # DeepSeek
192
+ export LLM_PROVIDER=openai
193
+ export LLM_BASE_URL=https://api.deepseek.com
194
+ export LLM_API_KEY=sk-xxx
195
+ export LLM_MODEL=deepseek-chat
196
+
197
+ # 阿里云通义千问
198
+ export LLM_PROVIDER=openai
199
+ export LLM_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
200
+ export LLM_API_KEY=sk-xxx
201
+ export LLM_MODEL=qwen-plus
202
+
203
+ # 智谱 GLM
204
+ export LLM_PROVIDER=openai
205
+ export LLM_BASE_URL=https://open.bigmodel.cn/api/paas/v4
206
+ export LLM_API_KEY=xxx
207
+ export LLM_MODEL=glm-4-plus
208
+
209
+ # 硅基流动
210
+ export LLM_PROVIDER=openai
211
+ export LLM_BASE_URL=https://api.siliconflow.cn/v1
212
+ export LLM_API_KEY=xxx
213
+ export LLM_MODEL=deepseek-ai/DeepSeek-R1-Distill-Llama-70B
214
+ ```
215
+
216
+ #### 官方 API
217
+
218
+ ```bash
219
+ # OpenAI(无需指定 BASE_URL)
220
+ export LLM_PROVIDER=openai
221
+ export LLM_API_KEY=sk-xxx
222
+ node repo-audit.mjs --repo ./my-repo
223
+
224
+ # Anthropic Claude
225
+ export LLM_PROVIDER=anthropic
226
+ export LLM_API_KEY=sk-ant-xxx
227
+ node repo-audit.mjs --repo ./my-repo
228
+ ```
229
+
230
+ #### 本地运行服务
231
+
232
+ ```bash
233
+ # Ollama
234
+ export LLM_PROVIDER=openai
235
+ export LLM_BASE_URL=http://localhost:11434/v1
236
+ export LLM_API_KEY=ollama
237
+ export LLM_MODEL=deepseek-r1:8b
238
+ node repo-audit.mjs --repo ./my-repo
239
+
240
+ # LM Studio
241
+ export LLM_PROVIDER=openai
242
+ export LLM_BASE_URL=http://localhost:1234/v1
243
+ export LLM_API_KEY=lm-studio
244
+ export LLM_MODEL=microsoft/Phi-3-mini-4k
245
+ node repo-audit.mjs --repo ./my-repo
246
+
247
+ # vLLM / TGI / 任何 OpenAI 兼容推理服务
248
+ export LLM_PROVIDER=openai
249
+ export LLM_BASE_URL=http://your-server:8000/v1
250
+ export LLM_API_KEY=xxx
251
+ export LLM_MODEL=your-model
252
+ node repo-audit.mjs --repo ./my-repo
253
+ ```
254
+
255
+ ### 厂商别名(快捷方式)
256
+
257
+ 以下别名可**直接作为 `LLM_PROVIDER` 的值**,工具自动映射到对应协议:
258
+
259
+ | 别名 | 映射协议 | 说明 |
260
+ |---|---|---|
261
+ | `openrouter` | openai | OpenRouter 聚合平台 |
262
+ | `deepseek` | openai | DeepSeek |
263
+ | `qwen` / `通义千问` | openai | 阿里云通义 |
264
+ | `glm` / `智谱` | openai | 智谱 AI |
265
+ | `siliconflow` / `硅基流动` | openai | 硅基流动 |
266
+ | `perplexity` | openai | Perplexity |
267
+ | `fireworks` | openai | Fireworks AI |
268
+ | `openai` | openai | OpenAI 官方 |
269
+ | `anthropic` / `claude` | anthropic | Anthropic 官方 |
270
+
271
+ > **注意**:使用非默认端点的别名时仍需设置 `LLM_BASE_URL`。
272
+
273
+ ---
274
+
275
+ ## 五、输出说明
276
+
277
+ ### 报告文件
278
+
279
+ 审计报告写入 `<repo>/audit-report/` 目录:
280
+
281
+ | 文件 | 格式 | 用途 |
282
+ |---|---|---|
283
+ | `report.md` | Markdown | 人类阅读,适合 PR 附件 |
284
+ | `report.json` | JSON | 程序解析,适合 CI 消费 |
285
+
286
+ ### 报告结构(JSON)
287
+
288
+ ```json
289
+ {
290
+ "generated_at": "2026-09-05T...",
291
+ "audit_type": "dsh-plugin",
292
+ "llm_used": false,
293
+ "metadata": {
294
+ "remote": "git@github.com:...",
295
+ "branch": "main",
296
+ "commitCount": 119,
297
+ "fileCount": 2734,
298
+ "lastCommit": "2026-09-05 20:25:29 +0800"
299
+ },
300
+ "summary": {
301
+ "critical": 1,
302
+ "major": 4,
303
+ "minor": 1,
304
+ "info": 0,
305
+ "pass": 22,
306
+ "total": 28,
307
+ "llmUsed": false
308
+ },
309
+ "findings": [
310
+ {
311
+ "id": "SEC-004",
312
+ "title": "publish workflow 含 id-token write",
313
+ "severity": "critical",
314
+ "domain": "security",
315
+ "status": "fail",
316
+ "message": "...",
317
+ "evidence": "字段不存在或值不匹配",
318
+ "hint": "在 workflow 顶部添加 permissions: id-token: write",
319
+ "template_ref": "templates/repo-root/.github/workflows/publish.yml",
320
+ "llm_analysis": null
321
+ }
322
+ ]
323
+ }
324
+ ```
325
+
326
+ ### stdout 摘要
327
+
328
+ ```
329
+ 分类: dsh-plugin | 评分: 32/100 (F) | 发现: 🔴1 🟠4 🟡1 🔵0 ✅22 | LLM 增强: 未启用 | 报告: audit-report/report.md + report.json
330
+ ```
331
+
332
+ ### 健康评分
333
+
334
+ ```
335
+ score = max(0, 100 - critical×25 - major×10 - minor×3)
336
+ 等级: A(90+) / B(80+) / C(70+) / D(60+) / F(<60)
337
+ ```
338
+
339
+ ---
340
+
341
+ ## 六、退出码
342
+
343
+ | 码 | 含义 | 适用场景 |
344
+ |---|---|---|
345
+ | `0` | 审计通过(无 Critical/Major 或 --strict 未启用) | 日常检查 |
346
+ | `1` | 有 Critical/Major 发现且 --strict 启用 | CI 门禁 |
347
+ | `2` | 参数错误 / 运行时错误 | 调试 |
348
+ | `3` | 目标不是 git 仓库 | 前置校验 |
349
+
350
+ ---
351
+
352
+ ## 七、审计维度说明
353
+
354
+ ### git(4 条)
355
+
356
+ | 规则 ID | 标题 | 严重度 | 说明 |
357
+ |---|---|---|---|
358
+ | GIT-001 | Conventional Commits 规范 | major | 最近 20 条提交符合规范 |
359
+ | GIT-002 | .gitignore 含机密防护段 | major | 排除构建产物和日志 |
360
+ | GIT-003 | 至少有一个有效提交 | critical | 仓库必须有提交 |
361
+ | GIT-004 | .scaffold/lock 存在 | info | scaffold 管理仓应有 lock;非 scaffold 仓自动豁免 pass |
362
+
363
+ ### docs(8 条)
364
+
365
+ | 规则 ID | 标题 | 严重度 | 适用分类 |
366
+ |---|---|---|---|
367
+ | DOC-001 | README.md 存在且非空 | critical | 所有 |
368
+ | DOC-002 | 双语 README 标配 | minor | 工程类 |
369
+ | DOC-003 | LICENSE 存在(MIT) | critical | 所有 |
370
+ | DOC-004 | AGENTS.md 含 AI 协作守则 | major | 所有 |
371
+ | DOC-005 | CONTRIBUTING.md 存在 | minor | 工程类 |
372
+ | DOC-006 | DEVELOPMENT.md 存在 | minor | 工程类 |
373
+ | DOC-007 | PUBLISHING.md 存在 | minor | 有发布面 |
374
+ | DOC-008 | SECURITY.md 存在 | minor | 工程类 |
375
+
376
+ ### security(4 条)
377
+
378
+ | 规则 ID | 标题 | 严重度 | 说明 |
379
+ |---|---|---|---|
380
+ | SEC-001 | 无硬编码密钥 | critical | grep 扫描源码 |
381
+ | SEC-002 | CI workflow 使用 SHA 固定 | major | 供应链安全 |
382
+ | SEC-003 | 无本机绝对路径 | major | 机密红线 |
383
+ | SEC-004 | publish workflow 含 id-token write | critical | OIDC 发布 |
384
+
385
+ ### quality(5 条)
386
+
387
+ | 规则 ID | 标题 | 严重度 | 说明 |
388
+ |---|---|---|---|
389
+ | QUA-001 | 验证链存在 | major | verify.mjs/scripts/verify.py/Makefile |
390
+ | QUA-002 | CI workflow 存在 | major | .github/workflows/ci.yml |
391
+ | QUA-003 | pre-commit 钩子配置 | minor | .githooks/pre-commit |
392
+ | QUA-004 | 锁文件入库 | major | package-lock.json 等 |
393
+ | QUA-005 | CLAUDE.md 桥接 | info | Claude Code 用户友好 |
394
+
395
+ ### 分类专属规则
396
+
397
+ | 分类 | 规则数 | 核心检查项 |
398
+ |---|---|---|
399
+ | dsh-plugin | 7 | dsh 字段、peer deps、publish workflow、verify.mjs、build.mjs |
400
+ | python-app | 5 | pyproject.toml、tests/、verify.py、PyPI publish |
401
+ | go-service | 5 | go.mod、cmd/、Makefile、CI、release |
402
+ | sandbox | 3 | 最小集(README+AGENTS)、无 CI |
403
+ | content | 3 | 主文 .md、review 分离、README 导航 |
404
+ | product-oss | 7 | CHANGELOG、CODEOWNERS、dependabot、release-please |
405
+
406
+ ---
407
+
408
+ ## 八、扩展指南
409
+
410
+ ### 新增维度
411
+
412
+ 在 `rules/domains/` 下加一个新 YAML 文件:
413
+
414
+ ```yaml
415
+ rules:
416
+ - id: NEW-001
417
+ title: "新规则标题"
418
+ severity: major
419
+ domain: new-domain
420
+ applies_to: ["*"]
421
+ check: file_exists
422
+ params:
423
+ paths: ["new-file.txt"]
424
+ description: "规则说明"
425
+ template_ref: "templates/common/..."
426
+ fix_hint: "修复建议"
427
+ ```
428
+
429
+ 然后重新运行审计,新规则自动生效。
430
+
431
+ ### 新增分类
432
+
433
+ 1. 在 `rules/categories/` 下加新 YAML 文件
434
+ 2. 在 `repo-audit.mjs` 的 `detectType()` 函数加分支
435
+
436
+ ```js
437
+ if (hasFile(repoPath, 'Cargo.toml')) scores['rust'] = 8
438
+ ```
439
+
440
+ 3. 可选:在 `REPO_AUDIT_TYPES` 环境变量中添加新分类名
441
+
442
+ ### 自定义规则
443
+
444
+ ```bash
445
+ node repo-audit.mjs --repo ./my-repo --rules ./my-custom-rules.yaml
446
+ ```
447
+
448
+ 自定义规则叠加到内置规则之上,不替代。
449
+
450
+ ---
451
+
452
+ ## 九、故障排查
453
+
454
+ ### "目标不是 git 仓库"
455
+
456
+ ```bash
457
+ # 检查是否是 git 仓
458
+ git -C /path/to/repo rev-parse --is-inside-work-tree
459
+ # 应该是 true
460
+
461
+ # 如果不是,初始化
462
+ git -C /path/to/repo init
463
+ git -C /path/to/repo add -A
464
+ git -C /path/to/repo commit -m "chore: initial commit"
465
+ ```
466
+
467
+ ### "未知参数"警告
468
+
469
+ 检查参数拼写,注意 `--` 双横线:
470
+
471
+ ```bash
472
+ # 正确
473
+ node repo-audit.mjs --repo ./my-repo --strict
474
+
475
+ # 错误(会报未知参数)
476
+ node repo-audit.mjs -repo ./my-repo # 单横线
477
+ ```
478
+
479
+ ### LLM 增强失败
480
+
481
+ LLM 失败不影响规则结果,静默跳过。查看详细错误:
482
+
483
+ ```bash
484
+ # LLM 失败的错误会输出到 stderr
485
+ node repo-audit.mjs --repo ./my-repo 2>&1 | grep "LLM"
486
+ ```
487
+
488
+ 常见原因:
489
+ - API Key 缺失或无效 → 检查 `LLM_API_KEY`
490
+ - 网络问题 → 检查 `LLM_BASE_URL` 是否可达
491
+ - 模型配额用完 → 换模型或等配额恢复
492
+
493
+ ### 分类检测不准
494
+
495
+ 用 `--type` 强制指定:
496
+
497
+ ```bash
498
+ node repo-audit.mjs --repo ./my-repo --type python-app
499
+ ```
500
+
501
+ 或通过 `REPO_AUDIT_TYPES` 环境变量添加自定义分类。
502
+
503
+ ---
504
+
505
+ ## 十、与 scaffold 的配合
506
+
507
+ | 操作 | 命令序列 |
508
+ |---|---|
509
+ | 新建仓库后验证 | `scaffold.mjs --type xxx --name yyy` → `repo-audit.mjs --repo ./yyy` |
510
+ | 模板升级后对比 | `repo-audit.mjs --repo ./zzz --output ./before` → `scaffold.mjs --update ./zzz` → `repo-audit.mjs --repo ./zzz --output ./after` |
511
+ | CI 门禁 | `repo-audit.mjs --repo . --strict --format json` |
512
+ | PR 描述附件 | `repo-audit.mjs --repo . --format md --output ./pr-audit` → 附 `pr-audit/report.md` 到 PR |
513
+
514
+ ---
515
+
516
+ ## 十一、操作授权与完成路径
517
+
518
+ ### 11.1 权限模型
519
+
520
+ `repo-audit` 是**纯只读工具**,不修改目标仓库任何文件,因此不需要写权限。
521
+
522
+ | 操作 | 所需权限 | 说明 |
523
+ |---|---|---|
524
+ | 运行审计 | 仓库读权限 | 只需 `git clone --local` 能读即可 |
525
+ | 写入报告 | 输出目录写权限 | 默认写入 `<repo>/audit-report/` |
526
+ | LLM 增强 | API Key | 需在环境变量中配置 `LLM_API_KEY` |
527
+ | CI 集成 | GitHub Actions `id-token: read` | 读取 OIDC token 用于 LLM 调用(可选) |
528
+
529
+ ### 11.2 执行路径
530
+
531
+ ```
532
+ 用户/Agent 发起审计请求
533
+ │
534
+ ├─ ① 前置校验
535
+ │ ├─ 目标路径存在? ── 否 ──→ 报错 exit 2
536
+ │ └─ 是 git 仓库? ── 否 ──→ 报错 exit 3
537
+ │
538
+ ├─ ② 分类检测
539
+ │ ├─ --type 指定? ── 是 ──→ 使用指定值
540
+ │ └─ 否 ──→ 自动推断(评分制)
541
+ │ └─ 置信度低? ── 是 ──→ 告警但继续(记录 reasons)
542
+ │
543
+ ├─ ③ 规则加载
544
+ │ ├─ 内置规则(domains/*.yaml + categories/<type>.yaml)
545
+ │ ├─ --rules 自定义规则叠加
546
+ │ └─ --dim 按维度过滤
547
+ │
548
+ ├─ ④ 规则执行
549
+ │ └─ 逐条检查 → findings 列表
550
+ │
551
+ ├─ ⑤ LLM 增强(可选)
552
+ │ ├─ LLM_API_KEY 已配置? ── 是 ──→ 对 fail 发现做增强(最多 5 条)
553
+ │ └─ 否 ──→ 跳过,纯规则结果完整可用
554
+ │
555
+ ├─ ⑥ 报告生成
556
+ │ ├─ JSON → <output>/report.json
557
+ │ ├─ Markdown → <output>/report.md
558
+ │ └─ stdout → 摘要行
559
+ │
560
+ └─ ⑦ 退出
561
+ ├─ exit 0 ──→ 通过(无 Critical/Major 或 --strict 未启用)
562
+ ├─ exit 1 ──→ 有 Critical/Major(--strict 启用)
563
+ └─ exit 2/3 ──→ 错误(参数/非 git)
564
+ ```
565
+
566
+ ### 11.3 典型操作场景
567
+
568
+ #### 场景 A:人工快速审计
569
+
570
+ ```bash
571
+ # 一步到位,stdout 看摘要,文件看详情
572
+ node repo-audit.mjs --repo ./my-repo
573
+ ```
574
+
575
+ #### 场景 B:CI 门禁(自动化)
576
+
577
+ ```yaml
578
+ # GitHub Actions
579
+ - name: Audit repo health
580
+ run: |
581
+ node repo-audit.mjs --repo . --strict --format json --output .ci-audit
582
+ SCORE=$(jq '.summary | 100 - (.critical * 25) - (.major * 10) - (.minor * 3)' .ci-audit/report.json)
583
+ echo "Health score: $SCORE"
584
+ # exit 1 if score < 70
585
+ [ "$SCORE" -ge 70 ] || exit 1
586
+ ```
587
+
588
+ #### 场景 C:模板升级前后对比
589
+
590
+ ```bash
591
+ # 升级前
592
+ node repo-audit.mjs --repo ./repo --format json --output ./before
593
+ # 升级后
594
+ node scaffold.mjs --update ./repo
595
+ node repo-audit.mjs --repo ./repo --format json --output ./after
596
+ # 对比
597
+ diff <(jq '.summary' ./before/report.json) <(jq '.summary' ./after/report.json)
598
+ ```
599
+
600
+ #### 场景 D:Agent 批量审计
601
+
602
+ ```bash
603
+ # Agent 一次审计多个仓库
604
+ for repo in ~/ninjasin-labs/*/; do
605
+ [ -d "$repo/.git" ] || continue
606
+ node repo-audit.mjs --repo "$repo" --format json --output "$repo/.audit/" 2>/dev/null
607
+ done
608
+ ```
609
+
610
+ ### 11.4 完成判定标准
611
+
612
+ | 场景 | 完成条件 |
613
+ |---|---|
614
+ | 单次审计 | 报告文件写入 + stdout 摘要输出 + 正确退出码 |
615
+ | CI 门禁 | exit 0 + score ≥ 70(可配置阈值)|
616
+ | 批量审计 | 所有仓库 exit 0 或可接受的非零码 |
617
+ | 升级对比 | before/after 两份报告 + 差异摘要 |
618
+ | PR 审查 | report.md 作为 PR 附件 + fail 项清单 |
619
+
620
+ ### 11.5 异常终止路径
621
+
622
+ | 异常类型 | 触发条件 | 处理方式 |
623
+ |---|---|---|
624
+ | 参数错误 | 未知 flag / 缺少必填值 | exit 2,打印用法 |
625
+ | 非 git 仓 | `--repo` 指向非 git 目录 | exit 3,提示初始化 |
626
+ | 规则文件缺失 | `--rules` 指向不存在文件 | 警告跳过,继续审计 |
627
+ | LLM API 失败 | Key 无效 / 网络超时 | 静默降级为纯规则模式 |
628
+ | 输出目录不可写 | 权限不足 / 磁盘满 | exit 2,提示创建目录 |
629
+
630
+ ---
631
+
632
+ *最后更新:2026-09-06(v1.2 新增 monorepo 感知 P6)
633
+
634
+ ---
635
+
636
+ ## 十二、新特性说明(2026-09-06)
637
+
638
+ ### 12.1 .auditrc.yaml 仓库级豁免
639
+
640
+ 当某条规则是**已知接受的历史决策**(如 Conventional Commits 个别非标准 commit),可创建 `.auditrc.yaml` 豁免:
641
+
642
+ ```yaml
643
+ # .auditrc.yaml(放在仓根,受版本控制)
644
+ waive:
645
+ - id: GIT-001
646
+ reason: "历史 commit fix(template)+docs 单条非标准;已知接受不回改"
647
+ since: "2026-09-06"
648
+ ```
649
+
650
+ 效果:该规则 finding 标 `status: "waived"`,评分排除此项,不阻塞 CI。
651
+
652
+ ### 12.2 项目定位识别(personal 模式)
653
+
654
+ 工具自动检测 README 首段关键词(自用/solo/personal/个人/单机):
655
+ - 命中 → `personal` 模式,自动豁免 7 条 N/A 规则(DOC-002/005/007、QUA-002/005、GO-004/005)
656
+ - 等效内容检测:docs/ 有开发文档 → DOC-006 pass;.githooks/ 有钩子 → QUA-003 pass
657
+
658
+ ### 12.3 archive 分类
659
+
660
+ 最后 commit 含 `archived`/`封版` 关键词 → 自动归类为 `archive`:
661
+ - 仅 SEC-001(密钥扫描)+ SEC-003(路径泄漏)两条安全规则
662
+ - 其余工程规范全部豁免
663
+
664
+ ### 12.4 file_exists fallback
665
+
666
+ 当主验证链路径(verify.mjs/Makefile)不存在时,自动尝试 `package.json scripts.test`:
667
+ ```yaml
668
+ params:
669
+ paths: ["verify.mjs", "Makefile"]
670
+ fallback_paths: ["package.json"]
671
+ fallback_field: "scripts.test"
672
+ ```
673
+
674
+ ### 12.5 toml_field expect_table
675
+
676
+ 检查 TOML 表头存在性(而非表内键值):
677
+ ```yaml
678
+ check: toml_field
679
+ params:
680
+ field: "tool.pytest.ini_options"
681
+ expect_table: true
682
+ ```
683
+
684
+ ### 12.6 Monorepo 感知(P6)
685
+
686
+ 工具自动探测 workspace 布局,无需手动指定子目录审计:
687
+
688
+ **支持布局**:根优先,fallback 搜索 `apps/*/`、`packages/*/`、`workspaces/*/`、`components/*/` 等常见 monorepo 目录。
689
+
690
+ **自动处理**:
691
+ - 分类检测:根无 `package.json` 时递归查找 → 正确识别 `javascript` 而非误判为 `content`
692
+ - `file_exists` fallback:锁文件、验证链自动搜索 workspace 子目录
693
+ - `QUA-004` 锁文件:`apps/desktop/package-lock.json` 被正确识别
694
+
695
+ **示例**:
696
+ ```
697
+ neonforge/( Electron monorepo,405 commits)
698
+ apps/desktop/package.json ← 工程实体
699
+ apps/desktop/package-lock.json ← 锁文件
700
+ # 根目录无 package.json
701
+ ```
702
+ 审计根目录 → 自动识别为 `javascript` 分类,19 条工程规则加载,评分 88/B。
703
+
704
+ **注意事项**:
705
+ - 若 workspace 有多个 package.json(如 apps/web + apps/api),取第一个命中的
706
+ - 可显式指定 `--type javascript` 跳过自动分类
707
+
708
+ **限制**:`json_field` 检查器(如 DOC-003b license 一致性)只读仓根 `package.json`,
709
+ 不递归 workspace。monorepo 中若子目录有 license 字段而根目录没有,可能误报。
710
+ workaround:对工程子目录单独跑审计,或人工确认。
711
+
712
+ ### 12.7 开源仓 workflow 审查清单
713
+
714
+ 首次将 scaffold 生成的仓库发布到 GitHub 前,逐项检查 CI/publish workflow 是否适配目标项目:
715
+
716
+ | # | 检查点 | 常见问题 | 验证方式 |
717
+ |---|---|---|---|
718
+ | 1 | **分支名** | workflow 触发 `main`,实际分支为 `master` | `git branch --show-current` vs `on.push.branches` |
719
+ | 2 | **依赖管理** | `npm ci` 但无 `package-lock.json`(零依赖项目) | `ls package-lock.json` 存在性 |
720
+ | 3 | **脚本路径** | `node scripts/verify.mjs` 但实际为 `verify.mjs` | `ls scripts/verify.mjs` 存在性 |
721
+ | 4 | **tag 格式** | `demo-v*` 但实际为 `v*`(匹配 package.json version) | 检查 `on.push.tags` 与 `package.json.version` 格式一致 |
722
+ | 5 | **action 版本** | `<commit-SHA>` 占位符未替换 | `grep -r '<commit-SHA>' .github/workflows/` 应为空 |
723
+ | 6 | **npm trusted publisher** | publish.yml 配置了 OIDC 但 npm 端未配置 trusted publisher | npmjs.com → Package Settings → Trusted Publishers |
724
+
725
+ **快速检查命令**:
726
+ ```bash
727
+ # 1) 检查分支名一致性
728
+ echo "实际分支: $(git branch --show-current)"
729
+ echo "workflow 触发: $(grep -A2 'branches:' .github/workflows/ci.yml | tail -1 | xargs)"
730
+
731
+ # 2) 检查依赖管理
732
+ [ -f package-lock.json ] && echo "✓ 有 lockfile" || echo "✗ 无 lockfile — 移除 npm ci"
733
+
734
+ # 3) 检查脚本路径
735
+ for f in scripts/verify.mjs verify.mjs; do [ -f "$f" ] && echo "✓ $f"; done
736
+
737
+ # 4) 检查 tag 格式
738
+ echo "package.json version: $(node -p "require('./package.json').version")"
739
+ echo "workflow tag: $(grep 'tags:' -A2 .github/workflows/publish.yml | tail -1)"
740
+
741
+ # 5) 检查占位符
742
+ grep -rE '<commit-SHA>|dsh-demo|demo-v' .github/workflows/ && echo "✗ 有残留" || echo "✓ 无残留"
743
+ ```
744
+
745
+ **教训来源**:`repo-audit` 开源仓首次 push 时,CI workflow 从 dsh-demo 模板直接复制,
746
+ `npm ci`(无 lockfile)、`scripts/verify.mjs`(路径错误)、`main` 分支(实际 master)、
747
+ `<commit-SHA>`(占位符未替换)四项同时失败,workflow 不触发。
748
+
749
+