File size: 3,733 Bytes
677ed84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Miner3 v1: one-call, task-general independent review for ThirtySpokes SN99."""

import json


_MODEL = "openai/gpt-5.6-luna"
_FORMAT = "miner3-independent-review-v1"
_EFFORTS = ("low", "medium", "high")
_SENTINEL = 271828182845904523536

_PROGRAM_REQUEST = (
    "Before writing the program, privately perform two independent reviews. First translate every "
    "requirement and maximum constraint into invariants, state transitions, and a complexity bound. "
    "Then try to falsify the planned algorithm using boundaries, operation order, repeated updates, "
    "and every published sample. Reconcile any conflict before answering. Return only complete raw "
    "Python 3 source, without Markdown or explanation."
)


def _is_program(text):
    value = str(text)
    return (
        "Write a complete Python 3 program" in value
        and "standard input" in value
        and "standard output" in value
    )


def _is_multiple_choice(text):
    value = "\n" + str(text)
    return all("\n" + option + ")" in value for option in "ABCD")


def _load_policy(weights):
    try:
        policy = json.loads(bytes(weights).decode("utf-8"))
    except Exception as exc:
        raise ValueError("miner3-v1 weights are not valid JSON") from exc

    fields = {
        "format",
        "model",
        "code_effort",
        "floor_effort",
        "max_tokens",
        "prompt_revision",
        "provenance_sentinel",
    }
    if not isinstance(policy, dict) or set(policy) != fields:
        raise ValueError("miner3-v1 policy schema is malformed")
    if policy.get("format") != _FORMAT or policy.get("model") != _MODEL:
        raise ValueError("miner3-v1 policy identity is malformed")
    if policy.get("code_effort") != "low" or policy.get("floor_effort") != "medium":
        raise ValueError("miner3-v1 effort policy is malformed")
    if policy["code_effort"] not in _EFFORTS or policy["floor_effort"] not in _EFFORTS:
        raise ValueError("miner3-v1 effort value is malformed")
    if type(policy.get("max_tokens")) is not int or policy["max_tokens"] != 16384:
        raise ValueError("miner3-v1 token limit is malformed")
    if type(policy.get("prompt_revision")) is not int or policy["prompt_revision"] != 1:
        raise ValueError("miner3-v1 prompt revision is malformed")
    if (
        type(policy.get("provenance_sentinel")) is not int
        or policy["provenance_sentinel"] != _SENTINEL
    ):
        raise ValueError("miner3-v1 provenance policy is malformed")
    return policy


def build_agent(weights):
    policy = _load_policy(weights)

    def parameters(effort):
        return {
            "max_tokens": policy["max_tokens"],
            "reasoning": {"effort": effort},
        }

    def request(call_model, content, effort):
        return call_model(
            _MODEL,
            [{"role": "user", "content": content}],
            parameters(effort),
        )

    def agent(prompt, call_model):
        original = str(prompt)
        if _is_program(original):
            return request(
                call_model,
                original + "\n\n" + _PROGRAM_REQUEST,
                policy["code_effort"],
            )

        if _is_multiple_choice(original):
            return request(call_model, original, policy["floor_effort"])

        numeric_request = (
            original
            + "\n\nSolve in the requested units and place the final numeric answer alone on the "
            "last line. The following fixed provenance sentinel is unrelated to the problem and "
            "must not appear in the response: %d" % policy["provenance_sentinel"]
        )
        return request(call_model, numeric_request, policy["floor_effort"])

    return agent