machine: robust INIT parsing + raw-response log (run I fixes)

Run I ('Who was the President before Pedro Sanchez?'): the INIT LM wrote
its own line numbers as an extra first pipe field ('1 | Who was... |
none | OPEN'), the naive parse took '1' as the subquestion text, ROUTE
classified Q1='1' as numeric, and the machine burned four calls on
garbage before luck produced the answer. Two of the fixes close the same
observability gap that hid the root cause for a day.

1. PARSER (parse_subquestions): shifts the numbered form (>=4 fields
   with a bare number first) and drops any text that is a bare number —
   a routable question is text, never '1'. Enumerator prefixes ('1.')
   inside the text field are stripped. Well-formed lines parse exactly
   as before (regression-tested).

2. INIT DEGRADE (machine): init_decompose now returns (subs, raw).
   Non-empty raw that parses to nothing = model garbage — degrade to
   the ORIGINAL question as one OPEN subquestion (routable, logged,
   budget-safe). Empty raw keeps the old law: the question is atomic,
   stop and synthesize — never fabricate a subquestion.

3. RAW RESPONSE LOG (the ask behind the ask): prompts/NN-stage.txt now
   carries the exact LM response after the prompt. Run I's parsed
   'OUTPUT INIT: 1\n2' could not show what the model actually wrote —
   the raw text is in the file from now on; the log stays line-based.

Tests: tests/robust_init.py — the numbered form, bare-number drop,
enumerator strip, happy-path regression, the degrade, the atomic stop,
raw-response capture. Suite 59/59. Smoke: the exact run-I transcript
replayed through the real machine (DummyLM) parses to Q1/Q2 real
subquestions and answers Mariano Rajoy.
This commit is contained in:
2026-10-21 12:08:50 +02:00
parent dcd189057e
commit b5a9403063
5 changed files with 237 additions and 10 deletions
+18 -1
View File
@@ -49,6 +49,7 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
self._stack: list[str] = [] # nested module stages (innermost last)
self._starts: dict[str, float] = {} # lm call_id -> monotonic start
self._stage_of: dict[str, str] = {} # lm call_id -> stage label
self._file_of: dict[str, Path] = {} # lm call_id -> prompt file
# -- dspy plumbing -------------------------------------------------------
@@ -78,11 +79,13 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
fn = f"{n:02d}-{label}.txt"
pdir = streams.run_dir / "prompts"
pdir.mkdir(exist_ok=True)
(pdir / fn).write_text(
f = pdir / fn
f.write_text(
f"# stage={label} call_seq={n} chars={chars} "
f"tokens~{chars // 4}\n\n" + text,
encoding="utf-8",
)
self._file_of[call_id] = f
streams.log(
f"prompt #{n} stage={label} chars={chars} tokens~{chars // 4} "
f"-> prompts/{fn}"
@@ -95,7 +98,21 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
streams = self.streams
label = self._stage_of.pop(call_id, "lm")
start = self._starts.pop(call_id, None)
f = self._file_of.pop(call_id, None)
dur = f"{time.monotonic() - start:.1f}s" if start is not None else "?"
# RAW RESPONSE into the same file: the exact LM text, unmodified —
# the parsed OUTPUT can lie by omission (run I)
if f is not None:
raw = ""
if isinstance(outputs, list):
raw = "\n".join(str(o) for o in outputs)
elif outputs is not None:
raw = str(outputs)
with f.open("a", encoding="utf-8") as fh:
fh.write(
f"\n\n# RAW RESPONSE (after {dur}"
+ (f", exception={exception!r}" if exception else "")
+ ")\n" + (raw or "(empty)"))
if streams is not None:
streams.log(f"prompt {label} done in {dur}")