Skip to content

test(lazy-import): pin laya.mcp's documented no-extra import surface - #1259

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:test-mcp-lazy-import
Oct 17, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:test-mcp-lazy-import

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

tests/test_lazy_import.py gains a mcp-blocked probe that pins the import surface
laya/mcp/__init__.py documents. Test-only: +69/-0, one file, no production line touched.

Why

laya/mcp/__init__.py:12-15 promises:

The submodules device, remote and tools are importable without the mcp dependency;
only server requires the extra.

The sentence is true today and checked by nothing. tests/test_mcp.py:26-28 is the suite that would
have the most to say about it, and it is the one suite that cannot: it exits 0 at the top of the file
when import mcp fails, so on an install without the extra it reports nothing, and both CI lanes that
run the shared suite list install the extra — pip install -e ".[mcp,serve,langchain]" at
.github/workflows/ci.yml:94 and :225, with python scripts/test_suites.py at :116 and :250. So
import mcp always succeeds where the suite runs, and the documented pure-Python path is never
exercised anywhere.

That is exactly the shape of gap tests/test_lazy_import.py exists to close — the file already blocks
torch and tensorflow through sys.meta_path and pins what survives (import laya,
laya.detect_script, laya.PINNED_REVISIONS, and import laya.serve staying cheap) — and it never
covers the one package that documents a dependency of its own. laya/mcp/tools.py is where tool
schemas are written, so a future from mcp.types import ... at its top is a plausible edit; it would
break the documented install path for every user without the extra and leave CI green, because in CI
the import succeeds.

The new block adds six checks in two subprocess arms, following the file's existing pattern of
reporting what happened rather than asserting blindly:

  • mcp blocked: laya.mcp, .device, .remote, .tools all import, and mcp is absent from
    sys.modules afterwards — the four are one assert, so a leak fails the arm rather than printing
    a green line.
  • mcp blocked: laya.mcp.server fails as an ImportError whose text names both laya[mcp] and
    pip install. The two substrings are pinned, not the sentence: a reworded hint stays green, No module named 'mcp' — which is what the try/except at laya/mcp/server.py:45-52 becomes if that
    re-raise is ever removed — does not.
  • Both mcp and torch blocked: the same three submodules still import and neither dependency
    reaches sys.modules. remote reaches ..router and ..serve, tools reaches
    ..confidence/..hooks/..presets, so this is the file's own torch-free premise extended to the
    MCP layer rather than a second copy of the first arm.

Blocking is done with a find_spec hook that raises, so the probe is meaningful whether or not the
extra is installed here: it is the absence of a module-level import under test, not the absence of
a package.

How it was verified

LAYA_TREE="$PWD" python -B laya-bench/ci_gate.py
#   tree=…/laya-mcp-lazy  laya_from=…/laya-mcp-lazy/laya/__init__.py
#   all 88 suites passed          → ci_gate exit=0

python -B tests/test_lazy_import.py   # 19 passed, 0 failed   (base: 13 passed, 0 failed)
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120   # All checks passed
python -m compileall -q laya/ tests/                                  # exit 0

Both mutation arms were run against this branch's test file and reverted with git checkout --:

  • A module-level from mcp.types import ToolCallToolResult added to laya/mcp/tools.py →
    13 passed, 6 failed: every new check fails, the 13 pre-existing ones still pass. This is the
    edit the docstring forbids, and before this branch nothing in the repo noticed it.
  • laya/mcp/server.py's guard neutered to except ImportError: pass (so the raw NameError from
    MCPServer(...) escapes instead of the install hint) → 16 passed, 3 failed:
    no-mcp/import laya.mcp succeeds, no-mcp/server fails as an ImportError, and
    no-mcp/server names the extra and how to install it. The three submodule-import checks stay green,
    which is the scoping being right rather than lucky.

Measured on the environment as it is: mcp is installed in the venv
(.venv/lib/python3.14/site-packages/mcp/__init__.py) and find_spec("mcp") resolves, so the
Blocker is refusing a dependency that is really present, not a module that was never there.

