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(mcp): laya_shortlist's metadata list must name passthrough, the fifth key

Nandakishor2392525765cd
diff --git a/README.md b/README.md
index 600b184e..e1c3aa78 100644
--- a/README.md
+++ b/README.md
@@ -1440,7 +1440,8 @@ knows the answer shape never parses an answer map by hand. `laya_shortlist` is t
it shortlists a many-option choice question to its `k` most likely labels by embedding
similarity (mean-pooled from the answering checkpoint's own encoder, so no extra model is
downloaded), answers in one forward pass, and returns per-question shortlist metadata
-(kept labels, cosine scores, `k`, option count). The guardrails shown on every decision
+(kept labels, cosine scores, `k`, option count, and whether the question passed through
+unshortlisted). The guardrails shown on every decision
tool point clients to `laya_shortlist` for >20-option choices. As with the SDK, use it for structured
decisions only; not for open Q&A or
text generation. Tests: `tests/test_mcp.py` (CI, no weights) and
diff --git a/docs/cli-mcp.md b/docs/cli-mcp.md
index 97edf633..558a1c95 100644
--- a/docs/cli-mcp.md
+++ b/docs/cli-mcp.md
@@ -165,8 +165,10 @@ does not require dropping to Python. For the schema-driven form in more depth, s
The shared guardrail says not to send choice questions with more than 20 options without
shortlisting. `laya_shortlist` keeps the `k` most likely labels before the forward pass; its
default is `k=20`. It uses mean-pooled embeddings from the answering checkpoint's own encoder,
-so it does not download a second model, and returns the kept labels, cosine scores, `k`, and
-option count for each shortlisted question.
+so it does not download a second model, and returns the kept labels, cosine scores, `k`,
+option count, and whether the question passed through unshortlisted for each shortlisted
+question. A question with `k` at or past its option count is passed through unchanged, so
+its cosine scores come back `null` rather than empty.
`state` must be a non-empty JSON object. `questions` must be a non-empty object whose values use
Laya's typed question schema. `laya_preset` accepts the same five presets the CLI does: `email`,
diff --git a/laya/mcp/server.py b/laya/mcp/server.py
index 82a60ff3..41a57990 100644
--- a/laya/mcp/server.py
+++ b/laya/mcp/server.py
@@ -386,7 +386,7 @@ def laya_route_batch_tool(requests: list, hooks_timeout: float = 0) -> str:
"model is downloaded), then answer in one forward pass. Use this instead of "
"laya_predict whenever a choice question has more options than the guardrails allow. "
"Returns the answers plus per-question shortlist metadata (kept labels, cosine "
- "scores, k, option count). "
+ "scores, k, option count, and whether the question passed through unshortlisted). "
"Shortlisting narrows the label set; head_max_len decides how many tokens each kept label "
"is read with, so the two together are the fix for a large-criteria question. "
+ _GUARDRAILS
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index c8882ecf..4ba59d25 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -2569,6 +2569,79 @@ def test_server_shortlist_k_default():
"schema=%r library=%r" % (advertised, DEFAULT_SHORTLIST_K))
+def test_shortlist_metadata_keys_are_documented():
+ """Every key `predict_shortlist` publishes must be named by all three surfaces that list it.
+
+ `laya/shortlist.py` writes a five-key block per question: ``labels``, ``scores``, ``k``,
+ ``n`` and ``passthrough``. The tool description, the README and `docs/cli-mcp.md` each
+ paraphrase that block, and all three stopped at four -- they dropped ``passthrough``, the
+ one key that tells a caller whether the shortlist actually narrowed anything. A question
+ whose option count is at or below ``k`` is answered whole with ``scores`` set to ``None``,
+ so a client that assumes a real ranking was produced reads ``None`` as "no scores" rather
+ than "nothing to rank".
+
+ The key set is read out of `laya/shortlist.py`'s AST rather than typed here, so a sixth key
+ added to the metadata fails this gate on all three pages at once.
+ """
+ import ast
+ import re
+
+ root = Path(__file__).resolve().parents[1]
+ with open(root / "laya" / "shortlist.py") as f:
+ shortlist_src = f.read()
+ # Scoped to predict_shortlist's own body: predict_tournament also builds a `meta[qid]` dict,
+ # and a module-wide walk would depend on which one ast.walk happens to reach first.
+ body = [node for node in ast.walk(ast.parse(shortlist_src))
+ if isinstance(node, ast.FunctionDef) and node.name == "predict_shortlist"]
+ meta_keys = None
+ for node in ast.walk(body[0] if body else ast.parse("")):
+ if (isinstance(node, ast.Assign) and isinstance(node.value, ast.Dict)
+ and isinstance(node.targets[0], ast.Subscript)
+ and isinstance(node.targets[0].value, ast.Name)
+ and node.targets[0].value.id == "meta"):
+ meta_keys = [ast.literal_eval(k) for k in node.value.keys]
+ break
+ ok("shortlist/meta_keys read from the AST",
+ meta_keys == ["labels", "scores", "k", "n", "passthrough"],
+ "AST read %r from laya/shortlist.py" % (meta_keys,))
+
+ # Each key needs a concept word on the page; the paraphrase never uses the raw key names.
+ REQUIRED = {
+ "labels": r"label",
+ "scores": r"score",
+ "k": r"\bk\b",
+ "n": r"option count",
+ "passthrough": r"passed through|unshortlisted",
+ }
+
+ by_name = {t.name: t for t in asyncio.run(mcp_server.list_tools())}
+ surfaces = {
+ "tool description": by_name["laya_shortlist"].description,
+ "README": (root / "README.md").read_text(),
+ "docs/cli-mcp.md": (root / "docs" / "cli-mcp.md").read_text(),
+ }
+ for where, text in surfaces.items():
+ flat = " ".join(text.split())
+ for key, pattern in REQUIRED.items():
+ ok("shortlist/%s names %s" % (where, key),
+ re.search(pattern, flat) is not None,
+ "the %s never spells the %r metadata key (looked for /%s/)" % (where, key, pattern))
+
+ # The pre-fix wording stopped the list at the option count; a surface that closes the list
+ # there again has dropped a key, whatever else it says.
+ for where, text in surfaces.items():
+ flat = " ".join(text.split())
+ ok("shortlist/%s does not end the list at option count" % where,
+ re.search(r"option count[),. ]*(?:The|the|for each| `k`|$)", flat) is None
+ or re.search(r"option count, and whether", flat) is not None,
+ "the list still closes on the option count: %s" % where)
+
+
+# Called here rather than from the tail list, because that block is where three open PRs each
+# append a line; one insertion point keeps this gate's diff to the lines it actually owns.
+test_shortlist_metadata_keys_are_documented()
+
+
test_device()
test_real_device()
test_private_contract()