File size: 11,964 Bytes
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574e1f0
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ed4bca
 
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ed4bca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574e1f0
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ed4bca
 
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574e1f0
 
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c58228
28c3844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from __future__ import annotations

from typing import Any


SUCCESS_VALIDATION_TOKENS = {
    "success",
    "succeeded",
    "passed",
    "full_inference_success",
    "validated",
    "validated_after_manual_space_test",
    "validated_after_space_test",
    "manual_validation_passed",
    "manual_validated",
    "recovered_by_manual_validation",
    "recovered_by_space_test",
}

PARTIAL_BUILD_TOKENS = {
    "partial",
    "partial_validation",
    "health_only",
    "full_inference_candidate_health_passed",
    "demo_usable_full_promise_not_verified",
    "placeholder_scaffold_deployed",
    "interactive_app_available_smoke_failed",
    "manual_test_required_smoke_failed",
    "completed_with_warnings",
    "unknown",
    "stale",
}

FAILED_BUILD_TOKENS = {"failed", "failure", "error"}
MANUAL_BUILD_TOKENS = {
    "manual_hardware_required",
    "generated_needs_manual_hardware",
    "waiting_manual_hardware",
    "manual_action_required",
    "waiting_manual_action",
}
BLOCKED_BUILD_TOKENS = {"technical_blocker", "technical_blocker_boot_only", "blocked"}


def _lower(value: Any) -> str:
    return str(value or "").strip().lower()


def _first_nonempty(*values: Any) -> str:
    for value in values:
        if value is not None and str(value).strip():
            return str(value).strip()
    return ""


def _target_space_from_bundle(bundle: dict[str, Any]) -> str:
    summary = bundle.get("summary") or bundle.get("summary_file") or {}
    state = bundle.get("state") or {}
    launch = bundle.get("launch") or {}
    return _first_nonempty(
        summary.get("target_space"),
        summary.get("target_space_id"),
        state.get("target_space"),
        state.get("target_space_id"),
        launch.get("target_space"),
        launch.get("target_space_id"),
    )


def _contract_declares_no_full_inference(bundle: dict[str, Any]) -> bool:
    if _has_authoritative_runtime_success(bundle):
        return False
    contract = bundle.get("inference_contract") or {}
    quality = bundle.get("demo_quality_contract") or {}
    gate = bundle.get("inference_gate") or {}
    blockers = bundle.get("technical_blockers") or {}
    for src in (contract, quality, gate, blockers):
        if not isinstance(src, dict):
            continue
        if src.get("full_inference_implemented") is False or src.get("real_inference_implemented") is False:
            return True
        text = " ".join(
            _lower(src.get(key))
            for key in (
                "status",
                "validation_level",
                "inference_strategy",
                "ui_status",
                "ui_badge",
                "reason",
                "message",
            )
        )
        if any(marker in text for marker in ("diagnostic_only", "diagnostic-only", "boot_only", "boot-only", "no_full_inference", "technical_blocker_boot_only")):
            return True
    return False


def _has_authoritative_runtime_success(bundle: dict[str, Any]) -> bool:
    """Return true when final runtime proof invalidates stale contract blockers."""
    def explicit_full_status(source: dict[str, Any]) -> bool:
        for key in ("status", "final_status", "ui_status", "display_status", "effective_status", "effective_verdict", "verdict"):
            if _lower(source.get(key)) == "full_inference_success":
                return True
        return False

    for source in (
        bundle.get("final_status_reconciliation") or {},
        bundle.get("summary") or bundle.get("summary_file") or {},
        bundle.get("state") or {},
    ):
        if not isinstance(source, dict):
            continue
        if explicit_full_status(source):
            return True
        if source.get("generation_smoke_passed") is True and source.get("promise_fulfilled") is True:
            return True

    live = bundle.get("live_status") or {}
    if isinstance(live, dict):
        if _lower(live.get("stage")) == "done" and _normalize_build_status(live.get("status")) == "full_inference_success":
            return True
        if _lower(live.get("stage")) == "done" and live.get("generation_smoke_passed") is True:
            return True

    gate = bundle.get("inference_gate") or {}
    if isinstance(gate, dict) and gate.get("strong_full_inference_success") is True:
        return True
    return False


def _linked_rows(bundle: dict[str, Any]) -> list[dict[str, Any]]:
    linked = bundle.get("linked_validations") or {}
    rows = linked.get("validations") if isinstance(linked, dict) else linked
    if isinstance(rows, list):
        return [row for row in rows if isinstance(row, dict)]
    return []


def _manual_or_post_build_payload(bundle: dict[str, Any]) -> dict[str, Any]:
    post_build = bundle.get("post_build_validation_status") or {}
    if isinstance(post_build, dict) and post_build:
        return post_build
    manual = bundle.get("manual_validation_status") or {}
    if isinstance(manual, dict) and manual:
        return manual
    summary = bundle.get("summary") or {}
    manual = summary.get("manual_validation_status") if isinstance(summary, dict) else {}
    if isinstance(manual, dict) and manual:
        return manual
    linked = bundle.get("linked_validations") or {}
    if isinstance(linked, dict) and _lower(linked.get("effective_status")) in SUCCESS_VALIDATION_TOKENS:
        rows = _linked_rows(bundle)
        first = next((row for row in rows if _lower(row.get("status") or row.get("effective_status")) in SUCCESS_VALIDATION_TOKENS), {})
        return {
            "status": "success",
            "effective_status": "validated_after_manual_space_test",
            "validation_run_id": first.get("validation_run_id") or first.get("run_id") or "",
            "target_space": first.get("target_space") or "",
            "api_name": first.get("api_name") or "",
            "updated_at": linked.get("updated_at") or first.get("updated_at") or "",
        }
    return {}


