Repository navigation
fix(shortlist): refuse an agent that has no tok or model.encoder - #1406
Conversation
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.
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>
|
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: Reading them with Splitting the test into Shipped in 0.4.17. |
What
embed_fn_from_agentchecks theagentshape the way every other entry point inlaya/shortlist.pyalready does:getattrfor the two attributes its docstring names, and aTypeErrorthat says so. Two lines of reads become four lines of checks; nothing that works today changes.Why
laya/shortlist.py:237states the contract -- "The callable embeds a list of strings withagent.tokandagent.model.encoder" -- and the function body read both with no check at all (:253-254):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
b9fe351worktree, calling it directly:That is the one leak in a function that otherwise validates its inputs hard:
max_lengthandbatch_sizeboth 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_predictat:530-534doesgetattr(agent, "predict", None), falls through tosystem_one, and raisesTypeError("agent must provide predict or system_one"). The third row above is the case that guard exists for: an agent that hastokandmodelbut no encoder is the same caller error, and anisinstance(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
tests/test_shortlist.pygains three agents next to the existingencoder/bad max_length/encoder/bad batch_sizecases --NoAttrs,TokOnly,ModelWithoutEncoder-- each asserted as aTypeError, plus one case that pins the message string itself and records a leakedAttributeErroras 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:
For the ports:
embed_fn_from_agentis deliberately not ported (laya-java/.../Shortlist.java:48,laya-go/shortlist/doc.go:17both say the torch-dependent encoder helper stays Python-only), andlaya-java/scripts/gen_fixtures.pypins onlycached_embed_fn/_embeddings, neither of which this touches --gen_fixtures.py --checkreports the same two stale families (lang_detect.json,python_json.json, the Linux/macOS regeneration noted atgen_fixtures.py:1922) as a base worktree. No open PR toucheslaya/shortlist.pyortests/test_shortlist.py.Checklist
main(b9fe351c, current HEAD):237already documentedtokandmodel.encoder, so this makes existing prose enforceable rather than changing itFixes #