Files
ThothII/harness/nsp/decisions.py
T
marcopan c0285e55a0 feat(harness): value grounding -- multi-column LSH + value_grounded (D14a)
Ports search/__init__.py (combined_search/RRF/aggregate) renamed psdwp3->nsp.

New aggregate_lsh_multi (the D14a deviation): groups LSH hits by table keeping
EVERY column where a value appears -- NOT collapsed to a single best column.
The old _aggregate_lsh hid alternative groundings (e.g. 'ablazione' matching both
a boolean flag and a free-text patologia field). aggregate_lsh_multi exposes all
columns so the value-grounding widget lets the reviewer choose the anchor(s).
Within one (table, column) the best-scored value is kept; columns ordered by score.

value_grounded added to DecisionType (records the reviewer's anchor choice).

L1: test_value_grounding (6 tests) -- multi-column exposure, grouping, within-column
best-value, ordering, empty, and the value_grounded decision-type existence.

Deferred: lshindex/ (needs vendor/thoth_lsh) and the L0 test_rrf.py land with the
nsp lsh build command + index-building path; not needed for the pure L1 core here.
2026-06-26 23:02:11 +02:00

90 lines
2.5 KiB
Python

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",
# D14a: valore citato nella domanda ancorato a una o piu' colonne. subject =
# "phase:4", detail = il valore (es. "ablazione"), rationale = la/e colonna/e scelta/e
# dal reviewer (aggregate_lsh_multi le espone tutte senza collassare al miglior match).
"value_grounded",
]
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