From b66d04aa3a08cc6326bb5522ef1325dfcfa16ed2 Mon Sep 17 00:00:00 2001 From: Jianfeng Pang <135638390+toubeichuan@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:21:06 +0800 Subject: [PATCH 1/2] docs: add inspector project todo and technical specification --- README.md | 7 +++++ docs/TECH_SPEC.md | 76 +++++++++++++++++++++++++++++++++++++++++++++++ docs/TODO.md | 47 +++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 docs/TECH_SPEC.md create mode 100644 docs/TODO.md diff --git a/README.md b/README.md index fa480b9a..8290d0f7 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ Translation Versions: [ENGLISH](./README.md) | [简体中文](./README.zh-CN.md) | [繁體中文](./README.zh-TW.md) | [한국어](./README.ko.md) | [日本語](./README.ja.md) +## inspector Project Documentation + +CS5351 Software Engineering · Group 2. + +- [开发 Todo](docs/TODO.md) +- [技术规范](docs/TECH_SPEC.md) + ## Bot Usage ❗️⚠️ `Due to cost considerations, BOT is only used for testing purposes and is currently deployed on AWS Lambda with ratelimit restrictions. Therefore, unstable situations are completely normal. It is recommended to deploy an app by yourself.` diff --git a/docs/TECH_SPEC.md b/docs/TECH_SPEC.md new file mode 100644 index 00000000..f5c24ae7 --- /dev/null +++ b/docs/TECH_SPEC.md @@ -0,0 +1,76 @@ +# inspector 技术规范 + +范围:GitHub TS/JS Pull Request 的 Context-Aware 审查与可解释风险管理。本文区分正确性约束、团队策略和待验证参数,不预先承诺效率提升。 + +## 运行与构建 + +沿用现有 TypeScript、Jest 和构建流程。固定基线的 Action runtime 为 Node 24,入口为 `action/index.cjs`;修改源码后重新构建并提交对应产物。工程 CI 明确执行 test/build,模型 API 烟雾测试单独运行。保留原 LICENSE 和上游归属。 + +## 提交与证据 + +一次审查固定目标分支快照 `baseSha`、实际 diff 比较基准 `diffBaseSha` 和被审查版本 `headSha`。PR diff 的比较基准可能是 merge-base,不应直接用后来变化的 main 代替。 + +新增侧代码和证据使用 `headSha`;旧侧及删除行证据使用实际 diff 基准。片段记录 `ref`,finding 记录 `side`(LEFT/RIGHT)与正确的行坐标。报告和 gate 绑定被审查的 `headSha`;发布前核对当前 head,过期运行不得放行新提交。 + +## 分析范围与状态 + +`COMPLETE` 表示声明支持范围内的必要分析完成,不代表整个仓库已充分审查。声明不支持的文件类型,以及可选上下文因预算裁剪,应披露范围,不必全部转为失败。 + +必要 diff、必要证据缺失或部分模型调用失败为 `PARTIAL`;没有可用分析结果为 `FAILED`。无 findings、空响应和分析失败分别表示。输出校验失败不得直接生成 LGTM。已发现的 HIGH 保留;分析不足且无法形成完整风险判断时显示 UNKNOWN。 + +## 统一数据 + +| 对象 | 最小字段 | +| --- | --- | +| PR 输入 | repo、prNumber、baseSha、diffBaseSha、headSha、diff、changedFiles | +| ContextSnippet | id、file、ref、startLine、endLine、code、reason | +| Finding | id、file、side、line(可空)、severity、message、evidenceIds | +| ReviewResult | headSha、mode、analysisStatus、scope、omissions、findings、risk、gate | +| 运行记录 | 模型及有效参数、prompt/规则版本、耗时、usage(缺失标未知) | + +`severity` 为 low/medium/high;`mode` 为 diff-only/context。risk 与 gate 分开保存,risk 包含等级、因素和规则版本,gate 包含状态、理由及批准条件。位置或证据未验证的结果应明确标记或降级,不编造链接。 + +## 上下文与资源预算 + +文件数、上下文大小、模型输入 token、调用次数、超时和重试均配置化。字节或行数限制用于约束读取量,不能代替模型的 token 上限;应预留输出 token 和请求开销。 + +5 个文件、200 行、40 KB、90 秒等数值仅可作为试跑候选,不是已验证的推荐值。基线按文件调用模型,因此必须先确定调用预算按文件还是整个 PR 计算,再决定是否批处理。重试消耗总流程剩余预算,并预留 GitHub 反馈时间。对比实验前测量并冻结配置。 + +## 风险与门禁 + +风险规则是审查优先级的启发式判断,不是缺陷概率。阈值、敏感路径和所需批准数量配置化,报告实际触发理由。风险等级 LOW/MEDIUM/HIGH/UNKNOWN 与分析覆盖分开。 + +| 条件 | 默认演示行为 | +| --- | --- | +| COMPLETE + LOW/MEDIUM | Gate 通过;仍满足仓库普通 PR 规则 | +| HIGH、UNKNOWN 或必要分析 PARTIAL/FAILED | 等待配置数量的有效人工批准,满足后通过 | +| 无效配置 | 明确错误;修复配置并重跑 | +| 新提交、批准撤销或有效请求修改 | 重新评估,旧运行不能放行新提交 | + +真正限制合并需要命名状态 `inspector/risk-gate` 与仓库 required status 规则共同生效。等待用 pending,可发布的运行错误用 failure;状态写入失败时记录错误并使流程失败,不报告发布成功。使用普通 Write 成员验收,记录管理员绕过条件。 + +## 人工批准策略 + +仅计仓库 Write 或更高权限的人类 reviewer,排除作者和 Bot,按有效 review 状态计算。`COMMENTED` 不算新批准,也不应仅因出现评论而删除原有有效批准;有效 `CHANGES_REQUESTED` 或撤销必须反映到门禁。 + +第一版演示可采用严格策略:只接受当前 head SHA 的批准,新提交要求重新批准。这是团队可选策略,不是 GitHub 所有批准行为的通用定义;评测应记录因此增加的批准次数和等待时间。 + +## 验证与完成标准 + +原版固定 commit 单独保留。受控 Diff-only 与 Context 比较使用同样样本、模型、有效参数和共同 prompt,只改变上下文供给;完整产品另测风险门禁和人工审阅。开发样例与独立评价样例分开,保留失败和负结果。 + +人工效率至少同时记录审阅耗时及判断正确性,避免仅以评论数量或 AI 延迟推断效率。风险规则与 gate 生命周期分别测试,不把“符合自己写的规则”当成规则能正确预测风险的证据。 + +自动测试使用固定响应,不依赖付费 API;API Key 使用 Secrets。可靠性验证失败/过期结果不产生错误放行;效率验证资源预算和实测开销。完成需真实 PR 链路、有效人工解锁、新提交重算、测试/build 通过,以及另一成员能按 README 复现。 + +## 核查依据 + +固定基线:`8bd66227f057f21e847a8c27c9b356c25271d292`。源码核查日期:2026-10-13;测试和构建实际结果由开发任务记录。 + +- [package.json](https://github.com/CS5351-inspector/context-aware-risk-gated-code-review/blob/8bd66227f057f21e847a8c27c9b356c25271d292/package.json):已有 test/build。 +- [Action 配置](https://github.com/CS5351-inspector/context-aware-risk-gated-code-review/blob/8bd66227f057f21e847a8c27c9b356c25271d292/action.yml):Node 24 和打包入口。 +- [现有 workflow](https://github.com/CS5351-inspector/context-aware-risk-gated-code-review/blob/8bd66227f057f21e847a8c27c9b356c25271d292/.github/workflows/cr.yml):审查调用上游 Action。 +- [chat.ts](https://github.com/CS5351-inspector/context-aware-risk-gated-code-review/blob/8bd66227f057f21e847a8c27c9b356c25271d292/src/chat.ts):仅 JSON 解析、空 choices 的 LGTM、非 reasoning 模型 temperature=0 的默认表达式等需验证和修正的路径。 +- [bot.ts](https://github.com/CS5351-inspector/context-aware-risk-gated-code-review/blob/8bd66227f057f21e847a8c27c9b356c25271d292/src/bot.ts):按文件审查、跳过 patch 和空反馈报告等需扩展覆盖记录的路径;API 异常已有重抛,不应概括成所有错误都被吞掉。 +- [GitHub 分支保护](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches):required checks 与 review 条件。 +- [GitHub PR 的比较基准](https://docs.github.com/en/pull-requests/reference/branches#three-dot-and-two-dot-git-diff-comparisons):PR 使用 merge-base 到 head 的三点比较。 diff --git a/docs/TODO.md b/docs/TODO.md new file mode 100644 index 00000000..b171486b --- /dev/null +++ b/docs/TODO.md @@ -0,0 +1,47 @@ +# inspector 开发 Todo + +项目:Context-Aware and Risk-Gated Code Review for GitHub Pull Requests。 + +技术约定见 [TECH_SPEC.md](TECH_SPEC.md)。按模块认领任务,完成后填写对应 PR 或测试链接。保留已有的分页、diff 筛选、位置校验和测试,在此基础上扩展。 + +## 基线与工程环境 + +- [ ] 固定原版 commit、依赖及有效模型参数;执行 `npm ci`、`npm test`、`npm run build`,记录结果并保存一次原版 PR 审查。 +- [ ] 建立独立的 test/build CI;让增强版 workflow 执行本组 Action,并更新对应构建产物。 +- [ ] 串联输入、context、review、risk 和报告;提供固定模拟响应,支持离线联调。 + +## Context-Aware + +- [ ] 固定一次审查的提交快照,读取 diff 及对应版本的代码,提取修改处的周围实现。 +- [ ] 先实现一种直接相关的定义、调用代码或测试检索规则,用样例验证其相关性。 +- [ ] 实现去重和资源限制;记录片段出处、选择理由,以及必要内容缺失和可选内容裁剪。 + +## 模型审查与 Findings + +- [ ] 实现受控 Diff-only 与 Diff+Context 模式,共用模型适配器、有效参数及 prompt 主体。 +- [ ] 校验输出字段、位置和证据引用;对空响应、无效结果及必要 diff 被跳过的情况补测试,避免错误 LGTM。 +- [ ] 处理超时、限流和有限重试;将实际失败及覆盖范围传给风险和报告模块。 + +## Risk Management 与 Gate + +- [ ] 用敏感路径、改动规模和有效 finding 等少量信号实现可解释风险规则,记录阈值理由。 +- [ ] 配置风险阈值与所需人工批准数量;发布 gate 状态,在演示分支启用 required status。 +- [ ] 验证 PR 更新、批准、请求修改和撤销后的状态变化,完成等待、批准放行及新提交重算的闭环。 + +## PR 报告与反馈 + +- [ ] 生成风险、findings、证据、覆盖范围和人工下一步的 Markdown 报告。 +- [ ] 更新同一份 PR 摘要;处理重复运行、无有效行号、长报告及 GitHub 权限错误。 +- [ ] 展示所评估提交;发布前核对当前 head,避免过期输出覆盖当前结论。 + +## 测试与对比 + +- [ ] 建立带预期答案的开发样例及独立评价样例,覆盖跨文件缺陷、普通/高风险/无缺陷改动和失败情况。 +- [ ] 扩展模块与集成测试,覆盖输出校验、风险边界、门禁解锁和过期批准;记录设计取舍及技术债。 +- [ ] 编写比较脚本,保存原始结果,比较正确 findings、误报/漏报、人工审阅时间、耗时与调用成本;整理 README 和复现命令。 + +## 范围与效果验证 + +Context-Aware 和 Risk Management 是本项目的核心目标,其收益需要上述对比验证。第一版先用直接检索和少量风险规则;完整调用图、向量检索、复杂过滤和额外网页视实测需求增加。 + +完成任务以可运行实现、测试结果和 review 记录为依据。若启用 Issues,将实际认领和状态记录在 Issues,本文保留任务总览及链接,避免重复维护两套进度。 From b51ecdf3e1c4c9ef8322ba46ca286f3998d8e42d Mon Sep 17 00:00:00 2001 From: luxinyi <1253425166@qq.com> Date: Mon, 2 Nov 2026 11:50:02 +0800 Subject: [PATCH 2/2] feat: baseline docs, CI, and offline pipeline skeleton - Record baseline results in docs/BASELINE.md - Add standalone test/build CI workflow - Point review workflow at the local action (uses: ./) - Add unified types + pipeline skeleton with mock responses - Fix package-lock.json so npm ci runs Co-Authored-By: Claude Opus 4.6 --- .github/workflows/cr.yml | 6 +- .github/workflows/test.yml | 26 +++ docs/BASELINE.md | 61 +++++++ package-lock.json | 357 +++++++++++++++++++++++++++++++++++++ src/mocks.ts | 62 +++++++ src/pipeline.ts | 84 +++++++++ src/types.ts | 83 +++++++++ test/pipeline.test.ts | 30 ++++ 8 files changed, 707 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 docs/BASELINE.md create mode 100644 src/mocks.ts create mode 100644 src/pipeline.ts create mode 100644 src/types.ts create mode 100644 test/pipeline.test.ts diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml index e47f531d..592d7d19 100644 --- a/.github/workflows/cr.yml +++ b/.github/workflows/cr.yml @@ -10,10 +10,12 @@ on: types: [opened, reopened, synchronize] jobs: - test: + review: runs-on: ubuntu-latest steps: - - uses: anc95/ChatGPT-CodeReview@main + - uses: actions/checkout@v4 + + - uses: ./ env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} LANGUAGE: English diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..0b02020d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,26 @@ +name: Test and Build + +on: + pull_request: + push: + branches: [main] + +jobs: + test-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 24 + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Run tests + run: npm test + + - name: Build + run: npm run build diff --git a/docs/BASELINE.md b/docs/BASELINE.md new file mode 100644 index 00000000..8c2f3532 --- /dev/null +++ b/docs/BASELINE.md @@ -0,0 +1,61 @@ +# 基线记录(Baseline) + +> 模块:基线与工程环境 · 任务 1 +> 记录日期:2026-10-28 +> 记录人:待填(组内认领本模块的成员) + +## 1. 固定 commit + +| 项 | 值 | +| --- | --- | +| 基线 commit(TECH_SPEC 固定) | `8bd66227f057f21e847a8c27c9b356c25271d292` | +| 当前 main 分支 HEAD | `fad72c5229ccef11448c3077a617fb7b2bdd934a` | + +> main 比基线多出 docs 更新(TODO.md / TECH_SPEC.md),`src/` 代码与基线一致。 + +## 2. 环境 + +| 项 | 值 | +| --- | --- | +| 操作系统 | macOS(Darwin 22.1.0) | +| Node.js | v24.14.1(满足 TECH_SPEC 要求的 Node 24) | +| npm | 11.11.0 | + +## 3. 依赖 + +- 包管理:npm(项目同时含 `package-lock.json` 和 `yarn.lock`,以 npm 为准) +- 依赖总数:652 个包(`npm install` 结果) +- 已知漏洞:62 个(15 low / 22 moderate / 20 high / 5 critical),来自上游遗留(probot 12、next 13),暂不处理 + +## 4. 有效模型参数 + +来源:`src/chat.ts` 与 `.env.example`。 + +| 参数 | 默认值 | 说明 | +| --- | --- | --- | +| `MODEL` | `gpt-4o-mini`(GitHub Models 下为 `openai/gpt-4o-mini`) | 模型名 | +| `temperature` | `1` | 仅非 reasoning 模型 | +| `top_p` | `1` | 仅非 reasoning 模型 | +| `max_tokens` | 未设置 | | +| `response_format` | `json_object` | 固定 | +| `REASONING_EFFORT` | 未设置 | 仅 reasoning 模型 | +| `LANGUAGE` | 未设置(默认英文) | 审查语言 | +| `PROMPT` | `Below there is a code diff please help me do a code review` | 默认 prompt | + +## 5. 命令执行结果 + +| 命令 | 结果 | 说明 | +| --- | --- | --- | +| `npm ci` | ❌ 失败 | lock 与 package.json 不同步,缺 esbuild 各平台可选依赖 | +| `npm install` | ✅ 成功 | 652 包;已修改本地 `package-lock.json` | +| `npm test` | ✅ 52/52 通过 | 2 个测试套件 | +| `npm run build` | ✅ 成功 | 生成 `dist/` 与 `action/index.cjs`(约 6.5MB) | + +## 6. 原版 PR 审查 + +- [ ] 待做:需要 `OPENAI_API_KEY`(或 GitHub Models token),真实跑一次 PR 审查并保存输出作为基线参考。 + +## 7. 待解决事项 + +- [ ] `npm ci` 需修复 lock 文件(缺少 esbuild 可选依赖),或团队约定改用 `npm install` +- [ ] 原版 PR 审查需 API key diff --git a/package-lock.json b/package-lock.json index 635299a4..4c7128ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -714,6 +714,57 @@ "dev": true, "license": "MIT" }, + "node_modules/@esbuild/android-arm": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.17.11.tgz", + "integrity": "sha512-CdyX6sRVh1NzFCsf5vw3kULwlAhfy9wVt8SZlrhQ7eL2qBjGbFhRBWkkAzuZm9IIEOCKJw4DXA6R85g+qc8RDw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.17.11.tgz", + "integrity": "sha512-QnK4d/zhVTuV4/pRM4HUjcsbl43POALU2zvBynmrrqZt9LPcLA3x1fTZPBg2RRguBQnJcnU059yKr+bydkntjg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.17.11.tgz", + "integrity": "sha512-3PL3HKtsDIXGQcSCKtWD/dy+mgc4p2Tvo2qKgKHj9Yf+eniwFnuoQ0OUhlSfAEpKAFzF9N21Nwgnap6zy3L3MQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, "node_modules/@esbuild/darwin-arm64": { "version": "0.17.11", "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.17.11.tgz", @@ -731,6 +782,312 @@ "node": ">=12" } }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.17.11.tgz", + "integrity": "sha512-iB0dQkIHXyczK3BZtzw1tqegf0F0Ab5texX2TvMQjiJIWXAfM4FQl7D909YfXWnB92OQz4ivBYQ2RlxBJrMJOw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.17.11.tgz", + "integrity": "sha512-7EFzUADmI1jCHeDRGKgbnF5sDIceZsQGapoO6dmw7r/ZBEKX7CCDnIz8m9yEclzr7mFsd+DyasHzpjfJnmBB1Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.17.11.tgz", + "integrity": "sha512-iPgenptC8i8pdvkHQvXJFzc1eVMR7W2lBPrTE6GbhR54sLcF42mk3zBOjKPOodezzuAz/KSu8CPyFSjcBMkE9g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.17.11.tgz", + "integrity": "sha512-M9iK/d4lgZH0U5M1R2p2gqhPV/7JPJcRz+8O8GBKVgqndTzydQ7B2XGDbxtbvFkvIs53uXTobOhv+RyaqhUiMg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.17.11.tgz", + "integrity": "sha512-Qxth3gsWWGKz2/qG2d5DsW/57SeA2AmpSMhdg9TSB5Svn2KDob3qxfQSkdnWjSd42kqoxIPy3EJFs+6w1+6Qjg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.17.11.tgz", + "integrity": "sha512-dB1nGaVWtUlb/rRDHmuDQhfqazWE0LMro/AIbT2lWM3CDMHJNpLckH+gCddQyhhcLac2OYw69ikUMO34JLt3wA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.17.11.tgz", + "integrity": "sha512-aCWlq70Q7Nc9WDnormntGS1ar6ZFvUpqr8gXtO+HRejRYPweAFQN615PcgaSJkZjhHp61+MNLhzyVALSF2/Q0g==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.17.11.tgz", + "integrity": "sha512-cGeGNdQxqY8qJwlYH1BP6rjIIiEcrM05H7k3tR7WxOLmD1ZxRMd6/QIOWMb8mD2s2YJFNRuNQ+wjMhgEL2oCEw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.17.11.tgz", + "integrity": "sha512-BdlziJQPW/bNe0E8eYsHB40mYOluS+jULPCjlWiHzDgr+ZBRXPtgMV1nkLEGdpjrwgmtkZHEGEPaKdS/8faLDA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.17.11.tgz", + "integrity": "sha512-MDLwQbtF+83oJCI1Cixn68Et/ME6gelmhssPebC40RdJaect+IM+l7o/CuG0ZlDs6tZTEIoxUe53H3GmMn8oMA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.17.11.tgz", + "integrity": "sha512-4N5EMESvws0Ozr2J94VoUD8HIRi7X0uvUv4c0wpTHZyZY9qpaaN7THjosdiW56irQ4qnJ6Lsc+i+5zGWnyqWqQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.17.11.tgz", + "integrity": "sha512-rM/v8UlluxpytFSmVdbCe1yyKQd/e+FmIJE2oPJvbBo+D0XVWi1y/NQ4iTNx+436WmDHQBjVLrbnAQLQ6U7wlw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.17.11.tgz", + "integrity": "sha512-4WaAhuz5f91h3/g43VBGdto1Q+X7VEZfpcWGtOFXnggEuLvjV+cP6DyLRU15IjiU9fKLLk41OoJfBFN5DhPvag==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.17.11.tgz", + "integrity": "sha512-UBj135Nx4FpnvtE+C8TWGp98oUgBcmNmdYgl5ToKc0mBHxVVqVE7FUS5/ELMImOp205qDAittL6Ezhasc2Ev/w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.17.11.tgz", + "integrity": "sha512-1/gxTifDC9aXbV2xOfCbOceh5AlIidUrPsMpivgzo8P8zUtczlq1ncFpeN1ZyQJ9lVs2hILy1PG5KPp+w8QPPg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.17.11.tgz", + "integrity": "sha512-vtSfyx5yRdpiOW9yp6Ax0zyNOv9HjOAw8WaZg3dF5djEHKKm3UnoohftVvIJtRh0Ec7Hso0RIdTqZvPXJ7FdvQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.17.11.tgz", + "integrity": "sha512-GFPSLEGQr4wHFTiIUJQrnJKZhZjjq4Sphf+mM76nQR6WkQn73vm7IsacmBRPkALfpOCHsopSvLgqdd4iUW2mYw==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.17.11", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.11.tgz", + "integrity": "sha512-N9vXqLP3eRL8BqSy8yn4Y98cZI2pZ8fyuHx6lKjiG2WABpT2l01TXdzq5Ma2ZUBzfB7tx5dXVhge8X9u0S70ZQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, "node_modules/@hapi/bourne": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@hapi/bourne/-/bourne-2.1.0.tgz", diff --git a/src/mocks.ts b/src/mocks.ts new file mode 100644 index 00000000..41ca4865 --- /dev/null +++ b/src/mocks.ts @@ -0,0 +1,62 @@ +// 固定模拟响应 —— 用于离线联调,不调真实模型、不联网。 +// 其他模块实现完成后,用真实实现替换这些 mock 即可。 + +import type { + PRInput, + ContextSnippet, + Finding, + Risk, + Gate, +} from './types.js'; + +export const mockPRInput: PRInput = { + repo: 'CS5351-inspector/context-aware-risk-gated-code-review', + prNumber: 1, + baseSha: 'base-sha', + diffBaseSha: 'diff-base-sha', + headSha: 'head-sha', + diff: '@@ -1,3 +1,4 @@\n-old\n+new', + changedFiles: [ + { + filename: 'src/bot.ts', + status: 'modified', + patch: '@@ -1,3 +1,4 @@\n-old\n+new', + }, + ], +}; + +export const mockContextSnippets: ContextSnippet[] = [ + { + id: 'ctx-1', + file: 'src/bot.ts', + ref: 'head-sha', + startLine: 1, + endLine: 10, + code: 'function example() {\n return 1;\n}', + reason: '修改处所在函数的定义', + }, +]; + +export const mockFindings: Finding[] = [ + { + id: 'f-1', + file: 'src/bot.ts', + side: 'RIGHT', + line: 3, + severity: 'high', + message: '示例 finding:变量未初始化', + evidenceIds: ['ctx-1'], + }, +]; + +export const mockRisk: Risk = { + level: 'HIGH', + factors: ['改动涉及敏感路径 src/bot.ts'], + ruleVersion: 'v1', +}; + +export const mockGate: Gate = { + status: 'pending', + reason: '存在 HIGH 风险,等待人工批准', + requiredApprovals: 1, +}; diff --git a/src/pipeline.ts b/src/pipeline.ts new file mode 100644 index 00000000..989c4b3c --- /dev/null +++ b/src/pipeline.ts @@ -0,0 +1,84 @@ +// 审查流水线骨架:把 输入 → context → review → risk 串起来,产出 ReviewResult。 +// 各阶段默认使用 mock 实现,支持离线联调;真实实现由对应模块成员提供后替换。 + +import type { + PRInput, + ContextSnippet, + Finding, + Risk, + Gate, + ReviewResult, + RunRecord, +} from './types.js'; +import { + mockContextSnippets, + mockFindings, + mockRisk, + mockGate, +} from './mocks.js'; + +// 各阶段的接口定义(契约),真实实现签名必须与这里一致。 +export interface ContextProvider { + (input: PRInput): Promise; +} + +export interface ReviewProvider { + (input: PRInput, context: ContextSnippet[]): Promise; +} + +export interface RiskProvider { + (input: PRInput, findings: Finding[]): Promise<{ risk: Risk; gate: Gate }>; +} + +// 默认 mock 实现 +const mockContext: ContextProvider = async () => mockContextSnippets; +const mockReview: ReviewProvider = async () => mockFindings; +const mockRiskProvider: RiskProvider = async () => ({ + risk: mockRisk, + gate: mockGate, +}); + +export async function runReviewPipeline( + input: PRInput, + providers: { + context?: ContextProvider; + review?: ReviewProvider; + risk?: RiskProvider; + } = {} +): Promise<{ result: ReviewResult; record: RunRecord }> { + const startedAt = Date.now(); + + const contextProvider = providers.context ?? mockContext; + const reviewProvider = providers.review ?? mockReview; + const riskProvider = providers.risk ?? mockRiskProvider; + + // 1. 提取上下文 + const context = await contextProvider(input); + + // 2. 模型审查,产出 findings + const findings = await reviewProvider(input, context); + + // 3. 风险评估与门禁 + const { risk, gate } = await riskProvider(input, findings); + + const result: ReviewResult = { + headSha: input.headSha, + mode: 'context', + analysisStatus: 'COMPLETE', + scope: input.changedFiles.map((f) => f.filename), + omissions: [], + findings, + risk, + gate, + }; + + const record: RunRecord = { + model: process.env.MODEL || 'gpt-4o-mini', + modelParams: {}, + promptVersion: 'v1', + ruleVersion: risk.ruleVersion, + durationMs: Date.now() - startedAt, + }; + + return { result, record }; +} diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..daf4e567 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,83 @@ +// 统一数据类型 —— 依据 TECH_SPEC「统一数据」表。 +// 这是各模块之间协作的接口契约,所有模块的输入输出都对齐这里。 + +export type Severity = 'low' | 'medium' | 'high'; +export type Mode = 'diff-only' | 'context'; +export type AnalysisStatus = 'COMPLETE' | 'PARTIAL' | 'FAILED'; +export type RiskLevel = 'LOW' | 'MEDIUM' | 'HIGH' | 'UNKNOWN'; +export type Side = 'LEFT' | 'RIGHT'; + +// PR 输入 +export interface ChangedFile { + filename: string; + status: string; + patch?: string; +} + +export interface PRInput { + repo: string; + prNumber: number; + baseSha: string; + diffBaseSha: string; + headSha: string; + diff: string; + changedFiles: ChangedFile[]; +} + +// 上下文片段(Context-Aware 模块产出) +export interface ContextSnippet { + id: string; + file: string; + ref: string; // 所属版本的 SHA + startLine: number; + endLine: number; + code: string; + reason: string; // 为什么选这段作为上下文 +} + +// 发现(模型审查模块产出) +export interface Finding { + id: string; + file: string; + side: Side; + line: number | null; // 可空:无有效行号时 + severity: Severity; + message: string; + evidenceIds: string[]; // 引用的 ContextSnippet id +} + +// 风险(Risk 模块产出) +export interface Risk { + level: RiskLevel; + factors: string[]; // 触发的规则/因素 + ruleVersion: string; +} + +// 门禁(Gate 模块产出) +export interface Gate { + status: 'pass' | 'pending' | 'failure'; + reason: string; + requiredApprovals: number; +} + +// 最终审查结果 +export interface ReviewResult { + headSha: string; + mode: Mode; + analysisStatus: AnalysisStatus; + scope: string[]; // 声明支持的范围 + omissions: string[]; // 披露的裁剪/缺失 + findings: Finding[]; + risk: Risk; + gate: Gate; +} + +// 运行记录 +export interface RunRecord { + model: string; + modelParams: Record; + promptVersion: string; + ruleVersion: string; + durationMs: number; + usage?: unknown; // 缺失时标未知 +} diff --git a/test/pipeline.test.ts b/test/pipeline.test.ts new file mode 100644 index 00000000..b38ba28e --- /dev/null +++ b/test/pipeline.test.ts @@ -0,0 +1,30 @@ +import { runReviewPipeline } from '../src/pipeline'; +import { + mockPRInput, + mockFindings, + mockRisk, + mockGate, +} from '../src/mocks'; + +describe('runReviewPipeline(离线联调)', () => { + test('用 mock 响应跑通 输入→context→review→risk 整条链路', async () => { + const { result, record } = await runReviewPipeline(mockPRInput); + + expect(result.headSha).toBe(mockPRInput.headSha); + expect(result.analysisStatus).toBe('COMPLETE'); + expect(result.scope).toEqual(['src/bot.ts']); + expect(result.findings).toEqual(mockFindings); + expect(result.risk).toEqual(mockRisk); + expect(result.gate).toEqual(mockGate); + expect(record.durationMs).toBeGreaterThanOrEqual(0); + }); + + test('允许替换单个阶段为自定义实现', async () => { + const customReview = async () => []; + const { result } = await runReviewPipeline(mockPRInput, { + review: customReview, + }); + + expect(result.findings).toEqual([]); + }); +});