105 lines
3.9 KiB
Python
105 lines
3.9 KiB
Python
|
|
from tht.taskdoc import generate_task_doc
|
|
|
|
|
|
def test_task_doc_includes_question_and_schema_scope(tmp_path):
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("# Domanda\nQuanti pazienti?\n## Assunzioni\n- a")
|
|
(s / "schema_linking.json").write_text(
|
|
'{"question":"q","candidates":[{"kind":"table","name":"pazienti"}],"joins":[],"excluded":[],"open_questions":[]}'
|
|
)
|
|
doc = generate_task_doc(session_dir=s, phase=7)
|
|
assert "Quanti pazienti?" in doc.body
|
|
assert "pazienti" in doc.body
|
|
assert doc.byte_budget_ok is True
|
|
|
|
|
|
def test_task_doc_never_embeds_full_physical_yaml(tmp_path):
|
|
"""physical.yaml e' fatale per un 35B/<200k (~190k token). Mai incorporarlo."""
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
# un physical.yaml enorme fuori dalla sessione (come in ChironeWp3: artifacts/mschema/)
|
|
(s.parent / "physical.yaml").write_text("x: " + "y" * 800_000)
|
|
doc = generate_task_doc(session_dir=s, phase=7)
|
|
assert "physical.yaml" not in doc.body
|
|
assert len(doc.body) < 100_000 # bounded
|
|
|
|
|
|
def test_task_doc_byte_budget_enforced_on_normal_input(tmp_path):
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
doc = generate_task_doc(session_dir=s, phase=1)
|
|
assert doc.byte_budget_ok is True
|
|
|
|
|
|
def test_task_doc_byte_budget_violation_flagged(tmp_path):
|
|
"""Se un artefatto di sessione e' enorme (input perverso), byte_budget_ok diventa False."""
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
(s / "schema_linking.json").write_text("x: " + "y" * 400_000) # ~400KB -> over budget
|
|
doc = generate_task_doc(session_dir=s, phase=7)
|
|
assert doc.byte_budget_ok is False
|
|
|
|
|
|
def test_taskdoc_truncates_over_budget(tmp_path):
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("# Domanda\n" + "x" * 200_000)
|
|
|
|
doc = generate_task_doc(session_dir=s, phase=1)
|
|
|
|
assert doc.byte_budget_ok is False
|
|
assert len(doc.body.encode()) <= 80_000
|
|
assert "troncato" in doc.body
|
|
|
|
|
|
def test_task_doc_carries_phase_header(tmp_path):
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
doc = generate_task_doc(session_dir=s, phase=4)
|
|
assert "fase 4" in doc.body.lower() or "fase 4" in doc.body
|
|
|
|
|
|
def test_task_doc_excludes_stale_decisions_post_rollback(tmp_path):
|
|
"""D15+D16: il task doc riflette lo stato effective, non quello stale.
|
|
Dopo rollback a F4, una sql_approved:7 stale non appare nel brief delle decisioni."""
|
|
from tht.decisions import append_decision
|
|
from tht.phase import current_phase
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
# simula: lavoro fino a F7, poi rollback a F4
|
|
append_decision(s, type="phase_approved", subject="phase:1")
|
|
append_decision(s, type="phase_approved", subject="phase:2")
|
|
append_decision(s, type="phase_approved", subject="phase:3")
|
|
append_decision(s, type="phase_approved", subject="phase:4")
|
|
append_decision(s, type="sql_approved", subject="phase:7", detail="SELECT 1")
|
|
append_decision(s, type="phase_reopened", subject="phase:4")
|
|
assert current_phase(s) == 4
|
|
doc = generate_task_doc(session_dir=s, phase=4)
|
|
# la decisione stale di fase 7 NON deve apparire nel brief
|
|
assert "sql_approved" not in doc.body
|
|
assert "phase:7" not in doc.body
|
|
|
|
|
|
def test_taskdoc_slices_to_promoted_tables(tmp_path):
|
|
s = tmp_path / "sess"
|
|
s.mkdir()
|
|
(s / "question.md").write_text("q")
|
|
(s / "schema_linking.json").write_text(
|
|
'{"question":"q","candidates":['
|
|
'{"kind":"table","name":"pazienti","decision":"promoted"},'
|
|
'{"kind":"table","name":"ricoveri","decision":"promoted"}],'
|
|
'"joins":[],"excluded":[],"open_questions":[]}'
|
|
)
|
|
|
|
doc = generate_task_doc(session_dir=s, phase=4, promoted_tables=["pazienti"])
|
|
|
|
assert "pazienti" in doc.body
|
|
assert "ricoveri" not in doc.body
|