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.

Merge pull request #990 from aashish254/fix-hooks-errors-surfaces

Nandakishor0a7c5f632cac
diff --cc tests/test_hooks_api.py
index c052870a,6b68b584..77cbdf5d
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@@ -1067,88 -970,87 +1067,169 @@@ check("compile_cpu/varargs", _args.vara
check("compile_cpu/kwargs", _args.kwarg.arg, "kwargs")
+# --------------------------------- docs/hooks/api.md signature blocks: hooks_timeout is
+# named next to every hooks_raise the page shows. Router.__init__, Router.route and
+# ONNXAgent.__init__ used to omit it while Agent, load, predict, predict_long, predict_batch
+# and system_one all carried it, so a reader copying one of those three blocks got the
+# instance default instead of the per-call override.
+_api_md_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
+ "docs", "hooks", "api.md")
+with open(_api_md_path, encoding="utf-8") as _api_f:
+ _api_text = _api_f.read()
+
+_python_blocks = re.findall(r"```python\n(.*?)\n```", _api_text, re.DOTALL)
+_raise_signatures = []
+for _block in _python_blocks:
+ for _chunk in _block.split("\n\n"):
+ _chunk = _chunk.strip()
+ if "hooks_raise" in _chunk and "(" in _chunk:
+ _raise_signatures.append(_chunk)
+
+check_true("docs/hooks/api.md has hooks_raise signature blocks to gate",
+ len(_raise_signatures) >= 6,
+ "found %d" % len(_raise_signatures))
+_missing_timeout = [s.splitlines()[0].split("(")[0].strip()
+ for s in _raise_signatures if "hooks_timeout" not in s]
+check("docs/hooks/api.md every block that names hooks_raise also names hooks_timeout",
+ _missing_timeout, [])
+
+# Ban the pre-fix wordings so a future edit cannot just rename the parameter and go green.
+check_true("docs/hooks/api.md Router constructor no longer ends at hooks_concurrent",
+ "hooks_raise=True, hooks_concurrent=True,\n)\n\nrouter.route" not in _api_text)
+check_true("docs/hooks/api.md route() no longer ends at hooks_raise=None",
+ "hooks=None, hooks_raise=None)\n\nrouter.predict(" not in _api_text)
+check_true("docs/hooks/api.md ONNXAgent constructor no longer ends at hooks_concurrent",
+ "hooks_raise=True, hooks_concurrent=True)\n\nonnx_agent.system_one" not in _api_text)
+
+# Code truth: every documented surface really accepts hooks_timeout, so the ban cannot be
+# re-falsified by removing the parameter from the code.
+from laya.router import Router as _ApiRouter # noqa: E402
+from laya.onnx_agent import ONNXAgent as _ApiONNXAgent # noqa: E402
+from laya.agent import Agent as _ApiAgent # noqa: E402
+from laya import load as _api_load # noqa: E402
+
+for _label, _obj in [
+ ("Agent.__init__", _ApiAgent.__init__),
+ ("load", _api_load),
+ ("Agent.system_one", _ApiAgent.system_one),
+ ("Agent.predict_batch", _ApiAgent.predict_batch),
+ ("Router.__init__", _ApiRouter.__init__),
+ ("Router.route", _ApiRouter.route),
+ ("Router.predict", _ApiRouter.predict),
+ ("Router.predict_batch", _ApiRouter.predict_batch),
+ ("ONNXAgent.__init__", _ApiONNXAgent.__init__),
+ ("ONNXAgent.system_one", _ApiONNXAgent.system_one),
+]:
+ check("hooks_timeout is a real parameter of %s" % _label,
+ "hooks_timeout" in inspect.signature(_obj).parameters, True)
+# --------------------------------------------- docs/hooks/tracing.md nested-calls example
+# reads the child's `run_id`. `Agent.predict` returns `model`/`answers`/`usage` and `Router.predict`
+# adds `routing` -- no path returns a `run_id`, which is a `PredictContext` field
+# (laya/hooks.py:35). The pre-fix example's `child.get("run_id")` silently recorded `None`, so a
+# reader copying it linked every child span to no run at all.
+_tracing_md = os.path.join(REPO, "docs", "hooks", "tracing.md")
+with open(_tracing_md, encoding="utf-8") as _tf:
+ _tracing_text = _tf.read()
+
+check_true("docs/hooks/tracing.md drops the child.get('run_id') read",
+ 'child.get("run_id")' not in _tracing_text and 'child["run_id"]' not in _tracing_text)
+check_true("docs/hooks/tracing.md nested-call example reads child_ctx.run_id",
+ "child_ctx.run_id" in _tracing_text)
+check_true("docs/hooks/tracing.md nested-call example passes a per-call on_predict_start",
+ "on_predict_start=link" in _tracing_text)
+
+# Code truth: no laya predict path ever writes `"run_id":` as a payload key, so the ban cannot be
+# re-falsified by adding the key at a later refactor.
+for _src_rel in ("laya/agent.py", "laya/router.py", "laya/onnx_agent.py", "laya/serve.py"):
+ with open(os.path.join(REPO, _src_rel), encoding="utf-8") as _sf:
+ _src_text = _sf.read()
+ check('%s never writes "run_id" as a payload key' % _src_rel,
+ '"run_id":' in _src_text, False)
+
+# PredictContext really exposes run_id, which is the field the corrected example reads.
+_pc_fields = {f.name for f in dataclasses.fields(PredictContext)}
+check("PredictContext/run_id is a dataclass field", "run_id" in _pc_fields, True)
+ # --------------------------------------- docs/hooks/errors.md hooks_raise and hooks_timeout
+ # enumerations. Pre-fix the page named 4 surfaces for hooks_raise and 5 for hooks_timeout, so a
+ # reader never knew predict_long / ONNXAgent.predict_batch / ONNXAgent.predict_long /
+ # Router.route_batch / Router.predict_batch carried the controls. The code truth is: every
+ # public class-body `def` on Agent / Router / ONNXAgent that takes a `hooks_raise` or
+ # `hooks_timeout` kwarg, with the alias assignments (`predict = system_one`,
+ # `system_one = predict`, `predict = system_one`) excluded so the page does not double-list.
+ _errors_md = os.path.join(REPO, "docs", "hooks", "errors.md")
+ with open(_errors_md, encoding="utf-8") as _ef:
+ _errors_text = _ef.read()
+
+ check_true("docs/hooks/errors.md drops the pre-fix hooks_raise parenthetical",
+ "(`hooks_raise=` on `predict_batch`,\n`system_one`, `Router.route`, `Router.predict`)"
+ not in _errors_text)
+ check_true("docs/hooks/errors.md drops the pre-fix hooks_timeout four-plus-one list",
+ "per call on `predict_batch`, `system_one`,\n`Router.route`, `Router.predict` "
+ "and `ONNXAgent.system_one`" not in _errors_text)
+
+
+ def _paragraph(marker):
+ start = _errors_text.index(marker)
+ stop = _errors_text.find("\n\n", start)
+ return _errors_text[start:stop if stop != -1 else len(_errors_text)]
+
+
+ _QUALIFIED = re.compile(r"`([A-Z][A-Za-z]+\.[a-z_]+)`")
+ _raise_doc = set(_QUALIFIED.findall(_paragraph("It is set per instance and can be overridden")))
+ _timeout_doc = set(_QUALIFIED.findall(_paragraph("It can be set per instance or overridden")))
+
+ check_true("docs/hooks/errors.md hooks_raise paragraph names at least 11 surfaces",
+ len(_raise_doc) >= 11, "found %d" % len(_raise_doc))
+ check_true("docs/hooks/errors.md hooks_timeout paragraph names at least 11 surfaces",
+ len(_timeout_doc) >= 11, "found %d" % len(_timeout_doc))
+
+
+ def _canonical_surfaces(module_path, cls_name):
+ with open(module_path, encoding="utf-8") as f:
+ tree = ast.parse(f.read())
+ alias_names = set()
+ for node in tree.body:
+ if isinstance(node, ast.Assign) and isinstance(node.value, ast.Name):
+ for tgt in node.targets:
+ if isinstance(tgt, ast.Name):
+ alias_names.add(tgt.id)
+ for cls in tree.body:
+ if isinstance(cls, ast.ClassDef) and cls.name == cls_name:
+ for sub in cls.body:
+ if isinstance(sub, ast.Assign) and isinstance(sub.value, ast.Name):
+ for tgt in sub.targets:
+ if isinstance(tgt, ast.Name):
+ alias_names.add(tgt.id)
+ out = {"hooks_raise": set(), "hooks_timeout": set()}
+ for sub in cls.body:
+ if not isinstance(sub, ast.FunctionDef):
+ continue
+ if sub.name in alias_names or sub.name.startswith("_"):
+ continue
+ kwnames = {a.arg for a in list(sub.args.args) + list(sub.args.kwonlyargs)}
+ for kw in ("hooks_raise", "hooks_timeout"):
+ if kw in kwnames:
+ out[kw].add("%s.%s" % (cls_name, sub.name))
+ return out
+ raise AssertionError("%s not a top-level class in %s" % (cls_name, module_path))
+
+
+ _real_raise, _real_timeout = set(), set()
+ for _mod_rel, _cls in [("laya/agent.py", "Agent"),
+ ("laya/router.py", "Router"),
+ ("laya/onnx_agent.py", "ONNXAgent")]:
+ _per = _canonical_surfaces(os.path.join(REPO, _mod_rel), _cls)
+ _real_raise |= _per["hooks_raise"]
+ _real_timeout |= _per["hooks_timeout"]
+
+ check("docs/hooks/errors.md hooks_raise surfaces match laya's class bodies",
+ _raise_doc, _real_raise)
+ check("docs/hooks/errors.md hooks_timeout surfaces match laya's class bodies",
+ _timeout_doc, _real_timeout)
+ check_true("laya's AST scan finds at least 9 hooks_raise surfaces",
+ len(_real_raise) >= 9, "found %d" % len(_real_raise))
+ check_true("laya's AST scan finds at least 11 hooks_timeout surfaces",
+ len(_real_timeout) >= 11, "found %d" % len(_real_timeout))
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))