Skip to content

fix(rag): re-index an edited file before a file-scoped query - #6936

Closed
github-actions[bot] wants to merge 1 commit into
mainfrom
autofix/issue-6932
Closed

github-actions[bot] wants to merge 1 commit into
mainfrom
autofix/issue-6932

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Asking about one specific document used to answer from the version GAIA indexed first. If you edited the file afterwards, every later question about it still got the old text, and nothing ever re-indexed it. Now the single-file question checks whether the file changed, the same check the index tool already makes. If it did, GAIA re-indexes it before answering. A large file that can't finish inside the usual indexing time reports "still indexing" instead of giving an old answer.

This is the fourth option from the issue thread, which still needs a product sign-off, so the PR is a draft until a maintainer confirms the direction.

Closes #6932

Test plan

  • python -m pytest tests/unit/rag/test_edited_document_reindex.py passes, and the new edited-file test fails without the fix
  • python util/lint.py --all passes
  • Full python -m pytest tests/unit/ in CI. Locally, only the RAG and indexing subset could run: 1,861 passed, and the 2 failures are python-pptx missing from this environment, which fail the same way without this change
  • Manual: in gaia chat, index a text file, ask about it by name, edit a fact in the file, ask again by name. The answer reflects the edit
  • Watch the rag_quality eval category on the next RC
🔍 Technical details
  • Root cause: query_specific_file resolved the target and called rag._retrieve_chunks_from_file without comparing the file's content key to the one it was indexed from.
  • Fix (src/gaia/agents/tools/rag_tools.py): once the target is resolved, if the file exists and rag.is_current() is false, the tool re-indexes through _index_within_budget.
    • It returns _indexing_in_progress when the budget runs out.
    • Embedding and cache-lock failures map to the existing helpers. Any other failure returns an actionable error, so the tool never quietly serves stale text.
    • Evicted documents are dropped from the agent's tracked set.
  • Path key: it re-indexes the path exactly as the index stores it, not the realpath, so a symlinked directory can't create a second index entry that would make the lookup ambiguous.
  • What stays the same: a deleted file keeps its current behavior. An unchanged file costs one content hash per call and is never re-embedded, which a test checks.
  • Tests: a same-size edit with the mtime restored, served through the real tool and a real FAISS index, plus a test that an unchanged file does not re-index.

A question scoped to one document searched its index without checking
whether the file had changed, so an edited document kept answering with
its old text on every file-scoped query. query_specific_file now runs the
same is_current check index_document does and re-indexes within the same
foreground budget, returning the still-indexing status for a big file.

Closes #6932
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Closing: #6947 covers this file-scoped fix and the other two search paths.

kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Nov 7, 2026
…md#6947)

If you edited a document after indexing it, document search kept
answering from the old text, across the whole library and for a single
file, until something re-indexed it. Now each search checks the files it
covers and re-indexes any edited file before answering. If a file can't
be served up to date, the search leaves it out and says why, with the
same fix-it advice the index tool gives (embedder down, document cache
locked). It never quietly serves the old text.

Closes amd#6939, closes amd#6932

Builds on @rezamuliawijaya's work: amd#6937 (the file-scoped re-index, the
kept error advice, and the same-size end-to-end test) and amd#6942
(checking the file's stat before hashing it).

## Test plan
- [ ] `python -m pytest tests/unit/rag/test_query_staleness.py
tests/unit/rag/test_edited_document_reindex.py`. The bug-reproduction
tests fail on `main` for the library-wide, file-scoped and exact-text
searches, and pass here.
- [ ] In `gaia chat`, index a text file and ask about it. Edit the file,
ask again, and the answer reflects the edit with no re-index request.
- [ ] Ask the same question twice about an unchanged file. The second
answer comes back as fast as the first and nothing re-embeds.
- [ ] Delete an indexed file and ask about it. The agent says the file
no longer exists instead of quoting it.
- [ ] Eval: this changes the tool results the model sees (new
`files_left_out` field, `in_progress` / `error` for stale files). Tool
docstrings are unchanged. Re-run `rag_quality` and `gaia_rag` on the
next RC.

<details>
<summary>🔍 Technical details</summary>

- **Cheap check:** `RAGSDK.freshness(path)` returns `current` /
`changed` / `missing` / `locked` / `unreadable` / `not_indexed`. It uses
a stat recorded at index time (size, mtime, inode, plus ctime on POSIX)
and hashes only when that stat changed, or when the file was written
within 2 s of being recorded. The 2 s rule covers the same-tick rewrite
that would otherwise fool the stat. Limitation: on Windows, which has no
change time, an edit that restores both size and mtime more than 2 s
after indexing passes the stat check. An explicit `index_document` still
catches it, because that path always hashes.
- **No global lock during the re-embed:** `index_document` on an edited
file no longer goes through `reindex_document`. It builds the new
generation outside `_state_lock` and swaps it in through the existing
`_drop_other_generation` publish step. The old text keeps serving until
the swap and stays if the re-index fails. A test probes the lock from
another thread mid-embed. `reindex_document` itself (the file-watcher
path) still holds the lock, so amd#6911 stays open for that path.
- **Budget:** edited files re-index through `_index_within_budget` and
share one 30 s deadline (`QUERY_REINDEX_BUDGET_S`) across the set.
- **What the model sees:**
- A file-scoped search on a file still re-indexing returns
`in_progress`.
- A failed re-index returns the index tool's own result:
`_index_embedding_error` (with `next_step`) or `_index_cache_error`
(with `hint`), or otherwise an `error` naming the cause.
  - A deleted or locked file returns `error`.
- Library-wide searches drop that file's chunks by owner (vector hits
through `_search_chunks`' indices, keyword hits and the small-index
"whole index" path through `chunk_to_file`). An unchanged file with an
identical chunk keeps it and is cited as the source. The skipped file is
listed in `files_left_out` (including any `error_type` and next step)
and named in the instruction. If every file is left out, the search
returns `error` / `in_progress`.
- **Adversarial tests:**
- An unchanged file is never re-embedded, whether recently written or
aged.
  - An aged unchanged file is never even hashed.
  - A touched but unchanged file is hashed once, then trusted.
  - Deleted, locked and failed re-index files are not served.
  - A failed re-index keeps the embedder or cache-lock advice.
  - A twin chunk in an unchanged file survives its left-out copy.
  - A slow re-index returns `in_progress` within the deadline.
- **Overlap:** besides amd#6937 and amd#6942, amd#6917 (lock-free re-index) and
the amd#6936 autofix cover parts of this. This PR covers all three search
paths in one change.
- **Regression run:** the RAG suites (`tests/unit/rag`,
`tests/test_rag.py`, the RAG tool, resolution, status, citation, cache
and library tests, `test_chat_reindex_locked.py`) pass with a temp HOME
after merging `main`. The earlier broad run also covered
`tests/unit/agents`, `tests/unit/chat/ui` and
`hub/agents/gaia/python/tests`. In that long combined run, some logging
and slot tests failed depending on test order. All 312 pass when their
files run alone. Black, isort and flake8 (repo settings) are clean on
the changed files.
</details>

Co-authored-by: rezamuliawijaya
<[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A file-scoped document query keeps serving an edited file's old text forever

1 participant