Skip to content

fix(core): reject a page selection that matches no page - #61

Merged
JJJYmmm merged 1 commit into
QwenLM:mainfrom
MaxFreedomPollard:fix/parse-pages-empty-selection
Oct 1, 2026
Merged

JJJYmmm merged 1 commit into
QwenLM:mainfrom
MaxFreedomPollard:fix/parse-pages-empty-selection

Conversation

@MaxFreedomPollard

Copy link
Copy Markdown
Contributor

What changed

parse_pages (src/capabilities/core/qwen_mm_plugins_core/renderers/__init__.py:175) ended by returning the first DEFAULT_MAX_PAGES pages whenever a selection produced no indices. It reads as the default for a missing spec, but five of the six callers only call parse_pages when the user supplied one (pdf.py:32, data.py:42, drawio.py:29, nifti.py:143, web.py:87), and the sixth, save_view.py:66, substitutes "1" for an omitted pages before calling it. So it only ever fired for a selection that matched no page, and answered with the first pages of the document.

What a user sees: on a 4-D NIfTI holding 3 volumes, visualize(file_path=vol.nii, pages="9") returned all 3 volumes under the line Selected volumes: pages (1, 2, 3); indices (0, 1, 2) (of 3), with nothing to say the request had been dropped. pages="0" and the reversed range pages="3-1" did the same. Through save_view on a PDF, the same kind of input wrote PNGs for pages nobody asked for.

A blank spec still returns the default page window. A non-blank spec that matched no page now raises ValueError. visualize.handle wraps the renderer call for a local file (visualizers/visualize.py:93), so the model reads Error rendering .nii file: no pages selected from '9' (document has 3 page(s)) instead of a substitution that contradicted save_view's own error string and src/capabilities/core/skill/SKILL.md:42.

save_view had written that error string but could not reach it for a non-blank spec: parse_pages substituted the default window first, and only a zero-page document reached the if not indices: guard. producers/save_view.py:91-97 now catches the ValueError, and that guard stays for a blank spec on a zero-page document, with its wording aligned to the new message.

One path lets the new ValueError out of visualize.handle, and it is the only behavior change a caller cannot predict. The web renderer always runs through run_isolated, and for a local .html file the try at visualizers/visualize.py:93 still catches what comes back. The URL branch returns at visualizers/visualize.py:52, before that try, so there the error arrives as IsolatedWorkerError("ValueError: no pages selected ..."), the shape tests/test_isolated_worker.py::test_isolated_worker_returns_structured_error pins, and FastMCP reports it as a tool error (Error executing tool visualize: IsolatedWorkerError: ...) rather than crashing. The web page count comes from slicing the screenshot (renderers/web.py:70-83), so pages="3" against a page that split into 2 now errors rather than returning both slices.

Scope. I left the rest of web.py alone: line 90 passes len(pages) after the truncation at line 83, and line 95 prints Total pages: {len(pages)} after selection has shrunk it again. Line 95 is a mislabel and line 90 selects against the truncated count, so a URL whose screenshot split into 30 slices with max_pages=20 now reports document has 20 page(s). Both are a different defect and belong in their own PR.

Regression tests

tests/test_renderers.py::test_parse_pages_keeps_selections_that_match_a_page pins what must not change against a 10-page document: 1-5, 3, 1,3,5-8 and 9-99. ::test_parse_pages_rejects_a_selection_that_matches_no_page asserts that 99, 0, 3-1 and 11-20 each raise. ::test_parse_pages_without_a_selection_returns_the_default_window covers a blank and a whitespace-only spec. tests/test_nifti.py::test_visualize_nifti_4d_reports_a_page_selection_that_matches_no_volume is the NIfTI case above end to end, with pages of 9, 0 and 3-1.

7 of those 12 cases fail on main with this branch's two test files checked out onto it; the 5 pinning unchanged behavior pass:

# on main, with the branch's tests/test_renderers.py and tests/test_nifti.py checked out:
$ .venv/bin/python -m pytest -q --tb=no -m "not reachability" tests/test_renderers.py tests/test_nifti.py -k "parse_pages or matches_no_volume"
....FFFF.FFF                                                             [100%]
=========================== short test summary info ============================
FAILED tests/test_renderers.py::test_parse_pages_rejects_a_selection_that_matches_no_page[99]
FAILED tests/test_renderers.py::test_parse_pages_rejects_a_selection_that_matches_no_page[0]
FAILED tests/test_renderers.py::test_parse_pages_rejects_a_selection_that_matches_no_page[3-1]
FAILED tests/test_renderers.py::test_parse_pages_rejects_a_selection_that_matches_no_page[11-20]
FAILED tests/test_nifti.py::test_visualize_nifti_4d_reports_a_page_selection_that_matches_no_volume[9]
FAILED tests/test_nifti.py::test_visualize_nifti_4d_reports_a_page_selection_that_matches_no_volume[0]
FAILED tests/test_nifti.py::test_visualize_nifti_4d_reports_a_page_selection_that_matches_no_volume[3-1]
7 failed, 5 passed, 38 deselected in 0.26s

The only other tests pinning a pages argument pass 1-3 or 1 (test_visualize_nifti_4d_defaults_and_selects_pages_lazily, tests/test_native_mode.py:169, tests/test_api_reachability.py:120), all in range.

Verification

macOS 26.2, Python 3.11.15, pytest 9.1.1, ruff 0.16.7, from the repo root:

$ .venv/bin/python -m pytest -q -m "not reachability" tests/ --ignore=tests/omni_chatcut
573 passed, 18 skipped, 8 deselected in 22.94s     # main: 561 passed, 18 skipped, 8 deselected in 23.01s

