floop: DATE answer type + deterministic year check #18
@@ -30,7 +30,8 @@ class Subquestion(BaseModel):
|
||||
desc="1-based positions of the subquestions whose answers this one needs first; empty when none")
|
||||
answer_type: AnswerType = dspy.OutputField(
|
||||
desc="OPEN for a single value, FUNCTION for a formula, INTERVAL for a range, "
|
||||
"CONSTANT for a universal constant, LIST for an enumeration of things")
|
||||
"CONSTANT for a universal constant, LIST for an enumeration of things, "
|
||||
"DATE for a specific date (a day, month and year — '15' alone is NOT a date)")
|
||||
|
||||
|
||||
class InitSignature(dspy.Signature):
|
||||
|
||||
@@ -23,6 +23,16 @@ from .stream import RunStreams
|
||||
|
||||
EXPERT_ENGINES = ("knowledge", "logic", "analytic", "numeric")
|
||||
|
||||
_YEAR_RE = re.compile(r"\b(?:1[5-9]\d{2}|20\d{2}|2[1-9]\d{2})\b") # 1500–2999
|
||||
|
||||
|
||||
def _looks_like_date(v: str) -> bool:
|
||||
"""Deterministic DATE admission: the value must carry a plausible
|
||||
year (1500-2999). '2 June 2018', 'June 2018', '2018' pass; a bare
|
||||
'15' — or a month name alone — is NOT a date and must never
|
||||
satisfy a DATE question (his 22/10 local run)."""
|
||||
return bool(_YEAR_RE.search(v or ""))
|
||||
|
||||
|
||||
def _fmt_num(x: float) -> str:
|
||||
"""Deterministic number formatting for computed rows: integers plain,
|
||||
@@ -479,6 +489,16 @@ class FloopRun:
|
||||
# fan-out bookkeeping: entity rows carry the group so the join finds them
|
||||
if q.fan_group:
|
||||
row.provenance["fan_group"] = q.fan_group
|
||||
# DATE type check (his 22/10 local run: term-start answered '15'):
|
||||
# deterministic — a DATE value must carry a year; bare numbers
|
||||
# are not dates and must never satisfy a DATE question
|
||||
if q.answer_type == AnswerType.DATE:
|
||||
v = str(row.payload.get("value", ""))
|
||||
if not _looks_like_date(v):
|
||||
self.notes.append(
|
||||
f"{q.qid}: DATE answer rejected (no year): {v[:60]!r}")
|
||||
row.payload["value"] = "UNKNOWN"
|
||||
row.payload["note"] = "rejected: DATE without a year"
|
||||
# PR-3: skim notices also land in the run notes — legibility law:
|
||||
# derived answers render CONTENT, not counts alone
|
||||
for n in (row.payload.get("skim") or [])[:2]:
|
||||
|
||||
@@ -20,6 +20,7 @@ class AnswerType(str, Enum):
|
||||
INTERVAL = "INTERVAL"
|
||||
CONSTANT = "CONSTANT"
|
||||
LIST = "LIST"
|
||||
DATE = "DATE"
|
||||
|
||||
|
||||
class BudgetExhausted(Exception):
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
"""DATE answer type tests (his 22/10 local run: term-start answered '15').
|
||||
|
||||
Laws under test:
|
||||
1. AnswerType.DATE exists; INIT's enum desc mentions it (the schema the
|
||||
model sees carries the date guidance).
|
||||
2. _looks_like_date is deterministic and strict: a plausible YEAR is the
|
||||
admission bar — '15' alone, a month name alone, garbage: NOT dates.
|
||||
3. The machine refuses a DATE answer without a year: the row becomes
|
||||
UNKNOWN and the question fails (re-buttable honestly, never a silent
|
||||
wrong type flowing downstream into dependents).
|
||||
|
||||
No network. Run: .venv/bin/python tests/date_type.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from floop.machine import _looks_like_date
|
||||
from floop.state import AnswerType, Budget, EvidenceRow, OpenQuestion
|
||||
from floop.machine import FloopRun
|
||||
|
||||
|
||||
def test_date_in_enum_and_desc():
|
||||
assert AnswerType.DATE.value == "DATE"
|
||||
from floop.dspy_modules import Subquestion
|
||||
import json
|
||||
schema = json.dumps(Subquestion.model_json_schema(), default=str)
|
||||
assert "DATE for a specific date" in schema, "desc must carry the date guidance"
|
||||
print("ok: AnswerType.DATE exists; the model-visible desc carries it")
|
||||
|
||||
|
||||
def test_looks_like_date():
|
||||
# admission: a plausible year
|
||||
for good in ("2 June 2018", "June 2018", "2018", "17 November 2023",
|
||||
"6–29 December 2023", "sworn in on 1 June 2018"):
|
||||
assert _looks_like_date(good), good
|
||||
# refusal: no year — the exact '15' shape from his run
|
||||
for bad in ("15", "June", "fifteen", "", "the fifteenth",
|
||||
"15 June", "239", "30000"):
|
||||
assert not _looks_like_date(bad), bad
|
||||
print("ok: _looks_like_date deterministic (year required; '15' refused)")
|
||||
|
||||
|
||||
def test_machine_rejects_bare_number_date():
|
||||
b = Budget(max_llm_calls=40, max_depth=2, max_retries=3,
|
||||
missing_extensions=1, min_child_budget=4)
|
||||
|
||||
class DateModules:
|
||||
def init_decompose(self, question):
|
||||
return [{"text": "When did the term begin?",
|
||||
"deps": [], "answer_type": "DATE"}], ""
|
||||
def missing_decompose(self, goal, evidence):
|
||||
return []
|
||||
def route_question(self, question, catalog, evidence):
|
||||
return "knowledge", "date lookup"
|
||||
def expert_draft(self, question, expert_kind, evidence, parent_question=""):
|
||||
return {"value": "15", "source_quote": "",
|
||||
"confidence": "high"}
|
||||
def synthesize(self, goal, evidence, unverified):
|
||||
return "synthesized"
|
||||
|
||||
run = FloopRun("Who was the President before X?", b, modules=DateModules())
|
||||
run._init_decompose() # creates Q1 (DATE) from init_decompose
|
||||
q1 = next(q for q in run.stack.items if q.text == "When did the term begin?")
|
||||
run._route_and_execute(q1)
|
||||
# the expert's '15' must NOT satisfy the DATE question
|
||||
assert q1.status == "failed", q1.status
|
||||
assert any("DATE answer rejected" in n for n in run.notes), run.notes
|
||||
print("ok: '15' rejected for a DATE question — row UNKNOWN, question failed")
|
||||
|
||||
|
||||
def test_machine_accepts_real_date():
|
||||
b = Budget(max_llm_calls=40, max_depth=2, max_retries=3,
|
||||
missing_extensions=1, min_child_budget=4)
|
||||
|
||||
class DateModules:
|
||||
def init_decompose(self, question):
|
||||
return [{"text": "When did the term begin?",
|
||||
"deps": [], "answer_type": "DATE"}], ""
|
||||
def missing_decompose(self, goal, evidence):
|
||||
return []
|
||||
def route_question(self, question, catalog, evidence):
|
||||
return "knowledge", "date lookup"
|
||||
def expert_draft(self, question, expert_kind, evidence, parent_question=""):
|
||||
return {"value": "2 June 2018", "source_quote": "",
|
||||
"confidence": "high"}
|
||||
def synthesize(self, goal, evidence, unverified):
|
||||
return "synthesized"
|
||||
|
||||
run = FloopRun("Who was the President before X?", b, modules=DateModules())
|
||||
run._init_decompose()
|
||||
q1 = next(q for q in run.stack.items if q.text == "When did the term begin?")
|
||||
run._route_and_execute(q1)
|
||||
assert q1.status == "answered", q1.status
|
||||
print("ok: '2 June 2018' accepted for a DATE question")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_date_in_enum_and_desc()
|
||||
test_looks_like_date()
|
||||
test_machine_rejects_bare_number_date()
|
||||
test_machine_accepts_real_date()
|
||||
print("\nDATE type tests passed")
|
||||
Reference in New Issue
Block a user