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.

docs(hooks): tracing.md nested-call example must read the child run_id from ctx

Aashish77cf06fbe003
diff --git a/docs/hooks/tracing.md b/docs/hooks/tracing.md
index cae13a7e..dfcd67ad 100644
--- a/docs/hooks/tracing.md
+++ b/docs/hooks/tracing.md
@@ -157,13 +157,16 @@ serving concurrently. See [patterns](patterns.md#blocking-work).
## Nested calls
A hook that calls `predict` again starts a new call with a new `run_id`. The parent and child are
-independent unless you link them yourself. Capture the parent id and pass it along:
+independent unless you link them yourself. The child's `run_id` lives on the child's own
+`PredictContext`, not on the payload `predict` returns, so capture it with a per-call hook:
```python
def enrich(ctx):
for state in ctx.states: # a hook sees every state of the call
- child = enricher.predict(state, EXTRA_QUESTIONS)
- record_child_span(parent_run_id=ctx.run_id, child_run_id=child.get("run_id"))
+ def link(child_ctx, parent_run_id=ctx.run_id):
+ record_child_span(parent_run_id=parent_run_id, child_run_id=child_ctx.run_id)
+
+ enricher.predict(state, EXTRA_QUESTIONS, on_predict_start=link)
```
One parent `run_id`, one child call per state. A body that reads `ctx.states[0]` links the first
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 29f66cbd..f77d8292 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -794,15 +794,24 @@ check("patterns.md/enrich still guards the recursion", len(ctx.results), len(BAT
class ChildCaller(Enricher):
- """The nested-call page's stand-in enricher: it remembers the child calls it is asked to make."""
+ """The nested-call page's stand-in enricher.
+
+ Mirrors `Agent.predict` exactly: `state` and `questions` positional, `on_predict_start`
+ fired before the return with a fresh child `PredictContext`, and NO `run_id` in the
+ payload (that field lives on `PredictContext`, `laya/hooks.py:35`, and no predict path
+ writes it into the response dict).
+ """
def __init__(self):
self.calls = []
- def predict(self, state, questions):
+ def predict(self, state, questions, on_predict_start=None):
result = Enricher.predict(self, state, questions)
- result["run_id"] = "child-%d" % (len(self.calls) + 1)
self.calls.append(state)
+ child_ctx = taught_ctx(states=[state], confidence=[0.4])
+ child_ctx.run_id = "child-%d" % len(self.calls)
+ if on_predict_start is not None:
+ on_predict_start(child_ctx)
return result
@@ -970,6 +979,35 @@ check("compile_cpu/varargs", _args.vararg.arg, "args")
check("compile_cpu/kwargs", _args.kwarg.arg, "kwargs")
+# --------------------------------------------- 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)
+
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL:
print(" FAIL", f)