Skip to content

fix(napcat): keep a message whose image declares a non-numeric file_size - #5914

Merged
Re-bin merged 2 commits into
HKUDS:mainfrom
Lesereingrape:fix/napcat-file-size-guard
Oct 18, 2026
Merged

Re-bin merged 2 commits into
HKUDS:mainfrom
Lesereingrape:fix/napcat-file-size-guard

Conversation

@Lesereingrape

Copy link
Copy Markdown
Contributor

What

_download_image rejects an image upfront when Napcat declares a size above max_image_bytes:

try:
    declared_size = int(info["file_size"])
    if declared_size > max_bytes:
        ...
        return None
except (TypeError, KeyError):
    pass

_parse_segments already documents that Napcat sends file_size as a string ("bytes, sometimes a string", runtime.py:331-333). A string that is not a number — "", "unknown", "12KB" — makes int() raise ValueError, and that is the one exception this guard does not catch.

Why it matters

The ValueError escapes _download_image into its caller _on_message (runtime.py:254-256), which has no surrounding try, so the loop aborts before _handle_message runs. The whole inbound message is dropped — the image and the text the user sent with it — and the only trace is a napcat: message handler failed: ... warning from the background-task callback.

Skipping the upfront check is also what the code already intends for a size it cannot read: the check is only an optimization, and the streamed download below still caps the body at max_image_bytes and returns None when it is exceeded, so nothing unbounded reaches disk. The new branch logs the unusable declaration instead of swallowing it silently.

Tests

Two regression tests in nanobot/channels/napcat/tests/test_napcat_channel.py:

  • test_download_image_ignores_unparsable_declared_size — declared sizes "", "unknown", "12KB" now download the image instead of raising.
  • test_message_with_unparsable_image_size_still_routes — a private message carrying text plus such an image still reaches the bus with its text and its media path.

Before the fix both fail on origin/main:

E   ValueError: invalid literal for int() with base 10: ''
    nanobot/channels/napcat/runtime.py:536: ValueError
2 failed, 7 passed in 4.66s

After it:

$ pytest nanobot/channels/napcat -q
9 passed in 1.40s
$ ruff check nanobot tests conftest.py
All checks passed!

basedpyright nanobot/channels/napcat reports the same unknown-type class as pristine main (69 errors before, 70 after — the extra one is the new logger.warning call, a class every existing log line in this file already belongs to locally).

@chengyongru chengyongru added bug Something isn't working channel fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test labels Sep 25, 2026
@Lesereingrape

Copy link
Copy Markdown
Contributor Author

CI note for reviewers: the only red check on this head is Python (Windows, 3.14). Detect changes, Python (minimum, 3.11), Python (Windows process compatibility) and Python (latest, 3.14 + coverage) are all green.

The failure is not in the napcat suite. On that job the full-suite step finished 7856 passed, 82 skipped (both new napcat tests included); the red comes from the next step, pytest tests/cli/test_commands.py, where test_webui_foreground_does_not_claim_unmanaged_gateway exits 1 (+ where 1 = <Result SystemExit(1)>.exit_code) in the same step whose npm install dies on a Windows npm-cache race:

npm error code EEXIST
npm error syscall rename
npm error path C:\npm\cache\_cacache\tmp\28881640
npm error errno -4058

The foreground-webui CLI tests shell out to that install, so they inherit it. The diff itself touches only nanobot/channels/napcat/runtime.py and nanobot/channels/napcat/tests/test_napcat_channel.py, so a re-run of that one job should be enough.

@Lesereingrape

Copy link
Copy Markdown
Contributor Author

Follow-up on my previous note: attempt 2 (started 13:14:49Z, finished 13:24:49Z) failed the same step the same way, so a plain re-run on the same host does not clear it. Exact evidence, from the Python (Windows, 3.14) job log (job 108088269139):

