fix: production hardening for merge races, timeouts, and webhook ops

Pin merge SHA, lock conflict repair, bound subprocess/proxy output,
fail-fast incomplete R2/webhook config, redact validation/stderr
secrets, and document that webhook 202 is best-effort after ACK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
wdawdwaandClaude Opus 4.8 committed 2026-08-23 15:44:08 +08:00
1 parent d6f33a71c3
commit 039acabd96
29 files changed
+1163 -240

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
# ghbot 项目宪法(Constitution)
> 版本: 1.0.0 · 生效日期: 2026-08-23
> 本文件是所有开发决策的最高约束。任何实现与本文冲突时,以本文为准。
## 核心价值观
1. **生产安全高于一切**:本机器人运行在生产仓库上,能关闭 PR、推送 commit、合并 PR。
任何改动若可能放大误操作半径,默认拒绝;必须改时需要显式护栏 + 测试证明。
2. **凭据零扩散**:GitHub token / App 私钥 / R2 密钥 / goose API key 绝不进入容器、快照、
git 子进程环境或日志。违反此条 = 阻塞级缺陷。
3. **PR 代码不可信**:自动审查/分类只用 API diff;PR 内容永远视为 untrusted data,
prompt 注入防护是功能需求而非可选项。
4. **诚实交付**:未验证的东西不宣称完成。测试失败必须如实报告,禁止为绿而削弱断言。
## 技术原则
### 架构
- 单一职责:一个模块只回答一个问题(谁调用它、它调用谁、失败时谁兜底)。
- 共享清单单一来源:安全排除/脱敏/路径策略只允许一份实现(src/security/, src/runtimePaths.ts)。
- 行为不变的重构必须与功能变更分开提交,且全量测试保护。
### 质量门禁(提交前全部通过)
1. `npm run typecheck` — 0 错误
2. `npm run lint` — 0 error
3. `npm run format:check` — 通过
4. `npm test` — 全绿;新增行为必须有新测试(TDD:先红后绿)
5. `npm run build` — dist 可被 node 加载
6. `npm audit --omit=dev` — 无 high 以上
### 安全红线(继承 CLAUDE.md,不可协商)
- AUTO_MERGE=false 默认值不可改
- 恢复缓存审查结果必须重新完整审查,不得直接复用旧决策
- fork 推送必须 SHA 钉住 --force-with-lease
- 知识缓存入库前拒绝凭据内容
## 决策框架
技术选型按序评估:
1. 是否触碰安全红线?(触碰 → 拒绝)
2. 是否可用现有轮子解决?(可以 → 不写新代码)
3. 失败时的降级行为是否清晰?(不清晰 → 先设计降级)
4. 是否可测试?(不可测 → 重构到可测)
## 变更治理
- 每个原子提交对应一个可独立回退的变更单元
- 公共接口(config schema、workflow inputs、评论文案契约)变更必须同步双语 README
- 文档与代码冲突时以代码为准,并在同一提交内修正文档
@@ -0,0 +1,29 @@
# Implementation Plan: 001-production-hardening
## Current architecture
ghbot is a GitHub Actions + optional webhook Node service. Review, triage, conflict repair, and @bot chat are orchestrated from `src/actions/runReview.ts`. Config is a single zod schema in `src/config.ts`. Persistence is optional R2 plus local scratch directories.
## Impacted modules
- `src/review/processor.ts` — merge SHA pinning, stale-head discard, approval lookup errors
- `src/review/conflictResolver.ts` — per-PR lock, git timeouts, worktree reset
- `src/ai/apiProxy.ts` / `src/ai/gooseCli.ts` — proxy/process timeouts and bounded output
- `src/webhook/server.ts` — body deadline, request catch, metrics
- `src/config.ts` / workflows / `.env.example` — GHBOT_RUNTIME_DIR, webhook/R2 cross-checks
- docs — webhook secret, docker run, goose aliases
## Implementation strategy
Fix production failure modes first. Keep AUTO_MERGE default false. Prefer additive guards over architecture rewrites. Do not extract the 1700-line processor router in this pass because there is no isolated event-loop test harness; that refactor is deferred with an explicit risk note.
## Verification
- npm run typecheck / lint / format:check / test
- production-build webhook HTTP smoke (healthz, HMAC, duplicate, metrics)
- npm audit --omit=dev against registry.npmjs.org
- independent six-dimension review after gates
## Rollback
Revert the production-hardening commit on the fork. Defaults remain conservative (AUTO_MERGE=false, WEBHOOK_ENABLED=false).
@@ -0,0 +1,61 @@
# Feature Specification: 001-production-hardening(生产硬化总审计)
## 问题陈述
ghbot 已在生产仓库运行(自动审查/分类/合并/冲突修复)。用户要求以"终局闭环总审计"标准,
对全部功能做查漏补缺:功能完整性、前后端衔接(此处为 workflow↔代码↔GitHub API 契约)、
调用体验、部署体验、文档一致性,并补齐指南中剩余未落地项。
## 用户故事
### US-1: 部署者首次接入
作为部署者,我希望按 README 从零配置后一次跑通,无需口口相传的隐性前提。
**验收**:
- [ ] .env.example 覆盖全部 config schema 字段且默认值一致
- [ ] README 步骤与实际命令一一对应
- [ ] 新增配置项有文档说明
### US-2: 事件全链路
作为仓库维护者,任何支持的事件触发后都有确定行为与反馈。
**验收**:
- [ ] workflow 触发的事件类型 ⊆ runReview.ts 处理集合(无静默吞事件)
- [ ] issue_comment 三链路互不阻断
- [ ] 每条失败路径都有可见反馈或日志
### US-3: 审查结果可信
作为 PR 作者,审查评论必须准确、可定位、不误导。
**验收**:
- [ ] finding 的 path/line 经净化后才提交
- [ ] 无法 inline 的 findings 折叠展示不丢失
- [ ] 状态横幅与 disposition 一致
### US-4: webhook 服务可运维
作为运维者,服务健康可探活、指标可观测、异常可恢复。
**验收**:
- [ ] /healthz 与 /metrics 真实可用
- [ ] 队列满/签名错/超大包均有明确响应码
- [ ] 失败投递进程内重试后仍可被 GitHub 重投
### US-5: 剩余指南项落地
指南 7.1 / 6.4 / 8.5 / 4.4 不再是"候选",全部实现或有明确的不适用结论。
**验收**:
- [ ] 每项有代码证据或书面不适用理由
- [ ] 全部门禁通过
## 非功能性需求
- 兼容性:Node >=22 <26;Windows/Linux 双平台测试通过
- 可观测性:关键路径日志含 owner/repo/pullNumber/eventName 四元组
- 安全性:宪法红线零违反
- 可维护性:无重复实现的安全清单;文件 <800 行
## Out of Scope
- 真实付费 LLM 调用的 E2E(预算 0)
- 前端 UI(本项目无前端)
## 成功指标
- 门禁六项全绿
- E2E:webhook 生产构建真实 HTTP 验收通过
- 需求追踪矩阵无"未闭环"项
@@ -0,0 +1,22 @@
# Implementation Tasks: 001-production-hardening
## Phase 1: Spec and audit
- [x] 1.1 Constitution + spec
- [x] 1.2 Parallel contract/security/error/docs audit
## Phase 2: Production guards
- [x] 2.1 Merge SHA recheck + pulls.merge sha
- [x] 2.2 Conflict lock + git command timeout + dirty worktree reset
- [x] 2.3 Webhook body timeout/size and request catch
- [x] 2.4 API proxy abort/timeout/body cap
- [x] 2.5 Goose process output cap and cleanup timeouts
- [x] 2.6 Config cross-field validation and GHBOT_RUNTIME_DIR contract
- [x] 2.7 Issue comment handler isolation with AggregateError
- [x] 2.8 Approval permission lookup no longer swallows retryable errors
- [x] 2.9 Event logger child bindings
## Phase 3: Verify and handoff
- [ ] 3.1 Local gates (typecheck/lint/format/test)
- [ ] 3.2 Webhook HTTP E2E
- [ ] 3.3 Independent review
- [ ] 3.4 HTML change report + skill + docs