floop: typed ROUTE attendant (closed enum) via dspy structured output

ROUTE migrates to the typed-output law: the attendant is a closed enum
rendered into the prompt's JSON schema — an invalid pick is impossible
by contract (AdapterParseError after dspy's own re-ask).

- Attendant(str, Enum): knowledge logic analytic numeric floop user
  (str mixin keeps the machine's == and `in EXPERT_ENGINES` intact)
- The per-field desc now carries the routing guidance (what each
  attendant is FOR) — replacing the 'one of: ...' list in the old
  string contract
- The machine's invalid-attendant branch stays as a contract guard
- New tests/typed_route.py: enum parses through the real adapter path
  (RawEngine + cache=False), out-of-set attendant raises, str
  compatibility holds

VERIFIED: suite 13/13 green; wire test with real glm-5.3-flash routes
all three probes correctly (knowledge / numeric / knowledge).
This commit is contained in:
2026-10-22 15:03:48 +02:00
parent f953778f0a
commit c3d1b5d6d3
2 changed files with 105 additions and 2 deletions
+85
View File
@@ -0,0 +1,85 @@
"""Typed ROUTE tests: the attendant is a closed enum — invalid picks are
impossible by schema (a response outside the set raises AdapterParseError
after dspy's own re-ask).
No network; the engine returns raw text (DummyLM bypasses the adapter —
useless here). Run: .venv/bin/python tests/typed_route.py
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import warnings
warnings.filterwarnings("ignore")
import dspy
from dspy._vendor.lm15 import Response, TextPart, Message, Usage
from dspy.utils.exceptions import AdapterParseError
from floop.dspy_modules import FloopModules
def _lm(scripted_raw: str) -> None:
class RawEngine:
def complete(self, request):
return Response(
id="x", model="t",
message=Message(role="assistant",
parts=(TextPart(text=scripted_raw),)),
finish_reason="stop",
usage=Usage(input_tokens=1, output_tokens=1, total_tokens=2))
dspy.configure(lm=dspy.LM("openai/test", engine=RawEngine(), cache=False))
def test_route_valid_enum():
raw = '''[[ ## attendant ## ]]
knowledge
[[ ## reason ## ]]
A factual lookup of who held the office before.
[[ ## completed ## ]]'''
_lm(raw)
attendant, reason = FloopModules().route_question(
"Who was the President before Pedro Sanchez?", "catalog", "")
assert attendant == "knowledge", attendant
assert reason.startswith("A factual")
print("ok: typed ROUTE parses through the real adapter path")
def test_route_invalid_attendant_raises():
# outside the closed set -> schema violation -> AdapterParseError
# (dspy's JSON re-ask gets the same bad value back and gives up)
raw = '''[[ ## attendant ## ]]
wikipedia
[[ ## reason ## ]]
It's an encyclopedia.
[[ ## completed ## ]]'''
_lm(raw)
try:
FloopModules().route_question("q", "catalog", "")
raise AssertionError("invalid attendant parsed — enum contract broken")
except AdapterParseError:
print("ok: attendant outside the enum raises AdapterParseError (schema holds)")
def test_route_enum_compatibility():
# the machine's comparisons survive typing: Attendant mixes str
from floop.machine import EXPERT_ENGINES
from floop.dspy_modules import Attendant
assert Attendant.FLOOP == "floop"
assert Attendant.KNOWLEDGE in EXPERT_ENGINES
assert Attendant.USER not in EXPERT_ENGINES
print("ok: enum is str-compatible — machine comparisons unchanged")
if __name__ == "__main__":
test_route_valid_enum()
test_route_invalid_attendant_raises()
test_route_enum_compatibility()
print("\ntyped-ROUTE tests passed")