Skip to content

Add named profiles, ported from basecamp-cli - #644

Merged
jeremy merged 10 commits into
mainfrom
profiles
Nov 12, 2026
Merged

jeremy merged 10 commits into
mainfrom
profiles

Conversation

@jeremy

@jeremy jeremy commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What it lets people do

Keep more than one HEY sign-in on one computer: a second HEY account, or staging or a beta beside the account you use every day. Each is a named profile with its own server, mail account and credential, chosen per command with -P/--profile or HEY_PROFILE, or by default_profile.

hey profile create staging --base-url https://staging.example.com
hey -P staging box view imbox
hey profile set-default personal

With no profiles configured nothing changes: the same config reads, the same credential key, the same answers.

What it ports

basecamp-cli's profiles at 48b01aaf, adapted to hey's names:

  • profiles and default_profile in the global config (internal/config/config.go's ProfileConfig: base_url, account_id), merged across files by mergeProfile's rule.
  • -P/--profile, HEY_PROFILE, and resolveProfile (internal/cli/root.go) with its order and its error words: flag, env, default_profile, a lone profile, a picker at a terminal (never under HEY_NONINTERACTIVE or machine output), else multiple profiles configured but none selected; use --profile or set default_profile (available: …).
  • Credentials keyed profile:<name> (ProfileCredentialKey); hey mcp's apart as mcp:profile:<name>. A response cache per profile.
  • hey profile list|show|create|delete|set-default (internal/commands/profile.go) with its fields and messages. create signs in before it writes anything and the first profile becomes the default; delete revokes the profile's sign-ins at its own server, removes them and clears default_profile.

hey-specific:

  • A repository's .hey/config.json sets profiles/default_profile only once hey config trust-local trusts them: they join the digest trusted_local_configs already records, so a change needs trust again, and a config trusted before profiles keeps its digest. Untrusted, they are ignored with basecamp-cli's warning.
  • meta.profile on every JSON answer while a profile is active; hey auth status, hey config show and hey doctor name it.
  • A profile that names no account uses All Accounts, not the top-level account default, which belongs to the top-level sign-in; hey account use under a profile saves the account on the profile.
  • hey profile … and the commands that read no server (version, upgrade, skill, setup subcommands) run with no profile selected, so hey profile set-default can answer the refusal that names it. A profile hey would refuse (plain HTTP to a remote host) doesn't lock out profile delete/set-default.
  • A sign-in hint under a profile reads Run: hey auth login -P <name>.
  • A profile's credential goes only to the profile's own server. basecamp-cli lets --base-url or BASECAMP_BASE_URL override a profile's server; hey refuses an override naming another server (HEY_BASE_URL https://… is not profile "work"'s server (…)), and refuses a stored sign-in whose token endpoint is on another server, so an edited base_url never carries the old token over.
  • A repository entry refines a global profile only on the same server; one that would move it elsewhere is skipped with a warning. A repository's account_id follows a profile only on the files' own server.
  • delete refuses a profile a repository config sets, alone or beside the global entry, and revokes only at the server that issued the sign-in, warning where it can't; hey auth logout --mcp -P <name> revokes only there too, and a profile create whose config write fails revokes the new sign-in.

Verification

  • make check passes.
  • Four fresh review rounds: the last, over the final fixes alone, found no High or Medium.
  • Each guard added after review (issuer check, server override, repository account, local-only delete, account type, default count, completion fallback, config subcommands, status hints) has a test that fails with the guard disabled.
  • Red first, on origin/main (e6d5bcb) with the new tests: TestProfilesConfiguredButNoneSelectedRefuses, TestProfileSelectionNamesWhatIsConfigured, TestProfileResolutionOrder, TestEnvironmentAndFlagsOverrideTheProfile, TestAProfilesCredentialIsNeverReadByAnother and TestRepositoryLocalProfilesNeedTrust failed; TestNoProfilesChangesNothing passed there and passes here.
  • TestNoProfilesChangesNothing pins the no-profile path: the token is stored under the origin, mcp::<origin> stays hey mcp's key, auth status and config show answer as before, and no answer names a profile. TestLocalConfigDigestWithoutProfilesIsUnchanged pins the trust digest.
  • hey --help against a build of origin/main differs only by profile Manage named profiles and -P, --profile Use a named profile; each subcommand's help only by the inherited -P flag.
  • .surface gains the profile commands and --profile, nothing else.

