Spaces:
Sleeping
Sleeping
File size: 4,493 Bytes
8e3a425 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | # core/validation_queue.py
"""
Validation Queue — Gestion des propositions en attente de validation humaine.
Permet de soumettre, approuver, rejeter ou modifier des propositions avant intégration.
"""
import json
import time
import sqlite3
import uuid
from typing import Dict, List, Optional
from dataclasses import dataclass, asdict
@dataclass
class Proposal:
"""Une proposition en attente de validation."""
id: str
type: str # "fable_optimization", "hermes_refactor", "code_deploy"
description: str
code: str
score: float
status: str # "pending", "approved", "rejected", "modified"
created_at: float
approved_at: Optional[float] = None
comment: Optional[str] = None
def to_dict(self) -> Dict:
return asdict(self)
@classmethod
def from_dict(cls, data: Dict) -> "Proposal":
return cls(**data)
class ValidationQueue:
"""
File d'attente des propositions à valider humainement.
Persistance SQLite pour survivre aux redémarrages.
"""
def __init__(self, db_path: str = "/data/validation.db"):
self.db_path = db_path
self.conn = sqlite3.connect(db_path, check_same_thread=False)
self._init_db()
def _init_db(self):
self.conn.execute("""
CREATE TABLE IF NOT EXISTS proposals (
id TEXT PRIMARY KEY,
type TEXT,
description TEXT,
code TEXT,
score REAL,
status TEXT,
created_at REAL,
approved_at REAL,
comment TEXT
)
""")
self.conn.commit()
def add_proposal(self, proposal: Proposal) -> str:
"""Ajoute une proposition en attente."""
self.conn.execute("""
INSERT INTO proposals (id, type, description, code, score, status, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
proposal.id,
proposal.type,
proposal.description,
proposal.code,
proposal.score,
"pending",
proposal.created_at
))
self.conn.commit()
return proposal.id
def get_pending(self) -> List[Proposal]:
"""Récupère toutes les propositions en attente."""
rows = self.conn.execute("""
SELECT id, type, description, code, score, status, created_at, approved_at, comment
FROM proposals WHERE status = 'pending'
""").fetchall()
return [
Proposal(
id=r[0], type=r[1], description=r[2], code=r[3],
score=r[4], status=r[5], created_at=r[6],
approved_at=r[7], comment=r[8]
)
for r in rows
]
def get_proposal(self, proposal_id: str) -> Optional[Proposal]:
row = self.conn.execute(
"SELECT * FROM proposals WHERE id = ?", (proposal_id,)
).fetchone()
if not row:
return None
return Proposal(
id=row[0], type=row[1], description=row[2], code=row[3],
score=row[4], status=row[5], created_at=row[6],
approved_at=row[7], comment=row[8]
)
def approve(self, proposal_id: str, comment: str = ""):
self.conn.execute("""
UPDATE proposals
SET status = 'approved', approved_at = ?, comment = ?
WHERE id = ?
""", (time.time(), comment, proposal_id))
self.conn.commit()
def reject(self, proposal_id: str, comment: str = ""):
self.conn.execute("""
UPDATE proposals
SET status = 'rejected', approved_at = ?, comment = ?
WHERE id = ?
""", (time.time(), comment, proposal_id))
self.conn.commit()
def modify(self, proposal_id: str, new_code: str, comment: str = ""):
self.conn.execute("""
UPDATE proposals
SET code = ?, status = 'modified', approved_at = ?, comment = ?
WHERE id = ?
""", (new_code, time.time(), comment, proposal_id))
self.conn.commit()
def delete_old(self, older_than_days: int = 30):
"""Supprime les propositions terminées de plus de N jours."""
cutoff = time.time() - older_than_days * 86400
self.conn.execute("""
DELETE FROM proposals
WHERE status IN ('approved', 'rejected') AND approved_at < ?
""", (cutoff,))
self.conn.commit() |