floop v0.1: design docs + executable dspy draft

Root: README, PIPELINE, ALGORITHM, GAPS, RUNLOG (design + first executable runs). code/: v0.1 draft on dspy 3.4 (INIT/ROUTE/EXPERT/MISSING/ANSWER as dspy calls, machine as fixed code, stub engines — honest Unverified). knowledge/: DESIGN.md for the KG-first knowledge expert (pre-implementation).
This commit is contained in:
2026-10-13 13:39:31 +02:00
commit d0942bdc69
18 changed files with 1844 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
"""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
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")