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(hooks): link every state of a call in the nested-call pattern

Aashish94e4c7dd7048
diff --git a/docs/hooks/tracing.md b/docs/hooks/tracing.md
index d01c4434..c63b8e21 100644
--- a/docs/hooks/tracing.md
+++ b/docs/hooks/tracing.md
@@ -160,10 +160,14 @@ independent unless you link them yourself. Capture the parent id and pass it alo
```python
def enrich(ctx):
- child = enricher.predict(ctx.states[0], EXTRA_QUESTIONS)
- record_child_span(parent_run_id=ctx.run_id, child_run_id=child.get("run_id"))
+ 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"))
```
+One parent `run_id`, one child call per state. A body that reads `ctx.states[0]` links the first
+decision of a batch and silently drops the rest.
+
Guard against recursion (see [anti-patterns](patterns.md#recursive-predict)); the easiest guard
is a separate `enricher` agent with no hooks.
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 635b3e21..59582f32 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -353,6 +353,42 @@ check("patterns.md/enrich stays aligned with states",
enrich(ctx)
check("patterns.md/enrich still guards the recursion", len(ctx.results), len(BATCH))
+
+class ChildCaller(Enricher):
+ """The nested-call page's stand-in enricher: it remembers the child calls it is asked to make."""
+
+ def __init__(self):
+ self.calls = []
+
+ def predict(self, state, questions):
+ result = Enricher.predict(self, state, questions)
+ result["run_id"] = "child-%d" % (len(self.calls) + 1)
+ self.calls.append(state)
+ return result
+
+
+SPAN_LINKS, CHILD = [], ChildCaller()
+nested = taught("docs/hooks/tracing.md", "enrich", after="## Nested calls",
+ enricher=CHILD, EXTRA_QUESTIONS=ASK,
+ record_child_span=lambda **kw: SPAN_LINKS.append(kw))
+check_true("docs/hooks/tracing.md/enrich reads every state, not index 0",
+ "[0]" not in nested.__taught_body__,
+ "the body still indexes [0]: %r" % nested.__taught_body__)
+nested_ctx = taught_ctx()
+nested(nested_ctx)
+check("docs/hooks/tracing.md/enrich makes one child call per state", CHILD.calls, BATCH)
+check("docs/hooks/tracing.md/enrich links every child to the parent run_id",
+ [link.get("parent_run_id") for link in SPAN_LINKS], [nested_ctx.run_id] * len(BATCH))
+check("docs/hooks/tracing.md/enrich records each child's own run_id",
+ [link.get("child_run_id") for link in SPAN_LINKS],
+ ["child-%d" % i for i in range(1, len(BATCH) + 1)])
+SPAN_LINKS.clear()
+CHILD.calls.clear()
+one_child = taught_ctx(states=[BATCH[1]], confidence=[0.4])
+nested(one_child)
+check("docs/hooks/tracing.md/enrich on one state still links", SPAN_LINKS,
+ [{"parent_run_id": one_child.run_id, "child_run_id": "child-1"}])
+
# ------------------------------------------------ taught token-budget bodies size the whole call
#
# A start hook's ctx.head_max_len / ctx.max_len REPLACE the budgets in force (laya/agent.py), and