From ed46c5e14985d5fac9c63f14bc5473e36246b1c2 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 20:21:32 -0800 Subject: [PATCH] fix(cloud): VM hostname is the workspace's name, not the provider's per-boot id (JA-27, hostname half) Terminal prompts read ubuntu@ and changed after every wake. Each start (create and wake) now sets the hostname to the workspace's title (else its repo's name) as a hostname label, plus an /etc/hosts entry, inline before orcad starts: no image change. Verified on a dev VM: hostname ja-hello, 127.0.1.1 ja-hello, no sudo warnings. --- cloud/apps/sandbox-control/src/lifecycle.ts | 3 ++ .../src/sandbox-vm-commands.ts | 38 ++++++++++++++++++- .../sandbox-control/test/create-speed.test.ts | 15 ++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index c2e9ed5032c..7532dda648e 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -28,6 +28,7 @@ import { ORCAD_PORT, SHARE_PROXY_PORT, VmCommandError, + workspaceHostname, drainOrcad, runningOrcad, startOrcad, @@ -387,6 +388,8 @@ async function boot( options.timer.time('orcad', async () => startOrcad(deps.provider, state, { sandboxId: row.id, + // The workspace's name, else its repo's: never the provider's per-boot VM id. + hostname: workspaceHostname(row.name ?? (row.repo_url ? parseGitHubRepo(row.repo_url)?.repo : null)), repo: options.repo, // Repos hosted outside github.com get no GitHub credential, not even the placeholder. githubPlaceholder: deps.config.egressProxyUrl && parseGitHubRepo(row.repo_url ?? '') ? githubPlaceholderToken(row) : undefined, diff --git a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts index 3e54d4266b6..0cb3c5c5c82 100644 --- a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts +++ b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts @@ -49,8 +49,33 @@ export type StartInputs = { prelude?: { script: string; env?: Record } /** This session's share-proxy key (src/share-crypto.ts); the proxy keeps it in memory only. */ shareProxyKey?: string + /** + * The VM's hostname for this boot (workspaceHostname): terminal prompts read `ubuntu@` + * instead of the provider's VM id, which changes on every wake (JA-27). Set before orcad starts, so + * every shell it opens sees it. Best effort: a failure keeps the provider's name. + */ + hostname?: string } +/** A hostname label from a workspace name: lowercase letters, digits and dashes, at most 63 chars. */ +export function workspaceHostname(name: string | null | undefined): string | undefined { + const label = (name ?? '') + .toLowerCase() + .replace(/[^a-z0-9-]+/g, '-') + .replace(/-+/g, '-') + .replace(/^-+/, '') + .slice(0, 63) + .replace(/-+$/, '') + return label.length > 0 ? label : undefined +} + +// Inline per boot, no image change: hostname plus an /etc/hosts entry (sudo would warn without one). +const HOSTNAME_STEP = [ + '{ [ -n "${ORCA_HOSTNAME:-}" ] && sudo -n hostname "$ORCA_HOSTNAME" &&', + '{ grep -q "[[:space:]]$ORCA_HOSTNAME\\$" /etc/hosts || printf "127.0.1.1 %s\\n" "$ORCA_HOSTNAME" | sudo -n tee -a /etc/hosts; }; }', + '>/dev/null 2>&1 || true; unset ORCA_HOSTNAME' +].join(' ') + /** `NAME=` lines, so values with newlines or quotes survive bash intact. */ export function encodeSecretEnv(env: Record): string { return Object.entries(env) @@ -100,6 +125,10 @@ export async function startOrcad( if (inputs.shareProxyKey) { env.ORCA_SHARE_PROXY_KEY = inputs.shareProxyKey } + if (inputs.hostname) { + env.ORCA_HOSTNAME = inputs.hostname + } + const hostnameStep = inputs.hostname ? `${HOSTNAME_STEP}; ` : '' // The prelude rides in an env var (the start script's `env -i` keeps it out of orcad) so one // round trip both prepares the checkout and starts orcad. const result = await provider.runCommand( @@ -108,11 +137,16 @@ export async function startOrcad( ? { command: 'bash', // The prelude never sees the share-proxy or ready keys; only the start script gets them. - args: ['-c', `env -u ORCA_SHARE_PROXY_KEY -u ORCA_READY_KEY bash -c "$ORCA_PRELUDE" 1>&2 || true; unset ORCA_PRELUDE; exec ${START_COMMAND}`], + args: [ + '-c', + `${hostnameStep}env -u ORCA_SHARE_PROXY_KEY -u ORCA_READY_KEY bash -c "$ORCA_PRELUDE" 1>&2 || true; unset ORCA_PRELUDE; exec ${START_COMMAND}` + ], env: { ...env, ...inputs.prelude.env, ORCA_PRELUDE: inputs.prelude.script }, timeoutMs: START_TIMEOUT_MS } - : { command: START_COMMAND, env, timeoutMs: START_TIMEOUT_MS } + : hostnameStep + ? { command: 'bash', args: ['-c', `${hostnameStep}exec ${START_COMMAND}`], env, timeoutMs: START_TIMEOUT_MS } + : { command: START_COMMAND, env, timeoutMs: START_TIMEOUT_MS } ) if (result.exitCode !== 0) { throw new VmCommandError('orcad_start_failed') diff --git a/cloud/apps/sandbox-control/test/create-speed.test.ts b/cloud/apps/sandbox-control/test/create-speed.test.ts index 94e72d4d0be..fb5d963451c 100644 --- a/cloud/apps/sandbox-control/test/create-speed.test.ts +++ b/cloud/apps/sandbox-control/test/create-speed.test.ts @@ -175,3 +175,18 @@ describe('repo-backed create', () => { expect(h.vercel.boxes.size).toBe(0) }) }) + +describe('VM hostname (JA-27)', () => { + it('is the workspace name (else the repo), as a hostname label, set at every start', async () => { + const { workspaceHostname } = await import('../src/sandbox-vm-commands') + expect(workspaceHostname('Fix Login Bug!')).toBe('fix-login-bug') + expect(workspaceHostname('--ja_hello--')).toBe('ja-hello') + expect(workspaceHostname('🙂')).toBeUndefined() + expect(workspaceHostname('a'.repeat(80))).toHaveLength(63) + const h = await createHarness() + await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'w1', machineSize: 'standard', title: 'Ja Hello', repo } }) + const start = h.vercel.commands.find((c) => c.command === START_COMMAND)! + expect(start.env.ORCA_HOSTNAME).toBe('ja-hello') + expect(start.args?.[1]).toContain('sudo -n hostname "$ORCA_HOSTNAME"') + }) +})