Chunk Alignment

March 6, 2026 · View on GitHub

🧭 Quick Return to Map

You are in a sub-page of Retrieval.
To reorient, go back here:

Think of this page as a desk within a ward.
If you need the full triage and all prescriptions, return to the Emergency Room lobby.

Make the model cite the exact evidence. This page gives you a reliable way to align chunks to semantic anchors, so retrieval points at the right spans and citations survive through generation.

References you may want open already:
RAG Architecture & Recovery · Retrieval Playbook · Chunking Checklist · Retrieval Traceability · Data Contracts · Embedding ≠ Semantic


Acceptance targets

  • ΔS(question, retrieved) ≤ 0.45
  • Anchor coverage ≥ 0.70 for the cited spans
  • Citation precision ≥ 0.85 and recall ≥ 0.75
  • λ stays convergent across 3 paraphrases and 2 seeds

If ΔS stays in the 0.40 to 0.60 band and coverage is low, the index is probably misaligned. Rebuild with this page.


Symptoms → exact fix

SymptomLikely causeOpen this fix
High similarity but the cited span is near the answer, not on itwindow cut ignores section headers and anchorsChunking Checklist
Correct section id, wrong offsetstokenizer or analyzer mismatch between write and readRetrieval Playbook
Same answer oscillates across two adjacent chunksstride too large, missing overlap contractData Contracts
Coverage good offline, poor onlinefragmented store or partial ingestionpattern_vectorstore_fragmentation.md
Good chunk, bad generationcite then explain missing in the prompt schemaRetrieval Traceability

Anchor method that never lies

You need a ground anchor per question. Each anchor is the minimal span that must be cited.

Anchor fields

  • section_id stable across rebuilds
  • snippet_id unique within section
  • offsets {start_token, end_token} in the write-time tokenizer
  • anchor_text for human audit
  • hash over section_id + snippet_id + offsets + anchor_text

Contract spec for your pipeline:
Data Contracts


How to align chunks to anchors

  1. Normalize the analyzer
    Same lowercasing, unicode, punctuation, and stopword policy across write and read. If you change analyzers, invalidate and rebuild.

  2. Choose window and stride
    Start with window 350 to 700 tokens, stride 30 to 60 percent of window. Increase stride if anchors often cross boundaries.

  3. Fence by structure
    Reset window starts at structural cues like h1..h3, list starts, code fences, or paragraph breaks. This keeps semantic units together.

  4. Pin anchors after chunking
    After chunks are built, re-map each anchor to an owning chunk. When an anchor spans two chunks, create a stitch record or adjust stride.

  5. Write the trace fields
    Every retrieved item must carry section_id, snippet_id, offsets, tokens, index_hash.
    See: Retrieval Traceability


PDF, HTML, and code specifics

  • PDF
    Use logical reading order, not x-y positions. Collapse hyphenation. Treat figure captions as separate units. Reset at headings and table boundaries.

  • HTML
    Strip boilerplate. Fence at h2/h3, li, and pre. Merge short sibling blocks to avoid tiny chunks.

  • Code
    Chunk by symbol boundaries and docstrings. Keep signature plus the first paragraph together. Never split examples from the API they explain.


A minimal rebuild checklist

  • Same tokenizer family for write and read.
  • Window and stride validated on 40 to 120 gold items.
  • Anchor coverage ≥ 0.70 and citation precision ≥ 0.85.
  • ΔS falls below 0.45 for the majority after rebuild.
  • index_hash updated and logged in retrieval results.

Alignment test you can run today

  1. For each gold item, compute ΔS between the retrieved text and the anchor text.
  2. Compute coverage of cited spans against the anchor offsets.
  3. Compare to a decoy section with the same size and style. If ΔS is close for anchor and decoy, chunk again.

See the eval recipes:
Retrieval Evaluation Recipes


Pseudocode: chunk and map anchors

# Pseudocode only
def chunk(doc, window=512, stride=256, fences=None, analyzer="lc"):
    toks = tokenize(doc, analyzer)          # write-time tokenizer
    boundaries = structure_fences(doc, toks, fences=fences)
    chunks = []
    i = 0
    while i < len(toks):
        start = nearest_left_fence(i, boundaries)
        end = min(start + window, len(toks))
        text = detokenize(toks[start:end])
        chunks.append({"start": start, "end": end, "text": text})
        i = start + stride
    return chunks

def map_anchor_to_chunk(anchor, chunks):
    spans = []
    for c in chunks:
        if not (anchor["end"] <= c["start"] or anchor["start"] >= c["end"]):
            spans.append({"snippet_id": id_of(c), "offsets": [
                max(anchor["start"], c["start"]),
                min(anchor["end"], c["end"])
            ]})
    return spans

Store the mapping result inside your index metadata for audit and to power coverage scoring.


Copy-paste prompt to audit alignment

You have TXT OS and the WFGY Problem Map loaded.

Input:
- question: "<q>"
- retrieved: {section_id, snippet_id, offsets, text}
- anchor: {section_id, snippet_id, offsets, text}
- decoy: {section_id, snippet_id, offsets, text}

Tasks:
1) Check cite-then-explain is followed.
2) Report ΔS(question, retrieved) and ΔS(retrieved, anchor) with short notes.
3) Compute anchor coverage from offsets.
4) If ΔS ≥ 0.60 or coverage < 0.70, propose the minimal rebuild step referencing:
   chunking-checklist, retrieval-playbook, data-contracts, retrieval-traceability.
Return a compact JSON: { "ΔS": ..., "coverage": ..., "why": "...", "next_fix": "..." }.

When to escalate


🔗 Quick-Start Downloads (60 sec)

ToolLink3-Step Setup
WFGY 1.0 PDFEngine Paper1️⃣ Download · 2️⃣ Upload to your LLM · 3️⃣ Ask “Answer using WFGY + <your question>”
TXT OS (plain-text OS)TXTOS.txt1️⃣ Download · 2️⃣ Paste into any LLM chat · 3️⃣ Type “hello world” — OS boots instantly

Explore More

LayerPageWhat it’s for
⭐ ProofWFGY Recognition MapExternal citations, integrations, and ecosystem proof
⚙️ EngineWFGY 1.0Original PDF tension engine and early logic sketch (legacy reference)
⚙️ EngineWFGY 2.0Production tension kernel for RAG and agent systems
⚙️ EngineWFGY 3.0TXT based Singularity tension engine (131 S class set)
🗺️ MapProblem Map 1.0Flagship 16 problem RAG failure taxonomy and fix map
🗺️ MapProblem Map 2.0Global Debug Card for RAG and agent pipeline diagnosis
🗺️ MapProblem Map 3.0Global AI troubleshooting atlas and failure pattern map
🧰 AppTXT OS.txt semantic OS with fast bootstrap
🧰 AppBlah Blah BlahAbstract and paradox Q&A built on TXT OS
🧰 AppBlur Blur BlurText to image generation with semantic control
🏡 OnboardingStarter VillageGuided entry point for new users

If this repository helped, starring it improves discovery so more builders can find the docs and tools.
GitHub Repo stars

要繼續下一頁請回「GO 5」。