Skip to content

fix(ui): bound the Agent UI file search and report truncation - #4354

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/ui-file-search-budget
Sep 26, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/ui-file-search-budget

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Searching files in the Agent UI could walk the entire home directory with no limit. On a real machine one search took minutes, and it never returned at all if one directory read blocked, for example on a cloud mount or a macOS privacy prompt. Now a search stops after 20,000 entries or about 10 seconds and returns what it found so far. The file browser says so when a search stopped early.

Test plan

  • python -m pytest tests/integration/test_files_search_budget.py -q: 5 passed. All 5 fail on main.
  • python -m pytest tests/integration/test_files_search_budget.py tests/integration/test_files_router.py tests/unit/chat/ui/test_models.py -q: 111 passed, 1 skipped. On this Mac, test_files_router.py's search tests hung past 10 minutes against the real home dir before this change; each now returns at the ~11 s cap.
  • cd src/gaia/apps/webui && npx vitest run: 400 passed. The new truncation-note test fails without the component change.
  • cd src/gaia/apps/webui && npx tsc --noEmit: clean
  • black, isort, pylint clean on the changed Python files
  • Manual: Agent UI file browser, search a common name on a large home dir, and see the "Search stopped early" note
  • gaia eval agent: the Agent UI MCP server's search_files tool result gains a truncated field
🔍 Technical details

Fixes #4352

A file search in the Agent UI could walk the whole home directory with no limit, tying up the server for minutes, or forever when one directory read blocked (a cloud mount, a macOS privacy prompt).

The walk now stops after 20,000 directory entries or 10 seconds, skips re-walking Documents/Downloads/Desktop/OneDrive during the home pass, and runs on its own daemon thread so a read that never returns can't hold an executor slot. The endpoint stops waiting shortly after the deadline and returns whatever it found. The response gains `truncated`, and the file browser tells the user when a search stopped early.

Fixes amd#4352
@github-actions github-actions Bot added documentation Documentation changes tests Test changes labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Approve with suggestions

File search in the Agent UI could walk an entire home directory without limit, and would never return at all if one directory read blocked. This PR bounds the walk (20,000 entries / ~10s), returns whatever it found, and tells the user when the search stopped early. The fix is well-targeted and the tests are honest — all five new ones fail on main.

One thing worth addressing before merge: every search now starts its own thread with no ceiling on how many can be in flight. On the exact machine this PR is written for — one with a cloud mount that blocks — a stuck search holds its thread long after the request has already answered, so a user mashing Enter, or an agent calling the file-search tool in a loop, can pile threads up indefinitely. The walk deliberately doesn't share the server's thread pool, which is the right call; it just needs a cap on how many walks can run at once (rejecting the extras rather than queueing them, since a queued walk would time out and report "stopped early" with nothing found).

Smaller things: the spec doc that describes this endpoint still says file search uses the old pattern and points future authors at it as the example to copy, so it now teaches the wrong thing. And if the walk crashes after the deadline has already passed, the error is dropped and the caller gets a normal "stopped early" answer instead of a failure.

Real-world evidence

N/A — no evidence-bundle.md was produced for this run, so the automated stage captured nothing.

The verdict therefore rests on static review plus what the PR description itself reports. The description's route-level evidence is real and matched to the change: five new integration tests drive the actual GET /api/files/search endpoint through TestClient against a throwaway home directory, covering the entry cap, the time budget, a blocked directory read, an early match, and a clean full walk — and the description notes the pre-existing search tests went from hanging past 10 minutes to returning at the cap. That is the backend surface exercised.

The user-visible half is not shown. The "Search stopped early" note is a new Agent UI element and the PR's own manual-check box for it is unchecked, with no screenshot on the PR. A Vitest test covers the note's render logic, which is not the same thing. Please add the before→after screenshot from a large home directory when the strix-halo lane is available — nudge, not a blocker, given the route evidence above.

🔍 Technical details

🟡 Important

Unbounded thread-per-request for the walk (src/gaia/ui/routers/files.py:538)

threading.Thread(...).start() on every request replaces the previously bounded default executor. After the SEARCH_TIME_BUDGET_SEC + SEARCH_STALL_GRACE_SEC wait expires the endpoint returns, but the thread survives until its blocking opendir returns — minutes on a stalled cloud mount, which is the scenario this PR exists to fix. Nothing limits how many can accumulate: FileBrowser.handleSearchKeyDown fires one per Enter keypress, and agent_ui_mcp.search_files (src/gaia/mcp/servers/agent_ui_mcp.py:471) is agent-driven.

The comment at files.py:536 is right that a shared executor is wrong here — a stalled walk must not occupy a slot other work needs. But a module-level cap on search threads specifically gives you both: threads get reused in the common case, and a pathological mount can only consume the search budget. A plain ThreadPoolExecutor(max_workers=N) would queue the overflow, and a queued walk would hit wait_for having done nothing and report truncated=True with zero results — misleading. So prefer an explicit in-flight counter that rejects (503/429) or returns truncated=True with an accurate reason once N walks are already running. Worth a maintainer call on which shape you want.

