Registry Mirror
Skip to content

Conversation

@Saifjafri212213

@Saifjafri212213 Saifjafri212213 commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Contextual retrieval for @chatcops/ingest and chatcops ingest. Before a chunk is embedded, a model writes a sentence or two that places it in its page, and that is embedded with the chunk, so "It costs $20 a month" still matches a question about the Team plan. It is the recipe Anthropic calls contextual retrieval.

Closes #554

What changed

@chatcops/ingest

  • IngestConfig.contextualize?: false | { provider: ProviderConfig | AIProvider }.
  • New src/contextualize.ts. For each chunk it asks the model one question: the whole page (doc.markdown, cut at 100,000 characters) is in the system prompt, and only the chunk (with its heading path) is in the message, with maxTokens 120. So the page is the same cached prefix for every chunk of a page. Chunks are asked one at a time and in order, so the first call can warm a provider's prompt cache and the rest read it. The system prompt says the page and the chunk are data, not instructions, since crawled text is untrusted. An answer is trimmed to one line and kept to 600 characters, and an empty answer means that chunk is embedded as it is.
  • ingest.ts: the embedded text is context + "\n\n" + chunk.text. The stored record's text stays the chunk alone, and the context is kept in the record's metadata as context. A ProviderConfig is started with createProvider from @chatcops/core (already a peer dependency), loaded only when a run uses a model, and before anything is fetched, so a model that can't start fails the run with The model for contextualize can't start: ... and not every page. A dry run starts no model and makes no call.
  • Re-embedding when the setting changes: the page's manifest record has a new optional contextualize field with provider:model (the config's type:model or the provider's name:model). A page is reused only when that matches what the run is using, so turning contextualize on or off, or changing the model, embeds pages again. That check comes before the conditional request (If-None-Match), so a page the server would answer 304 for is fetched and embedded again as well. force still re-embeds everything.
  • A model call that throws fails the page (failed, with the reason): the page keeps what was indexed and its manifest record, so the next run tries it again. I chose that over embedding without context, which would be recorded as done and never retried.
  • IngestStats has contextualized (chunks that got a context) and modelTokens ({ inputTokens, outputTokens }, from the provider's finish usage, cache reads and writes included for Claude).

@chatcops/cli

  • --contextualize (in the help). The config holds which model, in ingest.contextualize.provider, and the flag turns it on. Without the flag a run passes contextualize: false, so the CLI never spends on a model by surprise even when the config has a provider. With the flag and no provider it stops with exit 2 and --contextualize needs a model: set ingest.contextualize.provider in the config ..., before anything is embedded.
  • The summary says Context written for N chunks by the model (X input, Y output tokens)., and --json has contextualized and modelTokens in stats next to embedded.

Docs and the rest: a "Contextual retrieval" section, the option row, the stats and the CLI line in core/knowledge-base.mdx (with the cost note), minor changesets for both packages, and api/ingest.api.md.

Notes for review

  • I could not call a real model here, so the tests use scriptedProvider. The cost figure in the docs is Anthropic's (about $1 per million document tokens with caching), attributed to them, and the docs say a provider without prompt caching reads the page for every chunk.
  • Deciding what "the flag turns on" means was the one judgment call: the library ingest() uses contextualize whenever it is set, and only the CLI needs the flag. If you'd rather have the config alone turn it on in the CLI too, it is the one line that passes false.
  • IngestStats gained two required fields, so code that builds one by hand needs them (nothing in the repo does).

How I Tested This

  • Wrote the tests first: 16 of the 18 in tests/contextualize.test.ts (ingest) failed before the change (the other two, a dry run that never starts a model and a long-answer cap, passed as guards), and all 7 new CLI tests failed too.
  • Ingest tests with scriptedProvider: the embedded text has the context in front; the stored text doesn't and the metadata has it; the page is in the system prompt, the same for every chunk, and the message has only the chunk; the data-not-instructions line; one call at a time and in order (checked with a provider that counts how many are running); the counts and the tokens in the stats; a dry run calls nothing and doesn't even start a provider that couldn't start; turning it on re-embeds unchanged pages that answer 304, a second run with it embeds nothing, turning it off re-embeds again and clears the manifest field; changing the model re-embeds; the manifest records what wrote the context; force asks again; a failing model fails the page and leaves the index and the manifest as they were; an empty answer embeds the chunk as it is; a long answer is capped; a long page is cut and says so; a provider config that can't start fails before anything is embedded; false is off.
  • CLI tests: the flag in the help; the model's context is embedded and the summary line is printed; the JSON stats; no model without the flag even with a provider in the config; the error, with nothing embedded, when the config has no provider; no model with --dry-run; turning the flag on re-embeds an unchanged page and a second run with it doesn't.
  • Mutated the code in 22 ways (ingest: no re-embedding on a change, the model ignored in the id, the context stored with the text, no metadata, a model started in a dry run, tokens and chunks not counted, nothing in the manifest, the context not embedded, parallel calls, no page cut, no answer cap, the page not in the prompt, no injection note, the id ignoring the model; CLI: the config always applying, the flag never turning it on, no missing-provider error, no summary line, the flag not parsed, no help line). One survived because it can't differ: an empty context string is falsy where it is used, the same as undefined.
  • 62 ingest tests and 121 CLI tests pass, with coverage at 94.0 / 87.1 for ingest and 96.6 / 90.2 for the CLI (limits 85/80), typecheck is clean for both, lint is at 38 warnings (the same as dev), api:check is up to date for both, and pnpm docs:check finds no errors in the knowledge base docs (other packages' samples fail it here for missing builds).

AI assistance

An AI coding assistant helped write the change and the tests. I ran the checks listed above.

…ach chunk before embedding (codercops#554)

ingest() takes contextualize: { provider }. For each chunk a model writes a
sentence or two that places it in its page, with the page in the system prompt
(so a provider that caches prompts reads it once) and the chunk in the message.
The context is embedded with the chunk, the stored text stays the chunk and the
context goes in its metadata. Turning it on or off, or changing the model,
embeds pages again; a dry run asks no model; the stats count the chunks and the
model tokens. chatcops ingest --contextualize turns it on from the config.

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Saifjafri212213, this is careful work. With dev merged in, build, typecheck, lint (38), api:check, docs:check, coverage and all 85 e2e tests pass.

I also ran it for real with Claude over three docs pages:

  • all 22 chunks got a context (the theming one: "This chunk is from the Theming section's 'Custom colors' subsection…"), and the stored text stayed the chunk alone
  • a second run with the same setting embedded nothing
  • turning it off re-embedded all three pages

Failing the page on a model error, so it's retried, is the right call over embedding it without context and never coming back to it.

@anurag629
anurag629 merged commit b2ae289 into codercops:dev Oct 31, 2026
@anurag629

Copy link
Copy Markdown
Member

Merged, thanks @Saifjafri212213.

@github-actions github-actions Bot added codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com) hacktoberfest-accepted PR counts for Hacktoberfest labels Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codercops-accepted Merged PR that counts on the monthly leaderboard (oss.codercops.com) hacktoberfest-accepted PR counts for Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ingest: --contextualize, a short model-written context for each chunk before embedding

2 participants