diff --git a/.github/scripts/issue-automation.test.mjs b/.github/scripts/issue-automation.test.mjs index 744c2f0..65ba1b1 100644 --- a/.github/scripts/issue-automation.test.mjs +++ b/.github/scripts/issue-automation.test.mjs @@ -52,12 +52,17 @@ test("the comment guides the reporter with fixed text only", async () => { assert.equal(result.status, "commented"); assert.match(result.body, /请补充以下信息\*\*\n\n- Reims-Go、运行时和 macOS 的版本\n\n/); assert.match(result.body, /疑似重复[^\n]*\n\n- #3\n/); - assert.match(result.body, /路线图中\*\*\n\n- #75\n/); - assert.match(result.body, /steelbrain\/reims-vgpu/); - assert.doesNotMatch(result.body, /INJECTED|everyone|#999/); + assert.match(result.body, /路线图中\*\*\n\n- #75$/); + assert.doesNotMatch(result.body, /INJECTED|everyone|#999|steelbrain/); assert.deepEqual(writes.map(([method, route]) => method + " " + route), ["POST /issues/10/comments", "POST /issues/10/labels"]); }); +test("only complete bug reports are pointed to the upstream engine", async () => { + const run = (missing) => triageIssue(10, { ...fakeGitHub(issue()), publish: false, ask: async () => ({ kind: "bug", missing, target: "engine" }) }); + assert.match((await run([])).body, /steelbrain\/reims-vgpu/); + assert.doesNotMatch((await run(["logs"])).body, /steelbrain/); +}); + test("feature requests that fit a plugin are pointed to the plugin documentation", async () => { const target = issue({ title: "[Feature]: new theme", body: "Please add a theme" }); const result = await triageIssue(10, { ...fakeGitHub(target), publish: false, ask: async () => ({ kind: "feature", target: "plugin" }) }); @@ -146,3 +151,14 @@ test("diagnosis history covers every page of bot issues", async () => { }; assert.equal((await previousDiagnoses(github)).length, 130); }); + +test("rate-limited model requests are retried before failing", async () => { + const env = { PR_REVIEW_LLM_KEY: "key", PR_REVIEW_LLM_BASE_URL: "https://example.com/api/anthropic", PR_REVIEW_LLM_MODEL: "glm-5.4" }; + const waits = []; + const wait = async (ms) => waits.push(ms); + const replies = [{ ok: false, status: 429 }, { ok: false, status: 503 }, { ok: true, status: 200, json: async () => ({ content: [{ type: "text", text: "{}" }] }) }]; + assert.deepEqual(await askModel("rules", {}, { env, wait, request: async () => replies.shift() }), {}); + assert.deepEqual(waits, [20_000, 40_000]); + await assert.rejects(askModel("rules", {}, { env, wait, request: async () => ({ ok: false, status: 429 }) }), /HTTP 429/); + await assert.rejects(askModel("rules", {}, { env, wait: () => assert.fail("retried"), request: async () => ({ ok: false, status: 401 }) }), /HTTP 401/); +}); diff --git a/.github/scripts/issue-triage.mjs b/.github/scripts/issue-triage.mjs index 4782214..cef7ddd 100644 --- a/.github/scripts/issue-triage.mjs +++ b/.github/scripts/issue-triage.mjs @@ -87,11 +87,11 @@ export function issueLanguage({ title, body }) { return /[㐀-鿿]/.test(prose + String(title ?? "")) ? "zh" : "en"; } -export async function askModel(system, data, { env = process.env, request = fetch } = {}) { +export async function askModel(system, data, { env = process.env, request = fetch, wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) } = {}) { const protocol = env.PR_REVIEW_LLM_PROTOCOL?.trim() || "anthropic"; if (protocol !== "anthropic") throw new Error("Issue automation supports only the anthropic protocol"); const key = required("PR_REVIEW_LLM_KEY", env); - const response = await request(normalizeLLMBaseURL(required("PR_REVIEW_LLM_BASE_URL", env), protocol), { + const send = () => request(normalizeLLMBaseURL(required("PR_REVIEW_LLM_BASE_URL", env), protocol), { method: "POST", headers: { Authorization: "Bearer " + key, "x-api-key": key, "anthropic-version": "2023-06-01", "Content-Type": "application/json" }, body: JSON.stringify({ @@ -101,6 +101,13 @@ export async function askModel(system, data, { env = process.env, request = fetc redirect: "error", signal: AbortSignal.timeout(120_000), }); + let response = await send(); + // The plan's rate limit is shared with PR reviews, so wait for running requests to finish. + for (const seconds of [20, 40, 80, 160]) { + if (response.status !== 429 && !(response.status >= 500)) break; + await wait(seconds * 1000); + response = await send(); + } if (!response.ok) throw new Error("The model returned HTTP " + response.status); const reply = await response.json(); const text = (Array.isArray(reply.content) ? reply.content : []).filter((part) => part.type === "text").map((part) => part.text).join(""); @@ -138,7 +145,8 @@ export function triageComment(triage, language) { if (triage.missing.length) section(text.missing, triage.missing.map((field) => "- " + text.fields[field]).join("\n")); if (duplicates.length) section(text.duplicates, links(duplicates)); if (planned.length) section(text.planned, links(planned)); - if (triage.target === "engine") section(text.target, triage.feature ? text.engineFeature : text.engineBug); + // Without enough details, a guess would send reporters to the upstream tracker for nothing. + if (triage.target === "engine" && (triage.feature || !triage.missing.length)) section(text.target, triage.feature ? text.engineFeature : text.engineBug); if (triage.target === "plugin" && triage.feature) section(text.target, text.plugin); return sections.length ? [MARKER, text.intro, ...sections].join("\n\n") : null; } diff --git a/.github/workflows/issue-triage.yml b/.github/workflows/issue-triage.yml index 0db1f60..afaff28 100644 --- a/.github/workflows/issue-triage.yml +++ b/.github/workflows/issue-triage.yml @@ -24,7 +24,7 @@ jobs: triage: if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 20 permissions: contents: read issues: write