Skip to content

Read registry responses within a size limit and one timeout - #5

Merged
tolgaergin merged 3 commits into
mainfrom
fix/bounded-registry-responses
Nov 8, 2026
Merged

tolgaergin merged 3 commits into
mainfrom
fix/bounded-registry-responses

Conversation

@tolgaergin

@tolgaergin tolgaergin commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Bounds how much of a registry response the server reads, and for how long. Two independent reviews (Opus 5.5 and Sol 6.1) of the first commit found regressions and gaps, which the second commit fixes; re-checks of the second found more, which the third fixes.

Problem

  • apiGet parsed every response with response.json(), with no size limit, on the server's only thread.
  • It cleared its 30 s timeout once headers arrived, so a body that stalled never timed out.
  • lpm_docs had no timeout at all.
  • lpm_browse_source passed the whole tree to the model, up to 20,000 entries.

The server is a local stdio process for one user, so the impact is limited to that user's session.

Change

  • Size limit: bodies are read through the stream, up to 50 MB.
    • A larger declared or streamed size cancels the body and returns Response was larger than 50 MB without a retry.
    • A package document can legitimately pass 10 MB (versions × up to 128 KB of lpmConfig), and parsing 50 MB takes about 80 ms.
    • Node-stream bodies from fetch replacements are bounded too. Only a body-less response, which is empty or a test double, goes through json().
  • Timeouts: a request may wait 30 s for headers, then 30 s between chunks, and a call takes at most 120 s, its retries included; a retry isn't waited out past that. A stall ends the call, a slow but progressing download finishes, and a trickle can't hold it for long. Body reads race the abort signal, so a fetch replacement whose reads ignore it can't hang a call.
  • Error responses keep their status: a 401, 403 or 404 whose body breaks off, stalls or is too large is still reported with that status and data: {}, without a retry.
  • BOM: bounded bytes are decoded with TextDecoder, which drops a leading byte-order mark as response.text() does.
  • Retry-After: a request to wait longer than the backoff allows (10 s) returns rate limited instead of sleeping, which could otherwise last hours.
  • Retried responses: 429 and retryable-error bodies are cancelled before the backoff.
  • lpm_docs: uses the same limits through textGet. A search where every matching page failed returns an error naming them, not an empty success, and one where some failed names them after the pages it read.
  • lpm_browse_source: returns the whole tree when it fits, and lists a longer one 2,000 paths at a time.
    • Paging: a new treeOffset argument gives the next page, and the response says where it starts. Every path can be reached, so a package can't hide a directory behind the limit from a model browsing it.
    • Fields: treeTotal, treeOffset, treeTruncated, and a treeSummary counting files by top-level entry. A summary of more than 100 entries says so.
    • Scoping: with path, a tree too large to list whole is scoped to that directory and names it in treeScope. A path with no paths under it lists the whole package and says so.
    • Details: directory entries aren't counted as files, and slashes are trimmed with a loop rather than a trailing-run regex, since tar paths reach 32 KiB.
  • Session cache: bounded at 500 entries, dropping the least recently used; its size must be a positive integer.

Tests

  • New body tests (native Response streams):
    • An endless body is cancelled at the limit, with one fetch and no retry.
    • A declared oversized body is never pulled.
    • A reader-less Node-stream body is bounded and destroyed.
    • A stall after headers times out on each of 3 attempts.
    • A body arriving every 25 s for 75 s finishes, and one trickling past 120 s ends.
    • BOM-led JSON parses.
    • 401 and 403 bodies that break off or are too large keep their status with one fetch, and so does an oversized 404.
    • A retried 503 body is cancelled.
    • A long Retry-After returns rate limited at once.
    • textGet reads, strips a BOM, rejects oversized bodies and times out.
  • They fail on the first commit for each regression the reviewers found. Deleting the cancel or the content-length check fails the matching tests.
  • New lpm_docs tests: page and caching, 404, timeout, index, query, partial failure, all failing, and an unreadable index.
  • Tree tests: paging reaches every path (including a directory the summary leaves out), the summary discloses its limit, scoping is named, an unmatched path lists the whole package, directory entries aren't counted, and a bad treeOffset is rejected.
  • Deadline tests: a trickling body ends at 120 s with one fetch, a retry isn't waited out past the deadline, and a reader that ignores the abort signal is cancelled at the timeout.
  • Cache tests: the entry bound, and invalid sizes rejected.
  • Against the previous commit: each of these fails on 289eba8.
  • Suite: vitest run gives 354 tests in 26 files, all passing. biome check . is clean.

