Skip to content

Chunked media downloads misreport decimal MiB size limits #136397

Closed

Description

@steipete

What happened

On main 65e5b2e19c9db6460494682bacc4bee10db755fd, a valid decimal media limit can make an oversized chunked download fail as a generic fetch error instead of a size-limit error. Bounded diagnostic text reads also throw rather than returning their prefix when the byte budget is fractional.

Reproduction

  1. Set the existing agent default mediaMaxMb to 0.1 (the schema accepts positive fractional values).
  2. The shared channel resolver supplies a budget of 104857.6 bytes.
  3. Download a chunked HTTP response containing 104858 bytes through readRemoteMediaBuffer.

Expected: MediaFetchError with code max_bytes. Actual on Node 24.19.0: fetch_failed, caused by RangeError: length must be an integer. A 104857-byte control succeeds.

Directly reading the prefix of abcdef with a 3.5-byte budget also throws that RangeError instead of returning abc with truncated: true.

Root cause and scope

src/infra/http-body.ts counts a sliced overflow chunk using the fractional budget remainder, then supplies that fractional retained length to Buffer.concat. The slice contains whole bytes, so the counter and actual buffer disagree. The shared response reader owns this calculation; request-body limits, network policy, and configuration validation are separate unchanged contracts.

The failure is reproduced with a real loopback HTTP server and native Fetch, plus regressions that fail on the original code. Related: #135418, which investigates the same fractional media-limit trigger at the channel conversion layer.

Activity

  1. added
    maintainer
    on Sep 2, 2026
  2. added
    P2
    clawsweeper:source-repro
    impact:other
    issue-rating: 🦞 diamond lobster
    on Sep 2, 2026
  3. clawsweeper commented on Sep 2, 2026

    @clawsweeper
    Contributor

    Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed September 2, 2026, 11:50 AM ET / 15:50 UTC.

    Summary
    Keep open: current main still permits a fractional byte budget to reach the shared response reader, where fractional accounting reaches Buffer.concat before media code can emit its intended max_bytes error. The open related PR addresses channel-side normalization but is not merged and does not cover the shared reader’s direct diagnostic-prefix path; the maintainer label also requires explicit handling.

    Reproducibility: yes. source-reproducible with high confidence: a fractional cap makes remaining and total fractional on the overflow branch before buffer assembly. This read-only review did not execute the reporter’s loopback scenario.

    Maintainer decision needed

    Question Recommendation
    Should the shared-reader repair be a focused follow-up, or should maintainers explicitly expand #135418 to own both the resolver and reader invariants? Land a focused reader repair: Keep the channel normalization PR scoped to configuration conversion and add a small shared HTTP-reader fix with direct regressions.

    Why: The source defect is small and clear, but this issue is protected with the maintainer label and overlaps an active contributor PR without being fully covered by it.

    Next step
    The source-proven repair is small, but the protected maintainer label requires explicit human handling; the adjacent open PR is related but does not yet replace this issue.

    Review details

    Best possible solution:

    Normalize a finite response-reader byte budget to whole bytes once at the shared reader boundary, then add focused chunked-media and text-prefix regressions that prove the overflow classification remains max_bytes.

    Do we have a high-confidence way to reproduce the issue?

    Yes, source-reproducible with high confidence: a fractional cap makes remaining and total fractional on the overflow branch before buffer assembly. This read-only review did not execute the reporter’s loopback scenario.

    Is this the best way to solve the issue?

    Yes: normalizing at the shared response-reader boundary fixes both remote-media and direct-prefix callers, unlike a channel-only conversion guard.

    AGENTS.md: found and applied where relevant.

    Remaining risk / open question:

    • The adjacent open PR may remove the common channel-config trigger, but landing it alone leaves direct callers and bounded diagnostic prefixes vulnerable to fractional byte budgets.

    Codex review notes: model internal, reasoning high; reviewed against cf59ce401fee.

    Label changes

    Label changes:

    • add P2: Valid fractional configuration can make oversized streamed media fail with the wrong user-visible error, but the trigger is limited to decimal limits and chunked overflow.
    • add impact:other: The issue affects bounded media retrieval and diagnostic response handling, which does not map to a more specific owned impact label.
    • add issue-rating: 🦞 diamond lobster: Current issue advisory state selects this label.
    • add clawsweeper:source-repro: Current issue advisory state selects this label.

    Label justifications:

    • P2: Valid fractional configuration can make oversized streamed media fail with the wrong user-visible error, but the trigger is limited to decimal limits and chunked overflow.
    • impact:other: The issue affects bounded media retrieval and diagnostic response handling, which does not map to a more specific owned impact label.
    Evidence reviewed

    What I checked:

    • Fractional byte total reaches buffer assembly: Current main accepts every finite non-negative maxBytes value, then adds the fractional remaining allowance to total; Buffer.concat receives that fractional total even though the sliced Uint8Array contains whole bytes. (src/infra/http-body.ts:162, cf59ce401fee)
    • Media wrapper loses the size-limit classification: readRemoteMediaBuffer passes its effective cap to readResponseWithLimit, preserves MediaFetchError overflow results, and wraps any other reader exception as fetch_failed. This makes the shared reader failure observable as the reported generic media error. (src/media/fetch.ts:725, cf59ce401fee)
    • Fractional configuration is a supported input: The agent-default schema defines mediaMaxMb as positive number rather than an integer, so decimal MiB values are valid configuration. (src/config/zod-schema.agent-defaults.ts:197, cf59ce401fee)
    • Existing tests cover integer limits only: Response-reader tests verify integer overflow and reject invalid NaN/infinite/negative values, but contain no fractional-byte regression case. (src/infra/http-body.response.test.ts:189, cf59ce401fee)
    • Adjacent open fix remains incomplete for this issue: The supplied GitHub context identifies open PR fix(channels): honor decimal media limits for local attachments #135418 as normalizing channel media limits. Its stated scope is the channel resolver, whereas the current issue’s shared reader and direct text-prefix path remain present on main. (a2ccac347b52)
    • Recent history points to the affected refactor: History lists the recent HTTP-body refactor as c47f102. Its raw patch could not be inspected because this partial clone required an unavailable network fetch, so this is routing context rather than introduction proof. (src/infra/http-body.ts:213, c47f102224f9)

    Likely related people:

    • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
    • Dallin Romney: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
    How this review workflow works
    • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
    • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
    • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
    • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
    • Maintainers can also comment @clawsweeper review to request a fresh review only.
    • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
    • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
    • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
  4. added a commit that references this issue on Sep 2, 2026
    f7c8217
  5. added a commit that references this issue on Sep 3, 2026
    66bc994
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2clawsweeper:source-reproimpact:otherissue-rating: 🦞 diamond lobstermaintainer

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions