Skip to content

fix(rag): summarize each section on its own, and stop when abandoned - #4648

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:kalin/summarize-sections-standalone
Oct 4, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:kalin/summarize-sections-standalone

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Summarizing a long document failed partway through, and the failed job kept the model busy after the user's request had already timed out. summarize_document sent each section through the RAG chat SDK, which keeps conversation history, so section N carried every earlier section with it. Each section, the single-pass prompt and the final synthesis are now sent without history, and an abandoned summary stops at the next section.

Evidence from run 37110051697 (tools lane, safety_handbook_water, TIMEOUT at 916 s):

  • Document has 854658 words, 1666 pages, grouped into 44 sections. Sections 1–2 summarized, then Failed to summarize segment 3: … request (79102 tokens) exceeds the available context size (65536 tokens), and the same for segments 4–44 (42 failures). Each section is about 20k words, so the 79k-token request is the history.
  • Streaming response timed out after 600s came first, followed by Producer thread still running after stream ended. The abandoned summary kept sending ~75k-token requests to the shared model while the next scenario ran.
🔍 Technical details
  • AgentSDK.send(message, no_history=True) already exists. It sends only this prompt and leaves the stored history alone. All three self.rag.chat.send calls in summarize_document now pass it. The history was also leaking whole document sections into rag.chat for any later caller.
  • The section loop calls raise_if_cancelled() before each section, outside the per-section except Exception. This is the fix(agents): abandoned tool-timeout threads leak log records and state into unrelated code #2600 mechanism.
  • Not fixed here: the model picked summarize_document to answer a single question about a 1,666-page handbook. That is a tool-selection problem (model limit) and is out of scope.
  • skill_summarize_styles (same lane) produced generic summaries with none of the document's figures. Leaked history in rag.chat may have contributed; worth checking on the re-run.

Test plan

  • pytest tests/unit/test_summarize_document_sections.py tests/unit/test_agent_tool_timeout.py tests/unit/agents/test_parse_error_recovery.py. The three new tests fail on main.
  • Re-score rag_quality, tool_selection and gaia_rag, and confirm no exceeds the available context size from summarize_document in the backend logs.

summarize_document sent every section through the RAG chat SDK, which keeps
conversation history, so section N carried sections 1..N-1 with it. On a
1,666-page PDF the third request was 79,102 tokens against a 65,536-token
context and all 42 later sections failed; the abandoned tool then kept the
model busy for minutes after the eval's stream had timed out.

Section, single-pass and synthesis prompts are now sent with no_history, and
the section loop checks for cancellation so an abandoned summary stops.
@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

Long-document summaries no longer fail partway through. Each section, and the final synthesis, is now sent to the model without the earlier conversation. Before, every request carried all the previous sections and blew past the context window from section 3 onward. A summary the agent has already given up on now stops at the next section instead of keeping the shared model busy. The fix is small, matches the root cause in the CI logs, and comes with tests that fail on main.

Nothing blocks merge. One small cleanup is optional: a cancelled summary currently shows up as a generic "summary failed" error, not as a cancellation.

Real-world evidence

N/A: the evidence stage produced no bundle for this PR. The change is inside a single agent tool and has no CLI, REST or UI surface of its own. The PR description quotes the failing CI run (79,102-token request against a 65,536-token context, then 42 failed sections and a 600 s timeout). The re-score of rag_quality / tool_selection / gaia_rag is still unchecked in the test plan. Until that runs, the verdict rests on static review and the new unit tests.

🔍 Technical details

Issues found

🟢 Cancellation is reported as a generic tool error (src/gaia/agents/tools/rag_tools.py:1730)
raise_if_cancelled() at line 1628 is outside the per-section except, but the outer except Exception still catches ToolCancelled. It logs Error in summarize_document: tool call was cancelled… and returns the "Failed to generate summary" hint. The loop does stop, which is the point, and the abandoned worker's log records are filtered anyway. Still, letting ToolCancelled propagate keeps the semantics honest and matches how audio_tools.py treats it:

            except ToolCancelled:
                raise
            except Exception as e:

(This also needs ToolCancelled added to the gaia.tool_cancellation import.) If you take it, the third test should switch to pytest.raises(ToolCancelled).

Strengths

  • Reuses the existing AgentSDK.send(..., no_history=True) path and doesn't invent a new one. It also stops whole document sections leaking into rag.chat history for later callers.
  • The tests assert the shape of each outgoing call (no_history is True on every send, including the single-pass path), not just that send was invoked. The cancellation test also proves the loop stops after exactly one section.
  • The cancel check is placed per section, which is the right granularity for the fix(agents): abandoned tool-timeout threads leak log records and state into unrelated code #2600 cooperative-cancellation mechanism.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 4, 2026
Merged via the queue into amd:main with commit 638a89c Oct 4, 2026
@kovtcharov-amd
kovtcharov-amd deleted the kalin/summarize-sections-standalone 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