=================================== FAILURES ===================================
___________ test_webui_foreground_does_not_claim_unmanaged_gateway _____________
>       assert result.exit_code == 0
E        +  where 1 = <Result SystemExit(1)>.exit_code
tests\cli\test_commands.py:2858: AssertionError
---------------------------- Captured stderr call ------------------------------
npm warn cleanup Failed to remove some directories [... 'D:\a\nanobot\nanobot\webui\node_modules\@radix-ui\react-popover' EPERM rmdir ...]
npm error code EEXIST
npm error syscall rename
npm error path C:\npm\cache\_cacache\tmp\28881640
npm error dest C:\npm\cache\_cacache\content-v2\sha512\71\84\3ddf5d20aeac...
npm error errno -4058
npm error ENOENT: no such file or directory, rename 'C:\npm\cache\_cacache\tmp\28881640' -> ...
=========================== short test summary info ============================
FAILED tests/cli/test_commands.py::test_webui_foreground_does_not_claim_unmanaged_gateway
============= 1 failed, 155 passed, 1 skipped in 60.13s (0:01:00) ==============

Reading it again, the detail that matters is the path: the rename that fails is inside C:\npm\cache\_cacache, i.e. the image-level npm cache the webui command installs frontend deps through — not anything in the repository, and tests/cli/test_commands.py is not in this PR's diff (nanobot/channels/napcat/runtime.py, nanobot/channels/napcat/tests/test_napcat_channel.py). Because that cache is shared per runner machine, re-running on the same host can keep hitting the same corrupt cache entry; a re-run that lands elsewhere, or clearing C:\npm\cache on the host, should get it green. Test Suite on this PR is otherwise green, including the Linux and the other Windows jobs, and my sibling PR #5913 passes the identical serial-CLI step.

I am not going to push a churn commit for this. Separately: if a build that shells out to npm from a CLI test is worth pinning down, that looks like its own issue rather than something to fold in here — happy to file it if you want, or to re-run on request.

@Lesereingrape

Copy link
Copy Markdown
Contributor Author

The Python (Windows, 3.14) failure looks unrelated to this change.

tests/cli/test_commands.py::test_webui_foreground_does_not_claim_unmanaged_gateway ends with <Result SystemExit(1)> instead of exit code 0, and the captured stderr for that test shows the runner's npm step failing to clean up the checkout:

npm warn cleanup Failed to remove some directories [
npm warn cleanup   '\\\\?\\D:\\a\\nanobot\\nanobot\\webui\\node_modules',
npm warn cleanup   [Error: EPERM: operation not permitted, rmdir 'D:\a\nanobot\nanobot\webui\node_modules\@radix-ui\react-popover'] {
npm warn cleanup     errno: -4048,
npm warn cleanup     code: 'EPERM',

This PR only touches nanobot/channels/napcat/runtime.py and nanobot/channels/napcat/tests/test_napcat_channel.py; the failing test exercises the webui/gateway CLI path. My other open PR here, #5913 (nanobot/agent/loop.py, same base commit), has the identical Python (Windows, 3.14) job passing, which is why I read this as a Windows-runner flake in the frontend build step rather than a regression introduced by these commits.

Re-running the failed job should confirm it. If you would rather I push a rebase onto current main to get a fresh run, let me know and I will do that.

Lesereingrape and others added 2 commits October 18, 2026 18:34
Napcat reports an image segment's file_size as a string that is not
always numeric, so int() raises ValueError. The upfront size guard only
caught TypeError and KeyError, the ValueError escaped _download_image
into _on_message, and the whole inbound message was dropped — text
included — with only a background-handler warning left behind.

Handle ValueError as the guard already handles a missing size: skip the
upfront check and let the streamed download enforce max_image_bytes.
Log it, since a declared size we cannot read is worth knowing about.
@Re-bin
Re-bin force-pushed the fix/napcat-file-size-guard branch from 4e5d9e2 to e5b492b Compare October 18, 2026 10:34

@Re-bin Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unusable declared image size no longer causes Napcat to drop the enclosing message. Only metadata parsing changes: URL validation, streamed-byte limits and existing media filters remain enforced. Regressions cover the inbound message boundary and the download cap with malformed size metadata. No configuration, stored-data, model-prefix or tool-schema changes; no breaking change found. This is isolated channel coverage, not a live QQ delivery test.

Exact-head CI passed. The base passed one Windows-only retry for an unchanged process-timing test; the intermittent timing cause is not claimed fixed.

@Re-bin
Re-bin merged commit 271bda1 into HKUDS:main Oct 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working channel fix priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants