RDS777 commited on
Commit
924bc2c
·
verified ·
1 Parent(s): 3af4e20

Create core/constraint.py

Browse files
Files changed (1) hide show
  1. core/constraint.py +60 -0
core/constraint.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple
2
+
3
+ from core.state import WorldState
4
+
5
+ MAX_TASKS_PER_CYCLE = 3
6
+
7
+
8
+ def validate_plan(plan: dict, state: WorldState) -> Tuple[bool, str]:
9
+ if not isinstance(plan, dict):
10
+ return False, "Plan invalide : objet JSON attendu."
11
+
12
+ ids = plan.get("task_ids")
13
+ if not isinstance(ids, list):
14
+ return False, "Plan invalide : 'task_ids' manquant ou mal formé."
15
+ if not ids:
16
+ return False, "Plan vide : au moins une tâche doit être sélectionnée."
17
+ if not all(isinstance(i, int) and not isinstance(i, bool) for i in ids):
18
+ return False, "Plan invalide : 'task_ids' doit contenir des entiers."
19
+ if len(set(ids)) != len(ids):
20
+ return False, "Plan invalide : identifiants de tâches dupliqués."
21
+ if len(ids) > MAX_TASKS_PER_CYCLE:
22
+ return False, f"Trop de tâches : maximum {MAX_TASKS_PER_CYCLE} par cycle."
23
+
24
+ by_id = {t.id: t for t in state.tasks}
25
+ total = 0.0
26
+ for i in ids:
27
+ task = by_id.get(i)
28
+ if task is None:
29
+ return False, f"Tâche {i} inconnue."
30
+ if task.status == "completed":
31
+ return False, f"Tâche {i} déjà terminée."
32
+ total += task.cost
33
+
34
+ if total > state.budget_usd:
35
+ return False, f"Budget insuffisant : requis {total:.0f}, disponible {state.budget_usd:.0f}."
36
+
37
+ return True, "Plan approuvé."
38
+
39
+
40
+ def apply_plan(plan: dict, state: WorldState) -> WorldState:
41
+ """À appeler uniquement après validate_plan réussi, sous verrou."""
42
+ spent = 0.0
43
+ done = []
44
+ for t in state.tasks:
45
+ if t.id in plan["task_ids"] and t.status == "pending":
46
+ t.status = "completed"
47
+ state.budget_usd -= t.cost
48
+ spent += t.cost
49
+ done.append(t.id)
50
+ state.day += 1
51
+ state.history.append(
52
+ {
53
+ "day": state.day,
54
+ "executed": done,
55
+ "spent": spent,
56
+ "reasoning": str(plan.get("reasoning", ""))[:200],
57
+ }
58
+ )
59
+ state.history = state.history[-50:]
60
+ return state