File size: 4,350 Bytes
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""Shared feature-level acceptance logic for visual sculpt passes."""

from __future__ import annotations

from typing import Any


def is_number(value: Any) -> bool:
    return isinstance(value, (int, float)) and not isinstance(value, bool)


def feature_review_policy(spec: dict[str, Any]) -> dict[str, Any]:
    loop = spec.get("selfCorrectLoop")
    if not isinstance(loop, dict):
        return {}
    acceptance = loop.get("visualAcceptance")
    if not isinstance(acceptance, dict):
        return {}
    policy = acceptance.get("featureReviewPolicy")
    return policy if isinstance(policy, dict) else {}


def feature_targets_for_pass(spec: dict[str, Any], pass_id: str) -> list[dict[str, Any]]:
    targets = spec.get("featureReviewTargets", [])
    if not isinstance(targets, list):
        return []
    applicable: list[dict[str, Any]] = []
    for target in targets:
        if not isinstance(target, dict):
            continue
        pass_ids = target.get("passIds", [])
        if isinstance(pass_ids, list) and pass_id in pass_ids:
            applicable.append(target)
    return applicable


def feature_gate_failures(
    spec: dict[str, Any],
    entry: dict[str, Any],
    pass_id: str,
) -> list[str]:
    policy = feature_review_policy(spec)
    if policy.get("enabled") is not True:
        return []

    targets = feature_targets_for_pass(spec, pass_id)
    critical = [
        target
        for target in targets
        if target.get("tier") == "critical" or target.get("mustPass") is True
    ]
    max_critical = policy.get("maxCriticalFeaturesPerPass", 5)
    failures: list[str] = []
    if is_number(max_critical) and len(critical) > int(max_critical):
        failures.append(
            f"pass {pass_id!r} defines {len(critical)} critical features; "
            f"group them into at most {int(max_critical)} semantic systems"
        )
    important = [target for target in targets if target.get("tier") == "important"]
    max_important = policy.get("maxImportantFeaturesPerPass", 3)
    if is_number(max_important) and len(important) > int(max_important):
        failures.append(
            f"pass {pass_id!r} defines {len(important)} important features; "
            f"keep only the {int(max_important)} most uncertain or high-value systems"
        )

    reviews = entry.get("featureReviews", [])
    review_by_id = {
        review.get("id"): review
        for review in reviews
        if isinstance(review, dict) and isinstance(review.get("id"), str)
    } if isinstance(reviews, list) else {}

    default_threshold = policy.get("criticalDefaultThreshold", 0.8)
    for target in critical:
        target_id = target.get("id")
        if not isinstance(target_id, str) or not target_id:
            continue
        review = review_by_id.get(target_id)
        if not isinstance(review, dict):
            failures.append(f"critical feature {target_id!r} has no AI vision review")
            continue
        if review.get("visible") is False:
            failures.append(f"critical feature {target_id!r} is not visible in the review view")
            continue
        score = review.get("score")
        minimum = target.get("minimumScore", default_threshold)
        if not is_number(score):
            failures.append(f"critical feature {target_id!r} has no numeric score")
        elif not is_number(minimum) or float(score) < float(minimum):
            failures.append(
                f"critical feature {target_id!r} score {score} is below {minimum}"
            )
    important_ids = {
        target.get("id")
        for target in targets
        if target.get("tier") == "important" and isinstance(target.get("id"), str)
    }
    important_scores = [
        float(review["score"])
        for feature_id, review in review_by_id.items()
        if feature_id in important_ids and is_number(review.get("score"))
    ]
    important_threshold = policy.get("importantAverageThreshold", 0.65)
    if important_scores and is_number(important_threshold):
        average = sum(important_scores) / len(important_scores)
        if average < float(important_threshold):
            failures.append(
                f"reviewed important features average {average:.3f} is below "
                f"{float(important_threshold):.3f}"
            )
    return failures