diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b6b9f2e1..3c52020e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -126,6 +126,10 @@ jobs: - run: npm ci --no-audit --no-fund - run: npm run typecheck - run: npm test + # The GitHub Copilot app plugin's extension runs on the app's Node runtime, which has a + # global WebSocket (Node 22 and later). + - name: GitHub Copilot app plugin tests + run: node --test ../tests/*.test.mjs # ug packs the extension from these committed files, so they must match the sources. - name: Check the committed bundle is up to date run: | diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py new file mode 100644 index 000000000..0b5745afe --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -0,0 +1,652 @@ +"""The Databricks plugin for the GitHub Copilot desktop app. + +ug writes a local plugin marketplace it owns and turns it on in Copilot's ``settings.json``. The +plugin's extension writes ug's providers and model rows to the app's model picker over the app's +local WebSocket API and serves them straight from the AI Gateway under the same ids. It gets each +request's Databricks token from ``ug auth-token``, reusing it until shortly before it expires. +The app's window loads the picker's providers only when it starts, so after a sync that changed +ug's rows, the extension restarts the app once, when no chat is running a turn. + +With the app closed, ug can't delete its rows, so ``defer_remove`` leaves that and the settings keys +to the extension's next start, and ``cleanup_leftover`` deletes the directory afterwards. +""" + +from __future__ import annotations + +import json +import os +import shutil +import time +import uuid +from collections.abc import Callable, Iterator, Mapping +from contextlib import contextmanager +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any, Literal + +from ucode.config_io import atomic_write_json +from ucode.custom_oauth import CustomOAuthConfig, build_custom_auth_token_argv +from ucode.databricks import build_auth_token_argv + +MARKETPLACE = "databricks-unity-gateway" +PLUGIN = "unity-gateway" +PLUGIN_KEY = f"{PLUGIN}@{MARKETPLACE}" +# Earlier builds' ids. Configure moves them to the current ones, and removal takes out both. +_LEGACY_MARKETPLACE = "unity-gateway" +_LEGACY_PLUGIN = "databricks" +_IDS = ((MARKETPLACE, PLUGIN), (_LEGACY_MARKETPLACE, _LEGACY_PLUGIN)) +EXTENSION = "databricks" +VERSION = "0.1.0" + +Wire = Literal["anthropic", "responses", "completions"] +Status = Literal["installed", "removal_pending", "disabled", "not_installed", "invalid_settings"] + +_MARKETPLACES_KEY = "extraKnownMarketplaces" +_ENABLED_KEY = "enabledPlugins" +_SOURCE_FILES = ( + "extension.mjs", + "ug-plugin.mjs", + "app-rows.mjs", + "app-remove.mjs", + "app-restart.mjs", +) +# The app unloads the extension within about 3 s of its settings keys going away. Deleting the +# directory first leaves the running extensions serving open chats until a new chat starts. +UNLOAD_WAIT_S = 5.0 +# The runtime asks for 32K output tokens and fails a turn on a model that advertises fewer. +MIN_OUTPUT_TOKENS = 32_000 + + +class SettingsError(RuntimeError): + """Copilot's ``settings.json`` can't be safely rewritten.""" + + +@dataclass(frozen=True) +class PluginModel: + """A model the extension serves. The session uses these fields instead of the picker row's, + so they must repeat its name, limits and vision support.""" + + model_id: str + wire_model: str + name: str + # (max prompt tokens, max output tokens); None keeps the runtime's defaults. + limits: tuple[int, int] | None = None + vision: bool = False + # False stops the runtime from sending `reasoning_effort`; None keeps its default. + reasoning_effort: bool | None = None + + +@dataclass(frozen=True) +class PluginProvider: + """One provider the extension serves. ``id`` must match the provider ug registers in the app, + and each model's ``model_id`` the picker row, or the app's rows stop working.""" + + id: str + wire: Wire + base_path: str + models: tuple[PluginModel, ...] + + +@dataclass(frozen=True) +class AppProviderRows: + """A provider and its model rows as the app's WebSocket API takes them. The extension writes + them to the app's model picker.""" + + provider: Mapping[str, Any] + secret: Mapping[str, str] | None + models: tuple[Mapping[str, Any], ...] + + +@dataclass(frozen=True) +class PluginConfig: + workspace: str + ug_path: str + providers: tuple[PluginProvider, ...] + headers: Mapping[str, str] = field(default_factory=dict) + profile: str | None = None + use_pat: bool = False + custom_oauth: CustomOAuthConfig | None = None + # The extension's process gets the app runtime's PATH, which may lack the directories ug and + # the Databricks CLI live in. + path_dirs: tuple[str, ...] = () + # `ug auth-token` runs the Databricks CLI, so its directory goes on the PATH too. + databricks_path: str | None = None + app_providers: tuple[AppProviderRows, ...] = () + # Every provider id ug may have written to the app; the extension deletes those not in + # ``app_providers``. + owned_provider_ids: tuple[str, ...] = () + + +def copilot_settings_path() -> Path: + copilot_home = os.environ.get("COPILOT_HOME") + base = Path(copilot_home) if copilot_home else Path.home() / ".copilot" + return base / "settings.json" + + +def plugin_root() -> Path: + return Path.home() / ".ucode" / "copilot-app-plugin" + + +def _plugin_dir(root: Path, plugin: str = PLUGIN) -> Path: + return root / "plugins" / plugin + + +def _extension_dir(root: Path, plugin: str = PLUGIN) -> Path: + return _plugin_dir(root, plugin) / "extensions" / EXTENSION + + +def _token_command(config: PluginConfig) -> list[str]: + # `ug auth-token --host` ignores the saved PAT and custom OAuth choices, so the command names + # them itself. + if config.custom_oauth: + command = build_custom_auth_token_argv(config.workspace, config.custom_oauth) + else: + command = build_auth_token_argv(config.workspace, config.profile, use_pat=config.use_pat) + return [config.ug_path, *command[1:]] + + +def _login_hint(config: PluginConfig) -> str: + failed = f"Couldn't get a Databricks token for {config.workspace}." + if config.use_pat: + section = config.profile or "your-profile" + return ( + f"{failed} Add a `token = ` entry under [{section}] in ~/.databrickscfg, or rerun " + "`ug configure`, then try again." + ) + if config.custom_oauth: + return f"{failed} Rerun `ug configure` to sign in again, then try again." + login = f"databricks auth login --host {config.workspace}" + if config.profile: + login += f" --profile {config.profile}" + return f"{failed} Run `{login}`, then try again." + + +def _missing_ug_hint(config: PluginConfig) -> str: + return f"Unity Gateway wasn't found at {config.ug_path}. Rerun `ug configure`, then try again." + + +def _provider_entry(provider: PluginProvider, config: PluginConfig) -> dict: + entry: dict = { + "name": provider.id, + "type": "anthropic" if provider.wire == "anthropic" else "openai", + "baseUrl": config.workspace.rstrip("/") + provider.base_path, + "headers": dict(config.headers), + } + if provider.wire != "anthropic": + entry["wireApi"] = provider.wire + return entry + + +def _model_entry(model: PluginModel, provider: PluginProvider) -> dict: + entry: dict = { + "id": model.model_id, + "modelId": model.model_id, + "provider": provider.id, + "wireModel": model.wire_model, + "name": model.name, + } + if model.limits: + prompt, output = model.limits + entry["maxPromptTokens"] = prompt + if output >= MIN_OUTPUT_TOKENS: + entry["maxOutputTokens"] = output + entry["maxContextWindowTokens"] = prompt + output + supports: dict = {} + if model.vision: + supports["vision"] = True + if model.reasoning_effort is not None: + supports["reasoningEffort"] = model.reasoning_effort + if supports: + entry["capabilities"] = {"supports": supports} + return entry + + +def _app_entry(rows: AppProviderRows) -> dict: + return { + "provider": dict(rows.provider), + "secret": dict(rows.secret) if rows.secret else None, + "models": [dict(model) for model in rows.models], + } + + +def extension_config(config: PluginConfig) -> dict: + """The JSON the extension reads at start.""" + return { + "providers": [_provider_entry(provider, config) for provider in config.providers], + "models": [ + _model_entry(model, provider) + for provider in config.providers + for model in provider.models + ], + "tokenCommand": _token_command(config), + "ugPath": config.ug_path, + "pathDirs": list(config.path_dirs), + "databricksPath": config.databricks_path, + "loginHint": _login_hint(config), + "missingUgHint": _missing_ug_hint(config), + "app": { + "providers": [_app_entry(rows) for rows in config.app_providers], + "ownedProviderIds": list(config.owned_provider_ids), + }, + } + + +# What the extension keeps between runs, in its own directory; a reinstall carries them over, so +# an unchanged configure neither resyncs nor restarts the app. +_EXTENSION_STATE_FILES = ("app-sync.json", "app-restart.json", "app-restart.log") +_STAGING_PREFIX = ".staging-" +_REPLACED_PREFIX = ".replaced-" +# Older leftovers are from a run that crashed, never from one still writing. +_LEFTOVER_AGE_S = 600.0 + + +def _write_tree(config: PluginConfig, root: Path) -> None: + description = "Databricks models in the GitHub Copilot app, through Unity Gateway" + atomic_write_json( + root / ".github" / "plugin" / "marketplace.json", + { + "name": MARKETPLACE, + "owner": {"name": "Unity Gateway"}, + "plugins": [ + { + "name": PLUGIN, + "source": f"./plugins/{PLUGIN}", + "version": VERSION, + "description": description, + } + ], + }, + ) + atomic_write_json( + _plugin_dir(root) / ".plugin" / "plugin.json", + { + "name": PLUGIN, + "version": VERSION, + "description": description, + "author": {"name": "Unity Gateway"}, + }, + ) + extension_dir = _extension_dir(root) + extension_dir.mkdir(parents=True, exist_ok=True) + source = Path(__file__).parent + for name in _SOURCE_FILES: + shutil.copyfile(source / name, extension_dir / name) + atomic_write_json(extension_dir / "config.json", extension_config(config)) + + +def _carry_over(old: Path, new: Path) -> None: + for name in _EXTENSION_STATE_FILES: + if (_extension_dir(old) / name).is_file(): + shutil.copy2(_extension_dir(old) / name, _extension_dir(new) / name) + if (marker := _created_settings_marker(old)).is_file(): + shutil.copy2(marker, _created_settings_marker(new)) + # Settings may still name an earlier build's copy until ``enable`` moves them on. + if (legacy := _plugin_dir(old, _LEGACY_PLUGIN)).is_dir(): + shutil.copytree(legacy, _plugin_dir(new, _LEGACY_PLUGIN)) + + +def _fsync_tree(root: Path) -> None: + for path in root.rglob("*"): + if path.is_file(): + # Best effort: Windows syncs only a handle opened for writing. + try: + with path.open("rb+") as file: + os.fsync(file.fileno()) + except OSError: + pass + + +def _remove_leftovers(root: Path) -> None: + now = time.time() + for prefix in (_STAGING_PREFIX, _REPLACED_PREFIX): + for leftover in root.parent.glob(f".{root.name}{prefix}*"): + try: + if now - leftover.stat().st_mtime > _LEFTOVER_AGE_S: + shutil.rmtree(leftover) + except OSError: + pass + + +def _publish(staged: Path, root: Path) -> None: + """Move ``staged`` to ``root`` with renames only, so the app never reads a half-written plugin. + Windows can't rename onto an existing directory, so the old one is moved aside first.""" + replaced = root.with_name(f".{root.name}{_REPLACED_PREFIX}{uuid.uuid4().hex}") + had_old = root.exists() + if had_old: + os.replace(root, replaced) + try: + os.replace(staged, root) + except OSError: + if had_old: + os.replace(replaced, root) + raise + if had_old: + shutil.rmtree(replaced, ignore_errors=True) + + +def write_plugin(config: PluginConfig, root: Path | None = None) -> Path: + """Write the marketplace, the plugin and its extension to ``root``; return ``root``. The whole + tree is written next to it first and then swapped in.""" + root = root or plugin_root() + root.parent.mkdir(parents=True, exist_ok=True) + _remove_leftovers(root) + staged = root.with_name(f".{root.name}{_STAGING_PREFIX}{uuid.uuid4().hex}") + try: + _write_tree(config, staged) + if root.is_dir(): + _carry_over(root, staged) + _fsync_tree(staged) + _publish(staged, root) + finally: + shutil.rmtree(staged, ignore_errors=True) + return root + + +def read_settings(path: Path | None = None) -> dict: + """Copilot's ``settings.json`` (``{}`` when absent). Raises ``SettingsError`` when the file + isn't a JSON object: Copilot then ignores the whole file, so ug must not rewrite it.""" + path = path or copilot_settings_path() + if not path.exists(): + return {} + try: + data = json.loads(path.read_text(encoding="utf-8")) + except (OSError, UnicodeError, json.JSONDecodeError) as exc: + raise SettingsError(f"{path} is not valid JSON") from exc + if not isinstance(data, dict): + raise SettingsError(f"{path} is not a JSON object") + return data + + +def settings_valid(path: Path | None = None) -> bool: + try: + read_settings(path) + except SettingsError: + return False + return True + + +def _object(settings: dict, key: str, path: Path) -> dict: + value = settings.setdefault(key, {}) + if not isinstance(value, dict): + raise SettingsError(f"{path}: {key} is not a JSON object") + return value + + +def _check_writable(path: Path) -> None: + if path.is_symlink(): + raise SettingsError( + f"{path} is a symlink, which ug won't replace. Edit the file it points to instead." + ) + + +def _write_settings(path: Path, settings: dict) -> None: + _check_writable(path) + mode = path.stat().st_mode & 0o777 if path.exists() else None + atomic_write_json(path, settings) + if mode is not None: + path.chmod(mode) + + +def _owns_marketplace(settings: dict, root: Path, marketplace: str = MARKETPLACE) -> bool: + marketplaces = settings.get(_MARKETPLACES_KEY) + entry = marketplaces.get(marketplace) if isinstance(marketplaces, dict) else None + source = entry.get("source") if isinstance(entry, dict) else None + return isinstance(source, dict) and source.get("path") == str(root) + + +def _installed_id(settings: dict, root: Path) -> tuple[str, str] | None: + """The marketplace and plugin of ug's installed plugin, under its current or an earlier id.""" + for marketplace, plugin in _IDS: + if ( + _owns_marketplace(settings, root, marketplace) + and (_extension_dir(root, plugin) / "extension.mjs").exists() + ): + return marketplace, plugin + return None + + +def _installed(settings: dict, root: Path) -> bool: + return _installed_id(settings, root) is not None + + +def _ug_entries(settings: dict, root: Path, ids=_IDS) -> list[tuple[str, str]]: + """ug's entries in ``settings`` under ``ids``. A marketplace of that name at another path isn't + ug's, so it and its plugin key are left alone.""" + entries = [] + for marketplace, plugin in ids: + plugin_key = f"{plugin}@{marketplace}" + marketplaces = settings.get(_MARKETPLACES_KEY) + if ( + isinstance(marketplaces, dict) + and marketplace in marketplaces + and not _owns_marketplace(settings, root, marketplace) + ): + continue + for key, entry in ((_MARKETPLACES_KEY, marketplace), (_ENABLED_KEY, plugin_key)): + value = settings.get(key) + if isinstance(value, dict) and entry in value: + entries.append((key, entry)) + return entries + + +def _drop_entries(settings: dict, entries: list[tuple[str, str]]) -> None: + for key, entry in entries: + del settings[key][entry] + if not settings[key]: + del settings[key] + + +def _has_ug_keys(settings: dict, root: Path) -> bool: + return bool(_ug_entries(settings, root)) + + +def _read_extension_config(root: Path, plugin: str = PLUGIN) -> dict | None: + try: + path = _extension_dir(root, plugin) / "config.json" + config = json.loads(path.read_text(encoding="utf-8")) + except (OSError, UnicodeError, json.JSONDecodeError): + return None + return config if isinstance(config, dict) else None + + +def _removal_requested(root: Path) -> bool: + configs = (_read_extension_config(root, plugin) for _marketplace, plugin in _IDS) + return any(config is not None and config.get("remove") is True for config in configs) + + +def enable(root: Path | None = None, settings_path: Path | None = None) -> None: + """Add ug's marketplace and turn its plugin on, keeping every other setting.""" + root = root or plugin_root() + path = settings_path or copilot_settings_path() + settings = read_settings(path) + _drop_entries(settings, _ug_entries(settings, root, _IDS[1:])) + _object(settings, _MARKETPLACES_KEY, path)[MARKETPLACE] = { + "source": {"source": "directory", "path": str(root)} + } + _object(settings, _ENABLED_KEY, path)[PLUGIN_KEY] = True + _write_settings(path, settings) + + +def _created_settings_marker(root: Path) -> Path: + return root / "created-settings" + + +def _created_settings(root: Path, path: Path) -> bool: + """Whether ug created ``path`` when it installed the plugin under ``root``.""" + try: + return _created_settings_marker(root).read_text(encoding="utf-8") == str(path) + except OSError: + return False + + +def _drop_settings(root: Path, path: Path, settings: dict) -> bool: + """Delete ``path`` when ug created it and nothing else is left in it.""" + if settings or not _created_settings(root, path): + return False + path.unlink(missing_ok=True) + return True + + +def install( + config: PluginConfig, root: Path | None = None, settings_path: Path | None = None +) -> Path: + path = settings_path or copilot_settings_path() + settings = read_settings(path) + # Checked before anything is written, so a refusal leaves no plugin behind. + for key in (_MARKETPLACES_KEY, _ENABLED_KEY): + _object(dict(settings), key, path) + marketplaces = settings.get(_MARKETPLACES_KEY) or {} + if MARKETPLACE in marketplaces and not _owns_marketplace(settings, root or plugin_root()): + raise SettingsError( + f"{path}: {_MARKETPLACES_KEY}.{MARKETPLACE} already names another plugin marketplace" + ) + _check_writable(path) + created = not path.exists() + root = write_plugin(config, root) + if created: + _created_settings_marker(root).write_text(str(path), encoding="utf-8") + enable(root, path) + # An earlier build's copy, which the settings no longer name. + shutil.rmtree(_plugin_dir(root, _LEGACY_PLUGIN), ignore_errors=True) + return root + + +def remove( + root: Path | None = None, + settings_path: Path | None = None, + wait: Callable[[float], None] | None = None, +) -> bool: + """Remove ug's settings keys, give the app time to unload the extension, then remove the + plugin directory, under the current and earlier ids. A marketplace of ug's name at another + path isn't ug's, so it and its plugin key stay. Returns whether anything was removed.""" + root = root or plugin_root() + path = settings_path or copilot_settings_path() + settings = read_settings(path) + entries = _ug_entries(settings, root) + _drop_entries(settings, entries) + changed = bool(entries) + if changed and not _drop_settings(root, path, settings): + _write_settings(path, settings) + if root.exists(): + if changed: + (wait or time.sleep)(UNLOAD_WAIT_S) + shutil.rmtree(root) + changed = True + return changed + + +_SYNC_LOCK_FILE = "app-sync.lock" +# As app-rows.mjs's LOCK_STALE_MS: an older lock is from an extension that died holding it. +_SYNC_LOCK_STALE_S = 60.0 + + +def _take_sync_lock(path: Path, owner: str) -> bool: + try: + with path.open("x", encoding="utf-8") as file: + file.write(owner) + return True + except FileExistsError: + pass + try: + stale = path.read_text(encoding="utf-8") + if time.time() - path.stat().st_mtime < _SYNC_LOCK_STALE_S: + return False + # Only one process can move a stale lock away; a lock replaced meanwhile goes back. + moved = path.with_name(f"{path.name}.{owner}") + os.replace(path, moved) + if moved.read_text(encoding="utf-8") != stale: + os.replace(moved, path) + return False + moved.unlink() + with path.open("x", encoding="utf-8") as file: + file.write(owner) + return True + except OSError: + return False + + +@contextmanager +def sync_lock( + root: Path | None = None, + *, + timeout: float = 20.0, + sleep: Callable[[float], None] = time.sleep, + clock: Callable[[], float] = time.monotonic, +) -> Iterator[bool]: + """Hold the lock the plugin's extensions sync and remove under, so none writes the app's rows + meanwhile. Yields whether it got the lock within ``timeout``; an extension still syncing then + stops on its own once the plugin is marked for removal.""" + directory = _extension_dir(root or plugin_root()) + if not directory.is_dir(): + yield False + return + path = directory / _SYNC_LOCK_FILE + owner = f"ug-{os.getpid()}-{uuid.uuid4().hex}" + deadline = clock() + timeout + while not (held := _take_sync_lock(path, owner)) and clock() < deadline: + sleep(0.2) + try: + yield held + finally: + if held: + try: + if path.read_text(encoding="utf-8") == owner: + path.unlink() + except OSError: + pass + + +def defer_remove(root: Path | None = None, settings_path: Path | None = None) -> bool: + """Ask the extension to remove ug's rows from the app and then ug's settings keys the next time + it starts, for when the app isn't running. The plugin stays enabled so that the extension runs. + Returns whether there was an installed plugin to mark.""" + root = root or plugin_root() + path = settings_path or copilot_settings_path() + settings = read_settings(path) + # The extension rewrites settings.json under the same rules, so ug refuses what it would. + _check_writable(path) + installed = _installed_id(settings, root) + if installed is None: + return False + # An earlier build's install has its extension under its own plugin name. + plugin = installed[1] + config = _read_extension_config(root, plugin) + if config is None: + return False + config["remove"] = True + config["pluginRoot"] = str(root) + atomic_write_json(_extension_dir(root, plugin) / "config.json", config) + return True + + +def cleanup_leftover(root: Path | None = None, settings_path: Path | None = None) -> bool: + """Delete the plugin directory once the extension has finished a deferred remove, which leaves + it behind. Returns whether it was deleted.""" + root = root or plugin_root() + if not _removal_requested(root): + return False + try: + settings = read_settings(settings_path) + except SettingsError: + return False + if _has_ug_keys(settings, root): + return False + _drop_settings(root, settings_path or copilot_settings_path(), settings) + shutil.rmtree(root) + return True + + +def status(root: Path | None = None, settings_path: Path | None = None) -> Status: + root = root or plugin_root() + try: + settings = read_settings(settings_path) + except SettingsError: + return "invalid_settings" + installed = _installed_id(settings, root) + if installed is None: + return "not_installed" + marketplace, plugin = installed + enabled = settings.get(_ENABLED_KEY) + if isinstance(enabled, dict) and enabled.get(f"{plugin}@{marketplace}") is True: + return "removal_pending" if _removal_requested(root) else "installed" + return "disabled" diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs new file mode 100644 index 000000000..275f2aac5 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs @@ -0,0 +1,153 @@ +import { randomUUID } from "node:crypto"; +import { chmodSync, lstatSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs"; +import { basename, dirname, join } from "node:path"; + +import { + LOCK_FILE, + RESTART_MARKER_FILE, + SYNC_STAMP_FILE, + connectApp, + copilotHome, + deleteProviders, + readEndpoint, + tryLock, +} from "./app-rows.mjs"; + +export const MARKETPLACE = "databricks-unity-gateway"; +export const PLUGIN_KEY = "unity-gateway@databricks-unity-gateway"; +// Earlier builds' ids, which a removal takes out too. +const IDS = [ + [MARKETPLACE, PLUGIN_KEY], + ["unity-gateway", "databricks@unity-gateway"], +]; +const MARKETPLACES_KEY = "extraKnownMarketplaces"; +const ENABLED_KEY = "enabledPlugins"; + +export class SettingsError extends Error {} + +const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value); + +export function settingsPath(home = copilotHome()) { + return join(home, "settings.json"); +} + +// Copilot ignores a settings file that isn't a JSON object, so one is never rewritten. +export function readSettings(path) { + let stat; + try { + stat = lstatSync(path); + } catch (error) { + if (error.code === "ENOENT") return null; + throw error; + } + if (stat.isSymbolicLink()) { + throw new SettingsError(`${path} is a symlink, which ug won't replace. Edit the file it points to instead.`); + } + let settings; + try { + settings = JSON.parse(readFileSync(path, "utf8")); + } catch { + throw new SettingsError(`${path} is not valid JSON`); + } + if (!isObject(settings)) throw new SettingsError(`${path} is not a JSON object`); + return { settings, mode: stat.mode & 0o777 }; +} + +// A marketplace of ug's name at another path isn't ug's, so it and its plugin key stay. +function ugKeys(settings, pluginRoot) { + const marketplaces = settings[MARKETPLACES_KEY]; + return IDS.flatMap(([marketplace, pluginKey]) => { + if (isObject(marketplaces) && Object.hasOwn(marketplaces, marketplace)) { + const source = marketplaces[marketplace]?.source; + if (!isObject(source) || source.path !== pluginRoot) return []; + } + return [ + [MARKETPLACES_KEY, marketplace], + [ENABLED_KEY, pluginKey], + ].filter(([key, entry]) => isObject(settings[key]) && Object.hasOwn(settings[key], entry)); + }); +} + +export function hasUgKeys(settings, pluginRoot) { + return ugKeys(settings, pluginRoot).length > 0; +} + +function writeSettings(path, settings, mode) { + const temp = join(dirname(path), `.${basename(path)}.${process.pid}.${randomUUID()}.tmp`); + try { + writeFileSync(temp, JSON.stringify(settings, null, 2), { flag: "wx", mode }); + chmodSync(temp, mode); + renameSync(temp, path); + } catch (error) { + try { + unlinkSync(temp); + } catch {} + throw error; + } +} + +export function removeSettingsKeys(path, pluginRoot) { + const current = readSettings(path); + if (!current) return false; + const { settings, mode } = current; + const keys = ugKeys(settings, pluginRoot); + if (!keys.length) return false; + for (const [key, entry] of keys) { + delete settings[key][entry]; + if (!Object.keys(settings[key]).length) delete settings[key]; + } + writeSettings(path, settings, mode); + return true; +} + +// The keys go only once the rows are gone: without the plugin, no later start could delete them. +export async function removeOnce(config, { home = copilotHome(), stateDir, now = Date.now, ...connect } = {}) { + if (!config.remove) return "skipped"; + const path = settingsPath(home); + const release = tryLock(join(stateDir, LOCK_FILE), now); + if (!release) return "busy"; + try { + let current; + let refusal = null; + try { + current = readSettings(path); + } catch (error) { + if (!(error instanceof SettingsError)) throw error; + refusal = error; + } + if (!refusal && !(current && hasUgKeys(current.settings, config.pluginRoot))) return "done"; + const providerIds = config.app?.ownedProviderIds ?? []; + if (providerIds.length) { + const endpoint = readEndpoint(home); + if (!endpoint) return "not_running"; + const app = await connectApp(endpoint, connect); + try { + await deleteProviders(app, providerIds); + } finally { + app.close(); + } + // A later install must write the rows and restart the app again, not match the last sync. + for (const name of [SYNC_STAMP_FILE, RESTART_MARKER_FILE]) { + try { + unlinkSync(join(stateDir, name)); + } catch {} + } + } + if (refusal) throw refusal; + removeSettingsKeys(path, config.pluginRoot); + return "removed"; + } finally { + release(); + } +} + +export async function removeInBackground(config, options, log) { + try { + return await removeOnce(config, options); + } catch (error) { + try { + log(`Databricks models: couldn't remove Unity Gateway from the GitHub Copilot app: ${error?.message ?? error}`); + } catch {} + return error instanceof SettingsError ? "refused" : "failed"; + } +} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs new file mode 100644 index 000000000..a126a0b32 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs @@ -0,0 +1,91 @@ +import { execFile, spawn } from "node:child_process"; +import { existsSync } from "node:fs"; +import { join } from "node:path"; + +import { copilotHome, readAppPid } from "./app-rows.mjs"; + +export const RESTARTING_MESSAGE = + "The GitHub Copilot app restarts once, as soon as no chat is running a turn, to finish loading the Databricks models."; +export const RESTART_HINT = "Restart the GitHub Copilot app to show the Databricks model names and icons."; +export const RESTART_LOG_FILE = "app-restart.log"; +const COMMAND_TIMEOUT_MS = 10_000; +const LINUX_APP = "/usr/bin/github"; +const MAC_BUNDLE_ID = "com.github.githubapp"; + +export function isAlive(pid) { + try { + process.kill(pid, 0); + return true; + } catch (error) { + return error.code === "EPERM"; + } +} + +// Resolves to the command's output, or null when it failed. +function runCommand(command, args) { + return new Promise((resolve) => { + execFile(command, args, { timeout: COMMAND_TIMEOUT_MS, windowsHide: true }, (error, stdout) => + resolve(error ? null : String(stdout)), + ); + }); +} + +// What the restart needs to close the app and start it again, or null when it can't here. +export async function planRestart({ + home = copilotHome(), + platform = process.platform, + env = process.env, + alive = isAlive, + exists = existsSync, + run = runCommand, +} = {}) { + const pid = readAppPid(home); + if (!pid || pid === process.pid || !alive(pid)) return null; + if (platform === "linux") { + return (env.DISPLAY || env.WAYLAND_DISPLAY) && exists(LINUX_APP) ? { platform, pid, app: LINUX_APP } : null; + } + if (platform === "darwin") return { platform, pid, app: MAC_BUNDLE_ID }; + if (platform === "win32") { + const command = `(Get-Process -Id ${pid}).Path`; + const app = (await run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", command]))?.trim(); + return app ? { platform, pid, app } : null; + } + return null; +} + +// The app stops the extension soon after its rows change, so the wait for idle and the restart run +// in a detached `ug copilot-app-restart`. The extension's own executable is the Copilot runtime, +// which runs no scripts. +export function startHelper(target, stateDir, ugPath) { + return new Promise((resolve) => { + const argument = JSON.stringify({ ...target, log: join(stateDir, RESTART_LOG_FILE) }); + const child = spawn(ugPath, ["copilot-app-restart", argument], { + detached: true, + stdio: "ignore", + windowsHide: true, + }); + child.once("spawn", () => { + child.unref(); + resolve(true); + }); + child.once("error", () => resolve(false)); + }); +} + +// Started as soon as the sync returns, before the app can stop this process. Never throws. +export async function launchRestart({ home = copilotHome(), stateDir, ugPath, plan = planRestart, start = startHelper } = {}) { + try { + const target = ugPath ? await plan({ home }) : null; + if (!target) return "unsupported"; + return (await start({ ...target, home }, stateDir, ugPath)) ? "started" : "failed"; + } catch { + return "failed"; + } +} + +// Posts the line for ``launchRestart``'s result. Never throws. +export async function tellRestart(session, result) { + try { + await session.log(result === "started" ? RESTARTING_MESSAGE : RESTART_HINT, { level: "info" }); + } catch {} +} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs new file mode 100644 index 000000000..4c84225f2 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -0,0 +1,342 @@ +import { createHash, randomUUID } from "node:crypto"; +import { readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; + +export const REQUEST_TIMEOUT_MS = 15_000; +export const LOCK_STALE_MS = 60_000; +export const LOCK_FILE = "app-sync.lock"; +export const SYNC_STAMP_FILE = "app-sync.json"; +export const RESTART_MARKER_FILE = "app-restart.json"; + +export function copilotHome(env = process.env) { + return env.COPILOT_HOME || join(homedir(), ".copilot"); +} + +// A run file holds its value, and in some app versions the app's pid after it. +function runFields(home, name) { + return readFileSync(join(home, "run", name), "utf8").trim().split(/\s+/); +} + +function runValue(home, name) { + return runFields(home, name)[0] || null; +} + +function pidField(home, name, index) { + try { + const pid = Number.parseInt(runFields(home, name)[index], 10); + return Number.isInteger(pid) && pid > 1 ? pid : null; + } catch { + return null; + } +} + +// The app writes its own pid to run/app-owner.pid; ws.port carries none in app 1.4.2. +export function readAppPid(home) { + return pidField(home, "app-owner.pid", 0) ?? pidField(home, "ws.port", 1); +} + +export function readEndpoint(home) { + try { + const port = Number.parseInt(runValue(home, "ws.port"), 10); + const token = runValue(home, "ws.token"); + return Number.isInteger(port) && token ? { port, token } : null; + } catch { + return null; + } +} + +export class AppError extends Error {} + +class AppConnection { + constructor(ws, timeoutMs) { + this._ws = ws; + this._timeoutMs = timeoutMs; + this._backlog = []; + this._waiter = null; + this.closed = false; + ws.onmessage = (event) => { + let message; + try { + message = JSON.parse(typeof event.data === "string" ? event.data : String(event.data)); + } catch { + return; + } + if (message && typeof message === "object") this._deliver(message); + }; + ws.onclose = () => { + this.closed = true; + this._waiter?.reject(new AppError("The GitHub Copilot app closed the connection.")); + }; + } + + _deliver(message) { + if (this._waiter?.matches(message)) { + const waiter = this._waiter; + this._waiter = null; + waiter.resolve(message); + } else { + this._backlog.push(message); + } + } + + wait(matches) { + const index = this._backlog.findIndex(matches); + if (index >= 0) return Promise.resolve(this._backlog.splice(index, 1)[0]); + if (this.closed) return Promise.reject(new AppError("The GitHub Copilot app closed the connection.")); + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this._waiter = null; + reject(new AppError("The GitHub Copilot app didn't answer.")); + }, this._timeoutMs); + const done = (fn) => (value) => { + clearTimeout(timer); + fn(value); + }; + this._waiter = { matches, resolve: done(resolve), reject: done(reject) }; + }); + } + + async request(message, reply, match = {}) { + for (let attempt = 0; ; attempt++) { + const requestId = `ug-${randomUUID()}`; + this._ws.send(JSON.stringify({ ...message, request_id: requestId })); + // The app echoes request_id on its replies, but not on errors about the message's format, + // which then answer the pending request. + const answer = await this.wait((candidate) => + candidate.type === "error" + ? candidate.request_id === requestId || candidate.request_id === undefined + : candidate.request_id === requestId && + candidate.type === reply && + Object.entries(match).every(([name, value]) => candidate[name] === value), + ); + if (answer.type !== "error") return answer; + const error = String(answer.message ?? ""); + // The app refuses a provider change while another one is being applied. + if (!error.includes("still in progress") || attempt === 5) { + throw new AppError(`The GitHub Copilot app refused ${message.type}: ${error}`); + } + await new Promise((resolve) => setTimeout(resolve, 200 * 2 ** attempt)); + } + } + + close() { + try { + this._ws.close(); + } catch {} + } +} + +export async function connectApp(endpoint, { WebSocketImpl, timeoutMs = REQUEST_TIMEOUT_MS } = {}) { + const Impl = WebSocketImpl ?? globalThis.WebSocket; + if (!Impl) throw new AppError("This Copilot runtime has no WebSocket client."); + const url = `ws://127.0.0.1:${endpoint.port}/?token=${encodeURIComponent(endpoint.token)}&requester_id=ug&session_channels=false`; + // No Origin: the app refuses any origin but its own window. Node's WebSocket sends none. + const ws = new Impl(url); + const app = new AppConnection(ws, timeoutMs); + try { + await app.wait((message) => message.type === "server_hello"); + } catch (error) { + app.close(); + throw error; + } + return app; +} + +async function listModels(app, providerId) { + const { models } = await app.request( + { type: "list_provider_models", provider_id: providerId }, + "provider_models_list", + ); + if (!Array.isArray(models)) throw new AppError("The GitHub Copilot app listed its models in a new shape."); + return models.filter((m) => m && typeof m.id === "string" && m.providerId === providerId); +} + +async function deleteProvider(app, providerId) { + await app.request({ type: "delete_model_provider", provider_id: providerId }, "model_provider_deleted", { + provider_id: providerId, + }); +} + +async function listProviders(app) { + const { providers } = await app.request({ type: "list_model_providers" }, "model_providers_list"); + if (!Array.isArray(providers)) throw new AppError("The GitHub Copilot app listed its providers in a new shape."); + return new Map(providers.filter((p) => p && typeof p.id === "string").map((p) => [p.id, p])); +} + +export async function deleteProviders(app, providerIds) { + const existing = await listProviders(app); + for (const id of providerIds) { + if (existing.has(id)) await deleteProvider(app, id); + } +} + +// The app's list replies can carry fields of their own, so a row matches when every field ug writes +// has the same value. +function matches(wanted, actual) { + if (wanted === null || typeof wanted !== "object") return wanted === actual; + if (Array.isArray(wanted)) { + return Array.isArray(actual) && actual.length === wanted.length && wanted.every((value, i) => matches(value, actual[i])); + } + return ( + actual !== null && + typeof actual === "object" && + !Array.isArray(actual) && + Object.entries(wanted).every(([name, value]) => matches(value, actual[name])) + ); +} + +// Writes ``rows`` (config.app) to the app, and deletes the providers and models of ug's that +// aren't in it. Upserts with fixed ids, so running it again changes nothing. Returns whether the +// app's rows differed from ``rows``. +class RemovalRequested extends Error {} + +// ug marks the plugin's config for removal before it deletes ug's rows from a running app, so a +// sync already under way stops before it writes them back. +export function removalRequested(stateDir) { + try { + return JSON.parse(readFileSync(join(stateDir, "config.json"), "utf8")).remove === true; + } catch { + return false; + } +} + +export async function syncRows(app, rows, stop = () => false) { + const write = (message, reply, match) => { + if (stop()) throw new RemovalRequested(); + return app.request(message, reply, match); + }; + const existing = await listProviders(app); + const wanted = new Set(rows.providers.map((entry) => entry.provider.id)); + let changed = false; + for (const id of rows.ownedProviderIds) { + if (existing.has(id) && !wanted.has(id)) { + await deleteProvider(app, id); + changed = true; + } + } + for (const { provider, secret, models } of rows.providers) { + const message = { type: "upsert_model_provider", provider }; + if (secret && !existing.get(provider.id)?.hasSecret) message.secret = secret; + if (message.secret || !matches(provider, existing.get(provider.id))) changed = true; + await write(message, "model_provider_upserted"); + const wantedModels = new Set(models.map((model) => model.id)); + const listed = new Map(); + for (const model of await listModels(app, provider.id)) { + listed.set(model.id, model); + if (!wantedModels.has(model.id)) { + await app.request({ type: "delete_provider_model", model_id: model.id }, "provider_model_deleted", { + model_id: model.id, + }); + changed = true; + } + } + for (const model of models) { + if (!matches(model, listed.get(model.id))) changed = true; + await write({ type: "upsert_provider_model", model }, "provider_model_upserted"); + } + } + return changed; +} + +// Returns the function that releases the lock, or null while another extension process holds it. +// The lock file names its owner, so a process whose lock was taken over as stale can't release the +// new owner's. +export function tryLock(path, now) { + const owner = `${process.pid}-${randomUUID()}`; + const release = () => { + try { + if (readFileSync(path, "utf8") === owner) unlinkSync(path); + } catch {} + }; + const take = () => { + writeFileSync(path, owner, { flag: "wx" }); + return release; + }; + try { + return take(); + } catch (error) { + if (error.code !== "EEXIST") throw error; + } + try { + const stale = readFileSync(path, "utf8"); + if (now() - statSync(path).mtimeMs < LOCK_STALE_MS) return null; + // Only one process can move the stale lock away. If it was replaced in the meantime, the new + // owner's lock goes back. + const moved = `${path}.${owner}`; + renameSync(path, moved); + if (readFileSync(moved, "utf8") !== stale) { + renameSync(moved, path); + return null; + } + unlinkSync(moved); + return take(); + } catch { + return null; + } +} + +function readStamp(path) { + try { + return JSON.parse(readFileSync(path, "utf8")).digest; + } catch { + return null; + } +} + +function writeStamp(path, digest) { + const temp = `${path}.${process.pid}.tmp`; + writeFileSync(temp, JSON.stringify({ digest })); + renameSync(temp, path); +} + +// Every session runs its own extension process. Each one syncs only when the app's launch token or +// the rows changed since the last sync, and one at a time. "changed" tells the one process that +// first wrote these rows to the app to restart it: the window loads the picker's providers only +// when it starts, before any extension runs. +export async function syncOnce(config, { home = copilotHome(), stateDir, now = Date.now, ...connect } = {}) { + const rows = config.app; + if (!rows || !rows.providers?.length && !rows.ownedProviderIds?.length) return "skipped"; + const endpoint = readEndpoint(home); + if (!endpoint) return "not_running"; + const rowsDigest = createHash("sha256").update(JSON.stringify(rows)).digest("hex"); + const digest = createHash("sha256").update(JSON.stringify(rows)).update("\0").update(endpoint.token).digest("hex"); + const stampPath = join(stateDir, SYNC_STAMP_FILE); + if (readStamp(stampPath) === digest) return "current"; + const release = tryLock(join(stateDir, LOCK_FILE), now); + if (!release) return "busy"; + try { + const stop = () => removalRequested(stateDir); + if (stop()) return "removing"; + const app = await connectApp(endpoint, connect); + let changed; + try { + changed = await syncRows(app, rows, stop); + } catch (error) { + if (error instanceof RemovalRequested) return "removing"; + throw error; + } finally { + app.close(); + } + // Keyed by the rows alone, so the restarted app's sync can't restart it again, even if the + // app's lists never match the rows exactly. + const markerPath = join(stateDir, RESTART_MARKER_FILE); + const restart = changed && readStamp(markerPath) !== rowsDigest; + if (restart) writeStamp(markerPath, rowsDigest); + writeStamp(stampPath, digest); + return restart ? "changed" : "synced"; + } finally { + release(); + } +} + +export async function syncInBackground(config, options, log) { + try { + return await syncOnce(config, options); + } catch (error) { + try { + log(`Databricks models: couldn't update the GitHub Copilot app's model picker: ${error?.message ?? error}`); + } catch {} + return "failed"; + } +} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs new file mode 100644 index 000000000..a092bb652 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -0,0 +1,71 @@ +import { setTimeout as delay } from "node:timers/promises"; +import { fileURLToPath } from "node:url"; + +import { joinSession } from "@github/copilot-sdk/extension"; + +import { removeInBackground } from "./app-remove.mjs"; +import { launchRestart, tellRestart } from "./app-restart.mjs"; +import { syncInBackground } from "./app-rows.mjs"; +import { + buildProviders, + createTokenSource, + hintFor, + isAuthRejection, + loadConfig, + withLoginHint, +} from "./ug-plugin.mjs"; + +const config = loadConfig(new URL("./config.json", import.meta.url)); +const syncOptions = { stateDir: fileURLToPath(new URL(".", import.meta.url)) }; +const logSyncFailure = (message) => console.error(message); +// The extension can start before the app publishes its WebSocket endpoint. +const RETRY_DELAYS_MS = [2_000, 5_000, 10_000]; + +async function withRetries(attempt) { + let result = await attempt(); + for (const ms of RETRY_DELAYS_MS) { + if (!["not_running", "failed"].includes(result)) break; + await delay(ms); + result = await attempt(); + } + return result; +} + +if (config.remove) { + const removal = withRetries(() => removeInBackground(config, syncOptions, logSyncFailure)); + try { + await joinSession(); + } catch (error) { + logSyncFailure(`Databricks models: couldn't join the session: ${error?.message ?? error}`); + } + await removal; +} else { + const restart = withRetries(() => syncInBackground(config, syncOptions, logSyncFailure)).then((synced) => + synced === "changed" ? launchRestart({ ...syncOptions, ugPath: config.ugPath }) : null, + ); + let session; + // session.log is async, and a rejected one must not end the extension. + const say = (message) => + Promise.resolve() + .then(() => session.log(message, { level: "error" })) + .catch(() => {}); + const tokenSource = createTokenSource(config); + const bearerTokenProvider = withLoginHint(tokenSource, (error) => { + const hint = hintFor(error, config); + if (!session || !hint) return false; + say(hint); + return true; + }); + session = await joinSession({ + providers: buildProviders(config, bearerTokenProvider), + models: config.models, + }); + session.on((event) => { + if (!isAuthRejection(event)) return; + tokenSource.invalidate(); + // The app's own message for a 401 names Copilot's API-key settings, not Databricks sign-in. + if (event.type === "session.error") say(config.loginHint); + }); + const launched = await restart; + if (launched) await tellRestart(session, launched); +} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs new file mode 100644 index 000000000..75e5c7e4b --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs @@ -0,0 +1,151 @@ +import { execFile } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { delimiter, dirname } from "node:path"; + +export const REFRESH_MARGIN_MS = 120_000; +export const DEFAULT_TOKEN_TTL_MS = 300_000; +export const MIN_TOKEN_REUSE_MS = 30_000; +const TOKEN_COMMAND_TIMEOUT_MS = 30_000; + +export function loadConfig(url) { + return JSON.parse(readFileSync(url, "utf8")); +} + +export function jwtExpiryMs(token) { + const payload = token.split(".")[1]; + if (!payload) return null; + try { + const { exp } = JSON.parse(Buffer.from(payload, "base64url").toString("utf8")); + return typeof exp === "number" ? exp * 1000 : null; + } catch { + return null; + } +} + +// On Windows the variable is usually `Path`; a second, differently cased key would leave the child +// with two. +export function childEnv(config, env = process.env) { + const name = Object.keys(env).find((key) => key.toUpperCase() === "PATH") ?? "PATH"; + const databricksDir = config.databricksPath ? [dirname(config.databricksPath)] : []; + const path = [...(config.pathDirs || []), ...databricksDir, env[name]].filter(Boolean).join(delimiter); + return { ...env, [name]: path }; +} + +export function runCommand(argv, env) { + return new Promise((resolve, reject) => { + execFile(argv[0], argv.slice(1), { env, timeout: TOKEN_COMMAND_TIMEOUT_MS }, (error, stdout, stderr) => { + if (error) { + const detail = String(stderr || error.message).trim().split("\n").pop(); + const failure = new Error(`ug auth-token failed: ${detail}`); + // A number is ug's exit status; a string such as ENOENT means it didn't start. + failure.code = error.killed ? "TIMEOUT" : error.code; + reject(failure); + return; + } + const token = String(stdout).trim(); + if (token) resolve(token); + else reject(new Error("ug auth-token printed no token")); + }); + }); +} + +export const FORCE_REFRESH_FLAG = "--force-refresh"; + +// One mint at a time: concurrent requests share it, and the token is reused until shortly +// before it expires. After invalidate(), the next mint asks for a new token rather than the +// Databricks CLI's cached one, and a mint already running can't cache the rejected one. +export function createTokenSource(config, { run = runCommand, now = Date.now } = {}) { + let cached = null; + let pending = null; + let forceRefresh = false; + let generation = 0; + function token() { + if (cached && now() < cached.refreshAt) return Promise.resolve(cached.value); + if (!pending) { + const argv = forceRefresh ? [...config.tokenCommand, FORCE_REFRESH_FLAG] : config.tokenCommand; + forceRefresh = false; + const started = generation; + const mint = run(argv, childEnv(config)) + .then((value) => { + if (started === generation) { + const expiry = jwtExpiryMs(value); + // A token the CLI hands out close to its expiry is still reused briefly, rather than + // minting again for every request. + const refreshAt = expiry + ? Math.max(expiry - REFRESH_MARGIN_MS, Math.min(expiry, now() + MIN_TOKEN_REUSE_MS)) + : now() + DEFAULT_TOKEN_TTL_MS; + cached = { value, refreshAt }; + } + return value; + }) + .finally(() => { + if (pending === mint) pending = null; + }); + pending = mint; + } + return pending; + } + token.invalidate = () => { + generation++; + cached = null; + pending = null; + forceRefresh = true; + }; + return token; +} + +export function isAuthRejection(event) { + return ( + (event?.type === "session.error" || event?.type === "model.call_failure") && + event.data?.statusCode === 401 + ); +} + +// The hint for a failed `ug auth-token`: sign in again after an auth failure, or rerun configure +// when ug is gone. A timeout or other failure gets none. +export function hintFor(error, config) { + if (error?.code === "ENOENT") return config.missingUgHint; + if (typeof error?.code === "number") return config.loginHint; + return null; +} + +// The app retries a failing token provider several times, so a hint is shown once per run of +// failures. ``showHint(error)`` returns whether it showed one; until it does, the next failure +// tries again. +export function withLoginHint(token, showHint) { + let hinted = false; + return async function bearerToken() { + try { + const value = await token(); + hinted = false; + return value; + } catch (error) { + if (!hinted) { + hinted = await Promise.resolve() + .then(() => Boolean(showHint(error))) + .catch(() => false); + } + throw error; + } + }; +} + +// The gateway logs it as the usage table's client session id. An admin's header of that name wins. +export const SESSION_ID_HEADER = "session-id"; + +export function withSessionId(headers = {}, sessionId) { + if (!sessionId || Object.keys(headers).some((name) => name.toLowerCase() === SESSION_ID_HEADER)) { + return { ...headers }; + } + return { [SESSION_ID_HEADER]: sessionId, ...headers }; +} + +// ``sessionId`` is the app session this extension process serves, which the runtime passes in +// SESSION_ID: joinSession resumes that session. +export function buildProviders(config, bearerTokenProvider, sessionId = process.env.SESSION_ID) { + return config.providers.map((provider) => ({ + ...provider, + headers: withSessionId(provider.headers, sessionId), + bearerTokenProvider, + })); +} diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs new file mode 100644 index 000000000..3df503ae1 --- /dev/null +++ b/tests/copilot_app_fake.mjs @@ -0,0 +1,170 @@ +import { createHash } from "node:crypto"; +import { mkdirSync, writeFileSync } from "node:fs"; +import http from "node:http"; +import { join } from "node:path"; + +function encode(text) { + const payload = Buffer.from(text); + const length = payload.length; + let header; + if (length < 126) header = Buffer.from([0x81, length]); + else if (length < 65536) { + header = Buffer.from([0x81, 126, 0, 0]); + header.writeUInt16BE(length, 2); + } else { + header = Buffer.alloc(10); + header[0] = 0x81; + header[1] = 127; + header.writeBigUInt64BE(BigInt(length), 2); + } + return Buffer.concat([header, payload]); +} + +function parser(onText, onClose) { + let buffer = Buffer.alloc(0); + return (chunk) => { + buffer = Buffer.concat([buffer, chunk]); + while (buffer.length >= 2) { + const opcode = buffer[0] & 0x0f; + let length = buffer[1] & 0x7f; + let offset = 2; + if (length === 126) { + if (buffer.length < 4) return; + length = buffer.readUInt16BE(2); + offset = 4; + } else if (length === 127) { + if (buffer.length < 10) return; + length = Number(buffer.readBigUInt64BE(2)); + offset = 10; + } + if (buffer.length < offset + 4 + length) return; + const key = buffer.subarray(offset, offset + 4); + const payload = Buffer.from(buffer.subarray(offset + 4, offset + 4 + length)); + for (let i = 0; i < length; i++) payload[i] ^= key[i % 4]; + buffer = buffer.subarray(offset + 4 + length); + if (opcode === 1) onText(payload.toString("utf8")); + if (opcode === 8) onClose(); + } + }; +} + +// The app's WebSocket API as recorded from app 1.4.2: provider and model CRUD, its token and +// origin checks, session events for connections with session channels on, and injectable errors. +export class FakeApp { + constructor() { + this.token = "per-launch-token"; + this.providers = new Map(); + this.models = new Map(); + this.secrets = new Map(); + this.received = []; + this.handshakes = []; + this.connections = 0; + this.sockets = new Set(); + this.busy = 0; + this.fail = null; + this.drop = null; + this.beforeReply = {}; + // Called with each request before it's answered, as another client acting meanwhile. + this.onRequest = null; + this.server = http.createServer(); + this.server.on("upgrade", (request, socket) => this._upgrade(request, socket)); + } + + listen() { + return new Promise((resolve) => this.server.listen(0, "127.0.0.1", resolve)); + } + + get port() { + return this.server.address().port; + } + + writeRunFiles(home) { + mkdirSync(join(home, "run"), { recursive: true }); + writeFileSync(join(home, "run", "ws.port"), `${this.port}`); + writeFileSync(join(home, "run", "ws.token"), `${this.token}`); + writeFileSync(join(home, "run", "app-owner.pid"), "4242"); + } + + _upgrade(request, socket) { + this.handshakes.push(request.headers); + const params = new URL(request.url, "http://x").searchParams; + const token = params.get("token"); + if (request.headers.origin || token !== this.token) { + socket.end("HTTP/1.1 403 Forbidden\r\n\r\n"); + return; + } + this.connections++; + this.sockets.add(socket); + socket.on("close", () => { + this.sockets.delete(socket); + }); + const accept = createHash("sha1") + .update(request.headers["sec-websocket-key"] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + .digest("base64"); + socket.write( + `HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`, + ); + const send = (message) => socket.write(encode(JSON.stringify(message))); + send({ type: "server_hello", connection_id: this.connections }); + send({ type: "keep_awake_changed" }); + socket.on("error", () => {}); + socket.on( + "data", + parser((text) => { + const message = JSON.parse(text); + this.received.push(message); + if (message.type === this.drop) { + socket.destroy(); + return; + } + this.onRequest?.(message); + for (const frame of this.beforeReply[message.type] ?? []) send(frame); + delete this.beforeReply[message.type]; + const reply = this._reply(message); + if (!("request_id" in reply)) reply.request_id = message.request_id; + send(reply); + }, () => socket.end(Buffer.from([0x88, 0]))), + ); + } + + _reply(message) { + const kind = message.type; + if (this.fail === kind) return { type: "error", message: "refused", request_id: message.request_id }; + if (kind.startsWith("upsert") && this.busy) { + this.busy--; + return { type: "error", message: "provider update still in progress" }; + } + if (kind === "list_model_providers") return { type: "model_providers_list", providers: [...this.providers.values()] }; + if (kind === "upsert_model_provider") { + const { provider } = message; + if (message.secret) this.secrets.set(provider.id, message.secret.value); + this.providers.set(provider.id, { ...provider, hasSecret: this.secrets.has(provider.id) }); + return { type: "model_provider_upserted", provider: this.providers.get(provider.id) }; + } + if (kind === "delete_model_provider") { + this.providers.delete(message.provider_id); + this.secrets.delete(message.provider_id); + for (const [id, model] of this.models) if (model.providerId === message.provider_id) this.models.delete(id); + return { type: "model_provider_deleted", provider_id: message.provider_id }; + } + if (kind === "list_provider_models") return { type: "provider_models_list", models: [...this.models.values()] }; + if (kind === "upsert_provider_model") { + this.models.set(message.model.id, message.model); + return { type: "provider_model_upserted", model: message.model }; + } + if (kind === "delete_provider_model") { + this.models.delete(message.model_id); + return { type: "provider_model_deleted", model_id: message.model_id }; + } + return { type: "error", message: `unknown variant \`${kind}\`` }; + } + + sent(kind) { + return this.received.filter((message) => message.type === kind); + } + + close() { + for (const socket of this.sockets) socket.destroy(); + this.server.close(); + } +} diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs new file mode 100644 index 000000000..e2de5b6f2 --- /dev/null +++ b/tests/copilot_app_plugin.test.mjs @@ -0,0 +1,269 @@ +import assert from "node:assert/strict"; +import { delimiter } from "node:path"; +import { test } from "node:test"; + +import { + DEFAULT_TOKEN_TTL_MS, + FORCE_REFRESH_FLAG, + REFRESH_MARGIN_MS, + SESSION_ID_HEADER, + buildProviders, + childEnv, + createTokenSource, + hintFor, + isAuthRejection, + jwtExpiryMs, + MIN_TOKEN_REUSE_MS, + runCommand, + withLoginHint, + withSessionId, +} from "../src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs"; + +const config = { + providers: [{ name: "p1", type: "anthropic", baseUrl: "https://ws/ai-gateway/anthropic", headers: { A: "1" } }], + tokenCommand: ["/opt/ug/bin/ug", "auth-token", "--host", "https://ws"], + pathDirs: ["/opt/ug/bin", "/opt/databricks"], +}; + +const jwt = (exp) => `h.${Buffer.from(JSON.stringify({ exp })).toString("base64url")}.s`; + +test("jwtExpiryMs reads exp and tolerates opaque tokens", () => { + assert.equal(jwtExpiryMs(jwt(1000)), 1_000_000); + assert.equal(jwtExpiryMs("dapi-opaque"), null); + assert.equal(jwtExpiryMs("a.not-base64-json.b"), null); +}); + +test("childEnv puts the Databricks CLI's dir on PATH", () => { + const env = childEnv({ ...config, databricksPath: "/opt/homebrew/bin/databricks" }, { PATH: "/usr/bin" }); + assert.equal(env.PATH, ["/opt/ug/bin", "/opt/databricks", "/opt/homebrew/bin", "/usr/bin"].join(delimiter)); +}); + +test("childEnv puts the configured dirs ahead of PATH", () => { + const env = childEnv(config, { PATH: "/usr/bin", HOME: "/h" }); + assert.equal(env.PATH, ["/opt/ug/bin", "/opt/databricks", "/usr/bin"].join(delimiter)); + assert.equal(env.HOME, "/h"); +}); + +test("childEnv keeps Windows' Path casing", () => { + const env = childEnv(config, { Path: "C:\\bin", HOME: "/h" }); + assert.deepEqual(Object.keys(env).filter((key) => key.toUpperCase() === "PATH"), ["Path"]); + assert.equal(env.Path, ["/opt/ug/bin", "/opt/databricks", "C:\\bin"].join(delimiter)); +}); + +test("a token close to its expiry is still reused briefly", async () => { + let clock = 3600 * 1000 - 60_000; + let calls = 0; + const token = createTokenSource(config, { run: async () => (calls++, jwt(3600)), now: () => clock }); + await token(); + clock += MIN_TOKEN_REUSE_MS - 1; + await token(); + assert.equal(calls, 1); + clock += 1; + await token(); + assert.equal(calls, 2); +}); + +test("runCommand returns the token, and fails on a nonzero exit, no output or no program", async () => { + const script = (code) => [process.execPath, "-e", code]; + assert.equal(await runCommand(script("console.log(' tok ')"), process.env), "tok"); + await assert.rejects(runCommand(script("console.error('first\\nlast'); process.exit(3)"), process.env), (error) => { + assert.equal(error.code, 3); + assert.match(error.message, /last/); + return true; + }); + await assert.rejects(runCommand(script(""), process.env), /printed no token/); + await assert.rejects(runCommand(["/nonexistent/ug"], process.env), (error) => error.code === "ENOENT"); +}); + +test("createTokenSource caches until shortly before expiry", async () => { + let clock = 0; + const calls = []; + const run = async (argv, env) => { + calls.push({ argv, path: env.PATH }); + return jwt(3600); + }; + const token = createTokenSource(config, { run, now: () => clock }); + assert.equal(await token(), jwt(3600)); + clock = 3600 * 1000 - REFRESH_MARGIN_MS - 1; + await token(); + assert.equal(calls.length, 1); + assert.deepEqual(calls[0].argv, config.tokenCommand); + assert.ok(calls[0].path.startsWith("/opt/ug/bin")); + clock = 3600 * 1000 - REFRESH_MARGIN_MS; + await token(); + assert.equal(calls.length, 2); +}); + +test("createTokenSource gives opaque tokens a default lifetime", async () => { + let clock = 0; + let calls = 0; + const token = createTokenSource(config, { run: async () => (calls++, "opaque"), now: () => clock }); + await token(); + clock = DEFAULT_TOKEN_TTL_MS - 1; + await token(); + assert.equal(calls, 1); + clock = DEFAULT_TOKEN_TTL_MS; + await token(); + assert.equal(calls, 2); +}); + +test("concurrent requests share one mint, and a failure is not cached", async () => { + let calls = 0; + let fail = true; + const run = () => + new Promise((resolve, reject) => { + calls++; + setImmediate(() => (fail ? reject(new Error("no login")) : resolve("t"))); + }); + const token = createTokenSource(config, { run }); + const results = await Promise.allSettled([token(), token()]); + assert.equal(calls, 1); + assert.ok(results.every((r) => r.status === "rejected")); + fail = false; + assert.equal(await token(), "t"); + assert.equal(calls, 2); +}); + +test("withLoginHint shows the hint once per run of failures", async () => { + let fail = true; + let hints = 0; + const bearer = withLoginHint( + async () => { + if (fail) throw new Error("no login"); + return "t"; + }, + () => (hints++, true), + ); + await assert.rejects(bearer(), /no login/); + await assert.rejects(bearer(), /no login/); + assert.equal(hints, 1); + fail = false; + assert.equal(await bearer(), "t"); + fail = true; + await assert.rejects(bearer(), /no login/); + assert.equal(hints, 2); +}); + +test("withLoginHint still throws the token error when the hint fails", async () => { + const bearer = withLoginHint( + async () => { + throw new Error("no login"); + }, + () => { + throw new Error("log failed"); + }, + ); + await assert.rejects(bearer(), /no login/); +}); + +test("withLoginHint tries again until a hint is shown", async () => { + const shown = [false, true]; + let calls = 0; + const bearer = withLoginHint( + async () => { + throw new Error("no login"); + }, + () => shown[calls++], + ); + await assert.rejects(bearer()); + await assert.rejects(bearer()); + await assert.rejects(bearer()); + assert.equal(calls, 2); +}); + +test("hintFor picks the hint by how ug auth-token failed", () => { + const hints = { loginHint: "login", missingUgHint: "missing" }; + assert.equal(hintFor(Object.assign(new Error(), { code: 1 }), hints), "login"); + assert.equal(hintFor(Object.assign(new Error(), { code: "ENOENT" }), hints), "missing"); + assert.equal(hintFor(Object.assign(new Error(), { code: "TIMEOUT" }), hints), null); + assert.equal(hintFor(new Error("printed no token"), hints), null); +}); + +test("an invalidate during a mint keeps its token out of the cache", async () => { + const calls = []; + let release; + const token = createTokenSource(config, { + run: (argv) => { + calls.push(argv); + if (calls.length === 1) return new Promise((resolve) => (release = resolve)); + return Promise.resolve(jwt(3600)); + }, + now: () => 0, + }); + const first = token(); + token.invalidate(); + release(jwt(7200)); + assert.equal(await first, jwt(7200)); + assert.equal(await token(), jwt(3600)); + assert.equal(await token(), jwt(3600)); + assert.deepEqual(calls, [config.tokenCommand, [...config.tokenCommand, FORCE_REFRESH_FLAG]]); +}); + +test("buildProviders attaches the token provider to every provider", () => { + const provide = async () => "t"; + const [provider] = buildProviders(config, provide, "app-session"); + assert.equal(provider.bearerTokenProvider, provide); + assert.equal(provider.baseUrl, "https://ws/ai-gateway/anthropic"); + assert.equal(config.providers[0].bearerTokenProvider, undefined); +}); + +test("invalidate drops the cached token and force-refreshes the next mint once", async () => { + const calls = []; + const token = createTokenSource(config, { + run: async (argv) => (calls.push(argv), jwt(3600)), + now: () => 0, + }); + await token(); + await token(); + token.invalidate(); + await token(); + await token(); + token.invalidate(); + await token(); + assert.deepEqual(calls, [ + config.tokenCommand, + [...config.tokenCommand, FORCE_REFRESH_FLAG], + [...config.tokenCommand, FORCE_REFRESH_FLAG], + ]); +}); + +test("isAuthRejection matches only 401 errors and call failures", () => { + assert.equal(isAuthRejection({ type: "session.error", data: { statusCode: 401 } }), true); + assert.equal(isAuthRejection({ type: "model.call_failure", data: { statusCode: 401 } }), true); + assert.equal(isAuthRejection({ type: "model.call_failure", data: { statusCode: 429 } }), false); + assert.equal(isAuthRejection({ type: "session.error", data: {} }), false); + assert.equal(isAuthRejection({ type: "assistant.usage", data: { statusCode: 401 } }), false); +}); + +test("buildProviders sends the app session's id on every provider's requests", () => { + const providers = buildProviders( + { providers: [config.providers[0], { name: "p2", type: "openai", baseUrl: "https://ws/ai-gateway/openai" }] }, + async () => "t", + "app-session", + ); + assert.deepEqual( + providers.map((provider) => provider.headers), + [{ [SESSION_ID_HEADER]: "app-session", A: "1" }, { [SESSION_ID_HEADER]: "app-session" }], + ); + assert.deepEqual(config.providers[0].headers, { A: "1" }); +}); + +test("buildProviders takes the session id the runtime gives the extension", (t) => { + const previous = process.env.SESSION_ID; + process.env.SESSION_ID = "from-runtime"; + t.after(() => { + if (previous === undefined) delete process.env.SESSION_ID; + else process.env.SESSION_ID = previous; + }); + assert.equal(buildProviders(config, async () => "t")[0].headers[SESSION_ID_HEADER], "from-runtime"); +}); + +test("an admin's session-id header wins, in any case", () => { + assert.deepEqual(withSessionId({ "Session-Id": "admin" }, "app-session"), { "Session-Id": "admin" }); + assert.deepEqual(withSessionId({ "session-id": "admin", B: "2" }, "app-session"), { "session-id": "admin", B: "2" }); +}); + +test("without a session id the headers stay as configured", () => { + assert.deepEqual(withSessionId({ A: "1" }, undefined), { A: "1" }); + assert.deepEqual(withSessionId(undefined, ""), {}); +}); diff --git a/tests/copilot_app_remove.test.mjs b/tests/copilot_app_remove.test.mjs new file mode 100644 index 000000000..63b023411 --- /dev/null +++ b/tests/copilot_app_remove.test.mjs @@ -0,0 +1,285 @@ +import assert from "node:assert/strict"; +import { + chmodSync, + copyFileSync, + existsSync, + mkdtempSync, + readFileSync, + readdirSync, + statSync, + symlinkSync, + utimesSync, + writeFileSync, +} from "node:fs"; +import nodeModule from "node:module"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { test as nodeTest } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; + +import { removeInBackground, removeOnce, removeSettingsKeys, settingsPath } from "../src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs"; +import { LOCK_FILE, LOCK_STALE_MS, copilotHome, syncOnce } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs"; +import { FakeApp } from "./copilot_app_fake.mjs"; + +const skip = typeof WebSocket === "function" ? false : "this Node has no global WebSocket"; +const test = (name, fn) => nodeTest(name, { skip }, fn); + +const ROOT = "/home/me/.ucode/copilot-app-plugin"; +const CLAUDE = "11111111-1111-5111-8111-111111111111"; +const GPT = "22222222-2222-5222-8222-222222222222"; +const UG_KEYS = { + extraKnownMarketplaces: { "databricks-unity-gateway": { source: { source: "directory", path: ROOT } } }, + enabledPlugins: { "unity-gateway@databricks-unity-gateway": true }, +}; +const LEGACY_KEYS = { + extraKnownMarketplaces: { "unity-gateway": { source: { source: "directory", path: ROOT } } }, + enabledPlugins: { "databricks@unity-gateway": true }, +}; +const CONFIG = { remove: true, pluginRoot: ROOT, app: { providers: [], ownedProviderIds: [CLAUDE, GPT] } }; + +function seedApp(app) { + for (const id of [CLAUDE, GPT, "mine"]) { + app.providers.set(id, { id, name: id, kind: "custom" }); + app.models.set(`${id}/m`, { id: `${id}/m`, providerId: id, modelId: "m" }); + } +} + +async function setup(t, settings = UG_KEYS) { + const app = new FakeApp(); + await app.listen(); + t.after(() => app.close()); + const home = mkdtempSync(join(tmpdir(), "ug-copilot-home-")); + const stateDir = mkdtempSync(join(tmpdir(), "ug-copilot-state-")); + app.writeRunFiles(home); + seedApp(app); + const path = settingsPath(home); + if (settings !== null) writeFileSync(path, typeof settings === "string" ? settings : JSON.stringify(settings)); + return { app, path, options: { home, stateDir, timeoutMs: 2000 } }; +} + +const read = (path) => JSON.parse(readFileSync(path, "utf8")); + +test("removeOnce deletes ug's providers, then its settings keys, keeping everything else", async (t) => { + const settings = { + model: "gpt-5", + extraKnownMarketplaces: { other: { source: { source: "github", repo: "o/r" } }, ...UG_KEYS.extraKnownMarketplaces }, + enabledPlugins: { "x@other": false, ...UG_KEYS.enabledPlugins }, + }; + const { app, path, options } = await setup(t, settings); + chmodSync(path, 0o640); + + assert.equal(await removeOnce(CONFIG, options), "removed"); + + assert.deepEqual([...app.providers.keys()], ["mine"]); + assert.deepEqual([...app.models.keys()], ["mine/m"]); + assert.deepEqual(read(path), { + model: "gpt-5", + extraKnownMarketplaces: { other: { source: { source: "github", repo: "o/r" } } }, + enabledPlugins: { "x@other": false }, + }); + assert.equal(statSync(path).mode & 0o777, 0o640); + assert.deepEqual(readdirSync(options.home).sort(), ["run", "settings.json"]); +}); + +test("removeOnce drops a parent object it leaves empty", async (t) => { + const { path, options } = await setup(t, { model: "gpt-5", ...UG_KEYS }); + assert.equal(await removeOnce(CONFIG, options), "removed"); + assert.deepEqual(read(path), { model: "gpt-5" }); +}); + +test("once the keys are gone, another session's extension does nothing", async (t) => { + const { app, options } = await setup(t); + assert.equal(await removeOnce(CONFIG, options), "removed"); + const connections = app.connections; + assert.equal(await removeOnce(CONFIG, options), "done"); + assert.equal(app.connections, connections); + assert.equal(app.sent("delete_model_provider").length, 2); +}); + +test("only one extension process removes at a time; a stale lock is taken over", async (t) => { + const { app, path, options } = await setup(t); + const lock = join(options.stateDir, LOCK_FILE); + writeFileSync(lock, ""); + assert.equal(await removeOnce(CONFIG, options), "busy"); + assert.equal(app.connections, 0); + assert.deepEqual(read(path), UG_KEYS); + + const old = new Date(Date.now() - 2 * LOCK_STALE_MS); + utimesSync(lock, old, old); + assert.equal(await removeOnce(CONFIG, options), "removed"); + assert.equal(existsSync(lock), false); +}); + +test("concurrent extension processes delete each provider once", async (t) => { + const { app, options } = await setup(t); + const results = await Promise.all([0, 1, 2].map(() => removeOnce(CONFIG, options))); + assert.equal(results.filter((result) => result === "removed").length, 1); + assert.equal(app.sent("delete_model_provider").length, 2); +}); + +for (const [name, content] of [ + ["invalid JSON", "{not json"], + ["a commented file", "// comment\n{}"], + ["a non-object", "[]"], +]) { + test(`settings that are ${name} are left alone, but the rows still go`, async (t) => { + const { app, path, options } = await setup(t, content); + const logs = []; + assert.equal(await removeInBackground(CONFIG, options, (line) => logs.push(line)), "refused"); + assert.equal(readFileSync(path, "utf8"), content); + assert.deepEqual([...app.providers.keys()], ["mine"]); + assert.match(logs[0], /not valid JSON|not a JSON object/); + }); +} + +test("a symlinked settings file is refused", async (t) => { + const { path, options } = await setup(t, null); + const target = join(mkdtempSync(join(tmpdir(), "ug-copilot-target-")), "settings.json"); + writeFileSync(target, JSON.stringify(UG_KEYS)); + symlinkSync(target, path); + const logs = []; + assert.equal(await removeInBackground(CONFIG, options, (line) => logs.push(line)), "refused"); + assert.deepEqual(read(target), UG_KEYS); + assert.match(logs[0], /symlink/); + assert.throws(() => removeSettingsKeys(path, ROOT), /symlink/); +}); + +test("an earlier build's keys go too", async (t) => { + const both = { + model: "m", + extraKnownMarketplaces: { ...UG_KEYS.extraKnownMarketplaces, ...LEGACY_KEYS.extraKnownMarketplaces }, + enabledPlugins: { ...UG_KEYS.enabledPlugins, ...LEGACY_KEYS.enabledPlugins }, + }; + const { path } = await setup(t, both); + assert.equal(removeSettingsKeys(path, ROOT), true); + assert.deepEqual(read(path), { model: "m" }); +}); + +test("a foreign marketplace of ug's name and its plugin key stay", async (t) => { + const elsewhere = { source: { source: "directory", path: "/elsewhere" } }; + const foreign = { + extraKnownMarketplaces: { "databricks-unity-gateway": elsewhere, "unity-gateway": elsewhere }, + enabledPlugins: { "unity-gateway@databricks-unity-gateway": true, "databricks@unity-gateway": true }, + }; + const { app, path, options } = await setup(t, foreign); + assert.equal(await removeOnce(CONFIG, options), "done"); + assert.deepEqual(read(path), foreign); + assert.equal(app.connections, 0); +}); + +test("the keys stay until the rows are gone", async (t) => { + const { app, path, options } = await setup(t); + const closed = { ...options, home: mkdtempSync(join(tmpdir(), "ug-copilot-home-")) }; + writeFileSync(settingsPath(closed.home), JSON.stringify(UG_KEYS)); + assert.equal(await removeOnce(CONFIG, closed), "not_running"); + assert.deepEqual(read(settingsPath(closed.home)), UG_KEYS); + + const logs = []; + app.fail = "delete_model_provider"; + assert.equal(await removeInBackground(CONFIG, options, (line) => logs.push(line)), "failed"); + assert.match(logs[0], /refused delete_model_provider/); + assert.deepEqual(read(path), UG_KEYS); + + app.fail = null; + assert.equal(await removeInBackground(CONFIG, options, (line) => logs.push(line)), "removed"); + assert.deepEqual(read(path), {}); +}); + +test("a dropped connection is logged, not thrown", async (t) => { + const { app, path, options } = await setup(t); + app.drop = "list_model_providers"; + const logs = []; + assert.equal(await removeInBackground(CONFIG, options, (line) => logs.push(line)), "failed"); + assert.match(logs[0], /closed the connection/); + assert.deepEqual(read(path), UG_KEYS); + const throwingLog = () => { + throw new Error("log failed"); + }; + assert.equal(await removeInBackground(CONFIG, options, throwingLog), "failed"); +}); + +test("without owned providers the keys go without connecting", async (t) => { + const { app, path, options } = await setup(t); + const config = { ...CONFIG, app: { providers: [], ownedProviderIds: [] } }; + assert.equal(await removeOnce(config, options), "removed"); + assert.equal(app.connections, 0); + assert.deepEqual(read(path), {}); +}); + +test("a later install syncs its rows and restarts the app again", async (t) => { + const { app, options } = await setup(t); + const rows = { + providers: [{ provider: { id: GPT, name: "gpt", kind: "custom" }, secret: null, models: [] }], + ownedProviderIds: [CLAUDE, GPT], + }; + assert.equal(await syncOnce({ app: rows }, options), "changed"); + assert.equal(await removeOnce({ ...CONFIG, app: rows }, options), "removed"); + assert.equal(app.providers.has(GPT), false); + assert.equal(await syncOnce({ app: rows }, options), "changed"); + assert.equal(app.providers.has(GPT), true); +}); + +test("outside remove mode nothing is removed", async (t) => { + const { app, path, options } = await setup(t); + assert.equal(await removeOnce({ ...CONFIG, remove: false }, options), "skipped"); + assert.equal(app.connections, 0); + assert.deepEqual(read(path), UG_KEYS); +}); + +test("the settings file is found under COPILOT_HOME", () => { + assert.equal(settingsPath(copilotHome({ COPILOT_HOME: "/custom" })), join("/custom", "settings.json")); +}); + +const SOURCE = join(dirname(fileURLToPath(import.meta.url)), "..", "src", "ucode", "agents", "desktop", "copilot_app_plugin"); +const FAKE_SDK = `data:text/javascript,${encodeURIComponent(` + export async function joinSession(...args) { + globalThis.ugJoinCalls.push(args); + if (globalThis.ugJoinFails) throw new Error("no session"); + return { on() {}, log() {} }; + } +`)}`; + +async function runExtension(t, options, { joinFails = false } = {}) { + const dir = mkdtempSync(join(tmpdir(), "ug-copilot-extension-")); + for (const name of ["extension.mjs", "ug-plugin.mjs", "app-rows.mjs", "app-remove.mjs", "app-restart.mjs"]) { + copyFileSync(join(SOURCE, name), join(dir, name)); + } + writeFileSync(join(dir, "config.json"), JSON.stringify({ ...CONFIG, providers: [{ name: CLAUDE }], models: [{ id: "m" }] })); + const hooks = nodeModule.registerHooks({ + resolve: (specifier, context, next) => + specifier === "@github/copilot-sdk/extension" ? { url: FAKE_SDK, shortCircuit: true } : next(specifier, context), + }); + const previousHome = process.env.COPILOT_HOME; + process.env.COPILOT_HOME = options.home; + globalThis.ugJoinCalls = []; + globalThis.ugJoinFails = joinFails; + t.after(() => { + hooks.deregister(); + if (previousHome === undefined) delete process.env.COPILOT_HOME; + else process.env.COPILOT_HOME = previousHome; + }); + const errors = []; + t.mock.method(console, "error", (line) => errors.push(line)); + await import(pathToFileURL(join(dir, "extension.mjs")).href); + return { joins: globalThis.ugJoinCalls, errors }; +} + +const hookSkip = skip || (typeof nodeModule.registerHooks === "function" ? false : "this Node can't stub the SDK import"); +const extensionTest = (name, fn) => nodeTest(name, { skip: hookSkip }, fn); + +extensionTest("in remove mode the extension removes ug and joins its session without serving", async (t) => { + const { app, path, options } = await setup(t); + const { joins, errors } = await runExtension(t, options); + assert.deepEqual(joins, [[]]); + assert.deepEqual(errors, []); + assert.deepEqual([...app.providers.keys()], ["mine"]); + assert.deepEqual(read(path), {}); +}); + +extensionTest("in remove mode a failed join is logged, not thrown", async (t) => { + const { app, path, options } = await setup(t); + const { errors } = await runExtension(t, options, { joinFails: true }); + assert.match(errors[0], /couldn't join the session: no session/); + assert.deepEqual([...app.providers.keys()], ["mine"]); + assert.deepEqual(read(path), {}); +}); diff --git a/tests/copilot_app_restart.test.mjs b/tests/copilot_app_restart.test.mjs new file mode 100644 index 000000000..0d8de8bee --- /dev/null +++ b/tests/copilot_app_restart.test.mjs @@ -0,0 +1,112 @@ +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +import { + RESTART_HINT, + RESTARTING_MESSAGE, + launchRestart, + planRestart, + tellRestart, +} from "../src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs"; + +const TARGET = { platform: "linux", pid: 4242, app: "/usr/bin/github" }; + +function emptyHome() { + return mkdtempSync(join(tmpdir(), "ug-copilot-home-")); +} + +function homeWithPid(portFile) { + const home = emptyHome(); + mkdirSync(join(home, "run")); + writeFileSync(join(home, "run", "ws.port"), portFile); + return home; +} + +function fakeSession() { + const logs = []; + return { logs, log: async (message, options) => logs.push([message, options.level]) }; +} + +test("the extension starts the helper with the app's target and says so in the chat", async () => { + const started = []; + const home = emptyHome(); + const stateDir = mkdtempSync(join(tmpdir(), "ug-copilot-state-")); + const result = await launchRestart({ + home, + stateDir, + ugPath: "/opt/ug/bin/ug", + plan: async () => TARGET, + start: async (target, dir, ugPath) => (started.push([target, dir, ugPath]), true), + }); + assert.equal(result, "started"); + assert.deepEqual(started, [[{ ...TARGET, home }, stateDir, "/opt/ug/bin/ug"]]); + const session = fakeSession(); + await tellRestart(session, result); + assert.deepEqual(session.logs, [[RESTARTING_MESSAGE, "info"]]); +}); + +test("when the helper can't restart the app here, the chat gets the restart hint", async () => { + for (const options of [ + { plan: async () => TARGET, ugPath: undefined }, + { plan: async () => null }, + { plan: async () => Promise.reject(new Error("boom")) }, + { plan: async () => TARGET, start: async () => false }, + ]) { + const result = await launchRestart({ home: emptyHome(), stateDir: emptyHome(), ugPath: "/u/ug", ...options }); + assert.notEqual(result, "started"); + const session = fakeSession(); + await tellRestart(session, result); + assert.deepEqual(session.logs, [[RESTART_HINT, "info"]]); + } + await tellRestart({ log: () => { throw new Error("closed"); } }, "failed"); +}); + +test("planRestart takes the app's pid from its run file", async () => { + const alive = (pid) => pid === 4242; + const exists = () => true; + const home = homeWithPid("1234\n4242\n"); + const linux = { home, platform: "linux", alive, exists }; + assert.deepEqual(await planRestart({ ...linux, env: { DISPLAY: ":0" } }), TARGET); + assert.deepEqual(await planRestart({ ...linux, env: { WAYLAND_DISPLAY: "wayland-0" } }), TARGET); + assert.deepEqual(await planRestart({ home, platform: "darwin", alive }), { + platform: "darwin", + pid: 4242, + app: "com.github.githubapp", + }); + const calls = []; + const run = async (command, args) => (calls.push([command, ...args]), "C:\\Program Files\\GitHub\\github.exe\r\n"); + assert.deepEqual(await planRestart({ home, platform: "win32", alive, run }), { + platform: "win32", + pid: 4242, + app: "C:\\Program Files\\GitHub\\github.exe", + }); + assert.match(calls[0].at(-1), /Get-Process -Id 4242/); +}); + +test("planRestart reads the app's pid from app-owner.pid, as the app writes it", async () => { + const home = emptyHome(); + mkdirSync(join(home, "run")); + writeFileSync(join(home, "run", "ws.port"), "43339"); + writeFileSync(join(home, "run", "app-owner.pid"), "4242"); + const options = { home, platform: "linux", env: { DISPLAY: ":0" }, alive: (pid) => pid === 4242, exists: () => true }; + assert.deepEqual(await planRestart(options), TARGET); + writeFileSync(join(home, "run", "app-owner.pid"), "not a pid"); + assert.equal(await planRestart(options), null); +}); + +test("planRestart gives up without a display, the app's pid or a known launcher", async () => { + const alive = () => true; + const exists = () => true; + const home = homeWithPid("1234\n4242\n"); + assert.equal(await planRestart({ home, platform: "linux", env: {}, alive, exists }), null); + assert.equal(await planRestart({ home, platform: "linux", env: { DISPLAY: ":0" }, alive, exists: () => false }), null); + assert.equal(await planRestart({ home, platform: "linux", env: { DISPLAY: ":0" }, alive: () => false, exists }), null); + assert.equal(await planRestart({ home: homeWithPid("1234\n"), platform: "darwin", alive }), null); + assert.equal(await planRestart({ home: emptyHome(), platform: "darwin", alive }), null); + assert.equal(await planRestart({ home: homeWithPid(`1234\n${process.pid}\n`), platform: "darwin", alive }), null); + assert.equal(await planRestart({ home, platform: "win32", alive, run: async () => null }), null); + assert.equal(await planRestart({ home, platform: "freebsd", alive }), null); +}); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs new file mode 100644 index 000000000..7fe4e1196 --- /dev/null +++ b/tests/copilot_app_rows.test.mjs @@ -0,0 +1,286 @@ +import assert from "node:assert/strict"; +import { existsSync, mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test as nodeTest } from "node:test"; + +import { LOCK_FILE, LOCK_STALE_MS, copilotHome, readEndpoint, removalRequested, syncInBackground, syncOnce, tryLock } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs"; +import { FakeApp } from "./copilot_app_fake.mjs"; + +// The Copilot runtime's Node has a global WebSocket; Node before 22 doesn't. +const skip = typeof WebSocket === "function" ? false : "this Node has no global WebSocket"; +const test = (name, fn) => nodeTest(name, { skip }, fn); + +const CLAUDE = "11111111-1111-5111-8111-111111111111"; +const GPT = "22222222-2222-5222-8222-222222222222"; +const OPEN = "33333333-3333-5333-8333-333333333333"; + +function providerRows(id, name, models, secret = null) { + return { + provider: { id, name, kind: secret ? "anthropic" : "custom", settings: { baseUrl: `https://ws/${name}` } }, + secret, + models: models.map((modelId) => ({ id: `${id}/${modelId}`, providerId: id, modelId, wireModel: modelId, displayName: modelId })), + }; +} + +const PLACEHOLDER = { kind: "apiKey", value: "ug-placeholder-not-a-secret" }; +const ROWS = { + providers: [providerRows(CLAUDE, "claude", ["claude-sonnet-4.6"], PLACEHOLDER), providerRows(GPT, "gpt", ["gpt-5-2"])], + ownedProviderIds: [CLAUDE, GPT, OPEN], +}; + +async function setup(t) { + const app = new FakeApp(); + await app.listen(); + t.after(() => app.close()); + const home = mkdtempSync(join(tmpdir(), "ug-copilot-home-")); + const stateDir = mkdtempSync(join(tmpdir(), "ug-copilot-state-")); + app.writeRunFiles(home); + return { app, options: { home, stateDir, timeoutMs: 2000 } }; +} + +test("readEndpoint reads the first value of each run file and honours COPILOT_HOME", () => { + const home = mkdtempSync(join(tmpdir(), "ug-copilot-home-")); + assert.equal(readEndpoint(home), null); + mkdirSync(join(home, "run")); + writeFileSync(join(home, "run", "ws.port"), "4321\n99\n"); + writeFileSync(join(home, "run", "ws.token"), "tok\n99\n"); + assert.deepEqual(readEndpoint(home), { port: 4321, token: "tok" }); + assert.equal(copilotHome({ COPILOT_HOME: home }), home); +}); + +test("syncOnce writes the rows, then does nothing until they or the app launch change", async (t) => { + const { app, options } = await setup(t); + app.providers.set("mine", { id: "mine", name: "Mine", kind: "custom" }); + + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); + assert.deepEqual([...app.providers.keys()].sort(), [CLAUDE, GPT, "mine"].sort()); + assert.deepEqual([...app.secrets], [[CLAUDE, PLACEHOLDER.value]]); + assert.deepEqual([...app.models.keys()].sort(), [`${CLAUDE}/claude-sonnet-4.6`, `${GPT}/gpt-5-2`]); + assert.ok(app.handshakes.every((headers) => !headers.origin)); + const ids = app.received.map((message) => message.request_id); + assert.equal(new Set(ids).size, ids.length); + + const received = app.received.length; + assert.equal(await syncOnce({ app: ROWS }, options), "current"); + assert.equal(app.received.length, received); + + app.token = "next-launch"; + app.writeRunFiles(options.home); + assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + assert.ok(app.sent("upsert_model_provider").slice(-2).every((message) => !message.secret)); +}); + +function relaunch(app, options, token) { + app.token = token; + app.writeRunFiles(options.home); +} + +test("only the first sync that changes the app's rows asks for a restart", async (t) => { + const { app, options } = await setup(t); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); + assert.ok(existsSync(join(options.stateDir, "app-restart.json"))); + + relaunch(app, options, "restarted"); + assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + + const next = { ...ROWS, providers: [providerRows(GPT, "gpt", ["gpt-6"])] }; + assert.equal(await syncOnce({ app: next }, options), "changed"); + relaunch(app, options, "restarted-again"); + assert.equal(await syncOnce({ app: next }, options), "synced"); +}); + +test("rows the app already has don't ask for a restart", async (t) => { + const { app, options } = await setup(t); + await syncOnce({ app: ROWS }, { ...options, stateDir: mkdtempSync(join(tmpdir(), "ug-copilot-state-")) }); + assert.equal(await syncOnce({ app: structuredClone(ROWS) }, options), "synced"); + assert.ok(!existsSync(join(options.stateDir, "app-restart.json"))); +}); + +test("a deleted row, a new row or a missing secret is a change", async (t) => { + const { app, options } = await setup(t); + const fresh = () => ({ ...options, stateDir: mkdtempSync(join(tmpdir(), "ug-copilot-state-")) }); + await syncOnce({ app: ROWS }, fresh()); + + app.models.set(`${GPT}/old`, { id: `${GPT}/old`, providerId: GPT, modelId: "old" }); + assert.equal(await syncOnce({ app: ROWS }, fresh()), "changed"); + + app.models.delete(`${GPT}/gpt-5-2`); + assert.equal(await syncOnce({ app: ROWS }, fresh()), "changed"); + + app.models.get(`${CLAUDE}/claude-sonnet-4.6`).displayName = "renamed in the app"; + assert.equal(await syncOnce({ app: ROWS }, fresh()), "changed"); + + app.secrets.clear(); + app.providers.set(CLAUDE, { ...app.providers.get(CLAUDE), hasSecret: false }); + assert.equal(await syncOnce({ app: ROWS }, fresh()), "changed"); + + app.providers.set(OPEN, { id: OPEN, name: "open", kind: "custom" }); + assert.equal(await syncOnce({ app: ROWS }, fresh()), "changed"); + assert.equal(await syncOnce({ app: ROWS }, fresh()), "synced"); +}); + +test("an app that reports the rows differently asks for one restart, not one per launch", async (t) => { + const { app, options } = await setup(t); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); + for (const [launch, token] of ["first", "second"].entries()) { + for (const model of app.models.values()) delete model.displayName; + relaunch(app, options, token); + assert.equal(await syncOnce({ app: ROWS }, options), "synced", `launch ${launch}`); + } +}); + +test("of several extension processes, one asks for the restart", async (t) => { + const { options } = await setup(t); + const results = await Promise.all([0, 1, 2, 3].map(() => syncOnce({ app: ROWS }, options))); + assert.equal(results.filter((result) => result === "changed").length, 1); + assert.ok(results.every((result) => ["changed", "busy", "current"].includes(result))); +}); + +test("syncOnce deletes ug's stale providers and rows but no one else's", async (t) => { + const { app, options } = await setup(t); + await syncOnce({ app: ROWS }, options); + app.providers.set("mine", { id: "mine", name: "Mine", kind: "custom" }); + app.models.set("user-row", { id: "user-row", providerId: GPT, modelId: "x" }); + app.models.set("odd", { providerId: GPT, modelId: "no-id" }); + + const next = { + providers: [providerRows(GPT, "gpt", ["gpt-6"]), providerRows(OPEN, "open", ["glm-5"])], + ownedProviderIds: ROWS.ownedProviderIds, + }; + assert.equal(await syncOnce({ app: next }, options), "changed"); + + assert.deepEqual([...app.providers.keys()].sort(), [GPT, OPEN, "mine"].sort()); + assert.deepEqual([...app.models.keys()].sort(), [`${GPT}/gpt-6`, `${OPEN}/glm-5`, "odd"].sort()); +}); + +test("repeated syncs leave exactly one copy of each row", async (t) => { + const { app, options } = await setup(t); + const stateDirs = [0, 1, 2].map(() => mkdtempSync(join(tmpdir(), "ug-copilot-state-"))); + await Promise.all(stateDirs.map((stateDir) => syncOnce({ app: ROWS }, { ...options, stateDir }))); + assert.equal(app.providers.size, 2); + assert.equal(app.models.size, 2); +}); + +test("only one extension process syncs at a time; a stale lock is taken over", async (t) => { + const { app, options } = await setup(t); + const lock = join(options.stateDir, LOCK_FILE); + writeFileSync(lock, ""); + assert.equal(await syncOnce({ app: ROWS }, options), "busy"); + assert.equal(app.received.length, 0); + + const old = new Date(Date.now() - 2 * LOCK_STALE_MS); + utimesSync(lock, old, old); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); +}); + +test("a lock taken over as stale is released only by its new owner", () => { + const dir = mkdtempSync(join(tmpdir(), "ug-copilot-lock-")); + const lock = join(dir, LOCK_FILE); + const first = tryLock(lock, Date.now); + assert.equal(tryLock(lock, Date.now), null); + const old = new Date(Date.now() - 2 * LOCK_STALE_MS); + utimesSync(lock, old, old); + const second = tryLock(lock, Date.now); + assert.ok(second); + first(); + assert.ok(existsSync(lock)); + second(); + assert.equal(existsSync(lock), false); +}); + +test("a reply for another request is skipped", async (t) => { + const { app, options } = await setup(t); + app.beforeReply.list_model_providers = [ + { type: "model_providers_list", providers: [{ id: "x" }], request_id: "ug-someone-else" }, + { type: "model_providers_list", providers: [{ id: "y" }] }, + { type: "error", message: "boom", request_id: "ug-someone-else" }, + ]; + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); + assert.equal(app.providers.size, 2); +}); + +test("an error without a request_id answers the pending request", async (t) => { + const { app, options } = await setup(t); + app.beforeReply.list_model_providers = [{ type: "error", message: "Invalid message format" }]; + await assert.rejects(syncOnce({ app: ROWS }, options), /Invalid message format/); + assert.equal(app.providers.size, 0); +}); + +test("the sync retries while the app applies another change", async (t) => { + const { app, options } = await setup(t); + app.busy = 2; + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); + assert.equal(app.models.size, 2); +}); + +test("failures are logged, never thrown, and the next run retries", async (t) => { + const { app, options } = await setup(t); + const logs = []; + app.fail = "upsert_provider_model"; + assert.equal(await syncInBackground({ app: ROWS }, options, (line) => logs.push(line)), "failed"); + assert.match(logs[0], /refused upsert_provider_model/); + + app.fail = null; + app.drop = "list_model_providers"; + assert.equal(await syncInBackground({ app: ROWS }, options, (line) => logs.push(line)), "failed"); + assert.match(logs[1], /closed the connection/); + + app.drop = null; + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); +}); + +test("a closed app or a stale token is not synced", async (t) => { + const { app, options } = await setup(t); + const home = mkdtempSync(join(tmpdir(), "ug-copilot-home-")); + assert.equal(await syncOnce({ app: ROWS }, { ...options, home }), "not_running"); + + writeFileSync(join(options.home, "run", "ws.token"), "stale\n1\n"); + assert.equal(await syncInBackground({ app: ROWS }, options, () => {}), "failed"); + assert.equal(app.received.length, 0); + + mkdirSync(join(home, "run")); + writeFileSync(join(home, "run", "ws.port"), "1\n1\n"); + writeFileSync(join(home, "run", "ws.token"), "x\n1\n"); + assert.equal(await syncInBackground({ app: ROWS }, { ...options, home }, () => {}), "failed"); +}); + +test("no app rows in the config means nothing to sync", async (t) => { + const { app, options } = await setup(t); + assert.equal(await syncOnce({}, options), "skipped"); + assert.equal(await syncOnce({ app: { providers: [], ownedProviderIds: [] } }, options), "skipped"); + assert.equal(app.connections, 0); +}); + + +test("a sync racing a revert stops before it writes ug's rows back", async (t) => { + const { app, options } = await setup(t); + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: false })); + app.onRequest = (message) => { + if (message.type !== "upsert_model_provider" || app.onRequest === null) return; + app.onRequest = null; + // ug marks the plugin for removal, then deletes ug's rows from the running app. + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: true })); + setImmediate(() => { + app.providers.clear(); + app.models.clear(); + }); + }; + + assert.equal(await syncOnce({ app: ROWS }, options), "removing"); + await new Promise((resolve) => setImmediate(resolve)); + + assert.equal(app.providers.size, 0); + assert.equal(app.models.size, 0); + assert.equal(app.sent("upsert_model_provider").length, 1); + assert.equal(app.sent("upsert_provider_model").length, 0); +}); + +test("a plugin marked for removal doesn't sync at all", async (t) => { + const { app, options } = await setup(t); + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: true })); + + assert.equal(removalRequested(options.stateDir), true); + assert.equal(await syncOnce({ app: ROWS }, options), "removing"); + assert.equal(app.sent("upsert_model_provider").length, 0); +}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py new file mode 100644 index 000000000..d91ffe66d --- /dev/null +++ b/tests/test_copilot_app_plugin.py @@ -0,0 +1,867 @@ +import json +import os +import shutil +import subprocess +import sys +import time +from pathlib import Path + +import pytest + +from ucode.agents.desktop import copilot_app_plugin as plugin + +NODE = shutil.which("node") +PLUGIN_SOURCE = Path(plugin.__file__).parent + + +def _config(**overrides) -> plugin.PluginConfig: + values = { + "workspace": "https://example.cloud.databricks.com", + "ug_path": "/opt/ug/bin/ug", + "profile": "dev", + "headers": { + "User-Agent": "ucode/1.0 copilot/1.4.2", + "x-databricks-use-coding-agent-mode": "true", + }, + "path_dirs": ("/opt/ug/bin",), + "providers": ( + plugin.PluginProvider( + id="11111111-1111-5111-8111-111111111111", + wire="anthropic", + base_path="/ai-gateway/anthropic", + models=( + plugin.PluginModel( + "claude-sonnet-5", "system.ai.claude-sonnet-5", "Claude Sonnet 5" + ), + ), + ), + plugin.PluginProvider( + id="22222222-2222-5222-8222-222222222222", + wire="responses", + base_path="/ai-gateway/mlflow/v1", + models=(plugin.PluginModel("gpt-6", "system.ai.gpt-6", "GPT-6"),), + ), + plugin.PluginProvider( + id="33333333-3333-5333-8333-333333333333", + wire="completions", + base_path="/ai-gateway/mlflow/v1", + models=(plugin.PluginModel("glm-5", "system.ai.glm-5", "GLM 5"),), + ), + ), + } + values.update(overrides) + return plugin.PluginConfig(**values) + + +@pytest.fixture +def home(tmp_path, monkeypatch): + monkeypatch.setattr(Path, "home", lambda: tmp_path) + monkeypatch.setattr(plugin.time, "sleep", lambda seconds: None) + return tmp_path + + +def _settings(home: Path) -> Path: + return home / ".copilot" / "settings.json" + + +class TestExtensionConfig: + def test_providers_keep_ids_wires_and_headers(self): + config = plugin.extension_config(_config()) + claude, gpt, open_models = config["providers"] + assert claude == { + "name": "11111111-1111-5111-8111-111111111111", + "type": "anthropic", + "baseUrl": "https://example.cloud.databricks.com/ai-gateway/anthropic", + "headers": { + "User-Agent": "ucode/1.0 copilot/1.4.2", + "x-databricks-use-coding-agent-mode": "true", + }, + } + assert (gpt["type"], gpt["wireApi"], gpt["baseUrl"]) == ( + "openai", + "responses", + "https://example.cloud.databricks.com/ai-gateway/mlflow/v1", + ) + assert (open_models["type"], open_models["wireApi"]) == ("openai", "completions") + + def test_models_match_the_picker_rows(self): + models = plugin.extension_config(_config())["models"] + assert models[0] == { + "id": "claude-sonnet-5", + "modelId": "claude-sonnet-5", + "provider": "11111111-1111-5111-8111-111111111111", + "wireModel": "system.ai.claude-sonnet-5", + "name": "Claude Sonnet 5", + } + assert [m["provider"][0] for m in models] == ["1", "2", "3"] + + def test_models_repeat_the_rows_limits_and_vision(self): + model = plugin.PluginModel( + "claude-sonnet-5", + "system.ai.claude-sonnet-5", + "Claude Sonnet 5", + (936_000, 64_000), + True, + ) + provider = plugin.PluginProvider("p", "anthropic", "/ai-gateway/anthropic", (model,)) + (entry,) = plugin.extension_config(_config(providers=(provider,)))["models"] + assert entry["maxPromptTokens"] == 936_000 + assert entry["maxOutputTokens"] == 64_000 + assert entry["maxContextWindowTokens"] == 1_000_000 + assert entry["capabilities"] == {"supports": {"vision": True}} + + def test_a_small_output_limit_is_left_to_the_runtime(self): + model = plugin.PluginModel("m", "system.ai.m", "M", (100_000, 16_000)) + provider = plugin.PluginProvider("p", "completions", "/ai-gateway/mlflow/v1", (model,)) + (entry,) = plugin.extension_config(_config(providers=(provider,)))["models"] + assert entry["maxPromptTokens"] == 100_000 + assert "maxOutputTokens" not in entry + assert "maxContextWindowTokens" not in entry + + def test_reasoning_effort_can_be_turned_off(self): + model = plugin.PluginModel("glm-5", "system.ai.glm-5", "GLM 5", reasoning_effort=False) + provider = plugin.PluginProvider("p", "completions", "/ai-gateway/mlflow/v1", (model,)) + (entry,) = plugin.extension_config(_config(providers=(provider,)))["models"] + assert entry["capabilities"] == {"supports": {"reasoningEffort": False}} + assert "capabilities" not in plugin.extension_config(_config())["models"][1] + + def test_app_rows_and_databricks_path(self): + rows = plugin.AppProviderRows( + provider={"id": "p", "name": "Databricks GPT", "kind": "custom", "settings": {}}, + secret=None, + models=({"id": "m", "providerId": "p", "modelId": "gpt-6"},), + ) + config = plugin.extension_config( + _config( + app_providers=(rows,), + owned_provider_ids=("p", "q"), + databricks_path="/usr/local/bin/databricks", + ) + ) + assert config["app"] == { + "providers": [ + { + "provider": { + "id": "p", + "name": "Databricks GPT", + "kind": "custom", + "settings": {}, + }, + "secret": None, + "models": [{"id": "m", "providerId": "p", "modelId": "gpt-6"}], + } + ], + "ownedProviderIds": ["p", "q"], + } + assert config["databricksPath"] == "/usr/local/bin/databricks" + assert plugin.extension_config(_config())["app"] == { + "providers": [], + "ownedProviderIds": [], + } + + def test_a_pat_token_command_and_hint(self): + config = plugin.extension_config(_config(use_pat=True)) + assert config["tokenCommand"][-1] == "--use-pat" + assert config["tokenCommand"][0] == "/opt/ug/bin/ug" + assert config["ugPath"] == "/opt/ug/bin/ug" + assert "`token = ` entry under [dev]" in config["loginHint"] + + def test_a_custom_oauth_token_command_and_hint(self): + oauth = { + "client_id": "my-app", + "redirect_url": "http://localhost:8020", + "scopes": ["all-apis", "offline_access"], + } + config = plugin.extension_config(_config(profile=None, custom_oauth=oauth)) + assert config["tokenCommand"] == [ + "/opt/ug/bin/ug", + "auth-token", + "--host", + "https://example.cloud.databricks.com", + "--client-id", + "my-app", + "--redirect-url", + "http://localhost:8020", + "--scopes", + "all-apis,offline_access", + ] + assert "Rerun `ug configure`" in config["loginHint"] + with_profile = plugin.extension_config( + _config(profile=None, custom_oauth={**oauth, "profile": "ug-oauth"}) + ) + assert with_profile["tokenCommand"][-2:] == ["--profile", "ug-oauth"] + + def test_a_missing_ug_hint_names_its_path(self): + assert "/opt/ug/bin/ug" in plugin.extension_config(_config())["missingUgHint"] + + def test_token_command_and_hint_name_the_workspace_and_profile(self): + config = plugin.extension_config(_config()) + assert config["tokenCommand"] == [ + "/opt/ug/bin/ug", + "auth-token", + "--host", + "https://example.cloud.databricks.com", + "--profile", + "dev", + ] + assert ( + "databricks auth login --host https://example.cloud.databricks.com --profile dev" + in (config["loginHint"]) + ) + assert config["pathDirs"] == ["/opt/ug/bin"] + + def test_without_profile(self): + config = plugin.extension_config(_config(profile=None)) + assert "--profile" not in config["tokenCommand"] + assert "--profile" not in config["loginHint"] + + def test_trailing_slash_on_workspace(self): + config = plugin.extension_config(_config(workspace="https://example.cloud.databricks.com/")) + assert ( + config["providers"][0]["baseUrl"] + == "https://example.cloud.databricks.com/ai-gateway/anthropic" + ) + + +class TestWritePlugin: + def test_layout(self, home): + root = plugin.write_plugin(_config()) + assert root == home / ".ucode" / "copilot-app-plugin" + marketplace = json.loads((root / ".github" / "plugin" / "marketplace.json").read_text()) + assert marketplace["name"] == "databricks-unity-gateway" + assert marketplace["plugins"][0]["source"] == "./plugins/unity-gateway" + manifest = json.loads( + (root / "plugins" / "unity-gateway" / ".plugin" / "plugin.json").read_text() + ) + assert manifest["name"] == "unity-gateway" + extension = root / "plugins" / "unity-gateway" / "extensions" / "databricks" + assert {p.name for p in extension.iterdir()} == { + "extension.mjs", + "ug-plugin.mjs", + "app-rows.mjs", + "app-remove.mjs", + "app-restart.mjs", + "config.json", + } + assert (extension / "extension.mjs").read_text() == ( + PLUGIN_SOURCE / "extension.mjs" + ).read_text() + assert json.loads((extension / "config.json").read_text()) == plugin.extension_config( + _config() + ) + + def test_rewrite_replaces_config(self, home): + plugin.write_plugin(_config()) + root = plugin.write_plugin(_config(profile="other")) + config = json.loads( + ( + root / "plugins" / "unity-gateway" / "extensions" / "databricks" / "config.json" + ).read_text() + ) + assert config["tokenCommand"][-1] == "other" + + +class TestSettings: + def test_install_merges_only_ug_keys(self, home): + path = _settings(home) + path.parent.mkdir() + existing = { + "model": "gpt-5", + "extraKnownMarketplaces": {"other": {"source": {"source": "github", "repo": "o/r"}}}, + "enabledPlugins": {"x@other": False}, + } + path.write_text(json.dumps(existing)) + root = plugin.install(_config()) + settings = json.loads(path.read_text()) + assert settings["model"] == "gpt-5" + assert settings["extraKnownMarketplaces"] == { + "other": {"source": {"source": "github", "repo": "o/r"}}, + "databricks-unity-gateway": {"source": {"source": "directory", "path": str(root)}}, + } + assert settings["enabledPlugins"] == { + "x@other": False, + "unity-gateway@databricks-unity-gateway": True, + } + assert "telemetry" not in settings + + def test_install_creates_settings(self, home): + plugin.install(_config()) + settings = json.loads(_settings(home).read_text()) + assert set(settings) == {"extraKnownMarketplaces", "enabledPlugins"} + + def test_install_is_idempotent(self, home): + plugin.install(_config()) + first = _settings(home).read_text() + plugin.install(_config()) + assert _settings(home).read_text() == first + + @pytest.mark.parametrize("content", ["{not json", "// comment\n{}", "[]"]) + def test_invalid_settings_are_left_alone(self, home, content): + path = _settings(home) + path.parent.mkdir() + path.write_text(content) + with pytest.raises(plugin.SettingsError): + plugin.install(_config()) + assert path.read_text() == content + assert not (home / ".ucode" / "copilot-app-plugin").exists() + assert plugin.settings_valid() is False + assert plugin.status() == "invalid_settings" + + def test_non_object_plugin_keys_are_left_alone(self, home): + path = _settings(home) + path.parent.mkdir() + path.write_text(json.dumps({"enabledPlugins": ["x"]})) + with pytest.raises(plugin.SettingsError): + plugin.enable() + assert json.loads(path.read_text()) == {"enabledPlugins": ["x"]} + + def test_copilot_home(self, home, monkeypatch): + custom = home / "custom-copilot" + monkeypatch.setenv("COPILOT_HOME", str(custom)) + plugin.install(_config()) + assert (custom / "settings.json").exists() + assert not _settings(home).exists() + + def test_remove_leaves_no_ug_state(self, home): + path = _settings(home) + path.parent.mkdir() + path.write_text(json.dumps({"model": "gpt-5", "enabledPlugins": {"x@other": True}})) + root = plugin.install(_config()) + assert plugin.remove() is True + assert json.loads(path.read_text()) == { + "model": "gpt-5", + "enabledPlugins": {"x@other": True}, + } + assert not root.exists() + assert plugin.remove() is False + + def test_remove_deletes_a_settings_file_ug_created(self, home): + plugin.install(_config()) + assert plugin.remove() is True + assert not _settings(home).exists() + + def test_remove_keeps_a_created_settings_file_the_user_added_to(self, home): + plugin.install(_config()) + settings = json.loads(_settings(home).read_text()) + settings["model"] = "gpt-5" + _settings(home).write_text(json.dumps(settings)) + assert plugin.remove() is True + assert json.loads(_settings(home).read_text()) == {"model": "gpt-5"} + + def test_remove_keeps_an_empty_settings_file_ug_didnt_create(self, home): + _settings(home).parent.mkdir() + _settings(home).write_text("{}") + plugin.install(_config()) + assert plugin.remove() is True + assert json.loads(_settings(home).read_text()) == {} + + def test_a_finished_deferred_remove_deletes_a_settings_file_ug_created(self, home): + plugin.install(_config()) + plugin.defer_remove() + _settings(home).write_text("{}") + assert plugin.cleanup_leftover() is True + assert not _settings(home).exists() + + def test_remove_drops_the_keys_before_the_directory(self, home): + _settings(home).parent.mkdir() + _settings(home).write_text(json.dumps({"model": "x"})) + root = plugin.install(_config()) + seen = [] + + def wait(seconds): + settings = json.loads(_settings(home).read_text()) + seen.append((seconds, settings, root.exists())) + + assert plugin.remove(wait=wait) is True + assert seen == [(plugin.UNLOAD_WAIT_S, {"model": "x"}, True)] + assert not root.exists() + + def test_remove_without_keys_deletes_the_directory_at_once(self, home): + root = plugin.write_plugin(_config()) + waits = [] + assert plugin.remove(wait=waits.append) is True + assert waits == [] + assert not root.exists() + + def test_remove_keeps_a_foreign_unity_gateway_marketplace(self, home): + path = _settings(home) + path.parent.mkdir() + foreign = { + "extraKnownMarketplaces": { + "databricks-unity-gateway": { + "source": {"source": "directory", "path": "/elsewhere"} + } + }, + "enabledPlugins": {"unity-gateway@databricks-unity-gateway": True}, + } + path.write_text(json.dumps(foreign)) + assert plugin.remove(wait=lambda seconds: None) is False + assert json.loads(path.read_text()) == foreign + + def test_a_symlinked_settings_file_is_refused(self, home, tmp_path): + target = tmp_path / "real-settings.json" + target.write_text("{}") + _settings(home).parent.mkdir() + _settings(home).symlink_to(target) + with pytest.raises(plugin.SettingsError, match="symlink"): + plugin.install(_config()) + assert not plugin.plugin_root().exists() + assert target.read_text() == "{}" + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX file modes") + def test_rewriting_settings_keeps_their_mode(self, home): + path = _settings(home) + path.parent.mkdir() + path.write_text("{}") + path.chmod(0o644) + plugin.install(_config()) + assert path.stat().st_mode & 0o777 == 0o644 + plugin.remove() + assert path.stat().st_mode & 0o777 == 0o644 + + def test_remove_drops_empty_keys(self, home): + _settings(home).parent.mkdir() + _settings(home).write_text(json.dumps({"model": "x"})) + plugin.install(_config()) + plugin.remove() + assert json.loads(_settings(home).read_text()) == {"model": "x"} + + def test_remove_with_invalid_settings_changes_nothing(self, home): + root = plugin.install(_config()) + _settings(home).write_text("{oops") + with pytest.raises(plugin.SettingsError): + plugin.remove() + assert root.exists() + + +def _extension_config(root: Path) -> dict: + return json.loads( + ( + root / "plugins" / "unity-gateway" / "extensions" / "databricks" / "config.json" + ).read_text() + ) + + +def _remove_keys_as_the_extension_does(home: Path) -> None: + settings = json.loads(_settings(home).read_text()) + del settings["extraKnownMarketplaces"]["databricks-unity-gateway"] + del settings["enabledPlugins"]["unity-gateway@databricks-unity-gateway"] + _settings(home).write_text(json.dumps({k: v for k, v in settings.items() if v})) + + +class TestDeferredRemove: + def test_defer_remove_marks_the_config_and_keeps_the_plugin_enabled(self, home): + root = plugin.install(_config(owned_provider_ids=("p",))) + settings = _settings(home).read_text() + assert plugin.defer_remove() is True + config = _extension_config(root) + assert config["remove"] is True + assert config["pluginRoot"] == str(root) + assert config["app"]["ownedProviderIds"] == ["p"] + assert _settings(home).read_text() == settings + assert plugin.status() == "removal_pending" + + def test_defer_remove_without_a_plugin(self, home): + assert plugin.defer_remove() is False + assert not plugin.plugin_root().exists() + plugin.write_plugin(_config()) + assert plugin.defer_remove() is False + assert "remove" not in _extension_config(plugin.plugin_root()) + + @pytest.mark.parametrize("content", ["{not json", "[]"]) + def test_defer_remove_refuses_invalid_settings(self, home, content): + root = plugin.install(_config()) + _settings(home).write_text(content) + with pytest.raises(plugin.SettingsError): + plugin.defer_remove() + assert "remove" not in _extension_config(root) + + def test_defer_remove_refuses_a_symlinked_settings_file(self, home, tmp_path): + root = plugin.install(_config()) + target = tmp_path / "real-settings.json" + target.write_text(_settings(home).read_text()) + _settings(home).unlink() + _settings(home).symlink_to(target) + with pytest.raises(plugin.SettingsError, match="symlink"): + plugin.defer_remove() + assert "remove" not in _extension_config(root) + + def test_install_cancels_a_deferred_remove(self, home): + plugin.install(_config()) + plugin.defer_remove() + plugin.install(_config()) + assert plugin.status() == "installed" + + def test_a_disabled_plugin_is_not_pending(self, home): + plugin.install(_config()) + plugin.defer_remove() + settings = json.loads(_settings(home).read_text()) + settings["enabledPlugins"]["unity-gateway@databricks-unity-gateway"] = False + _settings(home).write_text(json.dumps(settings)) + assert plugin.status() == "disabled" + + def test_cleanup_leftover_waits_for_the_extension(self, home): + root = plugin.install(_config()) + plugin.defer_remove() + assert plugin.cleanup_leftover() is False + assert root.exists() + _remove_keys_as_the_extension_does(home) + assert plugin.status() == "not_installed" + assert plugin.cleanup_leftover() is True + assert not root.exists() + assert plugin.cleanup_leftover() is False + + def test_cleanup_leftover_keeps_a_plugin_without_a_deferred_remove(self, home): + root = plugin.install(_config()) + _remove_keys_as_the_extension_does(home) + assert plugin.cleanup_leftover() is False + assert root.exists() + + def test_cleanup_leftover_keeps_the_directory_while_a_key_is_left(self, home): + root = plugin.install(_config()) + plugin.defer_remove() + settings = json.loads(_settings(home).read_text()) + del settings["extraKnownMarketplaces"] + _settings(home).write_text(json.dumps(settings)) + assert plugin.cleanup_leftover() is False + assert root.exists() + + def test_cleanup_leftover_with_invalid_settings_changes_nothing(self, home): + root = plugin.install(_config()) + plugin.defer_remove() + _settings(home).write_text("{oops") + assert plugin.cleanup_leftover() is False + assert root.exists() + + def test_remove_after_the_extension_deletes_the_directory_at_once(self, home): + root = plugin.install(_config()) + plugin.defer_remove() + _remove_keys_as_the_extension_does(home) + waits = [] + assert plugin.remove(wait=waits.append) is True + assert waits == [] + assert not root.exists() + + def test_remove_while_pending_still_drops_the_keys_first(self, home): + root = plugin.install(_config()) + plugin.defer_remove() + waits = [] + assert plugin.remove(wait=waits.append) is True + assert waits == [plugin.UNLOAD_WAIT_S] + assert not _settings(home).exists() + assert not root.exists() + + +class TestStatus: + def test_not_installed(self, home): + assert plugin.status() == "not_installed" + + def test_installed(self, home): + plugin.install(_config()) + assert plugin.status() == "installed" + + def test_disabled(self, home): + plugin.install(_config()) + path = _settings(home) + settings = json.loads(path.read_text()) + settings["enabledPlugins"]["unity-gateway@databricks-unity-gateway"] = False + path.write_text(json.dumps(settings)) + assert plugin.status() == "disabled" + + def test_missing_directory_is_not_installed(self, home): + root = plugin.install(_config()) + shutil.rmtree(root) + assert plugin.status() == "not_installed" + + +@pytest.mark.skipif(NODE is None, reason="node is not installed") +class TestExtensionSource: + @pytest.mark.parametrize( + "name", + [ + "extension.mjs", + "ug-plugin.mjs", + "app-rows.mjs", + "app-remove.mjs", + "app-restart.mjs", + ], + ) + def test_syntax(self, name): + subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) + + def test_the_extension_finishes_a_deferred_remove_for_ug(self, home, tmp_path): + root = plugin.install(_config()) + plugin.defer_remove() + extension = root / "plugins" / "unity-gateway" / "extensions" / "databricks" + script = ( + "import { readFileSync } from 'node:fs';" + f"import {{ removeOnce }} from {json.dumps((extension / 'app-remove.mjs').as_uri())};" + "const [config, home, stateDir] = process.argv.slice(1);" + "console.log(await removeOnce(JSON.parse(readFileSync(config, 'utf8')), { home, stateDir }));" + ) + result = subprocess.run( + [ + NODE, + "--input-type=module", + "-e", + script, + str(extension / "config.json"), + str(_settings(home).parent), + str(extension), + ], + capture_output=True, + text=True, + ) + assert result.returncode == 0, result.stderr + assert result.stdout.strip() == "removed" + assert json.loads(_settings(home).read_text()) == {} + assert plugin.cleanup_leftover() is True + assert not root.exists() + + +class TestEarlierIds: + LEGACY_MARKETPLACE = "unity-gateway" + LEGACY_KEY = "databricks@unity-gateway" + + def _legacy_install(self, root: Path, path: str | None = None) -> None: + extension = root / "plugins" / "databricks" / "extensions" / "databricks" + extension.mkdir(parents=True) + (extension / "extension.mjs").write_text("") + settings = { + "model": "m", + "extraKnownMarketplaces": { + self.LEGACY_MARKETPLACE: { + "source": {"source": "directory", "path": path or str(root)} + } + }, + "enabledPlugins": {self.LEGACY_KEY: True}, + } + plugin.copilot_settings_path().parent.mkdir(parents=True, exist_ok=True) + plugin.copilot_settings_path().write_text(json.dumps(settings)) + + def test_an_earlier_install_counts_as_installed(self, home): + self._legacy_install(plugin.plugin_root()) + + assert plugin.status() == "installed" + + def test_install_moves_an_earlier_install_to_the_current_id(self, home): + root = plugin.plugin_root() + self._legacy_install(root) + + plugin.install(_config()) + + settings = json.loads(plugin.copilot_settings_path().read_text()) + assert settings["model"] == "m" + assert list(settings["extraKnownMarketplaces"]) == [plugin.MARKETPLACE] + assert settings["enabledPlugins"] == {plugin.PLUGIN_KEY: True} + assert not (root / "plugins" / "databricks").exists() + assert plugin.status() == "installed" + + def test_remove_takes_out_an_earlier_install(self, home): + self._legacy_install(plugin.plugin_root()) + + assert plugin.remove(wait=lambda _: None) + + assert json.loads(plugin.copilot_settings_path().read_text()) == {"model": "m"} + assert not plugin.plugin_root().exists() + + def test_a_deferred_remove_marks_an_earlier_installs_extension(self, home): + root = plugin.plugin_root() + self._legacy_install(root) + config = root / "plugins" / "databricks" / "extensions" / "databricks" / "config.json" + config.write_text("{}") + + assert plugin.defer_remove() + + assert json.loads(config.read_text())["remove"] is True + assert plugin.status() == "removal_pending" + + def test_an_earlier_id_at_another_path_isnt_ugs(self, home): + self._legacy_install(plugin.plugin_root(), path="/elsewhere") + + plugin.install(_config()) + plugin.remove(wait=lambda _: None) + + settings = json.loads(plugin.copilot_settings_path().read_text()) + assert settings["enabledPlugins"] == {self.LEGACY_KEY: True} + assert settings["extraKnownMarketplaces"][self.LEGACY_MARKETPLACE]["source"]["path"] == ( + "/elsewhere" + ) + + +@pytest.mark.parametrize("key", ["extraKnownMarketplaces", "enabledPlugins"]) +def test_install_refuses_a_non_object_key_before_writing_the_plugin(home, key): + path = plugin.copilot_settings_path() + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps({key: []})) + + with pytest.raises(plugin.SettingsError, match=key): + plugin.install(_config()) + + assert not plugin.plugin_root().exists() + assert json.loads(path.read_text()) == {key: []} + + +def test_install_leaves_another_marketplace_of_its_name_alone(home): + path = plugin.copilot_settings_path() + path.parent.mkdir(parents=True, exist_ok=True) + foreign = {"source": {"source": "directory", "path": "/elsewhere"}} + path.write_text(json.dumps({"extraKnownMarketplaces": {plugin.MARKETPLACE: foreign}})) + + with pytest.raises(plugin.SettingsError, match="another plugin marketplace"): + plugin.install(_config()) + + assert not plugin.plugin_root().exists() + assert json.loads(path.read_text())["extraKnownMarketplaces"] == {plugin.MARKETPLACE: foreign} + + +class TestStagedWrite: + def _extension(self, root: Path) -> Path: + return root / "plugins" / plugin.PLUGIN / "extensions" / plugin.EXTENSION + + def _siblings(self, root: Path) -> list[str]: + return sorted(p.name for p in root.parent.iterdir() if p.name.startswith(f".{root.name}")) + + def test_a_reinstall_swaps_in_a_new_tree_and_keeps_the_extensions_state(self, home): + root = plugin.install(_config()) + (self._extension(root) / "app-restart.json").write_text('{"digest": "d"}') + (self._extension(root) / "app-sync.lock").write_text("owner") + + plugin.install(_config(profile="other")) + + config = json.loads((self._extension(root) / "config.json").read_text()) + assert config["tokenCommand"][-1] == "other" + assert (self._extension(root) / "app-restart.json").read_text() == '{"digest": "d"}' + assert not (self._extension(root) / "app-sync.lock").exists() + assert (root / "created-settings").is_file() + assert self._siblings(root) == [] + + def test_a_failed_write_leaves_the_installed_plugin_and_settings_alone(self, home, monkeypatch): + root = plugin.install(_config()) + before = (self._extension(root) / "config.json").read_text() + settings = plugin.copilot_settings_path().read_text() + copies = [] + + real = shutil.copyfile + + def failing_copy(src, dst): + copies.append(dst) + if len(copies) == 2: + raise OSError("disk full") + return real(src, dst) + + monkeypatch.setattr(plugin.shutil, "copyfile", failing_copy) + + with pytest.raises(OSError, match="disk full"): + plugin.install(_config(profile="other")) + + assert (self._extension(root) / "config.json").read_text() == before + assert plugin.copilot_settings_path().read_text() == settings + assert self._siblings(root) == [] + + def test_a_first_install_that_fails_writes_no_settings(self, home, monkeypatch): + def fail(staged, root): + raise OSError("busy") + + monkeypatch.setattr(plugin, "_publish", fail) + + with pytest.raises(OSError, match="busy"): + plugin.install(_config()) + + assert not plugin.copilot_settings_path().exists() + assert not plugin.plugin_root().exists() + + def test_a_failed_swap_puts_the_old_plugin_back(self, home, monkeypatch): + root = plugin.install(_config()) + before = (self._extension(root) / "config.json").read_text() + real = os.replace + + def replace(src, dst): + if ".staging-" in Path(src).name: + raise PermissionError("in use") + return real(src, dst) + + monkeypatch.setattr(plugin.os, "replace", replace) + + with pytest.raises(PermissionError): + plugin.write_plugin(_config(profile="other")) + + assert (self._extension(root) / "config.json").read_text() == before + assert self._siblings(root) == [] + + def test_a_crashed_runs_leftovers_are_removed_once_old(self, home): + root = plugin.plugin_root() + root.parent.mkdir(parents=True, exist_ok=True) + old = root.with_name(f".{root.name}.staging-old") + fresh = root.with_name(f".{root.name}.replaced-fresh") + for leftover in (old, fresh): + (leftover / "x").mkdir(parents=True) + stale = time.time() - 3600 + os.utime(old, (stale, stale)) + + plugin.write_plugin(_config()) + + assert not old.exists() + assert fresh.exists() + + +class TestSyncLock: + def _lock(self, root: Path) -> Path: + return root / "plugins" / plugin.PLUGIN / "extensions" / plugin.EXTENSION / "app-sync.lock" + + def test_holds_the_extensions_lock_and_releases_only_its_own(self, home): + root = plugin.install(_config()) + + with plugin.sync_lock() as held: + assert held + owner = self._lock(root).read_text() + assert owner.startswith("ug-") + + assert not self._lock(root).exists() + + def test_waits_for_an_extension_then_gives_up(self, home): + root = plugin.install(_config()) + self._lock(root).write_text("1234-extension") + clock = [0.0] + + def sleep(seconds): + clock[0] += seconds + + with plugin.sync_lock(timeout=1.0, sleep=sleep, clock=lambda: clock[0]) as held: + assert not held + + assert self._lock(root).read_text() == "1234-extension" + assert clock[0] >= 1.0 + + def test_takes_over_a_stale_lock(self, home): + root = plugin.install(_config()) + self._lock(root).write_text("1234-dead") + stale = time.time() - 120 + os.utime(self._lock(root), (stale, stale)) + + with plugin.sync_lock(timeout=0) as held: + assert held + + @pytest.mark.skipif(NODE is None, reason="node is not installed") + def test_the_extension_sees_ugs_lock(self, home): + root = plugin.install(_config()) + rows = ( + root / "plugins" / plugin.PLUGIN / "extensions" / plugin.EXTENSION / "app-rows.mjs" + ).as_uri() + script = ( + f"import {{ tryLock }} from {json.dumps(rows)};" + "console.log(tryLock(process.argv[1], Date.now) === null);" + ) + with plugin.sync_lock() as held: + assert held + result = subprocess.run( + [NODE, "--input-type=module", "-e", script, str(self._lock(root))], + capture_output=True, + text=True, + check=True, + ) + assert result.stdout.strip() == "true" + + def test_without_a_plugin_it_holds_nothing(self, home): + with plugin.sync_lock() as held: + assert not held