From 978ccfdd3ed97d5236caecf604bfa336134dc718 Mon Sep 17 00:00:00 2001 From: peppa-bot Date: Fri, 27 Nov 2026 10:41:55 +0500 Subject: [PATCH] =?UTF-8?q?PEP-313:=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C?= =?UTF-8?q?=D1=82=D0=B0=D1=82=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=B8=D1=88=D0=B5=D1=82=D1=81=D1=8F=20=D0=BE?= =?UTF-8?q?=D0=B4=D0=BD=D0=BE=D0=B9=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B5=D0=B9=20=E2=80=94=20=D0=BE=D1=82=D1=87?= =?UTF-8?q?=D1=91=D1=82=20=D1=88=D0=B0=D0=B3=D0=B0=20=D0=B8=20=D0=BE=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=81=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=BD=D0=B5=20=D0=BB=D0=BE=D0=BC=D0=B0=D1=8E=D1=82=20?= =?UTF-8?q?=D0=B4=D1=80=D1=83=D0=B3=20=D0=B4=D1=80=D1=83=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 27.11.2026 в 02:32 веб ответил 500 на отчёт шага проверки: «UNIQUE constraint failed: check_runs.project_id, check_runs.sha, check_runs.check». Отчёт шага (веб) и опрос статусов (вачдог) писали одну проверку одного коммита одновременно: чтение шло отдельно от записи, оба не видели строки, вторая вставка падала, отчёт с журналом терялся. - core/ci.ts: `upsertCheckRun` — чтение и запись в одной синхронной транзакции (блокировка записи берётся первой); второй писатель видит строку первого, журнал отчёта сохраняется, «изменилось» — один раз. - Тест project-tabs.test.ts: два одновременных отчёта — одна запись; до исправления падал той же ошибкой, что на проде. Co-Authored-By: Claude Fable 5.2 --- packages/core/src/ci.ts | 45 +++++++++++++--------- packages/testkit/test/project-tabs.test.ts | 26 +++++++++++++ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/packages/core/src/ci.ts b/packages/core/src/ci.ts index 2f42411a..a0ee3d87 100644 --- a/packages/core/src/ci.ts +++ b/packages/core/src/ci.ts @@ -72,25 +72,32 @@ export function summarizeChecks(configured: string[], runs: RunLike[], sha: stri /** Stores a CI result (one row per project/commit/check). `changed` — the state is new. */ export async function upsertCheckRun(db: Db, project: Project, taskId: number | null, r: CheckReport): Promise<{ run: CheckRun; changed: boolean }> { - const [prev] = await db - .select() - .from(checkRuns) - .where(and(eq(checkRuns.projectId, project.id), eq(checkRuns.sha, r.sha), eq(checkRuns.check, r.check))); - const values = { - taskId: taskId ?? prev?.taskId ?? null, - branch: r.branch ?? prev?.branch ?? null, - prNumber: r.prNumber ?? prev?.prNumber ?? null, - state: r.state, - runUrl: r.runUrl ?? prev?.runUrl ?? null, - // A poll carries no log: keep the one the CI step sent. - logTail: r.logTail ?? prev?.logTail ?? null, - durationSec: r.durationSec ?? prev?.durationSec ?? null, - source: r.source ?? "report", - }; - const [run] = prev - ? await db.update(checkRuns).set(values).where(eq(checkRuns.id, prev.id)).returning() - : await db.insert(checkRuns).values({ projectId: project.id, sha: r.sha, check: r.check, ...values }).returning(); - return { run: run!, changed: prev?.state !== r.state }; + // PEP-313: one transaction (the write lock is taken first). The CI step's report (web) and the + // watchdog's poll write the same check of the same commit at the same moment: read apart from the + // write, both saw no row, the second insert failed on the unique index, and the report with the + // log was lost with a 500 (27.11.2026). In turn each sees what the other stored. + return db.transaction((tx) => { + const prev = tx + .select() + .from(checkRuns) + .where(and(eq(checkRuns.projectId, project.id), eq(checkRuns.sha, r.sha), eq(checkRuns.check, r.check))) + .get(); + const values = { + taskId: taskId ?? prev?.taskId ?? null, + branch: r.branch ?? prev?.branch ?? null, + prNumber: r.prNumber ?? prev?.prNumber ?? null, + state: r.state, + runUrl: r.runUrl ?? prev?.runUrl ?? null, + // A poll carries no log: keep the one the CI step sent. + logTail: r.logTail ?? prev?.logTail ?? null, + durationSec: r.durationSec ?? prev?.durationSec ?? null, + source: r.source ?? "report", + }; + const run = prev + ? tx.update(checkRuns).set(values).where(eq(checkRuns.id, prev.id)).returning().get() + : tx.insert(checkRuns).values({ projectId: project.id, sha: r.sha, check: r.check, ...values }).returning().get(); + return { run: run!, changed: prev?.state !== r.state }; + }); } /** PR number of a task's pr_url when it points at the project's repo. */ diff --git a/packages/testkit/test/project-tabs.test.ts b/packages/testkit/test/project-tabs.test.ts index 1c9ca2bb..ec012c33 100644 --- a/packages/testkit/test/project-tabs.test.ts +++ b/packages/testkit/test/project-tabs.test.ts @@ -85,6 +85,32 @@ describe("Вкладка «Код»", () => { }); }); +// PEP-313 (27.11.2026): the CI step's report and the watchdog's poll wrote the same check at once — +// «UNIQUE constraint failed: check_runs.project_id, check_runs.sha, check_runs.check», the report was lost. +describe("Результат проверки пишут двое сразу", () => { + test("Отчёт шага и опрос статусов одновременно — одна запись, журнал отчёта цел, «изменилось» — один раз", async () => { + const p = await tabsProject("MTRACE", { "README.md": "# R" }, { ".peppa/project.json": JSON.stringify({ key: "MTRACE", checks: { test: "pnpm test" } }) }); + const ref = await core.createTask(db, { projectId: p.id, title: "Гонка отчётов" }); + const sha = "c".repeat(40); + const [a, b] = await Promise.all([ + core.upsertCheckRun(db, p, ref.task.id, { sha, check: "test", state: "failure", logTail: "✖ упал один тест", source: "report" }), + core.upsertCheckRun(db, p, null, { sha, check: "test", state: "failure", source: "poll" }), + ]); + assert.equal(a.run.id, b.run.id, "one row for the commit and the check"); + assert.deepEqual([a.changed, b.changed], [true, false], "the red check is news once"); + assert.equal(b.run.logTail, "✖ упал один тест", "the poll keeps the log the CI step sent"); + assert.equal(b.run.taskId, ref.task.id); + // The other order: the poll first, the report with the log second. + const sha2 = "d".repeat(40); + const [c, d] = await Promise.all([ + core.upsertCheckRun(db, p, null, { sha: sha2, check: "test", state: "success", source: "poll" }), + core.upsertCheckRun(db, p, ref.task.id, { sha: sha2, check: "test", state: "success", logTail: "ok", source: "report" }), + ]); + assert.equal(c.run.id, d.run.id); + assert.equal(d.run.logTail, "ok"); + }); +}); + describe("Вкладка «PR»", () => { test("Scenario: PR стори — ключ стори и «Проверки: 2 из 2 пройдены»; PR без стори — без проверок", async () => { const p = await tabsProject("MTPR", { "README.md": "# PR" }, { ".peppa/project.json": JSON.stringify({ key: "MTPR", checks: { test: "pnpm test", build: "pnpm build" } }) });