novaplan-decentralized-priv.../nova_plan/catopt_bridge.py

87 lines
2.6 KiB
Python

"""Minimal CatOpt-style bridge for NovaPlan MVP interoperability.
This module provides tiny, drop-in helpers to map NovaPlan core primitives
to a canonical representation that can be consumed by interoperability bridges
or other runtimes in the ecosystem. The goal is to be lightweight and test-friendly
while not impacting the core MVP tests.
"""
from __future__ import annotations
from typing import Any, Dict
from dataclasses import dataclass
from .planner import LocalProblem
from .contracts import PlanDelta
@dataclass
class ObjectI:
"""Canonical Object interface used by CatOpt bridges."""
id: str
payload: Dict[str, Any]
@dataclass
class Morphism:
"""Canonical Morphism interface used by CatOpt bridges."""
source: str
target: str
data: Dict[str, Any]
contract_id: str
def to_object(local_problem: LocalProblem) -> ObjectI:
"""Convert a LocalProblem into a canonical Object representation.
The object is intentionally minimal: it contains the id, the local variables,
and basic metadata that downstream adapters can enrich.
"""
payload = {"variables": dict(local_problem.variables)}
return ObjectI(id=local_problem.id, payload=payload)
def to_morphism(plan_delta: PlanDelta) -> Morphism:
"""Convert a PlanDelta into a canonical Morphism representation.
This helper is a thin wrapper that exposes a Morphism data class.
Used primarily for internal interoperability and tests.
"""
return Morphism(
source=plan_delta.agent_id,
target="global",
data=plan_delta.delta,
contract_id=getattr(plan_delta, "contract_id", "default"),
)
def delta_to_morphism(
plan_delta: PlanDelta,
contract_id: str = "default",
source: str | None = None,
target: str = "global",
) -> Morphism:
"""Convert a PlanDelta into a Morphism, with optional overrides.
- If source is None, uses plan_delta.agent_id
- contract_id defaults to "default" unless explicitly provided
- target defaults to "global" unless overridden
- data is plan_delta.delta
"""
return Morphism(
source=(source if source is not None else plan_delta.agent_id),
target=target,
data=plan_delta.delta,
contract_id=contract_id,
)
def bridge_demo(local_problem: LocalProblem, plan_delta: PlanDelta) -> Dict[str, Any]:
"""Tiny end-to-end demo helper that returns both object and morphism representations.
This function is used by examples or quick tests to verify the bridge wiring.
"""
obj = to_object(local_problem)
morph = delta_to_morphism(plan_delta)
return {"object": obj, "morphism": morph}