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

@@ -207,6 +207,101 @@ describe('runPrimeAgentSkillsSync', () => {
expect(readFileSync(join(installRoot, 'x-skill', 'SKILL.md'), 'utf-8')).toBe('# version-a\n');
});
it('does not delete legacy, ownership-less state belonging to another project', async () => {
// State written by a CLI that predates the `project` field: the skill has
// no recorded owner and the file records projA as the last syncing project.
const legacyDir = join(installRoot, 'legacy-skill');
mkdirSync(legacyDir, { recursive: true });
writeFileSync(join(legacyDir, 'SKILL.md'), '# legacy\n', 'utf-8');
writeFileSync(statePath, JSON.stringify({
schemaVersion: 1,
lastSync: '2026-01-01T00:00:00.000Z',
lastSyncProject: 'projA',
skills: {
'legacy-skill': {
id: 'l-1', semver: '1.0.0', contentHash: 'sha256:l', scope: 'project',
installDir: legacyDir, files: {}, postInstallHash: null,
lastSyncedAt: '2026-01-01T00:00:00.000Z',
// note: no `project` field
},
},
}), 'utf-8');
// First sync after upgrading, for a *different* project.
const client = mockClient({ visible: [], full: {} });
const result = await runPrimeAgentSkillsSync({ project: 'projB', installRoot, statePath }, deps(client));
expect(result.removed).toEqual([]);
expect(existsSync(legacyDir)).toBe(true);
});
it('cleans up legacy state once the owning project syncs again', async () => {
const legacyDir = join(installRoot, 'legacy-skill');
mkdirSync(legacyDir, { recursive: true });
writeFileSync(join(legacyDir, 'SKILL.md'), '# legacy\n', 'utf-8');
writeFileSync(statePath, JSON.stringify({
schemaVersion: 1,
lastSync: '2026-01-01T00:00:00.000Z',
lastSyncProject: 'projA',
skills: {
'legacy-skill': {
id: 'l-1', semver: '1.0.0', contentHash: 'sha256:l', scope: 'project',
installDir: legacyDir, files: {}, postInstallHash: null,
lastSyncedAt: '2026-01-01T00:00:00.000Z',
},
},
}), 'utf-8');
const client = mockClient({ visible: [], full: {} });
const result = await runPrimeAgentSkillsSync({ project: 'projA', installRoot, statePath }, deps(client));
expect(result.removed).toContain('legacy-skill');
expect(existsSync(legacyDir)).toBe(false);
});
it('keeps global skills updatable after switching projects', async () => {
// A global installed while projA was active must not be pinned to projA —
// globals are visible from every project.
const gv = (hash: string) => [
{ id: 'g-1', name: 'shared-global', description: 'd', semver: '1.0.0', contentHash: hash, metadata: {}, scope: 'global' },
];
const gf = (hash: string, body: string) => ({
'g-1': { id: 'g-1', name: 'shared-global', description: 'd', semver: '1.0.0', contentHash: hash, content: body, files: {} },
});
const clientA = mockClient({ visible: gv('sha256:v1'), full: gf('sha256:v1', '# v1\n') });
await runPrimeAgentSkillsSync({ project: 'projA', installRoot, statePath }, deps(clientA));
expect((await loadState(statePath)).skills['shared-global']?.project).toBeNull();
// Switch to projB; the global has been updated server-side.
const clientB = mockClient({ visible: gv('sha256:v2'), full: gf('sha256:v2', '# v2\n') });
const resultB = await runPrimeAgentSkillsSync({ project: 'projB', installRoot, statePath }, deps(clientB));
expect(resultB.updated).toContain('shared-global');
expect(resultB.preserved).toEqual([]);
expect(readFileSync(join(installRoot, 'shared-global', 'SKILL.md'), 'utf-8')).toBe('# v2\n');
});
it('does not let a global-only sync clobber a project-owned skill', async () => {
const av = [
{ id: 'a-1', name: 'x-skill', description: 'd', semver: '1.0.0', contentHash: 'sha256:a', metadata: {}, scope: 'project' },
];
const af = { 'a-1': { id: 'a-1', name: 'x-skill', description: 'd', semver: '1.0.0', contentHash: 'sha256:a', content: '# version-a\n', files: {} } };
await runPrimeAgentSkillsSync({ project: 'projA', installRoot, statePath }, deps(mockClient({ visible: av, full: af })));
// A global of the same name shows up on a global-only sync.
const gv = [
{ id: 'g-9', name: 'x-skill', description: 'd', semver: '1.0.0', contentHash: 'sha256:g', metadata: {}, scope: 'global' },
];
const gf = { 'g-9': { id: 'g-9', name: 'x-skill', description: 'd', semver: '1.0.0', contentHash: 'sha256:g', content: '# global\n', files: {} } };
const empty = join(tmpDir, 'empty3');
mkdirSync(empty, { recursive: true });
const result = await runPrimeAgentSkillsSync({ cwd: empty, installRoot, statePath }, deps(mockClient({ visible: gv, full: gf })));
expect(result.preserved).toContain('x-skill');
expect(readFileSync(join(installRoot, 'x-skill', 'SKILL.md'), 'utf-8')).toBe('# version-a\n');
});
it('removes global orphans on a global-only sync', async () => {
// First sync a global skill.
const v = [