File size: 6,304 Bytes
4a9362b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Absolute resource admission and an exclusive, bounded optimizer-update ledger."""

from __future__ import annotations

import json
import os
import socket
import uuid
from pathlib import Path
from typing import Any, Literal

import psutil

from e_jepa_ttc.artifacts.risk_geometry_v10 import atomic_json
from e_jepa_ttc.artifacts.simplex_t_preflight import resource_headroom

TECHNICAL_UPDATE_CAP = 1125


class ExclusiveLease:
    """Never steal a stale-looking lease or remove another owner's file."""

    def __init__(self, path: Path) -> None:
        self.path = path
        self.owner = uuid.uuid4().hex
        self.acquired = False

    def __enter__(self) -> ExclusiveLease:
        self.path.parent.mkdir(parents=True, exist_ok=True)
        with self.path.open("x", encoding="utf-8") as stream:
            json.dump(
                {"owner": self.owner, "pid": os.getpid(), "host": socket.gethostname()}, stream
            )
            stream.flush()
            os.fsync(stream.fileno())
        self.acquired = True
        return self

    def __exit__(self, *exception: object) -> Literal[False]:
        if self.acquired:
            state = json.loads(self.path.read_text(encoding="utf-8"))
            if state["owner"] != self.owner:
                raise RuntimeError("lease owner changed; refusing removal")
            self.path.unlink()
            self.acquired = False
        return False


def admitted(written_volumes: list[Path]) -> dict[str, Any]:
    """Read process-tree memory and exact free bytes; never ratio-based admission."""
    process = psutil.Process()
    try:
        rss = process.memory_info().rss + sum(
            child.memory_info().rss for child in process.children(recursive=True)
        )
        available = psutil.virtual_memory().available
        free = [psutil.disk_usage(str(path)).free for path in written_volumes]
    except (psutil.Error, OSError) as error:
        return {"has_headroom": False, "reasons": [f"RESOURCE_COUNTER_UNAVAILABLE:{error}"]}
    return {
        **resource_headroom(
            available_ram=available, process_tree_rss=rss, written_volume_free=free
        ),
        "process_tree_rss_bytes": rss,
        "host_available_bytes": available,
        "written_volume_free_bytes": free,
    }


class TechnicalBudget:
    """Charge unique technical operations before execution, never refund crashes.

    Reservations are conservative upper bounds, not evidence of executed updates.
    The campaign must use one persistent path across output directories.
    """

    def __init__(self, path: Path) -> None:
        self.path = path

    def reserve(self, key: str, updates: int) -> dict[str, Any]:
        """Refuse duplicates; the cap includes retained failures and consumer-binding QA."""
        if not key or type(updates) is not int or not 1 <= updates <= TECHNICAL_UPDATE_CAP:
            raise ValueError("invalid technical reservation")
        with ExclusiveLease(self.path.with_suffix(".lock")):
            state = (
                json.loads(self.path.read_text(encoding="utf-8"))
                if self.path.exists()
                else {"schema": "simplex_t_technical_budget_v1", "reservations": {}}
            )
            if state.get("schema") != "simplex_t_technical_budget_v1":
                raise ValueError("unrecognized technical budget schema")
            reservations = state["reservations"]
            if any(type(value) is not int or value < 1 for value in reservations.values()):
                raise ValueError("invalid saved technical reservation")
            if key in reservations:
                raise ValueError("technical operation already reserved; do not repeat")
            if sum(reservations.values()) + updates > TECHNICAL_UPDATE_CAP:
                raise ValueError("technical budget exceeded")
            reservations[key] = updates
            atomic_json(self.path, state)
            return state


class UpdateLedger:
    """Reserve only a frozen graph; persist counted progress under a writer lease."""

    def __init__(self, path: Path, graph: dict[str, int]) -> None:
        if any(updates != 2500 for updates in graph.values()) or len(graph) > 84:
            raise ValueError("unregistered scientific endpoint graph")
        self.path, self.graph = path, graph

    def transaction(self, operation: str, key: str, amount: int = 0) -> dict[str, Any]:
        """Technical increments and monotonic fit counters cannot create new arms."""
        if type(amount) is not int or amount < 0:
            raise ValueError("nonnegative integer updates required")
        with ExclusiveLease(self.path.with_suffix(".lock")):
            state = (
                json.loads(self.path.read_text(encoding="utf-8"))
                if self.path.exists()
                else {"graph": self.graph, "fits": {}, "technical_updates": 0}
            )
            if state["graph"] != self.graph:
                raise ValueError("cannot change frozen fit graph during resume")
            if operation == "technical":
                state["technical_updates"] += amount
            elif operation == "reserve":
                if key not in self.graph or key in state["fits"]:
                    raise ValueError("unknown or already-reserved fit")
                state["fits"][key] = {"updates": 0, "status": "RESERVED"}
            elif operation == "progress":
                if key not in state["fits"]:
                    raise ValueError("fit not reserved")
                fit = state["fits"][key]
                if amount < fit["updates"] or amount > 2500:
                    raise ValueError("nonmonotonic or excessive update endpoint")
                fit.update(updates=amount, status="COMPLETED" if amount == 2500 else "PARTIAL")
            else:
                raise ValueError("unknown ledger operation")
            reserved = len(state["fits"]) * 2500
            if state["technical_updates"] > TECHNICAL_UPDATE_CAP or reserved > 210000:
                raise ValueError("registered scientific/technical budget exceeded")
            if reserved + state["technical_updates"] > 250000:
                raise ValueError("absolute optimizer update cap exceeded")
            atomic_json(self.path, state)
            return state