$ PYTHONPATH=src:src/capabilities/core .venv/bin/python -c "
from qwen_mm_plugins_core.renderers import parse_pages
for spec in ('1-5', '3', '1,3,5-8', '9-99', '99', '0', '3-1', ''):
    try:
        print(f'  parse_pages({spec!r}, 10) -> {parse_pages(spec, 10)}')
    except ValueError as exc:
        print(f'  parse_pages({spec!r}, 10) -> ValueError: {exc}')
"
  parse_pages('1-5', 10) -> [0, 1, 2, 3, 4]
  parse_pages('3', 10) -> [2]
  parse_pages('1,3,5-8', 10) -> [0, 2, 4, 5, 6, 7]
  parse_pages('9-99', 10) -> [8, 9]
  parse_pages('99', 10) -> ValueError: no pages selected from '99' (document has 10 page(s))
  parse_pages('0', 10) -> ValueError: no pages selected from '0' (document has 10 page(s))
  parse_pages('3-1', 10) -> ValueError: no pages selected from '3-1' (document has 10 page(s))
  parse_pages('', 10) -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The NIfTI case end to end, which the new test_nifti.py test asserts. On main the last three return 9 images each:

$ PYTHONPATH=src:src/capabilities/core .venv/bin/python - <<'PY'
import pathlib, tempfile
import nibabel as nib, numpy as np
from qwen_mm_plugins_core.visualizers import visualize

x, y, z = np.indices((4, 5, 6), dtype=np.float32)
path = pathlib.Path(tempfile.mkdtemp()) / "vol.nii"
nib.save(nib.Nifti1Image(np.stack([x + 10 * y + 100 * z] * 3, axis=-1), np.eye(4)), str(path))
for spec in ("2", "9", "0", "3-1"):
    blocks = visualize.handle({"file_path": str(path), "pages": spec, "budget": "small"})
    images = sum(1 for b in blocks if b.get("type") == "image")
    lines = [ln.lstrip("- ") for b in blocks if b.get("type") == "text"
             for ln in b["text"].splitlines() if "Selected volume" in ln or "Error" in ln]
    print(f"  pages={spec!r:6} -> {images} images | {' '.join(lines)}")
PY
  pages='2'    -> 3 images | Selected volume: page 2 / index 1 (of 3)
  pages='9'    -> 0 images | Error rendering .nii file: no pages selected from '9' (document has 3 page(s))
  pages='0'    -> 0 images | Error rendering .nii file: no pages selected from '0' (document has 3 page(s))
  pages='3-1'  -> 0 images | Error rendering .nii file: no pages selected from '3-1' (document has 3 page(s))

The project's own command is python3 -m pytest -m "not reachability" tests/. I added --ignore=tests/omni_chatcut because 3 of those tests fail here for unrelated reasons, the same 3 as on main:

$ .venv/bin/python -m pytest -q --tb=no -m "not reachability" tests/omni_chatcut
FAILED tests/omni_chatcut/test_music2mv_runtime.py::test_another_venv_with_the_same_base_python_is_rejected
FAILED tests/omni_chatcut/test_music_to_mv.py::test_pipeline_burns_auto_discovered_evidence_into_final_mv
FAILED tests/omni_chatcut/test_music_to_mv.py::test_bundled_subtitle_renderer_burns_and_preserves_audio
3 failed, 123 passed in 8.09s                      # main: the same 3, 3 failed, 123 passed in 8.31s

The two test_music_to_mv.py failures run burn_lyrics_subtitles.py, which exits 1 because the local ffmpeg 8.1.1 has no libass. The third is unrelated to ffmpeg: a child dies with SIGABRT while probing a venv it creates. This change touches none of them.

python3 scripts/check_manifests.py reports the 12 capabilities consistent across marketplace.json, the plugin manifests and pyproject.toml, plus its usual note about the example template.

Neither ruff command is clean repo-wide on main, so I ran both scoped to the four touched files. They are clean on main and on this branch alike: All checks passed! and 4 files already formatted.

Not run: the opt-in reachability tests, the Ubuntu CI leg, the save_view PDF path end to end, since pypdfium2 is not installed here, and the web renderer end to end, since playwright is not installed here either.

Compatibility

  • Existing MCP tool names, inputs, and outputs are unchanged, or the change is explained above. Every spec that selects at least one page returns what it did before. An omitted pages never reaches parse_pages in the renderers, so their defaults are unchanged (one volume for NIfTI, as SKILL.md says), and save_view still substitutes its "1" default. A spec that matched no page now errors, which is the fix.
  • Tests and documentation were updated where behavior changed. Four regression tests were added across the two existing test files. Nothing documented the old fallback, and SKILL.md already describes pages as a selector.
  • No credentials, private media, generated artifacts, or machine-specific configuration are included. The only fixture is the synthetic 4-D volume the test builds itself.

parse_pages fell back to the default page window whenever a selection produced
no indices, but five of its six callers only reach it with a spec the user
supplied, and save_view substitutes "1" first. An out-of-range page, a 0 (the
API is 1-based) or a reversed range therefore rendered the first pages of the
document and reported those pages back as the selection. The fallback now
applies only to a blank spec, and a non-blank spec that matches nothing raises
ValueError, which visualize already turns into a tool error and which save_view
now catches. save_view's own guard, reachable now only for a blank spec on a
zero-page document, gets the same wording.
@JJJYmmm
JJJYmmm merged commit 8a53d1d into QwenLM:main Oct 1, 2026
10 checks passed
@JJJYmmm

JJJYmmm commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

ty!

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