Skip to content

contextual: a title from front matter, and nothing the chunk already holds - #1

Merged
1ay1 merged 4 commits into
agentty-org:mainfrom
sail3r:contextual-title
Dec 3, 2026
Merged

1ay1 merged 4 commits into
agentty-org:mainfrom
sail3r:contextual-title

Conversation

@sail3r

@sail3r sail3r commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

contextual: a title from front matter, and nothing the chunk already holds

What was wrong

When no LLM contextualizer is set, contextualize() uses extractive_context(). It prepends the document's title and its best-overlapping sentence to every chunk before indexing. The title was simply the document's first line. On real corpora that goes wrong in three ways:

document context prepended before after
page with YAML front matter (pilot.md) ---, or --- — title: Harbour Pilot Manual Harbour Pilot Manual — The harbour pilot boards…
front matter, one paragraph (lighthouse.md) --- / --- — title: Lighthouse Keeping Lighthouse Keeping
one-line document (crane.md) the whole line, so the chunk reads twice nothing
# heading page, first chunk (tides.md) Tide Table Notes, which the chunk already starts with nothing

Each row is the real output of extractive_context() from upstream/main and this branch, on the same four files.

In agentty, which uses this as its retrieval backend:

  • Every page of its docs site starts with front matter, so every retrieved docs passage began with --- — and a fragment of a table row.
  • Every learned-memory fact is a one-line document. In one user's history, 213 of 213 retrieved memory passages reached the model twice, which was 50.1% of all retrieved bytes.

The context also feeds indexed_text(), so the noise went into BM25 and the embeddings as well as the text the model reads.

The change

src/text/contextual.cpp:

  • Title. It comes from front matter when there is any, otherwise from the first non-empty line after it, without heading marks. Supported front matter:

    • YAML title:, quoted or not, including a block title (title: >, which takes its first line);
    • TOML title = "..." between +++ fences;
    • either one after a UTF-8 byte-order mark.

    An unclosed fence isn't treated as front matter.

  • Front matter isn't prose. It's no longer a source of candidate sentences.

  • Nothing the chunk already holds is repeated.

    • The title is left out only when one of the chunk's lines is the title: its heading, its front-matter key, or the whole of a one-line document. A chunk that merely mentions a short title ("see Usage above") still gets it.
    • A heading line that is the title (# Title) and a setext underline (===, ---) are never chosen as the best sentence.

An LLM Contextualizer, when one is set, is unchanged.

Tests

tests/test_main.cpp, next to contextualize_adds_situating_context:

  • extractive_context_reads_the_title_from_front_matter:
    • title: wins, and the context holds no ---, front-matter key or # heading;
    • with no title: key, the first line after the front matter is used;
    • a quoted title containing a colon, a folded block title, TOML front matter, a byte-order mark, and an unclosed fence.
  • extractive_context_does_not_repeat_what_the_chunk_holds:
    • a one-line document and a whole single-chunk document get an empty context;
    • a later chunk still gets the title it lacks, as does a chunk that only mentions the title;
    • a setext heading gives its title and never its underline.

The existing contextual tests pass unchanged: their documents have no front matter, and their chunks don't contain the title.

No warnings outside the library

A full build (tests, examples, bench) printed 39 -Wunused-result warnings with GCC 16. Compiling the same files with the library's own -Wall -Wextra -Wpedantic turns up 4 more, which the default build doesn't show because only the ragcpp target gets those flags. CI didn't catch any of them because its -Werror job only covers ragcpp.

  • 39 -Wunused-result. libstdc++ 16 marks std::expected [[nodiscard]], so every discarded add() / build() result warned.
    • The examples now check each result through must() in examples/must.hpp, which prints the error and exits. Examples are what users copy, so they shouldn't show failures being dropped.
    • ragcpp_rcp_tests gets the same -Wno-unused-result that ragcpp_tests already has (/wd4834 on MSVC).
    • The two benches cast their setup results to void.
  • 1 -Wunused-variable (under -Wall) in include/rag/rcp/convert.hpp: an unused double b.
  • 3 -Wcomment (under -Wall): usage comments ending in a backslash, which carried the comment onto the next line.
    • The two plugin build commands are now one line each.
    • The code_retrieval_bench usage keeps its two lines, without the backslash.

tests/test_main.cpp still has three -Wmissing-field-initializers warnings under -Wextra (lines 1838, 1876, 3396). They were already there before this branch and fall outside it.

Note for hosts that persist an index

The stored chunk context changes, so an index saved with an earlier version keeps the old contexts until it's rebuilt. agentty will raise its index version when it bumps the submodule.

…holds

The extractive context took a document's first line as its title and
prepended it to every chunk. On real corpora that went wrong three ways:

- Front matter. A page that opens with YAML front matter got the title
  "---", and its best sentence could be a front-matter line
  ("--- — title: Harbour Pilot Manual"). Every page of a docs site built
  with front matter was affected.
- One-line documents. The title is the whole document, so the chunk was
  sent with its own text prepended: every memory fact in an agentty
  history (213 of 213 passages) reached the model twice.
- First chunks. The heading is both the title and inside the chunk, so it
  was repeated; and a later chunk could get the heading line ("# Title")
  as its best sentence, after the title itself.

The title is now front matter's `title:` when present, else the first
non-empty line after the front matter. Front matter is not a source of
sentences. The title is left out when the chunk contains it, and a heading
that is the title is not a candidate sentence: the rule the best sentence
already followed.
A full build (tests, examples, bench) printed 39 -Wunused-result warnings
under GCC 16, whose libstdc++ marks std::expected [[nodiscard]]. Compiling
the same files with the library's own -Wall -Wextra -Wpedantic shows four
more (three -Wcomment, one -Wunused-variable) that the default build hides,
because only the ragcpp target gets those flags. CI's -Werror lane covers
only ragcpp too, so none of them failed a build.

- Examples check every add() and build() through a small must() that
  prints the error and exits. They are what users copy, so they should
  not show failures being dropped.
- ragcpp_rcp_tests gets -Wno-unused-result (/wd4834 on MSVC), the same
  fixture idiom ragcpp_tests already has.
- The two benches cast their setup results to void.
- include/rag/rcp/convert.hpp: an unused `double b`.
- Three usage comments ended a line in a backslash, which continued the
  comment onto the next line; each command is now on one line.
… line

Review follow-ups to the front-matter title:

- The title check was a substring match, so a chunk that only mentioned a
  short title ("see Usage above") lost its title context. A chunk now holds
  the title only when one of its lines is the title: its heading, its
  front-matter key, or the whole of a one-line document.
- Front matter: a UTF-8 byte-order mark before the fence, TOML front
  matter (+++, title = "..."), and a YAML block title (title: >) taking
  its first line instead of ">".
- A setext underline (=== or ---) is heading markup, never a best
  sentence; the setext heading text is the title, as before.

Tests cover each shape, a quoted title with a colon, an unclosed fence,
the mid-sentence mention and a setext heading.
Each example carried its own copy of must(). They now include
examples/must.hpp, and the include blocks are back as they were plus that
one line. The code_retrieval_bench usage comment is split across two
lines again, without the backslash that made -Wcomment fire.
@1ay1
1ay1 merged commit 423a767 into agentty-org:main Dec 3, 2026
7 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.

2 participants