fix(iace): prevent mitigation explosion — fallback only for unassigned

Pattern-suggested measures go to all hazards in category (correct).
Category-based fallback only for hazards WITHOUT pattern suggestions
(max 3 per hazard). Prevents 1654 mitigations explosion.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-05-14 23:41:54 +02:00
parent 8069d0ea89
commit dc55253b9d
@@ -276,19 +276,37 @@ func (h *IACEHandler) InitializeProject(c *gin.Context) {
} }
} }
// Fallback: assign category-based measures to ALL hazards per category // Fallback: assign category-based measures only to hazards that
// didn't get any measures from pattern suggestions (max 3 per hazard)
hazardsWithMeasures := make(map[uuid.UUID]bool)
for _, sm := range matchOutput.SuggestedMeasures {
entry, ok := measureByID[sm.MeasureID]
if !ok {
continue
}
for _, ids := range hazardIDsByCategory {
for _, id := range ids {
_ = entry // suppress unused
hazardsWithMeasures[id] = true
}
}
}
for hazCat, hazIDs := range hazardIDsByCategory { for hazCat, hazIDs := range hazardIDsByCategory {
measCat := patternCatToMeasureCat(hazCat) measCat := patternCatToMeasureCat(hazCat)
measures := measuresByCat[measCat]
for _, hazID := range hazIDs {
if hazardsWithMeasures[hazID] {
continue // Already has pattern-suggested measures
}
added := 0 added := 0
for _, m := range measuresByCat[measCat] { for _, m := range measures {
if added >= 5 { if added >= 3 {
break break
} }
rt := iace.ReductionType(m.ReductionType) rt := iace.ReductionType(m.ReductionType)
if rt == "" { if rt == "" {
rt = iace.ReductionTypeInformation rt = iace.ReductionTypeInformation
} }
for _, hazID := range hazIDs {
_, cerr := h.store.CreateMitigation(ctx, iace.CreateMitigationRequest{ _, cerr := h.store.CreateMitigation(ctx, iace.CreateMitigationRequest{
HazardID: hazID, ReductionType: rt, HazardID: hazID, ReductionType: rt,
Name: m.Name, Description: m.Description, Name: m.Name, Description: m.Description,
@@ -296,10 +314,10 @@ func (h *IACEHandler) InitializeProject(c *gin.Context) {
if cerr == nil { if cerr == nil {
created++ created++
} }
}
added++ added++
} }
} }
}
mitStep = InitStep{Name: "Massnahmen erstellt", Status: "done", Count: created} mitStep = InitStep{Name: "Massnahmen erstellt", Status: "done", Count: created}
} else if len(existingMits) > 0 { } else if len(existingMits) > 0 {
mitStep.Details = "Bereits vorhanden" mitStep.Details = "Bereits vorhanden"