Skip to content

Commit 81bf741

Browse files
github-actions[bot]CopilotCopilot
authored
[bug-fix] Fix bundle-update-force-mislead: add refresh() to DefaultPrimitiveInstaller (#3452)
* Fix bundle-update-force-mislead: add refresh() to DefaultPrimitiveInstaller Apply the remediation from the bug assessment on issue #3424. DefaultPrimitiveInstaller lacked a refresh() method, causing _refresh_component() to fall back to install(), which calls ExtensionManager.install_from_directory() with force=False. This raised ExtensionError with a leaked --force hint that bundle update does not support, leaving users with no valid recovery path. Fix: add refresh() to each kind manager (ExtensionKindManager and PresetKindManager delegate to _do_install(force=True); WorkflowKindManager and StepKindManager delegate to install() as their callables are idempotent). DefaultPrimitiveInstaller.refresh() dispatches to the kind manager's refresh(). PresetManager.install_from_directory() and install_from_zip() gain a force parameter that removes the existing preset before reinstalling, mirroring ExtensionManager's force semantics. Refs #3424 Assisted-by: GitHub Copilot (model: claude-sonnet-4.6, autonomous) Co-authored-by: Copilot <[email protected]> * Address review feedback on primitives.py and test_bundler_primitives.py - Replace ... with pass in _KindManager Protocol method stubs - Conditionally pass force= keyword only when force=True in _PresetKindManager - Fix _StepKindManager.refresh() to remove step before re-installing - Rename test to reflect actual assertion (refresh succeeds + force=True) - Remove duplicate install_bundle import Assisted-by: GitHub Copilot (model: claude-sonnet-4.5, autonomous) * fix: add missing role/effective_integration to InstallPlan in _plan() and remove redundant import - Remove duplicate `DefaultPrimitiveInstaller` import inside test body (already imported at module scope on line 15) - Add required `role` and `effective_integration` fields to `InstallPlan` constructor in `_plan()` helper to prevent TypeError at runtime Assisted-by: GitHub Copilot (model: claude-sonnet-4.6, autonomous) * fix: address latest PR review comments Assisted-by: GitHub Copilot (model: gpt-5.6-terra, autonomous) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]>
1 parent 4803a22 commit 81bf741

4 files changed

Lines changed: 197 additions & 12 deletions

File tree

‎src/specify_cli/bundler/services/adapters.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ def install(self, project_root: Path, component: ComponentRef) -> None:
227227
manager = self._manager_for(component, project_root)
228228
manager.install(component)
229229

230+
def refresh(self, project_root: Path, component: ComponentRef) -> None:
231+
manager = self._manager_for(component, project_root)
232+
manager.refresh(component)
233+
230234
def remove(self, project_root: Path, component: ComponentRef) -> None:
231235
manager = self._manager_for(component, project_root)
232236
manager.remove(component)

‎src/specify_cli/bundler/services/primitives.py‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,17 @@ def _bundled_manifest_version(manifest_path: Path, root_key: str) -> str | None:
8585

8686

8787
class _KindManager(Protocol):
88-
def is_installed(self, component: ComponentRef) -> bool: ...
88+
def is_installed(self, component: ComponentRef) -> bool:
89+
pass
8990

90-
def install(self, component: ComponentRef) -> None: ...
91+
def install(self, component: ComponentRef) -> None:
92+
pass
9193

92-
def remove(self, component: ComponentRef) -> None: ...
94+
def refresh(self, component: ComponentRef) -> None:
95+
pass
96+
97+
def remove(self, component: ComponentRef) -> None:
98+
pass
9399

94100

95101
def primitive_manager(
@@ -151,6 +157,12 @@ def is_installed(self, component: ComponentRef) -> bool:
151157
return False
152158

153159
def install(self, component: ComponentRef) -> None:
160+
self._do_install(component, force=False)
161+
162+
def refresh(self, component: ComponentRef) -> None:
163+
self._do_install(component, force=True)
164+
165+
def _do_install(self, component: ComponentRef, *, force: bool) -> None:
154166
from ... import get_speckit_version
155167
from ..._assets import _locate_bundled_preset
156168

@@ -168,7 +180,9 @@ def install(self, component: ComponentRef) -> None:
168180
component.version,
169181
_bundled_manifest_version(bundled / "preset.yml", "preset"),
170182
)
171-
self._manager.install_from_directory(bundled, speckit_version, priority)
183+
self._manager.install_from_directory(
184+
bundled, speckit_version, priority, **({"force": True} if force else {})
185+
)
172186
return
173187

174188
if not self._allow_network:
@@ -194,7 +208,9 @@ def install(self, component: ComponentRef) -> None:
194208
)
195209
zip_path = catalog.download_pack(component.id)
196210
try:
197-
self._manager.install_from_zip(zip_path, speckit_version, priority)
211+
self._manager.install_from_zip(
212+
zip_path, speckit_version, priority, **({"force": True} if force else {})
213+
)
198214
finally:
199215
with contextlib.suppress(Exception):
200216
if zip_path.exists():
@@ -224,6 +240,12 @@ def is_installed(self, component: ComponentRef) -> bool:
224240
return False
225241

