From ace7978a08409372b3f823753daf69115a7c3be1 Mon Sep 17 00:00:00 2001 From: agent-ed374b2a16b664d2 Date: Wed, 15 Apr 2026 19:57:48 +0200 Subject: [PATCH] build(agent): molt-x#ed374b iteration --- .../contracts.py | 73 +++++++++++++++++++ .../reference_adapter.py | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/cosmosmesh_privacy_preserving_federated_/contracts.py diff --git a/src/cosmosmesh_privacy_preserving_federated_/contracts.py b/src/cosmosmesh_privacy_preserving_federated_/contracts.py new file mode 100644 index 0000000..e0be933 --- /dev/null +++ b/src/cosmosmesh_privacy_preserving_federated_/contracts.py @@ -0,0 +1,73 @@ +"""Contracts module for CosmosMesh MVP tests. + +This module provides minimal, test-friendly data structures for LocalProblem +and a small registry facade GraphOfContracts. The goal is to satisfy unit tests +without pulling in heavy dependencies. +""" + +from __future__ import annotations + +import json +from dataclasses import dataclass, field +from typing import Any, Dict, Optional + + +@dataclass +class LocalProblem: + """Minimal LocalProblem contract used by tests. + + Attributes: + agent_id: Unique identifier for the agent describing the problem. + variables: Mapping of decision variables and their current values. + objective: Dict describing the objective (could be coefficients, type, etc). + constraints: Dict describing constraints (types and limits). + version: Protocol version of this contract payload. + """ + + agent_id: str + variables: Dict[str, float] = field(default_factory=dict) + objective: Dict[str, Any] = field(default_factory=dict) + constraints: Dict[str, Any] = field(default_factory=dict) + version: str = "0.0.1" + + def to_dict(self) -> Dict[str, Any]: + return { + "agent_id": self.agent_id, + "variables": self.variables, + "objective": self.objective, + "constraints": self.constraints, + "version": self.version, + } + + def to_json(self) -> str: + return json.dumps(self.to_dict()) + + @classmethod + def from_json(cls, payload: str) -> "LocalProblem": + data = json.loads(payload) + return cls( + agent_id=data["agent_id"], + variables=data.get("variables", {}), + objective=data.get("objective", {}), + constraints=data.get("constraints", {}), + version=data.get("version", "0.0.1"), + ) + + +class GraphOfContracts: + """Tiny in-memory registry of contract names -> versions.""" + + def __init__(self) -> None: + self._registry: Dict[str, str] = {} + + def register(self, name: str, version: str) -> None: + self._registry[name] = version + + def get_version(self, name: str) -> Optional[str]: + return self._registry.get(name) + + def to_json(self) -> str: + return json.dumps(self._registry) + + +__all__ = ["LocalProblem", "GraphOfContracts"] diff --git a/src/cosmosmesh_privacy_preserving_federated_/reference_adapter.py b/src/cosmosmesh_privacy_preserving_federated_/reference_adapter.py index 3843d4f..732b403 100644 --- a/src/cosmosmesh_privacy_preserving_federated_/reference_adapter.py +++ b/src/cosmosmesh_privacy_preserving_federated_/reference_adapter.py @@ -12,7 +12,7 @@ bindings. from __future__ import annotations -from typing import Dict, Any +from typing import Dict, Any, Optional # Lightweight local DSL definitions (stand-ins for MVP scaffolding). from dataclasses import dataclass