feat(harness): decisions.py ported + decision_retracted for step rollback (D15)
- ported from ChironeWp3 (22 DecisionType, append-only jsonl, monotonic seq) - added decision_retracted type + retracts field for step-level rollback (D15): the retracted decision stays in the audit log, effective_decisions() (Task A5) will exclude it from the active view - 5 tests: retract marker + monotonic seq + retracts default + empty session + literal includes retracted. All 13 harness tests pass.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
DECISIONS_FILE = "review_decisions.jsonl"
|
||||
|
||||
# 22 tipi di ChironeWp3 (verified leggendo session/decisions.py) + 1 nuovo (D15):
|
||||
# `decision_retracted` per il rollback a granularità step (ritira una decisione
|
||||
# senza cancellarne la riga dal log di audit; effective_decisions la onora).
|
||||
DecisionType = Literal[
|
||||
"concept_clarified",
|
||||
"question_rewritten",
|
||||
"table_promoted",
|
||||
"table_excluded",
|
||||
"column_corrected",
|
||||
"join_modified",
|
||||
"evidence_accepted",
|
||||
"evidence_rejected",
|
||||
"ambiguity_open",
|
||||
"memory_rejected",
|
||||
"cte_approved",
|
||||
"cte_corrected",
|
||||
"cte_rejected",
|
||||
"sql_revised",
|
||||
"sql_approved",
|
||||
"sql_rejected",
|
||||
"phase_approved",
|
||||
"phase_auto_approved",
|
||||
"phase_reopened",
|
||||
"phase_skipped",
|
||||
"datamart_requested",
|
||||
"datamart_declined",
|
||||
# D15: marker di ritrazione. subject = "phase:N", retracts = decision_seq ritirata.
|
||||
# Resta nel log di audit (append-only); effective_decisions() la esclude dalla vista.
|
||||
"decision_retracted",
|
||||
]
|
||||
|
||||
|
||||
class DecisionRecord(BaseModel):
|
||||
seq: int
|
||||
ts: datetime
|
||||
type: DecisionType
|
||||
subject: str
|
||||
detail: str = ""
|
||||
rationale: str = ""
|
||||
# D15: se type == "decision_retracted", indica quale seq viene ritirata.
|
||||
retracts: int | None = None
|
||||
|
||||
|
||||
def list_decisions(session_dir: Path) -> list[DecisionRecord]:
|
||||
path = session_dir / DECISIONS_FILE
|
||||
if not path.exists():
|
||||
return []
|
||||
return [
|
||||
DecisionRecord.model_validate_json(line)
|
||||
for line in path.read_text().splitlines()
|
||||
if line.strip()
|
||||
]
|
||||
|
||||
|
||||
def append_decision(
|
||||
session_dir: Path,
|
||||
*,
|
||||
type: str,
|
||||
subject: str,
|
||||
detail: str = "",
|
||||
rationale: str = "",
|
||||
retracts: int | None = None,
|
||||
) -> DecisionRecord:
|
||||
record = DecisionRecord(
|
||||
seq=len(list_decisions(session_dir)) + 1,
|
||||
ts=datetime.now(UTC),
|
||||
type=type,
|
||||
subject=subject,
|
||||
detail=detail,
|
||||
rationale=rationale,
|
||||
retracts=retracts,
|
||||
)
|
||||
path = session_dir / DECISIONS_FILE
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("a") as f:
|
||||
f.write(record.model_dump_json() + "\n")
|
||||
return record
|
||||
Reference in New Issue
Block a user