Left out on purpose

  • The HEY app endpoint (endpoint: app, the gate, the per-endpoint checks) is the next PR, A, built on this one.
  • --scope and client_id on a profile. hey's CLI client holds only full, and a read-only sign-in against it is unverified; basecamp-cli's own profile scope is a record of the grant, not used at sign-in.
  • --expect-identity, auth login creating a profile (AnnotationProfileMayCreate), and the per-field origin bookkeeping basecamp-cli keeps for its account-binding hints.
  • hey setup creating profiles: a later PR, C.

RetriggerView in GreptileConfidence Score: 2/5

Fix the refresh issuer check and repository profile account saves before merging.

Findings

  1. P1 Security Refresh sends another server's token ▶
  2. P1 Profile account changes don't stick ▶
  3. P2 Version checks select broken profiles ▶
  4. P2 Failed answers omit the profile ▶
Fix with agent prompt
### Issue 1
internal/auth/auth.go:438-440
`checkProfileIssuer` runs on the first credential load, but not when `refreshLocked` rereads the store. Start `hey tui -P work` on server A, change `work.base_url` to server B, and sign in again as `work`. When the running TUI refreshes, it reads B's credential under the same key. Unlike MCP, the CLI manager has no pinned grant. If the access token changed, `refreshLocked` caches it and returns immediately, so the next request sends B's token to A.

Check the issuer of `stored` before caching or using it in `refreshLocked`.

**How this was verified:** The refresh path rereads the profile's shared credential key and accepts a changed access token without comparing its issuer to the running manager's server.

### Issue 2
internal/config/config.go:504-506
`SaveAccountID` sends every active profile to a writer that only edits the global config. This breaks two supported cases:
- For a profile defined only in a trusted `.hey/config.json`, `hey -P work account use all` fails with `Profile "work" not found`, even though `work` is active.
- If both files define the profile and the local entry sets `account_id`, the write succeeds but the local value still wins on the next run, with no warning.

Make the save path account for where the profile and its account value came from.

### Issue 3
internal/cmd/profile_select.go:76-84
`mayLeaveUnselected` is checked only after choosing the default, choosing a lone profile, and opening the picker. With a lone profile whose `base_url` uses remote plain HTTP, `hey version` fails in `ApplyProfile` instead of printing the version. With several profiles and no default, it unnecessarily opens the picker at a terminal.

Skip automatic selection for commands that read no server, rather than only suppressing the final selection error.

### Issue 4
internal/cmd/root.go:598-599
`profileOption` is added only to successful responses. A command that fails after selecting a profile, such as `hey -P work --json box list` while signed out, goes through `writer.Err` and returns no `meta.profile`. Programs therefore cannot consistently identify which sign-in failed.

Add the active profile to error metadata too, while keeping the no-profile output unchanged.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds named profiles with separate credentials and caches, trusted repository configuration, profile selection, and management commands.

  • HEY commands can use a named profile's server and mail account.
  • The hey profile commands manage named sign-ins and their settings.
  • Each profile keeps its own CLI and MCP sign-in and response cache.
  • Repository profiles take effect only after the repository config is trusted.
  • Active profiles are named in JSON answers and diagnostic output.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Load global and repository config] --> B[Trust repository profile keys]
  B --> C[Choose profile]
  C --> D[Apply server and account]
  D --> E[Use profile credential key]
  E --> F[Check stored credential issuer]
  F --> G[Make requests]
  G --> H[Refresh rereads shared credential key]
  H --> I[Cache replacement without issuer check]
  I --> G
Loading

Reviews (1) · Last reviewed commit: "The docs say which repository entry hold..." · Reviewed by Greptile

hey kept one sign-in per server, so a second HEY account, staging or a
beta could not sit beside the account you use every day without
swapping HEY_BASE_URL and signing in again.

Profiles are basecamp-cli's at 48b01aaf: profiles and default_profile
in the global config; -P/--profile, HEY_PROFILE and resolveProfile's
order and words (flag, env, default_profile, a lone profile, a picker
at a terminal, else "multiple profiles configured but none selected");
credentials keyed profile:<name>, and hey mcp's mcp:profile:<name>; a
cache per profile; and hey profile list|show|create|delete|set-default
with basecamp-cli's fields and messages. JSON answers carry
meta.profile, and auth status, config show and doctor name the
profile. A repository's .hey/config.json sets the profile keys only
once hey config trust-local trusts them: they join the digest
trusted_local_configs already records, which is unchanged for a
config without them. With no profiles configured nothing reads
differently.

