diff --git a/dev-docs/SETTINGS.md b/dev-docs/SETTINGS.md index 81753f59d..2480b8f0d 100644 --- a/dev-docs/SETTINGS.md +++ b/dev-docs/SETTINGS.md @@ -8,7 +8,7 @@ Settings rows and actions, the native Preferences window and its project context - The usage check reserves the Preferences renderer's current native remote context, including an unavailable project, before remote IO and after the inventory read. Reopening Settings from another project releases that context; native context read failures refuse the change. -- Usage refusals retain `RemoteServerInUseError` through profile save/removal consumers and resolve to `remoteRestore.inUse`. Unrecognized persistence and transport failures retain the localized `settings.preferenceSaveFailed` presentation. +- Positively observed tabs, restored occurrences, native Preferences context or occupied workspace inventory produce `RemoteServerInUseError`, retained through profile save/removal consumers as `remoteRestore.inUse`. Failed admission, native context reads and inventory checks block the mutation with the generic `settings.preferenceSaveFailed` presentation, alongside unrecognized persistence and transport failures. - Guarded profile edits apply the same usage check inside their serialized lane. Removal checks usage and confirmation before invoking its captured Forget callback, releases the same registry client after successful Forget, rechecks view authority, then persists the removal. A failed persistence keeps the saved row; fresh authentication restores the forgotten connection before another usage check. Cancelled policy edits retain the saved profile; connection admission requires the intended committed origin/TLS/SSH policy. diff --git a/packages/ui/src/stores/remote-server-usage.ts b/packages/ui/src/stores/remote-server-usage.ts index 1e76abf53..df199581f 100644 --- a/packages/ui/src/stores/remote-server-usage.ts +++ b/packages/ui/src/stores/remote-server-usage.ts @@ -15,10 +15,16 @@ export async function confirmRemoteServerChange(id: string): Promise { || loadedRestorableSession()?.tabs.some(tab => tab.kind === "workspace" && backendIdOf(tab) === id) || isPreferencesWindow() && parseInstanceKey((await getNativePreferencesRequest()).instanceId ?? "").backendId === id const refuse = () => { throw new RemoteServerInUseError(tGlobal("remoteRestore.inUse")) } + const unavailable = () => new Error(tGlobal("settings.preferenceSaveFailed")) try { if (await inUse()) refuse() const backend = await backendRegistry.ensure(id) - if (!backend || (await backend.api.fetchWorkspaces()).length || await inUse()) refuse() - } catch { refuse() } + if (!backend) throw unavailable() + const workspaces = await backend.api.fetchWorkspaces() + if (!Array.isArray(workspaces)) throw unavailable() + if (workspaces.length || await inUse()) refuse() + } catch (error) { + throw error instanceof RemoteServerInUseError ? error : unavailable() + } return showConfirmDialog(tGlobal("remoteRestore.confirmChange"), { variant: "warning" }) } diff --git a/packages/ui/tests/browser/preferences-backends.test.ts b/packages/ui/tests/browser/preferences-backends.test.ts index e27d3072e..a1a26e06e 100644 --- a/packages/ui/tests/browser/preferences-backends.test.ts +++ b/packages/ui/tests/browser/preferences-backends.test.ts @@ -60,6 +60,7 @@ async function host(page: Page, kind: "electron" | "tauri", initial: NativePrefe async function api(page: Page, selected: () => string) { const requests: { owner: string; path: string; method: string; body: any }[] = [], errors: string[] = [] let restored = false, unavailable = false, missing = false, failWrites = false, identity = "server-a" + let verificationFailure: { path: string; status: "network" | 401 | 503 } | undefined page.on("pageerror", error => errors.push(error.message)) await page.route(/\/(?:api|workspaces)\//, async route => { const request = route.request(), raw = new URL(request.url()).pathname @@ -75,6 +76,10 @@ async function api(page: Page, selected: () => string) { || method === "POST" && path === "/api/remote-servers/a/forget")) { return route.fulfill({ status: 500, json: { error: "private-transport-detail-do-not-display" } }) } + if (owner === "a" && path === verificationFailure?.path) { + return verificationFailure.status === "network" ? route.abort("connectionrefused") + : route.fulfill({ status: verificationFailure.status, json: { error: "private-verification-detail-do-not-display" } }) + } if (owner !== "local" && unavailable) return route.fulfill({ status: 503, json: {} }) const id = restored && owner !== "local" ? "restarted" : "same" const data = path.endsWith("/provider") ? [{ id: "ai", name: owner, activation: "enabled" }] @@ -96,14 +101,18 @@ async function api(page: Page, selected: () => string) { return route.fulfill({ json: value, headers: owner !== "local" && path === "/api/meta" ? { "X-CodeNomad-Backend-Authority": owner.repeat(43) } : {} }) }) return { requests, errors, restore: () => { restored = true }, unavailable: () => { unavailable = true }, - missing: () => { missing = true }, failWrites: () => { failWrites = true }, replace: () => { identity = "replacement" } } + missing: () => { missing = true }, failWrites: () => { failWrites = true }, + failVerification: (status: "network" | 401 | 503, path = "/api/meta") => { verificationFailure = { path, status } }, + replace: () => { identity = "replacement" } } } for (const kind of ["electron", "tauri"] as const) { - for (const reason of ["usage", "transport"] as const) for (const action of ["delete", "retarget"] as const) test(`${kind}: Servers ${action} shows ${reason === "usage" ? "the actionable remote-context usage refusal" : "a masked transport failure"}`, async () => { + for (const reason of ["usage", "transport", "network", "401", "503", "inventory"] as const) for (const action of ["delete", "retarget"] as const) test(`${kind}: Servers ${action} shows ${reason === "usage" ? "the actionable remote-context usage refusal" : `a masked ${reason} failure`}`, async () => { const page = await browser.newPage(), f = await api(page, () => "a") f.missing() if (reason === "transport") f.failWrites() + if (reason === "network" || reason === "401" || reason === "503") f.failVerification(reason === "network" ? reason : Number(reason) as 401 | 503) + if (reason === "inventory") f.failVerification(503, "/api/workspaces") await host(page, kind, reason === "usage" ? project("a", "servers") : { section: "servers" }) try { await page.goto(url) @@ -122,13 +131,18 @@ for (const kind of ["electron", "tauri"] as const) { assert.equal(await alert.innerText(), reason === "usage" ? "Close this server’s tabs and reopen Settings from another project before removing or changing its address." : "Could not save the preference.") - assert.equal((await page.locator("body").innerText()).includes("private-transport-detail-do-not-display"), false) + assert.equal((await page.locator("body").innerText()).includes("do-not-display"), false) const forget = (value: typeof f.requests[number]) => value.owner === "local" && value.method === "POST" && value.path === "/api/remote-servers/a/forget" const patch = (value: typeof f.requests[number]) => value.owner === "local" && value.method === "PATCH" && value.path === "/api/storage/state/ui" - if (reason === "usage") { - assert.equal(await page.getByText("Could not save the preference.", { exact: true }).count(), 0) + if (reason !== "transport") { + if (reason === "usage") assert.equal(await page.getByText("Could not save the preference.", { exact: true }).count(), 0) + else assert.equal(await page.getByText("Close this server’s tabs and reopen Settings from another project before removing or changing its address.", { exact: true }).count(), 0) assert.equal(f.requests.some(value => forget(value) || patch(value)), false) } else assert.ok(f.requests.some(action === "delete" ? forget : patch)) + if (!["usage", "transport"].includes(reason)) { + assert.ok(f.requests.some(value => value.owner === "a" && value.method === "GET" + && value.path === (reason === "inventory" ? "/api/workspaces" : "/api/meta"))) + } assert.equal(await page.locator('[data-remote-server="a"]').count(), 1) assert.deepEqual(f.errors, []) } finally { await page.close() }