Skip to content

fix(agent): warn on fallbacks instead of printing to stdout - #861

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/agent-warnings-not-print
Oct 2, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
thread3d:fix/agent-warnings-not-print

Conversation

@thread3d

@thread3d thread3d commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Eight load- and inference-time notes in laya/agent.py were bare print() calls, so they went to stdout:

  • laya/agent.py:598, :601, :604 — requested device unavailable, falling back to CPU
  • :744-752 — the model could not be placed on the requested device
  • :782 — the fast path is unavailable
  • :841 — the model could not be moved back after the CPU retry
  • :1121 — a GPU OOM retry
  • :1165 — autocast disabled after repeated failures

Stdout is a machine-readable stream on three surfaces: laya --json writes one JSON object, laya --batch --json writes one JSON object per line, and the MCP server speaks JSON-RPC over stdio. A fallback or an OOM retry interleaves Warning: ... into that stream and breaks a strict JSON/JSONL (or JSON-RPC) consumer. A caller also cannot silence a print the way it can filter a warning.

This routes all eight through warnings.warn(..., RuntimeWarning), keeping each message text identical. The same module already uses warnings.warn at :105, :679, :1619 and :1647.

tests/test_load_errors.py gains a check that a device="cuda" load on a CPU-only machine emits a RuntimeWarning and writes nothing to stdout (the checkpoints build in-process, no network).

The device-fallback, CPU-retry, fast-path, OOM-retry and autocast notes were
bare `print()` calls, so they went to stdout. `laya --json` and
`laya --batch --json` write their records there, and the MCP server speaks
JSON-RPC over stdio, so a fallback interleaved a `Warning: ...` line into a
machine-readable stream -- and a caller could not silence it the way a warning
filter can.

Route all eight through `warnings.warn(..., RuntimeWarning)`, keeping the
message text unchanged; the module already used `warnings.warn` elsewhere.

Pinned by a new test in tests/test_load_errors.py: a `device="cuda"` load on a
CPU-only machine emits a RuntimeWarning and writes nothing to stdout.
@NandhaKishorM
NandhaKishorM merged commit a99a699 into NandhaKishorM:main Oct 2, 2026
25 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.24.

Eight print() calls on load and inference paths is a real defect rather than a style preference, and the reason is in your description: laya --json and laya --batch --json write their output to stdout, so a device-fallback note landed in the middle of a document a caller was parsing. warnings.warn also gives operators a filter, which a print never can.

One integration note: #843 added its own section 7 to tests/test_load_errors.py in the same pass, so yours is renumbered to section 8 and both are in. The file runs 52 checks now.

Verified on the integrated branch: tests/test_load_errors.py 52 passed, full shared test list green, zero decision flips against 0.3.23 on the real checkpoints.

Thank you.

@thread3d
thread3d deleted the fix/agent-warnings-not-print branch October 6, 2026 10:13
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