| 446 | |
| 447 | |
| 448 | def normalize_result(result: dict[str, Any]) -> dict[str, Any]: |
| 449 | normalized = dict(result) |
| 450 | normalized['should_comment'] = as_bool(normalized.get('should_comment')) |
| 451 | normalized['is_duplicate'] = as_bool(normalized.get('is_duplicate')) |
| 452 | normalized['auto_close_candidate'] = as_bool(normalized.get('auto_close_candidate')) |
| 453 | |
| 454 | classification = str(normalized.get('classification') or 'no-match').strip().lower() |
| 455 | if classification not in { |
| 456 | 'duplicate', |
| 457 | 'overlapping-scope', |
| 458 | 'related-but-distinct', |
| 459 | 'no-match', |
| 460 | }: |
| 461 | classification = 'no-match' |
| 462 | normalized['classification'] = classification |
| 463 | |
| 464 | confidence = str(normalized.get('confidence') or 'low').strip().lower() |
| 465 | if confidence not in {'high', 'medium', 'low'}: |
| 466 | confidence = 'low' |
| 467 | normalized['confidence'] = confidence |
| 468 | |
| 469 | try: |
| 470 | canonical_issue_number = normalized.get('canonical_issue_number') |
| 471 | if canonical_issue_number in {None, ''}: |
| 472 | normalized['canonical_issue_number'] = None |
| 473 | else: |
| 474 | normalized['canonical_issue_number'] = int(str(canonical_issue_number)) |
| 475 | except (TypeError, ValueError): |
| 476 | normalized['canonical_issue_number'] = None |
| 477 | |
| 478 | candidate_issues = normalized.get('candidate_issues') |
| 479 | if not isinstance(candidate_issues, list): |
| 480 | candidate_issues = [] |
| 481 | normalized['candidate_issues'] = candidate_issues[:3] |
| 482 | |
| 483 | if classification not in {'duplicate', 'overlapping-scope'}: |
| 484 | normalized['should_comment'] = False |
| 485 | if classification != 'duplicate': |
| 486 | normalized['is_duplicate'] = False |
| 487 | normalized['auto_close_candidate'] = False |
| 488 | if ( |
| 489 | classification in {'duplicate', 'overlapping-scope'} |
| 490 | and normalized['candidate_issues'] |
| 491 | and confidence in {'high', 'medium'} |
| 492 | ): |
| 493 | normalized['should_comment'] = True |
| 494 | if normalized['auto_close_candidate'] and confidence != 'high': |
| 495 | normalized['auto_close_candidate'] = False |
| 496 | if normalized['auto_close_candidate'] and not normalized['candidate_issues']: |
| 497 | normalized['auto_close_candidate'] = False |
| 498 | if ( |
| 499 | normalized['auto_close_candidate'] |
| 500 | and normalized['canonical_issue_number'] is None |
| 501 | ): |
| 502 | first_candidate = ( |
| 503 | normalized['candidate_issues'][0] if normalized['candidate_issues'] else {} |
| 504 | ) |
| 505 | candidate_number = first_candidate.get('number') |