diff --git a/README.md b/README.md index ba1903a..1b263e3 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ codex-switch account start-window work The command always targets exactly one saved alias and intentionally consumes a small amount of that account's Codex allowance. It does not retry the model -request. Codex runs with a fixed short prompt, minimal reasoning, no persisted +request. Codex runs with a fixed short prompt, low reasoning, no persisted session, an empty temporary workspace, a read-only sandbox, and no user config, rules, plugins, or MCP servers. After the request completes, `codex-switch` refreshes the usage snapshot so the returned reset time can be inspected. The diff --git a/docs/architecture.md b/docs/architecture.md index 2feaa8c..af20a01 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -105,7 +105,8 @@ acquire shared operation lock -> decrypt and validate exactly one selected profile -> create a temporary CODEX_HOME and an unrelated empty workspace -> run one non-interactive, ephemeral Codex turn with a fixed short prompt - -> disable user config and rules; use minimal reasoning and a read-only sandbox + -> disable user config and rules; explicitly keep file-backed auth for the temporary home + -> use low reasoning and a read-only sandbox -> do not retry the turn -> query the account usage through App Server in the same temporary home -> reconcile any rotated credential generation and cache the new snapshot diff --git a/docs/compatibility.md b/docs/compatibility.md index 1e771a6..0091f51 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -73,7 +73,9 @@ Usage querying initializes `codex app-server` and calls: `codex exec` mode and its `--ephemeral`, `--ignore-user-config`, `--ignore-rules`, and read-only sandbox options. The wake turn uses the CLI's default ChatGPT-plan Codex model so it affects the ordinary Codex allowance -rather than selecting a separately metered model family. +rather than selecting a separately metered model family. Because user config is +ignored, the command explicitly overrides `cli_auth_credentials_store` to +`file` so the selected profile is read from the isolated temporary home. When returned by `account/rateLimits/read`, the client also preserves `rateLimitResetCredits.availableCount` and its optional grant/expiration detail diff --git a/docs/security.md b/docs/security.md index c7d93af..8e92a5b 100644 --- a/docs/security.md +++ b/docs/security.md @@ -42,8 +42,10 @@ focuses on: credential files with mode `0600`, then remove them after the App Server exits. - Quota-window wake requests use the same temporary credential home plus a separate empty workspace. They run one fixed prompt with an ephemeral session, - minimal reasoning, ignored user config and rules, a read-only sandbox, and no - automatic retry. Child stdout and diagnostics are never exposed or cached. + low reasoning, ignored user config and rules, an explicit file-auth + override for the temporary home, a read-only sandbox, and no automatic retry. + Child stdout is never exposed or cached. Child diagnostics are reduced to a + fixed, non-secret error category; unrecognized details are redacted. - The usage cache contains rate limits, earned-reset counts and metadata, aggregate token statistics, timestamps, and public account metadata only. It never contains authentication documents. diff --git a/internal/codexusage/runner.go b/internal/codexusage/runner.go index 7ed34a0..5e0e90e 100644 --- a/internal/codexusage/runner.go +++ b/internal/codexusage/runner.go @@ -240,8 +240,9 @@ func (runner Runner) runWake(ctx context.Context, temporaryHome string) error { "--skip-git-repo-check", "--sandbox", "read-only", "--color", "never", + "-c", `cli_auth_credentials_store="file"`, "-c", `approval_policy="never"`, - "-c", `model_reasoning_effort="minimal"`, + "-c", `model_reasoning_effort="low"`, "--cd", workspace, "Reply with exactly OK. Do not use tools.", ) @@ -258,14 +259,55 @@ func (runner Runner) runWake(ctx context.Context, temporaryHome string) error { if ctx.Err() != nil { return fmt.Errorf("send minimal Codex request: %w", ctx.Err()) } - if strings.TrimSpace(stderr.String()) != "" { - return fmt.Errorf("minimal Codex request failed: %w (Codex diagnostics redacted)", err) + if diagnostic := wakeDiagnostic(stderr.String()); diagnostic != "" { + return fmt.Errorf("minimal Codex request failed: %w (%s)", err, diagnostic) } return fmt.Errorf("minimal Codex request failed: %w", err) } return nil } +func wakeDiagnostic(stderr string) string { + if strings.TrimSpace(stderr) == "" { + return "" + } + lower := strings.ToLower(stderr) + switch { + case strings.Contains(lower, "401 unauthorized"), + strings.Contains(lower, "missing bearer or basic authentication"), + strings.Contains(lower, "authentication required"), + strings.Contains(lower, "invalid_grant"), + strings.Contains(lower, "refresh token") && strings.Contains(lower, "expired"): + return "Codex did not accept this account's saved credentials; re-login the account with 'codex-switch account login '" + case strings.Contains(lower, "429 too many requests"), + strings.Contains(lower, "rate limit"), + strings.Contains(lower, "usage limit"), + strings.Contains(lower, "quota exceeded"): + return "Codex rejected the request because this account is rate limited or out of quota" + case strings.Contains(lower, "model_not_found"), + strings.Contains(lower, "model is not supported"), + strings.Contains(lower, "unsupported model"), + strings.Contains(lower, "does not have access to model"): + return "the installed Codex default model is unavailable for this account" + case strings.Contains(lower, "unsupported value") && strings.Contains(lower, "reasoning"), + strings.Contains(lower, "reasoning effort") && strings.Contains(lower, "not supported"): + return "the configured reasoning effort is not supported by the installed Codex default model; update codex-switch and retry" + case strings.Contains(lower, "unrecognized option"), + strings.Contains(lower, "unexpected argument"), + strings.Contains(lower, "invalid value") && strings.Contains(lower, "--"): + return "the installed Codex CLI rejected the wake command options; update Codex and retry" + case strings.Contains(lower, "connection refused"), + strings.Contains(lower, "failed to connect"), + strings.Contains(lower, "dns error"), + strings.Contains(lower, "network is unreachable"), + strings.Contains(lower, "tls handshake"), + strings.Contains(lower, "timed out"): + return "Codex could not reach the service; check the network and retry" + default: + return "Codex returned an unrecognized diagnostic; details were redacted" + } +} + func readUpdatedAuth(temporaryHome string, original authschema.Document) (json.RawMessage, error) { updated, err := atomicfile.ReadLimited(filepath.Join(temporaryHome, "auth.json"), 2<<20) if err != nil { diff --git a/internal/codexusage/runner_test.go b/internal/codexusage/runner_test.go index 3ad7196..a68277a 100644 --- a/internal/codexusage/runner_test.go +++ b/internal/codexusage/runner_test.go @@ -117,8 +117,9 @@ func TestRunnerWakeSendsOneMinimalIsolatedRequestBeforeQuery(t *testing.T) { "--ignore-rules", "--skip-git-repo-check", "--sandbox read-only", + `cli_auth_credentials_store="file"`, `approval_policy="never"`, - `model_reasoning_effort="minimal"`, + `model_reasoning_effort="low"`, "Reply with exactly OK. Do not use tools.", } { if !strings.Contains(wake, expected) { @@ -130,6 +131,34 @@ func TestRunnerWakeSendsOneMinimalIsolatedRequestBeforeQuery(t *testing.T) { } } +func TestWakeDiagnosticClassifiesWithoutExposingChildOutput(t *testing.T) { + tests := []struct { + name string + stderr string + expected string + }{ + {name: "empty", stderr: "", expected: ""}, + {name: "auth", stderr: "private@example.com: 401 Unauthorized: Missing bearer or basic authentication in header", expected: "did not accept this account's saved credentials"}, + {name: "rate limit", stderr: "secret request id: 429 Too Many Requests", expected: "rate limited or out of quota"}, + {name: "model", stderr: "secret model value: model_not_found", expected: "default model is unavailable"}, + {name: "reasoning effort", stderr: "Unsupported value: 'minimal' is not supported for reasoning.effort", expected: "reasoning effort is not supported"}, + {name: "option", stderr: "unexpected argument '--future-option' found", expected: "rejected the wake command options"}, + {name: "network", stderr: "private proxy: failed to connect to host", expected: "check the network and retry"}, + {name: "unknown", stderr: "sensitive diagnostic", expected: "unrecognized diagnostic"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := wakeDiagnostic(test.stderr) + if !strings.Contains(result, test.expected) { + t.Fatalf("wakeDiagnostic() = %q, want text containing %q", result, test.expected) + } + if result != "" && strings.Contains(result, test.stderr) { + t.Fatalf("wakeDiagnostic() leaked child output: %q", result) + } + }) + } +} + func TestRunnerWakeReportsCompletedRequestWhenUsageVerificationFails(t *testing.T) { runner := helperRunnerMode(t, false, true) runner.Wake = true