Adapted for hey: a profile that names no account takes All Accounts,
not the top-level account default, which belongs to the top-level
sign-in, and hey account use under a profile saves to the profile;
profile and version-like commands run with no profile selected, so
the remedy the refusal names can be taken; a sign-in hint names -P.

Verified by make check. The selection, credential-keying and
repository-trust tests failed on the tree before this commit; a test
pins that with no profiles the config reads, the credential key and
the answers are as before, and root help differs only by -P and the
profile command.

Not ported: --scope and client_id on a profile (hey's CLI client holds
only full, and a read sign-in against it is unverified), the
auth-login-creates-a-profile path, --expect-identity and the
per-field origin bookkeeping. The HEY app endpoint is a later change.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

5 issues found across 23 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/cmd/profile.go">

<violation number="1" location="internal/cmd/profile.go:356">
P2: Profile deletion can send revocation to the wrong server because `Manager.Revoke` ignores the credential’s `TokenEndpoint`. Make revocation use the stored endpoint, falling back to the manager base URL like refresh does.</violation>

<violation number="2" location="internal/cmd/profile.go:364">
P1: Profile deletion can remove a refreshed token without revoking it because revocation and deletion use separate cross-process lock scopes. Keep credential load, revocation, and deletion under one store lock or add an atomic manager operation.

