PBI-50 F2-T1: claim-filter kind-based + lease_until persisten

Schema-sync vanaf Scrum4Me (PBI-50 F1):
- PrStrategy.SPRINT_BATCH, ClaudeJobKind.SPRINT_IMPLEMENTATION
- enum SprintTaskExecutionStatus, model SprintTaskExecution
- ClaudeJob.lease_until + status_lease_until index
- SprintRun.previous_run_id (self-relation)

tryClaimJob in src/tools/wait-for-job.ts:
- WHERE-clause refactor naar kind-based discriminatie. NULL-checks vervangen
  door expliciete `cj.kind IN (...)`. SPRINT_IMPLEMENTATION en TASK_IMPLEMENTATION
  vereisen beide actieve SprintRun (QUEUED/RUNNING). Idea-kinds blijven
  standalone claimable.
- UPDATE op claim zet `lease_until = NOW() + INTERVAL '5 minutes'`.

Tests: 19 wait-for-job tests groen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Madhura68 2026-05-07 12:27:48 +02:00
parent 7dbc9fe249
commit de6bbd4edd
2 changed files with 118 additions and 62 deletions

View file

@ -87,6 +87,7 @@ enum SprintRunStatus {
enum PrStrategy {
SPRINT
STORY
SPRINT_BATCH
}
enum IdeaStatus {
@ -105,6 +106,15 @@ enum ClaudeJobKind {
IDEA_GRILL
IDEA_MAKE_PLAN
PLAN_CHAT
SPRINT_IMPLEMENTATION
}
enum SprintTaskExecutionStatus {
PENDING
RUNNING
DONE
FAILED
SKIPPED
}
enum IdeaLogType {
@ -314,6 +324,9 @@ model SprintRun {
failed_task Task? @relation("SprintRunFailedTask", fields: [failed_task_id], references: [id], onDelete: SetNull)
failed_task_id String?
pause_context Json?
previous_run_id String? @unique
previous_run SprintRun? @relation("SprintRunChain", fields: [previous_run_id], references: [id], onDelete: SetNull)
next_run SprintRun? @relation("SprintRunChain")
created_at DateTime @default(now())
updated_at DateTime @updatedAt
jobs ClaudeJob[]
@ -350,6 +363,7 @@ model Task {
claude_questions ClaudeQuestion[]
claude_jobs ClaudeJob[]
sprint_run_failures SprintRun[] @relation("SprintRunFailedTask")
sprint_task_executions SprintTaskExecution[]
@@unique([product_id, code])
@@index([story_id, priority, sort_order])
@ -392,6 +406,8 @@ model ClaudeJob {
summary String?
error String?
retry_count Int @default(0)
lease_until DateTime?
task_executions SprintTaskExecution[] @relation("SprintJobExecutions")
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@ -401,9 +417,41 @@ model ClaudeJob {
@@index([sprint_run_id, status])
@@index([status, claimed_at])
@@index([status, finished_at])
@@index([status, lease_until])
@@map("claude_jobs")
}
// PBI-50: frozen scope-snapshot per SPRINT_IMPLEMENTATION-claim. Bij claim
// wordt voor elke TO_DO-task in scope één PENDING-record gemaakt met
// implementation_plan + verify_required gesnapshot. Worker en gate werken
// uitsluitend op deze rows; latere wijzigingen aan Task hebben geen
// invloed op de lopende batch.
model SprintTaskExecution {
id String @id @default(cuid())
sprint_job ClaudeJob @relation("SprintJobExecutions", fields: [sprint_job_id], references: [id], onDelete: Cascade)
sprint_job_id String
task Task @relation(fields: [task_id], references: [id], onDelete: Cascade)
task_id String
order Int
plan_snapshot String @db.Text
verify_required_snapshot VerifyRequired
verify_only_snapshot Boolean @default(false)
base_sha String?
head_sha String?
status SprintTaskExecutionStatus @default(PENDING)
verify_result VerifyResult?
verify_summary String? @db.Text
skip_reason String? @db.Text
started_at DateTime?
finished_at DateTime?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@unique([sprint_job_id, task_id])
@@index([sprint_job_id, order])
@@map("sprint_task_executions")
}
model ModelPrice {
id String @id @default(cuid())
model_id String @unique

View file

@ -308,12 +308,15 @@ export async function tryClaimJob(
): Promise<string | null> {
// Atomic claim in a single transaction — also captures plan_snapshot from task.
//
// Sprint-flow filter (PBI-46):
// Idea-jobs (task_id IS NULL) blijven onafhankelijk claimable.
// Task-jobs zijn alleen claimable wanneer ze aan een actieve SprintRun
// hangen (status QUEUED of RUNNING). Legacy task-jobs zonder sprint_run_id
// en jobs in PAUSED/FAILED/CANCELLED/DONE SprintRuns worden overgeslagen.
// PBI-50: claim-filter discrimineert via cj.kind:
// - IDEA_GRILL/IDEA_MAKE_PLAN/PLAN_CHAT: standalone idea-jobs.
// - TASK_IMPLEMENTATION/SPRINT_IMPLEMENTATION: alleen via actieve SprintRun
// (status QUEUED of RUNNING). Legacy task-jobs zonder sprint_run_id en
// jobs in PAUSED/FAILED/CANCELLED/DONE SprintRuns worden overgeslagen.
// Bij eerste claim van een nog QUEUED SprintRun → status RUNNING.
//
// PBI-50 lease: lease_until = NOW() + 5min op claim. resetStaleClaimedJobs
// reset bij verlopen lease.
const rows = await prisma.$transaction(async (tx) => {
const found = productId
? await tx.$queryRaw<
@ -327,8 +330,10 @@ export async function tryClaimJob(
AND cj.product_id = ${productId}
AND cj.status = 'QUEUED'
AND (
cj.task_id IS NULL
OR (cj.sprint_run_id IS NOT NULL AND sr.status IN ('QUEUED', 'RUNNING'))
cj.kind IN ('IDEA_GRILL', 'IDEA_MAKE_PLAN', 'PLAN_CHAT')
OR (cj.kind IN ('TASK_IMPLEMENTATION', 'SPRINT_IMPLEMENTATION')
AND cj.sprint_run_id IS NOT NULL
AND sr.status IN ('QUEUED', 'RUNNING'))
)
ORDER BY cj.created_at ASC
LIMIT 1
@ -344,8 +349,10 @@ export async function tryClaimJob(
WHERE cj.user_id = ${userId}
AND cj.status = 'QUEUED'
AND (
cj.task_id IS NULL
OR (cj.sprint_run_id IS NOT NULL AND sr.status IN ('QUEUED', 'RUNNING'))
cj.kind IN ('IDEA_GRILL', 'IDEA_MAKE_PLAN', 'PLAN_CHAT')
OR (cj.kind IN ('TASK_IMPLEMENTATION', 'SPRINT_IMPLEMENTATION')
AND cj.sprint_run_id IS NOT NULL
AND sr.status IN ('QUEUED', 'RUNNING'))
)
ORDER BY cj.created_at ASC
LIMIT 1
@ -362,7 +369,8 @@ export async function tryClaimJob(
SET status = 'CLAIMED',
claimed_by_token_id = ${tokenId},
claimed_at = NOW(),
plan_snapshot = ${snapshot}
plan_snapshot = ${snapshot},
lease_until = NOW() + INTERVAL '5 minutes'
WHERE id = ${jobId}
`