Skip to content

fix(code-index): stop an abandoned index build instead of racing the next one - #4644

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/code-index-build-bounded
Oct 4, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/code-index-build-bounded

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

On a large repository, code search used up most of an agent's time budget while producing no results. The first search_code_index call builds the index, but it ran under the default 180 s tool cap, so the agent abandoned it partway through. Python can't kill the thread, so that build kept running. The model then called index_codebase, which started a second full build on the same embedder; competing with the first, it overran its own 900 s cap. Now a first search gets the same window index_codebase has, and a build the agent has abandoned stops at its next file or batch, before writing anything.

Evidence from run 37110051697 (Qwen3-30B therock suite):

  • tr-8319 and tr-8319-cb (both TIMEOUT at 1800 s): the first two tool calls in each transcript were search_code_index → "did not return within 180s and was abandoned", then index_codebase → "did not return within 900s and was abandoned". That is 18 of the 30 minutes gone before the model read a file. Judge: "No changes and no answer."
🔍 Technical details
  • search_code_index is now @tool(timeout=900), matching index_codebase, because its lazy path calls the same sdk.index_repository().
  • CodeIndexSDK.index_repository calls raise_if_cancelled() before each file is parsed. Both embedding loops (_encode_texts, _encode_texts_with_sync) call it before each batch, outside the per-batch except Exception that would otherwise swallow it. This is the cooperative cancellation from fix(agents): abandoned tool-timeout threads leak log records and state into unrelated code #2600 that audio tools already use. Nothing is persisted before _save_atomic, so the previous index is left intact.
  • What this does not fix: whether a 900 s first search is the right UX on a very large repository. The tasks still need the model to stop exploring and edit; see tr-7998.

Test plan

  • pytest tests/unit/test_code_index_mixin.py tests/unit/test_code_index_sdk.py tests/unit/test_code_index_empty.py tests/unit/test_code_index.py tests/unit/test_flagship_code_index_scope.py. The three new tests fail on main.
  • This changes a tool's dispatch timeout, so re-run the flagship-tasks therock suite and the gaia_code category, and confirm no search_code_index … abandoned lines appear in the transcripts.

…next one

On a large repository the first search_code_index call builds the index under
the default 180 s tool cap, so the agent abandoned it mid-build; the build
kept running, and the model's follow-up index_codebase started a second full
build on the same embedder that then overran its own 900 s cap. Two TheRock
tasks lost 18 of their 30 minutes this way and never got to the fix.

search_code_index now gets index_codebase's window, since it runs the same
build, and the build checks for cancellation between files and between
embedding batches, so an abandoned one stops before it persists anything.
@github-actions github-actions Bot added tests Test changes agents labels Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

On a large repo, the agent's first code search no longer gets cut off partway through building the index. An index build the agent has given up on now stops before it writes anything, so it can't compete with the next build for the embedder. The fix uses the cancellation mechanism the audio tools already use, and the tests cover both stopping points.

Nothing blocks merge. One item from the PR's own test plan is still open: the therock flagship-tasks suite and the gaia_code eval haven't been re-run. Run them before merging to confirm the longer first-search window helps those tasks and doesn't just move the time somewhere else.

Real-world evidence

N/A: no evidence bundle was produced for this run. The change is to tool dispatch timing and index-build internals, and real inference runs on the strix-halo lane. The verdict rests on static review plus the unit tests the author reports. The PR names the failing transcripts (tr-8319, tr-8319-cb in run 37110051697) but doesn't include a post-fix run yet.

🔍 Technical details

Issues found

  • 🟡 Eval re-run still pending. CLAUDE.md requires an eval for changes to tool registration. This changes search_code_index's registered timeout (code_index_tools.py:223), and the PR's test-plan item for the therock + gaia_code re-run is unchecked. Attach the scorecard path, or at least confirm no search_code_index … abandoned lines appear in the transcripts. There's no committed flagship baseline, so report what was measured; don't claim "no regression".
  • 🟢 Only one embedding loop has a test (test_code_index_sdk.py). TestAbandonedBuild exercises _encode_texts_with_sync. The check added to _encode_texts (sdk.py:909) has no test, so a later refactor could drop it unnoticed. A copy of test_an_embedding_pass_stops_between_batches that targets _encode_texts would cover it.

Verified while reviewing

  • In both embedding loops, raise_if_cancelled() runs at the top of the loop. That puts it outside the per-batch except Exception handlers (sdk.py:948, :971), so ToolCancelled isn't swallowed there.
  • Outside a tool worker thread (CLI or direct SDK use), the thread-local event is unset, so the new checks do nothing on those paths.
  • _resolve_tool_timeout uses the per-tool override before the global GAIA_AGENT_TOOL_TIMEOUT, so the 900 s window actually applies.
  • If the build is cancelled, the tool bodies' outer except Exception turns ToolCancelled into an error JSON. The caller has already stopped waiting by then, so this doesn't hide anything.

