package app import ( "context" "encoding/json" "fmt" "sort" ) // Question-local data keeps unrelated candidates out of each judgment's context. // Jev evaluates the questions independently in parallel; IDs only route answers. // Bound both question count and encoded bytes without dropping/truncating items. func (j *Jev) evaluateItems(ctx context.Context, task, query, field string, items []map[string]string, questions map[string]Question) ([]map[string]Decision, error) { const maxQuestions = 96 state := map[string]string{"query": query} empty, err := j.requestBody(state, map[string]Question{}) if err != nil { return nil, err } names := make([]string, 0, len(questions)) for name := range questions { names = append(names, name) } sort.Strings(names) batches := []map[string]Question{} current := map[string]Question{} bytes := len(empty) for i, item := range items { for _, name := range names { q := questions[name] q.Instructions = map[string]any{"question": q.Instructions, field: item} id := fmt.Sprintf("%d_%s", i, name) encoded, err := json.Marshal(map[string]Question{id: q}) if err != nil { return nil, err } size := len(encoded) - 2 // exclude the enclosing braces if len(empty)+size > maxJevRequestBytes { return nil, fmt.Errorf("jev item exceeds the application state budget") } if len(current) > 0 && (bytes+1+size > maxJevRequestBytes || len(current) == maxQuestions) { batches = append(batches, current) current = map[string]Question{} bytes = len(empty) } if len(current) > 0 { bytes++ // comma between questions } current[id] = q bytes += size } } if len(current) > 0 { batches = append(batches, current) } replies := make([]JevResponse, len(batches)) err = parallel(ctx, len(batches), cap(j.slots), func(ctx context.Context, i int) error { var err error replies[i], err = j.evaluate(ctx, task, state, batches[i]) return err }) if err != nil { return nil, err } flat := map[string]Decision{} for _, r := range replies { for id, a := range r.Answers { flat[id] = a } } answers := make([]map[string]Decision, len(items)) for i := range items { answers[i] = make(map[string]Decision, len(questions)) for _, name := range names { answers[i][name] = flat[fmt.Sprintf("%d_%s", i, name)] } } return answers, nil }