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.
This commit is contained in:
Neil
2026-11-10 03:12:32 -08:00
parent be26e2f832
commit 233ddf8111
6 changed files with 42 additions and 46 deletions
+1 -1
View File
@@ -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
@@ -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<string, string> = { ORCA_PORT: '3000', CONDUCTOR_PORT: '3000' }
const b: Record<string, string> = { 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<string, string> = {
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()
})
})
+3 -3
View File
@@ -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))
}
@@ -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<string, string> {
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 {}
@@ -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()
})
})
-20
View File
@@ -24,23 +24,3 @@ export function buildWorkspacePortEnv(block: WorkspacePortBlock): Record<string,
export function isPortInWorkspaceBlock(port: number, block: WorkspacePortBlock): boolean {
return port >= 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
}
}