Strengths

  • Reuses the existing fix(agents): abandoned tool-timeout threads leak log records and state into unrelated code #2600 cooperative-cancellation primitive instead of adding a new lock or a "build already running" flag.
  • The new checks sit before _save_atomic, so a cancelled build leaves the previous index intact, and the first test asserts that directly (save.assert_not_called()).
  • The new tests assert on the outcome: no encode, no save, exactly one batch embedded. They don't just check that the cancellation function was called.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 4, 2026
@kovtcharov

Copy link
Copy Markdown
Contributor Author

The second embedding loop now has its own cancellation test. The eval re-run has to come from CI: gaia_code and the therock flagship-tasks suite need to run on the strix-halo lane before merge, because this changes a registered tool timeout.

🔍 Technical details
  • Added TestAbandonedBuild::test_a_plain_encode_pass_stops_between_batches, which drives _encode_texts with 60 texts and asserts it stops after the first batch of 25.
  • There is no committed flagship baseline, so the eval result will be reported as measured, including any search_code_index … abandoned lines in the transcripts, not as "no regression".

Merged via the queue into amd:main with commit 3f28709 Oct 4, 2026
@kovtcharov-amd
kovtcharov-amd deleted the kalin/code-index-build-bounded branch October 4, 2026 09:42
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Oct 5, 2026
File tools now all check the same allowed-folders rule. Before,
`read_file` and `write_file` checked a path against the session's
allowed folders (asking the user when it was outside), but several
neighbouring tools did not: folder browsing and directory search, recent
files, document and code indexing, folder watching, `list_files`, and
the paths screenshots, transcripts, speech and exports are saved to. Now
every one goes through the same check and the same approval prompt, so a
chat scoped to its documents behaves the same whichever tool the model
picks.

- **One rule, not a second one.** Tools call the existing
`PathValidator.validate_read` / `validate_write` through a thin helper.
The validator now expands `~` before resolving symlinks and `..`, so it
judges the path the tool actually opens.
- **Approving a folder now covers document indexing too.** The chat
agent's RAG SDK kept a separate validator, so a folder approved in the
chat was still refused at indexing time. It now shares the agent's.
- **Wider on purpose, and documented:** name search over the common
document folders (it returns paths only, and opening one is checked),
GAIA's own output folders, and `capture_skill`, which asks the user on
every call.

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

- Helper: `gaia.agents.tools.path_access` (`read_access_error`,
`write_access_error`, `readable_entry`). Folder walks call
`readable_entry` per file, which never prompts.
- Model-visible changes: refusal text from these tools is now the
validator's standard message, `list_recent_files` reports
`locations_not_searched`, and `search_directory` with no root searches
the allowed folders instead of the whole home folder. No tool docstrings
changed.
- Overlap with amd#4642: both PRs add the same `~` expansion to
`browse_directory`, `get_file_info`, `analyze_data_file` and
`search_file_content`, with identical lines, so either merge order
applies cleanly. Merging amd#4642 first is the simplest order.
- Nearby open PRs: amd#4613 and amd#4648 (`rag_tools.py`), amd#4644
(`code_index_tools.py`), amd#4626 (`filesystem_tools.py`), and amd#4647
(flagship CHANGELOG). Their hunks don't overlap these, but whichever
lands second may need a trivial rebase.
</details>

## Test plan
- [x] `pytest tests/unit/test_file_tools_allowed_folders.py
hub/agents/chat/python/tests/test_chat_file_tools_allowed_folders.py`:
66 tests. For each tool, a path outside the allowed folders (direct,
through `..`, and through a symlink) goes through the approval prompt
and is refused, and a path inside still works. 41 of them fail on main.
The ChatAgent tests live in the chat package, so the chat-agent lane
runs them; the core unit lane has no `gaia_agent_chat`.
- [x] Existing file, RAG, audio, screenshot, code-index, security and
chat-agent unit tests pass. The two local failures
(`test_persisted_grants_are_not_unioned_into_a_host_scope`,
`test_backups_directory_is_owner_only`) also fail on main on this
machine.
- [x] black, isort, flake8 and pylint are clean on the changed files.
- [ ] Follow-up after the core release that ships `path_access`: raise
the chat and flagship `amd-gaia` floors to it, as the chat CHANGELOG
records.
- [ ] CI eval re-run, since refusal text and the default search scope
are model-visible: `gaia_files`, `gaia_core`, `gaia_data`,
`tool_selection`.

---------

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

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants