"""Read-only source-level probe; no provider calls or Hermes installation required.
Run: python3 compression-relay-probe-2026-10-20.py
Prints JSON to stdout. Not an end-to-end compression/recall benchmark.
"""
import ast
import hashlib
import json
import platform
import urllib.request

REVISIONS = {'fixed': '12a33425db2cfcf07c0864ef4af8fa1bcb9dbc12', 'release': 'v0.22.2'}
receipts = {}
functions = {}
for label, revision in REVISIONS.items():
    url = f'https://raw.githubusercontent.com/NousResearch/hermes-agent/{revision}/agent/auxiliary_wire.py'
    with urllib.request.urlopen(url, timeout=30) as response:
        raw = response.read()
    tree = ast.parse(raw.decode())
    nodes = [node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == 'relay_chat_metadata']
    receipts[label] = {'revision': revision, 'url': url, 'sha256': hashlib.sha256(raw).hexdigest(), 'helperPresent': bool(nodes)}
    if nodes:
        # Only execute the inspected pure dict-copy function, not the module.
        expected = ast.parse('def relay_chat_metadata(metadata: dict) -> dict:\n return {**metadata, "api_mode": "chat_completions"}').body[0]
        assert isinstance(expected, ast.FunctionDef)
        body = [n for n in nodes[0].body if not isinstance(n, ast.Expr)]
        assert ast.dump(ast.Module(body=body, type_ignores=[])) == ast.dump(ast.Module(body=expected.body, type_ignores=[])), 'Unexpected helper body; inspect before running'
        namespace = {}
        exec(compile(ast.Module(body=[nodes[0]], type_ignores=[]), url, 'exec'), namespace)
        functions[label] = namespace['relay_chat_metadata']
assert receipts['fixed']['helperPresent'] and not receipts['release']['helperPresent']
rows = []
for mode in ['codex_responses', 'anthropic_messages', 'chat_completions']:
    source = {'api_mode': mode, 'provider': 'test-only', 'model': 'not-called'}
    before = dict(source)
    result = functions['fixed'](source)
    passed = result == {**before, 'api_mode': 'chat_completions'} and source == before and result is not source
    rows.append({'input': before, 'output': result, 'inputUnchanged': source == before, 'pass': passed})
assert all(row['pass'] for row in rows)
print(json.dumps({'scope': 'pure helper only; no live compression or recall measured', 'python': platform.python_version(), 'platform': platform.platform(), 'sources': receipts, 'cases': rows}, indent=2))
