From 299e36944801556e992424391563510d80a73ea9 Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Mon, 16 Nov 2026 15:23:22 -0600 Subject: [PATCH 1/3] Read Grok quota tokens from Grok and Pi before Hermes Allow quotas to use an existing Grok CLI or Pi subscription sign-in without requiring a separate Hermes sign-in. Prefer Grok's OIDC key, then Pi's OAuth access token, retaining Hermes as the final fallback. Skip missing or malformed stores and entries without an access token; API-key entries do not qualify as subscription sign-ins. Change-Id: If1349bd21f127c405eaa60b398b0388481dc7b87 --- internal/quotas/creds.go | 35 ++++++++++++++++++++-- internal/quotas/creds_test.go | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/internal/quotas/creds.go b/internal/quotas/creds.go index d749016..791c68e 100644 --- a/internal/quotas/creds.go +++ b/internal/quotas/creds.go @@ -14,7 +14,7 @@ import ( const commandWait = 10 * time.Second -// Source reads the sign-ins Hermes already stores on this machine. +// Source reads the sign-ins clients already store on this machine. // A zero Source reads the process environment, HERMES_HOME, and the user home. type Source struct { Env func(string) string @@ -113,13 +113,24 @@ func (s Source) MiniMaxToken() (string, error) { // XAIToken is a SuperGrok or X Premium sign-in, never an xAI API key. func (s Source) XAIToken() (string, error) { + if token := s.grokCLIToken(); token != "" { + return token, nil + } + pi := s.readJSON(filepath.Join(s.userHome(), ".pi", "agent", "auth.json")) + if state, ok := asObject(pi["xai"]); ok { + if state["type"] == "oauth" { + if token := oauthAccessToken(state, "", "access"); token != "" { + return token, nil + } + } + } if token := s.providerToken("xai-oauth"); token != "" { return token, nil } if token := s.poolToken("xai-oauth"); token != "" { return token, nil } - return "", MissingError{Text: "Sign in to Grok in Hermes to read its plan."} + return "", MissingError{Text: "Sign in to Grok with the Grok CLI, Pi, or Hermes to read its plan."} } func (s Source) authDocuments() []map[string]any { @@ -169,6 +180,26 @@ func (s Source) env(key string) string { return os.Getenv(key) } +func (s Source) grokCLIToken() string { + document := s.readJSON(filepath.Join(s.userHome(), ".grok", "auth.json")) + for id, value := range document { + if !strings.HasPrefix(id, "https://auth.x.ai::") { + continue + } + state, ok := asObject(value) + if !ok { + continue + } + if state["auth_mode"] != "oidc" { + continue + } + if token := oauthAccessToken(state, "", "key"); token != "" { + return token + } + } + return "" +} + func (s Source) hermesHome() string { if s.Hermes != nil { return s.Hermes() diff --git a/internal/quotas/creds_test.go b/internal/quotas/creds_test.go index 49a1ce6..9e8cce8 100644 --- a/internal/quotas/creds_test.go +++ b/internal/quotas/creds_test.go @@ -9,6 +9,61 @@ import ( "github.com/stretchr/testify/require" ) +func TestXAITokenUsesClientSignInsInOrder(t *testing.T) { + t.Parallel() + const grok = `{"https://auth.x.ai::client-id":{"auth_mode":"oidc","key":"grok-token"}}` + const pi = `{"xai":{"type":"oauth","access":"pi-token","refresh":"pi-refresh"}}` + const hermes = `{"providers":{"xai-oauth":{"tokens":{"access_token":"hermes-token"}}}}` + cases := []struct { + name string + grok string + pi string + hermes string + want string + }{ + {name: "Grok before Pi and Hermes", grok: grok, pi: pi, hermes: hermes, want: "grok-token"}, + {name: "Pi before Hermes", pi: pi, hermes: hermes, want: "pi-token"}, + {name: "Hermes provider", hermes: hermes, want: "hermes-token"}, + {name: "Hermes pool", hermes: `{"credential_pool":{"xai-oauth":[{"access_token":"pool-token"}]}}`, want: "pool-token"}, + {name: "malformed Grok", grok: `{`, pi: pi, want: "pi-token"}, + {name: "malformed Pi", pi: `[]`, hermes: hermes, want: "hermes-token"}, + {name: "empty Grok token", grok: `{"https://auth.x.ai::client-id":{"auth_mode":"oidc","key":""}}`, pi: pi, want: "pi-token"}, + {name: "Pi refresh alone", pi: `{"xai":{"type":"oauth","refresh":"not-an-access-token"}}`, hermes: hermes, want: "hermes-token"}, + {name: "Grok API key", grok: `{"https://auth.x.ai::client-id":{"auth_mode":"api_key","key":"not-a-plan-token"}}`, pi: pi, want: "pi-token"}, + {name: "unrelated issuer", grok: `{"https://other.example::client-id":{"auth_mode":"oidc","key":"not-a-grok-token"}}`, pi: pi, want: "pi-token"}, + {name: "Pi API key", pi: `{"xai":{"type":"api_key","key":"not-a-plan-token","access":"not-oauth"}}`}, + {name: "no sign-in"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + home := t.TempDir() + files := map[string]string{ + filepath.Join(home, ".grok", "auth.json"): tc.grok, + filepath.Join(home, ".pi", "agent", "auth.json"): tc.pi, + filepath.Join(home, ".hermes", "auth.json"): tc.hermes, + } + for path, body := range files { + if body == "" { + continue + } + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(body), 0o600)) + } + source := Source{UserHome: func() string { return home }, Env: func(string) string { return "" }} + token, err := source.XAIToken() + if tc.want == "" { + var missing MissingError + require.ErrorAs(t, err, &missing) + assert.Contains(t, err.Error(), "Sign in to Grok") + } else { + require.NoError(t, err) + } + assert.Equal(t, tc.want, token) + }) + } +} + func TestDotEnvBeatsAStaleEnvironmentKey(t *testing.T) { home := t.TempDir() t.Setenv("HERMES_HOME", home) From f1d3224ef23358207a160359b24e56d3ab72e58f Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Mon, 16 Nov 2026 17:41:28 -0600 Subject: [PATCH 2/3] Refactor: Extract Pi credential reader from Grok lookup Share Pi's typed credential lookup before adding its Copilot, Ollama Cloud, and OpenCode Go credentials to quotas. Keep Grok's lookup order and accepted credential format unchanged. Change-Id: I654845edaba2069af193340cb3f48093cba386dd --- internal/quotas/creds.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/quotas/creds.go b/internal/quotas/creds.go index 791c68e..5a26ad1 100644 --- a/internal/quotas/creds.go +++ b/internal/quotas/creds.go @@ -116,13 +116,8 @@ func (s Source) XAIToken() (string, error) { if token := s.grokCLIToken(); token != "" { return token, nil } - pi := s.readJSON(filepath.Join(s.userHome(), ".pi", "agent", "auth.json")) - if state, ok := asObject(pi["xai"]); ok { - if state["type"] == "oauth" { - if token := oauthAccessToken(state, "", "access"); token != "" { - return token, nil - } - } + if token := s.piToken("xai", "oauth", "access"); token != "" { + return token, nil } if token := s.providerToken("xai-oauth"); token != "" { return token, nil @@ -221,6 +216,18 @@ func (s Source) look(name string) (string, error) { return exec.LookPath(name) } +func (s Source) piToken(provider, kind, field string) string { + document := s.readJSON(filepath.Join(s.userHome(), ".pi", "agent", "auth.json")) + state, ok := asObject(document[provider]) + if !ok { + return "" + } + if state["type"] != kind { + return "" + } + return oauthAccessToken(state, "", field) +} + func (s Source) poolToken(provider string) string { for _, document := range s.authDocuments() { entries, present := poolEntries(document, provider) From 80b0935c2525e0fb3443414ff526cd0de539b34d Mon Sep 17 00:00:00 2001 From: Nathan Nutter Date: Mon, 16 Nov 2026 17:44:20 -0600 Subject: [PATCH 3/3] Read Pi credentials for Copilot, Ollama Cloud, and OpenCode Go Use existing Pi sign-ins to read these providers' quotas without requiring Hermes credentials or a GitHub CLI sign-in. Explicit environment variables remain overrides; Pi takes priority over Hermes for API keys and over the GitHub CLI for Copilot. Accept only API-key entries for Ollama Cloud and OpenCode Go and OAuth access tokens for Copilot. Missing or unusable entries retain the existing fallbacks. Codex and Muse continue to use their own CLIs. Change-Id: I0948c0cbb2938a75e2ccc76ec610fcbfb552b5b2 --- internal/quotas/creds.go | 16 ++++++- internal/quotas/creds_test.go | 82 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/internal/quotas/creds.go b/internal/quotas/creds.go index 5a26ad1..e3f3f46 100644 --- a/internal/quotas/creds.go +++ b/internal/quotas/creds.go @@ -25,8 +25,19 @@ type Source struct { ReadFile func(string) ([]byte, error) } -// APIKey is a provider key from Hermes's .env, the environment, or the credential pool. +// APIKey reads provider keys, including Pi keys for Ollama Cloud and OpenCode Go. func (s Source) APIKey(provider string, vars []string, what string) (string, error) { + switch provider { + case "ollama-cloud", "opencode-go": + for _, key := range vars { + if value := strings.TrimSpace(s.env(key)); value != "" { + return value, nil + } + } + if token := s.piToken(provider, "api_key", "key"); token != "" { + return token, nil + } + } dotenv := s.dotEnv() for _, key := range vars { if value := preferDotEnv(dotenv, key, s.env(key)); value != "" { @@ -81,6 +92,9 @@ func (s Source) CopilotToken(ctx context.Context) (string, error) { return value, nil } } + if token := s.piToken("github-copilot", "oauth", "access"); token != "" { + return token, nil + } gh, err := s.look("gh") if err != nil || blank(gh) { gh = s.shim("gh") diff --git a/internal/quotas/creds_test.go b/internal/quotas/creds_test.go index 9e8cce8..4553b32 100644 --- a/internal/quotas/creds_test.go +++ b/internal/quotas/creds_test.go @@ -1,6 +1,7 @@ package quotas import ( + "context" "os" "path/filepath" "testing" @@ -64,6 +65,87 @@ func TestXAITokenUsesClientSignInsInOrder(t *testing.T) { } } +func TestPiAPIKeysPrecedeHermesButNotEnvironment(t *testing.T) { + t.Parallel() + providers := []struct { + id string + envKey string + }{ + {id: "ollama-cloud", envKey: "OLLAMA_API_KEY"}, + {id: "opencode-go", envKey: "OPENCODE_GO_API_KEY"}, + } + cases := []struct { + name string + entry string + env string + want string + }{ + {name: "Pi before Hermes dotenv and pool", entry: `{"type":"api_key","key":"pi-key"}`, want: "pi-key"}, + {name: "environment override", entry: `{"type":"api_key","key":"pi-key"}`, env: "env-key", want: "env-key"}, + {name: "missing Pi entry", entry: `null`, want: "hermes-key"}, + {name: "empty Pi key", entry: `{"type":"api_key","key":""}`, want: "hermes-key"}, + {name: "OAuth is not an API key", entry: `{"type":"oauth","key":"wrong-type","access":"oauth-token"}`, want: "hermes-key"}, + } + for _, provider := range providers { + for _, tc := range cases { + t.Run(provider.id+"/"+tc.name, func(t *testing.T) { + t.Parallel() + source := envSource(map[string]string{provider.envKey: tc.env}) + source.ReadFile = func(path string) ([]byte, error) { + switch path { + case filepath.Join(".pi", "agent", "auth.json"): + return []byte(`{"` + provider.id + `":` + tc.entry + `}`), nil + case ".env": + return []byte(provider.envKey + "=hermes-key"), nil + case "auth.json": + return []byte(`{"credential_pool":{"` + provider.id + `":[{"access_token":"pool-key"}]}}`), nil + } + return nil, os.ErrNotExist + } + key, err := source.APIKey(provider.id, []string{provider.envKey}, provider.id) + require.NoError(t, err) + assert.Equal(t, tc.want, key) + }) + } + } +} + +func TestCopilotUsesPiBeforeGitHubCLI(t *testing.T) { + t.Parallel() + cases := []struct { + name string + entry string + env string + want string + }{ + {name: "Pi OAuth", entry: `{"type":"oauth","access":"pi-token","refresh":"pi-refresh"}`, want: "pi-token"}, + {name: "environment override", entry: `{"type":"oauth","access":"pi-token"}`, env: "env-token", want: "env-token"}, + {name: "GitHub CLI fallback", entry: `null`, want: "gh-token"}, + {name: "refresh is not access", entry: `{"type":"oauth","refresh":"pi-refresh"}`, want: "gh-token"}, + {name: "API key is not OAuth", entry: `{"type":"api_key","access":"wrong-type","key":"pi-key"}`, want: "gh-token"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + source := envSource(map[string]string{"GH_TOKEN": tc.env}) + source.ReadFile = func(path string) ([]byte, error) { + if path == filepath.Join(".pi", "agent", "auth.json") { + return []byte(`{"github-copilot":` + tc.entry + `}`), nil + } + return nil, os.ErrNotExist + } + source.LookPath = func(string) (string, error) { return "gh", nil } + source.Run = func(context.Context, string, ...string) ([]byte, error) { + assert.Equal(t, "gh-token", tc.want, "GitHub CLI should only run as a fallback") + return []byte("gh-token\n"), nil + } + token, err := source.CopilotToken(t.Context()) + require.NoError(t, err) + assert.Equal(t, tc.want, token) + }) + } +} + func TestDotEnvBeatsAStaleEnvironmentKey(t *testing.T) { home := t.TempDir() t.Setenv("HERMES_HOME", home)