import { resolve } from 'node:path'; import type { AuthProfile } from './auth.js'; import { resolveLocalAgentWorkspacePath, type LocalAgentWorkspaceSnapshot, } from './agent-workspace-sync.js'; import { capabilitySelectionRef, capabilityWorkspacePath, readCapabilityWorkspaceRegistry, type CapabilityWorkspaceEntry, } from './capability-workspace.js'; import { CommandOutputError } from './command-error.js'; import { readSkillName } from './skill-host.js'; export interface SkillNameCandidate { name: string; id?: string; working_copy_path: string; selection_ref?: string; resource_id?: string; workspace_item_id?: string; } export interface SkillNameConflict { code: 'SKILL_NAME_CONFLICT'; name: string; blocking: boolean; candidates: SkillNameCandidate[]; active?: SkillNameCandidate; unavailable?: SkillNameCandidate[]; hint: string; } export const skillNameKey = (name: string): string => name.toLowerCase(); export function skillNameCandidate( workspaceDir: string, profile: Pick, entry: CapabilityWorkspaceEntry, ): SkillNameCandidate { const path = capabilityWorkspacePath(workspaceDir, profile, entry); return { name: readSkillName(path) ?? entry.runtimeId, id: entry.runtimeId, working_copy_path: path, selection_ref: capabilitySelectionRef(entry), ...(entry.hub ? { resource_id: entry.hub.resourceId, workspace_item_id: entry.hub.workspaceItemId } : {}), }; } export function sameSkill(left: SkillNameCandidate, right: SkillNameCandidate): boolean { // Missing Hub IDs are not an identity. Two local resources must remain distinct. if (left.resource_id || right.resource_id) { return !!left.resource_id && left.resource_id === right.resource_id; } return resolve(left.working_copy_path) === resolve(right.working_copy_path); } export function skillNameConflicts(candidates: readonly SkillNameCandidate[]): SkillNameConflict[] { const groups = new Map(); for (const candidate of candidates) { const name = skillNameKey(candidate.name); const group = groups.get(name) ?? []; const previous = group.findIndex(other => sameSkill(other, candidate)); if (previous < 0) group.push(candidate); else if (candidate.selection_ref) group[previous] = candidate; groups.set(name, group); } return [...groups].filter(([, group]) => group.length > 1).map(([name, group]) => ({ code: 'SKILL_NAME_CONFLICT', name, blocking: true, candidates: group, hint: 'Only one Skill with this name may be equipped. Choose which resource to keep and unequip the others using their selection_ref (or in Hub), then run ccl prepare. No Skill is renamed or replaced automatically.', })); } /** Also freeze old host names if someone edited SKILL.md before the next load. */ export function skillConflictNames(conflicts: readonly SkillNameConflict[]): Set { return new Set(conflicts.flatMap(conflict => [conflict.name, ...conflict.candidates.flatMap(candidate => [candidate.name, ...(candidate.id ? [candidate.id] : [])]), ]).map(skillNameKey)); } /** A supplied Hub snapshot is authoritative; local-only choices remain independent. */ export function selectedSkillNameConflicts( workspaceDir: string, profile: Pick, snapshot?: LocalAgentWorkspaceSnapshot, ): SkillNameConflict[] { const candidates: SkillNameCandidate[] = []; const selectedIds = new Set(snapshot?.items.skill.map(item => item.resourceId)); for (const item of snapshot?.items.skill ?? []) { if (!item.skillRootPath) continue; const path = resolveLocalAgentWorkspacePath(workspaceDir, profile, snapshot!, item.skillRootPath); const name = readSkillName(path); if (name) candidates.push({ name, working_copy_path: path, resource_id: item.resourceId, workspace_item_id: item.workspaceItemId }); } for (const entry of Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries)) { if (entry.type !== 'skill' || entry.selection !== 'equipped' || entry.hub?.cloudStatus) continue; if (snapshot && entry.hub && !selectedIds.has(entry.hub.resourceId)) continue; candidates.push(skillNameCandidate(workspaceDir, profile, entry)); } return skillNameConflicts(candidates); } export function throwSkillNameConflict(conflict: SkillNameConflict): never { throw new CommandOutputError(`SKILL_NAME_CONFLICT:${conflict.name}`, { error: 'skill_name_conflict', ...conflict, retryable: false, }); }