Agent experience: env-var auth fallback, structured JSON errors, --agent rework#71
Agent experience: env-var auth fallback, structured JSON errors, --agent rework#71dylanjha wants to merge 9 commits into
Conversation
Test-first coverage for changes driven by AXP experiment trace analysis: - preprocessArgs no longer injects --json; new wantsJson() helper lets commands consult agent mode directly - getAuthContext/createAuthenticatedMuxClient fall back to MUX_TOKEN_ID and MUX_TOKEN_SECRET env vars (env vars take precedence over config, consistent with MUX_BASE_URL) - mux login reads credentials from env vars and supports --json - logout, env list, and env switch support --json with structured error payloads - buildErrorPayload adds error_code and hint fields to JSON errors - pollForRobotsJob emits JSON status lines to stderr in agent mode Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- preprocessArgs no longer injects --json; it strips --agent and sets agent mode, and commands consult wantsJson() to pick output format. This fixes 'Unknown option --json' failures on commands without a --json flag (login was the most common casualty in AXP traces). - getAuthContext/createAuthenticatedMuxClient fall back to MUX_TOKEN_ID and MUX_TOKEN_SECRET env vars, so agents with credentials in their environment need no login step. Env vars take precedence over stored config, consistent with MUX_BASE_URL. - mux login reads credentials from env vars (precedence: --env-file > env vars > interactive) and supports --json output. - buildErrorPayload gives JSON errors a stable error_code and a recovery hint; used by handleCommandError and the top-level catch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- logout, env list, and env switch gain --json with structured
success and error payloads (error_code: environment_not_found)
- pollForRobotsJob takes {json, pollIntervalMs} and, in JSON mode,
emits one {job_id, status} line per poll to stderr so agents can
observe --wait progress while stdout remains a single JSON result
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Mechanical sweep across all commands so agent mode produces JSON without relying on argv injection. No behavior change when --json is passed explicitly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Slim JSON error payloads: drop error_code/hint fields; error messages carry their own recovery guidance and API responses pass through unwrapped - Revert --json output on logout, env list, and env switch - Revert per-poll progress emission in pollForRobotsJob; JSON mode is silent until done so stdout stays a single pipeable result (the injectable poll interval is kept for tests) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Restores the error behavior from main, which honored --json when formatting command errors. Uses wantsJson() in place of the old options.json read so --agent callers get the same JSON errors they did when --agent injected --json. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Interactive prompts are not possible in machine-readable mode; without this, mux login --json or --agent with no env vars or --env-file would block on stdin waiting for input that never comes. Fail immediately with recovery guidance instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a1c7d16. Configure here.
|
|
||
| return args; | ||
| } | ||
|
|
There was a problem hiding this comment.
Login failures not JSON formatted
Medium Severity
When mux login fails in agent mode or with --json, errors are still printed as plain Error: … text from the top-level CLI handler. Other commands route failures through handleCommandError and emit JSON when wantsJson() is true.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit a1c7d16. Configure here.
| // Show files and confirm (unless -y flag) | ||
| if (!options.yes && files.length > 1) { | ||
| if (!options.json) { | ||
| if (!wantsJson(options)) { |
There was a problem hiding this comment.
Agent upload waits on prompt
Medium Severity
In agent or --json mode, multi-file uploads still call confirmPrompt when -y is omitted, while destructive commands require --force instead of prompting. Non-interactive runs can block on stdin or hang indefinitely.
Reviewed by Cursor Bugbot for commit a1c7d16. Configure here.


Summary
Fixes the top agent failure modes found by analyzing AXP experiment traces (Mux Onboarding and Robots Caption Translation experiments, ~600 tool failures across 180+ runs):
--agentno longer injects--jsoninto argv. It just sets agent mode, and commands consult a newwantsJson()helper to pick their output format. This fixes theerror: Unknown option "--json"crash on commands without a--jsonflag —mux login --agentwas the most common failure in the traces.MUX_TOKEN_ID/MUX_TOKEN_SECRETenv vars. Agents with credentials in their environment need no login step at all —mux assets listjust works. Env vars take precedence over stored config, consistent with existingMUX_BASE_URLbehavior. Traces showed agents with valid env creds hitting "Not logged in" walls and resorting to--env-file <(printf ...)process-substitution hacks.mux loginreads env vars (precedence:--env-file> env vars > interactive prompts) and supports--json. In JSON/agent mode with no credentials available, login fails fast with recovery guidance instead of blocking on interactive prompts.handleCommandErroremits JSON error payloads when machine-readable output is requested, now keyed offwantsJson()so--agentcallers get the same JSON errors they did when--agentimplied--json.Considered and deliberately dropped after review: an
error_codetaxonomy on JSON errors (avoids committing to a stable code surface; messages carry their own recovery guidance),--jsononlogout/env list/env switch(no trace evidence of need), and per-poll progress emission during robots--wait(keeps stdout a single pipeable JSON result).Test plan
0719743)mux --agent loginwith env creds,mux --agent assets listwithout login,mux --agent loginwith no credentials (fails fast), agent-mode JSON errors fromassets createmux-cli-publicvariant's tool-failure rate (12–19% baseline)🤖 Generated with Claude Code
Note
Medium Risk
Wide CLI surface area (auth precedence and output paths) but behavior is covered by tests; env-var auth precedence could surprise users who expect stored
mux loginconfig to win over partial or CI env overrides.Overview
Improves agent and automation use of the Mux CLI by centralizing JSON output decisions and fixing common auth and
--agentfailure modes.--agentbehavior: Global--agentno longer injects--jsoninto argv (which broke commands likeloginthat lack that flag).preprocessArgsonly strips--agentand sets agent mode;wantsJson()treats--jsonor agent mode as machine-readable output. Commands andhandleCommandErrorusewantsJson()instead ofoptions.jsondirectly.Authentication:
MUX_TOKEN_ID/MUX_TOKEN_SECRETare used when both are set, ahead of stored config (aligned withMUX_BASE_URL).mux loginadds--json,credentialsFromEnv, and a credential order of--env-file→ env vars → prompts; JSON/agent mode fails fast with guidance instead of interactive prompts.Robots polling:
pollForRobotsJobtakes an options object; JSON/agent waits stay silent on stderr so stdout remains a single JSON blob.Tests cover
wantsJson/preprocessArgs, login env flows, mux auth fallback, agent-mode JSON errors, and silent robots polling in JSON mode.Reviewed by Cursor Bugbot for commit a1c7d16. Bugbot is set up for automated code reviews on this repo. Configure here.