"""Retain the original task alongside the official Verifiers compaction summary.""" import ast import hashlib import importlib.metadata import importlib.util import py_compile from pathlib import Path UPSTREAM_SHA256 = "0de63a0982c4b2fefebc99b6eac4f699168f1eedd6db2c22661bc5affa62e878" ORIGINAL = ' rebuilt = [*system, {"role": "user", "content": framed}]' REPLACEMENT = ''' # Keep the exact task through every compaction, not just its summary. original_task = next( (message for message in messages if message.get("role") == "user"), None, ) rebuilt = [ *system, *([original_task] if original_task is not None else []), {"role": "user", "content": framed}, ]''' def patch_source(source: str) -> str: upstream = source.replace(REPLACEMENT, ORIGINAL) if hashlib.sha256(upstream.encode()).hexdigest() != UPSTREAM_SHA256: raise RuntimeError("Upstream compactor changed; refusing an unreviewed patch") if upstream.count(ORIGINAL) != 1: raise RuntimeError("Expected exactly one compaction rebuild") updated = upstream.replace(ORIGINAL, REPLACEMENT) ast.parse(updated) return updated def install(): if importlib.metadata.version("verifiers") != "0.4.1.dev12": raise RuntimeError("Task-retention patch requires reviewed Verifiers 0.4.1.dev12") package = importlib.util.find_spec("verifiers") path = Path(package.origin).parent / "v1/harnesses/utils/compaction.py" source = path.read_text() updated = patch_source(source) if source != updated: path.write_text(updated) for cached in (path.parent / "__pycache__").glob("compaction.*.pyc"): cached.unlink() py_compile.compile(str(path), doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH) print("Installed compaction task retention (original task + native summary)") if __name__ == "__main__": install()