File size: 5,847 Bytes
3f15e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import json
import re
import sys
from pathlib import Path

def _attempt_key(path: Path):
    m = re.search(r"attempt_(\d+)", str(path))
    return int(m.group(1)) if m else -1

def _resolve_manifest(case: Path, result_path: Path, value):
    if not isinstance(value, str) or not value:
        return None
    p = Path(value)
    if not p.is_absolute():
        for base in (result_path.parent, case):
            q = (base / p).resolve()
            if q.is_file():
                return q
        return None
    return p if p.is_file() else None


def _manifest_from_target(case: Path, result_path: Path, value):
    target = _resolve_manifest(case, result_path, value)
    if target is not None:
        return target
    if not isinstance(value, str) or not value:
        return None
    raw = Path(value).expanduser()
    if not raw.is_absolute():
        raw = (result_path.parent / raw).resolve()
    else:
        raw = raw.resolve()
    if not raw.is_dir():
        return None
    exact = raw / "generated" / "fullhome_asset_v16_000" / "composition_manifest.runtime.json"
    if exact.is_file():
        return exact
    candidates = sorted(raw.rglob("composition_manifest.runtime.json"))
    return candidates[0] if len(candidates) == 1 else None


def _verified_result_manifest(case: Path, result_path: Path):
    try:
        data = json.loads(result_path.read_text())
    except Exception:
        return None, None, False
    manifest = _manifest_from_target(case, result_path, data.get("generated_manifest"))
    if manifest is None:
        return None, data, False
    geometry_ok = data.get("full_geometry_passed") is True or data.get("geometry_passed") is True
    if data.get("generation_executed") is False or not geometry_ok or data.get("output_contract_passed") is not True:
        return None, data, True
    return manifest, data, False


def _final_output_result_paths(final_output: Path):
    return (final_output / "chain" / "result.json", final_output / "result.json")

def select(case: Path):
    rejected_targets = []
    summary = case / "execution_summary.json"
    if summary.is_file():
        try:
            data = json.loads(summary.read_text())
        except Exception:
            data = {}
        value = data.get("final_output")
        final_output = None
        if isinstance(value, str) and value:
            final_output = Path(value)
            if not final_output.is_absolute():
                final_output = (summary.parent / final_output).resolve()
            else:
                final_output = final_output.resolve()
        summary_ok = data.get("geometry_passed") is True and data.get("output_contract_passed") is True
        if summary_ok and final_output is not None:
            rejected_result = False
            for result_path in _final_output_result_paths(final_output):
                if not result_path.is_file():
                    continue
                manifest, result_data, rejected = _verified_result_manifest(case, result_path)
                rejected_result = rejected_result or rejected
                if manifest is not None:
                    return (10**9, result_path.stat().st_mtime_ns, result_path, manifest, result_data)
            if not rejected_result:
                manifest = _manifest_from_target(case, summary, value)
                if manifest is not None:
                    return (10**9, summary.stat().st_mtime_ns, summary, manifest, data)
        if not summary_ok and final_output is not None:
            target = final_output
            rejected_targets.append(target)
    paths = []
    direct = case / "execution/chain/result.json"
    if direct.is_file():
        paths.append(direct)
    paths.extend(sorted(case.glob("layout_recovery/attempt_*/execution/chain/result.json"), key=_attempt_key))
    for p in sorted(case.rglob("result.json")):
        if p not in paths and p.parts[-3:] == ("execution", "chain", "result.json"):
            paths.append(p)
    candidates = []
    for p in paths:
        try:
            data = json.loads(p.read_text())
        except Exception:
            continue
        manifest = _manifest_from_target(case, p, data.get("generated_manifest"))
        if not manifest or data.get("generation_executed") is False:
            continue
        if any(manifest == rejected or rejected in manifest.parents for rejected in rejected_targets):
            continue
        geometry_ok = data.get("full_geometry_passed") is True or data.get("geometry_passed") is True
        if not geometry_ok or data.get("output_contract_passed") is not True:
            continue
        candidates.append((_attempt_key(p), p.stat().st_mtime_ns, p, manifest, data))
    if not candidates:
        return None
    return max(candidates, key=lambda x: (x[0], x[1], str(x[2])))

def main():
    if len(sys.argv) != 2:
        raise SystemExit("usage: select_scene_manifest.py CASE")
    case = Path(sys.argv[1]).resolve()
    chosen = select(case)
    if chosen is None:
        return 0
    _, _, result_path, manifest, data = chosen
    if __import__("os").environ.get("RHG_SELECTION_RECORD", "1") != "0":
        (case / "selected_scene.json").write_text(json.dumps({
        "source_result": str(result_path),
        "generated_manifest": str(manifest),
        "attempt": _attempt_key(result_path),
        "generation_executed": data.get("generation_executed"),
        "full_geometry_passed": data.get("full_geometry_passed"),
        "geometry_passed": data.get("geometry_passed"),
        "output_contract_passed": data.get("output_contract_passed"),
        "inputs_unchanged": data.get("inputs_unchanged"),
        "native_status": data.get("native_status"),
        }, ensure_ascii=False, indent=2) + "\n")
    print(manifest)

if __name__ == "__main__":
    main()