feat(ST-006): voeg restartClaudeJobAction toe aan actions/claude-jobs.ts (#174)

- Exporteert restartClaudeJobAction(jobId) die FAILED/CANCELLED/SKIPPED jobs atomair reset naar QUEUED
- Valideert auth, demo-blokkade, ownership en restartbare status
- Gebruikt prisma.$transaction: claudeJob.updateMany + conditionale sprintTaskExecution.updateMany reset
- Verstuurt pg_notify claude_job_status zodat Jobs-pagina via SSE ververst
- Unit-tests: happy-path (FAILED/CANCELLED/SKIPPED), demo-blokkade, not-found, niet-restartbare status, race-conditie en sprint sub-task reset
This commit is contained in:
Janpeter Visser 2026-05-09 13:59:06 +02:00 committed by GitHub
parent 3c773421da
commit 35e37dac09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 218 additions and 6 deletions

View file

@ -1,6 +1,7 @@
'use server'
import { revalidatePath } from 'next/cache'
import { type ClaudeJobStatus } from '@prisma/client'
import { prisma } from '@/lib/prisma'
import { getSession } from '@/lib/auth'
import { ACTIVE_JOB_STATUSES, jobStatusToApi } from '@/lib/job-status'
@ -15,6 +16,9 @@ type EnqueueAllResult =
type CancelResult = { success: true } | { error: string }
type RestartResult = { success: true } | { error: string }
const RESTARTABLE_STATUSES: ClaudeJobStatus[] = ['FAILED', 'CANCELLED', 'SKIPPED']
export type PreviewTask = {
id: string
title: string
@ -109,3 +113,76 @@ export async function cancelClaudeJobAction(jobId: string): Promise<CancelResult
revalidatePath(`/products/${job.product_id}/solo`)
return { success: true }
}
export async function restartClaudeJobAction(jobId: string): Promise<RestartResult> {
const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd' }
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
if (!jobId) return { error: 'job_id is verplicht' }
const job = await prisma.claudeJob.findFirst({
where: { id: jobId, user_id: session.userId },
select: { id: true, status: true, kind: true, task_id: true, idea_id: true, sprint_run_id: true, product_id: true },
})
if (!job) return { error: 'Job niet gevonden' }
if (!RESTARTABLE_STATUSES.includes(job.status)) {
return { error: 'Alleen mislukte, geannuleerde of overgeslagen jobs kunnen opnieuw gestart worden' }
}
const updated = await prisma.$transaction(async (tx) => {
const result = await tx.claudeJob.updateMany({
where: { id: jobId, status: { in: RESTARTABLE_STATUSES } },
data: {
status: 'QUEUED',
retry_count: { increment: 1 },
claimed_by_token_id: null,
claimed_at: null,
started_at: null,
finished_at: null,
pushed_at: null,
verify_result: null,
error: null,
summary: null,
branch: null,
head_sha: null,
lease_until: null,
},
})
if (result.count === 0) return 0
if (job.kind === 'SPRINT_IMPLEMENTATION') {
await tx.sprintTaskExecution.updateMany({
where: { sprint_job_id: jobId },
data: {
status: 'PENDING',
verify_result: null,
verify_summary: null,
skip_reason: null,
head_sha: null,
started_at: null,
finished_at: null,
},
})
}
return result.count
})
if (updated === 0) {
return { error: 'Job-status is gewijzigd; herlaad en probeer opnieuw' }
}
await prisma.$executeRaw`
SELECT pg_notify('scrum4me_changes', ${JSON.stringify({
type: 'claude_job_status',
job_id: jobId,
kind: job.kind,
task_id: job.task_id,
idea_id: job.idea_id,
sprint_run_id: job.sprint_run_id,
user_id: session.userId,
product_id: job.product_id,
status: jobStatusToApi('QUEUED'),
})}::text)
`
revalidatePath('/jobs')
return { success: true }
}