Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(agent): catch an in-place question rewrite in the scan-budget guard

thread3d5f29170a12e5
diff --git a/laya/agent.py b/laya/agent.py
index 82342ade..0a335cc9 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -1,4 +1,5 @@
"""High-level inference runtime for laya System 1 decision models."""
+import copy
import json
import os
import tempfile
@@ -1560,6 +1561,15 @@ class Agent(HookRegistry):
internal = {qid: self._to_internal(questions[qid]) for qid in ids}
budget, step, _ = window_budget(self.tok, [internal[qid] for qid in ids], max_len,
head_max_len, window=window, stride=stride)
+ # Snapshot the questions the scan was just sized against, BEFORE any start hook can
+ # rewrite them, and compare against this instead of `questions` itself. `==` over the
+ # mapping cannot see an in-place rewrite: a hook that adds options to
+ # `ctx.questions[q]["criteria"]` -- the `widen_for_high_cardinality` pattern in
+ # `docs/hooks/patterns.md`, and the case this guard exists for -- mutates the same nested
+ # dict the caller's mapping holds, so both sides change together and compare equal. A deep
+ # copy is independent, so `_check_scan_budget` sees the difference; a shallow one would
+ # share the nested dicts and miss it exactly as before.
+ asked = copy.deepcopy(questions)
state_ids = self.tok(serialize_state(state).replace(self.tok.mask_token, " "),
add_special_tokens=False)["input_ids"]
@@ -1579,7 +1589,7 @@ class Agent(HookRegistry):
# fit one window was silently truncated by a re-budgeting hook and still reported
# `windows: 1`, i.e. "the model read all of it" -- measured, 138 of 240 state tokens
# never reached the model, while a longer document on the identical input hard-failed.
- _check_scan_budget(self, evidence, budget, max_len, head_max_len, questions)
+ _check_scan_budget(self, evidence, budget, max_len, head_max_len, asked)
single["usage"] = {**(single.get("usage") or {}), "windows": 0 if evidence["answered"] else 1}
return single
@@ -1605,7 +1615,7 @@ class Agent(HookRegistry):
batch_size)
results = self.predict_batch(list(windows), questions, batch_size=cap, lang=lang,
**_with_start_probe(hook_kwargs, probe))
- _check_scan_budget(self, evidence, budget, max_len, head_max_len, questions)
+ _check_scan_budget(self, evidence, budget, max_len, head_max_len, asked)
if evidence["answered"]:
# The hook replaced the call before any window was scored. Aggregating over its payload
diff --git a/tests/test_predict_long.py b/tests/test_predict_long.py
index 20095827..638ca9c2 100644
--- a/tests/test_predict_long.py
+++ b/tests/test_predict_long.py
@@ -997,6 +997,28 @@ check_raises("hook budget/the smallest room wins across questions", ValueError,
_probe_agent, _evidence(questions={"a": q_many(2), "b": q_many(16)}), 72, *_CFG,
asked={"a": q_many(2)}))
+
+# A hook that widens a question IN PLACE is the case the guard was written for, and the one a
+# shallow snapshot missed. `predict_long` has to compare against the questions as they were when it
+# sized the scan, not against the caller's mapping, which the hook mutates too: with the same object
+# on both sides `questions == asked` stayed True, the checker returned early, and the scan proceeded
+# with windows `build_sequence` would re-truncate -- silently worse than not windowing at all.
+# `widen_for_high_cardinality` (docs/hooks/patterns.md) is exactly this shape. The questions are a
+# fresh mapping because the hook mutates what it is handed.
+_inplace_questions = {"dept": {"type": "choice", "instructions": "?",
+ "criteria": {"a": "x", "b": "y"}},
+ "flag": dict(Q["flag"])}
+
+
+def _widen_in_place(ctx):
+ ctx.questions["dept"]["criteria"].update(q_many(16)["criteria"])
+
+
+_widened, _widen_exc = _attempt(
+ lambda: make_real_agent().predict_long(LONG, _inplace_questions,
+ on_predict_start=_widen_in_place))
+check("hook budget/an in-place question rewrite is refused", _kind(_widen_exc), "ValueError")
+
# What the probe RECORDS is half the check: synthesising an evidence dict in the tests above leaves
# the recording itself unpinned, and dropping either field made the checker silently inert.
_rec_probe, _rec = _start_evidence()