fix: distinguish unavailable server usage checks

Only positively observed tabs, restored occurrences, Preferences context or occupied workspace inventory produce RemoteServerInUseError. Missing admission, malformed inventory and failed verification now remain closed refusals with the generic localized save error, without exposing raw transport details or permitting Forget/PATCH.

Sixteen real Servers UI regressions first reproduced the incorrect in-use message for network/401/503 metadata failures and failed inventory reads across Electron and Tauri delete/SSH-retarget actions. The 24-case matrix also preserves known-in-use presentation and masked mutation failures.

Node 24.22.3: exact CI UI batches pass 751/751, 363/363 and 1058/1058; Servers browser 22/22; Preferences suites 36/36, 3/3, 18/18, 3/3 and 4/4; async inventory 7/7; typechecks pass. No failures or skipped tests. Update the current error-classification contract without adding translations or dependencies.
This commit is contained in:
Pascal André 2026-11-14 13:11:38 +01:00
parent ca86faaf71
commit 791c87c41d
No known key found for this signature in database
3 changed files with 28 additions and 8 deletions

View file

@ -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.

View file

@ -15,10 +15,16 @@ export async function confirmRemoteServerChange(id: string): Promise<boolean> {
|| 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" })
}

View file

@ -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() }