My coding agent kept re-reading the internet
Inside u2md: turning docs sites into local Markdown an agent can actually use, one cache shared by every repo on the machine, and the four tildes of a Python traceback that broke search.
Every time my coding agent needed a docs page, it went and fetched it. The same page, from the same site, in every repo and every session. Then it read all ten thousand tokens of it, sidebar included, to find the one paragraph it wanted.
u2md is what I built instead: one Go binary that turns a documentation site into clean Markdown once, keeps it on your machine, and hands an agent only the part it asked for. It started this month as a crawler and grew into something closer to a local library for docs. Almost everything worth writing about came from pointing it at real sites and watching it be wrong.
$ u2md mirror https://docusaurus.io/docs -o docs --max-pages 30 Mirrored 30 pages -> docs Stats: attempted=30 cached=0 revalidated=0 … version-skipped=1058 Methods: docusaurus=30 Warning: skipped 1058 pages in 12 other doc versions (/docs/2.x, /docs/3.0.1, …) Start with: docusaurus.io/INDEX.md $ u2md mirror https://docusaurus.io/docs -o docs --max-pages 30 --max-age 1h Stats: attempted=30 cached=30 revalidated=0 … version-skipped=1058 # 4.4 s, then 1.1 s with zero requests: all 30 from the store
The shape of it
One rule shapes the whole thing: the network is the slow, unreliable part, so touch it as little as possible. A page is downloaded once, stored under the hash of its contents, and turned into Markdown from there. Every read after that is local.
Extraction is mostly deleting things
A docs page is maybe a third documentation. The rest is sidebar, navbar, version picker, cookie banner, “Edit this
page” and “Was this helpful?”. u2md recognises Docusaurus, MkDocs Material, Mintlify, Sphinx and VitePress by their DOM
markers and knows what to cut from each. Anything else falls back to main, article or [role=main].
Knowing what to cut was the easy half. Knowing what not to cut took real sites. In my first run against docusaurus.io:
| on 30 docusaurus.io pages | first run | now |
|---|---|---|
| pages missing their title | 18 | 0 |
| pages that were old-version copies | 18 | 0 |
| callouts | a bare word: tip |
> **Tip** |
The titles were a lovely bug. Docusaurus puts a page’s only <h1> inside a <header>, and deleting <header> is the
first thing every extractor does. So the title left with the site chrome. Now the h1 gets lifted out first, unless
the page has another one.
The versions were worse. docusaurus.io keeps a full copy of its docs for every release (/docs/2.x/, /docs/3.0.1/,
a dozen more), and those sort before the current docs. A 30-page crawl spent most of its budget on near-identical old
copies. Directories named like dotted versions, or next, are now skipped by default with a warning that lists them.
Bare integers like /v1/ are left alone, because those are usually API versions rather than duplicates.
Python’s docs were wrong more quietly. They use Sphinx’s classic theme, which has none of the markers I’d taught it, so every page fell back to the generic extractor. The output still looked fine, which is exactly why nobody noticed. It knows the classic theme now.
Links are the hard part
Converting HTML to Markdown is a library call. Making the links work afterwards is not. A page links to
/docs/install, which redirects to /docs/install/, whose canonical URL is /docs/installation, and all three have
to land on the same local file.
So conversion runs in two passes. The first keeps every link absolute and only collects them. When the crawl is done and every redirect, canonical and dead link is known, the second pass rewrites links through goldmark’s syntax tree instead of a regex. That’s the difference between rewriting a link and “fixing” a URL that happened to appear inside a code sample.
The same page, byte for byte
Mirror the same site twice and you get identical files. The crawl goes level by level with each level sorted, and
everything after the download comes from the cached bytes, so --offline rebuilds are identical too.
The sneaky enemy was the date. Every page’s frontmatter records when it was fetched, and servers without ETag or
Last-Modified look new on every request. So every file changed every day, and every changelog was a list of nothing.
Now fetched_at means “first seen”: if the body hashes the same as the cached one, the old date stays.
That sounds like pedantry until you commit docs to git. Then a diff only shows up when the docs actually changed.
Four tildes broke my search
Search is BM25 over heading-sized chunks. I tested it on the Python tutorial with how do I handle exceptions with
finally, and the section that explains finally didn’t make the top five.
It wasn’t the ranking. One chunk, “8.2 Exceptions”, was 18,234 characters long. It had swallowed sections 8.3 to 8.7 whole, including the one I was looking for. The culprit was in a code block a few lines down:
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
'2' + 2
~~~~^~~
TypeError: can only concatenate str (not "int") to str
That last caret line is Python pointing at the bad expression. Here’s how my splitter decided what was code:
// the old splitter, trimmed
if strings.HasPrefix(trim, "```") || strings.HasPrefix(trim, "~~~") {
inFence = !inFence
}
~~~~^~~ starts with three tildes. So the parser thought the code block ended there, treated the real closing fence as
the start of a new one, and read every heading after it as code. Five sections became one, and the answer drowned in it.
Markdown has an actual rule for this. A fence only closes on a line made of the same character, at least as many of them, and nothing else:
func (f fence) closes(line string) bool {
s, ok := trimIndent(line)
if !ok {
return false
}
n := 0
for n < len(s) && s[n] == f.char {
n++
}
return n >= f.n && strings.TrimSpace(s[n:]) == ""
}
The fix that mattered more was structural. Headings, fences and anchors are now decided in exactly one package, which
search, filters and section reads all share. So an anchor that search returns is always one that read_section can
open. The finally section ranks first now.
While I was in there, the stemmer turned out to file handle and handling under different words. Search bugs are like that: they never crash, they just make you a little worse at finding things.
Download once per machine
Every repo used to keep its own cache. Now there’s one store per machine (~/.cache/u2md on Linux), with raw
responses kept by content hash. And with a max_age, a recent copy is served before anything else happens:
if f.opt.MaxAge > 0 {
if r, ok := f.lookup(norm); ok && time.Since(r.CheckedAt) < f.opt.MaxAge {
return r, nil
}
}
That check runs before DNS and before robots.txt. The robots file needed reading-through too, including the “this
site has none” answer, or every warm run would still make one request per host. Copies older than max_age are
revalidated with ETag/Last-Modified, which is usually a cheap 304. If the server is down, the agent gets the last
copy marked cache: stale. A refusal is never covered up that way. A 404, a robots rule or the SSRF guard saying no
stays an error, and a robots.txt that couldn’t be reached is a different error from one that said no.
Sharing the store broke something, of course. Each process loaded the index, added its entries and wrote its whole snapshot back on exit, so the last process to finish erased everyone else’s work. It’s the same lost update as the zombie worker in TaskForge wearing different clothes. Now a flush takes a lock file, re-reads what’s on disk and writes back only the keys it changed.
Agents don’t need the whole page
Every mirrored site starts with an INDEX.md listing pages with token counts, so an agent can budget before reading.
That still meant reading whole pages, so there are now three smaller ways in:
read_sectionreturns one section and its subsections, bypage.md#anchor.get_contextpacks the best-matching sections into a token budget.find_examplesreturns only the code blocks, which is often all an agent wanted.
Here’s the start of get_context with a budget of 600 tokens, on that same question:
<!-- u2md context: 2 sections, ~599 of 600 tokens, 11 relevant sections did not fit -->
## 8. Errors and Exceptions > 8.7. Defining Clean-up Actions
Source: 3/tutorial/errors.md#87-defining-clean-up-actions
Packing goes in rank order. A section that doesn’t fit is skipped, but smaller ones below it still get a chance. It stops once matches fall under a quarter of the best score, because past that point it’s filler, not context. If even the best section is too big on its own, it’s cut on a line or word boundary and marked as cut (closing any code fence it interrupted), so the answer is never empty. And it says what it left out, so the agent knows to ask for more.
Configure once
Flags are fine for a person and annoying for an agent, which has to remember them every call. So a u2md.yaml in the
repo, plus an optional global one, holds the settings:
defaults:
max_age: 7d
drop_sections: ['Was this helpful?', 'Changelog*']
sites:
- url: https://docs.stripe.com/api
keep_sections: ['Parameters', 'Returns']
The CLI and the MCP server both pick it up, so the agent never passes a setting. Unknown keys are an error, because a typo that silently does nothing is the worst kind. Pages converted with filters record a hash of the rules in their frontmatter, so a filtered copy is never mistaken for the real thing.
Sharing without the network
u2md pack bundles the Markdown together with the raw responses behind it into one file. Packing the same tree twice
gives the same bytes, so packs diff cleanly. The 17-page Python tutorial came to 265 KB.
A teammate with an empty cache can unpack it and rebuild with --offline, and get the original files byte for byte.
I checked. Since a pack can come from anywhere, unpacking is defensive: every body must hash to its own name, nothing
may land outside the target folder, only Markdown goes under docs/, and an old pack can never replace a newer local
copy.
A crawler you can’t point inward
A tool that fetches whatever URL an agent hands it is an SSRF bug waiting for a prompt. So u2md resolves a host once, refuses it if any address is private, loopback or link-local, and then connects to the checked IP instead of the hostname, so DNS can’t answer differently the second time. Every redirect gets the same check, and proxy settings are ignored on purpose, because a proxy would make the check meaningless.
It also has to explain itself. For a while, blocking localtest.me said disallowed by robots.txt, because fetching
robots.txt is what hit the guard first. Now it says:
u2md: error: fetch: blocked by SSRF guard: localtest.me -> 127.0.0.1 (pass --allow-private to permit private, loopback or link-local hosts)
Try it
You need Go 1.26 or newer:
go install github.com/CyberSarvesh/u2md/cmd/u2md@latest
u2md mirror https://docs.python.org/3/tutorial/ -o docs --max-pages 25
u2md context docs/docs.python.org "how do I handle exceptions with finally" --budget 1500
To give it to Claude Code or Cursor, add it to your MCP config:
{
"mcpServers": {
"u2md": { "command": "u2md", "args": ["mcp", "--dir", ".u2md-docs"] }
}
}
Some honest limits:
- No JavaScript. Docs that only exist after a browser runs a script come out empty. u2md warns when a page looks like that, but it can’t fix it.
- Search is words, not meaning. BM25 and a small stemmer are deterministic and fast, and they miss synonyms.
- Token counts are estimates, at about four characters a token.
- One known leftover: docusaurus.io’s
/docs/nextlanding page still gets mirrored next to the current one.
What I’d add next
- Section-level changelogs for
sync, so “the docs changed” becomes “the section on retries changed”. - Opt-in rendering for the JavaScript-only sites, kept out of the default path.
- Better ranking for short, precise sections, which still lose to long ones that mention everything.
The network is the part you can’t fix. Use it once, keep the bytes, and make everything after that local.
The code is on GitHub. If you have a docs site it mangles, that’s the issue I most want to read.