File size: 9,473 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | /**
* Background plugin autoupdate functionality
*
* At startup, this module:
* 1. First updates marketplaces that have autoUpdate enabled
* 2. Then checks all installed plugins from those marketplaces and updates them
*
* Updates are non-inplace (disk-only), requiring a restart to take effect.
* Official Anthropic marketplaces have autoUpdate enabled by default,
* but users can disable it per-marketplace.
*/
import { updatePluginOp } from '../../services/plugins/pluginOperations.js'
import { shouldSkipPluginAutoupdate } from '../config.js'
import { logForDebugging } from '../debug.js'
import { errorMessage } from '../errors.js'
import { logError } from '../log.js'
import {
getPendingUpdatesDetails,
hasPendingUpdates,
isInstallationRelevantToCurrentProject,
loadInstalledPluginsFromDisk,
} from './installedPluginsManager.js'
import {
getDeclaredMarketplaces,
loadKnownMarketplacesConfig,
refreshMarketplace,
} from './marketplaceManager.js'
import { parsePluginIdentifier } from './pluginIdentifier.js'
import { isMarketplaceAutoUpdate, type PluginScope } from './schemas.js'
/**
* Callback type for notifying when plugins have been updated
*/
export type PluginAutoUpdateCallback = (updatedPlugins: string[]) => void
// Store callback for plugin update notifications
let pluginUpdateCallback: PluginAutoUpdateCallback | null = null
// Store pending updates that occurred before callback was registered
// This handles the race condition where updates complete before REPL mounts
let pendingNotification: string[] | null = null
/**
* Register a callback to be notified when plugins are auto-updated.
* This is used by the REPL to show restart notifications.
*
* If plugins were already updated before the callback was registered,
* the callback will be invoked immediately with the pending updates.
*/
export function onPluginsAutoUpdated(
callback: PluginAutoUpdateCallback,
): () => void {
pluginUpdateCallback = callback
// If there are pending updates that happened before registration, deliver them now
if (pendingNotification !== null && pendingNotification.length > 0) {
callback(pendingNotification)
pendingNotification = null
}
return () => {
pluginUpdateCallback = null
}
}
/**
* Check if pending updates came from autoupdate (for notification purposes).
* Returns the list of plugin names that have pending updates.
*/
export function getAutoUpdatedPluginNames(): string[] {
if (!hasPendingUpdates()) {
return []
}
return getPendingUpdatesDetails().map(
d => parsePluginIdentifier(d.pluginId).name,
)
}
/**
* Get the set of marketplaces that have autoUpdate enabled.
* Returns the marketplace names that should be auto-updated.
*/
async function getAutoUpdateEnabledMarketplaces(): Promise<Set<string>> {
const config = await loadKnownMarketplacesConfig()
const declared = getDeclaredMarketplaces()
const enabled = new Set<string>()
for (const [name, entry] of Object.entries(config)) {
// Settings-declared autoUpdate takes precedence over JSON state
const declaredAutoUpdate = declared[name]?.autoUpdate
const autoUpdate =
declaredAutoUpdate !== undefined
? declaredAutoUpdate
: isMarketplaceAutoUpdate(name, entry)
if (autoUpdate) {
enabled.add(name.toLowerCase())
}
}
return enabled
}
/**
* Update a single plugin's installations.
* Returns the plugin ID if any installation was updated, null otherwise.
*/
async function updatePlugin(
pluginId: string,
installations: Array<{ scope: PluginScope; projectPath?: string }>,
): Promise<string | null> {
let wasUpdated = false
for (const { scope } of installations) {
try {
const result = await updatePluginOp(pluginId, scope)
if (result.success && !result.alreadyUpToDate) {
wasUpdated = true
logForDebugging(
`Plugin autoupdate: updated ${pluginId} from ${result.oldVersion} to ${result.newVersion}`,
)
} else if (!result.alreadyUpToDate) {
logForDebugging(
`Plugin autoupdate: failed to update ${pluginId}: ${result.message}`,
{ level: 'warn' },
)
}
} catch (error) {
logForDebugging(
`Plugin autoupdate: error updating ${pluginId}: ${errorMessage(error)}`,
{ level: 'warn' },
)
}
}
return wasUpdated ? pluginId : null
}
/**
* Update all project-relevant installed plugins from the given marketplaces.
*
* Iterates installed_plugins.json, filters to plugins whose marketplace is in
* the set, further filters each plugin's installations to those relevant to
* the current project (user/managed scope, or project/local scope matching
* cwd — see isInstallationRelevantToCurrentProject), then calls updatePluginOp
* per installation. Already-up-to-date plugins are silently skipped.
*
* Called by:
* - updatePlugins() below — background autoupdate path (autoUpdate-enabled
* marketplaces only; third-party marketplaces default autoUpdate: false)
* - ManageMarketplaces.tsx applyChanges() — user-initiated /plugin marketplace
* update. Before #29512 this path only called refreshMarketplace() (git
* pull on the marketplace clone), so the loader would create the new
* version cache dir but installed_plugins.json stayed on the old version,
* and the orphan GC stamped the NEW dir with .orphaned_at on next startup.
*
* @param marketplaceNames - lowercase marketplace names to update plugins from
* @returns plugin IDs that were actually updated (not already up-to-date)
*/
export async function updatePluginsForMarketplaces(
marketplaceNames: Set<string>,
): Promise<string[]> {
const installedPlugins = loadInstalledPluginsFromDisk()
const pluginIds = Object.keys(installedPlugins.plugins)
if (pluginIds.length === 0) {
return []
}
const results = await Promise.allSettled(
pluginIds.map(async pluginId => {
const { marketplace } = parsePluginIdentifier(pluginId)
if (!marketplace || !marketplaceNames.has(marketplace.toLowerCase())) {
return null
}
const allInstallations = installedPlugins.plugins[pluginId]
if (!allInstallations || allInstallations.length === 0) {
return null
}
const relevantInstallations = allInstallations.filter(
isInstallationRelevantToCurrentProject,
)
if (relevantInstallations.length === 0) {
return null
}
return updatePlugin(pluginId, relevantInstallations)
}),
)
return results
.filter(
(r): r is PromiseFulfilledResult<string> =>
r.status === 'fulfilled' && r.value !== null,
)
.map(r => r.value)
}
/**
* Update plugins from marketplaces that have autoUpdate enabled.
* Returns the list of plugin IDs that were updated.
*/
async function updatePlugins(
autoUpdateEnabledMarketplaces: Set<string>,
): Promise<string[]> {
return updatePluginsForMarketplaces(autoUpdateEnabledMarketplaces)
}
/**
* Auto-update marketplaces and plugins in the background.
*
* This function:
* 1. Checks which marketplaces have autoUpdate enabled
* 2. Refreshes only those marketplaces (git pull/re-download)
* 3. Updates installed plugins from those marketplaces
* 4. If any plugins were updated, notifies via the registered callback
*
* Official Anthropic marketplaces have autoUpdate enabled by default,
* but users can disable it per-marketplace in the UI.
*
* This function runs silently without blocking user interaction.
* Called from main.tsx during startup as a background job.
*/
export function autoUpdateMarketplacesAndPluginsInBackground(): void {
void (async () => {
if (shouldSkipPluginAutoupdate()) {
logForDebugging('Plugin autoupdate: skipped (auto-updater disabled)')
return
}
try {
// Get marketplaces with autoUpdate enabled
const autoUpdateEnabledMarketplaces =
await getAutoUpdateEnabledMarketplaces()
if (autoUpdateEnabledMarketplaces.size === 0) {
return
}
// Refresh only marketplaces with autoUpdate enabled
const refreshResults = await Promise.allSettled(
Array.from(autoUpdateEnabledMarketplaces).map(async name => {
try {
await refreshMarketplace(name, undefined, {
disableCredentialHelper: true,
})
} catch (error) {
logForDebugging(
`Plugin autoupdate: failed to refresh marketplace ${name}: ${errorMessage(error)}`,
{ level: 'warn' },
)
}
}),
)
// Log any refresh failures
const failures = refreshResults.filter(r => r.status === 'rejected')
if (failures.length > 0) {
logForDebugging(
`Plugin autoupdate: ${failures.length} marketplace refresh(es) failed`,
{ level: 'warn' },
)
}
logForDebugging('Plugin autoupdate: checking installed plugins')
const updatedPlugins = await updatePlugins(autoUpdateEnabledMarketplaces)
if (updatedPlugins.length > 0) {
if (pluginUpdateCallback) {
// Callback is already registered, invoke it immediately
pluginUpdateCallback(updatedPlugins)
} else {
// Callback not yet registered (REPL not mounted), store for later delivery
pendingNotification = updatedPlugins
}
}
} catch (error) {
logError(error)
}
})()
}
|