🟢 Minor

Stale pattern guidance in the spec doc (docs/spec/agent-ui-known-issues.md:54)

Two claims in §3 are no longer true after this change: search_files no longer uses run_in_executor, and line 62 tells future authors to fix the other blocking endpoints "using the same pattern as search_files" — which is now a dedicated daemon thread with a stall deadline, not what that section means. Per CLAUDE.md, a functional change updates every doc that describes it.

`search_files` runs its walk on a dedicated daemon thread with an entry cap and
a time budget (see `SEARCH_MAX_ENTRIES` / `SEARCH_TIME_BUDGET_SEC`), because it
also needs to survive a directory read that blocks indefinitely.

A post-deadline walk failure is swallowed (src/gaia/ui/routers/files.py:544)

_do_search can raise something the inner except (PermissionError, OSError) doesn't cover — datetime.datetime.fromtimestamp(st.st_mtime) at files.py:484 raises OverflowError/ValueError on a corrupt mtime, and neither that handler nor the outer except OSError catches it. Before the timeout that propagates through wait_for into a 500, which is correct. After the timeout, wait_for has cancelled the wrapper, _copy_future_state returns early on the cancelled destination, and the concurrent.futures.Future exception is never retrieved — the caller gets a 200 with truncated=true and no hint anything went wrong. A walk.add_done_callback that logs a non-cancelled exception closes it.

searchTruncated isn't reset with the other search state (src/gaia/apps/webui/src/components/FileBrowser.tsx:544)

The "Back to browsing" handler clears searchResults and searchedLocations but not searchTruncated; the empty-query early return at FileBrowser.tsx:239 has the same gap. Invisible today because the note is gated on searchResults, so this is tidiness rather than a bug — but it's the one piece of search state that goes stale if that gate ever changes.

                            <button className="fb-back-btn" onClick={() => { setSearchResults(null); setSearchQuery(''); setSearchedLocations([]); setSearchTruncated(false); }}>

Existing search tests still walk the real $HOME (tests/integration/test_files_router.py:700)

The new file's fake_home fixture is the right pattern — test_files_router.py's search tests now cost ~11s each instead of hanging, but they still scan the developer's actual home directory and write gaia_* files into it. Worth pointing them at the same fixture in a follow-up.

Strengths

  • truncated semantics are precisely right: sticky once a bound fires, and deliberately not set when max_results is reached. test_full_walk_under_the_bounds_is_not_truncated and test_early_match_returns_without_walking_the_rest pin both directions, so the flag can't quietly degrade into "we stopped for some reason".
  • The skip=frozenset(priority_dirs) pass correctly avoids spending the budget twice: the priority dirs are walked at max_depth=4 from themselves, which strictly covers what the max_depth=3 home pass would have reached, so nothing is lost by skipping them.
  • test_stalled_directory_read_returns_partial_results_at_the_deadline tests the failure mode that actually motivated the PR, and passes whether the walk stalls or the budget expires first — no timing race.
  • The truncated field reaches the MCP search_files tool for free, since agent_ui_mcp.py:480 passes the response through.

# Conflicts:
#	src/gaia/ui/routers/files.py
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 26, 2026
Merged via the queue into amd:main with commit c5027ad Sep 26, 2026
43 of 45 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/ui-file-search-budget branch September 26, 2026 06:53
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 26, 2026
amd#4354 added `truncated` to FileSearchResponse after this branch last
regenerated, so the committed artifacts were stale and this PR's own
drift check failed against them.
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 26, 2026
Main moved a long way under this branch. Conflict resolutions:

- test_unit.yml / test_lane_allowlist.yml: main fixed
  tests/mcp/test_mcp_cli_to_agent_workflow.py and test_mcp_sdk_integration.py
  and now runs them in offline-integration, so their allowlist entries are
  dropped and main's run-list wins. This branch's module_skips section and
  its in-lane guard step are kept -- they are the point of the PR.
- util/lint.py, docs/reference/dev.mdx: main's new --tool-descriptions check
  kept alongside this branch's module_skips documentation.
- tests/integration/test_tui_control_e2e.py: main's CLICOLOR_FORCE fix, which
  is what this branch's CI run was failing on.
- tests/integration/test_memory_integration.py: main's schema bump 4 -> 5.

Also closes the two review findings:

- tui/** added to both path-filter blocks of test_unit.yml. The offline
  integration job owns the TUI end-to-end test, but the workflow was not
  triggered by changes under tui/ -- a TUI-only PR went green without ever
  running the test that drives the TUI. That is the same "looks green, never
  ran" shape this PR exists to fix, one level up.
- The two no-lane allowlist groups now reference amd#4390 and amd#4391, so the
  owner decisions they are waiting on are actually tracked.

One extra fix the guard itself demanded: tests/integration/test_files_search_budget.py
(added by amd#4354) is run by no lane, which fails check_test_lane_coverage.py on
main today. This PR makes that check blocking, so the file is wired into
offline-integration beside test_files_router.py, which shares its dependencies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ui): file search walks the whole home directory with no time or entry limit

2 participants