fix(cli): close third review — token collision, migration, ownership
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m5s
CI/CD / lint (pull_request) Successful in 2m13s
CI/CD / test (pull_request) Successful in 1m20s
CI/CD / build (pull_request) Successful in 2m9s
CI/CD / smoke (pull_request) Failing after 2m44s
CI/CD / publish (pull_request) Has been skipped

Round 2 fixed the first review but introduced regressions of its own, all
of which only bite against state written by the previously installed build.

`config prime-agent`:
- Mint each credential under a unique `prime-agent-<stamp>` name again.
  `McpToken` is unique on (name, projectId) and revoke is a soft delete, so
  round 2's fixed `prime-agent` name could only ever be minted once per
  project — and the revoke-first ordering destroyed the working credential
  before discovering the mint would fail.
- Provision the credential BEFORE touching settings.json. Registering the
  new project unmounts the previously active one, so a failed mint must not
  be able to leave prime-agent with no working project at all. The command
  now aborts with settings.json untouched.
- Retire only the token this auth.json actually held, once its replacement
  is stored. Sweeping every `prime-agent*` token for the project would
  revoke the credential another install (or a custom --output run) is
  using; anything else that looks orphaned is reported, not deleted.
- Validate a pre-existing credential instead of trusting its presence: a
  revoked or expired token used to short-circuit provisioning and leave
  prime-agent broken while the command reported success. Matched by
  tokenPrefix against the project's active tokens, so the secret is never
  sent. Fails open when the API can't be consulted.
- Actually write auth.json 0600. `writeFile`'s mode is ignored for an
  existing file and prime-agent creates auth.json itself at 0644, so chmod
  after writing.
- Recognise the untagged mcpServers entries older CLIs wrote (canonical
  proxy URL + an `mcp:<name>` mcpctl PAT in auth.json) so a switch unmounts
  them instead of leaving two gateways live. Hand-configured servers have
  no such credential and are still preserved. Same rule in the `/mcpctl`
  switcher's active-project lookup.
- Add `--skip-marker`, and pass it from the `/mcpctl` switcher: the
  extension runs from whatever directory prime-agent was started in, and
  was silently re-scoping that repo's `.mcpctl-project`.

`skills sync --agent prime-agent`:
- Record ownership from the skill's own scope, not the syncing project's.
  Globals were being pinned to whichever project happened to sync them,
  after which every other project refused to update them forever.
- Never adopt legacy, ownership-less state into the current scope. Round 2
  did, which deleted the other project's skills on the first sync after
  upgrading. Such entries are attributed to the project that last wrote the
  state file, and left alone when that isn't the project syncing now.
- Close the overwrite-guard bypass: a sync with no project, or a global
  landing on a project-owned name, could still clobber and re-own a
  tracked skill.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017BMXdb2qZbPSh8Q7XpTyjB
This commit is contained in:
Michal
2026-08-08 12:01:52 +01:00
parent fa7055ac5e
commit 170dc06496
9 changed files with 629 additions and 81 deletions

View File

@@ -125,14 +125,19 @@ mcpctl config prime-agent --project monitoring
This:
1. Registers the proxy MCP gateway in `~/.prime/agent/settings.json` as
1. Provisions the project's bearer credential in `~/.prime/agent/auth.json`
(`mcp:monitoring`, written 0600) — either from `--token <pat>`, an existing
entry that is still active server-side, or a freshly minted project token.
This happens first: if no credential can be provisioned the command stops
here with a non-zero exit and leaves `settings.json` alone, so the project
you are currently on keeps working.
2. Registers the proxy MCP gateway in `~/.prime/agent/settings.json` as
`mcpServers.monitoring = { "type": "http", "url": "https://mcp.ad.itaz.eu/projects/monitoring/mcp" }`
(merging with any existing servers and preserving all other settings).
2. Provisions the project's bearer credential in `~/.prime/agent/auth.json`
(`mcp:monitoring`) — either from `--token <pat>`, an existing entry, or an
auto-minted project token.
(merging with any existing servers and preserving all other settings), and
unmounts the previously active mcpctl project so exactly one is live.
Servers you configured by hand are never touched.
3. Writes a `.mcpctl-project` marker (only if none exists higher up, and never
from `$HOME`) so later syncs resolve the project.
from `$HOME`) so later syncs resolve the project. Skip with `--skip-marker`.
4. Syncs the project's skills into `~/.prime/agent/skills/<name>/` as markdown
skills. The shared tree is ownership-tracked per project: it never deletes
another project's skills or an untracked hand-authored skill.
@@ -152,8 +157,13 @@ Skip individual steps as needed:
mcpctl config prime-agent --project monitoring --token mcpctl_pat_xxx # provide token, don't mint
mcpctl config prime-agent --project monitoring --skip-skills # don't sync skills
mcpctl config prime-agent --project monitoring --skip-extension # don't install /mcpctl switcher
mcpctl config prime-agent --project monitoring --skip-marker # don't touch .mcpctl-project here
```
The `/mcpctl` switcher runs with `--skip-extension --skip-marker`, so switching
projects from inside prime-agent never re-scopes whichever repository
prime-agent happened to be started in.
Preview the change without writing anything:
```bash