Repository navigation
fix(mcp): refuse a LAYA_DEVICE index torch rewrites instead of building another GPU - #1242
Merged
NandhaKishorM merged 1 commit intoOct 17, 2026
Conversation
…ng another GPU env_device asks torch to parse the value before returning it, which is how `gpu` and `cuda:` became a named environment error rather than a torch internal one. Parsing is only half of "the device a client is told about and the device torch is asked for cannot drift apart": an index torch cannot keep is read back as a different one -- on 2.6.0 it survives only in its low 8 bits, so `LAYA_DEVICE=cuda:258` parses happily, is reported verbatim by laya_status and laya.serve's /health, and builds GPU 2. The guard is now "what torch builds is what the string names": a value whose parsed device is not the device it names raises a ValueError naming LAYA_DEVICE, quoting both the operator's spelling and torch's answer, with its own sentence so the two refusals stay tellable apart. `force=` keeps passing through verbatim -- that asymmetry is the documented tests hook, and the suite pins it. Tests: 44 new arms in tests/test_mcp_device.py (51 -> 95), written as a biconditional against torch's own parser so a torch that stops truncating leaves the refusals unasserted rather than asserting a falsehood.
This was referenced Oct 15, 2026
Owner
|
Merged in 0.4.9. I measured this before merging because the whole case rests on it, and it is exactly as reported: So torch keeps a CUDA index in its low 8 bits, reads Using |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
laya/mcp/device.py::_check_torch_devicerefuses aLAYA_DEVICEvalue torch cannot parse. Itnow also refuses one torch rewrites: a value whose parsed device is not the device the string
names raises a
ValueErrornamingLAYA_DEVICE, quoting both the operator's spelling and thedevice torch would actually build.
Four files, +120/−8:
laya/mcp/device.pyenv_device/resolve_devicedocstrings that state its scopelaya/mcp/server.pyLAYA_DEVICEline of the server docstring, which listed only the parse refusalstests/test_mcp_device.pydocs/cli-mcp.mdLAYA_DEVICErow, which promised the refusal covers only unparseable stringsforce=(the tests hook, mirroring the CLI--deviceflag) is unchanged: still returnedverbatim, still unchecked. That asymmetry is the module's documented contract, and a new arm
pins it.
Why
The module's headline claim is that "the device a client is told about and the device torch is
asked for cannot drift apart". A device index breaks it while the parse check passes, because
torch reads back an index it cannot keep as a different one — on 2.6.0 only the low 8 bits
survive. Measured on the base commit (
4ac226d5, release 0.4.8), clean tree, torch 2.6.0:None of those raise. So
LAYA_DEVICE=cuda:258starts a healthy server, and every surface thatreports the device repeats the operator's typo back to them while the process asks torch for a
different GPU. Base-vs-branch probe, same command,
laya.__file__printed to prove which copyanswered:
The branch message:
The concrete use case is a multi-GPU box where the operator means
cuda:2and types an indexthat wraps, or reaches past the device count:
cuda:258is the same class of typo ascuda:,and today only one of the two is attributed to the environment.
laya.servereads the valuethrough the same
env_device(laya/serve.py:1115), so the HTTP/healthdevicefield hasthe same drift.
force=and the CLI--deviceflag are deliberately left as the uncheckedpath, exactly as the existing docstring says.
The new test arms are written as a biconditional against torch's own parser — refused iff
this torch would not build the string verbatim — so on a torch that stops truncating the
refusal arms stop asserting and the suite still passes, instead of asserting a falsehood. The
message-shape arms derive their inputs at runtime for the same reason, and print a
NOTElinewhen no candidate rewrites, so a vacuous pass is visible rather than silent.
How it was verified
Docs (
docs/cli-mcp.mdchanged) build strict and clean from the repo root —rc 0,No issues found,Build finished in 1.68s— and the rendered row was read back out ofsite/cli-mcp/index.html, so the sentence that reaches a user is the one asserted above.The guard was mutation-checked, six faithful mutants of
laya/mcp/device.pyagainsttests/test_mcp_device.py, each reverted before the next and the file restored byte-for-byte(
restored: True (sha256 84781a7cc173 == 84781a7cc173)). Predictions were written before therun; all six matched, 0 mismatched:
==)tests/test_mcp_device.py:56cpuitself stops being acceptedparsed.typeLAYA_DEVICE:56CUDA:0stops being acceptedforce=tooforcepass-through armThe two aborts count as detections: an uncaught
ValueErrorout ofenv_device_withis thesuite dying on a refusal it did not expect, and the harness reports the frame it died at rather
than calling the run inconclusive.
Cross-port check (before touching a parameter any fixture generator pins, since the port
generators record Python's edge values as their oracle): no port reads
LAYA_DEVICE— a grepover the tree finds it only in Python, its tests, the docs, the compose files,
Dockerfile,nix/and.github/— and no fixture carries a device string:laya-java(20 JSON),laya-go(1),laya-dotnet(71) andsdk(5) contain zero matches forcuda. There is nolaya-rustin this tree. So this is a Python-only change and the port oracles are unaffected.Checklist
main(4ac226d5 release: 0.4.8—HEAD^is that commit; verifiedwith
git merge-base --is-ancestor refs/remotes/upstream/main HEAD, since a stale localbranch named
upstream/mainmakes the short ref ambiguous in this checkout)tests/test_mcp_device.py95/0; the one gate invocation that differsfrom the control tree is that suite; the two env-red lanes are identical on both trees)
docs/cli-mcp.mdrow, plus thelaya/mcp/server.pyandlaya/mcp/device.pydocstrings that described the narrower guard)Fixes #