Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
feat(auth): fix the credential prompt role once per process
Jack Rudenko17c43922bac0
diff --git a/packages/cli/src/auth/credentials/prompt-role.ts b/packages/cli/src/auth/credentials/prompt-role.tsnew file mode 100644index 00000000..67937a47--- /dev/null+++ b/packages/cli/src/auth/credentials/prompt-role.ts@@ -0,0 +1,96 @@+/**+ * Whether THIS process may open a credential dialog — fixed once, by the entry+ * point, never by a request.+ *+ * ## Why a role, and why per process+ *+ * Reading another application's Keychain item (the Muse Code CLI's login) can+ * open the macOS access dialog. A dialog is acceptable only where a person is+ * at the terminal that caused it and expects it: an interactive+ * `claudish --model …` launch, or an action the user just took in the config+ * TUI. Everywhere else — an MCP server, a `team` slot or channel session in a+ * headless magmux pane, `--probe`, `claudish serve`, a piped `-p` or CI — a+ * dialog appears on a desktop nobody is watching, blocks a request on it, and+ * piles up across runs.+ *+ * The decision was ambient once (read per request, from whatever the request+ * looked like), and every review found another caller that raised it. So it is+ * decided ONCE, where the process starts, from facts about the process:+ *+ * - `isPaneChild()` — NOT `isatty`. A magmux pane has a real PTY, so a `team`+ * slot looks interactive to every TTY test (`pane/child-env.ts`).+ * - stdin AND stderr are TTYs — a piped or CI launch is not a person.+ * - `--probe` exits inside `parseArgs`, before any launch code, and is a+ * diagnostic: it never prompts.+ *+ * ## The rule for changing it+ *+ * The first `setCredentialPromptRole` wins. After that the role can only be+ * LOWERED to `never`, never raised: an entry point deeper in the same process+ * that knows it must not prompt (`--probe`, the MCP server) can always say so,+ * and nothing a request does can widen it. The default, before any entry point+ * has spoken, is `never`.+ *+ * `CLAUDISH_PANE_CHILD` is the marker `pane-launch.ts` sets for every MCP+ * `team` slot and `create_session` pane. CLI `--team` grid panes do not carry+ * it: the user who ran `--team` is at that terminal, so those panes take+ * `interactive-launch`, and the per-machine read lock bounds them to one dialog.+ */++import { isPaneChild } from "../../pane/child-env.js";++/**+ * - `never`: no credential dialog. Only claudish's own files are read.+ * - `interactive-launch`: an interactive `claudish` launch with a person at the+ * terminal; the launch hook and the request path may read the Keychain.+ * - `tui`: the config TUI; only an explicit user action (`withInteractiveCredential`)+ * may read, in-process, before it issues any proxy request.+ */+export type CredentialPromptRole = "never" | "interactive-launch" | "tui";++let role: CredentialPromptRole = "never";+let fixed = false;++/**+ * Set the process's role. The first call fixes it; later calls may only lower it+ * to `never` (see the module comment). Returns the role in effect afterwards.+ */+export function setCredentialPromptRole(next: CredentialPromptRole): CredentialPromptRole {+ if (!fixed) {+ role = next;+ fixed = true;+ } else if (next === "never") {+ role = "never";+ }+ return role;+}++/** The role in effect. Read-only for every caller that is not an entry point. */+export function credentialPromptRole(): CredentialPromptRole {+ return role;+}++/** Facts about a CLI launch, injectable so the decision is testable without a TTY. */+export interface LaunchFacts {+ argv: readonly string[];+ env?: Record<string, string | undefined>;+ stdinIsTTY: boolean;+ stderrIsTTY: boolean;+}++/**+ * The role of a `claudish` CLI launch (`index.ts` `runCli`), decided before+ * `parseArgs` so the model picker it may open already knows it.+ */+export function decideLaunchRole(facts: LaunchFacts): CredentialPromptRole {+ if (facts.argv.includes("--probe")) return "never";+ if (isPaneChild(facts.env ?? process.env)) return "never";+ if (!facts.stdinIsTTY || !facts.stderrIsTTY) return "never";+ return "interactive-launch";+}++/** Test seam: forget the role, as if no entry point had run. */+export function _resetCredentialPromptRoleForTest(): void {+ role = "never";+ fixed = false;+}diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.tsindex f2ac761b..7ca1b5b3 100644--- a/packages/cli/src/cli.ts+++ b/packages/cli/src/cli.ts@@ -10,6 +10,7 @@ import { homedir } from "node:os";import { dirname, join } from "node:path";import { fileURLToPath } from "node:url";import { EFFORT_LEVELS, isEffortLevel } from "./adapters/base-api-format.js";+import { setCredentialPromptRole } from "./auth/credentials/prompt-role.js";import { ENV } from "./config.js";import { setStderrQuiet } from "./logger.js";import {@@ -821,6 +822,10 @@ export async function parseArgs(args: string[]): Promise<ClaudishConfig> {await initializeClaudishSkill();process.exit(0);} else if (arg === "--probe") {+ // A probe is a diagnostic and never opens a credential dialog. `runCli`+ // already decided `never` from argv; this lowers it for any other caller+ // of parseArgs (`auth/credentials/prompt-role.ts`).+ setCredentialPromptRole("never");// Probe models — show fallback chain for each modelconst probeModels: string[] = [];while (i + 1 < args.length && !args[i + 1].startsWith("--")) {diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 8ab18103..1267351c 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -632,6 +632,23 @@ async function runCli() {await detectAndSetThemeMode();});+ // The credential prompt role, fixed BEFORE parseArgs: `--probe` exits inside+ // it, and the model picker below may need a credential. A pane child, a+ // piped or CI launch, and `--probe` never open a credential dialog+ // (`auth/credentials/prompt-role.ts`).+ {+ const { decideLaunchRole, setCredentialPromptRole } = await import(+ "./auth/credentials/prompt-role.js"+ );+ setCredentialPromptRole(+ decideLaunchRole({+ argv: process.argv.slice(2),+ stdinIsTTY: process.stdin.isTTY === true,+ stderrIsTTY: process.stderr.isTTY === true,+ })+ );+ }+// Parse CLI arguments (includes profile/config load; terminal flags like// --version/--models/--probe exit inside — the exit-hook fallback covers them)const cliConfig = await traceSpan("startup:parse-args", () => parseArgs(process.argv.slice(2)));diff --git a/packages/cli/src/mcp-server.ts b/packages/cli/src/mcp-server.tsindex 152b7c55..ee65763c 100644--- a/packages/cli/src/mcp-server.ts+++ b/packages/cli/src/mcp-server.ts@@ -21,6 +21,7 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotimport { config } from "dotenv";import { searchCatalogModels } from "./adapters/model-catalog.js";import { prehydrateCredentialsForSpawn } from "./auth/credentials/prehydrate.js";+import { setCredentialPromptRole } from "./auth/credentials/prompt-role.js";import { isSubscriptionProvider } from "./handlers/shared/remote-provider-types.js";import { SessionManager } from "./mcp-session/index.js";import { parentSessionForCall } from "./mcp-session/parent-session.js";@@ -2403,6 +2404,9 @@ function installMcpShutdown(sessionManager: SessionManager): void {* Called from index.ts when --mcp flag is used.*/export function startMcpServer() {+ // An MCP server never opens a credential dialog: its host is a program, and+ // its panes are pane children (`auth/credentials/prompt-role.ts`).+ setCredentialPromptRole("never");main().catch((error) => {console.error("[claudish] MCP fatal error:", error);process.exit(1);diff --git a/packages/cli/src/serve-command.ts b/packages/cli/src/serve-command.tsindex 365ab984..7040f1ac 100644--- a/packages/cli/src/serve-command.ts+++ b/packages/cli/src/serve-command.ts@@ -28,6 +28,7 @@*/import { existsSync, readFileSync } from "node:fs";+import { setCredentialPromptRole } from "./auth/credentials/prompt-role.js";import { ensureEndpointsRegistered } from "./providers/endpoint-registration.js";import { type SlotRoute, createProxyServer } from "./proxy-server.js";@@ -119,6 +120,9 @@ function loadModelMap(path: string): { slotMap: Map<string, SlotRoute>; slotIds:}export async function serveCommand(args: string[]): Promise<void> {+ // A gateway for Claude Desktop answers requests nobody at a terminal made:+ // it never opens a credential dialog (`auth/credentials/prompt-role.ts`).+ setCredentialPromptRole("never");let serveArgs: ServeArgs;try {serveArgs = parseServeArgs(args);diff --git a/packages/cli/src/tui/index.tsx b/packages/cli/src/tui/index.tsxindex 2f13bb95..c2f72b0d 100644--- a/packages/cli/src/tui/index.tsx+++ b/packages/cli/src/tui/index.tsx@@ -4,6 +4,7 @@ import { createCliRenderer } from "@opentui/core";import { createRoot } from "@opentui/react";import { _resetAntigravityTokenState } from "../auth/antigravity-token.js";import { getCodexOAuth } from "../auth/codex-oauth.js";+import { setCredentialPromptRole } from "../auth/credentials/prompt-role.js";import { getKimiOAuth } from "../auth/kimi-oauth.js";import { setStderrQuiet } from "../logger.js";import { applyRendererThemeMode } from "../theme/renderer-theme.js";@@ -31,6 +32,10 @@ import { invalidateProbeProxyHandlers, shutdownProbeProxy } from "./probe-proxy.* rare and the state machine is small.*/export async function startConfigTui(): Promise<void> {+ // The config TUI may read a credential only inside an explicit user action,+ // in-process, before that action issues any proxy request+ // (`auth/credentials/prompt-role.ts`).+ setCredentialPromptRole("tui");// Register bundled endpoints before the first render: the Providers tab is// built from `getAllProviders()`, so a catalog vendor the user has a key for// must already be in the provider list or it simply is not there to configure.