Download Biomanus-V2/Experiment1/scripts/bind_generated_artifacts.py from czty/BioOcto: direct link, hf CLI and curl.
- Browser
- Download file 7.8 kB
-
https://huggingface.co/datasets/czty/BioOcto/resolve/main/Biomanus-V2/Experiment1/scripts/bind_generated_artifacts.py
- Command line
-
hf download hf://datasets/czty/BioOcto/Biomanus-V2/Experiment1/scripts/bind_generated_artifacts.py
-
curl -L -o bind_generated_artifacts.py https://huggingface.co/datasets/czty/BioOcto/resolve/main/Biomanus-V2/Experiment1/scripts/bind_generated_artifacts.py
7.8 kB
| #!/usr/bin/env python3 | |
| """Bind frozen generation artifacts to curated cases for T0-T5 evaluation.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from common import read_json, sha256_file, write_json | |
| from generate_wrapper import normalize_ir_shape, validate_ir_minimal | |
| def relative_to_root(path: Path, root: Path) -> str: | |
| return str(path.resolve().relative_to(root.resolve())) | |
| def compile_against_target_interface(case: dict, raw_ir: dict) -> tuple[dict, list[str]]: | |
| """Apply only the frozen, method-neutral endpoint binding contract.""" | |
| compiled = json.loads(json.dumps(raw_ir)) | |
| changes: list[str] = [] | |
| expected_endpoint = case["target_endpoint"] | |
| if (compiled.get("endpoint") or {}).get("name") != expected_endpoint: | |
| compiled.setdefault("endpoint", {})["name"] = expected_endpoint | |
| changes.append("endpoint.name set from target interface") | |
| executable = case["native_executables"][0] | |
| tool = compiled.setdefault("tool", {}) | |
| if tool.get("executable") != executable: | |
| tool["executable"] = executable | |
| changes.append("tool.executable set from target interface") | |
| subcommand = case.get("target_subcommand") | |
| if tool.get("subcommand") != subcommand: | |
| tool["subcommand"] = subcommand | |
| changes.append("tool.subcommand set from target interface") | |
| original_by_name = {argument.get("name"): argument for argument in compiled.get("arguments") or []} | |
| normalized_arguments = [] | |
| positional_index = 0 | |
| for parameter in case["target_interface"]["parameters"]: | |
| name = parameter["name"] | |
| argument = json.loads(json.dumps(original_by_name.get(name) or {})) | |
| before = json.dumps(argument, sort_keys=True) | |
| argument.update( | |
| { | |
| "name": name, | |
| "value_type": parameter["value_type"], | |
| "required": bool(parameter["required"]), | |
| "repeat": bool(argument.get("repeat", False)), | |
| "description": argument.get("description") or f"Target-interface parameter {name}.", | |
| } | |
| ) | |
| if "default" in parameter: | |
| argument["default"] = parameter["default"] | |
| binding = parameter.get("native_binding") | |
| if binding in {"stdout", "stderr"}: | |
| argument.update({"flag": None, "kind": "option", "position": None, "pass_to_cli": False}) | |
| elif isinstance(binding, str) and binding.startswith("-"): | |
| argument.update({"flag": binding, "kind": "option", "position": None, "pass_to_cli": True}) | |
| elif binding == "positional": | |
| argument.update({"flag": None, "kind": "positional", "position": positional_index, "pass_to_cli": True}) | |
| positional_index += 1 | |
| normalized_arguments.append(argument) | |
| if json.dumps(argument, sort_keys=True) != before: | |
| changes.append(f"argument.{name} normalized from target interface") | |
| compiled["arguments"] = normalized_arguments | |
| outputs = list(compiled.get("outputs") or []) | |
| for parameter in case["target_interface"]["parameters"]: | |
| capture = parameter.get("native_binding") | |
| if capture not in {"stdout", "stderr"}: | |
| continue | |
| name = parameter["name"] | |
| port = next((item for item in outputs if item.get("binding") == name), None) | |
| if port is None: | |
| port = {"name": name, "artifact_type": "text", "binding": name, "assertions": []} | |
| outputs.append(port) | |
| changes.append(f"outputs.{name} added from target interface") | |
| if port.get("capture") != capture: | |
| port["capture"] = capture | |
| changes.append(f"outputs.{name}.capture set to {capture}") | |
| compiled["outputs"] = outputs | |
| target_plan = case.get("target_interface", {}).get("execution_plan") | |
| if target_plan and compiled.get("execution_plan") != target_plan: | |
| compiled["execution_plan"] = json.loads(json.dumps(target_plan)) | |
| changes.append("execution_plan set from frozen target interface") | |
| return compiled, changes | |
| def main() -> int: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--manifest", type=Path, required=True) | |
| parser.add_argument("--contract-ir-dir", type=Path, required=True) | |
| parser.add_argument("--compiled-ir-dir", type=Path, required=True) | |
| parser.add_argument("--direct-prompt-dir", type=Path, required=True) | |
| parser.add_argument("--case-id", action="append") | |
| parser.add_argument("--out", type=Path, required=True) | |
| args = parser.parse_args() | |
| source = read_json(args.manifest.resolve()) | |
| root = args.manifest.resolve().parent.parent.parent | |
| selected = set(args.case_id or []) | |
| items = [] | |
| checks = [] | |
| for original in source["items"]: | |
| if selected and original["case_id"] not in selected: | |
| continue | |
| case = json.loads(json.dumps(original)) | |
| case_id = case["case_id"] | |
| ir_path = args.contract_ir_dir.resolve() / f"{case_id}.json" | |
| direct_path = args.direct_prompt_dir.resolve() / f"{case_id}.py" | |
| missing = [str(path) for path in (ir_path, direct_path) if not path.is_file()] | |
| if missing: | |
| raise SystemExit(f"Missing generated artifacts for {case_id}: {missing}") | |
| raw_ir, shape_changes = normalize_ir_shape(read_json(ir_path)) | |
| errors = validate_ir_minimal(raw_ir) | |
| raw_interface_deviations = [] | |
| expected_endpoint = case["target_endpoint"] | |
| expected_arguments = set(case.get("endpoint_arguments") or []) | |
| actual_endpoint = (raw_ir.get("endpoint") or {}).get("name") | |
| actual_arguments = {argument.get("name") for argument in raw_ir.get("arguments") or []} | |
| if actual_endpoint != expected_endpoint: | |
| raw_interface_deviations.append( | |
| f"endpoint mismatch: expected {expected_endpoint}, got {actual_endpoint}" | |
| ) | |
| if actual_arguments != expected_arguments: | |
| raw_interface_deviations.append( | |
| "argument mismatch: expected " | |
| f"{sorted(expected_arguments)}, got {sorted(str(value) for value in actual_arguments)}" | |
| ) | |
| compiled_ir, changes = compile_against_target_interface(case, raw_ir) | |
| compiled_path = args.compiled_ir_dir.resolve() / f"{case_id}.json" | |
| write_json(compiled_path, compiled_ir) | |
| compiled_errors = validate_ir_minimal(compiled_ir) | |
| errors.extend(f"compiled IR: {error}" for error in compiled_errors) | |
| case["contract_ir"] = relative_to_root(compiled_path, root) | |
| case["raw_contract_ir"] = relative_to_root(ir_path, root) | |
| case.setdefault("generated_wrappers", {})["direct_prompt"] = relative_to_root(direct_path, root) | |
| items.append(case) | |
| checks.append( | |
| { | |
| "case_id": case_id, | |
| "contract_ir_errors": errors, | |
| "raw_interface_deviations": raw_interface_deviations, | |
| "interface_compilation_changes": [*shape_changes, *changes], | |
| "raw_ir_sha256": sha256_file(ir_path), | |
| "compiled_ir_sha256": sha256_file(compiled_path), | |
| } | |
| ) | |
| if not items: | |
| raise SystemExit("No cases selected") | |
| payload = { | |
| "metadata": { | |
| "schema_version": "0.1", | |
| "source_manifest": relative_to_root(args.manifest, root), | |
| "case_count": len(items), | |
| "artifact_binding_checks": checks, | |
| }, | |
| "items": items, | |
| } | |
| write_json(args.out, payload) | |
| print(json.dumps({"output": str(args.out), "case_count": len(items), "checks": checks}, indent=2)) | |
| return 0 if all(not check["contract_ir_errors"] for check in checks) else 2 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |