Repository navigation
refactor(email): dedupe envelope helpers, unify token path, guard tools_count - #2035
Conversation
…ath ACs These pin the three acceptance criteria for the upcoming email-agent refactor before the implementation lands, so the refactor has a concrete TDD contract: tools_count stays anti-drift guarded, the envelope helpers collapse to one source, and Calendar shares Gmail's token path.
The _envelope_ok/_envelope_err pair was copy-pasted byte-for-byte across 11 tool modules, with followup_tools importing them from read_tools as a 12th consumer. All 12 now import from tools/envelope.py, so the envelope contract has one home and a drift in one copy can no longer diverge the tool output format. Now-unused json/Any imports dropped per module.
…#1232) Calendar token resolution now goes through get_access_token_sync — the same gaia.connectors.api path Gmail uses — instead of the handler-level get_credential_sync. Besides removing the dict-unwrap asymmetry, this adds the scope-coverage pre-flight Gmail already had: a connection missing calendar scope now fails fast with an actionable CONNECTION_MISSING_SCOPES reconnect message (HTTP 403 at the email API boundary) instead of surfacing as an opaque Google 403 -> 502 after a wasted network call. Same immutable CALENDAR_SCOPES, same connector, no scope widening.
#1232) The live email-tool count is 52 (memory off), matching both pinned tools_count copies, so the values stand — but nothing guarded them, and the npm changelog shows the dual pin drifting stale before. Both copies now carry a pointer to the drift-guard test that derives the count from the live registry and fails on any unbumped tool addition. EmailTriageAgent was also the outlier that never called _snapshot_tools(): its registry stayed a live alias to the shared global dict, so constructing any other agent in the same process would silently swap the email agent's effective tool set. Snapshotting at the end of _register_tools() aligns it with every other agent and makes the guard deterministic by construction.
Consolidates the orchestration artifacts onto the PR branch: the plan (with its adversarial-reflection record) lives next to the change it drove, matching the committed .claude/plans/ convention.
|
Verdict: Approve ✅ This PR retires three copies of the same drift-prone tech debt in the packaged email agent: the JSON envelope helpers (copy-pasted across 12 tool modules → one shared module), Calendar's weaker OAuth token path (now unified onto Gmail's grant-checked The one thing worth a reviewer's eye is the intentional behavior change called out in the description: in the broken state (agent granted Calendar but the stored Google connection lacks calendar scope), Calendar now fails pre-flight with an actionable 403 reconnect message instead of a 502 after a wasted round-trip. Same immutable scope set, no widening — a strict fail-loud improvement aligned with GAIA's "No Silent Fallbacks" rule. 🔍 Technical detailsCorrectness checks performed (all pass):
🟢 Minor / optional (not blocking):
Strengths:
|
…or-cleanup # Conflicts: # hub/agents/python/email/gaia_agent_email/tools/read_tools.py
|
🟡 The planning document Fix before merging: The rest of the changes look clean — envelope dedup, token-path unification, 🔍 Technical details
|
Main gitignored .claude/plans/ and purged committed plans (#2041) after this branch added its plan file; remove it so the merge doesn't reintroduce session scratch to main.
|
The one red check ("Re-run eval, refresh-or-reject scorecard") is a pre-existing main-side failure, not something this PR introduced — this PR changes no eval code. Two independent problems, both filed as #2047:
All other checks are green. Details and evidence in #2047. |
Closes #1232
Three copies of the same tech debt in the packaged email agent could each drift silently: the JSON envelope helpers were copy-pasted across 12 tool modules, Calendar resolved OAuth tokens through a different (weaker) path than Gmail, and the hub-facing
tools_countwas hand-pinned in two files with nothing checking either. All three are now single-sourced and drift-guarded — a future tool added without bumping bothtools_countpins fails CI, and Calendar auth failures now surface as an actionable reconnect message instead of an opaque 502.Deliberate behavior change (Part 3): Calendar's move onto
get_access_token_syncadds the OAuth scope-coverage pre-flight Gmail already had. In the broken state (agent granted calendar, but the stored Google connection missing calendar scope) the API now returns 403 with a reconnect message pre-flight instead of 502 after a wasted Google round-trip. Same immutableCALENDAR_SCOPES, no scope widening; Outlook backends intentionally unchanged.Note: the issue's "actual ~37" premise was stale — the live registered count is 52, matching both existing pins; the fix here is the guard, not the number.
EmailTriageAgentalso now calls_snapshot_tools()like every other agent, fixing a latent bug where a second agent built in the same process could silently swap its effective tool set.Test plan
python -m pytest hub/agents/python/email/tests/ -q→ 317 passed, 0 failed (includes 20 new tests written TDD-first from the acceptance criteria, red before implementation)python util/lint.py --all— all blocking gates passEvidence
Real-world tier is degraded to a local end-to-end smoke by design: this is an internal refactor with no new user-facing feature, and the token-path contract is pinned by mocked tests (
AGENT_NOT_GRANTEDparity + the newCONNECTION_MISSING_SCOPESpropagation).CLI — full suite (provenance-verified worktree venv):
CLI — end-to-end smoke (instantiate agent, guarded count, calendar tool through the shared envelope):
Reviewer call-outs
src/gaia/ui/email_sidecar/proxy_agent.py:92keeps its own envelope copy intentionally — it lives in coregaia.ui, which cannot depend on the hub package (dependency direction).src/gaia/connectors/):OAuthPkceHandler.get_credentiallacks the scope-coverage checkapi.get_access_tokenhas, so Microsoft/Outlook token resolution retains the weaker pre-existing guarantee.AuthRequiredError.missing_scopesprecision differs between the two paths onAGENT_NOT_GRANTED(full requested list vs. computed diff); the rendered user message is unaffected.tests/test_envelope.pyfails on any future re-introduction of a local_envelope_ok/_envelope_errdef intools/.