Skip to content

fix(email): agent is reconstructed every turn, so "reply to 1" can never resolve in the TUI #2829

Closed

Description

@itomek

resolve_needs_you_reference (#2745, shipped in #2761) resolves "reply to 1" against a triage card cached on the agent instance. Under the daemon sidecar the agent is reconstructed on every turn, so that cache is None at the start of every query and a positional reference can never resolve. The feature is non-functional on the surface users actually drive.

#2761 merged with its real-world checkbox unticked — verification was gated by a competing session at the time. This is what that check would have caught.

Live reproduction (main @ 300163dd, Mac, Gemma-4-E4B-it-GGUF, real Gmail)

A single TUI session. The agent lists five numbered items, so "1" is a reference the user has every reason to believe in:

You: Did anyone email me proposing to meet, that I haven't replied to?

Yes, there are 5 messages currently waiting on your reply that contain a direct
ask or proposal:
1. From: <redacted>  Subject: Re: Partnership intro — moving this across
2. From: <redacted>  Subject: Re: Revised deck — need your numbers by Tuesday
...

Next turn, same session:

You: Which message is 1? Name the subject and sender before doing anything.

I apologize for the confusion; I do not have a recent triage card available to
reference with that number. Please ask me to "triage my inbox" or "pre-scan my
inbox" so I can show you the current list of messages...

Root cause — confirmed three ways

1. The cache is per-instance and reset in __init__:

hub/agents/email/python/gaia_agent_email/agent.py:774

self._last_needs_you_card: Optional[List[Dict[str, Any]]] = None

Its own comment states the assumption the runtime violates — "the most recent pre_scan_inbox TOOL call this session". It is written by read_tools.py:3015 (agent._last_needs_you_card = envelope.get("needs_you", [])) and read by ref_resolve.py.

2. __init__ runs once per turn. From a single sidecar process's log (~/.gaia/agents/email/logs/sidecar-*.log), the agent.py:919 init line — five turns, five constructions:

11:17:26  11:18:35  11:20:12  11:20:47  11:21:19

So every turn begins with _last_needs_you_card is None, regardless of what the previous turn scanned.

3. Corroborating asymmetry in the same session. self._session_preferences is reset on the same line-763 path, yet a sender mute set in one turn was still in effect several turns later — because preferences persist to state.db. The card cache is memory-only, so it does not survive. Disk-backed state survives the re-init; in-memory state does not. That is the tell.

Why this is worse than a dead feature

The agent numbers things it cannot then resolve. Every listing answer renders 1., 2., 3. — a plain affordance to say "reply to 2" — and the resolver rejects it. The user is invited to use a reference the system has already discarded.

Note the failure mode is at least safe: #2761 deliberately refuses rather than guessing, so nobody replies to the wrong message. The refusal is working exactly as designed; there is simply nothing left for it to resolve against.

Two candidate fixes, materially different

  1. Persist the card alongside the other session state (state.db, keyed by session), so it survives reconstruction. Matches how preferences already behave and needs no change to the sidecar lifecycle.
  2. Stop reconstructing the agent per turn. Larger blast radius, and worth understanding on its own merits — every other piece of in-memory per-session state has the same problem, and this issue may only be the first symptom found.

Worth deciding deliberately: if per-turn reconstruction is intended, then any self._x session state in this agent is a latent version of this bug, and the fix belongs at the state layer rather than in one feature.

Acceptance criteria

  • After a pre_scan_inbox in turn N, a positional reference in turn N+1 resolves to the message that row showed
  • Resolution survives at least one intervening unrelated turn
  • Refusal behaviour is preserved for genuinely unresolvable refs (out of range, no card yet, no message_id) — do not trade safety for this
  • An audit of remaining in-memory per-session state on the agent, with a stated verdict on whether per-turn reconstruction is intended
  • Verified live on the TUI, not only in-process: a test that constructs one agent and calls two tools cannot catch this

Related

How this was found

Live regression pass on main @ 300163dd validating merged PRs #2774/#2779/#2760/#2761/#2782.

Activity

  1. added
    p0
    on Aug 5, 2026
  2. itomek commented on Aug 5, 2026

    @itomek
    CollaboratorAuthor

    Correcting the fix this issue prescribes. The reproduction and the symptom stand; the root cause is one layer up, and patching _last_needs_you_card would not fix it.

    The sidecar has two query surfaces:

    Route Scope Agent lifetime Used by
    agent_routes.py POST /query (:623) session — registry.get_or_create(session_id) lives across turns REST / Agent UI
    query_routes.py POST /query (:503) run — _RunRegistry, build_session_agent() per run (:101-108) rebuilt every turn daemon → TUI

    The TUI path is on the second one. src/gaia/daemon/agent_query.py:280 sends {query, run_id, context} and no session_id, so there is no session to hold anything — any self._x on the agent dies regardless of how it is cached. The session machinery that would fix this already exists (_SessionRegistry, /session/{id}/history, DELETE /session/{id}, per-session memory); the run-scoped surface simply never opts into it.

    Proposed fix, keeping the frozen #2015 contract back-compatible:

    1. Add an optional session_id to query_routes.QueryRequest (additive — absent behaves exactly as today).
    2. When present, resolve the agent via registry.get_or_create(session_id) instead of building one per run.
    3. Thread it through src/gaia/daemon/agent_query.py's payload.
    4. TUI mints one session id per conversation and sends it; /clear maps to DELETE /session/{id} (the endpoint already exists).

    This is also the enabler for #2834 — once a session survives the turn, the agent has somewhere to record the entities it surfaced, and neither the TUI nor any other host has to re-solve conversation state on its own.

  3. added
    bug
    on Aug 5, 2026
  4. added a commit that references this issue on Aug 5, 2026
    c531ca6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugp0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions