Files
ThothII/harness/tht/rest/execute.py
T
marcopanandClaude Opus 4.8 c4d130828f fix(harness): remediation difetti review — gate↔CLI, D15, D7/D6, D14, robustezza
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>
2026-06-27 17:16:51 +02:00

49 lines
2.0 KiB
Python

"""Esecuzione controllata e EXPLAIN via REST (rpc `run_query`/`explain_query`).
Produce gli stessi tipi del transport diretto (`ExecResult`, `PlanSummary`) e traduce
`RestError` in `ExecutionError`, così le fasi 6/7 e la gestione errori della CLI non cambiano.
Differenze rispetto al diretto: read-only e statement_timeout sono garantiti lato server;
il client mantiene solo l'iniezione del LIMIT (per il rilevamento del troncamento in preview).
"""
import time
from tht.execute import ExecResult, ExecutionError, PlanSummary, _inject_limit, assert_read_only
from tht.rest.client import RestError
from tht.rest.explain import parse_text_plan
def run_controlled_rest(client, sql: str, *, limit: int) -> ExecResult:
# Guard read-only client-side anche sul path REST (D7): non delegare l'unica verifica
# al server. Stesso check strutturale del path diretto.
assert_read_only(sql)
final_sql, injected = _inject_limit(sql, limit)
start = time.monotonic()
try:
rows_dicts = client.run_query(final_sql)
except RestError as e:
raise ExecutionError(str(e)) from e
elapsed_ms = int((time.monotonic() - start) * 1000)
if not isinstance(rows_dicts, list):
# run_query atteso come lista di righe; una risposta inattesa (dict d'errore,
# scalare) non deve crashare con KeyError/TypeError opaco.
raise ExecutionError(
f"Risposta REST inattesa da run_query (atteso elenco di righe): {type(rows_dicts).__name__}"
)
columns = list(rows_dicts[0].keys()) if rows_dicts else []
rows = [tuple(r.get(c) for c in columns) for r in rows_dicts]
truncated = injected and len(rows) > limit
return ExecResult(
columns=columns, rows=rows[:limit], execution_ms=elapsed_ms, truncated=truncated
)
def explain_rest(client, sql: str) -> PlanSummary:
assert_read_only(sql)
try:
lines = client.explain_query(sql)
except RestError as e:
raise ExecutionError(str(e)) from e
return parse_text_plan(lines)