Skip to content

fix(mocactl): a failed login names the control plane and the real error - #836

Merged
webchang merged 2 commits into
rossoctl:mainfrom
webchang:pr/i700-login-errors
Nov 16, 2026
Merged

webchang merged 2 commits into
rossoctl:mainfrom
webchang:pr/i700-login-errors

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #700

What changed and why

  • When a login failed (mocactl login, mocactl sbx login, or the TUI Login overlay, which the onboarding overlay's login step reuses), the user saw one fixed sentence, for example the control plane is unavailable — try again shortly. It did not say which control plane was tried, and it left out the HTTP status, the API's error code and message, and the network cause. mocactl had already parsed all of these into the ApiError.
  • New describeLoginError(err, controlPlaneUrl) in packages/mocactl/src/core/messages.ts. It keeps the existing friendly sentence, names the control plane by the URL it is configured with, and appends the real cause:
    • an HTTP error with an API body: the control plane at https://… is unavailable (HTTP 503 redis_unavailable: <message>) — try again shortly
    • a body that is not the API's JSON (a proxy's or firewall's page): (HTTP 502, not an API answer: a proxy?)
    • a network error: cannot reach the control plane at https://…: fetch failed (ECONNREFUSED). DNS (ENOTFOUND) and TLS cause codes appear the same way, and a dropped connection also shows its cause.
    • any other failure, for example a 200 that is not JSON: its own text, then (from the control plane at https://…)
      The URL loses any userinfo, query and fragment before it is printed, so a password typed into the URL never reaches the screen. All text still goes through sanitizeRemote.
  • All three login paths use it: headless.ts (cmdLogin, and cmdLoginTestUser's general case), sbx/commands.ts (cmdLogin), and views/overlays/Login.tsx. sbx login keeps fail()'s exit codes (4 when unreachable, 1 otherwise) and its own wording for not_enrolled and the token codes. sbx login also no longer prints the doubled cannot reach the sandbox control plane: cannot reach the control plane: ….
  • networkError (api/errors.ts) now adds the cause's message when the cause has no .code, so such a failure is not just fetch failed.
  • Every other describeError caller is unchanged: the URL is added only on login paths.

Tests

Added tests, each run on main (with the src/ changes stashed) and then with the fix:

  • test/headless.test.ts, cmdLogin: a 503 with {"error":"redis_unavailable","message":"redis is down"} from POST /v1/auth/device (parsed by the real errorFromResponse), and a network error (ECONNREFUSED cause, through the real networkError) on the device-token poll.
    • main: login failed: the control plane is unavailable — try again shortly and login failed: cannot reach the control plane: fetch failed (ECONNREFUSED)
    • fixed: login failed: the control plane at http://cp is unavailable (HTTP 503 redis_unavailable: redis is down) — try again shortly and login failed: cannot reach the control plane at http://cp: fetch failed (ECONNREFUSED)
  • test/sbx-cli.test.ts, through the real HTTP client: a 503 credential_unavailable with a message from POST /v1/auth/device (exit 1), a non-JSON 502 (exit 1), and an ENOTFOUND on GET /v1/discovery (exit 4).
    • main: sbx: the control plane is unavailable — try again shortly and sbx: cannot reach the sandbox control plane: cannot reach the control plane: fetch failed (ENOTFOUND)
    • fixed: the URL and the cause appear in each case
  • test/overlay-login.test.tsx: a 500 internal_error in the overlay now shows the control plane at http://cp is unavailable (HTTP 500 internal_error: boom). The existing retry test's expected text now includes the URL.
  • test/messages.test.ts: describeLoginError table (a 5xx with and without a message, a non-JSON 5xx, a 4xx show, identity_provider_misconfigured, a network error, a dropped connection, a non-API error). It also checks that the URL's password and query are never printed and that escape sequences are stripped.
  • test/errors.test.ts: networkError folds in a cause's message when the cause has no code.

Before the fix: 18 of these tests fail (164 pass). After: all 182 pass. Full @moca/mocactl suite (vitest run --maxWorkers=2): 2019 passed and 4 skipped. Six timeouts on a heavily loaded machine (sbx-push, sbx-templates, sbx-up, none of them login code) passed when those files were rerun alone (133, 94 and 50 tests). tsc --noEmit is clean, and Prettier is clean.

Out of scope

  • --record for login. mocactl sbx refuses --record F on login, logout, token and credentials on purpose (NO_RECORD in sbx/cli.ts). mocactl login has no --record at all: only mocactl run does. Supporting login safely would need more than lifting the refusal. The device-token poll's request body carries the device code, and the recorder's SECRET_FIELDS does not list deviceCode today. The poll also writes one line every few seconds while it waits. That is a separate design decision, so this change leaves it out. The improved one-line error covers the "what came back" part of a login failure.
  • The issue's broader proposals: the URL's source (flag, env, sandbox, saved config); which step failed; separate advice per cause beyond the "not an API answer" hint; a --debug / MOCACTL_DEBUG switch; a rotated failure log; mocactl doctor --report; a server-side maintenance code; X-Request-Id. Each is a larger feature, and some need server work.

Every commit is signed off (DCO).

Assisted-By: Claude Code

A failed `mocactl login`, `mocactl sbx login` or TUI Login overlay printed
a fixed sentence such as "the control plane is unavailable — try again
shortly": not which control plane it tried, nor the HTTP status, the API's
error code and message, or the network error that mocactl had already
parsed into the ApiError.

describeLoginError (core/messages.ts) keeps the friendly sentence, names
the control plane at its configured URL (userinfo, query and fragment
stripped), and adds the cause: "HTTP 503 redis_unavailable: <message>", or
"HTTP 502, not an API answer: a proxy?" for a non-JSON body, or the network
error with its cause code. All three login paths use it; sbx login keeps
fail()'s exit codes (4 unreachable, 1 failed) and its own not-enrolled and
token-code wording. networkError now folds in a cause's message when the
cause has no code, so such a failure is no longer a bare "fetch failed".

Closes rossoctl#700

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 16, 2026 20:45
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 16, 2026

@ingpaolodettori-dev ingpaolodettori-dev 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.

Summary

This PR makes all three login paths (mocactl login, mocactl sbx login, and the TUI Login overlay) say which control plane they tried and what came back: the HTTP status with the API's code and message, or the network cause. It also strips userinfo, query and fragment from the URL before printing it. Error-text sanitization (sanitizeRemote), exit codes and the not_enrolled and token-code wording are unchanged, and other describeError callers behave as before. One suggestion: sbx login still drops the URL for failures that are not ApiErrors. One nit about the error code being printed twice.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (mocactl src + tests), Security (secrets in output, sanitization)
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: nothing failing; deploy-scripts, deploy-scripts-bash32 and k8s-kind-e2e still pending at submit time. This review was static (PR code was not run).

Comment thread packages/mocactl/src/sbx/commands.ts Outdated
if (end !== undefined) return end;
// A refusal `fail` words for itself (not enrolled, a token code) keeps its words; any other
// failure names the control plane tried and what came back (#700), at `fail`'s exit codes.
if (err instanceof ApiError && err.code !== 'not_enrolled' && !TOKEN_CODES.has(err.code)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion — This branch only covers ApiErrors, so a non-ApiError failure still goes to failed(c, err) → fail()'s last branch and prints sbx: <message> with no control-plane URL. Example: /v1/discovery or /v1/auth/device answering 200 with a non-JSON body (a captive portal or proxy page) throws a SyntaxError from res.json(). The comment above says "any other failure names the control plane tried", and the PR body says the non-API case gets (from the control plane at …) on all three login paths. mocactl login and the overlay do that, but sbx login does not. Consider sending non-ApiErrors (other than the cancel/expiry ones deviceLoginEnd already handled) through describeLoginError(err, c.url) at SBX_EXIT.failed too, plus a test for it, or else narrow the comment and the PR description.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 53bc544. sbx login now sends every failure that fail does not word for itself through describeLoginError(err, c.url): anything but not_enrolled and the token codes, an ApiError or not. Exit codes are unchanged: 4 for unreachable, 1 otherwise. New test: /v1/discovery answers 200 with an HTML page, and the output is sbx: login failed: … (from the control plane at <URL>), exit 1. It fails without the change.

Assisted-By: Claude Code

if (classify(err).kind === 'unavailable')
return sanitizeRemote(`${cp} is unavailable (${cause}) — try again shortly`);
// Most sentences name the control plane now; the server's own message ('show') does not.
return sanitizeRemote(text.includes(cp) ? `${text} (${cause})` : `${text} (${cp}: ${cause})`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit — When classify returns show and the body had no message, err.message falls back to the code (super(message ?? code)), so text is the bare code and the code appears twice: e.g. a 404 {"error":"not_found"} prints not_found (the control plane at https://…: HTTP 404 not_found), and a non-JSON 4xx prints http_404 (the control plane at https://…: HTTP 404, not an API answer: a proxy?). Maybe when err.message === err.code the sentence should start from the cause alone (e.g. the control plane at https://… refused the login (HTTP 404 not_found)).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 53bc544. When the body had no message (err.message === err.code), the sentence now starts from what happened: the control plane at <URL> refused the login (HTTP 404 not_found), and for a non-JSON 4xx … refused the login (HTTP 404, not an API answer: a proxy?). Both are new cases in messages.test.ts that fail without the change.

Assisted-By: Claude Code

…code twice

A failure that is not an ApiError (a proxy's 200 page that is not JSON) now goes through
describeLoginError on sbx login too, as on mocactl login and the overlay. And a body with no message
of its own reads 'the control plane at URL refused the login (HTTP 404 not_found)', not the code
twice (review suggestion and nit).

Part of rossoctl#700

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang merged commit 1e3da96 into rossoctl:main Nov 16, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox
idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837,
rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824),
egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer
(rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers
(rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors
(rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a
sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821).

Conflicts (one):

- deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843)
  rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most
  SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and
  SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had
  added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking
  upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA
  row after SBX_PROFILE, then re-padding the table with prettier. The
  fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly.

Merged without markers, checked: setup.sh keeps the fork's
SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays
in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS;
both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional
SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional
SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases
beside upstream's. The fork's RUNBOOK, README and template-onboarding
docs say nothing about sandbox lifetime, refusal bursts, login errors or
the create rollback, so none needed a correction; GUIDE.md is unchanged
and GUIDE.pdf is not regenerated.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mocactl: login failures hide the control plane and the API error; no way to capture failure details

3 participants