File size: 21,117 Bytes
6aa6c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""Private campaign continuation ledgers; read records, never native or actor paths.

Only the two original pilot formats are accepted as continuation sources. An
unsupported, incomplete or changed claim blocks continuation rather than being
treated as permission to try that task again. Future independent campaigns may
select the complete scope without a carry-forward source.
"""

from __future__ import annotations

import copy
import hashlib
import importlib.util
from pathlib import Path

AUDIT_FILES = ("benchkit/audits/closed_run.py",
               "benchkit/reports/progress.py")
HISTORICAL_AUDIT_FILES = (
    ("harness/audits/closed_run.py", "harness/reports/progress.py"),
)
SCHEMA = "starvla-rsi.campaign-coverage/v1"


def require(condition: bool, message: str) -> None:
    if not condition:
        raise ValueError(message)


def audit_tools(repository: Path | None = None):
    repository = repository or Path(__file__).resolve().parents[2]
    layouts = (AUDIT_FILES,) + HISTORICAL_AUDIT_FILES
    present = {relative for layout in layouts for relative in layout
               if (repository / relative).exists() or (repository / relative).is_symlink()}
    selected = [layout for layout in layouts if set(layout) == present]
    require(len(selected) == 1, "missing/mixed frozen carry-forward auditor layout")
    modules = []
    for index, relative in enumerate(selected[0]):
        path = repository / relative
        require(path.is_file() and not any((repository / parent).is_symlink()
                                          for parent in (Path(relative), *Path(relative).parents)),
                "missing/unsafe frozen carry-forward auditor")
        spec = importlib.util.spec_from_file_location(f"campaign_carry_auditor_{index}", path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        modules.append(module)
    return tuple(modules)


def artifact_hashes(files, relative: str) -> dict[str, str]:
    """Bind the retained artifact directory, including image/video bytes.

    Never follow paths inside native provenance, mailbox references or asset
    manifests. These are retained evidence, not renewed native-asset checks.
    """
    directory = files.root / relative
    require(directory.is_dir() and not directory.is_symlink(), "missing/unsafe prior artifact directory")
    result = {}
    total = 0
    entries = sorted(directory.rglob("*"))
    require(len(entries) <= 16384, "prior artifact inventory exceeds entry limit")
    for path in entries:
        require(not path.is_symlink(), "symlink in prior artifact inventory")
        if path.is_dir():
            continue
        key = path.relative_to(files.root).as_posix()
        path = files.path(key)
        before = path.stat()
        total += before.st_size
        require(total <= 4 * 1024**3, "prior artifact inventory exceeds 4 GiB")
        digest = hashlib.sha256()
        with path.open("rb") as source:
            while payload := source.read(1024 * 1024):
                digest.update(payload)
        after = path.stat()
        require((before.st_size, before.st_mtime_ns, before.st_ctime_ns) ==
                (after.st_size, after.st_mtime_ns, after.st_ctime_ns), "prior artifact changed while hashing")
        result[key] = digest.hexdigest()
    require(entries == sorted(directory.rglob("*")), "prior artifact inventory changed during audit")
    return result


def verify_native_reset(files, artifact_dir: str, benchmark: str, selected: dict, original: dict) -> dict:
    provenance = files.json(artifact_dir + "/native_provenance.json")
    episode = original["episode"]
    require(provenance["task"] == episode["task"] and provenance["seed"] == episode["seed"],
            "prior native task/seed mismatch")
    if benchmark == "simple_humanoid":
        metadata = selected["episode"]["metadata"]
        require(provenance["dr_level"] == metadata["dr_level"] == 0
                and provenance["episode_index"] == metadata["episode_index"] == 0
                and provenance["reset_metadata"]["sha256"] == metadata["reset_metadata_sha256"]
                and provenance["track"] == original["descriptor"]["protocol_version"]
                and provenance["git_revision"] == original["descriptor"]["source_revision"]
                and provenance["diagnostic_only"] is False, "prior SIMPLE reset/track mismatch")
        reset = files.json(artifact_dir + "/private_reset.json")
        require(files.hashes[artifact_dir + "/private_reset.json"] == provenance["selected_reset"]["sha256"],
                "prior SIMPLE selected reset changed")
        require(reset["uid"] == metadata["native_task_uid"], "prior SIMPLE selected reset task UID mismatch")
        return {"dr_level": 0, "episode_index": 0,
                "reset_metadata_sha256": provenance["reset_metadata"]["sha256"],
                "selected_reset_sha256": provenance["selected_reset"]["sha256"]}
    require(provenance["split"] == selected["episode"]["metadata"]["split"] == "target"
            and provenance["native_horizon"] == selected["episode"]["metadata"]["native_horizon"]
            and provenance["sources"]["robocasa"]["revision"] == original["descriptor"]["source_revision"],
            "prior RoboCasa split/horizon mismatch")
    files.raw(artifact_dir + "/initial_scene.xml")
    require(files.hashes[artifact_dir + "/initial_scene.xml"] == provenance["initial_model_xml_sha256"],
            "prior RoboCasa scene changed")
    files.raw(artifact_dir + "/initial_state.npy")
    return {key: provenance[key] for key in ("split", "layout_id", "style_id",
            "initial_model_xml_sha256", "initial_state_sha256", "used_assets_inventory_sha256")}


def read_prior(source: Path, benchmark: str, selected: list[dict], pilots: list[dict],
               *, auditor_repository: Path | None = None) -> dict:
    from benchkit.campaigns.workflow import verify_frozen_package

    audit, progress = audit_tools(auditor_repository)
    require(not source.is_symlink(), "carry-forward campaign must not be a symlink")
    files = audit.ClosedFiles(source)
    manifest = files.json("campaign.json")
    campaign_hash = files.hashes["campaign.json"]
    require(files.json("campaign_digest.json")["sha256"] == campaign_hash, "prior campaign digest mismatch")
    require(manifest["schema_version"] in ("starvla-rsi.campaign/v1", "starvla-rsi.campaign/v3")
            and manifest["benchmark"] == benchmark, "prior campaign benchmark/schema mismatch")
    require(manifest.get("purpose") == "development_pilot" and "coverage_ledger" not in manifest,
            "only original pilot carry-forward is supported; no claim may be discarded")
    require(isinstance(manifest["policy_prompt"], str)
            and hashlib.sha256(manifest["policy_prompt"].encode("utf-8")).hexdigest()
            == manifest["policy_prompt_sha256"], "prior policy prompt digest mismatch")
    require(manifest["planned_cases"] == len(manifest["cases"]) == len(pilots) == 2
            and manifest["attempts_per_task"] == 1, "prior campaign selection/attempt count mismatch")
    expected_budget = {"seed": selected[0]["episode"]["seed"], "budget_mode": "decisions",
                       "max_policy_calls": selected[0]["benchmark_args"]["max_policy_calls"],
                       "replan_interval": selected[0]["benchmark_args"]["replan_interval"]}
    expected_budget["max_steps"] = expected_budget["max_policy_calls"] * expected_budget["replan_interval"]
    require(all(type(manifest[key]) is type(value) and manifest[key] == value
                for key, value in expected_budget.items()), "prior campaign seed/budget mismatch")
    require({p.name for p in files.root.glob("case-*")} == {c["case_id"] for c in pilots},
            "missing or additional prior claim; continuation must not retry it")
    audit.audit_source(files, manifest)
    verify_frozen_package(files.root, manifest)
    # Bind the actual frozen executable source and supplementals as well as its
    # independently copied runner inventory; no old package is imported.
    index = files.json("harness_source/index.json")
    for row in index["files"]:
        prefix = "frozen/" if manifest["schema_version"] == "starvla-rsi.campaign/v3" else "frozen/starVLA/evaluation/"
        files.raw(prefix + row["path"])
    model_schema = (manifest.get("model_source") or {}).get("schema_version")
    from benchkit.model_profiles import _source_namespace
    namespace = "starVLA" if model_schema is None else _source_namespace(model_schema)
    files.raw(f"frozen/{namespace}/__init__.py")
    for row in manifest.get("supplemental_runtime_files", []):
        files.raw("frozen/" + row["path"])
    selected_by_task = {c["episode"]["task"]: c for c in selected}
    carried = []
    evidence = {}
    for case, expected in zip(manifest["cases"], pilots):
        require({key: case[key] for key in expected} == expected,
                "prior case episode/descriptor/protocol differs from supported frozen pilot")
        task = case["episode"]["task"]
        require(task in selected_by_task, "prior task is outside selected scope")
        directory = case["case_id"]
        attempt = files.json(directory + "/attempt.json")
        require(attempt["case_index"] == case["index"] and attempt["attempt"] == 1
                and attempt["campaign_sha256"] == campaign_hash, "prior permanent attempt mismatch")
        outcome = files.json(directory + "/outcome.json")
        require(outcome["case_index"] == case["index"]
                and outcome["status"] in ("completed", "infrastructure_or_policy_error"),
                "unsupported prior outcome; attempt remains non-retryable")
        run_relative = directory + "/run"
        # Read lifecycle before invoking either auditor; active/incomplete runs
        # are unsupported carry-forward evidence, not unattempted tasks.
        status = files.json(run_relative + "/status.json")
        require(status["status"] in ("completed", "completed_with_errors"),
                "prior claim has no closed run; continuation must not retry it")
        checked = audit.audit_run(files.root / run_relative)
        for relative, expected_hash in checked["read_file_sha256"].items():
            key = run_relative + "/" + relative
            files.raw(key)
            require(files.hashes[key] == expected_hash, "prior closed evidence changed after audit")
        records = files.jsonl(run_relative + "/episodes.jsonl")
        require(len(records) == 1 and records[0]["episode"] == case["episode"], "prior run/episode mismatch")
        run_manifest = files.json(run_relative + "/manifest.json")
        require(run_manifest["benchmark"] == case["descriptor"], "prior run/descriptor mismatch")
        if benchmark == "simple_humanoid":
            from benchmarks.simBenchmarks.SIMPLE.bench_server.adapter import SimpleHumanoidBenchmarkAdapter
            adapter = SimpleHumanoidBenchmarkAdapter(**case["benchmark_args"])
        else:
            from benchmarks.simBenchmarks.Robocasa_365.bench_server.adapter import RoboCasa365BenchmarkAdapter
            adapter = RoboCasa365BenchmarkAdapter(**case["benchmark_args"])
        require(run_manifest["action_spec"] == adapter.action_spec.to_dict()
                and run_manifest["observation_spec"] == adapter.observation_spec.to_dict(),
                "prior run observation/action contract mismatch")
        require(all(run_manifest["policy"].get(key) == value for key, value in manifest["policy"].items()),
                "prior run/campaign policy identity mismatch")
        # Bind the actual transport contract, not just its display identity.
        # Do not open the actor endpoint or instantiate MailboxPolicy here.
        expected_parameters = {
            "directory": case["policy_endpoint"],
            "timeout_s": float(manifest["response_timeout_s"]),
            "prompt_sha256": manifest["policy_prompt_sha256"],
            "max_response_bytes": 65536,
            "max_chunk_length": manifest["replan_interval"],
            "history": "current_observation_only",
            "usage_source": "client_reported_or_unavailable",
            "evaluation_status": "experimental",
            "provider_call_limit_verified": False,
            "public_path_base": "mailbox_root",
            "absolute_paths_exported": False,
            "endpoint_blinding": "operator_must_supply_seed_independent_root_and_opaque_endpoint",
        }
        parameters = run_manifest["policy"].get("parameters")
        expected_version = "starvla-rsi.mailbox/v2"
        if manifest["schema_version"] == "starvla-rsi.campaign/v3":
            expected_version = "starvla-rsi.mailbox/v3"
            expected_parameters.update(model_profile=manifest["model_profile"],
                                       policy_stop_rule=manifest["policy_stop_rule"])
        require(run_manifest["policy"].get("version") == expected_version
                and isinstance(parameters, dict) and parameters.keys() == expected_parameters.keys()
                and all(type(parameters[key]) is type(value) and parameters[key] == value
                        for key, value in expected_parameters.items()),
                "prior mailbox policy parameters/version mismatch")
        require(all(type(run_manifest["protocol"][key]) is type(value)
                    and run_manifest["protocol"][key] == value for key, value in expected_budget.items()),
                "prior run protocol budget mismatch")
        require(run_manifest["harness_source"]["inventory_sha256"] == manifest["harness_source"]["inventory_sha256"],
                "prior campaign/run source inventory mismatch")
        summary = files.json(run_relative + "/summary.json")
        require(outcome["summary"] == summary, "prior outcome/summary mismatch")
        completed = outcome["returncode"] == 0 and not outcome["timed_out"] and summary["errors"] == 0
        require((outcome["status"] == "completed") == completed, "prior outcome lifecycle mismatch")
        timing = progress.summarize_progress(files.root / run_relative)
        require(len(timing["episodes"]) == 1 and timing["episodes"][0]["episode"] == case["episode"],
                "prior progress episode mismatch")
        require(files.json(directory + "/progress.json") == timing, "prior stored/derived progress mismatch")
        result_path = next(p for p in checked["read_file_sha256"] if p.startswith("episodes/") and p.endswith("/result.json"))
        artifacts = run_relative + "/" + str(Path(result_path).parent / "artifacts")
        native_reset = verify_native_reset(files, artifacts, benchmark, selected_by_task[task], case)
        bound_artifacts = artifact_hashes(files, artifacts)
        for reference in records[0]["artifacts"].values():
            path = Path(reference)
            if path.is_absolute():
                require(path.is_relative_to(files.root / artifacts), "prior artifact reference escapes its private directory")
                relative = path.relative_to(files.root).as_posix()
            else:
                require(".." not in path.parts, "unsafe prior artifact reference")
                relative = str(Path(artifacts) / path)
            require(relative in bound_artifacts, "referenced prior artifact missing")
        evidence.update(bound_artifacts)
        carried.append({"task": task, "source_case_id": directory, "source_case_index": case["index"],
            "source_run": str(files.root / run_relative), "original_episode": case["episode"],
            "original_descriptor": case["descriptor"], "original_protocol": run_manifest["protocol"],
            "attempt_status": outcome["status"], "episode_status": records[0]["status"],
            "native_final_success": records[0]["success"], "original_error": records[0]["error"],
            "record_consistency": checked["record_consistency"], "native_reset": native_reset,
            "progress": timing["episodes"][0], "progress_sha256": files.hashes[directory + "/progress.json"],
            "record_auditor_sha256": checked["auditor"]["sha256"]})
    files.verify_unchanged()
    evidence.update(files.hashes)
    return {"campaign": str(files.root), "campaign_sha256": campaign_hash,
        "source_policy": manifest["policy"], "source_policy_prompt_sha256": manifest["policy_prompt_sha256"],
        "source_actor_isolation": manifest["actor_isolation"], "source_provider_accounting": manifest["provider_accounting"],
        "source_harness_inventory_sha256": manifest["harness_source"]["inventory_sha256"],
        "attempts": carried, "file_sha256": dict(sorted(evidence.items())),
        "evidence_scope": "campaign/claim/outcome/closed runner records/source inventories/progress and retained private artifact bytes; no mailbox or external native-asset paths followed",
        "caveats": ["Original pilot protocol, policy context and outcome remain unchanged; no homogeneous new score is asserted.",
                    "Record consistency and retained-byte binding are not native parity or execution attestation.",
                    "Reset artifacts are byte-bound; RoboCasa serialized NPY payload and SIMPLE reserialized environment configuration are not independently decoded/rehashed by this helper.",
                    "Provider identity/usage and actor isolation are not independently attested here."]}


def build_coverage(benchmark: str, task_scope: str, selected: list[dict], pilots: list[dict],
                   carry_forward_campaign: Path | None, *, auditor_repository: Path | None = None) -> tuple[list[dict], dict]:
    require((benchmark, task_scope) in (("robocasa365", "target50"), ("simple_humanoid", "released-12")),
            "unsupported expansion scope")
    expected_count = 50 if benchmark == "robocasa365" else 12
    require(len(selected) == len({c["episode"]["task"] for c in selected}) == expected_count,
            "selected scope must retain its complete distinct denominator")
    try:
        prior = None if carry_forward_campaign is None else read_prior(
            Path(carry_forward_campaign), benchmark, selected, pilots, auditor_repository=auditor_repository)
    except (KeyError, TypeError, OSError, StopIteration) as exc:
        raise ValueError(f"unsupported or missing carry-forward evidence; no retry is authorized: {exc}") from exc
    carried = {} if prior is None else {row["task"]: row for row in prior["attempts"]}
    cases, entries = [], []
    for selection_index, selected_case in enumerate(selected):
        task = selected_case["episode"]["task"]
        entry = {"selection_index": selection_index, "task": task,
                 "selected_episode": selected_case["episode"], "selected_descriptor": selected_case["descriptor"]}
        if task in carried:
            entry.update(disposition="carried_attempt", source_case_id=carried[task]["source_case_id"])
        else:
            case = copy.deepcopy(selected_case)
            case.update(index=len(cases), case_id=f"case-{len(cases):03d}")
            cases.append(case)
            entry.update(disposition="new_case", case_id=case["case_id"])
        entries.append(entry)
    return cases, {"schema_version": SCHEMA, "benchmark": benchmark, "task_scope": task_scope,
                   "selected_total": len(entries), "carried_attempts": len(carried),
                   "planned_new_cases": len(cases), "entries": entries, "carry_forward": prior,
                   "score_aggregation": "not_implemented; preserve original protocols and outcomes separately"}


def verify_coverage(output: Path, manifest: dict) -> None:
    """Rebuild and revalidate before creating a permanent new attempt claim."""
    from benchkit.campaigns.workflow import cases_for

    ledger = manifest["coverage_ledger"]
    prior = ledger["carry_forward"]
    selected = cases_for(manifest["benchmark"], manifest["max_policy_calls"],
                         manifest["replan_interval"], task_scope=manifest["task_scope"])
    pilots = cases_for(manifest["benchmark"], manifest["max_policy_calls"], manifest["replan_interval"])
    cases, expected = build_coverage(manifest["benchmark"], manifest["task_scope"], selected, pilots,
        None if prior is None else Path(prior["campaign"]), auditor_repository=output / "frozen")
    require(ledger == expected, "coverage ledger or bound prior evidence changed")
    actual = [{key: value for key, value in case.items() if key != "policy_endpoint"} for case in manifest["cases"]]
    require(actual == cases and manifest["planned_cases"] == len(cases),
            "executable selection changed or includes a carried task")
    require(all(manifest[key] == expected[key] for key in ("selected_total", "carried_attempts", "planned_new_cases")),
            "coverage denominator counters changed")
    require(manifest["seed"] == selected[0]["episode"]["seed"] and manifest["budget_mode"] == "decisions"
            and manifest["max_steps"] == manifest["max_policy_calls"] * manifest["replan_interval"],
            "expansion seed/budget changed")