PEP-313: результат проверки пишется одной транзакцией — отчёт шага и опрос статусов не ломают друг друга
All checks were successful
All checks were successful
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 <noreply@anthropic.com>
This commit is contained in:
parent
b664e6ad7c
commit
978ccfdd3e
@ -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. */
|
/** 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 }> {
|
export async function upsertCheckRun(db: Db, project: Project, taskId: number | null, r: CheckReport): Promise<{ run: CheckRun; changed: boolean }> {
|
||||||
const [prev] = await db
|
// PEP-313: one transaction (the write lock is taken first). The CI step's report (web) and the
|
||||||
.select()
|
// watchdog's poll write the same check of the same commit at the same moment: read apart from the
|
||||||
.from(checkRuns)
|
// write, both saw no row, the second insert failed on the unique index, and the report with the
|
||||||
.where(and(eq(checkRuns.projectId, project.id), eq(checkRuns.sha, r.sha), eq(checkRuns.check, r.check)));
|
// log was lost with a 500 (27.11.2026). In turn each sees what the other stored.
|
||||||
const values = {
|
return db.transaction((tx) => {
|
||||||
taskId: taskId ?? prev?.taskId ?? null,
|
const prev = tx
|
||||||
branch: r.branch ?? prev?.branch ?? null,
|
.select()
|
||||||
prNumber: r.prNumber ?? prev?.prNumber ?? null,
|
.from(checkRuns)
|
||||||
state: r.state,
|
.where(and(eq(checkRuns.projectId, project.id), eq(checkRuns.sha, r.sha), eq(checkRuns.check, r.check)))
|
||||||
runUrl: r.runUrl ?? prev?.runUrl ?? null,
|
.get();
|
||||||
// A poll carries no log: keep the one the CI step sent.
|
const values = {
|
||||||
logTail: r.logTail ?? prev?.logTail ?? null,
|
taskId: taskId ?? prev?.taskId ?? null,
|
||||||
durationSec: r.durationSec ?? prev?.durationSec ?? null,
|
branch: r.branch ?? prev?.branch ?? null,
|
||||||
source: r.source ?? "report",
|
prNumber: r.prNumber ?? prev?.prNumber ?? null,
|
||||||
};
|
state: r.state,
|
||||||
const [run] = prev
|
runUrl: r.runUrl ?? prev?.runUrl ?? null,
|
||||||
? await db.update(checkRuns).set(values).where(eq(checkRuns.id, prev.id)).returning()
|
// A poll carries no log: keep the one the CI step sent.
|
||||||
: await db.insert(checkRuns).values({ projectId: project.id, sha: r.sha, check: r.check, ...values }).returning();
|
logTail: r.logTail ?? prev?.logTail ?? null,
|
||||||
return { run: run!, changed: prev?.state !== r.state };
|
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. */
|
/** PR number of a task's pr_url when it points at the project's repo. */
|
||||||
|
|||||||
@ -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»", () => {
|
describe("Вкладка «PR»", () => {
|
||||||
test("Scenario: PR стори — ключ стори и «Проверки: 2 из 2 пройдены»; PR без стори — без проверок", async () => {
|
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" } }) });
|
const p = await tabsProject("MTPR", { "README.md": "# PR" }, { ".peppa/project.json": JSON.stringify({ key: "MTPR", checks: { test: "pnpm test", build: "pnpm build" } }) });
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user