build(agent): molt-d#cb502d iteration

This commit is contained in:
agent-cb502d7656738cf6 2026-04-17 09:16:11 +02:00
parent b35ed8f4c1
commit bf27f8aa1f
1 changed files with 51 additions and 38 deletions

View File

@ -1,7 +1,10 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass, asdict from dataclasses import dataclass
from typing import Dict, Any, List from typing import Dict, Any, Optional, List
# Reuse core CatOpt primitives for minimal compatibility in MVP
from .catopt_bridge import LocalProblem, SharedVariable, DualVariable, PlanDelta
@dataclass @dataclass
@ -13,7 +16,23 @@ class LocalProblemEP:
data_contracts: Dict[str, Any] data_contracts: Dict[str, Any]
def to_dict(self) -> Dict[str, Any]: def to_dict(self) -> Dict[str, Any]:
return asdict(self) return {
"problem_id": self.problem_id,
"assets": self.assets,
"objective": self.objective,
"constraints": self.constraints,
"data_contracts": self.data_contracts,
}
@dataclass
class EnergiSignal:
channel: str
value: Any
version: int = 0
def to_dict(self) -> Dict[str, Any]:
return {"channel": self.channel, "value": self.value, "version": self.version}
@dataclass @dataclass
@ -21,71 +40,65 @@ class SharedVariableEP:
channel: str channel: str
version: int version: int
payload: Dict[str, Any] payload: Dict[str, Any]
def to_dict(self) -> Dict[str, Any]: def to_dict(self) -> Dict[str, Any]:
return {"channel": self.channel, "version": self.version, "payload": self.payload} return {"channel": self.channel, "version": self.version, "payload": self.payload}
@dataclass @dataclass
class DualVariableEP: class DualVariableEP:
channel: str channel: str
version: int version: int
payload: Dict[str, Any] payload: Dict[str, Any]
def to_dict(self) -> Dict[str, Any]: def to_dict(self) -> Dict[str, Any]:
return {"channel": self.channel, "version": self.version, "payload": self.payload} return {"channel": self.channel, "version": self.version, "payload": self.payload}
@dataclass @dataclass
class PlanDeltaEP: class PlanDeltaEP:
delta_id: str delta_id: str
changes: Dict[str, Any] changes: Dict[str, Any]
timestamp: float timestamp: float
def to_dict(self) -> Dict[str, Any]: def to_dict(self) -> Dict[str, Any]:
return {"delta_id": self.delta_id, "changes": self.changes, "timestamp": self.timestamp} return {"delta_id": self.delta_id, "changes": self.changes, "timestamp": self.timestamp}
class EnergiBridge: class EnergiBridge:
"""Minimal EnergiBridge for cross-domain interoperability (MVP). """Minimal EnergiBridge canonical bridge implementation.
Exposes a to_ir method that translates a local problem and its signals into Maps CosmosMesh primitives into a vendor-agnostic Intermediary Representation
a canonical IR suitable for adapters in other domains. suitable for cross-domain adapters.
""" """
def __init__(self) -> None: def to_energi(
pass self,
lp: LocalProblemEP,
shared: List[SharedVariableEP],
duals: List[DualVariableEP],
deltas: Optional[List[PlanDeltaEP]] = None,
) -> Dict[str, Any]:
data = {
"Version": "0.1",
"Objects": {
"LocalProblem": lp.to_dict(),
},
"Morphisms": [
{"Morphisms": {"SharedVariable": sv.to_dict()}} for sv in shared
] + [
{"Morphisms": {"DualVariable": dv.to_dict()}} for dv in duals
],
}
if deltas:
data["Objects"]["PlanDeltas"] = [p.to_dict() for p in deltas]
return data
# Backwards-compatible API expected by existing tests
def to_ir( def to_ir(
self, self,
lp: LocalProblemEP, lp: LocalProblemEP,
shared: List[SharedVariableEP], shared: List[SharedVariableEP],
duals: List[DualVariableEP], duals: List[DualVariableEP],
deltas: List[PlanDeltaEP], deltas: Optional[List[PlanDeltaEP]] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
morphs = [] return self.to_energi(lp, shared, duals, deltas)
for s in shared:
morphs.append({"Morphisms": {"SharedVariable": s.channel, "version": s.version, "payload": s.payload}})
for d in duals:
morphs.append({"Morphisms": {"DualVariable": d.channel, "version": d.version, "payload": d.payload}})
# Lightweight per-round timing/provenance metadata aligned with the MVP"
time_monoid = {"round": 1, "timestamp": None}
metadata = {"generated_by": "EnergiBridge", "timestamp": None}
return {
"Version": "0.1", __all__ = ["EnergiBridge", "EnergiLocalProblem", "EnergiSignal"]
"Objects": {
"LocalProblem": {
"problem_id": lp.problem_id,
"assets": lp.assets,
"objective": lp.objective,
"constraints": lp.constraints,
"data_contracts": lp.data_contracts,
},
"PlanDeltas": [p.to_dict() for p in deltas],
},
"Morphisms": morphs,
"TimeMonoid": time_monoid,
"Metadata": metadata,
}