package demoseed import ( "fmt" "lincodex/backend/internal/domain" "lincodex/backend/internal/usecase/assessment" ) const ( // FullCategory is the bank category of the prepared full attempt and of // the full test the jury can start. It maps to Backend Junior with Go. FullCategory = domain.AssessmentCategory("demo-backend-junior") // TrainingCategory is the separate express training category. It never // has a real opening, so an express run never feeds a category. TrainingCategory = "demo-express" fullBundleName = "demo-seed-backend-junior" trainingBundleName = "demo-seed-express" fullFamilyStem = "demo-seed-backend" trainingFamilyStem = "demo-seed-express" // bankAuthor marks the families as synthetic drafts. bankReviewer is the // fixed audit label of the seed as their reviewer, not a subject review. // Both are part of the family content, so they stay the same for every // operator and a repeated import changes nothing. bankAuthor = "ai:demo-seed-synthetic-bank" bankReviewer = "operator:demo-seed-bank-reviewer" // maxBankFamilies bounds what a policy may ask for; the bundle loader // accepts at most 200 families per bundle. maxBankFamilies = 200 ) // fullSpecialization and friends are the public catalog mapping of the full // category. var ( fullSpecialization = domain.SpecializationBackend fullGrade = domain.GradeJunior fullTaskLanguage = domain.TaskLanguageGo ) // familyTemplate is one synthetic task family whose answer is an integer. // The seed answers the integer formats only, so every template has one. type familyTemplate struct { specialization string title string generator domain.AssessmentSeedGenerator solution string principle string criteria []domain.AssessmentRubricCriterion } const rubricPrinciple = "A submission is first compared with the reference answer of its variant and then judged against the criteria below. This rubric defines no points and no pass mark: how criteria turn into a grade is decided by the versioned grading policy." // familyTemplates are the synthetic task families of the demonstration bank, // the integer-answer families of the checked-in backend demo draft. func familyTemplates() []familyTemplate { return []familyTemplate{ { specialization: "rate-limiting", title: "Rejected requests of a sliding-window limiter", generator: domain.AssessmentSeedGenerator{ Algorithm: "rate_limit_window.v1", Parameters: map[string]domain.AssessmentParameterRange{ "requests": {Min: 12, Max: 30}, "window": {Min: 3, Max: 15}, "limit": {Min: 2, Max: 6}, "max_gap": {Min: 1, Max: 5}, }, StatementTemplate: "A gateway applies a sliding-window limiter. A request at second t is accepted only when fewer than {{.limit}} previously accepted requests arrived in (t - {{.window}}, t]. Rejected requests are not remembered. The gateway receives {{.requests}} requests at these non-decreasing arrival times: {{.timestamps}}. How many requests are rejected?", }, solution: "Keep accepted arrival times in order. Before each request at t, remove accepted times at or before t - window. Accept and append t when fewer than limit times remain; otherwise count a rejection without appending it.", principle: rubricPrinciple, criteria: []domain.AssessmentRubricCriterion{ {ID: "correct_answer", Description: "The submitted number equals the reference answer of the variant."}, {ID: "window_boundary", Description: "A request exactly window seconds old is outside the half-open interval."}, {ID: "rejected_not_counted", Description: "Rejected requests do not occupy capacity in later windows."}, }, }, { specialization: "messaging", title: "Idempotent consumer with a duplicate window", generator: domain.AssessmentSeedGenerator{ Algorithm: "dedup_window.v1", Parameters: map[string]domain.AssessmentParameterRange{ "events": {Min: 10, Max: 40}, "keys": {Min: 2, Max: 6}, "ttl": {Min: 3, Max: 20}, "max_gap": {Min: 1, Max: 6}, }, StatementTemplate: "A consumer receives {{.events}} messages written as KEY@TIME, where TIME is the arrival second and times never decrease. It processes a message unless the same KEY was processed less than {{.ttl}} seconds earlier; such a message is dropped as a duplicate. A dropped message does not postpone the next processing of its KEY. The stream is: {{.stream}}. How many messages does the consumer process?", }, solution: "Keep a map from key to the time of its last processed message. For each message, if the key is present and the time difference is smaller than ttl, drop it; otherwise record the time and count the message as processed.", principle: rubricPrinciple, criteria: []domain.AssessmentRubricCriterion{ {ID: "correct_answer", Description: "The submitted number equals the reference answer of the variant."}, {ID: "duplicate_boundary", Description: "A message exactly ttl seconds after the last processed one with the same key is processed, not dropped."}, {ID: "dropped_not_extending", Description: "A dropped duplicate does not move the time stored for its key."}, }, }, { specialization: "caching", title: "Hit count of a least-recently-used cache", generator: domain.AssessmentSeedGenerator{ Algorithm: "lru_hits.v1", Parameters: map[string]domain.AssessmentParameterRange{ "accesses": {Min: 15, Max: 50}, "keys": {Min: 3, Max: 10}, "capacity": {Min: 2, Max: 5}, }, StatementTemplate: "An in-memory cache holds at most {{.capacity}} entries and evicts the least recently used entry when it is full and a missing key must be stored. Reading a key that is present is a hit; reading a key that is absent is a miss, after which the key is stored. Both a hit and a miss make the key the most recently used one. The cache starts empty and serves these {{.accesses}} reads, drawn from {{.keys}} possible keys numbered from 1 to {{.keys}}: {{.sequence}}. How many reads are hits?", }, solution: "Maintain the cached keys ordered from least to most recently used. On a hit move the key to the recent end; on a miss evict from the old end when the cache is full, then add the key at the recent end. Count the hits.", principle: rubricPrinciple, criteria: []domain.AssessmentRubricCriterion{ {ID: "correct_answer", Description: "The submitted number equals the reference answer of the variant."}, {ID: "recency_on_hit", Description: "A hit refreshes the recency of the key, so it is evicted later."}, {ID: "eviction_order", Description: "On a miss in a full cache the least recently used key is evicted before the new key is stored."}, }, }, } } // syntheticBundle builds count reviewed families of the category by cycling // the templates under keys stem-01, stem-02, and so on. func syntheticBundle(name string, category string, stem string, count int) (assessment.Bundle, error) { if count < 1 || count > maxBankFamilies { return assessment.Bundle{}, fmt.Errorf("demoseed.syntheticBundle - %d families are outside 1 to %d", count, maxBankFamilies) } templates := familyTemplates() bundle := assessment.Bundle{ Name: name, Category: category, Author: bankAuthor, Review: assessment.BundleReview{Status: string(domain.AssessmentReviewApproved), Reviewer: bankReviewer}, Families: make([]assessment.BundleFamily, 0, count), } for index := range count { template := templates[index%len(templates)] bundle.Families = append(bundle.Families, assessment.BundleFamily{ Key: fmt.Sprintf("%s-%02d", stem, index+1), Version: 1, Specialization: template.specialization, Title: fmt.Sprintf("%s (synthetic demo %02d)", template.title, index+1), SeedGenerator: template.generator, AnswerKey: domain.AssessmentAnswerKey{AnswerFormat: domain.AnswerFormatInteger, ReferenceSolution: template.solution}, Rubric: domain.AssessmentRubric{Principle: template.principle, Criteria: template.criteria}, }) } if _, err := bundle.Drafts(); err != nil { return assessment.Bundle{}, fmt.Errorf("demoseed.syntheticBundle - validate bundle: %w", err) } return bundle, nil }