File size: 10,885 Bytes
767a051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""Independent Fraction baseline for the 0.4 corrective rebuild.

This module intentionally imports no ``tom_world03`` or ``tom_world04r`` code.
It independently parses rational records, linearizes expressions, selects exact
roots, merges simultaneous operations, and constructs the semantic chain.
"""
from __future__ import annotations

from collections import defaultdict
from fractions import Fraction
import hashlib
import json
from typing import Any, Mapping


def _canonical(value: Any) -> bytes:
    return json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode("utf-8")


def _attach(record: Mapping[str, Any]) -> dict[str, Any]:
    out = dict(record)
    out.pop("content_hash", None)
    out["content_hash"] = "sha256:" + hashlib.sha256(_canonical(out)).hexdigest()
    return out


def _f(value: Any) -> Fraction:
    if isinstance(value, Fraction):
        return value
    if isinstance(value, bool):
        return Fraction(int(value), 1)
    if isinstance(value, int):
        return Fraction(value, 1)
    if isinstance(value, str):
        return Fraction(value)
    if isinstance(value, Mapping):
        return Fraction(int(value["num"]), int(value["den"]))
    raise TypeError(f"cannot parse Fraction from {type(value).__name__}")


def _q(value: Fraction) -> dict[str, int]:
    return {"num": value.numerator, "den": value.denominator}


def _qmap(values: Mapping[str, Fraction]) -> dict[str, dict[str, int]]:
    return {name: _q(values[name]) for name in sorted(values)}


def _interval(value: Mapping[str, Any]) -> tuple[Fraction, Fraction]:
    lo, hi = _f(value["lower"]), _f(value["upper"])
    if hi < lo:
        raise ValueError("baseline interval upper below lower")
    return lo, hi


def _linear(
    expr: Mapping[str, Any],
    start: Fraction,
    state: Mapping[str, Fraction],
    rates: Mapping[str, Fraction],
) -> tuple[Fraction, Fraction] | None:
    op = expr.get("op")
    if op == "const":
        return Fraction(0), _f(expr["value"])
    if op == "time":
        return Fraction(1), Fraction(0)
    if op == "field":
        name = str(expr["name"])
        rate = rates[name]
        return rate, state[name] - rate * start
    if op == "neg":
        inner = _linear(expr["value"], start, state, rates)
        return None if inner is None else (-inner[0], -inner[1])
    args = expr.get("args")
    if not isinstance(args, list) or len(args) != 2:
        raise ValueError("baseline binary expression requires two args")
    left = _linear(args[0], start, state, rates)
    right = _linear(args[1], start, state, rates)
    if left is None or right is None:
        return None
    if op == "add":
        return left[0] + right[0], left[1] + right[1]
    if op == "sub":
        return left[0] - right[0], left[1] - right[1]
    if op == "mul":
        if left[0] == 0:
            return right[0] * left[1], right[1] * left[1]
        if right[0] == 0:
            return left[0] * right[1], left[1] * right[1]
        return None
    raise ValueError(f"unsupported baseline expression op {op}")


def _state_at(
    start: Fraction,
    state: Mapping[str, Fraction],
    rates: Mapping[str, Fraction],
    time: Fraction,
) -> dict[str, Fraction]:
    return {name: state[name] + rates[name] * (time - start) for name in state}


def _support_ok(record: Mapping[str, Any], state: Mapping[str, Fraction]) -> bool:
    for name, bound in record.get("bounds", {}).items():
        lo, hi = _interval(bound)
        if name not in state or not (lo <= state[name] <= hi):
            return False
    return True


def _compatibility_ok(record: Mapping[str, Any], state: Mapping[str, Fraction]) -> bool:
    return all(name in state and state[name] == _f(expected) for name, expected in record.get("equals", {}).items())


def _merge(
    relations: list[Mapping[str, Any]],
    attribute: str,
    source: Mapping[str, Fraction],
) -> dict[str, Fraction]:
    grouped: dict[str, list[tuple[str, Fraction]]] = defaultdict(list)
    for relation in relations:
        operations = relation.get(attribute, [])
        if not isinstance(operations, list):
            raise ValueError(f"baseline {attribute} must be an array")
        for operation in operations:
            grouped[str(operation["field"])].append((str(operation["mode"]), _f(operation["value"])))
    result = dict(source)
    for field in sorted(grouped):
        if field not in result:
            raise ValueError(f"baseline operation names unknown field {field}")
        modes = {mode for mode, _ in grouped[field]}
        if len(modes) != 1:
            raise ValueError(f"baseline mixed modes on {field}")
        mode = next(iter(modes))
        values = [value for _, value in grouped[field]]
        if mode == "set":
            if any(value != values[0] for value in values[1:]):
                raise ValueError(f"baseline unequal set values on {field}")
            result[field] = values[0]
        elif mode == "add":
            result[field] = result[field] + sum(values, Fraction(0))
        elif mode == "xor":
            if result[field].denominator != 1 or any(value.denominator != 1 for value in values):
                raise ValueError("baseline xor requires integers")
            merged = 0
            for value in values:
                merged ^= value.numerator
            result[field] = Fraction(result[field].numerator ^ merged, 1)
        else:
            raise ValueError(f"unsupported baseline transition mode {mode}")
    return result


def trusted_piecewise_baseline(world: Mapping[str, Any]) -> dict[str, Any]:
    """Exhaustively execute the literal world with independent arithmetic."""
    horizon_start, horizon_end = _interval(world["horizon"])
    initial = world["initial_segment"]
    start = _f(initial["domain"]["lower"])
    state = {name: _f(value) for name, value in initial["start_state"].items()}
    rates = {name: _f(initial["rates"].get(name, {"num": 0, "den": 1})) for name in state}
    fired: set[str] = set(initial.get("fired_relations", []))
    supports = {record["id"]: record for record in world["supports"]}
    compatibilities = {record["id"]: record for record in world["compatibilities"]}
    relations = list(world["relations"])
    max_events = int(world.get("solver", {}).get("max_event_sets", 64))

    realized: list[dict[str, Any]] = []
    event_sets: list[dict[str, Any]] = []
    sequence = 0
    while True:
        candidates: list[tuple[Fraction, int, str, str, str, Mapping[str, Any]]] = []
        for relation in relations:
            ident = str(relation["id"])
            if ident in fired:
                continue
            if relation.get("fire_policy") != "once":
                raise ValueError("baseline supports only once fire policy")
            coeff = _linear(relation["expression"], start, state, rates)
            if coeff is None:
                raise ValueError(f"baseline relation {ident} is non-affine")
            slope, intercept = coeff
            if slope == 0:
                if intercept == 0:
                    raise ValueError(f"baseline relation {ident} is identically zero")
                continue
            root = -intercept / slope
            active_lo, active_hi = _interval(relation["active_time"])
            if not (start < root <= horizon_end and active_lo <= root <= active_hi):
                continue
            root_state = _state_at(start, state, rates, root)
            if not _support_ok(supports[relation["support_id"]], root_state):
                continue
            if not _compatibility_ok(compatibilities[relation["compatibility_id"]], root_state):
                continue
            candidates.append((
                root,
                int(relation.get("priority", 0)),
                ident,
                str(relation["event_id"]),
                str(relation["content_hash"]),
                relation,
            ))
        candidates.sort(key=lambda item: item[:5])
        if not candidates:
            end_state = _state_at(start, state, rates, horizon_end)
            realized.append({
                "sequence": sequence,
                "domain": {"lower": _q(start), "upper": _q(horizon_end)},
                "start_state": _qmap(state),
                "rates": _qmap(rates),
                "end_state": _qmap(end_state),
            })
            final_state = end_state
            break
        if len(event_sets) >= max_events:
            raise ValueError("baseline event-set budget exhausted")
        root = candidates[0][0]
        simultaneous = [item for item in candidates if item[0] == root]
        selected_relations = [item[5] for item in simultaneous]
        pre_state = _state_at(start, state, rates, root)
        post_state = _merge(selected_relations, "transition", pre_state)
        post_rates = _merge(selected_relations, "rate_transition", rates)
        realized.append({
            "sequence": sequence,
            "domain": {"lower": _q(start), "upper": _q(root)},
            "start_state": _qmap(state),
            "rates": _qmap(rates),
            "end_state": _qmap(pre_state),
        })
        relation_order = [str(item[2]) for item in simultaneous]
        fired.update(relation_order)
        event_sets.append({
            "event_time": _q(root),
            "event_order": [str(item[3]) for item in simultaneous],
            "relation_order": relation_order,
            "pre_state": _qmap(pre_state),
            "post_state": _qmap(post_state),
            "pre_rates": _qmap(rates),
            "post_rates": _qmap(post_rates),
            "fired_relations_after": sorted(fired),
        })
        sequence += 1
        start, state, rates = root, post_state, post_rates

    baseline_pin = world["corrected_v03_baseline"]
    semantic = {
        "schema": "TOM-CONTINUATION-SEMANTIC-CHAIN-0.4.1",
        "world_hash": world["content_hash"],
        "corrected_v03_zip_sha256": baseline_pin["archive_sha256"],
        "corrected_interval_sha256": baseline_pin["interval_py_sha256"],
        "realized_segments": realized,
        "event_sets": event_sets,
        "final_time": _q(horizon_end),
        "final_state": _qmap(final_state),
        "fired_relations": sorted(fired),
        "boundary_policy": "event times are solver outputs; final boundary is the declared world horizon",
    }
    semantic_hash = "sha256:" + hashlib.sha256(_canonical(semantic)).hexdigest()
    return _attach({
        "schema": "TOM-INDEPENDENT-PIECEWISE-BASELINE-0.4.1",
        "implementation": "standalone fractions.Fraction exhaustive event continuation",
        "world_hash": world["content_hash"],
        "event_set_count": len(event_sets),
        "realized_segment_count": len(realized),
        "semantic_chain": semantic,
        "semantic_chain_sha256": semantic_hash,
    })