Skip to content

fix(email): reject --base-url instead of checking one server and using another - #4334

Merged
kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:claude/email-reject-base-url
Sep 26, 2026
Merged

kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:claude/email-reject-base-url

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

gaia email --base-url <url> used to health-check the given Lemonade server and then triage on a different one: whatever the daemon's email sidecar was already using. So users pointing email at another server got a passing check and requests to the wrong place. The flag now fails with exit 2 and says what works: restart the daemon with LEMONADE_BASE_URL set. The email agent already honours that variable.

Rejecting the flag was one of the two options in the issue. Wiring it through would mean adding a per-request server field to the frozen sidecar query contract, and letting any daemon client repoint the email agent's inference. That's a design decision, so this PR doesn't make it.

Overlaps:

Test plan

  • python -m pytest tests/unit/test_email_cli.py -q: the new test fails on main (1 failed, 3 passed) and passes on this branch (4 passed)
  • python -m pytest tests/unit/cli tests/unit/test_email_cli.py tests/unit/test_email_autonomy_cli.py tests/unit/test_amd_gaia_urls.py tests/unit/test_email_sidecar_relay.py -q: 348 passed, 7 skipped
  • gaia email --base-url http://x:1/api/v1 -q hi exits 2 with the guidance, and no daemon or Lemonade call is made
  • black, isort, pylint (repo config) on src/gaia/cli.py; python util/check_doc_links.py --internal-only

Fixes #4312

…g another

`gaia email --base-url <url>` health-checked the given server, then ran the
query on whatever Lemonade the daemon's email sidecar was already using. The
query contract has no server field, so the flag can't reach the agent; it now
exits 2 and tells the user to restart the daemon with LEMONADE_BASE_URL set,
which the email agent already honours.

Fixes amd#4312
@github-actions github-actions Bot added documentation Documentation changes cli CLI changes tests Test changes labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Approve with suggestions

This makes gaia email --base-url ... fail with a clear message instead of health-checking one Lemonade server and then sending the actual work to a different one. Good call on rejecting rather than quietly wiring the flag through the frozen sidecar contract — and the error tells the user exactly what does work.

One gap worth closing before merge: the guard only catches the flag when it comes after email. Put it before the subcommand (gaia --base-url ... email -q hi) and argparse throws the value away, so the user still gets no error and email still runs on the daemon's server — the same silent surprise this PR is fixing, one keystroke away. Worth catching both placements, or saying in the docs that only the post-subcommand form is rejected.

Smaller thing: gaia email --help still advertises --base-url as if it worked, so the only way to learn it's rejected is to try it.

Real-world evidence

No evidence-bundle.md in this checkout, so the verdict rests on static review plus the PR's own test plan. The description states the real CLI run (gaia email --base-url ... -q hi) exits 2 with the guidance and touches neither the daemon nor Lemonade, and the new unit test asserts exactly that — but the actual terminal output isn't pasted. Pasting those few lines would make the message wording reviewable at a glance; not a blocker given the test covers the same assertions.

🔍 Technical details

Issues found

🟡 The guard misses --base-url placed before the subcommand (src/gaia/cli.py:4840)

--base-url is defined twice: on the top-level parser (cli.py:1076) and on parent_parser (cli.py:1120), which email_parser inherits (cli.py:1344). _SubParsersAction parses the subcommand into a fresh namespace and copies every key back over the parent's, so the subparser's default=None overwrites a value captured before the subcommand. Verified against the same parser shape:

parse_args(["--base-url","http://x","email","-q","hi"]) -> base_url=None
parse_args(["email","--base-url","http://x","-q","hi"]) -> base_url='http://x'

So gaia --base-url http://remote:13305/api/v1 email -q hi silently proceeds on the daemon's server — exit 0, wrong server, no warning. One option, keeping the message's URL interpolation intact:

    # The query contract has no server field, so the sidecar would ignore it.
    # argparse drops a pre-subcommand `gaia --base-url ... email`, so scan argv too.
    base_url = getattr(args, "base_url", None)
    if not base_url:
        for i, arg in enumerate(sys.argv):
            if arg == "--base-url" and i + 1 < len(sys.argv):
                base_url = sys.argv[i + 1]
            elif arg.startswith("--base-url="):
                base_url = arg.split("=", 1)[1]
    if base_url:

If you'd rather not scan argv, capturing the top-level value in main() before dispatch works too — that would fix the same silent-ignore for every subcommand, not just email. Either way the new test deserves a second case with the flag ahead of email.

