Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 45 additions & 3 deletions internal/codexusage/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand All @@ -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 <alias>'"
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 {
Expand Down
31 changes: 30 additions & 1 deletion internal/codexusage/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Loading