226242
def install(self, component: ComponentRef) -> None:
243+
self._do_install(component, force=False)
244+
245+
def refresh(self, component: ComponentRef) -> None:
246+
self._do_install(component, force=True)
247+
248+
def _do_install(self, component: ComponentRef, *, force: bool) -> None:
227249
from ... import get_speckit_version
228250
from ..._assets import _locate_bundled_extension
229251

@@ -242,7 +264,7 @@ def install(self, component: ComponentRef) -> None:
242264
_bundled_manifest_version(bundled / "extension.yml", "extension"),
243265
)
244266
self._manager.install_from_directory(
245-
bundled, speckit_version, priority=priority
267+
bundled, speckit_version, priority=priority, force=force
246268
)
247269
return
248270

@@ -272,7 +294,7 @@ def install(self, component: ComponentRef) -> None:
272294
zip_path = catalog.download_extension(component.id)
273295
try:
274296
self._manager.install_from_zip(
275-
zip_path, speckit_version, priority=priority
297+
zip_path, speckit_version, priority=priority, force=force
276298
)
277299
finally:
278300
with contextlib.suppress(Exception):
@@ -318,6 +340,11 @@ def install(self, component: ComponentRef) -> None:
318340
lambda: workflow_add(component.id),
319341
)
320342

343+
def refresh(self, component: ComponentRef) -> None:
344+
# workflow_add is idempotent for already-installed workflows; delegate
345+
# to the standard install path which handles version refresh correctly.
346+
self.install(component)
347+
321348
def _assert_pinned_version(self, component: ComponentRef) -> None:
322349
if not component.version:
323350
return
@@ -378,6 +405,35 @@ def install(self, component: ComponentRef) -> None:
378405
lambda: workflow_step_add(component.id),
379406
)
380407

408+
def refresh(self, component: ComponentRef) -> None:
409+
# Preserve an existing step until we've validated we can perform refresh.
410+
# For already-installed steps, keep a backup and restore it if the
411+
# remove+reinstall path fails.
412+
if not (self._allow_network and self.is_installed(component)):
413+
self.install(component)
414+
return
415+
416+
import shutil
417+
import tempfile
418+
419+
step_dir = self._registry.steps_dir / component.id
420+
metadata = self._registry.get(component.id)
421+
backup_dir = Path(tempfile.mkdtemp(prefix="speckit-step-refresh-")) / component.id
422+
try:
423+
if step_dir.exists():
424+
shutil.copytree(step_dir, backup_dir)
425+
self.remove(component)
426+
try:
427+
self.install(component)
428+
except BundlerError:
429+
if backup_dir.exists():
430+
shutil.copytree(backup_dir, step_dir, dirs_exist_ok=True)
431+
if metadata is not None and not self._registry.is_installed(component.id):
432+
self._registry.add(component.id, metadata)
433+
raise
434+
finally:
435+
shutil.rmtree(backup_dir.parent, ignore_errors=True)
436+
381437
def remove(self, component: ComponentRef) -> None:
382438
from ... import workflow_step_remove
383439

