Competition/Round architecture: full platform rewrite (Phases 1-9)
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m45s

Replace Pipeline/Stage system with Competition/Round architecture.
New schema: Competition, Round (7 types), JuryGroup, AssignmentPolicy,
ProjectRoundState, DeliberationSession, ResultLock, SubmissionWindow.
New services: round-engine, round-assignment, deliberation, result-lock,
submission-manager, competition-context, ai-prompt-guard.
Full admin/jury/applicant/mentor UI rewrite. AI prompt hardening with
structured prompts, retry logic, and injection detection. All legacy
pipeline/stage code removed. 4 new migrations + seed aligned.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-15 23:04:15 +01:00
parent 9ab4717f96
commit 6ca39c976b
349 changed files with 69938 additions and 28767 deletions

View File

@@ -54,7 +54,7 @@ export const evaluationRouter = router({
// Get active form for this stage
const form = await ctx.prisma.evaluationForm.findFirst({
where: { stageId: assignment.stageId, isActive: true },
where: { roundId: assignment.roundId, isActive: true },
})
if (!form) {
throw new TRPCError({
@@ -152,23 +152,23 @@ export const evaluationRouter = router({
throw new TRPCError({ code: 'FORBIDDEN' })
}
// Check voting window via stage
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: evaluation.assignment.stageId },
// Check voting window via round
const round = await ctx.prisma.round.findUniqueOrThrow({
where: { id: evaluation.assignment.roundId },
})
const now = new Date()
if (stage.status !== 'STAGE_ACTIVE') {
if (round.status !== 'ROUND_ACTIVE') {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Stage is not active',
message: 'Round is not active',
})
}
// Check for grace period
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
where: {
stageId: stage.id,
roundId: round.id,
userId: ctx.user.id,
OR: [
{ projectId: null },
@@ -178,9 +178,9 @@ export const evaluationRouter = router({
},
})
const effectiveEndDate = gracePeriod?.extendedUntil ?? stage.windowCloseAt
const effectiveEndDate = gracePeriod?.extendedUntil ?? round.windowCloseAt
if (stage.windowOpenAt && now < stage.windowOpenAt) {
if (round.windowOpenAt && now < round.windowOpenAt) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Voting has not started yet',
@@ -219,7 +219,7 @@ export const evaluationRouter = router({
entityId: id,
detailsJson: {
projectId: evaluation.assignment.projectId,
stageId: evaluation.assignment.stageId,
roundId: evaluation.assignment.roundId,
globalScore: data.globalScore,
binaryDecision: data.binaryDecision,
},
@@ -275,14 +275,14 @@ export const evaluationRouter = router({
listByStage: adminProcedure
.input(
z.object({
stageId: z.string(),
roundId: z.string(),
status: z.enum(['NOT_STARTED', 'DRAFT', 'SUBMITTED', 'LOCKED']).optional(),
})
)
.query(async ({ ctx, input }) => {
return ctx.prisma.evaluation.findMany({
where: {
assignment: { stageId: input.stageId },
assignment: { roundId: input.roundId },
...(input.status && { status: input.status }),
},
include: {
@@ -301,13 +301,13 @@ export const evaluationRouter = router({
* Get my past evaluations (read-only for jury)
*/
myPastEvaluations: protectedProcedure
.input(z.object({ stageId: z.string().optional() }))
.input(z.object({ roundId: z.string().optional() }))
.query(async ({ ctx, input }) => {
return ctx.prisma.evaluation.findMany({
where: {
assignment: {
userId: ctx.user.id,
...(input.stageId && { stageId: input.stageId }),
...(input.roundId && { roundId: input.roundId }),
},
status: 'SUBMITTED',
},
@@ -315,7 +315,7 @@ export const evaluationRouter = router({
assignment: {
include: {
project: { select: { id: true, title: true } },
stage: { select: { id: true, name: true } },
round: { select: { id: true, name: true } },
},
},
},
@@ -340,12 +340,12 @@ export const evaluationRouter = router({
})
)
.mutation(async ({ ctx, input }) => {
// Look up the assignment to get projectId, stageId, userId
// Look up the assignment to get projectId, roundId, userId
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
where: { id: input.assignmentId },
include: {
project: { select: { title: true } },
stage: { select: { id: true, name: true } },
round: { select: { id: true, name: true } },
},
})
@@ -378,15 +378,15 @@ export const evaluationRouter = router({
await notifyAdmins({
type: NotificationTypes.JURY_INACTIVE,
title: 'Conflict of Interest Declared',
message: `${ctx.user.name || ctx.user.email} declared a conflict of interest (${input.conflictType || 'unspecified'}) for project "${assignment.project.title}" in ${assignment.stage.name}.`,
linkUrl: `/admin/stages/${assignment.stageId}/coi`,
message: `${ctx.user.name || ctx.user.email} declared a conflict of interest (${input.conflictType || 'unspecified'}) for project "${assignment.project.title}" in ${assignment.round.name}.`,
linkUrl: `/admin/stages/${assignment.roundId}/coi`,
linkLabel: 'Review COI',
priority: 'high',
metadata: {
assignmentId: input.assignmentId,
userId: ctx.user.id,
projectId: assignment.projectId,
stageId: assignment.stageId,
roundId: assignment.roundId,
conflictType: input.conflictType,
},
})
@@ -402,7 +402,7 @@ export const evaluationRouter = router({
detailsJson: {
assignmentId: input.assignmentId,
projectId: assignment.projectId,
stageId: assignment.stageId,
roundId: assignment.roundId,
hasConflict: input.hasConflict,
conflictType: input.conflictType,
},
@@ -430,14 +430,14 @@ export const evaluationRouter = router({
listCOIByStage: adminProcedure
.input(
z.object({
stageId: z.string(),
roundId: z.string(),
hasConflictOnly: z.boolean().optional(),
})
)
.query(async ({ ctx, input }) => {
return ctx.prisma.conflictOfInterest.findMany({
where: {
assignment: { stageId: input.stageId },
assignment: { roundId: input.roundId },
...(input.hasConflictOnly && { hasConflict: true }),
},
include: {
@@ -501,16 +501,16 @@ export const evaluationRouter = router({
* Manually trigger reminder check for a specific stage (admin only)
*/
triggerReminders: adminProcedure
.input(z.object({ stageId: z.string() }))
.input(z.object({ roundId: z.string() }))
.mutation(async ({ ctx, input }) => {
const result = await processEvaluationReminders(input.stageId)
const result = await processEvaluationReminders(input.roundId)
await logAudit({
prisma: ctx.prisma,
userId: ctx.user.id,
action: 'REMINDERS_TRIGGERED',
entityType: 'Stage',
entityId: input.stageId,
entityId: input.roundId,
detailsJson: {
sent: result.sent,
errors: result.errors,
@@ -533,13 +533,13 @@ export const evaluationRouter = router({
.input(
z.object({
projectId: z.string(),
stageId: z.string(),
roundId: z.string(),
})
)
.mutation(async ({ ctx, input }) => {
return generateSummary({
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
userId: ctx.user.id,
prisma: ctx.prisma,
})
@@ -552,15 +552,15 @@ export const evaluationRouter = router({
.input(
z.object({
projectId: z.string(),
stageId: z.string(),
roundId: z.string(),
})
)
.query(async ({ ctx, input }) => {
return ctx.prisma.evaluationSummary.findUnique({
where: {
projectId_stageId: {
projectId_roundId: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
},
})
@@ -570,12 +570,12 @@ export const evaluationRouter = router({
* Generate summaries for all projects in a stage with submitted evaluations (admin only)
*/
generateBulkSummaries: adminProcedure
.input(z.object({ stageId: z.string() }))
.input(z.object({ roundId: z.string() }))
.mutation(async ({ ctx, input }) => {
// Find all projects with at least 1 submitted evaluation in this stage
const assignments = await ctx.prisma.assignment.findMany({
where: {
stageId: input.stageId,
roundId: input.roundId,
evaluation: {
status: 'SUBMITTED',
},
@@ -594,7 +594,7 @@ export const evaluationRouter = router({
try {
await generateSummary({
projectId,
stageId: input.stageId,
roundId: input.roundId,
userId: ctx.user.id,
prisma: ctx.prisma,
})
@@ -625,7 +625,7 @@ export const evaluationRouter = router({
.input(
z.object({
projectIds: z.array(z.string()).min(2).max(3),
stageId: z.string(),
roundId: z.string(),
})
)
.query(async ({ ctx, input }) => {
@@ -633,7 +633,7 @@ export const evaluationRouter = router({
const assignments = await ctx.prisma.assignment.findMany({
where: {
userId: ctx.user.id,
stageId: input.stageId,
roundId: input.roundId,
projectId: { in: input.projectIds },
},
include: {
@@ -668,7 +668,7 @@ export const evaluationRouter = router({
// Fetch the active evaluation form for this stage to get criteria labels
const evaluationForm = await ctx.prisma.evaluationForm.findFirst({
where: { stageId: input.stageId, isActive: true },
where: { roundId: input.roundId, isActive: true },
select: { criteriaJson: true, scalesJson: true },
})
@@ -696,7 +696,7 @@ export const evaluationRouter = router({
.input(
z.object({
projectId: z.string(),
stageId: z.string(),
roundId: z.string(),
})
)
.query(async ({ ctx, input }) => {
@@ -705,7 +705,7 @@ export const evaluationRouter = router({
where: {
userId: ctx.user.id,
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
include: { evaluation: true },
})
@@ -718,8 +718,8 @@ export const evaluationRouter = router({
}
// Check stage settings for peer review
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: input.stageId },
const stage = await ctx.prisma.round.findUniqueOrThrow({
where: { id: input.roundId },
})
const settings = (stage.configJson as Record<string, unknown>) || {}
@@ -736,7 +736,7 @@ export const evaluationRouter = router({
status: 'SUBMITTED',
assignment: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
},
include: {
@@ -821,16 +821,16 @@ export const evaluationRouter = router({
.input(
z.object({
projectId: z.string(),
stageId: z.string(),
roundId: z.string(),
})
)
.query(async ({ ctx, input }) => {
// Get or create discussion
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
where: {
projectId_stageId: {
projectId_roundId: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
},
include: {
@@ -847,7 +847,7 @@ export const evaluationRouter = router({
discussion = await ctx.prisma.evaluationDiscussion.create({
data: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
include: {
comments: {
@@ -860,11 +860,11 @@ export const evaluationRouter = router({
})
}
// Anonymize comments based on stage settings
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: input.stageId },
// Anonymize comments based on round settings
const round = await ctx.prisma.round.findUniqueOrThrow({
where: { id: input.roundId },
})
const settings = (stage.configJson as Record<string, unknown>) || {}
const settings = (round.configJson as Record<string, unknown>) || {}
const anonymizationLevel = (settings.anonymization_level as string) || 'fully_anonymous'
const anonymizedComments = discussion.comments.map((c: { id: string; userId: string; user: { name: string | null }; content: string; createdAt: Date }, idx: number) => {
@@ -907,16 +907,16 @@ export const evaluationRouter = router({
.input(
z.object({
projectId: z.string(),
stageId: z.string(),
roundId: z.string(),
content: z.string().min(1).max(2000),
})
)
.mutation(async ({ ctx, input }) => {
// Check max comment length from stage settings
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: input.stageId },
// Check max comment length from round settings
const round = await ctx.prisma.round.findUniqueOrThrow({
where: { id: input.roundId },
})
const settings = (stage.configJson as Record<string, unknown>) || {}
const settings = (round.configJson as Record<string, unknown>) || {}
const maxLength = (settings.max_comment_length as number) || 2000
if (input.content.length > maxLength) {
throw new TRPCError({
@@ -928,9 +928,9 @@ export const evaluationRouter = router({
// Get or create discussion
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
where: {
projectId_stageId: {
projectId_roundId: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
},
})
@@ -939,7 +939,7 @@ export const evaluationRouter = router({
discussion = await ctx.prisma.evaluationDiscussion.create({
data: {
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
})
}
@@ -970,7 +970,7 @@ export const evaluationRouter = router({
detailsJson: {
discussionId: discussion.id,
projectId: input.projectId,
stageId: input.stageId,
roundId: input.roundId,
},
ipAddress: ctx.ip,
userAgent: ctx.userAgent,
@@ -1007,7 +1007,7 @@ export const evaluationRouter = router({
entityId: input.discussionId,
detailsJson: {
projectId: discussion.projectId,
stageId: discussion.stageId,
roundId: discussion.roundId,
},
ipAddress: ctx.ip,
userAgent: ctx.userAgent,
@@ -1030,11 +1030,11 @@ export const evaluationRouter = router({
.input(
z.object({
assignmentId: z.string(),
stageId: z.string(),
roundId: z.string(),
})
)
.mutation(async ({ ctx, input }) => {
// Verify assignment ownership and stageId match
// Verify assignment ownership and roundId match
const assignment = await ctx.prisma.assignment.findUniqueOrThrow({
where: { id: input.assignmentId },
})
@@ -1043,30 +1043,30 @@ export const evaluationRouter = router({
throw new TRPCError({ code: 'FORBIDDEN' })
}
if (assignment.stageId !== input.stageId) {
if (assignment.roundId !== input.roundId) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Assignment does not belong to this stage',
})
}
// Check stage window
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: input.stageId },
// Check round window
const round = await ctx.prisma.round.findUniqueOrThrow({
where: { id: input.roundId },
})
const now = new Date()
if (stage.status !== 'STAGE_ACTIVE') {
if (round.status !== 'ROUND_ACTIVE') {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: 'Stage is not active',
message: 'Round is not active',
})
}
// Check grace period
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
where: {
stageId: input.stageId,
roundId: input.roundId,
userId: ctx.user.id,
OR: [
{ projectId: null },
@@ -1076,8 +1076,8 @@ export const evaluationRouter = router({
},
})
const effectiveClose = gracePeriod?.extendedUntil ?? stage.windowCloseAt
if (stage.windowOpenAt && now < stage.windowOpenAt) {
const effectiveClose = gracePeriod?.extendedUntil ?? round.windowCloseAt
if (round.windowOpenAt && now < round.windowOpenAt) {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: 'Evaluation window has not opened yet',
@@ -1098,7 +1098,7 @@ export const evaluationRouter = router({
// Get active evaluation form for this stage
const form = await ctx.prisma.evaluationForm.findFirst({
where: { stageId: input.stageId, isActive: true },
where: { roundId: input.roundId, isActive: true },
})
if (!form) {
throw new TRPCError({
@@ -1120,10 +1120,10 @@ export const evaluationRouter = router({
* Get the active evaluation form for a stage
*/
getStageForm: protectedProcedure
.input(z.object({ stageId: z.string() }))
.input(z.object({ roundId: z.string() }))
.query(async ({ ctx, input }) => {
const form = await ctx.prisma.evaluationForm.findFirst({
where: { stageId: input.stageId, isActive: true },
where: { roundId: input.roundId, isActive: true },
})
if (!form) {
@@ -1152,13 +1152,13 @@ export const evaluationRouter = router({
checkStageWindow: protectedProcedure
.input(
z.object({
stageId: z.string(),
roundId: z.string(),
userId: z.string().optional(),
})
)
.query(async ({ ctx, input }) => {
const stage = await ctx.prisma.stage.findUniqueOrThrow({
where: { id: input.stageId },
const stage = await ctx.prisma.round.findUniqueOrThrow({
where: { id: input.roundId },
select: {
id: true,
status: true,
@@ -1173,7 +1173,7 @@ export const evaluationRouter = router({
// Check for grace period
const gracePeriod = await ctx.prisma.gracePeriod.findFirst({
where: {
stageId: input.stageId,
roundId: input.roundId,
userId,
extendedUntil: { gte: now },
},
@@ -1183,13 +1183,13 @@ export const evaluationRouter = router({
const effectiveClose = gracePeriod?.extendedUntil ?? stage.windowCloseAt
const isOpen =
stage.status === 'STAGE_ACTIVE' &&
stage.status === 'ROUND_ACTIVE' &&
(!stage.windowOpenAt || now >= stage.windowOpenAt) &&
(!effectiveClose || now <= effectiveClose)
let reason = ''
if (!isOpen) {
if (stage.status !== 'STAGE_ACTIVE') {
if (stage.status !== 'ROUND_ACTIVE') {
reason = 'Stage is not active'
} else if (stage.windowOpenAt && now < stage.windowOpenAt) {
reason = 'Window has not opened yet'
@@ -1214,7 +1214,7 @@ export const evaluationRouter = router({
listStageEvaluations: protectedProcedure
.input(
z.object({
stageId: z.string(),
roundId: z.string(),
projectId: z.string().optional(),
})
)
@@ -1222,7 +1222,7 @@ export const evaluationRouter = router({
const where: Record<string, unknown> = {
assignment: {
userId: ctx.user.id,
stageId: input.stageId,
roundId: input.roundId,
...(input.projectId ? { projectId: input.projectId } : {}),
},
}