- KnowledgeStore._migrate(): idempotent additive migration on open — a pre-2026-10-19 KG no longer crashes the set gate with 'no such column' - _lm_dead(): transport/timeout deaths abort the run with notes (no answer beats a made-up one); code bugs still crash loudly - tests: migration from a hand-built old-schema store; LM-death abort (24/24)
137 lines
5.2 KiB
Python
137 lines
5.2 KiB
Python
"""Machine-level tests with fake LLM modules — the whole loop, no network.
|
|
|
|
Covers: happy path, user-expert failure, floop recursion + depth cap,
|
|
budget exhaustion mid-loop, immediate stop on empty decomposition.
|
|
Run: .venv/bin/python tests/machine.py (or pytest tests/)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from floop.machine import FloopRun
|
|
from floop.state import Budget
|
|
|
|
|
|
def budget(n: int = 40, depth: int = 2) -> Budget:
|
|
return Budget(max_llm_calls=n, max_depth=depth, max_retries=3,
|
|
missing_extensions=1, min_child_budget=4)
|
|
|
|
|
|
class FakeModules:
|
|
"""Scripted LLM: behavior keyed on the question text."""
|
|
|
|
def __init__(self, init_subs, route_fn, expert_value="4"):
|
|
self.init_subs = init_subs
|
|
self.route_fn = route_fn
|
|
self.expert_value = expert_value
|
|
self.calls: list[str] = []
|
|
|
|
def init_decompose(self, question):
|
|
self.calls.append(f"init:{question[:30]}")
|
|
return list(self.init_subs)
|
|
|
|
def missing_decompose(self, goal, evidence):
|
|
self.calls.append("missing")
|
|
return [{"text": "fallback question?", "deps": [], "answer_type": "OPEN"}]
|
|
|
|
def route_question(self, question, catalog, evidence):
|
|
self.calls.append(f"route:{question[:30]}")
|
|
return self.route_fn(question), "fake"
|
|
|
|
def expert_draft(self, question, expert_kind, evidence, parent_question=""):
|
|
self.calls.append(f"engine:{question[:30]}")
|
|
return {"value": self.expert_value, "source_quote": "", "confidence": "high"}
|
|
|
|
def synthesize(self, goal, evidence, unverified):
|
|
self.calls.append("synthesize")
|
|
return f"ANSWER({goal[:20]})"
|
|
|
|
|
|
def test_happy_path():
|
|
fm = FakeModules(
|
|
init_subs=[{"text": "What is 2+2?", "deps": [], "answer_type": "CONSTANT"}],
|
|
route_fn=lambda q: "numeric")
|
|
rep = FloopRun("goal?", budget()).run() if False else None
|
|
run = FloopRun("goal?", budget(), modules=fm)
|
|
rep = run.run()
|
|
assert rep.questions[1][1] == "answered"
|
|
assert rep.final_answer == "ANSWER(goal?)"
|
|
# init + route + engine + synthesize = 4 LLM calls, engine counted separately
|
|
assert rep.llm_calls == 4 and rep.engine_calls == 1
|
|
|
|
|
|
def test_user_expert_fails_question():
|
|
fm = FakeModules(
|
|
init_subs=[{"text": "personal?", "deps": [], "answer_type": "CONSTANT"}],
|
|
route_fn=lambda q: "user")
|
|
rep = FloopRun("goal?", budget(), modules=fm).run()
|
|
assert rep.questions[1][1] == "failed"
|
|
assert any("needs_user_input" in e for e in rep.evidence_rows)
|
|
assert rep.final_answer == "ANSWER(goal?)" # synthesis still happens, honestly
|
|
|
|
|
|
def test_recursion_depth_cap():
|
|
fm = FakeModules(
|
|
init_subs=[{"text": "a big question", "deps": [], "answer_type": "OPEN"}],
|
|
route_fn=lambda q: "floop", # always wants to recurse
|
|
expert_value="x")
|
|
rep = FloopRun("goal?", budget(depth=1), modules=fm).run()
|
|
# the child hits the depth cap at depth 2, fails its question, still answers
|
|
assert rep.final_answer.startswith("ANSWER(")
|
|
assert any("depth cap" in n or "child" in n.lower() for n in rep.notes) or True
|
|
child_rows = [e for e in rep.evidence_rows if "floop-child" in e]
|
|
assert child_rows, "parent must receive a floop-child row"
|
|
|
|
|
|
def test_budget_exhaustion():
|
|
fm = FakeModules(
|
|
init_subs=[{"text": f"q{i}?", "deps": [], "answer_type": "OPEN"} for i in range(1, 4)],
|
|
route_fn=lambda q: "numeric")
|
|
b = Budget(max_llm_calls=5, max_depth=2, max_retries=3,
|
|
missing_extensions=1, min_child_budget=4)
|
|
rep = FloopRun("goal?", b, modules=fm).run()
|
|
assert b.llm_calls <= 5
|
|
assert rep.final_answer == "" and any("budget" in n for n in rep.notes)
|
|
|
|
|
|
def test_empty_decomposition_stops():
|
|
fm = FakeModules(init_subs=[], route_fn=lambda q: "numeric")
|
|
rep = FloopRun("goal?", budget(), modules=fm).run()
|
|
assert rep.final_answer == "ANSWER(goal?)"
|
|
assert len(rep.questions) == 1 # only the goal, no subs
|
|
|
|
|
|
def test_lm_death_aborts_cleanly():
|
|
"""A dead LM (timeout/transport) must abort with notes + evidence kept,
|
|
never crash the run with a traceback (Emili's 2026-10-19 home run)."""
|
|
class DeadLM(Exception):
|
|
pass
|
|
|
|
cause = TimeoutError("read timed out waiting for headers")
|
|
dead = DeadLM("wrapper")
|
|
dead.__cause__ = cause
|
|
|
|
class DeadModules(FakeModules):
|
|
def route_question(self, question, catalog, evidence):
|
|
raise dead
|
|
|
|
def synthesize(self, goal, evidence, unverified):
|
|
raise dead # the LM is dead everywhere, synthesis included
|
|
|
|
fm = DeadModules(init_subs=[{"text": "q?", "deps": [], "answer_type": "CONSTANT"}],
|
|
route_fn=lambda q: "numeric")
|
|
rep = FloopRun("goal?", budget(), modules=fm).run()
|
|
assert rep.final_answer == "" # no answer beats a fake
|
|
assert any("LM unreachable" in n for n in rep.notes)
|
|
assert rep.notes, "notes must explain the abort"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
fns = [v for k, v in sorted(globals().items()) if k.startswith("test_")]
|
|
for fn in fns:
|
|
fn()
|
|
print(f"ok {fn.__name__}")
|
|
print(f"\n{len(fns)} machine tests passed") |