🤖 Generated with Claude Code

tolgaergin and others added 3 commits November 8, 2026 09:12
apiGet parsed every registry response whole, with no size limit, and cleared
its timeout once headers arrived, so a slow body never timed out. Docs pages
were fetched with no timeout at all. A package's metadata can carry 10,000
versions with a 128 KB lpmConfig each, which the server parsed to read about
ten fields.

Bodies are now read up to 10 MB (the CLI's output limit) under one 30 s
timeout that covers the request and the body. A larger body is cancelled and
returned as an error without a retry. lpm_browse_source lists at most 2,000
paths and reports the total, since a tarball can hold 20,000 entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Findings from independent reviews (Opus 5.5, Sol 6.1) of the first commit:

- An auth or not-found response whose body broke off, stalled or was too
  large lost its status, and auth failures were retried. Error bodies are now
  read leniently: the status is kept and the body reads as `{}`.
- Decoding the bounded bytes with Buffer kept a byte-order mark, so BOM-led
  JSON parsed as `{}`. TextDecoder drops it, as response.text() does.
- The 30 s timeout covered the whole download, so a large body that kept
  arriving was cut off. Requests now have an idle timeout, restarted by each
  chunk, and a 2-minute total. The size limit is 50 MB, since a package
  document can legitimately pass 10 MB with large lpmConfig values.
- A Retry-After longer than the backoff allows was slept on, up to hours. The
  call now returns rate limited instead.
- Bodies of fetch replacements without a stream reader were read unbounded;
  Node streams are now bounded too, and only a body-less response uses json().
- lpm_docs search returned an empty success when every matching page failed;
  it now returns an error naming them.
- The tree cap cut the alphabetically first 2,000 paths, so whole directories
  could vanish, and `path` didn't help. A truncated tree now has a summary by
  top-level entry, and with `path` it lists that directory's paths.
- The session cache is bounded at 500 entries.
- Tests now cover each of these, the cancelled body of a retried response,
  that a declared oversized body is never read, and lpm_docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Findings from re-checks (Opus 5.5, Sol 6.1) of the second commit:

- The tree summary stopped at 100 entries without saying so, so a directory
  could be left out of both the listing and the summary, and a package could
  hide files from a model browsing it. lpm_browse_source now takes a
  treeOffset argument and lists a long tree a page at a time, so every path
  can be reached, and a truncated summary says so.
- Scoping a long tree to `path` was silent, and a `path` with no paths under
  it gave an empty tree. The scope is now named in treeScope, and an
  unmatched path lists the whole package with a note. Directory entries in
  the tree aren't counted as files, and slashes are trimmed with a loop
  instead of a trailing-run pattern.
- The 2-minute total timeout applied per attempt, so a trickling server held
  a call for about 6 minutes. It now spans the retries, and a retry isn't
  waited out past it. Body reads also race the abort signal, so a fetch
  replacement whose reads ignore it can't hang a call.
- MemoryCache rejected nothing for maxEntries, and 0 looped forever.
- lpm_docs names the pages it couldn't read after the ones it could.
- The Retry-After test no longer depends on wall-clock time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tolgaergin
tolgaergin merged commit d01852c into main Nov 8, 2026
2 checks passed
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.

1 participant