import type { AuthProfile } from './auth.js'; import type { LocalAgentWorkspaceSnapshot } from './agent-workspace-sync.js'; import type { WorkspaceResourceSummary } from './hub-client.js'; import { readCapabilityWorkspaceRegistry, writeCapabilityWorkspaceRegistry, type CapabilityWorkspaceEntry, type CapabilityWorkspaceType, } from './capability-workspace.js'; export type CapabilityCloudStatus = 'workspace_missing'; export type CapabilityDeactivationReason = CapabilityCloudStatus | 'unequipped'; export interface CapabilityPresenceChange { type: CapabilityWorkspaceType; id: string; resource_id: string; reason?: CapabilityDeactivationReason; } export interface CapabilityPresenceResult { deactivated: CapabilityPresenceChange[]; restored: CapabilityPresenceChange[]; inactiveResourceIds: string[]; } function publisher(item: WorkspaceResourceSummary): 'official' | 'self' | 'community' { if (item.owner.isOfficial) return 'official'; return item.isCreatedByMe ? 'self' : 'community'; } function setCloudStatus(entry: CapabilityWorkspaceEntry, status: CapabilityCloudStatus | undefined): boolean { if (!entry.hub || entry.hub.cloudStatus === status) return false; if (status) entry.hub.cloudStatus = status; else delete entry.hub.cloudStatus; return true; } export function reconcileCapabilityWorkspacePresence( workspaceDir: string, profile: Pick, inventory: readonly WorkspaceResourceSummary[], snapshot?: LocalAgentWorkspaceSnapshot, ): CapabilityPresenceResult { const registry = readCapabilityWorkspaceRegistry(workspaceDir, profile); const selected = snapshot ? new Set(Object.values(snapshot.items).flat().map(item => item.resourceId)) : undefined; const inventoryByResource = new Map(inventory.map(item => [item.resourceId, { ...item, equipped: selected ? selected.has(item.resourceId) : item.equipped, }])); const deactivated: CapabilityPresenceChange[] = []; const restored: CapabilityPresenceChange[] = []; const inactiveResourceIds = new Set( [...inventoryByResource.values()].filter((item) => !item.equipped).map((item) => item.resourceId), ); let changed = false; for (const entry of Object.values(registry.entries)) { if (!entry.hub || entry.runtimeId === 'clawclaw') continue; let entryChanged = false; const item = inventoryByResource.get(entry.hub.resourceId); const wasEquipped = entry.selection === 'equipped'; const nextStatus: CapabilityCloudStatus | undefined = item ? undefined : 'workspace_missing'; if (nextStatus) inactiveResourceIds.add(entry.hub.resourceId); if (setCloudStatus(entry, nextStatus)) { entryChanged = true; if (nextStatus && wasEquipped) { deactivated.push({ type: entry.type, id: entry.runtimeId, resource_id: entry.hub.resourceId, reason: nextStatus, }); } } if (item) { const nextPublisher = publisher(item); if (entry.hub.workspaceItemId !== item.workspaceItemId || entry.hub.publisher !== nextPublisher) { entry.hub.workspaceItemId = item.workspaceItemId; entry.hub.publisher = nextPublisher; entryChanged = true; } } if (nextStatus) { if (entry.selection !== 'unequipped') { entry.selection = 'unequipped'; entryChanged = true; } if (entry.conflictChoice) { delete entry.conflictChoice; entryChanged = true; } } else if (item) { const nextSelection = item.equipped ? 'equipped' : entry.selection === 'pending' ? 'pending' : 'unequipped'; if (entry.selection !== nextSelection) { entry.selection = nextSelection; entryChanged = true; if (wasEquipped && !item.equipped) { deactivated.push({ type: entry.type, id: entry.runtimeId, resource_id: entry.hub.resourceId, reason: 'unequipped', }); } else if (!wasEquipped && item.equipped) { restored.push({ type: entry.type, id: entry.runtimeId, resource_id: entry.hub.resourceId, }); } } } if (entryChanged) { entry.updatedAt = new Date().toISOString(); changed = true; } } // Cloud-backed entries use the cloud choice; a default false on a newly // uploaded item is not evidence that the player answered the first prompt. if (changed) writeCapabilityWorkspaceRegistry(workspaceDir, profile, registry); return { deactivated, restored, inactiveResourceIds: [...inactiveResourceIds], }; } export function filterInactiveAgentWorkspaceSnapshot( workspaceDir: string, profile: Pick, snapshot: LocalAgentWorkspaceSnapshot | undefined, additionalResourceIds: readonly string[] = [], ): LocalAgentWorkspaceSnapshot | undefined { if (!snapshot) return undefined; const inactive = new Set(additionalResourceIds); for (const entry of Object.values(readCapabilityWorkspaceRegistry(workspaceDir, profile).entries)) { if ( entry.hub && (entry.hub.cloudStatus || entry.selection !== 'equipped') ) inactive.add(entry.hub.resourceId); } if (inactive.size === 0) return snapshot; const filtered = structuredClone(snapshot); for (const type of Object.keys(filtered.items) as Array) { filtered.items[type] = filtered.items[type].filter((item) => !inactive.has(item.resourceId)) as never; } return filtered; } /** A successful loadout remains authoritative even when the backpack endpoint * is offline. Absence here means unequipped, not deleted. */ export function reconcileCapabilityLoadout( workspaceDir: string, profile: Pick, snapshot: LocalAgentWorkspaceSnapshot, ): CapabilityPresenceResult { const registry = readCapabilityWorkspaceRegistry(workspaceDir, profile); const selected = new Set(Object.values(snapshot.items).flat().map(item => item.resourceId)); const result: CapabilityPresenceResult = { deactivated: [], restored: [], inactiveResourceIds: [] }; let changed = false; for (const entry of Object.values(registry.entries)) { if (!entry.hub || entry.runtimeId === 'clawclaw') continue; const equipped = selected.has(entry.hub.resourceId); if (!equipped) result.inactiveResourceIds.push(entry.hub.resourceId); const selection = equipped ? 'equipped' : entry.selection === 'pending' ? 'pending' : 'unequipped'; if (selection !== entry.selection) { const change = { type: entry.type, id: entry.runtimeId, resource_id: entry.hub.resourceId }; if (equipped) result.restored.push(change); else if (entry.selection === 'equipped') result.deactivated.push({ ...change, reason: 'unequipped' }); entry.selection = selection; changed = true; } } if (changed) writeCapabilityWorkspaceRegistry(workspaceDir, profile, registry); return result; }