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(integrations): keep the abstention gate fail-closed via core's _gate_confidence

Aashish93bf34a67227
diff --git a/laya/integrations/crewai.py b/laya/integrations/crewai.py
index 808efadd..e8fe02d8 100644
--- a/laya/integrations/crewai.py
+++ b/laya/integrations/crewai.py
@@ -56,7 +56,7 @@ class LayaTaskGuardError(ValueError):
# them came to forward only `model`.
from ._controls import budget_kwargs as _budget_kwargs, hook_kwargs as _hook_kwargs # noqa: E402
from ._controls import predict_kwargs as _predict_kwargs, reject_remote_hooks as _reject_remote_hooks # noqa: E402
-from ..confidence import answer_confidence_value as _answer_confidence_value # noqa: E402
+from ..confidence import _gate_confidence # noqa: E402
# One class for every integration, so `except LayaLowConfidenceError` catches all of them.
from ._errors import LayaLowConfidenceError # noqa: E402
@@ -308,9 +308,11 @@ class LayaCrewRouter:
ans = res.get("answers", {}).get("delegation", {})
chosen_key = ans.get("choice")
- # The calibrated number core gates on, never the entropy field: `confidence` is on a
- # different scale (see `laya.confidence`), so a threshold on one must not read the other.
- conf = _answer_confidence_value(ans)
+ # Gate on the same number core's `flag_low_confidence` gates on: `answer_confidence`
+ # first, falling back to the entropy `confidence` so an answer that carries only the
+ # older field is still gated rather than silently passed (fail-closed). An answer with
+ # no usable number at all is treated as fully confident.
+ conf = _gate_confidence(ans)
if conf is None:
conf = 1.0
diff --git a/laya/integrations/langchain.py b/laya/integrations/langchain.py
index de96883a..78b20e16 100644
--- a/laya/integrations/langchain.py
+++ b/laya/integrations/langchain.py
@@ -29,7 +29,7 @@ except ImportError:
# The per-call control rules live in `._controls` because the CrewAI and LlamaIndex wrappers end
# at the same `predict` call and the same laya-serve body; keeping the rule in three places is how
# two of them ended up forwarding only `model`.
-from ..confidence import answer_confidence_value as _answer_confidence_value
+from ..confidence import _gate_confidence
from ._controls import budget_kwargs as _budget_kwargs, hook_kwargs as _hook_kwargs
from ._controls import predict_kwargs as _predict_kwargs
from ._controls import reject_remote_hooks as _reject_remote_hooks
@@ -467,9 +467,11 @@ class LayaRouter(_BatchedRunnable, RunnableSerializable):
self.last_decision = result
ans = result["answers"][self.question_id]
choice = ans["choice"]
- # The calibrated number core gates on, never the entropy field: `confidence` is on a
- # different scale (see `laya.confidence`), so a threshold on one must not read the other.
- confidence = _answer_confidence_value(ans)
+ # Gate on the same number core's `flag_low_confidence` gates on: `answer_confidence`
+ # first, falling back to the entropy `confidence` so an answer that carries only the
+ # older field is still gated rather than silently passed (fail-closed). An answer with
+ # no usable number at all is treated as fully confident.
+ confidence = _gate_confidence(ans)
if confidence is None:
confidence = 1.0
diff --git a/laya/integrations/llamaindex.py b/laya/integrations/llamaindex.py
index 6706621c..3b2f58fd 100644
--- a/laya/integrations/llamaindex.py
+++ b/laya/integrations/llamaindex.py
@@ -68,18 +68,19 @@ from ._controls import predict_kwargs as _predict_kwargs, reject_remote_hooks as
# One class for every integration, so `except LayaLowConfidenceError` catches all of them.
from ._errors import LayaLowConfidenceError # noqa: E402
-from ..confidence import answer_confidence_value as _answer_confidence_value # noqa: E402
+from ..confidence import _gate_confidence # noqa: E402
def _gated_confidence(answer: Dict[str, Any]) -> float:
- """The threshold number for one answer: the calibrated field, defaulting to 1.0.
+ """The threshold number for one answer, defaulting to 1.0 when it reports none.
- Every selector and router in this module gates `confidence_threshold` on this, and
- they must all read the same quantity -- `answer_confidence`, the calibrated number
- core gates on, never the entropy `confidence` (see `laya.confidence`). An answer that
- reports no usable confidence is treated as fully confident, exactly as before.
+ Every selector and router in this module gates `confidence_threshold` through this, and it
+ delegates to core's `_gate_confidence` so the wrappers and `flag_low_confidence` read the
+ same quantity: `answer_confidence` first, falling back to the entropy `confidence` so an
+ answer that carries only the older field is still gated rather than silently passed. An
+ answer with no usable number at all is treated as fully confident, exactly as before.
"""
- conf = _answer_confidence_value(answer or {})
+ conf = _gate_confidence(answer or {})
return conf if conf is not None else 1.0
diff --git a/tests/test_crewai.py b/tests/test_crewai.py
index 2212e97d..3b53efc7 100644
--- a/tests/test_crewai.py
+++ b/tests/test_crewai.py
@@ -453,8 +453,12 @@ except Exception as exc:
# --------------------------------------------------------------- Confidence source
-# The threshold gates the calibrated field, never the entropy one: `confidence` is on a
-# different scale (see `laya.confidence`), so the two must not be interchangeable here.
+# The threshold gates on core's own gate number (`_gate_confidence`): `answer_confidence` first,
+# falling back to the entropy `confidence` so an answer that carries only the older field is
+# still gated rather than silently passed (fail-closed). The entropy-only checks below are the
+# ones that regress-protect that fallback: a "read `answer_confidence` or treat as fully
+# confident" rule -- which is what `answer_confidence_value` returns -- would let a 0.10 entropy
+# answer through a 0.80 gate, and those checks would go RED.
class DisagreeingAgent(MockLayaAgent):
@@ -480,6 +484,25 @@ try:
except LayaLowConfidenceError as err:
check("gate/error carries the calibrated number", err.confidence, 0.4)
+
+# Fail-closed: an answer carrying ONLY the entropy field, below the threshold, is still gated.
+# This is the case a "calibrated number or nothing" reading gets wrong -- it would see no
+# `answer_confidence`, treat the answer as fully confident, and let a 0.10 answer past a 0.80
+# gate. Above the threshold the same shape passes.
+def _entropy_router(conf, **kwargs):
+ def response(state, questions):
+ return {"model": "mock-crew-router",
+ "answers": {"delegation": {"choice": "agent_0", "confidence": conf}}}
+ return LayaCrewRouter(agent=DisagreeingAgent(response), **kwargs)
+
+
+ent_low = _entropy_router(0.10, confidence_threshold=0.80, fallback_agent_index=1).route(
+ "anything", agents)
+check("gate/entropy-only below threshold is still gated (fail-closed)", ent_low.agent_index, 1)
+ent_high = _entropy_router(0.95, confidence_threshold=0.80, fallback_agent_index=1).route(
+ "anything", agents)
+check("gate/entropy-only above threshold passes", ent_high.agent_index, 0)
+
# An answer with no usable confidence keeps the old behaviour: treated as fully confident.
def _silent(state, questions):
return {"model": "mock-crew-router", "answers": {"delegation": {"choice": "agent_2"}}}