T-789: Snapshot van resolved JobConfig in ClaudeJob.requested_*
bij elke job-creatie. Helper in lib/job-config-snapshot.ts laadt
product (preferred_*) en task (requires_opus) en draait de resolver
uit lib/job-config.ts (mirror van scrum4me-mcp/src/lib/job-config.ts —
zelfde matrix, sync-comment in bestand). Toegepast op alle 5
enqueue-locaties:
- actions/user-questions.ts (PLAN_CHAT)
- actions/sprint-runs.ts × 3 (SPRINT_IMPLEMENTATION x2,
TASK_IMPLEMENTATION loop)
- actions/ideas.ts (IDEA_GRILL / IDEA_MAKE_PLAN)
Test-mocks uitgebreid met product.findUnique en task.findUnique zodat
de helper bij unit tests veilig terugvalt op kind-defaults (alle 563
tests groen).
T-790: Sectie 'Config doorgeven aan Claude Code' toegevoegd aan
docs/runbooks/worker-idempotency.md met CLI-flag-mapping en de
verwachte aanroep per kind. Forward-link naar
docs/runbooks/job-model-selection.md (volgt in T-794).
Plus: docs/plans/job-model-selection.md (de approved plan-doc).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { cookies } from 'next/headers'
|
|
import { getIronSession } from 'iron-session'
|
|
import { z } from 'zod'
|
|
|
|
import { prisma } from '@/lib/prisma'
|
|
import { SessionData, sessionOptions } from '@/lib/session'
|
|
import { enforceUserRateLimit } from '@/lib/rate-limit'
|
|
import { ACTIVE_JOB_STATUSES } from '@/lib/job-status'
|
|
import { getJobConfigSnapshot } from '@/lib/job-config-snapshot'
|
|
|
|
async function getSession() {
|
|
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
|
}
|
|
|
|
type ActionResult<T = void> =
|
|
| { success: true; data?: T }
|
|
| { error: string; code?: number }
|
|
|
|
const createSchema = z.object({
|
|
ideaId: z.string().cuid(),
|
|
question: z.string().min(1).max(2000),
|
|
})
|
|
|
|
export async function createUserQuestionAction(
|
|
ideaId: string,
|
|
question: string,
|
|
): Promise<ActionResult<{ questionId: string; jobId: string }>> {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd', code: 401 }
|
|
if (session.isDemo) return { error: 'Demo-gebruikers kunnen geen vragen stellen', code: 403 }
|
|
|
|
const limited = enforceUserRateLimit('create-user-question', session.userId)
|
|
if (limited) return limited
|
|
|
|
const parsed = createSchema.safeParse({ ideaId, question })
|
|
if (!parsed.success) return { error: 'Ongeldige invoer', code: 422 }
|
|
|
|
const idea = await prisma.idea.findFirst({
|
|
where: { id: parsed.data.ideaId, user_id: session.userId },
|
|
select: { id: true, plan_md: true, product_id: true },
|
|
})
|
|
if (!idea) return { error: 'Idee niet gevonden', code: 404 }
|
|
if (!idea.plan_md) return { error: 'Er is nog geen plan voor dit idee', code: 422 }
|
|
if (!idea.product_id) return { error: 'Koppel eerst een product aan dit idee', code: 422 }
|
|
|
|
// Idempotency: weiger als er al een actieve PLAN_CHAT job loopt voor dit idee.
|
|
const existing = await prisma.claudeJob.findFirst({
|
|
where: {
|
|
idea_id: parsed.data.ideaId,
|
|
kind: 'PLAN_CHAT',
|
|
status: { in: ACTIVE_JOB_STATUSES },
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (existing) return { error: 'Er loopt al een actieve PLAN_CHAT voor dit idee', code: 409 }
|
|
|
|
const snapshot = await getJobConfigSnapshot({ kind: 'PLAN_CHAT', productId: idea.product_id })
|
|
|
|
const [uq, job] = await prisma.$transaction([
|
|
prisma.userQuestion.create({
|
|
data: {
|
|
idea_id: parsed.data.ideaId,
|
|
user_id: session.userId,
|
|
question: parsed.data.question,
|
|
},
|
|
}),
|
|
prisma.claudeJob.create({
|
|
data: {
|
|
user_id: session.userId,
|
|
product_id: idea.product_id,
|
|
idea_id: parsed.data.ideaId,
|
|
kind: 'PLAN_CHAT',
|
|
status: 'QUEUED',
|
|
...snapshot,
|
|
},
|
|
}),
|
|
])
|
|
|
|
await prisma.ideaLog.create({
|
|
data: {
|
|
idea_id: parsed.data.ideaId,
|
|
type: 'JOB_EVENT',
|
|
content: 'PLAN_CHAT queued',
|
|
metadata: { job_id: job.id, kind: 'PLAN_CHAT', user_question_id: uq.id },
|
|
},
|
|
})
|
|
|
|
await prisma.$executeRaw`
|
|
SELECT pg_notify('scrum4me_changes', ${JSON.stringify({
|
|
type: 'claude_job_enqueued',
|
|
job_id: job.id,
|
|
idea_id: parsed.data.ideaId,
|
|
user_id: session.userId,
|
|
product_id: idea.product_id,
|
|
kind: 'PLAN_CHAT',
|
|
status: 'queued',
|
|
})}::text)
|
|
`
|
|
|
|
revalidatePath('/ideas/' + parsed.data.ideaId, 'page')
|
|
|
|
return { success: true, data: { questionId: uq.id, jobId: job.id } }
|
|
}
|