Skip to content

cloudpin switch: plan, confirm once, run, re-check (#14) - #102

Merged
paureis merged 30 commits into
mainfrom
feat/14-switch
Nov 4, 2026
Merged

paureis merged 30 commits into
mainfrom
feat/14-switch

Conversation

@paureis

@paureis paureis commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Closes #14

cloudpin switch [--yes] [--json] [--only <clouds>]: shows a plan for each pinned CLI that is not on its pinned account, asks once, runs the switch commands (argv, no shell), then re-checks. Design and sources: docs/superpowers/specs/2026-10-18-switch-design.md (owner decisions of 2026-10-18).

What it does

  • az account set --subscription <GUID>, gh auth switch --hostname --user, vercel teams switch <slug> (team ID mapped to slug), gcloud config set ... --quiet (with --configuration), kubectl config use-context (+ namespace via set-context --current when no context matches both).
  • AWS: matches profiles offline from the AWS config file (section names, sso_account_id, role_arn only; the credentials file is never read). The new cloudpin shell function applies AWS_PROFILE from a side file (CLOUDPIN_ENV_FILE, strict charset, no eval).
  • Missing logins, env-var overrides (GH_TOKEN, CLOUDSDK_CORE_PROJECT, HELM_KUBECONTEXT, ...), Supabase and Wrangler are manual lines; the rest still run.
  • Several candidates: numbered pick in a terminal, otherwise skipped with the list.
  • Agent hooks ask the human (deny where the agent can't ask), also through cloudpin exec -- cloudpin switch; --yes never changes that. MCP stays read-only.
  • README, SECURITY.md and DESIGN.md no longer say cloudpin never switches accounts.

Evidence

  • Typecheck and tests: 1072 passed, 16 skipped (bash/zsh real-shell tests, which run off Windows).
  • One review round (no must-fix; three fix-if-cheap findings and one doc wording) and one fix round, each fix with a test seen failing first; one fix independently mutation-tested.
  • Real accounts, read-only (switch --json without --yes, so nothing ran), in a temp folder pinned by cloudpin init: right pins all ok (exit 0); fake IDs all manual with the login hint (exit 1); a real but inactive Vercel team and Azure subscription each planned a run line (vercel teams switch <slug>, az account set --subscription <GUID>); active accounts unchanged afterwards. gcloud is unit-tested only (no test install on the dev machine).

🤖 Generated with Claude Code

Ported onto main (2026-11-04)

The branch was too stale to merge (8 conflicted files), so main was merged taking main's side and the PR's intent was ported as new commits. Beyond a straight port:

  • cloudpin exec is unwrapped in the parser, so what it starts is read as a call. This also fixes a gap on main: an agent's cloudpin exec -- bash -c 'gh ...' and cloudpin exec -- npx gh ... were allowed on the wrong account.
  • An unknown cloudpin subcommand asks (cloudpin $x), since it may be switch or use. Side effect: an agent's eval "$(cloudpin shell-init bash)" asks too.
  • npm exec cloudpin -- switch is read as cloudpin switch. --help counts only as the first word after switch, in the CLI and the hook alike.
  • planSwitch plans without running anything, ready for MCP: cloudpin_switch_plan and cloudpin_doctor tools (read-only) #112 part 2. No MCP tool here.
  • Plan: Windows variable names folded (Cloudflare: account variables with unknown values, and lowercase names on Windows #119, Windows: remaining variable-name case and folder gaps #136); manual lines for gcloud impersonation, a vercel.json scope, HELM_KUBEAPISERVER and HELM_NAMESPACE.
  • explain and MCP cloudpin_check report ask for an agent's cloudpin switch or cloudpin use <env>.
  • Cursor's matcher matches any case, so CLOUDPIN.CMD and AZ.CMD reach the hook. Existing installs get it when the hook is reinstalled.

Evidence for the port

  • npm run typecheck exit 0; npx vitest run: 85 files, 2629 passed, 19 skipped (before the last docs commit).
  • 19 mutations killed, including hook ask, parser unwrapping, folding, --yes, masking, and the unknown subcommand.
  • The built hook, end to end, 19 command forms against a made-up pin: all as expected. The same probe on main's build differs on 11, so the probe can fail.
  • Real accounts, read-only (switch --json, no --yes, GitHub and Azure): right pins ok, exit 0; made-up pins manual, exit 1; nothing ran and the active accounts were unchanged.

Not tested

No real switch has been run on the port (a real but inactive account's run line is from the earlier evidence above). gcloud is unit-tested only. Left as they are: Cursor's matcher is a text prefilter, so a name not literally on the line ("cloud"pin, -EncodedCommand) never reaches the hook there, as for every guarded CLI on main; node path/to/dist/cli.js switch isn't detected, like cloudpin use.

paureis and others added 14 commits October 18, 2026 19:15
Owner-approved decisions of 2026-10-18: agents are asked through the hook,
missing logins are skipped while the rest run, --json, AWS_PROFILE through a
wrapper side file, choose-or-skip for ambiguous matches, Supabase and
Wrangler stay manual, kube namespace preference. CLI facts checked against
each CLI's docs, help and source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lists the profiles in the AWS config file and the account each names,
from sso_account_id or the account field of role_arn only. Every other
value is dropped while reading; the credentials file is never read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
runCheck can fill a map with guard's trace per provider, so cloudpin
switch plans from the same resolution check used. The kubeconfig loader,
server normalising and the Vercel team listing are exported for reuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For each pinned provider not on its pin, plan the commands to run (az
account set, gh auth switch, vercel teams switch, gcloud config set,
kubectl use-context and set-context), a choice between several targets,
or a manual line naming the exact command or variable: logins, env-var
overrides, supabase link and the Wrangler config. AWS reads profiles
from the config file offline. Names with characters outside the allowed
set are quoted in a manual line and never run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shows the plan for every pinned provider, picks between several
candidates by number in a terminal, asks once, runs each command without
a shell (a failing command does not stop the others; a kube namespace
step is skipped when its use-context failed), writes AWS_PROFILE to
CLOUDPIN_ENV_FILE for the shell function, then re-checks. Exits 0 only
when every pinned, installed CLI matches. --json prints the plan, the
commands run with their exit codes, and the check report after, and
never prompts; without --yes it runs nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like cloudpin use <env>: agents that can ask (Claude Code, Copilot,
Cursor, Antigravity's force_ask) ask the user, naming the pins it would
switch to, read from the pin file without running a CLI; Codex and
Gemini CLI deny and tell the agent to ask the human to run it. --yes
does not change the decision; cloudpin switch --help is not asked about.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a mismatch the fix reads "<provider's switch hint> (or run cloudpin
switch)", in the block message and in check. Not for Supabase and
Wrangler, where switch only says what to do by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For cloudpin switch only, the function passes a temporary side file in
CLOUDPIN_ENV_FILE, then applies just AWS_PROFILE=<name> lines whose name
is [A-Za-z0-9._-]{1,64} and unsets AWS_DEFAULT_PROFILE. No eval or
Invoke-Expression; the file is deleted and cloudpin's exit code kept.
Every other subcommand passes straight through. The guarded functions
now look for the cloudpin executable with type -P (bash) or whence -p
(zsh), which a cloudpin function can't satisfy after an uninstall.
CLOUDPIN_ENV_FILE joins the variables an agent's line can't set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README and SECURITY.md no longer say cloudpin never switches accounts:
it switches only when you run cloudpin switch and confirm, and an
agent's attempt asks you first. Both note the AWS config read (profile
names, sso_account_id and role_arn only; never the credentials file).
README gains the command, the shell function and the exit code; DESIGN
gains the edge case and a Decisions row; CHANGELOG lists it under
Unreleased; AGENTS lists the new files; the plugin skill may suggest it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
botocore keeps the second word of any "profile..." header that splits
into two words, without checking the first (configloader.py
_parse_section); do the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After unwrapping `cloudpin exec`, an inner `cloudpin switch` or `cloudpin use`
went to guard, which ignores cloudpin, so the agent got no objection. The hook
now keeps unwrapping while the command is cloudpin and applies the same
confirmation at each level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hook built the cloudpin switch summary with the agent environment, which
drops user-only variables, while the real run honours them: with
CLOUDPIN_ENV=production on the line it named the staging pins. The summary now
uses the environment the command will have, and the command shown to the human
includes the line's user-only assignments. Guard's treatment of them for
agents is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With AWS_PROFILE and AWS_DEFAULT_PROFILE unset the AWS CLI uses [default], but
the plan treated no profile as in use, so a logged-out [default] naming the
pinned account planned AWS_PROFILE=default, which changes nothing. It now gets
the log-in line, as any other active profile does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README, SECURITY.md and DESIGN.md said cloudpin switches only when you run
cloudpin switch and confirm, but --yes skips the prompt. They now say it shows
the plan and asks first unless you pass --yes, and that an agent's attempt asks
the human through its hook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paureis paureis added this to the 1.3.0 milestone Oct 18, 2026
@paureis

paureis commented Oct 19, 2026

Copy link
Copy Markdown
Owner Author

Real end-to-end run on the maintainer's Vercel login (owner's explicit yes), built from bed5e28:

  1. A folder pinned to a team the login belongs to but is not current: cloudpin switch --yes --json ran vercel teams switch <slug> (code 0); after.result = match; Vercel's current team changed.
  2. Control: a folder pinned to the original team then failed cloudpin check (exit 1).
  3. cloudpin switch --yes in that folder switched back (exit 0, re-check ok); the original team is current again.

paureis and others added 13 commits November 4, 2026 17:46
Brings main (1.2.0, 1.2.1, and everything since) into the switch branch.
Every conflicted file takes main's side: AGENTS.md, CHANGELOG.md, README.md,
SECURITY.md, src/guard.ts, src/hooks/agents.ts, src/providers/kubernetes.ts,
src/shellwords.ts. The switch behaviour those files carried is ported back
on top in the commits that follow, so this merge alone does not pass tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The merge took main's src/providers/kubernetes.ts; switch-plan.ts reads the
kubeconfig through these three names, so they are exported again. Loaded stays
private: its one consumer only destructures the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
USER_ONLY_VARS moved to src/user-env.ts on main, so the switch side file's
variable goes there. A command line can't set it for an agent's guard check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Except for Supabase and Wrangler, where switch only says what to do by hand.
The hunk is on main's guard: the fix comes after the #165 unread-field check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The file is at the 5,000-token cap and the next commit adds to it. Only the
type moves; shellwords.ts re-exports it, so no caller changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…st one (#14)

`cloudpin exec -- bash -c '...'` and `cloudpin exec -- npx ...` reached guard as
an unguarded `bash` or `npx`, so the call inside was never checked. The switch
PR's loop in the hook only saw a directly nested `cloudpin`. Since exec starts
its command without a shell, findInvocations now reads those words as a call of
their own, so the hook and explain see the wrapped CLI whatever is around it.
This also closes the same gap on main for a wrapped az or gh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ads (#14)

The port of the switch PR's hook hunks, onto main's newer parsing: an agent's
`cloudpin switch` asks the human (deny where the hook can't ask), `--yes` or not.
The question names the pins, read with the line's own variables, with names
folded on Windows (#136) and flags masked (#134).

Beyond the straight port:
- A cloudpin call whose first word is only known when the line runs may be
  `switch`, so it asks too, rather than passing as "not cloudpin use".
- `npm exec cloudpin -- switch` runs `cloudpin switch` (npm drops the `--`), so a
  leading `--` is skipped.
- Help is only the first word after `switch`, in the command and the hook alike
  (#146); anywhere else it is an unknown argument and nothing runs.
- The exec loop is gone: findInvocations already reads what exec starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
src/switch-plan.ts was over the 5,000-token cap and has more to take. It keeps
the plan's shape and planProvider; the planners that ask a CLI move to
src/switch-planners.ts, and the two that read a config file (kubectl, AWS) to
src/switch-plan-files.ts. No behaviour changes; its test file is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…runs nothing (#14)

The plan read GH_TOKEN, CLOUDSDK_CORE_PROJECT, AWS_CONFIG_FILE and the rest in
one spelling. On Windows a name has no case and guard already folds it (#119,
#136), so a lowercase gh_token was seen by gh but missed by the plan, which then
ran a switch gh refuses. The environment is folded once, on the way in, and
everything after it (the check, the planners, the commands, the re-check, the
side file) sees those names.

The plan is now its own function, planSwitch, so it can be called without
running anything: the later MCP tool (#112 part 2) needs exactly that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The masking itself landed in the previous commit by accident (a scripted split
stopped before removing that hunk); this is its test. A CLI's stderr is the one
thing switch prints in words it didn't write, so its first line goes through
maskArgv like every other message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- gcloud: with impersonation on (#165) the identity is the service account, so
  `config set account` would change nothing; the line says what to clear instead.
- Vercel: a scope in the local config beats the active team (#127), so a team
  switch would change nothing; the line says so instead of running one.
- helm: HELM_KUBEAPISERVER and HELM_NAMESPACE decide its target too (#133), beside
  the HELM_KUBECONTEXT the design named, and use-context doesn't change them.

Tests also pin what stayed true: Wrangler's manual line still beats
CLOUDFLARE_ACCOUNT_ID and CF_ACCOUNT_ID (#103, #119), it gives an auth profile's
login, the block message doesn't send anyone to switch for it, and --only accepts
every current provider. No planner reads a flag, so targetFlags is untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…MAP (#14)

README and SECURITY keep main's wording plus the corrections that cloudpin no
longer "never switches accounts" and now reads the AWS config file's profile
names, sso_account_id and role_arn. The CHANGELOG entry moves under Unreleased.
ROADMAP marks the 1.3.0 line done. AGENTS.md lists the new source files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They reported "no command here that cloudpin guards" for cloudpin switch and
cloudpin use <env>, which the hook asks about. The MCP skill tells an agent to
run a command only on "allow", so the answer has to match the hook's. It uses
the hook's own function, so they can't drift. In a terminal the command asks by
itself, so nothing changes there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
paureis and others added 3 commits November 4, 2026 18:05
Cursor runs the hook only on lines its matcher finds, and the matcher was
lowercase, so CLOUDPIN.CMD switch (or AZ.CMD on a guarded command) never reached
the hook on Windows, where the shell runs it all the same. Reinstalling the hook
already moves an older matcher to the current one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paureis
paureis marked this pull request as ready for review November 4, 2026 23:17
@paureis
paureis merged commit 00f6ab9 into main Nov 4, 2026
5 checks passed
@paureis
paureis deleted the feat/14-switch branch November 4, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cloudpin switch: move every CLI to the pinned accounts

1 participant