Skip to content

Agent experience: env-var auth fallback, structured JSON errors, --agent rework#71

Open
dylanjha wants to merge 9 commits into
mainfrom
dj/default-to-existing-env-vars
Open

Agent experience: env-var auth fallback, structured JSON errors, --agent rework#71
dylanjha wants to merge 9 commits into
mainfrom
dj/default-to-existing-env-vars

Conversation

@dylanjha

@dylanjha dylanjha commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

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):

  • --agent no longer injects --json into argv. It just sets agent mode, and commands consult a new wantsJson() helper to pick their output format. This fixes the error: Unknown option "--json" crash on commands without a --json flag — mux login --agent was the most common failure in the traces.
  • Auth falls back to MUX_TOKEN_ID/MUX_TOKEN_SECRET env vars. Agents with credentials in their environment need no login step at all — mux assets list just works. Env vars take precedence over stored config, consistent with existing MUX_BASE_URL behavior. Traces showed agents with valid env creds hitting "Not logged in" walls and resorting to --env-file <(printf ...) process-substitution hacks.
  • mux login reads 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.
  • Recovery guidance in error messages. The "Not logged in" error now names the env vars as an option. Command error handling otherwise matches main: handleCommandError emits JSON error payloads when machine-readable output is requested, now keyed off wantsJson() so --agent callers get the same JSON errors they did when --agent implied --json.

Considered and deliberately dropped after review: an error_code taxonomy on JSON errors (avoids committing to a stable code surface; messages carry their own recovery guidance), --json on logout/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

  • Test-first: new tests written and reviewed before implementation (0719743)
  • Full suite: 855 tests, 0 fail; typecheck and biome clean
  • Smoke-tested the exact trace failure scenarios: mux --agent login with env creds, mux --agent assets list without login, mux --agent login with no credentials (fails fast), agent-mode JSON errors from assets create
  • Re-run the AXP onboarding experiment against a build with these changes and compare the mux-cli-public variant'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 login config 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 --agent failure modes.

--agent behavior: Global --agent no longer injects --json into argv (which broke commands like login that lack that flag). preprocessArgs only strips --agent and sets agent mode; wantsJson() treats --json or agent mode as machine-readable output. Commands and handleCommandError use wantsJson() instead of options.json directly.

Authentication: MUX_TOKEN_ID / MUX_TOKEN_SECRET are used when both are set, ahead of stored config (aligned with MUX_BASE_URL). mux login adds --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: pollForRobotsJob takes 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.

dylanjha and others added 4 commits July 9, 2026 13:27
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>
Comment thread src/commands/assets/create.ts
dylanjha and others added 2 commits July 9, 2026 14:32
- 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>
Comment thread src/lib/errors.ts
Comment thread src/commands/login.ts
dylanjha and others added 3 commits July 14, 2026 11:03
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>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ 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.

Comment thread src/index.ts

return args;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a1c7d16. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant