Merge pull request #765 from nwparker/cloud/cloud-env-unique-name

fix(cloud): a second cloud workspace from the same repository opens (unique server name)
This commit is contained in:
Neil
2026-11-15 00:00:48 -08:00
committed by GitHub
3 changed files with 66 additions and 3 deletions
@@ -0,0 +1,26 @@
import { describe, expect, it, vi } from 'vitest'
vi.mock('@/i18n/i18n', () => ({
translate: (_key: string, fallback: string, values?: Record<string, string>) =>
fallback.replace('{{value0}}', values?.value0 ?? '')
}))
import { pickCloudServerName } from './cloud-server-name'
describe('the server name a cloud workspace is paired under', () => {
it('a second workspace from the same repository gets its W-number instead of failing', () => {
expect(pickCloudServerName('xterm.js', 715, [])).toBe('xterm.js (cloud)')
expect(pickCloudServerName('xterm.js', 715, [{ name: 'xterm.js (cloud)' }])).toBe(
'xterm.js W-715 (cloud)'
)
expect(
pickCloudServerName('xterm.js', 715, [
{ name: 'XTERM.JS (cloud)' },
{ name: 'xterm.js W-715 (cloud)' }
])
).toBe('xterm.js W-715 2 (cloud)')
expect(pickCloudServerName('xterm.js', null, [{ name: 'xterm.js (cloud)' }])).toBe(
'xterm.js 2 (cloud)'
)
})
})
@@ -0,0 +1,38 @@
import { translate } from '@/i18n/i18n'
import { formatWorkspaceNumber } from '../../../../shared/cloud-workspaces/cloud-workspaces-wire'
import { findCollidingEnvironmentName } from '../../../../shared/remote-runtime/host-catalog/runtime-environment-name-collision'
/**
* The server name this computer saves for a cloud workspace. Server names are unique, and two
* workspaces from the same repository share a title, so the second one couldn't be paired ("Couldn't
* open the workspace on the sandbox"). A taken name gets the workspace's W-number, then a counter.
*/
export function pickCloudServerName(
base: string,
number: number | null,
existing: readonly { name: string }[]
): string {
const label = (value: string): string =>
translate('auto.components.cloudWorkspaces.environmentName', '{{value0}} (cloud)', {
value0: value
}).slice(0, 120)
const free = (candidate: string): boolean => !findCollidingEnvironmentName(existing, candidate)
const first = label(base)
if (free(first)) {
return first
}
const numbered = number ? `${base} ${formatWorkspaceNumber(number)}` : base
// "W-715", then "W-715 2", "W-715 3"; without a number, "2", "3".
for (let n = number ? 1 : 2; n < 100; n++) {
const candidate = label(n === 1 ? numbered : `${numbered} ${n}`)
if (free(candidate)) {
return candidate
}
}
return label(`${numbered} ${Date.now()}`)
}
export async function uniqueCloudServerName(base: string, number: number | null): Promise<string> {
const environments = await window.api.runtimeEnvironments.list().catch(() => [])
return pickCloudServerName(base, number, environments)
}
@@ -1,6 +1,7 @@
// Opening a cloud workspace from the team list or a link. The owner already has it locally (the
// hosted runtime they created); anyone else pairs this device through a per-device E2EE grant and
// the workspace's server joins the sidebar like any remote Orca server.
import { uniqueCloudServerName } from './cloud-server-name'
import { hasLocalSidebarRow } from './cloud-local-row'
import { toast } from 'sonner'
import { translate } from '@/i18n/i18n'
@@ -242,9 +243,7 @@ async function pairAndReveal(
}
}
const added = await window.api.runtimeEnvironments.verifyAndAddFromPairingCode({
name: translate('auto.components.cloudWorkspaces.environmentName', '{{value0}} (cloud)', {
value0: name
}).slice(0, 120),
name: await uniqueCloudServerName(name, workspace.number ?? null),
pairingCode: grant.value.pairingCode
})
if (!added.ok) {