Skip to content

fix(email): a follow-up can name a thread shown last turn - #5811

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/email-thread-followup-2834
Oct 26, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/email-thread-followup-2834

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

In the TUI, finding an email thread and then asking "who said what in it" failed every time. The host pushes back only the conversation's words, so the thread id never reached the second turn: the model made one up and Gmail answered 400. Now the email agent remembers the threads it showed and hands their ids back with the history, so the follow-up reads the right thread. A made-up id gets a clear "call search_messages first" error instead of an opaque API failure.

This covers hosts that keep a session (session_id, which the TUI sends). The Agent UI's email relay doesn't send one yet, so it still has the bug. That's a separate change, so this is Part of #2834, not a close.

Test plan

  • pytest hub/agents/email/python/tests/test_thread_id_across_turns_2834.py: 24 passed. It covers refusing a placeholder id before the API call, remembering threads from both tools, mailbox routing on the next turn, the cap and ordering, untrusted fencing of subjects, and attaching the ids to a real EmailTriageAgent's next request, including history trimmed down to an assistant turn.
  • Full email suites (hub/agents/email/python/tests, tests/unit/agents/email): 2964 passed. Five fail identically on clean main on this machine and are unrelated (test_no_in_process_cli cp1252 decode, test_npu_model_wiring env URL, test_sdist_packaging_2934, two time-window phishing tests).
  • python util/lint.py --all passes.
  • Live TUI against a real mailbox: run the issue's two-turn Northgate repro and confirm the second get_thread is ok=true.
  • Next RC eval: watch the email/mail categories. The request sent to the model now carries a thread-reference block on turns after a thread was shown.
🔍 Technical details
  • The thread memory lives on ReadToolsMixin (lazy per-instance map, capped at 10). EmailTriageAgent._history_for_request appends it to the last string user turn. If trimming left none, it leads with a user turn so the message order stays valid. Otherwise it logs a warning rather than dropping the ids silently.
  • Subjects are folded to one line, capped at 120 chars, and wrapped in the existing untrusted-body delimiters.
  • The id shape check is [A-Za-z0-9+/=_-]{1,512} (fullmatch). That fits Gmail hex ids and Graph conversationId base64.
  • The TUI displayedCard change and a summarize_thread render card were left out, per the issue's re-aimed scope: the fix belongs in the agent so every session-carrying host gets it.

Find a thread, then ask who said what in it, and the second question failed:
the host pushes back only the conversation's words, so the thread id never
reached the next turn and the model invented one that Gmail 400'd.

The session-scoped email agent now remembers the last 10 threads get_thread
and summarize_thread showed (and their mailbox) and attaches them, fenced as
untrusted, to the last user turn of the next request. Both tools refuse an id
that cannot be a thread id before calling the mail API.

Covers hosts that send session_id (the TUI); the Agent UI relay does not yet.

Part of amd#2834
@github-actions github-actions Bot added tests Test changes agent::email Email agent changes labels Oct 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

Asking a follow-up about an email thread from the previous turn no longer fails in the TUI. The email agent now remembers the last 10 threads it showed and gives their ids back to the model on the next turn. A made-up id now gets a clear "call search_messages first" error before it reaches Gmail. The fix is in the right layer. It only affects sessions that send a session id, the copied history is changed without touching what the host sent, and subjects are flattened to one line and marked as untrusted. I found no correctness bugs.

  • Attach proof that the original bug is fixed before merge. The tests use fakes, and the live two-turn Northgate check in the test plan is still unchecked. Add the real /v1/email/query request/response pair (or TUI output) showing the second turn's get_thread returning ok=true.

Real-world evidence

N/A: no evidence bundle was produced for this run. This verdict rests on static review and the author's reported test results. The live follow-up path (/v1/email/query with a session_id against a real mailbox) has not been run here.

🔍 Technical details

Issues

🟡 No live evidence for the user-visible fix. This changes what /v1/email/query sends the model, and that is visible to users. The PR's own live-TUI checkbox is unchecked, and no request/response evidence is linked. Unit tests cover the logic, not the surface. A captured two-turn /v1/email/query exchange with the same session_id would close this. The test plan already names the email categories to watch at the next RC eval, which covers the prompt-content change.

🟢 The regex comment cites the issue number (read_tools.py:376). Per CLAUDE.md's comment rule, (#2834) belongs in the commit, not inline. The same applies to the _surfaced_thread_map and note_surfaced_thread docstrings.

# Gmail thread ids are hex; Outlook conversation ids are base64. Neither has
# spaces or brackets, which is what a model's placeholder id looks like.

Checked and fine

  • The base _history_for_request returns a fresh list, so assigning history[index] = {...} doesn't change the pushed conversation_history. The test test_the_pushed_history_itself_is_not_rewritten pins this.
  • The base loop calls _history_for_request only when history is non-empty, so the "no user turn" warning won't fire on the first turn of a new conversation.
  • Agents without a session_id are built fresh for each turn (build_query_agent), so remembered threads can't leak between callers. Sessions are isolated by the registry.
  • summarize_thread_impl always returns subject, and get_thread records a thread only when it has messages and the call succeeded. The tests cover both.

Strengths

  • The safety measures are well chosen and tested. Subjects are flattened to one line, capped at 120 chars, and wrapped in the existing untrusted delimiters. The thread list is attached to a user turn, not a second consecutive user message, so chat templates that reject two user messages in a row still work.
  • It fails loudly in the right places. A bad id gets an actionable error before any API call, and when there is nowhere to put the thread list it logs a warning instead of dropping the ids silently.
  • The cross-turn tests use a real EmailTriageAgent, including history trimmed down to an assistant turn and remembering each thread's mailbox when two are connected.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 26, 2026
Merged via the queue into amd:main with commit cf32d2f Oct 26, 2026
23 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/email-thread-followup-2834 branch October 26, 2026 10:11
@kovtcharov-amd kovtcharov-amd mentioned this pull request Oct 26, 2026
5 of 9 tasks
grtninja pushed a commit to grtninja/gaia that referenced this pull request Oct 26, 2026
## Why this matters

v0.25.2 is a bug-fix release on top of v0.25.1: setup and install no
longer stop at the first failed step, shell and file access handle
Windows paths and text encodings, the Agent UI and desktop app stop
showing misleading states, and document search, email and skills each
lose a handful of rough edges. 55 commits since v0.25.1; notes are in
`docs/releases/v0.25.2.mdx` and credit 15 outside contributors.

## Test plan

- [x] `python util/validate_release_notes.py docs/releases/v0.25.2.mdx
--tag v0.25.2` passes
- [x] `mintlify validate` (from `docs/`) passes
- [x] `python util/check_doc_links.py --internal-only`, `python
util/check_doc_versions.py` and `node
installer/version/bump-ui-version.mjs --check` pass
- [x] `python util/check_component_core_api.py --release-version 0.25.2`
passes
- [x] `pytest tests/unit/test_docs_json_release.py
tests/unit/test_check_component_core_api.py
tests/unit/test_release_tag.py
tests/unit/test_hub_core_version_gate.py`: 156 passed
- [ ] CI green on the merge commit
- [ ] `release_train_dryrun.yml` passes on that commit before the RC tag

## Evals to re-run on the RC tag

LLM-affecting PRs merged without per-PR evals; run these on
`v0.25.2-rc1`:

- `gaia_shell` and shell/safety (amd#5794, amd#5751, amd#5768)
- `gaia_honesty` (amd#5753)
- `gaia_skills_tasks` and `gaia_skills_lifecycle` (amd#5422, amd#5755)
- `rag_retrieval` and `rag_quality` (amd#5742, amd#5754, amd#5761, amd#5817, amd#5812)
- email scenarios (amd#5767, amd#5811)
- `tool_selection` and `error_recovery` (amd#5732)
- Claude provider thinking replay (amd#5072)
- memory categories (amd#5818 merged; amd#5756 has not)

## Release checklist

- [ ] Freeze main, wait for green CI on the exact commit, pass
`release_train_dryrun.yml`, then push `v0.25.2-rc1`
- [ ] Extend the changelog if main gains commits before the tag
kovtcharov pushed a commit to kovtcharov/gaia that referenced this pull request Nov 2, 2026
In the Agent UI, a follow-up about an email thread now reads the thread
the previous turn found. Before, the email relay never sent the chat's
session id, so each turn ran on a brand-new email agent. The thread ids
that amd#5811 taught the agent to remember never reached the second turn,
so the model invented an id and Gmail answered 400. The TUI already
sends the session id. This closes the Agent UI half that amd#5811 left
open.

- **Older email binaries are safe.** The relay sends `session_id` only
to an email sidecar at contract 2.12 or later. Its request model rejects
unknown fields, so an older binary would otherwise fail every turn with
a 422.
- **Switching models mid-chat still works.** With turns now sharing one
session agent, a later turn that picked another model would have
silently kept the first one. The email sidecar now rebuilds the
session's agent on the requested model, and a failed rebuild is a 502
that leaves the session usable.

Fixes amd#2834

## Test plan
- [x] `python -m pytest tests/unit/chat/ui`: 2885 passed, 6 skipped.
- [x] New relay and dispatch tests: email sends `session_id`; it is
withheld from a 2.11 sidecar and sent at exactly 2.12; it is omitted
when the chat has none or it is empty; a profile without the floor never
sends it; malformed or missing versions make no claim. 11 of these fail
on `main`.
- [x] Email sidecar: `pytest
hub/agents/email/python/tests/test_query_route.py
test_thread_id_across_turns_2834.py test_email_agent_routes.py`: 119
passed. New tests: the same model keeps the session agent; a new model
rebuilds on it and closes the old agent; an omitted model keeps the
current agent; a failed rebuild returns 502, keeps the old agent and
releases the session lock.
- [x] black, isort, flake8, and the CI path-coverage check.
- [ ] Live Agent UI against a real mailbox: run the issue's two turns
("Show me the thread about …", then "who said what"). The second
`get_thread` should be `ok=true` in the sidecar log.
- [ ] Before merge, dispatch `eval_flagship.yml` for the email/mail
categories. The email agent's turn-2 request now carries the
thread-reference block in the Agent UI path as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent::email Email agent changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants