Canonical page: [Ingestion and Sources](https://cloudflavor.io/aemon/docs/ingestion/)
Source snapshot: https://github.com/cloudflavor/aemon/blob/a85ee1d024b81c6373acd86379fe12f15c26a7a5/docs/ingestion.md

# Ingestion and Sources

Eight source types feed one corpus: DevDocs docsets, docs.rs rustdoc JSON, git
repositories, `llms.txt` sites, npm and PyPI READMEs, Dash/Zeal docsets, the Apple
Developer JSON API, and crawled documentation sites. Each produces the same `pages` and
`entries` shape, which is why retrieval does not care where a chunk came from.

## Sources

| source | command | what it covers |
|---|---|---|
| DevDocs packages | `aemon devdocs add --all` | 828 versioned docsets across languages, DBs, servers |
| docs.rs rustdoc JSON | `aemon crate serde@0.9` | any Rust crate, exact version; facades follow their `-*` siblings |
| git repositories | `aemon repo docker/docs --path content` | docs that only exist in a repo (markdown or HTML) |
| `llms.txt` | `aemon llms https://nextjs.org/docs/llms.txt --name nextjs` | projects that publish LLM-ready docs (`-full` used automatically) |
| npm / PyPI README | `aemon pkg prisma` / `aemon pkg pydantic --lang pypi` | the registry long tail |
| Dash/Zeal docset | `aemon add <name>` | curated docsets outside DevDocs |
| Apple Developer | `aemon apple swiftui --pages 600` | walks the JSON API tree: declarations, prose, code listings, topic lists |
| a documentation site | `aemon crawl add ...` | anything with a sitemap or a `robots.txt`; see [crawling](crawling.md) |

`aemon repo` uses a path-scoped sparse checkout, so `--path articles` on a 2 GB
repository materialises only the articles directory. Clones are cached under
`~/.cache/aemon/repos`; `--ref <tag>` reads the docs at a specific version.

## What a docset is

A docset (the Dash/Zeal format, which DevDocs tooling also wraps) is:

```
<Rust>.docset/Contents/Resources/docSet.dsidx         SQLite: searchIndex(id, name, type, path)
<Rust>.docset/Contents/Resources/Documents/**/*.html  the actual documentation
```

`docSet.dsidx` contains **only symbol names and paths** — no prose. That is why a naive
dsidx reader looks like a great "docs database" and then fails on any question not
phrased as a symbol. aemon indexes both: symbol rows for exact lookups, page-body
chunks for concept queries.

Feeds are `https://kapeli.com/feeds/<Feed>.tgz` (`Rust`, `Bash`, `Python_3`,
`PostgreSQL`). Kapeli's catalog JSON (`/feeds/zzz/versions.json`) now 301s to the Dash
product page, so names must be supplied — `aemon add rust` guesses `Rust`, and
`--feed` overrides.

## Chunking

`<h1>`–`<h6>` **and `<dt>`** are marked before HTML flattening, so chunks break on
section boundaries first and then at ~1 400 characters on paragraph boundaries
(`--chunk` overrides the size). One chunk ≈ one topic, which is what makes the output
usable as model context.

Two data-shape lessons from the real Bash docset (GNU Texinfo output: 0 headings, 300
`<dt>` terms, index tables everywhere):

- **`<dt>` is a section anchor.** Heading-only chunking labelled every chunk "overview".
- **`<table>` blocks are dropped before flattening.** html2text renders them as
  box-drawing art that costs tokens and contains no prose.

Titles are cleaned of texinfo permalink and footnote artifacts
(`6.6 Aliases[ ¶][6]` → `6.6 Aliases`).

## Quality rules

- `<pre>` blocks are marked before flattening and emitted as ``` fences; a fence is
  never split by the chunker, and a runaway fence is force-closed at 4 000 characters
  (an unclosed `<pre>` in generated HTML once produced a 1.2 MB chunk; the hard cap is
  6 000).
- An unclosed `<table` no longer deletes the rest of the page. That bug reduced NumPy's
  89 KB *Broadcasting* page to 2 chunks; it now yields 12, and the page ranks first for
  "broadcasting rules".
- Chunks under 80 characters are dropped, duplicate bodies within a page are dropped,
  and `compact --dedupe` does the same across the whole corpus.
- `prune -d <corpus>` deletes `nav` and `boilerplate` chunks, keeping `content` and
  `example`; see [retrieval](retrieval.md).

## Corpus shape

`aemon audit` measures what a model actually gets. On a full corpus: 2.93 M chunks
(p50 397 chars, p95 1 445), **41 % contain code**, 577 k chunks carry fenced code
blocks, 0 % under 80 characters, no duplicate bodies, 1.75 G characters ≈ 437 M tokens
in 4.7 GB.

That is the number to watch when ingestion changes: a chunker regression shows up as a
collapsed p50 or a drop in the code share long before it shows up in a benchmark.

## Filling gaps from git repositories

```sh
aemon repo docker/docs --path content --name docker-docs
aemon repo serde-rs/serde-rs.github.io --name serde-attrs
```

Shallow clone (cached), sparse checkout, then markdown is chunked by its own headings
and HTML goes through the same extractor as DevDocs. Front matter, MDX imports and Hugo
shortcodes are stripped; generated HTML is skipped when the markdown source with the
same stem is present.

This closed two long-standing holes: Docker's DevDocs docset stopped at 20.10 (2021),
and serde's attribute reference only exists on serde.rs. The stale DevDocs Docker
corpora were removed afterwards, since current docs outrank them.

## Enrichment economics

`aemon enrich -d <corpus> --limit N` generates one model summary per chunk and indexes
it into FTS. It is expensive and does not pay for itself yet: ~0.5 s/chunk with 8
parallel calls. Summarising 100 chunks per library (1 321 chunks) lifted hit@1 87 % →
90 % but dropped judged quality 32/39 → 30/39, because only a fraction of the corpus was
enriched and the summary column skewed ranking.

Summaries are now display-only (bm25 weight 1.0, no heuristic boost) and `get`/`search`
show them as a `>` line. Uniform enrichment of a whole library is the only version
worth doing. Roles that run per chunk must point at a local model: `enrich` refuses a
metered endpoint unless `--allow-cloud` is passed.

## Adding a corpus

1. Ingest it with the matching subcommand.
2. Add the same command to `sources/` (`sources.txt`, `sources.json`, `rebuild.sh`) with
   its licence and attribution — that recipe is what is published, and every corpus row
   stores the same fields in the database.
3. Check `aemon audit` for the chunk shape and `aemon search` on two or three questions
   you know the answer to.
