Skip to content

fix(shortlist): refuse an agent that has no tok or model.encoder - #1406

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:shortlist-agent-attributes
Oct 28, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:shortlist-agent-attributes

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

embed_fn_from_agent checks the agent shape the way every other entry point in laya/shortlist.py already does: getattr for the two attributes its docstring names, and a TypeError that says so. Two lines of reads become four lines of checks; nothing that works today changes.

Why

laya/shortlist.py:237 states the contract -- "The callable embeds a list of strings with agent.tok and agent.model.encoder" -- and the function body read both with no check at all (:253-254):

    tok = agent.tok
    encoder = agent.model.encoder

So a caller who hands over something that is not a loaded checkpoint -- a wrapper, a half-built agent, a stub from a test fixture, an agent whose encoder was never attached -- got the interpreter's own message, naming neither the helper nor what the helper needs. Measured on a pristine b9fe351 worktree, calling it directly:

NoAttrs              -> AttributeError: 'NoAttrs' object has no attribute 'tok'
TokOnly              -> AttributeError: 'TokOnly' object has no attribute 'model'
ModelWithoutEncoder  -> AttributeError: 'M' object has no attribute 'encoder'

That is the one leak in a function that otherwise validates its inputs hard: max_length and batch_size both refuse a bool and a non-positive at :246-250, each naming itself. And the same module refuses exactly this class of mistake elsewhere -- _call_predict at :530-534 does getattr(agent, "predict", None), falls through to system_one, and raises TypeError("agent must provide predict or system_one"). The third row above is the case that guard exists for: an agent that has tok and model but no encoder is the same caller error, and an isinstance(agent, ...) test would not catch it.

The check is placed before import torch (:252) so a wrong argument does not pay for the torch import -- the refusal is now the cheapest path through the function, which matters for the callers this helper is aimed at (a CPU-only box that shortlists without loading a head).

How it was verified

LAYA_WT=$PWD .venv/bin/python -B tests/test_shortlist.py   # 239 passed, 0 failed
LAYA_WT=$PWD .venv/bin/python -B tests/test_shortlist.py   # same suite on pristine b9fe351: 235 passed, 0 failed
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/

tests/test_shortlist.py gains three agents next to the existing encoder/bad max_length / encoder/bad batch_size cases -- NoAttrs, TokOnly, ModelWithoutEncoder -- each asserted as a TypeError, plus one case that pins the message string itself and records a leaked AttributeError as a failure rather than letting it pass a loose regex.

Mutation arm, the guard reverted to the two bare attribute reads with the tests kept:

235 passed, 4 failed
  FAIL encoder/agent/NoAttrs refused: raised AttributeError ('NoAttrs' object has no attribute 'tok'), want TypeError
  FAIL encoder/agent/TokOnly refused: raised AttributeError ('TokOnly' object has no attribute 'model'), want TypeError
  FAIL encoder/agent/ModelWithoutEncoder refused: raised AttributeError ('M' object has no attribute 'encoder'), want TypeError
  FAIL encoder/agent refusal message: leaked AttributeError ('NoAttrs' object has no attribute 'tok')

For the ports: embed_fn_from_agent is deliberately not ported (laya-java/.../Shortlist.java:48, laya-go/shortlist/doc.go:17 both say the torch-dependent encoder helper stays Python-only), and laya-java/scripts/gen_fixtures.py pins only cached_embed_fn / _embeddings, neither of which this touches -- gen_fixtures.py --check reports the same two stale families (lang_detect.json, python_json.json, the Linux/macOS regeneration noted at gen_fixtures.py:1922) as a base worktree. No open PR touches laya/shortlist.py or tests/test_shortlist.py.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main (b9fe351c, current HEAD)
  • Tests pass locally (239 passed, 0 failed; 235 at base)
  • Docs or examples updated when the public API changed -- no new public surface; the docstring at :237 already documented tok and model.encoder, so this makes existing prose enforceable rather than changing it

Fixes #

embed_fn_from_agent validated max_length and batch_size but read agent.tok and
agent.model.encoder with no check, so passing something that is not a loaded
checkpoint answered with the interpreter's own AttributeError -- naming neither
the helper nor the missing attribute. _call_predict in the same module already
refuses that mistake for the agent it uses; this is the other half.
@NandhaKishorM
NandhaKishorM merged commit 962071c into NandhaKishorM:main Oct 28, 2026
52 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 28, 2026
24 PRs. Vietnamese kept off the English checkpoint (#1392), deciding-window
attribution preserved through end hooks (#1369), RemoteRouter identifying
itself with a configurable User-Agent (#1396), ten more inputs refused where
they enter (#1372, #1394, #1397, #1398, #1400, #1401, #1402, #1403, #1404,
#1405, #1406), per-language temperatures and per-call budgets in .NET and
Rust (#1370, #1371), .NET calibration files (#1384), the Go router lifecycle,
SHA-256 pins and routed inference (#1374, #1379, #1395), a C++ foundation
(#1399, #1407), and parity-lane sharding that replaces the budget headroom
added for 0.4.16 (#1373, #1375).

Verified before tagging: all 92 suites pass, 640 tests, up from 612. ruff
clean on both lists. laya-ts 1251 tests across 44 files, tsc --noEmit clean.
Every generated-artifact gate clean on CPython 3.12 / unicodedata 15.0.0:
Java 20 families match laya (9 unverified), UnicodeTables matches CPython
15.0.0, LanguageTables matches laya/lang.py, three Go tables_gen.go match,
both laya-cpp .inc files match. Version agrees across pyproject.toml,
laya.__version__ and nix/package.nix. README carries 0.4.17 only.

Security audit of the merge diff, 362766 added lines over 480 files: zero
obfuscation, zero secrets, zero boundary hits, zero binaries, no mode or
symlink changes, 20 of 20 actions SHA-pinned including the new cpp.yml, npm
install hooks unchanged, and no third-party host in any added line
(example.com 90, github.com 2, download.pytorch.org 1, docs.rs 1). The 12
dynamic-exec hits are the C++ test harness invoking its own checker through
sys.executable, the Go sample generator reading laya's own example by AST at
development time, and one comment in rust.yml.

Held: #1364 (unchanged, it moves a cross-port contract), #1381 (a #[doc(hidden)]
but pub Rust hook that would ship as public API able to replace the forward
pass), #1386 (route_batch switched from self.route to self._route, silently
bypassing subclass overrides the docstring promises), and nine stacked port
PRs that conflict with each other and need linearizing (#1377, #1378, #1380,
#1382, #1383, #1385, #1387, #1388, #1389, #1390).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged. The two attributes the docstring names were the only ones in the function never checked, and that asymmetry is the real argument for this change: _call_predict already refuses the same mistake for the agent it uses, so the helper was inconsistent with its own sibling rather than merely terse.

Reading them with getattr and raising before import torch is the detail that makes it better than a plain guard. A wrong argument should not pay for a heavy import first, and agent.model.encoder needed the nested lookup anyway since an agent can carry a model with no encoder.

Splitting the test into NoAttrs and TokOnly covers both halves rather than just the empty object, which is the case that would otherwise regress silently if someone later checked only tok.

Shipped in 0.4.17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants