Skip to content

feat(ui): rip out in-process email agent; consume packaged agent via REST bridge (Path 2) - #1785

Closed
itomek wants to merge 2 commits into
mainfrom
feat/ui-rip-out-inprocess-email-1653
Closed

itomek wants to merge 2 commits into
mainfrom
feat/ui-rip-out-inprocess-email-1653

Conversation

@itomek

@itomek itomek commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Closes #1653. The Path-2 capstone for the email-in-Agent-UI epic #1767.

Why this matters

Before: the Agent UI's email experience was a legacy in-process chat-loop agent embedded in the UI backend. After: the in-process email path is gone entirely — an agent_type:"email" interaction routes through a thin EmailRestBridge to the packaged email agent's triage service (the same gaia_agent_email service the /v1/email REST surface serves, #1768). No in-process EmailTriageAgent, no mail_provider→create_agent dispatch.

Option C (agreed): the UI keeps triage/draft/send; pre-scan/archive/calendar/search were agent-loop-only and are deferred to #1778/#1779/#1780/#1781. mail_provider column, connector grant flow, and the (now dormant) frontend cards are kept.

Evidence

  • Rip-out confirmed: _session_mail_provider → 0 occurrences; _RENDER_TOOL_TO_LANG["pre_scan_inbox"] mapping removed; test_session_mail_provider.py deleted.
  • Re-point: _chat_helpers._email_rest_bridge parses an email from the user's message and calls gaia_agent_email's triage service in-process (no HTTP round-trip), formats the schema-2.0 result as markdown.
  • Tests: python -m pytest tests/unit/chat/ui/test_email_rest_bridge.py tests/unit/chat/ui/test_sse_handler.py -q → 213 passed (16 new bridge tests + rewritten render-injection suite). Frontend npm run build clean; util/lint.py --all clean.
  • macOS real-world: captured below (paste-an-email → triage via the package; pre-scan now returns the "paste an email" guide — the in-process scan is gone).

Test plan

  • python -m pytest tests/unit/chat/ui/test_email_rest_bridge.py tests/unit/chat/ui/test_sse_handler.py -v → green
  • UI: agent_type:"email" → paste an email → triage output; "run a pre-scan" → guide (in-process scan removed)

Tomasz Iniewicz added 2 commits June 19, 2026 16:44
Removes all in-process EmailTriageAgent wiring from the Agent UI and replaces
it with a thin bridge that calls EmailTriageService directly — the same object
the mounted /v1/email/triage route uses — so no live OAuth or inbox scan is
required for a chat turn.

- _session_mail_provider() removed; mail_provider= kwarg dropped from both
  registry.create_agent() call sites (non-streaming + streaming paths).
- pre_scan_inbox removed from SSEOutputHandler._RENDER_TOOL_TO_LANG (map is
  now empty; EmailPreScanCard remains dormant per #1778 follow-up).
- sessions.py evict trigger: mail_provider is not None condition dropped; agent
  eviction still fires on agent_type/device changes.
- _parse_email_from_text + _email_rest_bridge added to _chat_helpers.py;
  both streaming and non-streaming paths intercept agent_type=="email" and
  route through the bridge before the agent cache / registry dispatch.
- Tests: test_session_mail_provider.py deleted; TestStructuredRenderInjection
  rewritten to guard the mechanism (not the removed pre_scan entry);
  test_email_rest_bridge.py added (16 tests covering parse, bridge, error
  surfaces, rip-out canaries).
- Remove dead AGENT_NOT_GRANTED exception branch: EmailTriageService.triage_request
  never raises 403 (no grant check in the triage path). Replaced with fail-loud
  re-raise for unexpected errors (no silent swallowing).
- Replace fabricated 403 mock test with test_unexpected_service_error_propagates
  which verifies fail-loud behavior for real unexpected errors.
- Use result.category.value instead of result.category to render the bare enum
  string without the Python repr wrapper.
- Move deferred imports (LLMTriageError, EmailSummarizeError) outside the try
  block so ImportError is a startup crash, not a silent mishandled exception.
@itomek
itomek requested a review from kovtcharov-amd as a code owner June 19, 2026 20:47
@github-actions github-actions Bot added the tests Test changes label Jun 19, 2026
@itomek
itomek marked this pull request as draft June 19, 2026 20:47
@github-actions

Copy link
Copy Markdown
Contributor

Review: rip out in-process email agent → REST bridge (#1653)

Summary

Clean, well-tested rip-out: the in-process EmailTriageAgent dispatch and the mail_provider→create_agent plumbing are gone, replaced by a thin _email_rest_bridge that constructs the frozen EmailTriageRequest and calls the packaged EmailTriageService in-process. The canary tests (_session_mail_provider deleted, mail_provider= no longer forwarded, pre_scan_inbox dropped from _RENDER_TOOL_TO_LANG) are a good way to lock the removal in, and the bridge tests assert the shape of the outgoing request — not just that the service was called — which is exactly the boundary-validity check this repo asks for.

The one thing to fix before merge: the non-streaming email path runs a blocking LLM triage call directly on the asyncio event loop, while every other slow path here defers to a worker thread. Separately, docs/guides/email.mdx still advertises the pre-scan/organize/calendar experience this PR removes.

Issues

🟡 Non-streaming bridge blocks the event loop (src/gaia/ui/_chat_helpers.py:1369)
_get_chat_response is async, and the email intercept returns _email_rest_bridge(...) synchronously. That call runs _service.triage_request — local LLM inference, multiple seconds — directly on the event loop, stalling every other concurrent request/SSE stream for the duration. Note the contrast: the mounted route uses await asyncio.to_thread(_service.triage_request, ...) (api_routes.py:890), and the streaming path here already runs the bridge inside _run_agent on a background thread. Only this path blocks.

            return await asyncio.to_thread(_email_rest_bridge, request.message)

🟡 Stale user-facing docs (docs/guides/email.mdx)
The guide's "Daily-driver pre-scan (Agent UI)" section (line 110+) tells users the Agent UI email experience is built around clicking "Run a pre-scan" — the exact capability this PR removes (it now returns the "paste an email" guide). The Organize/Calendar bullets (lines 13, 16) are likewise agent-loop-only features you've deferred to #1778–#1781 and are no longer reachable through the email chat path. Per CLAUDE.md, feature changes must keep docs in sync; please trim/flag these so the doc matches what ships. (Worth a one-line "pre-scan/organize/calendar temporarily unavailable in Agent UI" note rather than silently leaving the old instructions.)

🟡 Broad except can mask contract drift (src/gaia/ui/_chat_helpers.py:133)
_parse_email_from_text wraps the EmailTriageRequest construction in except Exception: ... return None, which collapses into the same "no email content" guide the parser returns for genuinely-empty input. If the frozen #1262 contract ever shifts shape, every pasted email would silently degrade to the guide message with only a debug log — the masked-regression failure mode CLAUDE.md calls out. Consider letting a pydantic ValidationError surface (or log at warning and return an actionable error) so a real construction failure is distinguishable from "user typed nothing email-like."

🟢 Docstring overstates grant handling (src/gaia/ui/_chat_helpers.py:1215)
The docstring says auth/grant errors "propagate the AGENT_NOT_GRANTED prefix so the frontend surfaces EmailConnectCta," but single-email paste triage reads no mailbox and the bridge only catches LLMTriageError/EmailSummarizeError — nothing in this path produces or forwards that sentinel. Either trim the claim or, if a CTA is desired, catch AuthRequiredError explicitly.

🟢 Permissive keyword set (src/gaia/ui/_chat_helpers.py:35)
"hi ", "hello ", "can you", "please " will classify casual messages ("Hi, can you help?") as an email to triage. It's the email agent_type, so the blast radius is small, but the triage-vs-guide boundary is fuzzy — fine to leave, just noting it.

🟢 Private-symbol coupling — importing _service from gaia_agent_email.api_routes reaches into a private module singleton. Pragmatic (avoids the HTTP hop), but a small coupling smell; an exported accessor would be more stable if the package refactors.

Strengths

  • Canary tests pin the removal at the source level — _session_mail_provider gone, mail_provider= no longer forwarded, pre_scan_inbox dropped — so a partial revert can't sneak back in.
  • Bridge tests assert the constructed EmailTriageRequest shape (payload.kind, from_.email), not just that the service was invoked, and test_unexpected_service_error_propagates enforces fail-loud on non-classified errors.
  • _RENDER_TOOL_TO_LANG emptied rather than deleted, with the injection suite kept as forward-looking infrastructure — good call for when a REST render tool returns.

Verdict: Request changes — the event-loop block and the stale email guide are the two items to resolve; the rest are minor. Nice, disciplined rip-out otherwise.

@itomek itomek closed this Jun 26, 2026
kovtcharov added a commit that referenced this pull request Jun 26, 2026
…tion)

itomek closed #1767/#1785 because mixing an npm package requiring Node.js into the
Python-backed Agent UI is too dirty. Remove that entirely: the Python backend fetches
the binary itself (verified SHA-256 download, no npx), spawns it, and proxies over
HTTP. No Node runtime, no npm package in the UI's path. The npm package remains the
external Node-integrator channel only.
pull Bot pushed a commit to bhardwajRahul/gaia that referenced this pull request Jun 29, 2026
…modes (amd#1882)

## Why this matters

Improving the email agent and getting the fix into the Agent UI today
means a freeze → \`npm publish\` → version-bump → re-integrate loop —
slow, and a wall of patches just to test a change. This plan removes
that loop and lets the UI triage **personal Gmail** through two modes
over one shared REST contract:

- **User mode** runs the exact **frozen sidecar published to the Agent
Hub** (pinned via \`binaries.lock.json\`), lazily downloaded on first
email use — so the Agent UI **validates the shipped product**, keeps the
core install **lightweight** (no email deps), and gets crash isolation.
- **Dev mode** runs the same contract from local Python source via
\`uvicorn --reload\` — edit a \`.py\` and the next chat action hits new
code, no freeze/publish/patch.

Both read the same \`~/.gaia\` Google grant, so personal Gmail works
identically in each; the mode only swaps which process answers the
calls.

This PR is **docs-only** — the implementation plan, for review before
any code lands. It went through an independent architecture review and
was corrected twice: the original draft wrongly assumed the renderer
calls a REST sidecar (it's actually chat-loop + SSE cards, in-process
today), and it has to be **sequenced behind the in-flight REST
capability build-out** (inbox pre-scan + amd#1779/amd#1780/amd#1781) since the
sidecar contract is triage/draft/send-only right now.

## Decision needed before build
This **pivots from epic amd#1767's in-process-mount decision** (whose
capstone PR amd#1785 was closed unmerged 2026-06-26). Needs maintainer +
@itomek sign-off to supersede it, plus confirmation to remove the
redundant in-process \`/v1/email\` mount (amd#1768).

## Test plan
- [ ] Review \`docs/plans/email-sidecar-agent-ui.md\` — architecture,
the user↔dev seam, fail-loud handling
- [ ] Confirm the sidecar-over-in-process rationale (product validation
+ lightweight core + isolation) is the direction we want
- [ ] Confirm the precondition/sequencing on the REST build-out
(amd#1779/amd#1780/amd#1781 + pre-scan) is correct
- [ ] Confirm the amd#1767 supersede and amd#1768 mount-removal calls with
@itomek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[packaging] Agent UI consumes @amd-gaia/agent-email sidecar (dogfood validation)

1 participant