mirror of
https://github.com/github/spec-kit.git
synced 2026-07-07 06:35:06 +08:00
fix(cli): harden extension registration and discovery workflows (#2499)
* chore: update community catalog with latest extension versions
- Update memory-md from 0.7.9 to 0.8.0
- Update architecture-guard from 1.6.7 to 1.8.0
* fix(cli): harden extension registration with project-level tracking in extensions.yml
* test(cli): add comprehensive unit tests for extension registration logic
* chore: remove out-of-scope catalog changes
* refactor: address PR feedback for extension registration hardening
* fix: harden extension registration defensive logic and add comprehensive unregister_hooks tests
- Add dict guard to register_hooks() to handle corrupted extensions.yml (non-dict root)
- Add 5 comprehensive tests for unregister_hooks() workflow:
* Full workflow with hooks + installed list removal
* Resilience when config has no 'hooks' key
* Corrupted YAML handling
* Multiple extension scenarios
* All 11 tests passing
* fix: sanitize installed to strings, guard unregister_hooks dict, handle null hook values
- register_extension(): filter non-string entries from installed before sort
- register_hooks(): normalize hooks to {} when missing or not a dict
- unregister_hooks(): add isinstance(config, dict) guard before key checks
- unregister_hooks(): coerce null/scalar hook lists to [] before iteration
- tests: add 3 regression tests for no-hooks manifest, mixed-type installed, null hook values
- All 14 tests passing
* fix(cli): persist sanitization results and harden hook registration
* Harden extension registration to always persist sanitization results
* Hardening extension registration: support mapping entries, improve persistence, and fix update rollback
* fix(cli): harden extension update and unregistration workflows
* fix(cli): move update sentinels outside try block to prevent NameError on rollback
* fix(cli): sanitize hook event lists in register_hooks to prevent crashes
* fix(cli): deduplicate hook entries and harden rollback hooks-restore guards
* test(cli): add regression tests for extension update and rollback hardening
* fix(cli): deduplicate installed list by id in register_extension
* fix(cli): consolidate and harden extension update rollback logic
* fix(cli): initialize backup_registry_entry before try block to prevent UnboundLocalError on rollback
* fix(tests): return Path from download_extension mock and add Path import
* fix(cli): normalize get_project_config() return to dict; deduplicate in unregister_extension()
* fix(cli): normalize hooks/installed/settings in get_project_config(); use tmp_path-scoped zip in tests
* fix(cli): set modified=True on hook coercion in rollback; sanitize hook event values in get_project_config(); harden test assertions
* fix(cli): filter non-dict hook entries in get_project_config(); remove dead MISSING sentinel
* fix(cli): gate extensions.yml rollback on backup_hooks is not None; update stale comment
* fix(cli): move _AgentReg import outside try block; assert result.exception is None in tests
* fix(extensions): consistent key order in default config; deep-copy backup_installed
* test: fix misleading comment; assert exit_code==1 in rollback test
* test: clean up duplicate imports in hardening tests
* refactor(extensions): extract _sanitize_installed_list helper; strengthen hook unregister assertion
* fix(extensions): validate extension IDs in _sanitize_installed_list; clarify test comment
This commit is contained in:
@@ -4295,6 +4295,10 @@ def extension_update(
|
||||
failed_updates = []
|
||||
registrar = CommandRegistrar()
|
||||
hook_executor = HookExecutor(project_root)
|
||||
from .agents import CommandRegistrar as _AgentReg # used in backup and rollback paths
|
||||
|
||||
# UNSET sentinel: backup not yet captured (exception before backup step)
|
||||
UNSET = object()
|
||||
|
||||
for update in updates_available:
|
||||
extension_id = update["id"]
|
||||
@@ -4308,8 +4312,9 @@ def extension_update(
|
||||
backup_config_dir = backup_base / "config"
|
||||
|
||||
# Store backup state
|
||||
backup_registry_entry = None
|
||||
backup_hooks = None # None means no hooks key in config; {} means hooks key existed
|
||||
backup_registry_entry = None # None means registry entry not yet captured
|
||||
backup_installed = UNSET # Original installed list from extensions.yml
|
||||
backup_hooks = None # None means backup step 4 not yet reached; {} or {...} means backup was captured
|
||||
backed_up_command_files = {}
|
||||
|
||||
try:
|
||||
@@ -4334,8 +4339,7 @@ def extension_update(
|
||||
shutil.copy2(cfg_file, backup_config_dir / cfg_file.name)
|
||||
|
||||
# 3. Backup command files for all agents
|
||||
from .agents import CommandRegistrar as _AgentReg
|
||||
registered_commands = backup_registry_entry.get("registered_commands", {})
|
||||
registered_commands = backup_registry_entry.get("registered_commands", {}) if isinstance(backup_registry_entry, dict) else {}
|
||||
for agent_name, cmd_names in registered_commands.items():
|
||||
if agent_name not in registrar.AGENT_CONFIGS:
|
||||
continue
|
||||
@@ -4360,14 +4364,20 @@ def extension_update(
|
||||
shutil.copy2(prompt_file, backup_prompt_path)
|
||||
backed_up_command_files[str(prompt_file)] = str(backup_prompt_path)
|
||||
|
||||
# 4. Backup hooks from extensions.yml
|
||||
# Use backup_hooks=None to indicate config had no "hooks" key (don't create on restore)
|
||||
# Use backup_hooks={} to indicate config had "hooks" key with no hooks for this extension
|
||||
# 4. Backup hooks and installed list from extensions.yml
|
||||
# get_project_config() always normalizes installed->[] and hooks->{},
|
||||
# so no sentinel is needed to distinguish key-absent from key-empty.
|
||||
config = hook_executor.get_project_config()
|
||||
if "hooks" in config:
|
||||
backup_hooks = {} # Config has hooks key - preserve this fact
|
||||
for hook_name, hook_list in config["hooks"].items():
|
||||
ext_hooks = [h for h in hook_list if h.get("extension") == extension_id]
|
||||
if isinstance(config, dict):
|
||||
import copy
|
||||
# Deep-copy so nested mapping entries (e.g. version-pin dicts)
|
||||
# are not affected by in-place mutations during the update.
|
||||
backup_installed = copy.deepcopy(config.get("installed", []))
|
||||
backup_hooks = {}
|
||||
for hook_name, hook_list in config.get("hooks", {}).items():
|
||||
if not isinstance(hook_list, list):
|
||||
continue
|
||||
ext_hooks = [h for h in hook_list if isinstance(h, dict) and h.get("extension") == extension_id]
|
||||
if ext_hooks:
|
||||
backup_hooks[hook_name] = ext_hooks
|
||||
|
||||
@@ -4520,35 +4530,51 @@ def extension_update(
|
||||
original_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(backup_file, original_file)
|
||||
|
||||
# Restore hooks in extensions.yml
|
||||
# - backup_hooks=None means original config had no "hooks" key
|
||||
# - backup_hooks={} or {...} means config had hooks key
|
||||
config = hook_executor.get_project_config()
|
||||
if "hooks" in config:
|
||||
# Restore metadata in extensions.yml (hooks and installed list).
|
||||
# Only run if backup step 4 was reached (backup_hooks is not None);
|
||||
# otherwise we have no safe baseline to restore from and could corrupt
|
||||
# the config by removing pre-existing hooks.
|
||||
if backup_hooks is not None:
|
||||
config = hook_executor.get_project_config()
|
||||
if not isinstance(config, dict):
|
||||
config = {}
|
||||
|
||||
modified = False
|
||||
|
||||
if backup_hooks is None:
|
||||
# Original config had no "hooks" key; remove it entirely
|
||||
del config["hooks"]
|
||||
# 1. Restore hooks in extensions.yml
|
||||
if not isinstance(config.get("hooks"), dict):
|
||||
config["hooks"] = {}
|
||||
modified = True
|
||||
else:
|
||||
# Remove any hooks for this extension added by failed install
|
||||
for hook_name, hooks_list in config["hooks"].items():
|
||||
original_len = len(hooks_list)
|
||||
config["hooks"][hook_name] = [
|
||||
h for h in hooks_list
|
||||
if h.get("extension") != extension_id
|
||||
]
|
||||
if len(config["hooks"][hook_name]) != original_len:
|
||||
modified = True
|
||||
|
||||
# Add back the backed up hooks if any
|
||||
if backup_hooks:
|
||||
for hook_name, hooks in backup_hooks.items():
|
||||
if hook_name not in config["hooks"]:
|
||||
config["hooks"][hook_name] = []
|
||||
config["hooks"][hook_name].extend(hooks)
|
||||
modified = True
|
||||
# Remove any hooks for this extension added by the failed install
|
||||
for hook_name in list(config["hooks"].keys()):
|
||||
hooks_list = config["hooks"][hook_name]
|
||||
if not isinstance(hooks_list, list):
|
||||
config["hooks"][hook_name] = []
|
||||
modified = True
|
||||
continue
|
||||
|
||||
original_len = len(hooks_list)
|
||||
config["hooks"][hook_name] = [
|
||||
h for h in hooks_list
|
||||
if isinstance(h, dict) and h.get("extension") != extension_id
|
||||
]
|
||||
if len(config["hooks"][hook_name]) != original_len:
|
||||
modified = True
|
||||
|
||||
# Add back the backed-up hooks
|
||||
if backup_hooks:
|
||||
for hook_name, hooks in backup_hooks.items():
|
||||
if not isinstance(config["hooks"].get(hook_name), list):
|
||||
config["hooks"][hook_name] = []
|
||||
config["hooks"][hook_name].extend(hooks)
|
||||
modified = True
|
||||
|
||||
# 2. Restore installed list in extensions.yml
|
||||
if backup_installed is not UNSET:
|
||||
if config.get("installed") != backup_installed:
|
||||
config["installed"] = backup_installed
|
||||
modified = True
|
||||
|
||||
if modified:
|
||||
hook_executor.save_project_config(config)
|
||||
|
||||
Reference in New Issue
Block a user