diff --git a/scripts/lib/managed-extensions.py b/scripts/lib/managed-extensions.py index 4388a41..6b05bf0 100644 --- a/scripts/lib/managed-extensions.py +++ b/scripts/lib/managed-extensions.py @@ -11,6 +11,7 @@ import stat import subprocess import sys import tempfile +import time import urllib.request import zipfile @@ -267,6 +268,16 @@ class Manager: else: run_command(arguments, self.summary, self.home) + def refresh_marketplace(self, client, name): + # Windows briefly locks a fresh marketplace clone (antivirus, indexers, open sessions), so the CLI's rename fails with EPERM. + for attempt in range(3): + try: + return self.command([client, 'plugin', 'marketplace', 'update', name]) + except ValueError as error: + if attempt == 2 or not re.search(r'\b(EPERM|EBUSY|EACCES)\b|try again', str(error), re.I): + raise + time.sleep(5 * (attempt + 1)) + def remove(self, record): if self.dry_run: if not self.summary: @@ -356,7 +367,7 @@ class Manager: self.save() return if item is not None and client == 'claude': - self.command([client, 'plugin', 'marketplace', 'update', selector.rsplit('@', 1)[1]]) + self.refresh_marketplace(client, selector.rsplit('@', 1)[1]) self.command([client, 'plugin', 'update', selector]) if item.get('enabled') is False: self.command([client, 'plugin', 'enable', selector]) @@ -376,7 +387,7 @@ class Manager: else: self.command([client, 'plugin', 'marketplace', 'add', marketplace]) if client == 'claude': - self.command([client, 'plugin', 'marketplace', 'update', selector.rsplit('@', 1)[1]]) + self.refresh_marketplace(client, selector.rsplit('@', 1)[1]) arguments = [client, 'plugin', 'install', selector, '--scope', 'user'] if client == 'claude' else [client, 'plugin', 'add', selector] self.command(arguments) resolved_version = None diff --git a/scripts/tests/test-managed-extensions.py b/scripts/tests/test-managed-extensions.py index bfc12ce..d88fe9e 100644 --- a/scripts/tests/test-managed-extensions.py +++ b/scripts/tests/test-managed-extensions.py @@ -203,8 +203,27 @@ class ManagedExtensionTests(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'refresh failed'): self.manager().sync([plugin()], ['claude'], {'Example'}) self.assertEqual(['claude', 'plugin', 'marketplace', 'update', 'test'], command.call_args.args[0]) + self.assertEqual(1, command.call_count) self.assertEqual([], self.manager().state['resources']) + def test_claude_marketplace_refresh_retries_transient_lock(self): + failures = [ValueError("EPERM: operation not permitted, rename 'caveman..clone'")] * 2 + def command(args, summary, home=None): + if args[2:4] == ['marketplace', 'update'] and failures: + raise failures.pop() + return self.command(args, summary, home) + with patch.object(extensions, 'run_command', side_effect=command), patch.object(extensions.time, 'sleep') as sleep: + self.manager().sync([plugin()], ['claude'], {'Example'}) + self.assertEqual(2, sleep.call_count) + self.assertEqual(['claude', 'plugin', 'install', 'example@test', '--scope', 'user'], self.commands[-1]) + self.assertEqual(['Example'], [r['name'] for r in self.manager().state['resources']]) + + def test_claude_marketplace_refresh_gives_up_after_three_transient_failures(self): + with patch.object(extensions, 'run_command', side_effect=ValueError('EBUSY')) as command, patch.object(extensions.time, 'sleep'): + with self.assertRaisesRegex(ValueError, 'EBUSY'): + self.manager().sync([plugin()], ['claude'], {'Example'}) + self.assertEqual(3, command.call_count) + @unittest.skipUnless(os.name == 'nt', 'Windows hook compatibility') def test_owned_caveman_hook_repair_runs_in_powershell_and_is_idempotent(self): entry = dict(plugin('Caveman'), codex_plugin='caveman@thinkhome-caveman')