From 9bd3f2aa543f77305d417e3f9e399d0d417cdf81 Mon Sep 17 00:00:00 2001 From: yuzhou <1991039819@qq.com> Date: Mon, 30 Nov 2026 11:09:02 +0800 Subject: [PATCH] Add review_round on jobs and compute it in insertJob Count prior done/failed jobs on the same pull request and store prior_rounds + 1 atomically so aborted superseded or cancelled runs do not inflate the round. First jobs start at 1. Closes #212 Signed-off-by: yuzhou <1991039819@qq.com> --- packages/core/test/in-memory.ts | 1 + packages/db/migrations/005_review_round.sql | 4 + packages/db/src/jobs-mapping.ts | 2 + packages/db/src/jobs.ts | 14 ++- packages/schema/src/schema.ts | 1 + playwright/fixtures.ts | 2 + test/db/review-round.spec.ts | 97 +++++++++++++++++++++ 7 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 packages/db/migrations/005_review_round.sql create mode 100644 test/db/review-round.spec.ts diff --git a/packages/core/test/in-memory.ts b/packages/core/test/in-memory.ts index e4f6c852..dc8794a4 100644 --- a/packages/core/test/in-memory.ts +++ b/packages/core/test/in-memory.ts @@ -47,6 +47,7 @@ export function makeJob( createdAt: ISO, updatedAt: ISO, startedAt: null, finishedAt: null, errorMessage: null, steps: [], checkRunId: null, configSnapshot: null, ...overrides, + reviewRound: overrides.reviewRound ?? 1, }; } diff --git a/packages/db/migrations/005_review_round.sql b/packages/db/migrations/005_review_round.sql new file mode 100644 index 00000000..05556a5d --- /dev/null +++ b/packages/db/migrations/005_review_round.sql @@ -0,0 +1,4 @@ +-- Per-PR review iteration number, counted from prior terminal review attempts +-- on the same (repository_id, pr_number). Aborted runs (superseded / cancelled) +-- do not consume a round; insertJob sets this atomically as prior_rounds + 1. +ALTER TABLE jobs ADD COLUMN IF NOT EXISTS review_round INTEGER NOT NULL DEFAULT 1; diff --git a/packages/db/src/jobs-mapping.ts b/packages/db/src/jobs-mapping.ts index 4ae2acd7..8d836415 100644 --- a/packages/db/src/jobs-mapping.ts +++ b/packages/db/src/jobs-mapping.ts @@ -55,6 +55,7 @@ export type JobRow = { summary_markdown: string | null; review_id: number | null; retry_of_job_id: string | null; + review_round?: number | null; summary_model: string | null; overall_confidence_score: number | null; steps: JobStep[] | string | null; @@ -150,6 +151,7 @@ export function mapJob(row: JobRow) { ) : null, retryOfJobId: row.retry_of_job_id, + reviewRound: row.review_round ?? 1, workflowInstanceId: row.workflow_instance_id, }); } diff --git a/packages/db/src/jobs.ts b/packages/db/src/jobs.ts index e4030b17..d3bc0175 100644 --- a/packages/db/src/jobs.ts +++ b/packages/db/src/jobs.ts @@ -105,9 +105,19 @@ export async function insertJob( config_snapshot, head_ref, base_ref, - retry_of_job_id + retry_of_job_id, + review_round + ) + VALUES ( + $1, $2, $3, $4, $5, $6, $7, 'queued', $8::text::jsonb, $9, $10, $11::uuid, + ( + SELECT COUNT(*)::int + 1 + FROM jobs + WHERE repository_id = $1 + AND pr_number = $2 + AND status IN ('done', 'failed') + ) ) - VALUES ($1, $2, $3, $4, $5, $6, $7, 'queued', $8::text::jsonb, $9, $10, $11::uuid) RETURNING * ) SELECT i.*, r.owner, r.repo, r.installation_id diff --git a/packages/schema/src/schema.ts b/packages/schema/src/schema.ts index 8f1856c9..1695d2dc 100644 --- a/packages/schema/src/schema.ts +++ b/packages/schema/src/schema.ts @@ -256,6 +256,7 @@ export const jobSummarySchema = z.object({ checkRunId: coerceNumberSchema.nullable().optional(), configSnapshot: repoConfigSchema.nullable().optional(), retryOfJobId: z.uuid().nullable().optional(), + reviewRound: z.number().int().positive().default(1), }); export const jobsQuerySchema = z.object({ diff --git a/playwright/fixtures.ts b/playwright/fixtures.ts index f906de84..5dc9b321 100644 --- a/playwright/fixtures.ts +++ b/playwright/fixtures.ts @@ -44,6 +44,7 @@ export function makeJobSummary(index: number): JobSummary { checkRunId: null, configSnapshot: null, retryOfJobId: null, + reviewRound: 1, }; } @@ -98,6 +99,7 @@ export const detailJob: JobDetail = { configSnapshot: null, reviewId: 501, retryOfJobId: null, + reviewRound: 1, summaryModel: "gpt-4o-mini", files: [ { diff --git a/test/db/review-round.spec.ts b/test/db/review-round.spec.ts new file mode 100644 index 00000000..c6a867ad --- /dev/null +++ b/test/db/review-round.spec.ts @@ -0,0 +1,97 @@ +import { expect, it } from "vitest"; +import { + cancelJob, + completeJob, + failJob, + insertJob, + supersedeOlderJobs, +} from "@codraoss/db/jobs"; +import { createTestEnv, dbDescribe, sha, uniqueName } from "../helpers"; + +const env = createTestEnv(); + +async function newJob(repo: string, prNumber: number, commitSeed: string) { + return insertJob(env, { + installationId: "123", + owner: "test-owner", + repo, + prNumber, + prTitle: "Review round", + prAuthor: "author", + commitSha: sha(commitSeed), + baseSha: sha("base"), + trigger: "auto", + headRef: "feature", + baseRef: "main", + }); +} + +async function markDone(jobId: string) { + await completeJob(env, jobId, { + verdict: "comment", + fileCount: 1, + commentCount: 1, + totalInputTokens: 1, + totalOutputTokens: 1, + summaryMarkdown: "done", + reviewId: null, + summaryModel: null, + }); +} + +dbDescribe("insertJob review_round", () => { + it("assigns reviewRound 1 to the first job on a pull request", async () => { + const repo = uniqueName("review-round-first"); + const job = await newJob(repo, 7, "c1"); + expect(job.reviewRound).toBe(1); + }); + + it("increments after done or failed jobs, but not superseded or cancelled ones", async () => { + const repo = uniqueName("review-round-inc"); + const first = await newJob(repo, 11, "c1"); + expect(first.reviewRound).toBe(1); + + const queuedIgnored = await newJob(repo, 11, "c-queued"); + expect(queuedIgnored.reviewRound).toBe(1); + + await markDone(first.id); + const afterDone = await newJob(repo, 11, "c2"); + expect(afterDone.reviewRound).toBe(2); + + await failJob(env, afterDone.id, "review failed"); + const afterFailed = await newJob(repo, 11, "c3"); + expect(afterFailed.reviewRound).toBe(3); + + const cancelled = await newJob(repo, 11, "c-cancel"); + expect(cancelled.reviewRound).toBe(3); + const cancelledOk = await cancelJob(env, cancelled.id); + expect(cancelledOk).toBe(true); + + const afterCancelled = await newJob(repo, 11, "c4"); + expect(afterCancelled.reviewRound).toBe(3); + + const superseded = await newJob(repo, 11, "c-super"); + expect(superseded.reviewRound).toBe(3); + const n = await supersedeOlderJobs(env, { + installationId: "123", + owner: "test-owner", + repo, + prNumber: 11, + newJobId: afterCancelled.id, + }); + expect(n).toBeGreaterThanOrEqual(1); + + const afterSuperseded = await newJob(repo, 11, "c5"); + expect(afterSuperseded.reviewRound).toBe(3); + }); + + it("counts rounds independently per pull request", async () => { + const repo = uniqueName("review-round-pr"); + const pr1 = await newJob(repo, 1, "a1"); + await markDone(pr1.id); + const pr1b = await newJob(repo, 1, "a2"); + const pr2 = await newJob(repo, 2, "b1"); + expect(pr1b.reviewRound).toBe(2); + expect(pr2.reviewRound).toBe(1); + }); +});