Merged
[feat] 同步 submit-pr skill 到 dev_0.4.0 #1246
zcklllyaoAdd on 25 days ago
[feat] 同步 submit-pr skill 到 dev_0.4.0 #1246
Merged
Total: 8 files changed+719-5
| @@ -125,4 +125,4 @@ python -m pre_commit run --all-files --show-diff-on-failure | |||
| 125 | - 需要精度验证的改动,必须在 PR 描述中提供对应验证记录:比较双方的代码版本及基线来源、运行环境与关键配置、可复现命令、指标和判定结果,以及原始日志或结果文件的可访问链接。未执行或未通过的项目必须明确状态、原因及待完成项,不得写为验证通过;无需精度验证时,须说明开发指南中的跳过依据。 | 125 | - 需要精度验证的改动,必须在 PR 描述中提供对应验证记录:比较双方的代码版本及基线来源、运行环境与关键配置、可复现命令、指标和判定结果,以及原始日志或结果文件的可访问链接。未执行或未通过的项目必须明确状态、原因及待完成项,不得写为验证通过;无需精度验证时,须说明开发指南中的跳过依据。 |
| 126 | - PR 描述解释「为什么」而非只写「做了什么」;非简单改动附数值证据,模型变更说明 checkpoint 兼容性。 | 126 | - PR 描述解释「为什么」而非只写「做了什么」;非简单改动附数值证据,模型变更说明 checkpoint 兼容性。 |
| 127 | - 准备、修改或 review PR 时,若改动涉及公开行为、模型支持、配置、训练或测试入口、依赖或软件栈,或用户明确要求检查文档遗漏,使用 `.agents/skills/check-pr-docs/`。默认只读;仅在用户明确要求同步修改文档或依赖时进入修改模式,已有授权继续有效。纯内部重构、普通新增测试且公开契约不变时不自动触发;仅推送已审查且未变化的内容时也不触发。只读模式不编辑文件、不运行可能改文件的检查。 | 127 | - 准备、修改或 review PR 时,若改动涉及公开行为、模型支持、配置、训练或测试入口、依赖或软件栈,或用户明确要求检查文档遗漏,使用 `.agents/skills/check-pr-docs/`。默认只读;仅在用户明确要求同步修改文档或依赖时进入修改模式,已有授权继续有效。纯内部重构、普通新增测试且公开契约不变时不自动触发;仅推送已审查且未变化的内容时也不触发。只读模式不编辑文件、不运行可能改文件的检查。 |
| 128 | -- 已安装 `gitcode-pr` skill 时,创建或推送 PR、读取改动与评论使用该 skill,并按 `.gitcode/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md` 填写。标题使用正确的英文类型标签,如 `feat`、`fix`、`refactor`、`docs` 或 `test`;`类型` 和 `Checklist` 只勾选真实完成项,`如何测试` 写实际命令或未执行原因。 | 128 | +- 创建或推送 PR、读取改动与评论使用 `.agents/skills/submit-pr/`;如果同时安装 `gitcode-pr`,以 `.agents/skills/submit-pr/` 的模板和流程为准。标题使用 `[type] 描述` 格式(如 `[feat]`、`[fix]`、`[refactor]`、`[docs]`、`[test]`);`类型` 和 `Checklist` 只勾选真实完成项,`如何测试` 写实际命令或未执行原因。 |
| @@ -47,9 +47,9 @@ description: "检查 torchtitan-npu PR 涉及的文档、示例命令、依赖 | |||
| 47 | 47 | ||
| 48 | ### 1. 确定 PR 范围和修复基线 | 48 | ### 1. 确定 PR 范围和修复基线 |
| 49 | 49 | ||
| 50 | -先确认目标仓库、目标分支、PR 状态和 base/head;已安装 `gitcode-pr` 时可用其读取元数据, | 50 | +先确认目标仓库、目标分支、PR 状态和 base/head;用 `submit-pr` skill 读取元数据,或使用 |
| 51 | -否则使用用户给出的信息或 GitCode 页面/API。不要默认 fork 的 `origin/master` 就是目标仓 | 51 | +用户给出的信息或 GitCode 页面/API。不要默认 fork 的 `origin/master` 就是目标仓最新代码。 |
| 52 | -最新代码。用户明确指定修复仓库或基线时按其要求,并记录与 PR 目标仓的差异。 | 52 | +用户明确指定修复仓库或基线时按其要求,并记录与 PR 目标仓的差异。 |
| 53 | 53 | ||
| 54 | 对仍待合并的 GitCode PR,可替换下面三个值后获取目标分支和 PR head;每次 fetch 成功后 | 54 | 对仍待合并的 GitCode PR,可替换下面三个值后获取目标分支和 PR head;每次 fetch 成功后 |
| 55 | 立即保存 SHA,任一步失败就停止这组命令,不能继续使用旧的 `FETCH_HEAD`: | 55 | 立即保存 SHA,任一步失败就停止这组命令,不能继续使用旧的 `FETCH_HEAD`: |
| @@ -0,0 +1,84 @@ | |||
| 1 | +--- | ||
| 2 | +name: submit-pr | ||
| 3 | +description: "在 torchtitan-npu 仓库创建、更新或推送 GitCode Pull Request。用于提交/创建/更新 PR、提 PR、发 PR、推送代码、生成精简 PR 标题或描述、提交前代码自审、精度/性能验证。要求检查各级 AGENTS.md、控制单个 PR 代码变更不超过 1k 行,并遵循 check-pr-docs 与 developer-tests-review。不负责业务实现或 CI 修复。" | ||
| 4 | +--- | ||
| 5 | + | ||
| 6 | +# torchtitan-npu 提交 PR | ||
| 7 | + | ||
| 8 | +只有用户明确要求提交、创建或更新 PR 时,才执行提交、推送和远端写操作。流程:读规则 → 定范围 → 自审 → 验证 → 判断精度影响(仅新建 PR)→ 写描述 → 推送创建 → 回读。 | ||
| 9 | + | ||
| 10 | +## 规则 | ||
| 11 | + | ||
| 12 | +- 读根 `AGENTS.md`、`.agents/AGENTS.md` 和改动路径上所有目录级 `AGENTS.md`,最近的目录规则优先。 | ||
| 13 | +- `.gitcode/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md` 是唯一模板来源;忽略远程 skill 的固定模板路径。 | ||
| 14 | +- 文档和依赖按 `check-pr-docs`,测试按 `developer-tests-review`,数值按 `numerical_alignment.md`,性能按 `metrics_and_debugging.md` 的 Profiling 部分。 | ||
| 15 | +- 详细自审项见 [references/review-checklist.md](references/review-checklist.md)。 | ||
| 16 | + | ||
| 17 | +## 范围与规模 | ||
| 18 | + | ||
| 19 | +- 新建 PR 时,目标分支使用用户指定分支或仓库默认分支(当前 `master`);更新已有 PR 时,先读取该 PR 的实际 base/head,不要默认 `master`。 | ||
| 20 | +- 用 merge-base 计算 diff,不要用 `HEAD~1`:`merge_base=$(git merge-base "$(git rev-parse <base_remote>/<base>)" HEAD)`,然后执行 `git diff --name-status "$merge_base...HEAD"`;更新已有 PR 时 `<base>` 用该 PR 的实际目标分支。 | ||
| 21 | +- 检查 `git status --short`;排除生成物、日志、密钥、`.agents/skills/_remote/` 和远程 skill 软链。 | ||
| 22 | +- 不要自行新建分支或 cherry-pick。如果当前分支是目标分支或含无关提交,暂停并让用户处理。 | ||
| 23 | +- 代码增删合计不超过 1000 行(纯文档、生成数据和测试数据不计);超出时拆分 PR,或取得用户或 reviewer 的明确同意。 | ||
| 24 | + | ||
| 25 | +## 自审与验证 | ||
| 26 | + | ||
| 27 | +- 满足各级 `AGENTS.md` 的要求:代码放置、依赖方向、导入和注册顺序、命名、断言、参数、注释和测试规则。 | ||
| 28 | +- 简化:复用现有实现;删除多余抽象、重复代码、未使用的导入和变量、死代码、注释掉的代码、调试输出和临时开关。 | ||
| 29 | +- 注释只保留维度语义、并行梯度 placement、workaround 原因、上游 PR 等必要信息;删除复述代码的注释;TODO 写明限制。 | ||
| 30 | +- 检查共享代码的所有调用点,标出 checkpoint、数值、性能或用户行为风险。 | ||
| 31 | +- 文档和依赖检查默认只读,未获授权不修改。 | ||
| 32 | +- 精简后重新统计行数;改代码后重跑受影响的检查。 | ||
| 33 | +- 未执行或未通过的检查必须写明原因,不能写成通过。以下 Lint 和 CPU UT 在 Linux 环境执行; | ||
| 34 | + | ||
| 35 | +```bash | ||
| 36 | +# Lint | ||
| 37 | +python -m pre_commit run --all-files --show-diff-on-failure | ||
| 38 | + | ||
| 39 | +# CPU UT | ||
| 40 | +TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -v --tb=short tests/unit_tests -x | ||
| 41 | +``` | ||
| 42 | + | ||
| 43 | +- 精度:按 `numerical_alignment.md` 执行验证;修改前后训练对比优先用 `scripts/numerical_alignment/loss_compare.py`(两个 Git ref),需要定位到算子/模块时再用 `.agents/skills/numerics-debugging/`;证据按下方“如何测试 / 精度和性能验证结果”记录。 | ||
| 44 | +- 性能:涉及 kernel、并行、编译、显存、融合或通信等性能敏感改动时,按 `metrics_and_debugging.md` 采集;相同环境和配置下对比 baseline,记录吞吐、时延、显存或 MFU;未测量就写“未测量”,且不得给出性能提升或回退结论。以性能优化为目标的 PR,未完成性能验证时同样不得创建可审查 PR。 | ||
| 45 | +- NPU:涉及分布式、kernel、override、patch、模型训练或 checkpoint 的改动,执行对应的 smoke 或 integration 测试;CPU UT 不计入 ST 覆盖。 | ||
| 46 | + | ||
| 47 | +## 提交前判断精度影响 | ||
| 48 | + | ||
| 49 | +创建新 PR 前,agent 按 `numerical_alignment.md` 第 4 节和 diff 判断并分类;更新已有 PR 时只更新本次修改涉及的描述部分,补充后重新压缩整段描述,删除过期、重复或已完成内容,保持精简,不改标题、draft 状态或 review 状态。 | ||
| 50 | + | ||
| 51 | +判断为有影响,或改动命中上方验证范围、可能改变模型输出、梯度、loss、参数更新、随机行为时: | ||
| 52 | + | ||
| 53 | +- **无影响**:直接创建可审查 PR;PR 描述仍要按 `numerical_alignment.md` / `.agents/AGENTS.md` 写明跳过精度验证的依据,并保留 `精度和性能验证结果` 段。 | ||
| 54 | +- **有影响**:只创建草稿 PR(`draft=true`),在 `如何测试` 的 `精度和性能验证结果` 段标注“精度验证待补充”,相关 Checklist 不勾选,不请求 review;由用户自行补充验证后转为可审查 PR。 | ||
| 55 | + | ||
| 56 | +判断不清时按“有影响”处理,直接创建草稿 PR。 | ||
| 57 | + | ||
| 58 | +## 标题与描述 | ||
| 59 | + | ||
| 60 | +- 标题固定为 `[type] 描述`:优先 `[feat]`、`[fix]`、`[refactor]`、`[docs]`、`[test]`;仓库已有其他类型且与改动一致时也可使用;只选一个类型;禁止 `feat: 描述`、`test: 描述` 或无标签标题。 | ||
| 61 | +- 严格使用仓库模板,保留全部二级标题:`描述`、`类型`、`Checklist:`、`如何测试`(含 `精度和性能验证结果` 三级标题)、`其他信息`。标题层级和顺序与模板一致;`类型` 和 `Checklist:` 条目逐字沿用模板,只改勾选状态。 | ||
| 62 | +- 提交前运行 `python .agents/skills/submit-pr/scripts/validate_pr_body.py --body <pr-body.md>`,失败则修正后重试。 | ||
| 63 | +- 描述只保留核心信息(正文 10-15 行,说明为什么改、行为变化和影响范围),不要逐文件复述 diff 或粘贴大段日志、trace、代码;如何测试写实际命令和结果摘要,未执行写原因;精度和性能结果用紧凑表格给出基线/版本、环境、命令、核心指标、判定和证据链接,无验证时写明跳过依据;其他信息只写 checkpoint 兼容性、风险、待完成项、文档和依赖影响、关联 issue。 | ||
| 64 | +- 类型默认只勾一个;只有模板或用户明确允许多选时才勾多个。Checklist 只勾真实完成项:Lint 通过才勾代码风格,测试执行才勾已测试,文档实际更新才勾文档,标题格式正确才勾类型标签。 | ||
| 65 | +- 隐私:不得包含个人隐私信息、token 或密钥、内网 IP、主机名、内部 URL、客户或未公开数据、带用户名的本地路径;用 `<user>`、`<host>`、`<token>` 等占位符或脱敏链接,敏感原文走仓库认可的私下渠道。 | ||
| 66 | + | ||
| 67 | +## 推送与创建 | ||
| 68 | + | ||
| 69 | +- 有写权限时推送到 `origin`,否则推送到个人 fork;使用 `git push -u <remote> <branch>`,不要强制推送;有未提交改动时先确认范围,不要直接用 `git add -A`。 | ||
| 70 | +- 只检查 `GITCODE_API_TOKEN` 是否存在,不打印、不写入文件;fork 使用 `parent.full_name`、`head=username:branch`,必要时传 `fork_path`;payload 用 `curl --data-binary`。 | ||
| 71 | +- 创建 PR 使用 `POST /repos/{owner}/{repo}/pulls`;已有同源 PR 时用 `PATCH` 只更新本次修改涉及的描述部分,不改 title、draft 或 review 状态,不要重复创建。端点见 [references/gitcode-api.md](references/gitcode-api.md)。 | ||
| 72 | +- 有影响时按上节创建 `draft=true` 的草稿 PR,在精度结果段标注“精度验证待补充”,不请求 review;有环境时先跑 `loss_compare.py` 并把结果或链接写进草稿 PR。不要替用户转为可审查 PR。 | ||
| 73 | +- 创建后回读 PR,核对标题、分支、描述、draft 状态和隐私保护;报告 `html_url` 和未完成项。 | ||
| 74 | +- 本仓 PR pipeline 由 `/compile` 评论触发。 | ||
| 75 | + | ||
| 76 | +## 完成检查 | ||
| 77 | + | ||
| 78 | +- [ ] 已读各级 `AGENTS.md` 和 PR 模板 | ||
| 79 | +- [ ] diff 中没有无关文件;代码增删合计 ≤1k 行或已获同意 | ||
| 80 | +- [ ] 代码已自审、精简,注释只保留必要项 | ||
| 81 | +- [ ] 测试、文档和依赖要求已按对应 skill 检查 | ||
| 82 | +- [ ] Lint、UT、NPU、性能完成或记录原因;精度影响已判断(更新 PR 只改描述并重新压缩,有影响的新 PR 仅建草稿并在精度段标注待补充) | ||
| 83 | +- [ ] 标题和描述符合模板;类型只勾一个;Checklist 只勾真实项;校验通过 | ||
| 84 | +- [ ] 隐私已保护;PR 已回读,链接和未完成项已交付 | ||
| @@ -0,0 +1,193 @@ | |||
| 1 | +# GitCode PR API 参考(v5) | ||
| 2 | + | ||
| 3 | +## 认证 | ||
| 4 | + | ||
| 5 | +API 根地址:`https://api.gitcode.com/api/v5` | ||
| 6 | + | ||
| 7 | +优先使用请求头:`Authorization: Bearer ${GITCODE_API_TOKEN}`,不要把 token 放进 | ||
| 8 | +URL。也支持 `PRIVATE-TOKEN` 头和 `?access_token=` 查询参数,但查询参数容易进入 | ||
| 9 | +日志和 shell 历史,只在无法使用请求头时使用。Token 至少需要 `read_repository`、 | ||
| 10 | +`write_repository` 和 `read_api` 权限。 | ||
| 11 | + | ||
| 12 | +只判断 token 是否存在,不要打印,也不要写入仓库文件、提交、PR 描述或日志;同样 | ||
| 13 | +不要把带 token 的 URL、个人邮箱或电话、内网地址和本地用户路径作为证据链接。 | ||
| 14 | + | ||
| 15 | +Windows PowerShell 中 `curl` 可能是 `Invoke-WebRequest` 的别名;使用 `curl.exe` | ||
| 16 | +或在 Git Bash 中执行本文命令。 | ||
| 17 | + | ||
| 18 | +## 仓库和 fork | ||
| 19 | + | ||
| 20 | +```bash | ||
| 21 | +GET /repos/{owner}/{repo} | ||
| 22 | +``` | ||
| 23 | + | ||
| 24 | +```bash | ||
| 25 | +curl -sS -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 26 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}" | | ||
| 27 | + jq '{fork, parent: .parent.full_name, default_branch}' | ||
| 28 | +``` | ||
| 29 | + | ||
| 30 | +- `fork=true` 时,目标仓库使用 `parent.full_name`;`head` 使用 `username:branch`; | ||
| 31 | + 跨仓 PR 传 `fork_path=owner/repo`。 | ||
| 32 | +- 当前用户:`GET /user`,取 `.login`。 | ||
| 33 | +- 解析 remote: | ||
| 34 | + | ||
| 35 | +```bash | ||
| 36 | +repo_url=$(git remote get-url origin) | ||
| 37 | +repo_url=${repo_url%.git} | ||
| 38 | +case "$repo_url" in | ||
| 39 | + *://*) repo_path=${repo_url#*://}; repo_path=${repo_path#*/} ;; | ||
| 40 | + *@*:*) repo_path=${repo_url#*:} ;; | ||
| 41 | + *) repo_path=$repo_url ;; | ||
| 42 | +esac | ||
| 43 | +owner=${repo_path%%/*} | ||
| 44 | +repo=${repo_path#*/} | ||
| 45 | +``` | ||
| 46 | + | ||
| 47 | +不依赖 `sed -E`,兼容 macOS;带端口的 `ssh://` remote 请先转成 `git@host:owner/repo` | ||
| 48 | +形式。 | ||
| 49 | + | ||
| 50 | +不要假设 `origin` 就是目标仓库;用户指定 `upstream` 时以用户指定为准。 | ||
| 51 | + | ||
| 52 | +## 创建 PR | ||
| 53 | + | ||
| 54 | +```bash | ||
| 55 | +POST /repos/{owner}/{repo}/pulls | ||
| 56 | +``` | ||
| 57 | + | ||
| 58 | +请求体:`title`、`head`、`base`、`body`,跨仓时加 `fork_path`;可选 `draft`、 | ||
| 59 | +`labels`、`assignees`、`testers`。精度证据待补充时可传 `"draft": true`;转为可审查 | ||
| 60 | +由用户在 PR 界面手动完成。 | ||
| 61 | + | ||
| 62 | +用 Python 读取 Markdown 正文,避免 shell 引号和换行问题: | ||
| 63 | + | ||
| 64 | +```bash | ||
| 65 | +python - <<'PY' | ||
| 66 | +import json, os, pathlib | ||
| 67 | +payload = { | ||
| 68 | + "title": os.environ["PR_TITLE"], | ||
| 69 | + "head": os.environ["PR_HEAD"], | ||
| 70 | + "base": os.environ["PR_BASE"], | ||
| 71 | + "body": pathlib.Path("pr-body.md").read_text(encoding="utf-8"), | ||
| 72 | + "draft": os.environ.get("PR_DRAFT", "false").lower() == "true", | ||
| 73 | +} | ||
| 74 | +if os.environ.get("PR_FORK_PATH"): | ||
| 75 | + payload["fork_path"] = os.environ["PR_FORK_PATH"] | ||
| 76 | +pathlib.Path("pr-payload.json").write_text( | ||
| 77 | + json.dumps(payload, ensure_ascii=False), encoding="utf-8") | ||
| 78 | +PY | ||
| 79 | + | ||
| 80 | +curl -sS -X POST \ | ||
| 81 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls" \ | ||
| 82 | + -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 83 | + -H "Content-Type: application/json" \ | ||
| 84 | + --data-binary @pr-payload.json | ||
| 85 | +``` | ||
| 86 | + | ||
| 87 | +有精度影响时设置 `PR_DRAFT=true`;创建后 `GET` 回读 `draft` 字段确认。响应中关注 | ||
| 88 | +`number` 和 `html_url`;PR 网页地址以响应为准,不要自行拼接。 | ||
| 89 | + | ||
| 90 | +## 更新 PR | ||
| 91 | + | ||
| 92 | +```bash | ||
| 93 | +PATCH /repos/{owner}/{repo}/pulls/{number} | ||
| 94 | +``` | ||
| 95 | + | ||
| 96 | +更新已有 PR 时只更新描述,payload 只放 `body`;不要带 `title`、`head`、`base` | ||
| 97 | +或 `draft`,避免覆盖标题和草稿状态: | ||
| 98 | + | ||
| 99 | +```bash | ||
| 100 | +python - <<'PY' | ||
| 101 | +import json, pathlib | ||
| 102 | +payload = { | ||
| 103 | + "body": pathlib.Path("pr-body.md").read_text(encoding="utf-8"), | ||
| 104 | +} | ||
| 105 | +pathlib.Path("pr-update-payload.json").write_text( | ||
| 106 | + json.dumps(payload, ensure_ascii=False), encoding="utf-8") | ||
| 107 | +PY | ||
| 108 | + | ||
| 109 | +curl -sS -X PATCH \ | ||
| 110 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls/${number}" \ | ||
| 111 | + -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 112 | + -H "Content-Type: application/json" \ | ||
| 113 | + --data-binary @pr-update-payload.json | ||
| 114 | +``` | ||
| 115 | + | ||
| 116 | +更新后重新 `GET` 一次确认。只有用户明确要求改标题时,才另做一次只带 `title` | ||
| 117 | +的更新,不要和描述更新混在一起。 | ||
| 118 | + | ||
| 119 | +## 获取 PR 和文件变更 | ||
| 120 | + | ||
| 121 | +```bash | ||
| 122 | +GET /repos/{owner}/{repo}/pulls/{number} | ||
| 123 | +GET /repos/{owner}/{repo}/pulls/{number}/files.json | ||
| 124 | +``` | ||
| 125 | + | ||
| 126 | +```bash | ||
| 127 | +curl -sS -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 128 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls/${number}" | | ||
| 129 | + jq '{number,title,state,target_branch,source_branch,html_url,web_url}' | ||
| 130 | + | ||
| 131 | +curl -sS -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 132 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls/${number}/files.json" | | ||
| 133 | + jq '.diffs[] | .statistic' | ||
| 134 | +``` | ||
| 135 | + | ||
| 136 | +## 查找已有 PR | ||
| 137 | + | ||
| 138 | +```bash | ||
| 139 | +GET /repos/{owner}/{repo}/pulls?state=open&per_page=100 | ||
| 140 | +``` | ||
| 141 | + | ||
| 142 | +```bash | ||
| 143 | +curl -sS -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 144 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls?state=open&per_page=100" | | ||
| 145 | + jq --arg b "$branch" --arg t "$base" \ | ||
| 146 | + '.[] | select(.source_branch == $b and .target_branch == $t) | | ||
| 147 | + {number, html_url, state}' | ||
| 148 | +``` | ||
| 149 | + | ||
| 150 | +找到 head 和 base 相同的 PR 时,用 `PATCH` 更新,不要重复创建。 | ||
| 151 | + | ||
| 152 | +## 评论 | ||
| 153 | + | ||
| 154 | +```bash | ||
| 155 | +GET /repos/{owner}/{repo}/pulls/{number}/comments | ||
| 156 | +POST /repos/{owner}/{repo}/pulls/{number}/comments | ||
| 157 | +POST /repos/{owner}/{repo}/pulls/{number}/discussions/{discussion_id}/comments | ||
| 158 | +``` | ||
| 159 | + | ||
| 160 | +列表接口的关键字段包括 `id`、`discussion_id`、`body`、`user.login` 和 | ||
| 161 | +`comment_type`;行内评论的位置在 `diff_position.start_new_line`,列表接口没有顶层 | ||
| 162 | +`path`/`position`。`comment_type` 为 `pr_comment` 或 `diff_comment`。 | ||
| 163 | + | ||
| 164 | +单条评论接口 `GET /repos/{owner}/{repo}/pulls/comments/{id}`:顶层 `path` 仍可能为 | ||
| 165 | +空,实际路径和行号在 `position.new_path` / `position.new_line`,`comment_type` 为 | ||
| 166 | +`DiffNote`。 | ||
| 167 | + | ||
| 168 | +```bash | ||
| 169 | +curl -sS -X POST \ | ||
| 170 | + "https://api.gitcode.com/api/v5/repos/${owner}/${repo}/pulls/${number}/comments" \ | ||
| 171 | + -H "Authorization: Bearer ${GITCODE_API_TOKEN}" \ | ||
| 172 | + -H "Content-Type: application/json" \ | ||
| 173 | + -d '{"body":"评论内容"}' | ||
| 174 | +``` | ||
| 175 | + | ||
| 176 | +## 常见错误 | ||
| 177 | + | ||
| 178 | +| 状态码 | 原因 | 处理 | | ||
| 179 | +| --- | --- | --- | | ||
| 180 | +| `400` | 缺少 `title`/`head`/`base` | 检查 payload | | ||
| 181 | +| `401` | token 缺失或无效 | 重新提供 token | | ||
| 182 | +| `403` | 无权限推送或创建 PR | 检查仓库权限和 fork | | ||
| 183 | +| `404` | 仓库、分支或 PR 不存在 | 检查 owner/repo、分支和编号 | | ||
| 184 | +| `422` | 分支不存在、重复 PR 或字段不合法 | 检查 head/base,查找已有 PR | | ||
| 185 | +| `429` | 速率限制 | 降低频率后重试 | | ||
| 186 | + | ||
| 187 | +## 官方文档 | ||
| 188 | + | ||
| 189 | +- https://docs.gitcode.com/docs/apis/ | ||
| 190 | +- https://docs.gitcode.com/docs/apis/post-api-v-5-repos-owner-repo-pulls | ||
| 191 | +- https://docs.gitcode.com/docs/apis/patch-api-v-5-repos-owner-repo-pulls-number | ||
| 192 | +- https://docs.gitcode.com/docs/apis/get-api-v-5-repos-owner-repo-pulls-number | ||
| 193 | +- https://docs.gitcode.com/docs/apis/get-api-v-5-repos-owner-repo-pulls-number-comments | ||
| @@ -0,0 +1,117 @@ | |||
| 1 | +# 提交前代码自审清单 | ||
| 2 | + | ||
| 3 | +按阻塞、重要、建议三个级别输出;每条给出文件与行号、证据、影响和最小修正。 | ||
| 4 | + | ||
| 5 | +## 1. 规则和范围 | ||
| 6 | + | ||
| 7 | +- 读取根 `AGENTS.md`、`.agents/AGENTS.md` 和改动路径上所有 `AGENTS.md`,最近的 | ||
| 8 | + 目录规则优先。 | ||
| 9 | +- 读取 PR 模板、`check-pr-docs`、`developer-tests-review`、`numerical_alignment.md` | ||
| 10 | + 和 `metrics_and_debugging.md` 的相关部分。 | ||
| 11 | +- diff 基于 merge-base,不包含无关提交、生成物、日志、密钥、`_remote` 或远程 | ||
| 12 | + skill 软链;更新已有 PR 时先读取实际 base/head,不用仓库默认分支。 | ||
| 13 | +- 代码增删合计不超过 1000 行(排除纯文档、生成数据和测试数据);超出时已拆分, | ||
| 14 | + 或已取得明确同意。 | ||
| 15 | +- 没有混入无关的格式化、重命名或重构。 | ||
| 16 | + | ||
| 17 | +## 2. AGENTS.md 合规 | ||
| 18 | + | ||
| 19 | +| 改动路径 | 重点 | | ||
| 20 | +| --- | --- | | ||
| 21 | +| 根目录、`.agents/**` | 仓库入口、代码归属、skill、测试和文档规则 | | ||
| 22 | +| `torchtitan_npu/**` | 目标结构、代码放置、依赖方向、注入顺序 | | ||
| 23 | +| `patches/**` | 临时补丁、`Upstream-PR`、`apply()`、显式导入 | | ||
| 24 | +| `override/**` | `override.imports` 显式启用,不自动导入 | | ||
| 25 | +| `models/**`、`ops/**` | 模型和算子边界、checkpoint 适配、注册 | | ||
| 26 | +| `tests/**` | UT 和 ST 分层、runner、格式、数值验证 | | ||
| 27 | +| `docs/**` | 文档归属、命令依据、支持范围、验证状态 | | ||
| 28 | +| `scripts/**`、`experiments/**`、`examples/**` | 脚本入口、实验隔离、示例依赖 | | ||
| 29 | + | ||
| 30 | +- 代码放在最通用的适用位置;模型无关的能力不要放进模型专属文件。 | ||
| 31 | +- `patches/` 不得新增对 `extensions/` 的依赖;`extensions/` 不得新增对 | ||
| 32 | + `models/`、`override/` 或 `ops/` 的依赖。 | ||
| 33 | +- `__init__.py` 的自动导入顺序为 `patches → config → extensions → ops`。 | ||
| 34 | +- 临时 patch 要记录 `Upstream-PR`;monkey-patch 使用完整点路径,并收敛到 `apply()`。 | ||
| 35 | +- 命名要准确、有描述性;生产代码不要使用 `toy`、`test`、`temp`;计数使用 `num_` | ||
| 36 | + 前缀。 | ||
| 37 | +- 用户可见的错误使用 `ValueError`;内部不变量使用 `assert`;分布式代码中显式验证 | ||
| 38 | + mesh、placement 和配置。 | ||
| 39 | +- 修改共享代码时,检查所有调用点,包括 DeepSeek-V3.2、DeepSeek-V4、override 和 | ||
| 40 | + patch 注册入口。 | ||
| 41 | + | ||
| 42 | +## 3. 精简和注释 | ||
| 43 | + | ||
| 44 | +- 复用现有实现,不新增重复的 wrapper、工具函数或抽象。 | ||
| 45 | +- 删除未使用的导入和变量、死代码、注释掉的代码、调试输出、临时开关和兼容垫片。 | ||
| 46 | +- 保持最小改动,不顺手修复无关问题,也不为单次使用增加配置、泛化或依赖。 | ||
| 47 | +- 检查是否有重复计算、重复 IO、重复拷贝和不必要的同步或等待;同时检查异常路径和 | ||
| 48 | + 资源清理。 | ||
| 49 | +- 注释只保留维度语义、并行梯度 placement、workaround 原因、上游 PR 引用和已知限制。 | ||
| 50 | +- 删除复述代码、历史过程和个人备注的注释;TODO 要说明限制;注释用英文,文档用 | ||
| 51 | + 中文。 | ||
| 52 | + | ||
| 53 | +## 4. 正确性和兼容性 | ||
| 54 | + | ||
| 55 | +- 检查空输入、最小 shape、单 rank 和多 rank、不同 mesh 和 placement 等边界。 | ||
| 56 | +- 检查配置字段名、默认值、命令行、公开 API 和序列化兼容性。 | ||
| 57 | +- 检查 checkpoint 和 state_dict 的键名、shape、dtype、layout、加载和保存路径。 | ||
| 58 | +- 检查随机性、确定性、归约顺序、混合精度和 HF32 设置。 | ||
| 59 | +- 检查是否引入额外拷贝、同步、通信、编译失败或显存峰值。 | ||
| 60 | +- 检查已验证的代码路径是否可能静默失效;存疑时先询问。 | ||
| 61 | +- 创建新 PR 前,按 `numerical_alignment.md` 第 4 节和 diff 判断并分类,不询问用户; | ||
| 62 | + 更新已有 PR 只更新本次修改涉及的描述部分,补充后重新压缩整段描述,删除过期或 | ||
| 63 | + 重复内容,不改标题、draft 或 review 状态。 | ||
| 64 | +- 新建 PR 时,无影响直接创建可审查 PR,描述仍写跳过依据;有影响只创建草稿 PR,在 | ||
| 65 | + `精度和性能验证结果` 段标注“精度验证待补充”,不请求 review,由用户自行补充验证 | ||
| 66 | + 后转为可审查 PR。未验证前不得创建可审查 PR;判断不清时按有影响处理。 | ||
| 67 | +- 修改前后精度对比优先用 `scripts/numerical_alignment/loss_compare.py`(两个 Git | ||
| 68 | + ref);只有结果不一致或需要定位到算子/模块时,才用 `numerics-debugging` 的激活 | ||
| 69 | + 捕获与 diff(两次运行同 dtype/seed,完成后还原临时 patch)。 | ||
| 70 | + | ||
| 71 | +## 5. 测试 | ||
| 72 | + | ||
| 73 | +按 `developer-tests-review` 检查: | ||
| 74 | + | ||
| 75 | +- UT 应保护正向功能和独立 expected,不要复制实现作为 expected。 | ||
| 76 | +- ST 应进入真实模型、override、并行和编译组合;CPU UT 不计入 ST 覆盖。 | ||
| 77 | +- 测试格式按 `references/format-review.md` 检查;格式检查不能代替覆盖结论。 | ||
| 78 | +- `review测试` 保持只读,不导入、收集或执行测试,并写明“测试执行:未执行 | ||
| 79 | + (仅静态审查)”。 | ||
| 80 | +- `更新测试` 要实际执行相关用例并迭代到通过;未执行不得写成通过。 | ||
| 81 | +- 结论只使用:`可以合入`、`补充测试后合入`、`暂停并澄清`、`不适用`。 | ||
| 82 | +- 除非用户授权,不要修改生产实现来迁就测试;第三方改动不影响本仓适配时,结论写 | ||
| 83 | + “不适用”。 | ||
| 84 | + | ||
| 85 | +## 6. 文档和依赖 | ||
| 86 | + | ||
| 87 | +按 `check-pr-docs` 检查: | ||
| 88 | + | ||
| 89 | +- 检查公开行为、模型支持、配置、训练和测试入口、依赖或软件栈的变化是否影响 | ||
| 90 | + README、docs、示例、安装说明或 CI。 | ||
| 91 | +- 影响分类包括:必须同步、条件性同步、无需同步、既有漂移、并行冲突。 | ||
| 92 | +- 依赖变更时,核对 `requirements.txt`、`pyproject.toml`、`requirements-dev.txt`、 | ||
| 93 | + `.ci/` 和 `.gitcode/`。 | ||
| 94 | +- 默认只读;未获授权不修改文档或依赖。需要修改时,遵循 | ||
| 95 | + `write-torchtitan-npu-docs`,并确保命令、路径、配置键和版本都有来源。 | ||
| 96 | +- 不要宣称未验证的结论,不要自动选择最新版本;文档与实现冲突时先报告,不要修改 | ||
| 97 | + 生产代码来迁就文档。 | ||
| 98 | + | ||
| 99 | +## 7. 隐私和敏感信息 | ||
| 100 | + | ||
| 101 | +- 检查标题、描述、评论、commit message、日志、trace、截图和证据链接。 | ||
| 102 | +- 不得包含个人隐私信息、token 或密钥、内网 IP、主机名、内部 URL、客户数据、 | ||
| 103 | + 未公开数据,以及带用户名的本地路径。 | ||
| 104 | +- 使用 `<user>`、`<host>`、`<token>` 等占位符或脱敏链接;测试数据使用合成或 | ||
| 105 | + 脱敏数据。 | ||
| 106 | +- 必须提供敏感原文时,改用仓库认可的私下渠道;同时检查临时 payload、日志和截图 | ||
| 107 | + 没有被提交。 | ||
| 108 | +- 用 `git log --format='%h %an <%ae>' <merge_base>..HEAD` 检查提交作者信息。 | ||
| 109 | + | ||
| 110 | +## 8. 输出 | ||
| 111 | + | ||
| 112 | +- **阻塞**:正确性、兼容性、违反规则、测试失败、精度验证缺失(可审查 PR)、隐私泄露 | ||
| 113 | + 或未授权改动;必须修复。 | ||
| 114 | +- **重要**:其他验证缺口、文档或依赖待同步、性能回退风险;修复或明确说明。 | ||
| 115 | +- **建议**:可以简化、可读性不足或注释冗余;不阻塞。 | ||
| 116 | + | ||
| 117 | +最后给出自审结论、已修复项、未解决风险和待补验证。不要只写“已 review”。 | ||
| @@ -0,0 +1,173 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""Validate a PR body against the repository PR template.""" | ||
| 3 | + | ||
| 4 | +import argparse | ||
| 5 | +import contextlib | ||
| 6 | +import pathlib | ||
| 7 | +import re | ||
| 8 | +import sys | ||
| 9 | + | ||
| 10 | +CHECKBOX_RE = re.compile(r"^\s*[-*+]\s*\[([ xX])\]\s*(.+?)\s*$") | ||
| 11 | +HEADING_RE = re.compile(r"^(#{2,3})\s+(.+?)\s*$") | ||
| 12 | +ANY_HEADING_RE = re.compile(r"^#{1,6}\s+") | ||
| 13 | +PROMPTS = [ | ||
| 14 | + "请提供此Pull Request的预期功能", | ||
| 15 | + "简要描述测试方案", | ||
| 16 | + "添加精度和性能验证结果", | ||
| 17 | + "在这里可以添加任何与这个 Pull Request 相关的其他说明", | ||
| 18 | +] | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +def read_text(path): | ||
| 22 | + return pathlib.Path(path).read_text(encoding="utf-8-sig") | ||
| 23 | + | ||
| 24 | + | ||
| 25 | +def unfenced_lines(text): | ||
| 26 | + in_fence = False | ||
| 27 | + for index, line in enumerate(text.splitlines()): | ||
| 28 | + if line.strip().startswith("```"): | ||
| 29 | + in_fence = not in_fence | ||
| 30 | + continue | ||
| 31 | + if not in_fence: | ||
| 32 | + yield index, line | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +def heading_entries(text): | ||
| 36 | + lines = text.splitlines() | ||
| 37 | + entries = [] | ||
| 38 | + for index, line in unfenced_lines(text): | ||
| 39 | + match = HEADING_RE.match(line) | ||
| 40 | + if match: | ||
| 41 | + entries.append((index, match.group(1), match.group(2))) | ||
| 42 | + return lines, entries | ||
| 43 | + | ||
| 44 | + | ||
| 45 | +def extract_headings(text): | ||
| 46 | + _, entries = heading_entries(text) | ||
| 47 | + return [(level, name) for _, level, name in entries] | ||
| 48 | + | ||
| 49 | + | ||
| 50 | +def extract_section(text, heading): | ||
| 51 | + lines, entries = heading_entries(text) | ||
| 52 | + for position, (index, level, name) in enumerate(entries): | ||
| 53 | + if name != heading: | ||
| 54 | + continue | ||
| 55 | + end = len(lines) | ||
| 56 | + for next_index, next_level, _ in entries[position + 1 :]: | ||
| 57 | + if len(next_level) <= len(level): | ||
| 58 | + end = next_index | ||
| 59 | + break | ||
| 60 | + return "\n".join(lines[index + 1 : end]) | ||
| 61 | + return None | ||
| 62 | + | ||
| 63 | + | ||
| 64 | +def extract_checkboxes(text, heading): | ||
| 65 | + section = extract_section(text, heading) | ||
| 66 | + if section is None: | ||
| 67 | + return None | ||
| 68 | + items = [] | ||
| 69 | + for line in section.splitlines(): | ||
| 70 | + match = CHECKBOX_RE.match(line) | ||
| 71 | + if match: | ||
| 72 | + items.append((match.group(1).lower() == "x", match.group(2).strip())) | ||
| 73 | + return items | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +def has_content(text, heading): | ||
| 77 | + section = extract_section(text, heading) | ||
| 78 | + if section is None: | ||
| 79 | + return False | ||
| 80 | + return any(line.strip() and not ANY_HEADING_RE.match(line.strip()) for line in section.splitlines()) | ||
| 81 | + | ||
| 82 | + | ||
| 83 | +def validate(template_path, body_path): | ||
| 84 | + template = read_text(template_path) | ||
| 85 | + body = read_text(body_path) | ||
| 86 | + errors = [] | ||
| 87 | + warnings = [] | ||
| 88 | + | ||
| 89 | + template_headings = extract_headings(template) | ||
| 90 | + body_headings = extract_headings(body) | ||
| 91 | + | ||
| 92 | + position = -1 | ||
| 93 | + for key in template_headings: | ||
| 94 | + try: | ||
| 95 | + position = body_headings.index(key, position + 1) | ||
| 96 | + continue | ||
| 97 | + except ValueError: | ||
| 98 | + pass | ||
| 99 | + try: | ||
| 100 | + position = body_headings.index(key) | ||
| 101 | + except ValueError: | ||
| 102 | + errors.append(f"missing or out-of-order heading: {key[0]} {key[1]}") | ||
| 103 | + continue | ||
| 104 | + errors.append(f"out-of-order heading: {key[0]} {key[1]}") | ||
| 105 | + | ||
| 106 | + template_h2 = {name for level, name in template_headings if level == "##"} | ||
| 107 | + for level, name in body_headings: | ||
| 108 | + if level == "##" and name not in template_h2: | ||
| 109 | + warnings.append(f"extra H2 heading: {name}") | ||
| 110 | + | ||
| 111 | + for section_name in ("类型", "Checklist:"): | ||
| 112 | + template_items = extract_checkboxes(template, section_name) | ||
| 113 | + body_items = extract_checkboxes(body, section_name) | ||
| 114 | + if template_items is None: | ||
| 115 | + errors.append(f"template missing section: {section_name}") | ||
| 116 | + continue | ||
| 117 | + if body_items is None: | ||
| 118 | + errors.append(f"body missing section: {section_name}") | ||
| 119 | + continue | ||
| 120 | + template_labels = [label for _, label in template_items] | ||
| 121 | + body_labels = [label for _, label in body_items] | ||
| 122 | + if body_labels != template_labels: | ||
| 123 | + errors.append(f"{section_name} items differ from template") | ||
| 124 | + if section_name == "类型": | ||
| 125 | + checked = sum(1 for value, _ in body_items if value) | ||
| 126 | + if checked == 0: | ||
| 127 | + errors.append("类型 section has no checked item") | ||
| 128 | + elif checked > 1: | ||
| 129 | + warnings.append(f"类型 section has {checked} checked items; default is one") | ||
| 130 | + | ||
| 131 | + for section_name in ("描述", "如何测试", "精度和性能验证结果", "其他信息"): | ||
| 132 | + if not has_content(body, section_name): | ||
| 133 | + errors.append(f"section has no content: {section_name}") | ||
| 134 | + | ||
| 135 | + unfenced_body = "\n".join(line for _, line in unfenced_lines(body)) | ||
| 136 | + for prompt in PROMPTS: | ||
| 137 | + if prompt in unfenced_body: | ||
| 138 | + errors.append(f"template prompt remains: {prompt}") | ||
| 139 | + | ||
| 140 | + return errors, warnings | ||
| 141 | + | ||
| 142 | + | ||
| 143 | +def main(): | ||
| 144 | + with contextlib.suppress(AttributeError): | ||
| 145 | + sys.stdout.reconfigure(encoding="utf-8", errors="replace") | ||
| 146 | + | ||
| 147 | + parser = argparse.ArgumentParser(description="Validate a PR body against the repository PR template.") | ||
| 148 | + parser.add_argument( | ||
| 149 | + "--template", | ||
| 150 | + default=".gitcode/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md", | ||
| 151 | + help="Path to the repository PR template.", | ||
| 152 | + ) | ||
| 153 | + parser.add_argument("--body", required=True, help="Path to the generated PR body.") | ||
| 154 | + args = parser.parse_args() | ||
| 155 | + | ||
| 156 | + errors, warnings = validate(args.template, args.body) | ||
| 157 | + | ||
| 158 | + print(f"template: {args.template}") | ||
| 159 | + print(f"body: {args.body}") | ||
| 160 | + for warning in warnings: | ||
| 161 | + print(f"WARN: {warning}") | ||
| 162 | + for error in errors: | ||
| 163 | + print(f"ERROR: {error}") | ||
| 164 | + | ||
| 165 | + if errors: | ||
| 166 | + print("RESULT: FAIL") | ||
| 167 | + return 1 | ||
| 168 | + print("RESULT: PASS") | ||
| 169 | + return 0 | ||
| 170 | + | ||
| 171 | + | ||
| 172 | +if __name__ == "__main__": | ||
| 173 | + sys.exit(main()) | ||
| @@ -22,4 +22,4 @@ | |||
| 22 | 22 | ||
| 23 | `requirements.txt` 声明 torchtitan 版本,`.ci/setup_torchtitan.sh` 从该版本推导对应 tag。更新版本时同步核对 patch 目标、接口和测试。 | 23 | `requirements.txt` 声明 torchtitan 版本,`.ci/setup_torchtitan.sh` 从该版本推导对应 tag。更新版本时同步核对 patch 目标、接口和测试。 |
| 24 | 24 | ||
| 25 | -完整 lint、测试分层和数值验证要求见[完整开发指南](.agents/AGENTS.md)及[测试规则](tests/AGENTS.md)。`gitcode-pr` 与 `gitcode-pipeline` 是已安装相应 skill 时的可选工作流;仓库本身不提供这些 skill。 | 25 | +完整 lint、测试分层和数值验证要求见[完整开发指南](.agents/AGENTS.md)及[测试规则](tests/AGENTS.md)。创建或更新 GitCode PR 使用 `.agents/skills/submit-pr/`;如果同时安装 `gitcode-pr`,以 `.agents/skills/submit-pr/` 的模板和流程为准。`gitcode-pipeline` 是已安装时的可选流水线 workflow。 |
| @@ -0,0 +1,147 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| 2 | +# | ||
| 3 | +# This source code is licensed under the BSD-style license found in the | ||
| 4 | +# LICENSE file in the root directory of this source tree. | ||
| 5 | + | ||
| 6 | +"""CPU tests for the submit-pr PR body template validator.""" | ||
| 7 | + | ||
| 8 | +import importlib.util | ||
| 9 | +import sys | ||
| 10 | +from pathlib import Path | ||
| 11 | + | ||
| 12 | +import pytest | ||
| 13 | + | ||
| 14 | +pytestmark = pytest.mark.tooling | ||
| 15 | + | ||
| 16 | +ROOT = Path(__file__).resolve().parents[3] | ||
| 17 | +VALIDATOR = ROOT / ".agents/skills/submit-pr/scripts/validate_pr_body.py" | ||
| 18 | +TEMPLATE = ROOT / ".gitcode/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md" | ||
| 19 | + | ||
| 20 | +_SPEC = importlib.util.spec_from_file_location("submit_pr_validate_pr_body", VALIDATOR) | ||
| 21 | +validate_pr_body = importlib.util.module_from_spec(_SPEC) | ||
| 22 | +_SPEC.loader.exec_module(validate_pr_body) | ||
| 23 | + | ||
| 24 | +VALID_BODY = """## 描述 | ||
| 25 | +说明改动。 | ||
| 26 | + | ||
| 27 | +## 类型 | ||
| 28 | +- [ ] Bug 修复 | ||
| 29 | +- [x] 新功能 | ||
| 30 | +- [ ] 重构(即不是新增功能,也不是修改bug的代码变动) | ||
| 31 | +- [ ] 构建过程或辅助工具的变动 | ||
| 32 | +- [ ] 文档内容更新 | ||
| 33 | + | ||
| 34 | +## Checklist: | ||
| 35 | +- [x] 我的代码遵循这个项目的代码风格 | ||
| 36 | +- [x] 我已经自己测试过我的代码 | ||
| 37 | +- [ ] 我已经更新了相应的文档 | ||
| 38 | +- [x] 我已经在标题中正确使用了类型标签(例如:`feat`, `fix`, `refactor`, `docs`, `test`) | ||
| 39 | + | ||
| 40 | +## 如何测试 | ||
| 41 | +执行了测试。 | ||
| 42 | + | ||
| 43 | +### 精度和性能验证结果 | ||
| 44 | +| 项目 | 结果 | | ||
| 45 | +| --- | --- | | ||
| 46 | +| loss | 通过 | | ||
| 47 | + | ||
| 48 | +## 其他信息 | ||
| 49 | +无。 | ||
| 50 | +""" | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def _write_body(tmp_path, text): | ||
| 54 | + path = tmp_path / "body.md" | ||
| 55 | + path.write_text(text, encoding="utf-8") | ||
| 56 | + return path | ||
| 57 | + | ||
| 58 | + | ||
| 59 | +def test_accepts_valid_body(tmp_path): | ||
| 60 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, VALID_BODY)) | ||
| 61 | + assert errors == [] | ||
| 62 | + | ||
| 63 | + | ||
| 64 | +def test_rejects_missing_heading(tmp_path): | ||
| 65 | + body = VALID_BODY.replace("## 其他信息\n无。\n", "") | ||
| 66 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 67 | + assert any("missing or out-of-order heading" in error for error in errors) | ||
| 68 | + | ||
| 69 | + | ||
| 70 | +def test_rejects_changed_checkbox_item(tmp_path): | ||
| 71 | + body = VALID_BODY.replace("- [ ] Bug 修复", "- [ ] Bug fix") | ||
| 72 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 73 | + assert "类型 items differ from template" in errors | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +def test_rejects_template_prompt(tmp_path): | ||
| 77 | + body = VALID_BODY.replace("执行了测试。", "执行了测试。\n简要描述测试方案。") | ||
| 78 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 79 | + assert any("template prompt remains" in error for error in errors) | ||
| 80 | + | ||
| 81 | + | ||
| 82 | +def test_rejects_empty_precision_section(tmp_path): | ||
| 83 | + body = VALID_BODY.replace("| 项目 | 结果 |\n| --- | --- |\n| loss | 通过 |\n", "") | ||
| 84 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 85 | + assert "section has no content: 精度和性能验证结果" in errors | ||
| 86 | + | ||
| 87 | + | ||
| 88 | +def test_rejects_changed_heading_level(tmp_path): | ||
| 89 | + body = VALID_BODY.replace("## 描述", "### 描述") | ||
| 90 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 91 | + assert any("missing or out-of-order heading" in error for error in errors) | ||
| 92 | + | ||
| 93 | + | ||
| 94 | +def test_rejects_body_inside_code_fence(tmp_path): | ||
| 95 | + body = "```markdown\n" + VALID_BODY + "```\n" | ||
| 96 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 97 | + assert any("missing or out-of-order heading" in error for error in errors) | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +def test_accepts_checkbox_only_section(tmp_path): | ||
| 101 | + body = VALID_BODY.replace("执行了测试。", "- [x] 运行 pytest\n- [x] 运行 lint") | ||
| 102 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 103 | + assert errors == [] | ||
| 104 | + | ||
| 105 | + | ||
| 106 | +def test_accepts_content_under_subheading(tmp_path): | ||
| 107 | + body = VALID_BODY.replace("执行了测试。", "### 单元测试\n运行 pytest。") | ||
| 108 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 109 | + assert errors == [] | ||
| 110 | + | ||
| 111 | + | ||
| 112 | +def test_ignores_template_prompt_inside_code_fence(tmp_path): | ||
| 113 | + body = VALID_BODY.replace("执行了测试。", "执行了测试。\n\n```\n简要描述测试方案。\n```") | ||
| 114 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 115 | + assert errors == [] | ||
| 116 | + | ||
| 117 | + | ||
| 118 | +def test_template_prompts_are_recognized(): | ||
| 119 | + template = TEMPLATE.read_text(encoding="utf-8") | ||
| 120 | + prompt_lines = [ | ||
| 121 | + line.strip() | ||
| 122 | + for _, line in validate_pr_body.unfenced_lines(template) | ||
| 123 | + if line.strip() and not line.strip().startswith("#") and not validate_pr_body.CHECKBOX_RE.match(line) | ||
| 124 | + ] | ||
| 125 | + assert prompt_lines | ||
| 126 | + for line in prompt_lines: | ||
| 127 | + assert any(prompt in line for prompt in validate_pr_body.PROMPTS), line | ||
| 128 | + | ||
| 129 | + | ||
| 130 | +def test_reports_out_of_order_heading_once(tmp_path): | ||
| 131 | + body = VALID_BODY.replace("## 描述\n说明改动。\n\n", "", 1) | ||
| 132 | + body = body.replace("## 如何测试", "## 描述\n说明改动。\n\n## 如何测试", 1) | ||
| 133 | + errors, _ = validate_pr_body.validate(TEMPLATE, _write_body(tmp_path, body)) | ||
| 134 | + heading_errors = [error for error in errors if "heading" in error] | ||
| 135 | + assert heading_errors == ["out-of-order heading: ## 类型"] | ||
| 136 | + | ||
| 137 | + | ||
| 138 | +def test_main_exit_codes(tmp_path, monkeypatch): | ||
| 139 | + valid = _write_body(tmp_path, VALID_BODY) | ||
| 140 | + invalid = tmp_path / "invalid.md" | ||
| 141 | + invalid.write_text("## 描述\n说明改动。\n", encoding="utf-8") | ||
| 142 | + | ||
| 143 | + monkeypatch.setattr(sys, "argv", ["validate_pr_body.py", "--template", str(TEMPLATE), "--body", str(valid)]) | ||
| 144 | + assert validate_pr_body.main() == 0 | ||
| 145 | + | ||
| 146 | + monkeypatch.setattr(sys, "argv", ["validate_pr_body.py", "--template", str(TEMPLATE), "--body", str(invalid)]) | ||
| 147 | + assert validate_pr_body.main() == 1 | ||
🟡 Medium Priority
变更行:
extract_section()第 49-50 行end = entries[position + 1][0] if position + 1 < len(entries) else len(lines)然后"\n".join(lines[index + 1 : end])。 行为/契约:entries由HEADING_RE(#{2,3})产生,包含 2、3 级标题;因此 section 的结束位置永远是“下一个 2 级或 3 级标题”,而不是“下一个同级或更高级标题”。这与validate()里has_content的语义(章节内容是否为空)不一致。 失败模式:本文档本身的结构就是把### 精度和性能验证结果嵌在## 如何测试之下(见测试用例 VALID_BODY)。当作者在章节下先写子标题、内容放在子标题里时,例如## 如何测试/### 单元测试/运行 pytest,extract_section("如何测试")只返回 H3 之前的空行,has_content返回 False,输出ERROR: section has no content: 如何测试与RESULT: FAIL——正文其实有内容,属于稳定的假失败;同理## 描述下写### 背景段、或子标题后的收尾文字都取不到。建议:让 extract_section 按标题层级确定 section 边界:结束于第一个 level 不高于当前标题的标题,而不是任意下一个 H2/H3;这样嵌套在子标题下的内容也会被计入章节内容。