(Based on your team's feedback about serialized logout revocation.) [e28f5d24-4cad-4a96-b372-94072401a059].</violation>

<violation number="3" location="internal/cmd/profile.go:391">
P2: Profile listing reports `authenticated: false` when the credential store is inaccessible, masking keyring and storage failures as a sign-out. Propagate non-`ErrCredentialsNotFound` errors instead of collapsing them into the unauthenticated state.</violation>
</file>

<file name="internal/config/profiles.go">

<violation number="1" location="internal/config/profiles.go:192">
P2: `ApplyProfile` rejects the profile URL before honoring higher-precedence overrides. A malformed or HTTP profile therefore makes `HEY_BASE_URL` and `--base-url` fail instead of using their safe URL; validate after the override is known.</violation>
</file>

<file name="internal/auth/mcp.go">

<violation number="1" location="internal/auth/mcp.go:49">
P2: MCP profile deletion can delete a credential without revoking its grant because `Revoke` ignores the stored credential's `TokenEndpoint`. Use `creds.TokenEndpoint`, falling back to `m.baseURL + "/oauth/tokens"`, for the DELETE URL so migrated or non-default grants are revoked at their issuing endpoint.

(Based on your team's feedback about using the stored OAuth token endpoint for revocation.) [e23596d0-4cad-4f93-ac20-e04bcd88950d]</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread internal/cmd/profile.go Resolved
Comment thread internal/config/profiles.go Resolved
Comment thread internal/config/profiles.go Resolved
Comment thread internal/cmd/doctor.go Resolved
Comment thread internal/config/local_trust.go Resolved
Comment thread internal/cmd/profile.go Resolved
Comment thread internal/cmd/profile.go Resolved
Comment thread AGENTS.md Resolved
Comment thread internal/cmd/profile_select.go Resolved
Comment thread internal/config/profiles.go Resolved
Review found that a profile's credential, keyed by its name, went to
whatever server HEY_BASE_URL or --base-url named, a lone profile being
chosen by itself; that a trusted repository entry could move a global
profile to another server and take its sign-in there; and that
profile delete sent the refresh token to the profile's base_url
unchecked, plain HTTP or a server that never issued it.

Now an override naming another server is refused, naming both; a
repository entry that would move a global profile is ignored with a
warning, and the trust prompt shows each local profile's server;
delete revokes only at a server hey sends credentials to whose token
endpoint issued the sign-in. A repository's account_id no longer
follows a profile to another server, and shell completion offers -P's
names with several profiles and none selected.

Verified by make check; each new test fails with its guard removed.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 existing issue remains and 1 new issue found across 8 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/config/profiles.go">

<violation number="1" location="internal/config/profiles.go:211">
P2: `filesOrigin` can be the environment-overridden server rather than the repository file's `base_url`. With a trusted local account for app, `HEY_BASE_URL=staging`, and an account-less staging profile, this preserves the local account on staging; compare against the local file's server when it is set.</violation>
</file>

Requires human review: Auto-approval blocked because this review re-detected 3 unresolved issues already reported by Cubic.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread internal/config/profiles.go Resolved
The second review and cubic's found ways a profile's sign-in could still
reach the wrong server or be lost: a stored token followed an edited
base_url, a repository's account followed a profile to another server, a
repository-only profile could be "deleted" and come straight back, delete
revoked at whatever server the config named, an access-only sign-in was
never revoked, a bad account_id read as none, and the guard on
--base-url also refused `hey config set base_url`.

Now a profile's stored sign-in is refused when its token endpoint is on
another server, and a pasted token records its server. A repository's
account_id applies only on the files' own server. Delete refuses a
profile set only by a repository config, builds the MCP manager even
when the client id is unreadable, and revokes only at the server that
issued the sign-in, access-only ones included, warning otherwise. The
--base-url check moved to root, so config set works under a profile.
account_id must be a string or number. A new profile becomes the default
only when no other entry with a base_url exists. Completion survives a
refused profile, the config subcommands but show run with none selected,
doctor's profile line is sanitized, the trust prompt lists each local
profile's server and account, and status hints name `-P <profile>`.

Verified: each new test fails with its guard disabled and passes with
it; make check passes.

Rejected: re-reading the credential under a lock across revoke and
delete. `hey auth logout --mcp` has the same shape, and the worst
interleaving leaves one rotated grant unrevoked at its own server.
The third review found that `hey auth logout --mcp -P <profile>` still
sent a profile's refresh token to whatever server its base_url named
now: the issuer check ran on reload, but Revoke loaded the credential
itself. It also found that delete reported success for a profile a
trusted repository config also sets, which then came back signed out;
that a failed `profile create` forgot its sign-in without revoking it;
that the trust prompt printed a numeric account as 4.56789e+06; and that
an empty account_id stopped every command from loading.

Now Revoke refuses a profile credential whose token endpoint is on
another server or not recorded, so every caller is covered. Delete
refuses a profile a trusted repository config also sets, naming the
file. A failed create revokes before it forgets. The trust prompt prints
the account whole, and an empty account_id reads as none.

Verified: each new test fails with its fix disabled and passes with it;
make check passes.
The fourth review found that delete refused a profile whenever a trusted
repository config had a key of its name, even one with no base_url,
which the loader skips and so can't bring the profile back; and that the
trust prompt printed ", mail account " for an empty account_id, which
now reads as none.

Now delete refuses only for a repository entry with a base_url, and the
prompt leaves an empty account out.

Verified: both tests fail with the old checks and pass with the new;
make check passes.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 7 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread docs/cli.md Resolved
A trusted repository entry with no base_url is no profile, so it does not
block a delete; the docs said any entry did. Docs only.
@jeremy
jeremy marked this pull request as ready for review November 12, 2026 05:35
@jeremy
jeremy requested a review from a team as a code owner November 12, 2026 05:35
Copilot AI balanced review requested due to automatic review settings November 12, 2026 05:35
Comment thread internal/auth/auth.go Resolved
Comment thread internal/config/config.go Resolved
Comment thread internal/cmd/profile_select.go Resolved
Comment thread internal/cmd/root.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The credential refresh path can adopt another server’s token without validating its issuer and send it to the original server, creating a critical credential-leak risk.

4 open issues
What changed in this PR

Adds named profiles to hey-cli so users can keep multiple HEY sign-ins and server configurations on one computer, while preserving behavior when no profiles are configured.

Changes:

  • Adds profile management commands and selection through -P, HEY_PROFILE, and defaults.
  • Separates profile credentials and caches, with server-binding checks and repository-config trust controls.
  • Adds profile-aware output, documentation, help, and regression tests.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
internal/​config/​profiles.go Implements profile parsing, merging, selection, and persistence.
internal/​config/​profiles_test.go Tests profile configuration and compatibility.
internal/​config/​local_trust.go Includes profile settings in local trust fingerprints.
internal/​config/​config.go Integrates profiles into configuration loading and saving.
internal/​cmd/​sdk.go Separates HTTP caches by profile.
internal/​cmd/​root.go Integrates profile selection, authentication, and response metadata.
internal/​cmd/​profile.go Adds profile management commands and credential cleanup.
internal/​cmd/​profile_test.go Tests profile commands, isolation, trust, and recovery.
internal/​cmd/​profile_select.go Implements selection precedence, picker, completion, and login hints.
internal/​cmd/​mcp_auth.go Selects profile-specific MCP credentials.
internal/​cmd/​local_config_trust.go Displays profile settings in trust prompts.
internal/​cmd/​help.go Adds profiles to command and flag help.
internal/​cmd/​help_topics.go Documents profile environment and account precedence.
internal/​cmd/​help_test.go Updates expected root help.
internal/​cmd/​doctor.go Reports the active profile in diagnostics.
internal/​cmd/​config.go Shows the active profile and selection source.
internal/​cmd/​auth.go Adds profile-aware status and login guidance.
internal/​cmd/​accounts.go Explains profile-specific account overrides.
internal/​auth/​mcp.go Guards profile credential revocation by issuer.
internal/​auth/​auth.go Adds profile credential keys, issuer validation, and login commands.
docs/​cli.md Documents profile workflows and safeguards.
AGENTS.md Records profile architecture and isolation requirements.
.surface Adds profile commands and flags to the CLI snapshot.

Review effort: Balanced

Comment thread internal/auth/auth.go Resolved
Comment thread internal/cmd/profile.go Resolved
Comment thread internal/cmd/root.go Resolved
Comment thread internal/cmd/root.go Resolved
Copilot and Greptile found that a running manager, a TUI or a watch,
adopted whatever its profile's key held when it refreshed, so a profile
signed in again at another server by another process had that token
sent to the old one. They also found that version, upgrade, commands and
completion still selected a profile and failed on a refused or unknown
one; that a failed answer carried no meta.profile; that delete dropped a
profile whose credential the store could not read or clear, leaving it
unreachable; and that `hey account use` under a repository-only profile
said "not found", and under a repository entry that sets an account
gave no word that the entry still wins.

Now the refresh path checks the stored credential's issuer before it
caches or uses it. Commands that read no server skip profile selection.
A failed JSON answer names the active profile, on a copy of the error.
Delete stops before unregistering when the credential can't be removed.
`hey account use` names the repository file for a repository-only
profile, and says when that file's entry remains higher precedence.

Verified: each new test fails with its fix disabled and passes with it;
make check passes.
The fifth review found the completion people run is shell-completion,
not Cobra's hidden completion, so a broken or unchosen profile still
broke the line a shell's rc sources at every start. It now skips
selection with the other commands that read no server. The account
notice sanitizes the repository path, and a delete stopped by the
credential store says how to check it.

Verified: the shell-completion case fails without the fix and passes
with it; make check passes.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 8 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread internal/cmd/accounts.go Resolved
Comment thread internal/cmd/profile_select.go Resolved
Comment thread internal/cmd/profile.go Resolved
Comment thread internal/cmd/profile_test.go Resolved
Comment thread internal/auth/auth_test.go Resolved
hey doctor checks the selected profile's credential store, so the hint
names it. The comment on commands that skip selection says what they
skip. Verified: make check passes.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 8 files (changes from recent commits).

Requires human review: Auto-approval blocked because this review re-detected 3 unresolved issues already reported by Cubic.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread internal/config/config.go Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread internal/cmd/profile.go Resolved
Comment thread internal/cmd/profile_select.go Resolved
cubic found that a credential store failure during delete came back as
an auth error with the sign-in hint, that the doctor hint dropped a
profile name the creation pattern refuses, and that one repository path
in an account error went unsanitized. Now delete returns the store's own
error, a refused keyring's hint included, and otherwise names the
profile for hey doctor, quoted where it needs it; the path is sanitized.
The refresh test primes the manager's cache first, so it also fails if a
refused record leaves the cached token usable.

Verified: the refresh test fails with the cache left in place; make
check passes.
@jeremy

jeremy commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

Review state at 9abec28: CI green, 38 review threads answered and resolved, none open.

Fresh review. Six rounds: the first over the whole diff, each later one over the fixes since. Rounds 1, 2, 3 and 5 found a High or a Medium, all fixed; rounds 4 and 6 found none, and their Lows are fixed too.

Declined, with reasons in each thread:

Reviewers. Copilot and Greptile reported on 5a8fd09, the head marked ready; their 8 findings are fixed. cubic reported through e1bbba6 but not yet on 9abec28, the last push, which fixes its findings from e1bbba6.

@jeremy
jeremy merged commit 2de736c into main Nov 12, 2026
35 checks passed
@jeremy
jeremy deleted the profiles branch November 12, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants