|
| 1 | +"""Self-improving loop: generate → critique → refine → execute. |
| 2 | +
|
| 3 | +Wraps VisionPlanner in a cycle that scores plans before executing, |
| 4 | +refining them until they pass the internal critic or hit exit conditions. |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import json |
| 9 | +import logging |
| 10 | +import time |
| 11 | +from dataclasses import dataclass, field |
| 12 | +from typing import Any, Optional, Callable |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | +CRITIQUE_SYSTEM = """You are a plan critic for MOSO AI. Given a user goal and a proposed plan, |
| 17 | +score it 0.0-1.0 and give brief feedback. Return ONLY valid JSON: |
| 18 | +{"score": 0.8, "feedback": "one sentence of what to improve"} |
| 19 | +Rules: score >= 0.75 means the plan is good enough to execute. |
| 20 | +Penalize: missing steps, wrong tool choices, unclear targets, unsafe actions.""" |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class ContextBoundary: |
| 25 | + goal: str |
| 26 | + allowed_tools: list[str] = field(default_factory=list) |
| 27 | + max_seconds: float = 60.0 |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class ExitCondition: |
| 32 | + max_cycles: int = 3 |
| 33 | + min_critic_score: float = 0.75 |
| 34 | + hard_deadline_s: float = 120.0 |
| 35 | + |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class LoopResult: |
| 39 | + plan: Any = None |
| 40 | + cycles: int = 0 |
| 41 | + final_score: float = 0.0 |
| 42 | + critiques: list[dict] = field(default_factory=list) |
| 43 | + timed_out: bool = False |
| 44 | + |
| 45 | + |
| 46 | +class SelfImprovingLoop: |
| 47 | + def __init__(self, llm: Any = None, exit_condition: Optional[ExitCondition] = None): |
| 48 | + self._llm = llm |
| 49 | + self._exit = exit_condition or ExitCondition() |
| 50 | + |
| 51 | + def run( |
| 52 | + self, |
| 53 | + goal: str, |
| 54 | + generate_fn: Callable[[ContextBoundary], Any], |
| 55 | + execute_fn: Optional[Callable[[Any], Any]] = None, |
| 56 | + ) -> LoopResult: |
| 57 | + boundary = ContextBoundary(goal=goal) |
| 58 | + start = time.time() |
| 59 | + result = LoopResult() |
| 60 | + |
| 61 | + plan = generate_fn(boundary) |
| 62 | + result.plan = plan |
| 63 | + |
| 64 | + for cycle in range(self._exit.max_cycles): |
| 65 | + # Check hard deadline |
| 66 | + if time.time() - start > self._exit.hard_deadline_s: |
| 67 | + result.timed_out = True |
| 68 | + logger.warning("Loop timed out after %.0fs", self._exit.hard_deadline_s) |
| 69 | + break |
| 70 | + |
| 71 | + score, feedback = self._critique(goal, plan) |
| 72 | + result.critiques.append({"cycle": cycle, "score": score, "feedback": feedback}) |
| 73 | + result.final_score = score |
| 74 | + result.cycles = cycle + 1 |
| 75 | + |
| 76 | + if score >= self._exit.min_critic_score: |
| 77 | + logger.info("Plan passed critique on cycle %d (score %.2f)", cycle + 1, score) |
| 78 | + break |
| 79 | + |
| 80 | + # Refine: re-generate with critique feedback appended to goal |
| 81 | + refined_goal = f"{goal}\n\n[CRITIC FEEDBACK]: {feedback}" |
| 82 | + refined_boundary = ContextBoundary(goal=refined_goal) |
| 83 | + plan = generate_fn(refined_boundary) |
| 84 | + result.plan = plan |
| 85 | + |
| 86 | + if execute_fn and result.plan: |
| 87 | + result.plan = execute_fn(result.plan) |
| 88 | + |
| 89 | + return result |
| 90 | + |
| 91 | + def _critique(self, goal: str, plan: Any) -> tuple[float, str]: |
| 92 | + if self._llm is None: |
| 93 | + return 0.8, "No LLM available for critique — passing by default" |
| 94 | + |
| 95 | + plan_text = self._plan_to_text(plan) |
| 96 | + prompt = f"Goal: {goal}\n\nProposed plan:\n{plan_text}\n\nScore and critique:" |
| 97 | + |
| 98 | + try: |
| 99 | + from moso_core.llm.models import LLMRequest |
| 100 | + response = self._llm.complete(LLMRequest( |
| 101 | + prompt=prompt, |
| 102 | + system_prompt=CRITIQUE_SYSTEM, |
| 103 | + max_tokens=128, |
| 104 | + temperature=0.3, |
| 105 | + )) |
| 106 | + if not response.success: |
| 107 | + return 0.8, f"LLM critique unavailable: {response.error}" |
| 108 | + |
| 109 | + parsed = json.loads(response.text.strip()) |
| 110 | + score = max(0.0, min(1.0, float(parsed.get("score", 0.8)))) |
| 111 | + feedback = parsed.get("feedback", "No feedback") |
| 112 | + return score, feedback |
| 113 | + except (json.JSONDecodeError, KeyError, ValueError): |
| 114 | + return 0.8, "Critique parse failed — passing by default" |
| 115 | + except Exception as e: |
| 116 | + logger.debug("Critique error: %s", e) |
| 117 | + return 0.8, "Critique unavailable — passing by default" |
| 118 | + |
| 119 | + def _plan_to_text(self, plan: Any) -> str: |
| 120 | + if hasattr(plan, "steps"): |
| 121 | + lines = [] |
| 122 | + for i, step in enumerate(plan.steps): |
| 123 | + desc = getattr(step, "description", str(step)) |
| 124 | + action = getattr(step, "action", "") |
| 125 | + lines.append(f" {i+1}. [{action}] {desc}") |
| 126 | + return "\n".join(lines) |
| 127 | + return str(plan)[:500] |
0 commit comments