knowledge+llm: dst-first ablation mask (run L) + re-ask budget meter

Two fixes from his 22/10 re-run log, both small and deterministic:

1. THE ABLATION MASK WAS QUOTE-BLINDED (run L: Q1 stayed silent over
   the Pedro page that run J flags). The Pedro paragraph interleaves
   all three readings in ONE quote ('...Secretary-General ... ninth
   president of Socialist International...'), and _carries matched
   the picked value by substring over dst OR quote — so masking SI's
   support masked the PM/PSOE rows too; round 1 answered UNKNOWN and
   the audit died silent. The DST-FIRST LAW: when a row has a
   meaningful dst (non-empty, non-numeric) it is the VALUE BEARER and
   the mask matches IT; the quote is a fallback only for
   empty/numeric dsts. Same law in _carries_target for each round's
   surviving value, with one addition: the judge's round value may be
   an OFFICE DESCRIPTION ('prime minister of Spain') while the row's
   dst is the ENTITY ('Spain') — resolve by reciprocal token
   containment, or the loop dies after one alternative.

2. THE ADAPTER RE-ASK WAS INVISIBLE TO THE BUDGET (run L: prompts #9
   and #10 were both ANSWER — #10 5 chars shorter, 340s, the
   ChatAdapter's JSON-fallback second wire call after a parse
   failure; llm_calls said 9/80 while 10 hit the wire). THE METER:
   the prompt-log callback already sees every LM call — a second LM
   call with NO new module forward between them is the adapter
   fallback re-firing inside the same Predict. Count it, log the
   line, and charge the budget through a meter hook the machine
   wires at construction. A re-ask at the ceiling does not kill the
   run (the call already happened). dspy's BEHAVIOUR IS UNTOUCHED —
   his call: the fallback stays, it is future self-improvement
   signal (which prompts the model formats badly), not a bug.

Tests: knowledge/tests/ablation.py +3 — run-L regression (interleaved
one-paragraph quote flags all three readings), office-description
resolution, numeric-dst quote fallback. tests/reask_meter.py +3 —
fallback detected and charged on the REAL adapter path (custom engine
returning raw text, marker-less first response), separate forwards NOT
flagged (the ablation-rounds shape), ceiling charge survives. Suite
12/12 files green (74 tests). Live smokes: the run-L interleaved-quote
page flags SI+PM+SG through the full pipeline; the run-J
separate-quotes smoke still passes.
This commit is contained in:
2026-10-22 11:21:25 +02:00
parent 2de9aaa738
commit efd539c128
5 changed files with 327 additions and 13 deletions
+31
View File
@@ -42,6 +42,14 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
(charge happens before the Predict runs), so 08-extract.txt IS the
call 8/80 of run G that hung for minutes. Plus size/duration lines in
run.log; the exact prompt is in the file, log stays line-based.
RE-ASK METER (run L): dspy's chat adapter, on a parse failure, re-fires
the SAME module's LM call through its JSON fallback — an extra wire call
the budget counter never charged (9/80 logged, 10 on the wire, the
second burning 340s of GPU). The meter sees them (same module depth in
the stack twice), charges the budget via an optional meter hook, and
logs the line — dspy's BEHAVIOUR is untouched (his call: the fallback
stays; it is future self-improvement signal, not a bug to fix).
"""
def __init__(self, streams) -> None:
@@ -50,6 +58,8 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
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
self.reasks = 0 # LM calls the budget did not charge
self.meter = None # callable(str stage) -> None; budget charge
# -- dspy plumbing -------------------------------------------------------
@@ -57,6 +67,7 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
sig = getattr(getattr(instance, "signature", None), "__name__", None)
label = _STAGE_BY_MODULE.get(sig or "", sig or type(instance).__name__)
self._stack.append(label)
self._fresh_forward = True # this forward's first LM call is not a re-ask
def on_module_end(self, call_id, outputs, exception):
if self._stack:
@@ -74,6 +85,26 @@ class PromptLogCallback(dspy.utils.callback.BaseCallback):
if isinstance(m, dict)
)
chars = len(text)
# RE-ASK METER: a second LM call with NO new module forward between
# them is dspy's adapter fallback re-firing inside the same Predict
# (ChatAdapter catches AdapterParseError and re-calls via JSONAdapter)
# — an extra wire call the budget did not charge. Consecutive SAME-
# stage calls from DIFFERENT forwards (the ablation rounds) are NOT
# re-asks: each round opens its own module.
fresh = getattr(self, "_fresh_forward", False)
self._fresh_forward = False
self._lm_seen = getattr(self, "_lm_seen", 0) + 1
reask = self._lm_seen > 1 and not fresh
if reask:
self.reasks += 1
if streams is not None:
streams.log(f"re-ask detected: stage={label} "
f"(adapter parse-failure fallback, charged to budget)")
if self.meter is not None:
try:
self.meter(label)
except Exception: # noqa: BLE001 — metering must never kill a run
pass
if streams is not None:
n = streams.bump_prompt_seq()
fn = f"{n:02d}-{label}.txt"