From 4c96710075d6ee86bfd704dc3dff05fef284d436 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:04:57 +0000 Subject: [PATCH 01/26] copilot: add a Databricks plugin module for the GitHub Copilot app An unwired module that writes a ug-owned local plugin marketplace and turns it on in Copilot's settings.json. The plugin's extension registers ug's provider and model ids and serves them straight from the AI Gateway, minting a token with `ug auth-token` per request (cached until shortly before expiry). Co-authored-by: Isaac --- src/ucode/copilot_app_plugin/__init__.py | 262 +++++++++++++++++++ src/ucode/copilot_app_plugin/extension.mjs | 13 + src/ucode/copilot_app_plugin/ug-plugin.mjs | 90 +++++++ tests/copilot_app_plugin.test.mjs | 122 +++++++++ tests/test_copilot_app_plugin.py | 276 +++++++++++++++++++++ 5 files changed, 763 insertions(+) create mode 100644 src/ucode/copilot_app_plugin/__init__.py create mode 100644 src/ucode/copilot_app_plugin/extension.mjs create mode 100644 src/ucode/copilot_app_plugin/ug-plugin.mjs create mode 100644 tests/copilot_app_plugin.test.mjs create mode 100644 tests/test_copilot_app_plugin.py diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/copilot_app_plugin/__init__.py new file mode 100644 index 000000000..a21865bc3 --- /dev/null +++ b/src/ucode/copilot_app_plugin/__init__.py @@ -0,0 +1,262 @@ +"""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 serves ug's models straight from the AI Gateway: it registers the same provider +and model ids ug adds to the app's model picker, and mints a fresh Databricks token for every +request with ``ug auth-token``. +""" + +from __future__ import annotations + +import json +import os +import shutil +from collections.abc import Mapping +from dataclasses import dataclass, field +from pathlib import Path +from typing import Literal + +from ucode.config_io import atomic_write_json + +MARKETPLACE = "unity-gateway" +PLUGIN = "databricks" +PLUGIN_KEY = f"{PLUGIN}@{MARKETPLACE}" +EXTENSION = "databricks" +VERSION = "0.1.0" + +Wire = Literal["anthropic", "responses", "completions"] +Status = Literal["installed", "disabled", "not_installed", "invalid_settings"] + +_MARKETPLACES_KEY = "extraKnownMarketplaces" +_ENABLED_KEY = "enabledPlugins" +_SOURCE_FILES = ("extension.mjs", "ug-plugin.mjs") + + +class SettingsError(RuntimeError): + """Copilot's ``settings.json`` can't be safely rewritten.""" + + +@dataclass(frozen=True) +class PluginModel: + model_id: str + wire_model: str + name: str + + +@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 PluginConfig: + workspace: str + ug_path: str + providers: tuple[PluginProvider, ...] + headers: Mapping[str, str] = field(default_factory=dict) + profile: str | 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, ...] = () + + +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) -> Path: + return root / "plugins" / PLUGIN + + +def _extension_dir(root: Path) -> Path: + return _plugin_dir(root) / "extensions" / EXTENSION + + +def _token_command(config: PluginConfig) -> list[str]: + command = [config.ug_path, "auth-token", "--host", config.workspace] + if config.profile: + command += ["--profile", config.profile] + return command + + +def _login_hint(config: PluginConfig) -> str: + login = f"databricks auth login --host {config.workspace}" + if config.profile: + login += f" --profile {config.profile}" + return f"Couldn't get a Databricks token for {config.workspace}. Run `{login}`, 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 extension_config(config: PluginConfig) -> dict: + """The JSON the extension reads at start.""" + return { + "providers": [_provider_entry(provider, config) for provider in config.providers], + "models": [ + { + "id": model.model_id, + "modelId": model.model_id, + "provider": provider.id, + "wireModel": model.wire_model, + "name": model.name, + } + for provider in config.providers + for model in provider.models + ], + "tokenCommand": _token_command(config), + "pathDirs": list(config.path_dirs), + "loginHint": _login_hint(config), + } + + +def write_plugin(config: PluginConfig, root: Path | None = None) -> Path: + """Write the marketplace, the plugin and its extension under ``root``; return ``root``.""" + root = root or plugin_root() + 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)) + 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 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) + _object(settings, _MARKETPLACES_KEY, path)[MARKETPLACE] = { + "source": {"source": "directory", "path": str(root)} + } + _object(settings, _ENABLED_KEY, path)[PLUGIN_KEY] = True + atomic_write_json(path, settings) + + +def install( + config: PluginConfig, root: Path | None = None, settings_path: Path | None = None +) -> Path: + path = settings_path or copilot_settings_path() + read_settings(path) + root = write_plugin(config, root) + enable(root, path) + return root + + +def remove(root: Path | None = None, settings_path: Path | None = None) -> bool: + """Remove ug's settings keys and plugin directory. Returns whether anything was removed.""" + root = root or plugin_root() + path = settings_path or copilot_settings_path() + settings = read_settings(path) + changed = False + for key, entry in ((_MARKETPLACES_KEY, MARKETPLACE), (_ENABLED_KEY, PLUGIN_KEY)): + value = settings.get(key) + if isinstance(value, dict) and entry in value: + del value[entry] + if not value: + del settings[key] + changed = True + if changed: + atomic_write_json(path, settings) + if root.exists(): + shutil.rmtree(root) + changed = True + return changed + + +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" + marketplaces = settings.get(_MARKETPLACES_KEY) + marketplace = marketplaces.get(MARKETPLACE) if isinstance(marketplaces, dict) else None + source = marketplace.get("source") if isinstance(marketplace, dict) else None + if not ( + isinstance(source, dict) + and source.get("path") == str(root) + and (_extension_dir(root) / "extension.mjs").exists() + ): + return "not_installed" + enabled = settings.get(_ENABLED_KEY) + if isinstance(enabled, dict) and enabled.get(PLUGIN_KEY) is True: + return "installed" + return "disabled" diff --git a/src/ucode/copilot_app_plugin/extension.mjs b/src/ucode/copilot_app_plugin/extension.mjs new file mode 100644 index 000000000..d6f338780 --- /dev/null +++ b/src/ucode/copilot_app_plugin/extension.mjs @@ -0,0 +1,13 @@ +import { joinSession } from "@github/copilot-sdk/extension"; + +import { buildProviders, createTokenSource, loadConfig, withLoginHint } from "./ug-plugin.mjs"; + +const config = loadConfig(new URL("./config.json", import.meta.url)); +let session; +const bearerTokenProvider = withLoginHint(createTokenSource(config), () => + session?.log(config.loginHint, { level: "error" }), +); +session = await joinSession({ + providers: buildProviders(config, bearerTokenProvider), + models: config.models, +}); diff --git a/src/ucode/copilot_app_plugin/ug-plugin.mjs b/src/ucode/copilot_app_plugin/ug-plugin.mjs new file mode 100644 index 000000000..2c053d2ce --- /dev/null +++ b/src/ucode/copilot_app_plugin/ug-plugin.mjs @@ -0,0 +1,90 @@ +import { execFile } from "node:child_process"; +import { readFileSync } from "node:fs"; +import { delimiter } from "node:path"; + +export const REFRESH_MARGIN_MS = 120_000; +export const DEFAULT_TOKEN_TTL_MS = 300_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; + } +} + +export function childEnv(config, env = process.env) { + const path = [...(config.pathDirs || []), env.PATH].filter(Boolean).join(delimiter); + return { ...env, PATH: 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(); + reject(new Error(`ug auth-token failed: ${detail}`)); + return; + } + const token = String(stdout).trim(); + if (token) resolve(token); + else reject(new Error("ug auth-token printed no token")); + }); + }); +} + +// One mint at a time: concurrent requests share it, and the token is reused until shortly +// before it expires. +export function createTokenSource(config, { run = runCommand, now = Date.now } = {}) { + let cached = null; + let pending = null; + return function token() { + if (cached && now() < cached.refreshAt) return Promise.resolve(cached.value); + if (!pending) { + pending = run(config.tokenCommand, childEnv(config)) + .then((value) => { + const expiry = jwtExpiryMs(value); + const refreshAt = expiry ? expiry - REFRESH_MARGIN_MS : now() + DEFAULT_TOKEN_TTL_MS; + cached = { value, refreshAt }; + return value; + }) + .finally(() => { + pending = null; + }); + } + return pending; + }; +} + +// The app retries a failing token provider several times, so the sign-in hint is shown once per +// run of failures. +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 = true; + await Promise.resolve() + .then(() => showHint()) + .catch(() => {}); + } + throw error; + } + }; +} + +export function buildProviders(config, bearerTokenProvider) { + return config.providers.map((provider) => ({ ...provider, bearerTokenProvider })); +} diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs new file mode 100644 index 000000000..847881b3a --- /dev/null +++ b/tests/copilot_app_plugin.test.mjs @@ -0,0 +1,122 @@ +import assert from "node:assert/strict"; +import { delimiter } from "node:path"; +import { test } from "node:test"; + +import { + DEFAULT_TOKEN_TTL_MS, + REFRESH_MARGIN_MS, + buildProviders, + childEnv, + createTokenSource, + jwtExpiryMs, + withLoginHint, +} from "../src/ucode/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 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("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++, + ); + 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("buildProviders attaches the token provider to every provider", () => { + const provide = async () => "t"; + const [provider] = buildProviders(config, provide); + assert.equal(provider.bearerTokenProvider, provide); + assert.equal(provider.baseUrl, "https://ws/ai-gateway/anthropic"); + assert.equal(config.providers[0].bearerTokenProvider, undefined); +}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py new file mode 100644 index 000000000..c69aee0bb --- /dev/null +++ b/tests/test_copilot_app_plugin.py @@ -0,0 +1,276 @@ +import json +import shutil +import subprocess +from pathlib import Path + +import pytest + +from ucode 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) + 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_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"] == "unity-gateway" + assert marketplace["plugins"][0]["source"] == "./plugins/databricks" + manifest = json.loads( + (root / "plugins" / "databricks" / ".plugin" / "plugin.json").read_text() + ) + assert manifest["name"] == "databricks" + extension = root / "plugins" / "databricks" / "extensions" / "databricks" + assert {p.name for p in extension.iterdir()} == { + "extension.mjs", + "ug-plugin.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" / "databricks" / "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"}}, + "unity-gateway": {"source": {"source": "directory", "path": str(root)}}, + } + assert settings["enabledPlugins"] == {"x@other": False, "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_drops_empty_keys(self, home): + plugin.install(_config()) + plugin.remove() + assert json.loads(_settings(home).read_text()) == {} + + 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() + + +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"]["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"]) + def test_syntax(self, name): + subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) + + def test_node_unit_tests(self): + result = subprocess.run( + [NODE, "--test", str(Path(__file__).with_name("copilot_app_plugin.test.mjs"))], + capture_output=True, + text=True, + ) + assert result.returncode == 0, result.stdout + result.stderr From 0f5d62a54fd7e280529f7fbc8f1da08baf5196a0 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:29:43 +0000 Subject: [PATCH 02/26] copilot: give the plugin's models the picker rows' limits and vision The session uses the extension's model rows instead of the app's, so they repeat the rows' token limits and, for Claude, vision support. Co-authored-by: Isaac --- src/ucode/copilot_app_plugin/__init__.py | 32 ++++++++++++++++++------ tests/test_copilot_app_plugin.py | 15 +++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/copilot_app_plugin/__init__.py index a21865bc3..6803554d4 100644 --- a/src/ucode/copilot_app_plugin/__init__.py +++ b/src/ucode/copilot_app_plugin/__init__.py @@ -38,9 +38,15 @@ class SettingsError(RuntimeError): @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 @dataclass(frozen=True) @@ -110,18 +116,30 @@ def _provider_entry(provider: PluginProvider, config: PluginConfig) -> dict: 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 + entry["maxOutputTokens"] = output + entry["maxContextWindowTokens"] = prompt + output + if model.vision: + entry["capabilities"] = {"supports": {"vision": True}} + return entry + + 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": [ - { - "id": model.model_id, - "modelId": model.model_id, - "provider": provider.id, - "wireModel": model.wire_model, - "name": model.name, - } + _model_entry(model, provider) for provider in config.providers for model in provider.models ], diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index c69aee0bb..7a165e238 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -91,6 +91,21 @@ def test_models_match_the_picker_rows(self): } 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_token_command_and_hint_name_the_workspace_and_profile(self): config = plugin.extension_config(_config()) assert config["tokenCommand"] == [ From ab7ef35d226da7b6d8909d74f9533f5e50c3800b Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:33:41 +0000 Subject: [PATCH 03/26] copilot: unload the plugin before deleting it, and refresh a rejected token remove() now drops the settings keys, waits for the app to unload the extension, then deletes the directory; deleting it first left running extensions serving open chats. The extension drops its cached token when a model call fails with a 401 and mints the next one with --force-refresh (untested live). Co-authored-by: Isaac --- src/ucode/copilot_app_plugin/__init__.py | 17 +++++++++--- src/ucode/copilot_app_plugin/extension.mjs | 14 ++++++++-- src/ucode/copilot_app_plugin/ug-plugin.mjs | 24 ++++++++++++++--- tests/copilot_app_plugin.test.mjs | 30 ++++++++++++++++++++++ tests/test_copilot_app_plugin.py | 20 +++++++++++++++ 5 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/copilot_app_plugin/__init__.py index 6803554d4..e7ee71ab2 100644 --- a/src/ucode/copilot_app_plugin/__init__.py +++ b/src/ucode/copilot_app_plugin/__init__.py @@ -11,7 +11,8 @@ import json import os import shutil -from collections.abc import Mapping +import time +from collections.abc import Callable, Mapping from dataclasses import dataclass, field from pathlib import Path from typing import Literal @@ -30,6 +31,9 @@ _MARKETPLACES_KEY = "extraKnownMarketplaces" _ENABLED_KEY = "enabledPlugins" _SOURCE_FILES = ("extension.mjs", "ug-plugin.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 class SettingsError(RuntimeError): @@ -238,8 +242,13 @@ def install( return root -def remove(root: Path | None = None, settings_path: Path | None = None) -> bool: - """Remove ug's settings keys and plugin directory. Returns whether anything was removed.""" +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. Returns whether anything was removed.""" root = root or plugin_root() path = settings_path or copilot_settings_path() settings = read_settings(path) @@ -254,6 +263,8 @@ def remove(root: Path | None = None, settings_path: Path | None = None) -> bool: if changed: atomic_write_json(path, settings) if root.exists(): + if changed: + (wait or time.sleep)(UNLOAD_WAIT_S) shutil.rmtree(root) changed = True return changed diff --git a/src/ucode/copilot_app_plugin/extension.mjs b/src/ucode/copilot_app_plugin/extension.mjs index d6f338780..1cf55a2e5 100644 --- a/src/ucode/copilot_app_plugin/extension.mjs +++ b/src/ucode/copilot_app_plugin/extension.mjs @@ -1,13 +1,23 @@ import { joinSession } from "@github/copilot-sdk/extension"; -import { buildProviders, createTokenSource, loadConfig, withLoginHint } from "./ug-plugin.mjs"; +import { + buildProviders, + createTokenSource, + isAuthRejection, + loadConfig, + withLoginHint, +} from "./ug-plugin.mjs"; const config = loadConfig(new URL("./config.json", import.meta.url)); let session; -const bearerTokenProvider = withLoginHint(createTokenSource(config), () => +const tokenSource = createTokenSource(config); +const bearerTokenProvider = withLoginHint(tokenSource, () => session?.log(config.loginHint, { level: "error" }), ); session = await joinSession({ providers: buildProviders(config, bearerTokenProvider), models: config.models, }); +session.on((event) => { + if (isAuthRejection(event)) tokenSource.invalidate(); +}); diff --git a/src/ucode/copilot_app_plugin/ug-plugin.mjs b/src/ucode/copilot_app_plugin/ug-plugin.mjs index 2c053d2ce..e86ce2bb3 100644 --- a/src/ucode/copilot_app_plugin/ug-plugin.mjs +++ b/src/ucode/copilot_app_plugin/ug-plugin.mjs @@ -41,15 +41,21 @@ export function runCommand(argv, env) { }); } +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. +// before it expires. After invalidate(), the next mint asks for a new token rather than the +// Databricks CLI's cached one. export function createTokenSource(config, { run = runCommand, now = Date.now } = {}) { let cached = null; let pending = null; - return function token() { + let forceRefresh = false; + function token() { if (cached && now() < cached.refreshAt) return Promise.resolve(cached.value); if (!pending) { - pending = run(config.tokenCommand, childEnv(config)) + const argv = forceRefresh ? [...config.tokenCommand, FORCE_REFRESH_FLAG] : config.tokenCommand; + forceRefresh = false; + pending = run(argv, childEnv(config)) .then((value) => { const expiry = jwtExpiryMs(value); const refreshAt = expiry ? expiry - REFRESH_MARGIN_MS : now() + DEFAULT_TOKEN_TTL_MS; @@ -61,7 +67,19 @@ export function createTokenSource(config, { run = runCommand, now = Date.now } = }); } return pending; + } + token.invalidate = () => { + cached = null; + forceRefresh = true; }; + return token; +} + +export function isAuthRejection(event) { + return ( + (event?.type === "session.error" || event?.type === "model.call_failure") && + event.data?.statusCode === 401 + ); } // The app retries a failing token provider several times, so the sign-in hint is shown once per diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index 847881b3a..595226d0d 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -4,10 +4,12 @@ import { test } from "node:test"; import { DEFAULT_TOKEN_TTL_MS, + FORCE_REFRESH_FLAG, REFRESH_MARGIN_MS, buildProviders, childEnv, createTokenSource, + isAuthRejection, jwtExpiryMs, withLoginHint, } from "../src/ucode/copilot_app_plugin/ug-plugin.mjs"; @@ -120,3 +122,31 @@ test("buildProviders attaches the token provider to every provider", () => { 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); +}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 7a165e238..31eb15cf2 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -53,6 +53,7 @@ def _config(**overrides) -> plugin.PluginConfig: @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 @@ -241,6 +242,25 @@ def test_remove_leaves_no_ug_state(self, home): assert not root.exists() assert plugin.remove() is False + def test_remove_drops_the_keys_before_the_directory(self, home): + 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, {}, 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_drops_empty_keys(self, home): plugin.install(_config()) plugin.remove() From c173e573b3a6b92ee1fde7738a867256e5805782 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:47:24 +0000 Subject: [PATCH 04/26] copilot: the plugin writes ug's rows to the app's model picker The extension now registers ug's providers and model rows over the app's local WebSocket API when it starts, from rows in its config, and deletes ug's providers and rows that the config no longer has. It syncs only when the rows or the app's launch token changed, one extension process at a time, and logs a failure without affecting the models it serves. Model rows also turn off reasoning_effort where asked (Chat Completions models), and never advertise an output limit below the 32K the runtime asks for. The Databricks CLI's directory goes on the token command's PATH. Co-authored-by: Isaac --- src/ucode/copilot_app_plugin/__init__.py | 55 +++- src/ucode/copilot_app_plugin/app-rows.mjs | 232 +++++++++++++++ src/ucode/copilot_app_plugin/extension.mjs | 10 + src/ucode/copilot_app_plugin/ug-plugin.mjs | 5 +- tests/copilot_app_plugin.test.mjs | 5 + tests/copilot_app_rows.test.mjs | 328 +++++++++++++++++++++ tests/test_copilot_app_plugin.py | 57 +++- 7 files changed, 679 insertions(+), 13 deletions(-) create mode 100644 src/ucode/copilot_app_plugin/app-rows.mjs create mode 100644 tests/copilot_app_rows.test.mjs diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/copilot_app_plugin/__init__.py index e7ee71ab2..3c37eb80a 100644 --- a/src/ucode/copilot_app_plugin/__init__.py +++ b/src/ucode/copilot_app_plugin/__init__.py @@ -1,9 +1,9 @@ """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 serves ug's models straight from the AI Gateway: it registers the same provider -and model ids ug adds to the app's model picker, and mints a fresh Databricks token for every -request with ``ug auth-token``. +plugin's extension writes ug's providers and model rows to the app's model picker over the app's +local WebSocket API, serves them straight from the AI Gateway under the same ids, and mints a fresh +Databricks token for every request with ``ug auth-token``. """ from __future__ import annotations @@ -15,7 +15,7 @@ from collections.abc import Callable, Mapping from dataclasses import dataclass, field from pathlib import Path -from typing import Literal +from typing import Any, Literal from ucode.config_io import atomic_write_json @@ -30,10 +30,12 @@ _MARKETPLACES_KEY = "extraKnownMarketplaces" _ENABLED_KEY = "enabledPlugins" -_SOURCE_FILES = ("extension.mjs", "ug-plugin.mjs") +_SOURCE_FILES = ("extension.mjs", "ug-plugin.mjs", "app-rows.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): @@ -51,6 +53,8 @@ class PluginModel: # (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) @@ -64,6 +68,16 @@ class PluginProvider: 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 @@ -74,6 +88,12 @@ class PluginConfig: # 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: @@ -131,13 +151,27 @@ def _model_entry(model: PluginModel, provider: PluginProvider) -> dict: if model.limits: prompt, output = model.limits entry["maxPromptTokens"] = prompt - entry["maxOutputTokens"] = output - entry["maxContextWindowTokens"] = prompt + output + if output >= MIN_OUTPUT_TOKENS: + entry["maxOutputTokens"] = output + entry["maxContextWindowTokens"] = prompt + output + supports: dict = {} if model.vision: - entry["capabilities"] = {"supports": {"vision": True}} + 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 { @@ -149,7 +183,12 @@ def extension_config(config: PluginConfig) -> dict: ], "tokenCommand": _token_command(config), "pathDirs": list(config.path_dirs), + "databricksPath": config.databricks_path, "loginHint": _login_hint(config), + "app": { + "providers": [_app_entry(rows) for rows in config.app_providers], + "ownedProviderIds": list(config.owned_provider_ids), + }, } diff --git a/src/ucode/copilot_app_plugin/app-rows.mjs b/src/ucode/copilot_app_plugin/app-rows.mjs new file mode 100644 index 000000000..fc7ec222b --- /dev/null +++ b/src/ucode/copilot_app_plugin/app-rows.mjs @@ -0,0 +1,232 @@ +import { createHash, randomUUID } from "node:crypto"; +import { closeSync, openSync, 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 function copilotHome(env = process.env) { + return env.COPILOT_HOME || join(homedir(), ".copilot"); +} + +// Each run file holds the value, then the app's pid. +function runValue(home, name) { + return readFileSync(join(home, "run", name), "utf8").trim().split(/\s+/)[0] || null; +} + +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, + }); +} + +// 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. +export async function syncRows(app, rows) { + 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."); + const existing = new Map(providers.filter((p) => p && typeof p.id === "string").map((p) => [p.id, p])); + const wanted = new Set(rows.providers.map((entry) => entry.provider.id)); + for (const id of rows.ownedProviderIds) { + if (existing.has(id) && !wanted.has(id)) await deleteProvider(app, id); + } + 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; + await app.request(message, "model_provider_upserted"); + const wantedModels = new Set(models.map((model) => model.id)); + for (const model of await listModels(app, provider.id)) { + if (!wantedModels.has(model.id)) { + await app.request({ type: "delete_provider_model", model_id: model.id }, "provider_model_deleted", { + model_id: model.id, + }); + } + } + for (const model of models) { + await app.request({ type: "upsert_provider_model", model }, "provider_model_upserted"); + } + } +} + +function tryLock(path, now) { + try { + closeSync(openSync(path, "wx")); + return true; + } catch (error) { + if (error.code !== "EEXIST") throw error; + } + try { + if (now() - statSync(path).mtimeMs < LOCK_STALE_MS) return false; + unlinkSync(path); + closeSync(openSync(path, "wx")); + return true; + } catch { + return false; + } +} + +function readStamp(path) { + try { + return JSON.parse(readFileSync(path, "utf8")).digest; + } catch { + return null; + } +} + +// 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. +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 digest = createHash("sha256").update(JSON.stringify(rows)).update("\0").update(endpoint.token).digest("hex"); + const stampPath = join(stateDir, "app-sync.json"); + if (readStamp(stampPath) === digest) return "current"; + const lockPath = join(stateDir, "app-sync.lock"); + if (!tryLock(lockPath, now)) return "busy"; + try { + const app = await connectApp(endpoint, connect); + try { + await syncRows(app, rows); + } finally { + app.close(); + } + const temp = `${stampPath}.${process.pid}.tmp`; + writeFileSync(temp, JSON.stringify({ digest })); + renameSync(temp, stampPath); + return "synced"; + } finally { + try { + unlinkSync(lockPath); + } catch {} + } +} + +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/copilot_app_plugin/extension.mjs b/src/ucode/copilot_app_plugin/extension.mjs index 1cf55a2e5..691b7c094 100644 --- a/src/ucode/copilot_app_plugin/extension.mjs +++ b/src/ucode/copilot_app_plugin/extension.mjs @@ -1,5 +1,8 @@ +import { fileURLToPath } from "node:url"; + import { joinSession } from "@github/copilot-sdk/extension"; +import { syncInBackground } from "./app-rows.mjs"; import { buildProviders, createTokenSource, @@ -9,6 +12,9 @@ import { } 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); +const firstSync = syncInBackground(config, syncOptions, logSyncFailure); let session; const tokenSource = createTokenSource(config); const bearerTokenProvider = withLoginHint(tokenSource, () => @@ -21,3 +27,7 @@ session = await joinSession({ session.on((event) => { if (isAuthRejection(event)) tokenSource.invalidate(); }); +// The extension can start before the app publishes its WebSocket endpoint. +if (["not_running", "failed"].includes(await firstSync)) { + await syncInBackground(config, syncOptions, logSyncFailure); +} diff --git a/src/ucode/copilot_app_plugin/ug-plugin.mjs b/src/ucode/copilot_app_plugin/ug-plugin.mjs index e86ce2bb3..396c39b9f 100644 --- a/src/ucode/copilot_app_plugin/ug-plugin.mjs +++ b/src/ucode/copilot_app_plugin/ug-plugin.mjs @@ -1,6 +1,6 @@ import { execFile } from "node:child_process"; import { readFileSync } from "node:fs"; -import { delimiter } from "node:path"; +import { delimiter, dirname } from "node:path"; export const REFRESH_MARGIN_MS = 120_000; export const DEFAULT_TOKEN_TTL_MS = 300_000; @@ -22,7 +22,8 @@ export function jwtExpiryMs(token) { } export function childEnv(config, env = process.env) { - const path = [...(config.pathDirs || []), env.PATH].filter(Boolean).join(delimiter); + const databricksDir = config.databricksPath ? [dirname(config.databricksPath)] : []; + const path = [...(config.pathDirs || []), ...databricksDir, env.PATH].filter(Boolean).join(delimiter); return { ...env, PATH: path }; } diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index 595226d0d..6fe1a6a18 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -28,6 +28,11 @@ test("jwtExpiryMs reads exp and tolerates opaque tokens", () => { 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)); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs new file mode 100644 index 000000000..26666516d --- /dev/null +++ b/tests/copilot_app_rows.test.mjs @@ -0,0 +1,328 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; +import http from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +import { copilotHome, readEndpoint, syncInBackground, syncOnce } from "../src/ucode/copilot_app_plugin/app-rows.mjs"; + +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, and injectable errors. +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 = {}; + 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}\n4242\n`); + writeFileSync(join(home, "run", "ws.token"), `${this.token}\n4242\n`); + } + + _upgrade(request, socket) { + this.handshakes.push(request.headers); + const token = new URL(request.url, "http://x").searchParams.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; + } + 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(); + } +} + +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), "synced"); + 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)); +}); + +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), "synced"); + + 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, "app-sync.lock"); + writeFileSync(lock, ""); + assert.equal(await syncOnce({ app: ROWS }, options), "busy"); + assert.equal(app.received.length, 0); + + const old = new Date(Date.now() - 120_000); + utimesSync(lock, old, old); + assert.equal(await syncOnce({ app: ROWS }, options), "synced"); +}); + +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), "synced"); + 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), "synced"); + 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), "synced"); +}); + +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); +}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 31eb15cf2..8db104ed2 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -107,6 +107,55 @@ def test_models_repeat_the_rows_limits_and_vision(self): 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_token_command_and_hint_name_the_workspace_and_profile(self): config = plugin.extension_config(_config()) assert config["tokenCommand"] == [ @@ -151,6 +200,7 @@ def test_layout(self, home): assert {p.name for p in extension.iterdir()} == { "extension.mjs", "ug-plugin.mjs", + "app-rows.mjs", "config.json", } assert (extension / "extension.mjs").read_text() == ( @@ -298,13 +348,14 @@ def test_missing_directory_is_not_installed(self, home): @pytest.mark.skipif(NODE is None, reason="node is not installed") class TestExtensionSource: - @pytest.mark.parametrize("name", ["extension.mjs", "ug-plugin.mjs"]) + @pytest.mark.parametrize("name", ["extension.mjs", "ug-plugin.mjs", "app-rows.mjs"]) def test_syntax(self, name): subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) - def test_node_unit_tests(self): + @pytest.mark.parametrize("name", ["copilot_app_plugin.test.mjs", "copilot_app_rows.test.mjs"]) + def test_node_unit_tests(self, name): result = subprocess.run( - [NODE, "--test", str(Path(__file__).with_name("copilot_app_plugin.test.mjs"))], + [NODE, "--test", str(Path(__file__).with_name(name))], capture_output=True, text=True, ) From 4489814bb5da53d36cdf5d95824cafa60ce736d0 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:51:01 +0000 Subject: [PATCH 05/26] copilot: address review of the Copilot app plugin - The token command comes from ug's own builders, so a PAT or custom OAuth setup keeps its mode: `ug auth-token --host` ignores the saved choices. The sign-in hint follows the mode. - A failed token command shows a hint only for an auth failure, or says ug wasn't found; a hint the session couldn't show yet doesn't count as shown. A 401 also logs the sign-in hint, since the app's own message names Copilot's API-key settings. - A mint running when the token is invalidated no longer caches its token. - remove() leaves a unity-gateway marketplace that points elsewhere. - settings.json: a symlink is refused, and rewrites keep the file's mode. - The row-sync node tests skip on a Node without a global WebSocket. Co-authored-by: Isaac --- src/ucode/copilot_app_plugin/__init__.py | 80 +++++++++++++++++----- src/ucode/copilot_app_plugin/extension.mjs | 15 ++-- src/ucode/copilot_app_plugin/ug-plugin.mjs | 44 ++++++++---- tests/copilot_app_plugin.test.mjs | 46 ++++++++++++- tests/copilot_app_rows.test.mjs | 6 +- tests/test_copilot_app_plugin.py | 67 ++++++++++++++++++ 6 files changed, 222 insertions(+), 36 deletions(-) diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/copilot_app_plugin/__init__.py index 3c37eb80a..50f435bd5 100644 --- a/src/ucode/copilot_app_plugin/__init__.py +++ b/src/ucode/copilot_app_plugin/__init__.py @@ -2,8 +2,8 @@ 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, serves them straight from the AI Gateway under the same ids, and mints a fresh -Databricks token for every request with ``ug auth-token``. +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. """ from __future__ import annotations @@ -18,6 +18,8 @@ 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 = "unity-gateway" PLUGIN = "databricks" @@ -85,6 +87,8 @@ class PluginConfig: 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, ...] = () @@ -115,17 +119,33 @@ def _extension_dir(root: Path) -> Path: def _token_command(config: PluginConfig) -> list[str]: - command = [config.ug_path, "auth-token", "--host", config.workspace] - if config.profile: - command += ["--profile", config.profile] - return command + # `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"Couldn't get a Databricks token for {config.workspace}. Run `{login}`, then try again." + 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: @@ -185,6 +205,7 @@ def extension_config(config: PluginConfig) -> dict: "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), @@ -259,6 +280,28 @@ def _object(settings: dict, key: str, path: Path) -> dict: 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) -> bool: + marketplaces = settings.get(_MARKETPLACES_KEY) + marketplace = marketplaces.get(MARKETPLACE) if isinstance(marketplaces, dict) else None + source = marketplace.get("source") if isinstance(marketplace, dict) else None + return isinstance(source, dict) and source.get("path") == str(root) + + 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() @@ -268,7 +311,7 @@ def enable(root: Path | None = None, settings_path: Path | None = None) -> None: "source": {"source": "directory", "path": str(root)} } _object(settings, _ENABLED_KEY, path)[PLUGIN_KEY] = True - atomic_write_json(path, settings) + _write_settings(path, settings) def install( @@ -276,6 +319,7 @@ def install( ) -> Path: path = settings_path or copilot_settings_path() read_settings(path) + _check_writable(path) root = write_plugin(config, root) enable(root, path) return root @@ -287,20 +331,27 @@ def remove( 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. Returns whether anything was removed.""" + plugin directory. A ``unity-gateway`` marketplace 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) changed = False + marketplaces = settings.get(_MARKETPLACES_KEY) + foreign = ( + isinstance(marketplaces, dict) + and MARKETPLACE in marketplaces + and not _owns_marketplace(settings, root) + ) for key, entry in ((_MARKETPLACES_KEY, MARKETPLACE), (_ENABLED_KEY, PLUGIN_KEY)): value = settings.get(key) - if isinstance(value, dict) and entry in value: + if not foreign and isinstance(value, dict) and entry in value: del value[entry] if not value: del settings[key] changed = True if changed: - atomic_write_json(path, settings) + _write_settings(path, settings) if root.exists(): if changed: (wait or time.sleep)(UNLOAD_WAIT_S) @@ -315,13 +366,8 @@ def status(root: Path | None = None, settings_path: Path | None = None) -> Statu settings = read_settings(settings_path) except SettingsError: return "invalid_settings" - marketplaces = settings.get(_MARKETPLACES_KEY) - marketplace = marketplaces.get(MARKETPLACE) if isinstance(marketplaces, dict) else None - source = marketplace.get("source") if isinstance(marketplace, dict) else None if not ( - isinstance(source, dict) - and source.get("path") == str(root) - and (_extension_dir(root) / "extension.mjs").exists() + _owns_marketplace(settings, root) and (_extension_dir(root) / "extension.mjs").exists() ): return "not_installed" enabled = settings.get(_ENABLED_KEY) diff --git a/src/ucode/copilot_app_plugin/extension.mjs b/src/ucode/copilot_app_plugin/extension.mjs index 691b7c094..74116d6d6 100644 --- a/src/ucode/copilot_app_plugin/extension.mjs +++ b/src/ucode/copilot_app_plugin/extension.mjs @@ -6,6 +6,7 @@ import { syncInBackground } from "./app-rows.mjs"; import { buildProviders, createTokenSource, + hintFor, isAuthRejection, loadConfig, withLoginHint, @@ -17,15 +18,21 @@ const logSyncFailure = (message) => console.error(message); const firstSync = syncInBackground(config, syncOptions, logSyncFailure); let session; const tokenSource = createTokenSource(config); -const bearerTokenProvider = withLoginHint(tokenSource, () => - session?.log(config.loginHint, { level: "error" }), -); +const bearerTokenProvider = withLoginHint(tokenSource, (error) => { + const hint = hintFor(error, config); + if (!session || !hint) return false; + session.log(hint, { level: "error" }); + return true; +}); session = await joinSession({ providers: buildProviders(config, bearerTokenProvider), models: config.models, }); session.on((event) => { - if (isAuthRejection(event)) tokenSource.invalidate(); + 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") session.log(config.loginHint, { level: "error" }); }); // The extension can start before the app publishes its WebSocket endpoint. if (["not_running", "failed"].includes(await firstSync)) { diff --git a/src/ucode/copilot_app_plugin/ug-plugin.mjs b/src/ucode/copilot_app_plugin/ug-plugin.mjs index 396c39b9f..321ebd4cb 100644 --- a/src/ucode/copilot_app_plugin/ug-plugin.mjs +++ b/src/ucode/copilot_app_plugin/ug-plugin.mjs @@ -32,7 +32,10 @@ export function runCommand(argv, env) { 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(); - reject(new Error(`ug auth-token failed: ${detail}`)); + 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(); @@ -46,31 +49,38 @@ 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. +// 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; - pending = run(argv, childEnv(config)) + const started = generation; + const mint = run(argv, childEnv(config)) .then((value) => { - const expiry = jwtExpiryMs(value); - const refreshAt = expiry ? expiry - REFRESH_MARGIN_MS : now() + DEFAULT_TOKEN_TTL_MS; - cached = { value, refreshAt }; + if (started === generation) { + const expiry = jwtExpiryMs(value); + const refreshAt = expiry ? expiry - REFRESH_MARGIN_MS : now() + DEFAULT_TOKEN_TTL_MS; + cached = { value, refreshAt }; + } return value; }) .finally(() => { - pending = null; + if (pending === mint) pending = null; }); + pending = mint; } return pending; } token.invalidate = () => { + generation++; cached = null; + pending = null; forceRefresh = true; }; return token; @@ -83,8 +93,17 @@ export function isAuthRejection(event) { ); } -// The app retries a failing token provider several times, so the sign-in hint is shown once per -// run of failures. +// 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() { @@ -94,10 +113,9 @@ export function withLoginHint(token, showHint) { return value; } catch (error) { if (!hinted) { - hinted = true; - await Promise.resolve() - .then(() => showHint()) - .catch(() => {}); + hinted = await Promise.resolve() + .then(() => Boolean(showHint(error))) + .catch(() => false); } throw error; } diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index 6fe1a6a18..905a4e777 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -9,6 +9,7 @@ import { buildProviders, childEnv, createTokenSource, + hintFor, isAuthRejection, jwtExpiryMs, withLoginHint, @@ -96,7 +97,7 @@ test("withLoginHint shows the hint once per run of failures", async () => { if (fail) throw new Error("no login"); return "t"; }, - () => hints++, + () => (hints++, true), ); await assert.rejects(bearer(), /no login/); await assert.rejects(bearer(), /no login/); @@ -120,6 +121,49 @@ test("withLoginHint still throws the token error when the hint fails", async () 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); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 26666516d..19854b2da 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -4,10 +4,14 @@ import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; import http from "node:http"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { test } from "node:test"; +import { test as nodeTest } from "node:test"; import { copilotHome, readEndpoint, syncInBackground, syncOnce } from "../src/ucode/copilot_app_plugin/app-rows.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); + function encode(text) { const payload = Buffer.from(text); const length = payload.length; diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 8db104ed2..7beb1d6a7 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -156,6 +156,40 @@ def test_app_rows_and_databricks_path(self): "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 "`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"] == [ @@ -311,6 +345,39 @@ def test_remove_without_keys_deletes_the_directory_at_once(self, home): 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": { + "unity-gateway": {"source": {"source": "directory", "path": "/elsewhere"}} + }, + "enabledPlugins": {"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() == "{}" + + 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): plugin.install(_config()) plugin.remove() From c5c0828a8e0cfb48db30710146fd1c29e746adbb Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:15:23 +0000 Subject: [PATCH 06/26] copilot: move the Copilot app plugin under agents/desktop Desktop-app adapters live in agents/desktop/. The wheel keeps shipping the extension's .mjs files, since hatch includes every file in the package. Co-authored-by: Isaac --- src/ucode/{ => agents/desktop}/copilot_app_plugin/__init__.py | 0 src/ucode/{ => agents/desktop}/copilot_app_plugin/app-rows.mjs | 0 src/ucode/{ => agents/desktop}/copilot_app_plugin/extension.mjs | 0 src/ucode/{ => agents/desktop}/copilot_app_plugin/ug-plugin.mjs | 0 tests/copilot_app_plugin.test.mjs | 2 +- tests/copilot_app_rows.test.mjs | 2 +- tests/test_copilot_app_plugin.py | 2 +- 7 files changed, 3 insertions(+), 3 deletions(-) rename src/ucode/{ => agents/desktop}/copilot_app_plugin/__init__.py (100%) rename src/ucode/{ => agents/desktop}/copilot_app_plugin/app-rows.mjs (100%) rename src/ucode/{ => agents/desktop}/copilot_app_plugin/extension.mjs (100%) rename src/ucode/{ => agents/desktop}/copilot_app_plugin/ug-plugin.mjs (100%) diff --git a/src/ucode/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py similarity index 100% rename from src/ucode/copilot_app_plugin/__init__.py rename to src/ucode/agents/desktop/copilot_app_plugin/__init__.py diff --git a/src/ucode/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs similarity index 100% rename from src/ucode/copilot_app_plugin/app-rows.mjs rename to src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs diff --git a/src/ucode/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs similarity index 100% rename from src/ucode/copilot_app_plugin/extension.mjs rename to src/ucode/agents/desktop/copilot_app_plugin/extension.mjs diff --git a/src/ucode/copilot_app_plugin/ug-plugin.mjs b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs similarity index 100% rename from src/ucode/copilot_app_plugin/ug-plugin.mjs rename to src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index 905a4e777..d90fdabe2 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -13,7 +13,7 @@ import { isAuthRejection, jwtExpiryMs, withLoginHint, -} from "../src/ucode/copilot_app_plugin/ug-plugin.mjs"; +} 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" } }], diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 19854b2da..e5411ad84 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -6,7 +6,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { test as nodeTest } from "node:test"; -import { copilotHome, readEndpoint, syncInBackground, syncOnce } from "../src/ucode/copilot_app_plugin/app-rows.mjs"; +import { copilotHome, readEndpoint, syncInBackground, syncOnce } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.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"; diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 7beb1d6a7..ce7c4a297 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -5,7 +5,7 @@ import pytest -from ucode import copilot_app_plugin as plugin +from ucode.agents.desktop import copilot_app_plugin as plugin NODE = shutil.which("node") PLUGIN_SOURCE = Path(plugin.__file__).parent From 1c6a416f191850412c6b919ee88010e6f9585ae8 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:51:13 +0000 Subject: [PATCH 07/26] copilot: let the plugin finish a removal when the app starts With the GitHub Copilot app closed, ug can't delete its rows from the app's picker. defer_remove marks the plugin's config, and on the app's next start the extension deletes ug's providers over the app's local API, then removes ug's two settings.json keys under the same rules as the Python writer, and serves nothing. A lock keeps sessions from racing. cleanup_leftover deletes the directory on a later ug run once the keys are gone. Not yet checked against the live app. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 76 ++++- .../desktop/copilot_app_plugin/app-remove.mjs | 145 ++++++++++ .../desktop/copilot_app_plugin/app-rows.mjs | 25 +- .../desktop/copilot_app_plugin/extension.mjs | 53 ++-- tests/copilot_app_fake.mjs | 163 +++++++++++ tests/copilot_app_remove.test.mjs | 269 ++++++++++++++++++ tests/copilot_app_rows.test.mjs | 162 +---------- tests/test_copilot_app_plugin.py | 159 ++++++++++- 8 files changed, 856 insertions(+), 196 deletions(-) create mode 100644 src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs create mode 100644 tests/copilot_app_fake.mjs create mode 100644 tests/copilot_app_remove.test.mjs diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 50f435bd5..63a88d1c2 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -4,6 +4,9 @@ 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. + +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 @@ -28,11 +31,11 @@ VERSION = "0.1.0" Wire = Literal["anthropic", "responses", "completions"] -Status = Literal["installed", "disabled", "not_installed", "invalid_settings"] +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") +_SOURCE_FILES = ("extension.mjs", "ug-plugin.mjs", "app-rows.mjs", "app-remove.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 @@ -302,6 +305,33 @@ def _owns_marketplace(settings: dict, root: Path) -> bool: return isinstance(source, dict) and source.get("path") == str(root) +def _installed(settings: dict, root: Path) -> bool: + return _owns_marketplace(settings, root) and (_extension_dir(root) / "extension.mjs").exists() + + +def _has_ug_keys(settings: dict, root: Path) -> bool: + if _owns_marketplace(settings, root): + return True + marketplaces = settings.get(_MARKETPLACES_KEY) + if isinstance(marketplaces, dict) and MARKETPLACE in marketplaces: + return False + enabled = settings.get(_ENABLED_KEY) + return isinstance(enabled, dict) and PLUGIN_KEY in enabled + + +def _read_extension_config(root: Path) -> dict | None: + try: + config = json.loads((_extension_dir(root) / "config.json").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: + config = _read_extension_config(root) + return config is not None and config.get("remove") is True + + 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() @@ -360,17 +390,51 @@ def remove( return changed +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) + if not _installed(settings, root): + return False + config = _read_extension_config(root) + if config is None: + return False + config["remove"] = True + config["pluginRoot"] = str(root) + atomic_write_json(_extension_dir(root) / "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 + 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" - if not ( - _owns_marketplace(settings, root) and (_extension_dir(root) / "extension.mjs").exists() - ): + if not _installed(settings, root): return "not_installed" enabled = settings.get(_ENABLED_KEY) if isinstance(enabled, dict) and enabled.get(PLUGIN_KEY) is True: - return "installed" + 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..038e0ed58 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs @@ -0,0 +1,145 @@ +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, + SYNC_STAMP_FILE, + connectApp, + copilotHome, + deleteProviders, + readEndpoint, + tryLock, +} from "./app-rows.mjs"; + +export const MARKETPLACE = "unity-gateway"; +export const PLUGIN_KEY = "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 unity-gateway marketplace at another path isn't ug's, so it and its plugin key stay. +function ugKeys(settings, pluginRoot) { + const marketplaces = settings[MARKETPLACES_KEY]; + 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, PLUGIN_KEY], + ].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 lockPath = join(stateDir, LOCK_FILE); + if (!tryLock(lockPath, now)) 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 again, not match the last sync. + try { + unlinkSync(join(stateDir, SYNC_STAMP_FILE)); + } catch {} + } + if (refusal) throw refusal; + removeSettingsKeys(path, config.pluginRoot); + return "removed"; + } finally { + try { + unlinkSync(lockPath); + } catch {} + } +} + +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-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index fc7ec222b..a76b6de72 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -5,6 +5,8 @@ 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 function copilotHome(env = process.env) { return env.COPILOT_HOME || join(homedir(), ".copilot"); @@ -137,12 +139,23 @@ async function deleteProvider(app, 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); + } +} + // 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. export async function syncRows(app, rows) { - 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."); - const existing = new Map(providers.filter((p) => p && typeof p.id === "string").map((p) => [p.id, p])); + const existing = await listProviders(app); const wanted = new Set(rows.providers.map((entry) => entry.provider.id)); for (const id of rows.ownedProviderIds) { if (existing.has(id) && !wanted.has(id)) await deleteProvider(app, id); @@ -165,7 +178,7 @@ export async function syncRows(app, rows) { } } -function tryLock(path, now) { +export function tryLock(path, now) { try { closeSync(openSync(path, "wx")); return true; @@ -198,9 +211,9 @@ export async function syncOnce(config, { home = copilotHome(), stateDir, now = D const endpoint = readEndpoint(home); if (!endpoint) return "not_running"; const digest = createHash("sha256").update(JSON.stringify(rows)).update("\0").update(endpoint.token).digest("hex"); - const stampPath = join(stateDir, "app-sync.json"); + const stampPath = join(stateDir, SYNC_STAMP_FILE); if (readStamp(stampPath) === digest) return "current"; - const lockPath = join(stateDir, "app-sync.lock"); + const lockPath = join(stateDir, LOCK_FILE); if (!tryLock(lockPath, now)) return "busy"; try { const app = await connectApp(endpoint, connect); diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index 74116d6d6..19406aae7 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -2,6 +2,7 @@ import { fileURLToPath } from "node:url"; import { joinSession } from "@github/copilot-sdk/extension"; +import { removeInBackground } from "./app-remove.mjs"; import { syncInBackground } from "./app-rows.mjs"; import { buildProviders, @@ -15,26 +16,36 @@ import { 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); -const firstSync = syncInBackground(config, syncOptions, logSyncFailure); -let session; -const tokenSource = createTokenSource(config); -const bearerTokenProvider = withLoginHint(tokenSource, (error) => { - const hint = hintFor(error, config); - if (!session || !hint) return false; - session.log(hint, { level: "error" }); - 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") session.log(config.loginHint, { level: "error" }); -}); // The extension can start before the app publishes its WebSocket endpoint. -if (["not_running", "failed"].includes(await firstSync)) { - await syncInBackground(config, syncOptions, logSyncFailure); +const retry = (result) => ["not_running", "failed"].includes(result); + +if (config.remove) { + const firstRemove = removeInBackground(config, syncOptions, logSyncFailure); + try { + await joinSession(); + } catch (error) { + logSyncFailure(`Databricks models: couldn't join the session: ${error?.message ?? error}`); + } + if (retry(await firstRemove)) await removeInBackground(config, syncOptions, logSyncFailure); +} else { + const firstSync = syncInBackground(config, syncOptions, logSyncFailure); + let session; + const tokenSource = createTokenSource(config); + const bearerTokenProvider = withLoginHint(tokenSource, (error) => { + const hint = hintFor(error, config); + if (!session || !hint) return false; + session.log(hint, { level: "error" }); + 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") session.log(config.loginHint, { level: "error" }); + }); + if (retry(await firstSync)) await syncInBackground(config, syncOptions, logSyncFailure); } diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs new file mode 100644 index 000000000..9be55cb21 --- /dev/null +++ b/tests/copilot_app_fake.mjs @@ -0,0 +1,163 @@ +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, 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 = {}; + 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}\n4242\n`); + writeFileSync(join(home, "run", "ws.token"), `${this.token}\n4242\n`); + } + + _upgrade(request, socket) { + this.handshakes.push(request.headers); + const token = new URL(request.url, "http://x").searchParams.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; + } + 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_remove.test.mjs b/tests/copilot_app_remove.test.mjs new file mode 100644 index 000000000..4f8642d89 --- /dev/null +++ b/tests/copilot_app_remove.test.mjs @@ -0,0 +1,269 @@ +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 { 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: { "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, "app-sync.lock"); + 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() - 120_000); + 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("a foreign unity-gateway marketplace and its plugin key stay", async (t) => { + const foreign = { + extraKnownMarketplaces: { "unity-gateway": { source: { source: "directory", path: "/elsewhere" } } }, + enabledPlugins: { "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 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), "synced"); + assert.equal(await removeOnce({ ...CONFIG, app: rows }, options), "removed"); + assert.equal(app.providers.has(GPT), false); + assert.equal(await syncOnce({ app: rows }, options), "synced"); + 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"]) { + 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_rows.test.mjs b/tests/copilot_app_rows.test.mjs index e5411ad84..928f087ea 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -1,176 +1,16 @@ import assert from "node:assert/strict"; -import { createHash } from "node:crypto"; import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; -import http from "node:http"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test as nodeTest } from "node:test"; import { copilotHome, readEndpoint, syncInBackground, syncOnce } 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); -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, and injectable errors. -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 = {}; - 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}\n4242\n`); - writeFileSync(join(home, "run", "ws.token"), `${this.token}\n4242\n`); - } - - _upgrade(request, socket) { - this.handshakes.push(request.headers); - const token = new URL(request.url, "http://x").searchParams.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; - } - 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(); - } -} - const CLAUDE = "11111111-1111-5111-8111-111111111111"; const GPT = "22222222-2222-5222-8222-222222222222"; const OPEN = "33333333-3333-5333-8333-333333333333"; diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index ce7c4a297..536cbc6f4 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -235,6 +235,7 @@ def test_layout(self, home): "extension.mjs", "ug-plugin.mjs", "app-rows.mjs", + "app-remove.mjs", "config.json", } assert (extension / "extension.mjs").read_text() == ( @@ -391,6 +392,122 @@ def test_remove_with_invalid_settings_changes_nothing(self, home): assert root.exists() +def _extension_config(root: Path) -> dict: + return json.loads( + (root / "plugins" / "databricks" / "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"]["unity-gateway"] + del settings["enabledPlugins"]["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"]["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 json.loads(_settings(home).read_text()) == {} + assert not root.exists() + + class TestStatus: def test_not_installed(self, home): assert plugin.status() == "not_installed" @@ -415,11 +532,20 @@ def test_missing_directory_is_not_installed(self, home): @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"]) + @pytest.mark.parametrize( + "name", ["extension.mjs", "ug-plugin.mjs", "app-rows.mjs", "app-remove.mjs"] + ) def test_syntax(self, name): subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) - @pytest.mark.parametrize("name", ["copilot_app_plugin.test.mjs", "copilot_app_rows.test.mjs"]) + @pytest.mark.parametrize( + "name", + [ + "copilot_app_plugin.test.mjs", + "copilot_app_rows.test.mjs", + "copilot_app_remove.test.mjs", + ], + ) def test_node_unit_tests(self, name): result = subprocess.run( [NODE, "--test", str(Path(__file__).with_name(name))], @@ -427,3 +553,32 @@ def test_node_unit_tests(self, name): text=True, ) assert result.returncode == 0, result.stdout + result.stderr + + def test_the_extension_finishes_a_deferred_remove_for_ug(self, home, tmp_path): + root = plugin.install(_config()) + plugin.defer_remove() + extension = root / "plugins" / "databricks" / "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() From 7388385767bc2e3402b697369a6ce08450a8561c Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:59:04 +0000 Subject: [PATCH 08/26] copilot: delete the settings file the plugin created when it's left empty install() records when Copilot's settings.json didn't exist before. remove(), and cleanup_leftover() after a deferred remove, delete the file only if ug created it and nothing is left in it; otherwise they keep it. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 26 ++++++++++++- tests/test_copilot_app_plugin.py | 37 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 63a88d1c2..42f1501cf 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -344,13 +344,36 @@ def enable(root: Path | None = None, settings_path: Path | None = None) -> None: _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() read_settings(path) _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) return root @@ -380,7 +403,7 @@ def remove( if not value: del settings[key] changed = True - if changed: + if changed and not _drop_settings(root, path, settings): _write_settings(path, settings) if root.exists(): if changed: @@ -422,6 +445,7 @@ def cleanup_leftover(root: Path | None = None, settings_path: Path | None = None 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 diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 536cbc6f4..11babad7b 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -327,7 +327,36 @@ def test_remove_leaves_no_ug_state(self, home): 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 = [] @@ -336,7 +365,7 @@ def wait(seconds): seen.append((seconds, settings, root.exists())) assert plugin.remove(wait=wait) is True - assert seen == [(plugin.UNLOAD_WAIT_S, {}, 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): @@ -380,9 +409,11 @@ def test_rewriting_settings_keeps_their_mode(self, home): 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()) == {} + assert json.loads(_settings(home).read_text()) == {"model": "x"} def test_remove_with_invalid_settings_changes_nothing(self, home): root = plugin.install(_config()) @@ -504,7 +535,7 @@ def test_remove_while_pending_still_drops_the_keys_first(self, home): waits = [] assert plugin.remove(wait=waits.append) is True assert waits == [plugin.UNLOAD_WAIT_S] - assert json.loads(_settings(home).read_text()) == {} + assert not _settings(home).exists() assert not root.exists() From 86eb7e8fa0f4147e83eb62d3dd614714a5077e60 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 10:48:42 +0000 Subject: [PATCH 09/26] copilot: restart the app once after the plugin changes its picker rows The app's window loads the model picker's providers only when it starts, about 7 s before the first extension runs, so rows the plugin has just created or changed show a generic icon and no Databricks label until the next launch. After a sync that changed ug's rows, one extension process claims a restart under the sync lock, keyed by a hash of the rows, so a relaunch, another chat or an unchanged configure never restarts again. It waits until no chat in the app is running a turn (the app's session event stream, else its own session, plus a quiet window), posts a line in the chat, and a detached helper closes the app without forcing it, waits for it to exit and starts it again with the user's session environment. When it can't (no display on Linux, the app stays up, three minutes without idle), it asks the user to restart instead. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 11 +- .../desktop/copilot_app_plugin/app-remove.mjs | 11 +- .../copilot_app_plugin/app-restart.mjs | 247 +++++++++++++ .../desktop/copilot_app_plugin/app-rows.mjs | 86 ++++- .../desktop/copilot_app_plugin/extension.mjs | 7 +- .../copilot_app_plugin/restart-helper.mjs | 3 + tests/copilot_app_fake.mjs | 17 +- tests/copilot_app_remove.test.mjs | 8 +- tests/copilot_app_restart.test.mjs | 341 ++++++++++++++++++ tests/copilot_app_rows.test.mjs | 80 +++- tests/test_copilot_app_plugin.py | 13 +- 11 files changed, 790 insertions(+), 34 deletions(-) create mode 100644 src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs create mode 100644 src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs create mode 100644 tests/copilot_app_restart.test.mjs diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 42f1501cf..f43c5a2fc 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -4,6 +4,8 @@ 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. @@ -35,7 +37,14 @@ _MARKETPLACES_KEY = "extraKnownMarketplaces" _ENABLED_KEY = "enabledPlugins" -_SOURCE_FILES = ("extension.mjs", "ug-plugin.mjs", "app-rows.mjs", "app-remove.mjs") +_SOURCE_FILES = ( + "extension.mjs", + "ug-plugin.mjs", + "app-rows.mjs", + "app-remove.mjs", + "app-restart.mjs", + "restart-helper.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 diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs index 038e0ed58..e0e6b811b 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs @@ -4,6 +4,7 @@ import { basename, dirname, join } from "node:path"; import { LOCK_FILE, + RESTART_MARKER_FILE, SYNC_STAMP_FILE, connectApp, copilotHome, @@ -118,10 +119,12 @@ export async function removeOnce(config, { home = copilotHome(), stateDir, now = } finally { app.close(); } - // A later install must write the rows again, not match the last sync. - try { - unlinkSync(join(stateDir, SYNC_STAMP_FILE)); - } catch {} + // 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); 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..571ef0f6c --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs @@ -0,0 +1,247 @@ +import { execFile, spawn } from "node:child_process"; +import { appendFileSync, existsSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; +import { fileURLToPath } from "node:url"; + +import { connectApp, copilotHome, readAppPid, readEndpoint } from "./app-rows.mjs"; + +export const RESTARTING_MESSAGE = "Restarting the GitHub Copilot app once 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"; +export const QUIET_MS = 15_000; +export const IDLE_TIMEOUT_MS = 180_000; +export const EXIT_TIMEOUT_MS = 15_000; +const IDLE_POLL_MS = 1_000; +const EXIT_POLL_MS = 100; +const CLOSE_DELAY_MS = 1_000; +const RELAUNCH_RETRY_MS = 2_000; +const LAUNCH_CHECK_MS = 3_000; +const COMMAND_TIMEOUT_MS = 10_000; +const LINUX_APP = "/usr/bin/github"; +const MAC_BUNDLE_ID = "com.github.githubapp"; +const HELPER = fileURLToPath(new URL("./restart-helper.mjs", import.meta.url)); +const TURN_STARTS = new Set(["user.message", "assistant.turn_start"]); + +// A running turn sends events all the time, so the app is idle once no turn is open and no session +// has sent anything for a while. That also covers a turn that started before the watch did. +export function createActivity(now = Date.now) { + const running = new Set(); + let last = now(); + return { + note(sessionId, type) { + last = now(); + if (type === "session.idle") running.delete(sessionId); + else if (TURN_STARTS.has(type)) running.add(sessionId); + }, + idle: (quietMs) => running.size === 0 && now() - last >= quietMs, + }; +} + +export async function waitForIdle( + activity, + { quietMs = QUIET_MS, timeoutMs = IDLE_TIMEOUT_MS, now = Date.now, sleep = delay } = {}, +) { + const deadline = now() + timeoutMs; + for (;;) { + if (activity.idle(quietMs)) return true; + if (now() >= deadline) return false; + await sleep(IDLE_POLL_MS); + } +} + +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 restartApp 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 extension exits with the app, so the restart runs in a detached process of its own. +export function startHelper(target, stateDir) { + return new Promise((resolve) => { + const argument = JSON.stringify({ ...target, log: join(stateDir, RESTART_LOG_FILE) }); + const child = spawn(process.execPath, [HELPER, argument], { detached: true, stdio: "ignore", windowsHide: true }); + child.once("spawn", () => { + child.unref(); + resolve(true); + }); + child.once("error", () => resolve(false)); + }); +} + +async function tryRestart( + activity, + say, + { home = copilotHome(), stateDir, plan = planRestart, start = startHelper, now, sleep, quietMs, timeoutMs, ...connect }, +) { + const target = await plan({ home }); + if (!target) return "unsupported"; + const endpoint = readEndpoint(home); + const onMessage = (message) => { + if (message.type === "session_event") activity.note(message.session_id, message.event?.type); + }; + // Without this connection, only this session's own turns are known. + const watch = endpoint ? await connectApp(endpoint, { ...connect, onMessage }).catch(() => null) : null; + try { + if (!(await waitForIdle(activity, { quietMs, timeoutMs, now, sleep }))) return "busy"; + } finally { + watch?.close(); + } + await say(RESTARTING_MESSAGE); + return (await start(target, stateDir)) ? "restarting" : "failed"; +} + +// Restarts the app once no chat is running a turn, or asks the user to restart it. Never throws. +export async function restartWhenIdle(session, activity, options = {}) { + const say = (message) => + Promise.resolve() + .then(() => session.log(message, { level: "info" })) + .catch(() => {}); + const result = await tryRestart(activity, say, options).catch(() => "failed"); + if (result !== "restarting") await say(RESTART_HINT); + return result; +} + +// The extension has the Copilot runtime's environment, which adds the runtime's own settings and +// credentials. The app gets only the user's session environment back. +const SESSION_ENV = new Set([ + "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", +]); +const SESSION_ENV_PREFIXES = ["LC_", "XDG_", "PROCESSOR_"]; + +export function sessionEnv(env) { + return Object.fromEntries( + Object.entries(env).filter(([name]) => { + const key = name.toUpperCase(); + return SESSION_ENV.has(key) || SESSION_ENV_PREFIXES.some((prefix) => key.startsWith(prefix)); + }), + ); +} + +function launchDetached(command, args, env) { + return new Promise((resolve) => { + let child; + try { + child = spawn(command, args, { cwd: homedir(), detached: true, env, stdio: "ignore" }); + } catch { + resolve(false); + return; + } + // A launch that fails exits at once and nonzero. + const timer = setTimeout(() => { + child.unref(); + resolve(true); + }, LAUNCH_CHECK_MS); + child.once("error", () => { + clearTimeout(timer); + resolve(false); + }); + child.once("exit", (code) => { + clearTimeout(timer); + resolve(code === 0); + }); + }); +} + +function terminate(kill, pid) { + try { + kill(pid, "SIGTERM"); + } catch {} +} + +// Closes the app and starts it again once it has exited. It's never forced closed, so an app that +// stays up is left running. +export async function restartApp( + { platform, pid, app }, + { + alive = isAlive, + kill = (target, signal) => process.kill(target, signal), + run = runCommand, + launch = launchDetached, + now = Date.now, + sleep = delay, + env = process.env, + } = {}, +) { + await sleep(CLOSE_DELAY_MS); + if (platform === "darwin") { + if ((await run("/usr/bin/osascript", ["-e", `quit app id "${app}"`])) === null) terminate(kill, pid); + } else if (platform === "win32") { + // Without /F, taskkill closes the app's window as its close button does, which can leave the + // app running in the tray. + await run("taskkill", ["/PID", String(pid)]); + } else { + terminate(kill, pid); + } + const deadline = now() + EXIT_TIMEOUT_MS; + while (alive(pid)) { + if (now() >= deadline) return "still_running"; + await sleep(EXIT_POLL_MS); + } + const [command, args] = platform === "darwin" ? ["/usr/bin/open", ["-b", app]] : [app, []]; + for (let attempt = 0; attempt < 2; attempt++) { + if (attempt) await sleep(RELAUNCH_RETRY_MS); + if (await launch(command, args, sessionEnv(env))) return "relaunched"; + } + return "relaunch_failed"; +} + +export async function runHelper(argument, deps) { + let target; + try { + target = JSON.parse(argument); + } catch { + return; + } + const log = (line) => { + try { + appendFileSync(target.log, `${new Date().toISOString()} ${line}\n`); + } catch {} + }; + try { + log(await restartApp(target, deps)); + } catch (error) { + log(`failed: ${error?.message ?? error}`); + } +} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index a76b6de72..71cddae37 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -7,14 +7,28 @@ 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"); } // Each run file holds the value, then the app's pid. +function runFields(home, name) { + return readFileSync(join(home, "run", name), "utf8").trim().split(/\s+/); +} + function runValue(home, name) { - return readFileSync(join(home, "run", name), "utf8").trim().split(/\s+/)[0] || null; + return runFields(home, name)[0] || null; +} + +export function readAppPid(home) { + try { + const pid = Number.parseInt(runFields(home, "ws.port")[1], 10); + return Number.isInteger(pid) && pid > 1 ? pid : null; + } catch { + return null; + } } export function readEndpoint(home) { @@ -30,9 +44,10 @@ export function readEndpoint(home) { export class AppError extends Error {} class AppConnection { - constructor(ws, timeoutMs) { + constructor(ws, timeoutMs, onMessage) { this._ws = ws; this._timeoutMs = timeoutMs; + this._onMessage = onMessage; this._backlog = []; this._waiter = null; this.closed = false; @@ -56,6 +71,10 @@ class AppConnection { const waiter = this._waiter; this._waiter = null; waiter.resolve(message); + } else if (this._onMessage) { + try { + this._onMessage(message); + } catch {} } else { this._backlog.push(message); } @@ -108,13 +127,15 @@ class AppConnection { } } -export async function connectApp(endpoint, { WebSocketImpl, timeoutMs = REQUEST_TIMEOUT_MS } = {}) { +// With ``onMessage``, the connection turns session channels on and passes it every message no +// request is waiting for. The app then sends every session's events, including its window's chats. +export async function connectApp(endpoint, { WebSocketImpl, timeoutMs = REQUEST_TIMEOUT_MS, onMessage } = {}) { 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`; + const url = `ws://127.0.0.1:${endpoint.port}/?token=${encodeURIComponent(endpoint.token)}&requester_id=ug&session_channels=${Boolean(onMessage)}`; // 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); + const app = new AppConnection(ws, timeoutMs, onMessage); try { await app.wait((message) => message.type === "server_hello"); } catch (error) { @@ -152,30 +173,56 @@ export async function deleteProviders(app, providerIds) { } } +// 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. +// aren't in it. Upserts with fixed ids, so running it again changes nothing. Returns whether the +// app's rows differed from ``rows``. export async function syncRows(app, rows) { 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); + 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 app.request(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 app.request({ type: "upsert_provider_model", model }, "provider_model_upserted"); } } + return changed; } export function tryLock(path, now) { @@ -203,13 +250,22 @@ function readStamp(path) { } } +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. +// 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"; @@ -217,15 +273,19 @@ export async function syncOnce(config, { home = copilotHome(), stateDir, now = D if (!tryLock(lockPath, now)) return "busy"; try { const app = await connectApp(endpoint, connect); + let changed; try { - await syncRows(app, rows); + changed = await syncRows(app, rows); } finally { app.close(); } - const temp = `${stampPath}.${process.pid}.tmp`; - writeFileSync(temp, JSON.stringify({ digest })); - renameSync(temp, stampPath); - return "synced"; + // 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 { try { unlinkSync(lockPath); diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index 19406aae7..e61feb60e 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url"; import { joinSession } from "@github/copilot-sdk/extension"; import { removeInBackground } from "./app-remove.mjs"; +import { createActivity, restartWhenIdle } from "./app-restart.mjs"; import { syncInBackground } from "./app-rows.mjs"; import { buildProviders, @@ -41,11 +42,15 @@ if (config.remove) { providers: buildProviders(config, bearerTokenProvider), models: config.models, }); + const activity = createActivity(); session.on((event) => { + activity.note(session.sessionId, event.type); 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") session.log(config.loginHint, { level: "error" }); }); - if (retry(await firstSync)) await syncInBackground(config, syncOptions, logSyncFailure); + let synced = await firstSync; + if (retry(synced)) synced = await syncInBackground(config, syncOptions, logSyncFailure); + if (synced === "changed") await restartWhenIdle(session, activity, syncOptions); } diff --git a/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs b/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs new file mode 100644 index 000000000..e3d4da6b2 --- /dev/null +++ b/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs @@ -0,0 +1,3 @@ +import { runHelper } from "./app-restart.mjs"; + +await runHelper(process.argv.at(-1)); diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs index 9be55cb21..859659aaf 100644 --- a/tests/copilot_app_fake.mjs +++ b/tests/copilot_app_fake.mjs @@ -49,7 +49,7 @@ function parser(onText, onClose) { } // The app's WebSocket API as recorded from app 1.4.2: provider and model CRUD, its token and -// origin checks, and injectable errors. +// origin checks, session events for connections with session channels on, and injectable errors. export class FakeApp { constructor() { this.token = "per-launch-token"; @@ -60,6 +60,7 @@ export class FakeApp { this.handshakes = []; this.connections = 0; this.sockets = new Set(); + this.watchers = new Set(); this.busy = 0; this.fail = null; this.drop = null; @@ -84,14 +85,19 @@ export class FakeApp { _upgrade(request, socket) { this.handshakes.push(request.headers); - const token = new URL(request.url, "http://x").searchParams.get("token"); + 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)); + if (params.get("session_channels") === "true") this.watchers.add(socket); + socket.on("close", () => { + this.sockets.delete(socket); + this.watchers.delete(socket); + }); const accept = createHash("sha1") .update(request.headers["sec-websocket-key"] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") .digest("base64"); @@ -152,6 +158,11 @@ export class FakeApp { return { type: "error", message: `unknown variant \`${kind}\`` }; } + emit(sessionId, type) { + const frame = encode(JSON.stringify({ type: "session_event", session_id: sessionId, event: { type, data: {} } })); + for (const socket of this.watchers) socket.write(frame); + } + sent(kind) { return this.received.filter((message) => message.type === kind); } diff --git a/tests/copilot_app_remove.test.mjs b/tests/copilot_app_remove.test.mjs index 4f8642d89..353b2c22c 100644 --- a/tests/copilot_app_remove.test.mjs +++ b/tests/copilot_app_remove.test.mjs @@ -190,16 +190,16 @@ test("without owned providers the keys go without connecting", async (t) => { assert.deepEqual(read(path), {}); }); -test("a later install syncs its rows again", async (t) => { +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), "synced"); + 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), "synced"); + assert.equal(await syncOnce({ app: rows }, options), "changed"); assert.equal(app.providers.has(GPT), true); }); @@ -225,7 +225,7 @@ const FAKE_SDK = `data:text/javascript,${encodeURIComponent(` 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"]) { + 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" }] })); diff --git a/tests/copilot_app_restart.test.mjs b/tests/copilot_app_restart.test.mjs new file mode 100644 index 000000000..1d5249dbd --- /dev/null +++ b/tests/copilot_app_restart.test.mjs @@ -0,0 +1,341 @@ +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; +import { test } from "node:test"; + +import { + EXIT_TIMEOUT_MS, + IDLE_TIMEOUT_MS, + QUIET_MS, + RESTART_HINT, + RESTARTING_MESSAGE, + createActivity, + planRestart, + restartApp, + restartWhenIdle, + runHelper, + sessionEnv, +} from "../src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs"; +import { FakeApp } from "./copilot_app_fake.mjs"; + +const TARGET = { platform: "linux", pid: 4242, app: "/usr/bin/github" }; + +function fakeClock() { + const clock = { t: 0, onSleep: () => {} }; + clock.now = () => clock.t; + clock.sleep = async (ms) => { + clock.t += ms; + await clock.onSleep(clock.t); + }; + return clock; +} + +function fakeSession() { + const logs = []; + return { logs, log: async (message, options) => logs.push([message, options.level]) }; +} + +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 restartOptions(clock, overrides = {}) { + const started = []; + return { + started, + options: { + home: emptyHome(), + stateDir: mkdtempSync(join(tmpdir(), "ug-copilot-state-")), + plan: async () => TARGET, + start: async (target) => (started.push({ target, at: clock.t }), true), + now: clock.now, + sleep: clock.sleep, + ...overrides, + }, + }; +} + +test("an idle app restarts after a quiet period, with one line in the chat first", async () => { + const clock = fakeClock(); + const session = fakeSession(); + const activity = createActivity(clock.now); + const { started, options } = restartOptions(clock); + assert.equal(await restartWhenIdle(session, activity, options), "restarting"); + assert.deepEqual(started, [{ target: TARGET, at: QUIET_MS }]); + assert.deepEqual(session.logs, [[RESTARTING_MESSAGE, "info"]]); +}); + +test("a running turn delays the restart until its session is idle", async () => { + const clock = fakeClock(); + const session = fakeSession(); + const activity = createActivity(clock.now); + activity.note("chat", "assistant.turn_start"); + clock.onSleep = (t) => { + if (t === 60_000) activity.note("chat", "session.idle"); + }; + const { started, options } = restartOptions(clock); + assert.equal(await restartWhenIdle(session, activity, options), "restarting"); + assert.deepEqual( + started.map(({ at }) => at), + [60_000 + QUIET_MS], + ); +}); + +test("a session that never goes idle gives up after the bound and asks for a restart", async () => { + const clock = fakeClock(); + const session = fakeSession(); + const activity = createActivity(clock.now); + activity.note("chat", "user.message"); + const { started, options } = restartOptions(clock); + assert.equal(await restartWhenIdle(session, activity, options), "busy"); + assert.equal(clock.t, IDLE_TIMEOUT_MS); + assert.deepEqual(started, []); + assert.deepEqual(session.logs, [[RESTART_HINT, "info"]]); +}); + +test("events that keep coming hold the restart off even without a turn start", async () => { + const clock = fakeClock(); + const activity = createActivity(clock.now); + clock.onSleep = () => activity.note("chat", "assistant.streaming_delta"); + const { started, options } = restartOptions(clock); + assert.equal(await restartWhenIdle(fakeSession(), activity, options), "busy"); + assert.deepEqual(started, []); +}); + +test("when the app can't be restarted here, the chat gets the restart hint", async () => { + const clock = fakeClock(); + for (const overrides of [ + { plan: async () => null }, + { plan: async () => Promise.reject(new Error("boom")) }, + { start: async () => false }, + ]) { + const session = fakeSession(); + const { started, options } = restartOptions(clock, overrides); + const result = await restartWhenIdle(session, createActivity(clock.now), options); + assert.notEqual(result, "restarting"); + assert.deepEqual(started, []); + assert.deepEqual(session.logs.at(-1), [RESTART_HINT, "info"]); + } +}); + +test("a session whose log fails doesn't make the restart throw", async () => { + const clock = fakeClock(); + const { options } = restartOptions(clock, { plan: async () => null }); + const session = { + log: () => { + throw new Error("closed"); + }, + }; + assert.equal(await restartWhenIdle(session, createActivity(clock.now), options), "unsupported"); +}); + +test( + "a turn in a chat the app's window runs delays the restart", + { skip: typeof WebSocket === "function" ? false : "this Node has no global WebSocket" }, + async (t) => { + const app = new FakeApp(); + await app.listen(); + t.after(() => app.close()); + const clock = fakeClock(); + const activity = createActivity(clock.now); + const { started, options } = restartOptions(clock, { timeoutMs: 600_000 }); + app.writeRunFiles(options.home); + let watchers; + clock.onSleep = async (time) => { + if (time === 5_000) { + watchers = app.watchers.size; + app.emit("ui-chat", "assistant.turn_start"); + } + if (time === 90_000) app.emit("ui-chat", "session.idle"); + await delay(2); + }; + assert.equal(await restartWhenIdle(fakeSession(), activity, options), "restarting"); + assert.ok(started[0].at >= 90_000 + QUIET_MS, `restarted at ${started[0].at}`); + assert.ok(started[0].at <= 90_000 + QUIET_MS + 5_000, `restarted at ${started[0].at}`); + assert.equal(watchers, 1); + }, +); + +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 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); +}); + +function fakeProcess({ exitAfterPolls = 2, launches = [true] } = {}) { + const clock = fakeClock(); + const calls = []; + let closed = false; + let polls = 0; + let osascriptFails = false; + const outcomes = [...launches]; + return { + clock, + calls, + deps: { + now: clock.now, + sleep: clock.sleep, + alive: () => !(closed && ++polls > exitAfterPolls), + kill: (pid, signal) => { + calls.push(["kill", pid, signal]); + closed = true; + }, + run: async (command, args) => { + calls.push(["run", command, ...args]); + if (command === "/usr/bin/osascript" && osascriptFails) return null; + closed = true; + return ""; + }, + launch: async (command, args, env) => { + calls.push(["launch", command, args, env]); + return outcomes.shift() ?? false; + }, + env: { HOME: "/home/u", PATH: "/usr/bin", DISPLAY: ":0", GH_TOKEN: "secret", COPILOT_SDK_AUTH_TOKEN: "x" }, + }, + setOsascriptFails() { + osascriptFails = true; + }, + }; +} + +const CLEAN_ENV = { HOME: "/home/u", PATH: "/usr/bin", DISPLAY: ":0" }; + +test("on Linux the app gets SIGTERM, then is relaunched with the session's environment once it exits", async () => { + const fake = fakeProcess(); + assert.equal(await restartApp(TARGET, fake.deps), "relaunched"); + assert.deepEqual(fake.calls, [ + ["kill", 4242, "SIGTERM"], + ["launch", "/usr/bin/github", [], CLEAN_ENV], + ]); +}); + +test("a failed relaunch is retried once", async () => { + const twice = fakeProcess({ launches: [false, true] }); + assert.equal(await restartApp(TARGET, twice.deps), "relaunched"); + assert.equal(twice.calls.filter(([kind]) => kind === "launch").length, 2); + + const never = fakeProcess({ launches: [false, false, true] }); + assert.equal(await restartApp(TARGET, never.deps), "relaunch_failed"); + assert.equal(never.calls.filter(([kind]) => kind === "launch").length, 2); +}); + +test("an app that doesn't exit is left running, not relaunched", async () => { + const fake = fakeProcess({ exitAfterPolls: Infinity }); + assert.equal(await restartApp(TARGET, fake.deps), "still_running"); + assert.ok(fake.clock.t <= EXIT_TIMEOUT_MS + 2_000); + assert.deepEqual( + fake.calls.map(([kind]) => kind), + ["kill"], + ); +}); + +test("on macOS the app is asked to quit, then opened by its bundle id", async () => { + const target = { platform: "darwin", pid: 4242, app: "com.github.githubapp" }; + const fake = fakeProcess(); + assert.equal(await restartApp(target, fake.deps), "relaunched"); + assert.deepEqual(fake.calls, [ + ["run", "/usr/bin/osascript", "-e", 'quit app id "com.github.githubapp"'], + ["launch", "/usr/bin/open", ["-b", "com.github.githubapp"], CLEAN_ENV], + ]); + + const refused = fakeProcess(); + refused.setOsascriptFails(); + assert.equal(await restartApp(target, refused.deps), "relaunched"); + assert.deepEqual(refused.calls[1], ["kill", 4242, "SIGTERM"]); +}); + +test("on Windows the app's window is closed without /F, and an app left in the tray isn't relaunched", async () => { + const target = { platform: "win32", pid: 4242, app: "C:\\Program Files\\GitHub\\github.exe" }; + const fake = fakeProcess(); + assert.equal(await restartApp(target, fake.deps), "relaunched"); + assert.deepEqual(fake.calls[0], ["run", "taskkill", "/PID", "4242"]); + assert.deepEqual(fake.calls[1].slice(0, 3), ["launch", target.app, []]); + + const tray = fakeProcess({ exitAfterPolls: Infinity }); + assert.equal(await restartApp(target, tray.deps), "still_running"); + assert.ok(tray.calls.every(([kind, command]) => kind === "run" && command === "taskkill")); +}); + +test("sessionEnv keeps the user's session variables and drops the runtime's", () => { + const env = { + HOME: "/h", + PATH: "/usr/bin", + DISPLAY: ":0", + WAYLAND_DISPLAY: "wayland-0", + XDG_RUNTIME_DIR: "/run/user/1", + DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1/bus", + LC_ALL: "C.UTF-8", + https_proxy: "http://proxy", + COPILOT_HOME: "/h/.copilot", + COPILOT_SDK_AUTH_TOKEN: "x", + COPILOT_AGENT_SESSION_ID: "s", + COPILOT_PLUGIN_ROOT: "/p", + GH_TOKEN: "t", + GIT_CONFIG_COUNT: "1", + SESSION_ID: "s", + NODE_OPTIONS: "--x", + ELECTRON_RUN_AS_NODE: "1", + OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel", + }; + assert.deepEqual(Object.keys(sessionEnv(env)).sort(), [ + "COPILOT_HOME", + "DBUS_SESSION_BUS_ADDRESS", + "DISPLAY", + "HOME", + "LC_ALL", + "PATH", + "WAYLAND_DISPLAY", + "XDG_RUNTIME_DIR", + "https_proxy", + ]); +}); + +test("the helper logs the outcome and never throws", async () => { + const log = join(mkdtempSync(join(tmpdir(), "ug-copilot-state-")), "app-restart.log"); + const fake = fakeProcess(); + await runHelper(JSON.stringify({ ...TARGET, log }), fake.deps); + assert.match(readFileSync(log, "utf8"), /relaunched\n$/); + await runHelper("not json", fake.deps); + const failing = { ...fake.deps, kill: () => {}, alive: () => true, sleep: () => Promise.reject(new Error("boom")) }; + await runHelper(JSON.stringify({ ...TARGET, log }), failing); + assert.match(readFileSync(log, "utf8"), /failed: boom\n$/); +}); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 928f087ea..094ac824e 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -1,5 +1,5 @@ import assert from "node:assert/strict"; -import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; +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"; @@ -53,7 +53,7 @@ test("syncOnce writes the rows, then does nothing until they or the app launch c const { app, options } = await setup(t); app.providers.set("mine", { id: "mine", name: "Mine", kind: "custom" }); - assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + 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`]); @@ -71,6 +71,72 @@ test("syncOnce writes the rows, then does nothing until they or the app launch c 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); @@ -82,7 +148,7 @@ test("syncOnce deletes ug's stale providers and rows but no one else's", async ( providers: [providerRows(GPT, "gpt", ["gpt-6"]), providerRows(OPEN, "open", ["glm-5"])], ownedProviderIds: ROWS.ownedProviderIds, }; - assert.equal(await syncOnce({ app: next }, options), "synced"); + 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()); @@ -105,7 +171,7 @@ test("only one extension process syncs at a time; a stale lock is taken over", a const old = new Date(Date.now() - 120_000); utimesSync(lock, old, old); - assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); }); test("a reply for another request is skipped", async (t) => { @@ -115,7 +181,7 @@ test("a reply for another request is skipped", async (t) => { { type: "model_providers_list", providers: [{ id: "y" }] }, { type: "error", message: "boom", request_id: "ug-someone-else" }, ]; - assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); assert.equal(app.providers.size, 2); }); @@ -129,7 +195,7 @@ test("an error without a request_id answers the pending request", async (t) => { 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), "synced"); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); assert.equal(app.models.size, 2); }); @@ -146,7 +212,7 @@ test("failures are logged, never thrown, and the next run retries", async (t) => assert.match(logs[1], /closed the connection/); app.drop = null; - assert.equal(await syncOnce({ app: ROWS }, options), "synced"); + assert.equal(await syncOnce({ app: ROWS }, options), "changed"); }); test("a closed app or a stale token is not synced", async (t) => { diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 11babad7b..d766b3473 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -236,6 +236,8 @@ def test_layout(self, home): "ug-plugin.mjs", "app-rows.mjs", "app-remove.mjs", + "app-restart.mjs", + "restart-helper.mjs", "config.json", } assert (extension / "extension.mjs").read_text() == ( @@ -564,7 +566,15 @@ def test_missing_directory_is_not_installed(self, home): @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"] + "name", + [ + "extension.mjs", + "ug-plugin.mjs", + "app-rows.mjs", + "app-remove.mjs", + "app-restart.mjs", + "restart-helper.mjs", + ], ) def test_syntax(self, name): subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) @@ -575,6 +585,7 @@ def test_syntax(self, name): "copilot_app_plugin.test.mjs", "copilot_app_rows.test.mjs", "copilot_app_remove.test.mjs", + "copilot_app_restart.test.mjs", ], ) def test_node_unit_tests(self, name): From 450d62512d73ae156bb47bb3189e327cbca12665 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 10:50:11 +0000 Subject: [PATCH 10/26] copilot: run the Copilot app plugin's node tests in CI The pytest wrappers ran them only where the unit-test runner happened to have Node, so they could skip. They now run in the VS Code extension job, which sets up Node 22 (with the global WebSocket the extension uses) and is part of the required test check. The pytest syntax checks and the Python-to-extension removal test still skip without Node. Co-authored-by: Isaac --- .github/workflows/ci.yml | 4 ++++ tests/test_copilot_app_plugin.py | 17 ----------------- 2 files changed, 4 insertions(+), 17 deletions(-) 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/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index d766b3473..c60ada28c 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -579,23 +579,6 @@ class TestExtensionSource: def test_syntax(self, name): subprocess.run([NODE, "--check", str(PLUGIN_SOURCE / name)], check=True) - @pytest.mark.parametrize( - "name", - [ - "copilot_app_plugin.test.mjs", - "copilot_app_rows.test.mjs", - "copilot_app_remove.test.mjs", - "copilot_app_restart.test.mjs", - ], - ) - def test_node_unit_tests(self, name): - result = subprocess.run( - [NODE, "--test", str(Path(__file__).with_name(name))], - capture_output=True, - text=True, - ) - assert result.returncode == 0, result.stdout + result.stderr - def test_the_extension_finishes_a_deferred_remove_for_ug(self, home, tmp_path): root = plugin.install(_config()) plugin.defer_remove() From 2c83a12ebdc7b57b01513341c8df4d35fb5abc6d Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 10:55:22 +0000 Subject: [PATCH 11/26] copilot: name the Copilot app plugin unity-gateway@databricks-unity-gateway The plugin is now unity-gateway in the databricks-unity-gateway marketplace, under plugins/unity-gateway. Configure moves an earlier build's unity-gateway marketplace and databricks@unity-gateway key to the new id when their source is ug's plugin directory, and deletes the old plugin copy; status counts either as installed, and removal (also the extension's deferred one) takes out both. A marketplace of either name at another path is still left alone. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 100 ++++++++++++------ .../desktop/copilot_app_plugin/app-remove.mjs | 29 +++-- tests/copilot_app_remove.test.mjs | 22 +++- tests/test_copilot_app_plugin.py | 98 ++++++++++++++--- 4 files changed, 184 insertions(+), 65 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index f43c5a2fc..9a66636aa 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -26,9 +26,13 @@ from ucode.custom_oauth import CustomOAuthConfig, build_custom_auth_token_argv from ucode.databricks import build_auth_token_argv -MARKETPLACE = "unity-gateway" -PLUGIN = "databricks" +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" @@ -122,12 +126,12 @@ def plugin_root() -> Path: return Path.home() / ".ucode" / "copilot-app-plugin" -def _plugin_dir(root: Path) -> Path: - return root / "plugins" / PLUGIN +def _plugin_dir(root: Path, plugin: str = PLUGIN) -> Path: + return root / "plugins" / plugin -def _extension_dir(root: Path) -> Path: - return _plugin_dir(root) / "extensions" / EXTENSION +def _extension_dir(root: Path, plugin: str = PLUGIN) -> Path: + return _plugin_dir(root, plugin) / "extensions" / EXTENSION def _token_command(config: PluginConfig) -> list[str]: @@ -307,25 +311,57 @@ def _write_settings(path: Path, settings: dict) -> None: path.chmod(mode) -def _owns_marketplace(settings: dict, root: Path) -> bool: +def _owns_marketplace(settings: dict, root: Path, marketplace: str = MARKETPLACE) -> bool: marketplaces = settings.get(_MARKETPLACES_KEY) - marketplace = marketplaces.get(MARKETPLACE) if isinstance(marketplaces, dict) else None - source = marketplace.get("source") if isinstance(marketplace, dict) else None + 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 _owns_marketplace(settings, root) and (_extension_dir(root) / "extension.mjs").exists() + 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: - if _owns_marketplace(settings, root): - return True - marketplaces = settings.get(_MARKETPLACES_KEY) - if isinstance(marketplaces, dict) and MARKETPLACE in marketplaces: - return False - enabled = settings.get(_ENABLED_KEY) - return isinstance(enabled, dict) and PLUGIN_KEY in enabled + return bool(_ug_entries(settings, root)) def _read_extension_config(root: Path) -> dict | None: @@ -346,6 +382,7 @@ def enable(root: Path | None = None, settings_path: Path | None = None) -> None: 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)} } @@ -384,6 +421,8 @@ def install( 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 @@ -393,25 +432,14 @@ def remove( 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. A ``unity-gateway`` marketplace at another path isn't ug's, so it and its - plugin key stay. Returns whether anything was removed.""" + 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) - changed = False - marketplaces = settings.get(_MARKETPLACES_KEY) - foreign = ( - isinstance(marketplaces, dict) - and MARKETPLACE in marketplaces - and not _owns_marketplace(settings, root) - ) - for key, entry in ((_MARKETPLACES_KEY, MARKETPLACE), (_ENABLED_KEY, PLUGIN_KEY)): - value = settings.get(key) - if not foreign and isinstance(value, dict) and entry in value: - del value[entry] - if not value: - del settings[key] - changed = True + 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(): @@ -465,9 +493,11 @@ def status(root: Path | None = None, settings_path: Path | None = None) -> Statu settings = read_settings(settings_path) except SettingsError: return "invalid_settings" - if not _installed(settings, root): + 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(PLUGIN_KEY) is True: + 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 index e0e6b811b..fd601c942 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs @@ -13,8 +13,13 @@ import { tryLock, } from "./app-rows.mjs"; -export const MARKETPLACE = "unity-gateway"; -export const PLUGIN_KEY = "databricks@unity-gateway"; +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"; @@ -48,17 +53,19 @@ export function readSettings(path) { return { settings, mode: stat.mode & 0o777 }; } -// A unity-gateway marketplace at another path isn't ug's, so it and its plugin key stay. +// 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]; - 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, PLUGIN_KEY], - ].filter(([key, entry]) => isObject(settings[key]) && Object.hasOwn(settings[key], entry)); + 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) { diff --git a/tests/copilot_app_remove.test.mjs b/tests/copilot_app_remove.test.mjs index 353b2c22c..a680a3af2 100644 --- a/tests/copilot_app_remove.test.mjs +++ b/tests/copilot_app_remove.test.mjs @@ -28,6 +28,10 @@ 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 }, }; @@ -140,10 +144,22 @@ test("a symlinked settings file is refused", async (t) => { assert.throws(() => removeSettingsKeys(path, ROOT), /symlink/); }); -test("a foreign unity-gateway marketplace and its plugin key stay", async (t) => { +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: { "unity-gateway": { source: { source: "directory", path: "/elsewhere" } } }, - enabledPlugins: { "databricks@unity-gateway": true }, + 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"); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index c60ada28c..f80df122e 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -224,13 +224,13 @@ 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"] == "unity-gateway" - assert marketplace["plugins"][0]["source"] == "./plugins/databricks" + assert marketplace["name"] == "databricks-unity-gateway" + assert marketplace["plugins"][0]["source"] == "./plugins/unity-gateway" manifest = json.loads( - (root / "plugins" / "databricks" / ".plugin" / "plugin.json").read_text() + (root / "plugins" / "unity-gateway" / ".plugin" / "plugin.json").read_text() ) - assert manifest["name"] == "databricks" - extension = root / "plugins" / "databricks" / "extensions" / "databricks" + 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", @@ -252,7 +252,7 @@ def test_rewrite_replaces_config(self, home): root = plugin.write_plugin(_config(profile="other")) config = json.loads( ( - root / "plugins" / "databricks" / "extensions" / "databricks" / "config.json" + root / "plugins" / "unity-gateway" / "extensions" / "databricks" / "config.json" ).read_text() ) assert config["tokenCommand"][-1] == "other" @@ -273,9 +273,12 @@ def test_install_merges_only_ug_keys(self, home): assert settings["model"] == "gpt-5" assert settings["extraKnownMarketplaces"] == { "other": {"source": {"source": "github", "repo": "o/r"}}, - "unity-gateway": {"source": {"source": "directory", "path": str(root)}}, + "databricks-unity-gateway": {"source": {"source": "directory", "path": str(root)}}, + } + assert settings["enabledPlugins"] == { + "x@other": False, + "unity-gateway@databricks-unity-gateway": True, } - assert settings["enabledPlugins"] == {"x@other": False, "databricks@unity-gateway": True} assert "telemetry" not in settings def test_install_creates_settings(self, home): @@ -382,9 +385,11 @@ def test_remove_keeps_a_foreign_unity_gateway_marketplace(self, home): path.parent.mkdir() foreign = { "extraKnownMarketplaces": { - "unity-gateway": {"source": {"source": "directory", "path": "/elsewhere"}} + "databricks-unity-gateway": { + "source": {"source": "directory", "path": "/elsewhere"} + } }, - "enabledPlugins": {"databricks@unity-gateway": True}, + "enabledPlugins": {"unity-gateway@databricks-unity-gateway": True}, } path.write_text(json.dumps(foreign)) assert plugin.remove(wait=lambda seconds: None) is False @@ -427,14 +432,16 @@ def test_remove_with_invalid_settings_changes_nothing(self, home): def _extension_config(root: Path) -> dict: return json.loads( - (root / "plugins" / "databricks" / "extensions" / "databricks" / "config.json").read_text() + ( + 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"]["unity-gateway"] - del settings["enabledPlugins"]["databricks@unity-gateway"] + 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})) @@ -485,7 +492,7 @@ 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"]["databricks@unity-gateway"] = False + settings["enabledPlugins"]["unity-gateway@databricks-unity-gateway"] = False _settings(home).write_text(json.dumps(settings)) assert plugin.status() == "disabled" @@ -553,7 +560,7 @@ def test_disabled(self, home): plugin.install(_config()) path = _settings(home) settings = json.loads(path.read_text()) - settings["enabledPlugins"]["databricks@unity-gateway"] = False + settings["enabledPlugins"]["unity-gateway@databricks-unity-gateway"] = False path.write_text(json.dumps(settings)) assert plugin.status() == "disabled" @@ -582,7 +589,7 @@ def test_syntax(self, name): def test_the_extension_finishes_a_deferred_remove_for_ug(self, home, tmp_path): root = plugin.install(_config()) plugin.defer_remove() - extension = root / "plugins" / "databricks" / "extensions" / "databricks" + 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())};" @@ -607,3 +614,62 @@ def test_the_extension_finishes_a_deferred_remove_for_ug(self, home, tmp_path): 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_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" + ) From 1f43107b891035d9e19e2e0f529659639a85b841 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 10:55:47 +0000 Subject: [PATCH 12/26] copilot: send the app session's id as session-id on plugin model requests Like ug copilot, every model request from the GitHub Copilot app now carries session-id, so the gateway's usage table groups an app chat's turns. It's the session the extension process serves (SESSION_ID, which joinSession resumes), not a random id; an admin header of that name wins. Subagent requests get no thread-id: the SDK's provider headers are static and it has no per-request header hook. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/ug-plugin.mjs | 20 +++++++++- tests/copilot_app_plugin.test.mjs | 37 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs index 321ebd4cb..fa869be3b 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs @@ -122,6 +122,22 @@ export function withLoginHint(token, showHint) { }; } -export function buildProviders(config, bearerTokenProvider) { - return config.providers.map((provider) => ({ ...provider, bearerTokenProvider })); +// The gateway logs it as the usage table's client session id. An admin's header of that name wins. +export const SESSION_ID_HEADER = "session-id"; + +export function withSessionId(headers = {}, sessionId) { + if (!sessionId || Object.keys(headers).some((name) => name.toLowerCase() === SESSION_ID_HEADER)) { + return { ...headers }; + } + return { [SESSION_ID_HEADER]: sessionId, ...headers }; +} + +// ``sessionId`` is the app session this extension process serves, which the runtime passes in +// SESSION_ID: joinSession resumes that session. +export function buildProviders(config, bearerTokenProvider, sessionId = process.env.SESSION_ID) { + return config.providers.map((provider) => ({ + ...provider, + headers: withSessionId(provider.headers, sessionId), + bearerTokenProvider, + })); } diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index d90fdabe2..b88836375 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -6,6 +6,7 @@ import { DEFAULT_TOKEN_TTL_MS, FORCE_REFRESH_FLAG, REFRESH_MARGIN_MS, + SESSION_ID_HEADER, buildProviders, childEnv, createTokenSource, @@ -13,6 +14,7 @@ import { isAuthRejection, jwtExpiryMs, withLoginHint, + withSessionId, } from "../src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs"; const config = { @@ -166,7 +168,7 @@ test("an invalidate during a mint keeps its token out of the cache", async () => test("buildProviders attaches the token provider to every provider", () => { const provide = async () => "t"; - const [provider] = buildProviders(config, provide); + 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); @@ -199,3 +201,36 @@ test("isAuthRejection matches only 401 errors and call failures", () => { 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, ""), {}); +}); From d7459994add2891007ab1019c35f6f0205f60269 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 11:18:05 +0000 Subject: [PATCH 13/26] copilot: find the GitHub Copilot app's pid in app-owner.pid App 1.4.2 writes only the port to run/ws.port and its own pid to run/app-owner.pid, so the restart never found the app and only posted the hint. The pid now comes from app-owner.pid, with ws.port's second field as a fallback. The restart stays one attempt per change: a launch after a failed one already loads the rows' labels, so it needs no retry. Co-authored-by: Isaac --- .../agents/desktop/copilot_app_plugin/app-rows.mjs | 11 ++++++++--- tests/copilot_app_fake.mjs | 5 +++-- tests/copilot_app_restart.test.mjs | 11 +++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index 71cddae37..eb582b10a 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -13,7 +13,7 @@ export function copilotHome(env = process.env) { return env.COPILOT_HOME || join(homedir(), ".copilot"); } -// Each run file holds the value, then the app's pid. +// 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+/); } @@ -22,15 +22,20 @@ function runValue(home, name) { return runFields(home, name)[0] || null; } -export function readAppPid(home) { +function pidField(home, name, index) { try { - const pid = Number.parseInt(runFields(home, "ws.port")[1], 10); + 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); diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs index 859659aaf..8a199fa76 100644 --- a/tests/copilot_app_fake.mjs +++ b/tests/copilot_app_fake.mjs @@ -79,8 +79,9 @@ export class FakeApp { writeRunFiles(home) { mkdirSync(join(home, "run"), { recursive: true }); - writeFileSync(join(home, "run", "ws.port"), `${this.port}\n4242\n`); - writeFileSync(join(home, "run", "ws.token"), `${this.token}\n4242\n`); + 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) { diff --git a/tests/copilot_app_restart.test.mjs b/tests/copilot_app_restart.test.mjs index 1d5249dbd..465269a6b 100644 --- a/tests/copilot_app_restart.test.mjs +++ b/tests/copilot_app_restart.test.mjs @@ -187,6 +187,17 @@ test("planRestart takes the app's pid from its run file", async () => { 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; From 4ef47c6f37c1f65a24fd6861f7443afa87ce9439 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 11:38:11 +0000 Subject: [PATCH 14/26] copilot: wait for idle in the detached restart helper The app stops the extension process a few seconds after the sync that changed its rows, so the idle wait and restart inside it never ran. The extension now starts the detached helper as soon as the sync returns (before the session is joined) and posts one line saying the app restarts once no chat is busy. The helper opens its own watch of the app's session events, waits for idle plus the quiet window, then restarts the app. Without the session events, or when the app closes while it waits, it gives up and leaves the app as it is. Co-authored-by: Isaac --- .../copilot_app_plugin/app-restart.mjs | 69 +++--- .../desktop/copilot_app_plugin/extension.mjs | 12 +- tests/copilot_app_restart.test.mjs | 220 +++++++++--------- 3 files changed, 160 insertions(+), 141 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs index 571ef0f6c..14d8a4024 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs @@ -7,7 +7,8 @@ import { fileURLToPath } from "node:url"; import { connectApp, copilotHome, readAppPid, readEndpoint } from "./app-rows.mjs"; -export const RESTARTING_MESSAGE = "Restarting the GitHub Copilot app once to finish loading the Databricks models."; +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"; export const QUIET_MS = 15_000; @@ -41,12 +42,13 @@ export function createActivity(now = Date.now) { export async function waitForIdle( activity, - { quietMs = QUIET_MS, timeoutMs = IDLE_TIMEOUT_MS, now = Date.now, sleep = delay } = {}, + { quietMs = QUIET_MS, timeoutMs = IDLE_TIMEOUT_MS, now = Date.now, sleep = delay, stopped = () => false } = {}, ) { const deadline = now() + timeoutMs; for (;;) { - if (activity.idle(quietMs)) return true; - if (now() >= deadline) return false; + if (stopped()) return "stopped"; + if (activity.idle(quietMs)) return "idle"; + if (now() >= deadline) return "busy"; await sleep(IDLE_POLL_MS); } } @@ -105,37 +107,46 @@ export function startHelper(target, stateDir) { }); } -async function tryRestart( - activity, - say, - { home = copilotHome(), stateDir, plan = planRestart, start = startHelper, now, sleep, quietMs, timeoutMs, ...connect }, +// The app stops the extension process soon after its rows change, so the wait for idle and the +// restart run in a detached helper, started as soon as the sync returns. Never throws. +export async function launchRestart({ home = copilotHome(), stateDir, plan = planRestart, start = startHelper } = {}) { + try { + const target = await plan({ home }); + if (!target) return "unsupported"; + return (await start({ ...target, home }, stateDir)) ? "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 {} +} + +// In the helper: waits until no chat in the app is running a turn, then restarts it. A turn is only +// visible on the app's session channels, so without them, or once the app closes, it gives up. +export async function restartWhenIdle( + target, + { now = Date.now, sleep = delay, quietMs, timeoutMs, WebSocketImpl, ...deps } = {}, ) { - const target = await plan({ home }); - if (!target) return "unsupported"; - const endpoint = readEndpoint(home); + const endpoint = readEndpoint(target.home ?? copilotHome()); + if (!endpoint) return "not_running"; + const activity = createActivity(now); const onMessage = (message) => { if (message.type === "session_event") activity.note(message.session_id, message.event?.type); }; - // Without this connection, only this session's own turns are known. - const watch = endpoint ? await connectApp(endpoint, { ...connect, onMessage }).catch(() => null) : null; + const watch = await connectApp(endpoint, { WebSocketImpl, onMessage }).catch(() => null); + if (!watch) return "no_session_events"; + let waited; try { - if (!(await waitForIdle(activity, { quietMs, timeoutMs, now, sleep }))) return "busy"; + waited = await waitForIdle(activity, { quietMs, timeoutMs, now, sleep, stopped: () => watch.closed }); } finally { - watch?.close(); + watch.close(); } - await say(RESTARTING_MESSAGE); - return (await start(target, stateDir)) ? "restarting" : "failed"; -} - -// Restarts the app once no chat is running a turn, or asks the user to restart it. Never throws. -export async function restartWhenIdle(session, activity, options = {}) { - const say = (message) => - Promise.resolve() - .then(() => session.log(message, { level: "info" })) - .catch(() => {}); - const result = await tryRestart(activity, say, options).catch(() => "failed"); - if (result !== "restarting") await say(RESTART_HINT); - return result; + return waited === "idle" ? restartApp(target, { now, sleep, ...deps }) : waited; } // The extension has the Copilot runtime's environment, which adds the runtime's own settings and @@ -240,7 +251,7 @@ export async function runHelper(argument, deps) { } catch {} }; try { - log(await restartApp(target, deps)); + log(await restartWhenIdle(target, deps)); } catch (error) { log(`failed: ${error?.message ?? error}`); } diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index e61feb60e..db1d7ce00 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -3,7 +3,7 @@ import { fileURLToPath } from "node:url"; import { joinSession } from "@github/copilot-sdk/extension"; import { removeInBackground } from "./app-remove.mjs"; -import { createActivity, restartWhenIdle } from "./app-restart.mjs"; +import { launchRestart, tellRestart } from "./app-restart.mjs"; import { syncInBackground } from "./app-rows.mjs"; import { buildProviders, @@ -30,6 +30,8 @@ if (config.remove) { if (retry(await firstRemove)) await removeInBackground(config, syncOptions, logSyncFailure); } else { const firstSync = syncInBackground(config, syncOptions, logSyncFailure); + const restartFor = (synced) => (synced === "changed" ? launchRestart(syncOptions) : null); + const firstRestart = firstSync.then(restartFor); let session; const tokenSource = createTokenSource(config); const bearerTokenProvider = withLoginHint(tokenSource, (error) => { @@ -42,15 +44,13 @@ if (config.remove) { providers: buildProviders(config, bearerTokenProvider), models: config.models, }); - const activity = createActivity(); session.on((event) => { - activity.note(session.sessionId, event.type); 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") session.log(config.loginHint, { level: "error" }); }); - let synced = await firstSync; - if (retry(synced)) synced = await syncInBackground(config, syncOptions, logSyncFailure); - if (synced === "changed") await restartWhenIdle(session, activity, syncOptions); + let restart = await firstRestart; + if (retry(await firstSync)) restart = await restartFor(await syncInBackground(config, syncOptions, logSyncFailure)); + if (restart) await tellRestart(session, restart); } diff --git a/tests/copilot_app_restart.test.mjs b/tests/copilot_app_restart.test.mjs index 465269a6b..57a2ef123 100644 --- a/tests/copilot_app_restart.test.mjs +++ b/tests/copilot_app_restart.test.mjs @@ -11,11 +11,12 @@ import { QUIET_MS, RESTART_HINT, RESTARTING_MESSAGE, - createActivity, + launchRestart, planRestart, restartApp, restartWhenIdle, runHelper, + tellRestart, sessionEnv, } from "../src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs"; import { FakeApp } from "./copilot_app_fake.mjs"; @@ -32,11 +33,6 @@ function fakeClock() { return clock; } -function fakeSession() { - const logs = []; - return { logs, log: async (message, options) => logs.push([message, options.level]) }; -} - function emptyHome() { return mkdtempSync(join(tmpdir(), "ug-copilot-home-")); } @@ -48,122 +44,132 @@ function homeWithPid(portFile) { return home; } -function restartOptions(clock, overrides = {}) { - const started = []; - return { - started, - options: { - home: emptyHome(), - stateDir: mkdtempSync(join(tmpdir(), "ug-copilot-state-")), - plan: async () => TARGET, - start: async (target) => (started.push({ target, at: clock.t }), true), - now: clock.now, - sleep: clock.sleep, - ...overrides, - }, - }; +const withWebSocket = { skip: typeof WebSocket === "function" ? false : "this Node has no global WebSocket" }; + +function fakeSession() { + const logs = []; + return { logs, log: async (message, options) => logs.push([message, options.level]) }; } -test("an idle app restarts after a quiet period, with one line in the chat first", async () => { - const clock = fakeClock(); +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, + plan: async () => TARGET, + start: async (target, dir) => (started.push([target, dir]), true), + }); + assert.equal(result, "started"); + assert.deepEqual(started, [[{ ...TARGET, home }, stateDir]]); const session = fakeSession(); - const activity = createActivity(clock.now); - const { started, options } = restartOptions(clock); - assert.equal(await restartWhenIdle(session, activity, options), "restarting"); - assert.deepEqual(started, [{ target: TARGET, at: QUIET_MS }]); + await tellRestart(session, result); assert.deepEqual(session.logs, [[RESTARTING_MESSAGE, "info"]]); }); -test("a running turn delays the restart until its session is idle", async () => { - const clock = fakeClock(); - const session = fakeSession(); - const activity = createActivity(clock.now); - activity.note("chat", "assistant.turn_start"); - clock.onSleep = (t) => { - if (t === 60_000) activity.note("chat", "session.idle"); - }; - const { started, options } = restartOptions(clock); - assert.equal(await restartWhenIdle(session, activity, options), "restarting"); - assert.deepEqual( - started.map(({ at }) => at), - [60_000 + QUIET_MS], - ); -}); - -test("a session that never goes idle gives up after the bound and asks for a restart", async () => { - const clock = fakeClock(); - const session = fakeSession(); - const activity = createActivity(clock.now); - activity.note("chat", "user.message"); - const { started, options } = restartOptions(clock); - assert.equal(await restartWhenIdle(session, activity, options), "busy"); - assert.equal(clock.t, IDLE_TIMEOUT_MS); - assert.deepEqual(started, []); - assert.deepEqual(session.logs, [[RESTART_HINT, "info"]]); -}); - -test("events that keep coming hold the restart off even without a turn start", async () => { - const clock = fakeClock(); - const activity = createActivity(clock.now); - clock.onSleep = () => activity.note("chat", "assistant.streaming_delta"); - const { started, options } = restartOptions(clock); - assert.equal(await restartWhenIdle(fakeSession(), activity, options), "busy"); - assert.deepEqual(started, []); -}); - -test("when the app can't be restarted here, the chat gets the restart hint", async () => { - const clock = fakeClock(); - for (const overrides of [ +test("when the helper can't restart the app here, the chat gets the restart hint", async () => { + for (const options of [ { plan: async () => null }, { plan: async () => Promise.reject(new Error("boom")) }, - { start: async () => false }, + { plan: async () => TARGET, start: async () => false }, ]) { + const result = await launchRestart({ home: emptyHome(), stateDir: emptyHome(), ...options }); + assert.notEqual(result, "started"); const session = fakeSession(); - const { started, options } = restartOptions(clock, overrides); - const result = await restartWhenIdle(session, createActivity(clock.now), options); - assert.notEqual(result, "restarting"); - assert.deepEqual(started, []); - assert.deepEqual(session.logs.at(-1), [RESTART_HINT, "info"]); + await tellRestart(session, result); + assert.deepEqual(session.logs, [[RESTART_HINT, "info"]]); } + await tellRestart({ log: () => { throw new Error("closed"); } }, "failed"); }); -test("a session whose log fails doesn't make the restart throw", async () => { - const clock = fakeClock(); - const { options } = restartOptions(clock, { plan: async () => null }); - const session = { - log: () => { - throw new Error("closed"); - }, +async function helperRun(t, script, overrides = {}, seen = () => {}) { + const app = new FakeApp(); + await app.listen(); + t.after(() => app.close()); + const home = emptyHome(); + app.writeRunFiles(home); + const fake = fakeProcess(); + seen(fake); + const clock = fake.clock; + clock.onSleep = async (time) => { + await script(time, app); + await delay(2); }; - assert.equal(await restartWhenIdle(session, createActivity(clock.now), options), "unsupported"); + const result = await restartWhenIdle({ ...TARGET, home }, { ...fake.deps, ...overrides }); + return { result, fake, clock, app }; +} + +test("the helper restarts an idle app after a quiet period", withWebSocket, async (t) => { + let restartAt; + const { result, fake } = await helperRun(t, async (time) => { + restartAt ??= time >= QUIET_MS ? time : undefined; + }); + assert.equal(result, "relaunched"); + assert.equal(restartAt, QUIET_MS); + assert.deepEqual(fake.calls[0], ["kill", 4242, "SIGTERM"]); }); -test( - "a turn in a chat the app's window runs delays the restart", - { skip: typeof WebSocket === "function" ? false : "this Node has no global WebSocket" }, - async (t) => { - const app = new FakeApp(); - await app.listen(); - t.after(() => app.close()); - const clock = fakeClock(); - const activity = createActivity(clock.now); - const { started, options } = restartOptions(clock, { timeoutMs: 600_000 }); - app.writeRunFiles(options.home); - let watchers; - clock.onSleep = async (time) => { +test("a turn in any chat delays the restart until that chat is idle", withWebSocket, async (t) => { + let watchers; + let fake; + const kill = (pid, signal) => fake.deps.killAt.push(fake.clock.t); + const run = helperRun( + t, + async (time, app) => { if (time === 5_000) { watchers = app.watchers.size; app.emit("ui-chat", "assistant.turn_start"); } if (time === 90_000) app.emit("ui-chat", "session.idle"); - await delay(2); - }; - assert.equal(await restartWhenIdle(fakeSession(), activity, options), "restarting"); - assert.ok(started[0].at >= 90_000 + QUIET_MS, `restarted at ${started[0].at}`); - assert.ok(started[0].at <= 90_000 + QUIET_MS + 5_000, `restarted at ${started[0].at}`); - assert.equal(watchers, 1); - }, -); + }, + { kill }, + (process) => { + fake = process; + fake.deps.killAt = []; + }, + ); + const { result } = await run; + assert.equal(watchers, 1); + assert.equal(result, "still_running"); + assert.ok(fake.deps.killAt[0] >= 90_000 + QUIET_MS, `killed at ${fake.deps.killAt[0]}`); + assert.ok(fake.deps.killAt[0] <= 90_000 + QUIET_MS + 5_000, `killed at ${fake.deps.killAt[0]}`); +}); + +test("a chat that never goes idle makes the helper give up", withWebSocket, async (t) => { + const { result, fake, clock } = await helperRun(t, async (time, app) => { + if (time === 1_000) app.emit("ui-chat", "user.message"); + }); + assert.equal(result, "busy"); + assert.equal(clock.t, IDLE_TIMEOUT_MS); + assert.deepEqual(fake.calls, []); +}); + +test("events that keep coming hold the restart off even without a turn start", withWebSocket, async (t) => { + const { result, fake } = await helperRun(t, async (time, app) => app.emit("chat", "assistant.streaming_delta")); + assert.equal(result, "busy"); + assert.deepEqual(fake.calls, []); +}); + +test("an app that closes while the helper waits isn't relaunched", withWebSocket, async (t) => { + const { result, fake } = await helperRun(t, async (time, app) => { + if (time === 1_000) { + app.emit("chat", "user.message"); + app.close(); + } + }); + assert.equal(result, "stopped"); + assert.deepEqual(fake.calls, []); +}); + +test("without the app's session events the helper gives up", async () => { + const fake = fakeProcess(); + assert.equal(await restartWhenIdle({ ...TARGET, home: emptyHome() }, fake.deps), "not_running"); + const home = homeWithPid("1"); + writeFileSync(join(home, "run", "ws.token"), "t"); + assert.equal(await restartWhenIdle({ ...TARGET, home }, fake.deps), "no_session_events"); + assert.deepEqual(fake.calls, []); +}); test("planRestart takes the app's pid from its run file", async () => { const alive = (pid) => pid === 4242; @@ -343,10 +349,12 @@ test("sessionEnv keeps the user's session variables and drops the runtime's", () test("the helper logs the outcome and never throws", async () => { const log = join(mkdtempSync(join(tmpdir(), "ug-copilot-state-")), "app-restart.log"); const fake = fakeProcess(); - await runHelper(JSON.stringify({ ...TARGET, log }), fake.deps); - assert.match(readFileSync(log, "utf8"), /relaunched\n$/); + await runHelper(JSON.stringify({ ...TARGET, home: emptyHome(), log }), fake.deps); + assert.match(readFileSync(log, "utf8"), /not_running\n$/); await runHelper("not json", fake.deps); - const failing = { ...fake.deps, kill: () => {}, alive: () => true, sleep: () => Promise.reject(new Error("boom")) }; - await runHelper(JSON.stringify({ ...TARGET, log }), failing); - assert.match(readFileSync(log, "utf8"), /failed: boom\n$/); + const home = homeWithPid("1"); + writeFileSync(join(home, "run", "ws.token"), "t"); + const failing = { ...fake.deps, WebSocketImpl: class { constructor() { throw new Error("boom"); } } }; + await runHelper(JSON.stringify({ ...TARGET, home, log }), failing); + assert.match(readFileSync(log, "utf8"), /no_session_events\n$/); }); From d31f44f9fb7a9265a4a5fc626d2f535bdbe3a83c Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 11:48:37 +0000 Subject: [PATCH 15/26] copilot: start the Copilot app restart through ug In the app, the extension's process.execPath is the Copilot runtime's own binary, which refuses to run a script, so the detached restart helper exited at once. The extension now passes the app's target to a detached `ug copilot-app-restart`, using the absolute ug path its config now carries; ug watches the app's session events, waits for idle and restarts the app. The JavaScript watch and restart code goes away. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 2 +- .../copilot_app_plugin/app-restart.mjs | 199 ++------------ .../desktop/copilot_app_plugin/app-rows.mjs | 15 +- .../desktop/copilot_app_plugin/extension.mjs | 2 +- .../copilot_app_plugin/restart-helper.mjs | 3 - tests/copilot_app_fake.mjs | 8 - tests/copilot_app_restart.test.mjs | 260 +----------------- tests/test_copilot_app_plugin.py | 3 +- 8 files changed, 29 insertions(+), 463 deletions(-) delete mode 100644 src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 9a66636aa..a60fb8d8a 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -47,7 +47,6 @@ "app-rows.mjs", "app-remove.mjs", "app-restart.mjs", - "restart-helper.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. @@ -218,6 +217,7 @@ def extension_config(config: PluginConfig) -> dict: 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), diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs index 14d8a4024..a126a0b32 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs @@ -1,57 +1,16 @@ import { execFile, spawn } from "node:child_process"; -import { appendFileSync, existsSync } from "node:fs"; -import { homedir } from "node:os"; +import { existsSync } from "node:fs"; import { join } from "node:path"; -import { setTimeout as delay } from "node:timers/promises"; -import { fileURLToPath } from "node:url"; -import { connectApp, copilotHome, readAppPid, readEndpoint } from "./app-rows.mjs"; +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"; -export const QUIET_MS = 15_000; -export const IDLE_TIMEOUT_MS = 180_000; -export const EXIT_TIMEOUT_MS = 15_000; -const IDLE_POLL_MS = 1_000; -const EXIT_POLL_MS = 100; -const CLOSE_DELAY_MS = 1_000; -const RELAUNCH_RETRY_MS = 2_000; -const LAUNCH_CHECK_MS = 3_000; const COMMAND_TIMEOUT_MS = 10_000; const LINUX_APP = "/usr/bin/github"; const MAC_BUNDLE_ID = "com.github.githubapp"; -const HELPER = fileURLToPath(new URL("./restart-helper.mjs", import.meta.url)); -const TURN_STARTS = new Set(["user.message", "assistant.turn_start"]); - -// A running turn sends events all the time, so the app is idle once no turn is open and no session -// has sent anything for a while. That also covers a turn that started before the watch did. -export function createActivity(now = Date.now) { - const running = new Set(); - let last = now(); - return { - note(sessionId, type) { - last = now(); - if (type === "session.idle") running.delete(sessionId); - else if (TURN_STARTS.has(type)) running.add(sessionId); - }, - idle: (quietMs) => running.size === 0 && now() - last >= quietMs, - }; -} - -export async function waitForIdle( - activity, - { quietMs = QUIET_MS, timeoutMs = IDLE_TIMEOUT_MS, now = Date.now, sleep = delay, stopped = () => false } = {}, -) { - const deadline = now() + timeoutMs; - for (;;) { - if (stopped()) return "stopped"; - if (activity.idle(quietMs)) return "idle"; - if (now() >= deadline) return "busy"; - await sleep(IDLE_POLL_MS); - } -} export function isAlive(pid) { try { @@ -71,7 +30,7 @@ function runCommand(command, args) { }); } -// What restartApp needs to close the app and start it again, or null when it can't here. +// 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, @@ -94,11 +53,17 @@ export async function planRestart({ return null; } -// The extension exits with the app, so the restart runs in a detached process of its own. -export function startHelper(target, stateDir) { +// 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(process.execPath, [HELPER, argument], { detached: true, stdio: "ignore", windowsHide: true }); + const child = spawn(ugPath, ["copilot-app-restart", argument], { + detached: true, + stdio: "ignore", + windowsHide: true, + }); child.once("spawn", () => { child.unref(); resolve(true); @@ -107,13 +72,12 @@ export function startHelper(target, stateDir) { }); } -// The app stops the extension process soon after its rows change, so the wait for idle and the -// restart run in a detached helper, started as soon as the sync returns. Never throws. -export async function launchRestart({ home = copilotHome(), stateDir, plan = planRestart, start = startHelper } = {}) { +// 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 = await plan({ home }); + const target = ugPath ? await plan({ home }) : null; if (!target) return "unsupported"; - return (await start({ ...target, home }, stateDir)) ? "started" : "failed"; + return (await start({ ...target, home }, stateDir, ugPath)) ? "started" : "failed"; } catch { return "failed"; } @@ -125,134 +89,3 @@ export async function tellRestart(session, result) { await session.log(result === "started" ? RESTARTING_MESSAGE : RESTART_HINT, { level: "info" }); } catch {} } - -// In the helper: waits until no chat in the app is running a turn, then restarts it. A turn is only -// visible on the app's session channels, so without them, or once the app closes, it gives up. -export async function restartWhenIdle( - target, - { now = Date.now, sleep = delay, quietMs, timeoutMs, WebSocketImpl, ...deps } = {}, -) { - const endpoint = readEndpoint(target.home ?? copilotHome()); - if (!endpoint) return "not_running"; - const activity = createActivity(now); - const onMessage = (message) => { - if (message.type === "session_event") activity.note(message.session_id, message.event?.type); - }; - const watch = await connectApp(endpoint, { WebSocketImpl, onMessage }).catch(() => null); - if (!watch) return "no_session_events"; - let waited; - try { - waited = await waitForIdle(activity, { quietMs, timeoutMs, now, sleep, stopped: () => watch.closed }); - } finally { - watch.close(); - } - return waited === "idle" ? restartApp(target, { now, sleep, ...deps }) : waited; -} - -// The extension has the Copilot runtime's environment, which adds the runtime's own settings and -// credentials. The app gets only the user's session environment back. -const SESSION_ENV = new Set([ - "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", -]); -const SESSION_ENV_PREFIXES = ["LC_", "XDG_", "PROCESSOR_"]; - -export function sessionEnv(env) { - return Object.fromEntries( - Object.entries(env).filter(([name]) => { - const key = name.toUpperCase(); - return SESSION_ENV.has(key) || SESSION_ENV_PREFIXES.some((prefix) => key.startsWith(prefix)); - }), - ); -} - -function launchDetached(command, args, env) { - return new Promise((resolve) => { - let child; - try { - child = spawn(command, args, { cwd: homedir(), detached: true, env, stdio: "ignore" }); - } catch { - resolve(false); - return; - } - // A launch that fails exits at once and nonzero. - const timer = setTimeout(() => { - child.unref(); - resolve(true); - }, LAUNCH_CHECK_MS); - child.once("error", () => { - clearTimeout(timer); - resolve(false); - }); - child.once("exit", (code) => { - clearTimeout(timer); - resolve(code === 0); - }); - }); -} - -function terminate(kill, pid) { - try { - kill(pid, "SIGTERM"); - } catch {} -} - -// Closes the app and starts it again once it has exited. It's never forced closed, so an app that -// stays up is left running. -export async function restartApp( - { platform, pid, app }, - { - alive = isAlive, - kill = (target, signal) => process.kill(target, signal), - run = runCommand, - launch = launchDetached, - now = Date.now, - sleep = delay, - env = process.env, - } = {}, -) { - await sleep(CLOSE_DELAY_MS); - if (platform === "darwin") { - if ((await run("/usr/bin/osascript", ["-e", `quit app id "${app}"`])) === null) terminate(kill, pid); - } else if (platform === "win32") { - // Without /F, taskkill closes the app's window as its close button does, which can leave the - // app running in the tray. - await run("taskkill", ["/PID", String(pid)]); - } else { - terminate(kill, pid); - } - const deadline = now() + EXIT_TIMEOUT_MS; - while (alive(pid)) { - if (now() >= deadline) return "still_running"; - await sleep(EXIT_POLL_MS); - } - const [command, args] = platform === "darwin" ? ["/usr/bin/open", ["-b", app]] : [app, []]; - for (let attempt = 0; attempt < 2; attempt++) { - if (attempt) await sleep(RELAUNCH_RETRY_MS); - if (await launch(command, args, sessionEnv(env))) return "relaunched"; - } - return "relaunch_failed"; -} - -export async function runHelper(argument, deps) { - let target; - try { - target = JSON.parse(argument); - } catch { - return; - } - const log = (line) => { - try { - appendFileSync(target.log, `${new Date().toISOString()} ${line}\n`); - } catch {} - }; - try { - log(await restartWhenIdle(target, deps)); - } catch (error) { - log(`failed: ${error?.message ?? error}`); - } -} diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index eb582b10a..179edc4f4 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -49,10 +49,9 @@ export function readEndpoint(home) { export class AppError extends Error {} class AppConnection { - constructor(ws, timeoutMs, onMessage) { + constructor(ws, timeoutMs) { this._ws = ws; this._timeoutMs = timeoutMs; - this._onMessage = onMessage; this._backlog = []; this._waiter = null; this.closed = false; @@ -76,10 +75,6 @@ class AppConnection { const waiter = this._waiter; this._waiter = null; waiter.resolve(message); - } else if (this._onMessage) { - try { - this._onMessage(message); - } catch {} } else { this._backlog.push(message); } @@ -132,15 +127,13 @@ class AppConnection { } } -// With ``onMessage``, the connection turns session channels on and passes it every message no -// request is waiting for. The app then sends every session's events, including its window's chats. -export async function connectApp(endpoint, { WebSocketImpl, timeoutMs = REQUEST_TIMEOUT_MS, onMessage } = {}) { +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=${Boolean(onMessage)}`; + 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, onMessage); + const app = new AppConnection(ws, timeoutMs); try { await app.wait((message) => message.type === "server_hello"); } catch (error) { diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index db1d7ce00..f470a83ca 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -30,7 +30,7 @@ if (config.remove) { if (retry(await firstRemove)) await removeInBackground(config, syncOptions, logSyncFailure); } else { const firstSync = syncInBackground(config, syncOptions, logSyncFailure); - const restartFor = (synced) => (synced === "changed" ? launchRestart(syncOptions) : null); + const restartFor = (synced) => (synced === "changed" ? launchRestart({ ...syncOptions, ugPath: config.ugPath }) : null); const firstRestart = firstSync.then(restartFor); let session; const tokenSource = createTokenSource(config); diff --git a/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs b/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs deleted file mode 100644 index e3d4da6b2..000000000 --- a/src/ucode/agents/desktop/copilot_app_plugin/restart-helper.mjs +++ /dev/null @@ -1,3 +0,0 @@ -import { runHelper } from "./app-restart.mjs"; - -await runHelper(process.argv.at(-1)); diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs index 8a199fa76..d6ba2a74b 100644 --- a/tests/copilot_app_fake.mjs +++ b/tests/copilot_app_fake.mjs @@ -60,7 +60,6 @@ export class FakeApp { this.handshakes = []; this.connections = 0; this.sockets = new Set(); - this.watchers = new Set(); this.busy = 0; this.fail = null; this.drop = null; @@ -94,10 +93,8 @@ export class FakeApp { } this.connections++; this.sockets.add(socket); - if (params.get("session_channels") === "true") this.watchers.add(socket); socket.on("close", () => { this.sockets.delete(socket); - this.watchers.delete(socket); }); const accept = createHash("sha1") .update(request.headers["sec-websocket-key"] + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") @@ -159,11 +156,6 @@ export class FakeApp { return { type: "error", message: `unknown variant \`${kind}\`` }; } - emit(sessionId, type) { - const frame = encode(JSON.stringify({ type: "session_event", session_id: sessionId, event: { type, data: {} } })); - for (const socket of this.watchers) socket.write(frame); - } - sent(kind) { return this.received.filter((message) => message.type === kind); } diff --git a/tests/copilot_app_restart.test.mjs b/tests/copilot_app_restart.test.mjs index 57a2ef123..0d8de8bee 100644 --- a/tests/copilot_app_restart.test.mjs +++ b/tests/copilot_app_restart.test.mjs @@ -1,38 +1,19 @@ import assert from "node:assert/strict"; -import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { setTimeout as delay } from "node:timers/promises"; import { test } from "node:test"; import { - EXIT_TIMEOUT_MS, - IDLE_TIMEOUT_MS, - QUIET_MS, RESTART_HINT, RESTARTING_MESSAGE, launchRestart, planRestart, - restartApp, - restartWhenIdle, - runHelper, tellRestart, - sessionEnv, } from "../src/ucode/agents/desktop/copilot_app_plugin/app-restart.mjs"; -import { FakeApp } from "./copilot_app_fake.mjs"; const TARGET = { platform: "linux", pid: 4242, app: "/usr/bin/github" }; -function fakeClock() { - const clock = { t: 0, onSleep: () => {} }; - clock.now = () => clock.t; - clock.sleep = async (ms) => { - clock.t += ms; - await clock.onSleep(clock.t); - }; - return clock; -} - function emptyHome() { return mkdtempSync(join(tmpdir(), "ug-copilot-home-")); } @@ -44,8 +25,6 @@ function homeWithPid(portFile) { return home; } -const withWebSocket = { skip: typeof WebSocket === "function" ? false : "this Node has no global WebSocket" }; - function fakeSession() { const logs = []; return { logs, log: async (message, options) => logs.push([message, options.level]) }; @@ -58,11 +37,12 @@ test("the extension starts the helper with the app's target and says so in the c const result = await launchRestart({ home, stateDir, + ugPath: "/opt/ug/bin/ug", plan: async () => TARGET, - start: async (target, dir) => (started.push([target, dir]), true), + start: async (target, dir, ugPath) => (started.push([target, dir, ugPath]), true), }); assert.equal(result, "started"); - assert.deepEqual(started, [[{ ...TARGET, home }, stateDir]]); + assert.deepEqual(started, [[{ ...TARGET, home }, stateDir, "/opt/ug/bin/ug"]]); const session = fakeSession(); await tellRestart(session, result); assert.deepEqual(session.logs, [[RESTARTING_MESSAGE, "info"]]); @@ -70,11 +50,12 @@ test("the extension starts the helper with the app's target and says so in the c 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(), ...options }); + const result = await launchRestart({ home: emptyHome(), stateDir: emptyHome(), ugPath: "/u/ug", ...options }); assert.notEqual(result, "started"); const session = fakeSession(); await tellRestart(session, result); @@ -83,94 +64,6 @@ test("when the helper can't restart the app here, the chat gets the restart hint await tellRestart({ log: () => { throw new Error("closed"); } }, "failed"); }); -async function helperRun(t, script, overrides = {}, seen = () => {}) { - const app = new FakeApp(); - await app.listen(); - t.after(() => app.close()); - const home = emptyHome(); - app.writeRunFiles(home); - const fake = fakeProcess(); - seen(fake); - const clock = fake.clock; - clock.onSleep = async (time) => { - await script(time, app); - await delay(2); - }; - const result = await restartWhenIdle({ ...TARGET, home }, { ...fake.deps, ...overrides }); - return { result, fake, clock, app }; -} - -test("the helper restarts an idle app after a quiet period", withWebSocket, async (t) => { - let restartAt; - const { result, fake } = await helperRun(t, async (time) => { - restartAt ??= time >= QUIET_MS ? time : undefined; - }); - assert.equal(result, "relaunched"); - assert.equal(restartAt, QUIET_MS); - assert.deepEqual(fake.calls[0], ["kill", 4242, "SIGTERM"]); -}); - -test("a turn in any chat delays the restart until that chat is idle", withWebSocket, async (t) => { - let watchers; - let fake; - const kill = (pid, signal) => fake.deps.killAt.push(fake.clock.t); - const run = helperRun( - t, - async (time, app) => { - if (time === 5_000) { - watchers = app.watchers.size; - app.emit("ui-chat", "assistant.turn_start"); - } - if (time === 90_000) app.emit("ui-chat", "session.idle"); - }, - { kill }, - (process) => { - fake = process; - fake.deps.killAt = []; - }, - ); - const { result } = await run; - assert.equal(watchers, 1); - assert.equal(result, "still_running"); - assert.ok(fake.deps.killAt[0] >= 90_000 + QUIET_MS, `killed at ${fake.deps.killAt[0]}`); - assert.ok(fake.deps.killAt[0] <= 90_000 + QUIET_MS + 5_000, `killed at ${fake.deps.killAt[0]}`); -}); - -test("a chat that never goes idle makes the helper give up", withWebSocket, async (t) => { - const { result, fake, clock } = await helperRun(t, async (time, app) => { - if (time === 1_000) app.emit("ui-chat", "user.message"); - }); - assert.equal(result, "busy"); - assert.equal(clock.t, IDLE_TIMEOUT_MS); - assert.deepEqual(fake.calls, []); -}); - -test("events that keep coming hold the restart off even without a turn start", withWebSocket, async (t) => { - const { result, fake } = await helperRun(t, async (time, app) => app.emit("chat", "assistant.streaming_delta")); - assert.equal(result, "busy"); - assert.deepEqual(fake.calls, []); -}); - -test("an app that closes while the helper waits isn't relaunched", withWebSocket, async (t) => { - const { result, fake } = await helperRun(t, async (time, app) => { - if (time === 1_000) { - app.emit("chat", "user.message"); - app.close(); - } - }); - assert.equal(result, "stopped"); - assert.deepEqual(fake.calls, []); -}); - -test("without the app's session events the helper gives up", async () => { - const fake = fakeProcess(); - assert.equal(await restartWhenIdle({ ...TARGET, home: emptyHome() }, fake.deps), "not_running"); - const home = homeWithPid("1"); - writeFileSync(join(home, "run", "ws.token"), "t"); - assert.equal(await restartWhenIdle({ ...TARGET, home }, fake.deps), "no_session_events"); - assert.deepEqual(fake.calls, []); -}); - test("planRestart takes the app's pid from its run file", async () => { const alive = (pid) => pid === 4242; const exists = () => true; @@ -217,144 +110,3 @@ test("planRestart gives up without a display, the app's pid or a known launcher" assert.equal(await planRestart({ home, platform: "win32", alive, run: async () => null }), null); assert.equal(await planRestart({ home, platform: "freebsd", alive }), null); }); - -function fakeProcess({ exitAfterPolls = 2, launches = [true] } = {}) { - const clock = fakeClock(); - const calls = []; - let closed = false; - let polls = 0; - let osascriptFails = false; - const outcomes = [...launches]; - return { - clock, - calls, - deps: { - now: clock.now, - sleep: clock.sleep, - alive: () => !(closed && ++polls > exitAfterPolls), - kill: (pid, signal) => { - calls.push(["kill", pid, signal]); - closed = true; - }, - run: async (command, args) => { - calls.push(["run", command, ...args]); - if (command === "/usr/bin/osascript" && osascriptFails) return null; - closed = true; - return ""; - }, - launch: async (command, args, env) => { - calls.push(["launch", command, args, env]); - return outcomes.shift() ?? false; - }, - env: { HOME: "/home/u", PATH: "/usr/bin", DISPLAY: ":0", GH_TOKEN: "secret", COPILOT_SDK_AUTH_TOKEN: "x" }, - }, - setOsascriptFails() { - osascriptFails = true; - }, - }; -} - -const CLEAN_ENV = { HOME: "/home/u", PATH: "/usr/bin", DISPLAY: ":0" }; - -test("on Linux the app gets SIGTERM, then is relaunched with the session's environment once it exits", async () => { - const fake = fakeProcess(); - assert.equal(await restartApp(TARGET, fake.deps), "relaunched"); - assert.deepEqual(fake.calls, [ - ["kill", 4242, "SIGTERM"], - ["launch", "/usr/bin/github", [], CLEAN_ENV], - ]); -}); - -test("a failed relaunch is retried once", async () => { - const twice = fakeProcess({ launches: [false, true] }); - assert.equal(await restartApp(TARGET, twice.deps), "relaunched"); - assert.equal(twice.calls.filter(([kind]) => kind === "launch").length, 2); - - const never = fakeProcess({ launches: [false, false, true] }); - assert.equal(await restartApp(TARGET, never.deps), "relaunch_failed"); - assert.equal(never.calls.filter(([kind]) => kind === "launch").length, 2); -}); - -test("an app that doesn't exit is left running, not relaunched", async () => { - const fake = fakeProcess({ exitAfterPolls: Infinity }); - assert.equal(await restartApp(TARGET, fake.deps), "still_running"); - assert.ok(fake.clock.t <= EXIT_TIMEOUT_MS + 2_000); - assert.deepEqual( - fake.calls.map(([kind]) => kind), - ["kill"], - ); -}); - -test("on macOS the app is asked to quit, then opened by its bundle id", async () => { - const target = { platform: "darwin", pid: 4242, app: "com.github.githubapp" }; - const fake = fakeProcess(); - assert.equal(await restartApp(target, fake.deps), "relaunched"); - assert.deepEqual(fake.calls, [ - ["run", "/usr/bin/osascript", "-e", 'quit app id "com.github.githubapp"'], - ["launch", "/usr/bin/open", ["-b", "com.github.githubapp"], CLEAN_ENV], - ]); - - const refused = fakeProcess(); - refused.setOsascriptFails(); - assert.equal(await restartApp(target, refused.deps), "relaunched"); - assert.deepEqual(refused.calls[1], ["kill", 4242, "SIGTERM"]); -}); - -test("on Windows the app's window is closed without /F, and an app left in the tray isn't relaunched", async () => { - const target = { platform: "win32", pid: 4242, app: "C:\\Program Files\\GitHub\\github.exe" }; - const fake = fakeProcess(); - assert.equal(await restartApp(target, fake.deps), "relaunched"); - assert.deepEqual(fake.calls[0], ["run", "taskkill", "/PID", "4242"]); - assert.deepEqual(fake.calls[1].slice(0, 3), ["launch", target.app, []]); - - const tray = fakeProcess({ exitAfterPolls: Infinity }); - assert.equal(await restartApp(target, tray.deps), "still_running"); - assert.ok(tray.calls.every(([kind, command]) => kind === "run" && command === "taskkill")); -}); - -test("sessionEnv keeps the user's session variables and drops the runtime's", () => { - const env = { - HOME: "/h", - PATH: "/usr/bin", - DISPLAY: ":0", - WAYLAND_DISPLAY: "wayland-0", - XDG_RUNTIME_DIR: "/run/user/1", - DBUS_SESSION_BUS_ADDRESS: "unix:path=/run/user/1/bus", - LC_ALL: "C.UTF-8", - https_proxy: "http://proxy", - COPILOT_HOME: "/h/.copilot", - COPILOT_SDK_AUTH_TOKEN: "x", - COPILOT_AGENT_SESSION_ID: "s", - COPILOT_PLUGIN_ROOT: "/p", - GH_TOKEN: "t", - GIT_CONFIG_COUNT: "1", - SESSION_ID: "s", - NODE_OPTIONS: "--x", - ELECTRON_RUN_AS_NODE: "1", - OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel", - }; - assert.deepEqual(Object.keys(sessionEnv(env)).sort(), [ - "COPILOT_HOME", - "DBUS_SESSION_BUS_ADDRESS", - "DISPLAY", - "HOME", - "LC_ALL", - "PATH", - "WAYLAND_DISPLAY", - "XDG_RUNTIME_DIR", - "https_proxy", - ]); -}); - -test("the helper logs the outcome and never throws", async () => { - const log = join(mkdtempSync(join(tmpdir(), "ug-copilot-state-")), "app-restart.log"); - const fake = fakeProcess(); - await runHelper(JSON.stringify({ ...TARGET, home: emptyHome(), log }), fake.deps); - assert.match(readFileSync(log, "utf8"), /not_running\n$/); - await runHelper("not json", fake.deps); - const home = homeWithPid("1"); - writeFileSync(join(home, "run", "ws.token"), "t"); - const failing = { ...fake.deps, WebSocketImpl: class { constructor() { throw new Error("boom"); } } }; - await runHelper(JSON.stringify({ ...TARGET, home, log }), failing); - assert.match(readFileSync(log, "utf8"), /no_session_events\n$/); -}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index f80df122e..fb7452979 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -160,6 +160,7 @@ 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): @@ -237,7 +238,6 @@ def test_layout(self, home): "app-rows.mjs", "app-remove.mjs", "app-restart.mjs", - "restart-helper.mjs", "config.json", } assert (extension / "extension.mjs").read_text() == ( @@ -580,7 +580,6 @@ class TestExtensionSource: "app-rows.mjs", "app-remove.mjs", "app-restart.mjs", - "restart-helper.mjs", ], ) def test_syntax(self, name): From 549cbfceccbba6b89d5dea33503d82401b7b9666 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 12:10:47 +0000 Subject: [PATCH 16/26] copilot: address review of the Copilot app plugin - install checks that extraKnownMarketplaces and enabledPlugins are JSON objects before it writes the plugin, so a refusal leaves no orphan plugin directory behind; - the token command's environment keeps Windows' Path casing instead of adding a second PATH key; - a token the CLI hands out within two minutes of its expiry is reused for 30 s rather than minted again for every request; - the sync lock names its owner, so a process whose lock was taken over as stale can't release the new owner's, and only one process can take a stale lock over; - the first sync (or deferred remove) retries three times with a backoff when the app hasn't published its endpoint yet. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 5 ++- .../desktop/copilot_app_plugin/app-remove.mjs | 8 ++-- .../desktop/copilot_app_plugin/app-rows.mjs | 44 +++++++++++++------ .../desktop/copilot_app_plugin/extension.mjs | 28 ++++++++---- .../desktop/copilot_app_plugin/ug-plugin.mjs | 14 ++++-- tests/copilot_app_plugin.test.mjs | 33 ++++++++++++++ tests/copilot_app_rows.test.mjs | 17 ++++++- tests/test_copilot_app_plugin.py | 13 ++++++ 8 files changed, 130 insertions(+), 32 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index a60fb8d8a..c5102c324 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -414,7 +414,10 @@ def install( config: PluginConfig, root: Path | None = None, settings_path: Path | None = None ) -> Path: path = settings_path or copilot_settings_path() - read_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) _check_writable(path) created = not path.exists() root = write_plugin(config, root) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs index fd601c942..275f2aac5 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-remove.mjs @@ -104,8 +104,8 @@ export function removeSettingsKeys(path, pluginRoot) { export async function removeOnce(config, { home = copilotHome(), stateDir, now = Date.now, ...connect } = {}) { if (!config.remove) return "skipped"; const path = settingsPath(home); - const lockPath = join(stateDir, LOCK_FILE); - if (!tryLock(lockPath, now)) return "busy"; + const release = tryLock(join(stateDir, LOCK_FILE), now); + if (!release) return "busy"; try { let current; let refusal = null; @@ -137,9 +137,7 @@ export async function removeOnce(config, { home = copilotHome(), stateDir, now = removeSettingsKeys(path, config.pluginRoot); return "removed"; } finally { - try { - unlinkSync(lockPath); - } catch {} + release(); } } diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index 179edc4f4..edc11d204 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -1,5 +1,5 @@ import { createHash, randomUUID } from "node:crypto"; -import { closeSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs"; +import { readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; @@ -223,20 +223,40 @@ export async function syncRows(app, rows) { 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 { - closeSync(openSync(path, "wx")); - return true; + return take(); } catch (error) { if (error.code !== "EEXIST") throw error; } try { - if (now() - statSync(path).mtimeMs < LOCK_STALE_MS) return false; - unlinkSync(path); - closeSync(openSync(path, "wx")); - return true; + 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 false; + return null; } } @@ -267,8 +287,8 @@ export async function syncOnce(config, { home = copilotHome(), stateDir, now = D 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 lockPath = join(stateDir, LOCK_FILE); - if (!tryLock(lockPath, now)) return "busy"; + const release = tryLock(join(stateDir, LOCK_FILE), now); + if (!release) return "busy"; try { const app = await connectApp(endpoint, connect); let changed; @@ -285,9 +305,7 @@ export async function syncOnce(config, { home = copilotHome(), stateDir, now = D writeStamp(stampPath, digest); return restart ? "changed" : "synced"; } finally { - try { - unlinkSync(lockPath); - } catch {} + release(); } } diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index f470a83ca..af0e1fd04 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -1,3 +1,4 @@ +import { setTimeout as delay } from "node:timers/promises"; import { fileURLToPath } from "node:url"; import { joinSession } from "@github/copilot-sdk/extension"; @@ -18,20 +19,30 @@ 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 = (result) => ["not_running", "failed"].includes(result); +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 firstRemove = removeInBackground(config, syncOptions, logSyncFailure); + const removal = withRetries(() => removeInBackground(config, syncOptions, logSyncFailure)); try { await joinSession(); } catch (error) { logSyncFailure(`Databricks models: couldn't join the session: ${error?.message ?? error}`); } - if (retry(await firstRemove)) await removeInBackground(config, syncOptions, logSyncFailure); + await removal; } else { - const firstSync = syncInBackground(config, syncOptions, logSyncFailure); - const restartFor = (synced) => (synced === "changed" ? launchRestart({ ...syncOptions, ugPath: config.ugPath }) : null); - const firstRestart = firstSync.then(restartFor); + const restart = withRetries(() => syncInBackground(config, syncOptions, logSyncFailure)).then((synced) => + synced === "changed" ? launchRestart({ ...syncOptions, ugPath: config.ugPath }) : null, + ); let session; const tokenSource = createTokenSource(config); const bearerTokenProvider = withLoginHint(tokenSource, (error) => { @@ -50,7 +61,6 @@ if (config.remove) { // The app's own message for a 401 names Copilot's API-key settings, not Databricks sign-in. if (event.type === "session.error") session.log(config.loginHint, { level: "error" }); }); - let restart = await firstRestart; - if (retry(await firstSync)) restart = await restartFor(await syncInBackground(config, syncOptions, logSyncFailure)); - if (restart) await tellRestart(session, restart); + 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 index fa869be3b..75e5c7e4b 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs @@ -4,6 +4,7 @@ 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) { @@ -21,10 +22,13 @@ export function jwtExpiryMs(token) { } } +// 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.PATH].filter(Boolean).join(delimiter); - return { ...env, PATH: path }; + const path = [...(config.pathDirs || []), ...databricksDir, env[name]].filter(Boolean).join(delimiter); + return { ...env, [name]: path }; } export function runCommand(argv, env) { @@ -65,7 +69,11 @@ export function createTokenSource(config, { run = runCommand, now = Date.now } = .then((value) => { if (started === generation) { const expiry = jwtExpiryMs(value); - const refreshAt = expiry ? expiry - REFRESH_MARGIN_MS : now() + DEFAULT_TOKEN_TTL_MS; + // 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; diff --git a/tests/copilot_app_plugin.test.mjs b/tests/copilot_app_plugin.test.mjs index b88836375..e2de5b6f2 100644 --- a/tests/copilot_app_plugin.test.mjs +++ b/tests/copilot_app_plugin.test.mjs @@ -13,6 +13,8 @@ import { hintFor, isAuthRejection, jwtExpiryMs, + MIN_TOKEN_REUSE_MS, + runCommand, withLoginHint, withSessionId, } from "../src/ucode/agents/desktop/copilot_app_plugin/ug-plugin.mjs"; @@ -42,6 +44,37 @@ test("childEnv puts the configured dirs ahead of PATH", () => { 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 = []; diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 094ac824e..20d977022 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -4,7 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { test as nodeTest } from "node:test"; -import { copilotHome, readEndpoint, syncInBackground, syncOnce } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs"; +import { copilotHome, readEndpoint, 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. @@ -174,6 +174,21 @@ test("only one extension process syncs at a time; a stale lock is taken over", a 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, "app-sync.lock"); + const first = tryLock(lock, Date.now); + assert.equal(tryLock(lock, Date.now), null); + const old = new Date(Date.now() - 120_000); + 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 = [ diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index fb7452979..f288922fe 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -672,3 +672,16 @@ def test_an_earlier_id_at_another_path_isnt_ugs(self, home): 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: []} From df24d5173adc32044c003d0a09248ef4d7086dd2 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 12:31:59 +0000 Subject: [PATCH 17/26] copilot: address the second review of the Copilot app plugin - A deferred remove on an earlier build's install marks that build's extension, under its own plugin directory, so the next app start still deletes the rows and keys; a pending removal is found there too. - install leaves another marketplace of ug's name alone instead of pointing it at ug's plugin, and refuses before writing anything. - The extension catches a rejected session.log, so a hint the chat can't show no longer ends the extension. - Tests take the lock file name and staleness from app-rows.mjs, and the file-mode test skips on Windows, which has no POSIX modes. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 23 +++++++++++----- .../desktop/copilot_app_plugin/extension.mjs | 9 +++++-- tests/copilot_app_remove.test.mjs | 6 ++--- tests/copilot_app_rows.test.mjs | 10 +++---- tests/test_copilot_app_plugin.py | 26 +++++++++++++++++++ 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index c5102c324..53adde371 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -364,17 +364,18 @@ def _has_ug_keys(settings: dict, root: Path) -> bool: return bool(_ug_entries(settings, root)) -def _read_extension_config(root: Path) -> dict | None: +def _read_extension_config(root: Path, plugin: str = PLUGIN) -> dict | None: try: - config = json.loads((_extension_dir(root) / "config.json").read_text(encoding="utf-8")) + 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: - config = _read_extension_config(root) - return config is not None and config.get("remove") is True + 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: @@ -418,6 +419,11 @@ def install( # 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) @@ -462,14 +468,17 @@ def defer_remove(root: Path | None = None, settings_path: Path | None = None) -> settings = read_settings(path) # The extension rewrites settings.json under the same rules, so ug refuses what it would. _check_writable(path) - if not _installed(settings, root): + installed = _installed_id(settings, root) + if installed is None: return False - config = _read_extension_config(root) + # 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) / "config.json", config) + atomic_write_json(_extension_dir(root, plugin) / "config.json", config) return True diff --git a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs index af0e1fd04..a092bb652 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/extension.mjs @@ -44,11 +44,16 @@ if (config.remove) { 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; - session.log(hint, { level: "error" }); + say(hint); return true; }); session = await joinSession({ @@ -59,7 +64,7 @@ if (config.remove) { 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") session.log(config.loginHint, { level: "error" }); + if (event.type === "session.error") say(config.loginHint); }); const launched = await restart; if (launched) await tellRestart(session, launched); diff --git a/tests/copilot_app_remove.test.mjs b/tests/copilot_app_remove.test.mjs index a680a3af2..63b023411 100644 --- a/tests/copilot_app_remove.test.mjs +++ b/tests/copilot_app_remove.test.mjs @@ -18,7 +18,7 @@ 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 { copilotHome, syncOnce } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.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"; @@ -98,13 +98,13 @@ test("once the keys are gone, another session's extension does nothing", async ( 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, "app-sync.lock"); + 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() - 120_000); + 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); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 20d977022..74250ed2b 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -4,7 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { test as nodeTest } from "node:test"; -import { copilotHome, readEndpoint, syncInBackground, syncOnce, tryLock } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs"; +import { LOCK_FILE, LOCK_STALE_MS, copilotHome, readEndpoint, 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. @@ -164,22 +164,22 @@ test("repeated syncs leave exactly one copy of each row", async (t) => { 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, "app-sync.lock"); + 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() - 120_000); + 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, "app-sync.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() - 120_000); + const old = new Date(Date.now() - 2 * LOCK_STALE_MS); utimesSync(lock, old, old); const second = tryLock(lock, Date.now); assert.ok(second); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index f288922fe..c7afc812a 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -1,6 +1,7 @@ import json import shutil import subprocess +import sys from pathlib import Path import pytest @@ -405,6 +406,7 @@ def test_a_symlinked_settings_file_is_refused(self, home, tmp_path): 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() @@ -661,6 +663,17 @@ def test_remove_takes_out_an_earlier_install(self, home): 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") @@ -685,3 +698,16 @@ def test_install_refuses_a_non_object_key_before_writing_the_plugin(home, key): 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} From 9aa8ea39a03f58b715e7bc610e7bff9908799939 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 13:04:35 +0000 Subject: [PATCH 18/26] copilot: stage the Copilot app plugin before swapping it in write_plugin used to write the marketplace, manifests, extension files and config.json one by one in place, so an app reading the plugin during configure, or a failed copy during a reinstall, could see a half written plugin. It now writes the whole tree to a sibling staging directory, syncs the files, and swaps it in by renames: the old tree moves aside first, as Windows can't rename onto a directory, and comes back if the swap fails. The extension's sync stamp, restart marker and log, ug's created-settings marker and an earlier build's copy are carried over. Settings keys are merged only after the swap, and leftovers from a crashed run are removed after ten minutes. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 81 +++++++++++++++- tests/test_copilot_app_plugin.py | 92 +++++++++++++++++++ 2 files changed, 170 insertions(+), 3 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 53adde371..414cbf0a4 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -17,6 +17,7 @@ import os import shutil import time +import uuid from collections.abc import Callable, Mapping from dataclasses import dataclass, field from pathlib import Path @@ -229,9 +230,16 @@ def extension_config(config: PluginConfig) -> dict: } -def write_plugin(config: PluginConfig, root: Path | None = None) -> Path: - """Write the marketplace, the plugin and its extension under ``root``; return ``root``.""" - root = root or plugin_root() +# 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", @@ -263,6 +271,73 @@ def write_plugin(config: PluginConfig, root: Path | None = None) -> Path: 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 diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index c7afc812a..71704690d 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -1,7 +1,9 @@ import json +import os import shutil import subprocess import sys +import time from pathlib import Path import pytest @@ -711,3 +713,93 @@ def test_install_leaves_another_marketplace_of_its_name_alone(home): 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() From 83ce6f7470cfd90b23be3efa2729e8d832402d75 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 13:05:12 +0000 Subject: [PATCH 19/26] copilot: stop a Copilot app row sync once ug marks the plugin for removal Before ug deletes its rows from a running app, it will mark the plugin for removal, the flag the deferred removal already uses. Under the sync lock, an extension now reads that flag from its config.json before the sync and before every write, and stops instead of writing ug's rows back. The plugin module also gets sync_lock(), which takes the extensions' lock file under the same protocol (an owner-named file, an atomic stale takeover), so ug can hold it while it unregisters. Co-authored-by: Isaac --- .../desktop/copilot_app_plugin/__init__.py | 64 ++++++++++++++++++- .../desktop/copilot_app_plugin/app-rows.mjs | 29 +++++++-- tests/copilot_app_fake.mjs | 3 + tests/copilot_app_rows.test.mjs | 35 +++++++++- tests/test_copilot_app_plugin.py | 62 ++++++++++++++++++ 5 files changed, 187 insertions(+), 6 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py index 414cbf0a4..0b5745afe 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/__init__.py +++ b/src/ucode/agents/desktop/copilot_app_plugin/__init__.py @@ -18,7 +18,8 @@ import shutil import time import uuid -from collections.abc import Callable, Mapping +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 @@ -534,6 +535,67 @@ def remove( 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. diff --git a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs index edc11d204..4c84225f2 100644 --- a/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs +++ b/src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs @@ -189,7 +189,23 @@ function matches(wanted, actual) { // 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``. -export async function syncRows(app, 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; @@ -203,7 +219,7 @@ export async function syncRows(app, rows) { 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 app.request(message, "model_provider_upserted"); + 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)) { @@ -217,7 +233,7 @@ export async function syncRows(app, rows) { } for (const model of models) { if (!matches(model, listed.get(model.id))) changed = true; - await app.request({ type: "upsert_provider_model", model }, "provider_model_upserted"); + await write({ type: "upsert_provider_model", model }, "provider_model_upserted"); } } return changed; @@ -290,10 +306,15 @@ export async function syncOnce(config, { home = copilotHome(), stateDir, now = D 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); + changed = await syncRows(app, rows, stop); + } catch (error) { + if (error instanceof RemovalRequested) return "removing"; + throw error; } finally { app.close(); } diff --git a/tests/copilot_app_fake.mjs b/tests/copilot_app_fake.mjs index d6ba2a74b..3df503ae1 100644 --- a/tests/copilot_app_fake.mjs +++ b/tests/copilot_app_fake.mjs @@ -64,6 +64,8 @@ export class FakeApp { 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)); } @@ -115,6 +117,7 @@ export class FakeApp { 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); diff --git a/tests/copilot_app_rows.test.mjs b/tests/copilot_app_rows.test.mjs index 74250ed2b..7fe4e1196 100644 --- a/tests/copilot_app_rows.test.mjs +++ b/tests/copilot_app_rows.test.mjs @@ -4,7 +4,7 @@ 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, syncInBackground, syncOnce, tryLock } from "../src/ucode/agents/desktop/copilot_app_plugin/app-rows.mjs"; +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. @@ -251,3 +251,36 @@ test("no app rows in the config means nothing to sync", async (t) => { assert.equal(await syncOnce({ app: { providers: [], ownedProviderIds: [] } }, options), "skipped"); assert.equal(app.connections, 0); }); + + +test("a sync racing a revert stops before it writes ug's rows back", async (t) => { + const { app, options } = await setup(t); + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: false })); + app.onRequest = (message) => { + if (message.type !== "upsert_model_provider" || app.onRequest === null) return; + app.onRequest = null; + // ug marks the plugin for removal, then deletes ug's rows from the running app. + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: true })); + setImmediate(() => { + app.providers.clear(); + app.models.clear(); + }); + }; + + assert.equal(await syncOnce({ app: ROWS }, options), "removing"); + await new Promise((resolve) => setImmediate(resolve)); + + assert.equal(app.providers.size, 0); + assert.equal(app.models.size, 0); + assert.equal(app.sent("upsert_model_provider").length, 1); + assert.equal(app.sent("upsert_provider_model").length, 0); +}); + +test("a plugin marked for removal doesn't sync at all", async (t) => { + const { app, options } = await setup(t); + writeFileSync(join(options.stateDir, "config.json"), JSON.stringify({ remove: true })); + + assert.equal(removalRequested(options.stateDir), true); + assert.equal(await syncOnce({ app: ROWS }, options), "removing"); + assert.equal(app.sent("upsert_model_provider").length, 0); +}); diff --git a/tests/test_copilot_app_plugin.py b/tests/test_copilot_app_plugin.py index 71704690d..d91ffe66d 100644 --- a/tests/test_copilot_app_plugin.py +++ b/tests/test_copilot_app_plugin.py @@ -803,3 +803,65 @@ def test_a_crashed_runs_leftovers_are_removed_once_old(self, home): 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 From d2343e39cc14aea2ab071febddb6e5bcd5b85623 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:15:21 +0000 Subject: [PATCH 20/26] copilot: register the app's Databricks providers over its local API An unwired module that adds ug's Claude, GPT and open-model providers and their models to the running GitHub Copilot app through its local WebSocket API, and deletes them again. One function builds both the app's provider and model rows and the Copilot plugin's providers from the model catalog VS Code uses, so their ids always match. The rows point at the AI Gateway; the plugin serves them with a fresh token. Co-authored-by: Isaac --- src/ucode/agents/vscode.py | 8 +- src/ucode/copilot_app.py | 462 +++++++++++++++++++++++++++++++++++++ tests/test_copilot_app.py | 458 ++++++++++++++++++++++++++++++++++++ 3 files changed, 925 insertions(+), 3 deletions(-) create mode 100644 src/ucode/copilot_app.py create mode 100644 tests/test_copilot_app.py diff --git a/src/ucode/agents/vscode.py b/src/ucode/agents/vscode.py index f40a2ef84..9ab942f91 100644 --- a/src/ucode/agents/vscode.py +++ b/src/ucode/agents/vscode.py @@ -200,7 +200,9 @@ def _model_entry(model: CopilotModel, name: str) -> dict[str, object]: return entry -def _catalog(state: dict, provider: str | None, location: str | None) -> list[CopilotModel]: +def copilot_models( + state: dict, provider: str | None = None, location: str | None = None +) -> list[CopilotModel]: """The models Copilot CLI would offer from the same source; raises when there are none.""" if provider: return provider_service_catalog(state, provider, None, tool="vscode") @@ -223,7 +225,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 +572,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/copilot_app.py b/src/ucode/copilot_app.py new file mode 100644 index 000000000..4e50fbdd0 --- /dev/null +++ b/src/ucode/copilot_app.py @@ -0,0 +1,462 @@ +"""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. The app must be running: it publishes the port and a per-launch token in +``~/.copilot/run/``. ug's Copilot plugin (``ucode.copilot_app_plugin``) re-supplies the same +provider and model ids in every session and serves them from the AI Gateway with a fresh token, so +both come from :func:`desired_providers`. The providers have fixed ids, so registering again +updates them in place and revert finds them without any state. +""" + +from __future__ import annotations + +import json +import os +import time +import uuid +from collections.abc import Iterable +from dataclasses import dataclass +from pathlib import Path +from typing import Any, Literal + +from ucode.agents.copilot import ( + WIRE_ANTHROPIC, + WIRE_COMPLETIONS, + WIRE_RESPONSES, + CopilotModel, + catalog_model_limits, + copilot_catalog_model_id, + model_labels, +) +from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER +from ucode.copilot_app_plugin import PluginModel, PluginProvider, Wire +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 + +AppStatus = Literal["registered", "outdated", "not_registered", "not_running"] + + +class CopilotAppError(RuntimeError): + """The app refused or didn't understand a request; nothing more was changed.""" + + +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) + + +@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] + + +def copilot_home() -> Path: + return Path(os.environ.get("COPILOT_HOME") or Path.home() / ".copilot") + + +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 _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, which is what makes the app apply Claude's request + settings, and its wire model is the gateway's id. Ids the app would see twice keep the first.""" + 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.id) + if model_id is None: + continue + else: + model_id = model.registry_id + if model_id in rows: + 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 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( + PluginModel(row["modelId"], row["wireModel"], row["displayName"]) + 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), provider_service=provider + ) + + +class _AppConnection: + """One connection to the app's WebSocket API, matching each reply to its request.""" + + def __init__(self, home: Path) -> 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; a stale one names a closed port. + port = int((run / "ws.port").read_text().split()[0]) + token = (run / "ws.token").read_text().split()[0] + except (OSError, ValueError, IndexError): + raise CopilotAppNotRunning("The GitHub Copilot app isn't running.") from None + url = f"ws://127.0.0.1:{port}/?token={token}&requester_id=ug&session_channels=false" + try: + # No Origin: the app refuses any origin but its own window. + self._connection = connect(url, origin=None, compression=None, open_timeout=5) + 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._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 _wait(self, matches, timeout: float = _REQUEST_TIMEOUT_S) -> dict[str, Any] | None: + deadline = time.monotonic() + 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() -> _AppConnection: + app = _AppConnection(copilot_home()) + try: + version = _version(app) + except CopilotAppError: + app.close() + raise + if 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 register(providers: Iterable[AppProvider]) -> None: + """Add or update ``providers`` (:attr:`DesiredProviders.app`) and their models in the running + app, and delete ug's other providers. + + Raises :class:`CopilotAppNotRunning` when the app is closed, else :class:`CopilotAppError` + when it refuses.""" + providers = list(providers) + app = _connect() + try: + existing = _providers(app) + wanted_ids = {provider.id for provider in providers} + for spec in _PROVIDERS: + provider_id = _provider_id(spec.key) + if provider_id in existing and provider_id not in wanted_ids: + _delete_provider(app, provider_id) + for provider in providers: + current = existing.get(provider.id) + message: dict[str, Any] = { + "type": "upsert_model_provider", + "provider": { + "id": provider.id, + "name": provider.name, + "kind": provider.kind, + "settings": provider.settings, + }, + } + if provider.secret and not (current and current.get("hasSecret")): + message["secret"] = provider.secret + app.request(message, "model_provider_upserted") + wanted = {provider.model_row_id(row["modelId"]) for row in provider.models} + for model in _models(app, provider.id): + if model["id"] not in wanted: + app.request( + {"type": "delete_provider_model", "model_id": model["id"]}, + "provider_model_deleted", + model_id=model["id"], + ) + for row in provider.models: + app.request( + { + "type": "upsert_provider_model", + "model": { + **row, + "id": provider.model_row_id(row["modelId"]), + "providerId": provider.id, + }, + }, + "provider_model_upserted", + ) + finally: + app.close() + + +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 status(providers: Iterable[AppProvider]) -> AppStatus: + """Whether the running app has exactly ``providers`` and their models among ug's providers. + + ``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() + 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: + listed = {model["id"] for model in _models(app, provider.id)} + if listed != {provider.model_row_id(row["modelId"]) for row in provider.models}: + return "outdated" + return "registered" + except CopilotAppNotRunning: + return "not_running" + finally: + app.close() diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py new file mode 100644 index 000000000..e36985672 --- /dev/null +++ b/tests/test_copilot_app.py @@ -0,0 +1,458 @@ +"""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 threading +from urllib.parse import parse_qs, urlparse + +import pytest +from websockets.sync.server import serve + +from ucode import copilot_app +from ucode import copilot_app_plugin as plugin +from ucode.agents.copilot import WIRE_ANTHROPIC, WIRE_COMPLETIONS, WIRE_RESPONSES, CopilotModel +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.busy = 0 + 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}\n4242\n") + (run / "ws.token").write_text(f"{self.token}\n4242\n") + + def _check(self, connection, request): + self.handshakes.append(dict(request.headers.raw_items())) + 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("upsert") 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) for model in plugin_provider.models + ] == [ + (row["modelId"], row["wireModel"], row["displayName"]) + 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_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_state_providers_use_the_vscode_catalog(self, monkeypatch): + from ucode.agents import vscode + + calls = [] + + def models(state, provider, location): + calls.append((provider, location)) + return CATALOG + + monkeypatch.setattr(vscode, "copilot_models", models) + + desired = copilot_app.state_providers({"workspace": WORKSPACE}, provider="my-mps") + + assert calls == [("my-mps", None)] + assert desired == copilot_app.desired_providers( + WORKSPACE, CATALOG, provider_service="my-mps" + ) + + +def test_registers_the_three_providers_with_their_models(app): + copilot_app.register(_app_providers()) + + providers = _by_name(app) + claude, gpt = providers["Databricks Claude"], providers["Databricks GPT"] + open_models = providers["Databricks Open Models"] + assert claude["settings"]["baseUrl"] == f"{WORKSPACE}/ai-gateway/anthropic" + assert gpt["settings"]["baseUrl"] == f"{WORKSPACE}/ai-gateway/mlflow/v1" + assert open_models["settings"]["wireApi"] == "completions" + # The app stores the placeholder in its own keychain slot; only the Claude provider needs one. + assert app.secrets == {claude["id"]: copilot_app.PLACEHOLDER_KEY} + rows = {m["providerId"]: m for m in app.models.values()} + assert rows[claude["id"]]["modelId"] == "claude-sonnet-4.6" + assert rows[claude["id"]]["wireModel"] == "system.ai.claude-sonnet-4-6" + assert rows[claude["id"]]["displayName"] == "Claude Sonnet 4.6" + assert rows[gpt["id"]]["modelId"] == "gpt-5-2" + assert rows[gpt["id"]]["wireModel"] == "system.ai.gpt-5-2" + assert rows[gpt["id"]]["wireApiOverride"] == "responses" + assert rows[open_models["id"]]["modelId"] == "glm-5" + assert rows[open_models["id"]]["wireModel"] == "system.ai.glm-5" + assert "wireApiOverride" not in rows[open_models["id"]] + assert len(app.models) == 3 + assert all("Origin" not in headers for headers in app.handshakes) + + +def test_registering_again_updates_in_place_without_resending_the_key(app): + copilot_app.register(_app_providers()) + claude_id = _by_name(app)["Databricks Claude"]["id"] + app.received.clear() + + copilot_app.register(_app_providers(workspace="https://other.cloud.databricks.com")) + + assert _by_name(app)["Databricks Claude"]["id"] == claude_id + assert all("secret" not in message for message in app.sent("upsert_model_provider")) + assert _by_name(app)["Databricks Claude"]["settings"]["baseUrl"] == ( + "https://other.cloud.databricks.com/ai-gateway/anthropic" + ) + + +def test_a_catalog_change_drops_ugs_stale_providers_and_models(app): + app.providers["mine"] = {"id": "mine", "name": "Mine", "kind": "custom"} + copilot_app.register(_app_providers()) + + copilot_app.register( + _app_providers([CATALOG[1], CopilotModel("system.ai.gpt-6", WIRE_RESPONSES)]) + ) + + assert sorted(app.providers) == sorted(["mine", _by_name(app)["Databricks GPT"]["id"]]) + assert sorted(m["modelId"] for m in app.models.values()) == ["gpt-5-2", "gpt-6"] + + copilot_app.register(_app_providers(CATALOG[2:])) + + assert [m["modelId"] for m in app.models.values()] == ["glm-5"] + + +def test_a_row_the_user_added_to_ugs_provider_is_replaced(app): + copilot_app.register(_app_providers()) + claude = _by_name(app)["Databricks Claude"] + app.models["user-row"] = {"id": "user-row", "providerId": claude["id"], "modelId": "x"} + + copilot_app.register(_app_providers()) + + assert "user-row" not in app.models + + +def test_unregister_deletes_only_ugs_providers(app): + app.providers["mine"] = {"id": "mine", "name": "Databricks Claude", "kind": "anthropic"} + copilot_app.register(_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 + + +class TestStatus: + def test_registered_outdated_and_not_registered(self, app): + providers = _app_providers() + assert copilot_app.status(providers) == "not_registered" + + copilot_app.register(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_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): + app.busy = 2 + + copilot_app.register(_app_providers()) + + assert len(app.providers) == 3 + + +def test_a_refusal_stops_the_registration(app): + app.fail = "upsert_provider_model" + + with pytest.raises(copilot_app.CopilotAppError, match="refused upsert_provider_model"): + copilot_app.register(_app_providers()) + + +def _healthcheck(version, **fields): + return {"type": "command_result", "data": {"app": {"version": version}}, **fields} + + +def test_every_request_carries_a_request_id(app): + copilot_app.register(_app_providers()) + + 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] + + copilot_app.register(_app_providers()) + + assert "hasn't been verified" not in capsys.readouterr().out + assert len(app.providers) == 3 + + +def test_an_error_for_another_request_is_skipped(app): + app.before_reply["get_healthcheck"] = [ + {"type": "error", "message": "boom", "request_id": "ug-someone-else"} + ] + + copilot_app.register(_app_providers()) + + assert len(app.providers) == 3 + + +def test_an_error_without_a_request_id_answers_the_pending_request(app): + app.before_reply["get_healthcheck"] = [{"type": "error", "message": "Invalid message format"}] + + with pytest.raises(copilot_app.CopilotAppError, match="Invalid message format"): + copilot_app.register(_app_providers()) + + +def test_an_app_that_quits_mid_registration_is_not_running(app): + app.drop = "upsert_provider_model" + + with pytest.raises(copilot_app.CopilotAppNotRunning): + copilot_app.register(_app_providers()) + + +def test_a_listed_model_without_an_id_is_left_alone(app): + copilot_app.register(_app_providers()) + claude = _by_name(app)["Databricks Claude"]["id"] + app.models["odd"] = {"providerId": claude, "modelId": "claude-odd"} + + copilot_app.register(_app_providers()) + + assert "odd" in app.models + + +def test_an_unknown_app_version_warns_and_still_registers(app, capsys): + app.version = "9.0.0" + + copilot_app.register(_app_providers()) + + assert "GitHub Copilot app 9.0.0 hasn't been verified with ug" in " ".join( + capsys.readouterr().out.split() + ) + assert len(app.providers) == 3 + + +def test_a_verified_app_version_registers_quietly(app, capsys): + copilot_app.register(_app_providers()) + + 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.register(_app_providers()) + + # 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_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.register(_app_providers()) From 451f73e14b93a18aed0c459d10d2452b66a4c7c5 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:30:08 +0000 Subject: [PATCH 21/26] copilot: pass the app rows' limits and vision to the plugin's models Co-authored-by: Isaac --- src/ucode/copilot_app.py | 14 ++++++++++---- tests/test_copilot_app.py | 13 +++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/ucode/copilot_app.py b/src/ucode/copilot_app.py index 4e50fbdd0..c5d15e672 100644 --- a/src/ucode/copilot_app.py +++ b/src/ucode/copilot_app.py @@ -145,6 +145,15 @@ def _model_rows( return list(rows.values()) +def _plugin_model(row: dict[str, Any]) -> PluginModel: + limits = None + if "maxPromptTokens" in row: + limits = (row["maxPromptTokens"], row["maxOutputTokens"]) + return PluginModel( + row["modelId"], row["wireModel"], row["displayName"], limits, row["supportsVision"] + ) + + def desired_providers( workspace: str, catalog: Iterable[CopilotModel], *, provider_service: str | None = None ) -> DesiredProviders: @@ -179,10 +188,7 @@ def desired_providers( id=provider.id, wire=spec.wire, base_path=base_path, - models=tuple( - PluginModel(row["modelId"], row["wireModel"], row["displayName"]) - for row in rows - ), + models=tuple(_plugin_model(row) for row in rows), ) ) headers = {MODEL_PROVIDER_SERVICE_HEADER: provider_service} if provider_service else {} diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py index e36985672..cbecfdd02 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -140,9 +140,18 @@ def test_the_plugin_gets_the_app_rows_ids(self): 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) for model in plugin_provider.models + (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["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 From 509df9bfb42e674eb1953c2990b1fd201971729e Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 08:52:23 +0000 Subject: [PATCH 22/26] copilot: hand the app rows to the plugin instead of registering them The plugin's extension now writes the providers and model rows to the app, so desired_providers() also returns them as the plugin's input, with every provider id ug owns, and register() is gone. unregister() and status() stay for revert and status. Chat Completions models turn off reasoning_effort. Co-authored-by: Isaac --- src/ucode/copilot_app.py | 100 +++++++----------- tests/test_copilot_app.py | 209 ++++++++++++++++++++------------------ 2 files changed, 150 insertions(+), 159 deletions(-) diff --git a/src/ucode/copilot_app.py b/src/ucode/copilot_app.py index c5d15e672..96f6cb8f1 100644 --- a/src/ucode/copilot_app.py +++ b/src/ucode/copilot_app.py @@ -1,11 +1,12 @@ """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. The app must be running: it publishes the port and a per-launch token in -``~/.copilot/run/``. ug's Copilot plugin (``ucode.copilot_app_plugin``) re-supplies the same -provider and model ids in every session and serves them from the AI Gateway with a fresh token, so -both come from :func:`desired_providers`. The providers have fixed ids, so registering again -updates them in place and revert finds them without any state. +own window uses. ug's Copilot plugin (``ucode.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 finds them +without any state. """ from __future__ import annotations @@ -29,7 +30,7 @@ model_labels, ) from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER -from ucode.copilot_app_plugin import PluginModel, PluginProvider, Wire +from ucode.copilot_app_plugin import AppProviderRows, PluginModel, PluginProvider, Wire from ucode.ui import print_warning # The app versions whose WebSocket messages ug was checked against; others get a warning. @@ -91,6 +92,17 @@ def id(self) -> str: 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: @@ -101,6 +113,15 @@ class DesiredProviders: # 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 Path(os.environ.get("COPILOT_HOME") or Path.home() / ".copilot") @@ -145,12 +166,20 @@ def _model_rows( return list(rows.values()) -def _plugin_model(row: dict[str, Any]) -> PluginModel: +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"] + 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, ) @@ -188,7 +217,7 @@ def desired_providers( id=provider.id, wire=spec.wire, base_path=base_path, - models=tuple(_plugin_model(row) for row in rows), + models=tuple(_plugin_model(row, spec.wire) for row in rows), ) ) headers = {MODEL_PROVIDER_SERVICE_HEADER: provider_service} if provider_service else {} @@ -369,59 +398,6 @@ def _delete_provider(app: _AppConnection, provider_id: str) -> None: ) -def register(providers: Iterable[AppProvider]) -> None: - """Add or update ``providers`` (:attr:`DesiredProviders.app`) and their models in the running - app, and delete ug's other providers. - - Raises :class:`CopilotAppNotRunning` when the app is closed, else :class:`CopilotAppError` - when it refuses.""" - providers = list(providers) - app = _connect() - try: - existing = _providers(app) - wanted_ids = {provider.id for provider in providers} - for spec in _PROVIDERS: - provider_id = _provider_id(spec.key) - if provider_id in existing and provider_id not in wanted_ids: - _delete_provider(app, provider_id) - for provider in providers: - current = existing.get(provider.id) - message: dict[str, Any] = { - "type": "upsert_model_provider", - "provider": { - "id": provider.id, - "name": provider.name, - "kind": provider.kind, - "settings": provider.settings, - }, - } - if provider.secret and not (current and current.get("hasSecret")): - message["secret"] = provider.secret - app.request(message, "model_provider_upserted") - wanted = {provider.model_row_id(row["modelId"]) for row in provider.models} - for model in _models(app, provider.id): - if model["id"] not in wanted: - app.request( - {"type": "delete_provider_model", "model_id": model["id"]}, - "provider_model_deleted", - model_id=model["id"], - ) - for row in provider.models: - app.request( - { - "type": "upsert_provider_model", - "model": { - **row, - "id": provider.model_row_id(row["modelId"]), - "providerId": provider.id, - }, - }, - "provider_model_upserted", - ) - finally: - app.close() - - def unregister() -> bool: """Delete ug's providers from the running app (it also deletes their models and key). diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py index cbecfdd02..4076da54e 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -41,6 +41,7 @@ def __init__(self, home, version="1.4.2"): self.received: list[dict] = [] self.handshakes: list[dict] = [] 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]] = {} @@ -64,7 +65,7 @@ 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("upsert") and self.busy: + 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": @@ -250,74 +251,81 @@ def models(state, provider, location): ) -def test_registers_the_three_providers_with_their_models(app): - copilot_app.register(_app_providers()) - - providers = _by_name(app) - claude, gpt = providers["Databricks Claude"], providers["Databricks GPT"] - open_models = providers["Databricks Open Models"] - assert claude["settings"]["baseUrl"] == f"{WORKSPACE}/ai-gateway/anthropic" - assert gpt["settings"]["baseUrl"] == f"{WORKSPACE}/ai-gateway/mlflow/v1" - assert open_models["settings"]["wireApi"] == "completions" - # The app stores the placeholder in its own keychain slot; only the Claude provider needs one. - assert app.secrets == {claude["id"]: copilot_app.PLACEHOLDER_KEY} - rows = {m["providerId"]: m for m in app.models.values()} - assert rows[claude["id"]]["modelId"] == "claude-sonnet-4.6" - assert rows[claude["id"]]["wireModel"] == "system.ai.claude-sonnet-4-6" - assert rows[claude["id"]]["displayName"] == "Claude Sonnet 4.6" - assert rows[gpt["id"]]["modelId"] == "gpt-5-2" - assert rows[gpt["id"]]["wireModel"] == "system.ai.gpt-5-2" - assert rows[gpt["id"]]["wireApiOverride"] == "responses" - assert rows[open_models["id"]]["modelId"] == "glm-5" - assert rows[open_models["id"]]["wireModel"] == "system.ai.glm-5" - assert "wireApiOverride" not in rows[open_models["id"]] - assert len(app.models) == 3 - assert all("Origin" not in headers for headers in app.handshakes) - - -def test_registering_again_updates_in_place_without_resending_the_key(app): - copilot_app.register(_app_providers()) - claude_id = _by_name(app)["Databricks Claude"]["id"] - app.received.clear() - - copilot_app.register(_app_providers(workspace="https://other.cloud.databricks.com")) - - assert _by_name(app)["Databricks Claude"]["id"] == claude_id - assert all("secret" not in message for message in app.sent("upsert_model_provider")) - assert _by_name(app)["Databricks Claude"]["settings"]["baseUrl"] == ( - "https://other.cloud.databricks.com/ai-gateway/anthropic" - ) +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) -def test_a_catalog_change_drops_ugs_stale_providers_and_models(app): - app.providers["mine"] = {"id": "mine", "name": "Mine", "kind": "custom"} - copilot_app.register(_app_providers()) - - copilot_app.register( - _app_providers([CATALOG[1], CopilotModel("system.ai.gpt-6", WIRE_RESPONSES)]) - ) - - assert sorted(app.providers) == sorted(["mine", _by_name(app)["Databricks GPT"]["id"]]) - assert sorted(m["modelId"] for m in app.models.values()) == ["gpt-5-2", "gpt-6"] - - copilot_app.register(_app_providers(CATALOG[2:])) - - assert [m["modelId"] for m in app.models.values()] == ["glm-5"] - +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_a_row_the_user_added_to_ugs_provider_is_replaced(app): - copilot_app.register(_app_providers()) - claude = _by_name(app)["Databricks Claude"] - app.models["user-row"] = {"id": "user-row", "providerId": claude["id"], "modelId": "x"} + 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["gpt-6"].limits == (794_000, 128_000) - copilot_app.register(_app_providers()) + def test_only_chat_completions_models_turn_off_reasoning_effort(self): + claude, gpt, open_models = copilot_app.desired_providers(WORKSPACE, CATALOG).plugin - assert "user-row" not in app.models + 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"} - copilot_app.register(_app_providers()) + seed(app, _app_providers()) assert len(app.providers) == 4 assert copilot_app.unregister() is True @@ -325,6 +333,7 @@ def test_unregister_deletes_only_ugs_providers(app): 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: @@ -332,7 +341,7 @@ def test_registered_outdated_and_not_registered(self, app): providers = _app_providers() assert copilot_app.status(providers) == "not_registered" - copilot_app.register(providers) + seed(app, providers) assert copilot_app.status(providers) == "registered" assert copilot_app.status(_app_providers(CATALOG[:1])) == "outdated" @@ -342,6 +351,21 @@ def test_registered_outdated_and_not_registered(self, app): copilot_app.unregister() assert copilot_app.status(providers) == "not_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")) @@ -354,18 +378,20 @@ def test_an_app_that_quits_mid_check_is_not_running(self, app): def test_retries_while_the_app_applies_another_change(app): + seed(app, _app_providers()) app.busy = 2 + app.busy_kinds = ("delete_model_provider",) - copilot_app.register(_app_providers()) - - assert len(app.providers) == 3 + assert copilot_app.unregister() is True + assert app.providers == {} -def test_a_refusal_stops_the_registration(app): - app.fail = "upsert_provider_model" +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 upsert_provider_model"): - copilot_app.register(_app_providers()) + with pytest.raises(copilot_app.CopilotAppError, match="refused delete_model_provider"): + copilot_app.unregister() def _healthcheck(version, **fields): @@ -373,7 +399,8 @@ def _healthcheck(version, **fields): def test_every_request_carries_a_request_id(app): - copilot_app.register(_app_providers()) + seed(app, _app_providers()) + copilot_app.unregister() ids = [message.get("request_id") for message in app.received] assert all(ids) @@ -387,60 +414,48 @@ def test_every_request_carries_a_request_id(app): ) def test_a_reply_not_for_this_request_is_skipped(app, capsys, stray): app.before_reply["get_healthcheck"] = [stray] + seed(app, _app_providers()) - copilot_app.register(_app_providers()) - + assert copilot_app.unregister() is True assert "hasn't been verified" not in capsys.readouterr().out - assert len(app.providers) == 3 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()) - copilot_app.register(_app_providers()) - - assert len(app.providers) == 3 + assert copilot_app.unregister() is True def test_an_error_without_a_request_id_answers_the_pending_request(app): app.before_reply["get_healthcheck"] = [{"type": "error", "message": "Invalid message format"}] with pytest.raises(copilot_app.CopilotAppError, match="Invalid message format"): - copilot_app.register(_app_providers()) + copilot_app.unregister() -def test_an_app_that_quits_mid_registration_is_not_running(app): - app.drop = "upsert_provider_model" +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.register(_app_providers()) - - -def test_a_listed_model_without_an_id_is_left_alone(app): - copilot_app.register(_app_providers()) - claude = _by_name(app)["Databricks Claude"]["id"] - app.models["odd"] = {"providerId": claude, "modelId": "claude-odd"} - - copilot_app.register(_app_providers()) - - assert "odd" in app.models + copilot_app.unregister() -def test_an_unknown_app_version_warns_and_still_registers(app, capsys): +def test_an_unknown_app_version_warns_and_continues(app, capsys): app.version = "9.0.0" + seed(app, _app_providers()) - copilot_app.register(_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() ) - assert len(app.providers) == 3 -def test_a_verified_app_version_registers_quietly(app, capsys): - copilot_app.register(_app_providers()) +def test_a_verified_app_version_is_quiet(app, capsys): + copilot_app.unregister() assert "hasn't been verified" not in capsys.readouterr().out @@ -450,7 +465,7 @@ def test_a_closed_app_is_not_running(tmp_path, monkeypatch): monkeypatch.setenv("COPILOT_HOME", str(home)) with pytest.raises(copilot_app.CopilotAppNotRunning): - copilot_app.register(_app_providers()) + copilot_app.unregister() # The app leaves its run files behind when it exits. (home / "run").mkdir(parents=True) @@ -464,4 +479,4 @@ 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.register(_app_providers()) + copilot_app.unregister() From c712d8504f9dd02dd91449ab67aaf058529c9959 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:09:55 +0000 Subject: [PATCH 23/26] copilot: address review of the app row builder Co-authored-by: Isaac --- src/ucode/agents/vscode.py | 4 +- src/ucode/copilot_app.py | 70 ++++++++++++---- src/ucode/os_compatibility/system_cross_os.py | 28 +++++++ tests/test_copilot_app.py | 82 ++++++++++++++++++- tests/test_system_cross_os.py | 11 +++ 5 files changed, 176 insertions(+), 19 deletions(-) diff --git a/src/ucode/agents/vscode.py b/src/ucode/agents/vscode.py index 9ab942f91..d7c706f6a 100644 --- a/src/ucode/agents/vscode.py +++ b/src/ucode/agents/vscode.py @@ -203,7 +203,9 @@ def _model_entry(model: CopilotModel, name: str) -> dict[str, object]: def copilot_models( state: dict, provider: str | None = None, location: str | None = None ) -> list[CopilotModel]: - """The models Copilot CLI would offer from the same source; raises when there are none.""" + """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.""" if provider: return provider_service_catalog(state, provider, None, tool="vscode") if location: diff --git a/src/ucode/copilot_app.py b/src/ucode/copilot_app.py index 96f6cb8f1..ebb786038 100644 --- a/src/ucode/copilot_app.py +++ b/src/ucode/copilot_app.py @@ -5,20 +5,20 @@ 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 finds them -without any state. +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 os 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, @@ -30,7 +30,14 @@ model_labels, ) from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER -from ucode.copilot_app_plugin import AppProviderRows, PluginModel, PluginProvider, Wire +from ucode.copilot_app_plugin import ( + AppProviderRows, + PluginModel, + PluginProvider, + Wire, + copilot_settings_path, +) +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. @@ -45,7 +52,8 @@ class CopilotAppError(RuntimeError): - """The app refused or didn't understand a request; nothing more was changed.""" + """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): @@ -95,7 +103,12 @@ def model_row_id(self, model_id: str) -> str: 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}, + 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} @@ -124,7 +137,7 @@ def plugin_app(self) -> tuple[AppProviderRows, ...]: def copilot_home() -> Path: - return Path(os.environ.get("COPILOT_HOME") or Path.home() / ".copilot") + return copilot_settings_path().parent def _base_path(wire: Wire, provider_service: str | None) -> str: @@ -138,19 +151,28 @@ def _model_rows( ) -> 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, which is what makes the app apply Claude's request - settings, and its wire model is the gateway's id. Ids the app would see twice keep the first.""" + 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. 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.id) + 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 = model.registry_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, @@ -245,12 +267,18 @@ def __init__(self, home: Path) -> None: run = home / "run" try: - # Each file holds the value, then the app's pid; a stale one names a closed port. - port = int((run / "ws.port").read_text().split()[0]) + # 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 - url = f"ws://127.0.0.1:{port}/?token={token}&requester_id=ug&session_channels=false" + if pid is not None and not process_running(pid): + raise CopilotAppNotRunning("The GitHub Copilot app isn't running.") + query = urlencode({"token": token, "requester_id": "ug", "session_channels": "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=5) @@ -416,8 +444,13 @@ def unregister() -> bool: 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 exactly ``providers`` and their models among ug's providers. + """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.""" @@ -434,8 +467,13 @@ def status(providers: Iterable[AppProvider]) -> AppStatus: if ours != {provider.id for provider in providers}: return "outdated" for provider in providers: - listed = {model["id"] for model in _models(app, provider.id)} - if listed != {provider.model_row_id(row["modelId"]) for row in provider.models}: + 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: 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/tests/test_copilot_app.py b/tests/test_copilot_app.py index 4076da54e..3abfabf68 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -5,6 +5,7 @@ import http import json +import os import threading from urllib.parse import parse_qs, urlparse @@ -50,8 +51,8 @@ def __init__(self, home, version="1.4.2"): threading.Thread(target=self._server.serve_forever, daemon=True).start() run = home / "run" run.mkdir(parents=True) - (run / "ws.port").write_text(f"{port}\n4242\n") - (run / "ws.token").write_text(f"{self.token}\n4242\n") + (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())) @@ -232,6 +233,46 @@ def test_two_models_with_one_copilot_id_keep_the_first(self): ("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 @@ -351,6 +392,27 @@ def test_registered_outdated_and_not_registered(self, app): 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) @@ -475,6 +537,22 @@ def test_a_closed_app_is_not_running(tmp_path, monkeypatch): 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") 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) From 010d10ea701fec4bae1fbb758c48657c2e45f1b1 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:16:20 +0000 Subject: [PATCH 24/26] copilot: move the GitHub Copilot app client under agents/desktop Co-authored-by: Isaac --- src/ucode/{ => agents/desktop}/copilot_app.py | 8 ++++---- tests/test_copilot_app.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) rename src/ucode/{ => agents/desktop}/copilot_app.py (98%) diff --git a/src/ucode/copilot_app.py b/src/ucode/agents/desktop/copilot_app.py similarity index 98% rename from src/ucode/copilot_app.py rename to src/ucode/agents/desktop/copilot_app.py index ebb786038..a9aa766cf 100644 --- a/src/ucode/copilot_app.py +++ b/src/ucode/agents/desktop/copilot_app.py @@ -1,8 +1,8 @@ """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 (``ucode.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 +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 @@ -29,14 +29,14 @@ copilot_catalog_model_id, model_labels, ) -from ucode.constants import MODEL_PROVIDER_SERVICE_HEADER -from ucode.copilot_app_plugin import ( +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 diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py index 3abfabf68..095d57f58 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -12,9 +12,9 @@ import pytest from websockets.sync.server import serve -from ucode import copilot_app -from ucode import copilot_app_plugin as plugin 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" From b1c0fad6cfd3731e888d53997f00dc19ac32f202 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 09:57:29 +0000 Subject: [PATCH 25/26] copilot: keep the app's GPT and open-model row ids out of Copilot's catalog The app applies its own settings to a row whose id is a Copilot catalog model's, over the row's: a Kimi K3 row with id kimi-k3 got reasoning_effort high despite reasoningEffort false. GPT and open-model rows now use the gateway's full id, or databricks. for a bare Model Provider Service target. Claude rows keep Copilot's catalog ids, which make the app apply Claude's request settings. Co-authored-by: Isaac --- src/ucode/agents/desktop/copilot_app.py | 14 +++++++++++--- tests/test_copilot_app.py | 21 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app.py b/src/ucode/agents/desktop/copilot_app.py index a9aa766cf..80359e652 100644 --- a/src/ucode/agents/desktop/copilot_app.py +++ b/src/ucode/agents/desktop/copilot_app.py @@ -146,14 +146,22 @@ def _base_path(wire: Wire, provider_service: str | None) -> str: 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. Ids the app would see - twice keep the first, with a warning.""" + 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]] = {} @@ -167,7 +175,7 @@ def _model_rows( ) continue else: - model_id = model.registry_id + 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 " diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py index 095d57f58..524a68294 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -213,6 +213,25 @@ def test_a_model_provider_service_uses_the_openai_route_and_its_header(self): 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]) @@ -354,7 +373,7 @@ def test_the_plugin_rows_and_the_picker_rows_agree(self): else None ) gpt = {m.model_id: m for m in desired.plugin[1].models} - assert gpt["gpt-6"].limits == (794_000, 128_000) + 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 From b079e4a3529e653a6e12be4a3cab2b0241779a04 Mon Sep 17 00:00:00 2001 From: Xi Lyu Date: Fri, 20 Nov 2026 12:11:32 +0000 Subject: [PATCH 26/26] copilot: treat a GitHub Copilot app without the healthcheck as an unknown version The app version only decides whether ug warns that it hasn't been verified, so an app that refuses get_healthcheck now gets that warning instead of failing unregister and status. Co-authored-by: Isaac --- src/ucode/agents/desktop/copilot_app.py | 5 ++++- tests/test_copilot_app.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ucode/agents/desktop/copilot_app.py b/src/ucode/agents/desktop/copilot_app.py index 80359e652..d70ab85d3 100644 --- a/src/ucode/agents/desktop/copilot_app.py +++ b/src/ucode/agents/desktop/copilot_app.py @@ -415,9 +415,12 @@ def _connect() -> _AppConnection: app = _AppConnection(copilot_home()) try: version = _version(app) - except CopilotAppError: + except CopilotAppNotRunning: app.close() raise + except CopilotAppError: + # The version only decides the warning below. + version = None if version not in VERIFIED_APP_VERSIONS: print_warning( f"GitHub Copilot app {version or '(unknown version)'} hasn't been verified with ug; " diff --git a/tests/test_copilot_app.py b/tests/test_copilot_app.py index 524a68294..062edfc4c 100644 --- a/tests/test_copilot_app.py +++ b/tests/test_copilot_app.py @@ -511,12 +511,24 @@ def test_an_error_for_another_request_is_skipped(app): def test_an_error_without_a_request_id_answers_the_pending_request(app): - app.before_reply["get_healthcheck"] = [{"type": "error", "message": "Invalid message format"}] + 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"