SKKAI / core /constraint.py
RDS777's picture
Update core/constraint.py
b402d93 verified
Raw History Blame Contribute Delete
2.14 kB
from typing import Tuple
from core.state import WorldState
MAX_TASKS_PER_CYCLE = 3
def validate_plan(plan: dict, state: WorldState) -> Tuple[bool, str]:
if not isinstance(plan, dict):
return False, "Plan invalide : objet JSON attendu."
ids = plan.get("task_ids")
if not isinstance(ids, list):
return False, "Plan invalide : 'task_ids' manquant ou mal formé."
if not ids:
return False, "Plan vide : au moins une tâche doit être sélectionnée."
if not all(isinstance(i, int) and not isinstance(i, bool) for i in ids):
return False, "Plan invalide : 'task_ids' doit contenir des entiers."
if len(set(ids)) != len(ids):
return False, "Plan invalide : identifiants de tâches dupliqués."
if len(ids) > MAX_TASKS_PER_CYCLE:
return False, f"Trop de tâches : maximum {MAX_TASKS_PER_CYCLE} par cycle."
by_id = {t.id: t for t in state.tasks}
total = 0.0
for i in ids:
task = by_id.get(i)
if task is None:
return False, f"Tâche {i} inconnue."
if task.status == "completed":
return False, f"Tâche {i} déjà terminée."
total += task.cost
if total > state.budget_usd:
return False, f"Budget insuffisant : requis {total:.0f}, disponible {state.budget_usd:.0f}."
return True, "Plan approuvé."
def apply_plan(plan: dict, state: WorldState) -> WorldState:
"""À appeler uniquement après validate_plan réussi, sous verrou."""
spent = 0.0
earned = 0.0
done = []
for t in state.tasks:
if t.id in plan["task_ids"] and t.status == "pending":
t.status = "completed"
state.budget_usd -= t.cost
state.budget_usd += t.revenue
spent += t.cost
earned += t.revenue
done.append(t.id)
state.day += 1
state.history.append(
{
"day": state.day,
"executed": done,
"spent": spent,
"earned": earned,
"reasoning": str(plan.get("reasoning", ""))[:200],
}
)
state.history = state.history[-50:]
return state