Download adapter/src/collab_harbor/file_integrity.py from HuggingFaceSwarms/collaborative-intelligence-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 3.95 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/collaborative-intelligence-benchmark/resolve/main/adapter/src/collab_harbor/file_integrity.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/collaborative-intelligence-benchmark/adapter/src/collab_harbor/file_integrity.py
-
curl -L -o file_integrity.py https://huggingface.co/datasets/HuggingFaceSwarms/collaborative-intelligence-benchmark/resolve/main/adapter/src/collab_harbor/file_integrity.py
3.95 kB
| """Versioned protected-file history rules at original checker boundaries.""" | |
| from __future__ import annotations | |
| import threading | |
| from contextlib import contextmanager | |
| from pathlib import PurePosixPath | |
| INTEGRITY_POLICIES = frozenset({"legacy-v1", "normalized-protected-v2"}) | |
| _CHECKER_CONTEXT = threading.Lock() | |
| class IntegrityError(ValueError): | |
| """Unsafe or ambiguous integrity evidence cannot be graded silently.""" | |
| def edited_protected_test(edits: list, protected: list, policy: str) -> bool: | |
| """Detect protected edits, preserving historical raw-string behavior in v1.""" | |
| if policy == "legacy-v1": | |
| return any(event.get("path") in protected for event in edits) | |
| if policy != "normalized-protected-v2": | |
| raise IntegrityError("Unknown protected-test integrity policy") | |
| if not isinstance(edits, list) or not isinstance(protected, list): | |
| raise IntegrityError("Invalid protected-file edit history") | |
| def canonical(name): | |
| if not isinstance(name, str) or not name or "\0" in name: | |
| raise IntegrityError("Invalid file-edit history path") | |
| path = PurePosixPath(name) | |
| if path.is_absolute() or not path.parts: | |
| raise IntegrityError("Unsafe file-edit history path") | |
| parts = [] | |
| for part in path.parts: | |
| if part == "..": | |
| if not parts: | |
| raise IntegrityError("Unsafe file-edit history path") | |
| parts.pop() | |
| else: | |
| parts.append(part) | |
| if not parts: | |
| raise IntegrityError("Unsafe file-edit history path") | |
| return "/".join(parts) | |
| protected_paths = {canonical(name) for name in protected} | |
| edited = False | |
| for event in edits: | |
| if not isinstance(event, dict): | |
| raise IntegrityError("Invalid file-edit history entry") | |
| # Validate every entry even after finding an edit, so malformed later | |
| # history cannot disappear behind a short-circuiting positive result. | |
| edited |= canonical(event.get("path")) in protected_paths | |
| return edited | |
| def checker_integrity(policy: str): | |
| """Apply v2 only after the original takeover wrapper slices predecessor edits. | |
| The inherited checker makes its own shallow t2 and resets this flag using raw | |
| strings. Its module-local delegate is the narrow point where the successor's | |
| already sliced edit history can receive the selected rule. Pass that same t2 | |
| through so original judge caches and wrapper/final-clean behavior survive. | |
| Original code bindings are process-wide. Overlapping/nested policy contexts | |
| fail rather than letting one episode inherit another episode's policy. | |
| """ | |
| if policy not in INTEGRITY_POLICIES: | |
| raise IntegrityError("Unknown protected-test integrity policy") | |
| if not _CHECKER_CONTEXT.acquire(blocking=False): | |
| raise IntegrityError("Overlapping checker integrity contexts are not supported") | |
| module = None | |
| original = None | |
| try: | |
| if policy == "normalized-protected-v2": | |
| from collab_intel.harness.checkers_ext import inherited_takeover | |
| module = inherited_takeover | |
| original = module.run_checker | |
| def protected_delegate(transcript, specification): | |
| sandbox = transcript.get("sandbox") | |
| if sandbox is not None: | |
| if not isinstance(sandbox, dict): | |
| raise IntegrityError("Invalid sandbox integrity evidence") | |
| sandbox["model_edited_test_file"] = edited_protected_test( | |
| sandbox.get("edits"), sandbox.get("test_files"), policy) | |
| return original(transcript, specification) | |
| module.run_checker = protected_delegate | |
| yield | |
| finally: | |
| if module is not None and original is not None: | |
| module.run_checker = original | |
| _CHECKER_CONTEXT.release() | |