File size: 2,097 Bytes
b1a18a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()