diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index afa089e..c5e7642 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -4,6 +4,10 @@ - 实现归属仓库与关联的跨仓提交或运行时版本(纯本仓改动可填无) - 旧版或缺失运行时下的兼容与降级表现(不涉及运行时可填不适用) +## 模型与 Agent 申报 + + + ## 验证记录 - 已通过的测试命令与实测运行时版本 diff --git a/.github/scripts/pr-model.mjs b/.github/scripts/pr-model.mjs new file mode 100644 index 0000000..35f917d --- /dev/null +++ b/.github/scripts/pr-model.mjs @@ -0,0 +1,58 @@ +import { readFile } from "node:fs/promises"; +import { pathToFileURL } from "node:url"; + +export function missingDeclarations(body) { + const missing = new Set(["model", "agent"]); + if (typeof body !== "string") return [...missing]; + let fence = null; + for (const line of body.replace(/|$)/g, "").split(/\r?\n/)) { + const delimiter = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line); + if (fence) { + if (delimiter && delimiter[1][0] === fence[0] && delimiter[1].length >= fence.length && !delimiter[2].trim()) fence = null; + } else if (delimiter) { + fence = delimiter[1]; + } else if (/^ {0,3}model:[ \t]+[\p{L}\p{N}_.-]+\/[\p{L}\p{N}_.-]+[ \t]*$/u.test(line)) { + missing.delete("model"); + } else if (/^ {0,3}agent:[ \t]+[\p{L}\p{N}][\p{L}\p{N}_.+() /-]*$/u.test(line.trimEnd())) { + missing.delete("agent"); + } + if (missing.size === 0) break; + } + return [...missing]; +} + +export async function checkPRModel(event, { apiUrl, repository, token, request = fetch }) { + if (event.action !== "opened" || event.pull_request?.base?.ref !== "main") return "skipped"; + const number = event.pull_request.number; + if (!Number.isSafeInteger(number) || number < 1) throw new Error("Invalid pull request number"); + const url = `${apiUrl}/repos/${repository}/pulls/${number}`; + const headers = { + Authorization: `Bearer ${token}`, + Accept: "application/vnd.github+json", + "Content-Type": "application/json", + }; + const read = await request(url, { headers, redirect: "error" }); + if (!read.ok) throw new Error(`Could not read PR #${number} (HTTP ${read.status})`); + const current = await read.json(); + if (current.state !== "open" || current.base?.ref !== "main") return "skipped"; + const missing = missingDeclarations(current.body); + if (!missing.length) return "accepted"; + const close = await request(url, { + method: "PATCH", headers, redirect: "error", body: JSON.stringify({ state: "closed" }), + }); + if (!close.ok) throw new Error(`Could not close PR #${number} (HTTP ${close.status})`); + return { status: "closed", missing }; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + const event = JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH, "utf8")); + const result = await checkPRModel(event, { + apiUrl: process.env.GITHUB_API_URL, + repository: process.env.GITHUB_REPOSITORY, + token: process.env.GITHUB_TOKEN, + }); + if (result.status === "closed") { + console.error(`::error::PR closed because its description is missing ${result.missing.join(" and ")}. Declare the actual model and agent used (format examples: model: deepseek/deepseek-R1 and agent: WorkBuddy).`); + process.exitCode = 1; + } +} diff --git a/.github/scripts/pr-model.test.mjs b/.github/scripts/pr-model.test.mjs new file mode 100644 index 0000000..b086f4e --- /dev/null +++ b/.github/scripts/pr-model.test.mjs @@ -0,0 +1,47 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { checkPRModel, missingDeclarations } from "./pr-model.mjs"; + +const declaration = "model: deepseek/deepseek-R1"; +const opened = { action: "opened", pull_request: { number: 42, base: { ref: "main" } } }; +const options = { apiUrl: "https://api.example.invalid", repository: "owner/repo", token: "test" }; + +test("requires visible model and agent declarations", () => { + for (const agent of ["WorkBuddy", "豆包", "OpenAI Codex"]) { + assert.deepEqual(missingDeclarations(`${declaration}\r\nagent: ${agent}`), []); + } + assert.deepEqual(missingDeclarations(declaration), ["agent"]); + assert.deepEqual(missingDeclarations("agent: 豆包"), ["model"]); + assert.deepEqual(missingDeclarations(`${declaration}\nagent: `), ["agent"]); + assert.deepEqual(missingDeclarations(`${declaration}\n`), ["agent"]); + assert.deepEqual(missingDeclarations("```\n" + declaration + "\nagent: 豆包\n```"), ["model", "agent"]); + assert.deepEqual(missingDeclarations(null), ["model", "agent"]); +}); + +test("closes a new PR when either declaration is missing from its current description", async () => { + const cases = [ + ["", ["model", "agent"]], + [declaration, ["agent"]], + ["agent: 豆包", ["model"]], + [`${declaration}\nagent: WorkBuddy`, []], + ]; + for (const [body, missing] of cases) { + const calls = []; + const request = async (url, init) => { + calls.push([url, init.method, init.body]); + return { ok: true, json: async () => ({ state: "open", base: { ref: "main" }, body }) }; + }; + assert.deepEqual(await checkPRModel(opened, { ...options, request }), missing.length ? { status: "closed", missing } : "accepted"); + assert.equal(calls.length, missing.length ? 2 : 1); + if (missing.length) assert.deepEqual(calls[1], ["https://api.example.invalid/repos/owner/repo/pulls/42", "PATCH", JSON.stringify({ state: "closed" })]); + } +}); + +test("skips existing PR updates and other base branches", async () => { + const request = () => assert.fail("The API must not be called"); + for (const action of ["edited", "reopened", "synchronize", "ready_for_review"]) { + assert.equal(await checkPRModel({ ...opened, action }, { ...options, request }), "skipped"); + } + const otherBase = { ...opened, pull_request: { ...opened.pull_request, base: { ref: "release" } } }; + assert.equal(await checkPRModel(otherBase, { ...options, request }), "skipped"); +}); diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66da195..0881449 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,6 +52,9 @@ jobs: cache: pnpm cache-dependency-path: frontend/pnpm-lock.yaml + - name: Test PR model and agent declarations + run: node --test .github/scripts/pr-model.test.mjs + - name: Verify CHANGELOG.md matches current version shell: pwsh run: | diff --git a/.github/workflows/pr-model.yml b/.github/workflows/pr-model.yml new file mode 100644 index 0000000..8f2008e --- /dev/null +++ b/.github/workflows/pr-model.yml @@ -0,0 +1,27 @@ +name: PR Model and Agent Declaration + +on: + pull_request_target: + branches: [main] + types: [opened] + +permissions: {} + +jobs: + model-declaration: + runs-on: ubuntu-latest + timeout-minutes: 2 + permissions: + contents: read + pull-requests: write + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + ref: ${{ github.workflow_sha }} + persist-credentials: false + sparse-checkout-cone-mode: false + sparse-checkout: /.github/scripts/pr-model.mjs + - name: Check the model and agent declarations + env: + GITHUB_TOKEN: ${{ github.token }} + run: node .github/scripts/pr-model.mjs diff --git a/AGENTS.md b/AGENTS.md index 8986b6a..27f2fd8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -40,6 +40,12 @@ Reims-Go 发布的便携运行时基于团队独立维护的 `reims-go/reims-vgp 7. 捕获异常后必须执行上报错误、记录诊断告警或回退状态等明确处理,前端测试会自动拦截空异常捕获块。 8. `docs/` 只存放当前生效且删掉会影响编码的文档,采用主谓清晰的简单句说明现状,不写本机路径、排期历史、内部变量函数名或「粗体标题加冒号」的口号句式。 +## PR 模型与 Agent 申报 + +提交新 PR 时,请在描述中逐行如实申报参与代码编写的模型和 Agent。模型使用 `model: 提供方/模型名称`,例如 `model: deepseek/deepseek-R1`。Agent 使用 `agent: 名称`,例如 `agent: WorkBuddy` 或 `agent: 豆包`。请填写实际使用的名称。 + +缺少模型或 Agent 申报的 PR 直接关闭。纯人工编写的提交不予合并。此规则仅适用于生效后新建的 PR,已创建的 PR 继续按原规则处理。 + ## 版本变更与开源合规 面向用户或集成方的变化随改动写入 `changes/.json`,准备正式版时编入 [`CHANGELOG.md`](CHANGELOG.md),格式与命令见 [`docs/changelog.md`](docs/changelog.md)。