A retain whose write transaction fails repeats its fact extraction on the task retry

Open
#7,218 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
53/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, python
Domain
ai, backend, databases

Research direction

Start in engine/retain/orchestrator.py and trace how _run_delta_db_work and _run_mini_batch_db_work handle write failures after extraction. Read the history of commit 5aa3de601 and issues #7216 and #7217 for the remaining lock-order cycles. Done means a bounded retry reruns the write phase within the same task using extracted facts, while a later task retry still starts from extraction.

Written by the indexing model from the issue text.

Description

core
Bug Description

When a retain's write transaction fails after fact extraction, the worker retries the whole task, and the retry runs the LLM extraction again for the same chunks.

_run_delta_db_work and _run_mini_batch_db_work (engine/retain/orchestrator.py) run outside retry_with_backoff, so a DeadlockDetectedError or an asyncpg TimeoutError in the write phase fails the task. The poller schedules a retry (HINDSIGHT_API_WORKER_TASK_RETRY_BACKOFF_SECONDS), and the retried task starts from extraction. The extracted facts and embeddings of the failed attempt are not kept, though nothing about them changed: PostgreSQL rolled back only the write. _run_delta_db_work already resets its per-attempt state (log buffer, fact document and chunk ids, pending entity stats) at its start.

5aa3de601 removed retry_with_backoff from both write phases because sorted inserts prevent deadlocks. #7216 and #7217 are two lock-order cycles that still occur on 1.3.1.

Steps to Reproduce
  1. Run two workers on one bank with the default HINDSIGHT_API_RETAIN_MAX_CONCURRENT=4.
  2. Retain a few hundred changed documents into the bank at once, so that write phases overlap.
  3. Read the bank's LLM request traces: GET /v1/default/banks/{bank_id}/llm-requests?scope=retain_extract_facts. Group the calls by metadata.operation_id and by the hash of the user message.
Expected Behavior

A write phase that PostgreSQL aborts as a deadlock victim runs again in the same task with the facts already extracted, a bounded number of times, before the task fails. A task retry after that reuses nothing it cannot trust, as today.

Actual Behavior

One bank, 24 hours on 1.3.1: 3,051 retain_extract_facts calls, of which 934 extracted a chunk that the same operation had already extracted. The worker log in the same window shows 75 task retries after DeadlockDetectedError and 43 after TimeoutError in the write phase. Each retry repeats every extraction call of the operation; three retries of a 20-chunk document cost 80 extraction calls instead of 20.

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.