From 6edab91dd6f029bbc8b90926c282b006f2757aae Mon Sep 17 00:00:00 2001 From: Bolin Chen Date: Sat, 7 Nov 2026 05:34:38 +0000 Subject: [PATCH] fix(apps): take an app down only to replace it on a registry update --- docs/system-specs/modules/app-kit-platform.md | 64 ++++++-- src/kiro_crew/apps/bridges_register.py | 30 ++-- src/kiro_crew/apps/registry.py | 2 + .../apps/registry_pipeline/install.py | 7 + src/kiro_crew/apps/routes.py | 5 + src/kiro_crew/apps/routes_install.py | 41 ++--- src/kiro_crew/apps/routes_lifecycle.py | 80 +++++++++- src/kiro_crew/apps/routes_registry.py | 14 +- test/apps_routes_coverage_helpers.py | 7 + test/test_app_update_keeps_agent_edits.py | 149 ++++++++++++++++-- ...test_apps_registry_composition_contract.py | 2 + test/test_apps_registry_git_preflight.py | 8 +- test/test_apps_registry_identity_admission.py | 64 +++++++- ...t_apps_routes_coverage_registry_install.py | 47 ++++++ ...t_apps_routes_coverage_update_uninstall.py | 145 ++++++++++++++--- test/test_gateway_appkit_endpoints_install.py | 12 +- 16 files changed, 571 insertions(+), 106 deletions(-) diff --git a/docs/system-specs/modules/app-kit-platform.md b/docs/system-specs/modules/app-kit-platform.md index df7a0463cdb5..8d7af379cf6f 100644 --- a/docs/system-specs/modules/app-kit-platform.md +++ b/docs/system-specs/modules/app-kit-platform.md @@ -1195,24 +1195,38 @@ The prior file is snapshotted BEFORE the replace (the write path unlinks a legac symlink first, so reading afterwards would find nothing). An unreadable prior file means "nothing to preserve", never "abort the refresh". -**An update carries the edits across its own deregister.** Both branches of -`POST /api/apps/{name}/update` deregister the app before re-registering it, and +**An update carries the edits across its own deregister.** The local-source branch +of `POST /api/apps/{name}/update` deregisters the app before re-registering it, and deregistering deletes the agent files, so the re-registration would find no prior -file. The route therefore snapshots the app's agent files first +file. That branch therefore snapshots the app's agent files first (`snapshot_app_agent_configs`, regular files only, keyed by file name) and hands the snapshot to `register_app(name, prior_agent_configs)`; an agent with no file on disk takes its prior config from it. The failed-update restore uses the same -snapshot. This is what keeps a user's `model` or `description` across a registry -update, a pin-follow of an adopted former built-in, or a local update. An agent -the new version renames or drops gets nothing from the snapshot. Disable and -re-enable still start from the template: a disabled app keeps no agent file and no -snapshot, so the same is true when an update newly asks for `sessionApproval` and -suspends the app. +snapshot. An agent the new version renames or drops gets nothing from the snapshot. + +**A registry update keeps the agent files instead.** The update route's registry +branch and a registry install over an installed app deregister with +`deregister_app(name, keep_agents=True)` (§17), so registration rewrites each file in +place and reads the edits from it. A file it does not rewrite keeps its last good +copy, as on the boot reconcile: a full disk, a data home that does not own a shared +agents directory (registration declines to write there, and a deleted file would +never come back), or an agent the new version ships unreadable. After the take-down +`_reinstall_from_registry` prunes the files the installed manifest no longer +declares (`_prune_stale_app_resources`), and an app the update left unable to run +loses them all: suspended on success, deregistered on a failure. Pinned by +`test_a_registry_update_keeps_an_agent_file_it_cannot_rewrite` and +`test_a_registry_update_that_needs_consent_leaves_no_agent_file`. + +Both keep a user's `model` or `description` across a registry update, a pin-follow of +an adopted former built-in, or a local update. Disable and re-enable still start from +the template: a disabled app keeps no agent file and no snapshot, so the same is true +when an update newly asks for `sessionApproval` and suspends the app. Writer: `apps/bridges_agents.py::_register_agents`, `_preserve_user_agent_edits`, `_read_agent_config`, `snapshot_app_agent_configs`; the update route `apps/routes_install.py::handle_update_app` with -`apps/routes_lifecycle.py::_snapshot_agent_configs_off_loop`. +`apps/routes_lifecycle.py::_snapshot_agent_configs_off_loop` and +`_reinstall_from_registry`. ## 4. A generated prompt is pinned through the app's policy @@ -4382,10 +4396,29 @@ state that is no longer on disk, so nothing retries. **The scrub also re-materia the tracking record, and that pop is what ends the watch's authority to reconcile MCP for the app — so it has to happen BEFORE `deregister_app`. Scrubbing first leaves a window in which a backend that recovers re-registers the OLD manifest's servers, and entries the -update removed survive it. Uninstall, the disable rollback and the two update paths in `kiro_crew.apps.routes` all -order it this way, and +update removed survive it. Uninstall, the disable rollback, the two update paths in +`kiro_crew.apps.routes` and a registry install over an installed app all order it +this way, and `test_update_stops_the_backend_before_deregistering_resources` pins it. +**A registry update takes the app down only to replace it.** The update route's +registry branch and both registry install routes, over an installed app, go through +`routes_lifecycle._reinstall_from_registry`, which hands `install_from_registry` the +stop and a deregister that keeps the agent files (§3) as `before_replace`. The +install awaits it right before `update_app` copies a managed app's new files in: once +every gate, the clone, the build, `setup.onInstall` and the startup-hook recheck have +passed. A refusal before that point leaves the app running, and a failure after the +take-down restores it (`_restore_app_after_failed_update`). The backend stops first +because on Windows the copy fails while it holds a file open under the tree. A +self-managed app (`resources: "app"`) is never taken down: it replaces no installed +file, and it owns the registrations a deregister would delete. A gateway-managed app +whose registry entry declares `resources: "app"` takes that path too: once the install has +written its record self-managed, even if a later step fails, its backend is stopped and +`deregister_app` runs in full, since from then on it registers its own. Pinned by +`test_an_update_takes_the_app_down_only_to_copy_its_files`, +`test_reinstall_over_an_installed_app_takes_it_down_to_replace_its_files` and +`test_registry_update_to_self_managed_cleans_up_only_gateway_owned_resources`. + `backend_status.running` on `/api/apps` is likewise read off the record (`AppProcess.is_running`) rather than asserted from the record merely existing. For an adopted backend, where we hold no handle to poll, "we still track it" @@ -5783,7 +5816,7 @@ records which module owns which part, so a change lands in its owner. | Module under `src/kiro_crew/apps/` | Owns | |---|---| | `routes.py` | The facade: the only import path and patch surface, installing `kiro_crew._facade_mirror` over the owners below, and the route table (`register_app_routes`) | -| `routes_lifecycle.py` | The steps the install, update, uninstall, enable and disable handlers share: `_check_min_version`, the dual-pass warning redactor `_redact_warning`, `_register_app_off_loop` / `_deregister_app_off_loop`, `_start_backend_after_install`, `_stop_backend_and_observe`, `_app_may_run_after_install`, `_restore_app_after_failed_update`, `_suspend_app_for_session_approval_reconsent`, `_refuse_while_startup_hook_runs` and `_unregister_notification_channels` | +| `routes_lifecycle.py` | The steps the install, update, uninstall, enable and disable handlers share: `_check_min_version`, the dual-pass warning redactor `_redact_warning`, `_register_app_off_loop` / `_deregister_app_off_loop`, `_start_backend_after_install`, `_stop_backend_and_observe`, `_app_may_run_after_install`, `_restore_app_after_failed_update`, `_reinstall_from_registry`, `_suspend_app_for_session_approval_reconsent`, `_refuse_while_startup_hook_runs` and `_unregister_notification_channels` | | `routes_proxy.py` | The reverse proxy from an app's dashboard UI to its own backend (`handle_app_api_proxy`): `_resolve_app_backend_url`, the per-app secret cache it signs with (`_get_app_secret`, `invalidate_app_secret_cache`), the principal claim (`_proxy_principal`), the headers and the `token` query credential it strips (`_forwarded_query`) and the event-stream relay | | `routes_static.py` | An installed app's UI bundle (`handle_app_ui_file`, `_open_ui_file` and the stream bounds), the content types the UI and art routes answer with, and `handle_app_dev_mode` | | `routes_art.py` | The installed-art route (`handle_app_art_file`) over an app's manifest-declared art (`_declared_art_paths`, `_read_declared_art`), and the git blob proxy (`handle_blob_proxy`, `_fetch_git_blob`) with the repository and ref validators (`_is_safe_repo_identifier`, `_SAFE_REF_RE`), the fetch timeout and concurrency bound, and the blob-cache touch. Both art paths screen on one extension set | @@ -6307,8 +6340,9 @@ over the same data. `install_clone._downgrade_refusal` compares the cloned manifest's `version` with the installed record's after the admission gate and before the build or `onInstall` runs. An older one is refused with code `would_downgrade` and a sentence naming both versions, the checkout is put back the -way an admission refusal puts it back, and the update route restores the stopped -app, so the installed version keeps its tree, record and commit. The comparison is +way an admission refusal puts it back, and the app keeps running (the refusal comes +before the copy step where an update takes it down), so the installed version keeps +its tree, record and commit. The comparison is `catalog._version_newer`, the one the store's `updateAvailable` uses: a pre-release of the installed release is older, a re-sync of the same version passes, and a version either side cannot read is never refused. A fresh install has no record diff --git a/src/kiro_crew/apps/bridges_register.py b/src/kiro_crew/apps/bridges_register.py index b1de22eb1947..5e65ea43dacb 100644 --- a/src/kiro_crew/apps/bridges_register.py +++ b/src/kiro_crew/apps/bridges_register.py @@ -225,13 +225,12 @@ def register_app( return result # NOTE: pruning of resources a manifest UPGRADE removed is NOT done here. - # The enable/update route handlers all deregister_app() first, so nothing - # stale survives for them to prune — a prune here (a directory walk over - # every agent file + lock acquisition, scaling with agent count) would run - # redundantly on every one of those deregister-first calls. The one path - # that re-registers WITHOUT a preceding deregister — the boot reconcile — - # does the prune itself, off the loop, in - # reconcile_enabled_app_resources(). See that function. + # Every path either deregisters first, so nothing stale survives for it to + # prune — a prune here (a directory walk over every agent file + lock + # acquisition, scaling with agent count) would run redundantly on each of + # those calls — or re-registers over kept agent specs and prunes itself, off + # the loop: the boot reconcile (reconcile_enabled_app_resources) and a + # registry update (routes_lifecycle._reinstall_from_registry). # MCP servers BEFORE agents: _register_agents copies the app's own registered # server specs into each agent config, so registering them afterwards leaves @@ -558,21 +557,26 @@ def discard_app_session_pointers(app_name: str) -> SessionPointerCleanup: return SessionPointerCleanup(dropped=cleared) -def deregister_app(app_name: str) -> RegistrationResult: +def deregister_app(app_name: str, *, keep_agents: bool = False) -> RegistrationResult: """Deregister all resources for an app. Removes symlinks and cron manifests. Does not remove the app directory. + ``keep_agents`` leaves the agent specs for a registry update that registers the + app again: registration rewrites each in place, so one it cannot rewrite keeps + its last good copy and the user's edits (app-kit-platform §3). + Does NOT touch session resume pointers — see :func:`discard_app_session_pointers` for why that belongs to uninstall alone. """ result = RegistrationResult() - try: - n = _deregister_agents(app_name) - result.agents = [f"removed {n} agent(s)"] - except Exception as exc: - result.errors.append(f"agent deregistration failed: {exc}") + if not keep_agents: + try: + n = _deregister_agents(app_name) + result.agents = [f"removed {n} agent(s)"] + except Exception as exc: + result.errors.append(f"agent deregistration failed: {exc}") try: _deregister_skills(app_name) diff --git a/src/kiro_crew/apps/registry.py b/src/kiro_crew/apps/registry.py index 9ecbeee3ac2a..992f457e8cd2 100644 --- a/src/kiro_crew/apps/registry.py +++ b/src/kiro_crew/apps/registry.py @@ -755,6 +755,8 @@ def _holders(name: str) -> tuple[str, ...]: _SCRIPT_TIMEOUT, NATIVE_WINDOWS_HOOK_REFUSAL, RESERVED_APP_NAME_CODE, + Awaitable, + Callable, StreamingLogLines, _adoption_pending, _install_script_platform_refusal, diff --git a/src/kiro_crew/apps/registry_pipeline/install.py b/src/kiro_crew/apps/registry_pipeline/install.py index 45ca93c60ee1..e64db2660f1a 100644 --- a/src/kiro_crew/apps/registry_pipeline/install.py +++ b/src/kiro_crew/apps/registry_pipeline/install.py @@ -18,6 +18,7 @@ import logging import os import sys +from collections.abc import Awaitable, Callable from pathlib import Path from typing import Any @@ -254,6 +255,7 @@ async def install_from_registry( *, resolved_entry: dict[str, Any] | None = None, start_disabled: bool = False, + before_replace: Callable[[], Awaitable[None]] | None = None, ) -> dict[str, Any]: """Clone an app from its git repo and install it. @@ -284,6 +286,9 @@ async def install_from_registry( start_disabled: Create new self-managed records disabled for a caller that performs the guarded enable separately. Managed installs already start disabled. Existing records keep their enabled state. + before_replace: Awaited right before a managed app's files are copied in, + once every gate, the build and ``onInstall`` have passed. An update takes + the app down here; a self-managed app replaces no installed file. Steps: 1. Validate the app exists in the trusted registry JSON @@ -1483,6 +1488,8 @@ async def _roll_back_refused() -> None: # the whole transaction (clone/build → copy → register → backend # start); asyncio.Lock is not reentrant, so no acquisition here. existing = get_app(name) + if before_replace is not None: + await before_replace() # Off-loop: install_app/update_app do a blocking filesystem copy # that can take minutes on large source trees — on the loop it # would trip the loop-stall watchdog and kill the gateway. diff --git a/src/kiro_crew/apps/routes.py b/src/kiro_crew/apps/routes.py index 868754cc47d8..547e815c9ee9 100644 --- a/src/kiro_crew/apps/routes.py +++ b/src/kiro_crew/apps/routes.py @@ -307,6 +307,10 @@ async def _close_proxy_session(app: web.Application) -> None: __all__ = sorted(n for n in set(globals()) | set(_EXPORTS) if not n.startswith("_")) if TYPE_CHECKING: # every owner-defined name in _EXPORTS, for type checkers and IDEs + from kiro_crew.apps.bridges import ( # noqa: F401 + _prune_stale_app_resources, + _registration_source, + ) from kiro_crew.apps.point_grants import ( # noqa: F401 point_grant_families, ) @@ -345,6 +349,7 @@ async def _close_proxy_session(app: web.Application) -> None: _redact_warning, _refuse_while_startup_hook_runs, _register_app_off_loop, + _reinstall_from_registry, _restore_app_after_failed_update, _snapshot_agent_configs_off_loop, _start_backend_after_install, diff --git a/src/kiro_crew/apps/routes_install.py b/src/kiro_crew/apps/routes_install.py index a20051d74c88..5f7ffed090b1 100644 --- a/src/kiro_crew/apps/routes_install.py +++ b/src/kiro_crew/apps/routes_install.py @@ -31,7 +31,6 @@ ) from kiro_crew.apps.registry import ( git_unavailable_refusal, - install_from_registry, is_registry_source, registry_name_from_source, ) @@ -42,6 +41,7 @@ _deregister_app_off_loop, _refuse_while_startup_hook_runs, _register_app_off_loop, + _reinstall_from_registry, _restore_app_after_failed_update, _snapshot_agent_configs_off_loop, _start_backend_after_install, @@ -90,25 +90,16 @@ async def handle_update_app(request: web.Request) -> web.Response: source = body.get("source", info.get("source", "")) # Registry-installed apps: re-clone from registry. - # Same order as the local-source branch below: stop the backend and scrub - # its resources BEFORE the files are replaced. For an already-installed - # app ``install_from_registry`` reaches ``update_app``, which renames the - # live tree aside and copies the new one in -- on Windows that fails with a - # sharing violation (WinError 32) while the backend still holds a file open - # under the tree. A failed install must not leave the app broken either: - # the failure path restores what was there (``_restore_app_after_failed_update``), - # exactly as the local-source branch does. if is_registry_source(source): registry_name = registry_name_from_source(source) async with app_lifecycle_lock(name): - # Preflight BEFORE the stop so a retryable refusal leaves app state - # untouched (app-kit-platform: refusal "without mutating app state"). + # Preflight so a retryable refusal is the local branch's 409, leaving app + # state untouched (app-kit-platform: refusal "without mutating app state"). # ``install_from_registry`` re-checks at its own replacement boundary. startup_refusal = await _refuse_while_startup_hook_runs(name, action="update") if startup_refusal is not None: return startup_refusal - # ``install_from_registry`` asks this too, but only after the stop below - # has taken the app down; a host without git must keep the app running. + # A host without git keeps the app running and reads the missing tool. git_refusal = git_unavailable_refusal(name) if git_refusal is not None: return web.json_response( @@ -121,22 +112,8 @@ async def handle_update_app(request: web.Request) -> web.Response: status=400, ) - # Stop the backend, then deregister old resources -- same order as - # uninstall and the disable rollback. Stopping pops the tracking record, - # so the health watch cannot re-register the OLD manifest's MCP servers - # after the scrub (see app-kit-platform §17). - await asyncio.get_running_loop().run_in_executor( - subprocess_executor(), stop_app_backend, name - ) - # Deregistering deletes the agent configs that hold the user's edits. - prior_agents = await _snapshot_agent_configs_off_loop(name) - await _deregister_app_off_loop(name) - - reg_install = await install_from_registry(registry_name) + reg_install = await _reinstall_from_registry(name, registry_name) if not reg_install.get("ok"): - restore_refused = await _restore_app_after_failed_update(name, prior_agents) - if restore_refused: - reg_install["backend_refused"] = restore_refused sel().log_api_access( caller="dashboard", operation="app_update", @@ -149,8 +126,12 @@ async def handle_update_app(request: web.Request) -> web.Response: # ``enabled`` when the new version adds ``permissions.sessionApproval``, # and a backend started here would run an app the UI shows as disabled. still_enabled = await _app_may_run_after_install(name) - if still_enabled: - reg_result = await _register_app_off_loop(name, prior_agents) + if not still_enabled: + # The take-down kept the agent specs for the re-registration; an app + # that may not run gets none (as on the registry install routes). + await _suspend_app_for_session_approval_reconsent(name) + else: + reg_result = await _register_app_off_loop(name) # Same promotion as the local-source branch: the re-install # rewrote app-crons.json, and only this call carries a changed # cron field into the RUNNING scheduler before the next start. diff --git a/src/kiro_crew/apps/routes_lifecycle.py b/src/kiro_crew/apps/routes_lifecycle.py index d47b60a0b0b3..34580c512ae8 100644 --- a/src/kiro_crew/apps/routes_lifecycle.py +++ b/src/kiro_crew/apps/routes_lifecycle.py @@ -3,8 +3,9 @@ The minimum-version check, warning redaction, registering and deregistering an app off the event loop, starting a backend after an install and stopping one with its outcome observed, whether an app may run once installed, restoring an app after a failed update, -suspending one for session-approval reconsent, the refusal while a startup hook still -runs, and dropping an app's notification channels. +re-installing an installed app from the registry, suspending one for session-approval +reconsent, the refusal while a startup hook still runs, and dropping an app's +notification channels. Part of the ``kiro_crew.apps.routes`` family: callers and tests reach every name here as ``kiro_crew.apps.routes.``, and a patch there reaches this module too @@ -28,12 +29,15 @@ ) from kiro_crew.apps.bridges import ( RegistrationResult, + _prune_stale_app_resources, + _registration_source, deregister_app, register_app, snapshot_app_agent_configs, ) from kiro_crew.apps.hooks_integration import stop_retained_startup_hooks from kiro_crew.apps.manager import get_app +from kiro_crew.apps.registry import install_from_registry from kiro_crew.apps.version import check_min_version as _check_min_version_str from kiro_crew.executors import subprocess_executor from kiro_crew.sel import sel @@ -180,11 +184,14 @@ async def _snapshot_agent_configs_off_loop(name: str) -> dict[str, dict[str, Any ) -async def _deregister_app_off_loop(name: str) -> RegistrationResult: +async def _deregister_app_off_loop(name: str, *, keep_agents: bool = False) -> RegistrationResult: """Run ``deregister_app`` off the event loop (see ``_register_app_off_loop``).""" - return await asyncio.get_running_loop().run_in_executor( - subprocess_executor(), deregister_app, name + job = ( + partial(deregister_app, name, keep_agents=True) + if keep_agents + else partial(deregister_app, name) ) + return await asyncio.get_running_loop().run_in_executor(subprocess_executor(), job) async def _stop_backend_and_observe(name: str) -> tuple[int | None, bool]: @@ -276,6 +283,69 @@ async def _restore_app_after_failed_update( return await _start_backend_after_install(name) +async def _reinstall_from_registry( + name: str, registry_name: str, log_lines: list[str] | None = None +) -> dict[str, Any]: + """Re-install installed *name* from the registry, taking it down only to replace it. + + ``install_from_registry`` awaits the take-down right before it copies a managed + app's new files in, so a refusal or a failed clone, build or ``onInstall`` leaves + the app running. The take-down stops the backend (on Windows the copy fails while + it holds a file open under the tree), then deregisters all but the agent specs: the + app-kit-platform §17 order. The caller's registration rewrites each spec in place, + so one it does not rewrite keeps its last good copy; the specs the installed + manifest does not declare are pruned here. A failure after the take-down + restores the app (:func:`_restore_app_after_failed_update`), or removes the kept + specs of one that may not run. An install that makes a gateway-managed app + self-managed (``resources: "app"``) skips the take-down, so once the record says so + the backend is stopped and the app fully deregistered: it registers its own from + then on. + """ + taken_down = False + loop = asyncio.get_running_loop() + + async def _is_self_managed() -> bool: + return (await _read_app_record(name) or {}).get("resources") == "app" + + was_self_managed = await _is_self_managed() + + async def _take_down() -> None: + nonlocal taken_down + taken_down = True + await loop.run_in_executor(subprocess_executor(), stop_app_backend, name) + await _deregister_app_off_loop(name, keep_agents=True) + + def _prune_dropped() -> None: + manifest, app_root = _registration_source(name) + if manifest is not None: + _prune_stale_app_resources(name, manifest, app_root) + + result = await install_from_registry( + registry_name, log_lines=log_lines, before_replace=_take_down + ) + if not taken_down: + if not was_self_managed and await _is_self_managed(): + await loop.run_in_executor(subprocess_executor(), stop_app_backend, name) + await _deregister_app_off_loop(name) + return result + try: + await loop.run_in_executor(subprocess_executor(), _prune_dropped) + except Exception: + # The files are in place; the boot reconcile prunes what this missed. + logger.warning("Could not prune dropped resources for app %s", name, exc_info=True) + if result.get("ok"): + return result + if not await _app_may_run_after_install(name): + # A failure after the copy can leave the record disabled (a new + # ``sessionApproval``); the agent specs the take-down kept go with it. + await _deregister_app_off_loop(name) + return result + restore_refused = await _restore_app_after_failed_update(name) + if restore_refused: + result["backend_refused"] = restore_refused + return result + + async def _suspend_app_for_session_approval_reconsent( name: str, ) -> RegistrationResult: diff --git a/src/kiro_crew/apps/routes_registry.py b/src/kiro_crew/apps/routes_registry.py index efeb752d1726..29de39b8d93e 100644 --- a/src/kiro_crew/apps/routes_registry.py +++ b/src/kiro_crew/apps/routes_registry.py @@ -47,6 +47,7 @@ from kiro_crew.apps.routes_lifecycle import ( _app_may_run_after_install, _register_app_off_loop, + _reinstall_from_registry, _start_backend_after_install, _suspend_app_for_session_approval_reconsent, ) @@ -199,9 +200,13 @@ async def handle_registry_install(request: web.Request) -> web.Response: # single acquisition covering clone/build → copy → register → backend. async with app_lifecycle_lock(name): # Read before the install writes the app: an app already installed makes - # this install an update, whose rewritten crons must reach the scheduler. + # this install an update, which takes the app down to replace its files and + # whose rewritten crons must reach the scheduler. was_installed = await asyncio.to_thread(lambda: get_app(name) is not None) - result = await install_from_registry(name) + if was_installed: + result = await _reinstall_from_registry(name, name) + else: + result = await install_from_registry(name) # Redact install log and error before returning to client — build output # may contain internal hostnames, package URLs, or credential fragments. @@ -368,7 +373,10 @@ async def _locked_install() -> dict[str, Any]: async with app_lifecycle_lock(name): # Read before the install, as in handle_registry_install. was_installed = await asyncio.to_thread(lambda: get_app(name) is not None) - r = await install_from_registry(name, log_lines=streaming_log) + if was_installed: + r = await _reinstall_from_registry(name, name, log_lines=streaming_log) + else: + r = await install_from_registry(name, log_lines=streaming_log) if r.get("ok") and not r.get("needsClientInstall"): may_run = await _app_may_run_after_install(r["name"]) if not may_run: diff --git a/test/apps_routes_coverage_helpers.py b/test/apps_routes_coverage_helpers.py index 020313f81aca..480ec49bae76 100644 --- a/test/apps_routes_coverage_helpers.py +++ b/test/apps_routes_coverage_helpers.py @@ -101,3 +101,10 @@ async def _identity(request: web.Request, handler: Any) -> web.StreamResponse: def _install(tmp_path: Path, **manifest_extra: Any) -> None: install_app(_make_app_source(tmp_path, **manifest_extra)) + + +def _deregister_into(calls: list[str]) -> Any: + """A ``deregister_app`` stand-in recording whether it kept the agent specs.""" + return lambda name, keep_agents=False: calls.append( + "deregister (agents kept)" if keep_agents else "deregister" + ) diff --git a/test/test_app_update_keeps_agent_edits.py b/test/test_app_update_keeps_agent_edits.py index 3512f36c15b5..fb637bcd45b7 100644 --- a/test/test_app_update_keeps_agent_edits.py +++ b/test/test_app_update_keeps_agent_edits.py @@ -1,10 +1,11 @@ """An app update keeps the user's own edits in the app's materialized agent configs. -Both update branches of ``POST /api/apps/{name}/update`` deregister the app before they -re-register it, and deregistering deletes ``/--.json``. The -registration that follows can only carry a user's edits (``model``, ``description``, -``welcomeMessage``...) across the refresh if it is handed what was on disk BEFORE that -delete, so the route snapshots the files first. A framework-owned key still refreshes +The local-source update deregisters the app before it re-registers it, and deregistering +deletes ``/--.json``, so the route snapshots the files first and +hands the snapshot to the registration. A registry update (the update route's registry +branch, a registry install over an installed app) keeps the files through its take-down +and the registration rewrites them in place. Either way the user's edits (``model``, +``description``, ``welcomeMessage``...) survive, a framework-owned key still refreshes from the new template, and the failed-update restore keeps the edits too. """ @@ -87,12 +88,26 @@ async def test_a_local_update_keeps_the_users_agent_edits( _assert_edits_kept(tmp_path, tools=_V2["tools"]) @pytest.mark.asyncio + @pytest.mark.parametrize( + ("path", "body", "status"), + [ + (f"/api/apps/{APP}/update", {}, 200), + ("/api/apps/registry/install", {"name": APP}, 201), + ("/api/apps/registry/install-stream", {"name": APP}, 200), + ], + ) async def test_a_registry_update_keeps_the_users_agent_edits( - self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + self, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + path: str, + body: dict[str, Any], + status: int, ) -> None: v2 = _installed_with_edits(tmp_path, monkeypatch) async def _install_v2(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() result = await asyncio.to_thread(update_app, v2, expected_name=name) return {"ok": result.ok, "name": name, "error": result.error} @@ -101,11 +116,107 @@ async def _install_v2(name: str, **kwargs: Any) -> dict[str, Any]: monkeypatch.setattr(routes_mod, "install_from_registry", _install_v2) async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: - resp = await client.post(f"/api/apps/{APP}/update", json={}) - assert resp.status == 200, await resp.text() + resp = await client.post(path, json=body) + text = await resp.text() # the stream route installs while it streams + assert resp.status == status, text _assert_edits_kept(tmp_path, tools=_V2["tools"]) + @pytest.mark.asyncio + @pytest.mark.parametrize("cause", ["declined", "disk full"]) + @pytest.mark.parametrize( + ("path", "body", "status"), + [ + (f"/api/apps/{APP}/update", {}, 200), + ("/api/apps/registry/install", {"name": APP}, 201), + ("/api/apps/registry/install-stream", {"name": APP}, 200), + ], + ) + async def test_a_registry_update_keeps_an_agent_file_it_cannot_rewrite( + self, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + path: str, + body: dict[str, Any], + status: int, + cause: str, + ) -> None: + """A data home that does not own a shared agents directory declines the + rewrite, and a full disk fails it: the user's file must still be there.""" + from kiro_crew import agent as agent_mod + from kiro_crew.apps import bridges + + v2 = _installed_with_edits(tmp_path, monkeypatch) + before = _agent_file(tmp_path).read_bytes() + + async def _install_v2(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() + result = await asyncio.to_thread(update_app, v2, expected_name=name) + return {"ok": result.ok, "name": name, "error": result.error} + + def _no_space(*_a: Any, **_k: Any) -> None: + raise OSError(28, "No space left on device") + + monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) + monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) + monkeypatch.setattr(routes_mod, "install_from_registry", _install_v2) + if cause == "declined": + monkeypatch.setattr(agent_mod, "_declined_foreign_spec_write", lambda p: True) + else: + monkeypatch.setattr(bridges, "atomic_write", _no_space) + + async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: + resp = await client.post(path, json=body) + text = await resp.text() + assert resp.status == status, text + + assert _agent_file(tmp_path).read_bytes() == before + + @pytest.mark.asyncio + @pytest.mark.parametrize( + ("path", "body", "ok", "status"), + [ + (f"/api/apps/{APP}/update", {}, True, 200), + (f"/api/apps/{APP}/update", {}, False, 400), + ("/api/apps/registry/install", {"name": APP}, True, 201), + ("/api/apps/registry/install", {"name": APP}, False, 400), + ("/api/apps/registry/install-stream", {"name": APP}, True, 200), + ], + ) + async def test_a_registry_update_that_needs_consent_leaves_no_agent_file( + self, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + path: str, + body: dict[str, Any], + ok: bool, + status: int, + ) -> None: + """A new ``sessionApproval`` leaves the app disabled, so the agent files the + take-down kept must go, on success and on a failure after the copy alike.""" + v2 = _installed_with_edits(tmp_path, monkeypatch) + manifest_path = v2 / APP_MANIFEST_FILENAME + manifest = json.loads(manifest_path.read_text(encoding="utf-8")) + manifest["permissions"] = {"sessionApproval": True} + manifest_path.write_text(json.dumps(manifest), encoding="utf-8") + + async def _install_v2(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() + result = await asyncio.to_thread(update_app, v2, expected_name=name) + assert result.ok, result.error + return {"ok": ok, "name": name, "error": "" if ok else "provenance write failed"} + + monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) + monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) + monkeypatch.setattr(routes_mod, "install_from_registry", _install_v2) + + async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: + resp = await client.post(path, json=body) + text = await resp.text() + assert resp.status == status, text + + assert not _agent_file(tmp_path).exists() + @pytest.mark.asyncio async def test_a_failed_registry_update_restores_the_users_agent_edits( self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch @@ -113,7 +224,8 @@ async def test_a_failed_registry_update_restores_the_users_agent_edits( _installed_with_edits(tmp_path, monkeypatch) async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: - return {"ok": False, "name": name, "error": "clone failed"} + await kwargs["before_replace"]() + return {"ok": False, "name": name, "error": "copy failed"} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) @@ -139,14 +251,29 @@ async def test_a_failed_local_update_restores_the_users_agent_edits( _assert_edits_kept(tmp_path, tools=_V1["tools"]) @pytest.mark.asyncio + @pytest.mark.parametrize("registry", [False, True]) async def test_an_agent_the_new_version_drops_does_not_come_back( - self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, registry: bool ) -> None: _installed_with_edits(tmp_path, monkeypatch) v2 = _source(tmp_path, "2.0.0", {**_V2, "name": "renamed"}) + body: dict[str, Any] = {"source": str(v2)} + if registry: + # The registry take-down keeps the agent files, so the dropped one has to + # be pruned after the install. + + async def _install_v2(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() + result = await asyncio.to_thread(update_app, v2, expected_name=name) + return {"ok": result.ok, "name": name, "error": result.error} + + monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) + monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) + monkeypatch.setattr(routes_mod, "install_from_registry", _install_v2) + body = {} async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: - resp = await client.post(f"/api/apps/{APP}/update", json={"source": str(v2)}) + resp = await client.post(f"/api/apps/{APP}/update", json=body) assert resp.status == 200, await resp.text() assert not _agent_file(tmp_path).exists() diff --git a/test/test_apps_registry_composition_contract.py b/test/test_apps_registry_composition_contract.py index 8b30379d170d..2b11b88a9901 100644 --- a/test/test_apps_registry_composition_contract.py +++ b/test/test_apps_registry_composition_contract.py @@ -92,6 +92,8 @@ FROZEN_NAMES: tuple[str, ...] = ( "Any", "AppManifest", + "Awaitable", + "Callable", "DESKTOP_BUILD_STEP_UNSUPPORTED", "FIRST_PARTY_AUTHORS", "IPv6Address", diff --git a/test/test_apps_registry_git_preflight.py b/test/test_apps_registry_git_preflight.py index eadb22ffb82f..72aad69d34ee 100644 --- a/test/test_apps_registry_git_preflight.py +++ b/test/test_apps_registry_git_preflight.py @@ -2,8 +2,7 @@ Every registry row is fetched with git. Without the preflight, a git-less host fails deep in the checkout (``git init failed`` on a fresh clone, an -``unreadable_clone_origin`` refusal over a healthy checkout on an update), and -the update route stops the app's backend and scrubs its resources first. These +``unreadable_clone_origin`` refusal over a healthy checkout on an update). These tests take git off ``PATH`` for real rather than patching the lookup, so they exercise the same ``shutil.which`` answer the gateway gets. """ @@ -85,9 +84,8 @@ async def _never_async(*args: Any, **kwargs: Any) -> Any: async def test_registry_update_refuses_before_stopping_the_app( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - """The update route stops the backend and deregisters BEFORE it installs, so - its own preflight must run first: the app stays enabled, registered and - running, and the user reads the missing tool.""" + """The update route asks before the install: the app stays enabled, registered + and running, and the user reads the missing tool.""" _setup_env(tmp_path, monkeypatch) _install(tmp_path) enable_app(APP) diff --git a/test/test_apps_registry_identity_admission.py b/test/test_apps_registry_identity_admission.py index 0fa0800c11f3..98401e211235 100644 --- a/test/test_apps_registry_identity_admission.py +++ b/test/test_apps_registry_identity_admission.py @@ -242,13 +242,75 @@ def _must_not_update(*args, **kwargs): raise AssertionError("registry reinstall must not replace old files") monkeypatch.setattr(registry, "update_app", _must_not_update) + taken_down: list[str] = [] - result = await registry.install_from_registry("demoapp") + async def _take_down() -> None: + taken_down.append("demoapp") + + result = await registry.install_from_registry("demoapp", before_replace=_take_down) assert result["ok"] is False assert result["code"] == "startup_hook_still_running" assert result["retryable"] is True assert cleanup_calls == [("demoapp", True), ("demoapp", True)] + assert taken_down == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("managed", [True, False]) +async def test_an_update_takes_the_app_down_only_to_copy_its_files(monkeypatch, tmp_path, managed): + """``before_replace`` runs after the clone, build and boundary recheck, right before + ``update_app`` copies a managed app in; a self-managed app replaces no installed + file, so its own registrations are never taken down.""" + src = tmp_path / "app-sources" / "demoapp" + _identity_harness(monkeypatch, src, cloned_manifest={"name": "demoapp", "version": "2.0.0"}) + entry = {"name": "demoapp", "repo": "https://example.com/demo.git", "branch": "main"} + if not managed: + entry["resources"] = "app" + monkeypatch.setattr(registry, "get_registry_app", lambda n: entry) + monkeypatch.setattr(registry, "get_app", lambda _name: {"name": "demoapp", "version": "1.0.0"}) + monkeypatch.setattr(registry, "_resolved_clone_commit", lambda root: "a" * 40) + monkeypatch.setattr(registry, "verified_signer", lambda manifest: "") + monkeypatch.setattr(registry, "set_app_provenance", lambda name, **kwargs: None) + calls: list[str] = [] + harness_build = registry._clone_build_app + + async def _build(*args, **kwargs): + calls.append("build") + return await harness_build(*args, **kwargs) + + monkeypatch.setattr(registry, "_clone_build_app", _build) + + from kiro_crew.apps import admission, hooks_integration, manager + + async def _recheck(app_name: str, *, bounded: bool) -> bool: + calls.append("recheck") + return True + + monkeypatch.setattr(hooks_integration, "stop_retained_startup_hooks", _recheck) + monkeypatch.setattr(admission, "app_version_refusal", lambda *a: None) + monkeypatch.setattr(manager, "refresh_app_config_keys", lambda: None) + + def _register_external(**kwargs): + calls.append("register") + return MagicMock(ok=True, notice="") + + monkeypatch.setattr(manager, "register_external_app", _register_external) + + async def _take_down() -> None: + calls.append("take-down") + + def _replace(path): + calls.append("replace") + return MagicMock(ok=True, name="demoapp", message="done", error="", notice="") + + monkeypatch.setattr(registry, "update_app", _replace) + + result = await registry.install_from_registry("demoapp", before_replace=_take_down) + + assert result["ok"] is True, result + copy = ["take-down", "replace"] if managed else ["register"] + assert calls == ["recheck", "build", "recheck", *copy] @pytest.mark.asyncio diff --git a/test/test_apps_routes_coverage_registry_install.py b/test/test_apps_routes_coverage_registry_install.py index 7c7aaa25f026..9cf29b04bf62 100644 --- a/test/test_apps_routes_coverage_registry_install.py +++ b/test/test_apps_routes_coverage_registry_install.py @@ -18,6 +18,7 @@ from aiohttp.test_utils import TestClient, TestServer from apps_routes_coverage_helpers import ( APP, + _deregister_into, _install, _make_app, _setup_env, @@ -177,6 +178,52 @@ async def _reconsent(name: str, **kwargs: Any) -> dict[str, Any]: assert body["registration"]["agents"] == [] assert calls == ["stop", "deregister"] + @pytest.mark.asyncio + @pytest.mark.parametrize( + "path", ["/api/apps/registry/install", "/api/apps/registry/install-stream"] + ) + @pytest.mark.parametrize("ok", [True, False]) + async def test_reinstall_over_an_installed_app_takes_it_down_to_replace_its_files( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, path: str, ok: bool + ) -> None: + # Replacing an installed app's files fails on Windows while its backend + # holds one open, so both routes stop the backend and scrub its resources + # at the replacement boundary, and a failed replacement brings it back. + _setup_env(tmp_path, monkeypatch) + _install(tmp_path) + enable_app(APP) + calls: list[str] = [] + + async def _reinstall(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() + calls.append("replace") + return {"ok": ok, "name": name, "error": "" if ok else "copy failed"} + + def _register(name: str) -> SimpleNamespace: + calls.append("register") + return SimpleNamespace(to_dict=dict) + + monkeypatch.setattr(routes_mod, "install_from_registry", _reinstall) + monkeypatch.setattr(routes_mod, "register_app", _register) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) + monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) + monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) + + async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: + resp = await client.post(path, json={"name": APP}) + text = await resp.text() + if path.endswith("-stream"): + assert json.loads(_sse_events(text)[-1][1])["ok"] is ok + else: + assert resp.status == (201 if ok else 400), text + assert calls == [ + "stop", + "deregister (agents kept)", + "replace", + "register", + "start", + ], calls + # POST /api/apps/registry/install-stream (SSE) diff --git a/test/test_apps_routes_coverage_update_uninstall.py b/test/test_apps_routes_coverage_update_uninstall.py index 014da102b91a..9a9852bb3b43 100644 --- a/test/test_apps_routes_coverage_update_uninstall.py +++ b/test/test_apps_routes_coverage_update_uninstall.py @@ -25,6 +25,7 @@ from aiohttp.test_utils import TestClient, TestServer from apps_routes_coverage_helpers import ( APP, + _deregister_into, _install, _make_app, _make_app_source, @@ -110,7 +111,8 @@ async def test_registry_update_stops_the_backend_before_replacing_files( # which replaces the live tree. On Windows that is a # sharing violation while the backend still holds a file open under it, # so the backend must be stopped (and its resources scrubbed) BEFORE the - # install runs -- the same order the local-source branch already uses. + # files are replaced: at the install's replacement boundary, after the + # clone and build. _setup_env(tmp_path, monkeypatch) _install(tmp_path) enable_app(APP) @@ -118,26 +120,28 @@ async def test_registry_update_stops_the_backend_before_replacing_files( async def _ok_install(name: str, **kwargs: Any) -> dict[str, Any]: calls.append("install") + await kwargs["before_replace"]() + calls.append("replace") return {"ok": True, "name": name} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _ok_install) - monkeypatch.setattr(routes_mod, "deregister_app", lambda n: calls.append("deregister")) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: resp = await client.post(f"/api/apps/{APP}/update", json={}) assert resp.status == 200 - assert calls == ["stop", "deregister", "install", "start"], calls + assert calls == ["install", "stop", "deregister (agents kept)", "replace", "start"], calls @pytest.mark.asyncio async def test_registry_update_failure_restores_an_enabled_app( self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - # The backend is down when the install fails, so the route must bring the - # app back itself: re-register the old resources and restart the backend. + # The backend is down when the replacement fails, so the route must bring + # the app back itself: re-register the old resources and restart the backend. _setup_env(tmp_path, monkeypatch) _install(tmp_path) enable_app(APP) @@ -145,11 +149,47 @@ async def test_registry_update_failure_restores_an_enabled_app( async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: calls.append("install") - return {"ok": False, "name": name, "error": "clone failed"} + await kwargs["before_replace"]() + return {"ok": False, "name": name, "error": "copy failed"} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _failed_install) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) + monkeypatch.setattr(routes_mod, "register_app", lambda n: calls.append("register")) + monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) + monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) + + async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: + resp = await client.post(f"/api/apps/{APP}/update", json={}) + assert resp.status == 400 + assert (await resp.json())["error"] == "copy failed" + assert calls == [ + "install", + "stop", + "deregister (agents kept)", + "register", + "start", + ], calls + + @pytest.mark.asyncio + async def test_registry_update_refused_before_the_replacement_leaves_the_app_running( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + # A clone, build or gate failure comes before the replacement boundary, so + # the backend never went down and there is nothing to restore. + _setup_env(tmp_path, monkeypatch) + _install(tmp_path) + enable_app(APP) + calls: list[str] = [] + + async def _refused_install(name: str, **kwargs: Any) -> dict[str, Any]: + calls.append("install") + return {"ok": False, "name": name, "error": "clone failed"} + + monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) + monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) + monkeypatch.setattr(routes_mod, "install_from_registry", _refused_install) monkeypatch.setattr(routes_mod, "deregister_app", lambda n: calls.append("deregister")) monkeypatch.setattr(routes_mod, "register_app", lambda n: calls.append("register")) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) @@ -159,7 +199,64 @@ async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: resp = await client.post(f"/api/apps/{APP}/update", json={}) assert resp.status == 400 assert (await resp.json())["error"] == "clone failed" - assert calls == ["stop", "deregister", "install", "register", "start"], calls + assert calls == ["install"], calls + + @pytest.mark.asyncio + @pytest.mark.parametrize( + ("was_self_managed", "ok", "status", "expected"), + [ + (False, True, 200, ["install", "stop", "deregister", "register", "start"]), + (False, False, 400, ["install", "stop", "deregister"]), + (True, True, 200, ["install", "register", "start"]), + ], + ids=["gateway_to_app", "gateway_to_app_then_failed", "app_to_app"], + ) + async def test_registry_update_to_self_managed_cleans_up_only_gateway_owned_resources( + self, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + was_self_managed: bool, + ok: bool, + status: int, + expected: list[str], + ) -> None: + # The self-managed install path never takes the app down, so a gateway-managed + # app it hands to the app must lose the gateway's backend and registrations, + # even when a step after the record write fails. + _setup_env(tmp_path, monkeypatch) + _install(tmp_path) + enable_app(APP) + + def _make_self_managed(version: str) -> None: + assert register_external_app( + APP, version, "Coverage Test App", resources="app", lifecycle="gateway" + ).ok + + if was_self_managed: + _make_self_managed("1.0.0") + calls: list[str] = [] + + async def _self_managed_install(name: str, **kwargs: Any) -> dict[str, Any]: + calls.append("install") + _make_self_managed("2.0.0") + return {"ok": ok, "name": name, "error": "" if ok else "provenance write failed"} + + def _register(name: str) -> SimpleNamespace: + calls.append("register") + return SimpleNamespace(to_dict=dict) + + monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) + monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) + monkeypatch.setattr(routes_mod, "install_from_registry", _self_managed_install) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) + monkeypatch.setattr(routes_mod, "register_app", _register) + monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) + monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) + + async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: + resp = await client.post(f"/api/apps/{APP}/update", json={}) + assert resp.status == status, await resp.text() + assert calls == expected, calls @pytest.mark.asyncio async def test_registry_update_failure_leaves_a_disabled_app_stopped( @@ -176,12 +273,13 @@ async def test_registry_update_failure_leaves_a_disabled_app_stopped( async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: calls.append("install") - return {"ok": False, "name": name, "error": "clone failed"} + await kwargs["before_replace"]() + return {"ok": False, "name": name, "error": "copy failed"} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _failed_install) - monkeypatch.setattr(routes_mod, "deregister_app", lambda n: calls.append("deregister")) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) monkeypatch.setattr(routes_mod, "register_app", lambda n: calls.append("register")) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) @@ -189,7 +287,9 @@ async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: async with TestClient(TestServer(_make_app(dashboard_user="owner"))) as client: resp = await client.post(f"/api/apps/{APP}/update", json={}) assert resp.status == 400 - assert calls == ["stop", "deregister", "install"], calls + # Nothing is registered or started; the full deregister drops whatever agent + # spec the take-down kept. + assert calls == ["install", "stop", "deregister (agents kept)", "deregister"], calls @pytest.mark.asyncio async def test_local_update_failure_leaves_a_disabled_app_unregistered( @@ -223,9 +323,9 @@ async def test_local_update_failure_leaves_a_disabled_app_unregistered( async def test_registry_update_refuses_before_stopping_while_startup_hook_runs( self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: - # The stop runs before ``install_from_registry``, so the route needs its - # own bounded preflight ahead of it: a retryable refusal must leave app - # state untouched (no stop, no scrub, no install), as on the local path. + # The route answers a retained startup hook before the install, as on the + # local path: a retryable 409 that leaves app state untouched (no stop, no + # scrub, no install). _setup_env(tmp_path, monkeypatch) _install(tmp_path) enable_app(APP) @@ -264,12 +364,13 @@ async def test_registry_update_success_reregisters( calls: list[str] = [] async def _ok_install(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() return {"ok": True, "name": name} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _ok_install) - monkeypatch.setattr(routes_mod, "deregister_app", lambda n: calls.append("deregister")) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) @@ -283,7 +384,7 @@ async def _ok_install(name: str, **kwargs: Any) -> dict[str, Any]: # watch has lost its tracking record and cannot re-register the old # manifest's MCP servers in between (see app-kit-platform §17); both run # before the re-install replaces the files. - assert calls == ["stop", "deregister", "start"] + assert calls == ["stop", "deregister (agents kept)", "start"] @pytest.mark.asyncio async def test_registry_update_that_widens_session_approval_does_not_restart( @@ -299,13 +400,14 @@ async def test_registry_update_that_widens_session_approval_does_not_restart( calls: list[str] = [] async def _reconsent_install(name: str, **kwargs: Any) -> dict[str, Any]: + await kwargs["before_replace"]() disable_app(name) # what update_app does on a widened grant return {"ok": True, "name": name, "notice": "session_approval_reconsent"} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _reconsent_install) - monkeypatch.setattr(routes_mod, "deregister_app", lambda n: calls.append("deregister")) + monkeypatch.setattr(routes_mod, "deregister_app", _deregister_into(calls)) monkeypatch.setattr(routes_mod, "register_app", lambda n: calls.append("register")) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: calls.append("stop")) monkeypatch.setattr(routes_mod, "start_app_backend", lambda n: calls.append("start")) @@ -317,7 +419,9 @@ async def _reconsent_install(name: str, **kwargs: Any) -> dict[str, Any]: assert data["ok"] is True assert data["notice"] == "session_approval_reconsent" assert "registration" not in data - assert calls == ["stop", "deregister"] + # The take-down kept the agent specs for a re-registration that does not come, + # so the app is suspended and loses them. + assert calls == ["stop", "deregister (agents kept)", "stop", "deregister"] @pytest.mark.asyncio async def test_local_update_failure_restores_registration( @@ -382,12 +486,13 @@ def _fn(*args: Any, **kwargs: Any) -> Any: if source == "registry": async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: - return {"ok": False, "name": name, "error": "clone failed"} + await kwargs["before_replace"]() + return {"ok": False, "name": name, "error": "copy failed"} monkeypatch.setattr(routes_mod, "is_registry_source", lambda s: True) monkeypatch.setattr(routes_mod, "registry_name_from_source", lambda s: APP) monkeypatch.setattr(routes_mod, "install_from_registry", _failed_install) - error = "clone failed" + error = "copy failed" else: error = "source manifest mismatch" monkeypatch.setattr( @@ -395,7 +500,7 @@ async def _failed_install(name: str, **kwargs: Any) -> dict[str, Any]: "update_app", lambda source, expected_name=None: AppResult(ok=False, name=APP, error=error), ) - monkeypatch.setattr(routes_mod, "deregister_app", lambda n: None) + monkeypatch.setattr(routes_mod, "deregister_app", lambda n, **_: None) monkeypatch.setattr(routes_mod, "stop_app_backend", lambda n: None) if broken == "register": monkeypatch.setattr(routes_mod, "register_app", _raise(OSError("disk full"))) diff --git a/test/test_gateway_appkit_endpoints_install.py b/test/test_gateway_appkit_endpoints_install.py index c61ebf030931..ee26211920a9 100644 --- a/test/test_gateway_appkit_endpoints_install.py +++ b/test/test_gateway_appkit_endpoints_install.py @@ -322,7 +322,9 @@ async def test_streams_log_lines_then_done( ) -> None: """Mock install_from_registry to verify SSE log + done events.""" - async def _fake_install(name: str, log_lines: list[str] | None = None) -> dict[str, Any]: + async def _fake_install( + name: str, log_lines: list[str] | None = None, **_: Any + ) -> dict[str, Any]: if log_lines is not None: log_lines.append("step 1: cloning") log_lines.append("step 2: building") @@ -540,7 +542,9 @@ async def test_multiline_log_does_not_break_sse_framing( a new SSE event. """ - async def _fake_install(name: str, log_lines: list[str] | None = None) -> dict[str, Any]: + async def _fake_install( + name: str, log_lines: list[str] | None = None, **_: Any + ) -> dict[str, Any]: if log_lines is not None: # Simulate a log line with embedded newlines that could # inject a fake "done" event if not properly escaped @@ -585,7 +589,9 @@ async def test_log_redaction_applied_per_line( Uses an AWS access key pattern which redact_credentials catches. """ - async def _fake_install(name: str, log_lines: list[str] | None = None) -> dict[str, Any]: + async def _fake_install( + name: str, log_lines: list[str] | None = None, **_: Any + ) -> dict[str, Any]: if log_lines is not None: # AWS access key ID pattern — caught by redact_credentials log_lines.append("Found key AKIAIOSFODNN7EXAMPLE in config")