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/__init__.py b/src/ucode/agents/__init__.py index 496bad8b0..b8a791788 100644 --- a/src/ucode/agents/__init__.py +++ b/src/ucode/agents/__init__.py @@ -780,6 +780,40 @@ def _remove_vscode_for_managed_config() -> None: ) +_copilot_app_with_copilot = True + + +def request_copilot_app_with_copilot(requested: bool = True) -> None: + """Whether configuring Copilot in this run also sets up the GitHub Copilot desktop app + (``ug configure --no-copilot-app`` passes ``False``).""" + global _copilot_app_with_copilot + _copilot_app_with_copilot = requested + + +def configure_copilot_app(state: dict, *, provider: str | None, parent_schema: str | None) -> None: + """Install ug's plugin in the GitHub Copilot desktop app when it is installed, or remove it + when the workspace's managed config turns the app off.""" + from ucode.agents.desktop import setup as desktop_setup + + if not desktop_setup.copilot_app_enabled(): + return + if desktop_setup.copilot_app_state(state).get("copilot_app_integration") is False: + try: + result = desktop_setup.revert_copilot_app() + except OSError as exc: + print_warning(f"Could not remove ug's plugin from the GitHub Copilot app: {exc}") + return + if result not in ("not set up", desktop_setup.COPILOT_APP_DRY_RUN): + print_note( + "This workspace's managed configuration turns off the GitHub Copilot app, so " + f"ug's plugin was removed from it ({result})." + ) + return + if not _copilot_app_with_copilot or not desktop_setup.copilot_app_installed(): + return + desktop_setup.configure_copilot_app(state, provider=provider, location=parent_schema) + + def configure_single_tool( tool: str, state: dict, @@ -806,6 +840,7 @@ def configure_single_tool( state = _configure_one(tool, state, provider, parent_schema=parent_schema) if with_vscode and tool == AGENT_COPILOT: configure_vscode(state, provider=provider, parent_schema=parent_schema) + configure_copilot_app(state, provider=provider, parent_schema=parent_schema) available_tools = list(set((state.get("available_tools") or []) + [tool])) state["available_tools"] = available_tools save_state(state) @@ -905,6 +940,7 @@ def configure_selected_tools( # `ug configure` sets up Copilot Chat in VS Code along with the Copilot CLI. if tool == AGENT_COPILOT: configure_vscode(state, provider=provider, parent_schema=parent_schema) + configure_copilot_app(state, provider=provider, parent_schema=parent_schema) existing = state.get("available_tools") or [] state["available_tools"] = sorted(set(existing) | set(configured)) diff --git a/src/ucode/agents/copilot.py b/src/ucode/agents/copilot.py index 6cf73385d..5e5ceb8b3 100644 --- a/src/ucode/agents/copilot.py +++ b/src/ucode/agents/copilot.py @@ -435,7 +435,12 @@ def model_labels( def provider_service_catalog( - state: dict, service_name: str, requested: str | None, *, tool: str = "copilot" + state: dict, + service_name: str, + requested: str | None, + *, + tool: str = "copilot", + agent: str | None = None, ) -> list[CopilotModel]: """The models of Model Provider Service ``service_name`` that this launch registers. @@ -447,7 +452,7 @@ def provider_service_catalog( service, error = resolve_provider_service(tool, service_name, workspace, token) if error or service is None: raise RuntimeError(error or f"Model provider service '{service_name}' was not found.") - agent = "Copilot" if tool == "copilot" else "VS Code" + agent = agent or ("Copilot" if tool == "copilot" else "VS Code") provider_type = service["provider_type"] named = list(service.get("targets") or []) listed: list[str] = [] diff --git a/src/ucode/agents/desktop/copilot_app.py b/src/ucode/agents/desktop/copilot_app.py new file mode 100644 index 000000000..d6e8e742d --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app.py @@ -0,0 +1,515 @@ +"""Registers ug's model providers in the GitHub Copilot desktop app. + +The app lists models in its picker only for providers written through the local WebSocket API its +own window uses. ug's Copilot plugin (``copilot_app_plugin``) writes them there whenever the app +runs, then re-supplies the same provider and model ids in every session and serves them from +the AI Gateway, so both come from :func:`desired_providers`. This module also talks to the running +app directly, to check the rows and to delete them at revert. The app publishes the port and a +per-launch token in ``~/.copilot/run/``. The providers have fixed ids, so revert deletes them by +id with no saved state. +""" + +from __future__ import annotations + +import json +import time +import uuid +from collections.abc import Iterable +from dataclasses import dataclass +from pathlib import Path +from typing import Any, Literal +from urllib.parse import urlencode + +from ucode.agents.copilot import ( + WIRE_ANTHROPIC, + WIRE_COMPLETIONS, + WIRE_RESPONSES, + CopilotModel, + catalog_model_limits, + copilot_catalog_model_id, + model_labels, +) +from ucode.agents.desktop.copilot_app_plugin import ( + AppProviderRows, + PluginModel, + PluginProvider, + Wire, + copilot_settings_path, +) +from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER +from ucode.os_compatibility.system_cross_os import process_running +from ucode.ui import print_warning + +# The app versions whose WebSocket messages ug was checked against; others get a warning. +VERIFIED_APP_VERSIONS = frozenset({"1.4.2"}) +# The app only lists models for an Anthropic provider that has a key. The plugin's provider +# replaces it with a bearer token, so it is never sent. +PLACEHOLDER_KEY = "ug-placeholder-not-a-secret" +_ID_NAMESPACE = uuid.UUID("6f1c2a4e-5d1b-4c39-9a57-0d2b8e7f4a11") +_REQUEST_TIMEOUT_S = 15 +# `ug status` reads the app on every run, so a hung app may hold it up only briefly. +_STATUS_TIMEOUT_S = 3 + +AppStatus = Literal["registered", "outdated", "not_registered", "not_running"] + + +class CopilotAppError(RuntimeError): + """The app refused, didn't understand or didn't answer a request. ug sent nothing after it, + but requests already sent may have been applied.""" + + +class CopilotAppNotRunning(CopilotAppError): + """The app isn't running, so its providers can't be changed now.""" + + +@dataclass(frozen=True) +class _ProviderSpec: + key: str + name: str + wire: Wire + + +_PROVIDERS = ( + _ProviderSpec("claude", "Databricks Claude", WIRE_ANTHROPIC), + _ProviderSpec("gpt", "Databricks GPT", WIRE_RESPONSES), + _ProviderSpec("open-models", "Databricks Open Models", WIRE_COMPLETIONS), +) + + +def _provider_id(key: str) -> str: + return str(uuid.uuid5(_ID_NAMESPACE, f"provider/{key}")) + + +def _model_row_id(key: str, model_id: str) -> str: + return str(uuid.uuid5(_ID_NAMESPACE, f"model/{key}/{model_id}")) + + +@dataclass(frozen=True) +class AppProvider: + """One provider as the app's WebSocket API stores it, with its model rows.""" + + key: str + name: str + kind: str + settings: dict[str, str] + secret: dict[str, str] | None + models: tuple[dict[str, Any], ...] + + @property + def id(self) -> str: + return _provider_id(self.key) + + def model_row_id(self, model_id: str) -> str: + return _model_row_id(self.key, model_id) + + def rows(self) -> AppProviderRows: + """The provider and its model rows as the plugin writes them to the app.""" + return AppProviderRows( + provider={ + "id": self.id, + "name": self.name, + "kind": self.kind, + "settings": self.settings, + }, + secret=self.secret, + models=tuple( + {**row, "id": self.model_row_id(row["modelId"]), "providerId": self.id} + for row in self.models + ), + ) + + +@dataclass(frozen=True) +class DesiredProviders: + """The app's providers and the plugin's, which must carry identical ids.""" + + app: tuple[AppProvider, ...] + plugin: tuple[PluginProvider, ...] + # Gateway headers the plugin must add for these providers, beside ug's usual ones. + headers: dict[str, str] + + @property + def plugin_app(self) -> tuple[AppProviderRows, ...]: + """``PluginConfig.app_providers``: the rows the plugin writes to the app.""" + return tuple(provider.rows() for provider in self.app) + + +# ``PluginConfig.owned_provider_ids``: every provider id ug may have written to the app. +OWNED_PROVIDER_IDS = tuple(_provider_id(spec.key) for spec in _PROVIDERS) + + +def copilot_home() -> Path: + return copilot_settings_path().parent + + +def _base_path(wire: Wire, provider_service: str | None) -> str: + if wire == WIRE_ANTHROPIC: + return "/ai-gateway/anthropic" + return "/ai-gateway/openai/v1" if provider_service else "/ai-gateway/mlflow/v1" + + +def _own_model_id(model: str) -> str: + """A row id for gateway model ``model`` that can't be a Copilot catalog id: the app applies its + own settings to a row with a catalog model's id, such as ``reasoning_effort`` for ``kimi-k3``, + over the row's.""" + return model if model.count(".") >= 2 else f"databricks.{model}" + + +def _model_rows( + catalog: Iterable[CopilotModel], wire: str, *, provider_service: str | None +) -> list[dict[str, Any]]: + """The app's model rows for ``catalog``'s models on ``wire``. + + A Claude row's id is Copilot's catalog id for the model behind it, which is what makes the app + apply Claude's request settings, and its wire model is the gateway's id. Other rows get an id + no Copilot catalog model has (see :func:`_own_model_id`). Ids the app would see twice keep the + first, with a warning.""" + models = [model for model in catalog if model.wire == wire] + labels = model_labels(models, provider_service=provider_service) + rows: dict[str, dict[str, Any]] = {} + for model in models: + if wire == WIRE_ANTHROPIC: + model_id = copilot_catalog_model_id(model.catalog_name) + if model_id is None: + print_warning( + f"Skipping {model.id} in the GitHub Copilot app: ug couldn't tell which " + "Claude model it serves." + ) + continue + else: + model_id = _own_model_id(model.id) + if model_id in rows: + print_warning( + f"Skipping {model.id} in the GitHub Copilot app: it shows as {model_id}, like " + f"{rows[model_id]['wireModel']}." + ) + continue + row: dict[str, Any] = { + "modelId": model_id, + "wireModel": model.id, + "displayName": labels[model], + "supportsVision": wire == WIRE_ANTHROPIC, + } + if wire == WIRE_RESPONSES: + row["wireApiOverride"] = "responses" + if limits := catalog_model_limits(model): + row["maxPromptTokens"], row["maxOutputTokens"] = limits + rows[model_id] = row + return list(rows.values()) + + +def _plugin_model(row: dict[str, Any], wire: Wire) -> PluginModel: + limits = None + if "maxPromptTokens" in row: + limits = (row["maxPromptTokens"], row["maxOutputTokens"]) + return PluginModel( + row["modelId"], + row["wireModel"], + row["displayName"], + limits, + row["supportsVision"], + # Every model on Chat Completions mishandles Copilot's `reasoning_effort`: OpenAI and Azure + # reject it for GPT before 5, GLM 5.3 rejects `none`, and GLM and Kimi think less at `high` + # than without it. + reasoning_effort=False if wire == WIRE_COMPLETIONS else None, + ) + + +def desired_providers( + workspace: str, catalog: Iterable[CopilotModel], *, provider_service: str | None = None +) -> DesiredProviders: + """ug's providers on ``workspace``'s AI Gateway for ``catalog`` (see + ``vscode.copilot_models``), each with its models; a wire with no models gets no provider.""" + catalog = list(catalog) + base = workspace.rstrip("/") + app: list[AppProvider] = [] + plugin: list[PluginProvider] = [] + for spec in _PROVIDERS: + rows = _model_rows(catalog, spec.wire, provider_service=provider_service) + if not rows: + continue + base_path = _base_path(spec.wire, provider_service) + anthropic = spec.wire == WIRE_ANTHROPIC + provider = AppProvider( + spec.key, + spec.name, + "anthropic" if anthropic else "custom", + { + "baseUrl": base + base_path, + "wireApi": "completions" if anthropic else spec.wire, + "authKind": "api_key" if anthropic else "none", + "headersJson": "{}", + }, + {"kind": "apiKey", "value": PLACEHOLDER_KEY} if anthropic else None, + tuple(rows), + ) + app.append(provider) + plugin.append( + PluginProvider( + id=provider.id, + wire=spec.wire, + base_path=base_path, + models=tuple(_plugin_model(row, spec.wire) for row in rows), + ) + ) + headers = {MODEL_PROVIDER_SERVICE_HEADER: provider_service} if provider_service else {} + return DesiredProviders(tuple(app), tuple(plugin), headers) + + +def state_providers( + state: dict, *, provider: str | None = None, location: str | None = None +) -> DesiredProviders: + """:func:`desired_providers` for the models VS Code gets from the same source: a Model Provider + Service (``provider``), a ``.`` (``location``) or the discovered catalog.""" + from ucode.agents.vscode import copilot_models + + return desired_providers( + state["workspace"], + copilot_models(state, provider, location, agent="the GitHub Copilot app"), + provider_service=provider, + ) + + +class _AppConnection: + """One connection to the app's WebSocket API, matching each reply to its request.""" + + def __init__( + self, home: Path, *, session_channels: bool = False, timeout: float = _REQUEST_TIMEOUT_S + ) -> None: + from websockets.exceptions import InvalidHandshake, InvalidStatus + from websockets.sync.client import connect + + run = home / "run" + try: + # Each file holds the value, then the app's pid. The app leaves them behind when it + # exits, and another program may since have taken the port. + port_file = (run / "ws.port").read_text().split() + port = int(port_file[0]) + token = (run / "ws.token").read_text().split()[0] + pid = int(port_file[1]) if len(port_file) > 1 else None + except (OSError, ValueError, IndexError): + raise CopilotAppNotRunning("The GitHub Copilot app isn't running.") from None + if pid is not None and not process_running(pid): + raise CopilotAppNotRunning("The GitHub Copilot app isn't running.") + # With session channels, the app also sends every session's events, its window's chats + # included. + query = urlencode( + { + "token": token, + "requester_id": "ug", + "session_channels": "true" if session_channels else "false", + } + ) + url = f"ws://127.0.0.1:{port}/?{query}" + try: + # No Origin: the app refuses any origin but its own window. + self._connection = connect( + url, origin=None, compression=None, open_timeout=min(5, timeout) + ) + self._ws = self._connection.__enter__() + except InvalidStatus as exc: + raise CopilotAppError(f"The GitHub Copilot app refused ug ({exc}).") from None + except (OSError, TimeoutError, InvalidHandshake): + raise CopilotAppNotRunning("The GitHub Copilot app isn't running.") from None + self._timeout = timeout + self._backlog: list[dict[str, Any]] = [] + try: + hello = self._wait(lambda message: message.get("type") == "server_hello") + except Exception: + self.close() + raise + if hello is None: + self.close() + raise CopilotAppError("The GitHub Copilot app didn't greet ug.") + + def _next(self, deadline: float) -> dict[str, Any] | None: + from websockets.exceptions import ConnectionClosed + + if self._backlog: + return self._backlog.pop(0) + while (remaining := deadline - time.monotonic()) > 0: + try: + text = self._ws.recv(timeout=remaining) + except TimeoutError: + return None + except ConnectionClosed: + raise CopilotAppNotRunning( + "The GitHub Copilot app closed ug's connection." + ) from None + try: + message = json.loads(text) + except ValueError: + continue + if isinstance(message, dict): + return message + return None + + def next_message(self, timeout: float) -> dict[str, Any] | None: + """The app's next message within ``timeout`` seconds, or None. Raises + ``CopilotAppNotRunning`` once the app closes the connection.""" + return self._next(time.monotonic() + timeout) + + def _wait(self, matches, timeout: float | None = None) -> dict[str, Any] | None: + deadline = time.monotonic() + (self._timeout if timeout is None else timeout) + skipped: list[dict[str, Any]] = [] + try: + while (message := self._next(deadline)) is not None: + if matches(message): + return message + skipped.append(message) + return None + finally: + self._backlog = skipped + self._backlog + + def request(self, message: dict[str, Any], reply: str, **match: str) -> dict[str, Any]: + """Send ``message`` and return the ``reply`` to it; ``match`` pins reply fields.""" + from websockets.exceptions import ConnectionClosed + + for attempt in range(6): + request_id = f"ug-{uuid.uuid4().hex[:12]}" + try: + self._ws.send(json.dumps({**message, "request_id": request_id})) + except ConnectionClosed: + raise CopilotAppNotRunning( + "The GitHub Copilot app closed ug's connection." + ) from None + + def answers(candidate: dict[str, Any], request_id=request_id) -> bool: + # The app echoes request_id on its replies, but not on errors about the + # message's format, which then answer the pending request. + if candidate.get("type") == "error": + return candidate.get("request_id") in (request_id, None) + return ( + candidate.get("request_id") == request_id + and candidate.get("type") == reply + and all(candidate.get(name) == value for name, value in match.items()) + ) + + answer = self._wait(answers) + if answer is None: + raise CopilotAppError(f"The GitHub Copilot app didn't answer {message['type']}.") + if answer.get("type") != "error": + return answer + error = str(answer.get("message", "")) + # The app refuses a provider change while another one is being applied. + if "still in progress" not in error or attempt == 5: + raise CopilotAppError(f"The GitHub Copilot app refused {message['type']}: {error}") + time.sleep(0.2 * 2**attempt) + raise AssertionError("unreachable") + + def close(self) -> None: + self._connection.__exit__(None, None, None) + + +def _version(app: _AppConnection) -> str | None: + reply = app.request({"type": "get_healthcheck"}, "command_result") + version = ((reply.get("data") or {}).get("app") or {}).get("version") + return version if isinstance(version, str) else None + + +def _providers(app: _AppConnection) -> dict[str, dict[str, Any]]: + providers = app.request({"type": "list_model_providers"}, "model_providers_list").get( + "providers" + ) + if not isinstance(providers, list) or not all( + isinstance(p, dict) and isinstance(p.get("id"), str) for p in providers + ): + raise CopilotAppError("The GitHub Copilot app listed its model providers in a new shape.") + return {provider["id"]: provider for provider in providers} + + +def _models(app: _AppConnection, provider_id: str) -> list[dict[str, Any]]: + models = app.request( + {"type": "list_provider_models", "provider_id": provider_id}, "provider_models_list" + ).get("models") + if not isinstance(models, list): + raise CopilotAppError("The GitHub Copilot app listed its models in a new shape.") + return [ + m + for m in models + if isinstance(m, dict) + and isinstance(m.get("id"), str) + and m.get("providerId") == provider_id + ] + + +def _connect(*, warn_version: bool = True, timeout: float = _REQUEST_TIMEOUT_S) -> _AppConnection: + app = _AppConnection(copilot_home(), timeout=timeout) + try: + version = _version(app) + except CopilotAppNotRunning: + app.close() + raise + except CopilotAppError: + # The version only decides the warning below. + version = None + if warn_version and version not in VERIFIED_APP_VERSIONS: + print_warning( + f"GitHub Copilot app {version or '(unknown version)'} hasn't been verified with ug; " + "continuing anyway." + ) + return app + + +def _delete_provider(app: _AppConnection, provider_id: str) -> None: + app.request( + {"type": "delete_model_provider", "provider_id": provider_id}, + "model_provider_deleted", + provider_id=provider_id, + ) + + +def unregister() -> bool: + """Delete ug's providers from the running app (it also deletes their models and key). + + Returns whether any was there. Raises :class:`CopilotAppNotRunning` when the app is closed.""" + app = _connect() + try: + existing = _providers(app) + removed = False + for spec in _PROVIDERS: + provider_id = _provider_id(spec.key) + if provider_id in existing: + _delete_provider(app, provider_id) + removed = True + return removed + finally: + app.close() + + +def _differs(listed: dict[str, Any], wanted: dict[str, Any]) -> bool: + return any(name in listed and listed[name] != value for name, value in wanted.items()) + + +def status(providers: Iterable[AppProvider]) -> AppStatus: + """Whether the running app has ``providers`` and their models as ug's only providers, with the + same names, settings and row fields (those the app lists back). + + ``outdated`` means it has some of ug's providers, but not these. Raises + :class:`CopilotAppError` when the app refuses.""" + providers = list(providers) + try: + app = _connect(warn_version=False, timeout=_STATUS_TIMEOUT_S) + except CopilotAppNotRunning: + return "not_running" + try: + existing = _providers(app) + ours = {_provider_id(spec.key) for spec in _PROVIDERS} & set(existing) + if not ours: + return "not_registered" + if ours != {provider.id for provider in providers}: + return "outdated" + for provider in providers: + wanted = provider.rows() + if _differs(existing[provider.id], dict(wanted.provider)): + return "outdated" + listed = {model["id"]: model for model in _models(app, provider.id)} + if set(listed) != {model["id"] for model in wanted.models}: + return "outdated" + if any(_differs(listed[model["id"]], dict(model)) for model in wanted.models): + return "outdated" + return "registered" + except CopilotAppNotRunning: + return "not_running" + finally: + app.close() 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/src/ucode/agents/desktop/copilot_app_restart.py b/src/ucode/agents/desktop/copilot_app_restart.py new file mode 100644 index 000000000..aa353d061 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_restart.py @@ -0,0 +1,264 @@ +"""Restarts the GitHub Copilot app once no chat in it is running a turn. + +The app's window loads its model picker's providers only when it starts, so ug's plugin starts this +(``ug copilot-app-restart``) after it changed ug's rows. It runs detached, because the app stops the +extension that starts it soon after the rows change. +""" + +from __future__ import annotations + +import json +import os +import signal +import subprocess +import sys +import time +from collections.abc import Callable, Mapping +from dataclasses import dataclass +from datetime import UTC, datetime +from pathlib import Path +from typing import Any, Protocol + +from ucode.agents.desktop import copilot_app +from ucode.os_compatibility import subprocess_cross_os +from ucode.os_compatibility.system_cross_os import process_running + +QUIET_S = 15.0 +IDLE_TIMEOUT_S = 180.0 +EXIT_TIMEOUT_S = 15.0 +_POLL_S = 1.0 +_EXIT_POLL_S = 0.1 +_RELAUNCH_RETRY_S = 2.0 +_LAUNCH_CHECK_S = 3.0 +_COMMAND_TIMEOUT_S = 10.0 +_PLATFORMS = ("linux", "darwin", "win32") +_TURN_STARTS = frozenset({"user.message", "assistant.turn_start"}) +# The extension that starts this has the Copilot runtime's environment, with the runtime's own +# settings and credentials; the relaunched app gets only the user's session environment. +_SESSION_ENV = frozenset( + { + "HOME", "USER", "LOGNAME", "SHELL", "PATH", "LANG", "LANGUAGE", "TZ", "TMPDIR", "DISPLAY", + "WAYLAND_DISPLAY", "XAUTHORITY", "DBUS_SESSION_BUS_ADDRESS", "DESKTOP_SESSION", + "SSH_AUTH_SOCK", "COPILOT_HOME", "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "ALL_PROXY", + "SYSTEMROOT", "SYSTEMDRIVE", "WINDIR", "COMSPEC", "PATHEXT", "TEMP", "TMP", "USERPROFILE", + "USERNAME", "USERDOMAIN", "HOMEDRIVE", "HOMEPATH", "APPDATA", "LOCALAPPDATA", + "PROGRAMDATA", "PROGRAMFILES", "PROGRAMFILES(X86)", "PROGRAMW6432", "COMMONPROGRAMFILES", + "COMMONPROGRAMFILES(X86)", "COMMONPROGRAMW6432", "ALLUSERSPROFILE", "PUBLIC", + "COMPUTERNAME", "OS", "NUMBER_OF_PROCESSORS", + } +) # fmt: skip +_SESSION_ENV_PREFIXES = ("LC_", "XDG_", "PROCESSOR_") + + +@dataclass(frozen=True) +class Target: + """The app to restart, as the plugin's extension found it.""" + + platform: str + pid: int + # The executable to start again, or the bundle id on macOS. + app: str + home: Path + log: Path | None = None + + +def parse_target(text: str) -> Target | None: + try: + data = json.loads(text) + except ValueError: + return None + if not isinstance(data, dict): + return None + platform, pid, app, home, log = ( + data.get(key) for key in ("platform", "pid", "app", "home", "log") + ) + if ( + platform not in _PLATFORMS + or not isinstance(pid, int) + or isinstance(pid, bool) + or pid <= 1 + or not isinstance(app, str) + or not app + or not isinstance(home, str) + ): + return None + return Target(platform, pid, app, Path(home), Path(log) if isinstance(log, str) else None) + + +class Events(Protocol): + def next_message(self, timeout: float) -> dict[str, Any] | None: ... + + def close(self) -> None: ... + + +def wait_for_idle( + events: Events, + *, + quiet_s: float = QUIET_S, + timeout_s: float = IDLE_TIMEOUT_S, + clock: Callable[[], float] = time.monotonic, +) -> str: + """``idle`` once no chat has a turn open and no session event has arrived for ``quiet_s``. A + running turn sends events all the time, so that also covers a turn that started before the + watch did. ``busy`` after ``timeout_s``, ``stopped`` when the app closes.""" + running: set[object] = set() + start = last = clock() + while True: + now = clock() + if not running and now - last >= quiet_s: + return "idle" + if now - start >= timeout_s: + return "busy" + try: + message = events.next_message(_POLL_S) + except copilot_app.CopilotAppNotRunning: + return "stopped" + if not isinstance(message, dict) or message.get("type") != "session_event": + continue + last = clock() + event = message.get("event") + kind = event.get("type") if isinstance(event, dict) else None + if kind == "session.idle": + running.discard(message.get("session_id")) + elif kind in _TURN_STARTS: + running.add(message.get("session_id")) + + +def session_env(env: Mapping[str, str]) -> dict[str, str]: + return { + name: value + for name, value in env.items() + if name.upper() in _SESSION_ENV or name.upper().startswith(_SESSION_ENV_PREFIXES) + } + + +def _run(argv: list[str]) -> bool: + try: + result = subprocess_cross_os.run( + argv, capture_output=True, timeout=_COMMAND_TIMEOUT_S, check=False + ) + except (OSError, subprocess.SubprocessError): + return False + return result.returncode == 0 + + +def _launch(argv: list[str], env: Mapping[str, str]) -> bool: + if sys.platform == "win32": + detach: dict[str, Any] = { + "creationflags": subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP + } + else: + detach = {"start_new_session": True} + try: + process = subprocess_cross_os.popen( + argv, + env=dict(env), + cwd=str(Path.home()), + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + **detach, + ) + except OSError: + return False + # A launch that fails exits at once and nonzero. + try: + return process.wait(timeout=_LAUNCH_CHECK_S) == 0 + except subprocess.TimeoutExpired: + return True + + +def _terminate(pid: int) -> None: + try: + os.kill(pid, signal.SIGTERM) + except OSError: + pass + + +def restart_app( + target: Target, + *, + alive: Callable[[int], bool] = process_running, + terminate: Callable[[int], None] = _terminate, + run: Callable[[list[str]], bool] = _run, + launch: Callable[[list[str], Mapping[str, str]], bool] = _launch, + sleep: Callable[[float], None] = time.sleep, + clock: Callable[[], float] = time.monotonic, + env: Mapping[str, str] = os.environ, +) -> str: + """Close the app and start it again once it has exited. It's never forced closed, so an app + that stays up is left running.""" + if target.platform == "darwin": + if not run(["/usr/bin/osascript", "-e", f'quit app id "{target.app}"']): + terminate(target.pid) + elif target.platform == "win32": + # Without /F, taskkill closes the app's window as its close button does, which can leave + # the app running in the tray. + run(["taskkill", "/PID", str(target.pid)]) + else: + terminate(target.pid) + deadline = clock() + EXIT_TIMEOUT_S + while alive(target.pid): + if clock() >= deadline: + return "still_running" + sleep(_EXIT_POLL_S) + argv = ["/usr/bin/open", "-b", target.app] if target.platform == "darwin" else [target.app] + clean = session_env(env) + for attempt in range(2): + if attempt: + sleep(_RELAUNCH_RETRY_S) + if launch(argv, clean): + return "relaunched" + return "relaunch_failed" + + +def _watch(home: Path) -> Events: + return copilot_app._AppConnection(home, session_channels=True) + + +def restart_when_idle( + target: Target, + *, + watch: Callable[[Path], Events] = _watch, + quiet_s: float = QUIET_S, + timeout_s: float = IDLE_TIMEOUT_S, + clock: Callable[[], float] = time.monotonic, + **system: Any, +) -> str: + """Restart the app once it's idle. A turn is visible only in the app's session events, so + without them, or once the app closes, it gives up and leaves the app as it is.""" + try: + events = watch(target.home) + except copilot_app.CopilotAppNotRunning: + return "not_running" + except copilot_app.CopilotAppError: + return "no_session_events" + try: + waited = wait_for_idle(events, quiet_s=quiet_s, timeout_s=timeout_s, clock=clock) + finally: + events.close() + if waited != "idle": + return waited + return restart_app(target, clock=clock, **system) + + +def _log(path: Path | None, line: str) -> None: + if path is None: + return + try: + with path.open("a", encoding="utf-8") as file: + file.write(f"{datetime.now(UTC).isoformat()} {line}\n") + except OSError: + pass + + +def main(argument: str) -> None: + """``ug copilot-app-restart``: never raises; the outcome goes to the target's log.""" + target = parse_target(argument) + if target is None: + return + try: + outcome = restart_when_idle(target) + except Exception as exc: # noqa: BLE001 -- a detached helper can report only to its log + outcome = f"failed: {exc}" + _log(target.log, outcome) diff --git a/src/ucode/agents/desktop/setup.py b/src/ucode/agents/desktop/setup.py index a8f2b69ce..6653a8300 100644 --- a/src/ucode/agents/desktop/setup.py +++ b/src/ucode/agents/desktop/setup.py @@ -1,4 +1,9 @@ -"""Best-effort Desktop configuration and cleanup for CLI commands.""" +"""Best-effort Desktop configuration and cleanup for CLI commands. + +For the GitHub Copilot app, ug installs its Copilot plugin. Whenever the app runs, the plugin adds +ug's providers to the app's model picker and serves their models straight from the AI Gateway, so +nothing of ug runs in the background. +""" from __future__ import annotations @@ -7,8 +12,12 @@ import shutil import sys from collections import Counter +from contextlib import nullcontext +from dataclasses import dataclass +from pathlib import Path -from ucode.agents.copilot import claude_model_name +from ucode.agents.copilot import CopilotModel, claude_model_name +from ucode.agents.desktop import copilot_app, copilot_app_plugin from ucode.agents.desktop.claude import ( can_initialize_claude_desktop_directory, claude_desktop_directory, @@ -21,7 +30,8 @@ from ucode.config_io import is_dry_run from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER, MODEL_SERVICE_PARENT_SCHEMA_HEADER from ucode.databricks import build_tool_base_url, get_databricks_token, list_anthropic_model_catalog -from ucode.managed_files import ManagedFileWriteUnavailable, machine_wide_only +from ucode.env import env_bool +from ucode.managed_files import OS, ManagedFileWriteUnavailable, current_os, machine_wide_only from ucode.state import desktop_uses_claude_subscription_relay, get_provider_service from ucode.string_utils import model_picker_label from ucode.ui import print_note, print_success, print_warning @@ -279,3 +289,385 @@ def revert_desktop() -> str: if "removed" in (native_result, *results): return "removed" return "skipped" if "skipped" in results else "unchanged" + + +COPILOT_APP_ENV = "UG_EXPERIMENTAL_COPILOT_APP" + +_COPILOT_APP_LINUX_BINARY = Path("/usr/bin/github") +_COPILOT_APP_WINDOWS_KEY = r"Software\GitHub Inc.\GitHub Copilot" +# User-level telemetry keys the app's runtime rejects; it then ignores the whole settings file, +# ug's plugin included. +_COPILOT_REJECTED_TELEMETRY_KEYS = ( + "enabled", + "endpoint", + "filePath", + "protocol", + "resourceAttributes", +) +_COPILOT_APP_DRY_RUN_NOTE = "Dry run: the GitHub Copilot app isn't changed." +COPILOT_APP_DRY_RUN = "unchanged (dry run)" + + +def copilot_app_enabled() -> bool: + return env_bool(COPILOT_APP_ENV) + + +def _copilot_app_windows_registered() -> bool: + if sys.platform != "win32": + return False + import winreg + + for hive in (winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER): + try: + with winreg.OpenKey(hive, _COPILOT_APP_WINDOWS_KEY): + return True + except OSError: + continue + return False + + +def copilot_app_installed() -> bool: + """Whether the GitHub Copilot desktop app is installed for this user.""" + system = current_os() + if system == OS.MACOS: + return any( + (root / "GitHub Copilot.app").is_dir() + for root in (Path("/Applications"), Path.home() / "Applications") + ) + if system == OS.WINDOWS: + return _copilot_app_windows_registered() + if system == OS.LINUX: + return _COPILOT_APP_LINUX_BINARY.is_file() + return False + + +def copilot_app_state(state: dict) -> dict: + """``state`` with the workspace's managed Copilot settings layered on, as ``ug copilot`` + sees them: admin headers, the provider service and the app switch aren't in state.json.""" + from ucode.managed_config import load_managed_state + from ucode.managed_resolve import resolve_state + from ucode.workspace import state_workspace_key + + if managed := load_managed_state(state_workspace_key(state)): + return resolve_state(managed, state, "copilot") + return state + + +def _with_expanded_sources(state: dict) -> dict: + """``state`` with the managed list's wildcard sources (``..*``) replaced by + what configure last expanded them to. Re-layering the managed config brings the raw sources + back, and a list of only wildcards would read as no list, so the app would get every + discovered model instead of the admin's. A source never expanded fails closed.""" + from ucode.agents.model_sources import is_model_source_wildcard + from ucode.state import get_managed_model_source_expansions + + sources = state.get("copilot_static_models") + if not isinstance(sources, list) or not any( + isinstance(source, str) and is_model_source_wildcard(source) for source in sources + ): + return state + expansions = get_managed_model_source_expansions(state, "copilot") + models: list[str] = [] + for source in sources: + if not isinstance(source, str) or not is_model_source_wildcard(source): + models.append(source) + elif source in expansions: + models.extend(expansions[source]) + else: + raise RuntimeError( + f"the workspace's managed model source {source} isn't expanded yet. Run " + "`ug configure --agents copilot` again" + ) + return {**state, "copilot_static_models": list(dict.fromkeys(models))} + + +def copilot_app_plugin_config( + state: dict, desired: copilot_app.DesiredProviders +) -> copilot_app_plugin.PluginConfig: + """The plugin's config for ``desired``, with the headers ``ug copilot`` sends but its + User-Agent.""" + from ucode.agents.copilot import proxy_headers + from ucode.databricks import databricks_cli_path, ug_binary + from ucode.workspace import selector_headers, workspace_headers + + workspace = state["workspace"] + # Without one, the app's runtime sends its own User-Agent, as Claude Desktop does. + headers = { + name: value + for name, value in {**proxy_headers(state), **desired.headers}.items() + if name.casefold() != "user-agent" + } + # Last, so an admin header can't select another workspace. The app outlives this process's + # workspace selection, so the saved id counts too. + headers.update(workspace_headers(workspace)) + workspace_id = state.get("workspace_id") + headers.update(selector_headers(workspace_id if isinstance(workspace_id, str) else None)) + ug_path = ug_binary() + cli = databricks_cli_path() + path_dirs: list[str] = [] + for executable in (ug_path, cli): + if os.path.isabs(executable) and (directory := os.path.dirname(executable)): + if directory not in path_dirs: + path_dirs.append(directory) + custom_oauth = state.get("custom_oauth") + return copilot_app_plugin.PluginConfig( + workspace=workspace, + ug_path=ug_path, + providers=desired.plugin, + headers=headers, + profile=state.get("profile"), + path_dirs=tuple(path_dirs), + databricks_path=cli if os.path.isabs(cli) else None, + use_pat=bool(state.get("use_pat")), + custom_oauth=custom_oauth if isinstance(custom_oauth, dict) else None, + app_providers=desired.plugin_app, + owned_provider_ids=copilot_app.OWNED_PROVIDER_IDS, + ) + + +def copilot_settings_ignored_reason(path: Path | None = None) -> str | None: + """Why the app's runtime would ignore Copilot's ``settings.json``, and ug's plugin with it.""" + path = path or copilot_app_plugin.copilot_settings_path() + try: + settings = copilot_app_plugin.read_settings(path) + except copilot_app_plugin.SettingsError: + return f"{path} isn't valid JSON" + telemetry = settings.get("telemetry") + if isinstance(telemetry, dict): + keys = [key for key in _COPILOT_REJECTED_TELEMETRY_KEYS if key in telemetry] + if keys: + names = ", ".join(f"telemetry.{key}" for key in keys) + return f"{path} sets {names}, which the app only accepts from managed settings" + return None + + +def copilot_app_seat_relay_note(provider: str) -> str: + return ( + f"{provider} uses a seat subscription, which the GitHub Copilot app doesn't support yet; " + "use `ug copilot` or VS Code for it." + ) + + +def _uses_seat_relay(state: dict, provider: str) -> bool: + """Whether ``provider`` relays a GitHub Copilot seat. The relay's swap header needs a fresh + value per request, and the app's provider headers are fixed.""" + from ucode.agents.copilot import uses_seat_relay + + return uses_seat_relay(state, provider, tool="vscode") + + +def _managed_default_display_name( + state: dict, catalog: list[CopilotModel], desired: copilot_app.DesiredProviders +) -> str | None: + """The app's name for the workspace's managed default model, when the app has a row for it. + ug doesn't set the app's default: the app keeps that per user.""" + from ucode.agents.vscode import managed_default_model + + if not (default := managed_default_model(state, catalog)): + return None + for app_provider in desired.app: + for row in app_provider.models: + if row["wireModel"] == default: + return row["displayName"] + return None + + +def configure_copilot_app(state: dict, *, provider: str | None, location: str | None) -> None: + """Install ug's plugin in the app with the models the Copilot CLI was configured with. + + Problems only warn: the app never fails configure.""" + if is_dry_run(): + print_note(_COPILOT_APP_DRY_RUN_NOTE) + return + _cleanup_copilot_app_leftover() + from ucode.agents.vscode import copilot_models + + try: + state = _with_expanded_sources(copilot_app_state(state)) + if provider and _uses_seat_relay(state, provider): + print_warning(copilot_app_seat_relay_note(provider)) + return + catalog = copilot_models(state, provider, location, agent="the GitHub Copilot app") + except RuntimeError as exc: + print_warning(f"Skipped the GitHub Copilot app: {exc}") + return + desired = copilot_app.desired_providers(state["workspace"], catalog, provider_service=provider) + try: + copilot_app_plugin.install(copilot_app_plugin_config(state, desired)) + except copilot_app_plugin.SettingsError as exc: + print_warning( + f"Skipped the GitHub Copilot app: {exc}, and ug doesn't rewrite a settings file it " + "can't read. Fix it, then run `ug configure --agents copilot` again." + ) + return + # ug's file writers raise RuntimeError for a failed write. + except (OSError, RuntimeError) as exc: + print_warning(f"Could not install ug's plugin in the GitHub Copilot app: {exc}") + return + from ucode.databricks import databricks_cli_path + from ucode.state import record_databricks_cli + + if os.path.isabs(cli := databricks_cli_path()): + try: + record_databricks_cli(cli) + except RuntimeError as exc: + print_warning(str(exc)) + if reason := copilot_settings_ignored_reason(): + print_warning( + f"The GitHub Copilot app ignores its whole settings file while {reason}, so it won't " + "load ug's plugin. Remove those keys." + ) + print_success("GitHub Copilot app: installed ug's plugin with the Databricks models.") + print_note("Open (or restart) the GitHub Copilot app to load the Databricks models.") + if name := _managed_default_display_name(state, catalog, desired): + print_note( + f"To start new chats on {name}, pick it in the GitHub Copilot app; Settings -> " + "Sessions -> Default model makes it stick." + ) + + +COPILOT_APP_REMOVAL_PENDING = ( + "removal pending: the Databricks models are removed the next time the GitHub Copilot app starts" +) + + +def revert_copilot_app() -> str: + """Remove ug's providers from the running app, then ug's plugin. With the app closed, leave + both to the plugin's next start. Never starts the app. Returns a summary for revert.""" + if is_dry_run(): + print_note(_COPILOT_APP_DRY_RUN_NOTE) + return COPILOT_APP_DRY_RUN + _cleanup_copilot_app_leftover() + had_plugin = copilot_app_plugin.plugin_root().exists() + if not had_plugin and not copilot_app_installed(): + return "not set up" + # Marked first, so no extension writes the rows back while ug deletes them. With the app + # closed, the same mark has the plugin delete them on the app's next start. + marked = False + if had_plugin: + try: + marked = copilot_app_plugin.defer_remove() + except copilot_app_plugin.SettingsError as exc: + return f"failed: {exc}; remove ug's databricks-unity-gateway entries from it yourself" + try: + with copilot_app_plugin.sync_lock() if marked else nullcontext(): + unregistered = copilot_app.unregister() + except copilot_app.CopilotAppNotRunning: + if not had_plugin: + return "not set up" + if marked: + return COPILOT_APP_REMOVAL_PENDING + # Without an installed plugin nothing can remove the rows later. + copilot_app_plugin.remove() + return ( + "removed ug's plugin; open the GitHub Copilot app and run `ug revert` again to remove " + "the Databricks models from it" + ) + except copilot_app.CopilotAppError as exc: + return f"failed: {exc}" + try: + plugin_removed = copilot_app_plugin.remove() + except copilot_app_plugin.SettingsError as exc: + if not had_plugin: + return "removed" if unregistered else "not set up" + return f"failed: {exc}; remove ug's databricks-unity-gateway entries from it yourself" + return "removed" if unregistered or plugin_removed else "not set up" + + +def _cleanup_copilot_app_leftover() -> None: + try: + copilot_app_plugin.cleanup_leftover() + except OSError as exc: + print_warning(f"Could not delete ug's old GitHub Copilot app plugin: {exc}") + + +@dataclass(frozen=True) +class CopilotAppStatus: + """The app's row in ``ug status``, and what to tell the user when its turns can't work.""" + + row: str + warnings: tuple[str, ...] = () + + +_COPILOT_PLUGIN_LABELS = { + "installed": "installed", + "removal_pending": "removal pending", + "disabled": "disabled", + "not_installed": "not installed", + "invalid_settings": "invalid settings", +} +_COPILOT_PLUGIN_PROBLEMS = { + "disabled": "is turned off", + "not_installed": "is missing", + "invalid_settings": "isn't loaded, since Copilot's settings.json isn't valid JSON", +} +_COPILOT_REGISTRATION_LABELS = { + "registered": "registered", + "outdated": "out of date", + "not_registered": "not registered", + "not_running": "unknown (app closed)", +} + + +def copilot_app_unconfigured_status() -> str | None: + """The app's row in ``ug status`` once Copilot is no longer configured: a revert with the app + closed is still pending until the plugin's next start.""" + if not copilot_app_enabled(): + return None + _cleanup_copilot_app_leftover() + if copilot_app_plugin.status() == "removal_pending": + return COPILOT_APP_REMOVAL_PENDING + return None + + +def copilot_app_status( + state: dict, *, provider: str | None, location: str | None +) -> CopilotAppStatus | None: + """The app's row in ``ug status`` for ``state`` (managed settings already layered on), or None + when ug doesn't set the app up.""" + if not copilot_app_enabled(): + return None + if state.get("copilot_app_integration") is False: + return CopilotAppStatus("off (workspace-managed)") + _cleanup_copilot_app_leftover() + plugin_status = copilot_app_plugin.status() + if plugin_status == "not_installed" and not copilot_app_installed(): + return None + if plugin_status == "removal_pending": + return CopilotAppStatus(COPILOT_APP_REMOVAL_PENDING) + warnings: list[str] = [] + try: + if provider and _uses_seat_relay(state, provider): + registration = "skipped (seat subscription)" + warnings.append(copilot_app_seat_relay_note(provider)) + else: + desired = copilot_app.state_providers( + _with_expanded_sources(state), provider=provider, location=location + ) + registration = copilot_app.status(desired.app) + except copilot_app.CopilotAppError as exc: + registration = f"unknown ({exc})" + except RuntimeError: + registration = "unknown (no models to compare)" + plugin_label = _COPILOT_PLUGIN_LABELS[plugin_status] + ignored = copilot_settings_ignored_reason() if plugin_status != "invalid_settings" else None + if ignored: + plugin_label += ", ignored" + warnings.append( + f"The GitHub Copilot app ignores its whole settings file while {ignored}, so it " + "doesn't load ug's plugin. Remove those keys." + ) + problem = _COPILOT_PLUGIN_PROBLEMS.get(plugin_status) or ("isn't loaded" if ignored else None) + if registration in ("registered", "outdated") and problem: + warnings.append( + f"ug's plugin for the GitHub Copilot app {problem}, but the Databricks models are in " + "the app's picker, and their turns fail without it. Run `ug configure --agents " + "copilot` again." + ) + elif registration == "outdated": + warnings.append( + "The Databricks models in the GitHub Copilot app don't match this configuration. Run " + "`ug configure --agents copilot` again." + ) + row = f"plugin {plugin_label}, models {_COPILOT_REGISTRATION_LABELS.get(registration, registration)}" + return CopilotAppStatus(row, tuple(warnings)) diff --git a/src/ucode/agents/vscode.py b/src/ucode/agents/vscode.py index f40a2ef84..fbfb8b7b8 100644 --- a/src/ucode/agents/vscode.py +++ b/src/ucode/agents/vscode.py @@ -200,10 +200,18 @@ def _model_entry(model: CopilotModel, name: str) -> dict[str, object]: return entry -def _catalog(state: dict, provider: str | None, location: str | None) -> list[CopilotModel]: - """The models Copilot CLI would offer from the same source; raises when there are none.""" +def copilot_models( + state: dict, + provider: str | None = None, + location: str | None = None, + *, + agent: str = "VS Code", +) -> list[CopilotModel]: + """The models of a Model Provider Service (``provider``), a ``.`` + (``location``) or the discovered catalog, as the Copilot CLI lists them; raises when there are + none. VS Code and the GitHub Copilot app both use it; ``agent`` names which in errors.""" if provider: - return provider_service_catalog(state, provider, None, tool="vscode") + return provider_service_catalog(state, provider, None, tool="vscode", agent=agent) if location: return location_catalog(state, location) catalog = copilot_catalog(state) @@ -213,7 +221,7 @@ def _catalog(state: dict, provider: str | None, location: str | None) -> list[Co if not catalog: reason = (state.get("_discovery_reasons") or {}).get("copilot") raise RuntimeError( - "No Claude, GPT, GLM, Kimi or DeepSeek models for VS Code " + f"No Claude, GPT, GLM, Kimi or DeepSeek models for {agent} " f"({reason or 'the workspace has none'})." ) return catalog @@ -223,7 +231,7 @@ def model_entries( state: dict, *, provider: str | None = None, location: str | None = None ) -> list[dict[str, object]]: """The models of the chosen source, each with the API the extension calls it on.""" - return _entries(_catalog(state, provider, location), provider) + return _entries(copilot_models(state, provider, location), provider) def _entries(catalog: list[CopilotModel], provider: str | None) -> list[dict[str, object]]: @@ -570,7 +578,7 @@ def configure(state: dict, *, provider: str | None = None, location: str | None try: seat_relay = bool(provider) and uses_seat_relay(state, cast(str, provider), tool="vscode") # A seat relay serves Copilot's own models, so the extension lists none of Databricks'. - catalog = [] if seat_relay else _catalog(state, provider, location) + catalog = [] if seat_relay else copilot_models(state, provider, location) except RuntimeError as exc: # Keep what an earlier configure installed: a failed listing shouldn't wipe a working setup. print_warning(f"{exc} VS Code was left unchanged.") diff --git a/src/ucode/cli.py b/src/ucode/cli.py index e2860fff3..a28a447f8 100644 --- a/src/ucode/cli.py +++ b/src/ucode/cli.py @@ -46,6 +46,7 @@ normalize_tool, refuse_user_scope_over_managed_config, refuse_user_scope_over_managed_files, + request_copilot_app_with_copilot, request_vscode_with_copilot, resolve_gemini_provider_model, resolve_launch_model, @@ -2006,6 +2007,18 @@ def status() -> int: ) if tool == AGENT_COPILOT and effective_state.get("copilot_vscode_integration") is False: rows.append(("VS Code", "off (workspace-managed)")) + if tool == AGENT_COPILOT: + from ucode.agents.desktop.setup import copilot_app_status + + app_status = copilot_app_status( + effective_state, + provider=provider_service, + location=get_model_location(effective_state, tool), + ) + if app_status: + rows.append(("GitHub Copilot app", app_status.row)) + for warning in app_status.warnings: + print_warning(warning) if tool in MCP_CLIENTS: # High-level overview: just a count per agent. `ug mcp list` (see the note below) shows # the per-server detail and live connection status, so status stays scannable. The shared @@ -2020,6 +2033,11 @@ def status() -> int: rows.append(("Endpoint", str(base_url or "not configured"))) _print_status_panel(_agent_label(tool), rows) + if AGENT_COPILOT not in configured_tools: + from ucode.agents.desktop.setup import copilot_app_unconfigured_status + + if pending := copilot_app_unconfigured_status(): + _print_status_panel("GitHub Copilot app", [("Status", pending)]) if not configured_tools: print_note("No coding agents are configured.") return 0 @@ -2115,6 +2133,13 @@ def revert() -> int: # VS Code's revert looks at the installed extension, not ug state, so a later run still finds it. print_warning(f"Could not revert VS Code: {exc}. Fix it, then run ug revert again.") vscode_result = "failed" + from ucode.agents.desktop.setup import revert_copilot_app + + try: + copilot_app_result = revert_copilot_app() + except OSError as exc: + print_warning(f"Could not revert the GitHub Copilot app: {exc}. Run ug revert again.") + copilot_app_result = "failed" results: dict[str, bool] = { # Antigravity's skills.json is shared with its app and IDE extensions, so revert @@ -2149,6 +2174,7 @@ def revert() -> int: print_kv("Published agent configuration settings", published_agent_config_result) print_kv("Pi settings", "restored" if pi_settings_restored else "unchanged") print_kv("VS Code extension", vscode_result) + print_kv("GitHub Copilot app", copilot_app_result) for client, spec in MCP_CLIENTS.items(): print_kv( f"{spec['display']} MCP config", @@ -3344,6 +3370,17 @@ def otel_proxy_cmd( raise typer.Exit(1) from None +@app.command("copilot-app-restart", hidden=True) +def copilot_app_restart_cmd( + target: Annotated[str, typer.Argument(help="The app to restart, as JSON from ug's plugin.")], +) -> None: + """Restart the GitHub Copilot app once no chat in it is running a turn. ug's plugin for the + app starts this, detached, after it changed the app's Databricks models.""" + from ucode.agents.desktop import copilot_app_restart + + copilot_app_restart.main(target) + + @app.command("otel-headers", hidden=True) def otel_headers_cmd( host: Annotated[ @@ -5490,6 +5527,15 @@ def configure( "Copilot Chat in VS Code.", ), ] = False, + no_copilot_app: Annotated[ + bool, + typer.Option( + "--no-copilot-app", + hidden=True, + help="Don't add the Databricks models to the GitHub Copilot desktop app. By default, " + "configuring copilot also sets up the app when it is installed.", + ), + ] = False, mcp: Annotated[ str | None, typer.Option( @@ -5609,6 +5655,7 @@ def configure( claude_agent.cleanup_auto_configuration() codex_agent.request_explicit_configure() request_vscode_with_copilot(not no_vscode) + request_copilot_app_with_copilot(not no_copilot_app) custom_oauth = _custom_oauth_config(client_id, redirect_url, scopes) if custom_oauth is not None and use_pat: raise RuntimeError("--client-id cannot be combined with --use-pat.") diff --git a/src/ucode/databricks.py b/src/ucode/databricks.py index 734bfe4b8..7013af98a 100644 --- a/src/ucode/databricks.py +++ b/src/ucode/databricks.py @@ -878,6 +878,12 @@ def perms_ok(path: str) -> bool: search_groups.append( ["/opt/homebrew/bin", "/usr/local/bin", os.path.expanduser("~/.local/bin")] ) + from ucode.state import recorded_databricks_cli + + # The GitHub Copilot app starts `ug auth-token` and `ug otel-headers` with a PATH that may + # lack the CLI's directory. + if recorded := recorded_databricks_cli(): + search_groups.append([os.path.dirname(recorded)]) found: list[str] = [] seen_dirs: set[str] = {""} # seed with "" so empty PATH entries are skipped diff --git a/src/ucode/managed_config.py b/src/ucode/managed_config.py index ef0aec97f..2dee4c1ab 100644 --- a/src/ucode/managed_config.py +++ b/src/ucode/managed_config.py @@ -199,6 +199,7 @@ class AgentConfig: smart_routing_enabled: bool = False otel_tracing_enabled: bool | None = None vscode_integration_enabled: bool | None = None + app_integration_enabled: bool | None = None # Harness-native settings delivered verbatim into the agent's managed settings file. settings: dict | None = None # `agent_native_config`'s variant, keyed by its proto field name (`claude_code`, `codex`, ...) @@ -226,6 +227,7 @@ def from_wire(cls, config: object) -> AgentConfig: smart_routing_enabled=smart_routing.get("enabled") is True, otel_tracing_enabled=_enabled_flag(config_dict.get("tracing")), vscode_integration_enabled=_enabled_flag(config_dict.get("vscode_integration")), + app_integration_enabled=_enabled_flag(config_dict.get("app_integration")), settings=_as_dict(config_dict.get("agent_native_settings")) or None, native_config=native_config or None, ) @@ -244,6 +246,8 @@ def to_internal(self) -> dict: result["otel_tracing_enabled"] = self.otel_tracing_enabled if self.vscode_integration_enabled is not None: result["vscode_integration_enabled"] = self.vscode_integration_enabled + if self.app_integration_enabled is not None: + result["app_integration_enabled"] = self.app_integration_enabled if self.settings: result["agent_native_settings"] = self.settings if self.native_config: diff --git a/src/ucode/managed_resolve.py b/src/ucode/managed_resolve.py index 0939fdf78..f6980bf7e 100644 --- a/src/ucode/managed_resolve.py +++ b/src/ucode/managed_resolve.py @@ -114,6 +114,8 @@ def managed_state_overrides(managed: dict, tool: str) -> dict[str, object]: overrides[f"{tool}_otel_tracing"] = True if tool == "copilot" and _agent_entry(managed, tool).get("vscode_integration_enabled") is False: overrides["copilot_vscode_integration"] = False + if tool == "copilot" and _agent_entry(managed, tool).get("app_integration_enabled") is False: + overrides["copilot_app_integration"] = False settings = _as_dict(_agent_entry(managed, tool).get("agent_native_settings")) if tool in SETTINGS_PASSTHROUGH_TOOLS and settings: overrides[f"{tool}_settings_passthrough"] = settings diff --git a/src/ucode/managed_setup.py b/src/ucode/managed_setup.py index 793a13e11..614cc1702 100644 --- a/src/ucode/managed_setup.py +++ b/src/ucode/managed_setup.py @@ -246,6 +246,9 @@ def _enabled_agent_payload(tool: str, agent_config: dict) -> dict: vscode_enabled = agent_config.get("vscode_integration_enabled") if isinstance(vscode_enabled, bool): config["vscode_integration"] = {"enabled": vscode_enabled} + app_enabled = agent_config.get("app_integration_enabled") + if isinstance(app_enabled, bool): + config["app_integration"] = {"enabled": app_enabled} settings = agent_config.get("agent_native_settings") if isinstance(settings, dict) and settings: config["agent_native_settings"] = settings diff --git a/src/ucode/mcp_proxy_runtime.py b/src/ucode/mcp_proxy_runtime.py index e0258bf90..1c6921c1c 100644 --- a/src/ucode/mcp_proxy_runtime.py +++ b/src/ucode/mcp_proxy_runtime.py @@ -285,6 +285,10 @@ def run_proxy( use_pat: bool = False, workspace_id: str | None = None, ) -> None: + # The GitHub Copilot app starts MCP servers with its runtime's trace exporter settings, which + # would send this process's and the Databricks CLI's spans to the gateway without a token. + for name in [name for name in os.environ if name.startswith(("OTEL_", "COPILOT_OTEL_"))]: + del os.environ[name] state = load_proxy_state() workspace = host or state.get("workspace") if not workspace: diff --git a/src/ucode/metrics/__init__.py b/src/ucode/metrics/__init__.py index 2af6ad2ce..2c76bd2c3 100644 --- a/src/ucode/metrics/__init__.py +++ b/src/ucode/metrics/__init__.py @@ -118,6 +118,7 @@ "codex-router-hook", "cowork-hook", "copilot-seat-runtime", + "copilot-app-restart", "claude-desktop-auth", "smart-router-describe", "smart-router-logs", diff --git a/src/ucode/os_compatibility/system_cross_os.py b/src/ucode/os_compatibility/system_cross_os.py index b6a60a041..ceeb9e63c 100644 --- a/src/ucode/os_compatibility/system_cross_os.py +++ b/src/ucode/os_compatibility/system_cross_os.py @@ -8,6 +8,7 @@ __all__ = [ "is_admin", + "process_running", "user_profiles_dir", "windows_program_data_dir", "windows_program_files_dir", @@ -25,6 +26,33 @@ def is_admin() -> bool: return os.geteuid() == 0 +def process_running(pid: int) -> bool: + """Whether a process with ``pid`` exists. ``os.kill(pid, 0)`` would terminate it on Windows.""" + if pid <= 0: + return False + if sys.platform == "win32": + import ctypes + + kernel32 = ctypes.windll.kernel32 + handle = kernel32.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION + if not handle: + return kernel32.GetLastError() == 5 # ERROR_ACCESS_DENIED: it exists + try: + code = ctypes.c_ulong() + return ( + bool(kernel32.GetExitCodeProcess(handle, ctypes.byref(code))) and code.value == 259 + ) + finally: + kernel32.CloseHandle(handle) + try: + os.kill(pid, 0) + except PermissionError: + return True + except OSError: + return False + return True + + def user_profiles_dir() -> Path | None: """The folder that holds every user's home folder, or ``None`` where there is no standard one.""" if sys.platform == "win32": diff --git a/src/ucode/state.py b/src/ucode/state.py index 3c730ef00..702d6a892 100644 --- a/src/ucode/state.py +++ b/src/ucode/state.py @@ -47,6 +47,7 @@ # next configure without --global runs that workspace-switch reconcile even though it sees no switch. PENDING_USER_MCP_RECONCILE_KEY = "pending_user_mcp_reconcile" CLAUDE_AUTO_REVERT_TARGETS_KEY = "claude_auto_revert_targets" +DATABRICKS_CLI_KEY = "databricks_cli_path" # Transient key holding the developer's own values for whatever a managed config layered over them. # Present only in memory: the layered values render the agent settings files, while `save_state` # restores what's under it so `state.json` keeps recording the developer's own configuration. @@ -90,6 +91,23 @@ def set_claude_auto_revert_targets(targets: dict[str, str]) -> None: raise RuntimeError(f"Failed to retain Claude Auto model targets at {STATE_PATH}") from exc +def recorded_databricks_cli() -> str | None: + """The ``databricks`` CLI configure found, for helpers an app starts without it on PATH.""" + path = load_full_state().get(DATABRICKS_CLI_KEY) + return path if isinstance(path, str) and path else None + + +def record_databricks_cli(path: str) -> None: + if is_dry_run() or recorded_databricks_cli() == path: + return + full = load_full_state() + full[DATABRICKS_CLI_KEY] = path + try: + atomic_write_json(STATE_PATH, full) + except OSError as exc: + raise RuntimeError(f"Failed to write state file: {STATE_PATH}") from exc + + def save_config_scope(scope: str) -> None: if is_dry_run() or load_config_scope() == scope: return diff --git a/tests/conftest.py b/tests/conftest.py index 05b4c2a31..ab8033116 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ import functools import json import os +from pathlib import Path from types import SimpleNamespace import pytest @@ -54,8 +55,11 @@ def _isolate_ucode_state(tmp_path, monkeypatch): from ucode.agents import copilot as copilot_mod from ucode.agents import vscode as vscode_mod from ucode.agents.desktop import claude as desktop_mod + from ucode.agents.desktop import copilot_app as copilot_app_mod + from ucode.agents.desktop import copilot_app_plugin as copilot_app_plugin_mod from ucode.agents.desktop import cowork as cowork_mod from ucode.agents.desktop import mdm as mdm_mod + from ucode.agents.desktop import setup as desktop_setup_mod from ucode.smart_routing import session_log as session_log_mod state_dir = tmp_path / ".ucode" @@ -70,6 +74,25 @@ def _isolate_ucode_state(tmp_path, monkeypatch): monkeypatch.setattr(session_log_mod, "LOG_DIR", state_dir / "smart-router" / "sessions") monkeypatch.setattr(cowork_mod, "_code_config_root", lambda: tmp_path / ".claude") monkeypatch.setattr(agents_mod, "_vscode_with_copilot", True) + monkeypatch.setattr(agents_mod, "_copilot_app_with_copilot", True) + # No test may find the developer's GitHub Copilot app, change its settings or providers, or + # write ug's plugin for it. + monkeypatch.delenv("UG_EXPERIMENTAL_COPILOT_APP", raising=False) + monkeypatch.setattr(desktop_setup_mod, "copilot_app_installed", lambda: False) + copilot_home = tmp_path / ".copilot" + monkeypatch.setattr( + copilot_app_mod, + "copilot_home", + lambda: Path(os.environ.get("COPILOT_HOME") or copilot_home), + ) + monkeypatch.setattr( + copilot_app_plugin_mod, + "copilot_settings_path", + lambda: Path(os.environ.get("COPILOT_HOME") or copilot_home) / "settings.json", + ) + monkeypatch.setattr( + copilot_app_plugin_mod, "plugin_root", lambda: tmp_path / ".ucode" / "copilot-app-plugin" + ) # No test may run the developer's `code` or look at their installed VS Code extensions. monkeypatch.setattr(vscode_mod, "find_code", lambda: None) monkeypatch.setattr(vscode_mod, "_find_command", lambda command: None) 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_cli.py b/tests/test_cli.py index 2f8a97a58..fd5ce5739 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3387,6 +3387,68 @@ def test_status_shows_copilot_vscode_turned_off_by_managed_config(self, monkeypa assert result.exit_code == 0, result.output assert ("VS Code: off (workspace-managed)" in output) is shown + def test_status_shows_the_copilot_app_row_and_its_warnings(self, monkeypatch): + from ucode.agents.desktop.setup import CopilotAppStatus + + state = { + **MINIMAL_STATE, + "available_tools": ["copilot"], + "base_urls": {"copilot": "https://example.databricks.com/ai-gateway/copilot"}, + } + seen = [] + + def app_status(effective_state, *, provider, location): + seen.append(effective_state) + return CopilotAppStatus( + "plugin disabled, models registered", ("their turns fail without it",) + ) + + with ( + patch("ucode.cli.load_state", return_value=state), + patch("ucode.cli.load_managed_state", return_value=None), + patch("ucode.agents.desktop.setup.copilot_app_status", side_effect=app_status), + ): + result = runner.invoke(app, ["status"]) + + output = re.sub(r"\s+", " ", _strip_ansi(result.output)) + assert result.exit_code == 0, result.output + assert "GitHub Copilot app: plugin disabled, models registered" in output + assert "their turns fail without it" in output + assert seen[0]["workspace"] == state["workspace"] + + def test_status_shows_the_copilot_app_turned_off_by_managed_config(self, monkeypatch): + monkeypatch.setenv("UG_EXPERIMENTAL_COPILOT_APP", "1") + state = { + **MINIMAL_STATE, + "available_tools": ["copilot"], + "base_urls": {"copilot": "https://example.databricks.com/ai-gateway/copilot"}, + } + managed = {"enabled_agents": {"copilot": {"app_integration_enabled": False}}} + with ( + patch("ucode.cli.load_state", return_value=state), + patch("ucode.cli.load_managed_state", return_value=managed), + ): + result = runner.invoke(app, ["status"]) + + output = re.sub(r"\s+", " ", _strip_ansi(result.output)) + assert result.exit_code == 0, result.output + assert "GitHub Copilot app: off (workspace-managed)" in output + + def test_status_shows_a_pending_copilot_app_removal_after_revert(self): + with ( + patch("ucode.cli.load_state", return_value={}), + patch( + "ucode.agents.desktop.setup.copilot_app_unconfigured_status", + return_value="removal pending: soon", + ), + ): + result = runner.invoke(app, ["status"]) + + output = re.sub(r"\s+", " ", _strip_ansi(result.output)) + assert result.exit_code == 0, result.output + assert "GitHub Copilot app" in output + assert "removal pending: soon" in output + def test_status_labels_self_configured_setup(self, monkeypatch): with ( patch("ucode.cli.load_state", return_value=MINIMAL_STATE), @@ -4686,6 +4748,27 @@ def test_vscode_revert_failure_is_reported_and_the_rest_still_reverts(self): assert restore_file.called clear_state.assert_called_once() + @pytest.mark.parametrize("result", ["removed", "not set up", "failed: x"]) + def test_reports_the_copilot_app(self, result): + with ( + patch("ucode.cli.load_state", return_value=MINIMAL_STATE), + patch("ucode.agents.desktop.setup.revert_desktop", return_value="unchanged"), + patch("ucode.cli.revert_mcp_configs", return_value={}), + patch("ucode.cli.claude_agent.revert_managed_settings", return_value="unchanged"), + patch("ucode.cli.codex_agent.revert_managed_config", return_value="unchanged"), + patch("ucode.cli.revert_machine_policy", return_value="unchanged"), + patch("ucode.cli.restore_file", return_value=False), + patch("ucode.cli.revert_legacy_shared_config", return_value=False), + patch("ucode.agents.desktop.setup.revert_copilot_app", return_value=result), + patch("ucode.cli.clear_state"), + ): + outcome = runner.invoke(app, ["revert"]) + + output = " ".join(outcome.output.split()) + assert outcome.exit_code == 0, outcome.output + assert f"GitHub Copilot app: {result}" in output + assert "keep the Databricks models" not in output + @pytest.mark.parametrize("args", [[], ["claude"], ["codex"]]) def test_unknown_routing_config_does_not_block_launch(monkeypatch, args): diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py new file mode 100644 index 000000000..9d741c970 --- /dev/null +++ b/tests/test_copilot_app.py @@ -0,0 +1,607 @@ +"""Registering ug's providers in the GitHub Copilot desktop app, against a fake of its local +WebSocket API (message shapes recorded from app 1.4.2).""" + +from __future__ import annotations + +import http +import json +import os +import threading +from urllib.parse import parse_qs, urlparse + +import pytest +from websockets.sync.server import serve + +from ucode.agents.copilot import WIRE_ANTHROPIC, WIRE_COMPLETIONS, WIRE_RESPONSES, CopilotModel +from ucode.agents.desktop import copilot_app +from ucode.agents.desktop import copilot_app_plugin as plugin +from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER + +WORKSPACE = "https://example.cloud.databricks.com" +CATALOG = [ + CopilotModel("system.ai.claude-sonnet-4-6", WIRE_ANTHROPIC), + CopilotModel("system.ai.gpt-5-2", WIRE_RESPONSES), + CopilotModel("system.ai.glm-5", WIRE_COMPLETIONS), +] + + +def _app_providers(catalog=CATALOG, workspace=WORKSPACE, **kwargs): + return copilot_app.desired_providers(workspace, catalog, **kwargs).app + + +class FakeApp: + """The app's WebSocket API: provider and model CRUD, the healthcheck, its token and origin + checks, and an optional injected error.""" + + def __init__(self, home, version="1.4.2"): + self.version = version + self.token = "per-launch-token" + self.providers: dict[str, dict] = {} + self.models: dict[str, dict] = {} + self.secrets: dict[str, str] = {} + self.received: list[dict] = [] + self.handshakes: list[dict] = [] + self.paths: list[str] = [] + self.busy = 0 + self.busy_kinds: tuple[str, ...] = ("upsert",) + self.fail: str | None = None + self.drop: str | None = None + self.before_reply: dict[str, list[dict]] = {} + self._server = serve(self._handle, "127.0.0.1", 0, process_request=self._check) + port = self._server.socket.getsockname()[1] + threading.Thread(target=self._server.serve_forever, daemon=True).start() + run = home / "run" + run.mkdir(parents=True) + (run / "ws.port").write_text(f"{port}\n{os.getpid()}\n") + (run / "ws.token").write_text(f"{self.token}\n{os.getpid()}\n") + + def _check(self, connection, request): + self.handshakes.append(dict(request.headers.raw_items())) + self.paths.append(request.path) + if request.headers.get("Origin"): + return connection.respond(http.HTTPStatus.FORBIDDEN, "origin") + if parse_qs(urlparse(request.path).query).get("token") != [self.token]: + return connection.respond(http.HTTPStatus.UNAUTHORIZED, "token") + return None + + def _reply(self, message): + kind = message["type"] + if self.fail == kind: + return {"type": "error", "message": "refused", "request_id": message.get("request_id")} + if kind.startswith(self.busy_kinds) and self.busy: + self.busy -= 1 + return {"type": "error", "message": "provider update still in progress"} + if kind == "get_healthcheck": + return {"type": "command_result", "data": {"app": {"version": self.version}}} + if kind == "list_model_providers": + return {"type": "model_providers_list", "providers": list(self.providers.values())} + if kind == "upsert_model_provider": + provider = message["provider"] + if "secret" in message: + self.secrets[provider["id"]] = message["secret"]["value"] + self.providers[provider["id"]] = { + **provider, + "hasSecret": provider["id"] in self.secrets, + } + return {"type": "model_provider_upserted", "provider": self.providers[provider["id"]]} + if kind == "delete_model_provider": + provider_id = message["provider_id"] + self.providers.pop(provider_id) + self.secrets.pop(provider_id, None) + self.models = {k: m for k, m in self.models.items() if m["providerId"] != provider_id} + return {"type": "model_provider_deleted", "provider_id": provider_id} + if kind == "list_provider_models": + return {"type": "provider_models_list", "models": list(self.models.values())} + if kind == "upsert_provider_model": + self.models[message["model"]["id"]] = message["model"] + return {"type": "provider_model_upserted", "model": message["model"]} + if kind == "delete_provider_model": + self.models.pop(message["model_id"]) + return {"type": "provider_model_deleted", "model_id": message["model_id"]} + return {"type": "error", "message": f"unknown variant `{kind}`"} + + def _handle(self, websocket): + websocket.send(json.dumps({"type": "server_hello", "connection_id": 1})) + websocket.send(json.dumps({"type": "keep_awake_changed"})) + for text in websocket: + message = json.loads(text) + self.received.append(message) + if message["type"] == self.drop: + websocket.close() + return + for frame in self.before_reply.pop(message["type"], []): + websocket.send(json.dumps(frame)) + reply = self._reply(message) + if "request_id" in message and "request_id" not in reply: + reply["request_id"] = message["request_id"] + websocket.send(json.dumps(reply)) + + def sent(self, kind): + return [message for message in self.received if message["type"] == kind] + + def stop(self): + self._server.shutdown() + + +@pytest.fixture +def app(tmp_path, monkeypatch): + home = tmp_path / ".copilot" + monkeypatch.setenv("COPILOT_HOME", str(home)) + fake = FakeApp(home) + yield fake + fake.stop() + + +def _by_name(app): + return {provider["name"]: provider for provider in app.providers.values()} + + +class TestDesiredProviders: + def test_the_plugin_gets_the_app_rows_ids(self): + desired = copilot_app.desired_providers(WORKSPACE, CATALOG) + + assert len(desired.app) == len(desired.plugin) == 3 + for app_provider, plugin_provider in zip(desired.app, desired.plugin, strict=True): + assert plugin_provider.id == app_provider.id + assert [ + (model.model_id, model.wire_model, model.name, model.limits, model.vision) + for model in plugin_provider.models + ] == [ + ( + row["modelId"], + row["wireModel"], + row["displayName"], + (row["maxPromptTokens"], row["maxOutputTokens"]) + if "maxPromptTokens" in row + else None, + row["supportsVision"], + ) + for row in app_provider.models + ] + assert app_provider.settings["baseUrl"] == WORKSPACE + plugin_provider.base_path + + def test_the_plugin_config_names_the_app_providers(self): + desired = copilot_app.desired_providers(WORKSPACE, CATALOG) + config = plugin.extension_config( + plugin.PluginConfig(workspace=WORKSPACE, ug_path="/opt/ug", providers=desired.plugin) + ) + + app_ids = {(p.id, row["modelId"]) for p in desired.app for row in p.models} + assert {(m["provider"], m["id"]) for m in config["models"]} == app_ids + assert [p["name"] for p in config["providers"]] == [p.id for p in desired.app] + + def test_the_three_wires(self): + claude, gpt, open_models = copilot_app.desired_providers(WORKSPACE + "/", CATALOG).plugin + + assert (claude.wire, claude.base_path) == ("anthropic", "/ai-gateway/anthropic") + assert (gpt.wire, gpt.base_path) == ("responses", "/ai-gateway/mlflow/v1") + assert (open_models.wire, open_models.base_path) == ("completions", "/ai-gateway/mlflow/v1") + app_claude, app_gpt, app_open = _app_providers(workspace=WORKSPACE + "/") + assert (app_claude.kind, app_claude.settings) == ( + "anthropic", + { + "baseUrl": f"{WORKSPACE}/ai-gateway/anthropic", + "wireApi": "completions", + "authKind": "api_key", + "headersJson": "{}", + }, + ) + assert app_claude.secret == {"kind": "apiKey", "value": copilot_app.PLACEHOLDER_KEY} + assert (app_gpt.kind, app_gpt.settings, app_gpt.secret) == ( + "custom", + { + "baseUrl": f"{WORKSPACE}/ai-gateway/mlflow/v1", + "wireApi": "responses", + "authKind": "none", + "headersJson": "{}", + }, + None, + ) + assert (app_open.kind, app_open.settings["wireApi"], app_open.secret) == ( + "custom", + "completions", + None, + ) + + def test_a_model_provider_service_uses_the_openai_route_and_its_header(self): + desired = copilot_app.desired_providers(WORKSPACE, CATALOG, provider_service="my-mps") + + assert [p.base_path for p in desired.plugin] == [ + "/ai-gateway/anthropic", + "/ai-gateway/openai/v1", + "/ai-gateway/openai/v1", + ] + assert desired.headers == {MODEL_PROVIDER_SERVICE_HEADER: "my-mps"} + assert [m.name for p in desired.plugin for m in p.models] == [m.id for m in CATALOG] + assert copilot_app.desired_providers(WORKSPACE, CATALOG).headers == {} + + def test_only_claude_rows_use_copilot_catalog_ids(self): + catalog = [ + *CATALOG, + CopilotModel("system.ai.kimi-k3", WIRE_COMPLETIONS), + CopilotModel("gpt-5.2", WIRE_RESPONSES), + ] + for desired in ( + copilot_app.desired_providers(WORKSPACE, catalog), + copilot_app.desired_providers(WORKSPACE, catalog, provider_service="my-mps"), + ): + plugin_ids = {p.wire: [m.model_id for m in p.models] for p in desired.plugin} + assert plugin_ids == { + "anthropic": ["claude-sonnet-4.6"], + "responses": ["system.ai.gpt-5-2", "databricks.gpt-5.2"], + "completions": ["system.ai.glm-5", "system.ai.kimi-k3"], + } + app_ids = [row["modelId"] for p in desired.app for row in p.models] + assert app_ids == [model_id for ids in plugin_ids.values() for model_id in ids] + + def test_a_wire_without_models_gets_no_provider(self): + desired = copilot_app.desired_providers(WORKSPACE, CATALOG[1:2]) + + assert [p.name for p in desired.app] == ["Databricks GPT"] + assert [p.wire for p in desired.plugin] == ["responses"] + + def test_two_models_with_one_copilot_id_keep_the_first(self): + # ug gives Haiku 5+ a Sonnet id so Copilot sends the thinking it requires. + (claude,) = copilot_app.desired_providers( + WORKSPACE, + [ + CopilotModel("system.ai.claude-sonnet-5-5", WIRE_ANTHROPIC), + CopilotModel("system.ai.claude-haiku-5-5", WIRE_ANTHROPIC), + ], + ).plugin + + assert [(m.model_id, m.wire_model) for m in claude.models] == [ + ("claude-sonnet-5.5", "system.ai.claude-sonnet-5-5") + ] + + def test_a_claude_service_gets_the_id_of_the_model_behind_it(self, capsys): + (claude,) = copilot_app.desired_providers( + WORKSPACE, + [ + CopilotModel( + "main.team.team-coder", + WIRE_ANTHROPIC, + served_model="system.ai.claude-sonnet-4-6", + ) + ], + ).plugin + + assert [(m.model_id, m.wire_model) for m in claude.models] == [ + ("claude-sonnet-4.6", "main.team.team-coder") + ] + assert capsys.readouterr().out == "" + + def test_a_claude_route_model_without_a_claude_id_is_skipped_with_a_warning(self, capsys): + desired = copilot_app.desired_providers( + WORKSPACE, [CopilotModel("main.team.mystery", WIRE_ANTHROPIC), *CATALOG[1:]] + ) + + assert [p.wire for p in desired.plugin] == ["responses", "completions"] + assert "Skipping main.team.mystery" in " ".join(capsys.readouterr().out.split()) + + def test_a_duplicate_is_skipped_with_a_warning(self, capsys): + (claude,) = copilot_app.desired_providers( + WORKSPACE, + [ + CopilotModel("us.anthropic.claude-sonnet-4-6", WIRE_ANTHROPIC), + CopilotModel("eu.anthropic.claude-sonnet-4-6", WIRE_ANTHROPIC), + ], + provider_service="my-mps", + ).plugin + + assert [m.wire_model for m in claude.models] == ["us.anthropic.claude-sonnet-4-6"] + out = " ".join(capsys.readouterr().out.split()) + assert "Skipping eu.anthropic.claude-sonnet-4-6" in out + assert "us.anthropic.claude-sonnet-4-6" in out + + def test_state_providers_use_the_vscode_catalog(self, monkeypatch): + from ucode.agents import vscode + + calls = [] + + def models(state, provider, location, *, agent): + calls.append((provider, location, agent)) + return CATALOG + + monkeypatch.setattr(vscode, "copilot_models", models) + + desired = copilot_app.state_providers({"workspace": WORKSPACE}, provider="my-mps") + + assert calls == [("my-mps", None, "the GitHub Copilot app")] + assert desired == copilot_app.desired_providers( + WORKSPACE, CATALOG, provider_service="my-mps" + ) + + +def seed(app, providers): + """Puts ``providers`` in the fake app the way the plugin writes them.""" + for rows in (provider.rows() for provider in providers): + app.providers[rows.provider["id"]] = { + **rows.provider, + "hasSecret": rows.secret is not None, + } + for model in rows.models: + app.models[model["id"]] = dict(model) + + +class TestPluginRows: + def test_the_plugin_writes_the_app_rows(self): + desired = copilot_app.desired_providers(WORKSPACE, CATALOG) + rows = desired.plugin_app + + assert [r.provider["id"] for r in rows] == [p.id for p in desired.plugin] + for provider_rows, provider in zip(rows, desired.app, strict=True): + assert provider_rows.provider == { + "id": provider.id, + "name": provider.name, + "kind": provider.kind, + "settings": provider.settings, + } + assert provider_rows.secret == provider.secret + assert [ + (m["id"], m["providerId"], m["modelId"], m["wireModel"]) + for m in provider_rows.models + ] == [ + ( + provider.model_row_id(row["modelId"]), + provider.id, + row["modelId"], + row["wireModel"], + ) + for row in provider.models + ] + assert set(copilot_app.OWNED_PROVIDER_IDS) >= {r.provider["id"] for r in rows} + assert len(copilot_app.OWNED_PROVIDER_IDS) == 3 + + def test_the_plugin_rows_and_the_picker_rows_agree(self): + desired = copilot_app.desired_providers( + WORKSPACE, + [ + *CATALOG, + CopilotModel("system.ai.gpt-6", WIRE_RESPONSES, listed_limits=(922_000, 128_000)), + ], + ) + for plugin_provider, provider_rows in zip(desired.plugin, desired.plugin_app, strict=True): + for model, row in zip(plugin_provider.models, provider_rows.models, strict=True): + assert (model.model_id, model.wire_model, model.name) == ( + row["modelId"], + row["wireModel"], + row["displayName"], + ) + assert model.vision == row["supportsVision"] + assert model.limits == ( + (row["maxPromptTokens"], row["maxOutputTokens"]) + if "maxPromptTokens" in row + else None + ) + gpt = {m.model_id: m for m in desired.plugin[1].models} + assert gpt["system.ai.gpt-6"].limits == (794_000, 128_000) + + def test_only_chat_completions_models_turn_off_reasoning_effort(self): + claude, gpt, open_models = copilot_app.desired_providers(WORKSPACE, CATALOG).plugin + + assert [m.reasoning_effort for m in open_models.models] == [False] + assert [m.reasoning_effort for m in (*claude.models, *gpt.models)] == [None, None] + assert [m.vision for m in claude.models] == [True] + + +def test_unregister_deletes_only_ugs_providers(app): + app.providers["mine"] = {"id": "mine", "name": "Databricks Claude", "kind": "anthropic"} + seed(app, _app_providers()) + assert len(app.providers) == 4 + + assert copilot_app.unregister() is True + assert len(app.sent("delete_model_provider")) == 3 + assert list(app.providers) == ["mine"] + assert app.models == {} + assert copilot_app.unregister() is False + assert all("Origin" not in headers for headers in app.handshakes) + + +class TestStatus: + def test_registered_outdated_and_not_registered(self, app): + providers = _app_providers() + assert copilot_app.status(providers) == "not_registered" + + seed(app, providers) + assert copilot_app.status(providers) == "registered" + assert copilot_app.status(_app_providers(CATALOG[:1])) == "outdated" + + newer = _app_providers([*CATALOG, CopilotModel("system.ai.gpt-6", WIRE_RESPONSES)]) + assert copilot_app.status(newer) == "outdated" + + copilot_app.unregister() + assert copilot_app.status(providers) == "not_registered" + + def test_another_workspace_or_changed_rows_are_outdated(self, app): + providers = _app_providers() + seed(app, providers) + + assert copilot_app.status( + _app_providers(workspace="https://other.cloud.databricks.com") + ) == ("outdated") + renamed = [ + copilot_app.AppProvider( + p.key, + p.name, + p.kind, + p.settings, + p.secret, + tuple({**row, "displayName": "Renamed"} for row in p.models), + ) + for p in providers + ] + assert copilot_app.status(renamed) == "outdated" + assert copilot_app.status(providers) == "registered" + + def test_a_row_the_user_added_counts_as_outdated(self, app): + providers = _app_providers() + seed(app, providers) + claude = providers[0].id + app.models["user-row"] = {"id": "user-row", "providerId": claude, "modelId": "x"} + + assert copilot_app.status(providers) == "outdated" + + def test_a_listed_model_without_an_id_is_ignored(self, app): + providers = _app_providers() + seed(app, providers) + app.models["odd"] = {"providerId": providers[0].id, "modelId": "claude-odd"} + + assert copilot_app.status(providers) == "registered" + + def test_a_closed_app_is_not_running(self, tmp_path, monkeypatch): + monkeypatch.setenv("COPILOT_HOME", str(tmp_path / ".copilot")) + + assert copilot_app.status(_app_providers()) == "not_running" + + def test_an_app_that_quits_mid_check_is_not_running(self, app): + app.drop = "list_model_providers" + + assert copilot_app.status(_app_providers()) == "not_running" + + +def test_retries_while_the_app_applies_another_change(app): + seed(app, _app_providers()) + app.busy = 2 + app.busy_kinds = ("delete_model_provider",) + + assert copilot_app.unregister() is True + assert app.providers == {} + + +def test_a_refusal_stops_the_unregister(app): + seed(app, _app_providers()) + app.fail = "delete_model_provider" + + with pytest.raises(copilot_app.CopilotAppError, match="refused delete_model_provider"): + copilot_app.unregister() + + +def _healthcheck(version, **fields): + return {"type": "command_result", "data": {"app": {"version": version}}, **fields} + + +def test_every_request_carries_a_request_id(app): + seed(app, _app_providers()) + copilot_app.unregister() + + ids = [message.get("request_id") for message in app.received] + assert all(ids) + assert len(set(ids)) == len(ids) + + +@pytest.mark.parametrize( + "stray", + [_healthcheck("0.0.1", request_id="ug-someone-else"), _healthcheck("0.0.1")], + ids=["another-request-id", "no-request-id"], +) +def test_a_reply_not_for_this_request_is_skipped(app, capsys, stray): + app.before_reply["get_healthcheck"] = [stray] + seed(app, _app_providers()) + + assert copilot_app.unregister() is True + assert "hasn't been verified" not in capsys.readouterr().out + + +def test_an_error_for_another_request_is_skipped(app): + app.before_reply["get_healthcheck"] = [ + {"type": "error", "message": "boom", "request_id": "ug-someone-else"} + ] + seed(app, _app_providers()) + + assert copilot_app.unregister() is True + + +def test_an_error_without_a_request_id_answers_the_pending_request(app): + app.before_reply["list_model_providers"] = [ + {"type": "error", "message": "Invalid message format"} + ] + + with pytest.raises(copilot_app.CopilotAppError, match="Invalid message format"): + copilot_app.unregister() + + +def test_an_app_without_the_healthcheck_is_an_unknown_version(app, capsys): + app.fail = "get_healthcheck" + seed(app, _app_providers()) + + assert copilot_app.unregister() is True + assert "GitHub Copilot app (unknown version) hasn't been verified" in " ".join( + capsys.readouterr().out.split() + ) + + +def test_an_app_that_quits_mid_unregister_is_not_running(app): + seed(app, _app_providers()) + app.drop = "delete_model_provider" + + with pytest.raises(copilot_app.CopilotAppNotRunning): + copilot_app.unregister() + + +def test_an_unknown_app_version_warns_and_continues(app, capsys): + app.version = "9.0.0" + seed(app, _app_providers()) + + assert copilot_app.unregister() is True + assert "GitHub Copilot app 9.0.0 hasn't been verified with ug" in " ".join( + capsys.readouterr().out.split() + ) + + +def test_a_verified_app_version_is_quiet(app, capsys): + copilot_app.unregister() + + assert "hasn't been verified" not in capsys.readouterr().out + + +def test_a_closed_app_is_not_running(tmp_path, monkeypatch): + home = tmp_path / ".copilot" + monkeypatch.setenv("COPILOT_HOME", str(home)) + + with pytest.raises(copilot_app.CopilotAppNotRunning): + copilot_app.unregister() + + # The app leaves its run files behind when it exits. + (home / "run").mkdir(parents=True) + (home / "run" / "ws.port").write_text("1\n4242\n") + (home / "run" / "ws.token").write_text("stale\n4242\n") + with pytest.raises(copilot_app.CopilotAppNotRunning): + copilot_app.unregister() + + +def test_a_port_file_of_an_exited_app_is_not_running(app, monkeypatch): + monkeypatch.setattr(copilot_app, "process_running", lambda pid: False) + + assert copilot_app.status(_app_providers()) == "not_running" + with pytest.raises(copilot_app.CopilotAppNotRunning): + copilot_app.unregister() + assert app.handshakes == [] + + +def test_the_token_is_url_encoded(app): + app.token = "a+b&c=d" + (copilot_app.copilot_home() / "run" / "ws.token").write_text(f"{app.token}\n4242\n") + + assert copilot_app.unregister() is False + + +def test_a_rejected_token_is_an_app_error(app): + (copilot_app.copilot_home() / "run" / "ws.token").write_text("wrong\n4242\n") + + with pytest.raises(copilot_app.CopilotAppError, match="refused ug"): + copilot_app.unregister() + + +def test_status_waits_only_briefly_for_a_hung_app(app, monkeypatch): + waits = [] + real_next = copilot_app._AppConnection._next + + def next_message(self, deadline): + waits.append(deadline - copilot_app.time.monotonic()) + return real_next(self, deadline) + + monkeypatch.setattr(copilot_app._AppConnection, "_next", next_message) + copilot_app.status(copilot_app.desired_providers(WORKSPACE, CATALOG).app) + + assert waits and max(waits) <= copilot_app._STATUS_TIMEOUT_S 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 diff --git a/tests/test_copilot_app_restart.py b/tests/test_copilot_app_restart.py new file mode 100644 index 000000000..816bfb370 --- /dev/null +++ b/tests/test_copilot_app_restart.py @@ -0,0 +1,310 @@ +"""``ug copilot-app-restart``: the detached helper ug's GitHub Copilot app plugin starts to restart +the app once no chat is running a turn.""" + +from __future__ import annotations + +import json +import time +from pathlib import Path + +import pytest + +from ucode.agents.desktop import copilot_app +from ucode.agents.desktop import copilot_app_restart as restart + +TARGET = restart.Target("linux", 4242, "/usr/bin/github", Path("/home/u")) + + +class Clock: + def __init__(self) -> None: + self.t = 0.0 + + def __call__(self) -> float: + return self.t + + def sleep(self, seconds: float) -> None: + self.t += seconds + + +class FakeEvents: + """The app's session events: ``script`` maps a time to the events sent then.""" + + def __init__(self, clock: Clock, script: dict[float, list] | None = None, closes_at=None): + self.clock = clock + self.script = dict(script or {}) + self.closes_at = closes_at + self.closed = False + + def next_message(self, timeout: float): + if self.closes_at is not None and self.clock.t >= self.closes_at: + raise copilot_app.CopilotAppNotRunning("closed") + due = sorted(t for t in self.script if t <= self.clock.t) + if due: + session, kind = self.script.pop(due[0]) + return {"type": "session_event", "session_id": session, "event": {"type": kind}} + self.clock.t += timeout + return None + + def close(self) -> None: + self.closed = True + + +class FakeSystem: + def __init__(self, clock: Clock, *, exits=True, launches=(True,), osascript=True) -> None: + self.clock = clock + self.calls: list = [] + self.exits = exits + self.stopped = False + self.launches = list(launches) + self.osascript = osascript + + def deps(self) -> dict: + return { + "alive": lambda pid: not (self.exits and self.stopped), + "terminate": self.terminate, + "run": self.run, + "launch": self.launch, + "sleep": self.clock.sleep, + "clock": self.clock, + "env": {"HOME": "/home/u", "DISPLAY": ":0", "GH_TOKEN": "secret", "COPILOT_X": "1"}, + } + + def terminate(self, pid): + self.calls.append(("terminate", pid, self.clock.t)) + self.stopped = True + + def run(self, argv): + self.calls.append(("run", argv)) + if argv[0] == "/usr/bin/osascript" and not self.osascript: + return False + self.stopped = True + return True + + def launch(self, argv, env): + self.calls.append(("launch", argv, dict(env))) + return self.launches.pop(0) if self.launches else False + + +def _restart(script=None, *, closes_at=None, **system_kwargs): + clock = Clock() + events = FakeEvents(clock, script, closes_at) + system = FakeSystem(clock, **system_kwargs) + result = restart.restart_when_idle(TARGET, watch=lambda home: events, **system.deps()) + assert events.closed + return result, system, clock + + +class TestTarget: + def test_parses_the_extensions_target(self): + text = json.dumps( + {"platform": "win32", "pid": 7, "app": "C:\\a\\github.exe", "home": "/h", "log": "/l"} + ) + + assert restart.parse_target(text) == restart.Target( + "win32", 7, "C:\\a\\github.exe", Path("/h"), Path("/l") + ) + + @pytest.mark.parametrize( + "text", + [ + "not json", + "[]", + json.dumps({"platform": "aix", "pid": 7, "app": "a", "home": "/h"}), + json.dumps({"platform": "linux", "pid": True, "app": "a", "home": "/h"}), + json.dumps({"platform": "linux", "pid": 1, "app": "a", "home": "/h"}), + json.dumps({"platform": "linux", "pid": 7, "app": "", "home": "/h"}), + json.dumps({"platform": "linux", "pid": 7, "app": "a"}), + ], + ) + def test_rejects_anything_else(self, text): + assert restart.parse_target(text) is None + + +class TestWaitForIdle: + def test_an_idle_app_restarts_after_the_quiet_period(self): + result, system, _ = _restart() + + assert result == "relaunched" + assert system.calls[0] == ("terminate", 4242, restart.QUIET_S) + + def test_a_turn_in_any_chat_delays_the_restart_until_that_chat_is_idle(self): + result, system, _ = _restart( + {5.0: ("ui-chat", "assistant.turn_start"), 90.0: ("ui-chat", "session.idle")} + ) + + assert result == "relaunched" + killed_at = system.calls[0][2] + assert 90.0 + restart.QUIET_S <= killed_at <= 90.0 + restart.QUIET_S + 1.0 + + def test_a_chat_that_never_goes_idle_gives_up(self): + result, system, clock = _restart({1.0: ("chat", "user.message")}) + + assert result == "busy" + assert clock.t == pytest.approx(restart.IDLE_TIMEOUT_S, abs=1.0) + assert system.calls == [] + + def test_events_that_keep_coming_hold_the_restart_off(self): + script = {float(t): ("chat", "assistant.streaming_delta") for t in range(0, 400, 5)} + + result, system, _ = _restart(script) + + assert result == "busy" + assert system.calls == [] + + def test_an_app_that_closes_while_waiting_isnt_relaunched(self): + result, system, _ = _restart({1.0: ("chat", "user.message")}, closes_at=3.0) + + assert result == "stopped" + assert system.calls == [] + + @pytest.mark.parametrize( + ("error", "outcome"), + [ + (copilot_app.CopilotAppNotRunning("closed"), "not_running"), + (copilot_app.CopilotAppError("refused"), "no_session_events"), + ], + ) + def test_without_the_apps_session_events_it_gives_up(self, error, outcome): + system = FakeSystem(Clock()) + + def watch(home): + raise error + + assert restart.restart_when_idle(TARGET, watch=watch, **system.deps()) == outcome + assert system.calls == [] + + +class TestRestartApp: + def test_linux_terminates_then_relaunches_with_the_session_environment(self): + system = FakeSystem(Clock()) + + assert restart.restart_app(TARGET, **system.deps()) == "relaunched" + + assert system.calls[1] == ( + "launch", + ["/usr/bin/github"], + {"HOME": "/home/u", "DISPLAY": ":0"}, + ) + + def test_a_failed_relaunch_is_retried_once(self): + system = FakeSystem(Clock(), launches=(False, True)) + assert restart.restart_app(TARGET, **system.deps()) == "relaunched" + + system = FakeSystem(Clock(), launches=(False, False)) + assert restart.restart_app(TARGET, **system.deps()) == "relaunch_failed" + assert [call[0] for call in system.calls] == ["terminate", "launch", "launch"] + + def test_an_app_that_doesnt_exit_is_left_running(self): + clock = Clock() + system = FakeSystem(clock, exits=False) + + assert restart.restart_app(TARGET, **system.deps()) == "still_running" + + assert [call[0] for call in system.calls] == ["terminate"] + assert clock.t <= restart.EXIT_TIMEOUT_S + 1 + + def test_macos_quits_the_app_then_opens_it_by_bundle_id(self): + target = restart.Target("darwin", 4242, "com.github.githubapp", Path("/h")) + system = FakeSystem(Clock()) + + assert restart.restart_app(target, **system.deps()) == "relaunched" + + assert system.calls[0] == ( + "run", + ["/usr/bin/osascript", "-e", 'quit app id "com.github.githubapp"'], + ) + assert system.calls[1][1] == ["/usr/bin/open", "-b", "com.github.githubapp"] + + def test_macos_falls_back_to_sigterm_when_osascript_fails(self): + target = restart.Target("darwin", 4242, "com.github.githubapp", Path("/h")) + system = FakeSystem(Clock(), osascript=False) + + restart.restart_app(target, **system.deps()) + + assert system.calls[1][0] == "terminate" + + def test_windows_closes_the_window_without_f_and_leaves_a_tray_app_running(self): + target = restart.Target("win32", 4242, "C:\\GitHub\\github.exe", Path("/h")) + system = FakeSystem(Clock(), exits=False) + + assert restart.restart_app(target, **system.deps()) == "still_running" + + assert system.calls == [("run", ["taskkill", "/PID", "4242"])] + + +def test_session_env_keeps_the_users_session_and_drops_the_runtimes(): + env = { + "HOME": "/h", + "PATH": "/bin", + "LC_ALL": "C", + "XDG_RUNTIME_DIR": "/run/u", + "https_proxy": "http://p", + "GH_TOKEN": "t", + "COPILOT_SDK_AUTH_TOKEN": "x", + "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "https://w", + } + + assert sorted(restart.session_env(env)) == [ + "HOME", + "LC_ALL", + "PATH", + "XDG_RUNTIME_DIR", + "https_proxy", + ] + + +def test_main_logs_the_outcome_and_never_raises(tmp_path, monkeypatch): + log = tmp_path / "app-restart.log" + target = {"platform": "linux", "pid": 4242, "app": "/a", "home": str(tmp_path), "log": str(log)} + outcomes = iter(["relaunched"]) + + def boom(target): + raise RuntimeError("x") + + monkeypatch.setattr(restart, "restart_when_idle", lambda target: next(outcomes)) + restart.main(json.dumps(target)) + monkeypatch.setattr(restart, "restart_when_idle", boom) + restart.main(json.dumps(target)) + restart.main("not json") + + lines = log.read_text().splitlines() + assert [line.split(" ", 1)[1] for line in lines] == ["relaunched", "failed: x"] + + +def test_without_a_running_app_the_real_watch_gives_up(tmp_path): + target = restart.Target("linux", 4242, "/a", tmp_path) + + assert restart.restart_when_idle(target) == "not_running" + + +def test_the_cli_command_hands_the_target_to_the_helper(monkeypatch): + from typer.testing import CliRunner + + from ucode.cli import app + + seen = [] + monkeypatch.setattr(restart, "main", seen.append) + + result = CliRunner().invoke(app, ["copilot-app-restart", '{"platform": "linux"}']) + + assert result.exit_code == 0, result.output + assert seen == ['{"platform": "linux"}'] + + +def test_the_real_watch_reads_the_apps_session_channels(tmp_path): + from urllib.parse import parse_qs, urlparse + + from tests.test_copilot_app import FakeApp + + home = tmp_path / ".copilot" + app = FakeApp(home) + try: + system = FakeSystem(Clock()) + deps = {**system.deps(), "clock": time.monotonic} + target = restart.Target("linux", 4242, "/usr/bin/github", home) + + assert restart.restart_when_idle(target, quiet_s=0.3, **deps) == "relaunched" + finally: + app.stop() + + assert parse_qs(urlparse(app.paths[0]).query)["session_channels"] == ["true"] diff --git a/tests/test_copilot_app_setup.py b/tests/test_copilot_app_setup.py new file mode 100644 index 000000000..65616d003 --- /dev/null +++ b/tests/test_copilot_app_setup.py @@ -0,0 +1,694 @@ +"""``ug configure``'s GitHub Copilot desktop app setup: ug's plugin, and ug's providers in a running +app, against a fake of the app's WebSocket API.""" + +from __future__ import annotations + +import json +import os +import shutil +from pathlib import Path +from unittest.mock import Mock + +import pytest + +from tests.test_copilot_app import CATALOG, FakeApp, seed +from ucode import agents as agents_mod +from ucode import databricks, state +from ucode.agents.desktop import copilot_app +from ucode.agents.desktop import copilot_app_plugin as plugin +from ucode.agents.desktop import setup as desktop_setup +from ucode.agents.vscode import copilot_models as real_copilot_models +from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER +from ucode.workspace import SELECTOR_HEADER_NAMES, selector_headers + +WS = "https://example.cloud.databricks.com" +STATE = {"workspace": WS, "profile": "dev"} + + +@pytest.fixture(autouse=True) +def _app_home(tmp_path, monkeypatch): + """The app's settings and ug's plugin live under ``tmp_path``; the conftest already keeps + every test away from a real app, and nothing here can start one.""" + monkeypatch.setenv("UG_EXPERIMENTAL_COPILOT_APP", "1") + monkeypatch.setattr(desktop_setup, "copilot_app_installed", lambda: True) + monkeypatch.setattr("ucode.agents.vscode.copilot_models", lambda *args, **kwargs: CATALOG) + monkeypatch.setattr("ucode.agents.copilot.uses_seat_relay", lambda *args, **kwargs: False) + monkeypatch.setattr("ucode.agents.copilot.agent_version", lambda binary: "1.0.91") + monkeypatch.setattr("ucode.databricks.ug_binary", lambda: "/opt/ug/bin/ug") + monkeypatch.setattr( + "ucode.databricks.databricks_cli_path", lambda: "/opt/databricks/bin/databricks" + ) + monkeypatch.setattr(plugin, "UNLOAD_WAIT_S", 0) + popen = Mock() + monkeypatch.setattr("ucode.os_compatibility.subprocess_cross_os.popen", popen) + yield + popen.assert_not_called() + + +@pytest.fixture +def app(tmp_path): + fake = FakeApp(tmp_path / ".copilot") + yield fake + fake.stop() + + +def _settings_path() -> Path: + return plugin.copilot_settings_path() + + +def _settings() -> dict: + return json.loads(_settings_path().read_text()) + + +def _extension_config() -> dict: + path = plugin.plugin_root() / "plugins" / plugin.PLUGIN / "extensions" / plugin.EXTENSION + return json.loads((path / "config.json").read_text()) + + +def _out(capsys) -> str: + return " ".join(capsys.readouterr().out.split()) + + +def _register(fake, app_state=STATE, **kwargs): + seed(fake, copilot_app.state_providers(app_state, **kwargs).app) + + +class TestConfigure: + def test_installs_the_plugin_with_the_app_rows(self, capsys): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert plugin.status() == "installed" + assert _settings()["enabledPlugins"] == {plugin.PLUGIN_KEY: True} + config = _extension_config() + desired = copilot_app.state_providers(STATE) + assert [p["name"] for p in config["providers"]] == [p.id for p in desired.app] + assert [rows["provider"]["id"] for rows in config["app"]["providers"]] == [ + p.id for p in desired.app + ] + assert config["app"]["ownedProviderIds"] == list(copilot_app.OWNED_PROVIDER_IDS) + assert config["databricksPath"] == "/opt/databricks/bin/databricks" + assert config["tokenCommand"][:2] == ["/opt/ug/bin/ug", "auth-token"] + assert "/opt/databricks/bin" in config["pathDirs"] + out = _out(capsys) + assert "Open (or restart) the GitHub Copilot app to load the Databricks models." in out + assert "Settings -> Model providers" not in out + + def test_never_talks_to_the_running_app(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert app.received == [] + + def test_the_plugin_sends_ugs_copilot_headers(self, monkeypatch): + monkeypatch.setattr( + desktop_setup, + "copilot_app_state", + lambda state: {**state, "copilot_http_headers": {"X-Team": "data"}}, + ) + monkeypatch.setattr( + "ucode.workspace.workspace_headers", lambda url=None: {"X-Databricks-Org-Id": "42"} + ) + + desktop_setup.configure_copilot_app(STATE, provider="main.default.mps", location=None) + + for provider in _extension_config()["providers"]: + headers = provider["headers"] + assert not [name for name in headers if name.casefold() == "user-agent"] + assert headers["x-databricks-use-coding-agent-mode"] == "true" + assert headers["X-Team"] == "data" + assert headers[MODEL_PROVIDER_SERVICE_HEADER] == "main.default.mps" + assert headers["X-Databricks-Org-Id"] == "42" + + def test_the_plugin_selects_the_saved_workspace_on_a_shared_host(self): + desktop_setup.configure_copilot_app( + {**STATE, "workspace_id": "1234567890"}, provider=None, location=None + ) + + for provider in _extension_config()["providers"]: + selected = { + name: provider["headers"][name] + for name in SELECTOR_HEADER_NAMES + if name in provider["headers"] + } + assert selected == selector_headers("1234567890") + + def test_an_admin_header_cant_select_another_workspace(self, monkeypatch): + monkeypatch.setattr( + desktop_setup, + "copilot_app_state", + lambda state: { + **state, + "copilot_http_headers": {SELECTOR_HEADER_NAMES[1]: "999"}, + }, + ) + + desktop_setup.configure_copilot_app( + {**STATE, "workspace_id": "1234567890"}, provider=None, location=None + ) + + for provider in _extension_config()["providers"]: + assert provider["headers"][SELECTOR_HEADER_NAMES[1]] == "1234567890" + + def test_records_the_databricks_cli_for_helpers_without_it_on_path(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert state.recorded_databricks_cli() == "/opt/databricks/bin/databricks" + + @pytest.mark.parametrize("content", ["{", "[]"]) + def test_unreadable_settings_are_left_alone(self, content, capsys): + _settings_path().parent.mkdir(parents=True) + _settings_path().write_text(content) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert _settings_path().read_text() == content + assert not plugin.plugin_root().exists() + assert "Skipped the GitHub Copilot app" in _out(capsys) + + def test_warns_about_telemetry_keys_that_disable_the_plugin(self, capsys): + _settings_path().parent.mkdir(parents=True) + _settings_path().write_text(json.dumps({"telemetry": {"filePath": "/tmp/spans"}})) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert plugin.status() == "installed" + out = _out(capsys) + assert "ignores its whole settings file" in out + assert "telemetry.filePath" in out + + def test_no_models_skips_the_app(self, monkeypatch, capsys): + def no_models(*args, **kwargs): + raise RuntimeError("No models for the GitHub Copilot app.") + + monkeypatch.setattr("ucode.agents.vscode.copilot_models", no_models) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert not plugin.plugin_root().exists() + assert "Skipped the GitHub Copilot app: No models" in _out(capsys) + + +class TestManagedDefaultModel: + def _name(self, model_id): + rows = [row for p in copilot_app.state_providers(STATE).app for row in p.models] + return next(row["displayName"] for row in rows if row["wireModel"] == model_id) + + def test_a_managed_default_in_the_app_gets_a_hint_only(self, capsys): + app_state = {**STATE, "copilot_default_model": "gpt-5-2"} + + desktop_setup.configure_copilot_app(app_state, provider=None, location=None) + + name = self._name("system.ai.gpt-5-2") + assert ( + f"To start new chats on {name}, pick it in the GitHub Copilot app; Settings -> " + "Sessions -> Default model makes it stick." + ) in _out(capsys) + assert "defaultModel" not in json.dumps(_extension_config()) + assert "defaultModel" not in _settings_path().read_text() + + def test_no_hint_without_a_managed_default_the_app_has(self, capsys): + app_state = {**STATE, "copilot_default_model": "kimi-k3"} + + desktop_setup.configure_copilot_app(app_state, provider=None, location=None) + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert "To start new chats on" not in _out(capsys) + + +class TestSeatRelayProvider: + NOTE = ( + "copilot-seats uses a seat subscription, which the GitHub Copilot app doesn't support " + "yet; use `ug copilot` or VS Code for it." + ) + + @pytest.fixture(autouse=True) + def _relay(self, monkeypatch): + self.checked = [] + + def uses_seat_relay(state, provider, *, tool): + self.checked.append((provider, tool)) + return provider == "copilot-seats" + + monkeypatch.setattr("ucode.agents.copilot.uses_seat_relay", uses_seat_relay) + + def test_configure_skips_the_app_with_a_note(self, capsys): + desktop_setup.configure_copilot_app(STATE, provider="copilot-seats", location=None) + + assert not plugin.plugin_root().exists() + assert self.NOTE in _out(capsys) + assert self.checked == [("copilot-seats", "vscode")] + + def test_another_provider_service_is_set_up(self, capsys): + desktop_setup.configure_copilot_app(STATE, provider="team-models", location=None) + + assert plugin.status() == "installed" + assert "seat subscription" not in _out(capsys) + + def test_status_notes_the_seat_subscription(self): + result = desktop_setup.copilot_app_status(STATE, provider="copilot-seats", location=None) + + assert result is not None + assert result.row == "plugin not installed, models skipped (seat subscription)" + assert result.warnings == (self.NOTE,) + + def test_a_failed_lookup_reads_as_unknown(self, monkeypatch): + def lookup_fails(*args, **kwargs): + raise RuntimeError("Model provider service 'copilot-seats' was not found.") + + monkeypatch.setattr("ucode.agents.copilot.uses_seat_relay", lookup_fails) + + result = desktop_setup.copilot_app_status(STATE, provider="copilot-seats", location=None) + + assert result is not None + assert result.row == "plugin not installed, models unknown (no models to compare)" + + +class TestConfigureCopilotApp: + def test_sets_up_the_installed_app(self): + agents_mod.configure_copilot_app(STATE, provider=None, parent_schema=None) + + assert plugin.status() == "installed" + + def test_opt_out_and_a_missing_app_skip_it(self, monkeypatch): + agents_mod.request_copilot_app_with_copilot(False) + try: + agents_mod.configure_copilot_app(STATE, provider=None, parent_schema=None) + finally: + agents_mod.request_copilot_app_with_copilot(True) + monkeypatch.setattr(desktop_setup, "copilot_app_installed", lambda: False) + agents_mod.configure_copilot_app(STATE, provider=None, parent_schema=None) + + assert not plugin.plugin_root().exists() + assert not _settings_path().exists() + + def test_without_the_experiment_flag_nothing_changes(self, monkeypatch, app): + monkeypatch.delenv("UG_EXPERIMENTAL_COPILOT_APP") + + agents_mod.configure_copilot_app(STATE, provider=None, parent_schema=None) + agents_mod.configure_copilot_app( + {**STATE, "copilot_app_integration": False}, provider=None, parent_schema=None + ) + + assert not plugin.plugin_root().exists() + assert app.received == [] + assert desktop_setup.copilot_app_status(STATE, provider=None, location=None) is None + + def test_managed_off_switch_removes_the_plugin_and_providers(self, app, capsys): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + + agents_mod.configure_copilot_app( + {**STATE, "copilot_app_integration": False}, provider=None, parent_schema=None + ) + + assert plugin.status() == "not_installed" + assert app.providers == {} + assert "turns off the GitHub Copilot app" in _out(capsys) + + def test_managed_off_switch_reads_the_managed_config(self, monkeypatch): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + monkeypatch.setattr( + "ucode.managed_config.load_managed_state", + lambda workspace: {"enabled_agents": {"copilot": {"app_integration_enabled": False}}}, + ) + + agents_mod.configure_copilot_app(STATE, provider=None, parent_schema=None) + + assert plugin.status() == "removal_pending" + + +class TestRemove: + def test_unregisters_the_running_app_and_removes_the_plugin(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + + assert desktop_setup.revert_copilot_app() == "removed" + assert app.providers == {} + assert plugin.status() == "not_installed" + assert not _settings_path().exists() + + def test_a_closed_app_leaves_the_removal_to_the_plugins_next_start(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + result = desktop_setup.revert_copilot_app() + + assert result == desktop_setup.COPILOT_APP_REMOVAL_PENDING + assert "the next time the GitHub Copilot app starts" in result + assert plugin.status() == "removal_pending" + assert _settings()["enabledPlugins"] == {plugin.PLUGIN_KEY: True} + assert _extension_config()["remove"] is True + + def test_a_later_run_deletes_what_the_plugin_left_behind(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + desktop_setup.revert_copilot_app() + _settings_path().write_text(json.dumps({"model": "kept"})) + + result = desktop_setup.copilot_app_status(STATE, provider=None, location=None) + + assert result is not None + assert result.row == "plugin not installed, models unknown (app closed)" + assert not plugin.plugin_root().exists() + assert _settings() == {"model": "kept"} + + def test_a_pending_removal_is_kept_while_its_keys_remain(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + desktop_setup.revert_copilot_app() + + result = desktop_setup.copilot_app_status(STATE, provider=None, location=None) + + assert result is not None + assert result.row == desktop_setup.COPILOT_APP_REMOVAL_PENDING + assert plugin.plugin_root().exists() + + def test_status_after_revert_shows_the_pending_removal_then_cleans_up(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + desktop_setup.revert_copilot_app() + + assert desktop_setup.copilot_app_unconfigured_status() == ( + desktop_setup.COPILOT_APP_REMOVAL_PENDING + ) + _settings_path().write_text("{}") + + assert desktop_setup.copilot_app_unconfigured_status() is None + assert not plugin.plugin_root().exists() + assert not _settings_path().exists() + + def test_reverting_with_the_app_open_after_a_pending_removal_finishes_it(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + desktop_setup.revert_copilot_app() + _register(app) + + assert desktop_setup.revert_copilot_app() == "removed" + assert app.providers == {} + assert not plugin.plugin_root().exists() + assert not _settings_path().exists() + + def test_configure_again_cancels_a_pending_removal(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + desktop_setup.revert_copilot_app() + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert plugin.status() == "installed" + assert "remove" not in _extension_config() + + def test_a_second_revert_after_opening_the_app_removes_the_rows(self, app): + _register(app) + + assert desktop_setup.revert_copilot_app() == "removed" + assert app.providers == {} + + def test_nothing_set_up(self): + assert desktop_setup.revert_copilot_app() == "not set up" + + +class TestStatus: + def _status(self, app_state=STATE): + return desktop_setup.copilot_app_status(app_state, provider=None, location=None) + + def test_installed_and_registered(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + + result = self._status() + + assert result is not None + assert result.row == "plugin installed, models registered" + assert result.warnings == () + + def test_registered_rows_without_the_plugin_fail(self, app): + _register(app) + + result = self._status() + + assert result is not None + assert result.row == "plugin not installed, models registered" + assert "their turns fail without it" in result.warnings[0] + + def test_a_disabled_plugin(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + settings = _settings() + settings["enabledPlugins"][plugin.PLUGIN_KEY] = False + _settings_path().write_text(json.dumps(settings)) + + result = self._status() + + assert result is not None + assert result.row == "plugin disabled, models registered" + assert "is turned off" in result.warnings[0] + + def test_invalid_settings(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + _settings_path().write_text("{") + + result = self._status() + + assert result is not None + assert result.row == "plugin invalid settings, models registered" + assert "isn't valid JSON" in result.warnings[0] + + def test_telemetry_keys_are_reported(self): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + settings = _settings() + settings["telemetry"] = {"enabled": True, "endpoint": "http://localhost:4318"} + _settings_path().write_text(json.dumps(settings)) + + result = self._status() + + assert result is not None + assert result.row == "plugin installed, ignored, models unknown (app closed)" + assert "telemetry.enabled, telemetry.endpoint" in result.warnings[0] + assert "managed settings" in result.warnings[0] + + def test_another_workspace_is_out_of_date(self, app): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app, {**STATE, "workspace": "https://other.cloud.databricks.com"}) + + result = self._status() + + assert result is not None + assert result.row == "plugin installed, models out of date" + assert "don't match" in result.warnings[0] + + def test_status_prints_no_version_warning(self, app, capsys): + app.version = "9.9.9" + _register(app) + capsys.readouterr() + + self._status() + + assert "hasn't been verified" not in capsys.readouterr().out + + def test_managed_off(self): + result = self._status({**STATE, "copilot_app_integration": False}) + + assert result is not None + assert result.row == "off (workspace-managed)" + + def test_not_installed_and_never_set_up(self, monkeypatch): + monkeypatch.setattr(desktop_setup, "copilot_app_installed", lambda: False) + + assert self._status() is None + + +def test_ignored_settings_reason_names_only_rejected_telemetry_keys(tmp_path): + path = tmp_path / "settings.json" + path.write_text(json.dumps({"telemetry": {"exporter": "x"}, "model": "y"})) + assert desktop_setup.copilot_settings_ignored_reason(path) is None + path.write_text(json.dumps({"telemetry": {"protocol": "http", "resourceAttributes": {}}})) + reason = desktop_setup.copilot_settings_ignored_reason(path) + assert reason is not None + assert "telemetry.protocol, telemetry.resourceAttributes" in reason + + +def test_a_recorded_databricks_cli_is_found_off_path(tmp_path, monkeypatch): + # Windows finds executables by PATHEXT, not an execute bit. + cli = tmp_path / "bin" / ("databricks.exe" if os.name == "nt" else "databricks") + cli.parent.mkdir() + cli.write_text("#!/bin/sh\necho 'Databricks CLI v0.280.0'\n") + cli.chmod(0o755) + monkeypatch.setenv("PATH", str(tmp_path / "empty")) + monkeypatch.setattr("sys.platform", "linux") + databricks.clear_databricks_cli_cache() + try: + assert databricks._iter_databricks_executables() == [] + state.record_databricks_cli(str(cli)) + found = databricks._iter_databricks_executables() + assert [os.path.normcase(path) for path in found] == [os.path.normcase(str(cli))] + finally: + databricks.clear_databricks_cli_cache() + + +def test_configure_no_copilot_app_opts_out_for_the_run(monkeypatch): + from typer.testing import CliRunner + + from ucode.cli import app + + monkeypatch.setattr("ucode.cli.install_databricks_cli", lambda: None) + monkeypatch.setattr("ucode.cli.configure_workspace_command", lambda **kwargs: 0) + + result = CliRunner().invoke( + app, ["configure", "--agents", "copilot", "--workspace", WS, "--no-copilot-app"] + ) + + assert result.exit_code == 0, result.output + assert agents_mod._copilot_app_with_copilot is False + + +def test_a_missing_catalog_names_the_app(): + with pytest.raises(RuntimeError, match="models for the GitHub Copilot app"): + real_copilot_models({"workspace": WS}, agent="the GitHub Copilot app") + + +def test_a_dry_run_changes_nothing(monkeypatch, app, capsys): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + before = _settings_path().read_text() + config = _extension_config() + monkeypatch.setattr("ucode.config_io._dry_run", True) + received = len(app.received) + + agents_mod.configure_copilot_app( + {**STATE, "workspace": "https://other.cloud.databricks.com"}, + provider=None, + parent_schema=None, + ) + agents_mod.configure_copilot_app( + {**STATE, "copilot_app_integration": False}, provider=None, parent_schema=None + ) + assert desktop_setup.revert_copilot_app() == "unchanged (dry run)" + + assert _settings_path().read_text() == before + assert _extension_config() == config + assert len(app.received) == received + assert "Dry run: the GitHub Copilot app isn't changed." in _out(capsys) + + +class TestManagedModelSources: + WILDCARD = "main.allowed.*" + + def _managed_state(self, monkeypatch, expansions): + from ucode.constants import MANAGED_MODEL_SOURCE_EXPANSIONS_STATE_KEY + + monkeypatch.setattr( + desktop_setup, + "copilot_app_state", + lambda state: { + **state, + "copilot_static_models": [self.WILDCARD, "system.ai.gpt-5-2"], + MANAGED_MODEL_SOURCE_EXPANSIONS_STATE_KEY: expansions, + }, + ) + seen = [] + monkeypatch.setattr( + "ucode.agents.vscode.copilot_models", + lambda state, *args, **kwargs: seen.append(state["copilot_static_models"]) or CATALOG, + ) + return seen + + def test_the_app_gets_the_admins_expanded_list(self, monkeypatch): + seen = self._managed_state( + monkeypatch, {"copilot": {self.WILDCARD: ["main.allowed.claude-x"]}} + ) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert seen == [["main.allowed.claude-x", "system.ai.gpt-5-2"]] + assert plugin.status() == "installed" + + def test_a_source_never_expanded_skips_the_app(self, monkeypatch, capsys): + seen = self._managed_state(monkeypatch, {}) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert seen == [] + assert "main.allowed.* isn't expanded yet" in _out(capsys) + assert plugin.status() == "not_installed" + + +class TestQuiescedRevert: + def _extension(self) -> Path: + return plugin.plugin_root() / "plugins" / plugin.PLUGIN / "extensions" / plugin.EXTENSION + + def test_marks_the_plugin_for_removal_and_holds_the_sync_lock_while_unregistering( + self, app, monkeypatch + ): + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + real = copilot_app.unregister + seen = {} + + def unregister(): + seen["remove"] = _extension_config().get("remove") + seen["lock"] = (self._extension() / "app-sync.lock").read_text() + return real() + + monkeypatch.setattr(copilot_app, "unregister", unregister) + + assert desktop_setup.revert_copilot_app() == "removed" + + assert seen["remove"] is True + assert seen["lock"].startswith("ug-") + assert app.providers == {} + assert not plugin.plugin_root().exists() + + @pytest.mark.skipif(shutil.which("node") is None, reason="node is not installed") + def test_a_sync_racing_the_revert_cant_write_the_rows_back(self, app, monkeypatch, tmp_path): + import subprocess + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + _register(app) + real = copilot_app.unregister + results = [] + rows = (self._extension() / "app-rows.mjs").as_uri() + script = ( + "import { readFileSync } from 'node:fs';" + f"import {{ syncOnce }} from {json.dumps(rows)};" + "const [config, home, stateDir] = process.argv.slice(1);" + "console.log(await syncOnce(JSON.parse(readFileSync(config, 'utf8')), { home, stateDir }));" + ) + + def sync(): + # The extension's own sync, started by a chat while the revert is under way. + done = subprocess.run( + [ + shutil.which("node"), + "--input-type=module", + "-e", + script, + str(self._extension() / "config.json"), + str(tmp_path / ".copilot"), + str(self._extension()), + ], + capture_output=True, + text=True, + timeout=30, + check=True, + ) + results.append(done.stdout.strip()) + + def unregister(): + sync() + removed = real() + sync() + return removed + + monkeypatch.setattr(copilot_app, "unregister", unregister) + + assert desktop_setup.revert_copilot_app() == "removed" + + assert set(results) <= {"busy", "removing"} + assert app.providers == {} + assert app.models == {} + + +def test_a_failed_plugin_write_only_warns(monkeypatch, capsys): + def fail(config): + raise RuntimeError("Failed to write file: config.json") + + monkeypatch.setattr(plugin, "install", fail) + + desktop_setup.configure_copilot_app(STATE, provider=None, location=None) + + assert "Could not install ug's plugin in the GitHub Copilot app" in _out(capsys) diff --git a/tests/test_managed_config.py b/tests/test_managed_config.py index 40964b88b..f861793e9 100644 --- a/tests/test_managed_config.py +++ b/tests/test_managed_config.py @@ -231,6 +231,19 @@ def test_copilot_vscode_integration_is_carried(self): copilot = normalize_managed_config(raw)["enabled_agents"]["copilot"] assert copilot["vscode_integration_enabled"] is enabled + def test_copilot_app_integration_is_carried(self): + for enabled in (True, False): + raw = { + "enabled_agents": [ + { + "agent": "CODING_AGENT_GITHUB_COPILOT", + "config": {"app_integration": {"enabled": enabled}}, + } + ] + } + copilot = normalize_managed_config(raw)["enabled_agents"]["copilot"] + assert copilot["app_integration_enabled"] is enabled + def test_unset_vscode_integration_is_omitted(self): raw = {"enabled_agents": [{"agent": "CODING_AGENT_GITHUB_COPILOT", "config": {}}]} copilot = normalize_managed_config(raw)["enabled_agents"]["copilot"] diff --git a/tests/test_managed_resolve.py b/tests/test_managed_resolve.py index 9c3a1237e..46b3fd879 100644 --- a/tests/test_managed_resolve.py +++ b/tests/test_managed_resolve.py @@ -104,6 +104,13 @@ def test_enabled_or_unset_vscode_adds_no_override(self): managed = {"enabled_agents": {"copilot": entry}} assert "copilot_vscode_integration" not in managed_state_overrides(managed, "copilot") + def test_disabled_copilot_app_overrides_copilot_state(self): + managed = {"enabled_agents": {"copilot": {"app_integration_enabled": False}}} + assert managed_state_overrides(managed, "copilot")["copilot_app_integration"] is False + for entry in ({"app_integration_enabled": True}, {}): + managed = {"enabled_agents": {"copilot": entry}} + assert "copilot_app_integration" not in managed_state_overrides(managed, "copilot") + def test_vscode_switch_is_copilot_only(self): managed = {"enabled_agents": {"claude": {"vscode_integration_enabled": False}}} assert "copilot_vscode_integration" not in managed_state_overrides(managed, "claude") diff --git a/tests/test_managed_setup.py b/tests/test_managed_setup.py index b4cd21692..87eeb71da 100644 --- a/tests/test_managed_setup.py +++ b/tests/test_managed_setup.py @@ -83,6 +83,7 @@ def _full_manifest() -> dict: }, "copilot": { "vscode_integration_enabled": False, + "app_integration_enabled": False, "model_config": { "default_model": "system.ai.claude-opus-4-8", "model_services": ["system.ai.claude-opus-4-8"], diff --git a/tests/test_mcp_proxy_cli.py b/tests/test_mcp_proxy_cli.py index ea4eb0926..7b57f7b3c 100644 --- a/tests/test_mcp_proxy_cli.py +++ b/tests/test_mcp_proxy_cli.py @@ -108,3 +108,25 @@ def test_proxy_missing_workspace_reports_error(monkeypatch): assert result.exit_code == 1 assert "No workspace configured" in result.stderr assert result.stdout == "" + + +def test_proxy_drops_the_app_runtimes_trace_exporter_env(monkeypatch): + import os + + from ucode import mcp_proxy_runtime + + for name in ("OTEL_EXPORTER_OTLP_ENDPOINT", "COPILOT_OTEL_ENABLED", "UG_KEEP"): + monkeypatch.setenv(name, "1") + seen = [] + monkeypatch.setattr( + mcp_proxy_runtime, "load_proxy_state", lambda **_: {"workspace": "https://saved"} + ) + monkeypatch.setattr( + mcp_proxy_runtime, "serve", lambda *args, **kwargs: seen.append(dict(os.environ)) + ) + + mcp_proxy_runtime.run_proxy("https://endpoint") + + assert "OTEL_EXPORTER_OTLP_ENDPOINT" not in seen[0] + assert "COPILOT_OTEL_ENABLED" not in seen[0] + assert seen[0]["UG_KEEP"] == "1" diff --git a/tests/test_system_cross_os.py b/tests/test_system_cross_os.py index bf4514c85..a5071938c 100644 --- a/tests/test_system_cross_os.py +++ b/tests/test_system_cross_os.py @@ -2,6 +2,9 @@ from __future__ import annotations +import os +import subprocess +import sys from pathlib import Path from types import SimpleNamespace @@ -98,3 +101,11 @@ def test_user_profiles_dir(monkeypatch, platform, system_drive, expected): monkeypatch.setenv("SystemDrive", system_drive) assert system_cross_os.user_profiles_dir() == expected + + +def test_process_running(): + assert system_cross_os.process_running(os.getpid()) + child = subprocess.Popen([sys.executable, "-c", "pass"]) + child.wait() + assert not system_cross_os.process_running(child.pid) + assert not system_cross_os.process_running(0)