‎src/specify_cli/presets/__init__.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,13 +3359,15 @@ def install_from_directory(
33593359
source_dir: Path,
33603360
speckit_version: str,
33613361
priority: int = 10,
3362+
force: bool = False,
33623363
) -> PresetManifest:
33633364
"""Install preset from a local directory.
33643365
33653366
Args:
33663367
source_dir: Path to preset directory
33673368
speckit_version: Current spec-kit version
33683369
priority: Resolution priority (lower = higher precedence, default 10)
3370+
force: If True and the preset is already installed, remove it first
33693371
33703372
Returns:
33713373
Installed preset manifest
@@ -3384,10 +3386,12 @@ def install_from_directory(
33843386
self.check_compatibility(manifest, speckit_version)
33853387

33863388
if self.registry.is_installed(manifest.id):
3387-
raise PresetError(
3388-
f"Preset '{manifest.id}' is already installed. "
3389-
f"Use 'specify preset remove {manifest.id}' first."
3390-
)
3389+
if not force:
3390+
raise PresetError(
3391+
f"Preset '{manifest.id}' is already installed. "
3392+
f"Use 'specify preset remove {manifest.id}' first."
3393+
)
3394+
self.remove(manifest.id)
33913395

33923396
dest_dir = self.presets_dir / manifest.id
33933397
if dest_dir.exists():
@@ -3535,13 +3539,15 @@ def install_from_zip(
35353539
zip_path: Path,
35363540
speckit_version: str,
35373541
priority: int = 10,
3542+
force: bool = False,
35383543
) -> PresetManifest:
35393544
"""Install preset from ZIP file.
35403545
35413546
Args:
35423547
zip_path: Path to preset ZIP file
35433548
speckit_version: Current spec-kit version
35443549
priority: Resolution priority (lower = higher precedence, default 10)
3550+
force: If True and the preset is already installed, remove it first
35453551
35463552
Returns:
35473553
Installed preset manifest
@@ -3573,7 +3579,7 @@ def install_from_zip(
35733579
"No preset.yml found in ZIP file"
35743580
)
35753581

3576-
return self.install_from_directory(pack_dir, speckit_version, priority)
3582+
return self.install_from_directory(pack_dir, speckit_version, priority, force=force)
35773583

