codescout icon indicating copy to clipboard operation
codescout copied to clipboard

Dense embedder slot context (ctx_size/parallel) silently drops large memory cross-embeds and large chunks

Open mic-urs opened this issue 3 months ago • 2 comments

Summary

When the dense embedder (llama-server) is started with --ctx-size N --parallel P, llama.cpp divides the context across slots, so each request is capped at N/P tokens. With the CPU stack defaults (--ctx-size 8192 --parallel 8), every slot accepts at most 1024 tokens. Any payload above that gets HTTP 400 (exceed_context_size_error), and codescout degrades silently: the caller only sees a non-fatal warning, and the content is stored without a vector.

Root cause

llama-server --ctx-size 8192 --parallel 8 → 1024 tokens per slot. Verified directly against the running server:

POST /v1/embeddings  (~4.5KB input)
HTTP 400 {"error":{"code":400,"message":"input (1682 tokens) is larger than the max context size (1024 tokens). skipping","type":"exceed_context_size_error"}}

Impact (two surfaces)

  1. Memory cross-embed (src/tools/memory/mod.rs::cross_embed_memory): any structured memory larger than ~1024 tokens (~3-4KB) is written to disk but never reaches the semantic store — invisible to recall, with only a warning:

    "cross-embed failed: dense openai status"
    "semantic anchor creation failed: dense openai status"
    

    The write reports "status": "ok", so from the caller's perspective nothing failed. Larger memories accumulate silently outside the semantic index.

  2. Code indexing: chunks above 1024 tokens (AST_CHUNK_TARGET is ~3000 chars ≈ 750-1000 tokens, so long functions / dense markdown blocks can exceed it) presumably hit the same 400 at sync time. Whether sync surfaces these as errors or drops them silently is worth verifying — if dropped, indexes have holes that a no-op sync will not reconcile (nothing marks them dirty).

Suggested fixes (any combination)

  • Config: raise effective per-slot context in the CPU stack script (retrieval-stack.sh / compose): e.g. --ctx-size 32768 --parallel 8 → 4096/slot, matching the GPU profile's 65536/16. KV-cache cost for a 137M embedding model is modest.
  • Code (robust): in cross_embed_memory, split content into sub-1024-token segments, embed each, and mean-pool (or store the first segment + warn). Same for oversized chunks in the sync path.
  • Observability: the warning "dense openai status" gives no hint that the cause is payload size. Lifting the HTTP body snippet (or at least exceed_context_size_error) into the warning would make this self-diagnosing. A one-line startup probe reporting effective per-slot context (ctx-size / parallel) would also catch misconfiguration early.
  • Repair path: memories already written without vectors need re-embedding; the new in-place re-embed migration (feat(migrate): re-embed memories in place) may already cover this — worth confirming it picks up entries that lack vectors rather than only those on the old convention.

Environment

  • codescout: experiments branch, build 2026-08-19 (post-merge including chunk_id start_line fix)
  • Backend: CODESCOUT_VECTOR_BACKEND=sqlite-vec, dense = llama-server CPU (CodeRankEmbed-Q4_K_M.gguf), ports 48081/48083/48084
  • Happy to provide logs or test a patch.

mic-urs avatar Aug 20 '26 09:08 mic-urs

Follow-up finding from the field fix: the slot math is only half the story. After recreating the dense container with --ctx-size 65536 --parallel 8 (8192/slot), a 3000-token payload still fails with max context size (2048 tokens) — the real ceiling is the model's own training context (CodeRankEmbed n_ctx_train = 2048), which llama.cpp enforces regardless of --ctx-size. Net effect: the config fix lifts the practical ceiling from 1024 to 2048 tokens, but memories/chunks above 2048 tokens still can't be embedded as a single call. This strengthens the case for the code-side fix (segment + pool in cross_embed_memory, and the same for oversized chunks in sync): no server configuration can push past the model ceiling. Also worth noting in docs: the error message does surface n_prompt_tokens/n_ctx, but codescout's warning currently drops the response body, so none of that reaches the user.

mic-urs avatar Aug 20 '26 10:08 mic-urs

Fixed — and your follow-up comment about the model ceiling is the reason the fix has the shape it does, so thank you for it. It inverted the remedy: no server configuration can push past n_ctx_train, so the config change alone would have been a false fix.

Fixes: 26feb1aa (patch-id 97a8127b4e761e065a2a8090107473f9a1186082) — segment and mean-pool oversized memory embeds; a5f8e5ad (f248f9e159cf60f848ddf487182f3dc3125ba21b) — stop an oversized chunk truncating the index build; 67c548b9 + 5f8c42ec — lift the HTTP response body into the error. Related: 8acec9c7 (de0b0990236e69bf18ef2cbff041cbaa3d565652) — the chunker's budget was a target, not a bound, so a single long line could not be split.

Answering your three surfaces directly:

1. Segmentation now exists, and the budget is a real bound. A shared primitive (embed_document_pooled) returns a single call below budget, and above it segments, embeds each, and mean-pools to a unit vector. segment_for_budget prefers line boundaries but hard-splits a line longer than the budget by characters — so it is a bound, not a target. Four production paths use it: cross_embed_memory, create_semantic_anchors (same point id, so it had to segment too or it would overwrite a good pooled vector), EmbeddingService::embed_artifact, and the memory migration embedder.

2. Your n_ctx_train = 2048 correction is encoded in code. chunk_size_for_model has a CodeRankEmbed arm returning 2048 tokens, annotated as measured by binary search against a live llama-server (8000 chars OK, 8250 HTTP 500). No consumer takes a literal; each takes its budget from that function.

3. The response body is surfaced. The remote embedder now builds EmbedError::Status { url, status, body } from resp.text(), and rendering interpolates the body (trimmed to 400 chars) after the status. So you would see cross-embed failed: embed status failed: <url> — HTTP 500 from embedding server: {"error":{...}} rather than dense openai status. That string no longer exists anywhere in the tree. A test pins the body surviving, so it is a contract rather than an accident.

Code indexing deliberately does not segment — it isolates a failed batch to one request per chunk, stores what embeds, and reports the rest as skipped with the cause, while still aborting loudly on a genuinely dead embedder. That was the #17 defect and is fixed with it.

Two residues from your report that are still open, both real:

  1. No startup probe of the effective per-request ceiling. You suggested one; it was not built. A misconfigured backend is still discovered at the first oversized write rather than at connect.
  2. No machine-readable marker on the memory write. You correctly noted the write reports "status": "ok". It still does — there are warnings, but no embedded: false field a caller could branch on. The failure cause can no longer arise for oversized content, so the warning path now means what it says; but a programmatic caller still cannot distinguish "stored and embedded" from "stored only".

Internally: docs/issues/archive/2026-08-26-dense-embedder-slot-context-drops-large-embeds.md.

mareurs avatar Sep 15 '26 08:09 mareurs