| 1 | package autoresearch |
| 2 | |
| 3 | import "path/filepath" |
| 4 | |
| 5 | func (s *Store) Readiness(taskID string) (*ReadinessReport, error) { |
| 6 | report := &ReadinessReport{} |
| 7 | validation, err := s.ValidateTask(taskID) |
| 8 | if err != nil { |
| 9 | return nil, err |
| 10 | } |
| 11 | if !validation.Valid { |
| 12 | for _, validationErr := range validation.Errors { |
| 13 | report.Errors = append(report.Errors, validationErr.File+":"+validationErr.Field+": "+validationErr.Error) |
| 14 | } |
| 15 | return report, nil |
| 16 | } |
| 17 | task, err := s.LoadTask(taskID) |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | storeRoot, taskRel, err := s.openTaskRoot(taskID) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | defer storeRoot.Close() |
| 26 | var progress Progress |
| 27 | if err := readJSONFile(storeRoot, filepath.Join(taskRel, "state", "progress.json"), &progress); err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | if progress.Status == StatusBlocked { |
| 31 | report.BlockedReason = progress.BlockedReason |
| 32 | if report.BlockedReason == "" { |
| 33 | report.BlockedReason = "task is blocked" |
| 34 | } |
| 35 | return report, nil |
| 36 | } |
| 37 | findings, err := s.Findings(taskID, 0) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | accepted := acceptedFindingIDs(findings) |
| 42 | for _, criterion := range task.Spec.SuccessCriteria { |
| 43 | if !criterion.Required { |
| 44 | continue |
| 45 | } |
| 46 | if countAcceptedEvidence(criterion.EvidenceIDs, accepted) == 0 { |
| 47 | report.MissingCriteria = append(report.MissingCriteria, criterion.ID) |
| 48 | } |
| 49 | } |
| 50 | report.Ready = len(report.MissingCriteria) == 0 && report.BlockedReason == "" && len(report.Errors) == 0 |
| 51 | return report, nil |
| 52 | } |
| 53 | |
| 54 | func acceptedFindingIDs(findings []Finding) map[string]bool { |
| 55 | accepted := make(map[string]bool, len(findings)) |
| 56 | for _, finding := range findings { |
| 57 | if finding.Accepted { |
| 58 | accepted[finding.ID] = true |
| 59 | } |
| 60 | } |
| 61 | return accepted |
| 62 | } |
| 63 | |
| 64 | func countAcceptedEvidence(ids []string, accepted map[string]bool) int { |
| 65 | count := 0 |
| 66 | for _, id := range ids { |
| 67 | if accepted[id] { |
| 68 | count++ |
| 69 | } |
| 70 | } |
| 71 | return count |
| 72 | } |
| 73 |