Delta retain matches chunks by position, so an insertion near the top re-extracts the rest of the document

Open
#7,243 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
ai, backend

Research direction

Read hindsight_api/engine/retain/orchestrator.py around _classify_chunk_diff (lines 4064–4080) and the full-retain fallback around line 4310. Trace how stored and new chunks are matched and how chunk_id depends on the index; reproduce with the 20-paragraph document described in the issue. Done means inserting one paragraph at the start triggers extraction only for that new chunk, without the full-retain fallback.

Written by the indexing model from the issue text.

Description

core
Bug Description

_classify_chunk_diff compares each new chunk with the stored chunk at the same chunk_index (engine/retain/orchestrator.py L4064-L4080). Text inserted or removed early in a document shifts every later chunk to a new index, so chunks whose content_hash matches a stored chunk count as changed and are extracted again. If no chunk keeps its index, delta falls back to a full retain (L4310).

#6763 raised positional identity more broadly; #6807 fixed the stale-replace case and left the rest out of scope. This is the narrower case inside one document.

Steps to Reproduce
  1. Retain a document of 20 paragraphs of about 2,900 characters each (one chunk each at the default retain_chunk_size).
  2. Retain it again with one new paragraph inserted first.
  3. Read GET /v1/default/banks/{bank_id}/llm-requests?scope=retain_extract_facts&document_id={document_id} for the second retain, and the worker log.
Expected Behavior

One extraction call, for the new chunk.

Matching by content_hash before index would avoid it; chunk_id is derived from the index, so a matched chunk that moves would need its id rewritten.

Actual Behavior

21 extraction calls, and the worker logs Delta retain: no unchanged chunks for {document_id}, falling back to full retain.

On one of our pages, 116 of 131 chunks had the same text as a stored chunk and were extracted again.

Version

1.3.1

LLM Provider

Gemini (Vertex AI)

Dominant language
Python
Stars
45.2k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vectorize-io/hindsight

All issues in vectorize-io/hindsight

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.