def _successful_post_build_validation(bundle: dict[str, Any]) -> dict[str, Any]:
    payload = _manual_or_post_build_payload(bundle)
    if not isinstance(payload, dict) or not payload:
        return {}
    if _lower(payload.get("status") or payload.get("effective_status") or payload.get("effective_verdict")) not in SUCCESS_VALIDATION_TOKENS:
        return {}
    if _contract_declares_no_full_inference(bundle):
        return {}
    target = _target_space_from_bundle(bundle)
    validation_target = _first_nonempty(payload.get("target_space"), payload.get("target_space_id"))
    if target and validation_target and target != validation_target:
        return {}
    return payload


def _normalize_build_status(value: Any) -> str:
    status = _lower(value)
    if status in {"succeeded", "success", "passed", "done", "completed"}:
        return "full_inference_success"
    if status in {"cancelled", "canceled"}:
        return "stopped"
    if status in {"full_inference_candidate_health_passed", "health_only", "partial", "completed_with_warnings"}:
        return "partial_validation"
    if status in {"demo_usable_full_promise_not_verified", "placeholder_scaffold_deployed", "interactive_app_available_smoke_failed", "manual_test_required_smoke_failed"}:
        return status
    return status or "unknown"


def compute_effective_run_status(
    bundle: dict[str, Any],
    *,
    build_status: str | None = None,
    build_verdict: str | None = None,
) -> dict[str, Any]:
    """Return the immutable build verdict plus any post-build display override.

    v198.26.1 deliberately separates the original build result from linked Space
    Test results.  Linked validation may change only the effective/display
    status shown by the UI; it must not rewrite the persisted build verdict.
    """
    summary = bundle.get("summary") or bundle.get("summary_file") or {}
    state = bundle.get("state") or {}
    launch = bundle.get("launch") or {}
    raw_build_status = _first_nonempty(
        build_status,
        summary.get("status"),
        state.get("status"),
        launch.get("status"),
        "unknown",
    )
    if _has_authoritative_runtime_success(bundle):
        raw_build_status = "full_inference_success"
    normalized_build = _normalize_build_status(raw_build_status)
    normalized_verdict = _normalize_build_status(build_verdict or normalized_build)
    post = _successful_post_build_validation(bundle)

    display_status = normalized_build
    display_label = ""
    post_build_status = "none"
    legacy_effective_status = ""
    can_promote = bool(post)
    promotion_reason = ""

    if can_promote:
        if normalized_build in FAILED_BUILD_TOKENS or "failed" in normalized_build:
            display_status = "recovered_by_space_test"
            legacy_effective_status = "recovered_by_manual_validation"
        elif normalized_build in BLOCKED_BUILD_TOKENS or "technical_blocker" in normalized_build:
            display_status = normalized_build
            can_promote = False
            promotion_reason = "blocked_build_not_promotable"
        else:
            display_status = "validated_after_space_test"
            legacy_effective_status = "validated_after_manual_space_test"
        if can_promote:
            post_build_status = display_status
            promotion_reason = "linked_space_test_success"

    if display_status == "validated_after_space_test":
        display_label = "Validated after Space Test"
    elif display_status == "recovered_by_space_test":
        display_label = "Recovered by Space Test"
    elif display_status == "full_inference_success":
        display_label = "Full inference success"
    elif display_status == "demo_usable_full_promise_not_verified":
        display_label = "Demo usable — full promise not verified"
    elif display_status == "placeholder_scaffold_deployed":
        display_label = "Placeholder demo — runtime not implemented"
    elif display_status == "interactive_app_available_smoke_failed":
        display_label = "Smoke input issue — Space reachable"
    elif display_status == "manual_test_required_smoke_failed":
        display_label = "Automatic smoke failed — Space Test recommended"
    elif display_status == "partial_validation":
        display_label = "Partial validation"
    elif display_status == "manual_hardware_required":
        display_label = "Manual hardware required"
    elif display_status in BLOCKED_BUILD_TOKENS:
        display_label = "Diagnostic Space"
    elif display_status in FAILED_BUILD_TOKENS:
        display_label = "Failed"
    elif display_status in {"running", "queued", "pending", "started", "scheduled"}:
        display_label = "Running"
    else:
        display_label = display_status.replace("_", " ").title() if display_status else "Unknown"

    return {
        "schema_version": "effective_run_status.v198_26_6",
        "build_status": normalized_build,
        "build_verdict": normalized_verdict,
        "build_verdict_preserved": True,
        "post_build_status": post_build_status,
        "post_build_validation": {
            "status": "success" if can_promote else ("blocked" if post and not can_promote else "none"),
            "source": "linked_space_test" if post else "none",
            "validation_run_id": post.get("validation_run_id") or post.get("run_id") or "",
            "target_space": post.get("target_space") or post.get("target_space_id") or "",
            "api_name": post.get("api_name") or "",
            "updated_at": post.get("updated_at") or post.get("validated_at") or "",
        },
        "display_status": display_status,
        "display_label": display_label,
        "effective_status": display_status,
        "effective_verdict": display_status,
        "legacy_effective_status": legacy_effective_status,
        "legacy_effective_verdict": legacy_effective_status,
        "promotion_reason": promotion_reason,
        "can_promote_from_space_test": can_promote,
    }