From 233ddf8111dee89a34a207b337d7d369e4493041 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 03:12:32 -0800 Subject: [PATCH] fix(ports): never pin every workspace on a host to the host's ORCA_PORT #216 let a launcher-set ORCA_PORT (the VM launcher sets 3000) override the per-workspace block, so two worktrees on one cloud VM would both get 3000. Each workspace always gets its own registry block again; a regression test covers a host env with ORCA_PORT=3000. --- docs/site/content/docs/model/orca-yaml.mdx | 2 +- src/main/ipc/pty/spawn-pane-env-ports.test.ts | 35 +++++++++++++++++++ src/main/ipc/pty/spawn-pane-env.ts | 6 ++-- .../workspace-hooks/setup-hook-env-vars.ts | 4 +-- .../workspace-port-block-registry.test.ts | 21 +---------- src/shared/workspace-port-block.ts | 20 ----------- 6 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 src/main/ipc/pty/spawn-pane-env-ports.test.ts diff --git a/docs/site/content/docs/model/orca-yaml.mdx b/docs/site/content/docs/model/orca-yaml.mdx index fac76241fc4..49bf4c2ca5a 100644 --- a/docs/site/content/docs/model/orca-yaml.mdx +++ b/docs/site/content/docs/model/orca-yaml.mdx @@ -133,7 +133,7 @@ scripts: - `autoRunAfterSetup` starts the action once the Setup terminal's script succeeds. - Shared `orca.yaml` actions go through the same approval prompt as setup scripts. Actions imported into local settings are treated as your own. -Every terminal in a worktree gets a stable block of 10 ports: `ORCA_PORT` through `ORCA_PORT+9` (`ORCA_PORT_COUNT` is `10`). Two worktrees on the same host never share a block, so the same dev server can run in each without port collisions. `CONDUCTOR_PORT` is set to the same value, so imported Conductor scripts work unchanged. A host that runs a single workspace can pin the block by starting Orca with `ORCA_PORT` (and optionally `ORCA_PORT_COUNT`) set. Ports a run opens appear in the Run menu and the **Ports** panel. +Every terminal in a worktree gets a stable block of 10 ports: `ORCA_PORT` through `ORCA_PORT+9` (`ORCA_PORT_COUNT` is `10`). Two worktrees on the same host never share a block, so the same dev server can run in each without port collisions. `CONDUCTOR_PORT` is set to the same value, so imported Conductor scripts work unchanged. Ports a run opens appear in the Run menu and the **Ports** panel. ### Terminal defaults and issue commands diff --git a/src/main/ipc/pty/spawn-pane-env-ports.test.ts b/src/main/ipc/pty/spawn-pane-env-ports.test.ts new file mode 100644 index 00000000000..16225d1e7c6 --- /dev/null +++ b/src/main/ipc/pty/spawn-pane-env-ports.test.ts @@ -0,0 +1,35 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' + +vi.mock('../../workspace-ports/workspace-port-block-registry', () => ({ + getWorkspacePortBlockForWorktreeId: async (worktreeId: string) => + worktreeId.endsWith('/a') ? { base: 21000, count: 10 } : { base: 21010, count: 10 } +})) + +const { stampWorkspacePortEnv, stampPtySpawnPaneEnv } = await import('./spawn-pane-env') + +afterEach(() => { + vi.unstubAllEnvs() +}) + +describe('workspace port env', () => { + it('gives each workspace its own block even when the host process has ORCA_PORT (VM launcher)', async () => { + vi.stubEnv('ORCA_PORT', '3000') + const a: Record = { ORCA_PORT: '3000', CONDUCTOR_PORT: '3000' } + const b: Record = { ORCA_PORT: '3000', CONDUCTOR_PORT: '3000' } + await stampWorkspacePortEnv(a, 'repo::/w/a') + await stampWorkspacePortEnv(b, 'repo::/w/b') + expect(a).toEqual({ ORCA_PORT: '21000', ORCA_PORT_COUNT: '10', CONDUCTOR_PORT: '21000' }) + expect(b).toEqual({ ORCA_PORT: '21010', ORCA_PORT_COUNT: '10', CONDUCTOR_PORT: '21010' }) + }) + + it('scrubs an inherited block from a spawn that names no pane', () => { + const env: Record = { + ORCA_PORT: '3000', + ORCA_PORT_COUNT: '10', + CONDUCTOR_PORT: '3000' + } + stampPtySpawnPaneEnv(env, { pane: null, jcodeRuntimeDir: undefined }) + expect(env.ORCA_PORT).toBeUndefined() + expect(env.CONDUCTOR_PORT).toBeUndefined() + }) +}) diff --git a/src/main/ipc/pty/spawn-pane-env.ts b/src/main/ipc/pty/spawn-pane-env.ts index 6e435d5707e..93b3896fda2 100644 --- a/src/main/ipc/pty/spawn-pane-env.ts +++ b/src/main/ipc/pty/spawn-pane-env.ts @@ -8,7 +8,6 @@ import { isTerminalLeafId, makePaneKey } from '../../../shared/workspace-layout/ import { isValidTerminalTabId } from '../../../shared/workspace-layout/terminal-tab-id' import { buildWorkspacePortEnv, - hostPinnedPortBlock, WORKSPACE_PORT_ENV_KEYS } from '../../../shared/workspace-port-block' import { getWorkspacePortBlockForWorktreeId } from '../../workspace-ports/workspace-port-block-registry' @@ -96,8 +95,9 @@ export async function stampWorkspacePortEnv( return } try { - const block = - hostPinnedPortBlock(process.env) ?? (await getWorkspacePortBlockForWorktreeId(worktreeId)) + // Always the workspace's own block: a host env's ORCA_PORT (a VM launcher sets one) would make + // every workspace on that host share it. + const block = await getWorkspacePortBlockForWorktreeId(worktreeId) if (block) { Object.assign(env, buildWorkspacePortEnv(block)) } diff --git a/src/main/workspace-hooks/setup-hook-env-vars.ts b/src/main/workspace-hooks/setup-hook-env-vars.ts index 1b6d7762ec7..94590520ad0 100644 --- a/src/main/workspace-hooks/setup-hook-env-vars.ts +++ b/src/main/workspace-hooks/setup-hook-env-vars.ts @@ -1,7 +1,7 @@ import { getRuntimePathBasename } from '../../shared/cross-platform-path' import { TERMINAL_GIT_CREDENTIAL_GUARD_POLICY_ENV } from '../../shared/shell/launch/terminal-git-credential-guard' import type { Repo } from '../../shared/repo-types' -import { buildWorkspacePortEnv, hostPinnedPortBlock } from '../../shared/workspace-port-block' +import { buildWorkspacePortEnv } from '../../shared/workspace-port-block' import { peekWorkspacePortBlock } from '../workspace-ports/workspace-port-block-registry' // Path-valued keys only, so WSL and posix-shell launches translate them; names are not paths. @@ -33,7 +33,7 @@ export function getSetupEnvVars( function getWorkspacePortEnv(worktreePath: string): Record { try { // Setup terminals get the block from their own spawn; this covers hook processes run directly. - const block = hostPinnedPortBlock(process.env) ?? peekWorkspacePortBlock(worktreePath) + const block = peekWorkspacePortBlock(worktreePath) return block ? buildWorkspacePortEnv(block) : {} } catch { return {} diff --git a/src/main/workspace-ports/workspace-port-block-registry.test.ts b/src/main/workspace-ports/workspace-port-block-registry.test.ts index c422fe296c4..973187f1601 100644 --- a/src/main/workspace-ports/workspace-port-block-registry.test.ts +++ b/src/main/workspace-ports/workspace-port-block-registry.test.ts @@ -6,8 +6,7 @@ import { WORKSPACE_PORT_BLOCK_SIZE, WORKSPACE_PORT_RANGE_END, WORKSPACE_PORT_RANGE_START, - buildWorkspacePortEnv, - hostPinnedPortBlock + buildWorkspacePortEnv } from '../../shared/workspace-port-block' import { WorkspacePortBlockRegistry } from './workspace-port-block-registry' @@ -125,21 +124,3 @@ describe('peekBlock', () => { expect(new Set(blocks.map((block) => block.base)).size).toBe(20) }) }) - -describe('hostPinnedPortBlock', () => { - it('honors a launcher-pinned ORCA_PORT on a one-workspace host', () => { - expect(hostPinnedPortBlock({ ORCA_PORT: '3000' })).toEqual({ base: 3000, count: 10 }) - expect(hostPinnedPortBlock({ ORCA_PORT: '3000', ORCA_PORT_COUNT: '5' })).toEqual({ - base: 3000, - count: 5 - }) - }) - - it('ignores a block inherited from a parent Orca pane, and bad values', () => { - expect(hostPinnedPortBlock({ ORCA_PORT: '28570', ORCA_WORKTREE_ID: 'r::/w' })).toBeNull() - expect(hostPinnedPortBlock({ ORCA_PORT: '28570', ORCA_PANE_KEY: 't:l' })).toBeNull() - expect(hostPinnedPortBlock({ ORCA_PORT: 'abc' })).toBeNull() - expect(hostPinnedPortBlock({ ORCA_PORT: '80' })).toBeNull() - expect(hostPinnedPortBlock({})).toBeNull() - }) -}) diff --git a/src/shared/workspace-port-block.ts b/src/shared/workspace-port-block.ts index 26a6a4be34e..761370ec365 100644 --- a/src/shared/workspace-port-block.ts +++ b/src/shared/workspace-port-block.ts @@ -24,23 +24,3 @@ export function buildWorkspacePortEnv(block: WorkspacePortBlock): Record= block.base && port < block.base + block.count } - -/** - * A host that runs exactly one workspace (a VM launcher, say) may pin the block by starting Orca - * with ORCA_PORT set. A copy inherited from a parent Orca pane (it also carries pane identity) - * names that pane's workspace and doesn't count. - */ -export function hostPinnedPortBlock(env: NodeJS.ProcessEnv): WorkspacePortBlock | null { - if (env.ORCA_PANE_KEY || env.ORCA_WORKTREE_ID) { - return null - } - const base = Number(env.ORCA_PORT) - if (!Number.isInteger(base) || base < 1024 || base > 65535 - WORKSPACE_PORT_BLOCK_SIZE) { - return null - } - const count = Number(env.ORCA_PORT_COUNT) - return { - base, - count: Number.isInteger(count) && count > 0 && count <= 100 ? count : WORKSPACE_PORT_BLOCK_SIZE - } -}