35783584
def remove(self, pack_id: str) -> bool:
35793585
"""Remove an installed preset.

‎tests/unit/test_bundler_primitives.py‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
_WorkflowKindManager,
2121
primitive_manager,
2222
)
23+
from tests.bundler_helpers import valid_manifest_dict
2324

2425

2526
def _component(kind: str, cid: str = "x") -> ComponentRef:
@@ -215,3 +216,121 @@ def test_bundled_preset_pin_match_installs(tmp_path: Path, monkeypatch):
215216
manager.install(ComponentRef(kind="presets", id="my-preset", version="1.0.0"))
216217
manager.install(ComponentRef(kind="presets", id="my-preset", version=None))
217218
assert len(called) == 2
219+
220+
221+
def test_extension_refresh_calls_install_with_force(tmp_path: Path, monkeypatch):
222+
"""_ExtensionKindManager.refresh() must pass force=True to install_from_directory
223+
so an already-installed extension is overwritten instead of raising an error."""
224+
import specify_cli._assets as assets
225+
from specify_cli.extensions import ExtensionManager
226+
227+
bundled = _write_manifest(tmp_path / "ext", "extension", "1.0.0")
228+
monkeypatch.setattr(assets, "_locate_bundled_extension", lambda cid: bundled)
229+
force_values: list = []
230+
monkeypatch.setattr(
231+
ExtensionManager, "install_from_directory",
232+
lambda self, *a, **k: force_values.append(k.get("force", False)),
233+
)
234+
235+
manager = primitive_manager("extensions", tmp_path, allow_network=False)
236+
manager.refresh(ComponentRef(kind="extensions", id="my-ext"))
237+
assert force_values == [True], "refresh() must pass force=True"
238+
239+
240+
def test_preset_refresh_calls_install_with_force(tmp_path: Path, monkeypatch):
241+
"""_PresetKindManager.refresh() must pass force=True to install_from_directory
242+
so an already-installed preset is overwritten instead of raising an error."""
243+
import specify_cli._assets as assets
244+
from specify_cli.presets import PresetManager
245+
246+
bundled = _write_manifest(tmp_path / "preset", "preset", "1.0.0")
247+
monkeypatch.setattr(assets, "_locate_bundled_preset", lambda cid: bundled)
248+
force_values: list = []
249+
monkeypatch.setattr(
250+
PresetManager, "install_from_directory",
251+
lambda self, *a, **k: force_values.append(k.get("force", False)),
252+
)
253+
254+
manager = primitive_manager("presets", tmp_path, allow_network=False)
255+
manager.refresh(ComponentRef(kind="presets", id="my-preset"))
256+
assert force_values == [True], "refresh() must pass force=True"
257+
258+
259+
def test_default_installer_refresh_dispatches_to_kind_manager(tmp_path: Path, monkeypatch):
260+
"""DefaultPrimitiveInstaller.refresh() must call the kind manager's refresh(),
261+
which is the hook _refresh_component() will find — fixing the --force leak."""
262+
import specify_cli._assets as assets
263+
from specify_cli.extensions import ExtensionManager
264+
265+
bundled = _write_manifest(tmp_path / "ext", "extension", "1.0.0")
266+
monkeypatch.setattr(assets, "_locate_bundled_extension", lambda cid: bundled)
267+
force_values: list = []
268+
monkeypatch.setattr(
269+
ExtensionManager, "install_from_directory",
270+
lambda self, *a, **k: force_values.append(k.get("force", False)),
271+
)
272+
273+
installer = DefaultPrimitiveInstaller(allow_network=False)
274+
installer.refresh(tmp_path, _component("extensions", "my-ext"))
275+
assert force_values == [True], "DefaultPrimitiveInstaller.refresh() must use force=True"
276+
277+
278+
def test_refresh_succeeds_and_passes_force_true(tmp_path: Path, monkeypatch):
279+
"""Regression: bundle update (refresh=True) of an already-installed extension
280+
must succeed and pass force=True to install_from_directory."""
281+
from specify_cli.bundler.services.installer import install_bundle
282+
from specify_cli.bundler.models.manifest import BundleManifest
283+
import specify_cli._assets as assets
284+
from specify_cli.extensions import ExtensionManager
285+
286+
bundled = _write_manifest(tmp_path / "ext", "extension", "1.0.0")
287+
monkeypatch.setattr(assets, "_locate_bundled_extension", lambda cid: bundled)
288+
# Simulate refresh succeeding (force=True removes the duplicate-install guard)
289+
force_seen: list = []
290+
def _fake_install_from_directory(self, *a, **k):
291+
force_seen.append(k.get("force", False))
292+
self.registry.add("my-ext", {"version": "1.0.0"})
293+
294+
monkeypatch.setattr(
295+
ExtensionManager, "install_from_directory", _fake_install_from_directory
296+
)
297+
298+
raw = valid_manifest_dict(
299+
bundle={
300+
"id": "test-bundle",
301+
"name": "Test",
302+
"version": "1.0.0",
303+
"role": "developer",
304+
"description": "Test bundle",
305+
"author": "Spec Kit",
306+
"license": "MIT",
307+
},
308+
provides={
309+
"extensions": [{"id": "my-ext", "version": "1.0.0"}],
310+
"presets": [],
311+
"steps": [],
312+
"workflows": [],
313+
},
314+
)
315+
manifest = BundleManifest.from_dict(raw)
316+
installer = DefaultPrimitiveInstaller(allow_network=False)
317+
# First install
318+
install_bundle(tmp_path, _plan(manifest), installer, manifest=manifest)
319+
# Refresh (bundle update) — must not raise with --force hint
320+
install_bundle(tmp_path, _plan(manifest), installer, manifest=manifest, refresh=True)
321+
# force=True must have been passed during the refresh call
322+
assert True in force_seen, "refresh path should have called install_from_directory with force=True"
323+
324+
325+
def _plan(manifest):
326+
from specify_cli.bundler.services.installer import InstallPlan
327+
from specify_cli.bundler.models.manifest import ComponentRef as CR
328+
329+
components = [CR(kind=c.kind, id=c.id) for c in manifest.components]
330+
return InstallPlan(
331+
bundle_id=manifest.bundle.id,
332+
version=manifest.bundle.version,
333+
role=manifest.bundle.role,
334+
effective_integration=None,
335+
components=components,
336+
)

0 commit comments

Comments
 (0)