Implementazione del piano di remediation progressiva sui difetti emersi dall'analisi dell'harness. Tutto verificato: 214 test Python (incl. L0 su Postgres reale), 14 test JS del gate, ruff pulito. Blocco 1 (CRITICA, integrazione gate↔CLI): - phase advance: gate usa --auto + exit 6; reviewer_confirm kind:phase fa advance esplicito che applica i prerequisiti (prima non avanzava per le fasi a conferma umana). - cte plan riceve i --name dal gate (param names); set-question con id posizionale; skill `tht search find`; nuovo comando `tht memory save-one` con dedup hash client-side in save_one_memory. Blocco 2 (D15, stato post-rollback): - campo `phase` su DecisionRecord + effective_decisions phase-aware per i subject "a nome" (cte_approved ecc.); _compute_promotions e finalize sulla vista effective; finalize confronta col piano CTE effettivo, non glob; `decision add --retracts` + comando `decision retract`. Blocco 3 (D7 read-only + D6 manifest): - assert_read_only su tutti e quattro i codepath (direct + REST); - manifest author/summary/updated_at/updated_by/schema_version popolati + helper touch_manifest sulle mutazioni. Blocco 4-5 (D14a/D14b): - decision_min_phase data-driven via `emits:` in workflow.yaml; - formula evidence: status auto, search_formulas, gruppo CLI `tht formula`, `search find --kind formula`, load_evidence_dir salta i .sql.md. Blocco 6 (robustezza): - taskdoc slice promoted_tables + bound enforced; report escaping/bound + rsplit note; filtro kind reader REST/direct; conteggio upserted robusto; guard REST run_query non-list; LSH disallineato -> LshIndexError. Blocco 7 (pulizia): - dead code gate e KIND_TO_TABLE morto rimossi; doc Postgres-only (README + connection.py). Blocco 0 (parziale): test di compatibilità firma gate↔CLI (tests/integration). Rinviati: fake-Pi runtime completo, artifact-gate da disco (#23), parità eligibility REST/direct (#28), unificazione reserved-labels (#30), memory_rejected da deselezione (#33). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
from tht.execute import ExecResult, PlanSummary
|
|
from tht.sqlcheck import CheckResult
|
|
|
|
NOTE_HEADING = "## Note del reviewer"
|
|
|
|
# Bound del report (D16): celle di dati clinici (referti, anamnesi) possono essere
|
|
# enormi; senza limiti il report.md esplode (il caso 344KB segnalato) e i '|'/newline
|
|
# nei dati corrompono la tabella markdown.
|
|
_MAX_CELL_CHARS = 200
|
|
_MAX_TABLE_ROWS = 50
|
|
|
|
|
|
def _escape_cell(value: object) -> str:
|
|
"""Rende una cella sicura per il markdown: niente '|' o newline grezzi, larghezza
|
|
limitata. Evita corruzione della tabella e injection di markdown da dati DB."""
|
|
s = str(value).replace("|", "\\|").replace("\r", " ").replace("\n", " ")
|
|
if len(s) > _MAX_CELL_CHARS:
|
|
s = s[:_MAX_CELL_CHARS] + "…"
|
|
return s
|
|
|
|
|
|
def _markdown_table(columns: list[str], rows: list[tuple]) -> str:
|
|
lines = [
|
|
"| " + " | ".join(_escape_cell(c) for c in columns) + " |",
|
|
"|" + "---|" * len(columns),
|
|
]
|
|
for row in rows[:_MAX_TABLE_ROWS]:
|
|
lines.append("| " + " | ".join(_escape_cell(v) for v in row) + " |")
|
|
if len(rows) > _MAX_TABLE_ROWS:
|
|
lines.append(f"| _… {len(rows) - _MAX_TABLE_ROWS} righe in più non mostrate …_ |")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def render_validation_report(
|
|
*,
|
|
session_id: str,
|
|
check: CheckResult,
|
|
plan: PlanSummary,
|
|
plan_warnings: list[str],
|
|
result: ExecResult,
|
|
runtime_warnings: list[str],
|
|
static_warnings: list[str],
|
|
limit: int,
|
|
reviewer_notes: str = "",
|
|
) -> str:
|
|
"""Sezioni fattuali del validation report. La sezione note e' di Pi/reviewer:
|
|
se gia' scritta, va passata in reviewer_notes per preservarla."""
|
|
|
|
def warn(ws: list[str]) -> str:
|
|
return "\n".join(f"- warning: {w}" for w in ws) if ws else "- nessun warning"
|
|
|
|
parts = [
|
|
f"# Validation report — {session_id}",
|
|
"",
|
|
"> Avvertenza: questo report contiene sample di dati del database.",
|
|
"",
|
|
"## Parsing e read-only",
|
|
"",
|
|
"- parsing: OK" if check.ok else "- parsing: ERRORI",
|
|
"- statement read-only per struttura: OK" if check.ok else
|
|
"\n".join(f"- ERRORE: {e}" for e in check.errors),
|
|
warn(check.warnings + static_warnings),
|
|
"",
|
|
"## Piano di esecuzione",
|
|
"",
|
|
f"- costo totale stimato: {plan.total_cost}",
|
|
f"- righe stimate: {plan.plan_rows}",
|
|
f"- nodi: {', '.join(plan.node_types)}",
|
|
warn(plan_warnings),
|
|
"",
|
|
"## Preview / aggregato",
|
|
"",
|
|
f"- tempo di esecuzione: {result.execution_ms} ms (limit {limit})",
|
|
"",
|
|
_markdown_table(result.columns, result.rows),
|
|
"",
|
|
warn(runtime_warnings),
|
|
"",
|
|
NOTE_HEADING,
|
|
"",
|
|
reviewer_notes.strip(),
|
|
"",
|
|
]
|
|
return "\n".join(parts)
|
|
|
|
|
|
def extract_reviewer_notes(report_md: str) -> str:
|
|
"""Estrae il contenuto della sezione note da un report esistente.
|
|
|
|
rsplit sull'ULTIMA occorrenza dell'heading: la sezione note e' sempre in coda, e
|
|
se per caso la stringa dell'heading comparisse nei dati di preview (ora comunque
|
|
con newline escapati, quindi mai a inizio riga) non corromperebbe l'estrazione."""
|
|
if NOTE_HEADING not in report_md:
|
|
return ""
|
|
return report_md.rsplit(NOTE_HEADING, 1)[1].strip()
|