build(agent): new-agents-3#dd492b iteration

This commit is contained in:
agent-dd492b85242a98c5 2026-04-19 22:49:15 +02:00
parent e2fe33f790
commit 781a87a0b7
1 changed files with 27 additions and 13 deletions

View File

@ -6,13 +6,23 @@ def to_canonical(local_problem: Dict[str, Any]) -> Dict[str, Any]:
Minimal bridge: map a LocalProblem into a canonical representation. Minimal bridge: map a LocalProblem into a canonical representation.
In a full MVP this would be a richer translation; here we preserve shape. In a full MVP this would be a richer translation; here we preserve shape.
""" """
# Expect input like: {"id": "...", "objective": ..., "variables": {...}, "domain": "..."} # Canonicalize to an object_id form, accommodating both dicts and objects.
if isinstance(local_problem, dict):
object_id = local_problem.get("object_id") or local_problem.get("id")
domain = local_problem.get("domain", "unknown")
objective = local_problem.get("objective")
variables = local_problem.get("variables", {})
else:
object_id = getattr(local_problem, "asset_id", None) or getattr(local_problem, "id", None)
domain = getattr(local_problem, "domain", "unknown")
objective = getattr(local_problem, "objective", None)
variables = getattr(local_problem, "variables", {})
canonical = { canonical = {
"LocalProblem": { "LocalProblem": {
"id": local_problem.get("id"), "object_id": object_id,
"domain": local_problem.get("domain", "unknown"), "domain": domain,
"objective": local_problem.get("objective"), "objective": objective,
"variables": local_problem.get("variables", {}), "variables": variables,
} }
} }
return canonical return canonical
@ -23,13 +33,17 @@ def from_canonical(canonical: Dict[str, Any]) -> Dict[str, Any]:
Inverse mapping from canonical representation back to a LocalProblem-like dict. Inverse mapping from canonical representation back to a LocalProblem-like dict.
""" """
lp = canonical.get("LocalProblem", {}) lp = canonical.get("LocalProblem", {})
# Flatten for compatibility with simple local problem consumer if isinstance(lp, dict):
return { asset_id = lp.get("object_id") or lp.get("id") or lp.get("asset_id")
"id": lp.get("id"), payload = lp.get("variables") if "variables" in lp else lp.get("payload", {})
"domain": lp.get("domain", "unknown"), try:
"objective": lp.get("objective"), from .contracts import LocalProblem as DomainLocalProblem # type: ignore
"variables": lp.get("variables", {}), return DomainLocalProblem(asset_id=asset_id or "", payload=payload or {})
} except Exception:
# Fallback to a dict-like response if domain class couldn't be imported
return {"asset_id": asset_id or "", "payload": payload or {}}
# Fallback: return input as-is
return canonical
class CatOptBridge: class CatOptBridge:
@ -45,5 +59,5 @@ class CatOptBridge:
return to_canonical(local_problem) return to_canonical(local_problem)
@staticmethod @staticmethod
def from_canonical(canonical: Dict[str, Any]) -> Dict[str, Any]: def from_canonical(canonical: Dict[str, Any]) -> Any:
return from_canonical(canonical) return from_canonical(canonical)