Deconfliction: of the 66 currently open PRs, none touches tests/test_lazy_import.py. Four touch
laya/mcp/ — #1242 (device.py, server.py), #1203 (tools.py), #738 (server.py, tools.py),
#687 (remote.py) — and none of them edits this file, so there is no competing hunk. Since the new
checks read those modules' behaviour, each was merged against 4ac226d5 with
git merge-tree --write-tree and its laya/mcp/*.py read out of the result tree: all four merged
trees have 0 module-level import mcp / from mcp lines in __init__.py, device.py, remote.py
and tools.py, and none adds a module-level torch/numpy/laya.agent import anywhere under
laya/. So these checks go green with each of them, not against them.

Rebased on current main: git ls-remote upstream refs/heads/main =
4ac226d5ee88ed6df8db288a05c766d7a34ee682, this branch's parent. No new dependency, no hosted
service, no behaviour change. Diff scanned for credentials: 0 matches on the 69 added lines.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed

Fixes #

laya/mcp/__init__.py promises that device, remote and tools import without the
mcp extra and that only server requires it. Both CI lanes that run the suite
list install -e ".[mcp,serve,langchain]", so `import mcp` always succeeds there,
and tests/test_mcp.py exits 0 when the extra is missing -- so nothing exercised
the promise, and a module-level mcp import added to tools.py would break every
install without the extra with CI still green.

Block mcp through sys.meta_path (the file's existing shape for torch and
tensorflow) and pin: the three submodules import, laya.mcp pulls in neither mcp
nor torch, and server fails as an ImportError that names the extra and the
install line.
@aashish254

Copy link
Copy Markdown
Contributor Author

Short answer: this branch also goes green against my own #1257, which edits laya/mcp/server.py.

git merge-tree --write-tree e3cd18ff <1257 head 58e4a981> is clean (tree 3c98dd42, commit 551f4a9d). Running tests/test_lazy_import.py from that merged tree gives 19 passed, 0 failed, with laya_from= pointing at the merged tree. #1257's change is inside the door handler, not the ImportError guard at laya/mcp/server.py:45-52, so the no-mcp/server names the extra and how to install it check is unaffected either way.

@aashish254

Copy link
Copy Markdown
Contributor Author

Short answer: the one red lane is a pre-existing Windows crash in a suite this PR does not touch.

tests (windows) fails with tests/test_runtime_fixes.py [exit 3221225501 (0xC000001D)], i.e. STATUS_ILLEGAL_INSTRUCTION from native code, not an assertion. The same lane on my #1168 fails on the identical suite name, and #1147's windows log ends in Illegal instruction ... python tests/<t>.py with exit 132. Nothing in this diff but tests/test_lazy_import.py (+69/-0), and that file's own suite reports all lazy-import tests passed on the windows runner in #1147's log.

The same run's log also shows the Windows runner reaching this with InductorError: InvalidCxxCompiler: Compiler: cl is not found, which is the compiler-less-runner context for the SIGILL.

@NandhaKishorM
NandhaKishorM merged commit 83f78b5 into NandhaKishorM:main Oct 17, 2026
42 of 43 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged in 0.4.9.

This closes a gap that was invisible by construction. laya/mcp/__init__.py promises that device, remote and tools import without the mcp extra, and both lanes that run this suite install -e ".[mcp,serve,langchain]", so import mcp always succeeded there and tests/test_mcp.py skips wholesale when the extra is absent. A module-level from mcp.types import ... added to tools.py would have broken every install without the extra and left CI green.

Blocking it as an ImportError rather than leaving it missing is the right shape, so reaching for it fails at the exact import. And asserting that the server refusal names both laya[mcp] and pip install, rather than matching the whole sentence, keeps the message free to be reworded while refusing a bare "No module named 'mcp'".

The second probe is the one I would have asked for: this file's premise is that the surface is pure Python, and remote reaches for laya.serve and laya.router while tools reaches for laya.confidence, hooks and presets, so blocking torch as well is what actually holds that.

The Windows check that failed here is not yours, and your run is the one that solved it. With the stage markers from 0.4.8 the log printed [stage] state reused once, [stage] autocast, then exit 3221225501 (0xC000001D), which is STATUS_ILLEGAL_INSTRUCTION inside the bf16 CPU autocast forward. I fixed that in the same release: the suite now qualifies the CPU in a child process, and docs/reference/agent.md says what LAYA_CPU_AMP=bf16 needs from the hardware.

joyanes97 pushed a commit to joyanes97/laya that referenced this pull request Oct 17, 2026
…hild

The stage markers from 0.4.8 answered on their first occurrence. NandhaKishorM#1259's Windows
run printed

    [stage] state reused once
    [stage] autocast
    ...
    tests/test_runtime_fixes.py [exit 3221225501 (0xC000001D)]

so the intermittent fault is inside the autocast section, which is the only one
that asks torch for a bf16 CPU forward. 0xC000001D is STATUS_ILLEGAL_INSTRUCTION:
oneDNN selects a kernel for an instruction set the CPU does not have. Same class
as NandhaKishorM#1065, where fbgemm raised SIGILL on a CPU with SSE4.2 and no AVX2. It is
intermittent on one runner label because a hosted fleet is not one CPU model.

Three parts:

* The suite asks whether this CPU can run a bf16 autocast forward at all, in a
  child process, because the answer on some machines is a fault no handler can
  see. Where it runs, the three checks are unchanged. Where it does not, the
  suite says so with get_cpu_capability() and the child's exit code instead of
  taking the lane down. Verified both ways: 86 passed with the probe succeeding,
  84 passed and the SKIPPED marker printed when it is forced to fail.
* _cpu_amp_dtype says what the failure looks like. It already said bf16 is for
  hardware with native BF16 instructions; it did not say that the alternative is
  a native fault, nor that _infer's autocast retry catches RuntimeError and
  therefore cannot see one.
* docs/reference/agent.md gains the section a user needs before setting
  LAYA_CPU_AMP=bf16: the capability reading to check, and a child-process probe,
  since a fault in the parent cannot be caught. It sits above the NandhaKishorM#1065 note,
  which is the same failure from the quantization side.

laya's own behaviour is unchanged. LAYA_CPU_AMP was opt-in before and still is.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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