Panniantong/Agent-Reach · error · PrivatePathError

读取目标超过大小上限:{target}

Error message

读取目标超过大小上限:{target}

What it means

First of two size-cap checks in the private file reader: after opening the fd, fstat reports st_size greater than max_bytes and the read is refused before any I/O. Message (Chinese): 'read target exceeds size limit: {target}'. It protects fixed-size readers (config/token files) from being fed huge files.

Source

Thrown at agent_reach/utils/paths.py:166

    target = ensure_no_symlink_path(path, "读取路径")
    flags = (
        os.O_RDONLY
        | getattr(os, "O_NOFOLLOW", 0)
        | getattr(os, "O_NONBLOCK", 0)
        | getattr(os, "O_CLOEXEC", 0)
    )
    try:
        fd = os.open(target, flags)
    except FileNotFoundError:
        return None

    try:
        file_stat = os.fstat(fd)
        if not stat.S_ISREG(file_stat.st_mode):
            raise PrivatePathError(f"读取目标不是常规文件:{target}")
        if file_stat.st_size > max_bytes:
            raise PrivatePathError(f"读取目标超过大小上限:{target}")

        chunks = []
        remaining = max_bytes + 1
        while remaining:
            chunk = os.read(fd, remaining)
            if not chunk:
                break
            chunks.append(chunk)
            remaining -= len(chunk)
        payload = b"".join(chunks)
        if len(payload) > max_bytes:
            raise PrivatePathError(f"读取目标超过大小上限:{target}")
        ensure_no_symlink_path(target, "读取路径")
    finally:
        os.close(fd)
    return payload.decode(encoding)

View on GitHub (pinned to 93ae1d18c3)

Solutions

  1. Check the actual file: ls -lh <target> — decide whether it is the right file
  2. Pass a max_bytes appropriate for the content you expect, if you are the caller choosing the cap
  3. If the file is legitimately huge, stream it with your own reader instead of this capped helper

Example fix

# before
data = read_private_file(cookie_export, max_bytes=1024)  # file is 2 MiB

# after
data = read_private_file(cookie_export, max_bytes=8 * 1024 * 1024)
Defensive patterns

Strategy: validation

Validate before calling

import os

def size_within(target: str, max_bytes: int) -> bool:
    try:
        return os.stat(target).st_size <= max_bytes
    except OSError:
        return False

Try / catch

from agent_reach.utils.paths import PrivatePathError
try:
    data = read_private(target, max_bytes=cap)
except PrivatePathError as e:
    if "大小上限" in str(e):
        real = os.path.getsize(target)
        if real > _HARD_MAX:
            raise ValueError(f"{target} is unexpectedly {real} bytes")
        data = read_private(target, max_bytes=_HARD_MAX)
    else:
        raise

Prevention

When it happens

Trigger: Calling the private read helper (the max_bytes function) with a target whose on-disk size already exceeds max_bytes — e.g. reading a multi-MiB export into a helper sized for a small token file.

Common situations: Cookie export files or logs grown far larger than expected; accidentally pointing at a media file; max_bytes left at a default meant for small secrets.

Related errors


AI-assisted analysis of Panniantong/Agent-Reach@93ae1d18c3 (2026-08-14). Data as JSON: /api/errors/1983b69a281b5682. Report an issue: GitHub.