🟢 gaia email -h still lists the flag (src/gaia/cli.py:1120) — inherited from parent_parser with help text promising it sets the Lemonade URL. No clean one-line fix (overriding a parent's argument needs conflict_handler="resolve"), but a conflict_handler override on email_parser with help=argparse.SUPPRESS would stop advertising a flag that always exits 2.

🟢 --spec now requires no --base-url either (src/gaia/cli.py:4840) — the guard runs ahead of the --spec short-circuit, so gaia email --spec --base-url ... exits 2 even though that path touches no server at all. Defensible (the flag is meaningless there), just worth being a deliberate choice rather than an ordering accident.

Verified as correct

  • The recommended workaround actually works: run_query → client.ensure_agent → start_or_attach starts the daemon from the current shell's environment, and SidecarManager spawns with {**os.environ, ...} (daemon/sidecars/manager.py:448), so LEMONADE_BASE_URL reaches the sidecar, which honours it (gaia_agent_email/config.py:55). "Stop the daemon, export, re-run" is accurate guidance, not a hopeful one.
  • No env-based argparse default on --base-url, so setting LEMONADE_BASE_URL doesn't accidentally trip the new rejection.
  • Doc surface is covered: no other page claims gaia email --base-url works, and the amd-gaia.ai/docs/guides/email link in the error keeps the /docs/ prefix that tests/unit/test_amd_gaia_urls.py enforces.

Strengths

  • Fails loudly with all three things an actionable error needs — what failed, what to do instead, where to read more — instead of adding a fallback.
  • The test asserts the absence of both downstream calls (run_query, initialize_lemonade_for_agent), which is the real contract here: reject before either server is touched.
  • The PR description names the road not taken (per-request server field on the frozen sidecar contract) and why it's a design decision rather than quietly picking one.

The flag is declared on both the top-level parser and parent_parser. argparse
parses a subcommand into a fresh namespace and copies every key back over the
parent's, so parent_parser's default=None erased a value given before the
subcommand: 'gaia --base-url X email -q hi' parsed to base_url=None.

For 'gaia email' that meant the new amd#4312 rejection never fired in that order --
the query ran on the daemon's server with no warning, the exact silent surprise
the guard exists to prevent. SUPPRESS keeps the key out of the subparser's
namespace so the top-level value survives, fixing the same silent-ignore for
every command that inherits parent_parser.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Reproduced the gap and fixed it at the cause rather than in the email handler.

gaia --base-url X email -q hi really did slip past the guard and run on the daemon's server. Rather than scan sys.argv, I fixed why argparse was dropping the value — which closes the same silent-ignore for every command that inherits the shared parent parser, not just email.

The existing rejection test is now parametrized over all three positions (after the subcommand, before it, and --base-url=). All three pass; the two new ones fail without the fix, and they fail by calling run_query — i.e. the test reproduces the exact silent wrong-server run, not just a missing error message.

tests/unit/test_email_cli.py + autonomy + sidecar-relay: 72 passed, 7 skipped. Merged origin/main (clean). black/isort clean.

🔍 Technical details

Cause: --base-url is declared twice — on the top-level parser (cli.py:1076) and on parent_parser (cli.py:1121). _SubParsersAction.__call__ parses the subcommand into a fresh namespace and then copies every key back over the parent's, so parent_parser's default=None overwrote a value captured before the subcommand.

Fix: default=argparse.SUPPRESS on the parent_parser declaration. The key is then absent from the sub-namespace, so nothing is copied and the top-level value survives. The top-level parser still declares --base-url with default=None, so args.base_url is always present — the five direct args.base_url accesses in cli.py are unaffected.

Verified against the real parser across both orders, including the nested case (gaia --base-url X mcp start and gaia mcp start --base-url X both resolve), and that omitting the flag still yields None.

tests/unit/cli reports 11 failures on this branch, all test_gaia_binary_on_path — identical set on the branch with my changes stashed, so they're a pre-existing artifact of this checkout not having the console scripts installed.

Not addressed (🟢): gaia email -h still advertising the flag, and --spec being caught by the guard — both read as deliberate in the review.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Sep 26, 2026
# Conflicts:
#	docs/reference/cli.mdx
#	src/gaia/cli.py
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 26, 2026
Merged via the queue into amd:main with commit aa2d7fd Sep 26, 2026
35 of 37 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/email-reject-base-url branch September 26, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(email): gaia email --base-url only affects the startup health check

2 participants