nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] fetch_url: let the model read past the 100 KB cut with an offset

Open akramcodez opened this issue 3 weeks ago • 5 comments

Description

When a page is longer than the fetch_url limit, the tool cuts it and the model has no way to read the rest. Add an offset (or page) argument so the model can continue where the previous call stopped.

Use Case

executeFetchUrl in source/tools/fetch-url.tsx converts the page to markdown and, if it is over MAX_URL_CONTENT_BYTES (100,000), returns the first part plus:

[Content truncated - original size was N characters]

The tool schema only has url, so calling it again returns the same first 100,000 characters. For long API references, changelogs and specs, the part the model needs is often past the cut, and it cannot get to it. In practice it either guesses, or gives up and says it could not find the information.

Proposed Solution

Add two optional arguments:

  • start_index (default 0): character offset into the converted markdown
  • max_length (default the current limit, with a hard upper bound)

and make the truncation note actionable, for example:

[Content truncated - showing characters 0-100000 of 482113. Call fetch_url again with start_index=100000 to continue.]

The conversion result is deterministic for a given URL, so a repeat call with an offset can slice the same markdown. A short in-memory cache keyed by URL (a minute or two) would avoid fetching and converting the page again for each chunk.

Alternatives Considered

  • Raising the limit. This costs context on every call, which is bad for small local models, and still has a ceiling.
  • Having the model use execute_bash with curl and grep. That needs approval each time and loses the HTML to markdown conversion.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Implementation Notes (optional)

The reference fetch MCP server uses the same start_index / max_length pair, so models are already used to it. The cut should avoid splitting a surrogate pair (see the truncation handling elsewhere in the codebase).

akramcodez avatar Nov 06 '26 09:11 akramcodez

Hi, I'd like to take this one. Here's my understanding and plan; let me know if it lines up before I start.

1. Understanding of the issue

executeFetchUrl in source/tools/fetch-url.tsx converts a page to markdown and, when it's over MAX_URL_CONTENT_BYTES (100,000), returns only the first 100k plus a [Content truncated - original size was N characters] note. Since the tool schema only exposes url, calling again just re-fetches the same first 100k, so anything past the cut (long API refs, changelogs, specs) is unreachable, and the model either guesses or gives up. The fix is to let the model ask for a later slice of the same converted markdown.

2. Approach

  • Add two optional args to the schema: start_index (default 0) and max_length (default MAX_URL_CONTENT_BYTES, clamped to that same value as a hard upper bound).
  • Slice the converted markdown to [start_index, start_index + max_length), with clamping for out-of-range offsets, and make the cut surrogate-safe at both ends so an emoji/astral char never gets split (mirroring the existing sliceWholeCharacters handling in source/services/lifecycle-hooks.ts).
  • Make the truncation note actionable, e.g.:
    [Content truncated - showing characters 0-100000 of 482113. Call fetch_url again with start_index=100000 to continue.]
    
  • Add a short in-memory cache keyed by URL (small, ~90s TTL, bounded with oldest-evicted) so paging through a long page doesn't re-fetch and re-convert each time, so keeps it cheap for small/local models.
  • Update the terminal formatter to show the real window (Showing characters X–Y of N) instead of a flat "truncated to 100,000" line.
  • This matches the arg names the reference fetch MCP server uses (start_index / max_length), so models are already familiar with the shape.
  • Tests + a changeset included; no behavior change for callers that pass only url.

3. Does this match the intent?

Does this match what you had in mind? A few specifics I can adjust:

  • Keep the hard cap on max_length equal to the current 100k limit, or allow a higher ceiling?
  • Cache TTL/scope preference (currently leaning ~90s, bounded), happy to tune or drop it if you'd rather keep the tool stateless.

If the plan looks good, @akramcodez or @addyCooks could you assign this to me? I'll open a PR.

SamanPandey-in avatar Nov 07 '26 14:11 SamanPandey-in

Hi @SamanPandey-in , I’m assigning this one to you. I’ll review your PR this time. Going forward, please comment on the issue and wait until you’re assigned before you start working on it. That way we avoid duplicate work and keep things fair for everyone.

Thanks for contributing!

addyCooks avatar Nov 10 '26 07:11 addyCooks

Thanks @SamanPandey-in, the plan looks good. Two answers:

  1. Keep the max_length cap at the current 100k limit. A higher ceiling costs context on every call, which hurts small local models.
  2. A short, bounded cache (~90s) is fine, but run the existing URL validation and redirect checks (resolveSafeRedirects) on every call, even on a cache hit. Only cache the converted markdown.

Also keep the [Content truncated prefix in the new note (the formatter detects it), and test that windows have no gap or overlap and never split a surrogate pair.

I'll review your PR when it's ready.

addyCooks avatar Nov 10 '26 07:11 addyCooks

Thanks @addyCooks, and apologies for jumping ahead. I'm new to the repo and missed the assignment process. Going forward I'll comment and wait to be assigned before starting.

Confirming the plan:

  • max_length hard cap stays at 100k
  • Cache only the converted markdown (~90s, bounded), with URL validation and resolveSafeRedirects running on every call, including cache hits
  • Keep the "[Content truncated" prefix in the new note so the formatter still detects it
  • Tests for no gap/overlap between windows and no split surrogate pairs

I'll update PR #2002 to match, will push the changes shortly and ping you when it's ready for review.

SamanPandey-in avatar Nov 10 '26 07:11 SamanPandey-in

Thanks @SamanPandey-in for confirming the plan. I see you've pushed new commits to #2002. I'll review the PR.

addyCooks avatar Nov 12 '26 06:11 addyCooks